LLVM + interface visitor

This commit is contained in:
Vu Tuan Minh
2025-04-04 16:33:42 +02:00
parent bd7b74461e
commit 23b4accb7e
10 changed files with 198 additions and 109 deletions

View File

@@ -1,4 +1,4 @@
// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1
// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1
package TP2;

View File

@@ -1,10 +1,9 @@
// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1
// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1
package TP2;
import TP2.asd.Program.*;
import TP2.asd.*;
import TP2.asd.Interface.*;
import TP2.asd.Eval.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;

View File

@@ -11,7 +11,6 @@ options {
import TP2.asd.Program.*;
import TP2.asd.*;
import TP2.asd.Interface.*;
import TP2.asd.Eval.*;
}
// On syntax error, raise exception rather than silently recovery

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 : \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 : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");

View File

@@ -1,42 +0,0 @@
package TP2.asd;
import java.util.Map;
import TP2.asd.Interface.*;
import TP2.asd.Program.*;
public class Eval {
public class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c();
}
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> h) {
switch(e.op()) {
case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h);
case Op.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h);
case Op.TIMES: return e.e1().accept(this, h)*e.e2().accept(this, h);
case Op.DIV: return e.e1().accept(this, h)/e.e2().accept(this, h);
case Op.MOD: return e.e1().accept(this, h)%e.e2().accept(this, h);
default: throw new IllegalArgumentException();
}
}
}
public class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
ExprEval exprEval = new ExprEval();
return e.e().accept(exprEval, h);
}
@Override
public Integer visitAssign(AssignImp e, Map<String, Integer> h) {
//h.put(e.i(), e.t());
//TODO
return 1;
}
}
}

View File

@@ -1,46 +1,52 @@
package TP2.asd;
import java.util.ArrayList;
import java.util.Map;
import TP2.asd.Program.*;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public interface Interface{
//////////Program
public interface ProgramI {
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
public String prettyprinter();
public ProgramLLVMImpl toLLVM();
}
public interface Function {
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public DefineLLVM toLLVM();
}
public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public InstructionLLVM toLLVM();
}
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface ProgramVisitor<H,S> {
public S visitProgram(ProgramImp programImp, H h);
}
//////////Function
public interface Function {
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public DefineLLVM toLLVM();
}
public interface FunctionVisitor<H,S> {
public S visitFunction(FunctionImp e, H h);
}
//////////Instruction
public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public InstructionLLVM toLLVM();
}
public interface InstrVisitor<H,S>{
public S visitReturn(Return_instrImp e, H h);
public S visitAssign(AssignImp e, H h);
}
//////////Expression
//We put prettyprinter here beause each expr will have to implement it like accept visitor
//but each implement will be different for prettyprinter
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface ExprVisitor<H,S> {
public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);

View File

@@ -2,15 +2,10 @@ package TP2.asd;
import java.util.ArrayList;
import java.util.Map;
import org.antlr.grammar.v3.ANTLRParser.defaultNodeOption_return;
import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*;
import TP2.llvm.Interface.*;
public class Program{
static String INDENT = " ";
@@ -35,10 +30,10 @@ public class Program{
for(int i = 0; i<fonctions.size(); i++){
fonctionLLVM.add(fonctions.get(i).toLLVM());
}
return new ProgramLLVMImpl(0,new ArrayList<>(),fonctionLLVM);
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
}
}
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
@@ -65,7 +60,8 @@ public class Program{
for(int i = 0; i<instructions.size(); i++){
instrLLVM.add(instructions.get(i).toLLVM());
}
return new DefineLLVMImpl(nom, 32, instrLLVM);
//return new DefineLLVMImpl(nom, "int", instrLLVM);
return null;
}
}

View File

@@ -1,7 +1,48 @@
package TP2.asd;
import java.util.Stack;
import org.pcollections.*;
import TP2.asd.Interface.Type;
public class SymTable {
private Stack<PMap<String,Type>> stackMap;
public SymTable(){
this.stackMap= new Stack<>();
}
public void next_layer(){
stackMap.push(HashTreePMap.empty());
}
public void quit_layer() throws Exception{
if(stackMap.isEmpty()){
throw new Exception();
}
stackMap.pop();
}
public PMap<String,Type> peppapeek(){
return stackMap.peek();
}
public void addVar(String s, Type t){
//Save temporary if not PMap wont save
PMap<String, Type> pmap = this.peppapeek();
pmap= pmap.plus(s,t);
//Delete old ones
stackMap.pop();
//Push the new one
stackMap.push(pmap);
}
//Usually look for var in highest level , if not found research.
public boolean searchVar(String s){
for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){
return true;
}
}
return false;
}
}

View File

@@ -1,15 +1,29 @@
package TP2.llvm;
import TP2.asd.Program.ProgramImp;
import TP2.llvm.ProgramLLVM.*;
public interface Interface {
//////////ProgramLLVM
public interface ProgLLVM{
public <H,S> S accept(ProgramLLVMVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface ProgramLLVMVisitor<H,S> {
public S visitProgramLLVM(ProgramLLVMImpl programImp, H h);
}
//////////DefineLLVM
public interface DefineLLVM{
public <H,S> S accept(DefineLLVMVisitor<H,S> v, H h);
public String prettyprinter();
}
public interface DefineLLVMVisitor<H,S> {
public S visitProgramLLVM(DefineLLVMImpl defineLLVMImp, H h);
}
public interface IdentifierLLVM{ //globaux @ et local %
public String prettyprinter();
}

View File

@@ -2,7 +2,8 @@ package TP2.llvm;
import java.util.ArrayList;
import TP2.asd.Program.ProgramImp;
import TP2.asd.Interface.*;
import TP2.asd.Program.*;
import TP2.llvm.Interface.*;
@@ -10,60 +11,141 @@ public class ProgramLLVM {
static String INDENT = " ";
//TODO //TODO
public static record ProgramLLVMImpl(int target ,ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
//Program
public static record ProgramLLVMImpl(ArrayList<Integer> declration ,ArrayList<DefineLLVM> fonctions) implements ProgLLVM{
public <H, S> S accept(ProgramLLVMVisitor<H, S> v, H h) {
return v.visitProgramLLVM(this, h);
}
public String prettyprinter(){
String str = "";
str += target + "\n"; //TODO
for(int i = 0; i<declration.size(); i++){
str += declration.get(i) + "\n"; //TODO
StringBuilder str = new StringBuilder();
str.append("; Target\n");
str.append("target triple = \"x86_64-pc-linux-gnu\"\n");
str.append("; ; External declaration of the printf function\n");
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");
// Déclaration pour les string
//for(int i = 0; i<declration.size(); i++){
//str.append(declration.get(i) + "\n");
//}
//PROTO et FUNC
for(DefineLLVM fonction : fonctions){
str.append(fonction.prettyprinter());
str.append("\n");
}
for(int i = 0; i < fonctions.size(); i++){
str += fonctions.get(i).prettyprinter() + "\n";
}
return str;
return str.toString();
}
}
public static record DefineLLVMImpl(String nom,int nbBits, ArrayList<InstructionLLVM> instrs) implements DefineLLVM{
public String prettyprinter(){
String str = "define i"+ nbBits + "@"+nom + "() {\n";
for(int i = 0; i < instrs.size(); i++){
str += instrs.get(i).prettyprinter() + "\n";
//Define
public static record DefineLLVMImpl(String nom, Type t, 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()){
case "INT":
return "i32";
case "VOID":
return "void";
default:
throw new AssertionError();
}
str += "}";
return str;
}
public String prettyprinter(){
StringBuilder str = new StringBuilder("define ");
str.append(type_toString()).append(" @").append(nom).append("(");
//TODO param
str.append("){\n");
for(InstructionLLVM instr : instrs){
str.append(instr.prettyprinter()).append("\n");
}
str.append("}");
return str.toString();
}
}
//Instructon :
/*
public static record LabelLLVMImpl(String nom) implements InstructionLLVM{
public String prettyprinter(){
String str = "" + nom + ":";
return str;
StringBuilder str = new StringBuilder();
return str.toString();
}
}
*/
public static record AffectLLVM(Var var, ExpressionLLVM e) implements InstructionLLVM{
public static record AssignLVMImp(Var 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();
}
}
//Expression :
public static record ReturnLLVMImp(ExpressionLLVM e) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReturnLLVM(this, h);
}
public static record AddLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{
public String prettyprinter(){
return "add" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
StringBuilder str = new StringBuilder();
return str.toString();
}
}
//Expression :
public static record BinOpLLVMImp(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");
break;
case "MINUS":
str.append("minus");
break;
default:
throw new AssertionError();
}
return str.append("i32").toString();
}
public String prettyprinter(){
return op_toString() + val1.prettyprinter() + ", " + val2.prettyprinter();
}
}
public static record SubLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{
public String prettyprinter(){
return "sub" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
public static record ConstLLVMImp() 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();
}
}
public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{
@@ -91,10 +173,4 @@ public class ProgramLLVM {
return "%"+nom;
}
}
}