diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java index 71cbb1f9..e1744b4d 100755 --- a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java @@ -58,8 +58,10 @@ public class Codegen { Scope scope = new Scope(null); int localCounter; MethodVisitor mv; + TargetType returnType; - State(MethodVisitor mv, int localCounter) { + State(TargetType returnType, MethodVisitor mv, int localCounter) { + this.returnType = returnType; this.mv = mv; this.localCounter = localCounter; } @@ -832,13 +834,15 @@ public class Codegen { break; } case TargetFieldVar dot: { + var fieldType = dot.type(); generate(state, dot.left()); generate(state, assign.right()); - boxPrimitive(state, assign.right().type()); + convertTo(state, assign.right().type(), fieldType); + boxPrimitive(state, fieldType); if (dot.isStatic()) mv.visitInsn(DUP); else mv.visitInsn(DUP_X1); - mv.visitFieldInsn(dot.isStatic() ? PUTSTATIC : PUTFIELD, dot.owner().getInternalName(), dot.right(), dot.type().toSignature()); + mv.visitFieldInsn(dot.isStatic() ? PUTSTATIC : PUTFIELD, dot.owner().getInternalName(), dot.right(), fieldType.toSignature()); break; } default: @@ -911,7 +915,8 @@ public class Codegen { case TargetReturn ret: { if (ret.expression() != null) { generate(state, ret.expression()); - boxPrimitive(state, ret.expression().type()); + convertTo(state, ret.expression().type(), state.returnType); + boxPrimitive(state, state.returnType); mv.visitInsn(ARETURN); } else mv.visitInsn(RETURN); break; @@ -975,7 +980,7 @@ public class Codegen { private void generateConstructor(TargetConstructor constructor) { MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); mv.visitCode(); - var state = new State(mv, 1); + var state = new State(null, mv, 1); for (var param: constructor.parameters()) state.createVariable(param.name(), param.type()); @@ -999,7 +1004,7 @@ public class Codegen { // TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null); mv.visitCode(); - var state = new State(mv, method.isStatic() ? 0 : 1); + var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1); for (var param: method.parameters()) state.createVariable(param.name(), param.type()); generate(state, method.block()); diff --git a/src/test/resources/bytecode/javFiles/Box.java b/src/test/resources/bytecode/javFiles/Box.java new file mode 100644 index 00000000..e69de29b