use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.
the class PackageInfoWriter method writeCP.
public static void writeCP(DataOutputStream dataOutStream, ConstantPoolEntry[] constPool) throws IOException {
dataOutStream.writeInt(constPool.length);
for (ConstantPoolEntry cpEntry : constPool) {
// Emitting the kind of the constant pool entry.
dataOutStream.writeByte(cpEntry.getEntryType().getValue());
int nameCPIndex;
switch(cpEntry.getEntryType()) {
case CP_ENTRY_UTF8:
String stringVal = ((UTF8CPEntry) cpEntry).getValue();
if (stringVal != null) {
byte[] bytes = toUTF(stringVal);
dataOutStream.writeShort(bytes.length);
dataOutStream.write(bytes);
} else {
// If the string value is null, we write the size as -1.
// This marks that the value followed by -1 size is a null value.
dataOutStream.writeShort(NULL_VALUE_FIELD_SIZE_TAG);
}
break;
case CP_ENTRY_INTEGER:
long longVal = ((IntegerCPEntry) cpEntry).getValue();
dataOutStream.writeLong(longVal);
break;
case CP_ENTRY_FLOAT:
double doubleVal = ((FloatCPEntry) cpEntry).getValue();
dataOutStream.writeDouble(doubleVal);
break;
case CP_ENTRY_STRING:
nameCPIndex = ((StringCPEntry) cpEntry).getStringCPIndex();
dataOutStream.writeInt(nameCPIndex);
break;
case CP_ENTRY_PACKAGE:
nameCPIndex = ((PackageRefCPEntry) cpEntry).nameCPIndex;
dataOutStream.writeInt(nameCPIndex);
break;
case CP_ENTRY_FUNCTION_REF:
FunctionRefCPEntry funcRefEntry = (FunctionRefCPEntry) cpEntry;
dataOutStream.writeInt(funcRefEntry.packageCPIndex);
dataOutStream.writeInt(funcRefEntry.nameCPIndex);
break;
case CP_ENTRY_ACTION_REF:
ActionRefCPEntry actionRefEntry = (ActionRefCPEntry) cpEntry;
dataOutStream.writeInt(actionRefEntry.getPackageCPIndex());
dataOutStream.writeInt(actionRefEntry.getNameCPIndex());
break;
case CP_ENTRY_STRUCTURE_REF:
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) cpEntry;
dataOutStream.writeInt(structureRefCPEntry.packageCPIndex);
dataOutStream.writeInt(structureRefCPEntry.nameCPIndex);
break;
case CP_ENTRY_TYPE_REF:
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
dataOutStream.writeInt(typeRefCPEntry.typeSigCPIndex);
break;
case CP_ENTRY_FORK_JOIN:
ForkJoinCPEntry forkJoinCPEntry = (ForkJoinCPEntry) cpEntry;
dataOutStream.writeInt(forkJoinCPEntry.forkJoinInfoIndex);
break;
case CP_ENTRY_WRKR_DATA_CHNL_REF:
WorkerDataChannelRefCPEntry workerDataChannelCPEntry = (WorkerDataChannelRefCPEntry) cpEntry;
dataOutStream.writeInt(workerDataChannelCPEntry.getUniqueNameCPIndex());
break;
case CP_ENTRY_TRANSFORMER_REF:
TransformerRefCPEntry transformerRefEntry = (TransformerRefCPEntry) cpEntry;
dataOutStream.writeInt(transformerRefEntry.packageCPIndex);
dataOutStream.writeInt(transformerRefEntry.nameCPIndex);
break;
}
}
}
use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.
the class ErrorTableEntry method setPackageInfo.
public void setPackageInfo(PackageInfo packageInfo) {
this.packageInfo = packageInfo;
// Load Cache values.
if (errorStructCPIndex < 0) {
return;
}
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(errorStructCPIndex);
// this.error = (StructInfo) structureRefCPEntry.getStructureTypeInfo();
}
use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangTryCatchFinally tryNode) {
Operand gotoTryCatchEndAddr = getOperand(-1);
Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
List<int[]> unhandledErrorRangeList = new ArrayList<>();
ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
// Handle try block.
int fromIP = nextIP();
genNode(tryNode.tryBody, env);
int toIP = nextIP() - 1;
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
unhandledErrorRangeList.add(new int[] { fromIP, toIP });
// Handle catch blocks.
int order = 0;
for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
addLineNumberInfo(bLangCatch.pos);
int targetIP = nextIP();
genNode(bLangCatch, env);
unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
// Append finally block instructions.
if (tryNode.finallyBody != null) {
genNode(tryNode.finallyBody, env);
}
emit(instructGotoTryCatchEnd);
// Create Error table entry for this catch block
BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
errorTableEntry.setError(errorStructInfo);
errorTable.addErrorTableEntry(errorTableEntry);
}
if (tryNode.finallyBody != null) {
// Create Error table entry for unhandled errors in try and catch(s) blocks
for (int[] range : unhandledErrorRangeList) {
ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
errorTable.addErrorTableEntry(errorTableEntry);
}
// Append finally block instruction.
genNode(tryNode.finallyBody, env);
emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
}
gotoTryCatchEndAddr.value = nextIP();
}
use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry 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.cpentries.StructureRefCPEntry 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);
}
Aggregations