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

@@ -158,7 +158,7 @@ public class TypeChecking {
}
@Override
public TypeCheckExprDiag visitAppeal(Appeal instr, SymTable h) {
public TypeCheckExprDiag visitAppeal(AppealImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitAppeal'");
}

View File

@@ -55,7 +55,7 @@ public class Main {
// Generate the intermediate representation
//System.out.println("todo");
ProgramLLVMImpl astLLVM = ast.toLLVM();
ProgramLLVMImp astLLVM = ast.toLLVM();
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
System.out.println(astLLVM.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");

View File

@@ -8,7 +8,7 @@ public interface Interface{
public interface ProgramI {
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
public String prettyprinter();
public ProgramLLVMImpl toLLVM();
public ProgramLLVMImp toLLVM();
}
public interface ProgramVisitor<H,S> {
@@ -66,7 +66,7 @@ public interface Interface{
public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);
public S visitVar(VarImp e,H h);
public S visitAppeal(Appeal instr, H h);
public S visitAppeal(AppealImp instr, H h);
}
public interface Type{

View File

@@ -156,7 +156,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
}
@Override
public String visitAppeal(Appeal instr,String indent){
public String visitAppeal(AppealImp instr,String indent){
String str = indent + instr.fName() + "(";
for(int i=0; i<instr.params().size();i++){
str += instr.params().get(i).accept(this, "");

View File

@@ -18,7 +18,7 @@ public class Program{
}
@Override
public ProgramLLVMImpl toLLVM() {
public ProgramLLVMImp toLLVM() {
toLLVM_Visitor llvmVisitor = new toLLVM_Visitor();
return this.accept(llvmVisitor,new SymTable());
}
@@ -62,7 +62,7 @@ public class Program{
}
}
public static record Appeal(String fName, ArrayList<Expression> params) implements Expression{
public static record AppealImp(String fName, ArrayList<Expression> params) implements Expression{
@Override
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitAppeal(this, h);

View File

@@ -3,13 +3,13 @@ import java.util.Stack;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
import TP2.llvm.ProgramLLVM.DefineLLVMImpl;
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
public class SymTable {
private PMap<String,ValueTable> varMap;
private PMap<String,DefineLLVMImpl> fuctionsMap;
private PMap<String,ValueVarMap> varMap;
private PMap<String,ValueFunMap> fuctionsMap;
private int id=1;
private int idLabel = 1;
@@ -17,19 +17,28 @@ public class SymTable {
this.varMap= HashTreePMap.empty();
this.fuctionsMap = HashTreePMap.empty();
}
public SymTable(PMap<String,ValueTable> varMap,PMap<String,DefineLLVMImpl> fuctionsMap, int id, int idLabel){
public SymTable(PMap<String,ValueVarMap> varMap, PMap<String,ValueFunMap> fuctionsMap, int id, int idLabel){
this.varMap= varMap;
this.id = id;
this.fuctionsMap = fuctionsMap;
this.idLabel = idLabel;
}
public static class ValueFunMap{
public DefineLLVMImp define;
public Boolean isProto;
public static class ValueTable{
public ValueFunMap(DefineLLVMImp define, Boolean isProto){
this.define = define;
this.isProto = isProto;
}
}
public static class ValueVarMap{
public Type type;
public int id;
public Boolean isParam;
public ValueTable(Type type,int id, Boolean isParam){
public ValueVarMap(Type type,int id, Boolean isParam){
this.type = type;
this.id = id;
this.isParam = isParam;
@@ -52,14 +61,20 @@ public class SymTable {
public SymTable addFunction(DefineLLVMImpl function){
if(!this.fuctionsMap.containsKey(function.name())){
return new SymTable(this.varMap,this.fuctionsMap.plus(function.name(),function),this.id,this.idLabel);
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
ValueFunMap value = this.fuctionsMap.get(function.name());
if(value == null || (value!=null && value.isProto && !isProto)){
return new SymTable(this.varMap, this.fuctionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel);
}
else{
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
else if(isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" doit être déclaré avant son implémentation");
else System.err.println("[VSL compile error] : La fonction "+function.name()+" existe déjà");
return null;
}
return this;
}
public DefineLLVMImpl getFunction(String name){
public ValueFunMap getFunction(String name){
return this.fuctionsMap.get(name);
}
@@ -104,7 +119,7 @@ public class SymTable {
//retourne le nom de la var déjà déclaré avec son id
public String getVar(String nomVar){
String prefix = "";
ValueTable value = this.varMap.get(nomVar);
ValueVarMap value = this.varMap.get(nomVar);
if(value.isParam){
prefix = "param_";
}
@@ -129,8 +144,8 @@ public class SymTable {
public SymTable addVar(String s, Type t,Boolean isParam){
PMap<String, ValueTable> pmap = this.varMap;
pmap= pmap.plus(s,new ValueTable(t, /*getNewId()*/ id,isParam));
PMap<String, ValueVarMap> pmap = this.varMap;
pmap= pmap.plus(s,new ValueVarMap(t, /*getNewId()*/ id,isParam));
return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel);
}
@@ -141,11 +156,11 @@ public class SymTable {
str.append("Id = " + id+"\n");
str.append("VAR :\n");
for(String s: this.varMap.keySet()){
str.append(s).append(" ").append(varMap.get(s)).append("\n");
str.append(s).append(" id :").append(varMap.get(s).id).append(" type :").append(varMap.get(s).type).append(" isParam :").append(varMap.get(s).isParam).append("\n");
}
str.append("FUNCTION :\n");
for(String f: this.fuctionsMap.keySet()){
str.append(f).append(" ").append(fuctionsMap.get(f)).append("\n");
str.append(f).append("\n");
}
return str.toString();
}

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();
}
}

View File

@@ -9,7 +9,7 @@ public interface Interface {
}
public interface ProgramLLVMVisitor<H,S> {
public S visitProgramLLVM(ProgramLLVMImpl prog, H h);
public S visitProgramLLVM(ProgramLLVMImp prog, H h);
}
//////////DefineLLVM (function)
@@ -18,7 +18,7 @@ public interface Interface {
}
public interface DefineLLVMVisitor<H,S> {
public S visitDefineLLVM(DefineLLVMImpl define, H h);
public S visitDefineLLVM(DefineLLVMImp define, H h);
}
public interface InstructionLLVM{
@@ -26,15 +26,15 @@ public interface Interface {
}
public interface InstructionLLVMVisitor<H,S> {
public S visitReturnLLVM(ReturnLLVMImpl instr, H h);
public S visitAssignLLVM(AssignLVMImpl instr, H h);
public S visitStoreLLVM(StoreLLVMImpl instr, H h);
public S visitPrintLLVM(PrintLLVMImpl instr, H h);
public S visitReadLLVM(ReadLLVMImpl instr, H h);
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
public S visitAssignLLVM(AssignLVMImp instr, H h);
public S visitStoreLLVM(StoreLLVMImp instr, H h);
public S visitPrintLLVM(PrintLLVMImp instr, H h);
public S visitReadLLVM(ReadLLVMImp instr, H h);
public S visitLabelLLVM(LabelLLVMImp instr, H h);
public S visitBrLLVM(BrLLVMImp instr, H h);
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
public S visitCallLLVM(CallLLVMImpl instr, H h);
public S visitCallLLVM(CallLLVMImp instr, H h);
}
//////////ExpressionLLVM (expression)
@@ -47,11 +47,11 @@ public interface Interface {
}
public interface ExpressionLLVMVisitor<H,S> {
public S visitBinOpLLVM(BinOpLLVMImpl e, H h);
public S visitAllocaLLVM(allocaLLVMImpl e,H h);
public S visitLoadLLVM(LoadLLVMImpl e,H h);
public S visitValLLVM(ValLLVMImpl e,H h);
public S visitVarLLVM(VarLLVMImpl e,H h);
public S visitBinOpLLVM(BinOpLLVMImp e, H h);
public S visitAllocaLLVM(allocaLLVMImp e,H h);
public S visitLoadLLVM(LoadLLVMImp e,H h);
public S visitValLLVM(ValLLVMImp e,H h);
public S visitVarLLVM(VarLLVMImp e,H h);
public S visitIcmpLLVM(IcmpLLVMImp e, H h);
}
@@ -67,8 +67,8 @@ public interface Interface {
}
public interface TypeLLVMVisitor<H,S> {
public S visitIntLLVM(IntLLVMImpl e,H h);
public S visitVoidLLVM(VoidLLVMImpl e, H h);
public S visitIntLLVM(IntLLVMImp e,H h);
public S visitVoidLLVM(VoidLLVMImp e, H h);
public S visitBooleanLLVM(BooleanLLVMImp e, H h);
public S visitStringLLVM(StringLLVMImp e, H h);
}

View File

@@ -15,7 +15,7 @@ TypeLLVMVisitor<String,String>
static String INDENT = " ";
@Override
public String visitProgramLLVM(ProgramLLVMImpl prog, String indent) {
public String visitProgramLLVM(ProgramLLVMImp prog, String indent) {
StringBuilder str = new StringBuilder();
str.append("; Target\n");
str.append("target triple = \"x86_64-pc-linux-gnu\"\n");
@@ -40,13 +40,13 @@ TypeLLVMVisitor<String,String>
}
@Override
public String visitDefineLLVM(DefineLLVMImpl define, String indent) {
public String visitDefineLLVM(DefineLLVMImp define, String indent) {
StringBuilder str = new StringBuilder("define ");
str.append(define.type().accept(this,indent)).append(" @").append(define.name()).append("(");
List<VarLLVMImpl> params = define.params();
List<VarLLVMImp> params = define.params();
for (int i = 0; i < params.size(); i++) {
VarLLVMImpl param = params.get(i);
VarLLVMImp param = params.get(i);
str.append(param.getType().accept(this, indent)).append(" ").append(param.accept(this, indent));
if (i < params.size() - 1) str.append(", ");
}
@@ -60,17 +60,17 @@ TypeLLVMVisitor<String,String>
}
@Override
public String visitReturnLLVM(ReturnLLVMImpl instr, String h) {
public String visitReturnLLVM(ReturnLLVMImp instr, String h) {
return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h);
}
@Override
public String visitAssignLLVM(AssignLVMImpl instr, String h) {
public String visitAssignLLVM(AssignLVMImp instr, String h) {
return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h);
}
@Override
public String visitBinOpLLVM(BinOpLLVMImpl e, String h) {
public String visitBinOpLLVM(BinOpLLVMImp e, String h) {
String str = "";
switch(e.op()){
case PLUS:
@@ -100,23 +100,23 @@ TypeLLVMVisitor<String,String>
}
@Override
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
public String visitAllocaLLVM(allocaLLVMImp e, String h) {
return "alloca " + e.type().accept(this, h);
}
@Override
public String visitStoreLLVM(StoreLLVMImpl instr, String h) {
public String visitStoreLLVM(StoreLLVMImp instr, String h) {
return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,"");
}
@Override
public String visitLoadLLVM(LoadLLVMImpl e, String h) {
public String visitLoadLLVM(LoadLLVMImp e, String h) {
return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h);
}
@Override
public String visitCallLLVM(CallLLVMImpl instr, String h) {
public String visitCallLLVM(CallLLVMImp instr, String h) {
String str = INDENT+ "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "(";
for(int i = 0; i<instr.params().size(); i++){
str += instr.params().get(i).getType().accept(this, h) + " " + instr.params().get(i).accept(this,h);
@@ -126,16 +126,16 @@ TypeLLVMVisitor<String,String>
}
@Override
public String visitPrintLLVM(PrintLLVMImpl instr, String h) { //TODO
DefineLLVMImpl printLLVM = new DefineLLVMImpl("printf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>());
CallLLVMImpl callPrint = new CallLLVMImpl(printLLVM, new ArrayList<>(),"(i8*,...) ");
public String visitPrintLLVM(PrintLLVMImp instr, String h) { //TODO
DefineLLVMImp printLLVM = new DefineLLVMImp("printf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
CallLLVMImp callPrint = new CallLLVMImp(printLLVM, new ArrayList<>(),"(i8*,...) ");
return callPrint.accept(this, h);
}
@Override
public String visitReadLLVM(ReadLLVMImpl instr, String h) { //TODO
DefineLLVMImpl readLLVM = new DefineLLVMImpl("scanf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>());
CallLLVMImpl callRead = new CallLLVMImpl(readLLVM, instr.l(),"(i8*,...) ");
public String visitReadLLVM(ReadLLVMImp instr, String h) { //TODO
DefineLLVMImp readLLVM = new DefineLLVMImp("scanf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
CallLLVMImp callRead = new CallLLVMImp(readLLVM, instr.l(),"(i8*,...) ");
return callRead.accept(this, h);
}
@@ -158,22 +158,22 @@ TypeLLVMVisitor<String,String>
@Override
public String visitValLLVM(ValLLVMImpl e, String h) {
public String visitValLLVM(ValLLVMImp e, String h) {
return e.val() + "";
}
@Override
public String visitVarLLVM(VarLLVMImpl e, String h) {
public String visitVarLLVM(VarLLVMImp e, String h) {
return "%"+e.nom();
}
@Override
public String visitIntLLVM(IntLLVMImpl e, String h) {
public String visitIntLLVM(IntLLVMImp e, String h) {
return "i32";
}
@Override
public String visitVoidLLVM(VoidLLVMImpl e, String h) {
public String visitVoidLLVM(VoidLLVMImp e, String h) {
return "void";
}

View File

@@ -9,7 +9,7 @@ import TP2.llvm.Interface.*;
public class ProgramLLVM {
//Program
public static record ProgramLLVMImpl(ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
public static record ProgramLLVMImp(ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
public <H, S> S accept(ProgramLLVMVisitor<H, S> v, H h) {
return v.visitProgramLLVM(this, h);
}
@@ -22,7 +22,7 @@ public class ProgramLLVM {
//Define
public static record DefineLLVMImpl(String name, TypeLLVM type, ArrayList<VarLLVMImpl> params, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public static record DefineLLVMImp(String name, TypeLLVM type, ArrayList<VarLLVMImp> params, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
return v.visitDefineLLVM(this, h);
}
@@ -45,7 +45,7 @@ public class ProgramLLVM {
}
}
public static record BrCondLLVMImp(VarLLVMImpl var, String label, String labelElse) implements InstructionLLVM{
public static record BrCondLLVMImp(VarLLVMImp var, String label, String labelElse) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitBrCondLLVM(this, h);
@@ -53,42 +53,42 @@ public class ProgramLLVM {
}
public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
public static record AssignLVMImp(VarLLVMImp var, ExpressionLLVM e) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitAssignLLVM(this, h);
}
}
public static record ReturnLLVMImpl(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReturnLLVM(this, h);
}
}
public static record StoreLLVMImpl(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{
public static record StoreLLVMImp(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitStoreLLVM(this, h);
}
}
public static record CallLLVMImpl(DefineLLVMImpl f, ArrayList<ValLLVM> params, String str) implements InstructionLLVM{
public static record CallLLVMImp(DefineLLVMImp f, ArrayList<ValLLVM> params, String str) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitCallLLVM(this, h);
}
}
public static record PrintLLVMImpl(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print
public static record PrintLLVMImp(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitPrintLLVM(this, h);
}
}
public static record ReadLLVMImpl(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read
public static record ReadLLVMImp(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -97,7 +97,7 @@ public class ProgramLLVM {
}
//Expression :
public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitBinOpLLVM(this, h);
@@ -123,7 +123,7 @@ public class ProgramLLVM {
}*/
public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{
public static record allocaLLVMImp(TypeLLVM type) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitAllocaLLVM(this, h);
@@ -136,7 +136,7 @@ public class ProgramLLVM {
}
public static record LoadLLVMImpl(ValLLVM val) implements ExpressionLLVM{
public static record LoadLLVMImp(ValLLVM val) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitLoadLLVM(this, h);
@@ -163,7 +163,7 @@ public class ProgramLLVM {
//Val
public static record ValLLVMImpl(TypeLLVM type, int val) implements ValLLVM{
public static record ValLLVMImp(TypeLLVM type, int val) implements ValLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitValLLVM(this, h);
@@ -176,7 +176,7 @@ public class ProgramLLVM {
}
public static record VarLLVMImpl(TypeLLVM type, String nom) implements ValLLVM{
public static record VarLLVMImp(TypeLLVM type, String nom) implements ValLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitVarLLVM(this, h);
@@ -189,7 +189,7 @@ public class ProgramLLVM {
}
public static record IntLLVMImpl() implements TypeLLVM{
public static record IntLLVMImp() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitIntLLVM(this, h);
@@ -197,7 +197,7 @@ public class ProgramLLVM {
}
public static record VoidLLVMImpl() implements TypeLLVM{
public static record VoidLLVMImp() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitVoidLLVM(this, h);