今日のLLVM - フィボナッチ数列

投稿者 nanki 2009-12-25 10:35:00 GMT

LLVM言語にて、フィボナッチ数列。

@str = internal constant [4 x i8] c"%d\0A\00"

define void @main() nounwind {
init:
  br label %loop
loop:
  %i = phi i32 [0, %init], [%i.next, %loop]
  %fib = call i32 @fib(i32 %i)

  call i32 (i8*, ...) @printf( i8* getelementptr ([4 x i8]* @str, i32 0,i32 0), i32 %fib)

  %i.next = add i32 %i, 1

  %cond = icmp ult i32 %i.next, 30
  br i1 %cond, label %loop, label %exit

exit:
  ret void
}

define i32 @fib(i32 %n) nounwind {
  %cond = icmp ult i32 %n, 2
  br i1 %cond, label %c1, label %c2

c1:
  ret i32 1
c2:
  %n1 = sub i32 %n, 1
  %n2 = sub i32 %n, 2

  %fib1 = call i32 @fib(i32 %n1)
  %fib2 = call i32 @fib(i32 %n2)

  %r = add i32 %fib1, %fib2
  ret i32 %r
}

declare i32 @printf(i8*, ...) nounwind

This entry was posted on 2009-12-25 10:35:00 GMT and カテゴリ . You can follow any response to this entry through the Atom feed. or a trackback from your own site.

タグ


トラックバック

トラックバックリンク:
http://blog.netswitch.jp/trackbacks?article_id=8655

コメント

  1. Azia 2011-05-02 16:38:07 GMT:

    That’s ralely shrewd! Good to see the logic set out so well.