Compare commits

...

3 Commits

Author SHA1 Message Date
Jochen Seyfried
90936affb9 Changed "bool" to "boolean" 2024-06-22 16:39:29 +02:00
Jochen Seyfried
9ff069827a Fixed Bugs in bytecode generation 2024-06-22 16:33:53 +02:00
Jochen Seyfried
87e863e773 Implemented CodeGen for Constants and fixed an issue regarding the placements of local variables on the JVM stack.
( index JVM = index localVars + 1)
2024-06-22 12:30:06 +02:00
11 changed files with 25 additions and 18 deletions

View File

@ -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

View File

@ -24,7 +24,7 @@ public class TypeCheckHelper {
}
public static boolean typeExists(String type, List<String> customTypeslist) {
if(type.equals("int") || type.equals("bool") || type.equals("char")){
if(type.equals("int") || type.equals("boolean") || type.equals("char")){
return true;
}
return customTypeslist.contains(type);

View File

@ -125,7 +125,7 @@ public class MethodDecl implements Node {
for (Parameter param : parameters.parameterList) {
switch (param.type) {
case "int" -> descriptor.append("I");
case "bool" -> descriptor.append("Z");
case "boolean" -> descriptor.append("Z");
case "char" -> descriptor.append("C");
case "void" -> descriptor.append("V");
default -> {
@ -144,11 +144,12 @@ public class MethodDecl implements Node {
} else {
switch (returnType) {
case "int" -> descriptor.append("I");
case "bool" -> descriptor.append("Z");
case "boolean" -> descriptor.append("Z");
case "char" -> descriptor.append("C");
case "void" -> descriptor.append("V");
default -> {
// object
// methodContext (class, (returnType, (identifier, parameter)))
HashMap<String, HashMap<String, ParameterList>> classMethods = methodContext.get(classThatContainsMethod);
HashMap<String, ParameterList> methodDetails = classMethods.get(name);

View File

@ -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() {

View File

@ -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

View File

@ -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

View File

@ -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++;
@ -65,7 +65,7 @@ public class LocalVarIdentifier extends AbstractType implements IExpression{
case "int":
mv.visitVarInsn(Opcodes.ILOAD, index);
break;
case "bool":
case "boolean":
mv.visitVarInsn(Opcodes.ILOAD, index);
break;
case "void":

View File

@ -51,7 +51,7 @@ public class LocalVarDecl extends AbstractType implements IStatement{
mv.visitInsn(Opcodes.ICONST_0);
mv.visitVarInsn(Opcodes.ISTORE, index);
break;
case "bool":
case "boolean":
mv.visitInsn(Opcodes.ICONST_0);
mv.visitVarInsn(Opcodes.ISTORE, index);
break;

View File

@ -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);

View File

@ -47,7 +47,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
String upperbound = helper.upperBound(leftType.type, rightType.type);
if(Objects.equals(leftType.type, "boolean"))
leftType.type = "bool";
leftType.type = "boolean";
if (!Objects.equals(upperbound, leftType.type)) {
throw new TypeCheckException("The upper bound of assignment is not the left type.");
}
@ -78,7 +78,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
int counter = 0;
for (String key : localVars.keySet()) {
if (key.equals(varName)) {
index = counter;
index = counter+1;
break;
}
counter++;
@ -90,8 +90,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
String type = localVars.get(localVar.getIdentifier());
switch (type) {
case "int":
case "bool":
case "int", "char", "boolean":
mv.visitVarInsn(Opcodes.ISTORE, index);
break;
default:
@ -117,7 +116,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
case "int":
descriptor.append("I");
break;
case "bool":
case "boolean":
descriptor.append("Z");
break;
default:

View File

@ -66,7 +66,7 @@ public class NewStatementExpression extends AbstractType implements IExpression,
case "int":
descriptor.append("I");
break;
case "bool":
case "boolean":
descriptor.append("Z");
break;
case "char":