clean toLLVM

This commit is contained in:
Rochas
2025-04-30 19:31:59 +02:00
parent 114d2c22aa
commit c8bbfc991d
3 changed files with 75 additions and 69 deletions

View File

@@ -66,8 +66,8 @@ public interface Interface{
public interface ExprVisitor<H,S> {
public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);
public S visitVar(VarImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);
public S visitCall(CallImp instr, H h);
}

View File

@@ -195,11 +195,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
//EXPRESSION -----------------------------------
@Override
public String visitConst(ConstImp e, String indent) {
return e.c()+"";
}
@Override
public String visitBinOp(BinopExpressionImp e, String indent) {
String opStr = "?";
@@ -215,12 +210,17 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
}
@Override
public String visitConst(ConstImp e, String indent) {
return e.c()+"";
}
@Override
public String visitVar(VarImp e, String h) {
return e.name();
}
@Override
public String visitCall(CallImp instr,String indent){
String str = indent + instr.fName() + "(";

View File

@@ -59,7 +59,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
}
//FUNCTION
//FUNCTION -----------------------------------
@Override
public DefineLLVMImp visitFunction(FunctionImp fun, SymTable h) {
@@ -94,6 +94,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return define;
}
@Override
public DefineLLVMImp visitPrototype(PrototypeImp fun, SymTable h) {
ArrayList<VarLLVMImp> params = new ArrayList<>();
@@ -103,7 +104,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), params,null);
}
//DECLARATION
//DECLARATION -----------------------------------
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
@@ -145,35 +146,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return new InstrAndSymTable(list,h);
}
//INSTRUCTION
@Override
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
h= h.newBlock();
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
h= h.outBlock();
return instrLLVM;
}
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
h = temp.symTable;
instrLLVM.addAll(temp.instrs);
}
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
return instrLLVM;
}
//INSTRUCTION -----------------------------------
@Override
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
@@ -193,6 +166,36 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
}
@Override
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
h= h.newBlock();
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
h= h.outBlock();
return instrLLVM;
}
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
h = temp.symTable;
instrLLVM.addAll(temp.instrs);
}
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
return instrLLVM;
}
@Override
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
InstrAndVal res = instr.e().accept(this,h);
@@ -258,6 +261,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return l;
}
@Override
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
@@ -291,30 +295,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return l;
}
@Override
public ArrayList<InstructionLLVM> visitVoidFunction(VoidFunctionImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
for(Expression param: instr.expr()){
InstrAndVal result = param.accept(this, h);
l.addAll(result.instrs);
paramsLLVM.add(result.val);
}
ValueFunMap fun= h.getFunction(instr.nom());
if(fun == null){
System.err.println("Function n'est pas trouvé");
return l;
}
if (!(fun.define.type() instanceof VoidLLVMImp)){
System.err.println("Fonction n'est pas un void");
return l;
}
l.add(new CallVoidLLVMImp(fun.define,paramsLLVM,""));
return l;
}
@Override
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
@@ -345,6 +325,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return l;
}
@Override
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
@@ -379,6 +360,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return l;
}
@Override
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
@@ -412,8 +394,34 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return l;
}
@Override
public ArrayList<InstructionLLVM> visitVoidFunction(VoidFunctionImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
//EXPRESSION
for(Expression param: instr.expr()){
InstrAndVal result = param.accept(this, h);
l.addAll(result.instrs);
paramsLLVM.add(result.val);
}
ValueFunMap fun= h.getFunction(instr.nom());
if(fun == null){
System.err.println("Function n'est pas trouvé");
return l;
}
if (!(fun.define.type() instanceof VoidLLVMImp)){
System.err.println("Fonction n'est pas un void");
return l;
}
l.add(new CallVoidLLVMImp(fun.define,paramsLLVM,""));
return l;
}
//EXPRESSION -----------------------------------
@Override
public InstrAndVal visitConst(ConstImp e, SymTable h) {
@@ -465,12 +473,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
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
//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);
//}
ValueFunMap fLLVM = h.getFunction(instr.fName());
//Pour c=func(x,y)
if (fLLVM.define.type() instanceof VoidLLVMImp) {
l.add(new CallVoidLLVMImp(fLLVM.define, paramsLLVM, ""));
@@ -485,6 +489,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
}
}
//TYPE -----------------------------------
@Override
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
return new IntLLVMImp();