toLLVM, manque table des symboles, et expression pas correct

This commit is contained in:
Rochas
2025-04-05 00:08:50 +02:00
parent 59ba13fe18
commit 7dfb07d59f
5 changed files with 124 additions and 46 deletions

View File

@@ -47,16 +47,16 @@ public class Main {
// Pretty-print the program (to debug parsing) // Pretty-print the program (to debug parsing)
System.err.println("todo " + ast); System.err.println("todo " + ast);
System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); System.out.println("\n\n PRETTYPRINTER VSK : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
// Verify the program semantic // Verify the program semantic
// Generate the intermediate representation // Generate the intermediate representation
System.out.println("todo"); System.out.println("todo");
//ProgramLLVMImpl astLLVM = ast.toLLVM(); ProgramLLVMImpl astLLVM = ast.toLLVM();
//System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); System.out.println("\n\n PRETTYPRINTER LLBD : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");

View File

@@ -1,5 +1,8 @@
package TP2.asd; package TP2.asd;
import java.util.ArrayList;
import java.util.List;
import TP2.asd.Program.*; import TP2.asd.Program.*;
import TP2.llvm.Interface.*; import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
@@ -31,7 +34,7 @@ public interface Interface{
public interface Instruction { public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h); public <H,S> S accept(InstrVisitor<H,S> v, H h);
public String prettyprinter(String indent); public String prettyprinter(String indent);
public InstructionLLVM toLLVM(); public ArrayList<InstructionLLVM> toLLVM();
} }
public interface InstrVisitor<H,S>{ public interface InstrVisitor<H,S>{
@@ -45,7 +48,7 @@ public interface Interface{
public interface Expression { public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h); public <H,S> S accept(ExprVisitor<H,S> v, H h);
public String prettyprinter(); public String prettyprinter();
public DefineLLVM toLLVM(); public ArrayList<AssignLVMImp> toLLVM();
} }
public interface ExprVisitor<H,S> { public interface ExprVisitor<H,S> {
@@ -55,6 +58,7 @@ public interface Interface{
public interface Type{ public interface Type{
public String prettyprinter(); public String prettyprinter();
public TypeLLVM toLLVM();
} }
public enum Op {PLUS, MINUS, TIMES, DIV, MOD} public enum Op {PLUS, MINUS, TIMES, DIV, MOD}

View File

@@ -1,6 +1,7 @@
package TP2.asd; package TP2.asd;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import TP2.asd.Interface.*; import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
@@ -36,6 +37,7 @@ public class Program{
} }
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function { public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
public FunctionImp(Type type, String name, Instruction instruction) { public FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }}); this(type, name, new ArrayList<>() {{ add(instruction); }});
@@ -58,13 +60,13 @@ public class Program{
public DefineLLVM toLLVM() { public DefineLLVM toLLVM() {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>(); ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instructions.size(); i++){ for(int i = 0; i<instructions.size(); i++){
instrLLVM.add(instructions.get(i).toLLVM()); instrLLVM.addAll(instructions.get(i).toLLVM());
} }
//return new DefineLLVMImpl(nom, "int", instrLLVM); return new DefineLLVMImpl(nom, type.toLLVM(), instrLLVM);
return null;
} }
} }
public static record ConstImp(int c) implements Expression{ public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) { public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h); return v.visitConst(this, h);
@@ -75,12 +77,16 @@ public class Program{
} }
@Override @Override
public DefineLLVM toLLVM() { public ArrayList<AssignLVMImp> toLLVM() { //TODO
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); ArrayList<AssignLVMImp> list = new ArrayList<>();
ConstLLVMImp cLLVM = new ConstLLVMImp(new IntLLVMImpl(),c);
list.add(new AssignLVMImp(new VarLLVMImpl("todo"), cLLVM));
return list;
} }
} }
public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{ public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) { public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h); return v.visitBinOp(this, h);
@@ -100,13 +106,22 @@ public class Program{
} }
@Override @Override
public DefineLLVM toLLVM() { public ArrayList<AssignLVMImp> toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir être mise directement dans l'expression
// TODO Auto-generated method stub ArrayList<AssignLVMImp> list = new ArrayList<>();
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); ArrayList<AssignLVMImp> eLLVM1 = e1.toLLVM();
ArrayList<AssignLVMImp> eLLVM2 = e2.toLLVM();
list.addAll(eLLVM1);
list.addAll(eLLVM2);
VarLLVMImpl var1 = eLLVM1.getLast().getVar();
VarLLVMImpl var2 = eLLVM2.getLast().getVar();
list.add(new AssignLVMImp(new VarLLVMImpl("todo"), new BinOpLLVMImp(new IntLLVMImpl(),op,var1,var2)));
return list;
} }
} }
public static record Return_instrImp(Expression e) implements Instruction{ public static record Return_instrImp(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) { public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h); return v.visitReturn(this,h);
@@ -117,11 +132,18 @@ public class Program{
} }
@Override @Override
public InstructionLLVM toLLVM() { public ArrayList<InstructionLLVM> toLLVM() {
// TODO Auto-generated method stub ArrayList<AssignLVMImp> list = e.toLLVM();
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl(),list.getLast().getVar());
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
result.add(r);
return result;
} }
} }
public static record AssignImp(String t, Expression e) implements Instruction{ public static record AssignImp(String t, Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) { public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitAssign(this, h); return v.visitAssign(this, h);
@@ -134,24 +156,41 @@ public class Program{
} }
@Override @Override
public InstructionLLVM toLLVM() { public ArrayList<InstructionLLVM> toLLVM() {
// TODO Auto-generated method stub ArrayList<AssignLVMImp> list = e.toLLVM();
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'"); InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(t),list.getLast().getVar());
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
result.add(r);
return result;
} }
} }
public static record Type_voidImp() implements Type{ public static record Type_voidImp() implements Type{
public String prettyprinter() { public String prettyprinter() {
return "VOID"; return "VOID";
} }
@Override
public TypeLLVM toLLVM() {
return new IntLLVMImpl();
}
} }
public static record Type_intImp() implements Type{ public static record Type_intImp() implements Type{
public String prettyprinter() { public String prettyprinter() {
return "INT"; return "INT";
} }
@Override
public TypeLLVM toLLVM() {
return new VoidLLVMImpl();
}
} }
//Eval //Eval
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> { public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
@Override @Override
@@ -165,6 +204,7 @@ public class Program{
} }
} }
public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> { public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> {
@Override @Override
@@ -178,6 +218,7 @@ public class Program{
} }
} }
public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{ public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override @Override
@@ -193,6 +234,7 @@ public class Program{
} }
} }
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{ public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){ public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c(); return c.c();

View File

@@ -47,7 +47,7 @@ public interface Interface {
public S visitBinOpLLVM(BinOpLLVMImp e, H h); public S visitBinOpLLVM(BinOpLLVMImp e, H h);
} }
public interface Val{ public interface Val extends ExpressionLLVM{
public String prettyprinter(); public String prettyprinter();
} }
@@ -55,4 +55,8 @@ public interface Interface {
public String prettyprinter(); public String prettyprinter();
} }
public interface TypeLLVM{
public String prettyprinter();
}
} }

View File

@@ -26,7 +26,7 @@ public class ProgramLLVM {
str.append("declare i32 @printf (i8 * noalias nocapture, ...)\n"); str.append("declare i32 @printf (i8 * noalias nocapture, ...)\n");
str.append("declare i32 @scanf (i8 * noalias nocapture, ...)\n"); str.append("declare i32 @scanf (i8 * noalias nocapture, ...)\n");
str.append("\n"); str.append("\n");
str.append("; Actual code begins"); str.append("; Actual code begins\n");
// Déclaration pour les string // Déclaration pour les string
//for(int i = 0; i<declration.size(); i++){ //for(int i = 0; i<declration.size(); i++){
@@ -44,14 +44,15 @@ public class ProgramLLVM {
} }
//Define //Define
public static record DefineLLVMImpl(String nom, Type t, 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.visitProgramLLVM(this, h);
} }
public String type_toString(){ /*public String type_toString(){
switch(t.toString()){ switch(t.prettyprinter()){
case "INT": case "INT":
return "i32"; return "i32";
case "VOID": case "VOID":
@@ -59,11 +60,11 @@ public class ProgramLLVM {
default: default:
throw new AssertionError(); throw new AssertionError();
} }
} }*/
public String prettyprinter(){ public String prettyprinter(){
StringBuilder str = new StringBuilder("define "); StringBuilder str = new StringBuilder("define ");
str.append(type_toString()).append(" @").append(nom).append("("); str.append(type.prettyprinter()).append(" @").append(nom).append("(");
//TODO param //TODO param
@@ -76,6 +77,7 @@ public class ProgramLLVM {
} }
} }
//Instructon : //Instructon :
/* /*
public static record LabelLLVMImpl(String nom) implements InstructionLLVM{ public static record LabelLLVMImpl(String nom) implements InstructionLLVM{
@@ -86,91 +88,117 @@ public class ProgramLLVM {
} }
*/ */
public static record AssignLVMImp(Var var, ExpressionLLVM e) implements InstructionLLVM{ public static record AssignLVMImp(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
@Override @Override
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(){ public String prettyprinter(){
return "%" + var.prettyprinter() + " = " + e.prettyprinter(); return INDENT+var.prettyprinter() + " = " + e.prettyprinter();
}
public VarLLVMImpl getVar(){
return var;
} }
} }
public static record ReturnLLVMImp(ExpressionLLVM e) implements InstructionLLVM{ public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
@Override @Override
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(){ public String prettyprinter(){
StringBuilder str = new StringBuilder(); return INDENT+"ret " + type.prettyprinter() + " " + e.prettyprinter();
return str.toString();
} }
} }
//Expression : //Expression :
public static record BinOpLLVMImp(Op op, Val val1,Val val2) implements ExpressionLLVM{ public static record BinOpLLVMImp(TypeLLVM type,Op op, Val val1,Val val2) implements ExpressionLLVM{
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(){ public String op_toString(){
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
switch(op.toString()){ switch(op){
case "PLUS": case PLUS:
str.append("add"); str.append("add ");
break; break;
case "MINUS": case MINUS:
str.append("minus"); str.append("minus ");
break; break;
default: default:
throw new AssertionError(); throw new AssertionError();
} }
return str.append("i32").toString(); return str.toString(); //TODO
} }
public String prettyprinter(){ public String prettyprinter(){
return op_toString() + val1.prettyprinter() + ", " + val2.prettyprinter(); return op_toString() + type.prettyprinter() + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
} }
} }
public static record ConstLLVMImp() implements ExpressionLLVM{
public static record ConstLLVMImp(TypeLLVM type, int val) implements ExpressionLLVM{
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitConstLLVM(this, h); return v.visitConstLLVM(this, h);
} }
@Override @Override
public String prettyprinter() { public String prettyprinter() {
StringBuilder str= new StringBuilder(); return val+"";
return str.toString();
} }
} }
public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{
public static record allocaLLVMImpl(TypeLLVM type, int nbBits) implements ExpressionLLVM{
public String prettyprinter(){ public String prettyprinter(){
return "aloca" + " i" + nbBits; return "aloca" + " i" + nbBits;
} }
} }
public static record loadLLVMImpl(int nbBits,int nbBits2, Val val) implements ExpressionLLVM{
public static record loadLLVMImpl(TypeLLVM type, int nbBits,int nbBits2, Val val) implements ExpressionLLVM{
public String prettyprinter(){ public String prettyprinter(){
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter(); return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter();
} }
} }
//Val //Val
public static record ValImpl(int val) implements Val{ public static record ValLLVMImpl(int val) implements Val{
public String prettyprinter(){ public String prettyprinter(){
return val + ""; return val + "";
} }
} }
public static record Var(String nom) implements Val{
public static record VarLLVMImpl(String nom) implements Val{
public String prettyprinter(){ public String prettyprinter(){
return "%"+nom; return "%"+nom;
} }
} }
public static record IntLLVMImpl() implements TypeLLVM{
@Override
public String prettyprinter() {
return "i32";
}
}
public static record VoidLLVMImpl() implements TypeLLVM{
@Override
public String prettyprinter() {
return "void";
}
}
} }