todo declaration c pt

This commit is contained in:
trochas
2025-04-10 11:13:09 +02:00
parent ecdb8ac687
commit 9905831b24
5 changed files with 72 additions and 45 deletions

View File

@@ -10,6 +10,7 @@ import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVM>,
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
TypeVisitor<SymTable,TypeLLVM>
@@ -22,15 +23,23 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
une simplement un val (var ou const) ou un binop
*/
public static class InstrAndVal{
public ArrayList<AssignLVMImpl> instr = null;
public ValLLVM val = null;
public ArrayList<AssignLVMImpl> instrs;
public ValLLVM val;
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
this.instr = instr;
this.instrs = instr;
this.val = val;
}
}
public static class InstrAndSymTable{
public ArrayList<InstructionLLVM> instrs;
public SymTable symTable;
public InstrAndSymTable(ArrayList<InstructionLLVM> instrs, SymTable symTable){
this.instrs = instrs;
this.symTable = symTable;
}
}
//PROGRAM
@Override
@@ -53,6 +62,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
}
//DECLARATION
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
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)));
}
return new InstrAndSymTable(list,h);
}
//INSTRUCTION
@Override
@@ -62,7 +87,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
result.addAll(res.instrs);
result.add(r);
return result;
@@ -73,26 +98,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
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(),instr.t()/*"h.getVar(instr.t())*/));
InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),h.getVar(instr.t())));
result.add(r);
return result;
}
@Override
public ArrayList<InstructionLLVM> visitDeclaration(DeclarationImp instr, SymTable h) {
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
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)));
}
return list;
}
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
@@ -151,8 +163,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ValLLVM val1 = res1.val;
ValLLVM val2 = res2.val;
list.addAll(res1.instr);
list.addAll(res2.instr);
list.addAll(res1.instrs);
list.addAll(res2.instrs);
TypeLLVM type = val1.getType();