Search in sources :

Example 26 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(BLangMapLiteral mapLiteral) {
    Operand mapVarRegIndex = calcAndGetExprRegIndex(mapLiteral);
    Operand typeCPIndex = getTypeCPIndex(mapLiteral.type);
    emit(InstructionCodes.NEWMAP, mapVarRegIndex, typeCPIndex);
    // Handle Map init stuff
    for (BLangRecordKeyValue keyValue : mapLiteral.keyValuePairs) {
        BLangExpression keyExpr = keyValue.key.expr;
        genNode(keyExpr, this.env);
        BLangExpression valueExpr = keyValue.valueExpr;
        genNode(valueExpr, this.env);
        BMapType mapType = (BMapType) mapLiteral.type;
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, valueExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, valueExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, refRegMapValue);
        }
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 27 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(BLangStructLiteral structLiteral) {
    BStructSymbol structSymbol = (BStructSymbol) structLiteral.type.tsymbol;
    int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
    Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
    // Emit an instruction to create a new struct.
    RegIndex structRegIndex = calcAndGetExprRegIndex(structLiteral);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct default values init function here.
    if (structSymbol.defaultsValuesInitFunc != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structSymbol.defaultsValuesInitFunc.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Invoke the struct initializer here.
    if (structLiteral.initializer != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structLiteral.initializer.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Generate code the struct literal.
    for (BLangRecordKeyValue keyValue : structLiteral.keyValuePairs) {
        BLangRecordKey key = keyValue.key;
        Operand fieldIndex = key.fieldSymbol.varIndex;
        genNode(keyValue.valueExpr, this.env);
        int opcode = getOpcode(key.fieldSymbol.type.tag, InstructionCodes.IFIELDSTORE);
        emit(opcode, structRegIndex, fieldIndex, keyValue.valueExpr.regIndex);
    }
}
Also used : BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 28 with Operand

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

the class CodeGenerator method visit.

public void visit(BLangTernaryExpr ternaryExpr) {
    // Determine the reg index of the ternary expression and this reg index will be used by both then and else
    // expressions to store their result
    RegIndex ternaryExprRegIndex = calcAndGetExprRegIndex(ternaryExpr);
    // Generate code for the condition
    this.genNode(ternaryExpr.expr, this.env);
    Operand ifFalseJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.BR_FALSE, ternaryExpr.expr.regIndex, ifFalseJumpAddr);
    // Generate code for the then expression
    ternaryExpr.thenExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.thenExpr, this.env);
    Operand endJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.GOTO, endJumpAddr);
    ifFalseJumpAddr.value = nextIP();
    // Generate code for the then expression
    ternaryExpr.elseExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.elseExpr, this.env);
    endJumpAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 29 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(BLangEnumeratorAccessExpr enumeratorAccessExpr) {
    Operand typeCPIndex = getTypeCPIndex(enumeratorAccessExpr.type);
    Operand varIndex = enumeratorAccessExpr.symbol.varIndex;
    emit(InstructionCodes.ENUMERATORLOAD, typeCPIndex, varIndex, calcAndGetExprRegIndex(enumeratorAccessExpr));
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

Example 30 with Operand

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

the class CodeGenerator method getTypeCPIndex.

/**
 * Get the constant pool entry index of a given type.
 *
 * @param type Type to get the constant pool entry index
 * @return constant pool entry index of the type
 */
private Operand getTypeCPIndex(BType type) {
    int typeSigCPIndex = addUTF8CPEntry(currentPkgInfo, type.getDesc());
    TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex);
    return getOperand(currentPkgInfo.addCPEntry(typeRefCPEntry));
}
Also used : TypeRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry) 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