micropython

docs/evaluation.txt

209:eb198a981723
2009-05-05 Paul Boddie Added note about accommodating default arguments on all object types.
     1 Instruction Evaluation Model
     2 ============================
     3 
     4 Programs use a value stack containing local and temporary storage. A value
     5 stack pointer indicates the top of this stack. In addition, a separate stack
     6 is used to record the invocation frames. All stack pointers refer to the next
     7 address to be used by the stack, not the address of the uppermost element.
     8 
     9     Frame Stack     Value Stack
    10     -----------     -----------     Address of Callable
    11                                     -------------------
    12     previous        ...
    13     current ------> callable -----> identifier
    14                     arg1            reference to code
    15                     arg2
    16                     arg3
    17                     local4
    18                     local5
    19                     ...
    20 
    21 Unlike the CPython virtual machine, programs do not use a value stack
    22 containing the results of expression evaluations. Instead, temporary storage
    23 is statically allocated and employed by instructions.
    24 
    25 Loading local names and temporary values is a matter of performing
    26 frame-relative accesses to the value stack.
    27 
    28 Invocations and Argument Evaluation
    29 -----------------------------------
    30 
    31 When preparing for an invocation, the caller first sets the invocation frame
    32 pointer. A number of locations for the arguments required by the invocation
    33 are then reserved. With a frame to prepare, positional arguments are added to
    34 the frame in order such that the first argument positions are filled. The
    35 names of keyword arguments are used (in the form of table index values) to
    36 consult the parameter table and to find the frame location in which such
    37 arguments are to be stored.
    38 
    39     fn(a, b, d=1, e=2, c=3) -> fn(a, b, c, d, e)
    40 
    41     Frame
    42     -----
    43 
    44     a               a               a               a
    45     b               b               b               b
    46     ___             ___             ___         --> 3
    47     ___         --> 1               1           |   1
    48     ___         |   ___         --> 2           |   2
    49                 |               |               |
    50     1 -----------   2 -----------   3 -----------
    51 
    52 Conceptually, the frame can be considered as a collection of attributes, as
    53 seen in other kinds of structures:
    54 
    55 Frame for invocation of fn:
    56 
    57     0           1           2           3           4           5
    58     code        a           b           c           d           e
    59     reference
    60 
    61 However, where arguments are specified positionally, such "attributes" are not
    62 set using a comparable approach to that employed with other structures.
    63 Keyword arguments are set using an attribute-like mechanism, though, where the
    64 position of each argument discovered using the parameter table.
    65 
    66 Method Invocations
    67 ------------------
    68 
    69 Method invocations incorporate an implicit first argument which is obtained
    70 from the context of the method:
    71 
    72     method(a, b, d=1, e=2, c=3) -> method(self, a, b, c, d, e)
    73 
    74     Frame
    75     -----
    76 
    77     context of method
    78     a
    79     b
    80     3
    81     1
    82     2
    83 
    84 Although it could be possible to permit any object to be provided as the first
    85 argument, in order to optimise instance attribute access in methods, we should
    86 seek to restrict the object type.
    87 
    88 Verifying Supplied Arguments
    89 ----------------------------
    90 
    91 In order to ensure a correct invocation, it is also necessary to check the
    92 number of supplied arguments. If the target of the invocation is known at
    93 compile-time, no additional instructions need to be emitted; otherwise, the
    94 generated code must test for the following situations:
    95 
    96  1. That the number of supplied arguments is equal to the number of expected
    97     parameters.
    98 
    99  2. That no keyword argument overwrites an existing positional parameter.
   100 
   101 Default Arguments
   102 -----------------
   103 
   104 Some arguments may have default values which are used if no value is provided
   105 in an invocation. Such defaults are initialised when the function itself is
   106 initialised, and are used to fill in any invocation frames that are known at
   107 compile-time.
   108 
   109 Tuples, Frames and Allocation
   110 -----------------------------
   111 
   112 Using the approach where arguments are treated like attributes in some kind of
   113 structure, we could choose to allocate frames in places other than a stack.
   114 This would produce something somewhat similar to a plain tuple object.