Reputation: 5
I have followed the LLVM tutorial Kaleidoscope. Now I'm trying to extern this front-end compiler for language similar to Pascal. I have this code:
program main;
function rett(): integer;
begin
rett := 1;
exit;
rett := 5;
end;
var a: integer;
begin
a := rett();
writeln(a);
end.
An output of this program should be a single integer '1', because of the exit command (so the line rett := 5;
should'n be execute). However the output is '5'. For this input I have generate this LLVM IR:
define i32 @rett() {
entry:
%rett = alloca i32
store i32 0, i32* %rett
store i32 1, i32* %rett
%rett1 = load i32, i32* %rett
ret i32 %rett1
store i32 5, i32* %rett
%rett2 = load i32, i32* %rett
ret i32 %rett2
}
define i32 @main() {
entry:
%a = alloca i32
%main = alloca i32
store i32 0, i32* %main
store i32 0, i32* %a
%calltmp = call i32 @rett()
store i32 %calltmp, i32* %a
%a1 = load i32, i32* %a
%calltmp2 = call i32 (i8*, ...) @printf([4 x i8]* @.str, i32 %a1)
%main3 = load i32, i32* %main
ret i32 %main3
}
Why is not execute the first 'ret' command? Is there something what am I missing?
Update: In documentations is written:
Each basic block may optionally start with a label (giving the basic block a symbol table entry), contains a list of instructions, and ends with a terminator instruction (such as a branch or function return).
Is that mean that a "terminator" instruction can by only at the end of basic block? And if yes how should I implement return and break command for my language?
Upvotes: 0
Views: 2043
Reputation: 4088
I could not reproduce the problem (your example outputs 1
), but I will answer the questions anyway.
Is that mean that a "terminator" instruction can by only at the end of basic block?
That's correct. In fact, opt
shows that @rett
function in your example consists of these two basic blocks:
And if yes how should I implement return and break command for my language?
With ret
and br
.
You don't ever need to generate a terminator amidst a basic block, because, as you have noted, any instructions past it simply won't execute, so you might as well not generate them.
Upvotes: 0