paul@15 | 1 | Namespace Definition
|
paul@15 | 2 | ====================
|
paul@15 | 3 |
|
paul@15 | 4 | Module attributes are defined either at the module level or by global
|
paul@15 | 5 | statements.
|
paul@15 | 6 |
|
paul@15 | 7 | Class attributes are defined only within class statements.
|
paul@15 | 8 |
|
paul@15 | 9 | Instance attributes are defined only by assignments to attributes of self
|
paul@15 | 10 | within __init__ methods.
|
paul@15 | 11 |
|
paul@11 | 12 | Data Structures
|
paul@11 | 13 | ===============
|
paul@11 | 14 |
|
paul@11 | 15 | Since classes, functions and instances are all "objects", each must support
|
paul@11 | 16 | certain features and operations in the same way.
|
paul@11 | 17 |
|
paul@11 | 18 | The __class__ Attribute
|
paul@11 | 19 | -----------------------
|
paul@11 | 20 |
|
paul@11 | 21 | All objects support the __class__ attribute:
|
paul@11 | 22 |
|
paul@11 | 23 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@11 | 24 | Function: refers to the function class
|
paul@11 | 25 | Instance: refers to the class instantiated to make the object
|
paul@11 | 26 |
|
paul@11 | 27 | Invocation
|
paul@11 | 28 | ----------
|
paul@11 | 29 |
|
paul@11 | 30 | The following actions need to be supported:
|
paul@11 | 31 |
|
paul@11 | 32 | Class: create instance, call __init__ with instance, return object
|
paul@11 | 33 | Function: call function body, return result
|
paul@11 | 34 | Instance: call __call__ method, return result
|
paul@11 | 35 |
|
paul@11 | 36 | Structure Layout
|
paul@11 | 37 | ----------------
|
paul@11 | 38 |
|
paul@11 | 39 | A suitable structure layout might be something like this:
|
paul@11 | 40 |
|
paul@11 | 41 | 0 1 2 3 4
|
paul@11 | 42 | classcode invocation __class__ attribute ...
|
paul@11 | 43 | reference reference reference
|
paul@11 | 44 |
|
paul@11 | 45 | Here, the classcode refers to the attribute lookup table for the object. Since
|
paul@11 | 46 | classes and instances share the same classcode, they might resemble the
|
paul@11 | 47 | following:
|
paul@11 | 48 |
|
paul@11 | 49 | Class C:
|
paul@11 | 50 |
|
paul@11 | 51 | 0 1 2 3 4
|
paul@11 | 52 | code for C __new__ class type attribute ...
|
paul@11 | 53 | reference reference reference
|
paul@11 | 54 |
|
paul@11 | 55 | Instance of C:
|
paul@11 | 56 |
|
paul@11 | 57 | 0 1 2 3 4
|
paul@11 | 58 | code for C C.__call__ class C attribute ...
|
paul@11 | 59 | reference reference reference
|
paul@11 | 60 | (if exists)
|
paul@11 | 61 |
|
paul@11 | 62 | The __new__ reference would lead to code consisting of the following
|
paul@11 | 63 | instructions:
|
paul@11 | 64 |
|
paul@11 | 65 | create instance for C
|
paul@11 | 66 | call C.__init__(instance, ...)
|
paul@11 | 67 | return instance
|
paul@11 | 68 |
|
paul@11 | 69 | If C has a __call__ attribute, the invocation "slot" of C instances would
|
paul@11 | 70 | refer to the same thing as C.__call__.
|
paul@11 | 71 |
|
paul@11 | 72 | For functions, the same general layout applies:
|
paul@11 | 73 |
|
paul@11 | 74 | Function f:
|
paul@11 | 75 |
|
paul@11 | 76 | 0 1 2 3 4
|
paul@11 | 77 | code for code class attribute ...
|
paul@11 | 78 | function reference function reference
|
paul@11 | 79 | reference
|
paul@11 | 80 |
|
paul@37 | 81 | Here, the code reference would lead to code for the function. Note that the
|
paul@37 | 82 | function locals are completely distinct from this structure and are not
|
paul@37 | 83 | comparable to attributes.
|
paul@37 | 84 |
|
paul@37 | 85 | For modules, the invocation reference would point to the start of the
|
paul@37 | 86 | module's code:
|
paul@37 | 87 |
|
paul@37 | 88 | Module m:
|
paul@37 | 89 |
|
paul@37 | 90 | 0 1 2 3 4
|
paul@37 | 91 | code for m code module type attribute ...
|
paul@37 | 92 | reference reference (global)
|
paul@37 | 93 | reference
|
paul@11 | 94 |
|
paul@11 | 95 | Invocation Operation
|
paul@11 | 96 | --------------------
|
paul@11 | 97 |
|
paul@11 | 98 | Consequently, regardless of the object an invocation is always done as
|
paul@11 | 99 | follows:
|
paul@11 | 100 |
|
paul@11 | 101 | get invocation reference (at object+1)
|
paul@11 | 102 | jump to reference
|
paul@11 | 103 |
|
paul@11 | 104 | Additional preparation is necessary before the above code: positional
|
paul@11 | 105 | arguments must be saved to the parameter stack, and keyword arguments must be
|
paul@11 | 106 | resolved and saved to the appropriate position in the parameter stack.
|
paul@11 | 107 |
|
paul@11 | 108 | Attribute Operations
|
paul@11 | 109 | --------------------
|
paul@11 | 110 |
|
paul@11 | 111 | Attribute access needs to go through the attribute lookup table.
|
paul@21 | 112 |
|
paul@21 | 113 | Instruction Evaluation Model
|
paul@21 | 114 | ============================
|
paul@21 | 115 |
|
paul@21 | 116 | Programs use a value stack where evaluated instructions may save their
|
paul@21 | 117 | results. A value stack pointer indicates the top of this stack. In addition, a
|
paul@21 | 118 | separate stack is used to record the invocation frames. All stack pointers
|
paul@21 | 119 | refer to the next address to be used by the stack, not the address of the
|
paul@21 | 120 | uppermost element.
|
paul@21 | 121 |
|
paul@21 | 122 | Frame Stack Value Stack
|
paul@21 | 123 | ----------- ----------- Address of Callable
|
paul@21 | 124 | -------------------
|
paul@21 | 125 | previous ...
|
paul@21 | 126 | current ------> callable -----> identifier
|
paul@21 | 127 | arg1 reference to code
|
paul@21 | 128 | arg2
|
paul@21 | 129 | arg3
|
paul@21 | 130 | local4
|
paul@21 | 131 | local5
|
paul@21 | 132 | ...
|
paul@21 | 133 |
|
paul@21 | 134 | Loading local names is a matter of performing frame-relative accesses to the
|
paul@21 | 135 | value stack.
|
paul@21 | 136 |
|
paul@21 | 137 | Invocations and Argument Evaluation
|
paul@21 | 138 | -----------------------------------
|
paul@21 | 139 |
|
paul@21 | 140 | When preparing for an invocation, the caller first sets the invocation frame
|
paul@21 | 141 | pointer. Then, positional arguments are added to the stack such that the first
|
paul@21 | 142 | argument positions are filled. A number of stack locations for the remaining
|
paul@21 | 143 | arguments specified in the program are then reserved. The names of keyword
|
paul@21 | 144 | arguments are used (in the form of table index values) to consult the
|
paul@21 | 145 | parameter table and to find the location in which such arguments are to be
|
paul@21 | 146 | stored.
|
paul@21 | 147 |
|
paul@21 | 148 | fn(a, b, d=1, e=2, c=3) -> fn(a, b, c, d, e)
|
paul@21 | 149 |
|
paul@21 | 150 | Value Stack
|
paul@21 | 151 | -----------
|
paul@21 | 152 |
|
paul@21 | 153 | ... ... ... ...
|
paul@21 | 154 | fn fn fn fn
|
paul@21 | 155 | a a a a
|
paul@21 | 156 | b b b b
|
paul@21 | 157 | ___ ___ ___ --> 3
|
paul@21 | 158 | ___ --> 1 1 | 1
|
paul@21 | 159 | ___ | ___ --> 2 | 2
|
paul@21 | 160 | 1 ----------- 2 ----------- 3 -----------
|
paul@21 | 161 |
|
paul@21 | 162 | Conceptually, the frame can be considered as a collection of attributes, as
|
paul@21 | 163 | seen in other kinds of structures:
|
paul@21 | 164 |
|
paul@21 | 165 | Frame for invocation of fn:
|
paul@21 | 166 |
|
paul@21 | 167 | 0 1 2 3 4 5
|
paul@21 | 168 | code a b c d e
|
paul@21 | 169 | reference
|
paul@21 | 170 |
|
paul@21 | 171 | However, where arguments are specified positionally, such "attributes" are not
|
paul@21 | 172 | set using a comparable approach to that employed with other structures.
|
paul@21 | 173 | Keyword arguments are set using an attribute-like mechanism, though, where the
|
paul@21 | 174 | position of each argument discovered using the parameter table.
|
paul@21 | 175 |
|
paul@21 | 176 | Tuples, Frames and Allocation
|
paul@21 | 177 | -----------------------------
|
paul@21 | 178 |
|
paul@21 | 179 | Using the approach where arguments are treated like attributes in some kind of
|
paul@21 | 180 | structure, we could choose to allocate frames in places other than a stack.
|
paul@21 | 181 | This would produce something somewhat similar to a plain tuple object.
|
paul@23 | 182 |
|
paul@23 | 183 | Optimisations
|
paul@23 | 184 | =============
|
paul@23 | 185 |
|
paul@29 | 186 | Some optimisations around constant objects might be possible; these depend on
|
paul@29 | 187 | the following:
|
paul@29 | 188 |
|
paul@29 | 189 | * Reliable tracking of assignments: where assignment operations occur, the
|
paul@29 | 190 | target of the assignment should be determined if any hope of optimisation
|
paul@29 | 191 | is to be maintained. Where no guarantees can be made about the target of
|
paul@29 | 192 | an assignment, no assignment-related information should be written to
|
paul@29 | 193 | potential targets.
|
paul@29 | 194 |
|
paul@29 | 195 | * Objects acting as "containers" of attributes must be regarded as "safe":
|
paul@29 | 196 | where assignments are recorded as occurring on an attribute, it must be
|
paul@29 | 197 | guaranteed that no other unforeseen ways exist to assign to such
|
paul@29 | 198 | attributes.
|
paul@29 | 199 |
|
paul@29 | 200 | The discussion below presents certain rules which must be imposed to uphold
|
paul@29 | 201 | the above requirements.
|
paul@29 | 202 |
|
paul@30 | 203 | Safe Containers
|
paul@30 | 204 | ---------------
|
paul@28 | 205 |
|
paul@23 | 206 | Where attributes of modules, classes and instances are only set once and are
|
paul@23 | 207 | effectively constant, it should be possible to circumvent the attribute lookup
|
paul@28 | 208 | mechanism and to directly reference the attribute value. This technique may
|
paul@30 | 209 | only be considered applicable for the following "container" objects, subject
|
paul@30 | 210 | to the noted constraints:
|
paul@28 | 211 |
|
paul@30 | 212 | 1. For modules, "safety" is enforced by ensuring that assignments to module
|
paul@30 | 213 | attributes are only permitted within the module itself either at the
|
paul@30 | 214 | top-level or via names declared as globals. Thus, the following would not
|
paul@30 | 215 | be permitted:
|
paul@28 | 216 |
|
paul@28 | 217 | another_module.this_module.attr = value
|
paul@28 | 218 |
|
paul@29 | 219 | In the above, this_module is a reference to the current module.
|
paul@28 | 220 |
|
paul@30 | 221 | 2. For classes, "safety" is enforced by ensuring that assignments to class
|
paul@30 | 222 | attributes are only permitted within the class definition, outside
|
paul@30 | 223 | methods. This would mean that classes would be "sealed" at definition time
|
paul@30 | 224 | (like functions).
|
paul@28 | 225 |
|
paul@28 | 226 | Unlike the property of function locals that they may only sensibly be accessed
|
paul@28 | 227 | within the function in which they reside, these cases demand additional
|
paul@28 | 228 | controls or assumptions on or about access to the stored data. Meanwhile, it
|
paul@28 | 229 | would be difficult to detect eligible attributes on arbitrary instances due to
|
paul@28 | 230 | the need for some kind of type inference or abstract execution.
|
paul@28 | 231 |
|
paul@30 | 232 | Constant Attributes
|
paul@30 | 233 | -------------------
|
paul@30 | 234 |
|
paul@30 | 235 | When accessed via "safe containers", as described above, any attribute with
|
paul@30 | 236 | only one recorded assignment on it can be considered a constant attribute and
|
paul@30 | 237 | this eligible for optimisation, the consequence of which would be the
|
paul@30 | 238 | replacement of a LoadAttrIndex instruction (which needs to look up an
|
paul@30 | 239 | attribute using the run-time details of the "container" and the compile-time
|
paul@30 | 240 | details of the attribute) with a LoadAttr instruction.
|
paul@30 | 241 |
|
paul@30 | 242 | However, some restrictions exist on assignment operations which may be
|
paul@30 | 243 | regarded to cause only one assignment in the lifetime of a program:
|
paul@30 | 244 |
|
paul@30 | 245 | 1. For module attributes, only assignments at the top-level outside loop
|
paul@30 | 246 | statements can be reliably assumed to cause only a single assignment.
|
paul@30 | 247 |
|
paul@30 | 248 | 2. For class attributes, only assignments at the top-level within class
|
paul@30 | 249 | definitions and outside loop statements can be reliably assumed to cause
|
paul@30 | 250 | only a single assignment.
|
paul@30 | 251 |
|
paul@30 | 252 | All assignments satisfying the "safe container" requirements, but not the
|
paul@30 | 253 | requirements immediately above, should each be recorded as causing at least
|
paul@30 | 254 | one assignment.
|
paul@28 | 255 |
|
paul@29 | 256 | Additional Controls
|
paul@29 | 257 | -------------------
|
paul@29 | 258 |
|
paul@29 | 259 | For the above cases for "container" objects, the following controls would need
|
paul@29 | 260 | to apply:
|
paul@29 | 261 |
|
paul@29 | 262 | 1. Modules would need to be immutable after initialisation. However, during
|
paul@29 | 263 | initialisation, there remains a possibility of another module attempting
|
paul@29 | 264 | to access the original module. For example, if ppp/__init__.py contained
|
paul@29 | 265 | the following...
|
paul@29 | 266 |
|
paul@29 | 267 | x = 1
|
paul@29 | 268 | import ppp.qqq
|
paul@29 | 269 | print x
|
paul@29 | 270 |
|
paul@29 | 271 | ...and if ppp/qqq.py contained the following...
|
paul@29 | 272 |
|
paul@29 | 273 | import ppp
|
paul@29 | 274 | ppp.x = 2
|
paul@29 | 275 |
|
paul@29 | 276 | ...then the value 2 would be printed. Since modules are objects which are
|
paul@29 | 277 | registered globally in a program, it would be possible to set attributes
|
paul@29 | 278 | in the above way.
|
paul@29 | 279 |
|
paul@29 | 280 | 2. Classes would need to be immutable after initialisation. However, since
|
paul@29 | 281 | classes are objects, any reference to a class after initialisation could
|
paul@29 | 282 | be used to set attributes on the class.
|
paul@29 | 283 |
|
paul@29 | 284 | Solutions:
|
paul@29 | 285 |
|
paul@29 | 286 | 1. Insist on global scope for module attribute assignments.
|
paul@29 | 287 |
|
paul@29 | 288 | 2. Insist on local scope within classes.
|
paul@29 | 289 |
|
paul@29 | 290 | Both of the above measures need to be enforced at run-time, since an arbitrary
|
paul@29 | 291 | attribute assignment could be attempted on any kind of object, yet to uphold
|
paul@29 | 292 | the properties of "safe containers", attempts to change attributes of such
|
paul@29 | 293 | objects should be denied. Since foreseen attribute assignment operations have
|
paul@29 | 294 | certain properties detectable at compile-time, it could be appropriate to
|
paul@29 | 295 | generate special instructions (or modified instructions) during the
|
paul@29 | 296 | initialisation of modules and classes for such foreseen assignments, whilst
|
paul@29 | 297 | employing normal attribute assignment operations in all other cases. Indeed,
|
paul@29 | 298 | the StoreAttr instruction, which is used to set attributes in "safe
|
paul@29 | 299 | containers" would be used exclusively for this purpose; the StoreAttrIndex
|
paul@29 | 300 | instruction would be used exclusively for all other attribute assignments.
|
paul@29 | 301 |
|
paul@28 | 302 | Constant Attribute Values
|
paul@28 | 303 | -------------------------
|
paul@28 | 304 |
|
paul@29 | 305 | Where an attribute value is itself regarded as constant, is a "safe container"
|
paul@29 | 306 | and is used in an operation accessing its own attributes, the value can be
|
paul@29 | 307 | directly inspected for optimisations or employed in the generated code. For
|
paul@29 | 308 | the attribute values themselves, only objects of a constant nature may be
|
paul@28 | 309 | considered suitable for this particular optimisation:
|
paul@28 | 310 |
|
paul@28 | 311 | * Classes
|
paul@28 | 312 | * Modules
|
paul@28 | 313 | * Instances defined as constant literals
|
paul@28 | 314 |
|
paul@28 | 315 | This is because arbitrary objects (such as most instances) have no
|
paul@28 | 316 | well-defined form before run-time and cannot be investigated further at
|
paul@28 | 317 | compile-time or have a representation inserted into the generated code.
|
paul@29 | 318 |
|
paul@29 | 319 | Class Attributes and Access via Instances
|
paul@29 | 320 | -----------------------------------------
|
paul@29 | 321 |
|
paul@29 | 322 | Unlike module attributes, class attributes can be accessed in a number of
|
paul@29 | 323 | different ways:
|
paul@29 | 324 |
|
paul@29 | 325 | * Using the class itself:
|
paul@29 | 326 |
|
paul@29 | 327 | C.x = 123
|
paul@29 | 328 | cls = C; cls.x = 234
|
paul@29 | 329 |
|
paul@29 | 330 | * Using a subclass of the class (for reading attributes):
|
paul@29 | 331 |
|
paul@29 | 332 | class D(C):
|
paul@29 | 333 | pass
|
paul@29 | 334 | D.x # setting D.x would populate D, not C
|
paul@29 | 335 |
|
paul@29 | 336 | * Using instances of the class or a subclass of the class (for reading
|
paul@29 | 337 | attributes):
|
paul@29 | 338 |
|
paul@29 | 339 | c = C()
|
paul@29 | 340 | c.x # setting c.x would populate c, not C
|
paul@29 | 341 |
|
paul@29 | 342 | Since assignments are only achieved using direct references to the class, and
|
paul@29 | 343 | since class attributes should be defined only within the class initialisation
|
paul@29 | 344 | process, the properties of class attributes should be consistent with those
|
paul@29 | 345 | desired.
|
paul@29 | 346 |
|
paul@29 | 347 | Method Access via Instances
|
paul@29 | 348 | ---------------------------
|
paul@29 | 349 |
|
paul@29 | 350 | It is desirable to optimise method access, even though most method calls are
|
paul@29 | 351 | likely to occur via instances. It is possible, given the properties of methods
|
paul@29 | 352 | as class attributes to investigate the kind of instance that the self
|
paul@29 | 353 | parameter/local refers to within each method: it should be an instance either
|
paul@29 | 354 | of the class in which the method is defined or a compatible class, although
|
paul@29 | 355 | situations exist where this might not be the case:
|
paul@29 | 356 |
|
paul@29 | 357 | * Explicit invocation of a method:
|
paul@29 | 358 |
|
paul@29 | 359 | d = D() # D is not related to C
|
paul@29 | 360 | C.f(d) # calling f(self) in C
|
paul@29 | 361 |
|
paul@30 | 362 | If blatant usage of incompatible instances were somehow disallowed, it would
|
paul@30 | 363 | still be necessary to investigate the properties of an instance's class and
|
paul@30 | 364 | its relationship with other classes. Consider the following example:
|
paul@30 | 365 |
|
paul@30 | 366 | class A:
|
paul@30 | 367 | def f(self): ...
|
paul@30 | 368 |
|
paul@30 | 369 | class B:
|
paul@30 | 370 | def f(self): ...
|
paul@30 | 371 | def g(self):
|
paul@30 | 372 | self.f()
|
paul@30 | 373 |
|
paul@30 | 374 | class C(A, B):
|
paul@30 | 375 | pass
|
paul@30 | 376 |
|
paul@30 | 377 | Here, instances of B passed into the method B.g could be assumed to provide
|
paul@30 | 378 | access to B.f when self.f is resolved at compile-time. However, instances of C
|
paul@30 | 379 | passed into B.g would instead provide access to A.f when self.f is resolved at
|
paul@30 | 380 | compile-time (since the method resolution order is C, A, B instead of just B).
|
paul@30 | 381 |
|
paul@30 | 382 | One solution might be to specialise methods for each instance type, but this
|
paul@30 | 383 | could be costly. Another less ambitious solution might only involve the
|
paul@30 | 384 | optimisation of such internal method calls if an unambiguous target can be
|
paul@30 | 385 | resolved.
|
paul@30 | 386 |
|
paul@29 | 387 | Optimising Function Invocations
|
paul@29 | 388 | -------------------------------
|
paul@29 | 389 |
|
paul@29 | 390 | Where an attribute value is itself regarded as constant and is a function,
|
paul@29 | 391 | knowledge about the parameters of the function can be employed to optimise the
|
paul@29 | 392 | preparation of the invocation frame.
|