avancement sur Read et Print, todo : @.ftm

This commit is contained in:
Rochas
2025-04-13 16:00:06 +02:00
parent 2f16120dae
commit 9ef84876f7
4 changed files with 55 additions and 15 deletions

View File

@@ -57,7 +57,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) { public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>(); ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
instrLLVM.addAll(fun.instruction().accept(this, h)); 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 //DECLARATION
@@ -130,7 +130,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override @Override
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<>();
l.add(new PrintLLVMImpl(new ArrayList())); //TODO ArrayList<ValLLVM> params = new ArrayList<>();
l.add(new PrintLLVMImpl(params)); //TODO
return l; return l;
} }
@@ -141,7 +142,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
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());
VarLLVMImpl newVar = new VarLLVMImpl(typeVar.accept(this,h), nomVar); VarLLVMImpl newVar = new VarLLVMImpl(typeVar.accept(this,h), nomVar);
l.add(new ReadLLVMImpl(newVar)); ArrayList<ValLLVM> params = new ArrayList<>();
params.add(newVar);
l.add(new ReadLLVMImpl(params));
} }
return l; return l;
} }

View File

@@ -1,5 +1,4 @@
package TP2.llvm; package TP2.llvm;
import TP2.asd.Program.IfThenElseImp;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
public interface Interface { public interface Interface {
@@ -35,6 +34,7 @@ public interface Interface {
public S visitLabelLLVM(LabelLLVMImp instr, H h); public S visitLabelLLVM(LabelLLVMImp instr, H h);
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 visitCallLLVM(CallLLVMImpl instr, H h);
} }
//////////ExpressionLLVM (expression) //////////ExpressionLLVM (expression)
@@ -70,6 +70,7 @@ public interface Interface {
public S visitIntLLVM(IntLLVMImpl e,H h); public S visitIntLLVM(IntLLVMImpl e,H h);
public S visitVoidLLVM(VoidLLVMImpl e, H h); public S visitVoidLLVM(VoidLLVMImpl e, H h);
public S visitBooleanLLVM(BooleanLLVMImp 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; package TP2.llvm;
import java.util.ArrayList;
import TP2.asd.Program.IfThenElseImp; import TP2.asd.Program.IfThenElseImp;
import TP2.llvm.Interface.*; import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
@@ -29,6 +31,7 @@ TypeLLVMVisitor<String,String>
//} //}
//PROTO et FUNC //PROTO et FUNC
for(DefineLLVM fonction : prog.fonctions()){ for(DefineLLVM fonction : prog.fonctions()){
str.append(fonction.accept(this,indent)); str.append(fonction.accept(this,indent));
str.append("\n"); str.append("\n");
@@ -39,7 +42,7 @@ TypeLLVMVisitor<String,String>
@Override @Override
public String visitDefineLLVM(DefineLLVMImpl define, String indent) { public String visitDefineLLVM(DefineLLVMImpl define, String indent) {
StringBuilder str = new StringBuilder("define "); 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 //TODO param
@@ -107,21 +110,34 @@ TypeLLVMVisitor<String,String>
return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h); return "load" + " " + e.getType().accept(this, h) + ", "+ e.getType().accept(this, h) + "* " + e.val().accept(this, h);
} }
@Override @Override
public String visitPrintLLVM(PrintLLVMImpl instr, String h) { public String visitCallLLVM(CallLLVMImpl instr, String h) {
return INDENT+"call " + "...TODO..." +" printf " + "...TODO..."; 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 @Override
public String visitReadLLVM(ReadLLVMImpl instr, String h) { public String visitPrintLLVM(PrintLLVMImpl instr, String h) { //TODO
return INDENT+"call " + instr.var().type().accept(this,h) + "(" + "i8*, ..." + ")" + " scanf(i8* " + "...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 //label
@Override @Override
public String visitLabelLLVM(LabelLLVMImp instr, String h) { public String visitLabelLLVM(LabelLLVMImp instr, String h) {
return instr.nom()+":"; return instr.name()+":";
} }
@Override @Override
@@ -160,4 +176,10 @@ TypeLLVMVisitor<String,String>
public String visitBooleanLLVM(BooleanLLVMImp e, String h) { public String visitBooleanLLVM(BooleanLLVMImp e, String h) {
return "i1"; return "i1";
} }
@Override
public String visitStringLLVM(StringLLVMImp e, String h) {
return "i8*";
}
} }

View File

@@ -22,7 +22,7 @@ public class ProgramLLVM {
//Define //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) { public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
return v.visitDefineLLVM(this, h); return v.visitDefineLLVM(this, h);
} }
@@ -31,7 +31,7 @@ public class ProgramLLVM {
//Instructon : //Instructon :
//Label //Label
public static record LabelLLVMImp(String nom) implements InstructionLLVM{ public static record LabelLLVMImp(String name) 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.visitLabelLLVM(this, 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 @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 ReadLLVMImpl(VarLLVMImpl var) implements InstructionLLVM{ public static record ReadLLVMImpl(ArrayList<ValLLVM> l) implements InstructionLLVM{ //TODO c'est un Call qui appel la fonction read
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -203,4 +210,11 @@ public class ProgramLLVM {
return v.visitBooleanLLVM(this, h); 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);
}
}
} }