Search in sources :

Example 41 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangMapAccessExpr mapKeyAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(mapKeyAccessExpr.expr, this.env);
    Operand varRefRegIndex = mapKeyAccessExpr.expr.regIndex;
    genNode(mapKeyAccessExpr.indexExpr, this.env);
    Operand keyRegIndex = mapKeyAccessExpr.indexExpr.regIndex;
    BMapType mapType = (BMapType) mapKeyAccessExpr.expr.type;
    if (variableStore) {
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, mapKeyAccessExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, mapKeyAccessExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, refRegMapValue);
        }
    } else {
        int opcode = getRefToValueTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, calcAndGetExprRegIndex(mapKeyAccessExpr));
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, refRegMapValue);
            emit(opcode, refRegMapValue, calcAndGetExprRegIndex(mapKeyAccessExpr));
        }
    }
    this.varAssignment = variableStore;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 42 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangWhile whileNode) {
    Instruction gotoTopJumpInstr = InstructionFactory.get(InstructionCodes.GOTO, getOperand(this.nextIP()));
    this.genNode(whileNode.expr, this.env);
    Operand exitLoopJumpAddr = getOperand(-1);
    Instruction exitLoopJumpInstr = InstructionFactory.get(InstructionCodes.GOTO, exitLoopJumpAddr);
    emit(InstructionCodes.BR_FALSE, whileNode.expr.regIndex, exitLoopJumpAddr);
    this.loopResetInstructionStack.push(gotoTopJumpInstr);
    this.loopExitInstructionStack.push(exitLoopJumpInstr);
    this.genNode(whileNode.body, this.env);
    this.loopResetInstructionStack.pop();
    this.loopExitInstructionStack.pop();
    this.emit(gotoTopJumpInstr);
    exitLoopJumpAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) Instruction(org.wso2.ballerinalang.programfile.Instruction)

Example 43 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.

the class CodeGenerator method generateNamedArgs.

private int generateNamedArgs(BLangInvocation iExpr, Operand[] operands, int currentIndex) {
    if (iExpr.namedArgs.isEmpty()) {
        return currentIndex;
    }
    PackageInfo pkgInfo = programFile.packageInfoMap.get(iExpr.symbol.pkgID.bvmAlias());
    CallableUnitInfo callableUnitInfo;
    if (iExpr.symbol.kind == SymbolKind.FUNCTION) {
        callableUnitInfo = pkgInfo.functionInfoMap.get(iExpr.symbol.name.value);
    } else if (iExpr.symbol.kind == SymbolKind.ACTION) {
        ConnectorInfo connectorInfo = pkgInfo.connectorInfoMap.get(iExpr.symbol.owner.name.value);
        callableUnitInfo = connectorInfo.actionInfoMap.get(iExpr.symbol.name.value);
    } else {
        throw new IllegalStateException("Unsupported callable unit");
    }
    ParamDefaultValueAttributeInfo defaultValAttrInfo = (ParamDefaultValueAttributeInfo) callableUnitInfo.getAttributeInfo(AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE);
    for (int i = 0; i < iExpr.namedArgs.size(); i++) {
        BLangExpression argExpr = iExpr.namedArgs.get(i);
        // at this point. If so, get the default value for that parameter from the function info.
        if (argExpr == null) {
            DefaultValue defaultVal = defaultValAttrInfo.getDefaultValueInfo()[i];
            argExpr = getDefaultValExpr(defaultVal);
        }
        operands[currentIndex++] = genNode(argExpr, this.env).regIndex;
    }
    return currentIndex;
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) ConnectorInfo(org.wso2.ballerinalang.programfile.ConnectorInfo) ImportPackageInfo(org.wso2.ballerinalang.programfile.ImportPackageInfo) PackageInfo(org.wso2.ballerinalang.programfile.PackageInfo) CallableUnitInfo(org.wso2.ballerinalang.programfile.CallableUnitInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 44 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.

the class CodeGenerator method visitAndExpression.

private void visitAndExpression(BLangBinaryExpr binaryExpr) {
    // Code address to jump if at least one of the expressions get evaluated to false.
    // short-circuit evaluation
    Operand falseJumpAddr = getOperand(-1);
    // Generate code for the left hand side
    genNode(binaryExpr.lhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.lhsExpr.regIndex, falseJumpAddr);
    // Generate code for the right hand side
    genNode(binaryExpr.rhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.rhsExpr.regIndex, falseJumpAddr);
    // If both l and r conditions are true, then load 'true'
    calcAndGetExprRegIndex(binaryExpr);
    emit(InstructionCodes.BCONST_1, binaryExpr.regIndex);
    Operand gotoAddr = getOperand(-1);
    emit(InstructionCodes.GOTO, gotoAddr);
    falseJumpAddr.value = nextIP();
    // Load 'false' if the both conditions are false;
    emit(InstructionCodes.BCONST_0, binaryExpr.regIndex);
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

Example 45 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

// Expressions
@Override
public void visit(BLangLiteral literalExpr) {
    int opcode;
    Operand regIndex = calcAndGetExprRegIndex(literalExpr);
    int typeTag = literalExpr.type.tag;
    switch(typeTag) {
        case TypeTags.INT:
            long longVal = (Long) literalExpr.value;
            if (longVal >= 0 && longVal <= 5) {
                opcode = InstructionCodes.ICONST_0 + (int) longVal;
                emit(opcode, regIndex);
            } else {
                int intCPEntryIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(longVal));
                emit(InstructionCodes.ICONST, getOperand(intCPEntryIndex), regIndex);
            }
            break;
        case TypeTags.FLOAT:
            double doubleVal = (Double) literalExpr.value;
            if (doubleVal == 0 || doubleVal == 1 || doubleVal == 2 || doubleVal == 3 || doubleVal == 4 || doubleVal == 5) {
                opcode = InstructionCodes.FCONST_0 + (int) doubleVal;
                emit(opcode, regIndex);
            } else {
                int floatCPEntryIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(doubleVal));
                emit(InstructionCodes.FCONST, getOperand(floatCPEntryIndex), regIndex);
            }
            break;
        case TypeTags.STRING:
            String strValue = (String) literalExpr.value;
            StringCPEntry stringCPEntry = new StringCPEntry(addUTF8CPEntry(currentPkgInfo, strValue), strValue);
            int strCPIndex = currentPkgInfo.addCPEntry(stringCPEntry);
            emit(InstructionCodes.SCONST, getOperand(strCPIndex), regIndex);
            break;
        case TypeTags.BOOLEAN:
            boolean booleanVal = (Boolean) literalExpr.value;
            if (!booleanVal) {
                opcode = InstructionCodes.BCONST_0;
            } else {
                opcode = InstructionCodes.BCONST_1;
            }
            emit(opcode, regIndex);
            break;
        case TypeTags.NULL:
            emit(InstructionCodes.RCONST_NULL, regIndex);
    }
}
Also used : StringCPEntry(org.wso2.ballerinalang.programfile.cpentries.StringCPEntry) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)42 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)28 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 Instruction (org.wso2.ballerinalang.programfile.Instruction)6 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)5 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)4 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)4 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)4 StructureRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)4 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)3 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)3 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)3 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)3 WorkerDataChannelInfo (org.wso2.ballerinalang.programfile.WorkerDataChannelInfo)3 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)3 StringCPEntry (org.wso2.ballerinalang.programfile.cpentries.StringCPEntry)3 TypeRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry)3 WorkerDataChannelRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry)3