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