diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 4475192..e408b17 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -61,10 +61,16 @@ public class SymTable { return new Result(newSymTab,newVar); } + //retourne le nom de la var déjà déclaré avec son id public String getVar(String nomVar){ return nomVar + this.stackMap.getLast().get(nomVar).id; } + //retourne le type de la var + public Type getType(String nomVar){ + return this.stackMap.getLast().get(nomVar).type; + } + public PStack> next_layer(){ return stackMap.plus(HashTreePMap.empty()); } @@ -109,10 +115,10 @@ public class SymTable { return this.stackmap(); } - public ValueTable getvar_Type(String s){ + public Type getvar_Type(String s){ for(int i= stackMap.size()-1; i>=0; i--){ if(stackMap.get(i).containsKey(s)){ - return stackMap.get(i).get(s); + return stackMap.get(i).get(s).type; } } return null; diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 0bd5656..4e5566b 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -57,7 +57,7 @@ public class toLLVM_Visitor implements ProgramVisitor public DefineLLVM visitFunction(FunctionImp fun, SymTable h) { ArrayList instrLLVM = new ArrayList<>(); instrLLVM.addAll(fun.instruction().accept(this, h)); - return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM); + return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), new ArrayList<>(), instrLLVM); } //DECLARATION @@ -130,14 +130,22 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public ArrayList visitPrint(PrintImp instr, SymTable h) { ArrayList l = new ArrayList<>(); - l.add(new PrintLLVMImpl(new ArrayList())); //TODO + ArrayList params = new ArrayList<>(); + l.add(new PrintLLVMImpl(params)); //TODO return l; } @Override public ArrayList visitRead(ReadImp instr, SymTable h) { ArrayList l = new ArrayList<>(); - l.add(new ReadLLVMImpl(new ArrayList())); //TODO + for(int i = 0; i params = new ArrayList<>(); + params.add(newVar); + l.add(new ReadLLVMImpl(params)); + } return l; } @@ -213,7 +221,6 @@ public class toLLVM_Visitor implements ProgramVisitor ValLLVM val = temp.val; //temp6 ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0)); Result temp2 = h.addNewTempVar(); - h = temp2.symTable; VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var); l.add(new AssignLVMImpl(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); @@ -238,8 +245,12 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public InstrAndVal visitVar(VarImp e, SymTable h) { - ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name()); - return new InstrAndVal(new ArrayList<>(), val); + ArrayList l =new ArrayList<>(); + ValLLVM val = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name())); + + VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.addNewTempVar().var); + l.add(new AssignLVMImpl(varTemp,((ExpressionLLVM)(new LoadLLVMImpl(val))))); + return new InstrAndVal(l, varTemp); } @Override diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 672c00d..30c6bf6 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -1,5 +1,4 @@ package TP2.llvm; -import TP2.asd.Program.IfThenElseImp; import TP2.llvm.ProgramLLVM.*; public interface Interface { @@ -32,12 +31,10 @@ public interface Interface { public S visitStoreLLVM(StoreLLVMImpl instr, H h); public S visitPrintLLVM(PrintLLVMImpl instr, H h); public S visitReadLLVM(ReadLLVMImpl instr, H h); - public S visitIfThenElseLLVM(IfThenElseLLVMImp instr, H h); - public S visitIfThenLLVM(IfThenLLVMImp instr, H h); - public S visitWhileLLVM(WhileLLVMImp instr, H h); public S visitLabelLLVM(LabelLLVMImp instr, H h); public S visitBrLLVM(BrLLVMImp instr, H h); public S visitBrCondLLVM(BrCondLLVMImp instr, H h); + public S visitCallLLVM(CallLLVMImpl instr, H h); } //////////ExpressionLLVM (expression) @@ -52,7 +49,7 @@ public interface Interface { public interface ExpressionLLVMVisitor { public S visitBinOpLLVM(BinOpLLVMImpl e, H h); public S visitAllocaLLVM(allocaLLVMImpl e,H h); - public S visitLoadLLVM(loadLLVMImpl e,H h); + public S visitLoadLLVM(LoadLLVMImpl e,H h); public S visitValLLVM(ValLLVMImpl e,H h); public S visitVarLLVM(VarLLVMImpl e,H h); public S visitIcmpLLVM(IcmpLLVMImp e, H h); @@ -73,6 +70,7 @@ public interface Interface { public S visitIntLLVM(IntLLVMImpl e,H h); public S visitVoidLLVM(VoidLLVMImpl e, H h); public S visitBooleanLLVM(BooleanLLVMImp e, H h); + public S visitStringLLVM(StringLLVMImp e, H h); } } diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index 158bb79..9b8ad86 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -1,5 +1,7 @@ package TP2.llvm; +import java.util.ArrayList; + import TP2.asd.Program.IfThenElseImp; import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; @@ -29,6 +31,7 @@ TypeLLVMVisitor //} //PROTO et FUNC + for(DefineLLVM fonction : prog.fonctions()){ str.append(fonction.accept(this,indent)); str.append("\n"); @@ -39,7 +42,7 @@ TypeLLVMVisitor @Override public String visitDefineLLVM(DefineLLVMImpl define, String indent) { StringBuilder str = new StringBuilder("define "); - str.append(define.type().accept(this,indent)).append(" @").append(define.nom()).append("("); + str.append(define.type().accept(this,indent)).append(" @").append(define.name()).append("("); //TODO param @@ -103,25 +106,38 @@ TypeLLVMVisitor @Override - public String visitLoadLLVM(loadLLVMImpl e, String h) { - return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h); - } - - - @Override - public String visitPrintLLVM(PrintLLVMImpl instr, String h) { - return INDENT+"call " + "...TODO..." +" printf " + "...TODO..."; + public String visitLoadLLVM(LoadLLVMImpl e, String h) { + return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h); } @Override - public String visitReadLLVM(ReadLLVMImpl instr, String h) { - return INDENT+"call " + "...TODO..." +" scanf " + "...TODO..."; + public String visitCallLLVM(CallLLVMImpl instr, String h) { + String str = INDENT+ "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "("; + for(int i = 0; i(), new ArrayList<>()); + CallLLVMImpl callPrint = new CallLLVMImpl(printLLVM, new ArrayList<>(),"(i8*,...) "); + return callPrint.accept(this, h); + } + + @Override + public String visitReadLLVM(ReadLLVMImpl instr, String h) { //TODO + DefineLLVMImpl readLLVM = new DefineLLVMImpl("scanf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>()); + CallLLVMImpl callRead = new CallLLVMImpl(readLLVM, instr.l(),"(i8*,...) "); + return callRead.accept(this, h); } //label @Override public String visitLabelLLVM(LabelLLVMImp instr, String h) { - return instr.nom()+":"; + return instr.name()+":"; } @Override @@ -134,24 +150,6 @@ TypeLLVMVisitor return INDENT+"br " + instr.var().type().accept(this, h) +" "+ instr.var().accept(this, h) + ", label %" + instr.label() + ", label %" + instr.labelElse() ; } - - @Override - public String visitIfThenElseLLVM(IfThenElseLLVMImp instr, String h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visitIfThenElseLLVM'"); - } - - @Override - public String visitIfThenLLVM(IfThenLLVMImp instr, String h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visitIfThenLLVM'"); - } - - @Override - public String visitWhileLLVM(WhileLLVMImp instr, String h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visitWhileLLVM'"); - } @Override @@ -178,4 +176,10 @@ TypeLLVMVisitor public String visitBooleanLLVM(BooleanLLVMImp e, String h) { return "i1"; } + + @Override + public String visitStringLLVM(StringLLVMImp e, String h) { + return "i8*"; + } + } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index 488c31a..8f2f585 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -22,7 +22,7 @@ public class ProgramLLVM { //Define - public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList instrs) implements DefineLLVM{ + public static record DefineLLVMImpl(String name, TypeLLVM type, ArrayList params, ArrayList instrs) implements DefineLLVM{ public S accept(DefineLLVMVisitor v, H h) { return v.visitDefineLLVM(this, h); } @@ -31,7 +31,7 @@ public class ProgramLLVM { //Instructon : //Label - public static record LabelLLVMImp(String nom) implements InstructionLLVM{ + public static record LabelLLVMImp(String name) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitLabelLLVM(this, h); @@ -74,14 +74,21 @@ public class ProgramLLVM { } } - public static record PrintLLVMImpl(ArrayList l) implements InstructionLLVM{ + public static record CallLLVMImpl(DefineLLVMImpl f, ArrayList params, String str) implements InstructionLLVM{ + @Override + public S accept(InstructionLLVMVisitor v, H h) { + return v.visitCallLLVM(this, h); + } + } + + public static record PrintLLVMImpl(ArrayList l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitPrintLLVM(this, h); } } - public static record ReadLLVMImpl(ArrayList l) implements InstructionLLVM{ + public static record ReadLLVMImpl(ArrayList l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read @Override public S accept(InstructionLLVMVisitor v, H h) { @@ -89,30 +96,6 @@ public class ProgramLLVM { } } - public static record IfThenLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{ - @Override - public S accept(InstructionLLVMVisitor v, H h) { - return v.visitIfThenLLVM(this, h); - } - - } - - public static record IfThenElseLLVMImp(ExpressionLLVM cond, InstructionLLVM intr1, InstructionLLVM instr2) implements InstructionLLVM{ - - @Override - public S accept(InstructionLLVMVisitor v, H h) { - return v.visitIfThenElseLLVM(this, h); - } - } - - public static record WhileLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{ - - @Override - public S accept(InstructionLLVMVisitor v, H h) { - return v.visitWhileLLVM(this, h); - } - } - //Expression : public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ @Override @@ -153,7 +136,7 @@ public class ProgramLLVM { } - public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, ValLLVM val) implements ExpressionLLVM{ + public static record LoadLLVMImpl(ValLLVM val) implements ExpressionLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitLoadLLVM(this, h); @@ -161,7 +144,7 @@ public class ProgramLLVM { @Override public TypeLLVM getType() { - return type; + return val().getType(); } } @@ -227,4 +210,11 @@ public class ProgramLLVM { return v.visitBooleanLLVM(this, h); } } + + public static record StringLLVMImp() implements TypeLLVM{ + @Override + public S accept(TypeLLVMVisitor v, H h) { + return v.visitStringLLVM(this, h); + } + } }