| 1 |
Goals and Architecture Overview |
| 2 |
=============================== |
| 3 |
|
| 4 |
High Level Goals |
| 5 |
---------------- |
| 6 |
|
| 7 |
Traditionally, language interpreters are written in a target platform language |
| 8 |
such as C/Posix, Java or C#. Each implementation provides |
| 9 |
a fundamental mapping between application source code and the target |
| 10 |
environment. One of |
| 11 |
the goals of the "all-encompassing" environments, such as the .NET framework |
| 12 |
and to some extent the Java virtual machine, is to provide standardized |
| 13 |
and higher level functionalities in order to support language implementers |
| 14 |
for writing language implementations. |
| 15 |
|
| 16 |
PyPy is experimenting with a more ambitious approach. We are using a |
| 17 |
subset of the high-level language Python, called :doc:`rpython`, in which we |
| 18 |
write languages as simple interpreters with few references to and |
| 19 |
dependencies on lower level details. The RPython toolchain |
| 20 |
produces a concrete virtual machine for the platform of our choice by |
| 21 |
inserting appropriate lower level aspects. The result can be customized |
| 22 |
by selecting other feature and platform configurations. |
| 23 |
|
| 24 |
Our goal is to provide a possible solution to the problem of language |
| 25 |
implementers: having to write ``l * o * p`` interpreters for ``l`` |
| 26 |
dynamic languages and ``p`` platforms with ``o`` crucial design |
| 27 |
decisions. PyPy aims at making it possible to change each of these |
| 28 |
variables independently such that: |
| 29 |
|
| 30 |
* ``l``: the language that we analyze can be evolved or entirely replaced; |
| 31 |
|
| 32 |
* ``o``: we can tweak and optimize the translation process to produce |
| 33 |
platform specific code based on different models and trade-offs; |
| 34 |
|
| 35 |
* ``p``: we can write new translator back-ends to target different |
| 36 |
physical and virtual platforms. |
| 37 |
|
| 38 |
By contrast, a standardized target environment - say .NET - |
| 39 |
enforces ``p=1`` as far as it's concerned. This helps making ``o`` a |
| 40 |
bit smaller by providing a higher-level base to build upon. Still, |
| 41 |
we believe that enforcing the use of one common environment |
| 42 |
is not necessary. PyPy's goal is to give weight to this claim - at least |
| 43 |
as far as language implementation is concerned - showing an approach |
| 44 |
to the ``l * o * p`` problem that does not rely on standardization. |
| 45 |
|
| 46 |
The most ambitious part of this goal is to :doc:`generate Just-In-Time |
| 47 |
Compilers <jit/index>` in a language-independent way, instead of only translating |
| 48 |
the source interpreter into an interpreter for the target platform. |
| 49 |
This is an area of language implementation that is commonly considered |
| 50 |
very challenging because of the involved complexity. |
| 51 |
|
| 52 |
|
| 53 |
Architecture |
| 54 |
------------ |
| 55 |
|
| 56 |
The job of the RPython toolchain is to translate :doc:`rpython` programs |
| 57 |
into an efficient version of that program for one of the various target |
| 58 |
platforms, generally one that is considerably lower-level than Python. |
| 59 |
|
| 60 |
The approach we have taken is to reduce the level of abstraction of the |
| 61 |
source RPython program in several steps, from the high level down to the |
| 62 |
level of the target platform, whatever that may be. Currently we |
| 63 |
support two broad flavours of target platforms: the ones that assume a |
| 64 |
C-like memory model with structures and pointers, and the ones that |
| 65 |
assume an object-oriented model with classes, instances and methods (as, |
| 66 |
for example, the Java and .NET virtual machines do). |
| 67 |
|
| 68 |
The RPython toolchain never sees the RPython source code or syntax |
| 69 |
trees, but rather starts with the *code objects* that define the |
| 70 |
behaviour of the function objects one gives it as input. It can be |
| 71 |
considered as "freezing" a pre-imported RPython program into an |
| 72 |
executable form suitable for the target platform. |
| 73 |
|
| 74 |
The steps of the translation process can be summarized as follows: |
| 75 |
|
| 76 |
* The code object of each source functions is converted to a :ref:`control |
| 77 |
flow graph <flow-model>` by the :ref:`flow graph builder<flow-graphs>`. |
| 78 |
|
| 79 |
* The control flow graphs are processed by the :ref:`Annotator <annotator>`, which |
| 80 |
performs whole-program type inference to annotate each variable of |
| 81 |
the control flow graph with the types it may take at run-time. |
| 82 |
|
| 83 |
* The information provided by the annotator is used by the :doc:`RTyper <rtyper>` to |
| 84 |
convert the high level operations of the control flow graphs into |
| 85 |
operations closer to the abstraction level of the target platform. |
| 86 |
|
| 87 |
* Optionally, `various transformations <optional-transformations>` can then be applied which, for |
| 88 |
example, perform optimizations such as inlining, add capabilities |
| 89 |
such as stackless-style concurrency, or insert code for the |
| 90 |
:doc:`garbage collector <garbage_collection>`. |
| 91 |
|
| 92 |
* Then, the graphs are converted to source code for the target platform |
| 93 |
and compiled into an executable. |
| 94 |
|
| 95 |
This process is described in much more detail in the :doc:`document about |
| 96 |
the RPython toolchain <translation>` and in the paper `Compiling dynamic language |
| 97 |
implementations`_. |
| 98 |
|
| 99 |
.. _Compiling dynamic language implementations: https://foss.heptapod.net/pypy/extradoc/-/tree/branch/extradoc/eu-report/D05.1_Publish_on_translating_a_very-high-level_description.pdf |
| 100 |
|
| 101 |
Further reading |
| 102 |
--------------- |
| 103 |
|
| 104 |
* :doc:`getting-started`: a hands-on guide to getting involved with the |
| 105 |
PyPy source code. |
| 106 |
|
| 107 |
* `PyPy's approach to virtual machine construction`_: a paper |
| 108 |
presented to the Dynamic Languages Symposium attached to OOPSLA |
| 109 |
2006. |
| 110 |
|
| 111 |
* :doc:`The translation document <translation>`: a detailed description of our |
| 112 |
translation process. |
| 113 |
|
| 114 |
* :doc:`JIT Generation in PyPy <jit/index>`, describing how we produce a Just-in-time |
| 115 |
Compiler from an interpreter. |
| 116 |
|
| 117 |
* A tutorial of how to use the :doc:`RPython toolchain <translation>` to `implement your own |
| 118 |
interpreter`_. |
| 119 |
|
| 120 |
.. _PyPy's approach to virtual machine construction: https://foss.heptapod.net/pypy/extradoc/-/tree/branch/extradoc/talk/dls2006/pypy-vm-construction.pdf |
| 121 |
.. _implement your own interpreter: https://www.pypy.org/posts/2011/04/tutorial-writing-interpreter-with-pypy-3785910476193156295.html |