From c5fc378eed99941adc4854c7aa6092276b95e467 Mon Sep 17 00:00:00 2001 From: i22007 Date: Thu, 27 Jun 2024 17:32:50 -0400 Subject: [PATCH] Bytecode for loops --- .../statementexpressions/AssignableNode.java | 5 -- src/main/java/bytecode/MethodCodeGen.java | 70 ++++++++++++++----- .../java/bytecode/visitor/MethodVisitor.java | 1 - 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/main/java/ast/statementexpressions/AssignableNode.java b/src/main/java/ast/statementexpressions/AssignableNode.java index 8f527e0..18b6a45 100644 --- a/src/main/java/ast/statementexpressions/AssignableNode.java +++ b/src/main/java/ast/statementexpressions/AssignableNode.java @@ -23,9 +23,4 @@ public class AssignableNode implements IStatementExpressionNode { return visitor.analyze(this); } - @Override - public void accept(MethodVisitor methodVisitor) { - methodVisitor.visit(this); - } - } diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index e50ed3f..b94aa57 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -72,7 +72,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { statementNode.accept(this); } - methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); } @@ -165,13 +164,13 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { public void visit(DotSubstractionNode dotSubstractionNode) { if (dotSubstractionNode.value != null) { dotSubstractionNode.value.accept(this); - } else if(dotSubstractionNode.identifier != null) { + } else if (dotSubstractionNode.identifier != null) { methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier)); - } else if(dotSubstractionNode.memberAccess != null) { + } else if (dotSubstractionNode.memberAccess != null) { dotSubstractionNode.memberAccess.accept(this); - } else if(dotSubstractionNode.methodCall != null) { + } else if (dotSubstractionNode.methodCall != null) { dotSubstractionNode.methodCall.accept(this); - } else if(dotSubstractionNode.calculationExpression != null) { + } else if (dotSubstractionNode.calculationExpression != null) { dotSubstractionNode.calculationExpression.accept(this); } } @@ -250,7 +249,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { @Override public void visit(MemberAccessNode memberAccessNode) { - if(memberAccessNode.thisExpr) { + if (memberAccessNode.thisExpr) { //methodVisitor.visitFieldInsn(PUTFIELD); } } @@ -339,17 +338,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { public void visit(ValueNode valueNode) { switch (valueNode.valueType) { case INT_VALUE: - int intValue = Integer.parseInt(valueNode.value); - if(intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE ) { // load int as byte + int intValue = Integer.parseInt(valueNode.value); + if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) { // load int as byte methodVisitor.visitIntInsn(BIPUSH, intValue); - } else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE ) { // load int as short + } else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE) { // load int as short methodVisitor.visitIntInsn(SIPUSH, intValue); } else { // load int as const methodVisitor.visitLdcInsn(intValue); } break; case BOOLEAN_VALUE: - if(valueNode.value.equals("true")) { + if (valueNode.value.equals("true")) { methodVisitor.visitInsn(ICONST_1); } else { methodVisitor.visitInsn(ICONST_0); @@ -357,7 +356,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { break; case CHAR_VALUE: char charValue = valueNode.value.charAt(0); - if(charValue <= Byte.MAX_VALUE ) { // load char as byte + if (charValue <= Byte.MAX_VALUE) { // load char as byte methodVisitor.visitIntInsn(BIPUSH, charValue); } else if (charValue <= Short.MAX_VALUE) { // load char as short methodVisitor.visitIntInsn(SIPUSH, charValue); @@ -386,17 +385,61 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { @Override public void visit(WhileNode whileNode) { + Label loopLabel = new Label(); + Label endOfLoopLabel = new Label(); + methodVisitor.visitLabel(loopLabel); + // while loop + whileNode.expression.accept(this); + methodVisitor.visitJumpInsn(IFEQ, endOfLoopLabel); // if condition is false, jump out of loop + + whileNode.block.accept(this); + methodVisitor.visitJumpInsn(GOTO, loopLabel); + + methodVisitor.visitLabel(endOfLoopLabel); } @Override public void visit(DecrementNode decrementNode) { + switch (decrementNode.crementType) { + case PREFIX: // --i + if(decrementNode.assignableExpression.memberAccess == null) { // local Var + methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1); + } else { // Field or var from other object + } + break; + case SUFFIX: // i-- + if(decrementNode.assignableExpression.memberAccess == null) { // local Var + methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1); + } else { // Field or var from other object + + } + break; + } } @Override public void visit(IncrementNode incrementNode) { + switch (incrementNode.crementType) { + case PREFIX: // ++i + if(incrementNode.assignableExpression.memberAccess == null) { // local Var + methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1); + methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier)); + } else { // Field or var from other object + + } + break; + case SUFFIX: // i++ + if(incrementNode.assignableExpression.memberAccess == null) { // local Var + methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier)); + methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1); + } else { // Field or var from other object + + } + break; + } } @Override @@ -413,9 +456,4 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { public void visit(TargetNode targetNode) { } - - @Override - public void visit(AssignableNode assignableNode) { - - } } diff --git a/src/main/java/bytecode/visitor/MethodVisitor.java b/src/main/java/bytecode/visitor/MethodVisitor.java index 1108a86..b1d3957 100644 --- a/src/main/java/bytecode/visitor/MethodVisitor.java +++ b/src/main/java/bytecode/visitor/MethodVisitor.java @@ -53,7 +53,6 @@ public interface MethodVisitor { void visit(MethodCallNode methodCallNode); void visit(TargetNode targetNode); - void visit(AssignableNode assignableNode); void visit(AssignNode assignNode); void visit(NewDeclarationNode newDeclarationNode);