maj SymTable (id global) + DeclGlobal
This commit is contained in:
@@ -53,7 +53,7 @@ public class Main {
|
|||||||
// Verify the program semantic
|
// Verify the program semantic
|
||||||
|
|
||||||
// Generate the intermediate representation
|
// Generate the intermediate representation
|
||||||
//System.out.println("todo");
|
System.out.println("\n\n");
|
||||||
|
|
||||||
ProgramLLVMImp astLLVM = ast.toLLVM();
|
ProgramLLVMImp astLLVM = ast.toLLVM();
|
||||||
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package TP2.asd;
|
package TP2.asd;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.pcollections.*;
|
import org.pcollections.*;
|
||||||
import TP2.asd.Interface.Type;
|
import TP2.asd.Interface.Type;
|
||||||
import TP2.asd.Program.Type_intImp;
|
import TP2.asd.Program.Type_intImp;
|
||||||
|
import TP2.llvm.ProgramLLVM.DeclarGlobalLLVMImp;
|
||||||
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
|
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
|
||||||
|
|
||||||
|
|
||||||
@@ -9,18 +12,22 @@ public class SymTable {
|
|||||||
|
|
||||||
private PStack<PMap<String,ValueVarMap>> varMap;
|
private PStack<PMap<String,ValueVarMap>> varMap;
|
||||||
private PMap<String,ValueFunMap> functionsMap;
|
private PMap<String,ValueFunMap> functionsMap;
|
||||||
private int id=1;
|
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : id Label, [2] : global
|
||||||
private int idLabel = 1;
|
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal;
|
||||||
|
|
||||||
public SymTable(){
|
public SymTable(){
|
||||||
|
this.id = new int[3];
|
||||||
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
|
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
|
||||||
this.functionsMap = 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.varMap= varMap;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.functionsMap = functionsMap;
|
this.functionsMap = functionsMap;
|
||||||
this.idLabel = idLabel;
|
this.declarationsGlobal = declarationsGlobal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ValueFunMap{
|
public static class ValueFunMap{
|
||||||
@@ -34,14 +41,13 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SymTable newBlock() {
|
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() {
|
public SymTable outBlock() {
|
||||||
if (varMap.size() > 1) {
|
if (varMap.size() > 1) {
|
||||||
return new SymTable(varMap.minus(0), functionsMap, id, idLabel);
|
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Vide");
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,9 +63,14 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateId(SymTable symTable2){
|
/*public void update(SymTable symTable2){
|
||||||
this.id = symTable2.getId();
|
this.id = symTable2.getId();
|
||||||
this.idLabel = symTable2.getIdLabel();
|
this.idLabel = symTable2.getIdLabel();
|
||||||
|
this.declarationsGlobal = symTable2.getDeclarationGlobal();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public ArrayList<DeclarGlobalLLVMImp> getDeclarationGlobal(){
|
||||||
|
return this.declarationsGlobal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Result{
|
public static class Result{
|
||||||
@@ -75,7 +86,7 @@ public class SymTable {
|
|||||||
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
|
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
|
||||||
ValueFunMap value = this.functionsMap.get(function.name());
|
ValueFunMap value = this.functionsMap.get(function.name());
|
||||||
if(value == null || (value!=null && value.isProto && !isProto)){
|
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{
|
else{
|
||||||
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
|
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(){
|
public int getId(){
|
||||||
return this.id;
|
return this.id[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIdLabel(){
|
public int getIdLabel(){
|
||||||
return this.idLabel;
|
return this.id[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNewId(){
|
public int getNewId(){
|
||||||
int a = this.id;
|
int a = this.id[0];
|
||||||
this.id++;
|
this.id[0]++;
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNewIdLabel(){
|
public int getNewIdLabel(){
|
||||||
int a = this.idLabel;
|
int a = this.id[1];
|
||||||
this.idLabel++;
|
this.id[1]++;
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result addNewTempVar(){
|
public Result addNewTempVar(){
|
||||||
String newVar = "temp"+id;
|
String newVar = "temp"+id[0];
|
||||||
SymTable newSymTab = this.addVarInTab(newVar,new Type_intImp(),false);
|
SymTable newSymTab = this.addVarInTab(newVar,new Type_intImp(),false);
|
||||||
return new Result(newSymTab,newVar);
|
return new Result(newSymTab,newVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result addParam(String nomParam){
|
public Result addParam(String nomParam){
|
||||||
String newParam = "param_"+nomParam+id;
|
String newParam = "param_"+nomParam;
|
||||||
SymTable newSymTab = this.addVarInTab(nomParam,new Type_intImp(),true);
|
SymTable newSymTab = this.addVarInTab(nomParam,new Type_intImp(),true);
|
||||||
return new Result(newSymTab,newParam);
|
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) {
|
public Result addVar(String nomVar) {
|
||||||
PMap<String, ValueVarMap> top = varMap.get(0);
|
PMap<String, ValueVarMap> top = varMap.get(0);
|
||||||
if (top.containsKey(nomVar)) {
|
if (top.containsKey(nomVar)) {
|
||||||
System.err.println("[VSL compile error] :" + "Erreur");
|
System.err.println("[VSL compile error] :" + "Erreur");
|
||||||
return new Result(this, null);
|
return new Result(this, null);
|
||||||
}
|
}
|
||||||
String realName = nomVar + id;
|
String realName = nomVar + id[0];
|
||||||
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false));
|
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id[0], false));
|
||||||
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
id[0]++;
|
||||||
|
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal);
|
||||||
return new Result(newSym, realName);
|
return new Result(newSym, realName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,13 +207,14 @@ public class SymTable {
|
|||||||
System.err.println(nomVar+ " déjà déclaré.");
|
System.err.println(nomVar+ " déjà déclaré.");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
top = top.plus(nomVar, new ValueVarMap(type, id, isParam));
|
top = top.plus(nomVar, new ValueVarMap(type, id[0], isParam));
|
||||||
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
if(!isParam) this.id[0]++;
|
||||||
|
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String print_all(){
|
public String print_all(){
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
str.append("Id = ").append(id).append("\n");
|
str.append("Id = ").append(id[0]).append("\n");
|
||||||
str.append("VARIABLES:\n");
|
str.append("VARIABLES:\n");
|
||||||
|
|
||||||
int scopeLevel = varMap.size();
|
int scopeLevel = varMap.size();
|
||||||
|
|||||||
@@ -54,8 +54,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
fonctionLLVM.add(function);
|
fonctionLLVM.add(function);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(h.print_all());
|
return new ProgramLLVMImp(h.getDeclarationGlobal(),fonctionLLVM);
|
||||||
return new ProgramLLVMImp(new ArrayList<>(),fonctionLLVM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,19 +62,17 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefineLLVMImp visitFunction(FunctionImp fun, SymTable h) {
|
public DefineLLVMImp visitFunction(FunctionImp fun, SymTable h) {
|
||||||
SymTable prevSymTable = h;
|
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
ArrayList<VarLLVMImp> paramsLLVM = new ArrayList<>();
|
ArrayList<VarLLVMImp> 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;
|
||||||
VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name);
|
VarLLVMImp var = new VarLLVMImp(new IntLLVMImp(), name,false);
|
||||||
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));
|
||||||
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
|
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
|
||||||
prevSymTable.updateId(h);
|
|
||||||
return define;
|
return define;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +85,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
@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);
|
||||||
@@ -96,9 +92,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
Result r = h.addVar(instr.s().get(i));
|
Result r = h.addVar(instr.s().get(i));
|
||||||
String name = r.var;
|
String name = r.var;
|
||||||
h = r.symTable;
|
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);
|
return new InstrAndSymTable(list,h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +101,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
|
||||||
SymTable prev = h;
|
|
||||||
h= h.newBlock();
|
h= h.newBlock();
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
for(int i = 0; i<instr.instrs().size(); i++){
|
for(int i = 0; i<instr.instrs().size(); i++){
|
||||||
@@ -114,13 +108,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
h= h.outBlock();
|
h= h.outBlock();
|
||||||
prev.updateId(h);
|
|
||||||
return instrLLVM;
|
return instrLLVM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
|
||||||
SymTable prev = 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++){
|
||||||
@@ -132,7 +124,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
|
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
|
||||||
}
|
}
|
||||||
|
|
||||||
prev.updateId(h);
|
|
||||||
return instrLLVM;
|
return instrLLVM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +148,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
result.addAll(res.instrs);
|
result.addAll(res.instrs);
|
||||||
//InstructionLLVM r = new AssignLLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
//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);
|
result.add(r);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -167,7 +158,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
ArrayList<ValLLVM> params = 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;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,10 +176,18 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
for(int i = 0; i<instr.t().size(); i++){
|
for(int i = 0; i<instr.t().size(); i++){
|
||||||
String nomVar = h.getVar(instr.t().get(i).name());
|
String nomVar = h.getVar(instr.t().get(i).name());
|
||||||
Type typeVar = h.getType(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<>();
|
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);
|
params.add(newVar);
|
||||||
l.add(new ReadLLVMImp(params));
|
|
||||||
|
l.add(new ReadLLVMImp(globalDecl,params));
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
@@ -212,7 +219,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
@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()+":";
|
||||||
@@ -225,7 +231,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||||
Result r = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
h = r.symTable;
|
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 AssignLLVMImp(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
|
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
|
||||||
|
|
||||||
@@ -236,13 +242,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
h=h.outBlock();
|
h=h.outBlock();
|
||||||
|
|
||||||
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()+":";
|
||||||
@@ -256,7 +260,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||||
Result r = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
h = r.symTable;
|
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 AssignLLVMImp(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
|
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 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()+":";
|
||||||
@@ -291,7 +293,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImp(new IntLLVMImp(), 0));
|
||||||
Result r = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
h = r.symTable;
|
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 AssignLLVMImp(varCond,exTemp));
|
||||||
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
|
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 BrLLVMImp(labelWhile));
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelDone));
|
l.add(new LabelLLVMImp(labelDone));
|
||||||
prevSymTable.updateId(h);
|
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,20 +320,17 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
@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 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();
|
Result r = h.addNewTempVar();
|
||||||
h = r.symTable;
|
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)))));
|
l.add(new AssignLLVMImp(varTemp,((ExpressionLLVM)(new LoadLLVMImp(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);
|
||||||
@@ -351,14 +349,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
Result r = h.addNewTempVar();
|
Result r = h.addNewTempVar();
|
||||||
String temp = r.var;
|
String temp = r.var;
|
||||||
h = r.symTable;
|
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)));
|
list.add(new AssignLLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2)));
|
||||||
prevSymTable.updateId(h);
|
|
||||||
return new InstrAndVal(list, var);
|
return new InstrAndVal(list, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstrAndVal visitAppeal(AppealImp instr,SymTable h){
|
public InstrAndVal visitAppeal(AppealImp instr,SymTable h){
|
||||||
SymTable prevSymTable = h;
|
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
|
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
|
||||||
for(Expression param : instr.params()){
|
for(Expression param : instr.params()){
|
||||||
@@ -375,10 +371,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
//Pour c=func(x,y)
|
//Pour c=func(x,y)
|
||||||
Result res = h.addNewTempVar();
|
Result res = h.addNewTempVar();
|
||||||
h = res.symTable;
|
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,"")));
|
l.add(new AssignLLVMImp(var, new CallLLVMImp(fLLVM.define,paramsLLVM,"")));
|
||||||
prevSymTable.updateId(h);
|
|
||||||
return new InstrAndVal(l, var);
|
return new InstrAndVal(l, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ public interface Interface {
|
|||||||
public S visitBrLLVM(BrLLVMImp instr, H h);
|
public S visitBrLLVM(BrLLVMImp instr, H h);
|
||||||
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
|
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
|
||||||
public S visitCallVoidLLVM(CallVoidLLVMImp instr, H h);
|
public S visitCallVoidLLVM(CallVoidLLVMImp instr, H h);
|
||||||
|
public S visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////ExpressionLLVM (expression)
|
//////////ExpressionLLVM (expression)
|
||||||
|
|||||||
@@ -25,10 +25,11 @@ TypeLLVMVisitor<String,String>
|
|||||||
str.append("\n");
|
str.append("\n");
|
||||||
str.append("; Actual code begins\n");
|
str.append("; Actual code begins\n");
|
||||||
|
|
||||||
// Déclaration pour les string
|
for(int i = 0; i<prog.declarGlobal().size(); i++){
|
||||||
//for(int i = 0; i<declration.size(); i++){
|
str.append(prog.declarGlobal().get(i).accept(this, indent));
|
||||||
//str.append(declration.get(i) + "\n");
|
}
|
||||||
//}
|
str.append("\n");
|
||||||
|
|
||||||
|
|
||||||
//PROTO et FUNC
|
//PROTO et FUNC
|
||||||
|
|
||||||
@@ -149,6 +150,15 @@ TypeLLVMVisitor<String,String>
|
|||||||
return callRead.accept(this, h);
|
return callRead.accept(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, String h) {
|
||||||
|
String str = "@."+instr.name() + " = global";
|
||||||
|
str += "[" + "x"+ "]";
|
||||||
|
str+= "c\"\"\n";
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
//label
|
//label
|
||||||
@Override
|
@Override
|
||||||
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
|
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
|
||||||
@@ -174,7 +184,9 @@ TypeLLVMVisitor<String,String>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitVarLLVM(VarLLVMImp e, String h) {
|
public String visitVarLLVM(VarLLVMImp e, String h) {
|
||||||
return "%"+e.nom();
|
String prefix = "%";
|
||||||
|
if(e.isGlobal()) prefix = "@";
|
||||||
|
return prefix+e.nom();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -198,4 +210,5 @@ TypeLLVMVisitor<String,String>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import TP2.llvm.Interface.*;
|
|||||||
public class ProgramLLVM {
|
public class ProgramLLVM {
|
||||||
|
|
||||||
//Program
|
//Program
|
||||||
public static record ProgramLLVMImp(ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
|
public static record ProgramLLVMImp(ArrayList<DeclarGlobalLLVMImp> declarGlobal ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
|
||||||
public <H, S> S accept(ProgramLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(ProgramLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitProgramLLVM(this, h);
|
return v.visitProgramLLVM(this, h);
|
||||||
}
|
}
|
||||||
@@ -81,14 +81,14 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record PrintLLVMImp(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print
|
public static record PrintLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList<ValLLVM> l) implements InstructionLLVM{
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitPrintLLVM(this, h);
|
return v.visitPrintLLVM(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record ReadLLVMImp(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read
|
public static record ReadLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList<ValLLVM> l) implements InstructionLLVM{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
@@ -96,6 +96,14 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static record DeclarGlobalLLVMImp(String name) implements InstructionLLVM{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitDeclarGlobalLLVM(this, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Expression :
|
//Expression :
|
||||||
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
|
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
|
||||||
@Override
|
@Override
|
||||||
@@ -170,13 +178,12 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeLLVM getType() {
|
public TypeLLVM getType() {
|
||||||
System.out.println("getType");
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static record VarLLVMImp(TypeLLVM type, String nom) implements ValLLVM{
|
public static record VarLLVMImp(TypeLLVM type, String nom, Boolean isGlobal) implements ValLLVM{
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitVarLLVM(this, h);
|
return v.visitVarLLVM(this, h);
|
||||||
@@ -184,7 +191,6 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeLLVM getType() {
|
public TypeLLVM getType() {
|
||||||
System.out.println("getType");
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user