diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index f8263fe..fe77088 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -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"); diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 34f774f..2dd98c0 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -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 functionsMap; private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : idLabel, [2] : idGlobalVar private ArrayList 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> varMap, PMap functionsMap, int[] id,ArrayList declarationsGlobal){ + public SymTable(PStack> varMap, PMap functionsMap, + int[] id,ArrayList 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(){ diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 0ce01c4..9f6a0c3 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -49,7 +49,8 @@ public class toLLVM_Visitor implements ProgramVisitor, for(int i = 0; i, @Override public ArrayList visitIfThen(IfThenImp instr, SymTable h) { ArrayList 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, 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 visitIfThenElse(IfThenElseImp instr, SymTable h) { ArrayList 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); diff --git a/tests/fragment0/add0 b/tests/fragment0/add0 new file mode 100755 index 0000000..9dfdd70 Binary files /dev/null and b/tests/fragment0/add0 differ diff --git a/tests/fragment0/add1 b/tests/fragment0/add1 new file mode 100755 index 0000000..b03d758 Binary files /dev/null and b/tests/fragment0/add1 differ diff --git a/tests/fragment0/const0 b/tests/fragment0/const0 new file mode 100755 index 0000000..1e56730 Binary files /dev/null and b/tests/fragment0/const0 differ diff --git a/tests/fragment0/const1 b/tests/fragment0/const1 new file mode 100755 index 0000000..8963a2c Binary files /dev/null and b/tests/fragment0/const1 differ diff --git a/tests/fragment0/div0 b/tests/fragment0/div0 new file mode 100755 index 0000000..313c5d0 Binary files /dev/null and b/tests/fragment0/div0 differ diff --git a/tests/fragment0/div1 b/tests/fragment0/div1 new file mode 100755 index 0000000..b37ffbb Binary files /dev/null and b/tests/fragment0/div1 differ diff --git a/tests/fragment0/mod b/tests/fragment0/mod new file mode 100755 index 0000000..d8a31cb Binary files /dev/null and b/tests/fragment0/mod differ diff --git a/tests/fragment0/mult1 b/tests/fragment0/mult1 new file mode 100755 index 0000000..a5c3dba Binary files /dev/null and b/tests/fragment0/mult1 differ diff --git a/tests/fragment0/mult2 b/tests/fragment0/mult2 new file mode 100755 index 0000000..5879018 Binary files /dev/null and b/tests/fragment0/mult2 differ diff --git a/tests/fragment0/paren b/tests/fragment0/paren new file mode 100755 index 0000000..8c2d236 Binary files /dev/null and b/tests/fragment0/paren differ diff --git a/tests/fragment0/priority1 b/tests/fragment0/priority1 new file mode 100755 index 0000000..b3776d2 Binary files /dev/null and b/tests/fragment0/priority1 differ diff --git a/tests/fragment0/priority2 b/tests/fragment0/priority2 new file mode 100755 index 0000000..38cc9eb Binary files /dev/null and b/tests/fragment0/priority2 differ diff --git a/tests/fragment0/sub0 b/tests/fragment0/sub0 new file mode 100755 index 0000000..a71c29d Binary files /dev/null and b/tests/fragment0/sub0 differ diff --git a/tests/fragment0/sub1 b/tests/fragment0/sub1 new file mode 100755 index 0000000..55e36b6 Binary files /dev/null and b/tests/fragment0/sub1 differ diff --git a/tests/fragment1/assign1 b/tests/fragment1/assign1 new file mode 100755 index 0000000..2c0cb1c Binary files /dev/null and b/tests/fragment1/assign1 differ diff --git a/tests/fragment1/assign2 b/tests/fragment1/assign2 new file mode 100755 index 0000000..2ba9eb3 Binary files /dev/null and b/tests/fragment1/assign2 differ diff --git a/tests/fragment1/block b/tests/fragment1/block new file mode 100755 index 0000000..458dda7 Binary files /dev/null and b/tests/fragment1/block differ diff --git a/tests/fragment1/decl b/tests/fragment1/decl new file mode 100755 index 0000000..60670d4 Binary files /dev/null and b/tests/fragment1/decl differ diff --git a/tests/fragment2/proto1 b/tests/fragment2/proto1 new file mode 100755 index 0000000..974577e Binary files /dev/null and b/tests/fragment2/proto1 differ