code-generator #13
@ -19,9 +19,11 @@ import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
|||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.BaseType;
|
import ast.type.type.BaseType;
|
||||||
|
import ast.type.type.TypeEnum;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -51,14 +53,27 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
methodVisitor =
|
methodVisitor =
|
||||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType),
|
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType),
|
||||||
"<init>",
|
"<init>",
|
||||||
"()V",
|
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
|
||||||
null,
|
null,
|
||||||
null);
|
null);
|
||||||
|
|
||||||
methodVisitor.visitCode();
|
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.visitVarInsn(ALOAD, 0);
|
||||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||||
|
|
||||||
|
// Visit all statements
|
||||||
|
for (IStatementNode statementNode : constructorNode.block.statements) {
|
||||||
|
statementNode.accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
methodVisitor.visitInsn(RETURN);
|
methodVisitor.visitInsn(RETURN);
|
||||||
methodVisitor.visitMaxs(1, 1);
|
methodVisitor.visitMaxs(1, localVaribales.size());
|
||||||
methodVisitor.visitEnd();
|
methodVisitor.visitEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +183,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
switch (nonCalculationNode.operator) {
|
switch (nonCalculationNode.operator) {
|
||||||
case AND:
|
case AND:
|
||||||
nonCalculationNode.unaryExpression.accept(this);
|
nonCalculationNode.unaryExpression.accept(this);
|
||||||
methodVisitor.visitJumpInsn(IFEQ, labelFalse);
|
methodVisitor.visitJumpInsn(IFEQ, labelFalse); // Value compared to 0 (false)
|
||||||
nonCalculationNode.expression.accept(this);
|
nonCalculationNode.expression.accept(this);
|
||||||
methodVisitor.visitJumpInsn(IFEQ, labelFalse);
|
methodVisitor.visitJumpInsn(IFEQ, labelFalse);
|
||||||
break;
|
break;
|
||||||
@ -240,7 +255,22 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(NotNode notNode) {
|
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
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user