Skip to content

Commit

Permalink
[jvm] Move emitValue() into JvmHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Sep 30, 2024
1 parent 185ff7e commit 19c58e1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
37 changes: 1 addition & 36 deletions aeneas/src/jvm/JvmBuilder.v3
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,7 @@ class JvmClassfileBuilder(jprog: JvmProgram, jclass: JvmClass) {
file.super_class = newClassref(jclass.superName);
}
def emitValue(code: JvmCodeBuilder, etype: Type, val: Val) -> int {
match (etype.typeCon.kind) {
BOOL => code.iconst(Bool.toInt(Bool.unbox(val)));
ENUM_SET => emitIntValue(code, V3.getEnumSetType(etype), val);
ENUM => emitIntValue(code, V3.getVariantTagType(etype), val);
INT => emitIntValue(code, IntType.!(etype), val);
FLOAT => emitFloatValue(code, FloatType.!(etype), val);
ARRAY,
VARIANT,
CLASS => jprog.jheap.emitRecordValue(code, Record.!(val));
TUPLE => jprog.context.fail("normalization should remove all tuple values");
ANYFUNC,
FUNCREF => jprog.jheap.emitFunctionValue(code, etype, FuncVal.!(val));
VOID,
COMPONENT => ; // emit nothing
} else {
unknownValue(etype, val);
}
return 1;
}
def emitIntValue(code: JvmCodeBuilder, tt: IntType, val: Val) {
if (tt.size <= 4) code.iconst(V3.unboxI32(val));
else code.lconst(Long.unboxSU(val, tt.signed));
}
def emitFloatValue(code: JvmCodeBuilder, ft: FloatType, val: Val) {
if (ft.is64) {
code.dconst(if(val == null, 0, Float64Val.!(val).bits));
} else {
code.fconst(if(val == null, 0, Float32Val.!(val).bits));
}
}
private def unknownValue(etype: Type, val: Val) {
var buffer = Strings.builderOf("Jvm Compiler error: ");
buffer.puts(jclass.name).puts(": unknown value type \"");
etype.render(buffer);
buffer.putc('\"');
jprog.context.fail(buffer.toString());
return jprog.jheap.emitValue(code, etype, val);
}
def newField(name: string, desc: string) -> JvmField {
var fld = JvmField.new(newUtf8(name), newUtf8(desc));
Expand Down
55 changes: 44 additions & 11 deletions aeneas/src/jvm/JvmHeap.v3
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,41 @@ class JvmHeap(jprog: JvmProgram) {
heapBuilder = JvmClassfileBuilder.new(jprog, heapClass);
jvmRecords = Array<JvmHI_Record>.new(jprog.prog.recordCount);
}
def emitFunctionValue(code: JvmCodeBuilder, etype: Type, fval: FuncVal) {
def emitValue(code: JvmCodeBuilder, etype: Type, val: Val) -> int {
match (etype.typeCon.kind) {
BOOL => code.iconst(Bool.toInt(Bool.unbox(val)));
ENUM_SET => emitIntValue(code, V3.getEnumSetType(etype), val);
ENUM => emitIntValue(code, V3.getVariantTagType(etype), val);
INT => emitIntValue(code, IntType.!(etype), val);
FLOAT => emitFloatValue(code, FloatType.!(etype), val);
ARRAY,
VARIANT,
CLASS => emitRecordValue(code, Record.!(val));
ANYFUNC,
FUNCREF => emitFunctionValue(code, etype, FuncVal.!(val));
VOID,
COMPONENT => ; // emit nothing
} else {
var buf = StringBuilder.new().puts("unexpected value: ");
V3.renderResult(val, etype, buf);
buf.puts(" of type ");
etype.render(buf);
jprog.context.fail(buf.toString());
}
return 1;
}
private def emitIntValue(code: JvmCodeBuilder, tt: IntType, val: Val) {
if (tt.size <= 4) code.iconst(V3.unboxI32(val));
else code.lconst(Long.unboxSU(val, tt.signed));
}
private def emitFloatValue(code: JvmCodeBuilder, ft: FloatType, val: Val) {
if (ft.is64) {
code.dconst(if(val == null, 0, Float64Val.!(val).bits));
} else {
code.fconst(if(val == null, 0, Float32Val.!(val).bits));
}
}
private def emitFunctionValue(code: JvmCodeBuilder, etype: Type, fval: FuncVal) {
if (fval != null) {
var jclass = jprog.newClosure(fval.memberRef);
code.getstatic(jclass, "instance", jclass);
Expand All @@ -24,7 +58,7 @@ class JvmHeap(jprog: JvmProgram) {
code.aconst_null();
}
}
def emitRecordValue(code: JvmCodeBuilder, rval: Record) {
private def emitRecordValue(code: JvmCodeBuilder, rval: Record) {
if (rval != Values.BOTTOM) {
var instr = visitRecord(rval, rval.rtype, null, 0);
code.getstatic(heapBuilder.jclass, instr.fname, instr.jtype);
Expand Down Expand Up @@ -83,14 +117,14 @@ class JvmHeap(jprog: JvmProgram) {
clinit_code.attach(clinit_method);
jprog.emitJvmClassfile(heapBuilder.file);
}
def newField(prefix: string, ref: JvmHI_Ref, jtype: JvmType) {
private def newField(prefix: string, ref: JvmHI_Ref, jtype: JvmType) {
if (ref.fname == null) {
ref.fname = Strings.format2("%s%d", prefix, fieldID++);
var fld = heapBuilder.newField(ref.fname, jtype.descriptor());
fld.setFlag(true, JvmConstant.ACC_STATIC);
}
}
def visitValue(val: Val, etype: Type, ref: JvmHI_Ref, index: int) {
private def visitValue(val: Val, etype: Type, ref: JvmHI_Ref, index: int) {
if (val == null) {
instrs.put(JvmHI_Value.new(val, etype));
return;
Expand Down Expand Up @@ -134,12 +168,12 @@ class JvmHeap(jprog: JvmProgram) {
}
return instr;
}
def addCycle(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
private def addCycle(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
newField("c", ref, ref.jtype);
instr.inner = true;
instr.cycles = List.new(JvmHI_CycleRef.new(ref, index), instr.cycles);
}
def visitUselessArray(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
private def visitUselessArray(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
instrs.put(JvmHI_UselessArray.new(instr, instr.rval.values.length));
if (ref == null) newField("a", instr, instr.jtype);
else instr.inner = true;
Expand All @@ -162,7 +196,7 @@ class JvmHeap(jprog: JvmProgram) {
if (ref == null) newField("a", instr, instr.jtype);
else instr.inner = true;
}
def visitString(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
private def visitString(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
// first check whether the string has any negative characters
var values = instr.rval.values, str = Array<byte>.new(values.length);
for (i < values.length) {
Expand All @@ -178,7 +212,7 @@ class JvmHeap(jprog: JvmProgram) {
if (ref == null) newField("s", instr, instr.jtype);
else instr.inner = true;
}
def visitObject(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
private def visitObject(instr: JvmHI_Record, ref: JvmHI_Ref, index: int) {
var ctype = instr.rval.rtype;
var ic = jprog.prog.ir.getIrClass(ctype);
var values = instr.rval.values;
Expand All @@ -191,7 +225,7 @@ class JvmHeap(jprog: JvmProgram) {
if (ref == null) newField("o", instr, instr.jtype);
else instr.inner = true;
}
def newRecord(rval: Record) -> JvmHI_Record {
private def newRecord(rval: Record) -> JvmHI_Record {
var jtype = jprog.jvmType(rval.rtype);
var jrecord = JvmHI_Record.new(rval, jtype);
if (rval.id >= jvmRecords.length) {
Expand Down Expand Up @@ -223,8 +257,7 @@ class JvmHI_Ref(jtype: JvmType) extends JvmHI {

class JvmHI_Value(val: Val, vtype: Type) extends JvmHI {
def emitInit(heap: JvmHeap, code: JvmCodeBuilder) {
heap.heapBuilder.emitValue(code, vtype, val);
code.pushN(1);
heap.emitValue(code, vtype, val);
}
}

Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/main/Version.v3
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

// Updated by VCS scripts. DO NOT EDIT.
component Version {
def version: string = "III-7.1761";
def version: string = "III-7.1762";
var buildData: string;
}

0 comments on commit 19c58e1

Please sign in to comment.