PyPy ソースコードリーディング事始め

PyPyフレームワークを使うと俺言語が作れると聞いて!

というわけで動かして遊んだりソース読んだりしてた。

RPython

なにやら RPython ってのがキーワードらしいっすよ。

http://codespeak.net/pypy/dist/pypy/doc/translation.html

  • Python ソースを RPython に食わせると Flow graph なるものができる
  • flow graph に型 annotation してあげる
  • 各種環境にコンパイルできる

って寸法らしいですぜ。

なので、ソースコード -> flow graph な何かを作れれば俺言語できるわけですよねそうですよね。

PyPy ソース

bitbucket から取ってくる。

結構量ある。

$ hg clone http://bitbucket.org/pypy/pypy

取ってきたのは

$ hg log --limit 1
チェンジセット:   45696:559198bd1ea6
ブランチ:         heap-caching-during-tracing
タグ:             tip
ユーザ:           Carl Friedrich Bolz <cfbolz@gmx.de>
日付:             Sun Jul 17 09:31:01 2011 +0200
要約:             caching for pure ops

ってことらしい。

pypy

pypy/bin/py.py にある。

Python でできた Pythonインタプリタ

実行するとなんかをコンパイルしているっぽいけど、 RPython とかあんまり関係ないっぽい感じ。

pypy/rpython 以下のソースを弄っても変化なかったのできっと関係ない、はず。

$ python pypy/bin/py.py 
[version:WARNING] Errors getting Mercurial information: command does not identify itself as Mercurial
[platform:execute] gcc -c -O3 -pthread -fomit-frame-pointer -Wall -Wno-unused /tmp/usession-unknown-18/platcheck_2.c -o /tmp/usession-unknown-18/platcheck_2.o
[platform:execute] gcc /tmp/usession-unknown-18/platcheck_2.o -pthread -lrt -lintl -o /tmp/usession-unknown-18/platcheck_2
[platform:execute] gcc -c -O3 -pthread -fomit-frame-pointer -Wall -Wno-unused /tmp/usession-unknown-18/platcheck_7.c -o /tmp/usession-unknown-18/platcheck_7.o
[platform:execute] gcc -c -O3 -pthread -fomit-frame-pointer -Wall -Wno-unused /tmp/usession-unknown-18/platcheck_8.c -o /tmp/usession-unknown-18/platcheck_8.o
faking <type 'module'>
faking <type 'member_descriptor'>
[version:WARNING] Errors getting Mercurial information: command does not identify itself as Mercurial
[version:WARNING] Errors getting Mercurial information: command does not identify itself as Mercurial
PyPy 1.6.0-dev1 in StdObjSpace on top of Python 2.6.5 (startuptime: 19.47 secs)
>>>> 

至って普通の Python Shell ですね。多分。

translatorshell.py

もう一個あるのがこっちの pypy/bin/translatorshell.py

起動するとなにやら flow graph とか出てますよ。

なので多分本命はこっち。

python pypy/bin/translatorshell.py 
[version:WARNING] Errors getting Mercurial information: command does not identify itself as Mercurial
PyPy Translator Frontend

Glue script putting together the various pieces of the translator.
Can be used for interactive testing of the translator.

Example:

    t = Translation(func)
    t.view()                           # control flow graph

    t.annotate([int])                  # pass the list of args types
    t.view()                           # graph + annotations under the mouse

    t.rtype()                          # use low level operations 
    f = t.compile_c()                  # C compilation
    assert f(arg) == func(arg)         # sanity check (for C)
    

Some functions are provided for the benefit of interactive testing.
Try dir(snippet) for list of current snippets.

>>>
試してみる

b.view() してみたらエラー出ましたよ。

pygame が必要らしい。

>>> def a(b, c): return b+c
... 
>>> b = Translation(a)
[flowgraph] (__main__:1)a
>>> 
>>> b.context
<pypy.translator.translator.TranslationContext object at 0x8e9066c>
>>> b.view()
/bin/sh: dot: not found
Unhandled exception in thread started by <function bkgndwrite at 0x8f0579c>
Traceback (most recent call last):
  File "/home/shoma/files/pypy/dotviewer/graphparse.py", line 84, in bkgndwrite
    f.write(data)
IOError: [Errno 32] Broken pipe
* posting: http://codespeak.net/pypy/convertdot.cgi
Exception in the graph viewer: ImportError
 | Pygame is not installed; either install it, or
 | if you want to debug on a remote machine, see
 | instructions in dotviewer/sshgraphserver.py


>>> 

で、 easy_isntall pygame してからやったら graphical に flow graph がでました。やったね!

Translation

で、この Translation って何よ? ってことで表示。

>>> Translation
<class 'pypy.translator.interactive.Translation'>
>>> 

pypy/translator/interactive.py を読めばいいんじゃないのってことでそっち読む。

pypy/translator/interactive.py の 32 行目あたりに

        graph = self.context.buildflowgraph(entry_point)

って記述があるからここ読めばいいんじゃないかなあ。

多分バイトコードから flow graph 作っていると思われるけどまあそれはそれで。

ぼちぼち読んでいきましょうかねー。