From 918acef610eb9b1f58e9087c905eef527ad57944 Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Wed, 8 May 2024 22:16:09 +0200 Subject: [PATCH] Add Bytecodegenerator for LocalVariable --- .../de/maishai/typedast/MethodContext.java | 18 +++++++++++++++--- .../typedclass/TypedLocalVariable.java | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index b1b78f1..392f4de 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -1,10 +1,22 @@ package de.maishai.typedast; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; +import org.objectweb.asm.Label; + +import java.util.HashMap; +import java.util.Map; @NoArgsConstructor +@Getter +@Setter @Data public class MethodContext { + private Label startLabel; + private Label endLabel; + private int localVarIndex = 0; + private Map variableIndex = new HashMap<>(); + + public void addVariable(String name, int index) { + variableIndex.put(name, index); + } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index bb5b483..5177987 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -38,5 +38,9 @@ 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); + mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index); } }