Fix some type conversions

This commit is contained in:
Victorious3 2022-10-02 18:27:41 +02:00
parent a660d7a295
commit b4e1be0fb7
2 changed files with 11 additions and 6 deletions

View File

@ -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, "<init>", 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());