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

View File

@@ -1,6 +1,7 @@
package TP2.asd;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import TP2.asd.Interface.*;
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 FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
@@ -58,13 +60,13 @@ public class Program{
public DefineLLVM toLLVM() {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
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 null;
return new DefineLLVMImpl(nom, type.toLLVM(), instrLLVM);
}
}
public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
@@ -75,12 +77,16 @@ public class Program{
}
@Override
public DefineLLVM toLLVM() {
public ArrayList<AssignLVMImp> toLLVM() { //TODO
// 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 <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h);
@@ -100,13 +106,22 @@ public class Program{
}
@Override
public DefineLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
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<>();
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 <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h);
@@ -117,11 +132,18 @@ public class Program{
}
@Override
public InstructionLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.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 <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitAssign(this, h);
@@ -134,24 +156,41 @@ public class Program{
}
@Override
public InstructionLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.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 String prettyprinter() {
return "VOID";
}
@Override
public TypeLLVM toLLVM() {
return new IntLLVMImpl();
}
}
public static record Type_intImp() implements Type{
public String prettyprinter() {
return "INT";
}
@Override
public TypeLLVM toLLVM() {
return new VoidLLVMImpl();
}
}
//Eval
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
@Override
@@ -165,6 +204,7 @@ public class Program{
}
}
public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> {
@Override
@@ -178,6 +218,7 @@ public class Program{
}
}
public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override
@@ -193,6 +234,7 @@ public class Program{
}
}
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c();

View File

@@ -47,7 +47,7 @@ public interface Interface {
public S visitBinOpLLVM(BinOpLLVMImp e, H h);
}
public interface Val{
public interface Val extends ExpressionLLVM{
public String prettyprinter();
}
@@ -55,4 +55,8 @@ public interface Interface {
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 @scanf (i8 * noalias nocapture, ...)\n");
str.append("\n");
str.append("; Actual code begins");
str.append("; Actual code begins\n");
// Déclaration pour les string
//for(int i = 0; i<declration.size(); i++){
@@ -44,14 +44,15 @@ public class ProgramLLVM {
}
//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) {
return v.visitProgramLLVM(this, h);
}
public String type_toString(){
switch(t.toString()){
/*public String type_toString(){
switch(t.prettyprinter()){
case "INT":
return "i32";
case "VOID":
@@ -59,11 +60,11 @@ public class ProgramLLVM {
default:
throw new AssertionError();
}
}
}*/
public String prettyprinter(){
StringBuilder str = new StringBuilder("define ");
str.append(type_toString()).append(" @").append(nom).append("(");
str.append(type.prettyprinter()).append(" @").append(nom).append("(");
//TODO param
@@ -76,6 +77,7 @@ public class ProgramLLVM {
}
}
//Instructon :
/*
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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitAssignLLVM(this, h);
}
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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReturnLLVM(this, h);
}
public String prettyprinter(){
StringBuilder str = new StringBuilder();
return str.toString();
return INDENT+"ret " + type.prettyprinter() + " " + e.prettyprinter();
}
}
//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) {
return v.visitBinOpLLVM(this, h);
}
public String op_toString(){
StringBuilder str = new StringBuilder();
switch(op.toString()){
case "PLUS":
str.append("add");
switch(op){
case PLUS:
str.append("add ");
break;
case "MINUS":
str.append("minus");
case MINUS:
str.append("minus ");
break;
default:
throw new AssertionError();
}
return str.append("i32").toString();
return str.toString(); //TODO
}
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) {
return v.visitConstLLVM(this, h);
}
@Override
public String prettyprinter() {
StringBuilder str= new StringBuilder();
return str.toString();
return val+"";
}
}
public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{
public static record allocaLLVMImpl(TypeLLVM type, int nbBits) implements ExpressionLLVM{
public String prettyprinter(){
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(){
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter();
}
}
//Val
public static record ValImpl(int val) implements Val{
public static record ValLLVMImpl(int val) implements Val{
public String prettyprinter(){
return val + "";
}
}
public static record Var(String nom) implements Val{
public static record VarLLVMImpl(String nom) implements Val{
public String prettyprinter(){
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";
}
}
}