paul@199 | 1 | Concepts
|
paul@199 | 2 | ========
|
paul@199 | 3 |
|
paul@201 | 4 | This document describes the underlying concepts employed in micropython.
|
paul@201 | 5 |
|
paul@201 | 6 | * Namespaces and attribute definition
|
paul@570 | 7 | * Attribute usage observations
|
paul@584 | 8 | * Imports and circular import detection
|
paul@199 | 9 | * Contexts and values
|
paul@200 | 10 | * Tables, attributes and lookups
|
paul@199 | 11 | * Objects and structures
|
paul@200 | 12 | * Parameters and lookups
|
paul@200 | 13 | * Instantiation
|
paul@222 | 14 | * Register usage
|
paul@245 | 15 | * List and tuple representations
|
paul@199 | 16 |
|
paul@201 | 17 | Namespaces and Attribute Definition
|
paul@201 | 18 | ===================================
|
paul@201 | 19 |
|
paul@201 | 20 | Namespaces are any objects which can retain attributes.
|
paul@201 | 21 |
|
paul@201 | 22 | * Module attributes are defined either at the module level or by global
|
paul@201 | 23 | statements.
|
paul@201 | 24 | * Class attributes are defined only within class statements.
|
paul@534 | 25 | * Instance attributes are defined only by assignments to attributes of self
|
paul@534 | 26 | or tentatively as references to attributes of self.
|
paul@201 | 27 |
|
paul@201 | 28 | These restrictions apply because such attributes are thus explicitly declared,
|
paul@201 | 29 | permitting the use of tables (described below). Module and class attributes
|
paul@201 | 30 | can also be finalised in this way in order to permit certain optimisations.
|
paul@201 | 31 |
|
paul@243 | 32 | An additional restriction required for the current implementation of tables
|
paul@243 | 33 | (as described below) applies to class definitions: each class must be defined
|
paul@243 | 34 | using a unique name; repeated definition of classes having the same name is
|
paul@243 | 35 | thus not permitted. This restriction arises from the use of the "full name" of
|
paul@243 | 36 | a class as a key to the object table, where the full name is a qualified path
|
paul@243 | 37 | via the module hierarchy ending with the name of the class.
|
paul@243 | 38 |
|
paul@519 | 39 | Rebinding of attributes outside classes and modules can be allowed if
|
paul@519 | 40 | attribute usage observations are being used to detect such external
|
paul@519 | 41 | modifications to such objects. Without such observations, such rebinding
|
paul@519 | 42 | should be forbidden since apparently constant attributes might be modified in
|
paul@519 | 43 | a running program, but code may have been generated that provides specific
|
paul@519 | 44 | objects for those attributes under the assumption that they will not be
|
paul@519 | 45 | changed.
|
paul@519 | 46 |
|
paul@201 | 47 | See rejected.txt for complicating mechanisms which could be applied to
|
paul@201 | 48 | mitigate the effects of these restrictions on optimisations.
|
paul@201 | 49 |
|
paul@550 | 50 | Attribute Usage Observations
|
paul@550 | 51 | ============================
|
paul@550 | 52 |
|
paul@550 | 53 | Within a scope, a name may be used in conjunction with attribute names in
|
paul@550 | 54 | order to access attributes on objects referenced by the name. However, such
|
paul@550 | 55 | observations can only be regarded as reliable if the object referenced is not
|
paul@550 | 56 | changed independently by some other mechanism or activity.
|
paul@550 | 57 |
|
paul@550 | 58 | With conventional functions and methods, any locally defined names can be
|
paul@550 | 59 | considered private to that scope and thus immune to independent modification,
|
paul@550 | 60 | at least within reasonable features of the language. Although stack
|
paul@550 | 61 | modification may be possible, it seems appropriate to reject such features,
|
paul@550 | 62 | especially since they lend themselves to unmaintainable programs.
|
paul@550 | 63 |
|
paul@550 | 64 | For names defined during the initialisation of a class, since the class itself
|
paul@550 | 65 | cannot be referenced by name until its declaration has been completely
|
paul@550 | 66 | evaluated, no independent modification can occur from outside the class scope.
|
paul@550 | 67 |
|
paul@550 | 68 | For names defined during the initialisation of a module, global declarations
|
paul@550 | 69 | in functions permit the rebinding of global variables and since functions may
|
paul@550 | 70 | be invoked during module initialisation, independent modification can
|
paul@550 | 71 | potentially occur if any functions are called.
|
paul@550 | 72 |
|
paul@550 | 73 | Module globals can be accessed from other modules that can refer to a module
|
paul@550 | 74 | by its name. Initially, an import is required to make a module available for
|
paul@550 | 75 | modification, but there is no restriction on whether a module has been
|
paul@550 | 76 | completely imported (and thus defined) before an import statement can make it
|
paul@550 | 77 | available to other modules. Consider the following package root definition:
|
paul@550 | 78 |
|
paul@557 | 79 | # Module changed:
|
paul@550 | 80 | def f():
|
paul@557 | 81 | import changed.modifier
|
paul@550 | 82 | x = 123
|
paul@550 | 83 | f()
|
paul@550 | 84 |
|
paul@557 | 85 | # Module changed.modifier:
|
paul@557 | 86 | import changed
|
paul@557 | 87 | changed.x = 456
|
paul@550 | 88 |
|
paul@557 | 89 | Here, an import of changed will initially set x to 123, but then the function
|
paul@557 | 90 | f will be invoked and cause the changed.modifier module to be imported. Since
|
paul@557 | 91 | the changed module is already being imported, the import statement will not
|
paul@557 | 92 | try to perform the import operation again, but it will make the partially
|
paul@557 | 93 | defined module available for access. Thus, the changed.modifier module will
|
paul@557 | 94 | then set x to 456, and independent modification of the changed namespace will
|
paul@557 | 95 | have been performed.
|
paul@550 | 96 |
|
paul@550 | 97 | In conclusion, module globals cannot therefore be regarded as immune to
|
paul@550 | 98 | operations that would disrupt usage observations. Consequently, only locals
|
paul@550 | 99 | and class definition "locals" can be reliably employed in attribute usage
|
paul@550 | 100 | observations.
|
paul@550 | 101 |
|
paul@584 | 102 | Imports and Circular Import Detection
|
paul@584 | 103 | =====================================
|
paul@584 | 104 |
|
paul@584 | 105 | The matter of whether any given module is potentially modified by another
|
paul@584 | 106 | module before it has been completely imported can be addressed by separating
|
paul@584 | 107 | the import process into distinct phases:
|
paul@584 | 108 |
|
paul@584 | 109 | 1. Discovery/loading
|
paul@584 | 110 | 2. Parsing/structure processing
|
paul@584 | 111 | 3. Completion/code processing
|
paul@584 | 112 |
|
paul@584 | 113 | Upon discovering a module, a test is made to determine whether it is already
|
paul@584 | 114 | being imported; if not, it is then loaded and its structure inspected to
|
paul@584 | 115 | determine whether it may import other modules, which will then in turn be
|
paul@584 | 116 | discovered and loaded. Once no more modules can be loaded, they will then be
|
paul@584 | 117 | completed by undergoing the more thorough systematic processing of each
|
paul@584 | 118 | module's contents, defining program units and requesting the completion of
|
paul@584 | 119 | other modules when import statements are encountered.
|
paul@584 | 120 |
|
paul@584 | 121 | The motivation for such a multi-phase approach is to detect circular imports
|
paul@584 | 122 | in the structure processing phase before modules are populated and deductions
|
paul@584 | 123 | made about their objects' behaviour. Thus, globals belonging to a module known
|
paul@584 | 124 | to be imported in a circular fashion will already be regarded as potentially
|
paul@584 | 125 | modifiable by other modules and attribute usage observations will not be
|
paul@584 | 126 | recorded.
|
paul@584 | 127 |
|
paul@199 | 128 | Contexts and Values
|
paul@199 | 129 | ===================
|
paul@199 | 130 |
|
paul@199 | 131 | Values are used as the common reference representation in micropython: as
|
paul@199 | 132 | stored representations of attributes (of classes, instances, modules, and
|
paul@199 | 133 | other objects supporting attribute-like entities) as well as the stored values
|
paul@199 | 134 | associated with names in functions and methods.
|
paul@199 | 135 |
|
paul@199 | 136 | Unlike other implementations, micropython does not create things like bound
|
paul@199 | 137 | method objects for individual instances. Instead, all objects are referenced
|
paul@199 | 138 | using a context, reference pair:
|
paul@199 | 139 |
|
paul@199 | 140 | Value Layout
|
paul@199 | 141 | ------------
|
paul@199 | 142 |
|
paul@199 | 143 | 0 1
|
paul@199 | 144 | context object
|
paul@199 | 145 | reference reference
|
paul@199 | 146 |
|
paul@199 | 147 | Specific implementations might reverse this ordering for optimisation
|
paul@199 | 148 | purposes.
|
paul@199 | 149 |
|
paul@199 | 150 | Rationale
|
paul@199 | 151 | ---------
|
paul@199 | 152 |
|
paul@199 | 153 | To reduce the number of created objects whilst retaining the ability to
|
paul@199 | 154 | support bound method invocations. The context indicates the context in which
|
paul@199 | 155 | an invocation is performed, typically the owner of the method.
|
paul@199 | 156 |
|
paul@199 | 157 | Usage
|
paul@199 | 158 | -----
|
paul@199 | 159 |
|
paul@199 | 160 | The context may be inserted as the first argument when a value is involved in
|
paul@199 | 161 | an invocation. This argument may then be omitted from the invocation if its
|
paul@199 | 162 | usage is not appropriate.
|
paul@199 | 163 |
|
paul@199 | 164 | See invocation.txt for details.
|
paul@199 | 165 |
|
paul@237 | 166 | Context Value Types
|
paul@237 | 167 | -------------------
|
paul@237 | 168 |
|
paul@237 | 169 | The following types of context value exist:
|
paul@237 | 170 |
|
paul@237 | 171 | Type Usage Transformations
|
paul@237 | 172 | ---- ----- ---------------
|
paul@237 | 173 |
|
paul@237 | 174 | Replaceable With functions (not methods) May be replaced with an
|
paul@237 | 175 | instance or a class when a
|
paul@237 | 176 | value is stored on an
|
paul@237 | 177 | instance or class
|
paul@237 | 178 |
|
paul@237 | 179 | Placeholder With classes May not be replaced
|
paul@237 | 180 |
|
paul@237 | 181 | Instance With instances (and constants) May not be replaced
|
paul@237 | 182 | or functions as methods
|
paul@237 | 183 |
|
paul@237 | 184 | Class With functions as methods May be replaced when a
|
paul@237 | 185 | value is loaded from a
|
paul@237 | 186 | class attribute via an
|
paul@237 | 187 | instance
|
paul@237 | 188 |
|
paul@199 | 189 | Contexts in Acquired Values
|
paul@199 | 190 | ---------------------------
|
paul@199 | 191 |
|
paul@237 | 192 | There are four classes of instructions which provide values:
|
paul@199 | 193 |
|
paul@199 | 194 | Instruction Purpose Context Operations
|
paul@199 | 195 | ----------- ------- ------------------
|
paul@199 | 196 |
|
paul@237 | 197 | 1) LoadConst Load module, constant Use loaded object with itself
|
paul@237 | 198 | as context
|
paul@199 | 199 |
|
paul@237 | 200 | 2) LoadFunction Load function Combine replaceable context
|
paul@237 | 201 | with loaded object
|
paul@223 | 202 |
|
paul@237 | 203 | 3) LoadClass Load class Combine placeholder context
|
paul@237 | 204 | with loaded object
|
paul@237 | 205 |
|
paul@237 | 206 | 4) LoadAddress* Load attribute from Preserve or override stored
|
paul@201 | 207 | LoadAttr* class, module, context (as described in
|
paul@201 | 208 | instance assignment.txt)
|
paul@199 | 209 |
|
paul@199 | 210 | In order to comply with traditional Python behaviour, contexts may or may not
|
paul@199 | 211 | represent the object from which an attribute has been acquired.
|
paul@199 | 212 |
|
paul@199 | 213 | See assignment.txt for details.
|
paul@199 | 214 |
|
paul@199 | 215 | Contexts in Stored Values
|
paul@199 | 216 | -------------------------
|
paul@199 | 217 |
|
paul@223 | 218 | There are two classes of instruction for storing values:
|
paul@199 | 219 |
|
paul@223 | 220 | Instruction Purpose Context Operations
|
paul@223 | 221 | ----------- ------- ------------------
|
paul@199 | 222 |
|
paul@223 | 223 | 1) StoreAddress Store attribute in a Preserve context; note that no
|
paul@223 | 224 | known object test for class attribute
|
paul@223 | 225 | assignment should be necessary
|
paul@223 | 226 | since this instruction should only
|
paul@223 | 227 | be generated for module globals
|
paul@199 | 228 |
|
paul@223 | 229 | StoreAttr Store attribute in an Preserve context; note that no
|
paul@223 | 230 | instance test for class attribute
|
paul@223 | 231 | assignment should be necessary
|
paul@223 | 232 | since this instruction should only
|
paul@223 | 233 | be generated for self accesses
|
paul@199 | 234 |
|
paul@223 | 235 | StoreAttrIndex Store attribute in an Preserve context; since the index
|
paul@223 | 236 | unknown object lookup could yield a class
|
paul@223 | 237 | attribute, a test of the nature of
|
paul@223 | 238 | the nature of the structure is
|
paul@223 | 239 | necessary in order to prevent
|
paul@223 | 240 | assignments to classes
|
paul@199 | 241 |
|
paul@223 | 242 | 2) StoreAddressContext Store attribute in a Override context if appropriate;
|
paul@237 | 243 | known object if the value has a replaceable
|
paul@237 | 244 | context, permit the target to
|
paul@237 | 245 | take ownership of the value
|
paul@199 | 246 |
|
paul@199 | 247 | See assignment.txt for details.
|
paul@199 | 248 |
|
paul@200 | 249 | Tables, Attributes and Lookups
|
paul@200 | 250 | ==============================
|
paul@199 | 251 |
|
paul@199 | 252 | Attribute lookups, where the exact location of an object attribute is deduced,
|
paul@199 | 253 | are performed differently in micropython than in other implementations.
|
paul@199 | 254 | Instead of providing attribute dictionaries, in which attributes are found,
|
paul@199 | 255 | attributes are located at fixed places in object structures (described below)
|
paul@199 | 256 | and their locations are stored using a special representation known as a
|
paul@199 | 257 | table.
|
paul@199 | 258 |
|
paul@199 | 259 | For a given program, a table can be considered as being like a matrix mapping
|
paul@199 | 260 | classes to attribute names. For example:
|
paul@199 | 261 |
|
paul@199 | 262 | class A:
|
paul@200 | 263 | # instances have attributes x, y
|
paul@199 | 264 |
|
paul@199 | 265 | class B(A):
|
paul@200 | 266 | # introduces attribute z for instances
|
paul@199 | 267 |
|
paul@199 | 268 | class C:
|
paul@200 | 269 | # instances have attributes a, b, z
|
paul@199 | 270 |
|
paul@200 | 271 | This would provide the following table, referred to as an object table in the
|
paul@200 | 272 | context of classes and instances:
|
paul@199 | 273 |
|
paul@199 | 274 | Class/attr a b x y z
|
paul@199 | 275 |
|
paul@199 | 276 | A 1 2
|
paul@199 | 277 | B 1 2 3
|
paul@199 | 278 | C 1 2 3
|
paul@199 | 279 |
|
paul@199 | 280 | A limitation of this representation is that instance attributes may not shadow
|
paul@199 | 281 | class attributes: if an attribute with a given name is not defined on an
|
paul@199 | 282 | instance, an attribute with the same name cannot be provided by the class of
|
paul@401 | 283 | the instance or any superclass of the instance's class. This impacts the
|
paul@401 | 284 | provision of the __class__ attribute, as described below.
|
paul@199 | 285 |
|
paul@199 | 286 | The table can be compacted using a representation known as a displacement
|
paul@200 | 287 | list (referred to as an object list in this context):
|
paul@199 | 288 |
|
paul@199 | 289 | Classes with attribute offsets
|
paul@199 | 290 |
|
paul@199 | 291 | classcode A
|
paul@199 | 292 | attrcode a b x y z
|
paul@199 | 293 |
|
paul@199 | 294 | B
|
paul@199 | 295 | a b x y z
|
paul@199 | 296 |
|
paul@199 | 297 | C
|
paul@199 | 298 | a b x y z
|
paul@199 | 299 |
|
paul@199 | 300 | List . . 1 2 1 2 3 1 2 . . 3
|
paul@199 | 301 |
|
paul@199 | 302 | Here, the classcode refers to the offset in the list at which a class's
|
paul@199 | 303 | attributes are defined, whereas the attrcode defines the offset within a
|
paul@199 | 304 | region of attributes corresponding to a single attribute of a given name.
|
paul@199 | 305 |
|
paul@200 | 306 | Attribute Locations
|
paul@200 | 307 | -------------------
|
paul@200 | 308 |
|
paul@394 | 309 | The locations stored in table/list elements are generally for instance
|
paul@394 | 310 | attributes relative to the location of the instance, whereas those for class
|
paul@394 | 311 | attributes and module attributes are generally absolute addresses. Thus, each
|
paul@394 | 312 | occupied table cell has the following structure:
|
paul@242 | 313 |
|
paul@242 | 314 | attrcode, uses-absolute-address, address (or location)
|
paul@200 | 315 |
|
paul@247 | 316 | This could be given instead as follows:
|
paul@247 | 317 |
|
paul@247 | 318 | attrcode, is-class-or-module, location
|
paul@247 | 319 |
|
paul@247 | 320 | Since uses-absolute-address corresponds to is-class-or-module, and since there
|
paul@247 | 321 | is a need to test for classes and modules to prevent assignment to attributes
|
paul@247 | 322 | of such objects, this particular information is always required.
|
paul@247 | 323 |
|
paul@394 | 324 | The __class__ Attribute
|
paul@394 | 325 | -----------------------
|
paul@394 | 326 |
|
paul@401 | 327 | In Python 2.x, at least with new-style classes, instances have __class__
|
paul@401 | 328 | attributes which indicate the class from which they have been instantiated,
|
paul@401 | 329 | whereas classes have __class__ attributes which reference the type class.
|
paul@401 | 330 | With the object table, it is not possible to provide absolute addresses which
|
paul@401 | 331 | can be used for both classes and instances, since this would result in classes
|
paul@401 | 332 | and instances having the same class, and thus the class of a class would be
|
paul@401 | 333 | the class itself.
|
paul@397 | 334 |
|
paul@401 | 335 | One solution is to use object-relative values in the table so that referencing
|
paul@401 | 336 | the __class__ attribute of an instance produces a value which can be combined
|
paul@401 | 337 | with an instance's address to yield the address of the attribute, which itself
|
paul@401 | 338 | refers to the instance's class, whereas referencing the __class__ attribute of
|
paul@401 | 339 | a class produces a similar object-relative value that is combined with the
|
paul@401 | 340 | class's address to yield the address of the attribute, which itself refers to
|
paul@401 | 341 | the special type class.
|
paul@401 | 342 |
|
paul@401 | 343 | Obviously, the above solution requires both classes and instances to retain an
|
paul@401 | 344 | attribute location specifically to hold the value appropriate for each object
|
paul@401 | 345 | type, whereas a scheme which omits the __class__ attribute on classes would be
|
paul@401 | 346 | able to employ an absolute address in the table and maintain only a single
|
paul@401 | 347 | address to refer to the class for all instances. The only problem with not
|
paul@401 | 348 | providing a sensible __class__ attribute entry for classes would be the need
|
paul@401 | 349 | for special treatment of __class__ to prevent inappropriate consultation of
|
paul@401 | 350 | the table for classes.
|
paul@394 | 351 |
|
paul@247 | 352 | Comparing Tables as Matrices with Displacement Lists
|
paul@247 | 353 | ----------------------------------------------------
|
paul@247 | 354 |
|
paul@247 | 355 | Although displacement lists can provide reasonable levels of compaction for
|
paul@247 | 356 | attribute data, the element size is larger than that required for a simple
|
paul@247 | 357 | matrix: the attribute code (attrcode) need not be stored since each element
|
paul@247 | 358 | unambiguously refers to the availability of an attribute for a particular
|
paul@247 | 359 | class or instance of that class, and so the data at a given element need not
|
paul@247 | 360 | be tested for relevance to a given attribute access operation.
|
paul@247 | 361 |
|
paul@247 | 362 | Given a program with 20 object types and 100 attribute types, a matrix would
|
paul@247 | 363 | occupy the following amount of space:
|
paul@247 | 364 |
|
paul@247 | 365 | number of object types * number of attribute types * element size
|
paul@247 | 366 | = 20 * 100 * 1 (assuming that a single location is sufficient for an element)
|
paul@247 | 367 | = 2000
|
paul@247 | 368 |
|
paul@247 | 369 | In contrast, given a compaction to 40% of the matrix size (without considering
|
paul@247 | 370 | element size) in a displacement list, the amount of space would be as follows:
|
paul@247 | 371 |
|
paul@247 | 372 | number of elements * element size
|
paul@247 | 373 | = 40% * (20 * 100) * 2 (assuming that one additional location is required)
|
paul@247 | 374 | = 1600
|
paul@247 | 375 |
|
paul@247 | 376 | Consequently, the principal overhead of using a displacement list is likely to
|
paul@247 | 377 | be in the need to check element relevance when retrieving values from such a
|
paul@247 | 378 | list.
|
paul@247 | 379 |
|
paul@199 | 380 | Objects and Structures
|
paul@199 | 381 | ======================
|
paul@199 | 382 |
|
paul@199 | 383 | As well as references, micropython needs to have actual objects to refer to.
|
paul@199 | 384 | Since classes, functions and instances are all objects, it is desirable that
|
paul@199 | 385 | certain common features and operations are supported in the same way for all
|
paul@199 | 386 | of these things. To permit this, a common data structure format is used.
|
paul@199 | 387 |
|
paul@215 | 388 | Header.................................................... Attributes.................
|
paul@200 | 389 |
|
paul@401 | 390 | Identifier Identifier Address Identifier Size Object ...
|
paul@199 | 391 |
|
paul@401 | 392 | 0 1 2 3 4 5 6
|
paul@401 | 393 | classcode attrcode/ invocation funccode size attribute ...
|
paul@401 | 394 | instance reference reference
|
paul@215 | 395 | status
|
paul@199 | 396 |
|
paul@206 | 397 | Classcode
|
paul@206 | 398 | ---------
|
paul@206 | 399 |
|
paul@206 | 400 | Used in attribute lookup.
|
paul@206 | 401 |
|
paul@199 | 402 | Here, the classcode refers to the attribute lookup table for the object (as
|
paul@200 | 403 | described above). Classes and instances share the same classcode, and their
|
paul@200 | 404 | structures reflect this. Functions all belong to the same type and thus employ
|
paul@200 | 405 | the classcode for the function built-in type, whereas modules have distinct
|
paul@200 | 406 | types since they must support different sets of attributes.
|
paul@199 | 407 |
|
paul@206 | 408 | Attrcode
|
paul@206 | 409 | --------
|
paul@206 | 410 |
|
paul@206 | 411 | Used to test instances for membership of classes (or descendants of classes).
|
paul@206 | 412 |
|
paul@242 | 413 | Since, in traditional Python, classes are only ever instances of some generic
|
paul@242 | 414 | built-in type, support for testing such a relationship directly has been
|
paul@207 | 415 | removed and the attrcode is not specified for classes: the presence of an
|
paul@242 | 416 | attrcode indicates that a given object is an instance. In addition, support
|
paul@242 | 417 | has also been removed for testing modules in the same way, meaning that the
|
paul@242 | 418 | attrcode is also not specified for modules.
|
paul@206 | 419 |
|
paul@215 | 420 | See the "Testing Instance Compatibility with Classes (Attrcode)" section below
|
paul@215 | 421 | for details of attrcodes.
|
paul@214 | 422 |
|
paul@213 | 423 | Invocation Reference
|
paul@213 | 424 | --------------------
|
paul@213 | 425 |
|
paul@213 | 426 | Used when an object is called.
|
paul@213 | 427 |
|
paul@213 | 428 | This is the address of the code to be executed when an invocation is performed
|
paul@213 | 429 | on the object.
|
paul@213 | 430 |
|
paul@215 | 431 | Funccode
|
paul@215 | 432 | --------
|
paul@213 | 433 |
|
paul@215 | 434 | Used to look up argument positions by name.
|
paul@213 | 435 |
|
paul@215 | 436 | The strategy with keyword arguments in micropython is to attempt to position
|
paul@215 | 437 | such arguments in the invocation frame as it is being constructed.
|
paul@215 | 438 |
|
paul@215 | 439 | See the "Parameters and Lookups" section for more information.
|
paul@215 | 440 |
|
paul@215 | 441 | Size
|
paul@215 | 442 | ----
|
paul@215 | 443 |
|
paul@219 | 444 | Used to indicate the size of an object including attributes.
|
paul@213 | 445 |
|
paul@209 | 446 | Attributes
|
paul@209 | 447 | ----------
|
paul@209 | 448 |
|
paul@209 | 449 | For classes, modules and instances, the attributes in the structure correspond
|
paul@209 | 450 | to the attributes of each kind of object. For functions, however, the
|
paul@209 | 451 | attributes in the structure correspond to the default arguments for each
|
paul@209 | 452 | function, if any.
|
paul@209 | 453 |
|
paul@206 | 454 | Structure Types
|
paul@206 | 455 | ---------------
|
paul@206 | 456 |
|
paul@199 | 457 | Class C:
|
paul@199 | 458 |
|
paul@401 | 459 | 0 1 2 3 4 5 6
|
paul@401 | 460 | classcode (unused) __new__ funccode size attribute ...
|
paul@401 | 461 | for C reference for reference
|
paul@215 | 462 | instantiator
|
paul@199 | 463 |
|
paul@199 | 464 | Instance of C:
|
paul@199 | 465 |
|
paul@401 | 466 | 0 1 2 3 4 5 6
|
paul@401 | 467 | classcode attrcode C.__call__ funccode size attribute ...
|
paul@401 | 468 | for C for C reference for reference
|
paul@215 | 469 | (if exists) C.__call__
|
paul@199 | 470 |
|
paul@200 | 471 | Function f:
|
paul@199 | 472 |
|
paul@401 | 473 | 0 1 2 3 4 5 6
|
paul@401 | 474 | classcode attrcode code funccode size attribute ...
|
paul@401 | 475 | for for reference (default)
|
paul@401 | 476 | function function reference
|
paul@200 | 477 |
|
paul@200 | 478 | Module m:
|
paul@200 | 479 |
|
paul@401 | 480 | 0 1 2 3 4 5 6
|
paul@401 | 481 | classcode attrcode (unused) (unused) (unused) attribute ...
|
paul@401 | 482 | for m for m (global)
|
paul@401 | 483 | reference
|
paul@200 | 484 |
|
paul@200 | 485 | The __class__ Attribute
|
paul@200 | 486 | -----------------------
|
paul@200 | 487 |
|
paul@401 | 488 | All objects should support the __class__ attribute, and in most cases this is
|
paul@401 | 489 | done using the object table, yielding a common address for all instances of a
|
paul@401 | 490 | given class.
|
paul@401 | 491 |
|
paul@401 | 492 | Function: refers to the function class
|
paul@401 | 493 | Instance: refers to the class instantiated to make the object
|
paul@401 | 494 |
|
paul@401 | 495 | The object table cannot support two definitions simultaneously for both
|
paul@401 | 496 | instances and their classes. Consequently, __class__ access on classes must be
|
paul@401 | 497 | tested for and a special result returned.
|
paul@200 | 498 |
|
paul@200 | 499 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@401 | 500 |
|
paul@401 | 501 | For convenience, the first attribute of a class will be the common __class__
|
paul@401 | 502 | attribute for all its instances. As noted above, direct access to this
|
paul@401 | 503 | attribute will not be possible for classes, and a constant result will be
|
paul@401 | 504 | returned instead.
|
paul@200 | 505 |
|
paul@203 | 506 | Lists and Tuples
|
paul@203 | 507 | ----------------
|
paul@203 | 508 |
|
paul@203 | 509 | The built-in list and tuple sequences employ variable length structures using
|
paul@203 | 510 | the attribute locations to store their elements, where each element is a
|
paul@203 | 511 | reference to a separately stored object.
|
paul@203 | 512 |
|
paul@214 | 513 | Testing Instance Compatibility with Classes (Attrcode)
|
paul@200 | 514 | ------------------------------------------------------
|
paul@200 | 515 |
|
paul@200 | 516 | Although it would be possible to have a data structure mapping classes to
|
paul@200 | 517 | compatible classes, such as a matrix indicating the subclasses (or
|
paul@200 | 518 | superclasses) of each class, the need to retain the key to such a data
|
paul@200 | 519 | structure for each class might introduce a noticeable overhead.
|
paul@200 | 520 |
|
paul@200 | 521 | Instead of having a separate structure, descendant classes of each class are
|
paul@200 | 522 | inserted as special attributes into the object table. This requires an extra
|
paul@200 | 523 | key to be retained, since each class must provide its own attribute code such
|
paul@200 | 524 | that upon an instance/class compatibility test, the code may be obtained and
|
paul@200 | 525 | used in the object table.
|
paul@200 | 526 |
|
paul@200 | 527 | Invocation and Code References
|
paul@200 | 528 | ------------------------------
|
paul@200 | 529 |
|
paul@200 | 530 | Modules: there is no meaningful invocation reference since modules cannot be
|
paul@200 | 531 | explicitly called.
|
paul@200 | 532 |
|
paul@200 | 533 | Functions: a simple code reference is employed pointing to code implementing
|
paul@200 | 534 | the function. Note that the function locals are completely distinct from this
|
paul@200 | 535 | structure and are not comparable to attributes. Instead, attributes are
|
paul@200 | 536 | reserved for default parameter values, although they do not appear in the
|
paul@200 | 537 | object table described above, appearing instead in a separate parameter table
|
paul@200 | 538 | described below.
|
paul@200 | 539 |
|
paul@200 | 540 | Classes: given that classes must be invoked in order to create instances, a
|
paul@200 | 541 | reference must be provided in class structures. However, this reference does
|
paul@200 | 542 | not point directly at the __init__ method of the class. Instead, the
|
paul@200 | 543 | referenced code belongs to a special initialiser function, __new__, consisting
|
paul@200 | 544 | of the following instructions:
|
paul@200 | 545 |
|
paul@200 | 546 | create instance for C
|
paul@200 | 547 | call C.__init__(instance, ...)
|
paul@200 | 548 | return instance
|
paul@200 | 549 |
|
paul@200 | 550 | Instances: each instance employs a reference to any __call__ method defined in
|
paul@200 | 551 | the class hierarchy for the instance, thus maintaining its callable nature.
|
paul@200 | 552 |
|
paul@200 | 553 | Both classes and modules may contain code in their definitions - the former in
|
paul@200 | 554 | the "body" of the class, potentially defining attributes, and the latter as
|
paul@200 | 555 | the "top-level" code in the module, potentially defining attributes/globals -
|
paul@200 | 556 | but this code is not associated with any invocation target. It is thus
|
paul@200 | 557 | generated in order of appearance and is not referenced externally.
|
paul@200 | 558 |
|
paul@200 | 559 | Invocation Operation
|
paul@200 | 560 | --------------------
|
paul@200 | 561 |
|
paul@200 | 562 | Consequently, regardless of the object an invocation is always done as
|
paul@200 | 563 | follows:
|
paul@200 | 564 |
|
paul@200 | 565 | get invocation reference from the header
|
paul@200 | 566 | jump to reference
|
paul@200 | 567 |
|
paul@200 | 568 | Additional preparation is necessary before the above code: positional
|
paul@200 | 569 | arguments must be saved in the invocation frame, and keyword arguments must be
|
paul@200 | 570 | resolved and saved to the appropriate position in the invocation frame.
|
paul@200 | 571 |
|
paul@200 | 572 | See invocation.txt for details.
|
paul@200 | 573 |
|
paul@200 | 574 | Parameters and Lookups
|
paul@200 | 575 | ======================
|
paul@200 | 576 |
|
paul@200 | 577 | Since Python supports keyword arguments when making invocations, it becomes
|
paul@200 | 578 | necessary to record the parameter names associated with each function or
|
paul@200 | 579 | method. Just as object tables record attributes positions on classes and
|
paul@200 | 580 | instances, parameter tables record parameter positions in function or method
|
paul@200 | 581 | parameter lists.
|
paul@200 | 582 |
|
paul@200 | 583 | For a given program, a parameter table can be considered as being like a
|
paul@200 | 584 | matrix mapping functions/methods to parameter names. For example:
|
paul@200 | 585 |
|
paul@200 | 586 | def f(x, y, z):
|
paul@200 | 587 | pass
|
paul@200 | 588 |
|
paul@200 | 589 | def g(a, b, c):
|
paul@200 | 590 | pass
|
paul@200 | 591 |
|
paul@200 | 592 | def h(a, x):
|
paul@200 | 593 | pass
|
paul@200 | 594 |
|
paul@200 | 595 | This would provide the following table, referred to as a parameter table in
|
paul@200 | 596 | the context of functions and methods:
|
paul@200 | 597 |
|
paul@200 | 598 | Function/param a b c x y z
|
paul@200 | 599 |
|
paul@200 | 600 | f 1 2 3
|
paul@200 | 601 | g 1 2 3
|
paul@200 | 602 | h 1 2
|
paul@200 | 603 |
|
paul@233 | 604 | Confusion can occur when functions are adopted as methods, since the context
|
paul@233 | 605 | then occupies the first slot in the invocation frame:
|
paul@233 | 606 |
|
paul@233 | 607 | def f(x, y, z):
|
paul@233 | 608 | pass
|
paul@233 | 609 |
|
paul@233 | 610 | f(x=1, y=2, z=3) -> f(<context>, 1, 2, 3)
|
paul@233 | 611 | -> f(1, 2, 3)
|
paul@233 | 612 |
|
paul@233 | 613 | class C:
|
paul@233 | 614 | f = f
|
paul@233 | 615 |
|
paul@233 | 616 | def g(x, y, z):
|
paul@233 | 617 | pass
|
paul@233 | 618 |
|
paul@233 | 619 | c = C()
|
paul@233 | 620 |
|
paul@233 | 621 | c.f(y=2, z=3) -> f(<context>, 2, 3)
|
paul@233 | 622 | c.g(y=2, z=3) -> C.g(<context>, 2, 3)
|
paul@233 | 623 |
|
paul@200 | 624 | Just as with parameter tables, a displacement list can be prepared from a
|
paul@200 | 625 | parameter table:
|
paul@200 | 626 |
|
paul@200 | 627 | Functions with parameter (attribute) offsets
|
paul@200 | 628 |
|
paul@200 | 629 | funccode f
|
paul@200 | 630 | attrcode a b c x y z
|
paul@200 | 631 |
|
paul@200 | 632 | g
|
paul@200 | 633 | a b c x y z
|
paul@200 | 634 |
|
paul@200 | 635 | h
|
paul@200 | 636 | a b c x y z
|
paul@200 | 637 |
|
paul@200 | 638 | List . . . 1 2 3 1 2 3 1 . . 2 . .
|
paul@200 | 639 |
|
paul@200 | 640 | Here, the funccode refers to the offset in the list at which a function's
|
paul@200 | 641 | parameters are defined, whereas the attrcode defines the offset within a
|
paul@200 | 642 | region of attributes corresponding to a single parameter of a given name.
|
paul@200 | 643 |
|
paul@200 | 644 | Instantiation
|
paul@200 | 645 | =============
|
paul@200 | 646 |
|
paul@200 | 647 | When instantiating classes, memory must be reserved for the header of the
|
paul@200 | 648 | resulting instance, along with locations for the attributes of the instance.
|
paul@200 | 649 | Since the instance header contains data common to all instances of a class, a
|
paul@200 | 650 | template header is copied to the start of the newly reserved memory region.
|
paul@222 | 651 |
|
paul@222 | 652 | Register Usage
|
paul@222 | 653 | ==============
|
paul@222 | 654 |
|
paul@222 | 655 | During code generation, much of the evaluation produces results which are
|
paul@450 | 656 | implicitly recorded in the "active value" or "working" register, and various
|
paul@450 | 657 | instructions will consume this active value. In addition, some instructions
|
paul@450 | 658 | will consume a separate "active source value" from a register, typically those
|
paul@450 | 659 | which are assigning the result of an expression to an assignment target.
|
paul@222 | 660 |
|
paul@222 | 661 | Since values often need to be retained for later use, a set of temporary
|
paul@222 | 662 | storage locations are typically employed. However, optimisations may reduce
|
paul@222 | 663 | the need to use such temporary storage where instructions which provide the
|
paul@450 | 664 | "active value" can be re-executed and will produce the same result. Whether
|
paul@450 | 665 | re-use of instructions is possible or not, values still need to be loaded into
|
paul@450 | 666 | a "source" register to be accessed by an assignment instruction.
|
paul@450 | 667 |
|
paul@450 | 668 | RSVP instructions generally have the notion of working, target and source
|
paul@450 | 669 | registers (see registers.txt). These register "roles" are independent from the
|
paul@450 | 670 | actual registers defined for the RSVP machine itself, even though the naming
|
paul@450 | 671 | is similar. Generally, instructions do regard the RSVP "working" registers as
|
paul@450 | 672 | the class of register in the "working" role, although some occurrences of
|
paul@450 | 673 | instruction usage may employ other registers (such as the "result" registers)
|
paul@450 | 674 | and thus take advantage of the generality of the RSVP implementation of such
|
paul@450 | 675 | instructions.
|
paul@245 | 676 |
|
paul@245 | 677 | List and Tuple Representations
|
paul@245 | 678 | ==============================
|
paul@245 | 679 |
|
paul@245 | 680 | Since tuples have a fixed size, the representation of a tuple instance is
|
paul@245 | 681 | merely a header describing the size of the entire object, together with a
|
paul@245 | 682 | sequence of references to the object "stored" at each position in the
|
paul@245 | 683 | structure. Such references consist of the usual context and reference pair.
|
paul@245 | 684 |
|
paul@245 | 685 | Lists, however, have a variable size and must be accessible via an unchanging
|
paul@245 | 686 | location even as more memory is allocated elsewhere to accommodate the
|
paul@245 | 687 | contents of the list. Consequently, the representation must resemble the
|
paul@245 | 688 | following:
|
paul@245 | 689 |
|
paul@245 | 690 | Structure header for list (size == header plus special attribute)
|
paul@245 | 691 | Special attribute referencing the underlying sequence
|
paul@245 | 692 |
|
paul@245 | 693 | The underlying sequence has a fixed size, like a tuple, but may contain fewer
|
paul@245 | 694 | elements than the size of the sequence permits:
|
paul@245 | 695 |
|
paul@245 | 696 | Special header indicating the current size and allocated size
|
paul@245 | 697 | Element
|
paul@245 | 698 | ... <-- current size
|
paul@245 | 699 | (Unused space)
|
paul@245 | 700 | ... <-- allocated size
|
paul@245 | 701 |
|
paul@245 | 702 | This representation permits the allocation of a new sequence when space is
|
paul@245 | 703 | exhausted in an existing sequence, with the new sequence address stored in the
|
paul@245 | 704 | main list structure. Since access to the contents of the list must go through
|
paul@245 | 705 | the main list structure, underlying allocation activities may take place
|
paul@245 | 706 | without the users of a list having to be aware of such activities.
|