From 5676fb31def67f5f69c50ca50ea55c911e45079c Mon Sep 17 00:00:00 2001 From: 404Simon Date: Wed, 8 May 2024 23:21:09 +0200 Subject: [PATCH] refactor methodcontext --- .../de/maishai/typedast/MethodContext.java | 22 +++++++++++++++++-- .../typedast/typedclass/TypedConstructor.java | 7 ++---- .../typedclass/TypedLocalVariable.java | 5 ++--- .../typedast/typedclass/TypedMethod.java | 7 +----- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index 392f4de..8ed53fd 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -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 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(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index cd66533..1b61a15 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -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, "", 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", "", "()V", false); - MethodContext context = new MethodContext(); typedBlock.codeGen(mv, context); mv.visitInsn(Opcodes.RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); + context.wrapUp(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index 5177987..a7c20e1 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -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); + } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index ea27c4f..0de7fe6 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -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(); } }