flxc is slightly more than useless with conditional branching
posted on September 16, 2009 - 12:33 AM PDT by Erick Tryzelaar
Just got basic conditional branching working. With this code:
>>> type int = "%i32";
>>> typedef bool = 2;
>>> fun add : int*int -> int = "%add";
>>> fun eq : int*int -> bool = "%eq";
>>> fun lnot : bool -> bool = "%lnot";
>>> proc exit : int = "exit";
>>> fun foo (x:int) = {
... if x == 1 do
... return 2;
... else
... return 3;
... done;
... }
>>> exit $ foo 1;
flxc will generate and execute this code:
declare void @exit(i32)
define i32 @foo(i32 %x) {
entry:
%x1 = alloca i32 ; <i32*> [#uses=2]
store i32 %x, i32* %x1
%0 = load i32* %x1 ; <i32> [#uses=1]
%1 = icmp eq i32 %0, 1 ; <i1> [#uses=1]
%2 = icmp eq i1 %1, false ; <i1> [#uses=1]
br i1 %2, label %_ifdoend_1, label %else
_ifdoend_1: ; preds = %entry
ret i32 3
else: ; preds = %entry
ret i32 2
}
define void @0() {
entry:
%0 = call i32 @foo(i32 1) ; <i32> [#uses=1]
call void @exit(i32 %0)
ret void
}
The program will exit with 2. It'll even return 3 if you call foo with 2. Unfortunately, branching only works inside functions.