Debian Codegraph @

pypy/doc/architecture.rst

raw
1 Goals and Architecture Overview
2 ===============================
3
4 .. _contents:
5
6 This document gives an overview of the goals and architecture of PyPy. If you're
7 interested in :ref:`using PyPy <using-pypy>` or hacking on it,
8 have a look at our :ref:`getting started <getting-started-index>` section.
9
10
11 Mission statement
12 -----------------
13
14 We aim to provide a compliant, flexible and fast implementation of the Python_
15 Language which uses the RPython toolchain to enable new advanced high-level
16 features without having to encode the low-level details. We call this PyPy.
17
18 .. _Python: https://docs.python.org/3/reference/
19
20
21 High Level Goals
22 ----------------
23
24 Our main motivation for developing the translation framework is to
25 provide a full featured, customizable, :ref:`fast <how-fast-is-pypy>` and
26 :doc:`very compliant <cpython_differences>` Python
27 implementation, working on and interacting with a large variety of
28 platforms and allowing the quick introduction of new advanced language
29 features.
30
31 This Python implementation is written in RPython as a relatively simple
32 interpreter, in some respects easier to understand than CPython, the C
33 reference implementation of Python. We are using its high level and
34 flexibility to quickly experiment with features or implementation
35 techniques in ways that would, in a traditional approach, require
36 pervasive changes to the source code. For example, PyPy's Python
37 interpreter can optionally provide lazily computed objects - a small
38 extension that would require global changes in CPython. Another example
39 is the garbage collection technique: changing CPython to use a garbage
40 collector not based on reference counting would be a major undertaking,
41 whereas in PyPy it is an issue localized in the translation framework,
42 and fully orthogonal to the interpreter source code.
43
44
45 .. _python-interpreter:
46
47 PyPy Python Interpreter
48 -----------------------
49
50 PyPy's *Python Interpreter* is written in RPython and implements the
51 full Python language. This interpreter very closely emulates the
52 behavior of CPython. It contains the following key components:
53
54 - a bytecode compiler responsible for producing Python code objects
55 from the source code of a user application;
56
57 - a :doc:`bytecode evaluator <interpreter>` responsible for interpreting
58 Python code objects;
59
60 - a :ref:`standard object space <standard-object-space>`, responsible for creating and manipulating
61 the Python objects seen by the application.
62
63 The *bytecode compiler* is the preprocessing phase that produces a
64 compact bytecode format via a chain of flexible passes (tokenizer,
65 lexer, parser, abstract syntax tree builder, bytecode generator). The
66 *bytecode evaluator* interprets this bytecode. It does most of its work
67 by delegating all actual manipulations of user objects to the *object
68 space*. The latter can be thought of as the library of built-in types.
69 It defines the implementation of the user objects, like integers and
70 lists, as well as the operations between them, like addition or
71 truth-value-testing.
72
73 This division between bytecode evaluator and object space gives a lot of
74 flexibility. One can plug in different :doc:`object spaces <objspace>` to get
75 different or enriched behaviours of the Python objects.
76
77 Layers
78 ------
79
80 RPython
81 ~~~~~~~
82 :ref:`RPython <rpython:language>` is the language in which we write interpreters.
83 Not the entire PyPy project is written in RPython, only the parts that are
84 compiled in the translation process. The interesting point is that RPython
85 has no parser, it's compiled from the live python objects, which makes it
86 possible to do all kinds of metaprogramming during import time. In short,
87 Python is a meta programming language for RPython.
88
89 The RPython standard library is to be found in the ``rlib`` subdirectory.
90
91 Consult `Getting Started with RPython`_ for further reading or `RPython By
92 Example`_ for another take on what can be done using RPython without writing an
93 interpreter over it.
94
95 Translation
96 ~~~~~~~~~~~
97 The translation toolchain - this is the part that takes care of translating
98 RPython to flow graphs and then to C. There is more in the
99 :doc:`architecture <architecture>` document written about it.
100
101 It lives in the ``rpython`` directory: ``flowspace``, ``annotator``
102 and ``rtyper``.
103
104 PyPy Interpreter
105 ~~~~~~~~~~~~~~~~
106 This is in the ``pypy`` directory. ``pypy/interpreter`` is a standard
107 interpreter for Python written in RPython. The fact that it is
108 RPython is not apparent at first. Built-in modules are written in
109 ``pypy/module/*``. Some modules that CPython implements in C are
110 simply written in pure Python; they are in the top-level ``lib_pypy``
111 directory. The standard library of Python (with a few changes to
112 accommodate PyPy) is in ``lib-python``.
113
114 JIT Compiler
115 ~~~~~~~~~~~~
116 :ref:`Just-in-Time Compiler (JIT) <rpython:jit>`: we have a tracing JIT that traces the
117 interpreter written in RPython, rather than the user program that it
118 interprets. As a result it applies to any interpreter, i.e. any
119 language. But getting it to work correctly is not trivial: it
120 requires a small number of precise "hints" and possibly some small
121 refactorings of the interpreter. The JIT itself also has several
122 almost-independent parts: the tracer itself in ``rpython/jit/metainterp``, the
123 optimizer in ``rpython/jit/metainterp/optimizer`` that optimizes a list of
124 residual operations, and the backend in ``rpython/jit/backend/<machine-name>``
125 that turns it into machine code. Writing a new backend is a
126 traditional way to get into the project.
127
128 Garbage Collectors
129 ~~~~~~~~~~~~~~~~~~
130 Garbage Collectors (GC): as you may notice if you are used to CPython's
131 C code, there are no ``Py_INCREF/Py_DECREF`` equivalents in RPython code.
132 :ref:`rpython:garbage-collection` is inserted
133 during translation. Moreover, this is not reference counting; it is a real
134 GC written as more RPython code. The best one we have so far is in
135 ``rpython/memory/gc/incminimark.py``.
136
137 .. _`Getting Started with RPython`: https://rpython.readthedocs.io/en/latest/getting-started.html
138 .. _RPython By Example: https://mssun.github.io/rpython-by-example/index.html
139