1 Concepts
2 ========
3
4 This document describes the underlying concepts employed in micropython.
5
6 * Namespaces and attribute definition
7 * Contexts and values
8 * Tables, attributes and lookups
9 * Objects and structures
10 * Parameters and lookups
11 * Instantiation
12
13 Namespaces and Attribute Definition
14 ===================================
15
16 Namespaces are any objects which can retain attributes.
17
18 * Module attributes are defined either at the module level or by global
19 statements.
20 * Class attributes are defined only within class statements.
21 * Instance attributes are defined only by assignments to attributes of self
22 within __init__ methods.
23
24 These restrictions apply because such attributes are thus explicitly declared,
25 permitting the use of tables (described below). Module and class attributes
26 can also be finalised in this way in order to permit certain optimisations.
27
28 See rejected.txt for complicating mechanisms which could be applied to
29 mitigate the effects of these restrictions on optimisations.
30
31 Contexts and Values
32 ===================
33
34 Values are used as the common reference representation in micropython: as
35 stored representations of attributes (of classes, instances, modules, and
36 other objects supporting attribute-like entities) as well as the stored values
37 associated with names in functions and methods.
38
39 Unlike other implementations, micropython does not create things like bound
40 method objects for individual instances. Instead, all objects are referenced
41 using a context, reference pair:
42
43 Value Layout
44 ------------
45
46 0 1
47 context object
48 reference reference
49
50 Specific implementations might reverse this ordering for optimisation
51 purposes.
52
53 Rationale
54 ---------
55
56 To reduce the number of created objects whilst retaining the ability to
57 support bound method invocations. The context indicates the context in which
58 an invocation is performed, typically the owner of the method.
59
60 Usage
61 -----
62
63 The context may be inserted as the first argument when a value is involved in
64 an invocation. This argument may then be omitted from the invocation if its
65 usage is not appropriate.
66
67 See invocation.txt for details.
68
69 Contexts in Acquired Values
70 ---------------------------
71
72 There are two classes of instructions which provide values:
73
74 Instruction Purpose Context Operations
75 ----------- ------- ------------------
76
77 LoadConst Load class, function, Combine null context with
78 module, constant loaded object
79
80 LoadAddress* Load attribute from Preserve or override stored
81 LoadAttr* class, module, context (as described in
82 instance assignment.txt)
83
84 In order to comply with traditional Python behaviour, contexts may or may not
85 represent the object from which an attribute has been acquired.
86
87 See assignment.txt for details.
88
89 Contexts in Stored Values
90 -------------------------
91
92 There is only one class of instruction for storing values:
93
94 Instruction Purpose Context Operations
95 ----------- ------- ------------------
96
97 StoreAddress Store attribute in a Preserve context; note that no
98 known object test for class attribute
99 assignment should be necessary
100 since this instruction should only
101 be generated for module globals
102
103 StoreAttr Store attribute in an Preserve context; note that no
104 instance test for class attribute
105 assignment should be necessary
106 since this instruction should only
107 be generated for self accesses
108
109 StoreAttrIndex Store attribute in an Preserve context; since the index
110 unknown object lookup could yield a class
111 attribute, a test of the nature of
112 the nature of the structure is
113 necessary in order to prevent
114 assignments to classes
115
116 Note that contexts are never changed in the stored value: they are preserved.
117
118 See assignment.txt for details.
119
120 Tables, Attributes and Lookups
121 ==============================
122
123 Attribute lookups, where the exact location of an object attribute is deduced,
124 are performed differently in micropython than in other implementations.
125 Instead of providing attribute dictionaries, in which attributes are found,
126 attributes are located at fixed places in object structures (described below)
127 and their locations are stored using a special representation known as a
128 table.
129
130 For a given program, a table can be considered as being like a matrix mapping
131 classes to attribute names. For example:
132
133 class A:
134 # instances have attributes x, y
135
136 class B(A):
137 # introduces attribute z for instances
138
139 class C:
140 # instances have attributes a, b, z
141
142 This would provide the following table, referred to as an object table in the
143 context of classes and instances:
144
145 Class/attr a b x y z
146
147 A 1 2
148 B 1 2 3
149 C 1 2 3
150
151 A limitation of this representation is that instance attributes may not shadow
152 class attributes: if an attribute with a given name is not defined on an
153 instance, an attribute with the same name cannot be provided by the class of
154 the instance or any superclass of the instance's class.
155
156 The table can be compacted using a representation known as a displacement
157 list (referred to as an object list in this context):
158
159 Classes with attribute offsets
160
161 classcode A
162 attrcode a b x y z
163
164 B
165 a b x y z
166
167 C
168 a b x y z
169
170 List . . 1 2 1 2 3 1 2 . . 3
171
172 Here, the classcode refers to the offset in the list at which a class's
173 attributes are defined, whereas the attrcode defines the offset within a
174 region of attributes corresponding to a single attribute of a given name.
175
176 Attribute Locations
177 -------------------
178
179 The locations stored in table/list elements are for instance attributes
180 relative to the location of the instance, whereas those for class attributes
181 and modules are absolute addresses (although these could also be changed to
182 object-relative locations).
183
184 Objects and Structures
185 ======================
186
187 As well as references, micropython needs to have actual objects to refer to.
188 Since classes, functions and instances are all objects, it is desirable that
189 certain common features and operations are supported in the same way for all
190 of these things. To permit this, a common data structure format is used.
191
192 Header............................................................................ Attributes.................
193
194 Identifier Identifier Address Details Flag Identifier Size Object Object ...
195
196 0 1 2 3 4 5 6 7 8 9
197 classcode attrcode invocation invocation instance funccode size __class__ attribute ...
198 reference #args, status reference reference
199 defaults
200 reference
201
202 Here, the classcode refers to the attribute lookup table for the object (as
203 described above). Classes and instances share the same classcode, and their
204 structures reflect this. Functions all belong to the same type and thus employ
205 the classcode for the function built-in type, whereas modules have distinct
206 types since they must support different sets of attributes.
207
208 Class C:
209
210 0 1 2 3 4 5 6 7 8 9
211 classcode attrcode __new__ __new__ false size class type attribute ...
212 for C for C reference #args, reference reference
213 defaults
214 reference
215
216 Instance of C:
217
218 0 1 2 3 4 5 6 7 8 9
219 classcode attrcode C.__call__ C.__call__ true size class C attribute ...
220 for C for C reference #args, reference reference
221 (if exists) defaults
222 reference
223
224 Function f:
225
226 0 1 2 3 4 5 6 7 8 9
227 classcode attrcode code code true funccode size class attribute ...
228 for for reference #args, function (default)
229 function function defaults reference reference
230 reference
231
232 Module m:
233
234 0 1 2 3 4 5 6 7 8 9
235 classcode attrcode (unused) (unused) true module type attribute ...
236 for m for m reference (global)
237 reference
238
239 The __class__ Attribute
240 -----------------------
241
242 All objects support the __class__ attribute and this is illustrated above with
243 the first attribute.
244
245 Class: refers to the type class (type.__class__ also refers to the type class)
246 Function: refers to the function class
247 Instance: refers to the class instantiated to make the object
248
249 Lists and Tuples
250 ----------------
251
252 The built-in list and tuple sequences employ variable length structures using
253 the attribute locations to store their elements, where each element is a
254 reference to a separately stored object.
255
256 Testing Instance Compatibility with Classes (attrcode)
257 ------------------------------------------------------
258
259 Although it would be possible to have a data structure mapping classes to
260 compatible classes, such as a matrix indicating the subclasses (or
261 superclasses) of each class, the need to retain the key to such a data
262 structure for each class might introduce a noticeable overhead.
263
264 Instead of having a separate structure, descendant classes of each class are
265 inserted as special attributes into the object table. This requires an extra
266 key to be retained, since each class must provide its own attribute code such
267 that upon an instance/class compatibility test, the code may be obtained and
268 used in the object table.
269
270 Invocation and Code References
271 ------------------------------
272
273 Modules: there is no meaningful invocation reference since modules cannot be
274 explicitly called.
275
276 Functions: a simple code reference is employed pointing to code implementing
277 the function. Note that the function locals are completely distinct from this
278 structure and are not comparable to attributes. Instead, attributes are
279 reserved for default parameter values, although they do not appear in the
280 object table described above, appearing instead in a separate parameter table
281 described below.
282
283 Classes: given that classes must be invoked in order to create instances, a
284 reference must be provided in class structures. However, this reference does
285 not point directly at the __init__ method of the class. Instead, the
286 referenced code belongs to a special initialiser function, __new__, consisting
287 of the following instructions:
288
289 create instance for C
290 call C.__init__(instance, ...)
291 return instance
292
293 Instances: each instance employs a reference to any __call__ method defined in
294 the class hierarchy for the instance, thus maintaining its callable nature.
295
296 Both classes and modules may contain code in their definitions - the former in
297 the "body" of the class, potentially defining attributes, and the latter as
298 the "top-level" code in the module, potentially defining attributes/globals -
299 but this code is not associated with any invocation target. It is thus
300 generated in order of appearance and is not referenced externally.
301
302 Invocation Operation
303 --------------------
304
305 Consequently, regardless of the object an invocation is always done as
306 follows:
307
308 get invocation reference from the header
309 jump to reference
310
311 Additional preparation is necessary before the above code: positional
312 arguments must be saved in the invocation frame, and keyword arguments must be
313 resolved and saved to the appropriate position in the invocation frame.
314
315 See invocation.txt for details.
316
317 Parameters and Lookups
318 ======================
319
320 Since Python supports keyword arguments when making invocations, it becomes
321 necessary to record the parameter names associated with each function or
322 method. Just as object tables record attributes positions on classes and
323 instances, parameter tables record parameter positions in function or method
324 parameter lists.
325
326 For a given program, a parameter table can be considered as being like a
327 matrix mapping functions/methods to parameter names. For example:
328
329 def f(x, y, z):
330 pass
331
332 def g(a, b, c):
333 pass
334
335 def h(a, x):
336 pass
337
338 This would provide the following table, referred to as a parameter table in
339 the context of functions and methods:
340
341 Function/param a b c x y z
342
343 f 1 2 3
344 g 1 2 3
345 h 1 2
346
347 Just as with parameter tables, a displacement list can be prepared from a
348 parameter table:
349
350 Functions with parameter (attribute) offsets
351
352 funccode f
353 attrcode a b c x y z
354
355 g
356 a b c x y z
357
358 h
359 a b c x y z
360
361 List . . . 1 2 3 1 2 3 1 . . 2 . .
362
363 Here, the funccode refers to the offset in the list at which a function's
364 parameters are defined, whereas the attrcode defines the offset within a
365 region of attributes corresponding to a single parameter of a given name.
366
367 Instantiation
368 =============
369
370 When instantiating classes, memory must be reserved for the header of the
371 resulting instance, along with locations for the attributes of the instance.
372 Since the instance header contains data common to all instances of a class, a
373 template header is copied to the start of the newly reserved memory region.