From d238ac08877bed31db43de6882fb19159c18ae55 Mon Sep 17 00:00:00 2001 From: Rochas Date: Tue, 29 Apr 2025 23:25:07 +0200 Subject: [PATCH] =?UTF-8?q?return=200=20=C3=A0=20la=20fin,=20suppression?= =?UTF-8?q?=20du=20typeFunction=20dans=20la=20symTable,=20Appeal=20renom?= =?UTF-8?q?=C3=A9=20en=20Call?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/antlr/VSLParser.g | 2 +- src/main/java/TP2/Main.java | 4 +- src/main/java/TP2/asd/Interface.java | 2 +- .../java/TP2/asd/PrettyprinterVisitor.java | 2 +- src/main/java/TP2/asd/Program.java | 4 +- src/main/java/TP2/asd/SymTable.java | 61 +++++++--------- src/main/java/TP2/asd/toLLVM_Visitor.java | 25 ++++--- .../TP2/llvm/PrettyprinterLLVM_Visitor.java | 2 +- tests/aLaMain2.vsl | 73 +++++-------------- 9 files changed, 67 insertions(+), 108 deletions(-) diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 4f04922..ed53161 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -226,7 +226,7 @@ lit returns [Expression out]: } | t=ident ( - ParO (para=list_expression)? ParF {$out = new AppealImp($t.out,($para.out !=null) ?$para.out : new ArrayList<>());} + ParO (para=list_expression)? ParF {$out = new CallImp($t.out,($para.out !=null) ?$para.out : new ArrayList<>());} | {$out=new VarImp($t.out);} ) ; diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index fe77088..f8263fe 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -56,12 +56,12 @@ public class Main { //System.err.println("todo " + ast); //System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n"); - //System.out.println(ast.prettyprinter()); + System.out.println(ast.prettyprinter()); //System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n"); // Verify the program semantic // Generate the intermediate representation - //System.out.println("\n\n"); + 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/Interface.java b/src/main/java/TP2/asd/Interface.java index c424f54..47d1480 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -67,7 +67,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(AppealImp instr, H h); + public S visitCall(CallImp 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 1fe1026..8b6293b 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -189,7 +189,7 @@ public class PrettyprinterVisitor implements ProgramVisitor, } @Override - public String visitAppeal(AppealImp instr,String indent){ + public String visitCall(CallImp instr,String indent){ String str = indent + instr.fName() + "("; for(int i=0; i params) implements Expression{ + public static record CallImp(String fName, ArrayList params) implements Expression{ @Override public S accept(ExprVisitor v, H h) { - return v.visitAppeal(this, h); + return v.visitCall(this, h); } } diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 2dd98c0..4afbb34 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -15,7 +15,6 @@ public class SymTable { private PMap functionsMap; private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : idLabel, [2] : idGlobalVar private ArrayList declarationsGlobal; //aussi partagé entre toute les symTable (.fmt pour les print et scan) - private TypeLLVM currentFuncType; public SymTable(){ this.id = new int[3]; @@ -27,12 +26,11 @@ public class SymTable { this.id[2] = 1; //idGlobalVar } public SymTable(PStack> varMap, PMap functionsMap, - int[] id,ArrayList declarationsGlobal,TypeLLVM type){ + int[] id,ArrayList declarationsGlobal){ this.varMap= varMap; this.id = id; this.functionsMap = functionsMap; this.declarationsGlobal = declarationsGlobal; - this.currentFuncType=type; } public static class ValueFunMap{ @@ -45,18 +43,6 @@ public class SymTable { } } - public SymTable newBlock() { - return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal,currentFuncType); - } - - public SymTable outBlock() { - if (varMap.size() > 1) { - return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal,currentFuncType); - } else { - return this; - } - } - public static class ValueVarMap{ public Type type; public int id; @@ -67,12 +53,18 @@ public class SymTable { this.isParam = isParam; } } - - /*public void update(SymTable symTable2){ - this.id = symTable2.getId(); - this.idLabel = symTable2.getIdLabel(); - this.declarationsGlobal = symTable2.getDeclarationGlobal(); - }*/ + + public SymTable newBlock() { + 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,declarationsGlobal); + } else { + return this; + } + } public ArrayList getDeclarationGlobal(){ return this.declarationsGlobal; @@ -88,26 +80,25 @@ public class SymTable { } - public SymTable addFunction(DefineLLVMImp function, Boolean isProto,TypeLLVM type){ + 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.declarationsGlobal,type); + 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à"); - 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à"); + 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; } } public ValueFunMap getFunction(String name){ - return this.functionsMap.get(name); + ValueFunMap f = this.functionsMap.get(name); + if (f==null) System.err.println("[VSL compile error] : la fonction '" + name + "()' n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel"); + return f; } - public TypeLLVM getFuncType(){ - return this.currentFuncType; - } public int getId(){ return this.id[0]; } @@ -152,13 +143,13 @@ public class SymTable { public Result addVar(String nomVar) { PMap top = varMap.get(0); if (top.containsKey(nomVar)) { - System.err.println("[VSL compile error] :" + "Erreur"); + System.err.println("[VSL compile error] : '" + nomVar+ "' Erreur"); return new Result(this, null); } 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,currentFuncType); + SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal); return new Result(newSym, realName); } @@ -174,7 +165,7 @@ public class SymTable { return prefix + nomVar + value.id; } } - System.err.println("[VSL compile error] :" + nomVar+" n'est pas trovué"); + System.err.println("[VSL compile error] : '" + nomVar + "' n'a pas été déclaré"); return null; } @@ -184,7 +175,7 @@ public class SymTable { return true; } } - System.err.println("[VSL compile error] :" + nomVar+" n'est pas trouvé"); + System.err.println("[VSL compile error] : '" + nomVar+"' n'est pas trouvé"); return false; } @@ -217,7 +208,7 @@ public class SymTable { } 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,currentFuncType); + return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal); } public String print_all(){ diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 9333409..bf7ae92 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -49,8 +49,8 @@ public class toLLVM_Visitor implements ProgramVisitor, for(int i = 0; i, paramsLLVM.add(var); } instrLLVM.addAll(fun.instruction().accept(this, h)); - DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM); + TypeLLVM type = fun.type().accept(this, h); + if(!(type instanceof VoidLLVMImp)){ //on ajoute un return 0 si la fonction n'est pas un void, et ne finit ni par un return ni pas un goto (sans condition) + if(!(instrLLVM.getLast() instanceof ReturnLLVMImp || instrLLVM.getLast() instanceof BrLLVMImp)){ + instrLLVM.add(new ReturnLLVMImp(type, new ValLLVMImp(type,0))); + } + } + DefineLLVMImp define = new DefineLLVMImp(fun.nom(), type, paramsLLVM, instrLLVM); return define; } @@ -273,9 +279,6 @@ public class toLLVM_Visitor implements ProgramVisitor, h=h.outBlock(); l.add(new LabelLLVMImp(labelFin)); - if (h.getFuncType() instanceof IntLLVMImp) { - l.add(new ReturnLLVMImp(new IntLLVMImp(), new ValLLVMImp(new IntLLVMImp(), 0))); - } return l; } @@ -388,7 +391,7 @@ public class toLLVM_Visitor implements ProgramVisitor, return new InstrAndVal(list, var); } - public InstrAndVal visitAppeal(AppealImp instr,SymTable h){ + public InstrAndVal visitCall(CallImp instr,SymTable h){ ArrayList l = new ArrayList<>(); ArrayList paramsLLVM = new ArrayList<>(); for(Expression param : instr.params()){ @@ -397,10 +400,10 @@ public class toLLVM_Visitor implements ProgramVisitor, paramsLLVM.add(result.val); } 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"); - return new InstrAndVal(l, null); - } + //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"); + // return new InstrAndVal(l, null); + //} //Pour c=func(x,y) Result res = h.addNewTempVar(); diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index 54f8fdb..c7c9004 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -128,7 +128,7 @@ TypeLLVMVisitor @Override public String visitCallLLVM(CallLLVMImp instr, String h) { - String str = INDENT+ "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "("; + String str = "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "("; for(int i = 0; i