From d8d24e618d1340e549f76d06b1a4682bff515c5d Mon Sep 17 00:00:00 2001 From: Rochas Date: Sun, 27 Apr 2025 23:59:34 +0200 Subject: [PATCH] Appeal, todo : corriger callLLVM --- src/main/java/TP2/asd/toLLVM_Visitor.java | 25 ++++++++++++------- src/main/java/TP2/llvm/Interface.java | 2 +- .../TP2/llvm/PrettyprinterLLVM_Visitor.java | 2 +- src/main/java/TP2/llvm/ProgramLLVM.java | 19 +++----------- tests/aLaMain.vsl | 1 + 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 5344722..4c55926 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -96,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 AssignLVMImp(new VarLLVMImp(t2, name),new allocaLLVMImp(t2))); + list.add(new AssignLLVMImp(new VarLLVMImp(t2, name),new allocaLLVMImp(t2))); } prevSymTable.updateId(h); return new InstrAndSymTable(list,h); @@ -145,9 +145,9 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public ArrayList visitAssign(AssignImp instr, SymTable h) { + ArrayList result = new ArrayList<>(); InstrAndVal res = instr.e().accept(this,h); ValLLVM var = res.val; - ArrayList result = new ArrayList<>(); result.addAll(res.instrs); //InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var); InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t()))); @@ -194,7 +194,7 @@ public class toLLVM_Visitor implements ProgramVisitor, Result r = h.addNewTempVar(); h = r.symTable; VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); - l.add(new AssignLVMImp(varCond,exTemp)); + l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); l.add(new LabelLLVMImp(labelThen)); @@ -222,7 +222,7 @@ public class toLLVM_Visitor implements ProgramVisitor, Result r = h.addNewTempVar(); h = r.symTable; VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); - l.add(new AssignLVMImp(varCond,exTemp)); + l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); l.add(new LabelLLVMImp(labelThen)); @@ -256,7 +256,7 @@ public class toLLVM_Visitor implements ProgramVisitor, Result r = h.addNewTempVar(); h = r.symTable; VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var); - l.add(new AssignLVMImp(varCond,exTemp)); + l.add(new AssignLLVMImp(varCond,exTemp)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); l.add(new LabelLLVMImp(labelDo)); @@ -286,7 +286,7 @@ public class toLLVM_Visitor implements ProgramVisitor, Result r = h.addNewTempVar(); h = r.symTable; VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var); - l.add(new AssignLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(val))))); + l.add(new AssignLLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(val))))); prevSymTable.updateId(h); return new InstrAndVal(l, varTemp); } @@ -313,17 +313,18 @@ public class toLLVM_Visitor implements ProgramVisitor, String temp = r.var; h = r.symTable; VarLLVMImp var = new VarLLVMImp(type,temp); - list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2))); + 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()){ InstrAndVal result = param.accept(this,h); - l.add((InstructionLLVM) result.instrs); + l.addAll(result.instrs); paramsLLVM.add(result.val); } ValueFunMap fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles @@ -331,7 +332,13 @@ public class toLLVM_Visitor implements ProgramVisitor, 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 CallLLVMImp(fLLVM.define,paramsLLVM,"")); - return new InstrAndVal(l, null); + + //TODO Call est une expression + Result r = h.addNewTempVar(); + ValLLVM val = new VarLLVMImp(new IntLLVMImp(),r.var); + h = r.symTable; + prevSymTable.updateId(h); + return new InstrAndVal(l, val); } @Override diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 235c30d..e00a7b5 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -27,7 +27,7 @@ public interface Interface { public interface InstructionLLVMVisitor { public S visitReturnLLVM(ReturnLLVMImp instr, H h); - public S visitAssignLLVM(AssignLVMImp instr, H h); + public S visitAssignLLVM(AssignLLVMImp instr, H h); public S visitStoreLLVM(StoreLLVMImp instr, H h); public S visitPrintLLVM(PrintLLVMImp instr, H h); public S visitReadLLVM(ReadLLVMImp instr, H h); diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index 0d9f4ed..a68cee0 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -65,7 +65,7 @@ TypeLLVMVisitor } @Override - public String visitAssignLLVM(AssignLVMImp instr, String h) { + public String visitAssignLLVM(AssignLLVMImp instr, String h) { return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h); } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index d008dd3..da8e26d 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -53,7 +53,7 @@ public class ProgramLLVM { } - public static record AssignLVMImp(VarLLVMImp var, ExpressionLLVM e) implements InstructionLLVM{ + public static record AssignLLVMImp(VarLLVMImp var, ExpressionLLVM e) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitAssignLLVM(this, h); @@ -74,7 +74,7 @@ public class ProgramLLVM { } } - public static record CallLLVMImp(DefineLLVMImp f, ArrayList params, String str) implements InstructionLLVM{ + public static record CallLLVMImp(DefineLLVMImp f, ArrayList params, String str) implements InstructionLLVM{ //une expression ? @Override public S accept(InstructionLLVMVisitor v, H h) { return v.visitCallLLVM(this, h); @@ -110,19 +110,6 @@ public class ProgramLLVM { } - /*public static record ConstLLVMImp(TypeLLVM type, int val) implements ExpressionLLVM{ - public S accept(ExpressionLLVMVisitor v, H h) { - return v.visitConstLLVM(this, h); - } - - @Override - public String prettyprinter() { - return val+""; - } - - }*/ - - public static record allocaLLVMImp(TypeLLVM type) implements ExpressionLLVM{ @Override public S accept(ExpressionLLVMVisitor v, H h) { @@ -171,6 +158,7 @@ public class ProgramLLVM { @Override public TypeLLVM getType() { + System.out.println("getType"); return type; } } @@ -184,6 +172,7 @@ public class ProgramLLVM { @Override public TypeLLVM getType() { + System.out.println("getType"); return type; } } diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index af2188a..3624a73 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -11,6 +11,7 @@ FUNC INT main(x,y) { x := 5 minh := x * y b:=3 + c:=3 c:=add(x,b) PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b WHILE b - 1