diff --git a/src/main/java/TP2/Error/TypeChecking.java b/src/main/java/TP2/Error/TypeChecking.java index 7d604b7..e5c7303 100644 --- a/src/main/java/TP2/Error/TypeChecking.java +++ b/src/main/java/TP2/Error/TypeChecking.java @@ -56,7 +56,7 @@ public class TypeChecking { @Override public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) { - if(!h.searchVar(instr.t())){ + if(!h.isPresentVar(instr.t())){ return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas"); } Type t_type=h.getvar_Type(instr.t()); @@ -85,7 +85,7 @@ public class TypeChecking { @Override public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) { for(VarImp v: instr.t()){ - if(!h.searchVar(v.name())){ + if(!h.isPresentVar(v.name())){ return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas"); } } @@ -134,8 +134,8 @@ public class TypeChecking { @Override public TypeCheckExprDiag visitVar(VarImp e, SymTable h) { - if(!h.searchVar(e.name())){ - return TypeCheckExprDiag.error("Ce variable n'existe pas"); + if(!h.isPresentVar(e.name())){ + return TypeCheckExprDiag.error("Cette variable n'existe pas"); } Type e_type= h.getvar_Type(e.name()); return TypeCheckExprDiag.checked(e_type); diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 51ea6b0..8f80561 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -8,11 +8,23 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImpl; public class SymTable { - private PStack> stackMap; + private PMap varMap; private PMap fuctionsMap; - private int id=1; + private int id=1; private int idLabel = 1; + public SymTable(){ + this.varMap= HashTreePMap.empty(); + this.fuctionsMap = HashTreePMap.empty(); + } + public SymTable(PMap varMap,PMap fuctionsMap, int id, int idLabel){ + this.varMap= varMap; + this.id = id; + this.fuctionsMap = fuctionsMap; + this.idLabel = idLabel; + } + + public static class ValueTable{ public Type type; public int id; @@ -23,6 +35,11 @@ public class SymTable { this.isParam = isParam; } } + + public void updateId(SymTable symTable2){ + this.id = symTable2.getId(); + this.idLabel = symTable2.getIdLabel(); + } public static class Result{ public SymTable symTable; @@ -33,26 +50,27 @@ public class SymTable { } } - public SymTable(){ - this.stackMap= ConsPStack.empty(); //todo : HashTreePMap.empty() stack sers à rien - this.fuctionsMap = HashTreePMap.empty(); - } - public SymTable(PStack> stackMap, int id){ - this.stackMap= stackMap; - this.id = id; - } - public void addFunction(DefineLLVMImpl function){ + public SymTable addFunction(DefineLLVMImpl function){ if(!this.fuctionsMap.containsKey(function.name())){ - this.fuctionsMap.plus(function.name(),function); + return new SymTable(this.varMap,this.fuctionsMap.plus(function.name(),function),this.id,this.idLabel); } + return this; } public DefineLLVMImpl getFunction(String name){ return this.fuctionsMap.get(name); } + public int getId(){ + return this.id; + } + + public int getIdLabel(){ + return this.idLabel; + } + public int getNewId(){ int a = this.id; this.id++; @@ -65,10 +83,9 @@ public class SymTable { return a; } - public Result addNewTempVar(/*Type type*/){ - //TODO + public Result addNewTempVar(){ String newVar = "temp"+id; - SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false); //TODO + SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false); return new Result(newSymTab,newVar); } @@ -80,85 +97,55 @@ public class SymTable { public Result addVar(String nomVar){ String newVar = nomVar+id; - SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); //TODO + SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); return new Result(newSymTab,newVar); } //retourne le nom de la var déjà déclaré avec son id public String getVar(String nomVar){ String prefix = ""; - ValueTable value = this.stackMap.getLast().get(nomVar); + ValueTable value = this.varMap.get(nomVar); if(value.isParam){ prefix = "param_"; } return prefix + nomVar + value.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()); - } - - public PMap peppapeek(){ - if(stackMap.isEmpty()){ - return this.next_layer().getLast(); - } - return stackMap.getLast(); - } - - public SymTable addVar(String s, Type t,Boolean isParam){ - PStack> newpstack = null; - if(this.stackMap.isEmpty()){ - newpstack = this.next_layer(); - } - else newpstack = this.stackMap; - - //Save temporary if not PMap wont save - PMap pmap = this.peppapeek(); - pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId(),isParam)); - //this.id++; - //Delete old ones - newpstack = newpstack.minus(newpstack.indexOf(newpstack.getLast())); - //Push the new one - newpstack = newpstack.plus(pmap); - return new SymTable(newpstack,this.id); - } - - //Usually look for var in highest level , if not found research. - public boolean searchVar(String s){ - //TOTO - for(int i= stackMap.size()-1; i>=0; i--){ - if(stackMap.get(i).containsKey(s)){ - return true; - } - } - return false; - } - - public Stack> stackmap(){ - return this.stackmap(); + public Boolean isPresentVar(String nomVar){ + return this.varMap.containsKey("nomVar"); } 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).type; - } + if(this.varMap.containsKey(s)){ + return this.varMap.get(s).type; } return null; } + //retourne le type de la var + public Type getType(String nomVar){ + return this.varMap.get(nomVar).type; + } + + + public SymTable addVar(String s, Type t,Boolean isParam){ + PMap pmap = this.varMap; + pmap= pmap.plus(s,new ValueTable(t, /*getNewId()*/ id,isParam)); + + return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel); + } + + public String print_all(){ StringBuilder str = new StringBuilder(); - for(int i= stackMap.size()-1; i>=0; i--){ - str.append("level ").append(i).append("\n"); - for(String s: stackMap.get(i).keySet()){ - str.append(s).append(" ").append(stackMap.get(i).get(s)).append("\n"); - } + str.append("Id = " + id+"\n"); + str.append("VAR :\n"); + for(String s: this.varMap.keySet()){ + str.append(s).append(" ").append(varMap.get(s)).append("\n"); + } + str.append("FUNCTION :\n"); + for(String f: this.fuctionsMap.keySet()){ + str.append(f).append(" ").append(fuctionsMap.get(f)).append("\n"); } return str.toString(); } diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 7b6f7c1..34ed664 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -40,6 +40,7 @@ public class toLLVM_Visitor implements ProgramVisitor this.symTable = symTable; } } + //PROGRAM @Override @@ -47,8 +48,8 @@ public class toLLVM_Visitor implements ProgramVisitor ArrayList fonctionLLVM = new ArrayList<>(); for(int i = 0; i @Override public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) { + SymTable prevSymTable = h; ArrayList instrLLVM = new ArrayList<>(); ArrayList paramsLLVM = new ArrayList<>(); - for(VarImp param: fun.params()){ Result r = h.addParam(param.name()); String name = r.var; @@ -70,9 +71,10 @@ public class toLLVM_Visitor implements ProgramVisitor h = r.symTable; paramsLLVM.add(var); } - instrLLVM.addAll(fun.instruction().accept(this, h)); - return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); + DefineLLVMImpl define = new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); + prevSymTable.updateId(h); + return define; } @Override @@ -84,6 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) { + SymTable prevSymTable = h; ArrayList list = new ArrayList<>(); for(int i = 0; i h = r.symTable; list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2))); } + prevSymTable.updateId(h); return new InstrAndSymTable(list,h); } @@ -109,6 +113,7 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public ArrayList visitBlocDec(BlocDecImp instr, SymTable h) { + SymTable prevSymTable = h; ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i for(int i = 0; i @Override public ArrayList visitIfThen(IfThenImp instr, SymTable h) { + SymTable prevSymTable = h; ArrayList l = new ArrayList<>(); String labelIf= "if"+h.getNewIdLabel()+":"; String labelThen= "then"+h.getNewIdLabel()+":"; @@ -182,9 +189,9 @@ public class toLLVM_Visitor implements ProgramVisitor l.addAll(temp.instrs); ValLLVM val = temp.val; 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); + Result r = h.addNewTempVar(); + h = r.symTable; + VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); l.add(new AssignLVMImpl(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); @@ -192,12 +199,13 @@ public class toLLVM_Visitor implements ProgramVisitor l.addAll(instr.i1().accept(this,h)); l.add(new LabelLLVMImp(labelFin)); - + prevSymTable.updateId(h); return l; } @Override public ArrayList visitIfThenElse(IfThenElseImp instr, SymTable h) { + SymTable prevSymTable = h; ArrayList l = new ArrayList<>(); String labelIf= "if"+h.getNewIdLabel()+":"; String labelThen= "then"+h.getNewIdLabel()+":"; @@ -209,9 +217,9 @@ public class toLLVM_Visitor implements ProgramVisitor l.addAll(temp.instrs); ValLLVM val = temp.val; 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); + Result r = h.addNewTempVar(); + h = r.symTable; + VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); l.add(new AssignLVMImpl(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); @@ -223,13 +231,15 @@ public class toLLVM_Visitor implements ProgramVisitor l.addAll(instr.i2().accept(this,h)); l.add(new BrLLVMImp(labelFin)); - l.add(new LabelLLVMImp(labelFin)); + + prevSymTable.updateId(h); return l; } @Override public ArrayList visitWhile(WhileImp instr, SymTable h) { + SymTable prevSymTable = h; ArrayList l = new ArrayList<>(); String labelWhile = "while"+h.getNewIdLabel()+":"; @@ -241,8 +251,9 @@ public class toLLVM_Visitor implements ProgramVisitor l.addAll(temp.instrs); //instructions ValLLVM val = temp.val; //temp6 ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0)); - Result temp2 = h.addNewTempVar(); - VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var); + Result r = h.addNewTempVar(); + h = r.symTable; + VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); l.add(new AssignLVMImpl(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); @@ -252,7 +263,7 @@ public class toLLVM_Visitor implements ProgramVisitor l.add(new BrLLVMImp(labelWhile)); l.add(new LabelLLVMImp(labelDone)); - + prevSymTable.updateId(h); return l; } @@ -267,18 +278,21 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public InstrAndVal visitVar(VarImp e, SymTable h) { + SymTable prevSymTable = h; 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); + Result r = h.addNewTempVar(); + h = r.symTable; + VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),r.var); l.add(new AssignLVMImpl(varTemp,((ExpressionLLVM)(new LoadLLVMImpl(val))))); + prevSymTable.updateId(h); return new InstrAndVal(l, varTemp); } @Override public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) { + SymTable prevSymTable = h; ArrayList list = new ArrayList<>(); - InstrAndVal res1 = e.e1().accept(this, h); InstrAndVal res2 = e.e2().accept(this, h); @@ -298,7 +312,7 @@ public class toLLVM_Visitor implements ProgramVisitor h = r.symTable; VarLLVMImpl var = new VarLLVMImpl(type,temp); list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2))); - + prevSymTable.updateId(h); return new InstrAndVal(list, var); } diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index 4b7cad9..e098917 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -101,7 +101,7 @@ TypeLLVMVisitor @Override public String visitAllocaLLVM(allocaLLVMImpl e, String h) { - return "alloca" + e.type().accept(this, h); + return "alloca " + e.type().accept(this, h); } @Override diff --git a/tests/aLaMain2.vsl b/tests/aLaMain2.vsl new file mode 100644 index 0000000..1796837 --- /dev/null +++ b/tests/aLaMain2.vsl @@ -0,0 +1,58 @@ +PROTO INT add(x,y) + +FUNC INT add(x,y,z) { + INT z,a,b,c + y := x+y + RETURN z +} + +FUNC INT main(x,y) { + INT a,b,c,minh + x := 5 + minh := x * y + b:=3 + c:=1 + PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b + WHILE b - 1 + DO{ + b := b - 1 + c := c + 1 + WHILE b -1 + DO{ + b:= b - 1 + c:= c + 1 + WHILE b -1 + DO{ + b:= b - 1 + c:= c + 1 + } + DONE + } + DONE + } + DONE + WHILE b - 1 + DO{ + b := b - 1 + c := c + 1 + WHILE b -1 + DO{ + b:= b - 1 + c:= c + 1 + WHILE b -1 + DO{ + b:= b - 1 + c:= c + 1 + } + DONE + } + DONE + } + DONE + IF c -1 + THEN READ a ELSE READ b + FI + b:=c+1 + RETURN 4 + 6 * 5 + 2 } + +PROTO INT type(x,y) \ No newline at end of file