Merge remote-tracking branch 'origin/master'

This commit is contained in:
Krauß, Josefine 2024-05-08 13:51:13 +02:00
commit 0f61e843a4
18 changed files with 51 additions and 48 deletions

View File

@ -17,7 +17,7 @@ public class BoolDatatype extends AbstractType implements IDatatype{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
if(value) {
mv.visitInsn(Opcodes.ICONST_1); // Pushes the int 1 on the stack (true)
} else {

View File

@ -17,7 +17,7 @@ public class CharDatatype extends AbstractType implements IDatatype{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
// Possible use of BIPUSH and SIPUSH if the value is small enough
//This saves space in the bytecode which is not very relevant at this point, but could be implemented anyway

View File

@ -9,7 +9,7 @@ public interface IDatatype {
// visit method for code generation
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}
//TODO: Check if we need to differentiate between primitive types and reference types --> for example in "=="

View File

@ -21,7 +21,7 @@ public class IntDatatype extends AbstractType implements IDatatype{
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
//Example of using BIPUSH and SIPUSH for optimizing bytecode size
if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)

View File

@ -6,6 +6,7 @@ import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Program;
import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List;
@ -50,7 +51,7 @@ public class RefType extends AbstractType implements IDatatype {
// Method for code generation which iterates over all the field declarations
// and method declarations and calls their CodeGen methods
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new ExecutionControl.NotImplementedException("CodeGen not implemented for RefType");
}

View File

@ -59,7 +59,7 @@ public class BinaryExpression extends AbstractType implements IExpression{
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
// Label for the jump instruction
Label operationFalse = new Label(); //Operation is false
Label operationTrue = new Label(); //Operation is true
@ -69,88 +69,88 @@ public class BinaryExpression extends AbstractType implements IExpression{
// Bytecode for the binary operation
switch (operator) {
case "&&":
left.CodeGen(mv);
left.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // IFEQ --> "if equals to zero" (false) --> if left exp is false
right.CodeGen(mv);
right.codeGen(mv);
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);
left.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFNE, operationTrue); // IFNE --> "if not equals to zero" (true) --> if left exp is true
right.CodeGen(mv);
right.codeGen(mv);
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);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression
break;
case "<":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPLT, operationTrue); // Checks only on less than, not equal
break;
case ">":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPGT, operationTrue); // Checks only on greater than, not equal
break;
case "<=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPLE, operationTrue); // Checks on less than OR equal
break;
case ">=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPGE, operationTrue); // Checks on greater than OR equal
break;
case "!=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPNE, operationTrue); // Checks on not equal
break;
case "+":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IADD);
break;
case "-":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.ISUB);
break;
case "*":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IMUL);
break;
case "/":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IDIV);
break;

View File

@ -8,5 +8,5 @@ public interface IExpression {
TypeCheckResult typeCheck() throws Exception;
// visit method for code generation
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}

View File

@ -22,7 +22,7 @@ public class InstVarExpression implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new ExecutionControl.NotImplementedException("CodeGen not implemented for InstVarExpression");
//ALOAD the index of the var

View File

@ -42,9 +42,9 @@ public class UnaryExpression extends AbstractType implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
operand.CodeGen(mv);
operand.codeGen(mv);
switch (operator) {
case "!":

View File

@ -17,7 +17,7 @@ public class VarRefExpression implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new Exception("CodeGen not implemented for VarRefExpression");
}
}

View File

@ -3,6 +3,7 @@ package abstractSyntaxTree;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Datatype.RefType;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List;

View File

@ -2,6 +2,7 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import org.objectweb.asm.*;
import java.util.HashMap;
import java.util.List;
@ -40,9 +41,9 @@ public class BlockStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
for (IStatement statement : statements) {
statement.CodeGen(mv); //TODO: I think we need to pass the symbol table here
statement.codeGen(mv); //TODO: I think we need to pass the symbol table here
}
}
}

View File

@ -13,7 +13,7 @@ public class EmptyStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
//An empty statement does not generate any code
}
}

View File

@ -9,5 +9,5 @@ public interface IStatement {
TypeCheckResult typeCheck() throws Exception;
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}

View File

@ -38,7 +38,7 @@ public class IfElseStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
Label statementEnd = new Label();
@ -46,11 +46,11 @@ public class IfElseStatement extends AbstractType implements IStatement{
condition.CodeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.CodeGen(mv); //If the condition is true, execute the ifBlock
ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock
mv.visitJumpInsn(Opcodes.GOTO, statementEnd); //Jump to the end of the if-else statement
mv.visitLabel(conditionFalse);
elseStatement.CodeGen(mv); //If the condition is false, execute the elseBlock
elseStatement.codeGen(mv); //If the condition is false, execute the elseBlock
mv.visitLabel(statementEnd); //End of the if-else statement

View File

@ -31,14 +31,14 @@ public class IfStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
condition.CodeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.CodeGen(mv);
ifStatement.codeGen(mv);
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
}

View File

@ -31,7 +31,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
// This is a problem at "BinaryExpression" and here because we need to know the type to return
// At this point in time we can either return reference types or have an error message
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
if (expression != null) {
expression.CodeGen(mv);

View File

@ -31,16 +31,16 @@ public class WhileStatement extends AbstractType implements IStatement {
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
Label LoopStart = new Label();
mv.visitLabel(LoopStart);
condition.CodeGen(mv);
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
statement.CodeGen(mv);
statement.codeGen(mv);
//TODO: If the block ends with a return statement, we might have to pop it from the stack
// So the next iteration starts with a clean stack
mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop