From 5dae1674431c9f9cde28ecb373521de74384a6ec Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Tue, 2 Jul 2024 18:35:25 +0200 Subject: [PATCH] Fixed TestClassInput to run through which included the right usage of the class fields as it always loaded this onto the stack, regardless of the class in which the field is in --- .../Expression/InstVarExpression.java | 25 ++++++++++++++++++- .../AssignStatementExpression.java | 23 +++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index d012940..05a5144 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -50,7 +50,30 @@ public class InstVarExpression extends AbstractType implements IExpression{ // typeContext: (ClassName, (FieldName, FieldType)) public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext, HashMap>> methodContext) throws Exception { // Load "this" onto the stack - mv.visitVarInsn(Opcodes.ALOAD, 0); + + // Determine if the reference is this or not + if (this.receivers.get(0).identifier != null) { + + // Load the local variable onto the stack + int index = -1; + int counter = 0; + for (String key : localVars.keySet()){ + if (key.equals(this.receivers.get(0).identifier)){ + index = counter+1; // +1 because the first local variable is at index 1, 0 is used for "this" + break; + } + counter++; + } + + if (index == -1){ + throw new Exception("Variable " + this.receivers.get(0).identifier + " not found"); + } + mv.visitVarInsn(Opcodes.ALOAD, index); + + } else { + // Load "this" onto the stack + mv.visitVarInsn(Opcodes.ALOAD, 0); + } //Get the field information String fieldType = typeContext.get(thisClass).get(fieldName); diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index e970e61..2b1b305 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -117,8 +117,27 @@ public class AssignStatementExpression extends AbstractType implements IExpressi } else if (left instanceof InstVarExpression) { instVar = (InstVarExpression) left; - // Load "this" onto the stack - mv.visitVarInsn(Opcodes.ALOAD, 0); + // Load the reference onto the stack + // Determine if the reference is this or another object + if (instVar.receivers.get(0).identifier != null) { + // Load the local variable (another object) onto the stack + int index = -1; + int counter = 0; + for (String key : localVars.keySet()) { + if (key.equals(instVar.receivers.get(0).identifier)) { + index = counter + 1; // Local variables start at index 1, 0 is "this" + break; + } + counter++; + } + if (index == -1) { + throw new Exception("Variable " + instVar.receivers.get(0).identifier + " not found"); + } + mv.visitVarInsn(Opcodes.ALOAD, index); + } else { + // Load "this" onto the stack + mv.visitVarInsn(Opcodes.ALOAD, 0); + } // Call the codeGen on the right expression which will push the value of the right expression onto the stack right.codeGen(mv, localVars, typeContext, methodContext);