Search in sources :

Example 31 with Operand

use of org.wso2.ballerinalang.programfile.Instruction.Operand 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 32 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(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 33 with Operand

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

the class CodeGenerator method visit.

public void visit(BLangWorkerSend workerSendStmt) {
    WorkerDataChannelInfo workerDataChannelInfo = this.getWorkerDataChannelInfo(this.currentCallableUnitInfo, this.currentWorkerInfo.getWorkerName(), workerSendStmt.workerIdentifier.value);
    WorkerDataChannelRefCPEntry wrkrInvRefCPEntry = new WorkerDataChannelRefCPEntry(workerDataChannelInfo.getUniqueNameCPIndex(), workerDataChannelInfo.getUniqueName());
    wrkrInvRefCPEntry.setWorkerDataChannelInfo(workerDataChannelInfo);
    Operand wrkrInvRefCPIndex = getOperand(currentPkgInfo.addCPEntry(wrkrInvRefCPEntry));
    if (workerSendStmt.isForkJoinSend) {
        this.currentWorkerInfo.setWrkrDtChnlRefCPIndex(wrkrInvRefCPIndex.value);
        this.currentWorkerInfo.setWorkerDataChannelInfoForForkJoin(workerDataChannelInfo);
    }
    workerDataChannelInfo.setDataChannelRefIndex(wrkrInvRefCPIndex.value);
    int nArgExprs = workerSendStmt.exprs.size();
    RegIndex[] argRegs = new RegIndex[nArgExprs];
    BType[] bTypes = new BType[nArgExprs];
    for (int i = 0; i < nArgExprs; i++) {
        BLangExpression argExpr = workerSendStmt.exprs.get(i);
        genNode(argExpr, this.env);
        argRegs[i] = argExpr.regIndex;
        bTypes[i] = argExpr.type;
    }
    UTF8CPEntry sigCPEntry = new UTF8CPEntry(this.generateSig(bTypes));
    Operand sigCPIndex = getOperand(this.currentPkgInfo.addCPEntry(sigCPEntry));
    // WRKSEND wrkrInvRefCPIndex typesCPIndex nRegIndexes, regIndexes[nRegIndexes]
    Operand[] wrkSendArgRegs = new Operand[nArgExprs + 3];
    wrkSendArgRegs[0] = wrkrInvRefCPIndex;
    wrkSendArgRegs[1] = sigCPIndex;
    wrkSendArgRegs[2] = getOperand(nArgExprs);
    System.arraycopy(argRegs, 0, wrkSendArgRegs, 3, argRegs.length);
    this.emit(InstructionCodes.WRKSEND, wrkSendArgRegs);
}
Also used : UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) WorkerDataChannelRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry) WorkerDataChannelInfo(org.wso2.ballerinalang.programfile.WorkerDataChannelInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 34 with Operand

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

the class CodeGenerator method visitOrExpression.

private void visitOrExpression(BLangBinaryExpr binaryExpr) {
    // short-circuit evaluation
    // Code address to jump if the lhs expression gets evaluated to 'true'.
    Operand lExprTrueJumpAddr = getOperand(-1);
    // Code address to jump if the rhs expression gets evaluated to 'false'.
    Operand rExprFalseJumpAddr = getOperand(-1);
    // Generate code for the left hand side
    genNode(binaryExpr.lhsExpr, this.env);
    emit(InstructionCodes.BR_TRUE, binaryExpr.lhsExpr.regIndex, lExprTrueJumpAddr);
    // Generate code for the right hand side
    genNode(binaryExpr.rhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.rhsExpr.regIndex, rExprFalseJumpAddr);
    lExprTrueJumpAddr.value = nextIP();
    RegIndex exprRegIndex = calcAndGetExprRegIndex(binaryExpr);
    emit(InstructionCodes.BCONST_1, exprRegIndex);
    Operand gotoAddr = getOperand(-1);
    emit(InstructionCodes.GOTO, gotoAddr);
    rExprFalseJumpAddr.value = nextIP();
    // Load 'false' if the both conditions are false;
    emit(InstructionCodes.BCONST_0, exprRegIndex);
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 35 with Operand

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

the class CodeGenerator method visit.

public void visit(BLangAwaitExpr awaitExpr) {
    Operand valueRegIndex;
    if (awaitExpr.type != null) {
        valueRegIndex = calcAndGetExprRegIndex(awaitExpr);
    } else {
        valueRegIndex = this.getOperand(-1);
    }
    genNode(awaitExpr.expr, this.env);
    Operand futureRegIndex = awaitExpr.expr.regIndex;
    this.emit(InstructionCodes.AWAIT, futureRegIndex, valueRegIndex);
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

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