Search in sources :

Example 6 with Instruction

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

the class CodeGenerator method visit.

public void visit(BLangForeach foreach) {
    // Calculate temporary scope variables for iteration.
    Operand iteratorVar = getLVIndex(TypeTags.ITERATOR);
    Operand conditionVar = getLVIndex(TypeTags.BOOLEAN);
    // Create new Iterator for given collection.
    this.genNode(foreach.collection, env);
    this.emit(InstructionCodes.ITR_NEW, foreach.collection.regIndex, iteratorVar);
    Operand foreachStartAddress = new Operand(nextIP());
    Operand foreachEndAddress = new Operand(-1);
    Instruction gotoStartInstruction = InstructionFactory.get(InstructionCodes.GOTO, foreachStartAddress);
    Instruction gotoEndInstruction = InstructionFactory.get(InstructionCodes.GOTO, foreachEndAddress);
    // Checks given iterator has a next value.
    this.emit(InstructionCodes.ITR_HAS_NEXT, iteratorVar, conditionVar);
    this.emit(InstructionCodes.BR_FALSE, conditionVar, foreachEndAddress);
    // assign variables.
    generateForeachVarAssignment(foreach, iteratorVar);
    this.loopResetInstructionStack.push(gotoStartInstruction);
    this.loopExitInstructionStack.push(gotoEndInstruction);
    // generate foreach body.
    this.genNode(foreach.body, env);
    this.loopResetInstructionStack.pop();
    this.loopExitInstructionStack.pop();
    // move to next iteration.
    this.emit(gotoStartInstruction);
    foreachEndAddress.value = this.nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) Instruction(org.wso2.ballerinalang.programfile.Instruction)

Example 7 with Instruction

use of org.wso2.ballerinalang.programfile.Instruction 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 8 with Instruction

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

the class CodeGenerator method visit.

public void visit(BLangTypeInit cIExpr) {
    BSymbol structSymbol = cIExpr.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(cIExpr);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct initializer here.
    Operand[] operands = getFuncOperands(cIExpr.objectInitInvocation);
    Operand[] callOperands = new Operand[operands.length + 1];
    callOperands[0] = operands[0];
    callOperands[1] = operands[1];
    callOperands[2] = getOperand(operands[2].value + 1);
    callOperands[3] = structRegIndex;
    System.arraycopy(operands, 3, callOperands, 4, operands.length - 3);
    emit(InstructionCodes.CALL, callOperands);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 9 with Instruction

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

the class CodeGenerator method visit.

@Override
public void visit(BLangArrayLiteral arrayLiteral) {
    BType etype;
    if (arrayLiteral.type.tag == TypeTags.ANY) {
        etype = arrayLiteral.type;
    } else {
        etype = ((BArrayType) arrayLiteral.type).eType;
    }
    // Emit create array instruction
    int opcode = getOpcode(etype.tag, InstructionCodes.INEWARRAY);
    Operand arrayVarRegIndex = calcAndGetExprRegIndex(arrayLiteral);
    Operand typeCPIndex = getTypeCPIndex(arrayLiteral.type);
    emit(opcode, arrayVarRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < arrayLiteral.exprs.size(); i++) {
        BLangExpression argExpr = arrayLiteral.exprs.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = arrayLiteral.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        opcode = getOpcode(argExpr.type.tag, InstructionCodes.IASTORE);
        emit(opcode, arrayVarRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 10 with Instruction

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

the class CodeGenerator method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Emit create array instruction
    RegIndex exprRegIndex = calcAndGetExprRegIndex(bracedOrTupleExpr);
    Operand typeCPIndex = getTypeCPIndex(bracedOrTupleExpr.type);
    emit(InstructionCodes.RNEWARRAY, exprRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
        BLangExpression argExpr = bracedOrTupleExpr.expressions.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = argExpr.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        emit(InstructionCodes.RASTORE, exprRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)11 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 Instruction (org.wso2.ballerinalang.programfile.Instruction)5 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)4 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)3 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)3 StructureRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangRecordKey (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey)1 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)1