micropython

docs/assignment.txt

746:59be62d8e75f
2013-11-25 Paul Boddie Fixed loadconstant usage to include both the constant and the new context. syspython-as-target
     1 Object contexts:
     2 
     3   Object      Context
     4   ------      -------
     5   function    overridable
     6   method      defining/originating class
     7   class       null
     8   instance    self
     9   module      null
    10 
    11 Assignment types:
    12 
    13   Assignment of stored value to...  Effect on context
    14   --------------------------------  -----------------
    15   local                             preserved
    16   global (module)                   preserved
    17   class                             overridden (if overridable)
    18   instance                          preserved
    19 
    20   Assignment to a namespace preserves the context except for some class
    21   assignments
    22 
    23 Assigning to classes:
    24 
    25   Assignment of...                  Effect on context
    26   ----------------                  -----------------
    27   function (overridable context)    overridden by class (becomes method)
    28   method (existing context)         preserved
    29   class (null context)              preserved
    30   instance (self context)           preserved
    31   module (null context)             preserved
    32 
    33 With run-time restrictions on assignment targets:
    34 
    35   Assignment of stored value to...  Effect on context   Optimised instruction   Unoptimised instruction
    36   --------------------------------  -----------------   ---------------------   -----------------------
    37   local                             preserved           StoreName
    38   global (module)                   preserved           StoreAddress            StoreAttrIndex
    39   instance                          preserved           StoreAttr               StoreAttrIndex
    40   class                             -> class            StoreAddressContext	StoreAttrIndex
    41 
    42   Assignment of new class attributes is only permitted during initialisation
    43   unless attribute usage observations are employed to detect possible attribute
    44   assignments
    45 
    46   Remember that once a function is assigned to a class, it will have that class
    47   as context (as an unbound method)
    48 
    49 Access types:
    50 
    51   Access to stored value from...    Effect on context   Optimised instruction     Unoptimised instruction
    52   ------------------------------    -----------------   ---------------------     -----------------------
    53   local                             preserved           LoadName
    54   global (module)                   preserved           LoadAddress               LoadAttrIndex
    55   class                             preserved           LoadAddress               LoadAttrIndex
    56   class via instance                -> instance         LoadAddressContext(Cond)  LoadAttrIndexContext(Cond)
    57   instance                          preserved           LoadAttr                  LoadAttrIndex
    58 
    59   Access to a namespace may not preserve the stored context
    60 
    61 Access to class attributes via instances:
    62 
    63   Access to stored value with...    Effect on context
    64   ------------------------------    -----------------
    65   compatible class as context       -> instance
    66   incompatible class as context     preserved
    67   null context                      preserved
    68   other context (instance)          preserved
    69 
    70 Optimisation possibilities for class attribute access via instances:
    71 
    72   Class       Class attribute   Context of attribute  Instruction
    73   -----       ---------------   --------------------  -----------
    74   known       constant          preserved             LoadAddress
    75   known       constant          -> instance           LoadAddressContext
    76   known       not constant      preserved             LoadAddress              (attribute may always be preserved)
    77   known       not constant      -> instance           LoadAddressContext       (attribute may always be overridden)
    78   known       not constant      not known             LoadAddressContextCond   (perform context check)
    79   not known   not known         preserved             LoadAttrIndex            (attribute may have preserved context in all classes)
    80 * not known   not known         -> instance           LoadAttrIndexContext     (attribute may have overridden context in all classes)
    81   not known   not known         not known             LoadAttrIndexContextCond (perform context check for class attribute access)
    82 
    83 * Since knowing that an instance will replace the context might also mean
    84   knowing the nature of the attribute, LoadAttrIndexContext is not likely to
    85   be useful in practice.
    86 
    87   Since the object table encodes sufficient information (an instance must be
    88   compatible to access the class attribute, and compatibility information is
    89   stored), an explicit compatibility test may not always be required at
    90   run-time
    91 
    92   Definite compile-time knowledge about instance usage is likely to be
    93   restricted to literal constants: in general, no assertions can be made about
    94   an accessor never being a class, module or constant and therefore being an
    95   instance