This commit is contained in:
Vu Tuan Minh
2025-04-11 13:58:57 +02:00
7 changed files with 83 additions and 31 deletions

View File

@@ -131,10 +131,10 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override
public String visitWhile(WhileImp instr, String indent) {
String str = indent+"WHILE ";
str += (instr.e().accept(this, ""));
str+= " DO ";
str+= instr.i1().accept(this, "");
str+= " DONE";
str += (instr.e().accept(this, indent))+"\n";
str+= indent+"DO ";
str+= instr.i1().accept(this, indent)+"\n";
str+= indent+"DONE";
return str;
}

View File

@@ -44,7 +44,7 @@ public class SymTable {
public int getNewIdLabel(){
int a = this.idLabel;
this.id++;
this.idLabel++;
return a;
}
@@ -62,7 +62,6 @@ public class SymTable {
}
public String getVar(String nomVar){
System.out.println("getVar(" + nomVar +") -------------------------------------------");
return nomVar + this.stackMap.getLast().get(nomVar).id;
}

View File

@@ -174,14 +174,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
String labelDone = "done"+h.getNewIdLabel();
l.add(new LabelLLVMImp(labelWhile));
InstrAndVal temp = instr.e().accept(this,h); //retourne les instruction pour optenir le résultat ainsi que la variable contenant le résultat final
l.addAll(temp.instrs);
InstrAndVal temp = instr.e().accept(this,h); //retourne les instructionz pour obtenir le résultat de l'expression ainsi que la variable contenant le résultat final
l.addAll(temp.instrs); //instructions
ValLLVM val = temp.val; //temp6
//TODO : icmp et br
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));
l.add(new LabelLLVMImp(labelDo));
l.addAll(instr.i1().accept(this,h));
//TODO br vers label while
l.add(new BrLLVMImp(labelWhile));
l.add(new LabelLLVMImp(labelDone));
return l;

View File

@@ -36,6 +36,8 @@ public interface Interface {
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);
}
//////////ExpressionLLVM (expression)
@@ -53,6 +55,7 @@ public interface Interface {
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);
}
/*public interface IdentifierLLVM{ //globaux @ et local %
@@ -64,12 +67,12 @@ public interface Interface {
public interface TypeLLVM{
public <H,S> S accept(TypeLLVMVisitor<H,S> v, H h);
public int getNbBit();
}
public interface TypeLLVMVisitor<H,S> {
public S visitIntLLVM(IntLLVMImpl e,H h);
public S visitVoidLLVM(VoidLLVMImpl e, H h);
public S visitBooleanLLVM(BooleanLLVMImp e, H h);
}
}

View File

@@ -86,9 +86,14 @@ TypeLLVMVisitor<String,String>
return str + e.type().accept(this,h) + " " + e.val1().accept(this,h) + ", " + e.val2().accept(this,h);
}
@Override
public String visitIcmpLLVM(IcmpLLVMImp e, String h) {
return "icmp ne " + e.val1().getType().accept(this, h) +" "+ e.val1().accept(this, h) + ", " + e.val2().accept(this, h);
}
@Override
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
return "alloca" + " i" + e.type().getNbBit();
return "alloca" + e.type().accept(this, h);
}
@Override
@@ -113,6 +118,23 @@ TypeLLVMVisitor<String,String>
return INDENT+"call " + "...TODO..." +" scanf " + "...TODO...";
}
//label
@Override
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
return instr.nom()+":";
}
@Override
public String visitBrLLVM(BrLLVMImp instr, String h) {
return INDENT+"br label %" + instr.label();
}
@Override
public String visitBrCondLLVM(BrCondLLVMImp instr, String h) {
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
@@ -153,7 +175,7 @@ TypeLLVMVisitor<String,String>
}
@Override
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
return "";
public String visitBooleanLLVM(BooleanLLVMImp e, String h) {
return "i1";
}
}

View File

@@ -30,19 +30,28 @@ public class ProgramLLVM {
//Instructon :
//Label
public static record LabelLLVMImp(String nom) implements InstructionLLVM{
public String prettyprinter(){
StringBuilder str = new StringBuilder();
return str.toString();
}
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitLabelLLVM(this, h);
}
}
public static record BrLLVMImp(String label) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitBrLLVM(this, h);
}
}
public static record BrCondLLVMImp(VarLLVMImpl var, String label, String labelElse) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitBrCondLLVM(this, h);
}
}
public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
@Override
@@ -156,6 +165,18 @@ public class ProgramLLVM {
}
}
public static record IcmpLLVMImp(ValLLVM val1, ValLLVM val2) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitIcmpLLVM(this, h);
}
@Override
public TypeLLVM getType() {
return new BooleanLLVMImp();
}
}
//Val
@@ -186,27 +207,24 @@ public class ProgramLLVM {
public static record IntLLVMImpl() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitIntLLVM(this, h);
}
public int getNbBit(){
return 32;
}
}
public static record VoidLLVMImpl() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitVoidLLVM(this, h);
}
}
public int getNbBit(){
return 0;
public static record BooleanLLVMImp() implements TypeLLVM{
@Override
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitBooleanLLVM(this, h);
}
}
}