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