use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangMapLiteral mapLiteral) {
Operand mapVarRegIndex = calcAndGetExprRegIndex(mapLiteral);
Operand typeCPIndex = getTypeCPIndex(mapLiteral.type);
emit(InstructionCodes.NEWMAP, mapVarRegIndex, typeCPIndex);
// Handle Map init stuff
for (BLangRecordKeyValue keyValue : mapLiteral.keyValuePairs) {
BLangExpression keyExpr = keyValue.key.expr;
genNode(keyExpr, this.env);
BLangExpression valueExpr = keyValue.valueExpr;
genNode(valueExpr, this.env);
BMapType mapType = (BMapType) mapLiteral.type;
int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
if (opcode == InstructionCodes.NOP) {
emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, valueExpr.regIndex);
} else {
RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
emit(opcode, valueExpr.regIndex, refRegMapValue);
emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, refRegMapValue);
}
}
}
use of org.wso2.ballerinalang.programfile.Instruction.Operand 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.Operand 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.Operand in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangEnumeratorAccessExpr enumeratorAccessExpr) {
Operand typeCPIndex = getTypeCPIndex(enumeratorAccessExpr.type);
Operand varIndex = enumeratorAccessExpr.symbol.varIndex;
emit(InstructionCodes.ENUMERATORLOAD, typeCPIndex, varIndex, calcAndGetExprRegIndex(enumeratorAccessExpr));
}
use of org.wso2.ballerinalang.programfile.Instruction.Operand in project ballerina by ballerina-lang.
the class CodeGenerator method getTypeCPIndex.
/**
* Get the constant pool entry index of a given type.
*
* @param type Type to get the constant pool entry index
* @return constant pool entry index of the type
*/
private Operand getTypeCPIndex(BType type) {
int typeSigCPIndex = addUTF8CPEntry(currentPkgInfo, type.getDesc());
TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex);
return getOperand(currentPkgInfo.addCPEntry(typeRefCPEntry));
}
Aggregations