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@185 | 16 | Such values are used as the stored representations of attributes (of classes,
|
paul@185 | 17 | instances, modules, and other objects supporting attribute-like entities) as
|
paul@185 | 18 | well as the stored values associated with names in functions and methods.
|
paul@185 | 19 |
|
paul@185 | 20 | Stored Values and Contexts
|
paul@185 | 21 | --------------------------
|
paul@185 | 22 |
|
paul@185 | 23 | In a program image, generated attribute data will employ values, and these
|
paul@185 | 24 | values will generally have the following context definitions according to the
|
paul@185 | 25 | type of the referenced objects:
|
paul@185 | 26 |
|
paul@185 | 27 | Referenced Object Type Context
|
paul@185 | 28 | ---------------------- -------
|
paul@185 | 29 |
|
paul@185 | 30 | Function None
|
paul@185 | 31 |
|
paul@185 | 32 | Method Parent object (class)
|
paul@185 | 33 |
|
paul@185 | 34 | Class None
|
paul@185 | 35 |
|
paul@185 | 36 | Value and Context Transformations
|
paul@185 | 37 | ---------------------------------
|
paul@109 | 38 |
|
paul@109 | 39 | Values are acquired through name lookups and attribute access, yielding
|
paul@109 | 40 | the appropriate object reference together with a context reference as
|
paul@109 | 41 | indicated in the following table:
|
paul@109 | 42 |
|
paul@109 | 43 | Type of Access Context Notes
|
paul@109 | 44 | -------------- ------- -----
|
paul@109 | 45 |
|
paul@109 | 46 | Local name Preserved Functions provide no context
|
paul@109 | 47 |
|
paul@109 | 48 | Global name Preserved Modules provide no context
|
paul@109 | 49 |
|
paul@109 | 50 | Class-originating Accessor Class accessor preserves the stored
|
paul@109 | 51 | attribute -or- context; instance accessor overrides
|
paul@109 | 52 | Preserved the stored context if it is null or
|
paul@109 | 53 | belongs to the instance's class
|
paul@109 | 54 | hierarchy
|
paul@109 | 55 |
|
paul@109 | 56 | Instance-originating Preserved Methods retain their original context
|
paul@109 | 57 | attribute
|
paul@109 | 58 |
|
paul@109 | 59 | There may be some scope for simplifying the above, to the detriment of Python
|
paul@109 | 60 | compatibility, since the unbound vs. bound methods situation can be confusing.
|
paul@109 | 61 |
|
paul@119 | 62 | Acquiring Values
|
paul@119 | 63 | ----------------
|
paul@109 | 64 |
|
paul@109 | 65 | According to the table describing value acquisition, different instructions
|
paul@109 | 66 | must implement different operations when acquiring values:
|
paul@109 | 67 |
|
paul@109 | 68 | Instruction Purpose Context Operations
|
paul@109 | 69 | ----------- ------- ------------------
|
paul@109 | 70 |
|
paul@109 | 71 | LoadConst Load class, function, Combine null context with loaded
|
paul@109 | 72 | module, constant object
|
paul@109 | 73 |
|
paul@109 | 74 | LoadAddress Load attribute from Classes, functions and modules
|
paul@119 | 75 | known object cause the loaded attribute to be
|
paul@119 | 76 | retrieved unchanged; whereas
|
paul@109 | 77 | constants (representing instances)
|
paul@109 | 78 | cause the constant to override the
|
paul@109 | 79 | attribute's own context (since all
|
paul@109 | 80 | attributes should belong to the
|
paul@109 | 81 | constant's class hierarchy)
|
paul@109 | 82 |
|
paul@119 | 83 | LoadAddressContext Override loaded context with a
|
paul@123 | 84 | predetermined object (provided
|
paul@123 | 85 | that the object and context are
|
paul@123 | 86 | compatible, which can be tested at
|
paul@123 | 87 | compile-time)
|
paul@119 | 88 |
|
paul@109 | 89 | LoadAttr Load attribute from Attributes with null contexts or
|
paul@119 | 90 | instance contexts compatible with the
|
paul@119 | 91 | instance cause loaded attributes
|
paul@109 | 92 | to combine the instance as context
|
paul@109 | 93 | with the object from the
|
paul@109 | 94 | attribute; other attributes have
|
paul@109 | 95 | their context preserved
|
paul@109 | 96 |
|
paul@127 | 97 | LoadAttrIndex Load attribute from Functions and modules as unknown
|
paul@127 | 98 | object the unknown object accessor cause
|
paul@119 | 99 | the loaded attribute to be
|
paul@127 | 100 | retrieved unchanged; classes and
|
paul@109 | 101 | instances cause the LoadAttr rules
|
paul@127 | 102 | to apply (class compatibility
|
paul@127 | 103 | applies)
|
paul@109 | 104 |
|
paul@119 | 105 | A certain amount of run-time testing might be required for both LoadAttr and
|
paul@119 | 106 | LoadAttrIndex instructions. However, with certain restrictions in place around
|
paul@119 | 107 | class attributes, some simplifications are possible:
|
paul@119 | 108 |
|
paul@119 | 109 | * Since only class-originating attributes may cause context overriding, and
|
paul@119 | 110 | since class attributes may only be defined within class definitions, the
|
paul@119 | 111 | attributes whose context may be modified should be known at compile-time.
|
paul@123 | 112 | (These will be those attributes whose context agrees with their parent
|
paul@123 | 113 | class.)
|
paul@119 | 114 |
|
paul@119 | 115 | * By recording a special context value for attributes whose context can be
|
paul@119 | 116 | overridden, this value can be tested efficiently at run-time where the
|
paul@123 | 117 | appropriate conditions are satisfied. (This special context value or
|
paul@123 | 118 | indicator will be present in the object table record for the attribute.)
|
paul@119 | 119 |
|
paul@119 | 120 | * It should be possible to move the instance compatibility condition testing
|
paul@119 | 121 | to compile-time by testing the compatibility of the origin of an attribute
|
paul@123 | 122 | with the class on which it is stored. However, some compatibility testing
|
paul@123 | 123 | will still be required if invoking methods via classes, since the instance
|
paul@123 | 124 | will be specified in the argument list instead of being presented in an
|
paul@123 | 125 | attribute lookup instruction.
|
paul@119 | 126 |
|
paul@119 | 127 | Storing Values
|
paul@119 | 128 | --------------
|
paul@119 | 129 |
|
paul@119 | 130 | According to the table describing value acquisition, different instructions
|
paul@119 | 131 | must implement different operations when acquiring values:
|
paul@119 | 132 |
|
paul@119 | 133 | Instruction Purpose Context Operations
|
paul@119 | 134 | ----------- ------- ------------------
|
paul@119 | 135 |
|
paul@119 | 136 | StoreAddress Store attribute in a Preserve context; note that no
|
paul@119 | 137 | known object test for class attribute
|
paul@119 | 138 | assignment should be necessary
|
paul@119 | 139 | since this instruction should only
|
paul@119 | 140 | be generated for module globals
|
paul@119 | 141 |
|
paul@119 | 142 | StoreAttr Store attribute in an Preserve context; note that no
|
paul@119 | 143 | instance test for class attribute
|
paul@119 | 144 | assignment should be necessary
|
paul@119 | 145 | since this instruction should only
|
paul@119 | 146 | be generated for self accesses
|
paul@119 | 147 |
|
paul@119 | 148 | StoreAttrIndex Store attribute in an Preserve context; since the index
|
paul@119 | 149 | unknown object lookup could yield a class
|
paul@119 | 150 | attribute, a test of the nature of
|
paul@119 | 151 | the nature of the structure is
|
paul@119 | 152 | necessary in order to prevent
|
paul@119 | 153 | assignments to classes
|
paul@109 | 154 |
|
paul@109 | 155 | Objects
|
paul@109 | 156 | -------
|
paul@109 | 157 |
|
paul@109 | 158 | Since classes, functions and instances are all "objects", each must support
|
paul@109 | 159 | certain features and operations in the same way.
|
paul@109 | 160 |
|
paul@109 | 161 | The __class__ Attribute
|
paul@109 | 162 | -----------------------
|
paul@109 | 163 |
|
paul@109 | 164 | All objects support the __class__ attribute:
|
paul@109 | 165 |
|
paul@109 | 166 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@109 | 167 | Function: refers to the function class
|
paul@109 | 168 | Instance: refers to the class instantiated to make the object
|
paul@109 | 169 |
|
paul@109 | 170 | Invocation
|
paul@109 | 171 | ----------
|
paul@109 | 172 |
|
paul@109 | 173 | The following actions need to be supported:
|
paul@109 | 174 |
|
paul@109 | 175 | Class: create instance, call __init__ with instance, return object
|
paul@109 | 176 | Function: call function body, return result
|
paul@109 | 177 | Instance: call __call__ method, return result
|
paul@109 | 178 |
|
paul@109 | 179 | Structure Layout
|
paul@109 | 180 | ----------------
|
paul@109 | 181 |
|
paul@109 | 182 | A suitable structure layout might be something like this:
|
paul@109 | 183 |
|
paul@134 | 184 | Identifier Identifier Address Details Type Object ...
|
paul@109 | 185 |
|
paul@134 | 186 | 0 1 2 3 4 5 6
|
paul@134 | 187 | classcode attrcode invocation invocation __class__ attribute ...
|
paul@134 | 188 | reference #args, reference reference
|
paul@137 | 189 | defaults
|
paul@137 | 190 | reference
|
paul@109 | 191 |
|
paul@109 | 192 | Here, the classcode refers to the attribute lookup table for the object. Since
|
paul@109 | 193 | classes and instances share the same classcode, they might resemble the
|
paul@109 | 194 | following:
|
paul@109 | 195 |
|
paul@109 | 196 | Class C:
|
paul@109 | 197 |
|
paul@134 | 198 | 0 1 2 3 4 5 6
|
paul@134 | 199 | code for C attrcode __new__ __new__ class type attribute ...
|
paul@134 | 200 | for C reference #args, reference reference
|
paul@137 | 201 | defaults
|
paul@137 | 202 | reference
|
paul@109 | 203 |
|
paul@109 | 204 | Instance of C:
|
paul@109 | 205 |
|
paul@134 | 206 | 0 1 2 3 4 5 6
|
paul@134 | 207 | code for C attrcode C.__call__ C.__call__ class C attribute ...
|
paul@134 | 208 | for C reference #args, reference reference
|
paul@137 | 209 | (if exists) defaults
|
paul@137 | 210 | reference
|
paul@109 | 211 |
|
paul@109 | 212 | The __new__ reference would lead to code consisting of the following
|
paul@109 | 213 | instructions:
|
paul@109 | 214 |
|
paul@109 | 215 | create instance for C
|
paul@109 | 216 | call C.__init__(instance, ...)
|
paul@109 | 217 | return instance
|
paul@109 | 218 |
|
paul@109 | 219 | If C has a __call__ attribute, the invocation "slot" of C instances would
|
paul@182 | 220 | refer to the same thing as C.__call__. This "slot" has to be prepared when
|
paul@184 | 221 | creating instances, either by modifying the sequence of instructions used in,
|
paul@184 | 222 | amongst other places, the instantiator function, or by generating a template
|
paul@184 | 223 | instance whose details are copied when new instances are created.
|
paul@109 | 224 |
|
paul@109 | 225 | For functions, the same general layout applies:
|
paul@109 | 226 |
|
paul@109 | 227 | Function f:
|
paul@109 | 228 |
|
paul@134 | 229 | 0 1 2 3 4 5 6
|
paul@134 | 230 | code for attrcode code code class attribute ...
|
paul@134 | 231 | function for reference #args, function (default)
|
paul@137 | 232 | function defaults reference reference
|
paul@137 | 233 | reference
|
paul@109 | 234 |
|
paul@109 | 235 | Here, the code reference would lead to code for the function. Note that the
|
paul@109 | 236 | function locals are completely distinct from this structure and are not
|
paul@109 | 237 | comparable to attributes. Instead, attributes are reserved for default
|
paul@109 | 238 | parameter values.
|
paul@109 | 239 |
|
paul@109 | 240 | For modules, there is no meaningful invocation reference:
|
paul@109 | 241 |
|
paul@109 | 242 | Module m:
|
paul@109 | 243 |
|
paul@134 | 244 | 0 1 2 3 4 5 6
|
paul@134 | 245 | code for m attrcode (unused) (unused) module type attribute ...
|
paul@134 | 246 | for m reference (global)
|
paul@134 | 247 | reference
|
paul@109 | 248 |
|
paul@109 | 249 | Both classes and modules have code in their definitions, but this would be
|
paul@109 | 250 | generated in order and not referenced externally.
|
paul@109 | 251 |
|
paul@109 | 252 | Invocation Operation
|
paul@109 | 253 | --------------------
|
paul@109 | 254 |
|
paul@109 | 255 | Consequently, regardless of the object an invocation is always done as
|
paul@109 | 256 | follows:
|
paul@109 | 257 |
|
paul@109 | 258 | get invocation reference (at object+1)
|
paul@109 | 259 | jump to reference
|
paul@109 | 260 |
|
paul@109 | 261 | Additional preparation is necessary before the above code: positional
|
paul@109 | 262 | arguments must be saved to the parameter stack, and keyword arguments must be
|
paul@109 | 263 | resolved and saved to the appropriate position in the parameter stack.
|
paul@109 | 264 |
|
paul@109 | 265 | Attribute Operations
|
paul@109 | 266 | --------------------
|
paul@109 | 267 |
|
paul@109 | 268 | Attribute access needs to go through the attribute lookup table. Some
|
paul@109 | 269 | optimisations are possible and are described in the appropriate section.
|
paul@109 | 270 |
|
paul@109 | 271 | One important aspect of attribute access is the appropriate setting of the
|
paul@109 | 272 | context in the acquired attribute value. From the table describing the
|
paul@109 | 273 | acquisition of values, it is clear that the principal exception is that where
|
paul@109 | 274 | a class-originating attribute is accessed on an instance. Consequently, the
|
paul@109 | 275 | following algorithm could be employed once an attribute has been located:
|
paul@109 | 276 |
|
paul@109 | 277 | 1. If the attribute's context is a special value, indicating that it should
|
paul@109 | 278 | be replaced upon instance access, then proceed to the next step;
|
paul@109 | 279 | otherwise, acquire both the context and the object as they are.
|
paul@109 | 280 |
|
paul@109 | 281 | 2. If the accessor is an instance, use that as the value's context, acquiring
|
paul@109 | 282 | only the object from the attribute.
|
paul@109 | 283 |
|
paul@109 | 284 | Where accesses can be determined ahead of time (as discussed in the
|
paul@109 | 285 | optimisations section), the above algorithm may not necessarily be employed in
|
paul@109 | 286 | the generated code for some accesses.
|
paul@134 | 287 |
|
paul@134 | 288 | Instance/Class Compatibility
|
paul@134 | 289 | ----------------------------
|
paul@134 | 290 |
|
paul@134 | 291 | Although it would be possible to have a data structure mapping classes to
|
paul@134 | 292 | compatible classes, which in the case of context (or self argument)
|
paul@134 | 293 | suitability in invocations would involve a mapping from a class to itself plus
|
paul@134 | 294 | its descendants, the need to retain the key to such a data structure for each
|
paul@134 | 295 | class might introduce a noticeable overhead. Such a structure would
|
paul@134 | 296 | effectively be a matrix with each dimension indexed using the same sequence of
|
paul@134 | 297 | codes for each of the classes in a program.
|
paul@134 | 298 |
|
paul@181 | 299 | The current solution is to insert descendants as special attributes into the
|
paul@181 | 300 | object/attribute lookup table. This requires an extra key to be retained,
|
paul@181 | 301 | since each class must provide its own attribute code such that upon an
|
paul@181 | 302 | instance/class compatibility test, the code may be obtained and used in the
|
paul@181 | 303 | object table.
|