return 0 à la fin, suppression du typeFunction dans la symTable, Appeal renomé en Call

This commit is contained in:
Rochas
2025-04-29 23:25:07 +02:00
parent 7e9b49b08a
commit d238ac0887
9 changed files with 67 additions and 108 deletions

View File

@@ -226,7 +226,7 @@ lit returns [Expression out]:
}
| t=ident
(
ParO (para=list_expression)? ParF {$out = new AppealImp($t.out,($para.out !=null) ?$para.out : new ArrayList<>());}
ParO (para=list_expression)? ParF {$out = new CallImp($t.out,($para.out !=null) ?$para.out : new ArrayList<>());}
| {$out=new VarImp($t.out);}
)
;

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

@@ -67,7 +67,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(AppealImp instr, H h);
public S visitCall(CallImp instr, H h);
}
public interface Type{

View File

@@ -189,7 +189,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
}
@Override
public String visitAppeal(AppealImp instr,String indent){
public String visitCall(CallImp 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

@@ -62,10 +62,10 @@ public class Program{
}
}
public static record AppealImp(String fName, ArrayList<Expression> params) implements Expression{
public static record CallImp(String fName, ArrayList<Expression> params) implements Expression{
@Override
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitAppeal(this, h);
return v.visitCall(this, h);
}
}

View File

@@ -15,7 +15,6 @@ 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];
@@ -27,12 +26,11 @@ public class SymTable {
this.id[2] = 1; //idGlobalVar
}
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap,
int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal,TypeLLVM type){
int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal){
this.varMap= varMap;
this.id = id;
this.functionsMap = functionsMap;
this.declarationsGlobal = declarationsGlobal;
this.currentFuncType=type;
}
public static class ValueFunMap{
@@ -45,18 +43,6 @@ public class SymTable {
}
}
public SymTable newBlock() {
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,currentFuncType);
} else {
return this;
}
}
public static class ValueVarMap{
public Type type;
public int id;
@@ -67,12 +53,18 @@ public class SymTable {
this.isParam = isParam;
}
}
/*public void update(SymTable symTable2){
this.id = symTable2.getId();
this.idLabel = symTable2.getIdLabel();
this.declarationsGlobal = symTable2.getDeclarationGlobal();
}*/
public SymTable newBlock() {
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal);
}
public SymTable outBlock() {
if (varMap.size() > 1) {
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal);
} else {
return this;
}
}
public ArrayList<DeclarGlobalLLVMImp> getDeclarationGlobal(){
return this.declarationsGlobal;
@@ -88,26 +80,25 @@ public class SymTable {
}
public SymTable addFunction(DefineLLVMImp function, Boolean isProto,TypeLLVM type){
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
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,type);
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id,this.declarationsGlobal);
}
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à");
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;
}
}
public ValueFunMap getFunction(String name){
return this.functionsMap.get(name);
ValueFunMap f = this.functionsMap.get(name);
if (f==null) System.err.println("[VSL compile error] : la fonction '" + name + "()' n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel");
return f;
}
public TypeLLVM getFuncType(){
return this.currentFuncType;
}
public int getId(){
return this.id[0];
}
@@ -152,13 +143,13 @@ public class SymTable {
public Result addVar(String nomVar) {
PMap<String, ValueVarMap> top = varMap.get(0);
if (top.containsKey(nomVar)) {
System.err.println("[VSL compile error] :" + "Erreur");
System.err.println("[VSL compile error] : '" + nomVar+ "' Erreur");
return new Result(this, null);
}
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,currentFuncType);
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal);
return new Result(newSym, realName);
}
@@ -174,7 +165,7 @@ public class SymTable {
return prefix + nomVar + value.id;
}
}
System.err.println("[VSL compile error] :" + nomVar+" n'est pas trovué");
System.err.println("[VSL compile error] : '" + nomVar + "' n'a pas été déclaré");
return null;
}
@@ -184,7 +175,7 @@ public class SymTable {
return true;
}
}
System.err.println("[VSL compile error] :" + nomVar+" n'est pas trouvé");
System.err.println("[VSL compile error] : '" + nomVar+"' n'est pas trouvé");
return false;
}
@@ -217,7 +208,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,currentFuncType);
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal);
}
public String print_all(){

View File

@@ -49,8 +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);
TypeLLVM type = prog.fonctions().get(i); //TODO
h = h.addFunction(function,isProto,type);
TypeLLVM type = function.type();
h = h.addFunction(function,isProto);
if(!isProto){ //les prototypes n'existent pas dans LLVM
fonctionLLVM.add(function);
}
@@ -73,7 +73,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
paramsLLVM.add(var);
}
instrLLVM.addAll(fun.instruction().accept(this, h));
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
TypeLLVM type = fun.type().accept(this, h);
if(!(type instanceof VoidLLVMImp)){ //on ajoute un return 0 si la fonction n'est pas un void, et ne finit ni par un return ni pas un goto (sans condition)
if(!(instrLLVM.getLast() instanceof ReturnLLVMImp || instrLLVM.getLast() instanceof BrLLVMImp)){
instrLLVM.add(new ReturnLLVMImp(type, new ValLLVMImp(type,0)));
}
}
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), type, paramsLLVM, instrLLVM);
return define;
}
@@ -273,9 +279,6 @@ 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;
}
@@ -388,7 +391,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
return new InstrAndVal(list, var);
}
public InstrAndVal visitAppeal(AppealImp instr,SymTable h){
public InstrAndVal visitCall(CallImp instr,SymTable h){
ArrayList<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
for(Expression param : instr.params()){
@@ -397,10 +400,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
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);
}
//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);
//}
//Pour c=func(x,y)
Result res = h.addNewTempVar();

View File

@@ -128,7 +128,7 @@ TypeLLVMVisitor<String,String>
@Override
public String visitCallLLVM(CallLLVMImp instr, String h) {
String str = INDENT+ "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "(";
String str = "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);
if(i<instr.params().size()-1) str += ", ";

View File

@@ -1,58 +1,23 @@
PROTO INT add(x,y)
FUNC INT test1(a,b){
a := a+b
}
FUNC INT add(x,y,z) {
INT z,a,b,c
y := x+y
RETURN z
FUNC INT test2(a,b){
IF a
THEN
RETURN a
FI
}
FUNC INT test3(a,b){
IF a
THEN
RETURN a
ELSE
RETURN b
FI
}
FUNC INT main(x,y) {
INT a,b,c,minh
x := 5
minh := x * y
b:=3
c:=1
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
WHILE b - 1
DO{
b := b - 1
c := c + 1
WHILE b -1
DO{
b:= b - 1
c:= c + 1
WHILE b -1
DO{
b:= b - 1
c:= c + 1
}
DONE
}
DONE
}
DONE
WHILE b - 1
DO{
b := b - 1
c := c + 1
WHILE b -1
DO{
b:= b - 1
c:= c + 1
WHILE b -1
DO{
b:= b - 1
c:= c + 1
}
DONE
}
DONE
}
DONE
IF c -1
THEN READ a ELSE READ b
FI
b:=c+1
RETURN 4 + 6 * 5 + 2 }
PROTO INT type(x,y)
x := test1(x,y)
}