-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Mypyc Intermediate Representation (IR)
The mypyc IR is defined in mypyc.ir
. It covers several key concepts
that are essential to understand by all mypyc contributors:
-
mypyc.ir.ops.Op
is an Abstract Base Class for all IR operations. These are low-level and generally map to simple fragments of C each. Mypy expressions are translated to linear sequences of these ops. -
mypyc.ir.ops.BasicBlock
is a container of a sequence of ops with a branch/goto/return at the end, and no branch/goto/return ops in the middle. Each function is compiled to a bunch of basic blocks. -
mypyc.ir.rtypes.RType
and its subclasses are the types used for everything in the IR. These are lower-level and simpler than mypy or PEP 484 types. For example, there are no general-purpose generic types types here. EachList[X]
type (for anyX
) is represented by a singlelist
type, for example. -
Primitive types are special RTypes of which mypyc has some special understanding, and there are typically some specialized ops. Examples include
int
(referred to asint_rprimitive
in the code) andlist
(list_rprimitive
). Python types for which there is no specific RType type will be represented by the catch-allobject_rprimitive
type. -
Instances of compiled classes are generally represented using the
RInstance
type. Classes are compiled to C extension classes and contain vtables for fast method calls and fast attribute access. -
IR representations of functions and classes live in
mypyc.ir.func_ir
andmypyc.ir.class_ir
, respectively.
Look at the docstrings and comments in mypyc.ir
for additional
information. See the test cases in
mypyc/test-data/irbuild-basic.test
for examples of what the IR looks
like in a pretty-printed form.