Debian Codegraph @

rpython/doc/getting-started.rst

raw
1 Getting Started with RPython
2 ============================
3
4 .. contents::
5
6 .. warning::
7
8 Please `read this FAQ entry`_ first!
9
10 .. _`read this FAQ entry`: http://rpython.readthedocs.org/en/latest/faq.html#do-i-have-to-rewrite-my-programs-in-rpython
11
12 RPython is a subset of Python2 that can be statically compiled. The PyPy
13 interpreter is written mostly in RPython (with pieces in Python), while
14 the RPython compiler is written in Python. The hard to understand part
15 is that Python is a meta-programming language for RPython, that is,
16 "being valid RPython" is a question that only makes sense on the
17 live objects **after** the imports are done.
18 This might require more explanation. You start writing RPython from
19 ``entry_point``, a good starting point is
20 :source:`rpython/translator/goal/targetnopstandalone.py`. This does not do all that
21 much, but is a start. Now if code analyzed (in this case ``entry_point``)
22 calls some functions, those calls will be followed. Those followed calls
23 have to be RPython themselves (and everything they call etc.), however not
24 entire module files. To show how you can use metaprogramming, we can do
25 a silly example (note that closures are not RPython)::
26
27 def generator(operation):
28 if operation == 'add':
29 def f(a, b):
30 return a + b
31 else:
32 def f(a, b):
33 return a - b
34 return f
35
36 add = generator('add')
37 sub = generator('sub')
38
39 def entry_point(argv):
40 print add(sub(int(argv[1]), 3) 4)
41 return 0
42
43 In this example ``entry_point`` is RPython, ``add`` and ``sub`` are RPython,
44 however, ``generator`` is not.
45
46 The following introductory level articles are available:
47
48 * Laurence Tratt -- `Fast Enough VMs in Fast Enough Time`_.
49
50 * `How to write interpreters in RPython`_
51
52 * `RPython By Example`_
53
54 .. _Fast Enough VMs in Fast Enough Time: http://tratt.net/laurie/tech_articles/articles/fast_enough_vms_in_fast_enough_time
55
56 .. _How to write interpreters in RPython: https://www.pypy.org/posts/2011/04/tutorial-writing-interpreter-with-pypy-3785910476193156295.html
57
58 .. _RPython By Example: https://mssun.github.io/rpython-by-example/
59
60 .. _try-out-the-translator:
61
62 Trying out the translator
63 -------------------------
64
65 The translator is a tool based on the PyPy interpreter which can translate
66 sufficiently static RPython programs into low-level code (in particular it can
67 be used to translate the `full Python interpreter`_). To be able to experiment with it
68 you need to download and install the usual (CPython2) version of:
69
70 * Pygame_
71 * `Dot Graphviz`_
72
73 .. _Pygame: http://www.pygame.org/
74 .. _Dot Graphviz: http://www.graphviz.org/
75
76 To start the interactive translator shell do::
77
78 cd rpython
79 python2 bin/translatorshell.py
80
81 Test snippets of translatable code are provided in the file
82 :source:`rpython/translator/test/snippet.py`, which is imported under the name
83 ``snippet``. For example::
84
85 >>> t = Translation(snippet.is_perfect_number, [int])
86 >>> t.view()
87
88 After that, the graph viewer pops up, that lets you interactively inspect the
89 flow graph. To move around, click on something that you want to inspect.
90 To get help about how to use it, press 'H'. To close it again, press 'Q'.
91
92
93 Trying out the type annotator
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
96 We have a type annotator that can completely infer types for functions like
97 ``is_perfect_number`` (as well as for much larger examples)::
98
99 >>> t.annotate()
100 >>> t.view()
101
102 Move the mouse over variable names (in red) to see their inferred types.
103
104
105 Translating the flow graph to C code
106 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
108 The graph can be turned into C code::
109
110 >>> t.rtype()
111 >>> lib = t.compile_c()
112
113 The first command replaces the operations with other low level versions that
114 only use low level types that are available in C (e.g. int). The compiled
115 version is now in a ``.so`` library. You can run it say using ctypes:
116
117 >>> f = get_c_function(lib, snippet.is_perfect_number)
118 >>> f(5)
119 0
120 >>> f(6)
121 1
122
123
124 A slightly larger example
125 ~~~~~~~~~~~~~~~~~~~~~~~~~
126
127 There is a small-to-medium demo showing the translator and the annotator::
128
129 python2 bin/rpython --view --annotate translator/goal/bpnn.py
130
131 This causes ``bpnn.py`` to display itself as a call graph and class
132 hierarchy. Clicking on functions shows the flow graph of the particular
133 function. Clicking on a class shows the attributes of its instances. All
134 this information (call graph, local variables' types, attributes of
135 instances) is computed by the annotator.
136
137 To turn this example to C code (compiled to the executable ``bpnn-c``),
138 type simply::
139
140 python2 bin/rpython translator/goal/bpnn.py
141
142
143 Translating Full Programs
144 ~~~~~~~~~~~~~~~~~~~~~~~~~
145
146 To translate full RPython programs, there is the script ``rpython/bin/rpython``.
147 Examples for this are a slightly changed version of Pystone::
148
149 python2 bin/rpython translator/goal/targetrpystonedalone
150
151 This will produce the executable "targetrpystonedalone-c".
152
153 The largest example of this process is to translate the `full Python
154 interpreter`_. There is also an FAQ about how to set up this process for :ref:`your
155 own interpreters <compile-own-interpreters>`.
156
157 There are several environment variables you can find useful while playing with the RPython:
158
159 ``PYPY_USESSION_DIR``
160 RPython uses temporary session directories to store files that are generated during the
161 translation process(e.g., translated C files). ``PYPY_USESSION_DIR`` serves as a base directory for these session
162 dirs. The default value for this variable is the system's temporary dir.
163
164 ``PYPY_USESSION_KEEP``
165 By default RPython keeps only the last ``PYPY_USESSION_KEEP`` (defaults to 3) session dirs inside ``PYPY_USESSION_DIR``.
166 Increase this value if you want to preserve C files longer (useful when producing lots of lldebug builds).
167
168
169 Sources
170 -------
171
172 * :source:`rpython/translator` contains the code analysis and generation stuff.
173 Start reading from translator.py, from which it should be easy to follow
174 the pieces of code involved in the various translation phases.
175
176 * :source:`rpython/annotator` contains the data model for the type annotation that
177 can be inferred about a graph. The graph "walker" that uses this is in
178 :source:`rpython/annotator/annrpython.py`.
179
180 * :source:`rpython/rtyper` contains the code of the RPython typer. The typer transforms
181 annotated flow graphs in a way that makes them very similar to C code so
182 that they can be easy translated. The graph transformations are controlled
183 by the code in :source:`rpython/rtyper/rtyper.py`. The object model that is used can
184 be found in :source:`rpython/rtyper/lltypesystem/lltype.py`. For each RPython type
185 there is a file rxxxx.py that contains the low level functions needed for
186 this type.
187
188 * :source:`rpython/rlib` contains the :doc:`RPython standard library <rlib>`, things that you can
189 use from rpython.
190
191
192 .. _full Python interpreter: http://pypy.readthedocs.org/en/latest/getting-started-python.html