LLVM + interface visitor
This commit is contained in:
@@ -2,7 +2,8 @@ package TP2.llvm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import TP2.asd.Program.ProgramImp;
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.asd.Program.*;
|
||||
import TP2.llvm.Interface.*;
|
||||
|
||||
|
||||
@@ -10,60 +11,141 @@ public class ProgramLLVM {
|
||||
|
||||
static String INDENT = " ";
|
||||
|
||||
//TODO //TODO
|
||||
public static record ProgramLLVMImpl(int target ,ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
|
||||
|
||||
//Program
|
||||
public static record ProgramLLVMImpl(ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
|
||||
public <H, S> S accept(ProgramLLVMVisitor<H, S> v, H h) {
|
||||
return v.visitProgramLLVM(this, h);
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
String str = "";
|
||||
str += target + "\n"; //TODO
|
||||
for(int i = 0; i<declration.size(); i++){
|
||||
str += declration.get(i) + "\n"; //TODO
|
||||
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");
|
||||
|
||||
// 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");
|
||||
}
|
||||
for(int i = 0; i < fonctions.size(); i++){
|
||||
str += fonctions.get(i).prettyprinter() + "\n";
|
||||
}
|
||||
return str;
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static record DefineLLVMImpl(String nom,int nbBits, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
|
||||
public String prettyprinter(){
|
||||
String str = "define i"+ nbBits + "@"+nom + "() {\n";
|
||||
for(int i = 0; i < instrs.size(); i++){
|
||||
str += instrs.get(i).prettyprinter() + "\n";
|
||||
//Define
|
||||
public static record DefineLLVMImpl(String nom, Type t, 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.toString()){
|
||||
case "INT":
|
||||
return "i32";
|
||||
case "VOID":
|
||||
return "void";
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
str += "}";
|
||||
return str;
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
StringBuilder str = new StringBuilder("define ");
|
||||
str.append(type_toString()).append(" @").append(nom).append("(");
|
||||
|
||||
//TODO param
|
||||
|
||||
str.append("){\n");
|
||||
for(InstructionLLVM instr : instrs){
|
||||
str.append(instr.prettyprinter()).append("\n");
|
||||
}
|
||||
str.append("}");
|
||||
return str.toString();
|
||||
}
|
||||
}
|
||||
|
||||
//Instructon :
|
||||
|
||||
/*
|
||||
public static record LabelLLVMImpl(String nom) implements InstructionLLVM{
|
||||
public String prettyprinter(){
|
||||
String str = "" + nom + ":";
|
||||
return str;
|
||||
StringBuilder str = new StringBuilder();
|
||||
return str.toString();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static record AffectLLVM(Var var, ExpressionLLVM e) implements InstructionLLVM{
|
||||
public static record AssignLVMImp(Var var, ExpressionLLVM e) implements InstructionLLVM{
|
||||
@Override
|
||||
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||
return v.visitAssignLLVM(this, h);
|
||||
}
|
||||
public String prettyprinter(){
|
||||
return "%" + var.prettyprinter() + " = " + e.prettyprinter();
|
||||
}
|
||||
}
|
||||
|
||||
//Expression :
|
||||
public static record ReturnLLVMImp(ExpressionLLVM e) implements InstructionLLVM{
|
||||
@Override
|
||||
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||
return v.visitReturnLLVM(this, h);
|
||||
}
|
||||
|
||||
public static record AddLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{
|
||||
public String prettyprinter(){
|
||||
return "add" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
|
||||
StringBuilder str = new StringBuilder();
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Expression :
|
||||
public static record BinOpLLVMImp(Op op, Val val1,Val val2) implements ExpressionLLVM{
|
||||
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.toString()){
|
||||
case "PLUS":
|
||||
str.append("add");
|
||||
break;
|
||||
case "MINUS":
|
||||
str.append("minus");
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
return str.append("i32").toString();
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
return op_toString() + val1.prettyprinter() + ", " + val2.prettyprinter();
|
||||
}
|
||||
}
|
||||
|
||||
public static record SubLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{
|
||||
public String prettyprinter(){
|
||||
return "sub" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
|
||||
public static record ConstLLVMImp() implements ExpressionLLVM{
|
||||
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
|
||||
return v.visitConstLLVM(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String prettyprinter() {
|
||||
StringBuilder str= new StringBuilder();
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{
|
||||
@@ -91,10 +173,4 @@ public class ProgramLLVM {
|
||||
return "%"+nom;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user