Implemented CodeGen for Constants and fixed an issue regarding the placements of local variables on the JVM stack.
( index JVM = index localVars + 1)
This commit is contained in:
parent
c764b710ea
commit
87e863e773
@ -28,7 +28,7 @@ public class Compiler {
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
|
||||
Path filePath = Paths.get("src/main/java/Input.java");
|
||||
Path filePath = Paths.get("src/main/java/InputTest.java");
|
||||
|
||||
|
||||
// todo remove this debug info
|
||||
|
@ -5,6 +5,7 @@ import TypeCheck.TypeCheckException;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -26,7 +27,11 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
if (value){
|
||||
mv.visitInsn(Opcodes.ICONST_1);
|
||||
} else {
|
||||
mv.visitInsn(Opcodes.ICONST_0);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
|
@ -4,6 +4,7 @@ import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -25,7 +26,7 @@ public class CharConstantExpression extends AbstractType implements IExpression{
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
mv.visitIntInsn(Opcodes.BIPUSH, (int) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,6 +4,7 @@ import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -27,7 +28,8 @@ public class IntConstantExpression extends AbstractType implements IExpression{
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
//TODO: When we are finished this can be done more efficiently
|
||||
mv.visitLdcInsn(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -45,12 +45,12 @@ public class LocalVarIdentifier extends AbstractType implements IExpression{
|
||||
throw new Exception("Variable " + identifier + " not declared");
|
||||
}
|
||||
|
||||
// Load the variable onto the stack
|
||||
// Find the index of the variable
|
||||
int index = -1;
|
||||
int counter = 0;
|
||||
for (String key : localVars.keySet()){
|
||||
if (key.equals(identifier)){
|
||||
index = counter;
|
||||
index = counter+1; // +1 because the first local variable is at index 1, 0 is used for "this"
|
||||
break;
|
||||
}
|
||||
counter++;
|
||||
|
@ -50,7 +50,6 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
mv.visitInsn(Opcodes.IRETURN);
|
||||
} else {
|
||||
mv.visitInsn(Opcodes.ARETURN);
|
||||
|
||||
}
|
||||
} else {
|
||||
mv.visitInsn(Opcodes.RETURN);
|
||||
|
Loading…
Reference in New Issue
Block a user