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 Identifier Size Object Object ...
195
196 0 1 2 3 4 5 6 7 8
197 classcode attrcode/ invocation invocation funccode size __class__ attribute ...
198 instance reference #args, reference reference
199 status defaults
200 reference
201
202 Classcode
203 ---------
204
205 Used in attribute lookup.
206
207 Here, the classcode refers to the attribute lookup table for the object (as
208 described above). Classes and instances share the same classcode, and their
209 structures reflect this. Functions all belong to the same type and thus employ
210 the classcode for the function built-in type, whereas modules have distinct
211 types since they must support different sets of attributes.
212
213 Attrcode
214 --------
215
216 Used to test instances for membership of classes (or descendants of classes).
217
218 Since, in traditional Python, classes are only ever instances of the "type"
219 built-in class, support for testing such a relationship directly has been
220 removed and the attrcode is not specified for classes: the presence of an
221 attrcode indicates that a given object is an instance.
222
223 Attributes
224 ----------
225
226 For classes, modules and instances, the attributes in the structure correspond
227 to the attributes of each kind of object. For functions, however, the
228 attributes in the structure correspond to the default arguments for each
229 function, if any.
230
231 The two kinds of structure attribute should be accommodated by both groups of
232 objects, since without a default argument section, classes and instances may
233 not employ default arguments in their respective instantiator/__init__ and
234 __call__ methods.
235
236 Structure Types
237 ---------------
238
239 Class C:
240
241 0 1 2 3 4 5 6 7 8
242 classcode (unused) __new__ __new__ size class type attribute ...
243 for C reference #args, reference reference
244 defaults
245 reference
246
247 Instance of C:
248
249 0 1 2 3 4 5 6 7 8
250 classcode attrcode C.__call__ C.__call__ size class C attribute ...
251 for C for C reference #args, reference reference
252 (if exists) defaults
253 reference
254
255 Function f:
256
257 0 1 2 3 4 5 6 7 8
258 classcode attrcode code code funccode size class attribute ...
259 for for reference #args, function (default)
260 function function defaults reference reference
261 reference
262
263 Module m:
264
265 0 1 2 3 4 5 6 7 8
266 classcode attrcode (unused) (unused) module type attribute ...
267 for m for m reference (global)
268 reference
269
270 The __class__ Attribute
271 -----------------------
272
273 All objects support the __class__ attribute and this is illustrated above with
274 the first attribute.
275
276 Class: refers to the type class (type.__class__ also refers to the type class)
277 Function: refers to the function class
278 Instance: refers to the class instantiated to make the object
279
280 Lists and Tuples
281 ----------------
282
283 The built-in list and tuple sequences employ variable length structures using
284 the attribute locations to store their elements, where each element is a
285 reference to a separately stored object.
286
287 Testing Instance Compatibility with Classes (attrcode)
288 ------------------------------------------------------
289
290 Although it would be possible to have a data structure mapping classes to
291 compatible classes, such as a matrix indicating the subclasses (or
292 superclasses) of each class, the need to retain the key to such a data
293 structure for each class might introduce a noticeable overhead.
294
295 Instead of having a separate structure, descendant classes of each class are
296 inserted as special attributes into the object table. This requires an extra
297 key to be retained, since each class must provide its own attribute code such
298 that upon an instance/class compatibility test, the code may be obtained and
299 used in the object table.
300
301 Invocation and Code References
302 ------------------------------
303
304 Modules: there is no meaningful invocation reference since modules cannot be
305 explicitly called.
306
307 Functions: a simple code reference is employed pointing to code implementing
308 the function. Note that the function locals are completely distinct from this
309 structure and are not comparable to attributes. Instead, attributes are
310 reserved for default parameter values, although they do not appear in the
311 object table described above, appearing instead in a separate parameter table
312 described below.
313
314 Classes: given that classes must be invoked in order to create instances, a
315 reference must be provided in class structures. However, this reference does
316 not point directly at the __init__ method of the class. Instead, the
317 referenced code belongs to a special initialiser function, __new__, consisting
318 of the following instructions:
319
320 create instance for C
321 call C.__init__(instance, ...)
322 return instance
323
324 Instances: each instance employs a reference to any __call__ method defined in
325 the class hierarchy for the instance, thus maintaining its callable nature.
326
327 Both classes and modules may contain code in their definitions - the former in
328 the "body" of the class, potentially defining attributes, and the latter as
329 the "top-level" code in the module, potentially defining attributes/globals -
330 but this code is not associated with any invocation target. It is thus
331 generated in order of appearance and is not referenced externally.
332
333 Invocation Operation
334 --------------------
335
336 Consequently, regardless of the object an invocation is always done as
337 follows:
338
339 get invocation reference from the header
340 jump to reference
341
342 Additional preparation is necessary before the above code: positional
343 arguments must be saved in the invocation frame, and keyword arguments must be
344 resolved and saved to the appropriate position in the invocation frame.
345
346 See invocation.txt for details.
347
348 Parameters and Lookups
349 ======================
350
351 Since Python supports keyword arguments when making invocations, it becomes
352 necessary to record the parameter names associated with each function or
353 method. Just as object tables record attributes positions on classes and
354 instances, parameter tables record parameter positions in function or method
355 parameter lists.
356
357 For a given program, a parameter table can be considered as being like a
358 matrix mapping functions/methods to parameter names. For example:
359
360 def f(x, y, z):
361 pass
362
363 def g(a, b, c):
364 pass
365
366 def h(a, x):
367 pass
368
369 This would provide the following table, referred to as a parameter table in
370 the context of functions and methods:
371
372 Function/param a b c x y z
373
374 f 1 2 3
375 g 1 2 3
376 h 1 2
377
378 Just as with parameter tables, a displacement list can be prepared from a
379 parameter table:
380
381 Functions with parameter (attribute) offsets
382
383 funccode f
384 attrcode a b c x y z
385
386 g
387 a b c x y z
388
389 h
390 a b c x y z
391
392 List . . . 1 2 3 1 2 3 1 . . 2 . .
393
394 Here, the funccode refers to the offset in the list at which a function's
395 parameters are defined, whereas the attrcode defines the offset within a
396 region of attributes corresponding to a single parameter of a given name.
397
398 Instantiation
399 =============
400
401 When instantiating classes, memory must be reserved for the header of the
402 resulting instance, along with locations for the attributes of the instance.
403 Since the instance header contains data common to all instances of a class, a
404 template header is copied to the start of the newly reserved memory region.