toLLVM en visitor, todo : réparer la Declaration
This commit is contained in:
@@ -23,7 +23,6 @@ public interface Interface{
|
||||
//////////Function
|
||||
public interface Function {
|
||||
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
||||
public DefineLLVM toLLVM();
|
||||
}
|
||||
|
||||
public interface FunctionVisitor<H,S> {
|
||||
@@ -34,13 +33,12 @@ public interface Interface{
|
||||
//////////Instruction
|
||||
public interface Instruction {
|
||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||
public ArrayList<InstructionLLVM> toLLVM();
|
||||
}
|
||||
|
||||
public interface InstrVisitor<H,S>{
|
||||
public S visitReturn(Return_instrImp e, H h);
|
||||
public S visitAssign(AssignImp e, H h);
|
||||
public S visitDeclaration(DeclarationImp e,H h);
|
||||
public S visitReturn(Return_instrImp instr, H h);
|
||||
public S visitAssign(AssignImp instr, H h);
|
||||
public S visitDeclaration(DeclarationImp instr,H h);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +47,6 @@ public interface Interface{
|
||||
//but each implement will be different for prettyprinter
|
||||
public interface Expression {
|
||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
||||
public ArrayList<AssignLVMImp> toLLVM();
|
||||
}
|
||||
|
||||
public interface Val extends Expression{
|
||||
@@ -65,7 +62,6 @@ public interface Interface{
|
||||
|
||||
public interface Type{
|
||||
public <H,S> S accept(TypeVisitor<H,S> v, H h);
|
||||
public TypeLLVM toLLVM();
|
||||
}
|
||||
|
||||
public interface TypeVisitor<H,S>{
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
package TP2.asd;
|
||||
|
||||
import TP2.asd.Interface.TypeVisitor;
|
||||
import TP2.asd.Interface.ExprVisitor;
|
||||
import TP2.asd.Interface.FunctionVisitor;
|
||||
import TP2.asd.Interface.InstrVisitor;
|
||||
import TP2.asd.Interface.ProgramVisitor;
|
||||
|
||||
import TP2.asd.Program.AssignImp;
|
||||
import TP2.asd.Program.BinopExpressionImp;
|
||||
import TP2.asd.Program.ConstImp;
|
||||
import TP2.asd.Program.DeclarationImp;
|
||||
import TP2.asd.Program.FunctionImp;
|
||||
import TP2.asd.Program.ProgramImp;
|
||||
import TP2.asd.Program.Return_instrImp;
|
||||
import TP2.asd.Program.Type_intImp;
|
||||
import TP2.asd.Program.Type_voidImp;
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.asd.Program.*;
|
||||
|
||||
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
FunctionVisitor<String,String>,
|
||||
@@ -25,6 +12,8 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
|
||||
static String INDENT = " ";
|
||||
|
||||
//PROGRAM
|
||||
|
||||
@Override
|
||||
public String visitProgram(ProgramImp prog, String indent) {
|
||||
String str ="";
|
||||
@@ -35,6 +24,8 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
return str;
|
||||
}
|
||||
|
||||
//FUNCTION
|
||||
|
||||
@Override
|
||||
public String visitFunction(FunctionImp fun, String indent) {
|
||||
String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"(){\n";
|
||||
@@ -45,25 +36,29 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
return str;
|
||||
}
|
||||
|
||||
//INSTRUCTION
|
||||
|
||||
@Override
|
||||
public String visitReturn(Return_instrImp e, String indent) {
|
||||
return indent+"RETURN " + e.e().accept(this,"");
|
||||
public String visitReturn(Return_instrImp instr, String indent) {
|
||||
return indent+"RETURN " + instr.e().accept(this,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAssign(AssignImp e, String indent) {
|
||||
return e.t()+ " :=" + e.e().accept(this,"");
|
||||
public String visitAssign(AssignImp instr, String indent) {
|
||||
return instr.t()+ " :=" + instr.e().accept(this,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitDeclaration(DeclarationImp e, String indent) {
|
||||
String str = indent + "declare "+e.t().accept(this,"") + " ";
|
||||
for(int i = 0; i<e.s().size();i++){
|
||||
str += e.s().get(i) + ",";
|
||||
public String visitDeclaration(DeclarationImp instr, String indent) {
|
||||
String str = indent + "declare "+instr.t().accept(this,"") + " ";
|
||||
for(int i = 0; i<instr.s().size();i++){
|
||||
str += instr.s().get(i) + ",";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
//EXPRESSION
|
||||
|
||||
@Override
|
||||
public String visitConst(ConstImp e, String indent) {
|
||||
return e.c()+"";
|
||||
@@ -83,6 +78,8 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
return "(" + e.e1().accept(this,"") +" "+ opStr +" " + e.e2().accept(this,"") + ")";
|
||||
}
|
||||
|
||||
//TYPE
|
||||
|
||||
@Override
|
||||
public String visitInt(Type_intImp t, String h) {
|
||||
return "INT";
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.plaf.synth.SynthLabelUI;
|
||||
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.llvm.ProgramLLVM.*;
|
||||
import TP2.llvm.Interface.*;
|
||||
@@ -24,11 +26,13 @@ public class Program{
|
||||
|
||||
@Override
|
||||
public ProgramLLVMImpl toLLVM() {
|
||||
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
||||
/**ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<fonctions.size(); i++){
|
||||
fonctionLLVM.add(fonctions.get(i).toLLVM());
|
||||
}
|
||||
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
|
||||
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);*/
|
||||
toLLVM_Visitor llvmVisitor = new toLLVM_Visitor();
|
||||
return this.accept(llvmVisitor,new SymTable());
|
||||
}
|
||||
|
||||
|
||||
@@ -43,15 +47,6 @@ public class Program{
|
||||
public <H, S> S accept(FunctionVisitor<H, S> v, H h) {
|
||||
return v.visitFunction(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefineLLVM toLLVM() {
|
||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<instructions.size(); i++){
|
||||
instrLLVM.addAll(instructions.get(i).toLLVM());
|
||||
}
|
||||
return new DefineLLVMImpl(nom, type.toLLVM(), instrLLVM);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,14 +60,6 @@ public class Program{
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<AssignLVMImp> toLLVM() { //TODO
|
||||
// TODO Auto-generated method stub
|
||||
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
||||
ValLLVM cLLVM = new ValLLVMImpl(new IntLLVMImpl(),c);
|
||||
list.add(new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl(),"todo"), cLLVM));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,32 +68,6 @@ public class Program{
|
||||
return v.visitBinOp(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<AssignLVMImp> toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir être mise directement dans l'expression
|
||||
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
||||
ValLLVM val1 = null;
|
||||
ValLLVM val2 = null;
|
||||
if(e1 instanceof Val){
|
||||
val1 = ((Val) e1).getValLLVM();
|
||||
}
|
||||
else {
|
||||
ArrayList<AssignLVMImp> eLLVM1 = e1.toLLVM();
|
||||
list.addAll(eLLVM1);
|
||||
val1 = eLLVM1.getLast().getVar();
|
||||
}
|
||||
if(e2 instanceof Val){
|
||||
val2 = ((Val) e2).getValLLVM();
|
||||
}
|
||||
else {
|
||||
ArrayList<AssignLVMImp> eLLVM2 = e2.toLLVM();
|
||||
list.addAll(eLLVM2);
|
||||
val2 = eLLVM2.getLast().getVar();
|
||||
}
|
||||
|
||||
list.add(new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,"todo"), new BinOpLLVMImp(new IntLLVMImpl()/*TODO*/,op,val1,val2)));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -116,16 +77,6 @@ public class Program{
|
||||
return v.visitReturn(this,h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> toLLVM() {
|
||||
ArrayList<AssignLVMImp> list = e.toLLVM();
|
||||
InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl()/*TODO*/,list.getLast().getVar());
|
||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||
result.addAll(list);
|
||||
result.add(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -133,30 +84,13 @@ public class Program{
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitAssign(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> toLLVM() {
|
||||
ArrayList<AssignLVMImp> list = e.toLLVM();
|
||||
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,t),list.getLast().getVar());
|
||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||
result.addAll(list);
|
||||
result.add(r);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static record DeclarationImp(Type t, ArrayList<String> s) implements Instruction{
|
||||
|
||||
@Override
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitDeclaration(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> toLLVM() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
|
||||
}
|
||||
}
|
||||
|
||||
//Type
|
||||
@@ -165,12 +99,6 @@ public class Program{
|
||||
public <H, S> S accept(TypeVisitor<H, S> v, H h) {
|
||||
return v.visitVoid(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLLVM toLLVM() {
|
||||
return new IntLLVMImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -179,11 +107,6 @@ public class Program{
|
||||
public <H, S> S accept(TypeVisitor<H, S> v, H h) {
|
||||
return v.visitInt(this, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLLVM toLLVM() {
|
||||
return new VoidLLVMImpl();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
118
src/main/java/TP2/asd/toLLVM_Visitor.java
Normal file
118
src/main/java/TP2/asd/toLLVM_Visitor.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.asd.Program.*;
|
||||
import TP2.asd.SymTable.*;
|
||||
import TP2.llvm.Interface.*;
|
||||
import TP2.llvm.ProgramLLVM;
|
||||
import TP2.llvm.ProgramLLVM.*;
|
||||
|
||||
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
||||
FunctionVisitor<SymTable,DefineLLVM>,
|
||||
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
||||
ExprVisitor<SymTable,ArrayList<AssignLVMImp>>,
|
||||
TypeVisitor<SymTable,TypeLLVM>
|
||||
{
|
||||
|
||||
|
||||
//PROGRAM
|
||||
|
||||
@Override
|
||||
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
|
||||
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<prog.fonctions().size(); i++){
|
||||
fonctionLLVM.add(prog.fonctions().get(i).accept(this, h));
|
||||
}
|
||||
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
|
||||
}
|
||||
|
||||
//FUNCTION
|
||||
|
||||
@Override
|
||||
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
|
||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||
for(int i = 0; i<fun.instructions().size(); i++){
|
||||
instrLLVM.addAll(fun.instructions().get(i).accept(this, h));
|
||||
}
|
||||
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
|
||||
}
|
||||
|
||||
//INSTRUCTION
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
|
||||
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
|
||||
InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl()/*TODO*/,list.getLast().getVar());
|
||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||
result.addAll(list);
|
||||
result.add(r);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
||||
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
|
||||
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,instr.t()),list.getLast().getVar());
|
||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||
result.addAll(list);
|
||||
result.add(r);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitDeclaration(DeclarationImp instr, SymTable h) {
|
||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||
for(int i = 0; i<instr.s().size();i++){
|
||||
TypeLLVM t2 = instr.t().accept(this,h);
|
||||
String name = instr.s().get(i);
|
||||
list.add(new AssignLVMImp(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<AssignLVMImp> visitConst(ConstImp e, SymTable h) {
|
||||
throw new UnsupportedOperationException("Can't call toLLVM on Expression Const");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<AssignLVMImp> visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
||||
ValLLVM val1 = null;
|
||||
ValLLVM val2 = null;
|
||||
if(e.e1() instanceof Val){
|
||||
val1 = ((Val) e.e1()).getValLLVM();
|
||||
}
|
||||
else {
|
||||
ArrayList<AssignLVMImp> eLLVM1 = e.e1().accept(this,h);
|
||||
list.addAll(eLLVM1);
|
||||
val1 = eLLVM1.getLast().getVar();
|
||||
}
|
||||
if(e.e2() instanceof Val){
|
||||
val2 = ((Val) e.e2()).getValLLVM();
|
||||
}
|
||||
else {
|
||||
ArrayList<AssignLVMImp> eLLVM2 = e.e2().accept(this,h);
|
||||
list.addAll(eLLVM2);
|
||||
val2 = eLLVM2.getLast().getVar();
|
||||
}
|
||||
|
||||
list.add(new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,"todo"), new BinOpLLVMImp(new IntLLVMImpl()/*TODO*/,e.op(),val1,val2)));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
|
||||
return new IntLLVMImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLLVM visitVoid(Type_voidImp t, SymTable h) {
|
||||
return new VoidLLVMImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,6 +57,7 @@ public interface Interface {
|
||||
|
||||
public interface TypeLLVM{
|
||||
public String prettyprinter();
|
||||
public int getNbBit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -153,9 +153,9 @@ public class ProgramLLVM {
|
||||
}*/
|
||||
|
||||
|
||||
public static record allocaLLVMImpl(TypeLLVM type, int nbBits) implements ExpressionLLVM{
|
||||
public static record allocaLLVMImpl(TypeLLVM type) implements ExpressionLLVM{
|
||||
public String prettyprinter(){
|
||||
return "aloca" + " i" + nbBits;
|
||||
return "aloca" + " i" + type.getNbBit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +190,10 @@ public class ProgramLLVM {
|
||||
return "i32";
|
||||
}
|
||||
|
||||
public int getNbBit(){
|
||||
return 32;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -200,5 +204,9 @@ public class ProgramLLVM {
|
||||
return "void";
|
||||
}
|
||||
|
||||
public int getNbBit(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user