This commit is contained in:
Vu Tuan Minh
2025-04-28 05:43:08 +02:00
parent 38e3f992b9
commit 85a693acbd
2 changed files with 105 additions and 39 deletions

View File

@@ -106,16 +106,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
@Override
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
SymTable prev = h;
h= h.newBlock();
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
h= h.outBlock();
prev.updateId(h);
return instrLLVM;
}
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
SymTable prevSymTable = h;
SymTable prev = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
@@ -125,7 +131,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
prevSymTable.updateId(h);
prev.updateId(h);
return instrLLVM;
}
@@ -198,7 +205,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
l.add(new LabelLLVMImp(labelThen));
h = h.newBlock();
l.addAll(instr.i1().accept(this,h));
h=h.outBlock();
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
@@ -226,16 +236,16 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
l.add(new LabelLLVMImp(labelThen));
l.addAll(instr.i1().accept(this,h));
SymTable h0 = h.newBlock();
l.addAll(instr.i1().accept(this,h0));
l.add(new BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelElse));
l.addAll(instr.i2().accept(this,h));
SymTable h1 = h.newBlock();
l.addAll(instr.i2().accept(this,h1));
l.add(new BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l;
}
@@ -260,8 +270,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
l.add(new LabelLLVMImp(labelDo));
h=h.outBlock();
l.addAll(instr.i1().accept(this,h));
h = h.outBlock();
l.add(new BrLLVMImp(labelWhile));
l.add(new LabelLLVMImp(labelDone));