Bytecode for loops
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
70a3788030
commit
c5fc378eed
@ -23,9 +23,4 @@ public class AssignableNode implements IStatementExpressionNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(MethodVisitor methodVisitor) {
|
|
||||||
methodVisitor.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
statementNode.accept(this);
|
statementNode.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
methodVisitor.visitInsn(RETURN);
|
|
||||||
methodVisitor.visitMaxs(0, 0);
|
methodVisitor.visitMaxs(0, 0);
|
||||||
methodVisitor.visitEnd();
|
methodVisitor.visitEnd();
|
||||||
}
|
}
|
||||||
@ -165,13 +164,13 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
public void visit(DotSubstractionNode dotSubstractionNode) {
|
public void visit(DotSubstractionNode dotSubstractionNode) {
|
||||||
if (dotSubstractionNode.value != null) {
|
if (dotSubstractionNode.value != null) {
|
||||||
dotSubstractionNode.value.accept(this);
|
dotSubstractionNode.value.accept(this);
|
||||||
} else if(dotSubstractionNode.identifier != null) {
|
} else if (dotSubstractionNode.identifier != null) {
|
||||||
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier));
|
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier));
|
||||||
} else if(dotSubstractionNode.memberAccess != null) {
|
} else if (dotSubstractionNode.memberAccess != null) {
|
||||||
dotSubstractionNode.memberAccess.accept(this);
|
dotSubstractionNode.memberAccess.accept(this);
|
||||||
} else if(dotSubstractionNode.methodCall != null) {
|
} else if (dotSubstractionNode.methodCall != null) {
|
||||||
dotSubstractionNode.methodCall.accept(this);
|
dotSubstractionNode.methodCall.accept(this);
|
||||||
} else if(dotSubstractionNode.calculationExpression != null) {
|
} else if (dotSubstractionNode.calculationExpression != null) {
|
||||||
dotSubstractionNode.calculationExpression.accept(this);
|
dotSubstractionNode.calculationExpression.accept(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -250,7 +249,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MemberAccessNode memberAccessNode) {
|
public void visit(MemberAccessNode memberAccessNode) {
|
||||||
if(memberAccessNode.thisExpr) {
|
if (memberAccessNode.thisExpr) {
|
||||||
//methodVisitor.visitFieldInsn(PUTFIELD);
|
//methodVisitor.visitFieldInsn(PUTFIELD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -339,17 +338,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
public void visit(ValueNode valueNode) {
|
public void visit(ValueNode valueNode) {
|
||||||
switch (valueNode.valueType) {
|
switch (valueNode.valueType) {
|
||||||
case INT_VALUE:
|
case INT_VALUE:
|
||||||
int intValue = Integer.parseInt(valueNode.value);
|
int intValue = Integer.parseInt(valueNode.value);
|
||||||
if(intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE ) { // load int as byte
|
if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) { // load int as byte
|
||||||
methodVisitor.visitIntInsn(BIPUSH, intValue);
|
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);
|
methodVisitor.visitIntInsn(SIPUSH, intValue);
|
||||||
} else { // load int as const
|
} else { // load int as const
|
||||||
methodVisitor.visitLdcInsn(intValue);
|
methodVisitor.visitLdcInsn(intValue);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BOOLEAN_VALUE:
|
case BOOLEAN_VALUE:
|
||||||
if(valueNode.value.equals("true")) {
|
if (valueNode.value.equals("true")) {
|
||||||
methodVisitor.visitInsn(ICONST_1);
|
methodVisitor.visitInsn(ICONST_1);
|
||||||
} else {
|
} else {
|
||||||
methodVisitor.visitInsn(ICONST_0);
|
methodVisitor.visitInsn(ICONST_0);
|
||||||
@ -357,7 +356,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
break;
|
break;
|
||||||
case CHAR_VALUE:
|
case CHAR_VALUE:
|
||||||
char charValue = valueNode.value.charAt(0);
|
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);
|
methodVisitor.visitIntInsn(BIPUSH, charValue);
|
||||||
} else if (charValue <= Short.MAX_VALUE) { // load char as short
|
} else if (charValue <= Short.MAX_VALUE) { // load char as short
|
||||||
methodVisitor.visitIntInsn(SIPUSH, charValue);
|
methodVisitor.visitIntInsn(SIPUSH, charValue);
|
||||||
@ -386,17 +385,61 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(WhileNode whileNode) {
|
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
|
@Override
|
||||||
public void visit(DecrementNode decrementNode) {
|
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
|
@Override
|
||||||
public void visit(IncrementNode incrementNode) {
|
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
|
@Override
|
||||||
@ -413,9 +456,4 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
public void visit(TargetNode targetNode) {
|
public void visit(TargetNode targetNode) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(AssignableNode assignableNode) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,6 @@ public interface MethodVisitor {
|
|||||||
void visit(MethodCallNode methodCallNode);
|
void visit(MethodCallNode methodCallNode);
|
||||||
void visit(TargetNode targetNode);
|
void visit(TargetNode targetNode);
|
||||||
|
|
||||||
void visit(AssignableNode assignableNode);
|
|
||||||
void visit(AssignNode assignNode);
|
void visit(AssignNode assignNode);
|
||||||
void visit(NewDeclarationNode newDeclarationNode);
|
void visit(NewDeclarationNode newDeclarationNode);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user