toLLVM prettyprinter pour assign
This commit is contained in:
@@ -45,19 +45,20 @@ public class Main {
|
|||||||
ProgramImp ast = parser.program();
|
ProgramImp ast = parser.program();
|
||||||
|
|
||||||
// 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 VSL : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
|
|
||||||
|
|
||||||
|
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
||||||
|
// System.out.println(ast.prettyprinter());
|
||||||
|
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
||||||
// 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 LLVM : \n--------------\n");
|
||||||
System.out.println("\n\n PRETTYPRINTER LLBD : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
|
System.out.println(astLLVM.prettyprinter());
|
||||||
|
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException | RecognitionException e) {
|
} catch (IOException | RecognitionException e) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import TP2.asd.Program.*;
|
import TP2.asd.Program.*;
|
||||||
|
import TP2.asd.toLLVM_Visitor.InstrOrVal;
|
||||||
import TP2.llvm.Interface.*;
|
import TP2.llvm.Interface.*;
|
||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitAssign(AssignImp instr, String indent) {
|
public String visitAssign(AssignImp instr, String indent) {
|
||||||
return instr.t()+ " :=" + instr.e().accept(this,"");
|
return indent + instr.t()+ " := " + instr.e().accept(this,"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -85,11 +85,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
||||||
InstrOrVal res = instr.e().accept(this,h);
|
InstrOrVal res = instr.e().accept(this,h);
|
||||||
ValLLVM var = res.getVal();
|
ValLLVM var = res.getVal();
|
||||||
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
if(!res.isVal()){
|
if(!res.isVal()){
|
||||||
result.addAll(res.instr);
|
result.addAll(res.instr);
|
||||||
}
|
}
|
||||||
|
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
||||||
|
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()));
|
||||||
result.add(r);
|
result.add(r);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,6 +112,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return new InstrOrVal(null, val);
|
return new InstrOrVal(null, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InstrOrVal visitVar(VarImp e, SymTable h) {
|
||||||
|
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
|
||||||
|
return new InstrOrVal(null, val);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
||||||
@@ -156,12 +163,4 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
public TypeLLVM visitVoid(Type_voidImp t, SymTable h) {
|
public TypeLLVM visitVoid(Type_voidImp t, SymTable h) {
|
||||||
return new VoidLLVMImpl();
|
return new VoidLLVMImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InstrOrVal visitVar(VarImp e, SymTable h) {
|
|
||||||
return new InstrOrVal(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public interface Interface {
|
|||||||
public interface InstructionLLVMVisitor<H,S> {
|
public interface InstructionLLVMVisitor<H,S> {
|
||||||
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
|
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
|
||||||
public S visitAssignLLVM(AssignLVMImp instr, H h);
|
public S visitAssignLLVM(AssignLVMImp instr, H h);
|
||||||
|
public S visitStoreLLVM(StoreLLVMImp instr, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////ExpressionLLVM (expression)
|
//////////ExpressionLLVM (expression)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ TypeLLVMVisitor<String,String>
|
|||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
str.append("; Target\n");
|
str.append("; Target\n");
|
||||||
str.append("target triple = \"x86_64-pc-linux-gnu\"\n");
|
str.append("target triple = \"x86_64-pc-linux-gnu\"\n");
|
||||||
str.append("; ; External declaration of the printf function\n");
|
str.append("; External declaration of the printf function\n");
|
||||||
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");
|
||||||
@@ -87,7 +87,12 @@ TypeLLVMVisitor<String,String>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
|
||||||
return "aloca" + " i" + e.type().getNbBit();
|
return "alloca" + " i" + e.type().getNbBit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitStoreLLVM(StoreLLVMImp instr, String h) {
|
||||||
|
return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -115,4 +120,5 @@ TypeLLVMVisitor<String,String>
|
|||||||
return "void";
|
return "void";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public static record AssignLVMImp(VarLLVMImpl 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) {
|
||||||
@@ -56,6 +57,14 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static record StoreLLVMImp(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{
|
||||||
|
@Override
|
||||||
|
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
|
||||||
|
return v.visitStoreLLVM(this, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Expression :
|
//Expression :
|
||||||
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
|
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
FUNC INT main() {
|
FUNC INT main() {
|
||||||
INT a,b,c
|
INT a,b,c
|
||||||
a := 2
|
b:=3
|
||||||
|
c:=1
|
||||||
|
a := b+c
|
||||||
RETURN 4 + 6 * 5 + a }
|
RETURN 4 + 6 * 5 + a }
|
||||||
Reference in New Issue
Block a user