This commit is contained in:
Vu Tuan Minh
2025-04-24 06:07:30 +02:00
5 changed files with 82 additions and 73 deletions

View File

@@ -61,10 +61,16 @@ public class SymTable {
return new Result(newSymTab,newVar);
}
//retourne le nom de la var déjà déclaré avec son id
public String getVar(String nomVar){
return nomVar + this.stackMap.getLast().get(nomVar).id;
}
//retourne le type de la var
public Type getType(String nomVar){
return this.stackMap.getLast().get(nomVar).type;
}
public PStack<PMap<String,ValueTable>> next_layer(){
return stackMap.plus(HashTreePMap.empty());
}
@@ -109,10 +115,10 @@ public class SymTable {
return this.stackmap();
}
public ValueTable getvar_Type(String s){
public Type getvar_Type(String s){
for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){
return stackMap.get(i).get(s);
return stackMap.get(i).get(s).type;
}
}
return null;

View File

@@ -57,7 +57,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
instrLLVM.addAll(fun.instruction().accept(this, h));
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), new ArrayList<>(), instrLLVM);
}
//DECLARATION
@@ -130,14 +130,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new PrintLLVMImpl(new ArrayList())); //TODO
ArrayList<ValLLVM> params = new ArrayList<>();
l.add(new PrintLLVMImpl(params)); //TODO
return l;
}
@Override
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new ReadLLVMImpl(new ArrayList())); //TODO
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());
VarLLVMImpl newVar = new VarLLVMImpl(typeVar.accept(this,h), nomVar);
ArrayList<ValLLVM> params = new ArrayList<>();
params.add(newVar);
l.add(new ReadLLVMImpl(params));
}
return l;
}
@@ -213,7 +221,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ValLLVM val = temp.val; //temp6
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
h = temp2.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
@@ -238,8 +245,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndVal visitVar(VarImp e, SymTable h) {
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
return new InstrAndVal(new ArrayList<>(), val);
ArrayList<AssignLVMImpl> l =new ArrayList<>();
ValLLVM val = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name()));
VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.addNewTempVar().var);
l.add(new AssignLVMImpl(varTemp,((ExpressionLLVM)(new LoadLLVMImpl(val)))));
return new InstrAndVal(l, varTemp);
}
@Override

View File

@@ -1,5 +1,4 @@
package TP2.llvm;
import TP2.asd.Program.IfThenElseImp;
import TP2.llvm.ProgramLLVM.*;
public interface Interface {
@@ -32,12 +31,10 @@ public interface Interface {
public S visitStoreLLVM(StoreLLVMImpl instr, H h);
public S visitPrintLLVM(PrintLLVMImpl instr, H h);
public S visitReadLLVM(ReadLLVMImpl instr, H h);
public S visitIfThenElseLLVM(IfThenElseLLVMImp instr, H h);
public S visitIfThenLLVM(IfThenLLVMImp instr, H h);
public S visitWhileLLVM(WhileLLVMImp instr, H h);
public S visitLabelLLVM(LabelLLVMImp instr, H h);
public S visitBrLLVM(BrLLVMImp instr, H h);
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
public S visitCallLLVM(CallLLVMImpl instr, H h);
}
//////////ExpressionLLVM (expression)
@@ -52,7 +49,7 @@ public interface Interface {
public interface ExpressionLLVMVisitor<H,S> {
public S visitBinOpLLVM(BinOpLLVMImpl e, H h);
public S visitAllocaLLVM(allocaLLVMImpl e,H h);
public S visitLoadLLVM(loadLLVMImpl e,H h);
public S visitLoadLLVM(LoadLLVMImpl e,H h);
public S visitValLLVM(ValLLVMImpl e,H h);
public S visitVarLLVM(VarLLVMImpl e,H h);
public S visitIcmpLLVM(IcmpLLVMImp e, H h);
@@ -73,6 +70,7 @@ public interface Interface {
public S visitIntLLVM(IntLLVMImpl e,H h);
public S visitVoidLLVM(VoidLLVMImpl e, H h);
public S visitBooleanLLVM(BooleanLLVMImp e, H h);
public S visitStringLLVM(StringLLVMImp e, H h);
}
}

View File

@@ -1,5 +1,7 @@
package TP2.llvm;
import java.util.ArrayList;
import TP2.asd.Program.IfThenElseImp;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;
@@ -29,6 +31,7 @@ TypeLLVMVisitor<String,String>
//}
//PROTO et FUNC
for(DefineLLVM fonction : prog.fonctions()){
str.append(fonction.accept(this,indent));
str.append("\n");
@@ -39,7 +42,7 @@ TypeLLVMVisitor<String,String>
@Override
public String visitDefineLLVM(DefineLLVMImpl define, String indent) {
StringBuilder str = new StringBuilder("define ");
str.append(define.type().accept(this,indent)).append(" @").append(define.nom()).append("(");
str.append(define.type().accept(this,indent)).append(" @").append(define.name()).append("(");
//TODO param
@@ -103,25 +106,38 @@ TypeLLVMVisitor<String,String>
@Override
public String visitLoadLLVM(loadLLVMImpl e, String h) {
return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h);
}
@Override
public String visitPrintLLVM(PrintLLVMImpl instr, String h) {
return INDENT+"call " + "...TODO..." +" printf " + "...TODO...";
public String visitLoadLLVM(LoadLLVMImpl e, String h) {
return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h);
}
@Override
public String visitReadLLVM(ReadLLVMImpl instr, String h) {
return INDENT+"call " + "...TODO..." +" scanf " + "...TODO...";
public String visitCallLLVM(CallLLVMImpl instr, String h) {
String str = INDENT+ "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 += ", ";
}
return str + ")";
}
@Override
public String visitPrintLLVM(PrintLLVMImpl instr, String h) { //TODO
DefineLLVMImpl printLLVM = new DefineLLVMImpl("printf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>());
CallLLVMImpl callPrint = new CallLLVMImpl(printLLVM, new ArrayList<>(),"(i8*,...) ");
return callPrint.accept(this, h);
}
@Override
public String visitReadLLVM(ReadLLVMImpl instr, String h) { //TODO
DefineLLVMImpl readLLVM = new DefineLLVMImpl("scanf", new IntLLVMImpl(), new ArrayList<>(), new ArrayList<>());
CallLLVMImpl callRead = new CallLLVMImpl(readLLVM, instr.l(),"(i8*,...) ");
return callRead.accept(this, h);
}
//label
@Override
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
return instr.nom()+":";
return instr.name()+":";
}
@Override
@@ -134,24 +150,6 @@ TypeLLVMVisitor<String,String>
return INDENT+"br " + instr.var().type().accept(this, h) +" "+ instr.var().accept(this, h) + ", label %" + instr.label() + ", label %" + instr.labelElse() ;
}
@Override
public String visitIfThenElseLLVM(IfThenElseLLVMImp instr, String h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitIfThenElseLLVM'");
}
@Override
public String visitIfThenLLVM(IfThenLLVMImp instr, String h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitIfThenLLVM'");
}
@Override
public String visitWhileLLVM(WhileLLVMImp instr, String h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitWhileLLVM'");
}
@Override
@@ -178,4 +176,10 @@ TypeLLVMVisitor<String,String>
public String visitBooleanLLVM(BooleanLLVMImp e, String h) {
return "i1";
}
@Override
public String visitStringLLVM(StringLLVMImp e, String h) {
return "i8*";
}
}

View File

@@ -22,7 +22,7 @@ public class ProgramLLVM {
//Define
public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public static record DefineLLVMImpl(String name, TypeLLVM type, ArrayList<VarLLVMImpl> params, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
return v.visitDefineLLVM(this, h);
}
@@ -31,7 +31,7 @@ public class ProgramLLVM {
//Instructon :
//Label
public static record LabelLLVMImp(String nom) implements InstructionLLVM{
public static record LabelLLVMImp(String name) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitLabelLLVM(this, h);
@@ -74,14 +74,21 @@ public class ProgramLLVM {
}
}
public static record PrintLLVMImpl(ArrayList<Object> l) implements InstructionLLVM{
public static record CallLLVMImpl(DefineLLVMImpl f, ArrayList<ValLLVM> params, String str) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitCallLLVM(this, h);
}
}
public static record PrintLLVMImpl(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction print
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitPrintLLVM(this, h);
}
}
public static record ReadLLVMImpl(ArrayList<VarLLVMImpl> l) implements InstructionLLVM{
public static record ReadLLVMImpl(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -89,30 +96,6 @@ public class ProgramLLVM {
}
}
public static record IfThenLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitIfThenLLVM(this, h);
}
}
public static record IfThenElseLLVMImp(ExpressionLLVM cond, InstructionLLVM intr1, InstructionLLVM instr2) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitIfThenElseLLVM(this, h);
}
}
public static record WhileLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitWhileLLVM(this, h);
}
}
//Expression :
public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override
@@ -153,7 +136,7 @@ public class ProgramLLVM {
}
public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, ValLLVM val) implements ExpressionLLVM{
public static record LoadLLVMImpl(ValLLVM val) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitLoadLLVM(this, h);
@@ -161,7 +144,7 @@ public class ProgramLLVM {
@Override
public TypeLLVM getType() {
return type;
return val().getType();
}
}
@@ -227,4 +210,11 @@ public class ProgramLLVM {
return v.visitBooleanLLVM(this, h);
}
}
public static record StringLLVMImp() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitStringLLVM(this, h);
}
}
}