This commit is contained in:
laurenz 2024-05-08 23:54:56 +02:00
commit dbca4ac327
4 changed files with 25 additions and 16 deletions

View File

@ -2,21 +2,39 @@ package de.maishai.typedast;
import lombok.*;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.Map;
@NoArgsConstructor
@Getter
@Setter
@Data
public class MethodContext {
private Label startLabel;
private Label endLabel;
private MethodVisitor mv;
private int localVarIndex = 0;
private Map<String, Integer> variableIndex = new HashMap<>();
public void addVariable(String name, int index) {
public MethodContext(MethodVisitor mv) {
startLabel = new Label();
endLabel = new Label();
this.mv = mv;
mv.visitCode();
mv.visitLabel(startLabel);
}
public int addVariable(String name) {
int index = localVarIndex;
localVarIndex++;
variableIndex.put(name, index);
return index;
}
public void wrapUp() {
mv.visitLabel(endLabel);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
}

View File

@ -91,18 +91,15 @@ public class TypedConstructor implements TypedNode {
public void codeGen(ClassWriter cw) {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
MethodVisitor mv = cw.visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
mv.visitCode();
MethodContext context = new MethodContext(mv);
//super();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
MethodContext context = new MethodContext();
typedBlock.codeGen(mv, context);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
context.wrapUp();
}
}

View File

@ -38,9 +38,8 @@ public final class TypedLocalVariable implements TypedNode {
}
public void codeGen(MethodVisitor mv, MethodContext ctx) {
int index = ctx.getLocalVarIndex();
ctx.setLocalVarIndex(index + 1);
ctx.addVariable(name, index);
int index = ctx.addVariable(name);
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index);
}
}

View File

@ -93,14 +93,9 @@ public class TypedMethod implements TypedNode {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
MethodVisitor mv = cw.visitMethod(accessModifier, name,
CodeGenUtils.generateDescriptor(typedParameters, returnType), null, null);
mv.visitCode();
MethodContext context = new MethodContext(mv);
MethodContext context = new MethodContext();
typedBlock.codeGen(mv, context);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
}