diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index 96d430d..dd34045 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -47,16 +47,16 @@ public class Main { // Pretty-print the program (to debug parsing) System.err.println("todo " + ast); - System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); + System.out.println("\n\n PRETTYPRINTER VSK : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); // Verify the program semantic // Generate the intermediate representation System.out.println("todo"); - //ProgramLLVMImpl astLLVM = ast.toLLVM(); + ProgramLLVMImpl astLLVM = ast.toLLVM(); - //System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); + System.out.println("\n\n PRETTYPRINTER LLBD : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index e920987..d072b61 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -1,5 +1,8 @@ package TP2.asd; +import java.util.ArrayList; +import java.util.List; + import TP2.asd.Program.*; import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; @@ -31,7 +34,7 @@ public interface Interface{ public interface Instruction { public S accept(InstrVisitor v, H h); public String prettyprinter(String indent); - public InstructionLLVM toLLVM(); + public ArrayList toLLVM(); } public interface InstrVisitor{ @@ -45,7 +48,7 @@ public interface Interface{ public interface Expression { public S accept(ExprVisitor v, H h); public String prettyprinter(); - public DefineLLVM toLLVM(); + public ArrayList toLLVM(); } public interface ExprVisitor { @@ -55,6 +58,7 @@ public interface Interface{ public interface Type{ public String prettyprinter(); + public TypeLLVM toLLVM(); } public enum Op {PLUS, MINUS, TIMES, DIV, MOD} diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index 9c80a2e..ea26c6c 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -1,6 +1,7 @@ package TP2.asd; import java.util.ArrayList; +import java.util.List; import java.util.Map; import TP2.asd.Interface.*; import TP2.llvm.ProgramLLVM.*; @@ -36,6 +37,7 @@ public class Program{ } + public static record FunctionImp(Type type, String nom, ArrayList instructions)implements Function { public FunctionImp(Type type, String name, Instruction instruction) { this(type, name, new ArrayList<>() {{ add(instruction); }}); @@ -58,13 +60,13 @@ public class Program{ public DefineLLVM toLLVM() { ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i S accept(ExprVisitor v, H h) { return v.visitConst(this, h); @@ -75,12 +77,16 @@ public class Program{ } @Override - public DefineLLVM toLLVM() { + public ArrayList toLLVM() { //TODO // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); + ArrayList list = new ArrayList<>(); + ConstLLVMImp cLLVM = new ConstLLVMImp(new IntLLVMImpl(),c); + list.add(new AssignLVMImp(new VarLLVMImpl("todo"), cLLVM)); + return list; } } + public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{ public S accept(ExprVisitor v, H h) { return v.visitBinOp(this, h); @@ -100,13 +106,22 @@ public class Program{ } @Override - public DefineLLVM toLLVM() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); + public ArrayList toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir ĂȘtre mise directement dans l'expression + ArrayList list = new ArrayList<>(); + ArrayList eLLVM1 = e1.toLLVM(); + ArrayList eLLVM2 = e2.toLLVM(); + list.addAll(eLLVM1); + list.addAll(eLLVM2); + VarLLVMImpl var1 = eLLVM1.getLast().getVar(); + VarLLVMImpl var2 = eLLVM2.getLast().getVar(); + list.add(new AssignLVMImp(new VarLLVMImpl("todo"), new BinOpLLVMImp(new IntLLVMImpl(),op,var1,var2))); + + return list; } } + public static record Return_instrImp(Expression e) implements Instruction{ public S accept(InstrVisitor v, H h) { return v.visitReturn(this,h); @@ -117,11 +132,18 @@ public class Program{ } @Override - public InstructionLLVM toLLVM() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); + public ArrayList toLLVM() { + ArrayList list = e.toLLVM(); + InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl(),list.getLast().getVar()); + ArrayList result = new ArrayList<>(); + result.addAll(list); + result.add(r); + + return result; } } + + public static record AssignImp(String t, Expression e) implements Instruction{ public S accept(InstrVisitor v, H h) { return v.visitAssign(this, h); @@ -134,24 +156,41 @@ public class Program{ } @Override - public InstructionLLVM toLLVM() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); + public ArrayList toLLVM() { + ArrayList list = e.toLLVM(); + InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(t),list.getLast().getVar()); + ArrayList result = new ArrayList<>(); + result.addAll(list); + result.add(r); + return result; } } + public static record Type_voidImp() implements Type{ public String prettyprinter() { return "VOID"; } + + @Override + public TypeLLVM toLLVM() { + return new IntLLVMImpl(); + } } + public static record Type_intImp() implements Type{ public String prettyprinter() { return "INT"; } + + @Override + public TypeLLVM toLLVM() { + return new VoidLLVMImpl(); + } } + //Eval public static class ProgramEval implements ProgramVisitor, Integer> { @Override @@ -165,6 +204,7 @@ public class Program{ } } + public static class FunctionEval implements FunctionVisitor, Integer> { @Override @@ -178,6 +218,7 @@ public class Program{ } } + public static class InstructionEval implements InstrVisitor,Integer>{ @Override @@ -193,6 +234,7 @@ public class Program{ } } + public static class ExprEval implements ExprVisitor,Integer>{ public Integer visitConst(ConstImp c, Map h){ return c.c(); diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 3dff36b..af5053a 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -47,7 +47,7 @@ public interface Interface { public S visitBinOpLLVM(BinOpLLVMImp e, H h); } - public interface Val{ + public interface Val extends ExpressionLLVM{ public String prettyprinter(); } @@ -55,4 +55,8 @@ public interface Interface { public String prettyprinter(); } + public interface TypeLLVM{ + public String prettyprinter(); + } + } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index ab01564..1bddbbc 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -26,7 +26,7 @@ public class ProgramLLVM { str.append("declare i32 @printf (i8 * noalias nocapture, ...)\n"); str.append("declare i32 @scanf (i8 * noalias nocapture, ...)\n"); str.append("\n"); - str.append("; Actual code begins"); + str.append("; Actual code begins\n"); // DĂ©claration pour les string //for(int i = 0; i instrs) implements DefineLLVM{ + public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList instrs) implements DefineLLVM{ public S accept(DefineLLVMVisitor v, H h) { return v.visitProgramLLVM(this, h); } - public String type_toString(){ - switch(t.toString()){ + /*public String type_toString(){ + switch(t.prettyprinter()){ case "INT": return "i32"; case "VOID": @@ -59,11 +60,11 @@ public class ProgramLLVM { default: throw new AssertionError(); } - } + }*/ public String prettyprinter(){ StringBuilder str = new StringBuilder("define "); - str.append(type_toString()).append(" @").append(nom).append("("); + str.append(type.prettyprinter()).append(" @").append(nom).append("("); //TODO param @@ -76,6 +77,7 @@ public class ProgramLLVM { } } + //Instructon : /* public static record LabelLLVMImpl(String nom) implements InstructionLLVM{ @@ -86,91 +88,117 @@ public class ProgramLLVM { } */ - public static record AssignLVMImp(Var var, ExpressionLLVM e) implements InstructionLLVM{ + public static record AssignLVMImp(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitAssignLLVM(this, h); } public String prettyprinter(){ - return "%" + var.prettyprinter() + " = " + e.prettyprinter(); + return INDENT+var.prettyprinter() + " = " + e.prettyprinter(); + } + public VarLLVMImpl getVar(){ + return var; } } - public static record ReturnLLVMImp(ExpressionLLVM e) implements InstructionLLVM{ + public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitReturnLLVM(this, h); } public String prettyprinter(){ - StringBuilder str = new StringBuilder(); - return str.toString(); + return INDENT+"ret " + type.prettyprinter() + " " + e.prettyprinter(); } } //Expression : - public static record BinOpLLVMImp(Op op, Val val1,Val val2) implements ExpressionLLVM{ + public static record BinOpLLVMImp(TypeLLVM type,Op op, Val val1,Val val2) implements ExpressionLLVM{ public S accept(ExpressionLLVMVisitor v, H h) { return v.visitBinOpLLVM(this, h); } public String op_toString(){ StringBuilder str = new StringBuilder(); - switch(op.toString()){ - case "PLUS": - str.append("add"); + switch(op){ + case PLUS: + str.append("add "); break; - case "MINUS": - str.append("minus"); + case MINUS: + str.append("minus "); break; default: throw new AssertionError(); } - return str.append("i32").toString(); + return str.toString(); //TODO } public String prettyprinter(){ - return op_toString() + val1.prettyprinter() + ", " + val2.prettyprinter(); + return op_toString() + type.prettyprinter() + " " + val1.prettyprinter() + ", " + val2.prettyprinter(); } } - public static record ConstLLVMImp() implements ExpressionLLVM{ + + public static record ConstLLVMImp(TypeLLVM type, int val) implements ExpressionLLVM{ public S accept(ExpressionLLVMVisitor v, H h) { return v.visitConstLLVM(this, h); } @Override public String prettyprinter() { - StringBuilder str= new StringBuilder(); - return str.toString(); + return val+""; } } - public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{ + + public static record allocaLLVMImpl(TypeLLVM type, int nbBits) implements ExpressionLLVM{ public String prettyprinter(){ return "aloca" + " i" + nbBits; } } - public static record loadLLVMImpl(int nbBits,int nbBits2, Val val) implements ExpressionLLVM{ + + public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, Val val) implements ExpressionLLVM{ public String prettyprinter(){ return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter(); } } + //Val - public static record ValImpl(int val) implements Val{ + public static record ValLLVMImpl(int val) implements Val{ public String prettyprinter(){ return val + ""; } } - public static record Var(String nom) implements Val{ + + public static record VarLLVMImpl(String nom) implements Val{ public String prettyprinter(){ return "%"+nom; } } + + + public static record IntLLVMImpl() implements TypeLLVM{ + + @Override + public String prettyprinter() { + return "i32"; + } + + } + + + public static record VoidLLVMImpl() implements TypeLLVM{ + + @Override + public String prettyprinter() { + return "void"; + } + + } }