From d631b01c8276d508a96d1df3f828b00b0dc5101b Mon Sep 17 00:00:00 2001 From: i22007 Date: Sun, 23 Jun 2024 16:06:07 -0400 Subject: [PATCH] ConstructorNode and NotNode --- src/main/java/bytecode/MethodCodeGen.java | 36 +++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index 563027e..9b799ab 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -19,9 +19,11 @@ import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; import ast.type.ValueNode; import ast.type.type.BaseType; +import ast.type.type.TypeEnum; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; import java.util.ArrayList; import java.util.List; @@ -51,14 +53,27 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType), "", - "()V", + mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters), null, null); + methodVisitor.visitCode(); + localVaribales.add("this"); + // Add all method parameters to localVariables + for (ParameterNode parameterNode : constructorNode.parameters) { + localVaribales.add(parameterNode.identifier); + } + methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false); + + // Visit all statements + for (IStatementNode statementNode : constructorNode.block.statements) { + statementNode.accept(this); + } + methodVisitor.visitInsn(RETURN); - methodVisitor.visitMaxs(1, 1); + methodVisitor.visitMaxs(1, localVaribales.size()); methodVisitor.visitEnd(); } @@ -168,7 +183,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { switch (nonCalculationNode.operator) { case AND: nonCalculationNode.unaryExpression.accept(this); - methodVisitor.visitJumpInsn(IFEQ, labelFalse); + methodVisitor.visitJumpInsn(IFEQ, labelFalse); // Value compared to 0 (false) nonCalculationNode.expression.accept(this); methodVisitor.visitJumpInsn(IFEQ, labelFalse); break; @@ -240,7 +255,22 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { @Override public void visit(NotNode notNode) { + Label labelFalse = new Label(); + Label labelTrue = new Label(); + notNode.expression.accept(this); + methodVisitor.visitJumpInsn(Opcodes.IFNE, labelFalse); // false if value is true (value != 0) + + Label labelEndLogicalExpression = new Label(); + + methodVisitor.visitLabel(labelTrue); + methodVisitor.visitInsn(ICONST_1); // true + methodVisitor.visitJumpInsn(GOTO, labelEndLogicalExpression); + + methodVisitor.visitLabel(labelFalse); + methodVisitor.visitInsn(ICONST_0); // false + + methodVisitor.visitLabel(labelEndLogicalExpression); } @Override