print et read fini !
This commit is contained in:
@@ -9,6 +9,8 @@ import org.antlr.runtime.CommonTokenStream;
|
|||||||
import org.antlr.runtime.RecognitionException;
|
import org.antlr.runtime.RecognitionException;
|
||||||
import org.antlr.runtime.Token;
|
import org.antlr.runtime.Token;
|
||||||
|
|
||||||
|
import .antlr.VSLLexer;
|
||||||
|
import .antlr.VSLParser;
|
||||||
import TP2.asd.Program.*;
|
import TP2.asd.Program.*;
|
||||||
import TP2.llvm.ProgramLLVM;
|
import TP2.llvm.ProgramLLVM;
|
||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
@@ -21,6 +23,18 @@ import java.util.*;
|
|||||||
java -jar build/libs/TP2.jar tests/fragment0/priority2.vsl
|
java -jar build/libs/TP2.jar tests/fragment0/priority2.vsl
|
||||||
java -jar build/libs/TP2.jar tests/aLaMain.vsl
|
java -jar build/libs/TP2.jar tests/aLaMain.vsl
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* TODO :
|
||||||
|
frament 1 :
|
||||||
|
java -jar build/libs/TP2.jar tests/fragment1/print4.vsl
|
||||||
|
|
||||||
|
fragment 2 :
|
||||||
|
java -jar build/libs/TP2.jar tests/fragment2/call3expr.vsl
|
||||||
|
|
||||||
|
java -jar build/libs/TP2.jar tests/fragment2/call4if.vsl
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -101,7 +101,8 @@ public class Program{
|
|||||||
return v.visitAssign(this, h);
|
return v.visitAssign(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//t : String et Expression
|
||||||
public static record PrintImp(ArrayList<Object> t) implements Instruction{
|
public static record PrintImp(ArrayList<Object> t) implements Instruction{
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class SymTable {
|
|||||||
private PStack<PMap<String,ValueVarMap>> varMap;
|
private PStack<PMap<String,ValueVarMap>> varMap;
|
||||||
private PMap<String,ValueFunMap> functionsMap;
|
private PMap<String,ValueFunMap> functionsMap;
|
||||||
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : idLabel, [2] : idGlobalVar
|
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : idLabel, [2] : idGlobalVar
|
||||||
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal;
|
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal; //aussi partagé entre toute les symTable (.fmt pour les print et scan)
|
||||||
|
|
||||||
public SymTable(){
|
public SymTable(){
|
||||||
this.id = new int[3];
|
this.id = new int[3];
|
||||||
|
|||||||
@@ -161,10 +161,32 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
|
|
||||||
String name = h.getGlobalDeclName();
|
String name = h.getGlobalDeclName();
|
||||||
VarLLVMImp varGlobal = new VarLLVMImp(new CharLLVMImp(true),name,true);
|
VarLLVMImp varGlobal = new VarLLVMImp(new CharLLVMImp(true),name,true);
|
||||||
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(varGlobal,new CharLLVMImp(false),0); //TODO
|
|
||||||
h.addGlobalDecl(globalDecl);
|
|
||||||
|
|
||||||
params.add(varGlobal);
|
params.add(varGlobal);
|
||||||
|
String strGlobal = "";
|
||||||
|
int size = 1; //le \00 est focément à la fin et compte comme un seul char
|
||||||
|
for(int i = 0; i<instr.t().size(); i++){
|
||||||
|
Object obj = instr.t().get(i);
|
||||||
|
if(obj instanceof String){
|
||||||
|
String str = (String)obj;
|
||||||
|
size+=str.length(); //on compte avant car \0A compte pour 1 en LLVM, mais 3 pour java
|
||||||
|
str = str.replace("\n", "\\0A");
|
||||||
|
strGlobal+=str;
|
||||||
|
}
|
||||||
|
else if(obj instanceof Expression){
|
||||||
|
Expression exp = (Expression)obj;
|
||||||
|
InstrAndVal r = exp.accept(this, h);
|
||||||
|
l.addAll(r.instrs);
|
||||||
|
params.add(r.val);
|
||||||
|
strGlobal+="%d";
|
||||||
|
size+=2;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strGlobal+="\\00";
|
||||||
|
|
||||||
|
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(varGlobal,new CharLLVMImp(false),strGlobal,size); //TODO
|
||||||
|
h.addGlobalDecl(globalDecl);
|
||||||
|
|
||||||
l.add(new PrintLLVMImp(globalDecl,params));
|
l.add(new PrintLLVMImp(globalDecl,params));
|
||||||
return l;
|
return l;
|
||||||
@@ -173,22 +195,30 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
|
||||||
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
ArrayList<InstructionLLVM> l = new ArrayList<>();
|
||||||
|
ArrayList<ValLLVM> params = new ArrayList<>();
|
||||||
|
|
||||||
|
String name = h.getGlobalDeclName();
|
||||||
|
VarLLVMImp varGlobal = new VarLLVMImp(new CharLLVMImp(true),name,true);
|
||||||
|
|
||||||
|
params.add(varGlobal);
|
||||||
|
|
||||||
|
String strGlobal = "";
|
||||||
|
int size = 1; //le \00 est focément à la fin et compte comme un seul char
|
||||||
for(int i = 0; i<instr.t().size(); i++){
|
for(int i = 0; i<instr.t().size(); i++){
|
||||||
String nomVar = h.getVar(instr.t().get(i).name());
|
String nomVar = h.getVar(instr.t().get(i).name());
|
||||||
Type typeVar = h.getType(instr.t().get(i).name());
|
Type typeVar = h.getType(instr.t().get(i).name());
|
||||||
VarLLVMImp newVar = new VarLLVMImp(typeVar.accept(this,h), nomVar,false);
|
VarLLVMImp newVar = new VarLLVMImp(typeVar.accept(this,h), nomVar,false);
|
||||||
ArrayList<ValLLVM> params = new ArrayList<>();
|
strGlobal+="%d"; //2 char de long
|
||||||
|
size+=2;
|
||||||
String name = h.getGlobalDeclName();
|
|
||||||
VarLLVMImp varGlobal = new VarLLVMImp(new CharLLVMImp(true),name,true);
|
|
||||||
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(varGlobal,new CharLLVMImp(false),0);//TODO
|
|
||||||
h.addGlobalDecl(globalDecl);
|
|
||||||
|
|
||||||
params.add(varGlobal);
|
|
||||||
params.add(newVar);
|
params.add(newVar);
|
||||||
|
}
|
||||||
l.add(new ReadLLVMImp(globalDecl,params));
|
strGlobal+="\\00";
|
||||||
}
|
|
||||||
|
DeclarGlobalLLVMImp globalDecl = new DeclarGlobalLLVMImp(varGlobal,new CharLLVMImp(false),strGlobal,size);//TODO
|
||||||
|
h.addGlobalDecl(globalDecl);
|
||||||
|
|
||||||
|
l.add(new ScanLLVMImp(globalDecl,params));
|
||||||
|
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public interface Interface {
|
|||||||
public S visitAssignLLVM(AssignLLVMImp instr, H h);
|
public S visitAssignLLVM(AssignLLVMImp instr, H h);
|
||||||
public S visitStoreLLVM(StoreLLVMImp instr, H h);
|
public S visitStoreLLVM(StoreLLVMImp instr, H h);
|
||||||
public S visitPrintLLVM(PrintLLVMImp instr, H h);
|
public S visitPrintLLVM(PrintLLVMImp instr, H h);
|
||||||
public S visitReadLLVM(ReadLLVMImp instr, H h);
|
public S visitScanLLVM(ScanLLVMImp instr, H h);
|
||||||
public S visitLabelLLVM(LabelLLVMImp instr, H h);
|
public S visitLabelLLVM(LabelLLVMImp instr, H h);
|
||||||
public S visitBrLLVM(BrLLVMImp instr, H h);
|
public S visitBrLLVM(BrLLVMImp instr, H h);
|
||||||
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
|
public S visitBrCondLLVM(BrCondLLVMImp instr, H h);
|
||||||
|
|||||||
@@ -139,12 +139,12 @@ TypeLLVMVisitor<String,String>
|
|||||||
@Override
|
@Override
|
||||||
public String visitPrintLLVM(PrintLLVMImp instr, String h) { //TODO
|
public String visitPrintLLVM(PrintLLVMImp instr, String h) { //TODO
|
||||||
DefineLLVMImp printLLVM = new DefineLLVMImp("printf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
|
DefineLLVMImp printLLVM = new DefineLLVMImp("printf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
|
||||||
CallLLVMImp callPrint = new CallLLVMImp(printLLVM, new ArrayList<>(),"(i8*,...) ");
|
CallLLVMImp callPrint = new CallLLVMImp(printLLVM, instr.l(),"(i8*,...) ");
|
||||||
return callPrint.accept(this, h);
|
return callPrint.accept(this, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitReadLLVM(ReadLLVMImp instr, String h) { //TODO
|
public String visitScanLLVM(ScanLLVMImp instr, String h) { //TODO
|
||||||
DefineLLVMImp readLLVM = new DefineLLVMImp("scanf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
|
DefineLLVMImp readLLVM = new DefineLLVMImp("scanf", new IntLLVMImp(), new ArrayList<>(), new ArrayList<>());
|
||||||
CallLLVMImp callRead = new CallLLVMImp(readLLVM, instr.l(),"(i8*,...) ");
|
CallLLVMImp callRead = new CallLLVMImp(readLLVM, instr.l(),"(i8*,...) ");
|
||||||
return callRead.accept(this, h);
|
return callRead.accept(this, h);
|
||||||
@@ -153,8 +153,8 @@ TypeLLVMVisitor<String,String>
|
|||||||
@Override
|
@Override
|
||||||
public String visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, String h) {
|
public String visitDeclarGlobalLLVM(DeclarGlobalLLVMImp instr, String h) {
|
||||||
String str = "@."+instr.var().nom() + " = global";
|
String str = "@."+instr.var().nom() + " = global";
|
||||||
str += " [" + instr.type().accept(this, h)+ "x"+ instr.size()+ "] ";
|
str += " [" + instr.size() + " x "+ instr.type().accept(this, h) + "] ";
|
||||||
str+= "c\"\"\n";
|
str+= "c\""+ instr.str()+"\"\n";
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,15 +88,15 @@ public class ProgramLLVM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record ReadLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList<ValLLVM> l) implements InstructionLLVM{
|
public static record ScanLLVMImp(DeclarGlobalLLVMImp fmt,ArrayList<ValLLVM> l) 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) {
|
||||||
return v.visitReadLLVM(this, h);
|
return v.visitScanLLVM(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record DeclarGlobalLLVMImp(VarLLVMImp var,TypeLLVM type, int size) implements InstructionLLVM{
|
public static record DeclarGlobalLLVMImp(VarLLVMImp var,TypeLLVM type,String str, int size) 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) {
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ FUNC INT main(x,y) {
|
|||||||
b:=3
|
b:=3
|
||||||
affiche(b)
|
affiche(b)
|
||||||
c:=add(x,b)
|
c:=add(x,b)
|
||||||
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
|
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b, x
|
||||||
|
PRINT "test",a
|
||||||
WHILE b - 1
|
WHILE b - 1
|
||||||
DO{
|
DO{
|
||||||
b := b - 1
|
b := b - 1
|
||||||
|
|||||||
Reference in New Issue
Block a user