use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.
the class CodeGenerator method getRegIndexInternal.
private RegIndex getRegIndexInternal(int typeTag, VariableIndex.Kind varIndexKind) {
int index;
switch(varIndexKind) {
case REG:
return new RegIndex(getNextIndex(typeTag, regIndexes), typeTag);
case PACKAGE:
index = getNextIndex(typeTag, pvIndexes);
break;
case FIELD:
index = getNextIndex(typeTag, fieldIndexes);
break;
default:
index = getNextIndex(typeTag, lvIndexes);
break;
}
RegIndex regIndex = new RegIndex(index, typeTag);
regIndex.isVarIndex = true;
return regIndex;
}
use of org.wso2.ballerinalang.programfile.Instruction.RegIndex 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.RegIndex 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();
}
use of org.wso2.ballerinalang.programfile.Instruction.RegIndex 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.RegIndex in project ballerina by ballerina-lang.
the class CodeGenerator method getRegIndex.
private RegIndex getRegIndex(int typeTag) {
RegIndex regIndex = getRegIndexInternal(typeTag, REG);
addToRegIndexList(regIndex);
return regIndex;
}
Aggregations