toLLVM prettyprinter pour assign

This commit is contained in:
trochas
2025-04-07 16:06:21 +02:00
parent deafaa26fc
commit b3ed282f9a
8 changed files with 39 additions and 20 deletions

View File

@@ -45,19 +45,20 @@ public class Main {
ProgramImp ast = parser.program();
// Pretty-print the program (to debug parsing)
System.err.println("todo " + ast);
System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
//System.err.println("todo " + ast);
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
// System.out.println(ast.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
// Verify the program semantic
// Generate the intermediate representation
System.out.println("todo");
//System.out.println("todo");
ProgramLLVMImpl astLLVM = ast.toLLVM();
System.out.println("\n\n PRETTYPRINTER LLBD : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
System.out.println(astLLVM.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
} catch (IOException | RecognitionException e) {

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import TP2.asd.Program.*;
import TP2.asd.toLLVM_Visitor.InstrOrVal;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;

View File

@@ -45,7 +45,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override
public String visitAssign(AssignImp instr, String indent) {
return instr.t()+ " :=" + instr.e().accept(this,"");
return indent + instr.t()+ " := " + instr.e().accept(this,"");
}
@Override

View File

@@ -85,11 +85,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
InstrOrVal res = instr.e().accept(this,h);
ValLLVM var = res.getVal();
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
if(!res.isVal()){
result.addAll(res.instr);
}
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()));
result.add(r);
return result;
}
@@ -111,6 +112,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new InstrOrVal(null, val);
}
@Override
public InstrOrVal visitVar(VarImp e, SymTable h) {
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
return new InstrOrVal(null, val);
}
@Override
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
ArrayList<AssignLVMImp> list = new ArrayList<>();
@@ -156,12 +163,4 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
public TypeLLVM visitVoid(Type_voidImp t, SymTable h) {
return new VoidLLVMImpl();
}
@Override
public InstrOrVal visitVar(VarImp e, SymTable h) {
return new InstrOrVal(null, null);
}
}

View File

@@ -28,6 +28,7 @@ public interface Interface {
public interface InstructionLLVMVisitor<H,S> {
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
public S visitAssignLLVM(AssignLVMImp instr, H h);
public S visitStoreLLVM(StoreLLVMImp instr, H h);
}
//////////ExpressionLLVM (expression)

View File

@@ -16,7 +16,7 @@ TypeLLVMVisitor<String,String>
StringBuilder str = new StringBuilder();
str.append("; Target\n");
str.append("target triple = \"x86_64-pc-linux-gnu\"\n");
str.append("; ; External declaration of the printf function\n");
str.append("; External declaration of the printf function\n");
str.append("declare i32 @printf (i8 * noalias nocapture, ...)\n");
str.append("declare i32 @scanf (i8 * noalias nocapture, ...)\n");
str.append("\n");
@@ -87,7 +87,12 @@ TypeLLVMVisitor<String,String>
@Override
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
return "aloca" + " i" + e.type().getNbBit();
return "alloca" + " i" + e.type().getNbBit();
}
@Override
public String visitStoreLLVM(StoreLLVMImp instr, String h) {
return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,"");
}
@Override
@@ -115,4 +120,5 @@ TypeLLVMVisitor<String,String>
return "void";
}
}

View File

@@ -42,6 +42,7 @@ public class ProgramLLVM {
}
*/
public static record AssignLVMImp(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -56,6 +57,14 @@ public class ProgramLLVM {
}
}
public static record StoreLLVMImp(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitStoreLLVM(this, h);
}
}
//Expression :
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override