Debian Codegraph @

src/doc/manual/developer-guide/compiler.txi

raw
1 @node The compiler
2 @section The compiler
3
4 @subsection The compiler translates to C
5 The ECL compiler is essentially a translator from Common-Lisp to C. Given a Lisp source file, the compiler first generates three intermediate files:
6
7 @itemize
8 @item a C-file which consists of the C version of the Lisp program
9 @item an H-file which consists of declarations referenced in the C-file
10 @item a Data-file which consists of Lisp data to be used at load time
11 @end itemize
12
13 The ECL compiler then invokes the C compiler to compile the C-file into an object file. Finally, the contents of the Data-file is appended to the object file to make a @dfn{Fasl-file}. The generated Fasl-file can be loaded into the ECL system by the Common-Lisp function @code{load}. By default, the three intermediate files are deleted after the compilation, but, if asked, the compiler leaves them.
14
15 The merits of the use of C as the intermediate language are:
16
17 @itemize
18 @item The ECL compiler is highly portable.
19 @item Cross compilation is possible, because the contents of the intermediate files are common to all versions of ECL. For example, one can compile his or her Lisp program by the ECL compiler on a Sun, bring the intermediate files to DOS, compile the C-file with the gcc compiler under DOS, and then append the Data-file to the object file. This procedure generates the Fasl-file for the ECL system on DOS. This kind of cross compilation makes it easier to port ECL.
20 @item Hardware-dependent optimizations such as register allocations are done by the C compiler.
21 @end itemize
22
23 The demerits are:
24
25 @itemize
26 @item At those sites where no C compiler is available, the users cannot compile their Lisp programs.
27 @item The compilation time is long. 70% to 80% of the compilation time is used by the C compiler. The ECL compiler is therefore slower than compiler generating machine code directly.
28 @end itemize
29
30
31 @subsection The compiler mimics human C programmer
32
33 The format of the intermediate C code generated by the ECL compiler is the same as the hand-coded C code of the ECL source programs. For example, supposing that the Lisp source file contains the following function definition:
34
35 @lisp
36 (defvar *delta* 2)
37 (defun add1 (x) (+ *delta* x))
38 @end lisp
39
40
41 The compiler generates the following intermediate C code.
42
43 @example
44 @verbatim
45 /* function definition for ADD1 */
46 /* optimize speed 3, debug 0, space 0, safety 2 */
47 static cl_object L1add1(cl_object v1x)
48 {
49 cl_object env0 = ECL_NIL;
50 const cl_env_ptr cl_env_copy = ecl_process_env();
51 cl_object value0;
52 ecl_cs_check(cl_env_copy,value0);
53 {
54 TTL:
55 value0 = ecl_plus(ecl_symbol_value(VV[0]),v1x);
56 cl_env_copy->nvalues = 1;
57 return value0;
58 }
59 }
60
61 /* initialization of this module */
62 ECL_DLLEXPORT void init_fas_CODE(cl_object flag)
63 {
64 const cl_env_ptr cl_env_copy = ecl_process_env();
65 cl_object value0;
66 cl_object *VVtemp;
67 if (flag != OBJNULL){
68 Cblock = flag;
69 #ifndef ECL_DYNAMIC_VV
70 flag->cblock.data = VV;
71 #endif
72 flag->cblock.data_size = VM;
73 flag->cblock.temp_data_size = VMtemp;
74 flag->cblock.data_text = compiler_data_text;
75 flag->cblock.cfuns_size = compiler_cfuns_size;
76 flag->cblock.cfuns = compiler_cfuns;
77 flag->cblock.source = make_constant_base_string("test.lisp");
78 return;}
79 #ifdef ECL_DYNAMIC_VV
80 VV = Cblock->cblock.data;
81 #endif
82 Cblock->cblock.data_text = (const cl_object *)"@EcLtAg:init_fas_CODE@";
83 VVtemp = Cblock->cblock.temp_data;
84 ECL_DEFINE_SETF_FUNCTIONS
85 si_Xmake_special(VV[0]);
86 if (ecl_boundp(cl_env_copy,VV[0])) { goto L2; }
87 cl_set(VV[0],ecl_make_fixnum(2));
88 L2:;
89 ecl_cmp_defun(VV[2]); /* ADD1 */
90 }
91 @end verbatim
92 @end example
93
94 The C function @code{L1add1} implements the Lisp function @code{add1}. This relation is established by @code{ecl_cmp_defun} in the initialization function @code{init_fas_CODE}, which is invoked at load time. There, the vector @code{VV} consists of Lisp objects; @code{VV[0]} and @code{VV[1]} in this example hold the Lisp symbols @code{*delta*} and @code{add1}, while @code{VV[2]} holds the function object for @code{add1}, which is created during initialization of the module. @code{VM} in the definition of @code{L1add1} is a C macro declared in the corresponding H-file. The actual value of @code{VM} is the number of value stack locations used by this module, i.e., 3 in this example. Thus the following macro definition is found in the H-file.
95
96 @example
97 #define VM 3
98 @end example
99
100 @subsection Implementation of Compiled Closures
101 The ECL compiler takes two passes before it invokes the C compiler. The major role of the first pass is to detect function closures and to detect, for each function closure, those lexical objects (i.e., lexical variable, local function definitions, tags, and block-names) to be enclosed within the closure. This check must be done before the C code generation in the second pass, because lexical objects to be enclosed in function closures are treated in a different way from those not enclosed.
102
103 Ordinarily, lexical variables in a compiled function @code{f} are allocated on the C stack. However, if a lexical variable is to be enclosed in function closures, it is allocated on a list, called the "environment list", which is local to @code{f}. In addition, a local variable is created which points to the lexical variable's location (within the environment list), so that the variable may be accessed through an indirection rather than by list traversal.
104
105 The environment list is a pushdown list: It is empty when @code{f} is called. An element is pushed on the environment list when a variable to be enclosed in closures is bound, and is popped when the binding is no more in effect. That is, at any moment during execution of @code{f}, the environment list contains those lexical variables whose binding is still in effect and which should be enclosed in closures. When a compiled closure is created during execution of @code{f}, the compiled code for the closure is coupled with the environment list at that moment to form the compiled closure.
106
107 Later, when the compiled closure is invoked, a pointer is set up to each lexical variable in the environment list, so that each object may be referenced through a memory indirection.
108
109 Let us see an example. Suppose the following function has been compiled.
110
111 @lisp
112 (defun foo (x)
113 (let ((a #'(lambda () (incf x)))
114 (y x))
115 (values a #'(lambda () (incf x y)))))
116 @end lisp
117
118 @code{foo} returns two compiled closures. The first closure increments @var{x} by one, whereas the second closure increments @var{x} by the initial value of @var{x}. Both closures return the incremented value of @var{x}.
119
120 @example
121 >(multiple-value-setq (f g) (foo 10))
122 #<compiled-closure nil>
123
124 >(funcall f)
125 11
126
127 >(funcall g)
128 21
129
130 >
131 @end example
132
133 After this, the two compiled closures look like:
134
135 @verbatim
136 second closure y: x:
137 |-------|------| |-------|------| |------|------|
138 | ** | --|----->| 10 | --|------>| 21 | nil |
139 |-------|------| |-------|------| |------|------|
140 ^
141 first closure |
142 |-------|------| |
143 | * | --|----------|
144 |-------|------|
145
146 * : address of the compiled code for #'(lambda () (incf x))
147 ** : address of the compiled code for #'(lambda () (incf x y))
148 @end verbatim
149
150 @subsection Use of Declarations to Improve Efficiency
151 Declarations, especially type and function declarations, increase the efficiency of the compiled code. For example, for the following Lisp source file, with two Common-Lisp declarations added,
152
153 @lisp
154 (eval-when (:compile-toplevel)
155 (proclaim '(ftype (function (fixnum fixnum) fixnum) tak))
156 (proclaim '(optimize (speed 3) (debug 0) (safety 0))))
157
158 (defun tak (x y)
159 (declare (fixnum x y))
160 (if (not (< y x))
161 y
162 (tak (tak (1- x) y)
163 (tak (1- y) x))))
164 @end lisp
165
166
167 The compiler generates the following C code (Note that the tail-recursive call of @code{tak} was replaced by iteration):
168 @example
169 @verbatim
170 /* function definition for TAK */
171 /* optimize speed 3, debug 0, space 0, safety 0 */
172 static cl_object L1tak(cl_object v1x, cl_object v2y)
173 {
174 cl_object env0 = ECL_NIL;
175 const cl_env_ptr cl_env_copy = ecl_process_env();
176 cl_object value0;
177 cl_fixnum v3x;
178 cl_fixnum v4y;
179 v3x = ecl_fixnum(v1x);
180 v4y = ecl_fixnum(v2y);
181 TTL:
182 if ((v4y)<(v3x)) { goto L1; }
183 value0 = ecl_make_fixnum(v4y);
184 cl_env_copy->nvalues = 1;
185 return value0;
186 L1:;
187 {
188 cl_fixnum v5;
189 {
190 cl_fixnum v6;
191 v6 = (v3x)-1;
192 v5 = ecl_fixnum(L1tak(ecl_make_fixnum(v6), ecl_make_fixnum(v4y)));
193 }
194 {
195 cl_fixnum v6;
196 v6 = (v4y)-1;
197 v4y = ecl_fixnum(L1tak(ecl_make_fixnum(v6), ecl_make_fixnum(v3x)));
198 }
199 v3x = v5;
200 }
201 goto TTL;
202 }
203 @end verbatim
204 @end example
205
206 @subsection Inspecting generated C code
207 Common-Lisp defines a function disassemble, which is supposed to disassemble a compiled function and to display the assembler code. According to @cite{Common-Lisp: The Language},
208
209 @quotation
210 This is primary useful for debugging the compiler, ..\\
211 @end quotation
212
213 This is, however, @emph{useless} in our case, because we are not concerned with assembly language. Rather, we are interested in the C code generated by the ECL compiler. Thus the disassemble function in ECL accepts not-yet-compiled functions only and displays the translated C code.
214
215 @example
216 @verbatim
217 > (defun add1 (x) (1+ x))
218
219 ADD1
220 > (disassemble *)
221
222 /* function definition for ADD1 */
223 /* optimize speed 3, debug 0, space 0, safety 2 */
224 static cl_object L1add1(cl_object v1x)
225 {
226 cl_object env0 = ECL_NIL;
227 const cl_env_ptr cl_env_copy = ecl_process_env();
228 cl_object value0;
229 ecl_cs_check(cl_env_copy,value0);
230 {
231 TTL:
232 value0 = ecl_one_plus(v1x);
233 cl_env_copy->nvalues = 1;
234 return value0;
235 }
236 }
237 @end verbatim
238 @end example