Search in sources :

Example 1 with BLangForkJoin

use of org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

@Override
public void visit(BLangForkJoin forkJoin) {
    SymbolEnv forkJoinEnv = SymbolEnv.createFolkJoinEnv(forkJoin, this.env);
    forkJoin.workers.forEach(e -> this.symbolEnter.defineNode(e, forkJoinEnv));
    forkJoin.workers.forEach(e -> this.analyzeDef(e, forkJoinEnv));
    if (!this.isJoinResultType(forkJoin.joinResultVar)) {
        this.dlog.error(forkJoin.joinResultVar.pos, DiagnosticCode.INVALID_WORKER_JOIN_RESULT_TYPE);
    }
    /* create code black and environment for join result section, i.e. (map results) */
    BLangBlockStmt joinResultsBlock = this.generateCodeBlock(this.createVarDef(forkJoin.joinResultVar));
    SymbolEnv joinResultsEnv = SymbolEnv.createBlockEnv(joinResultsBlock, this.env);
    this.analyzeNode(joinResultsBlock, joinResultsEnv);
    /* create an environment for the join body, making the enclosing environment the earlier
         * join result's environment */
    SymbolEnv joinBodyEnv = SymbolEnv.createBlockEnv(forkJoin.joinedBody, joinResultsEnv);
    this.analyzeNode(forkJoin.joinedBody, joinBodyEnv);
    if (forkJoin.timeoutExpression != null) {
        if (!this.isJoinResultType(forkJoin.timeoutVariable)) {
            this.dlog.error(forkJoin.timeoutVariable.pos, DiagnosticCode.INVALID_WORKER_TIMEOUT_RESULT_TYPE);
        }
        /* create code black and environment for timeout section */
        BLangBlockStmt timeoutVarBlock = this.generateCodeBlock(this.createVarDef(forkJoin.timeoutVariable));
        SymbolEnv timeoutVarEnv = SymbolEnv.createBlockEnv(timeoutVarBlock, this.env);
        this.typeChecker.checkExpr(forkJoin.timeoutExpression, timeoutVarEnv, Arrays.asList(symTable.intType));
        this.analyzeNode(timeoutVarBlock, timeoutVarEnv);
        /* create an environment for the timeout body, making the enclosing environment the earlier
             * timeout var's environment */
        SymbolEnv timeoutBodyEnv = SymbolEnv.createBlockEnv(forkJoin.timeoutBody, timeoutVarEnv);
        this.analyzeNode(forkJoin.timeoutBody, timeoutBodyEnv);
    }
    this.validateJoinWorkerList(forkJoin, forkJoinEnv);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 2 with BLangForkJoin

use of org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin in project ballerina by ballerina-lang.

the class CodeGenerator method processTimeoutBlock.

/* generate code for timeout block */
private void processTimeoutBlock(BLangForkJoin forkJoin, SymbolEnv forkJoinEnv, RegIndex timeoutVarRegIndex, Operand timeoutBlockAddr) {
    /* emit a GOTO instruction to jump out of the timeout block */
    Operand gotoAddr = getOperand(-1);
    this.emit(InstructionCodes.GOTO, gotoAddr);
    timeoutBlockAddr.value = nextIP();
    if (forkJoin.timeoutVariable != null) {
        visitForkJoinParameterDefs(forkJoin.timeoutVariable, forkJoinEnv);
        timeoutVarRegIndex.value = forkJoin.timeoutVariable.symbol.varIndex.value;
    }
    if (forkJoin.timeoutBody != null) {
        this.genNode(forkJoin.timeoutBody, forkJoinEnv);
    }
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

Example 3 with BLangForkJoin

use of org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangForkJoin forkJoin) {
    SymbolEnv forkJoinEnv = SymbolEnv.createForkJoinSymbolEnv(forkJoin, this.env);
    ForkjoinInfo forkjoinInfo = new ForkjoinInfo(this.lvIndexes.toArray());
    this.populateForkJoinWorkerInfo(forkJoin, forkjoinInfo);
    int forkJoinInfoIndex = this.forkJoinCount++;
    /* was I already inside a fork/join */
    if (this.env.forkJoin != null) {
        this.currentWorkerInfo.addForkJoinInfo(forkjoinInfo);
    } else {
        this.currentCallableUnitInfo.defaultWorkerInfo.addForkJoinInfo(forkjoinInfo);
    }
    ForkJoinCPEntry forkJoinCPEntry = new ForkJoinCPEntry(forkJoinInfoIndex);
    Operand forkJoinCPIndex = getOperand(this.currentPkgInfo.addCPEntry(forkJoinCPEntry));
    forkjoinInfo.setIndexCPIndex(forkJoinCPIndex.value);
    RegIndex timeoutRegIndex = new RegIndex(-1, TypeTags.INT);
    addToRegIndexList(timeoutRegIndex);
    if (forkJoin.timeoutExpression != null) {
        forkjoinInfo.setTimeoutAvailable(true);
        this.genNode(forkJoin.timeoutExpression, forkJoinEnv);
        timeoutRegIndex.value = forkJoin.timeoutExpression.regIndex.value;
    }
    // FORKJOIN forkJoinCPIndex timeoutRegIndex joinVarRegIndex joinBlockAddr timeoutVarRegIndex timeoutBlockAddr
    RegIndex joinVarRegIndex = new RegIndex(-1, TypeTags.MAP);
    Operand joinBlockAddr = getOperand(-1);
    RegIndex timeoutVarRegIndex = new RegIndex(-1, TypeTags.MAP);
    Operand timeoutBlockAddr = getOperand(-1);
    this.emit(InstructionCodes.FORKJOIN, forkJoinCPIndex, timeoutRegIndex, joinVarRegIndex, joinBlockAddr, timeoutVarRegIndex, timeoutBlockAddr);
    this.processJoinWorkers(forkJoin, forkjoinInfo, forkJoinEnv);
    int i = 0;
    int[] joinWrkrNameCPIndexes = new int[forkJoin.joinedWorkers.size()];
    String[] joinWrkrNames = new String[joinWrkrNameCPIndexes.length];
    for (BLangIdentifier workerName : forkJoin.joinedWorkers) {
        UTF8CPEntry workerNameCPEntry = new UTF8CPEntry(workerName.value);
        int workerNameCPIndex = this.currentPkgInfo.addCPEntry(workerNameCPEntry);
        joinWrkrNameCPIndexes[i] = workerNameCPIndex;
        joinWrkrNames[i] = workerName.value;
        i++;
    }
    forkjoinInfo.setJoinWrkrNameIndexes(joinWrkrNameCPIndexes);
    forkjoinInfo.setJoinWorkerNames(joinWrkrNames);
    forkjoinInfo.setWorkerCount(forkJoin.joinedWorkerCount);
    this.processJoinBlock(forkJoin, forkjoinInfo, forkJoinEnv, joinVarRegIndex, joinBlockAddr);
    this.processTimeoutBlock(forkJoin, forkJoinEnv, timeoutVarRegIndex, timeoutBlockAddr);
}
Also used : UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) ForkjoinInfo(org.wso2.ballerinalang.programfile.ForkjoinInfo) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ForkJoinCPEntry(org.wso2.ballerinalang.programfile.cpentries.ForkJoinCPEntry) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 4 with BLangForkJoin

use of org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin in project ballerina by ballerina-lang.

the class CodeGenerator method processJoinBlock.

/* generate code for Join block */
private void processJoinBlock(BLangForkJoin forkJoin, ForkjoinInfo forkjoinInfo, SymbolEnv forkJoinEnv, RegIndex joinVarRegIndex, Operand joinBlockAddr) {
    UTF8CPEntry joinType = new UTF8CPEntry(forkJoin.joinType.name());
    int joinTypeCPIndex = this.currentPkgInfo.addCPEntry(joinType);
    forkjoinInfo.setJoinType(forkJoin.joinType.name());
    forkjoinInfo.setJoinTypeCPIndex(joinTypeCPIndex);
    joinBlockAddr.value = nextIP();
    if (forkJoin.joinResultVar != null) {
        visitForkJoinParameterDefs(forkJoin.joinResultVar, forkJoinEnv);
        joinVarRegIndex.value = forkJoin.joinResultVar.symbol.varIndex.value;
    }
    if (forkJoin.joinedBody != null) {
        this.genNode(forkJoin.joinedBody, forkJoinEnv);
    }
}
Also used : UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 5 with BLangForkJoin

use of org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addForkJoinStmt.

public void addForkJoinStmt(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangForkJoin forkJoin = (BLangForkJoin) this.forkJoinNodesStack.pop();
    forkJoin.pos = pos;
    forkJoin.addWS(ws);
    this.addStmtToCurrentBlock(forkJoin);
}
Also used : BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin)

Aggregations

BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 BLangForkJoin (org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin)4 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)4 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)3 BLangWorker (org.wso2.ballerinalang.compiler.tree.BLangWorker)3 Whitespace (org.ballerinalang.model.Whitespace)2 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)2 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 WorkerInfo (org.wso2.ballerinalang.programfile.WorkerInfo)2 InvokableNode (org.ballerinalang.model.tree.InvokableNode)1 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)1 ForkjoinInfo (org.wso2.ballerinalang.programfile.ForkjoinInfo)1 ForkJoinCPEntry (org.wso2.ballerinalang.programfile.cpentries.ForkJoinCPEntry)1