diff --git a/src/main/java/TP2/Error/TypeChecking.java b/src/main/java/TP2/Error/TypeChecking.java index e5c7303..54cda8d 100644 --- a/src/main/java/TP2/Error/TypeChecking.java +++ b/src/main/java/TP2/Error/TypeChecking.java @@ -158,7 +158,7 @@ public class TypeChecking { } @Override - public TypeCheckExprDiag visitAppeal(Appeal instr, SymTable h) { + public TypeCheckExprDiag visitAppeal(AppealImp instr, SymTable h) { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'visitAppeal'"); } diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index 181b173..466cdec 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -55,7 +55,7 @@ public class Main { // Generate the intermediate representation //System.out.println("todo"); - ProgramLLVMImpl astLLVM = ast.toLLVM(); + ProgramLLVMImp astLLVM = ast.toLLVM(); //System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n"); System.out.println(astLLVM.prettyprinter()); //System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n"); diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index bb2feb2..215bd00 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -8,7 +8,7 @@ public interface Interface{ public interface ProgramI { public S accept(ProgramVisitor v, H h); public String prettyprinter(); - public ProgramLLVMImpl toLLVM(); + public ProgramLLVMImp toLLVM(); } public interface ProgramVisitor { @@ -66,7 +66,7 @@ public interface Interface{ public S visitConst(ConstImp e,H h); public S visitBinOp(BinopExpressionImp e, H h); public S visitVar(VarImp e,H h); - public S visitAppeal(Appeal instr, H h); + public S visitAppeal(AppealImp instr, H h); } public interface Type{ diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 2b8c8ff..cc916a9 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -156,7 +156,7 @@ public class PrettyprinterVisitor implements ProgramVisitor, } @Override - public String visitAppeal(Appeal instr,String indent){ + public String visitAppeal(AppealImp instr,String indent){ String str = indent + instr.fName() + "("; for(int i=0; i params) implements Expression{ + public static record AppealImp(String fName, ArrayList params) implements Expression{ @Override public S accept(ExprVisitor v, H h) { return v.visitAppeal(this, h); diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 8f80561..8d4260e 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -3,13 +3,13 @@ import java.util.Stack; import org.pcollections.*; import TP2.asd.Interface.Type; import TP2.asd.Program.Type_intImp; -import TP2.llvm.ProgramLLVM.DefineLLVMImpl; +import TP2.llvm.ProgramLLVM.DefineLLVMImp; public class SymTable { - private PMap varMap; - private PMap fuctionsMap; + private PMap varMap; + private PMap fuctionsMap; private int id=1; private int idLabel = 1; @@ -17,19 +17,28 @@ public class SymTable { this.varMap= HashTreePMap.empty(); this.fuctionsMap = HashTreePMap.empty(); } - public SymTable(PMap varMap,PMap fuctionsMap, int id, int idLabel){ + 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 ValueFunMap{ + public DefineLLVMImp define; + public Boolean isProto; - public static class ValueTable{ + public ValueFunMap(DefineLLVMImp define, Boolean isProto){ + this.define = define; + this.isProto = isProto; + } + } + + public static class ValueVarMap{ public Type type; public int id; public Boolean isParam; - public ValueTable(Type type,int id, Boolean isParam){ + public ValueVarMap(Type type,int id, Boolean isParam){ this.type = type; this.id = id; this.isParam = isParam; @@ -52,14 +61,20 @@ public class SymTable { - public SymTable addFunction(DefineLLVMImpl function){ - if(!this.fuctionsMap.containsKey(function.name())){ - return new SymTable(this.varMap,this.fuctionsMap.plus(function.name(),function),this.id,this.idLabel); + public SymTable addFunction(DefineLLVMImp function, Boolean isProto){ + ValueFunMap value = this.fuctionsMap.get(function.name()); + if(value == null || (value!=null && value.isProto && !isProto)){ + return new SymTable(this.varMap, this.fuctionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel); + } + else{ + if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà"); + else if(isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" doit être déclaré avant son implémentation"); + else System.err.println("[VSL compile error] : La fonction "+function.name()+" existe déjà"); + return null; } - return this; } - public DefineLLVMImpl getFunction(String name){ + public ValueFunMap getFunction(String name){ return this.fuctionsMap.get(name); } @@ -104,7 +119,7 @@ public class SymTable { //retourne le nom de la var déjà déclaré avec son id public String getVar(String nomVar){ String prefix = ""; - ValueTable value = this.varMap.get(nomVar); + ValueVarMap value = this.varMap.get(nomVar); if(value.isParam){ prefix = "param_"; } @@ -129,8 +144,8 @@ public class SymTable { public SymTable addVar(String s, Type t,Boolean isParam){ - PMap pmap = this.varMap; - pmap= pmap.plus(s,new ValueTable(t, /*getNewId()*/ id,isParam)); + PMap pmap = this.varMap; + pmap= pmap.plus(s,new ValueVarMap(t, /*getNewId()*/ id,isParam)); return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel); } @@ -141,11 +156,11 @@ public class SymTable { 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(s).append(" id :").append(varMap.get(s).id).append(" type :").append(varMap.get(s).type).append(" isParam :").append(varMap.get(s).isParam).append("\n"); } str.append("FUNCTION :\n"); for(String f: this.fuctionsMap.keySet()){ - str.append(f).append(" ").append(fuctionsMap.get(f)).append("\n"); + str.append(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 34ed664..5344722 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -8,8 +8,8 @@ import TP2.asd.SymTable.*; import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; -public class toLLVM_Visitor implements ProgramVisitor, - FunctionVisitor, +public class toLLVM_Visitor implements ProgramVisitor, + FunctionVisitor, DeclVisitor, InstrVisitor>, ExprVisitor, @@ -44,42 +44,44 @@ public class toLLVM_Visitor implements ProgramVisitor //PROGRAM @Override - public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) { + public ProgramLLVMImp visitProgram(ProgramImp prog, SymTable h) { ArrayList fonctionLLVM = new ArrayList<>(); for(int i = 0; i(),fonctionLLVM); + System.out.println(h.print_all()); + return new ProgramLLVMImp(new ArrayList<>(),fonctionLLVM); } //FUNCTION @Override - public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) { + public DefineLLVMImp visitFunction(FunctionImp fun, SymTable h) { SymTable prevSymTable = h; ArrayList instrLLVM = new ArrayList<>(); - ArrayList paramsLLVM = new ArrayList<>(); + ArrayList paramsLLVM = new ArrayList<>(); for(VarImp param: fun.params()){ Result r = h.addParam(param.name()); String name = r.var; - VarLLVMImpl var = new VarLLVMImpl(new IntLLVMImpl(), name); + VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name); h = r.symTable; paramsLLVM.add(var); } instrLLVM.addAll(fun.instruction().accept(this, h)); - DefineLLVMImpl define = new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); + DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); prevSymTable.updateId(h); return define; } @Override - public DefineLLVMImpl visitPrototype(PrototypeImp fun, SymTable h) { - return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), null,null); + public DefineLLVMImp visitPrototype(PrototypeImp fun, SymTable h) { + return new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), null,null); } //DECLARATION @@ -94,7 +96,7 @@ public class toLLVM_Visitor implements ProgramVisitor Result r = h.addVar(instr.s().get(i)); String name = r.var; h = r.symTable; - list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2))); + list.add(new AssignLVMImp(new VarLLVMImp(t2, name),new allocaLLVMImp(t2))); } prevSymTable.updateId(h); return new InstrAndSymTable(list,h); @@ -132,7 +134,7 @@ public class toLLVM_Visitor implements ProgramVisitor InstrAndVal res = instr.e().accept(this,h); ValLLVM var = res.val; - InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var); + InstructionLLVM r = new ReturnLLVMImp(var.getType(),var); ArrayList result = new ArrayList<>(); result.addAll(res.instrs); result.add(r); @@ -148,7 +150,7 @@ public class toLLVM_Visitor implements ProgramVisitor ArrayList result = new ArrayList<>(); result.addAll(res.instrs); //InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var); - InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),h.getVar(instr.t()))); + InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t()))); result.add(r); return result; } @@ -158,7 +160,7 @@ public class toLLVM_Visitor implements ProgramVisitor public ArrayList visitPrint(PrintImp instr, SymTable h) { ArrayList l = new ArrayList<>(); ArrayList params = new ArrayList<>(); - l.add(new PrintLLVMImpl(params)); //TODO + l.add(new PrintLLVMImp(params)); //TODO return l; } @@ -168,10 +170,10 @@ public class toLLVM_Visitor implements ProgramVisitor for(int i = 0; i params = new ArrayList<>(); params.add(newVar); - l.add(new ReadLLVMImpl(params)); + l.add(new ReadLLVMImp(params)); } return l; } @@ -188,11 +190,11 @@ public class toLLVM_Visitor implements ProgramVisitor InstrAndVal temp = instr.e().accept(this,h); l.addAll(temp.instrs); ValLLVM val = temp.val; - ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0)); + ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); - l.add(new AssignLVMImpl(varCond,exTemp)); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + l.add(new AssignLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); l.add(new LabelLLVMImp(labelThen)); @@ -216,11 +218,11 @@ public class toLLVM_Visitor implements ProgramVisitor InstrAndVal temp = instr.e().accept(this,h); l.addAll(temp.instrs); ValLLVM val = temp.val; - ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0)); + ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); - l.add(new AssignLVMImpl(varCond,exTemp)); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + l.add(new AssignLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); l.add(new LabelLLVMImp(labelThen)); @@ -250,11 +252,11 @@ public class toLLVM_Visitor implements ProgramVisitor InstrAndVal temp = instr.e().accept(this,h); //retourne les instructionz pour obtenir le résultat de l'expression ainsi que la variable contenant le résultat final l.addAll(temp.instrs); //instructions ValLLVM val = temp.val; //temp6 - ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0)); + ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0)); Result r = h.addNewTempVar(); h = r.symTable; - VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var); - l.add(new AssignLVMImpl(varCond,exTemp)); + VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); + l.add(new AssignLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); l.add(new LabelLLVMImp(labelDo)); @@ -272,7 +274,7 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public InstrAndVal visitConst(ConstImp e, SymTable h) { - ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c()); + ValLLVM val = new ValLLVMImp(new IntLLVMImp(),e.c()); return new InstrAndVal(new ArrayList<>(), val); } @@ -280,11 +282,11 @@ public class toLLVM_Visitor implements ProgramVisitor 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())); + ValLLVM val = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name())); 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))))); + VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var); + l.add(new AssignLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(val))))); prevSymTable.updateId(h); return new InstrAndVal(l, varTemp); } @@ -310,13 +312,13 @@ public class toLLVM_Visitor implements ProgramVisitor Result r = h.addNewTempVar(); String temp = r.var; h = r.symTable; - VarLLVMImpl var = new VarLLVMImpl(type,temp); - list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2))); + VarLLVMImp var = new VarLLVMImp(type,temp); + list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2))); prevSymTable.updateId(h); return new InstrAndVal(list, var); } - public InstrAndVal visitAppeal(Appeal instr,SymTable h){ + public InstrAndVal visitAppeal(AppealImp instr,SymTable h){ ArrayList l = new ArrayList<>(); ArrayList paramsLLVM = new ArrayList<>(); for(Expression param : instr.params()){ @@ -324,21 +326,21 @@ public class toLLVM_Visitor implements ProgramVisitor l.add((InstructionLLVM) result.instrs); paramsLLVM.add(result.val); } - DefineLLVMImpl fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles + ValueFunMap fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles if(fLLVM == null){ System.err.println("[VSL compile error] : la fonction n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel"); } - l.add(new CallLLVMImpl(fLLVM,paramsLLVM,"")); + l.add(new CallLLVMImp(fLLVM.define,paramsLLVM,"")); return new InstrAndVal(l, null); } @Override public TypeLLVM visitInt(Type_intImp t, SymTable h) { - return new IntLLVMImpl(); + return new IntLLVMImp(); } @Override public TypeLLVM visitVoid(Type_voidImp t, SymTable h) { - return new VoidLLVMImpl(); + return new VoidLLVMImp(); } } diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 69ea46e..235c30d 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -9,7 +9,7 @@ public interface Interface { } public interface ProgramLLVMVisitor { - public S visitProgramLLVM(ProgramLLVMImpl prog, H h); + public S visitProgramLLVM(ProgramLLVMImp prog, H h); } //////////DefineLLVM (function) @@ -18,7 +18,7 @@ public interface Interface { } public interface DefineLLVMVisitor { - public S visitDefineLLVM(DefineLLVMImpl define, H h); + public S visitDefineLLVM(DefineLLVMImp define, H h); } public interface InstructionLLVM{ @@ -26,15 +26,15 @@ public interface Interface { } public interface InstructionLLVMVisitor { - public S visitReturnLLVM(ReturnLLVMImpl instr, H h); - public S visitAssignLLVM(AssignLVMImpl instr, H h); - public S visitStoreLLVM(StoreLLVMImpl instr, H h); - public S visitPrintLLVM(PrintLLVMImpl instr, H h); - public S visitReadLLVM(ReadLLVMImpl instr, H h); + public S visitReturnLLVM(ReturnLLVMImp instr, H h); + public S visitAssignLLVM(AssignLVMImp instr, H h); + public S visitStoreLLVM(StoreLLVMImp instr, H h); + public S visitPrintLLVM(PrintLLVMImp instr, H h); + public S visitReadLLVM(ReadLLVMImp 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); + public S visitCallLLVM(CallLLVMImp instr, H h); } //////////ExpressionLLVM (expression) @@ -47,11 +47,11 @@ 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 visitValLLVM(ValLLVMImpl e,H h); - public S visitVarLLVM(VarLLVMImpl e,H h); + public S visitBinOpLLVM(BinOpLLVMImp e, H h); + public S visitAllocaLLVM(allocaLLVMImp e,H h); + public S visitLoadLLVM(LoadLLVMImp e,H h); + public S visitValLLVM(ValLLVMImp e,H h); + public S visitVarLLVM(VarLLVMImp e,H h); public S visitIcmpLLVM(IcmpLLVMImp e, H h); } @@ -67,8 +67,8 @@ public interface Interface { } public interface TypeLLVMVisitor { - public S visitIntLLVM(IntLLVMImpl e,H h); - public S visitVoidLLVM(VoidLLVMImpl e, H h); + public S visitIntLLVM(IntLLVMImp e,H h); + public S visitVoidLLVM(VoidLLVMImp 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 e098917..0d9f4ed 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -15,7 +15,7 @@ TypeLLVMVisitor static String INDENT = " "; @Override - public String visitProgramLLVM(ProgramLLVMImpl prog, String indent) { + public String visitProgramLLVM(ProgramLLVMImp prog, String indent) { StringBuilder str = new StringBuilder(); str.append("; Target\n"); str.append("target triple = \"x86_64-pc-linux-gnu\"\n"); @@ -40,13 +40,13 @@ TypeLLVMVisitor } @Override - public String visitDefineLLVM(DefineLLVMImpl define, String indent) { + public String visitDefineLLVM(DefineLLVMImp define, String indent) { StringBuilder str = new StringBuilder("define "); str.append(define.type().accept(this,indent)).append(" @").append(define.name()).append("("); - List params = define.params(); + List params = define.params(); for (int i = 0; i < params.size(); i++) { - VarLLVMImpl param = params.get(i); + VarLLVMImp param = params.get(i); str.append(param.getType().accept(this, indent)).append(" ").append(param.accept(this, indent)); if (i < params.size() - 1) str.append(", "); } @@ -60,17 +60,17 @@ TypeLLVMVisitor } @Override - public String visitReturnLLVM(ReturnLLVMImpl instr, String h) { + public String visitReturnLLVM(ReturnLLVMImp instr, String h) { return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h); } @Override - public String visitAssignLLVM(AssignLVMImpl instr, String h) { + public String visitAssignLLVM(AssignLVMImp instr, String h) { return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h); } @Override - public String visitBinOpLLVM(BinOpLLVMImpl e, String h) { + public String visitBinOpLLVM(BinOpLLVMImp e, String h) { String str = ""; switch(e.op()){ case PLUS: @@ -100,23 +100,23 @@ TypeLLVMVisitor } @Override - public String visitAllocaLLVM(allocaLLVMImpl e, String h) { + public String visitAllocaLLVM(allocaLLVMImp e, String h) { return "alloca " + e.type().accept(this, h); } @Override - public String visitStoreLLVM(StoreLLVMImpl instr, String h) { + public String visitStoreLLVM(StoreLLVMImp instr, String h) { return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,""); } @Override - public String visitLoadLLVM(LoadLLVMImpl e, String h) { + public String visitLoadLLVM(LoadLLVMImp e, String h) { return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h); } @Override - public String visitCallLLVM(CallLLVMImpl instr, String h) { + public String visitCallLLVM(CallLLVMImp instr, String h) { String str = INDENT+ "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "("; for(int i = 0; i } @Override - public String visitPrintLLVM(PrintLLVMImpl instr, String h) { //TODO - DefineLLVMImpl printLLVM = new DefineLLVMImpl("printf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>()); - CallLLVMImpl callPrint = new CallLLVMImpl(printLLVM, new ArrayList<>(),"(i8*,...) "); + public String visitPrintLLVM(PrintLLVMImp instr, String h) { //TODO + DefineLLVMImp printLLVM = new DefineLLVMImp("printf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>()); + CallLLVMImp callPrint = new CallLLVMImp(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*,...) "); + public String visitReadLLVM(ReadLLVMImp instr, String h) { //TODO + DefineLLVMImp readLLVM = new DefineLLVMImp("scanf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>()); + CallLLVMImp callRead = new CallLLVMImp(readLLVM, instr.l(),"(i8*,...) "); return callRead.accept(this, h); } @@ -158,22 +158,22 @@ TypeLLVMVisitor @Override - public String visitValLLVM(ValLLVMImpl e, String h) { + public String visitValLLVM(ValLLVMImp e, String h) { return e.val() + ""; } @Override - public String visitVarLLVM(VarLLVMImpl e, String h) { + public String visitVarLLVM(VarLLVMImp e, String h) { return "%"+e.nom(); } @Override - public String visitIntLLVM(IntLLVMImpl e, String h) { + public String visitIntLLVM(IntLLVMImp e, String h) { return "i32"; } @Override - public String visitVoidLLVM(VoidLLVMImpl e, String h) { + public String visitVoidLLVM(VoidLLVMImp e, String h) { return "void"; } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index 8f2f585..d008dd3 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 ProgramLLVMImpl(ArrayList declration ,ArrayList fonctions) implements ProgLLVM{ + public static record ProgramLLVMImp(ArrayList declration ,ArrayList fonctions) implements ProgLLVM{ public S accept(ProgramLLVMVisitor v, H h) { return v.visitProgramLLVM(this, h); } @@ -22,7 +22,7 @@ public class ProgramLLVM { //Define - public static record DefineLLVMImpl(String name, TypeLLVM type, ArrayList params, ArrayList instrs) implements DefineLLVM{ + public static record DefineLLVMImp(String name, TypeLLVM type, ArrayList params, ArrayList instrs) implements DefineLLVM{ public S accept(DefineLLVMVisitor v, H h) { return v.visitDefineLLVM(this, h); } @@ -45,7 +45,7 @@ public class ProgramLLVM { } } - public static record BrCondLLVMImp(VarLLVMImpl var, String label, String labelElse) implements InstructionLLVM{ + public static record BrCondLLVMImp(VarLLVMImp var, String label, String labelElse) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitBrCondLLVM(this, h); @@ -53,42 +53,42 @@ public class ProgramLLVM { } - public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{ + public static record AssignLVMImp(VarLLVMImp var, ExpressionLLVM e) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitAssignLLVM(this, h); } } - public static record ReturnLLVMImpl(TypeLLVM type, 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 static record StoreLLVMImpl(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{ + public static record StoreLLVMImp(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitStoreLLVM(this, h); } } - public static record CallLLVMImpl(DefineLLVMImpl f, ArrayList params, String str) implements InstructionLLVM{ + public static record CallLLVMImp(DefineLLVMImp 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 + public static record PrintLLVMImp(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{ //TODO c'est un Call qui appel la fonction read + public static record ReadLLVMImp(ArrayList l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read @Override public S accept(InstructionLLVMVisitor v, H h) { @@ -97,7 +97,7 @@ public class ProgramLLVM { } //Expression : - public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ + public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitBinOpLLVM(this, h); @@ -123,7 +123,7 @@ public class ProgramLLVM { }*/ - public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{ + public static record allocaLLVMImp(TypeLLVM type) implements ExpressionLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitAllocaLLVM(this, h); @@ -136,7 +136,7 @@ public class ProgramLLVM { } - public static record LoadLLVMImpl(ValLLVM val) implements ExpressionLLVM{ + public static record LoadLLVMImp(ValLLVM val) implements ExpressionLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitLoadLLVM(this, h); @@ -163,7 +163,7 @@ public class ProgramLLVM { //Val - public static record ValLLVMImpl(TypeLLVM type, int val) implements ValLLVM{ + public static record ValLLVMImp(TypeLLVM type, int val) implements ValLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitValLLVM(this, h); @@ -176,7 +176,7 @@ public class ProgramLLVM { } - public static record VarLLVMImpl(TypeLLVM type, String nom) implements ValLLVM{ + public static record VarLLVMImp(TypeLLVM type, String nom) implements ValLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { return v.visitVarLLVM(this, h); @@ -189,7 +189,7 @@ public class ProgramLLVM { } - public static record IntLLVMImpl() implements TypeLLVM{ + public static record IntLLVMImp() implements TypeLLVM{ @Override public S accept(TypeLLVMVisitor v, H h) { return v.visitIntLLVM(this, h); @@ -197,7 +197,7 @@ public class ProgramLLVM { } - public static record VoidLLVMImpl() implements TypeLLVM{ + public static record VoidLLVMImp() implements TypeLLVM{ @Override public S accept(TypeLLVMVisitor v, H h) { return v.visitVoidLLVM(this, h);