tesst bugged

This commit is contained in:
Vu Tuan Minh
2025-04-29 14:05:51 +02:00
parent 0929ce1b71
commit 7fc7eb773f
22 changed files with 29 additions and 18 deletions

View File

@@ -56,12 +56,12 @@ public class Main {
//System.err.println("todo " + ast);
//System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n");
System.out.println(ast.prettyprinter());
//System.out.println(ast.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n");
// Verify the program semantic
// Generate the intermediate representation
System.out.println("\n\n");
//System.out.println("\n\n");
ProgramLLVMImp astLLVM = ast.toLLVM();
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
import TP2.llvm.Interface.TypeLLVM;
import TP2.llvm.ProgramLLVM.DeclarGlobalLLVMImp;
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
@@ -14,7 +15,8 @@ public class SymTable {
private PMap<String,ValueFunMap> functionsMap;
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : idLabel, [2] : idGlobalVar
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal; //aussi partagé entre toute les symTable (.fmt pour les print et scan)
private TypeLLVM currentFuncType;
public SymTable(){
this.id = new int[3];
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
@@ -24,11 +26,13 @@ public class SymTable {
this.id[1] = 1; //idLaber
this.id[2] = 1; //idGlobalVar
}
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal){
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap,
int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal,TypeLLVM type){
this.varMap= varMap;
this.id = id;
this.functionsMap = functionsMap;
this.declarationsGlobal = declarationsGlobal;
this.currentFuncType=type;
}
public static class ValueFunMap{
@@ -42,12 +46,12 @@ public class SymTable {
}
public SymTable newBlock() {
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal);
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal,currentFuncType);
}
public SymTable outBlock() {
if (varMap.size() > 1) {
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal);
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal,currentFuncType);
} else {
return this;
}
@@ -84,10 +88,10 @@ public class SymTable {
}
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
public SymTable addFunction(DefineLLVMImp function, Boolean isProto,TypeLLVM type){
ValueFunMap value = this.functionsMap.get(function.name());
if(value == null || (value!=null && value.isProto && !isProto)){
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id,this.declarationsGlobal);
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id,this.declarationsGlobal,type);
}
else{
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
@@ -101,6 +105,9 @@ public class SymTable {
return this.functionsMap.get(name);
}
public TypeLLVM getFuncType(){
return this.currentFuncType;
}
public int getId(){
return this.id[0];
}
@@ -151,7 +158,7 @@ public class SymTable {
String realName = nomVar + id[0];
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id[0], false));
id[0]++;
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal);
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal,currentFuncType);
return new Result(newSym, realName);
}
@@ -210,7 +217,7 @@ public class SymTable {
}
top = top.plus(nomVar, new ValueVarMap(type, id[0], isParam));
if(!isParam) this.id[0]++;
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal);
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal,currentFuncType);
}
public String print_all(){

View File

@@ -49,7 +49,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
for(int i = 0; i<prog.fonctions().size(); i++){
DefineLLVMImp function = prog.fonctions().get(i).accept(this, h);
Boolean isProto = (prog.fonctions().get(i) instanceof PrototypeImp);
h = h.addFunction(function,isProto);
TypeLLVM type = prog.fonctions().get(i); //TODO
h = h.addFunction(function,isProto,type);
if(!isProto){ //les prototypes n'existent pas dans LLVM
fonctionLLVM.add(function);
}
@@ -250,9 +251,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
@Override
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
String labelFin= "fin"+h.getNewIdLabel();
String labelIf= "if"+h.getNewIdLabel();
String labelThen= "then"+h.getNewIdLabel();
String labelFin= "fi"+h.getNewIdLabel();
l.add(new LabelLLVMImp(labelIf));
InstrAndVal temp = instr.e().accept(this,h);
@@ -272,16 +273,19 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
h=h.outBlock();
l.add(new LabelLLVMImp(labelFin));
if (h.getFuncType() instanceof IntLLVMImp) {
l.add(new ReturnLLVMImp(new IntLLVMImp(), new ValLLVMImp(new IntLLVMImp(), 0)));
}
return l;
}
@Override
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
String labelElse= "else"+h.getNewIdLabel()+":";
String labelFin= "fin"+h.getNewIdLabel();
String labelIf= "if"+h.getNewIdLabel();
String labelThen= "then"+h.getNewIdLabel();
String labelElse= "else"+h.getNewIdLabel();
String labelFin= "fi"+h.getNewIdLabel();
l.add(new LabelLLVMImp(labelIf));
InstrAndVal temp = instr.e().accept(this,h);