Started documenting the compiler.
[bpt/guile.git] / src / vm_system.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* This file is included in vm_engine.c */
43
44 \f
45 /*
46 * Basic operations
47 */
48
49 /* This must be the first instruction! */
50 VM_DEFINE_INSTRUCTION (nop, "nop", 0, 0, 0)
51 {
52 NEXT;
53 }
54
55 VM_DEFINE_INSTRUCTION (halt, "halt", 0, 0, 0)
56 {
57 SCM ret;
58 vp->time += scm_c_get_internal_run_time () - start_time;
59 HALT_HOOK ();
60 POP (ret);
61 FREE_FRAME ();
62 SYNC_ALL ();
63 return ret;
64 }
65
66 VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
67 {
68 BREAK_HOOK ();
69 NEXT;
70 }
71
72 VM_DEFINE_INSTRUCTION (drop, "drop", 0, 0, 0)
73 {
74 DROP ();
75 NEXT;
76 }
77
78 VM_DEFINE_INSTRUCTION (mark, "mark", 0, 0, 1)
79 {
80 PUSH (SCM_UNDEFINED);
81 NEXT;
82 }
83
84 VM_DEFINE_INSTRUCTION (dup, "dup", 0, 0, 1)
85 {
86 SCM x = *sp;
87 PUSH (x);
88 NEXT;
89 }
90
91 \f
92 /*
93 * Object creation
94 */
95
96 VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
97 {
98 PUSH (SCM_UNSPECIFIED);
99 NEXT;
100 }
101
102 VM_DEFINE_INSTRUCTION (make_true, "make-true", 0, 0, 1)
103 {
104 PUSH (SCM_BOOL_T);
105 NEXT;
106 }
107
108 VM_DEFINE_INSTRUCTION (make_false, "make-false", 0, 0, 1)
109 {
110 PUSH (SCM_BOOL_F);
111 NEXT;
112 }
113
114 VM_DEFINE_INSTRUCTION (make_eol, "make-eol", 0, 0, 1)
115 {
116 PUSH (SCM_EOL);
117 NEXT;
118 }
119
120 VM_DEFINE_INSTRUCTION (make_int8, "make-int8", 1, 0, 1)
121 {
122 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
123 NEXT;
124 }
125
126 VM_DEFINE_INSTRUCTION (make_int8_0, "make-int8:0", 0, 0, 1)
127 {
128 PUSH (SCM_INUM0);
129 NEXT;
130 }
131
132 VM_DEFINE_INSTRUCTION (make_int8_1, "make-int8:1", 0, 0, 1)
133 {
134 PUSH (SCM_I_MAKINUM (1));
135 NEXT;
136 }
137
138 VM_DEFINE_INSTRUCTION (make_int16, "make-int16", 2, 0, 1)
139 {
140 int h = FETCH ();
141 int l = FETCH ();
142 PUSH (SCM_I_MAKINUM ((signed short) (h << 8) + l));
143 NEXT;
144 }
145
146 VM_DEFINE_INSTRUCTION (make_char8, "make-char8", 1, 0, 1)
147 {
148 PUSH (SCM_MAKE_CHAR (FETCH ()));
149 NEXT;
150 }
151
152 VM_DEFINE_INSTRUCTION (list, "list", 1, -1, 1)
153 {
154 int n = FETCH ();
155 POP_LIST (n);
156 NEXT;
157 }
158
159 VM_DEFINE_INSTRUCTION (vector, "vector", 1, -1, 1)
160 {
161 int n = FETCH ();
162 POP_LIST (n);
163 *sp = scm_vector (*sp);
164 NEXT;
165 }
166
167 VM_DEFINE_INSTRUCTION (list_mark, "list-mark", 0, 0, 0)
168 {
169 POP_LIST_MARK ();
170 NEXT;
171 }
172
173 VM_DEFINE_INSTRUCTION (vector_mark, "vector-mark", 0, 0, 0)
174 {
175 POP_LIST_MARK ();
176 *sp = scm_vector (*sp);
177 NEXT;
178 }
179
180 VM_DEFINE_INSTRUCTION (list_break, "list-break", 0, 0, 0)
181 {
182 SCM l;
183 POP (l);
184 for (; !SCM_NULLP (l); l = SCM_CDR (l))
185 PUSH (SCM_CAR (l));
186 NEXT;
187 }
188
189 \f
190 /*
191 * Variable access
192 */
193
194 #define OBJECT_REF(i) objects[i]
195 #define OBJECT_SET(i,o) objects[i] = o
196
197 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
198 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
199
200 /* For the variable operations, we _must_ obviously avoid function calls to
201 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
202 nothing more than the corresponding macros. */
203 #define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
204 #define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
205 #define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
206
207 /* ref */
208
209 VM_DEFINE_INSTRUCTION (object_ref, "object-ref", 1, 0, 1)
210 {
211 register unsigned objnum = FETCH ();
212 CHECK_OBJECT (objnum);
213 PUSH (OBJECT_REF (objnum));
214 NEXT;
215 }
216
217 VM_DEFINE_INSTRUCTION (local_ref, "local-ref", 1, 0, 1)
218 {
219 PUSH (LOCAL_REF (FETCH ()));
220 NEXT;
221 }
222
223 VM_DEFINE_INSTRUCTION (external_ref, "external-ref", 1, 0, 1)
224 {
225 unsigned int i;
226 SCM e = external;
227 for (i = FETCH (); i; i--)
228 {
229 CHECK_EXTERNAL(e);
230 e = SCM_CDR (e);
231 }
232 CHECK_EXTERNAL(e);
233 PUSH (SCM_CAR (e));
234 NEXT;
235 }
236
237 VM_DEFINE_INSTRUCTION (variable_ref, "variable-ref", 0, 0, 1)
238 {
239 SCM x = *sp;
240
241 if (!VARIABLE_BOUNDP (x))
242 {
243 err_args = SCM_LIST1 (x);
244 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
245 goto vm_error_unbound;
246 }
247 else
248 {
249 SCM o = VARIABLE_REF (x);
250 *sp = o;
251 }
252
253 NEXT;
254 }
255
256 /* set */
257
258 VM_DEFINE_INSTRUCTION (local_set, "local-set", 1, 1, 0)
259 {
260 LOCAL_SET (FETCH (), *sp);
261 DROP ();
262 NEXT;
263 }
264
265 VM_DEFINE_INSTRUCTION (external_set, "external-set", 1, 1, 0)
266 {
267 unsigned int i;
268 SCM e = external;
269 for (i = FETCH (); i; i--)
270 {
271 CHECK_EXTERNAL(e);
272 e = SCM_CDR (e);
273 }
274 CHECK_EXTERNAL(e);
275 SCM_SETCAR (e, *sp);
276 DROP ();
277 NEXT;
278 }
279
280 VM_DEFINE_INSTRUCTION (variable_set, "variable-set", 0, 1, 0)
281 {
282 VARIABLE_SET (sp[0], sp[-1]);
283 scm_set_object_property_x (sp[-1], scm_sym_name, SCM_CAR (sp[0]));
284 sp -= 2;
285 NEXT;
286 }
287
288 \f
289 /*
290 * branch and jump
291 */
292
293 #define BR(p) \
294 { \
295 int h = FETCH (); \
296 int l = FETCH (); \
297 signed short offset = (h << 8) + l; \
298 if (p) \
299 ip += offset; \
300 DROP (); \
301 NEXT; \
302 }
303
304 VM_DEFINE_INSTRUCTION (br, "br", 2, 0, 0)
305 {
306 int h = FETCH ();
307 int l = FETCH ();
308 ip += (signed short) (h << 8) + l;
309 NEXT;
310 }
311
312 VM_DEFINE_INSTRUCTION (br_if, "br-if", 2, 0, 0)
313 {
314 BR (!SCM_FALSEP (*sp));
315 }
316
317 VM_DEFINE_INSTRUCTION (br_if_not, "br-if-not", 2, 0, 0)
318 {
319 BR (SCM_FALSEP (*sp));
320 }
321
322 VM_DEFINE_INSTRUCTION (br_if_eq, "br-if-eq", 2, 0, 0)
323 {
324 BR (SCM_EQ_P (sp[0], sp--[1]));
325 }
326
327 VM_DEFINE_INSTRUCTION (br_if_not_eq, "br-if-not-eq", 2, 0, 0)
328 {
329 BR (!SCM_EQ_P (sp[0], sp--[1]));
330 }
331
332 VM_DEFINE_INSTRUCTION (br_if_null, "br-if-null", 2, 0, 0)
333 {
334 BR (SCM_NULLP (*sp));
335 }
336
337 VM_DEFINE_INSTRUCTION (br_if_not_null, "br-if-not-null", 2, 0, 0)
338 {
339 BR (!SCM_NULLP (*sp));
340 }
341
342 \f
343 /*
344 * Subprogram call
345 */
346
347 VM_DEFINE_INSTRUCTION (make_closure, "make-closure", 0, 1, 1)
348 {
349 SYNC_BEFORE_GC ();
350 *sp = scm_c_make_closure (*sp, external);
351 NEXT;
352 }
353
354 VM_DEFINE_INSTRUCTION (call, "call", 1, -1, 1)
355 {
356 SCM x;
357 nargs = FETCH ();
358
359 vm_call:
360 x = sp[-nargs];
361
362 /*
363 * Subprogram call
364 */
365 if (SCM_PROGRAM_P (x))
366 {
367 program = x;
368 vm_call_program:
369 CACHE_PROGRAM ();
370 INIT_ARGS ();
371 NEW_FRAME ();
372 ENTER_HOOK ();
373 APPLY_HOOK ();
374 NEXT;
375 }
376 /*
377 * Function call
378 */
379 if (!SCM_FALSEP (scm_procedure_p (x)))
380 {
381 /* At this point, the stack contains the procedure and each one of its
382 arguments. */
383 SCM args;
384
385 #if 1
386 POP_LIST (nargs);
387 #else
388 /* Experimental: Build the arglist on the VM stack. XXX */
389 POP_LIST_ON_STACK (nargs);
390 #endif
391 POP (args);
392 *sp = scm_apply (x, args, SCM_EOL);
393 NEXT;
394 }
395 /*
396 * Continuation call
397 */
398 if (SCM_VM_CONT_P (x))
399 {
400 vm_call_cc:
401 /* Check the number of arguments */
402 if (nargs != 1)
403 scm_wrong_num_args (x);
404
405 /* Reinstate the continuation */
406 EXIT_HOOK ();
407 reinstate_vm_cont (vp, x);
408 CACHE_REGISTER ();
409 program = SCM_FRAME_PROGRAM (fp);
410 CACHE_PROGRAM ();
411 NEXT;
412 }
413
414 program = x;
415 goto vm_error_wrong_type_apply;
416 }
417
418 VM_DEFINE_INSTRUCTION (tail_call, "tail-call", 1, -1, 1)
419 {
420 register SCM x;
421 nargs = FETCH ();
422 x = sp[-nargs];
423
424 SCM_TICK; /* allow interrupt here */
425
426 /*
427 * Tail recursive call
428 */
429 if (SCM_EQ_P (x, program))
430 {
431 int i;
432
433 /* Move arguments */
434 INIT_ARGS ();
435 sp -= bp->nargs - 1;
436 for (i = 0; i < bp->nargs; i++)
437 LOCAL_SET (i, sp[i]);
438
439 /* Drop the first argument and the program itself. */
440 sp -= 2;
441
442 /* Call itself */
443 ip = bp->base;
444 APPLY_HOOK ();
445 NEXT;
446 }
447 /*
448 * Proper tail call
449 */
450 if (SCM_PROGRAM_P (x))
451 {
452 EXIT_HOOK ();
453 FREE_FRAME ();
454 program = x;
455 goto vm_call_program;
456 }
457 /*
458 * Function call
459 */
460 if (!SCM_FALSEP (scm_procedure_p (x)))
461 {
462 SCM args;
463 POP_LIST (nargs);
464 POP (args);
465 *sp = scm_apply (x, args, SCM_EOL);
466 goto vm_return;
467 }
468 /*
469 * Continuation call
470 */
471 if (SCM_VM_CONT_P (x))
472 goto vm_call_cc;
473
474 program = x;
475 goto vm_error_wrong_type_apply;
476 }
477
478 VM_DEFINE_INSTRUCTION (apply, "apply", 1, -1, 1)
479 {
480 int len;
481 SCM ls;
482 POP (ls);
483
484 nargs = FETCH ();
485 if (nargs < 2)
486 goto vm_error_wrong_num_args;
487
488 len = scm_ilength (ls);
489 if (len < 0)
490 goto vm_error_wrong_type_arg;
491
492 for (; !SCM_NULLP (ls); ls = SCM_CDR (ls))
493 PUSH (SCM_CAR (ls));
494
495 nargs += len - 2;
496 goto vm_call;
497 }
498
499 VM_DEFINE_INSTRUCTION (call_cc, "call/cc", 1, 1, 1)
500 {
501 SYNC_BEFORE_GC ();
502 PUSH (capture_vm_cont (vp));
503 POP (program);
504 nargs = 1;
505 goto vm_call;
506 }
507
508 VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
509 {
510 vm_return:
511 EXIT_HOOK ();
512 RETURN_HOOK ();
513 FREE_FRAME ();
514
515 /* Restore the last program */
516 program = SCM_FRAME_PROGRAM (fp);
517 CACHE_PROGRAM ();
518 CACHE_EXTERNAL ();
519 NEXT;
520 }
521
522 /*
523 Local Variables:
524 c-file-style: "gnu"
525 End:
526 */