correction SymTable

This commit is contained in:
Rochas
2025-04-27 16:55:47 +02:00
parent af2e42ab89
commit 0105a3f59e
5 changed files with 157 additions and 98 deletions

View File

@@ -40,6 +40,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
this.symTable = symTable;
}
}
//PROGRAM
@Override
@@ -47,8 +48,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
for(int i = 0; i<prog.fonctions().size(); i++){
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){
//h = h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){ //les prototypes n'existent pas dans LLVM
fonctionLLVM.add(function);
}
}
@@ -60,9 +61,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
for(VarImp param: fun.params()){
Result r = h.addParam(param.name());
String name = r.var;
@@ -70,9 +71,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
paramsLLVM.add(var);
}
instrLLVM.addAll(fun.instruction().accept(this, h));
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
DefineLLVMImpl define = new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
prevSymTable.updateId(h);
return define;
}
@Override
@@ -84,6 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
@@ -93,6 +96,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
}
prevSymTable.updateId(h);
return new InstrAndSymTable(list,h);
}
@@ -109,6 +113,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
@@ -118,6 +123,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
prevSymTable.updateId(h);
return instrLLVM;
}
@@ -172,6 +178,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
@@ -182,9 +189,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs);
ValLLVM val = temp.val;
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
h = temp2.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
@@ -192,12 +199,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(instr.i1().accept(this,h));
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l;
}
@Override
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
@@ -209,9 +217,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs);
ValLLVM val = temp.val;
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
h = temp2.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
@@ -223,13 +231,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(instr.i2().accept(this,h));
l.add(new BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l;
}
@Override
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelWhile = "while"+h.getNewIdLabel()+":";
@@ -241,8 +251,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs); //instructions
ValLLVM val = temp.val; //temp6
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
@@ -252,7 +263,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.add(new BrLLVMImp(labelWhile));
l.add(new LabelLLVMImp(labelDone));
prevSymTable.updateId(h);
return l;
}
@@ -267,18 +278,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
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()));
VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.addNewTempVar().var);
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)))));
prevSymTable.updateId(h);
return new InstrAndVal(l, varTemp);
}
@Override
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> list = new ArrayList<>();
InstrAndVal res1 = e.e1().accept(this, h);
InstrAndVal res2 = e.e2().accept(this, h);
@@ -298,7 +312,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
VarLLVMImpl var = new VarLLVMImpl(type,temp);
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));
prevSymTable.updateId(h);
return new InstrAndVal(list, var);
}