156 lines
5.1 KiB
Java
156 lines
5.1 KiB
Java
package TP2.asd;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import TP2.asd.Interface.*;
|
|
import TP2.asd.Program.*;
|
|
import TP2.asd.SymTable.*;
|
|
import TP2.llvm.Interface.*;
|
|
import TP2.llvm.ProgramLLVM.*;
|
|
|
|
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
|
FunctionVisitor<SymTable,DefineLLVM>,
|
|
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
|
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
|
|
TypeVisitor<SymTable,TypeLLVM>
|
|
{
|
|
|
|
|
|
/*
|
|
pour les Expression,
|
|
toLLVM ne renvoit pas la même chose si l'expression est
|
|
une simplement un val (var ou const) ou un binop
|
|
*/
|
|
public static class InstrAndVal{
|
|
public ArrayList<AssignLVMImp> instr = null;
|
|
public ValLLVM val = null;
|
|
|
|
public InstrAndVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
|
|
this.instr = instr;
|
|
this.val = val;
|
|
}
|
|
}
|
|
|
|
//PROGRAM
|
|
|
|
@Override
|
|
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
|
|
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
|
for(int i = 0; i<prog.fonctions().size(); i++){
|
|
fonctionLLVM.add(prog.fonctions().get(i).accept(this, h));
|
|
}
|
|
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
|
|
}
|
|
|
|
//FUNCTION
|
|
|
|
@Override
|
|
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
|
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
|
for(int i = 0; i<fun.instructions().size(); i++){
|
|
instrLLVM.addAll(fun.instructions().get(i).accept(this, h));
|
|
}
|
|
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
|
|
}
|
|
|
|
//INSTRUCTION
|
|
|
|
@Override
|
|
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
|
|
InstrAndVal res = instr.e().accept(this,h);
|
|
ValLLVM var = res.val;
|
|
|
|
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
|
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
|
result.addAll(res.instr);
|
|
result.add(r);
|
|
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
|
InstrAndVal res = instr.e().accept(this,h);
|
|
ValLLVM var = res.val;
|
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
|
result.addAll(res.instr);
|
|
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
|
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"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));
|
|
list.add(new AssignLVMImp(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
|
l.add(new PrintLLVMImp(new ArrayList())); //TODO
|
|
return l;
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
|
|
// TODO Auto-generated method stub
|
|
throw new UnsupportedOperationException("Unimplemented method 'visitRead'");
|
|
}
|
|
|
|
//EXPRESSION
|
|
|
|
@Override
|
|
public InstrAndVal visitConst(ConstImp e, SymTable h) {
|
|
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
|
|
return new InstrAndVal(new ArrayList<>(), val);
|
|
}
|
|
|
|
@Override
|
|
public InstrAndVal visitVar(VarImp e, SymTable h) {
|
|
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
|
|
return new InstrAndVal(new ArrayList<>(), val);
|
|
}
|
|
|
|
@Override
|
|
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
|
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
|
|
|
InstrAndVal res1 = e.e1().accept(this, h);
|
|
InstrAndVal res2 = e.e2().accept(this, h);
|
|
|
|
ValLLVM val1 = res1.val;
|
|
ValLLVM val2 = res2.val;
|
|
|
|
list.addAll(res1.instr);
|
|
list.addAll(res2.instr);
|
|
|
|
|
|
TypeLLVM type = val1.getType();
|
|
if(val1.getType().getClass() != val1.getType().getClass()){
|
|
throw new UnsupportedOperationException("Type error in VSL file");
|
|
}
|
|
String temp = h.addNewTempVar();
|
|
VarLLVMImpl var = new VarLLVMImpl(type,temp);
|
|
list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2)));
|
|
|
|
return new InstrAndVal(list, var);
|
|
}
|
|
|
|
@Override
|
|
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
|
|
return new IntLLVMImpl();
|
|
}
|
|
|
|
@Override
|
|
public TypeLLVM visitVoid(Type_voidImp t, SymTable h) {
|
|
return new VoidLLVMImpl();
|
|
}
|
|
}
|