correction bug expression de toLLVM, tout les test fragment0 marchent

This commit is contained in:
Rochas
2025-04-05 23:33:38 +02:00
parent 5e36f25197
commit c9f1f86a2c
6 changed files with 114 additions and 48 deletions

View File

@@ -49,10 +49,6 @@ public interface Interface{
public <H,S> S accept(ExprVisitor<H,S> v, H h);
}
public interface Val extends Expression{
public ValLLVM getValLLVM();
}
public interface ExprVisitor<H,S> {
public S visitConst(ConstImp e,H h);

View File

@@ -50,16 +50,10 @@ public class Program{
}
public static record ConstImp(int c) implements Val{
public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
}
public ValLLVM getValLLVM(){
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),c);
return val;
}
}

View File

@@ -4,14 +4,23 @@ import java.util.Stack;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
public class SymTable {
private Stack<PMap<String,Type>> stackMap;
private int id=1;
public SymTable(){
this.stackMap= new Stack<>();
}
public String addNewTempVar(/*Type type*/){
//TODO
String newVar = "temp"+this.id;
//this.addVar(newVar,new Type_intImp()); //TODO
id++;
return newVar;
}
public void next_layer(){
stackMap.push(HashTreePMap.empty());
}
@@ -29,7 +38,8 @@ public class SymTable {
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);
pmap= pmap.plus(s/*+"_"+this.id*/,t);
//this.id++;
//Delete old ones
stackMap.pop();
//Push the new one
@@ -38,6 +48,7 @@ public class SymTable {
//Usually look for var in highest level , if not found research.
public boolean searchVar(String s){
//TOTO
for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){
return true;

View File

@@ -6,17 +6,42 @@ import TP2.asd.Interface.*;
import TP2.asd.Program.*;
import TP2.asd.SymTable.*;
import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM;
import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVM>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,ArrayList<AssignLVMImp>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrOrVal>,
TypeVisitor<SymTable,TypeLLVM>
{
/*
pour les Expression,
toLLVM ne renvoit pas la même chose si l'expression est
une simplement un val (var ou const) ou un binop
*/
public static class InstrOrVal{
public ArrayList<AssignLVMImp> instr = null;
public ValLLVM val = null;
public InstrOrVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
this.instr = instr;
this.val = val;
}
public Boolean isVal(){
return instr==null;
}
public ValLLVM getVal(){
if(instr==null){
return val;
}
else{
return instr.getLast().var();
}
}
}
//PROGRAM
@Override
@@ -43,10 +68,14 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl()/*TODO*/,list.getLast().getVar());
InstrOrVal res = instr.e().accept(this,h);
ValLLVM var = res.getVal();
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
if(!res.isVal()){
result.addAll(res.instr);
}
result.add(r);
return result;
@@ -54,10 +83,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,instr.t()),list.getLast().getVar());
InstrOrVal res = instr.e().accept(this,h);
ValLLVM var = res.getVal();
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
if(!res.isVal()){
result.addAll(res.instr);
}
result.add(r);
return result;
}
@@ -74,35 +106,45 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
}
@Override
public ArrayList<AssignLVMImp> visitConst(ConstImp e, SymTable h) {
throw new UnsupportedOperationException("Can't call toLLVM on Expression Const");
public InstrOrVal visitConst(ConstImp e, SymTable h) {
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
return new InstrOrVal(null, val);
}
@Override
public ArrayList<AssignLVMImp> visitBinOp(BinopExpressionImp e, SymTable h) {
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
ArrayList<AssignLVMImp> list = new ArrayList<>();
ValLLVM val1 = null;
ValLLVM val2 = null;
if(e.e1() instanceof Val){
val1 = ((Val) e.e1()).getValLLVM();
}
else {
ArrayList<AssignLVMImp> eLLVM1 = e.e1().accept(this,h);
list.addAll(eLLVM1);
val1 = eLLVM1.getLast().getVar();
}
if(e.e2() instanceof Val){
val2 = ((Val) e.e2()).getValLLVM();
}
else {
ArrayList<AssignLVMImp> eLLVM2 = e.e2().accept(this,h);
list.addAll(eLLVM2);
val2 = eLLVM2.getLast().getVar();
}
list.add(new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,"todo"), new BinOpLLVMImp(new IntLLVMImpl()/*TODO*/,e.op(),val1,val2)));
InstrOrVal res1 = e.e1().accept(this, h);
InstrOrVal res2 = e.e2().accept(this, h);
return list;
ValLLVM val1 = null;
ValLLVM val2 = null;
if(res1.isVal()){
val1 = res1.val;
}
else{
val1 = res1.instr.getLast().var();
list.addAll(res1.instr);
}
if(res2.isVal()){
val2 = res2.val;
}
else{
val2 = res2.instr.getLast().var();
list.addAll(res2.instr);
}
TypeLLVM type = val1.getType();
if(val1.getType().getClass() != val1.getType().getClass()){
throw new UnsupportedOperationException("Type error in VSL file");
}
String temp = h.addNewTempVar();
list.add(new AssignLVMImp(new VarLLVMImpl(type,temp), new BinOpLLVMImp(type,e.op(),val1,val2)));
return new InstrOrVal(list, null);
}
@Override
@@ -115,4 +157,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new VoidLLVMImpl();
}
}

View File

@@ -40,6 +40,7 @@ public interface Interface {
//////////ExpressionLLVM (expression)
public interface ExpressionLLVM{
public String prettyprinter();
public TypeLLVM getType();
}
public interface ExpressionLLVMVisitor<H,S> {

View File

@@ -96,9 +96,6 @@ public class ProgramLLVM {
public String prettyprinter(){
return INDENT+var.prettyprinter() + " = " + e.prettyprinter();
}
public VarLLVMImpl getVar(){
return var;
}
}
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
@@ -136,7 +133,7 @@ public class ProgramLLVM {
break;
case MOD:
str.append("srem "); //modulo signé
break;
default:
throw new AssertionError();
}
@@ -146,6 +143,11 @@ public class ProgramLLVM {
public String prettyprinter(){
return op_toString() + type.prettyprinter() + " " + val1.prettyprinter() + ", " + val2.prettyprinter();
}
@Override
public TypeLLVM getType() {
return type;
}
}
@@ -166,6 +168,10 @@ public class ProgramLLVM {
public String prettyprinter(){
return "aloca" + " i" + type.getNbBit();
}
@Override
public TypeLLVM getType() {
return type;
}
}
@@ -173,6 +179,10 @@ public class ProgramLLVM {
public String prettyprinter(){
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.prettyprinter();
}
@Override
public TypeLLVM getType() {
return type;
}
}
@@ -182,6 +192,11 @@ public class ProgramLLVM {
public String prettyprinter(){
return val + "";
}
@Override
public TypeLLVM getType() {
return type;
}
}
@@ -189,6 +204,11 @@ public class ProgramLLVM {
public String prettyprinter(){
return "%"+nom;
}
@Override
public TypeLLVM getType() {
return type;
}
}