Changed the order of arguments
This commit is contained in:
parent
9fa9dfdfb6
commit
21dff015b5
@ -67,7 +67,7 @@ public class BinaryExpression extends AbstractType implements IExpression{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
// Label for the jump instruction
|
||||
Label operationFalse = new Label(); //Operation is false
|
||||
Label operationTrue = new Label(); //Operation is true
|
||||
@ -77,88 +77,88 @@ public class BinaryExpression extends AbstractType implements IExpression{
|
||||
// Bytecode for the binary operation
|
||||
switch (operator) {
|
||||
case "&&":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // IFEQ --> "if equals to zero" (false) --> if left exp is false
|
||||
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // If right exp is false, jump to the end of the whole expression
|
||||
|
||||
mv.visitJumpInsn(Opcodes.GOTO, operationTrue); // If it reaches this point, the right exp is true
|
||||
break;
|
||||
|
||||
case "||":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
mv.visitJumpInsn(Opcodes.IFNE, operationTrue); // IFNE --> "if not equals to zero" (true) --> if left exp is true
|
||||
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitJumpInsn(Opcodes.IFNE, operationTrue);
|
||||
break;
|
||||
|
||||
case "==":
|
||||
// Keep in mind that only primitive types are allowed in this case (at this time)
|
||||
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression
|
||||
break;
|
||||
|
||||
case "<":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPLT, operationTrue); // Checks only on less than, not equal
|
||||
break;
|
||||
|
||||
case ">":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPGT, operationTrue); // Checks only on greater than, not equal
|
||||
break;
|
||||
|
||||
case "<=":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPLE, operationTrue); // Checks on less than OR equal
|
||||
break;
|
||||
|
||||
case ">=":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPGE, operationTrue); // Checks on greater than OR equal
|
||||
break;
|
||||
|
||||
case "!=":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IF_ICMPNE, operationTrue); // Checks on not equal
|
||||
break;
|
||||
|
||||
case "+":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitInsn(Opcodes.IADD);
|
||||
break;
|
||||
|
||||
case "-":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitInsn(Opcodes.ISUB);
|
||||
break;
|
||||
|
||||
case "*":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitInsn(Opcodes.IMUL);
|
||||
break;
|
||||
|
||||
case "/":
|
||||
left.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
left.codeGen(mv, localVars, typeContext);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
mv.visitInsn(Opcodes.IDIV);
|
||||
break;
|
||||
|
||||
|
@ -14,5 +14,5 @@ public interface IExpression extends Node {
|
||||
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception;
|
||||
|
||||
// visit method for code generation
|
||||
void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception;
|
||||
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class InstVarExpression implements IExpression{
|
||||
|
||||
@Override
|
||||
// typeContext: (ClassName, (FieldName, FieldType))
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
// Load "this" onto the stack
|
||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class LocalVarIdentifier implements IExpression{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
// Check if the variable is in the list of local variables
|
||||
String type = localVars.get(identifier);
|
||||
if (type == null){
|
||||
|
@ -50,7 +50,7 @@ public class UnaryExpression extends AbstractType implements IExpression{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
operand.codeGen(mv);
|
||||
|
||||
|
@ -51,7 +51,7 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
||||
Label conditionFalse = new Label();
|
||||
Label statementEnd = new Label();
|
||||
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
condition.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
|
||||
ifStatement.codeGen(mv, blockLocalVars, typeContext); //If the condition is true, execute the ifBlock
|
||||
|
@ -43,7 +43,7 @@ public class IfStatement extends AbstractType implements IStatement{
|
||||
|
||||
Label conditionFalse = new Label();
|
||||
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
condition.codeGen(mv, localVars, typeContext);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
|
||||
ifStatement.codeGen(mv, blockLocalVars, typeContext);
|
||||
|
@ -38,7 +38,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
if (expression != null) {
|
||||
expression.codeGen(mv, typeContext, localVars);
|
||||
expression.codeGen(mv, localVars, typeContext);
|
||||
//Get the Type of the expression
|
||||
//TODO: Resolve how do we get the type of the expression
|
||||
String type = expression.typeCheck(null, null, null).type;
|
||||
|
@ -45,7 +45,7 @@ public class WhileStatement extends AbstractType implements IStatement {
|
||||
|
||||
mv.visitLabel(LoopStart);
|
||||
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
condition.codeGen(mv, localVars, typeContext);
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
|
||||
|
||||
statement.codeGen(mv, blockLocalVars, typeContext);
|
||||
|
@ -12,6 +12,7 @@ import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AssignStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
public String operator;
|
||||
@ -50,25 +51,17 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
if (left instanceof VarRefExpression varRef) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public TypeCheckResult typeCheck() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
//TODO: Do we need the value on the stack after assigning it?
|
||||
//TODO: WE do not differentiate between InstanceVar and FieldVar
|
||||
|
||||
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
|
||||
right.codeGen(mv, typeContext, localVars);
|
||||
right.codeGen(mv, localVars, typeContext);
|
||||
|
||||
if (left instanceof LocalVarIdentifier) {
|
||||
LocalVarIdentifier localVar = (LocalVarIdentifier) left;
|
||||
@ -105,15 +98,18 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
||||
|
||||
// Load "this" onto the stack
|
||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||
}
|
||||
/*
|
||||
if (left instanceof VarRefExpression varRef) {
|
||||
//TODO: Implement the handling of a variable reference --> I need a list of local variables
|
||||
// for that to determine if the variable is a local or field variable
|
||||
} else if (left instanceof InstVarExpression instVar) {
|
||||
mv.visitInsn(Opcodes.DUP_X1);
|
||||
|
||||
// if (left instanceof VarRefExpression varRef) {
|
||||
// //TODO: Implement the handling of a variable reference --> I need a list of local variables
|
||||
// // for that to determine if the variable is a local or field variable
|
||||
// } else if (left instanceof InstVarExpression instVar) {
|
||||
// mv.visitInsn(Opcodes.DUP_X1);
|
||||
//
|
||||
// // We now again need the owner (class reference), name (of the Field in the owner) and type of the field
|
||||
// //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
|
||||
// }
|
||||
// We now again need the owner (class reference), name (of the Field in the owner) and type of the field
|
||||
//mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
@ -51,11 +51,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
//Errors occur due to the change in parameter in the RefType class
|
||||
// I need the methodContext here to get the method descriptor
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
//Generate Bytecode for the receiver
|
||||
if(classThatHasTheMethodIfNotThis != null){
|
||||
//TODO: classThatHasTheMethodIfNotThis must be an object --> instance of the class not the class itself
|
||||
@ -66,10 +65,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
for (MethodDecl methodDecl : methodDecls) {
|
||||
if (methodDecl.name.equals(methodName)) {
|
||||
//Get the method descriptor
|
||||
descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||
//descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||
}
|
||||
}
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
|
||||
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
|
||||
|
||||
} else {
|
||||
// Load this onto the stack
|
||||
@ -77,7 +76,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
}
|
||||
|
||||
for (IExpression argument : arguments) {
|
||||
argument.codeGen(mv, typeContext, localVars);
|
||||
argument.codeGen(mv, localVars, typeContext);
|
||||
}
|
||||
|
||||
// Get the method descriptor
|
||||
@ -86,9 +85,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
for (MethodDecl methodDecl : methodDecls) {
|
||||
if (methodDecl.name.equals(methodName)) {
|
||||
//Get the method descriptor
|
||||
descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||
//descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||
}
|
||||
}
|
||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
|
||||
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import abstractSyntaxTree.Statement.IStatement;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
@ -19,12 +20,12 @@ public class NewStatementExpression extends AbstractType implements IExpression,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
throw new Exception("CodeGen not implemented for NewStatementExpression");
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user