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

@@ -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";
}
}
}