correction SymTable
This commit is contained in:
@@ -56,7 +56,7 @@ public class TypeChecking {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
||||||
if(!h.searchVar(instr.t())){
|
if(!h.isPresentVar(instr.t())){
|
||||||
return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
||||||
}
|
}
|
||||||
Type t_type=h.getvar_Type(instr.t());
|
Type t_type=h.getvar_Type(instr.t());
|
||||||
@@ -85,7 +85,7 @@ public class TypeChecking {
|
|||||||
@Override
|
@Override
|
||||||
public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
||||||
for(VarImp v: instr.t()){
|
for(VarImp v: instr.t()){
|
||||||
if(!h.searchVar(v.name())){
|
if(!h.isPresentVar(v.name())){
|
||||||
return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,8 +134,8 @@ public class TypeChecking {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
||||||
if(!h.searchVar(e.name())){
|
if(!h.isPresentVar(e.name())){
|
||||||
return TypeCheckExprDiag.error("Ce variable n'existe pas");
|
return TypeCheckExprDiag.error("Cette variable n'existe pas");
|
||||||
}
|
}
|
||||||
Type e_type= h.getvar_Type(e.name());
|
Type e_type= h.getvar_Type(e.name());
|
||||||
return TypeCheckExprDiag.checked(e_type);
|
return TypeCheckExprDiag.checked(e_type);
|
||||||
|
|||||||
@@ -8,11 +8,23 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImpl;
|
|||||||
|
|
||||||
public class SymTable {
|
public class SymTable {
|
||||||
|
|
||||||
private PStack<PMap<String,ValueTable>> stackMap;
|
private PMap<String,ValueTable> varMap;
|
||||||
private PMap<String,DefineLLVMImpl> fuctionsMap;
|
private PMap<String,DefineLLVMImpl> fuctionsMap;
|
||||||
private int id=1;
|
private int id=1;
|
||||||
private int idLabel = 1;
|
private int idLabel = 1;
|
||||||
|
|
||||||
|
public SymTable(){
|
||||||
|
this.varMap= HashTreePMap.empty();
|
||||||
|
this.fuctionsMap = HashTreePMap.empty();
|
||||||
|
}
|
||||||
|
public SymTable(PMap<String,ValueTable> varMap,PMap<String,DefineLLVMImpl> fuctionsMap, int id, int idLabel){
|
||||||
|
this.varMap= varMap;
|
||||||
|
this.id = id;
|
||||||
|
this.fuctionsMap = fuctionsMap;
|
||||||
|
this.idLabel = idLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ValueTable{
|
public static class ValueTable{
|
||||||
public Type type;
|
public Type type;
|
||||||
public int id;
|
public int id;
|
||||||
@@ -23,6 +35,11 @@ public class SymTable {
|
|||||||
this.isParam = isParam;
|
this.isParam = isParam;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateId(SymTable symTable2){
|
||||||
|
this.id = symTable2.getId();
|
||||||
|
this.idLabel = symTable2.getIdLabel();
|
||||||
|
}
|
||||||
|
|
||||||
public static class Result{
|
public static class Result{
|
||||||
public SymTable symTable;
|
public SymTable symTable;
|
||||||
@@ -33,26 +50,27 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SymTable(){
|
|
||||||
this.stackMap= ConsPStack.empty(); //todo : HashTreePMap.empty() stack sers à rien
|
|
||||||
this.fuctionsMap = HashTreePMap.empty();
|
|
||||||
}
|
|
||||||
public SymTable(PStack<PMap<String,ValueTable>> stackMap, int id){
|
|
||||||
this.stackMap= stackMap;
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void addFunction(DefineLLVMImpl function){
|
public SymTable addFunction(DefineLLVMImpl function){
|
||||||
if(!this.fuctionsMap.containsKey(function.name())){
|
if(!this.fuctionsMap.containsKey(function.name())){
|
||||||
this.fuctionsMap.plus(function.name(),function);
|
return new SymTable(this.varMap,this.fuctionsMap.plus(function.name(),function),this.id,this.idLabel);
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefineLLVMImpl getFunction(String name){
|
public DefineLLVMImpl getFunction(String name){
|
||||||
return this.fuctionsMap.get(name);
|
return this.fuctionsMap.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId(){
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdLabel(){
|
||||||
|
return this.idLabel;
|
||||||
|
}
|
||||||
|
|
||||||
public int getNewId(){
|
public int getNewId(){
|
||||||
int a = this.id;
|
int a = this.id;
|
||||||
this.id++;
|
this.id++;
|
||||||
@@ -65,10 +83,9 @@ public class SymTable {
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result addNewTempVar(/*Type type*/){
|
public Result addNewTempVar(){
|
||||||
//TODO
|
|
||||||
String newVar = "temp"+id;
|
String newVar = "temp"+id;
|
||||||
SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false); //TODO
|
SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false);
|
||||||
return new Result(newSymTab,newVar);
|
return new Result(newSymTab,newVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,85 +97,55 @@ public class SymTable {
|
|||||||
|
|
||||||
public Result addVar(String nomVar){
|
public Result addVar(String nomVar){
|
||||||
String newVar = nomVar+id;
|
String newVar = nomVar+id;
|
||||||
SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); //TODO
|
SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false);
|
||||||
return new Result(newSymTab,newVar);
|
return new Result(newSymTab,newVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
//retourne le nom de la var déjà déclaré avec son id
|
//retourne le nom de la var déjà déclaré avec son id
|
||||||
public String getVar(String nomVar){
|
public String getVar(String nomVar){
|
||||||
String prefix = "";
|
String prefix = "";
|
||||||
ValueTable value = this.stackMap.getLast().get(nomVar);
|
ValueTable value = this.varMap.get(nomVar);
|
||||||
if(value.isParam){
|
if(value.isParam){
|
||||||
prefix = "param_";
|
prefix = "param_";
|
||||||
}
|
}
|
||||||
return prefix + nomVar + value.id;
|
return prefix + nomVar + value.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
//retourne le type de la var
|
public Boolean isPresentVar(String nomVar){
|
||||||
public Type getType(String nomVar){
|
return this.varMap.containsKey("nomVar");
|
||||||
return this.stackMap.getLast().get(nomVar).type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PStack<PMap<String,ValueTable>> next_layer(){
|
|
||||||
return stackMap.plus(HashTreePMap.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
public PMap<String,ValueTable> peppapeek(){
|
|
||||||
if(stackMap.isEmpty()){
|
|
||||||
return this.next_layer().getLast();
|
|
||||||
}
|
|
||||||
return stackMap.getLast();
|
|
||||||
}
|
|
||||||
|
|
||||||
public SymTable addVar(String s, Type t,Boolean isParam){
|
|
||||||
PStack<PMap<String,ValueTable>> newpstack = null;
|
|
||||||
if(this.stackMap.isEmpty()){
|
|
||||||
newpstack = this.next_layer();
|
|
||||||
}
|
|
||||||
else newpstack = this.stackMap;
|
|
||||||
|
|
||||||
//Save temporary if not PMap wont save
|
|
||||||
PMap<String, ValueTable> pmap = this.peppapeek();
|
|
||||||
pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId(),isParam));
|
|
||||||
//this.id++;
|
|
||||||
//Delete old ones
|
|
||||||
newpstack = newpstack.minus(newpstack.indexOf(newpstack.getLast()));
|
|
||||||
//Push the new one
|
|
||||||
newpstack = newpstack.plus(pmap);
|
|
||||||
return new SymTable(newpstack,this.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Usually look for var in highest level , if not found research.
|
|
||||||
public boolean searchVar(String s){
|
|
||||||
//TOTO
|
|
||||||
for(int i= stackMap.size()-1; i>=0; i--){
|
|
||||||
if(stackMap.get(i).containsKey(s)){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Stack<PMap<String,ValueTable>> stackmap(){
|
|
||||||
return this.stackmap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getvar_Type(String s){
|
public Type getvar_Type(String s){
|
||||||
for(int i= stackMap.size()-1; i>=0; i--){
|
if(this.varMap.containsKey(s)){
|
||||||
if(stackMap.get(i).containsKey(s)){
|
return this.varMap.get(s).type;
|
||||||
return stackMap.get(i).get(s).type;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//retourne le type de la var
|
||||||
|
public Type getType(String nomVar){
|
||||||
|
return this.varMap.get(nomVar).type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public String print_all(){
|
public String print_all(){
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
for(int i= stackMap.size()-1; i>=0; i--){
|
str.append("Id = " + id+"\n");
|
||||||
str.append("level ").append(i).append("\n");
|
str.append("VAR :\n");
|
||||||
for(String s: stackMap.get(i).keySet()){
|
for(String s: this.varMap.keySet()){
|
||||||
str.append(s).append(" ").append(stackMap.get(i).get(s)).append("\n");
|
str.append(s).append(" ").append(varMap.get(s)).append("\n");
|
||||||
}
|
}
|
||||||
|
str.append("FUNCTION :\n");
|
||||||
|
for(String f: this.fuctionsMap.keySet()){
|
||||||
|
str.append(f).append(" ").append(fuctionsMap.get(f)).append("\n");
|
||||||
}
|
}
|
||||||
return str.toString();
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
this.symTable = symTable;
|
this.symTable = symTable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//PROGRAM
|
//PROGRAM
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,8 +48,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
||||||
for(int i = 0; i<prog.fonctions().size(); i++){
|
for(int i = 0; i<prog.fonctions().size(); i++){
|
||||||
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
|
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
|
||||||
h.addFunction(function);
|
//h = h.addFunction(function);
|
||||||
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){
|
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){ //les prototypes n'existent pas dans LLVM
|
||||||
fonctionLLVM.add(function);
|
fonctionLLVM.add(function);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,9 +61,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
|
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
|
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
|
||||||
|
|
||||||
for(VarImp param: fun.params()){
|
for(VarImp param: fun.params()){
|
||||||
Result r = h.addParam(param.name());
|
Result r = h.addParam(param.name());
|
||||||
String name = r.var;
|
String name = r.var;
|
||||||
@@ -70,9 +71,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
h = r.symTable;
|
h = r.symTable;
|
||||||
paramsLLVM.add(var);
|
paramsLLVM.add(var);
|
||||||
}
|
}
|
||||||
|
|
||||||
instrLLVM.addAll(fun.instruction().accept(this, h));
|
instrLLVM.addAll(fun.instruction().accept(this, h));
|
||||||
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
|
DefineLLVMImpl define = new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
|
||||||
|
prevSymTable.updateId(h);
|
||||||
|
return define;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -84,6 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||||
for(int i = 0; i<instr.s().size();i++){
|
for(int i = 0; i<instr.s().size();i++){
|
||||||
TypeLLVM t2 = instr.t().accept(this,h);
|
TypeLLVM t2 = instr.t().accept(this,h);
|
||||||
@@ -93,6 +96,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
h = r.symTable;
|
h = r.symTable;
|
||||||
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
||||||
}
|
}
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return new InstrAndSymTable(list,h);
|
return new InstrAndSymTable(list,h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +113,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
for(int i = 0; i<instr.decls().size(); i++){
|
for(int i = 0; i<instr.decls().size(); i++){
|
||||||
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
|
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
|
||||||
@@ -118,6 +123,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
for(int i = 0; i<instr.instrs().size(); i++){
|
for(int i = 0; i<instr.instrs().size(); i++){
|
||||||
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
|
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
|
||||||
}
|
}
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return instrLLVM;
|
return instrLLVM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,6 +178,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
String labelIf= "if"+h.getNewIdLabel()+":";
|
String labelIf= "if"+h.getNewIdLabel()+":";
|
||||||
String labelThen= "then"+h.getNewIdLabel()+":";
|
String labelThen= "then"+h.getNewIdLabel()+":";
|
||||||
@@ -182,9 +189,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.addAll(temp.instrs);
|
l.addAll(temp.instrs);
|
||||||
ValLLVM val = temp.val;
|
ValLLVM val = temp.val;
|
||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
||||||
Result temp2 = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
h = temp2.symTable;
|
h = r.symTable;
|
||||||
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
|
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
|
||||||
l.add(new AssignLVMImpl(varCond,exTemp));
|
l.add(new AssignLVMImpl(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
|
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
|
||||||
|
|
||||||
@@ -192,12 +199,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.addAll(instr.i1().accept(this,h));
|
l.addAll(instr.i1().accept(this,h));
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelFin));
|
l.add(new LabelLLVMImp(labelFin));
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
String labelIf= "if"+h.getNewIdLabel()+":";
|
String labelIf= "if"+h.getNewIdLabel()+":";
|
||||||
String labelThen= "then"+h.getNewIdLabel()+":";
|
String labelThen= "then"+h.getNewIdLabel()+":";
|
||||||
@@ -209,9 +217,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.addAll(temp.instrs);
|
l.addAll(temp.instrs);
|
||||||
ValLLVM val = temp.val;
|
ValLLVM val = temp.val;
|
||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
||||||
Result temp2 = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
h = temp2.symTable;
|
h = r.symTable;
|
||||||
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
|
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
|
||||||
l.add(new AssignLVMImpl(varCond,exTemp));
|
l.add(new AssignLVMImpl(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
|
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
|
||||||
|
|
||||||
@@ -223,13 +231,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.addAll(instr.i2().accept(this,h));
|
l.addAll(instr.i2().accept(this,h));
|
||||||
l.add(new BrLLVMImp(labelFin));
|
l.add(new BrLLVMImp(labelFin));
|
||||||
|
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelFin));
|
l.add(new LabelLLVMImp(labelFin));
|
||||||
|
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
|
|
||||||
String labelWhile = "while"+h.getNewIdLabel()+":";
|
String labelWhile = "while"+h.getNewIdLabel()+":";
|
||||||
@@ -241,8 +251,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.addAll(temp.instrs); //instructions
|
l.addAll(temp.instrs); //instructions
|
||||||
ValLLVM val = temp.val; //temp6
|
ValLLVM val = temp.val; //temp6
|
||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
|
||||||
Result temp2 = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
|
h = r.symTable;
|
||||||
|
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
|
||||||
l.add(new AssignLVMImpl(varCond,exTemp));
|
l.add(new AssignLVMImpl(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
|
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
|
||||||
|
|
||||||
@@ -252,7 +263,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
l.add(new BrLLVMImp(labelWhile));
|
l.add(new BrLLVMImp(labelWhile));
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelDone));
|
l.add(new LabelLLVMImp(labelDone));
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,18 +278,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrAndVal visitVar(VarImp e, SymTable h) {
|
public InstrAndVal visitVar(VarImp e, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> l =new ArrayList<>();
|
ArrayList<InstructionLLVM> l =new ArrayList<>();
|
||||||
ValLLVM val = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name()));
|
ValLLVM val = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name()));
|
||||||
|
Result r = h.addNewTempVar();
|
||||||
VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.addNewTempVar().var);
|
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)))));
|
l.add(new AssignLVMImpl(varTemp,((ExpressionLLVM)(new LoadLLVMImpl(val)))));
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return new InstrAndVal(l, varTemp);
|
return new InstrAndVal(l, varTemp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
|
SymTable prevSymTable = h;
|
||||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||||
|
|
||||||
InstrAndVal res1 = e.e1().accept(this, h);
|
InstrAndVal res1 = e.e1().accept(this, h);
|
||||||
InstrAndVal res2 = e.e2().accept(this, h);
|
InstrAndVal res2 = e.e2().accept(this, h);
|
||||||
|
|
||||||
@@ -298,7 +312,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
h = r.symTable;
|
h = r.symTable;
|
||||||
VarLLVMImpl var = new VarLLVMImpl(type,temp);
|
VarLLVMImpl var = new VarLLVMImpl(type,temp);
|
||||||
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));
|
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));
|
||||||
|
prevSymTable.updateId(h);
|
||||||
return new InstrAndVal(list, var);
|
return new InstrAndVal(list, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ TypeLLVMVisitor<String,String>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
||||||
return "alloca" + e.type().accept(this, h);
|
return "alloca " + e.type().accept(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
58
tests/aLaMain2.vsl
Normal file
58
tests/aLaMain2.vsl
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
PROTO INT add(x,y)
|
||||||
|
|
||||||
|
FUNC INT add(x,y,z) {
|
||||||
|
INT z,a,b,c
|
||||||
|
y := x+y
|
||||||
|
RETURN z
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user