detection d'erreur Fonction/Proto + harmonisation nom des Class dans ProgramLLVM (mélange de 'Imp' et 'Impl')

This commit is contained in:
Rochas
2025-04-27 19:02:08 +02:00
parent 0105a3f59e
commit 6dfa1c6fd8
10 changed files with 131 additions and 114 deletions

View File

@@ -8,8 +8,8 @@ import TP2.asd.SymTable.*;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVMImpl>,
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
FunctionVisitor<SymTable,DefineLLVMImp>,
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
@@ -44,42 +44,44 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
//PROGRAM
@Override
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
public ProgramLLVMImp visitProgram(ProgramImp prog, SymTable h) {
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
for(int i = 0; i<prog.fonctions().size(); i++){
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
//h = h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){ //les prototypes n'existent pas dans LLVM
DefineLLVMImp function = prog.fonctions().get(i).accept(this, h);
Boolean isProto = (prog.fonctions().get(i) instanceof PrototypeImp);
h = h.addFunction(function,isProto);
if(!isProto){ //les prototypes n'existent pas dans LLVM
fonctionLLVM.add(function);
}
}
return new ProgramLLVMImpl(new ArrayList<>(),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<InstructionLLVM> instrLLVM = new ArrayList<>();
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
ArrayList<VarLLVMImp> 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<SymTable,ProgramLLVMImpl>
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<SymTable,ProgramLLVMImpl>
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<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instrs);
result.add(r);
@@ -148,7 +150,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ArrayList<InstructionLLVM> 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<SymTable,ProgramLLVMImpl>
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> 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<SymTable,ProgramLLVMImpl>
for(int i = 0; i<instr.t().size(); i++){
String nomVar = h.getVar(instr.t().get(i).name());
Type typeVar = h.getType(instr.t().get(i).name());
VarLLVMImpl newVar = new VarLLVMImpl(typeVar.accept(this,h), nomVar);
VarLLVMImp newVar = new VarLLVMImp(typeVar.accept(this,h), nomVar);
ArrayList<ValLLVM> 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<SymTable,ProgramLLVMImpl>
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<SymTable,ProgramLLVMImpl>
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<SymTable,ProgramLLVMImpl>
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<SymTable,ProgramLLVMImpl>
@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<SymTable,ProgramLLVMImpl>
public InstrAndVal visitVar(VarImp e, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> 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<SymTable,ProgramLLVMImpl>
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<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
for(Expression param : instr.params()){
@@ -324,21 +326,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
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();
}
}