refactor and make typedFieldVarAccess Work

This commit is contained in:
404Simon 2024-05-14 00:02:47 +02:00
parent 922a0b7ffd
commit 9f084a79bb
6 changed files with 36 additions and 83 deletions

View File

@ -6,11 +6,13 @@ import org.objectweb.asm.ClassWriter;
@Data
public class ClassContext {
private Type name;
private String name;
private Type type;
private ClassWriter cw;
public ClassContext(String name, ClassWriter cw) {
this.name = Type.REFERENCE(name);
this.name = name;
type = Type.REFERENCE(name);
this.cw = cw;
}
}

View File

@ -28,7 +28,7 @@ public class MethodContext {
endLabel = new Label();
this.mv = mv;
this.classContext = classContext;
registerVariable("this", classContext.getName());
registerVariable("this", classContext.getType());
mv.visitCode();
mv.visitLabel(startLabel);
}
@ -58,6 +58,7 @@ public class MethodContext {
} else {
mv.visitVarInsn(Opcodes.ILOAD, localVariable.index);
}
System.out.println("Pushed " + variableIndex.get(varName) + " to stack");
}
public void popStack() {
@ -66,6 +67,7 @@ public class MethodContext {
public void wrapUp() {
mv.visitLabel(endLabel);
System.out.println("maxStack: " + maxStack + " localVarIndex: " + localVarIndex);
mv.visitMaxs(maxStack, localVarIndex);
mv.visitEnd();
}

View File

@ -63,9 +63,19 @@ public class TypedAssignment implements TypedStatement {
@Override
public void codeGen(MethodContext ctx) {
if (value instanceof TypedIntLiteral) {
ctx.getMv().visitIntInsn(Opcodes.BIPUSH, ((TypedIntLiteral) value).getValue());
ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVarIndex());
ctx.pushStack("this");
// load location recursively on stack
location.codeGen(ctx);
// put value on stack (WIP!!)
ctx.pushStack("x");
//save value in field
String receiver = ctx.getClassContext().getName();
if (location.getRecursiveOwnerChain() != null) {
receiver = location.getRecursiveOwnerChain().getType().getReference();
}
ctx.getMv().visitFieldInsn(Opcodes.PUTFIELD, receiver, location.getName(), value.getType().getDescriptor());
System.out.println("PUTFIELD: " + receiver + " " + location.getName() + " " + value.getType().getDescriptor());
}
}

View File

@ -77,11 +77,14 @@ public class TypedConstructor implements TypedNode {
public void codeGen(ClassContext ctx) {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
System.out.println("Visiting method: " + "<init>" + CodeGenUtils.generateDescriptor(typedParameters, Type.VOID));
MethodContext mctx = new MethodContext(ctx, mv);
typedParameters.forEach(param -> mctx.registerVariable(param.getParaName(), param.getType()));
//super();
mctx.pushStack("this");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
System.out.println("INVOKESPECIAL: " + "java/lang/Object" + " " + "<init>" + " " + "()V");
mctx.popStack();
typedBlock.codeGen(mctx);

View File

@ -7,6 +7,7 @@ import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
@ -76,6 +77,12 @@ public class TypedFieldVarAccess implements TypedExpression {
@Override
public void codeGen(MethodContext ctx) {
if (recursiveOwnerChain != null) {
recursiveOwnerChain.codeGen(ctx);
}
if (!field) {
ctx.getMv().visitFieldInsn(Opcodes.GETFIELD, ctx.getClassContext().getName(), name, type.getDescriptor());
System.out.println("GETFIELD: " + ctx.getClassContext().getName() + " " + name + " " + type.getDescriptor());
}
}
}

View File

@ -1,81 +1,10 @@
public class ClassCanBeTyped {
public ClassCanBeTyped c;
public int x;
int x;
int y;
ClassCanBeTyped b;
ClassCanBeTyped c;
public ClassCanBeTyped(int x) {
this.x = x;
this.c.x = x;
}
public ClassCanBeTyped(int x, int y) {
this.x = x;
this.y = y;
}
public ClassCanBeTyped initClassCanBeTyped(int x) {
int a;
a = 10;
b = new ClassCanBeTyped(x);
b.x = 10 + a;
b.y = 20;
b.c.x = 20 + a;
b.c.b.y = b.x;
return b;
}
public ClassCanBeTyped init(int x, int y) {
return new ClassCanBeTyped(x, y);
}
public ClassCanBeTyped(int x) {
this.x = x;
int i;
b = b.getX().c.getC();
i = b.getX().c.getX();
}
public ClassCanBeTyped() {
this.x = 10;
int i;
for (i = 0; i < (x + 1); i = i + 1) {
int j;
for (j = 0; j < this.x; j += 1) {
this.x = this.x * this.x;
break;
}
}
do {
this.y = this.y + 1;
} while (this.y < 10);
int k;
k = 0;
for (k = 0; k < 10; k = k + 1) {
if (k == 5) {
return;
}
}
}
public ClassCanBeTyped(int x, int y, char z) {
this.x = x;
this.y = y;
}
public int getX(char z) {
return this.x;
}
public ClassCanBeTyped getC() {
return c;
}
public int getX() {
return x;
}
public boolean methodCall() {
return false;
}
}