correction bug expression de toLLVM, tout les test fragment0 marchent
This commit is contained in:
@@ -49,10 +49,6 @@ public interface Interface{
|
|||||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
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 interface ExprVisitor<H,S> {
|
||||||
public S visitConst(ConstImp e,H h);
|
public S visitConst(ConstImp e,H h);
|
||||||
|
|||||||
@@ -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) {
|
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||||
return v.visitConst(this, h);
|
return v.visitConst(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValLLVM getValLLVM(){
|
|
||||||
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),c);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,23 @@ import java.util.Stack;
|
|||||||
|
|
||||||
import org.pcollections.*;
|
import org.pcollections.*;
|
||||||
import TP2.asd.Interface.Type;
|
import TP2.asd.Interface.Type;
|
||||||
|
import TP2.asd.Program.Type_intImp;
|
||||||
|
|
||||||
public class SymTable {
|
public class SymTable {
|
||||||
private Stack<PMap<String,Type>> stackMap;
|
private Stack<PMap<String,Type>> stackMap;
|
||||||
|
private int id=1;
|
||||||
public SymTable(){
|
public SymTable(){
|
||||||
this.stackMap= new Stack<>();
|
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(){
|
public void next_layer(){
|
||||||
stackMap.push(HashTreePMap.empty());
|
stackMap.push(HashTreePMap.empty());
|
||||||
}
|
}
|
||||||
@@ -29,7 +38,8 @@ public class SymTable {
|
|||||||
public void addVar(String s, Type t){
|
public void addVar(String s, Type t){
|
||||||
//Save temporary if not PMap wont save
|
//Save temporary if not PMap wont save
|
||||||
PMap<String, Type> pmap = this.peppapeek();
|
PMap<String, Type> pmap = this.peppapeek();
|
||||||
pmap= pmap.plus(s,t);
|
pmap= pmap.plus(s/*+"_"+this.id*/,t);
|
||||||
|
//this.id++;
|
||||||
//Delete old ones
|
//Delete old ones
|
||||||
stackMap.pop();
|
stackMap.pop();
|
||||||
//Push the new one
|
//Push the new one
|
||||||
@@ -38,6 +48,7 @@ public class SymTable {
|
|||||||
|
|
||||||
//Usually look for var in highest level , if not found research.
|
//Usually look for var in highest level , if not found research.
|
||||||
public boolean searchVar(String s){
|
public boolean searchVar(String s){
|
||||||
|
//TOTO
|
||||||
for(int i= stackMap.size()-1; i>=0; i--){
|
for(int i= stackMap.size()-1; i>=0; i--){
|
||||||
if(stackMap.get(i).containsKey(s)){
|
if(stackMap.get(i).containsKey(s)){
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -6,17 +6,42 @@ import TP2.asd.Interface.*;
|
|||||||
import TP2.asd.Program.*;
|
import TP2.asd.Program.*;
|
||||||
import TP2.asd.SymTable.*;
|
import TP2.asd.SymTable.*;
|
||||||
import TP2.llvm.Interface.*;
|
import TP2.llvm.Interface.*;
|
||||||
import TP2.llvm.ProgramLLVM;
|
|
||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
||||||
FunctionVisitor<SymTable,DefineLLVM>,
|
FunctionVisitor<SymTable,DefineLLVM>,
|
||||||
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
||||||
ExprVisitor<SymTable,ArrayList<AssignLVMImp>>,
|
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrOrVal>,
|
||||||
TypeVisitor<SymTable,TypeLLVM>
|
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
|
//PROGRAM
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -43,10 +68,14 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
|
||||||
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
|
InstrOrVal res = instr.e().accept(this,h);
|
||||||
InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl()/*TODO*/,list.getLast().getVar());
|
ValLLVM var = res.getVal();
|
||||||
|
|
||||||
|
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
result.addAll(list);
|
if(!res.isVal()){
|
||||||
|
result.addAll(res.instr);
|
||||||
|
}
|
||||||
result.add(r);
|
result.add(r);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -54,10 +83,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
||||||
ArrayList<AssignLVMImp> list = instr.e().accept(this,h);
|
InstrOrVal res = instr.e().accept(this,h);
|
||||||
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,instr.t()),list.getLast().getVar());
|
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<>();
|
||||||
result.addAll(list);
|
if(!res.isVal()){
|
||||||
|
result.addAll(res.instr);
|
||||||
|
}
|
||||||
result.add(r);
|
result.add(r);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -74,35 +106,45 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<AssignLVMImp> visitConst(ConstImp e, SymTable h) {
|
public InstrOrVal visitConst(ConstImp e, SymTable h) {
|
||||||
throw new UnsupportedOperationException("Can't call toLLVM on Expression Const");
|
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
|
||||||
|
return new InstrOrVal(null, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<AssignLVMImp> visitBinOp(BinopExpressionImp e, SymTable h) {
|
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
ArrayList<AssignLVMImp> list = new ArrayList<>();
|
||||||
|
|
||||||
|
InstrOrVal res1 = e.e1().accept(this, h);
|
||||||
|
InstrOrVal res2 = e.e2().accept(this, h);
|
||||||
|
|
||||||
ValLLVM val1 = null;
|
ValLLVM val1 = null;
|
||||||
ValLLVM val2 = null;
|
ValLLVM val2 = null;
|
||||||
if(e.e1() instanceof Val){
|
|
||||||
val1 = ((Val) e.e1()).getValLLVM();
|
if(res1.isVal()){
|
||||||
|
val1 = res1.val;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
ArrayList<AssignLVMImp> eLLVM1 = e.e1().accept(this,h);
|
val1 = res1.instr.getLast().var();
|
||||||
list.addAll(eLLVM1);
|
list.addAll(res1.instr);
|
||||||
val1 = eLLVM1.getLast().getVar();
|
|
||||||
}
|
}
|
||||||
if(e.e2() instanceof Val){
|
if(res2.isVal()){
|
||||||
val2 = ((Val) e.e2()).getValLLVM();
|
val2 = res2.val;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
ArrayList<AssignLVMImp> eLLVM2 = e.e2().accept(this,h);
|
val2 = res2.instr.getLast().var();
|
||||||
list.addAll(eLLVM2);
|
list.addAll(res2.instr);
|
||||||
val2 = eLLVM2.getLast().getVar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list.add(new AssignLVMImp(new VarLLVMImpl(new IntLLVMImpl()/*TODO*/,"todo"), new BinOpLLVMImp(new IntLLVMImpl()/*TODO*/,e.op(),val1,val2)));
|
|
||||||
|
|
||||||
return list;
|
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
|
@Override
|
||||||
@@ -115,4 +157,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return new VoidLLVMImpl();
|
return new VoidLLVMImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public interface Interface {
|
|||||||
//////////ExpressionLLVM (expression)
|
//////////ExpressionLLVM (expression)
|
||||||
public interface ExpressionLLVM{
|
public interface ExpressionLLVM{
|
||||||
public String prettyprinter();
|
public String prettyprinter();
|
||||||
|
public TypeLLVM getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ExpressionLLVMVisitor<H,S> {
|
public interface ExpressionLLVMVisitor<H,S> {
|
||||||
|
|||||||
@@ -96,9 +96,6 @@ public class ProgramLLVM {
|
|||||||
public String prettyprinter(){
|
public String prettyprinter(){
|
||||||
return INDENT+var.prettyprinter() + " = " + e.prettyprinter();
|
return INDENT+var.prettyprinter() + " = " + e.prettyprinter();
|
||||||
}
|
}
|
||||||
public VarLLVMImpl getVar(){
|
|
||||||
return var;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
|
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
|
||||||
@@ -136,7 +133,7 @@ public class ProgramLLVM {
|
|||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
str.append("srem "); //modulo signé
|
str.append("srem "); //modulo signé
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new AssertionError();
|
throw new AssertionError();
|
||||||
}
|
}
|
||||||
@@ -146,6 +143,11 @@ public class ProgramLLVM {
|
|||||||
public String prettyprinter(){
|
public String prettyprinter(){
|
||||||
return op_toString() + type.prettyprinter() + " " + val1.prettyprinter() + ", " + val2.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(){
|
public String prettyprinter(){
|
||||||
return "aloca" + " i" + type.getNbBit();
|
return "aloca" + " i" + type.getNbBit();
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public TypeLLVM getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -173,6 +179,10 @@ public class ProgramLLVM {
|
|||||||
public String prettyprinter(){
|
public String prettyprinter(){
|
||||||
return "load" + " i" + nbBits + ", i"+ nbBits2 + "* %" + val.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(){
|
public String prettyprinter(){
|
||||||
return val + "";
|
return val + "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeLLVM getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -189,6 +204,11 @@ public class ProgramLLVM {
|
|||||||
public String prettyprinter(){
|
public String prettyprinter(){
|
||||||
return "%"+nom;
|
return "%"+nom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeLLVM getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user