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);
}
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);
}
}
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);
}
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();
}
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);
}
Aggregations