1 Data Structures
2 ===============
3
4 The fundamental "value type" is a pair of references: one pointing to the
5 referenced object represented by the interchangeable value; one referring to
6 the context of the referenced object, typically the object through which the
7 referenced object was acquired as an attribute.
8
9 Value Layout
10 ------------
11
12 0 1
13 object context
14 reference reference
15
16 Acquiring Values
17 ----------------
18
19 Values are acquired through name lookups and attribute access, yielding
20 the appropriate object reference together with a context reference as
21 indicated in the following table:
22
23 Type of Access Context Notes
24 -------------- ------- -----
25
26 Local name Preserved Functions provide no context
27
28 Global name Preserved Modules provide no context
29
30 Class-originating Accessor Class accessor preserves the stored
31 attribute -or- context; instance accessor overrides
32 Preserved the stored context if it is null or
33 belongs to the instance's class
34 hierarchy
35
36 Instance-originating Preserved Methods retain their original context
37 attribute
38
39 There may be some scope for simplifying the above, to the detriment of Python
40 compatibility, since the unbound vs. bound methods situation can be confusing.
41
42 Acquiring Values
43 ----------------
44
45 According to the table describing value acquisition, different instructions
46 must implement different operations when acquiring values:
47
48 Instruction Purpose Context Operations
49 ----------- ------- ------------------
50
51 LoadConst Load class, function, Combine null context with loaded
52 module, constant object
53
54 LoadAddress Load attribute from Classes, functions and modules
55 known object cause the loaded attribute to be
56 retrieved unchanged; whereas
57 constants (representing instances)
58 cause the constant to override the
59 attribute's own context (since all
60 attributes should belong to the
61 constant's class hierarchy)
62
63 LoadAddressContext Override loaded context with a
64 predetermined object
65
66 LoadAttr Load attribute from Attributes with null contexts or
67 instance contexts compatible with the
68 instance cause loaded attributes
69 to combine the instance as context
70 with the object from the
71 attribute; other attributes have
72 their context preserved
73
74 LoadAttrIndex Load attribute from Classes, functions and modules as
75 unknown object the unknown object accessor cause
76 the loaded attribute to be
77 retrieved unchanged; whereas
78 instances cause the LoadAttr rules
79 to apply
80
81 A certain amount of run-time testing might be required for both LoadAttr and
82 LoadAttrIndex instructions. However, with certain restrictions in place around
83 class attributes, some simplifications are possible:
84
85 * Since only class-originating attributes may cause context overriding, and
86 since class attributes may only be defined within class definitions, the
87 attributes whose context may be modified should be known at compile-time.
88
89 * By recording a special context value for attributes whose context can be
90 overridden, this value can be tested efficiently at run-time where the
91 appropriate conditions are satisfied.
92
93 * It should be possible to move the instance compatibility condition testing
94 to compile-time by testing the compatibility of the origin of an attribute
95 with the class on which it is stored.
96
97 Storing Values
98 --------------
99
100 According to the table describing value acquisition, different instructions
101 must implement different operations when acquiring values:
102
103 Instruction Purpose Context Operations
104 ----------- ------- ------------------
105
106 StoreAddress Store attribute in a Preserve context; note that no
107 known object test for class attribute
108 assignment should be necessary
109 since this instruction should only
110 be generated for module globals
111
112 StoreAttr Store attribute in an Preserve context; note that no
113 instance test for class attribute
114 assignment should be necessary
115 since this instruction should only
116 be generated for self accesses
117
118 StoreAttrIndex Store attribute in an Preserve context; since the index
119 unknown object lookup could yield a class
120 attribute, a test of the nature of
121 the nature of the structure is
122 necessary in order to prevent
123 assignments to classes
124
125 Objects
126 -------
127
128 Since classes, functions and instances are all "objects", each must support
129 certain features and operations in the same way.
130
131 The __class__ Attribute
132 -----------------------
133
134 All objects support the __class__ attribute:
135
136 Class: refers to the type class (type.__class__ also refers to the type class)
137 Function: refers to the function class
138 Instance: refers to the class instantiated to make the object
139
140 Invocation
141 ----------
142
143 The following actions need to be supported:
144
145 Class: create instance, call __init__ with instance, return object
146 Function: call function body, return result
147 Instance: call __call__ method, return result
148
149 Structure Layout
150 ----------------
151
152 A suitable structure layout might be something like this:
153
154 Identifier Address Details Type Object ...
155
156 0 1 2 3 4 5
157 classcode invocation invocation __class__ attribute ...
158 reference #args, reference reference
159 #defaults
160
161 Here, the classcode refers to the attribute lookup table for the object. Since
162 classes and instances share the same classcode, they might resemble the
163 following:
164
165 Class C:
166
167 0 1 2 3 4 5
168 code for C __new__ __new__ class type attribute ...
169 reference #args, reference reference
170 #defaults
171
172 Instance of C:
173
174 0 1 2 3 4 5
175 code for C C.__call__ C.__call__ class C attribute ...
176 reference #args, reference reference
177 (if exists) #defaults
178
179 The __new__ reference would lead to code consisting of the following
180 instructions:
181
182 create instance for C
183 call C.__init__(instance, ...)
184 return instance
185
186 If C has a __call__ attribute, the invocation "slot" of C instances would
187 refer to the same thing as C.__call__.
188
189 For functions, the same general layout applies:
190
191 Function f:
192
193 0 1 2 3 4 5
194 code for code code class attribute ...
195 function reference #args, function reference
196 #defaults reference
197
198 Here, the code reference would lead to code for the function. Note that the
199 function locals are completely distinct from this structure and are not
200 comparable to attributes. Instead, attributes are reserved for default
201 parameter values.
202
203 For modules, there is no meaningful invocation reference:
204
205 Module m:
206
207 0 1 2 3 4 5
208 code for m (unused) (unused) module type attribute ...
209 reference (global)
210 reference
211
212 Both classes and modules have code in their definitions, but this would be
213 generated in order and not referenced externally.
214
215 Invocation Operation
216 --------------------
217
218 Consequently, regardless of the object an invocation is always done as
219 follows:
220
221 get invocation reference (at object+1)
222 jump to reference
223
224 Additional preparation is necessary before the above code: positional
225 arguments must be saved to the parameter stack, and keyword arguments must be
226 resolved and saved to the appropriate position in the parameter stack.
227
228 Attribute Operations
229 --------------------
230
231 Attribute access needs to go through the attribute lookup table. Some
232 optimisations are possible and are described in the appropriate section.
233
234 One important aspect of attribute access is the appropriate setting of the
235 context in the acquired attribute value. From the table describing the
236 acquisition of values, it is clear that the principal exception is that where
237 a class-originating attribute is accessed on an instance. Consequently, the
238 following algorithm could be employed once an attribute has been located:
239
240 1. If the attribute's context is a special value, indicating that it should
241 be replaced upon instance access, then proceed to the next step;
242 otherwise, acquire both the context and the object as they are.
243
244 2. If the accessor is an instance, use that as the value's context, acquiring
245 only the object from the attribute.
246
247 Where accesses can be determined ahead of time (as discussed in the
248 optimisations section), the above algorithm may not necessarily be employed in
249 the generated code for some accesses.