add ret void pour func void

This commit is contained in:
Vu Tuan Minh
2025-04-30 09:38:30 +02:00
parent 0e82527b4b
commit 20f6170168
11 changed files with 31 additions and 40 deletions

View File

@@ -218,6 +218,10 @@ td_expression returns [Expression out]:
; ;
lit returns [Expression out]: lit returns [Expression out]:
MINUS n=lit {
$out = new BinopExpressionImp(Op.MINUS, new ConstImp(0), $n.out);
}
|
NUMBER { NUMBER {
$out = new ConstImp($NUMBER.int); $out = new ConstImp($NUMBER.int);
} }

View File

@@ -78,8 +78,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
if(!(instrLLVM.getLast() instanceof ReturnLLVMImp || instrLLVM.getLast() instanceof BrLLVMImp)){ if(!(instrLLVM.getLast() instanceof ReturnLLVMImp || instrLLVM.getLast() instanceof BrLLVMImp)){
instrLLVM.add(new ReturnLLVMImp(type, new ValLLVMImp(type,0))); instrLLVM.add(new ReturnLLVMImp(type, new ValLLVMImp(type,0)));
} }
}else { }else if(type instanceof VoidLLVMImp){
//TODO instrLLVM.add(new ReturnLLVMImp(type,null));
} }
DefineLLVMImp define = new DefineLLVMImp(fun.nom(), type, paramsLLVM, instrLLVM); DefineLLVMImp define = new DefineLLVMImp(fun.nom(), type, paramsLLVM, instrLLVM);
return define; return define;
@@ -138,14 +138,18 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
@Override @Override
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>(); ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instrs); //ret void
result.add(r); if (instr.e() == null) {
result.add(new ReturnLLVMImp(new VoidLLVMImp(), null));
}else{
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
result.addAll(res.instrs);
result.add(r);
}
return result; return result;
} }

View File

@@ -62,7 +62,11 @@ TypeLLVMVisitor<String,String>
@Override @Override
public String visitReturnLLVM(ReturnLLVMImp instr, String h) { public String visitReturnLLVM(ReturnLLVMImp instr, String h) {
return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h); if (instr.e() == null) {
return INDENT + "ret " + instr.type().accept(this, h);
} else {
return INDENT + "ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h);
}
} }
@Override @Override
@@ -128,31 +132,32 @@ TypeLLVMVisitor<String,String>
@Override @Override
public String visitCallLLVM(CallLLVMImp instr, String h) { public String visitCallLLVM(CallLLVMImp instr, String h) {
String str = "call " + instr.str() + instr.f().type().accept(this, h) + " @"+instr.f().name() + "("; String str = "call " + instr.f().type().accept(this, h) + " (" + instr.str() + ") @" + instr.f().name() + "(";
for(int i = 0; i<instr.params().size(); i++){
str += instr.params().get(i).getType().accept(this, h) + " " + instr.params().get(i).accept(this,h); for (int i = 0; i < instr.params().size(); i++) {
if(i<instr.params().size()-1) str += ", "; str += instr.params().get(i).getType().accept(this, h) + " " + instr.params().get(i).accept(this, h);
if (i < instr.params().size() - 1) str += ", ";
} }
return str + ")"; return str + ")";
} }
@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, instr.l(),"(i8*,...) "); CallLLVMImp callPrint = new CallLLVMImp(printLLVM, instr.l(),"i8*, ...");
return callPrint.accept(this, h); return callPrint.accept(this, h);
} }
@Override @Override
public String visitScanLLVM(ScanLLVMImp 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);
} }
@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.size() + " x "+ instr.type().accept(this, h) + "] "; str += " [" + instr.size() + " x "+ instr.type().accept(this, h) + "] ";
str+= "c\""+ instr.str()+"\"\n"; str+= "c\""+ instr.str()+"\"\n";

View File

@@ -1,23 +1 @@
FUNC INT test1(a,b){ FUNC VOID main() PRINT 1
a := a+b
}
FUNC INT test2(a,b){
IF a
THEN
RETURN a
FI
}
FUNC INT test3(a,b){
IF a
THEN
RETURN a
ELSE
RETURN b
FI
}
FUNC INT main(x,y) {
x := test1(x,y)
}

BIN
tests/fragment1/hello_world Executable file

Binary file not shown.

BIN
tests/fragment1/print1 Executable file

Binary file not shown.

BIN
tests/fragment1/print2 Executable file

Binary file not shown.

BIN
tests/fragment1/print3 Executable file

Binary file not shown.

BIN
tests/fragment1/print4 Executable file

Binary file not shown.

BIN
tests/fragment1/sequence Executable file

Binary file not shown.

BIN
tests/fragment2/call Executable file

Binary file not shown.