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

@@ -1,9 +1,4 @@
package TP2.llvm;
import TP2.asd.Program.AssignImp;
import TP2.asd.Program.BinopExpressionImp;
import TP2.asd.Program.ConstImp;
import TP2.asd.Program.ProgramImp;
import TP2.asd.Program.Return_instrImp;
import TP2.llvm.ProgramLLVM.*;
public interface Interface {
@@ -14,51 +9,59 @@ public interface Interface {
}
public interface ProgramLLVMVisitor<H,S> {
public S visitProgramLLVM(ProgramLLVMImpl e, H h);
public S visitProgramLLVM(ProgramLLVMImpl prog, H h);
}
//////////DefineLLVM (function)
public interface DefineLLVM{
public <H,S> S accept(DefineLLVMVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface DefineLLVMVisitor<H,S> {
public S visitProgramLLVM(DefineLLVMImpl e, H h);
public S visitDefineLLVM(DefineLLVMImpl define, H h);
}
public interface InstructionLLVM{
public <H,S> S accept(InstructionLLVMVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface InstructionLLVMVisitor<H,S> {
public S visitReturnLLVM(ReturnLLVMImp e, H h);
public S visitAssignLLVM(AssignLVMImp e, H h);
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
public S visitAssignLLVM(AssignLVMImp instr, H h);
}
//////////ExpressionLLVM (expression)
public interface ExpressionLLVM{
public String prettyprinter();
public <H,S> S accept(ExpressionLLVMVisitor<H,S> v, H h);
public TypeLLVM getType();
}
public interface ExpressionLLVMVisitor<H,S> {
public S visitConstLLVM(ValLLVM e,H h);
public S visitBinOpLLVM(BinOpLLVMImp e, H h);
}
public interface ValLLVM extends ExpressionLLVM{
public String prettyprinter();
}
public interface IdentifierLLVM{ //globaux @ et local %
public interface ExpressionLLVMVisitor<H,S> {
public S visitBinOpLLVM(BinOpLLVMImp e, H h);
public S visitAllocaLLVM(allocaLLVMImpl e,H h);
public S visitLoadLLVM(loadLLVMImpl e,H h);
public S visitValLLVM(ValLLVMImpl e,H h);
public S visitVarLLVM(VarLLVMImpl e,H h);
}
/*public interface IdentifierLLVM{ //globaux @ et local %
public String prettyprinter();
}
public interface IndentifierLLVMVisitor<H,S> {
}*/
public interface TypeLLVM{
public String prettyprinter();
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);
}
}

View File

@@ -0,0 +1,118 @@
package TP2.llvm;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class PrettyprinterLLVM_Visitor implements ProgramLLVMVisitor<String,String>,
DefineLLVMVisitor<String,String>,
InstructionLLVMVisitor<String,String>,
ExpressionLLVMVisitor<String,String>,
TypeLLVMVisitor<String,String>
{
static String INDENT = " ";
@Override
public String visitProgramLLVM(ProgramLLVMImpl prog, String indent) {
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 : prog.fonctions()){
str.append(fonction.accept(this,indent));
str.append("\n");
}
return str.toString();
}
@Override
public String visitDefineLLVM(DefineLLVMImpl define, String indent) {
StringBuilder str = new StringBuilder("define ");
str.append(define.type().accept(this,indent)).append(" @").append(define.nom()).append("(");
//TODO param
str.append("){\n");
for(InstructionLLVM instr : define.instrs()){
str.append(instr.accept(this,indent)).append("\n");
}
str.append("}");
return str.toString();
}
@Override
public String visitReturnLLVM(ReturnLLVMImp instr, String h) {
return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h);
}
@Override
public String visitAssignLLVM(AssignLVMImp instr, String h) {
return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h);
}
@Override
public String visitBinOpLLVM(BinOpLLVMImp e, String h) {
String str = "";
switch(e.op()){
case PLUS:
str += "add ";
break;
case MINUS:
str += "sub ";
break;
case TIMES:
str += "mul ";
break;
case DIV:
str += "sdiv "; //division signé
break;
case MOD:
str += "srem "; //modulo signé
break;
default:
throw new AssertionError("Binop : opération inconne");
}
return str + e.type().accept(this,h) + " " + e.val1().accept(this,h) + ", " + e.val2().accept(this,h);
}
@Override
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
return "aloca" + " i" + e.type().getNbBit();
}
@Override
public String visitLoadLLVM(loadLLVMImpl e, String h) {
return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h);
}
@Override
public String visitValLLVM(ValLLVMImpl e, String h) {
return e.val() + "";
}
@Override
public String visitVarLLVM(VarLLVMImpl e, String h) {
return "%"+e.nom();
}
@Override
public String visitIntLLVM(IntLLVMImpl e, String h) {
return "i32";
}
@Override
public String visitVoidLLVM(VoidLLVMImpl e, String h) {
return "void";
}
}

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;
}
}
}