Created
January 3, 2010 19:27
-
-
Save nanki/268094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@str = internal constant [4 x i8] c"%d\0A\00" | |
define void @main() nounwind { | |
%fact = call i32 @fact.tr(i32 5) | |
call i32 @printf( i8* getelementptr ([4 x i8]* @str, i32 0,i32 0), i32 %fact) | |
ret void | |
} | |
; tail recurse | |
define i32 @fact.tr(i32 %n) nounwind { | |
%fact = call i32 @fact0(i32 %n, i32 1) | |
ret i32 %fact | |
} | |
define i32 @fact0(i32 %n, i32 %f) nounwind { | |
%cond = icmp ule i32 %n, 1 | |
br i1 %cond, label %c1, label %c2 | |
c1: | |
ret i32 %f | |
c2: | |
%f1 = mul i32 %n, %f | |
%n1 = sub i32 %n, 1 | |
%r = call i32 @fact0(i32 %n1, i32 %f1) | |
ret i32 %r | |
} | |
; non-tail-recurse | |
define i32 @fact.ntr(i32 %n) nounwind { | |
%cond = icmp ule i32 %n, 1 | |
br i1 %cond, label %c1, label %c2 | |
c1: | |
ret i32 1 | |
c2: | |
%n1 = sub i32 %n, 1 | |
%fact1 = call i32 @fact.ntr(i32 %n1) | |
%r = mul i32 %fact1, %n | |
ret i32 %r | |
} | |
declare i32 @printf(i8*, ...) nounwind |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment