mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 17:08:03 +00:00
Merge branch 'main' of https://github.com/JonathanFleischmann/CompilerULTIMATE
This commit is contained in:
commit
dbca4ac327
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user