Compare commits

..

No commits in common. "3628a0a4d8bfbb18e8e8acdd137a8acb532492b6" and "97aadb9ba8303119dcb9681f5e588e21437d4e62" have entirely different histories.

View File

@ -10,7 +10,6 @@ import ast.members.MainMethodNode;
import ast.members.MethodNode; import ast.members.MethodNode;
import ast.parameters.ParameterNode; import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode; import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.NewDeclarationNode; import ast.statementexpressions.NewDeclarationNode;
import ast.statementexpressions.crementexpressions.CrementType; import ast.statementexpressions.crementexpressions.CrementType;
import ast.statementexpressions.crementexpressions.DecrementNode; import ast.statementexpressions.crementexpressions.DecrementNode;
@ -305,8 +304,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(UnaryNode unaryNode) { public void visit(UnaryNode unaryNode) {
if (unaryNode.thisExp != null) { if (unaryNode.thisExp != null) {
methodVisitor.visitVarInsn(ALOAD, 0); // this methodVisitor.visitVarInsn(ALOAD, 0); // this
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} else if (unaryNode.memberAccess != null) { } else if (unaryNode.memberAccess != null) {
unaryNode.memberAccess.setTypeNode(unaryNode.getType());
unaryNode.memberAccess.accept(this); unaryNode.memberAccess.accept(this);
} else if (unaryNode.value != null) { } else if (unaryNode.value != null) {
unaryNode.value.accept(this); unaryNode.value.accept(this);
@ -316,8 +316,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
unaryNode.statement.accept(this); unaryNode.statement.accept(this);
} else if (unaryNode.expression != null) { } else if (unaryNode.expression != null) {
unaryNode.expression.accept(this); unaryNode.expression.accept(this);
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} }
} }
@ -326,7 +324,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(BlockNode blockNode) { public void visit(BlockNode blockNode) {
for (IStatementNode statementNode : blockNode.statements) { for(IStatementNode statementNode : blockNode.statements) {
statementNode.accept(this); statementNode.accept(this);
} }
} }
@ -382,20 +380,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (localVariableDeclarationNode.expression != null) { if (localVariableDeclarationNode.expression != null) {
// Process expression // Process expression
localVariableDeclarationNode.expression.accept(this); localVariableDeclarationNode.expression.accept(this);
if (localVariableDeclarationNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) localVariableDeclarationNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.type;
loadInrement(incrementNode);
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.type;
loadDecrement(decrementNode);
}
}
// add local var to list if not in list // add local var to list if not in list
if (!localVariables.contains(localVariableDeclarationNode.identifier)) { if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
localVariables.add(localVariableDeclarationNode.identifier); localVariables.add(localVariableDeclarationNode.identifier);
@ -416,37 +400,24 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(AssignNode assignNode) { public void visit(AssignNode assignNode) {
if (assignNode.assignable.memberAccess != null) { // this / object if (assignNode.assignable.memberAccess != null) { // this / object
assignNode.assignable.memberAccess.setTypeNode(assignNode.assignable.typeNode); if (assignNode.expression instanceof IncrementNode) {
if (assignNode.expression instanceof UnaryNode) { if (((IncrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) { // ++i
UnaryNode unaryNode = (UnaryNode) assignNode.expression; fieldOrObjectVarPrefixCrementAssign(assignNode);
if (unaryNode.statement instanceof IncrementNode) { } else { // i++
IncrementNode incrementNode = (IncrementNode) unaryNode.statement; fieldOrObjectVarSuffixCrementAssign(assignNode);
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i }
incrementNode.accept(this); // crement } else if (assignNode.expression instanceof DecrementNode) {
fieldOrObjectVarCrementAssign(assignNode); // assign if (((DecrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) {
} else { // i++ fieldOrObjectVarPrefixCrementAssign(assignNode);
fieldOrObjectVarCrementAssign(assignNode); // assign } else {
incrementNode.accept(this); // crement fieldOrObjectVarSuffixCrementAssign(assignNode);
}
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this); // crement
fieldOrObjectVarCrementAssign(assignNode); // assign
} else {
fieldOrObjectVarCrementAssign(assignNode); // assign
decrementNode.accept(this); // crement
}
} }
} else { } else {
assignFieldOrObjectVar(assignNode); assignFieldOrObjectVar(assignNode);
} }
} else { // local var } else { // local var
if (assignNode.expression instanceof UnaryNode) { if (assignNode.expression instanceof IncrementNode || assignNode.expression instanceof DecrementNode) {
UnaryNode unaryNode = (UnaryNode) assignNode.expression; localVarCrementAssign(assignNode);
if (unaryNode.statement instanceof IncrementNode || unaryNode.statement instanceof DecrementNode) {
localVarCrementAssign(assignNode);
}
} else { } else {
assignNode.expression.accept(this); assignNode.expression.accept(this);
assignLocalVar(assignNode); assignLocalVar(assignNode);
@ -454,37 +425,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} }
} }
private void loadInrement(IncrementNode incrementNode) {
AssignableNode assignableNode = incrementNode.assignableExpression;
if (assignableNode.memberAccess != null) {
assignableNode.memberAccess.accept(this);
} else { // TODO: typeNode incrementNode.assignableExpression = null, erledigt
if (assignableNode.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
} else if (assignableNode.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
}
}
}
private void loadDecrement(DecrementNode decrementNode) {
AssignableNode assignableNode = decrementNode.assignableExpression;
if (assignableNode.memberAccess != null) {
assignableNode.memberAccess.accept(this);
} else { // TODO: typeNode decrementNode.assignableExpression = null, erledigt
if (assignableNode.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
} else if (assignableNode.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
}
}
}
private void assignLocalVar(AssignNode assignNode) { private void assignLocalVar(AssignNode assignNode) {
if (!localVariables.contains(assignNode.assignable.identifier)) { if (!localVariables.contains(assignNode.assignable.identifier)) {
localVariables.add(assignNode.assignable.identifier); localVariables.add(assignNode.assignable.identifier);
} }
if (assignNode.expression.getType() instanceof BaseType) { if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier)); methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
} else if (assignNode.expression.getType() instanceof ReferenceType) { } else if (assignNode.expression.getType() instanceof ReferenceType) {
@ -500,6 +444,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} else { } else {
localVariables.add(assignNode.assignable.identifier); localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
} }
assignNode.expression.accept(this); assignNode.expression.accept(this);
@ -513,56 +458,41 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} }
private void localVarCrementAssign(AssignNode assignNode) { private void localVarCrementAssign(AssignNode assignNode) {
UnaryNode unaryNode = (UnaryNode) assignNode.expression; if (assignNode.expression instanceof IncrementNode) {
if (unaryNode.statement instanceof IncrementNode) { IncrementNode incrementNode = (IncrementNode) assignNode.expression;
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
incrementNode.accept(this); incrementNode.accept(this);
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement(incrementNode);
assignLocalVar(assignNode); assignLocalVar(assignNode);
} else { // i++ } else { // i++
incrementNode.assignableExpression.typeNode = unaryNode.getType(); loadBeforeCrement(assignNode);
loadInrement(incrementNode);
assignLocalVar(assignNode); assignLocalVar(assignNode);
incrementNode.accept(this); incrementNode.accept(this);
} }
} else if (unaryNode.statement instanceof DecrementNode) { } else if (assignNode.expression instanceof DecrementNode decrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if (decrementNode.crementType.equals(CrementType.PREFIX)) { if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this); decrementNode.accept(this);
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
assignLocalVar(assignNode); assignLocalVar(assignNode);
} else { } else {
decrementNode.assignableExpression.typeNode = unaryNode.getType(); loadBeforeCrement(assignNode);
loadDecrement(decrementNode);
assignLocalVar(assignNode); assignLocalVar(assignNode);
decrementNode.accept(this); decrementNode.accept(this);
} }
} }
} }
private void fieldOrObjectVarCrementAssign(AssignNode assignNode) { private void fieldOrObjectVarPrefixCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier); int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if (localVarIndex >= 0) { // object if(localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex); methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if (assignNode.assignable.memberAccess.thisExpr) { // field } else if(assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} else { } else {
localVariables.add(assignNode.assignable.identifier); localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
} }
UnaryNode unaryNode = (UnaryNode) assignNode.expression; assignNode.expression.accept(this);
if (unaryNode.statement instanceof IncrementNode) { methodVisitor.visitInsn(DUP_X1);
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement((incrementNode));
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
if (assignNode.expression.getType() instanceof BaseType) { if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType())); methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
@ -572,6 +502,55 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} }
} }
private void fieldOrObjectVarSuffixCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if(localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
loadBeforeCrement(assignNode);
if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
} else if (assignNode.expression.getType() instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
}
assignNode.expression.accept(this);
}
private void loadBeforeCrement(AssignNode assignNode) {
if(assignNode.expression instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
if(incrementNode.assignableExpression.memberAccess != null) {
incrementNode.assignableExpression.memberAccess.accept(this);
} else {
if(assignNode.assignable.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
}
} else if(assignNode.expression instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
if(decrementNode.assignableExpression.memberAccess != null) {
decrementNode.assignableExpression.memberAccess.accept(this);
} else {
if(assignNode.assignable.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
}
}
}
@Override @Override
public void visit(IncrementNode incrementNode) { public void visit(IncrementNode incrementNode) {
if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field
@ -586,6 +565,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) { } else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";")); methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
} }
methodVisitor.visitInsn(DUP);
methodVisitor.visitInsn(ICONST_1); methodVisitor.visitInsn(ICONST_1);
methodVisitor.visitInsn(IADD); methodVisitor.visitInsn(IADD);
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) { if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
@ -607,6 +587,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // this } else { // this
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} }
methodVisitor.visitInsn(DUP);
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) { if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode())); methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) { } else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
@ -679,21 +660,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // Return something } else { // Return something
// Process expression // Process expression
returnNode.expression.accept(this); returnNode.expression.accept(this);
if (returnNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) returnNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement(incrementNode);
}
if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
}
// Return result of expression // Return result of expression
if (returnNode.expression.getType() instanceof BaseType) { if (returnNode.expression.getType() instanceof BaseType) {
methodVisitor.visitInsn(IRETURN); methodVisitor.visitInsn(IRETURN);