From ce399626dac29071d2b4c65a0bfd42ed229990b3 Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Wed, 8 May 2024 12:56:40 +0200 Subject: [PATCH] Fixed typo in codeGen method declaration --- .../Datatype/BoolDatatype.java | 2 +- .../Datatype/CharDatatype.java | 2 +- .../Datatype/IDatatype.java | 2 +- .../Datatype/IntDatatype.java | 2 +- .../abstractSyntaxTree/Datatype/RefType.java | 3 +- .../Expression/BinaryExpression.java | 50 +++++++++---------- .../Expression/IExpression.java | 2 +- .../Expression/InstVarExpression.java | 2 +- .../Expression/UnaryExpression.java | 4 +- .../Expression/VarRefExpression.java | 2 +- Source/abstractSyntaxTree/Program.java | 1 + .../Statement/BlockStatement.java | 5 +- .../Statement/EmptyStatement.java | 2 +- .../Statement/IStatement.java | 2 +- .../Statement/IfElseStatement.java | 6 +-- .../Statement/IfStatement.java | 4 +- .../Statement/ReturnStatement.java | 2 +- .../Statement/WhileStatement.java | 6 +-- 18 files changed, 51 insertions(+), 48 deletions(-) diff --git a/Source/abstractSyntaxTree/Datatype/BoolDatatype.java b/Source/abstractSyntaxTree/Datatype/BoolDatatype.java index 6f571fb..6b8ccf0 100644 --- a/Source/abstractSyntaxTree/Datatype/BoolDatatype.java +++ b/Source/abstractSyntaxTree/Datatype/BoolDatatype.java @@ -17,7 +17,7 @@ public class BoolDatatype extends AbstractType implements IDatatype{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { if(value) { mv.visitInsn(Opcodes.ICONST_1); // Pushes the int 1 on the stack (true) } else { diff --git a/Source/abstractSyntaxTree/Datatype/CharDatatype.java b/Source/abstractSyntaxTree/Datatype/CharDatatype.java index 12424fa..1346f0d 100644 --- a/Source/abstractSyntaxTree/Datatype/CharDatatype.java +++ b/Source/abstractSyntaxTree/Datatype/CharDatatype.java @@ -17,7 +17,7 @@ public class CharDatatype extends AbstractType implements IDatatype{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { // Possible use of BIPUSH and SIPUSH if the value is small enough //This saves space in the bytecode which is not very relevant at this point, but could be implemented anyway diff --git a/Source/abstractSyntaxTree/Datatype/IDatatype.java b/Source/abstractSyntaxTree/Datatype/IDatatype.java index 0947c89..d729f96 100644 --- a/Source/abstractSyntaxTree/Datatype/IDatatype.java +++ b/Source/abstractSyntaxTree/Datatype/IDatatype.java @@ -9,7 +9,7 @@ public interface IDatatype { // visit method for code generation - void CodeGen(MethodVisitor mv) throws Exception; + void codeGen(MethodVisitor mv) throws Exception; } //TODO: Check if we need to differentiate between primitive types and reference types --> for example in "==" \ No newline at end of file diff --git a/Source/abstractSyntaxTree/Datatype/IntDatatype.java b/Source/abstractSyntaxTree/Datatype/IntDatatype.java index a49c496..4733931 100644 --- a/Source/abstractSyntaxTree/Datatype/IntDatatype.java +++ b/Source/abstractSyntaxTree/Datatype/IntDatatype.java @@ -21,7 +21,7 @@ public class IntDatatype extends AbstractType implements IDatatype{ @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { //Example of using BIPUSH and SIPUSH for optimizing bytecode size if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) diff --git a/Source/abstractSyntaxTree/Datatype/RefType.java b/Source/abstractSyntaxTree/Datatype/RefType.java index 42f9876..1c3d0ab 100644 --- a/Source/abstractSyntaxTree/Datatype/RefType.java +++ b/Source/abstractSyntaxTree/Datatype/RefType.java @@ -6,6 +6,7 @@ import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Program; import jdk.jshell.spi.ExecutionControl; +import org.objectweb.asm.MethodVisitor; import java.util.HashMap; import java.util.List; @@ -50,7 +51,7 @@ public class RefType extends AbstractType implements IDatatype { // Method for code generation which iterates over all the field declarations // and method declarations and calls their CodeGen methods @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { throw new ExecutionControl.NotImplementedException("CodeGen not implemented for RefType"); } diff --git a/Source/abstractSyntaxTree/Expression/BinaryExpression.java b/Source/abstractSyntaxTree/Expression/BinaryExpression.java index 990ebde..72af1be 100644 --- a/Source/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/Source/abstractSyntaxTree/Expression/BinaryExpression.java @@ -59,7 +59,7 @@ public class BinaryExpression extends AbstractType implements IExpression{ @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { // Label for the jump instruction Label operationFalse = new Label(); //Operation is false Label operationTrue = new Label(); //Operation is true @@ -69,88 +69,88 @@ public class BinaryExpression extends AbstractType implements IExpression{ // Bytecode for the binary operation switch (operator) { case "&&": - left.CodeGen(mv); + left.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // IFEQ --> "if equals to zero" (false) --> if left exp is false - right.CodeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // If right exp is false, jump to the end of the whole expression mv.visitJumpInsn(Opcodes.GOTO, operationTrue); // If it reaches this point, the right exp is true break; case "||": - left.CodeGen(mv); + left.codeGen(mv); mv.visitJumpInsn(Opcodes.IFNE, operationTrue); // IFNE --> "if not equals to zero" (true) --> if left exp is true - right.CodeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IFNE, operationTrue); break; case "==": // Keep in mind that only primitive types are allowed in this case (at this time) - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression break; case "<": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPLT, operationTrue); // Checks only on less than, not equal break; case ">": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPGT, operationTrue); // Checks only on greater than, not equal break; case "<=": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPLE, operationTrue); // Checks on less than OR equal break; case ">=": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPGE, operationTrue); // Checks on greater than OR equal break; case "!=": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPNE, operationTrue); // Checks on not equal break; case "+": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitInsn(Opcodes.IADD); break; case "-": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitInsn(Opcodes.ISUB); break; case "*": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitInsn(Opcodes.IMUL); break; case "/": - left.CodeGen(mv); - right.CodeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitInsn(Opcodes.IDIV); break; diff --git a/Source/abstractSyntaxTree/Expression/IExpression.java b/Source/abstractSyntaxTree/Expression/IExpression.java index 6ef53bd..a679711 100644 --- a/Source/abstractSyntaxTree/Expression/IExpression.java +++ b/Source/abstractSyntaxTree/Expression/IExpression.java @@ -8,5 +8,5 @@ public interface IExpression { TypeCheckResult typeCheck() throws Exception; // visit method for code generation - void CodeGen(MethodVisitor mv) throws Exception; + void codeGen(MethodVisitor mv) throws Exception; } diff --git a/Source/abstractSyntaxTree/Expression/InstVarExpression.java b/Source/abstractSyntaxTree/Expression/InstVarExpression.java index 4cefa0c..7b45d3a 100644 --- a/Source/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/Source/abstractSyntaxTree/Expression/InstVarExpression.java @@ -22,7 +22,7 @@ public class InstVarExpression implements IExpression{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { throw new ExecutionControl.NotImplementedException("CodeGen not implemented for InstVarExpression"); //ALOAD the index of the var diff --git a/Source/abstractSyntaxTree/Expression/UnaryExpression.java b/Source/abstractSyntaxTree/Expression/UnaryExpression.java index 890a43a..4acdc53 100644 --- a/Source/abstractSyntaxTree/Expression/UnaryExpression.java +++ b/Source/abstractSyntaxTree/Expression/UnaryExpression.java @@ -42,9 +42,9 @@ public class UnaryExpression extends AbstractType implements IExpression{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { - operand.CodeGen(mv); + operand.codeGen(mv); switch (operator) { case "!": diff --git a/Source/abstractSyntaxTree/Expression/VarRefExpression.java b/Source/abstractSyntaxTree/Expression/VarRefExpression.java index b94b166..9276930 100644 --- a/Source/abstractSyntaxTree/Expression/VarRefExpression.java +++ b/Source/abstractSyntaxTree/Expression/VarRefExpression.java @@ -17,7 +17,7 @@ public class VarRefExpression implements IExpression{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { throw new Exception("CodeGen not implemented for VarRefExpression"); } } diff --git a/Source/abstractSyntaxTree/Program.java b/Source/abstractSyntaxTree/Program.java index 8d06220..0c9aa5d 100644 --- a/Source/abstractSyntaxTree/Program.java +++ b/Source/abstractSyntaxTree/Program.java @@ -3,6 +3,7 @@ package abstractSyntaxTree; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Datatype.RefType; +import org.objectweb.asm.MethodVisitor; import java.util.HashMap; import java.util.List; diff --git a/Source/abstractSyntaxTree/Statement/BlockStatement.java b/Source/abstractSyntaxTree/Statement/BlockStatement.java index 84cb0d7..da8e26d 100644 --- a/Source/abstractSyntaxTree/Statement/BlockStatement.java +++ b/Source/abstractSyntaxTree/Statement/BlockStatement.java @@ -2,6 +2,7 @@ package abstractSyntaxTree.Statement; import TypeCheck.TypeCheckResult; import TypeCheck.AbstractType; +import org.objectweb.asm.*; import java.util.HashMap; import java.util.List; @@ -40,9 +41,9 @@ public class BlockStatement extends AbstractType implements IStatement{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { for (IStatement statement : statements) { - statement.CodeGen(mv); //TODO: I think we need to pass the symbol table here + statement.codeGen(mv); //TODO: I think we need to pass the symbol table here } } } diff --git a/Source/abstractSyntaxTree/Statement/EmptyStatement.java b/Source/abstractSyntaxTree/Statement/EmptyStatement.java index b6ce18e..01818d7 100644 --- a/Source/abstractSyntaxTree/Statement/EmptyStatement.java +++ b/Source/abstractSyntaxTree/Statement/EmptyStatement.java @@ -13,7 +13,7 @@ public class EmptyStatement extends AbstractType implements IStatement{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { //An empty statement does not generate any code } } diff --git a/Source/abstractSyntaxTree/Statement/IStatement.java b/Source/abstractSyntaxTree/Statement/IStatement.java index dd75e23..d15e56b 100644 --- a/Source/abstractSyntaxTree/Statement/IStatement.java +++ b/Source/abstractSyntaxTree/Statement/IStatement.java @@ -9,5 +9,5 @@ public interface IStatement { TypeCheckResult typeCheck() throws Exception; - void CodeGen(MethodVisitor mv) throws Exception; + void codeGen(MethodVisitor mv) throws Exception; } diff --git a/Source/abstractSyntaxTree/Statement/IfElseStatement.java b/Source/abstractSyntaxTree/Statement/IfElseStatement.java index 55f7619..cd0fe10 100644 --- a/Source/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/Source/abstractSyntaxTree/Statement/IfElseStatement.java @@ -38,7 +38,7 @@ public class IfElseStatement extends AbstractType implements IStatement{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { Label conditionFalse = new Label(); Label statementEnd = new Label(); @@ -46,11 +46,11 @@ public class IfElseStatement extends AbstractType implements IStatement{ condition.CodeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) - ifStatement.CodeGen(mv); //If the condition is true, execute the ifBlock + ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock mv.visitJumpInsn(Opcodes.GOTO, statementEnd); //Jump to the end of the if-else statement mv.visitLabel(conditionFalse); - elseStatement.CodeGen(mv); //If the condition is false, execute the elseBlock + elseStatement.codeGen(mv); //If the condition is false, execute the elseBlock mv.visitLabel(statementEnd); //End of the if-else statement diff --git a/Source/abstractSyntaxTree/Statement/IfStatement.java b/Source/abstractSyntaxTree/Statement/IfStatement.java index c1a915d..665f4f1 100644 --- a/Source/abstractSyntaxTree/Statement/IfStatement.java +++ b/Source/abstractSyntaxTree/Statement/IfStatement.java @@ -31,14 +31,14 @@ public class IfStatement extends AbstractType implements IStatement{ } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { Label conditionFalse = new Label(); condition.CodeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) - ifStatement.CodeGen(mv); + ifStatement.codeGen(mv); mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed } diff --git a/Source/abstractSyntaxTree/Statement/ReturnStatement.java b/Source/abstractSyntaxTree/Statement/ReturnStatement.java index 2ee7536..0005827 100644 --- a/Source/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/Source/abstractSyntaxTree/Statement/ReturnStatement.java @@ -31,7 +31,7 @@ public class ReturnStatement extends AbstractType implements IStatement{ // This is a problem at "BinaryExpression" and here because we need to know the type to return // At this point in time we can either return reference types or have an error message @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { if (expression != null) { expression.CodeGen(mv); diff --git a/Source/abstractSyntaxTree/Statement/WhileStatement.java b/Source/abstractSyntaxTree/Statement/WhileStatement.java index 128660e..e923fa1 100644 --- a/Source/abstractSyntaxTree/Statement/WhileStatement.java +++ b/Source/abstractSyntaxTree/Statement/WhileStatement.java @@ -31,16 +31,16 @@ public class WhileStatement extends AbstractType implements IStatement { } @Override - public void CodeGen(MethodVisitor mv) throws Exception { + public void codeGen(MethodVisitor mv) throws Exception { Label conditionFalse = new Label(); Label LoopStart = new Label(); mv.visitLabel(LoopStart); - condition.CodeGen(mv); + condition.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0) - statement.CodeGen(mv); + statement.codeGen(mv); //TODO: If the block ends with a return statement, we might have to pop it from the stack // So the next iteration starts with a clean stack mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop