Search in sources :

Example 1 with BLangWorkerReceive

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

the class CodeAnalyzer method validateWorkerInteractions.

private void validateWorkerInteractions(WorkerActionSystem workerActionSystem) {
    BLangStatement currentAction;
    WorkerActionStateMachine currentSM;
    String currentWorkerId;
    boolean systemRunning;
    do {
        systemRunning = false;
        for (Map.Entry<String, WorkerActionStateMachine> entry : workerActionSystem.entrySet()) {
            currentWorkerId = entry.getKey();
            currentSM = entry.getValue();
            if (currentSM.done()) {
                continue;
            }
            currentAction = currentSM.currentAction();
            if (isWorkerSend(currentAction)) {
                if (isWorkerForkSend(currentAction)) {
                    currentSM.next();
                    systemRunning = true;
                } else {
                    WorkerActionStateMachine otherSM = workerActionSystem.get(this.extractWorkerId(currentAction));
                    if (otherSM.currentIsReceive(currentWorkerId)) {
                        this.validateWorkerActionParameters((BLangWorkerSend) currentAction, (BLangWorkerReceive) otherSM.currentAction());
                        otherSM.next();
                        currentSM.next();
                        systemRunning = true;
                    }
                }
            }
        }
    } while (systemRunning);
    if (!workerActionSystem.everyoneDone()) {
        this.reportInvalidWorkerInteractionDiagnostics(workerActionSystem);
    }
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BLangWorkerReceive

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

the class CodeGenerator method visit.

public void visit(BLangWorkerReceive workerReceiveStmt) {
    WorkerDataChannelInfo workerDataChannelInfo = this.getWorkerDataChannelInfo(this.currentCallableUnitInfo, workerReceiveStmt.workerIdentifier.value, this.currentWorkerInfo.getWorkerName());
    WorkerDataChannelRefCPEntry wrkrChnlRefCPEntry = new WorkerDataChannelRefCPEntry(workerDataChannelInfo.getUniqueNameCPIndex(), workerDataChannelInfo.getUniqueName());
    wrkrChnlRefCPEntry.setWorkerDataChannelInfo(workerDataChannelInfo);
    Operand wrkrRplyRefCPIndex = getOperand(currentPkgInfo.addCPEntry(wrkrChnlRefCPEntry));
    workerDataChannelInfo.setDataChannelRefIndex(wrkrRplyRefCPIndex.value);
    List<BLangExpression> lhsExprs = workerReceiveStmt.exprs;
    int nLHSExprs = lhsExprs.size();
    RegIndex[] regIndexes = new RegIndex[nLHSExprs];
    BType[] bTypes = new BType[nLHSExprs];
    for (int i = 0; i < nLHSExprs; i++) {
        BLangExpression lExpr = lhsExprs.get(i);
        if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
            lExpr.regIndex = ((BLangLocalVarRef) lExpr).symbol.varIndex;
            regIndexes[i] = lExpr.regIndex;
        } else {
            lExpr.regIndex = getRegIndex(lExpr.type.tag);
            lExpr.regIndex.isLHSIndex = true;
            regIndexes[i] = lExpr.regIndex;
        }
        bTypes[i] = lExpr.type;
    }
    UTF8CPEntry sigCPEntry = new UTF8CPEntry(this.generateSig(bTypes));
    Operand sigCPIndex = getOperand(currentPkgInfo.addCPEntry(sigCPEntry));
    // WRKRECEIVE wrkrRplyRefCPIndex typesCPIndex nRegIndexes, regIndexes[nRegIndexes]
    Operand[] wrkReceiveArgRegs = new Operand[nLHSExprs + 3];
    wrkReceiveArgRegs[0] = wrkrRplyRefCPIndex;
    wrkReceiveArgRegs[1] = sigCPIndex;
    wrkReceiveArgRegs[2] = getOperand(nLHSExprs);
    System.arraycopy(regIndexes, 0, wrkReceiveArgRegs, 3, regIndexes.length);
    emit(InstructionCodes.WRKRECEIVE, wrkReceiveArgRegs);
    for (BLangExpression lExpr : lhsExprs) {
        if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
            continue;
        }
        this.varAssignment = true;
        this.genNode(lExpr, this.env);
        this.varAssignment = false;
    }
}
Also used : BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) WorkerDataChannelRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) WorkerDataChannelInfo(org.wso2.ballerinalang.programfile.WorkerDataChannelInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 3 with BLangWorkerReceive

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

the class BLangPackageBuilder method addWorkerReceiveStmt.

public void addWorkerReceiveStmt(DiagnosticPos pos, Set<Whitespace> ws, String workerName) {
    BLangWorkerReceive workerReceiveNode = (BLangWorkerReceive) TreeBuilder.createWorkerReceiveNode();
    workerReceiveNode.setWorkerName(this.createIdentifier(workerName));
    exprNodeListStack.pop().forEach(expr -> workerReceiveNode.exprs.add((BLangExpression) expr));
    workerReceiveNode.addWS(commaWsStack.pop());
    workerReceiveNode.pos = pos;
    workerReceiveNode.addWS(ws);
    addStmtToCurrentBlock(workerReceiveNode);
}
Also used : BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangLocalVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef)1 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)1 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)1 BLangWorkerReceive (org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1 WorkerDataChannelInfo (org.wso2.ballerinalang.programfile.WorkerDataChannelInfo)1 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)1 WorkerDataChannelRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry)1