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@123 | 16 | Values and Contexts
|
paul@123 | 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@123 | 64 | predetermined object (provided
|
paul@123 | 65 | that the object and context are
|
paul@123 | 66 | compatible, which can be tested at
|
paul@123 | 67 | compile-time)
|
paul@119 | 68 |
|
paul@109 | 69 | LoadAttr Load attribute from Attributes with null contexts or
|
paul@119 | 70 | instance contexts compatible with the
|
paul@119 | 71 | instance cause loaded attributes
|
paul@109 | 72 | to combine the instance as context
|
paul@109 | 73 | with the object from the
|
paul@109 | 74 | attribute; other attributes have
|
paul@109 | 75 | their context preserved
|
paul@109 | 76 |
|
paul@127 | 77 | LoadAttrIndex Load attribute from Functions and modules as unknown
|
paul@127 | 78 | object the unknown object accessor cause
|
paul@119 | 79 | the loaded attribute to be
|
paul@127 | 80 | retrieved unchanged; classes and
|
paul@109 | 81 | instances cause the LoadAttr rules
|
paul@127 | 82 | to apply (class compatibility
|
paul@127 | 83 | applies)
|
paul@109 | 84 |
|
paul@119 | 85 | A certain amount of run-time testing might be required for both LoadAttr and
|
paul@119 | 86 | LoadAttrIndex instructions. However, with certain restrictions in place around
|
paul@119 | 87 | class attributes, some simplifications are possible:
|
paul@119 | 88 |
|
paul@119 | 89 | * Since only class-originating attributes may cause context overriding, and
|
paul@119 | 90 | since class attributes may only be defined within class definitions, the
|
paul@119 | 91 | attributes whose context may be modified should be known at compile-time.
|
paul@123 | 92 | (These will be those attributes whose context agrees with their parent
|
paul@123 | 93 | class.)
|
paul@119 | 94 |
|
paul@119 | 95 | * By recording a special context value for attributes whose context can be
|
paul@119 | 96 | overridden, this value can be tested efficiently at run-time where the
|
paul@123 | 97 | appropriate conditions are satisfied. (This special context value or
|
paul@123 | 98 | indicator will be present in the object table record for the attribute.)
|
paul@119 | 99 |
|
paul@119 | 100 | * It should be possible to move the instance compatibility condition testing
|
paul@119 | 101 | to compile-time by testing the compatibility of the origin of an attribute
|
paul@123 | 102 | with the class on which it is stored. However, some compatibility testing
|
paul@123 | 103 | will still be required if invoking methods via classes, since the instance
|
paul@123 | 104 | will be specified in the argument list instead of being presented in an
|
paul@123 | 105 | attribute lookup instruction.
|
paul@119 | 106 |
|
paul@119 | 107 | Storing Values
|
paul@119 | 108 | --------------
|
paul@119 | 109 |
|
paul@119 | 110 | According to the table describing value acquisition, different instructions
|
paul@119 | 111 | must implement different operations when acquiring values:
|
paul@119 | 112 |
|
paul@119 | 113 | Instruction Purpose Context Operations
|
paul@119 | 114 | ----------- ------- ------------------
|
paul@119 | 115 |
|
paul@119 | 116 | StoreAddress Store attribute in a Preserve context; note that no
|
paul@119 | 117 | known object test for class attribute
|
paul@119 | 118 | assignment should be necessary
|
paul@119 | 119 | since this instruction should only
|
paul@119 | 120 | be generated for module globals
|
paul@119 | 121 |
|
paul@119 | 122 | StoreAttr Store attribute in an Preserve context; note that no
|
paul@119 | 123 | instance test for class attribute
|
paul@119 | 124 | assignment should be necessary
|
paul@119 | 125 | since this instruction should only
|
paul@119 | 126 | be generated for self accesses
|
paul@119 | 127 |
|
paul@119 | 128 | StoreAttrIndex Store attribute in an Preserve context; since the index
|
paul@119 | 129 | unknown object lookup could yield a class
|
paul@119 | 130 | attribute, a test of the nature of
|
paul@119 | 131 | the nature of the structure is
|
paul@119 | 132 | necessary in order to prevent
|
paul@119 | 133 | assignments to classes
|
paul@109 | 134 |
|
paul@109 | 135 | Objects
|
paul@109 | 136 | -------
|
paul@109 | 137 |
|
paul@109 | 138 | Since classes, functions and instances are all "objects", each must support
|
paul@109 | 139 | certain features and operations in the same way.
|
paul@109 | 140 |
|
paul@109 | 141 | The __class__ Attribute
|
paul@109 | 142 | -----------------------
|
paul@109 | 143 |
|
paul@109 | 144 | All objects support the __class__ attribute:
|
paul@109 | 145 |
|
paul@109 | 146 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@109 | 147 | Function: refers to the function class
|
paul@109 | 148 | Instance: refers to the class instantiated to make the object
|
paul@109 | 149 |
|
paul@109 | 150 | Invocation
|
paul@109 | 151 | ----------
|
paul@109 | 152 |
|
paul@109 | 153 | The following actions need to be supported:
|
paul@109 | 154 |
|
paul@109 | 155 | Class: create instance, call __init__ with instance, return object
|
paul@109 | 156 | Function: call function body, return result
|
paul@109 | 157 | Instance: call __call__ method, return result
|
paul@109 | 158 |
|
paul@109 | 159 | Structure Layout
|
paul@109 | 160 | ----------------
|
paul@109 | 161 |
|
paul@109 | 162 | A suitable structure layout might be something like this:
|
paul@109 | 163 |
|
paul@134 | 164 | Identifier Identifier Address Details Type Object ...
|
paul@109 | 165 |
|
paul@134 | 166 | 0 1 2 3 4 5 6
|
paul@134 | 167 | classcode attrcode invocation invocation __class__ attribute ...
|
paul@134 | 168 | reference #args, reference reference
|
paul@137 | 169 | defaults
|
paul@137 | 170 | reference
|
paul@109 | 171 |
|
paul@109 | 172 | Here, the classcode refers to the attribute lookup table for the object. Since
|
paul@109 | 173 | classes and instances share the same classcode, they might resemble the
|
paul@109 | 174 | following:
|
paul@109 | 175 |
|
paul@109 | 176 | Class C:
|
paul@109 | 177 |
|
paul@134 | 178 | 0 1 2 3 4 5 6
|
paul@134 | 179 | code for C attrcode __new__ __new__ class type attribute ...
|
paul@134 | 180 | for C reference #args, reference reference
|
paul@137 | 181 | defaults
|
paul@137 | 182 | reference
|
paul@109 | 183 |
|
paul@109 | 184 | Instance of C:
|
paul@109 | 185 |
|
paul@134 | 186 | 0 1 2 3 4 5 6
|
paul@134 | 187 | code for C attrcode C.__call__ C.__call__ class C attribute ...
|
paul@134 | 188 | for C reference #args, reference reference
|
paul@137 | 189 | (if exists) defaults
|
paul@137 | 190 | reference
|
paul@109 | 191 |
|
paul@109 | 192 | The __new__ reference would lead to code consisting of the following
|
paul@109 | 193 | instructions:
|
paul@109 | 194 |
|
paul@109 | 195 | create instance for C
|
paul@109 | 196 | call C.__init__(instance, ...)
|
paul@109 | 197 | return instance
|
paul@109 | 198 |
|
paul@109 | 199 | If C has a __call__ attribute, the invocation "slot" of C instances would
|
paul@109 | 200 | refer to the same thing as C.__call__.
|
paul@109 | 201 |
|
paul@109 | 202 | For functions, the same general layout applies:
|
paul@109 | 203 |
|
paul@109 | 204 | Function f:
|
paul@109 | 205 |
|
paul@134 | 206 | 0 1 2 3 4 5 6
|
paul@134 | 207 | code for attrcode code code class attribute ...
|
paul@134 | 208 | function for reference #args, function (default)
|
paul@137 | 209 | function defaults reference reference
|
paul@137 | 210 | reference
|
paul@109 | 211 |
|
paul@109 | 212 | Here, the code reference would lead to code for the function. Note that the
|
paul@109 | 213 | function locals are completely distinct from this structure and are not
|
paul@109 | 214 | comparable to attributes. Instead, attributes are reserved for default
|
paul@109 | 215 | parameter values.
|
paul@109 | 216 |
|
paul@109 | 217 | For modules, there is no meaningful invocation reference:
|
paul@109 | 218 |
|
paul@109 | 219 | Module m:
|
paul@109 | 220 |
|
paul@134 | 221 | 0 1 2 3 4 5 6
|
paul@134 | 222 | code for m attrcode (unused) (unused) module type attribute ...
|
paul@134 | 223 | for m reference (global)
|
paul@134 | 224 | reference
|
paul@109 | 225 |
|
paul@109 | 226 | Both classes and modules have code in their definitions, but this would be
|
paul@109 | 227 | generated in order and not referenced externally.
|
paul@109 | 228 |
|
paul@109 | 229 | Invocation Operation
|
paul@109 | 230 | --------------------
|
paul@109 | 231 |
|
paul@109 | 232 | Consequently, regardless of the object an invocation is always done as
|
paul@109 | 233 | follows:
|
paul@109 | 234 |
|
paul@109 | 235 | get invocation reference (at object+1)
|
paul@109 | 236 | jump to reference
|
paul@109 | 237 |
|
paul@109 | 238 | Additional preparation is necessary before the above code: positional
|
paul@109 | 239 | arguments must be saved to the parameter stack, and keyword arguments must be
|
paul@109 | 240 | resolved and saved to the appropriate position in the parameter stack.
|
paul@109 | 241 |
|
paul@109 | 242 | Attribute Operations
|
paul@109 | 243 | --------------------
|
paul@109 | 244 |
|
paul@109 | 245 | Attribute access needs to go through the attribute lookup table. Some
|
paul@109 | 246 | optimisations are possible and are described in the appropriate section.
|
paul@109 | 247 |
|
paul@109 | 248 | One important aspect of attribute access is the appropriate setting of the
|
paul@109 | 249 | context in the acquired attribute value. From the table describing the
|
paul@109 | 250 | acquisition of values, it is clear that the principal exception is that where
|
paul@109 | 251 | a class-originating attribute is accessed on an instance. Consequently, the
|
paul@109 | 252 | following algorithm could be employed once an attribute has been located:
|
paul@109 | 253 |
|
paul@109 | 254 | 1. If the attribute's context is a special value, indicating that it should
|
paul@109 | 255 | be replaced upon instance access, then proceed to the next step;
|
paul@109 | 256 | otherwise, acquire both the context and the object as they are.
|
paul@109 | 257 |
|
paul@109 | 258 | 2. If the accessor is an instance, use that as the value's context, acquiring
|
paul@109 | 259 | only the object from the attribute.
|
paul@109 | 260 |
|
paul@109 | 261 | Where accesses can be determined ahead of time (as discussed in the
|
paul@109 | 262 | optimisations section), the above algorithm may not necessarily be employed in
|
paul@109 | 263 | the generated code for some accesses.
|
paul@134 | 264 |
|
paul@134 | 265 | Instance/Class Compatibility
|
paul@134 | 266 | ----------------------------
|
paul@134 | 267 |
|
paul@134 | 268 | Although it would be possible to have a data structure mapping classes to
|
paul@134 | 269 | compatible classes, which in the case of context (or self argument)
|
paul@134 | 270 | suitability in invocations would involve a mapping from a class to itself plus
|
paul@134 | 271 | its descendants, the need to retain the key to such a data structure for each
|
paul@134 | 272 | class might introduce a noticeable overhead. Such a structure would
|
paul@134 | 273 | effectively be a matrix with each dimension indexed using the same sequence of
|
paul@134 | 274 | codes for each of the classes in a program.
|
paul@134 | 275 |
|
paul@134 | 276 | An alternative might be to insert descendants as special attributes into the
|
paul@134 | 277 | object/attribute lookup table. This would also require an extra key to be
|
paul@134 | 278 | retained, since each class would have its own attribute code.
|