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

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