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