prettyprinter LLVM en visitor
This commit is contained in:
@@ -1,9 +1,4 @@
|
|||||||
package TP2.llvm;
|
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.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
public interface Interface {
|
public interface Interface {
|
||||||
@@ -14,51 +9,59 @@ public interface Interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface ProgramLLVMVisitor<H,S> {
|
public interface ProgramLLVMVisitor<H,S> {
|
||||||
public S visitProgramLLVM(ProgramLLVMImpl e, H h);
|
public S visitProgramLLVM(ProgramLLVMImpl prog, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////DefineLLVM (function)
|
//////////DefineLLVM (function)
|
||||||
public interface DefineLLVM{
|
public interface DefineLLVM{
|
||||||
public <H,S> S accept(DefineLLVMVisitor<H,S> v, H h);
|
public <H,S> S accept(DefineLLVMVisitor<H,S> v, H h);
|
||||||
public String prettyprinter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface DefineLLVMVisitor<H,S> {
|
public interface DefineLLVMVisitor<H,S> {
|
||||||
public S visitProgramLLVM(DefineLLVMImpl e, H h);
|
public S visitDefineLLVM(DefineLLVMImpl define, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface InstructionLLVM{
|
public interface InstructionLLVM{
|
||||||
public <H,S> S accept(InstructionLLVMVisitor<H,S> v, H h);
|
public <H,S> S accept(InstructionLLVMVisitor<H,S> v, H h);
|
||||||
public String prettyprinter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface InstructionLLVMVisitor<H,S> {
|
public interface InstructionLLVMVisitor<H,S> {
|
||||||
public S visitReturnLLVM(ReturnLLVMImp e, H h);
|
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
|
||||||
public S visitAssignLLVM(AssignLVMImp e, H h);
|
public S visitAssignLLVM(AssignLVMImp instr, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////ExpressionLLVM (expression)
|
//////////ExpressionLLVM (expression)
|
||||||
public interface ExpressionLLVM{
|
public interface ExpressionLLVM{
|
||||||
public String prettyprinter();
|
public <H,S> S accept(ExpressionLLVMVisitor<H,S> v, H h);
|
||||||
public TypeLLVM getType();
|
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 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 String prettyprinter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IndentifierLLVMVisitor<H,S> {
|
||||||
|
}*/
|
||||||
|
|
||||||
public interface TypeLLVM{
|
public interface TypeLLVM{
|
||||||
public String prettyprinter();
|
public <H,S> S accept(TypeLLVMVisitor<H,S> v, H h);
|
||||||
public int getNbBit();
|
public int getNbBit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface TypeLLVMVisitor<H,S> {
|
||||||
|
public S visitIntLLVM(IntLLVMImpl e,H h);
|
||||||
|
public S visitVoidLLVM(VoidLLVMImpl e, H h);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
118
src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java
Normal file
118
src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java
Normal 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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package TP2.llvm;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import TP2.asd.Interface.*;
|
import TP2.asd.Interface.*;
|
||||||
import TP2.asd.Program.*;
|
|
||||||
import TP2.llvm.Interface.*;
|
import TP2.llvm.Interface.*;
|
||||||
|
|
||||||
|
|
||||||
@@ -19,61 +18,16 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String prettyprinter(){
|
public String prettyprinter(){
|
||||||
StringBuilder str = new StringBuilder();
|
PrettyprinterLLVM_Visitor ppVisitor = new PrettyprinterLLVM_Visitor();
|
||||||
str.append("; Target\n");
|
return this.accept(ppVisitor, "");
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Define
|
//Define
|
||||||
public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
|
public static record DefineLLVMImpl(String nom, TypeLLVM type, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
|
||||||
public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(DefineLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitProgramLLVM(this, h);
|
return v.visitDefineLLVM(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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,9 +47,6 @@ public class ProgramLLVM {
|
|||||||
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitAssignLLVM(this, 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{
|
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) {
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitReturnLLVM(this, h);
|
return v.visitReturnLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String prettyprinter(){
|
|
||||||
return INDENT+"ret " + type.prettyprinter() + " " + e.prettyprinter();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Expression :
|
//Expression :
|
||||||
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
|
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) {
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
return v.visitBinOpLLVM(this, 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
|
@Override
|
||||||
public TypeLLVM getType() {
|
public TypeLLVM getType() {
|
||||||
@@ -165,9 +84,11 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
|
|
||||||
public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{
|
public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{
|
||||||
public String prettyprinter(){
|
@Override
|
||||||
return "aloca" + " i" + type.getNbBit();
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitAllocaLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeLLVM getType() {
|
public TypeLLVM getType() {
|
||||||
return type;
|
return type;
|
||||||
@@ -176,9 +97,11 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
|
|
||||||
public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, ValLLVM val) implements ExpressionLLVM{
|
public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, ValLLVM val) implements ExpressionLLVM{
|
||||||
public String prettyprinter(){
|
@Override
|
||||||
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter();
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitLoadLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeLLVM getType() {
|
public TypeLLVM getType() {
|
||||||
return type;
|
return type;
|
||||||
@@ -189,8 +112,9 @@ public class ProgramLLVM {
|
|||||||
//Val
|
//Val
|
||||||
|
|
||||||
public static record ValLLVMImpl(TypeLLVM type, int val) implements ValLLVM{
|
public static record ValLLVMImpl(TypeLLVM type, int val) implements ValLLVM{
|
||||||
public String prettyprinter(){
|
@Override
|
||||||
return val + "";
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitValLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -201,8 +125,9 @@ public class ProgramLLVM {
|
|||||||
|
|
||||||
|
|
||||||
public static record VarLLVMImpl(TypeLLVM type, String nom) implements ValLLVM{
|
public static record VarLLVMImpl(TypeLLVM type, String nom) implements ValLLVM{
|
||||||
public String prettyprinter(){
|
@Override
|
||||||
return "%"+nom;
|
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitVarLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -215,27 +140,25 @@ public class ProgramLLVM {
|
|||||||
public static record IntLLVMImpl() implements TypeLLVM{
|
public static record IntLLVMImpl() implements TypeLLVM{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String prettyprinter() {
|
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
||||||
return "i32";
|
return v.visitIntLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNbBit(){
|
public int getNbBit(){
|
||||||
return 32;
|
return 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static record VoidLLVMImpl() implements TypeLLVM{
|
public static record VoidLLVMImpl() implements TypeLLVM{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String prettyprinter() {
|
public <H, S> S accept(TypeLLVMVisitor<H, S> v, H h) {
|
||||||
return "void";
|
return v.visitVoidLLVM(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNbBit(){
|
public int getNbBit(){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user