use of org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry 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.IntegerCPEntry in project ballerina by ballerina-lang.
the class CodeGenerator method getDefaultValue.
private DefaultValue getDefaultValue(BLangLiteral literalExpr) {
String desc = literalExpr.type.getDesc();
int typeDescCPIndex = addUTF8CPEntry(currentPkgInfo, desc);
DefaultValue defaultValue = new DefaultValue(typeDescCPIndex, desc);
int typeTag = literalExpr.type.tag;
switch(typeTag) {
case TypeTags.INT:
defaultValue.intValue = (Long) literalExpr.value;
defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(defaultValue.intValue));
break;
case TypeTags.FLOAT:
defaultValue.floatValue = (Double) literalExpr.value;
defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(defaultValue.floatValue));
break;
case TypeTags.STRING:
defaultValue.stringValue = (String) literalExpr.value;
defaultValue.valueCPIndex = currentPkgInfo.addCPEntry(new UTF8CPEntry(defaultValue.stringValue));
break;
case TypeTags.BOOLEAN:
defaultValue.booleanValue = (Boolean) literalExpr.value;
break;
default:
defaultValue = null;
}
return defaultValue;
}
use of org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
// Expressions
@Override
public void visit(BLangLiteral literalExpr) {
int opcode;
Operand regIndex = calcAndGetExprRegIndex(literalExpr);
int typeTag = literalExpr.type.tag;
switch(typeTag) {
case TypeTags.INT:
long longVal = (Long) literalExpr.value;
if (longVal >= 0 && longVal <= 5) {
opcode = InstructionCodes.ICONST_0 + (int) longVal;
emit(opcode, regIndex);
} else {
int intCPEntryIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(longVal));
emit(InstructionCodes.ICONST, getOperand(intCPEntryIndex), regIndex);
}
break;
case TypeTags.FLOAT:
double doubleVal = (Double) literalExpr.value;
if (doubleVal == 0 || doubleVal == 1 || doubleVal == 2 || doubleVal == 3 || doubleVal == 4 || doubleVal == 5) {
opcode = InstructionCodes.FCONST_0 + (int) doubleVal;
emit(opcode, regIndex);
} else {
int floatCPEntryIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(doubleVal));
emit(InstructionCodes.FCONST, getOperand(floatCPEntryIndex), regIndex);
}
break;
case TypeTags.STRING:
String strValue = (String) literalExpr.value;
StringCPEntry stringCPEntry = new StringCPEntry(addUTF8CPEntry(currentPkgInfo, strValue), strValue);
int strCPIndex = currentPkgInfo.addCPEntry(stringCPEntry);
emit(InstructionCodes.SCONST, getOperand(strCPIndex), regIndex);
break;
case TypeTags.BOOLEAN:
boolean booleanVal = (Boolean) literalExpr.value;
if (!booleanVal) {
opcode = InstructionCodes.BCONST_0;
} else {
opcode = InstructionCodes.BCONST_1;
}
emit(opcode, regIndex);
break;
case TypeTags.NULL:
emit(InstructionCodes.RCONST_NULL, regIndex);
}
}
Aggregations