<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xml:lang="ja">
	<channel>
		<title>LLVMメモ (with MacRuby)</title>
		<link>http://llvm.g.hatena.ne.jp/takuma104/</link>
		<description>LLVMメモ (with MacRuby)</description>
		<dc:creator>takuma104</dc:creator>


		<item>
			<title>macrubyc つかって LLVM IR (.ll) 出すまで</title>
			<link>http://llvm.g.hatena.ne.jp/takuma104/20090722/1248278866</link>

			<description><![CDATA[
		<div class="section">
<pre>
$ svn co http://svn.macosforge.org/repository/ruby/MacRuby/branches/experimental/
</pre>

			<p>とかして、READMEに従ってLLVMもろともビルド&インストール。(とりあえず、r2044付近を使用してます)</p>
			<p>macrubyc が MacRuby のコンパイラで、.oおよびa.out形式(x86_64)ができる。が、そこはまず置いておいて、macrubycコマンドを改造(もちろん、別スクリプトにするでも良いかな)して、.bcなどの中間ファイルを消さないようにしてみる。</p>
			<p>具体的には、macrubyc の</p>
<pre class="syntax-highlight">
app = <span class="synIdentifier">Compiler</span>.new(<span class="synIdentifier">ARGV</span>)
<span class="synStatement">begin</span>
  app.run
<span class="synStatement">ensure</span>
  app.cleanup
<span class="synStatement">end</span>
</pre>

			<p>の app.cleanup をコメントアウト。</p>
			<p>ほいでもって、macrubyc -c test.rb すると、 $TMPDIR に test.bc ができるので、</p>
<pre>
$ llvm-dis test.bc
</pre>

			<p>とかすると、IR(.ll)がとれる。たとえば</p>
<pre class="syntax-highlight">
p <span class="synConstant">1</span>+<span class="synConstant">2</span>
</pre>

			<p>を macrubyc して出てくるIRは、</p>
<pre>
; ModuleID = &#39;test.bc&#39;
internal global i8&#42; null		; &#60;i8&#42;&#42;&#62;:0 &#91;#uses=1]
internal global i8&#42; null		; &#60;i8&#42;&#42;&#62;:1 &#91;#uses=2]
@redefined = internal constant i1 false		; &#60;i1&#42;&#62; &#91;#uses=0]
internal global i8&#42; null		; &#60;i8&#42;&#42;&#62;:2 &#91;#uses=2]
internal global i8&#42; null		; &#60;i8&#42;&#42;&#62;:3 &#91;#uses=3]
internal constant &#91;3 x i8] c&#34;+:&#92;00&#34;		; &#60;&#91;3 x i8]&#42;&#62;:4 &#91;#uses=1]
internal constant &#91;3 x i8] c&#34;p:&#92;00&#34;		; &#60;&#91;3 x i8]&#42;&#62;:5 &#91;#uses=1]

define i64 @MREP_test(i64 %self, i8&#42; %sel) {
MainBlock:
	%0 = tail call i8&#42; @sel_registerName(i8&#42; getelementptr (&#91;3 x i8]&#42; @5, i32 0, i32 0))		; &#60;i8&#42;&#62; &#91;#uses=1]
	volatile store i8&#42; %0, i8&#42;&#42; @3
	%1 = tail call i8&#42; @sel_registerName(i8&#42; getelementptr (&#91;3 x i8]&#42; @4, i32 0, i32 0))		; &#60;i8&#42;&#62; &#91;#uses=1]
	volatile store i8&#42; %1, i8&#42;&#42; @1
	%2 = load i8&#42;&#42; @3		; &#60;i8&#42;&#62; &#91;#uses=1]
	%3 = tail call i8&#42; @rb_vm_get_method_cache(i8&#42; %2)		; &#60;i8&#42;&#62; &#91;#uses=1]
	volatile store i8&#42; %3, i8&#42;&#42; @2
	%4 = load i8&#42;&#42; @1		; &#60;i8&#42;&#62; &#91;#uses=1]
	%5 = tail call i8&#42; @rb_vm_get_method_cache(i8&#42; %4)		; &#60;i8&#42;&#62; &#91;#uses=1]
	volatile store i8&#42; %5, i8&#42;&#42; @0
	%6 = load i8&#42;&#42; @2		; &#60;i8&#42;&#62; &#91;#uses=1]
	%7 = load i8&#42;&#42; @3		; &#60;i8&#42;&#62; &#91;#uses=1]
	%8 = tail call i64 (i8&#42;, i64, i8&#42;, i8&#42;, i8, i32, ...)&#42; @rb_vm_dispatch(i8&#42; %6, i64 %self, i8&#42; %7, i8&#42; null, i8 0, i32 1, i64 13)		; &#60;i64&#62; &#91;#uses=1]
	ret i64 %8
}

declare i64 @rb_vm_dispatch(i8&#42;, i64, i8&#42;, i8&#42;, i8, i32, ...)

declare i8&#42; @rb_vm_get_method_cache(i8&#42;)

declare i8&#42; @rb_vm_get_constant_cache(i8&#42;)

declare i8&#42; @sel_registerName(i8&#42;)

declare i8&#42; @rb_global_entry(i64)

declare i64 @rb_intern(i8&#42;)

declare i64 @objc_getClass(i8&#42;)
</pre>

			<p>なかんじ。がんばって読むと、</p>
			<ul>
				<li> test.rb ファイルをコンパイルすると、最上位階層が MREP_test という名前の関数(Block?)になる</li>
				<li> sel_registerName, rb_vm_get_method_cache, rb_vm_dispatch の 3つの関数が呼び出されてる</li>
				<li> rb_vm_dispatch がおそらく p: セレクタで p を評価しようとしているコードっぽく見えるが、その引数の即値が13 (Ruby処理系のVALUEでいうところの、Fixnumで3) と入っている</li>
			</ul>
			<p>とかが分かる。</p>
			<p>さらに、これは</p>
<pre class="syntax-highlight">
a = <span class="synConstant">1</span>
b = a
b += <span class="synConstant">2</span>
p b
</pre>

			<p>をコンパイルしても同じ。どこのオプティマイザがこんなに利口なのか(ソースぜんぜん読んでないので)不明ですが、かなり期待できる予感。</p>
		</div>
]]></description>

			<dc:creator>takuma104</dc:creator>

			<pubDate>Wed, 22 Jul 2009 16:07:46 GMT</pubDate>



		</item>

		<item>
			<title>はじめまして</title>
			<link>http://llvm.g.hatena.ne.jp/takuma104/20090722/1248278281</link>

			<description><![CDATA[
		<div class="section">
			<p>ここは MacRuby 0.5 (LLVM backend) の解析の軌跡になるはずです。LLVM初心者。</p>
		</div>
]]></description>

			<dc:creator>takuma104</dc:creator>

			<pubDate>Wed, 22 Jul 2009 15:58:01 GMT</pubDate>



		</item>

	</channel>
</rss>
