Merge branch 'main' of https://gitlab2.istic.univ-rennes1.fr/tuvu/tp2-vsl-pds
This commit is contained in:
@@ -131,10 +131,10 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
@Override
|
@Override
|
||||||
public String visitWhile(WhileImp instr, String indent) {
|
public String visitWhile(WhileImp instr, String indent) {
|
||||||
String str = indent+"WHILE ";
|
String str = indent+"WHILE ";
|
||||||
str += (instr.e().accept(this, ""));
|
str += (instr.e().accept(this, indent))+"\n";
|
||||||
str+= " DO ";
|
str+= indent+"DO ";
|
||||||
str+= instr.i1().accept(this, "");
|
str+= instr.i1().accept(this, indent)+"\n";
|
||||||
str+= " DONE";
|
str+= indent+"DONE";
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class SymTable {
|
|||||||
|
|
||||||
public int getNewIdLabel(){
|
public int getNewIdLabel(){
|
||||||
int a = this.idLabel;
|
int a = this.idLabel;
|
||||||
this.id++;
|
this.idLabel++;
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,6 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getVar(String nomVar){
|
public String getVar(String nomVar){
|
||||||
System.out.println("getVar(" + nomVar +") -------------------------------------------");
|
|
||||||
return nomVar + this.stackMap.getLast().get(nomVar).id;
|
return nomVar + this.stackMap.getLast().get(nomVar).id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -174,14 +174,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
String labelDone = "done"+h.getNewIdLabel();
|
String labelDone = "done"+h.getNewIdLabel();
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelWhile));
|
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
|
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);
|
l.addAll(temp.instrs); //instructions
|
||||||
ValLLVM val = temp.val; //temp6
|
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.add(new LabelLLVMImp(labelDo));
|
||||||
l.addAll(instr.i1().accept(this,h));
|
l.addAll(instr.i1().accept(this,h));
|
||||||
//TODO br vers label while
|
|
||||||
|
l.add(new BrLLVMImp(labelWhile));
|
||||||
|
|
||||||
l.add(new LabelLLVMImp(labelDone));
|
l.add(new LabelLLVMImp(labelDone));
|
||||||
|
|
||||||
return l;
|
return l;
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ public interface Interface {
|
|||||||
public S visitIfThenLLVM(IfThenLLVMImp instr, H h);
|
public S visitIfThenLLVM(IfThenLLVMImp instr, H h);
|
||||||
public S visitWhileLLVM(WhileLLVMImp instr, H h);
|
public S visitWhileLLVM(WhileLLVMImp instr, H h);
|
||||||
public S visitLabelLLVM(LabelLLVMImp 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)
|
//////////ExpressionLLVM (expression)
|
||||||
@@ -53,6 +55,7 @@ public interface Interface {
|
|||||||
public S visitLoadLLVM(loadLLVMImpl e,H h);
|
public S visitLoadLLVM(loadLLVMImpl e,H h);
|
||||||
public S visitValLLVM(ValLLVMImpl e,H h);
|
public S visitValLLVM(ValLLVMImpl e,H h);
|
||||||
public S visitVarLLVM(VarLLVMImpl e,H h);
|
public S visitVarLLVM(VarLLVMImpl e,H h);
|
||||||
|
public S visitIcmpLLVM(IcmpLLVMImp e, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public interface IdentifierLLVM{ //globaux @ et local %
|
/*public interface IdentifierLLVM{ //globaux @ et local %
|
||||||
@@ -64,12 +67,12 @@ public interface Interface {
|
|||||||
|
|
||||||
public interface TypeLLVM{
|
public interface TypeLLVM{
|
||||||
public <H,S> S accept(TypeLLVMVisitor<H,S> v, H h);
|
public <H,S> S accept(TypeLLVMVisitor<H,S> v, H h);
|
||||||
public int getNbBit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface TypeLLVMVisitor<H,S> {
|
public interface TypeLLVMVisitor<H,S> {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,9 +86,14 @@ TypeLLVMVisitor<String,String>
|
|||||||
return str + e.type().accept(this,h) + " " + e.val1().accept(this,h) + ", " + e.val2().accept(this,h);
|
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
|
@Override
|
||||||
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
||||||
return "alloca" + " i" + e.type().getNbBit();
|
return "alloca" + e.type().accept(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -113,6 +118,23 @@ TypeLLVMVisitor<String,String>
|
|||||||
return INDENT+"call " + "...TODO..." +" scanf " + "...TODO...";
|
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
|
@Override
|
||||||
public String visitIfThenElseLLVM(IfThenElseLLVMImp instr, String h) {
|
public String visitIfThenElseLLVM(IfThenElseLLVMImp instr, String h) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
@@ -153,7 +175,7 @@ TypeLLVMVisitor<String,String>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitLabelLLVM(LabelLLVMImp instr, String h) {
|
public String visitBooleanLLVM(BooleanLLVMImp e, String h) {
|
||||||
return "";
|
return "i1";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,19 +30,28 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
|
|
||||||
//Instructon :
|
//Instructon :
|
||||||
|
//Label
|
||||||
public static record LabelLLVMImp(String nom) implements InstructionLLVM{
|
public static record LabelLLVMImp(String nom) implements InstructionLLVM{
|
||||||
public String prettyprinter(){
|
|
||||||
StringBuilder str = new StringBuilder();
|
|
||||||
return str.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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{
|
public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
|
||||||
@Override
|
@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
|
//Val
|
||||||
|
|
||||||
@@ -186,27 +207,24 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
|
|
||||||
public static record IntLLVMImpl() implements TypeLLVM{
|
public static record IntLLVMImpl() implements TypeLLVM{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitIntLLVM(this, h);
|
return v.visitIntLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNbBit(){
|
|
||||||
return 32;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static record VoidLLVMImpl() implements TypeLLVM{
|
public static record VoidLLVMImpl() implements TypeLLVM{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitVoidLLVM(this, h);
|
return v.visitVoidLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getNbBit(){
|
public static record BooleanLLVMImp() implements TypeLLVM{
|
||||||
return 0;
|
@Override
|
||||||
|
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitBooleanLLVM(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ FUNC INT main() {
|
|||||||
b:=3
|
b:=3
|
||||||
c:=1
|
c:=1
|
||||||
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
|
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
|
||||||
IF 1 THEN a:=a+1 FI
|
WHILE b - 1
|
||||||
IF 2 THEN b:=c+1 ELSE
|
DO{
|
||||||
|
b := b - 1
|
||||||
|
c := c + 1
|
||||||
|
}
|
||||||
|
DONE
|
||||||
b:=c+1
|
b:=c+1
|
||||||
FI
|
|
||||||
RETURN 4 + 6 * 5 + 2 }
|
RETURN 4 + 6 * 5 + 2 }
|
||||||
Reference in New Issue
Block a user