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 Manipulating 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 stored as cause the loaded attribute to be
56 an attribute 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 LoadAttr Load attribute from Attributes with null contexts or
64 instance stored as an contexts compatible with the
65 attribute instance cause loaded attributes
66 to combine the instance as context
67 with the object from the
68 attribute; other attributes have
69 their context preserved
70
71 LoadAttrIndex Load attribute from Classes, functions and modules as
72 unknown object stored the unknown object accessor cause
73 as an attribute the loaded attribute to be
74 retrieved unchanged; whereas
75 instances cause the LoadAttr rules
76 to apply
77
78 Consequently, a certain amount of run-time testing is required for both
79 LoadAttr and LoadAttrIndex.
80
81 Objects
82 -------
83
84 Since classes, functions and instances are all "objects", each must support
85 certain features and operations in the same way.
86
87 The __class__ Attribute
88 -----------------------
89
90 All objects support the __class__ attribute:
91
92 Class: refers to the type class (type.__class__ also refers to the type class)
93 Function: refers to the function class
94 Instance: refers to the class instantiated to make the object
95
96 Invocation
97 ----------
98
99 The following actions need to be supported:
100
101 Class: create instance, call __init__ with instance, return object
102 Function: call function body, return result
103 Instance: call __call__ method, return result
104
105 Structure Layout
106 ----------------
107
108 A suitable structure layout might be something like this:
109
110 Identifier Address Type Object ...
111
112 0 1 2 3 4
113 classcode invocation __class__ attribute ...
114 reference reference reference
115
116 Here, the classcode refers to the attribute lookup table for the object. Since
117 classes and instances share the same classcode, they might resemble the
118 following:
119
120 Class C:
121
122 0 1 2 3 4
123 code for C __new__ class type attribute ...
124 reference reference reference
125
126 Instance of C:
127
128 0 1 2 3 4
129 code for C C.__call__ class C attribute ...
130 reference reference reference
131 (if exists)
132
133 The __new__ reference would lead to code consisting of the following
134 instructions:
135
136 create instance for C
137 call C.__init__(instance, ...)
138 return instance
139
140 If C has a __call__ attribute, the invocation "slot" of C instances would
141 refer to the same thing as C.__call__.
142
143 For functions, the same general layout applies:
144
145 Function f:
146
147 0 1 2 3 4
148 code for code class attribute ...
149 function reference function reference
150 reference
151
152 Here, the code reference would lead to code for the function. Note that the
153 function locals are completely distinct from this structure and are not
154 comparable to attributes. Instead, attributes are reserved for default
155 parameter values.
156
157 For modules, there is no meaningful invocation reference:
158
159 Module m:
160
161 0 1 2 3 4
162 code for m (unused) module type attribute ...
163 reference (global)
164 reference
165
166 Both classes and modules have code in their definitions, but this would be
167 generated in order and not referenced externally.
168
169 Invocation Operation
170 --------------------
171
172 Consequently, regardless of the object an invocation is always done as
173 follows:
174
175 get invocation reference (at object+1)
176 jump to reference
177
178 Additional preparation is necessary before the above code: positional
179 arguments must be saved to the parameter stack, and keyword arguments must be
180 resolved and saved to the appropriate position in the parameter stack.
181
182 Attribute Operations
183 --------------------
184
185 Attribute access needs to go through the attribute lookup table. Some
186 optimisations are possible and are described in the appropriate section.
187
188 One important aspect of attribute access is the appropriate setting of the
189 context in the acquired attribute value. From the table describing the
190 acquisition of values, it is clear that the principal exception is that where
191 a class-originating attribute is accessed on an instance. Consequently, the
192 following algorithm could be employed once an attribute has been located:
193
194 1. If the attribute's context is a special value, indicating that it should
195 be replaced upon instance access, then proceed to the next step;
196 otherwise, acquire both the context and the object as they are.
197
198 2. If the accessor is an instance, use that as the value's context, acquiring
199 only the object from the attribute.
200
201 Where accesses can be determined ahead of time (as discussed in the
202 optimisations section), the above algorithm may not necessarily be employed in
203 the generated code for some accesses.