prettyprinter LLVM en visitor

This commit is contained in:
Rochas
2025-04-06 11:26:19 +02:00
parent c9f1f86a2c
commit 7076633d6a
3 changed files with 163 additions and 119 deletions

View File

@@ -3,7 +3,6 @@ package TP2.llvm;
import java.util.ArrayList;
import TP2.asd.Interface.*;
import TP2.asd.Program.*;
import TP2.llvm.Interface.*;
@@ -19,61 +18,16 @@ public class ProgramLLVM {
}
public String prettyprinter(){
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("declare i32 @printf (i8 * noalias nocapture, ...)\n");
str.append("declare i32 @scanf (i8 * noalias nocapture, ...)\n");
str.append("\n");
str.append("; Actual code begins\n");
// Déclaration pour les string
//for(int i = 0; i<declration.size(); i++){
//str.append(declration.get(i) + "\n");
//}
//PROTO et FUNC
for(DefineLLVM fonction : fonctions){
str.append(fonction.prettyprinter());
str.append("\n");
}
return str.toString();
PrettyprinterLLVM_Visitor ppVisitor = new PrettyprinterLLVM_Visitor();
return this.accept(ppVisitor, "");
}
}
//Define
public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
return v.visitProgramLLVM(this, h);
}
/*public String type_toString(){
switch(t.prettyprinter()){
case "INT":
return "i32";
case "VOID":
return "void";
default:
throw new AssertionError();
}
}*/
public String prettyprinter(){
StringBuilder str = new StringBuilder("define ");
str.append(type.prettyprinter()).append(" @").append(nom).append("(");
//TODO param
str.append("){\n");
for(InstructionLLVM instr : instrs){
str.append(instr.prettyprinter()).append("\n");
}
str.append("}");
return str.toString();
return v.visitDefineLLVM(this, h);
}
}
@@ -93,9 +47,6 @@ public class ProgramLLVM {
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitAssignLLVM(this, h);
}
public String prettyprinter(){
return INDENT+var.prettyprinter() + " = " + e.prettyprinter();
}
}
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
@@ -103,46 +54,14 @@ public class ProgramLLVM {
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReturnLLVM(this, h);
}
public String prettyprinter(){
return INDENT+"ret " + type.prettyprinter() + " " + e.prettyprinter();
}
}
//Expression :
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitBinOpLLVM(this, h);
}
public String op_toString(){
StringBuilder str = new StringBuilder();
switch(op){
case PLUS:
str.append("add ");
break;
case MINUS:
str.append("sub ");
break;
case TIMES:
str.append("mul ");
break;
case DIV:
str.append("sdiv "); //division signé
break;
case MOD:
str.append("srem "); //modulo signé
break;
default:
throw new AssertionError();
}
return str.toString(); //TODO
}
public String prettyprinter(){
return op_toString() + type.prettyprinter() + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
}
@Override
public TypeLLVM getType() {
@@ -165,9 +84,11 @@ public class ProgramLLVM {
public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{
public String prettyprinter(){
return "aloca" + " i" + type.getNbBit();
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitAllocaLLVM(this, h);
}
@Override
public TypeLLVM getType() {
return type;
@@ -176,9 +97,11 @@ public class ProgramLLVM {
public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, ValLLVM val) implements ExpressionLLVM{
public String prettyprinter(){
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter();
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitLoadLLVM(this, h);
}
@Override
public TypeLLVM getType() {
return type;
@@ -189,8 +112,9 @@ public class ProgramLLVM {
//Val
public static record ValLLVMImpl(TypeLLVM type, int val) implements ValLLVM{
public String prettyprinter(){
return val + "";
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitValLLVM(this, h);
}
@Override
@@ -201,8 +125,9 @@ public class ProgramLLVM {
public static record VarLLVMImpl(TypeLLVM type, String nom) implements ValLLVM{
public String prettyprinter(){
return "%"+nom;
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitVarLLVM(this, h);
}
@Override
@@ -215,27 +140,25 @@ public class ProgramLLVM {
public static record IntLLVMImpl() implements TypeLLVM{
@Override
public String prettyprinter() {
return "i32";
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 String prettyprinter() {
return "void";
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
return v.visitVoidLLVM(this, h);
}
public int getNbBit(){
return 0;
}
}
}