Search in sources :

Example 1 with BLangLock

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

the class CodeGenerator method visit.

public void visit(BLangLock lockNode) {
    if (lockNode.lockVariables.isEmpty()) {
        this.genNode(lockNode.body, this.env);
        return;
    }
    Operand gotoLockEndAddr = getOperand(-1);
    Instruction instructGotoLockEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoLockEndAddr);
    Operand[] operands = getOperands(lockNode);
    ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
    int fromIP = nextIP();
    emit((InstructionCodes.LOCK), operands);
    this.genNode(lockNode.body, this.env);
    int toIP = nextIP() - 1;
    emit((InstructionCodes.UNLOCK), operands);
    emit(instructGotoLockEnd);
    ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, nextIP(), 0, -1);
    errorTable.addErrorTableEntry(errorTableEntry);
    emit((InstructionCodes.UNLOCK), operands);
    emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
    gotoLockEndAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) Instruction(org.wso2.ballerinalang.programfile.Instruction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry)

Example 2 with BLangLock

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

the class CodeGenerator method getOperands.

private Operand[] getOperands(BLangLock lockNode) {
    Operand[] operands = new Operand[(lockNode.lockVariables.size() * 2) + 1];
    int i = 0;
    operands[i++] = new Operand(lockNode.lockVariables.size());
    for (BVarSymbol varSymbol : lockNode.lockVariables) {
        int typeSigCPIndex = addUTF8CPEntry(currentPkgInfo, varSymbol.getType().getDesc());
        TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex);
        operands[i++] = getOperand(currentPkgInfo.addCPEntry(typeRefCPEntry));
        operands[i++] = varSymbol.varIndex;
    }
    return operands;
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) TypeRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 3 with BLangLock

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

the class CodeGenerator method generateFinallyInstructions.

private void generateFinallyInstructions(BLangStatement statement, NodeKind... expectedParentKinds) {
    BLangStatement current = statement;
    while (current != null && current.statementLink.parent != null) {
        BLangStatement parent = current.statementLink.parent.statement;
        for (NodeKind expected : expectedParentKinds) {
            if (expected == parent.getKind()) {
                return;
            }
        }
        if (NodeKind.TRY == parent.getKind()) {
            BLangTryCatchFinally tryCatchFinally = (BLangTryCatchFinally) parent;
            if (tryCatchFinally.finallyBody != null && current != tryCatchFinally.finallyBody) {
                genNode(tryCatchFinally.finallyBody, env);
            }
        } else if (NodeKind.LOCK == parent.getKind()) {
            BLangLock lockNode = (BLangLock) parent;
            if (!lockNode.lockVariables.isEmpty()) {
                Operand[] operands = getOperands(lockNode);
                emit((InstructionCodes.UNLOCK), operands);
            }
        }
        current = parent;
    }
}
Also used : NodeKind(org.ballerinalang.model.tree.NodeKind) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)

Example 4 with BLangLock

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

the class BLangPackageBuilder method addLockStmt.

public void addLockStmt(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangLock lockNode = (BLangLock) TreeBuilder.createLockNode();
    lockNode.pos = pos;
    lockNode.addWS(ws);
    BLangBlockStmt lockBlock = (BLangBlockStmt) this.blockNodeStack.pop();
    lockBlock.pos = pos;
    lockNode.setBody(lockBlock);
    addStmtToCurrentBlock(lockNode);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock)

Example 5 with BLangLock

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

the class Desugar method visit.

@Override
public void visit(BLangLock lockNode) {
    enclLocks.push(lockNode);
    lockNode.body = rewrite(lockNode.body, env);
    enclLocks.pop();
    lockNode.lockVariables = lockNode.lockVariables.stream().sorted((v1, v2) -> {
        String o1FullName = String.join(":", v1.pkgID.getName().getValue(), v1.name.getValue());
        String o2FullName = String.join(":", v2.pkgID.getName().getValue(), v2.name.getValue());
        return o1FullName.compareTo(o2FullName);
    }).collect(Collectors.toSet());
    result = lockNode;
}
Also used : BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)

Aggregations

BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangLock (org.wso2.ballerinalang.compiler.tree.statements.BLangLock)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 NodeKind (org.ballerinalang.model.tree.NodeKind)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)1 BLangTryCatchFinally (org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally)1 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)1 Instruction (org.wso2.ballerinalang.programfile.Instruction)1 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)1 TypeRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry)1