maj SymTable (id global) + DeclGlobal
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package TP2.asd;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.pcollections.*;
|
||||
import TP2.asd.Interface.Type;
|
||||
import TP2.asd.Program.Type_intImp;
|
||||
import TP2.llvm.ProgramLLVM.DeclarGlobalLLVMImp;
|
||||
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
|
||||
|
||||
|
||||
@@ -9,18 +12,22 @@ public class SymTable {
|
||||
|
||||
private PStack<PMap<String,ValueVarMap>> varMap;
|
||||
private PMap<String,ValueFunMap> functionsMap;
|
||||
private int id=1;
|
||||
private int idLabel = 1;
|
||||
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : id Label, [2] : global
|
||||
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal;
|
||||
|
||||
public SymTable(){
|
||||
public SymTable(){
|
||||
this.id = new int[3];
|
||||
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
|
||||
this.functionsMap = HashTreePMap.empty();
|
||||
this.declarationsGlobal = new ArrayList<>();
|
||||
this.id[0] = 1;
|
||||
this.id[1] = 1;
|
||||
}
|
||||
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int id, int idLabel){
|
||||
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal){
|
||||
this.varMap= varMap;
|
||||
this.id = id;
|
||||
this.functionsMap = functionsMap;
|
||||
this.idLabel = idLabel;
|
||||
this.declarationsGlobal = declarationsGlobal;
|
||||
}
|
||||
|
||||
public static class ValueFunMap{
|
||||
@@ -34,14 +41,13 @@ public class SymTable {
|
||||
}
|
||||
|
||||
public SymTable newBlock() {
|
||||
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id, idLabel);
|
||||
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, idLabel);
|
||||
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal);
|
||||
} else {
|
||||
System.err.println("Vide");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -57,9 +63,14 @@ public class SymTable {
|
||||
}
|
||||
}
|
||||
|
||||
public void updateId(SymTable symTable2){
|
||||
/*public void update(SymTable symTable2){
|
||||
this.id = symTable2.getId();
|
||||
this.idLabel = symTable2.getIdLabel();
|
||||
this.declarationsGlobal = symTable2.getDeclarationGlobal();
|
||||
}*/
|
||||
|
||||
public ArrayList<DeclarGlobalLLVMImp> getDeclarationGlobal(){
|
||||
return this.declarationsGlobal;
|
||||
}
|
||||
|
||||
public static class Result{
|
||||
@@ -75,7 +86,7 @@ public class SymTable {
|
||||
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.idLabel);
|
||||
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à");
|
||||
@@ -90,46 +101,56 @@ public class SymTable {
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return this.id;
|
||||
return this.id[0];
|
||||
}
|
||||
|
||||
public int getIdLabel(){
|
||||
return this.idLabel;
|
||||
return this.id[1];
|
||||
}
|
||||
|
||||
public int getNewId(){
|
||||
int a = this.id;
|
||||
this.id++;
|
||||
int a = this.id[0];
|
||||
this.id[0]++;
|
||||
return a;
|
||||
}
|
||||
|
||||
public int getNewIdLabel(){
|
||||
int a = this.idLabel;
|
||||
this.idLabel++;
|
||||
int a = this.id[1];
|
||||
this.id[1]++;
|
||||
return a;
|
||||
}
|
||||
|
||||
public Result addNewTempVar(){
|
||||
String newVar = "temp"+id;
|
||||
String newVar = "temp"+id[0];
|
||||
SymTable newSymTab = this.addVarInTab(newVar,new Type_intImp(),false);
|
||||
return new Result(newSymTab,newVar);
|
||||
}
|
||||
|
||||
public Result addParam(String nomParam){
|
||||
String newParam = "param_"+nomParam+id;
|
||||
String newParam = "param_"+nomParam;
|
||||
SymTable newSymTab = this.addVarInTab(nomParam,new Type_intImp(),true);
|
||||
return new Result(newSymTab,newParam);
|
||||
}
|
||||
|
||||
public String getGlobalDeclName(){;
|
||||
return "fmt"+id[2];
|
||||
}
|
||||
|
||||
public void addGlobalDecl(DeclarGlobalLLVMImp decl){
|
||||
id[2]++;
|
||||
this.declarationsGlobal.add(decl);
|
||||
}
|
||||
|
||||
public Result addVar(String nomVar) {
|
||||
PMap<String, ValueVarMap> top = varMap.get(0);
|
||||
if (top.containsKey(nomVar)) {
|
||||
System.err.println("[VSL compile error] :" + "Erreur");
|
||||
return new Result(this, null);
|
||||
}
|
||||
String realName = nomVar + id;
|
||||
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false));
|
||||
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
||||
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);
|
||||
return new Result(newSym, realName);
|
||||
}
|
||||
|
||||
@@ -186,13 +207,14 @@ public class SymTable {
|
||||
System.err.println(nomVar+ " déjà déclaré.");
|
||||
return this;
|
||||
}
|
||||
top = top.plus(nomVar, new ValueVarMap(type, id, isParam));
|
||||
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
||||
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);
|
||||
}
|
||||
|
||||
public String print_all(){
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append("Id = ").append(id).append("\n");
|
||||
str.append("Id = ").append(id[0]).append("\n");
|
||||
str.append("VARIABLES:\n");
|
||||
|
||||
int scopeLevel = varMap.size();
|
||||
|
||||
@@ -54,8 +54,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
fonctionLLVM.add(function);
|
||||
}
|
||||
}
|
||||
System.out.println(h.print_all());
|
||||
return new ProgramLLVMImp(new ArrayList<>(),fonctionLLVM);
|
||||
return new ProgramLLVMImp(h.getDeclarationGlobal(),fonctionLLVM);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,19 +62,17 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
|
||||
@Override
|
||||
public DefineLLVMImp visitFunction(FunctionImp fun, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||
ArrayList<VarLLVMImp> paramsLLVM = new ArrayList<>();
|
||||
for(VarImp param: fun.params()){
|
||||
Result r = h.addParam(param.name());
|
||||
String name = r.var;
|
||||
VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name);
|
||||
VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name,false);
|
||||
h = r.symTable;
|
||||
paramsLLVM.add(var);
|
||||
}
|
||||
instrLLVM.addAll(fun.instruction().accept(this, h));
|
||||
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
|
||||
prevSymTable.updateId(h);
|
||||
return define;
|
||||
}
|
||||
|
||||
@@ -88,7 +85,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
|
||||
@Override
|
||||
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||
for(int i = 0; i<instr.s().size();i++){
|
||||
TypeLLVM t2 = instr.t().accept(this,h);
|
||||
@@ -96,9 +92,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
Result r = h.addVar(instr.s().get(i));
|
||||
String name = r.var;
|
||||
h = r.symTable;
|
||||
list.add(new AssignLLVMImp(new VarLLVMImp(t2, name),new allocaLLVMImp(t2)));
|
||||
list.add(new AssignLLVMImp(new VarLLVMImp(t2, name,false),new allocaLLVMImp(t2)));
|
||||
}
|
||||
prevSymTable.updateId(h);
|
||||
return new InstrAndSymTable(list,h);
|
||||
}
|
||||
|
||||
@@ -106,7 +101,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
|
||||
SymTable prev = h;
|
||||
h= h.newBlock();
|
||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<instr.instrs().size(); i++){
|
||||
@@ -114,13 +108,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
}
|
||||
|
||||
h= h.outBlock();
|
||||
prev.updateId(h);
|
||||
return instrLLVM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
|
||||
SymTable prev = h;
|
||||
|
||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<instr.decls().size(); i++){
|
||||
@@ -132,7 +124,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
|
||||
}
|
||||
|
||||
prev.updateId(h);
|
||||
return instrLLVM;
|
||||
}
|
||||
|
||||
@@ -157,7 +148,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||
result.addAll(res.instrs);
|
||||
//InstructionLLVM r = new AssignLLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
||||
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t())));
|
||||
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImp(var.getType(),h.getVar(instr.t()),false));
|
||||
result.add(r);
|
||||
return result;
|
||||
}
|
||||
@@ -167,7 +158,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||
ArrayList<ValLLVM> params = new ArrayList<>();
|
||||
l.add(new PrintLLVMImp(params)); //TODO
|
||||
|
||||
String name = h.getGlobalDeclName();
|
||||
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(name);
|
||||
VarLLVMImp varGlobal = new VarLLVMImp(new StringLLVMImp(),name,true);
|
||||
h.addGlobalDecl(globalDecl);
|
||||
|
||||
params.add(varGlobal);
|
||||
|
||||
l.add(new PrintLLVMImp(globalDecl,params));
|
||||
return l;
|
||||
}
|
||||
|
||||
@@ -177,10 +176,18 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
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());
|
||||
VarLLVMImp newVar = new VarLLVMImp(typeVar.accept(this,h), nomVar);
|
||||
VarLLVMImp newVar = new VarLLVMImp(typeVar.accept(this,h), nomVar,false);
|
||||
ArrayList<ValLLVM> params = new ArrayList<>();
|
||||
|
||||
String name = h.getGlobalDeclName();
|
||||
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(name);
|
||||
VarLLVMImp varGlobal = new VarLLVMImp(new StringLLVMImp(),name,true);
|
||||
h.addGlobalDecl(globalDecl);
|
||||
|
||||
params.add(varGlobal);
|
||||
params.add(newVar);
|
||||
l.add(new ReadLLVMImp(params));
|
||||
|
||||
l.add(new ReadLLVMImp(globalDecl,params));
|
||||
}
|
||||
return l;
|
||||
}
|
||||
@@ -212,7 +219,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||
String labelIf= "if"+h.getNewIdLabel()+":";
|
||||
String labelThen= "then"+h.getNewIdLabel()+":";
|
||||
@@ -225,7 +231,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||
Result r = h.addNewTempVar();
|
||||
h = r.symTable;
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var);
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false);
|
||||
l.add(new AssignLLVMImp(varCond,exTemp));
|
||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
|
||||
|
||||
@@ -236,13 +242,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
h=h.outBlock();
|
||||
|
||||
l.add(new LabelLLVMImp(labelFin));
|
||||
prevSymTable.updateId(h);
|
||||
return l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||
String labelIf= "if"+h.getNewIdLabel()+":";
|
||||
String labelThen= "then"+h.getNewIdLabel()+":";
|
||||
@@ -256,7 +260,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||
Result r = h.addNewTempVar();
|
||||
h = r.symTable;
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var);
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false);
|
||||
l.add(new AssignLLVMImp(varCond,exTemp));
|
||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
|
||||
|
||||
@@ -271,13 +275,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
l.add(new BrLLVMImp(labelFin));
|
||||
|
||||
l.add(new LabelLLVMImp(labelFin));
|
||||
prevSymTable.updateId(h);
|
||||
return l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||
|
||||
String labelWhile = "while"+h.getNewIdLabel()+":";
|
||||
@@ -291,7 +293,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||
Result r = h.addNewTempVar();
|
||||
h = r.symTable;
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var);
|
||||
VarLLVMImp varCond = new VarLLVMImp(exTemp.getType(), r.var,false);
|
||||
l.add(new AssignLLVMImp(varCond,exTemp));
|
||||
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
|
||||
|
||||
@@ -304,7 +306,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
l.add(new BrLLVMImp(labelWhile));
|
||||
|
||||
l.add(new LabelLLVMImp(labelDone));
|
||||
prevSymTable.updateId(h);
|
||||
return l;
|
||||
}
|
||||
|
||||
@@ -319,20 +320,17 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
|
||||
@Override
|
||||
public InstrAndVal visitVar(VarImp e, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> l =new ArrayList<>();
|
||||
ValLLVM val = new VarLLVMImp(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()),false);
|
||||
Result r = h.addNewTempVar();
|
||||
h = r.symTable;
|
||||
VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var);
|
||||
VarLLVMImp varTemp = new VarLLVMImp(h.getvar_Type(e.name()).accept(this,h),r.var,false);
|
||||
l.add(new AssignLLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(val)))));
|
||||
prevSymTable.updateId(h);
|
||||
return new InstrAndVal(l, varTemp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||
InstrAndVal res1 = e.e1().accept(this, h);
|
||||
InstrAndVal res2 = e.e2().accept(this, h);
|
||||
@@ -351,14 +349,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
Result r = h.addNewTempVar();
|
||||
String temp = r.var;
|
||||
h = r.symTable;
|
||||
VarLLVMImp var = new VarLLVMImp(type,temp);
|
||||
VarLLVMImp var = new VarLLVMImp(type,temp,false);
|
||||
list.add(new AssignLLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2)));
|
||||
prevSymTable.updateId(h);
|
||||
return new InstrAndVal(list, var);
|
||||
}
|
||||
|
||||
public InstrAndVal visitAppeal(AppealImp instr,SymTable h){
|
||||
SymTable prevSymTable = h;
|
||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
|
||||
for(Expression param : instr.params()){
|
||||
@@ -375,10 +371,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
||||
//Pour c=func(x,y)
|
||||
Result res = h.addNewTempVar();
|
||||
h = res.symTable;
|
||||
VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var);
|
||||
VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var,false);
|
||||
|
||||
l.add(new AssignLLVMImp(var, new CallLLVMImp(fLLVM.define,paramsLLVM,"")));
|
||||
prevSymTable.updateId(h);
|
||||
return new InstrAndVal(l, var);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user