clean toLLVM
This commit is contained in:
@@ -38,7 +38,7 @@ PRINT : 'PRINT'
|
|||||||
;
|
;
|
||||||
RETURN: 'RETURN'
|
RETURN: 'RETURN'
|
||||||
;
|
;
|
||||||
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1)}
|
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1);}
|
||||||
;
|
;
|
||||||
NUMBER : (DIGIT)+
|
NUMBER : (DIGIT)+
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class Main {
|
|||||||
// 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 LLVM : \n--------------\n");
|
||||||
System.out.println(astLLVM.prettyprinter());
|
System.out.println(astLLVM.prettyprinter());
|
||||||
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
|
||||||
|
|||||||
@@ -27,18 +27,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
public InstrOrVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
|
public InstrOrVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
|
||||||
this.instr = instr;
|
this.instr = instr;
|
||||||
this.val = val;
|
this.val = instr.getLast().var();
|
||||||
}
|
|
||||||
public Boolean isVal(){
|
|
||||||
return instr==null;
|
|
||||||
}
|
|
||||||
public ValLLVM getVal(){
|
|
||||||
if(instr==null){
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return instr.getLast().var();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,13 +58,11 @@ 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) {
|
||||||
InstrOrVal res = instr.e().accept(this,h);
|
InstrOrVal res = instr.e().accept(this,h);
|
||||||
ValLLVM var = res.getVal();
|
ValLLVM var = res.val;
|
||||||
|
|
||||||
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
|
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
if(!res.isVal()){
|
|
||||||
result.addAll(res.instr);
|
result.addAll(res.instr);
|
||||||
}
|
|
||||||
result.add(r);
|
result.add(r);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -84,11 +71,9 @@ 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) {
|
||||||
InstrOrVal res = instr.e().accept(this,h);
|
InstrOrVal res = instr.e().accept(this,h);
|
||||||
ValLLVM var = res.getVal();
|
ValLLVM var = res.val;
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
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 AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
||||||
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/));
|
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/));
|
||||||
result.add(r);
|
result.add(r);
|
||||||
@@ -109,13 +94,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
@Override
|
@Override
|
||||||
public InstrOrVal visitConst(ConstImp e, SymTable h) {
|
public InstrOrVal visitConst(ConstImp e, SymTable h) {
|
||||||
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
|
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
|
||||||
return new InstrOrVal(null, val);
|
return new InstrOrVal(new ArrayList<>(), val);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrOrVal visitVar(VarImp e, SymTable h) {
|
public InstrOrVal visitVar(VarImp e, SymTable h) {
|
||||||
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
|
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
|
||||||
return new InstrOrVal(null, val);
|
return new InstrOrVal(new ArrayList<>(), val);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -125,23 +110,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
InstrOrVal res1 = e.e1().accept(this, h);
|
InstrOrVal res1 = e.e1().accept(this, h);
|
||||||
InstrOrVal res2 = e.e2().accept(this, h);
|
InstrOrVal res2 = e.e2().accept(this, h);
|
||||||
|
|
||||||
ValLLVM val1 = null;
|
ValLLVM val1 = res1.val;
|
||||||
ValLLVM val2 = null;
|
ValLLVM val2 = res2.val;
|
||||||
|
|
||||||
if(res1.isVal()){
|
|
||||||
val1 = res1.val;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
val1 = res1.instr.getLast().var();
|
|
||||||
list.addAll(res1.instr);
|
list.addAll(res1.instr);
|
||||||
}
|
|
||||||
if(res2.isVal()){
|
|
||||||
val2 = res2.val;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
val2 = res2.instr.getLast().var();
|
|
||||||
list.addAll(res2.instr);
|
list.addAll(res2.instr);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TypeLLVM type = val1.getType();
|
TypeLLVM type = val1.getType();
|
||||||
@@ -149,9 +122,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
throw new UnsupportedOperationException("Type error in VSL file");
|
throw new UnsupportedOperationException("Type error in VSL file");
|
||||||
}
|
}
|
||||||
String temp = h.addNewTempVar();
|
String temp = h.addNewTempVar();
|
||||||
list.add(new AssignLVMImp(new VarLLVMImpl(type,temp), new BinOpLLVMImp(type,e.op(),val1,val2)));
|
VarLLVMImpl var = new VarLLVMImpl(type,temp);
|
||||||
|
list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2)));
|
||||||
|
|
||||||
return new InstrOrVal(list, null);
|
return new InstrOrVal(list, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user