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