diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index 466cdec..9839e76 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -53,7 +53,7 @@ public class Main { // Verify the program semantic // Generate the intermediate representation - //System.out.println("todo"); + System.out.println("\n\n"); ProgramLLVMImp astLLVM = ast.toLLVM(); //System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n"); diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index d98ad4a..9411331 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -1,7 +1,10 @@ package TP2.asd; +import java.util.ArrayList; + import org.pcollections.*; import TP2.asd.Interface.Type; import TP2.asd.Program.Type_intImp; +import TP2.llvm.ProgramLLVM.DeclarGlobalLLVMImp; import TP2.llvm.ProgramLLVM.DefineLLVMImp; @@ -9,18 +12,22 @@ public class SymTable { private PStack> varMap; private PMap functionsMap; - private int id=1; - private int idLabel = 1; + private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : id Label, [2] : global + private ArrayList declarationsGlobal; - public SymTable(){ + public SymTable(){ + this.id = new int[3]; this.varMap= ConsPStack.singleton(HashTreePMap.empty()); this.functionsMap = HashTreePMap.empty(); + this.declarationsGlobal = new ArrayList<>(); + this.id[0] = 1; + this.id[1] = 1; } - public SymTable(PStack> varMap, PMap functionsMap, int id, int idLabel){ + public SymTable(PStack> varMap, PMap functionsMap, int[] id,ArrayList declarationsGlobal){ this.varMap= varMap; this.id = id; this.functionsMap = functionsMap; - this.idLabel = idLabel; + this.declarationsGlobal = declarationsGlobal; } public static class ValueFunMap{ @@ -34,14 +41,13 @@ public class SymTable { } public SymTable newBlock() { - return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id, idLabel); + return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal); } public SymTable outBlock() { if (varMap.size() > 1) { - return new SymTable(varMap.minus(0), functionsMap, id, idLabel); + return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal); } else { - System.err.println("Vide"); return this; } } @@ -57,9 +63,14 @@ public class SymTable { } } - public void updateId(SymTable symTable2){ + /*public void update(SymTable symTable2){ this.id = symTable2.getId(); this.idLabel = symTable2.getIdLabel(); + this.declarationsGlobal = symTable2.getDeclarationGlobal(); + }*/ + + public ArrayList getDeclarationGlobal(){ + return this.declarationsGlobal; } public static class Result{ @@ -75,7 +86,7 @@ public class SymTable { public SymTable addFunction(DefineLLVMImp function, Boolean isProto){ ValueFunMap value = this.functionsMap.get(function.name()); if(value == null || (value!=null && value.isProto && !isProto)){ - return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel); + return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id,this.declarationsGlobal); } else{ if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà"); @@ -90,46 +101,56 @@ public class SymTable { } public int getId(){ - return this.id; + return this.id[0]; } public int getIdLabel(){ - return this.idLabel; + return this.id[1]; } public int getNewId(){ - int a = this.id; - this.id++; + int a = this.id[0]; + this.id[0]++; return a; } public int getNewIdLabel(){ - int a = this.idLabel; - this.idLabel++; + int a = this.id[1]; + this.id[1]++; return a; } public Result addNewTempVar(){ - String newVar = "temp"+id; + String newVar = "temp"+id[0]; SymTable newSymTab = this.addVarInTab(newVar,new Type_intImp(),false); return new Result(newSymTab,newVar); } public Result addParam(String nomParam){ - String newParam = "param_"+nomParam+id; + String newParam = "param_"+nomParam; SymTable newSymTab = this.addVarInTab(nomParam,new Type_intImp(),true); return new Result(newSymTab,newParam); } + public String getGlobalDeclName(){; + return "fmt"+id[2]; + } + + public void addGlobalDecl(DeclarGlobalLLVMImp decl){ + id[2]++; + this.declarationsGlobal.add(decl); + } + public Result addVar(String nomVar) { PMap top = varMap.get(0); if (top.containsKey(nomVar)) { System.err.println("[VSL compile error] :" + "Erreur"); return new Result(this, null); } - String realName = nomVar + id; - top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false)); - SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel); + String realName = nomVar + id[0]; + top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id[0], false)); + id[0]++; + SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal); return new Result(newSym, realName); } @@ -186,13 +207,14 @@ public class SymTable { System.err.println(nomVar+ " déjà déclaré."); return this; } - top = top.plus(nomVar, new ValueVarMap(type, id, isParam)); - return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel); + top = top.plus(nomVar, new ValueVarMap(type, id[0], isParam)); + if(!isParam) this.id[0]++; + return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal); } public String print_all(){ StringBuilder str = new StringBuilder(); - str.append("Id = ").append(id).append("\n"); + str.append("Id = ").append(id[0]).append("\n"); str.append("VARIABLES:\n"); int scopeLevel = varMap.size(); diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 3eea0d1..b820f08 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -54,8 +54,7 @@ public class toLLVM_Visitor implements ProgramVisitor, fonctionLLVM.add(function); } } - System.out.println(h.print_all()); - return new ProgramLLVMImp(new ArrayList<>(),fonctionLLVM); + return new ProgramLLVMImp(h.getDeclarationGlobal(),fonctionLLVM); } @@ -63,19 +62,17 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public DefineLLVMImp 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; - VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name); + VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name,false); h = r.symTable; paramsLLVM.add(var); } instrLLVM.addAll(fun.instruction().accept(this, h)); DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); - prevSymTable.updateId(h); return define; } @@ -88,7 +85,6 @@ 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, Result r = h.addVar(instr.s().get(i)); String name = r.var; h = r.symTable; - list.add(new AssignLLVMImp(new VarLLVMImp(t2, name),new allocaLLVMImp(t2))); + list.add(new AssignLLVMImp(new VarLLVMImp(t2, name,false),new allocaLLVMImp(t2))); } - prevSymTable.updateId(h); return new InstrAndSymTable(list,h); } @@ -106,7 +101,6 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public ArrayList visitBloc(BlocImp instr, SymTable h) { - SymTable prev = h; h= h.newBlock(); ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i, } h= h.outBlock(); - prev.updateId(h); return instrLLVM; } @Override public ArrayList visitBlocDec(BlocDecImp instr, SymTable h) { - SymTable prev = h; ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i, instrLLVM.addAll(instr.instrs().get(i).accept(this, h)); } - prev.updateId(h); return instrLLVM; } @@ -157,7 +148,7 @@ public class toLLVM_Visitor implements ProgramVisitor, ArrayList result = new ArrayList<>(); result.addAll(res.instrs); //InstructionLLVM r = new AssignLLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var); - InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t()))); + InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t()),false)); result.add(r); return result; } @@ -167,7 +158,15 @@ public class toLLVM_Visitor implements ProgramVisitor, public ArrayList visitPrint(PrintImp instr, SymTable h) { ArrayList l = new ArrayList<>(); ArrayList params = new ArrayList<>(); - l.add(new PrintLLVMImp(params)); //TODO + + String name = h.getGlobalDeclName(); + DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(name); + VarLLVMImp varGlobal = new VarLLVMImp(new StringLLVMImp(),name,true); + h.addGlobalDecl(globalDecl); + + params.add(varGlobal); + + l.add(new PrintLLVMImp(globalDecl,params)); return l; } @@ -177,10 +176,18 @@ public class toLLVM_Visitor implements ProgramVisitor, for(int i = 0; i params = new ArrayList<>(); + + String name = h.getGlobalDeclName(); + DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(name); + VarLLVMImp varGlobal = new VarLLVMImp(new StringLLVMImp(),name,true); + h.addGlobalDecl(globalDecl); + + params.add(varGlobal); params.add(newVar); - l.add(new ReadLLVMImp(params)); + + l.add(new ReadLLVMImp(globalDecl,params)); } return l; } @@ -212,7 +219,6 @@ public class toLLVM_Visitor implements ProgramVisitor, @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()+":"; @@ -225,7 +231,7 @@ public class toLLVM_Visitor implements ProgramVisitor, ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false); l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); @@ -236,13 +242,11 @@ public class toLLVM_Visitor implements ProgramVisitor, h=h.outBlock(); 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()+":"; @@ -256,7 +260,7 @@ public class toLLVM_Visitor implements ProgramVisitor, ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false); l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); @@ -271,13 +275,11 @@ public class toLLVM_Visitor implements ProgramVisitor, 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()+":"; @@ -291,7 +293,7 @@ public class toLLVM_Visitor implements ProgramVisitor, ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false); l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); @@ -304,7 +306,6 @@ public class toLLVM_Visitor implements ProgramVisitor, l.add(new BrLLVMImp(labelWhile)); l.add(new LabelLLVMImp(labelDone)); - prevSymTable.updateId(h); return l; } @@ -319,20 +320,17 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public InstrAndVal visitVar(VarImp e, SymTable h) { - SymTable prevSymTable = h; ArrayList l =new ArrayList<>(); - ValLLVM val = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name())); + ValLLVM val = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name()),false); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var); + VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var,false); l.add(new AssignLLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(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); @@ -351,14 +349,12 @@ public class toLLVM_Visitor implements ProgramVisitor, Result r = h.addNewTempVar(); String temp = r.var; h = r.symTable; - VarLLVMImp var = new VarLLVMImp(type,temp); + VarLLVMImp var = new VarLLVMImp(type,temp,false); list.add(new AssignLLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2))); - prevSymTable.updateId(h); return new InstrAndVal(list, var); } public InstrAndVal visitAppeal(AppealImp instr,SymTable h){ - SymTable prevSymTable = h; ArrayList l = new ArrayList<>(); ArrayList paramsLLVM = new ArrayList<>(); for(Expression param : instr.params()){ @@ -375,10 +371,9 @@ public class toLLVM_Visitor implements ProgramVisitor, //Pour c=func(x,y) Result res = h.addNewTempVar(); h = res.symTable; - VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var); + VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var,false); l.add(new AssignLLVMImp(var, new CallLLVMImp(fLLVM.define,paramsLLVM,""))); - prevSymTable.updateId(h); return new InstrAndVal(l, var); } diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 830e3fc..61d5d0c 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -35,6 +35,7 @@ public interface Interface { public S visitBrLLVM(BrLLVMImp instr, H h); public S visitBrCondLLVM(BrCondLLVMImp instr, H h); public S visitCallVoidLLVM(CallVoidLLVMImp instr, H h); + public S visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, H h); } //////////ExpressionLLVM (expression) diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index 646d671..80d9ea0 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -25,10 +25,11 @@ TypeLLVMVisitor str.append("\n"); str.append("; Actual code begins\n"); - // Déclaration pour les string - //for(int i = 0; i return callRead.accept(this, h); } + @Override + public String visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, String h) { + String str = "@."+instr.name() + " = global"; + str += "[" + "x"+ "]"; + str+= "c\"\"\n"; + + return str; + } + //label @Override public String visitLabelLLVM(LabelLLVMImp instr, String h) { @@ -174,7 +184,9 @@ TypeLLVMVisitor @Override public String visitVarLLVM(VarLLVMImp e, String h) { - return "%"+e.nom(); + String prefix = "%"; + if(e.isGlobal()) prefix = "@"; + return prefix+e.nom(); } @Override @@ -198,4 +210,5 @@ TypeLLVMVisitor } + } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index 5ae6cb6..e01271a 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -9,7 +9,7 @@ import TP2.llvm.Interface.*; public class ProgramLLVM { //Program - public static record ProgramLLVMImp(ArrayList declration ,ArrayList fonctions) implements ProgLLVM{ + public static record ProgramLLVMImp(ArrayList declarGlobal ,ArrayList fonctions) implements ProgLLVM{ public S accept(ProgramLLVMVisitor v, H h) { return v.visitProgramLLVM(this, h); } @@ -81,14 +81,14 @@ public class ProgramLLVM { } } - public static record PrintLLVMImp(ArrayList l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print + public static record PrintLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList l) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitPrintLLVM(this, h); } } - public static record ReadLLVMImp(ArrayList l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read + public static record ReadLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList l) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { @@ -96,6 +96,14 @@ public class ProgramLLVM { } } + public static record DeclarGlobalLLVMImp(String name) implements InstructionLLVM{ + + @Override + public S accept(InstructionLLVMVisitor v, H h) { + return v.visitDeclarGlobalLLVM(this, h); + } + } + //Expression : public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ @Override @@ -170,13 +178,12 @@ public class ProgramLLVM { @Override public TypeLLVM getType() { - System.out.println("getType"); return type; } } - public static record VarLLVMImp(TypeLLVM type, String nom) implements ValLLVM{ + public static record VarLLVMImp(TypeLLVM type, String nom, Boolean isGlobal) implements ValLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitVarLLVM(this, h); @@ -184,7 +191,6 @@ public class ProgramLLVM { @Override public TypeLLVM getType() { - System.out.println("getType"); return type; } }