| 1 |
@node The interpreter |
| 2 |
@section The interpreter |
| 3 |
|
| 4 |
@menu |
| 5 |
* ECL stacks:: |
| 6 |
* Procedure Call Conventions:: |
| 7 |
* The lexical environment:: |
| 8 |
* The interpreter stack:: |
| 9 |
@end menu |
| 10 |
|
| 11 |
@node ECL stacks |
| 12 |
@subsection ECL stacks |
| 13 |
ECL uses the following stacks: |
| 14 |
@multitable @columnfractions .3 .7 |
| 15 |
@item Frame Stack |
| 16 |
@tab consisting of catch, block, tagbody frames |
| 17 |
@item Bind Stack |
| 18 |
@tab for shallow binding of dynamic variables |
| 19 |
@item Interpreter Stack |
| 20 |
@tab acts as a Forth data stack, keeping intermediate arguments to interpreted functions, plus a history of called functions. |
| 21 |
@item C Control Stack |
| 22 |
@tab used for arguments/values passing, typed lexical variables, temporary values, and function invocation. |
| 23 |
@end multitable |
| 24 |
|
| 25 |
@node Procedure Call Conventions |
| 26 |
@subsection Procedure Call Conventions |
| 27 |
ECL employs standard C calling conventions to achieve efficiency and |
| 28 |
interoperability with other languages. Each Lisp function is |
| 29 |
implemented as a C function which takes as many arguments as the Lisp |
| 30 |
original. If the function takes optional or keyword arguments, the |
| 31 |
corresponding C function takes one additional integer argument which |
| 32 |
holds the number of actual arguments. The function sets @code{nvalues} |
| 33 |
in the thread local environment to the number of Lisp values produced, |
| 34 |
it returns the first one and the remaining ones are kept in a global |
| 35 |
(per thread) array (@code{values}). |
| 36 |
|
| 37 |
To show the argument/value passing mechanism, here we list the actual |
| 38 |
code for the Common-Lisp function last. |
| 39 |
|
| 40 |
@example |
| 41 |
@verbatim |
| 42 |
cl_object |
| 43 |
cl_last(cl_narg narg, cl_object l, ...) |
| 44 |
{ |
| 45 |
const cl_env_ptr the_env = ecl_process_env(); |
| 46 |
cl_object k; |
| 47 |
va_list ARGS; |
| 48 |
va_start(ARGS, l); |
| 49 |
if (ecl_unlikely(narg < 1|| narg > 2)) FEwrong_num_arguments(/* ... */); |
| 50 |
if (narg > 1) { |
| 51 |
k = va_arg(ARGS,cl_object); |
| 52 |
} else { |
| 53 |
k = ecl_make_fixnum(1); |
| 54 |
} |
| 55 |
cl_object __value0 = ecl_last(l, ecl_to_size(k)); |
| 56 |
the_env->nvalues = 1; |
| 57 |
the_env->values[0] = __value0; |
| 58 |
va_end(ARGS); |
| 59 |
return __value0; |
| 60 |
} |
| 61 |
@end verbatim |
| 62 |
@end example |
| 63 |
|
| 64 |
ECL adopts the convention that the name of a function that implements |
| 65 |
a Common-Lisp function begins with a short package name (@code{cl} for |
| 66 |
@code{COMMON-LISP}, @code{si} for @code{SYSTEM}, etc), followed by |
| 67 |
@code{L}, and followed by the name of the Common-Lisp |
| 68 |
function. (Strictly speaking, `@code{-}' and `@code{*}' in the |
| 69 |
Common-Lisp function name are replaced by `@code{_}' and `@code{A}', |
| 70 |
respectively, to obey the syntax of C.) |
| 71 |
|
| 72 |
The code for the function @code{last} first checks that the right |
| 73 |
number of arguments are supplied to @code{cl_last}. That is, it checks |
| 74 |
that @code{narg} is 1 or 2, and otherwise, it causes an error. |
| 75 |
Following that, the optional variable @code{k} is initialized and the |
| 76 |
return value @code{__value0} is computed. The number assigned to |
| 77 |
@code{nvalues} set by the function (1 in this case) represents the |
| 78 |
number of values of the function. The return value of the function is |
| 79 |
copied in the @code{values} array as well as returned directly. |
| 80 |
|
| 81 |
In general, if one is to play with the C kernel of ECL there is no |
| 82 |
need to know about all these conventions. There is a preprocessor |
| 83 |
(@pxref{Defun preprocessor}) that takes care of the details, by using a |
| 84 |
lisp representation of the statements that output values, and of the |
| 85 |
function definitions. For instance, the actual source code for |
| 86 |
@code{cl_last} in @file{src/c/list.d} is |
| 87 |
|
| 88 |
@example |
| 89 |
@verbatim |
| 90 |
@(defun last (l &optional (k ecl_make_fixnum(1))) |
| 91 |
@ |
| 92 |
@(return ecl_last(l, ecl_to_size(k))); |
| 93 |
@) |
| 94 |
@end verbatim |
| 95 |
@end example |
| 96 |
|
| 97 |
@node The lexical environment |
| 98 |
@subsection The lexical environment |
| 99 |
The ECL interpreter uses a list containing local functions and macros, |
| 100 |
variables, tags and blocks to represent the lexical environment. When |
| 101 |
a function closure is created, the current lexical environment is |
| 102 |
saved in the closure along with the lambda expression. Later, when the |
| 103 |
closure is invoked, this list is used to recover the lexical |
| 104 |
environment. |
| 105 |
|
| 106 |
Note that this list is different from what the Common Lisp standard |
| 107 |
calls a lexical environment, which is the content of a |
| 108 |
@code{&environment} parameter to @code{defmacro}. For the differences |
| 109 |
between this two environments see the comments in |
| 110 |
@file{src/c/compiler.d} and @file{src/c/interpreter.d}. |
| 111 |
|
| 112 |
@node The interpreter stack |
| 113 |
@subsection The interpreter stack |
| 114 |
|
| 115 |
The bytecodes interpreter uses a stack of its own to save and restore |
| 116 |
values from intermediate calculations. This Forth-like data stack is |
| 117 |
also used in other parts of the C kernel for various purposes, such as |
| 118 |
saving compiled code, keeping arguments to @code{format}, etc. |
| 119 |
|
| 120 |
However, one of the most important roles of the Interpreter Stack is |
| 121 |
to keep a log of the functions which are called during the execution |
| 122 |
of bytecodes. For each function invoked, the interpreter keeps three |
| 123 |
lisp objects on the stack: |
| 124 |
|
| 125 |
@verbatim |
| 126 |
+----------+------------------------------------------------+ |
| 127 |
| function | lexical environment | index to previous record | |
| 128 |
+----------+---------------------+--------------------------+ |
| 129 |
@end verbatim |
| 130 |
|
| 131 |
The first item is the object which is funcalled. It can be a bytecodes |
| 132 |
object, a compiled function or a generic function. In the last two |
| 133 |
cases the lexical environment is just @code{nil}. In the first case, |
| 134 |
the second item on the stack is the lexical environment on which the |
| 135 |
code is executed. Each of these records are popped out of the stack |
| 136 |
after function invocation. |
| 137 |
|
| 138 |
Let us see how these invocation records are used for debugging. |
| 139 |
|
| 140 |
@example |
| 141 |
@verbatim |
| 142 |
> (defun fact (x) ;;; Wrong definition of the |
| 143 |
(if (= x 0) ;;; factorial function. |
| 144 |
one ;;; one should be 1. |
| 145 |
(* x (fact (1- x))))) |
| 146 |
FACT |
| 147 |
|
| 148 |
> (fact 3) ;;; Tries 3! |
| 149 |
Error: The variable ONE is unbound. |
| 150 |
Error signalled by IF. |
| 151 |
Broken at IF. |
| 152 |
>> :b ;;; Backtrace. |
| 153 |
Backtrace: eval > fact > if > fact > if > fact > if > fact > IF |
| 154 |
;;; Currently at the last IF. |
| 155 |
>> :h ;;; Help. |
| 156 |
|
| 157 |
Break commands: |
| 158 |
:q(uit) Return to some previous break level. |
| 159 |
:pop Pop to previous break level. |
| 160 |
:c(ontinue) Continue execution. |
| 161 |
:b(acktrace) Print backtrace. |
| 162 |
:f(unction) Show current function. |
| 163 |
:p(revious) Go to previous function. |
| 164 |
:n(ext) Go to next function. |
| 165 |
:g(o) Go to next function. |
| 166 |
:fs Search forward for function. |
| 167 |
:bs Search backward for function. |
| 168 |
:v(ariables) Show local variables, functions, blocks, and tags. |
| 169 |
:l(ocal) Return the nth local value on the stack. |
| 170 |
:hide Hide function. |
| 171 |
:unhide Unhide function. |
| 172 |
:hp Hide package. |
| 173 |
:unhp Unhide package. |
| 174 |
:unhide-all Unhide all variables and packages. |
| 175 |
:bds Show binding stack. |
| 176 |
:m(essage) Show error message. |
| 177 |
:hs Help stack. |
| 178 |
Top level commands: |
| 179 |
:cf Compile file. |
| 180 |
:exit or ^D Exit Lisp. |
| 181 |
:ld Load file. |
| 182 |
:step Single step form. |
| 183 |
:tr(ace) Trace function. |
| 184 |
:untr(ace) Untrace function. |
| 185 |
|
| 186 |
Help commands: |
| 187 |
:apropos Apropos. |
| 188 |
:doc(ument) Document. |
| 189 |
:h(elp) or ? Help. Type ":help help" for more information. |
| 190 |
|
| 191 |
>> :p ;;; Move to the last call of FACT. |
| 192 |
Broken at IF. |
| 193 |
|
| 194 |
>> :b |
| 195 |
Backtrace: eval > fact > if > fact > if > fact > if > FACT > if |
| 196 |
;;; Now at the last FACT. |
| 197 |
>> :v ;;; The environment at the last call |
| 198 |
Local variables: ;;; to FACT is recovered. |
| 199 |
X: 0 ;;; X is the only bound variable. |
| 200 |
Block names: FACT. ;;; The block FACT is established. |
| 201 |
|
| 202 |
>> x |
| 203 |
0 ;;; The value of x is 0. |
| 204 |
|
| 205 |
>>(return-from fact 1) ;;; Return from the last call of |
| 206 |
6 ;;; FACT with the value of 0. |
| 207 |
;;; The execution is resumed and |
| 208 |
> ;;; the value 6 is returned. |
| 209 |
;;; Again at the top-level loop. |
| 210 |
@end verbatim |
| 211 |
@end example |