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@189 | 23 | See assignment.txt for information about contexts and transformations.
|
paul@109 | 24 |
|
paul@119 | 25 | Acquiring Values
|
paul@119 | 26 | ----------------
|
paul@109 | 27 |
|
paul@189 | 28 | There are two classes of instructions which provide values:
|
paul@109 | 29 |
|
paul@189 | 30 | Instruction Purpose Context Operations
|
paul@189 | 31 | ----------- ------- ------------------
|
paul@119 | 32 |
|
paul@189 | 33 | LoadConst Load class, function, Combine null context with
|
paul@189 | 34 | module, constant loaded object
|
paul@119 | 35 |
|
paul@189 | 36 | LoadAddress Load attribute from Preserve or override stored
|
paul@189 | 37 | LoadAddressContext class, module, context (as described in
|
paul@189 | 38 | LoadAttr instance assignment.txt)
|
paul@189 | 39 | LoadAttrIndex
|
paul@119 | 40 |
|
paul@119 | 41 | Storing Values
|
paul@119 | 42 | --------------
|
paul@119 | 43 |
|
paul@189 | 44 | There is only one class of instruction for storing values:
|
paul@119 | 45 |
|
paul@119 | 46 | Instruction Purpose Context Operations
|
paul@119 | 47 | ----------- ------- ------------------
|
paul@119 | 48 |
|
paul@119 | 49 | StoreAddress Store attribute in a Preserve context; note that no
|
paul@119 | 50 | known object test for class attribute
|
paul@119 | 51 | assignment should be necessary
|
paul@119 | 52 | since this instruction should only
|
paul@119 | 53 | be generated for module globals
|
paul@119 | 54 |
|
paul@119 | 55 | StoreAttr Store attribute in an Preserve context; note that no
|
paul@119 | 56 | instance test for class attribute
|
paul@119 | 57 | assignment should be necessary
|
paul@119 | 58 | since this instruction should only
|
paul@119 | 59 | be generated for self accesses
|
paul@119 | 60 |
|
paul@119 | 61 | StoreAttrIndex Store attribute in an Preserve context; since the index
|
paul@119 | 62 | unknown object lookup could yield a class
|
paul@119 | 63 | attribute, a test of the nature of
|
paul@119 | 64 | the nature of the structure is
|
paul@119 | 65 | necessary in order to prevent
|
paul@119 | 66 | assignments to classes
|
paul@109 | 67 |
|
paul@186 | 68 | Note that contexts are never changed in the stored value: they are preserved.
|
paul@189 | 69 | See assignment.txt for more information.
|
paul@186 | 70 |
|
paul@109 | 71 | Objects
|
paul@109 | 72 | -------
|
paul@109 | 73 |
|
paul@109 | 74 | Since classes, functions and instances are all "objects", each must support
|
paul@109 | 75 | certain features and operations in the same way.
|
paul@109 | 76 |
|
paul@109 | 77 | The __class__ Attribute
|
paul@109 | 78 | -----------------------
|
paul@109 | 79 |
|
paul@109 | 80 | All objects support the __class__ attribute:
|
paul@109 | 81 |
|
paul@109 | 82 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@109 | 83 | Function: refers to the function class
|
paul@109 | 84 | Instance: refers to the class instantiated to make the object
|
paul@109 | 85 |
|
paul@109 | 86 | Invocation
|
paul@109 | 87 | ----------
|
paul@109 | 88 |
|
paul@109 | 89 | The following actions need to be supported:
|
paul@109 | 90 |
|
paul@109 | 91 | Class: create instance, call __init__ with instance, return object
|
paul@109 | 92 | Function: call function body, return result
|
paul@109 | 93 | Instance: call __call__ method, return result
|
paul@109 | 94 |
|
paul@109 | 95 | Structure Layout
|
paul@109 | 96 | ----------------
|
paul@109 | 97 |
|
paul@109 | 98 | A suitable structure layout might be something like this:
|
paul@109 | 99 |
|
paul@134 | 100 | Identifier Identifier Address Details Type Object ...
|
paul@109 | 101 |
|
paul@134 | 102 | 0 1 2 3 4 5 6
|
paul@134 | 103 | classcode attrcode invocation invocation __class__ attribute ...
|
paul@134 | 104 | reference #args, reference reference
|
paul@137 | 105 | defaults
|
paul@137 | 106 | reference
|
paul@109 | 107 |
|
paul@109 | 108 | Here, the classcode refers to the attribute lookup table for the object. Since
|
paul@109 | 109 | classes and instances share the same classcode, they might resemble the
|
paul@109 | 110 | following:
|
paul@109 | 111 |
|
paul@109 | 112 | Class C:
|
paul@109 | 113 |
|
paul@134 | 114 | 0 1 2 3 4 5 6
|
paul@134 | 115 | code for C attrcode __new__ __new__ class type attribute ...
|
paul@134 | 116 | for C reference #args, reference reference
|
paul@137 | 117 | defaults
|
paul@137 | 118 | reference
|
paul@109 | 119 |
|
paul@109 | 120 | Instance of C:
|
paul@109 | 121 |
|
paul@134 | 122 | 0 1 2 3 4 5 6
|
paul@134 | 123 | code for C attrcode C.__call__ C.__call__ class C attribute ...
|
paul@134 | 124 | for C reference #args, reference reference
|
paul@137 | 125 | (if exists) defaults
|
paul@137 | 126 | reference
|
paul@109 | 127 |
|
paul@109 | 128 | The __new__ reference would lead to code consisting of the following
|
paul@109 | 129 | instructions:
|
paul@109 | 130 |
|
paul@109 | 131 | create instance for C
|
paul@109 | 132 | call C.__init__(instance, ...)
|
paul@109 | 133 | return instance
|
paul@109 | 134 |
|
paul@109 | 135 | If C has a __call__ attribute, the invocation "slot" of C instances would
|
paul@182 | 136 | refer to the same thing as C.__call__. This "slot" has to be prepared when
|
paul@184 | 137 | creating instances, either by modifying the sequence of instructions used in,
|
paul@184 | 138 | amongst other places, the instantiator function, or by generating a template
|
paul@184 | 139 | instance whose details are copied when new instances are created.
|
paul@109 | 140 |
|
paul@109 | 141 | For functions, the same general layout applies:
|
paul@109 | 142 |
|
paul@109 | 143 | Function f:
|
paul@109 | 144 |
|
paul@134 | 145 | 0 1 2 3 4 5 6
|
paul@134 | 146 | code for attrcode code code class attribute ...
|
paul@134 | 147 | function for reference #args, function (default)
|
paul@137 | 148 | function defaults reference reference
|
paul@137 | 149 | reference
|
paul@109 | 150 |
|
paul@109 | 151 | Here, the code reference would lead to code for the function. Note that the
|
paul@109 | 152 | function locals are completely distinct from this structure and are not
|
paul@109 | 153 | comparable to attributes. Instead, attributes are reserved for default
|
paul@109 | 154 | parameter values.
|
paul@109 | 155 |
|
paul@109 | 156 | For modules, there is no meaningful invocation reference:
|
paul@109 | 157 |
|
paul@109 | 158 | Module m:
|
paul@109 | 159 |
|
paul@134 | 160 | 0 1 2 3 4 5 6
|
paul@134 | 161 | code for m attrcode (unused) (unused) module type attribute ...
|
paul@134 | 162 | for m reference (global)
|
paul@134 | 163 | reference
|
paul@109 | 164 |
|
paul@109 | 165 | Both classes and modules have code in their definitions, but this would be
|
paul@109 | 166 | generated in order and not referenced externally.
|
paul@109 | 167 |
|
paul@109 | 168 | Invocation Operation
|
paul@109 | 169 | --------------------
|
paul@109 | 170 |
|
paul@109 | 171 | Consequently, regardless of the object an invocation is always done as
|
paul@109 | 172 | follows:
|
paul@109 | 173 |
|
paul@109 | 174 | get invocation reference (at object+1)
|
paul@109 | 175 | jump to reference
|
paul@109 | 176 |
|
paul@109 | 177 | Additional preparation is necessary before the above code: positional
|
paul@109 | 178 | arguments must be saved to the parameter stack, and keyword arguments must be
|
paul@109 | 179 | resolved and saved to the appropriate position in the parameter stack.
|
paul@109 | 180 |
|
paul@109 | 181 | Attribute Operations
|
paul@109 | 182 | --------------------
|
paul@109 | 183 |
|
paul@109 | 184 | Attribute access needs to go through the attribute lookup table. Some
|
paul@109 | 185 | optimisations are possible and are described in the appropriate section.
|
paul@109 | 186 |
|
paul@109 | 187 | One important aspect of attribute access is the appropriate setting of the
|
paul@109 | 188 | context in the acquired attribute value. From the table describing the
|
paul@109 | 189 | acquisition of values, it is clear that the principal exception is that where
|
paul@109 | 190 | a class-originating attribute is accessed on an instance. Consequently, the
|
paul@109 | 191 | following algorithm could be employed once an attribute has been located:
|
paul@109 | 192 |
|
paul@109 | 193 | 1. If the attribute's context is a special value, indicating that it should
|
paul@109 | 194 | be replaced upon instance access, then proceed to the next step;
|
paul@109 | 195 | otherwise, acquire both the context and the object as they are.
|
paul@109 | 196 |
|
paul@109 | 197 | 2. If the accessor is an instance, use that as the value's context, acquiring
|
paul@109 | 198 | only the object from the attribute.
|
paul@109 | 199 |
|
paul@109 | 200 | Where accesses can be determined ahead of time (as discussed in the
|
paul@109 | 201 | optimisations section), the above algorithm may not necessarily be employed in
|
paul@109 | 202 | the generated code for some accesses.
|
paul@134 | 203 |
|
paul@134 | 204 | Instance/Class Compatibility
|
paul@134 | 205 | ----------------------------
|
paul@134 | 206 |
|
paul@134 | 207 | Although it would be possible to have a data structure mapping classes to
|
paul@134 | 208 | compatible classes, which in the case of context (or self argument)
|
paul@134 | 209 | suitability in invocations would involve a mapping from a class to itself plus
|
paul@134 | 210 | its descendants, the need to retain the key to such a data structure for each
|
paul@134 | 211 | class might introduce a noticeable overhead. Such a structure would
|
paul@134 | 212 | effectively be a matrix with each dimension indexed using the same sequence of
|
paul@134 | 213 | codes for each of the classes in a program.
|
paul@134 | 214 |
|
paul@181 | 215 | The current solution is to insert descendants as special attributes into the
|
paul@181 | 216 | object/attribute lookup table. This requires an extra key to be retained,
|
paul@181 | 217 | since each class must provide its own attribute code such that upon an
|
paul@181 | 218 | instance/class compatibility test, the code may be obtained and used in the
|
paul@181 | 219 | object table.
|