Remove uses of discouraged constructs.
[bpt/guile.git] / libguile / vm-i-system.c
1 /* Copyright (C) 2001,2008,2009 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 /* This file is included in vm_engine.c */
21
22 \f
23 /*
24 * Basic operations
25 */
26
27 VM_DEFINE_INSTRUCTION (0, nop, "nop", 0, 0, 0)
28 {
29 NEXT;
30 }
31
32 VM_DEFINE_INSTRUCTION (1, halt, "halt", 0, 0, 0)
33 {
34 vp->time += scm_c_get_internal_run_time () - start_time;
35 HALT_HOOK ();
36 nvalues = SCM_I_INUM (*sp--);
37 NULLSTACK (1);
38 if (nvalues == 1)
39 POP (finish_args);
40 else
41 {
42 POP_LIST (nvalues);
43 POP (finish_args);
44 SYNC_REGISTER ();
45 finish_args = scm_values (finish_args);
46 }
47
48 {
49 #ifdef VM_ENABLE_STACK_NULLING
50 SCM *old_sp = sp;
51 #endif
52
53 /* Restore registers */
54 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
55 /* Setting the ip here doesn't actually affect control flow, as the calling
56 code will restore its own registers, but it does help when walking the
57 stack */
58 ip = SCM_FRAME_RETURN_ADDRESS (fp);
59 fp = SCM_FRAME_DYNAMIC_LINK (fp);
60 NULLSTACK (old_sp - sp);
61 }
62
63 goto vm_done;
64 }
65
66 VM_DEFINE_INSTRUCTION (2, break, "break", 0, 0, 0)
67 {
68 BREAK_HOOK ();
69 NEXT;
70 }
71
72 VM_DEFINE_INSTRUCTION (3, drop, "drop", 0, 1, 0)
73 {
74 DROP ();
75 NEXT;
76 }
77
78 VM_DEFINE_INSTRUCTION (4, dup, "dup", 0, 0, 1)
79 {
80 SCM x = *sp;
81 PUSH (x);
82 NEXT;
83 }
84
85 \f
86 /*
87 * Object creation
88 */
89
90 VM_DEFINE_INSTRUCTION (5, void, "void", 0, 0, 1)
91 {
92 PUSH (SCM_UNSPECIFIED);
93 NEXT;
94 }
95
96 VM_DEFINE_INSTRUCTION (6, make_true, "make-true", 0, 0, 1)
97 {
98 PUSH (SCM_BOOL_T);
99 NEXT;
100 }
101
102 VM_DEFINE_INSTRUCTION (7, make_false, "make-false", 0, 0, 1)
103 {
104 PUSH (SCM_BOOL_F);
105 NEXT;
106 }
107
108 VM_DEFINE_INSTRUCTION (8, make_eol, "make-eol", 0, 0, 1)
109 {
110 PUSH (SCM_EOL);
111 NEXT;
112 }
113
114 VM_DEFINE_INSTRUCTION (9, make_int8, "make-int8", 1, 0, 1)
115 {
116 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
117 NEXT;
118 }
119
120 VM_DEFINE_INSTRUCTION (10, make_int8_0, "make-int8:0", 0, 0, 1)
121 {
122 PUSH (SCM_INUM0);
123 NEXT;
124 }
125
126 VM_DEFINE_INSTRUCTION (11, make_int8_1, "make-int8:1", 0, 0, 1)
127 {
128 PUSH (SCM_I_MAKINUM (1));
129 NEXT;
130 }
131
132 VM_DEFINE_INSTRUCTION (12, make_int16, "make-int16", 2, 0, 1)
133 {
134 int h = FETCH ();
135 int l = FETCH ();
136 PUSH (SCM_I_MAKINUM ((signed short) (h << 8) + l));
137 NEXT;
138 }
139
140 VM_DEFINE_INSTRUCTION (13, make_int64, "make-int64", 8, 0, 1)
141 {
142 scm_t_uint64 v = 0;
143 v += FETCH ();
144 v <<= 8; v += FETCH ();
145 v <<= 8; v += FETCH ();
146 v <<= 8; v += FETCH ();
147 v <<= 8; v += FETCH ();
148 v <<= 8; v += FETCH ();
149 v <<= 8; v += FETCH ();
150 v <<= 8; v += FETCH ();
151 PUSH (scm_from_int64 ((scm_t_int64) v));
152 NEXT;
153 }
154
155 VM_DEFINE_INSTRUCTION (14, make_uint64, "make-uint64", 8, 0, 1)
156 {
157 scm_t_uint64 v = 0;
158 v += FETCH ();
159 v <<= 8; v += FETCH ();
160 v <<= 8; v += FETCH ();
161 v <<= 8; v += FETCH ();
162 v <<= 8; v += FETCH ();
163 v <<= 8; v += FETCH ();
164 v <<= 8; v += FETCH ();
165 v <<= 8; v += FETCH ();
166 PUSH (scm_from_uint64 (v));
167 NEXT;
168 }
169
170 VM_DEFINE_INSTRUCTION (15, make_char8, "make-char8", 1, 0, 1)
171 {
172 scm_t_uint8 v = 0;
173 v = FETCH ();
174
175 PUSH (SCM_MAKE_CHAR (v));
176 /* Don't simplify this to PUSH (SCM_MAKE_CHAR (FETCH ())). The
177 contents of SCM_MAKE_CHAR may be evaluated more than once,
178 resulting in a double fetch. */
179 NEXT;
180 }
181
182 VM_DEFINE_INSTRUCTION (16, make_char32, "make-char32", 4, 0, 1)
183 {
184 scm_t_wchar v = 0;
185 v += FETCH ();
186 v <<= 8; v += FETCH ();
187 v <<= 8; v += FETCH ();
188 v <<= 8; v += FETCH ();
189 PUSH (SCM_MAKE_CHAR (v));
190 NEXT;
191 }
192
193
194
195 VM_DEFINE_INSTRUCTION (17, list, "list", 2, -1, 1)
196 {
197 unsigned h = FETCH ();
198 unsigned l = FETCH ();
199 unsigned len = ((h << 8) + l);
200 POP_LIST (len);
201 NEXT;
202 }
203
204 VM_DEFINE_INSTRUCTION (18, vector, "vector", 2, -1, 1)
205 {
206 unsigned h = FETCH ();
207 unsigned l = FETCH ();
208 unsigned len = ((h << 8) + l);
209 SCM vect;
210
211 SYNC_REGISTER ();
212 sp++; sp -= len;
213 CHECK_UNDERFLOW ();
214 vect = scm_make_vector (scm_from_uint (len), SCM_BOOL_F);
215 memcpy (SCM_I_VECTOR_WELTS(vect), sp, sizeof(SCM) * len);
216 NULLSTACK (len);
217 *sp = vect;
218
219 NEXT;
220 }
221
222 \f
223 /*
224 * Variable access
225 */
226
227 #define OBJECT_REF(i) objects[i]
228 #define OBJECT_SET(i,o) objects[i] = o
229
230 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
231 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
232
233 /* For the variable operations, we _must_ obviously avoid function calls to
234 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
235 nothing more than the corresponding macros. */
236 #define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
237 #define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
238 #define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
239
240 #define FREE_VARIABLE_REF(i) free_vars[i]
241
242 /* ref */
243
244 VM_DEFINE_INSTRUCTION (19, object_ref, "object-ref", 1, 0, 1)
245 {
246 register unsigned objnum = FETCH ();
247 CHECK_OBJECT (objnum);
248 PUSH (OBJECT_REF (objnum));
249 NEXT;
250 }
251
252 /* FIXME: necessary? elt 255 of the vector could be a vector... */
253 VM_DEFINE_INSTRUCTION (20, long_object_ref, "long-object-ref", 2, 0, 1)
254 {
255 unsigned int objnum = FETCH ();
256 objnum <<= 8;
257 objnum += FETCH ();
258 CHECK_OBJECT (objnum);
259 PUSH (OBJECT_REF (objnum));
260 NEXT;
261 }
262
263 VM_DEFINE_INSTRUCTION (21, local_ref, "local-ref", 1, 0, 1)
264 {
265 PUSH (LOCAL_REF (FETCH ()));
266 ASSERT_BOUND (*sp);
267 NEXT;
268 }
269
270 VM_DEFINE_INSTRUCTION (22, long_local_ref, "long-local-ref", 2, 0, 1)
271 {
272 unsigned int i = FETCH ();
273 i <<= 8;
274 i += FETCH ();
275 PUSH (LOCAL_REF (i));
276 ASSERT_BOUND (*sp);
277 NEXT;
278 }
279
280 VM_DEFINE_INSTRUCTION (23, local_bound, "local-bound?", 1, 0, 1)
281 {
282 if (LOCAL_REF (FETCH ()) == SCM_UNDEFINED)
283 PUSH (SCM_BOOL_F);
284 else
285 PUSH (SCM_BOOL_T);
286 NEXT;
287 }
288
289 VM_DEFINE_INSTRUCTION (24, long_local_bound, "long-local-bound?", 2, 0, 1)
290 {
291 unsigned int i = FETCH ();
292 i <<= 8;
293 i += FETCH ();
294 if (LOCAL_REF (i) == SCM_UNDEFINED)
295 PUSH (SCM_BOOL_F);
296 else
297 PUSH (SCM_BOOL_T);
298 NEXT;
299 }
300
301 VM_DEFINE_INSTRUCTION (25, variable_ref, "variable-ref", 0, 0, 1)
302 {
303 SCM x = *sp;
304
305 if (!VARIABLE_BOUNDP (x))
306 {
307 finish_args = scm_list_1 (x);
308 /* Was: finish_args = SCM_LIST1 (SCM_CAR (x)); */
309 goto vm_error_unbound;
310 }
311 else
312 {
313 SCM o = VARIABLE_REF (x);
314 *sp = o;
315 }
316
317 NEXT;
318 }
319
320 VM_DEFINE_INSTRUCTION (26, variable_bound, "variable-bound?", 0, 0, 1)
321 {
322 if (VARIABLE_BOUNDP (*sp))
323 *sp = SCM_BOOL_T;
324 else
325 *sp = SCM_BOOL_F;
326 NEXT;
327 }
328
329 VM_DEFINE_INSTRUCTION (27, toplevel_ref, "toplevel-ref", 1, 0, 1)
330 {
331 unsigned objnum = FETCH ();
332 SCM what;
333 CHECK_OBJECT (objnum);
334 what = OBJECT_REF (objnum);
335
336 if (!SCM_VARIABLEP (what))
337 {
338 SYNC_REGISTER ();
339 what = resolve_variable (what, scm_program_module (program));
340 if (!VARIABLE_BOUNDP (what))
341 {
342 finish_args = scm_list_1 (what);
343 goto vm_error_unbound;
344 }
345 OBJECT_SET (objnum, what);
346 }
347
348 PUSH (VARIABLE_REF (what));
349 NEXT;
350 }
351
352 VM_DEFINE_INSTRUCTION (28, long_toplevel_ref, "long-toplevel-ref", 2, 0, 1)
353 {
354 SCM what;
355 unsigned int objnum = FETCH ();
356 objnum <<= 8;
357 objnum += FETCH ();
358 CHECK_OBJECT (objnum);
359 what = OBJECT_REF (objnum);
360
361 if (!SCM_VARIABLEP (what))
362 {
363 SYNC_REGISTER ();
364 what = resolve_variable (what, scm_program_module (program));
365 if (!VARIABLE_BOUNDP (what))
366 {
367 finish_args = scm_list_1 (what);
368 goto vm_error_unbound;
369 }
370 OBJECT_SET (objnum, what);
371 }
372
373 PUSH (VARIABLE_REF (what));
374 NEXT;
375 }
376
377 /* set */
378
379 VM_DEFINE_INSTRUCTION (29, local_set, "local-set", 1, 1, 0)
380 {
381 LOCAL_SET (FETCH (), *sp);
382 DROP ();
383 NEXT;
384 }
385
386 VM_DEFINE_INSTRUCTION (30, long_local_set, "long-local-set", 2, 1, 0)
387 {
388 unsigned int i = FETCH ();
389 i <<= 8;
390 i += FETCH ();
391 LOCAL_SET (i, *sp);
392 DROP ();
393 NEXT;
394 }
395
396 VM_DEFINE_INSTRUCTION (31, variable_set, "variable-set", 0, 1, 0)
397 {
398 VARIABLE_SET (sp[0], sp[-1]);
399 DROPN (2);
400 NEXT;
401 }
402
403 VM_DEFINE_INSTRUCTION (32, toplevel_set, "toplevel-set", 1, 1, 0)
404 {
405 unsigned objnum = FETCH ();
406 SCM what;
407 CHECK_OBJECT (objnum);
408 what = OBJECT_REF (objnum);
409
410 if (!SCM_VARIABLEP (what))
411 {
412 SYNC_BEFORE_GC ();
413 what = resolve_variable (what, scm_program_module (program));
414 OBJECT_SET (objnum, what);
415 }
416
417 VARIABLE_SET (what, *sp);
418 DROP ();
419 NEXT;
420 }
421
422 VM_DEFINE_INSTRUCTION (33, long_toplevel_set, "long-toplevel-set", 2, 1, 0)
423 {
424 SCM what;
425 unsigned int objnum = FETCH ();
426 objnum <<= 8;
427 objnum += FETCH ();
428 CHECK_OBJECT (objnum);
429 what = OBJECT_REF (objnum);
430
431 if (!SCM_VARIABLEP (what))
432 {
433 SYNC_BEFORE_GC ();
434 what = resolve_variable (what, scm_program_module (program));
435 OBJECT_SET (objnum, what);
436 }
437
438 VARIABLE_SET (what, *sp);
439 DROP ();
440 NEXT;
441 }
442
443 \f
444 /*
445 * branch and jump
446 */
447
448 /* offset must be at least 24 bits wide, and signed */
449 #define FETCH_OFFSET(offset) \
450 { \
451 offset = FETCH () << 16; \
452 offset += FETCH () << 8; \
453 offset += FETCH (); \
454 offset -= (offset & (1<<23)) << 1; \
455 }
456
457 #define BR(p) \
458 { \
459 scm_t_int32 offset; \
460 FETCH_OFFSET (offset); \
461 if (p) \
462 ip += offset; \
463 NULLSTACK (1); \
464 DROP (); \
465 NEXT; \
466 }
467
468 VM_DEFINE_INSTRUCTION (34, br, "br", 3, 0, 0)
469 {
470 scm_t_int32 offset;
471 FETCH_OFFSET (offset);
472 ip += offset;
473 NEXT;
474 }
475
476 VM_DEFINE_INSTRUCTION (35, br_if, "br-if", 3, 0, 0)
477 {
478 BR (scm_is_true_and_not_nil (*sp));
479 }
480
481 VM_DEFINE_INSTRUCTION (36, br_if_not, "br-if-not", 3, 0, 0)
482 {
483 BR (scm_is_false_or_nil (*sp));
484 }
485
486 VM_DEFINE_INSTRUCTION (37, br_if_eq, "br-if-eq", 3, 0, 0)
487 {
488 sp--; /* underflow? */
489 BR (scm_is_eq (sp[0], sp[1]));
490 }
491
492 VM_DEFINE_INSTRUCTION (38, br_if_not_eq, "br-if-not-eq", 3, 0, 0)
493 {
494 sp--; /* underflow? */
495 BR (!scm_is_eq (sp[0], sp[1]));
496 }
497
498 VM_DEFINE_INSTRUCTION (39, br_if_null, "br-if-null", 3, 0, 0)
499 {
500 BR (scm_is_null_or_nil (*sp));
501 }
502
503 VM_DEFINE_INSTRUCTION (40, br_if_not_null, "br-if-not-null", 3, 0, 0)
504 {
505 BR (!scm_is_null_or_nil (*sp));
506 }
507
508 \f
509 /*
510 * Subprogram call
511 */
512
513 VM_DEFINE_INSTRUCTION (41, br_if_nargs_ne, "br-if-nargs-ne", 5, 0, 0)
514 {
515 scm_t_ptrdiff n;
516 scm_t_int32 offset;
517 n = FETCH () << 8;
518 n += FETCH ();
519 FETCH_OFFSET (offset);
520 if (sp - (fp - 1) != n)
521 ip += offset;
522 NEXT;
523 }
524
525 VM_DEFINE_INSTRUCTION (42, br_if_nargs_lt, "br-if-nargs-lt", 5, 0, 0)
526 {
527 scm_t_ptrdiff n;
528 scm_t_int32 offset;
529 n = FETCH () << 8;
530 n += FETCH ();
531 FETCH_OFFSET (offset);
532 if (sp - (fp - 1) < n)
533 ip += offset;
534 NEXT;
535 }
536
537 VM_DEFINE_INSTRUCTION (43, br_if_nargs_gt, "br-if-nargs-gt", 5, 0, 0)
538 {
539 scm_t_ptrdiff n;
540 scm_t_int32 offset;
541
542 n = FETCH () << 8;
543 n += FETCH ();
544 FETCH_OFFSET (offset);
545 if (sp - (fp - 1) > n)
546 ip += offset;
547 NEXT;
548 }
549
550 VM_DEFINE_INSTRUCTION (44, assert_nargs_ee, "assert-nargs-ee", 2, 0, 0)
551 {
552 scm_t_ptrdiff n;
553 n = FETCH () << 8;
554 n += FETCH ();
555 if (sp - (fp - 1) != n)
556 goto vm_error_wrong_num_args;
557 NEXT;
558 }
559
560 VM_DEFINE_INSTRUCTION (45, assert_nargs_ge, "assert-nargs-ge", 2, 0, 0)
561 {
562 scm_t_ptrdiff n;
563 n = FETCH () << 8;
564 n += FETCH ();
565 if (sp - (fp - 1) < n)
566 goto vm_error_wrong_num_args;
567 NEXT;
568 }
569
570 VM_DEFINE_INSTRUCTION (46, bind_optionals, "bind-optionals", 2, -1, -1)
571 {
572 scm_t_ptrdiff n;
573 n = FETCH () << 8;
574 n += FETCH ();
575 while (sp - (fp - 1) < n)
576 PUSH (SCM_UNDEFINED);
577 NEXT;
578 }
579
580 VM_DEFINE_INSTRUCTION (47, bind_optionals_shuffle, "bind-optionals/shuffle", 6, -1, -1)
581 {
582 SCM *walk;
583 scm_t_ptrdiff nreq, nreq_and_opt, ntotal;
584 nreq = FETCH () << 8;
585 nreq += FETCH ();
586 nreq_and_opt = FETCH () << 8;
587 nreq_and_opt += FETCH ();
588 ntotal = FETCH () << 8;
589 ntotal += FETCH ();
590
591 /* look in optionals for first keyword or last positional */
592 /* starting after the last required positional arg */
593 walk = fp + nreq;
594 while (/* while we have args */
595 walk <= sp
596 /* and we still have positionals to fill */
597 && walk - fp < nreq_and_opt
598 /* and we haven't reached a keyword yet */
599 && !scm_is_keyword (*walk))
600 /* bind this optional arg (by leaving it in place) */
601 walk++;
602 /* now shuffle up, from walk to ntotal */
603 {
604 scm_t_ptrdiff nshuf = sp - walk + 1, i;
605 sp = (fp - 1) + ntotal + nshuf;
606 CHECK_OVERFLOW ();
607 for (i = 0; i < nshuf; i++)
608 sp[-i] = walk[nshuf-i-1];
609 }
610 /* and fill optionals & keyword args with SCM_UNDEFINED */
611 while (walk <= (fp - 1) + ntotal)
612 *walk++ = SCM_UNDEFINED;
613
614 NEXT;
615 }
616
617 /* Flags that determine whether other keywords are allowed, and whether a
618 rest argument is expected. These values must match those used by the
619 glil->assembly compiler. */
620 #define F_ALLOW_OTHER_KEYS 1
621 #define F_REST 2
622
623 VM_DEFINE_INSTRUCTION (48, bind_kwargs, "bind-kwargs", 5, 0, 0)
624 {
625 scm_t_uint16 idx;
626 scm_t_ptrdiff nkw;
627 int kw_and_rest_flags;
628 SCM kw;
629 idx = FETCH () << 8;
630 idx += FETCH ();
631 /* XXX: We don't actually use NKW. */
632 nkw = FETCH () << 8;
633 nkw += FETCH ();
634 kw_and_rest_flags = FETCH ();
635
636 if (!(kw_and_rest_flags & F_REST)
637 && ((sp - (fp - 1) - nkw) % 2))
638 goto vm_error_kwargs_length_not_even;
639
640 CHECK_OBJECT (idx);
641 kw = OBJECT_REF (idx);
642
643 /* Switch NKW to be a negative index below SP. */
644 for (nkw = -(sp - (fp - 1) - nkw) + 1; nkw < 0; nkw++)
645 {
646 SCM walk;
647
648 if (scm_is_keyword (sp[nkw]))
649 {
650 for (walk = kw; scm_is_pair (walk); walk = SCM_CDR (walk))
651 {
652 if (scm_is_eq (SCM_CAAR (walk), sp[nkw]))
653 {
654 SCM si = SCM_CDAR (walk);
655 LOCAL_SET (SCM_I_INUMP (si) ? SCM_I_INUM (si) : scm_to_long (si),
656 sp[nkw + 1]);
657 break;
658 }
659 }
660 if (!(kw_and_rest_flags & F_ALLOW_OTHER_KEYS) && !scm_is_pair (walk))
661 goto vm_error_kwargs_unrecognized_keyword;
662
663 nkw++;
664 }
665 else if (!(kw_and_rest_flags & F_REST))
666 goto vm_error_kwargs_invalid_keyword;
667 }
668
669 NEXT;
670 }
671
672 #undef F_ALLOW_OTHER_KEYS
673 #undef F_REST
674
675
676 VM_DEFINE_INSTRUCTION (49, push_rest, "push-rest", 2, -1, -1)
677 {
678 scm_t_ptrdiff n;
679 SCM rest = SCM_EOL;
680 n = FETCH () << 8;
681 n += FETCH ();
682 while (sp - (fp - 1) > n)
683 /* No need to check for underflow. */
684 CONS (rest, *sp--, rest);
685 PUSH (rest);
686 NEXT;
687 }
688
689 VM_DEFINE_INSTRUCTION (50, bind_rest, "bind-rest", 4, -1, -1)
690 {
691 scm_t_ptrdiff n;
692 scm_t_uint32 i;
693 SCM rest = SCM_EOL;
694 n = FETCH () << 8;
695 n += FETCH ();
696 i = FETCH () << 8;
697 i += FETCH ();
698 while (sp - (fp - 1) > n)
699 /* No need to check for underflow. */
700 CONS (rest, *sp--, rest);
701 LOCAL_SET (i, rest);
702 NEXT;
703 }
704
705 VM_DEFINE_INSTRUCTION (51, reserve_locals, "reserve-locals", 2, -1, -1)
706 {
707 SCM *old_sp;
708 scm_t_int32 n;
709 n = FETCH () << 8;
710 n += FETCH ();
711 old_sp = sp;
712 sp = (fp - 1) + n;
713
714 if (old_sp < sp)
715 {
716 CHECK_OVERFLOW ();
717 while (old_sp < sp)
718 *++old_sp = SCM_UNDEFINED;
719 }
720 else
721 NULLSTACK (old_sp - sp);
722
723 NEXT;
724 }
725
726 VM_DEFINE_INSTRUCTION (52, new_frame, "new-frame", 0, 0, 3)
727 {
728 /* NB: if you change this, see frames.c:vm-frame-num-locals */
729 /* and frames.h, vm-engine.c, etc of course */
730 PUSH ((SCM)fp); /* dynamic link */
731 PUSH (0); /* mvra */
732 PUSH (0); /* ra */
733 NEXT;
734 }
735
736 VM_DEFINE_INSTRUCTION (53, call, "call", 1, -1, 1)
737 {
738 SCM x;
739 nargs = FETCH ();
740
741 vm_call:
742 x = sp[-nargs];
743
744 SYNC_REGISTER ();
745 SCM_TICK; /* allow interrupt here */
746
747 /*
748 * Subprogram call
749 */
750 if (SCM_PROGRAM_P (x))
751 {
752 program = x;
753 CACHE_PROGRAM ();
754 fp = sp - nargs + 1;
755 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0);
756 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0);
757 SCM_FRAME_SET_RETURN_ADDRESS (fp, ip);
758 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, 0);
759 ip = bp->base;
760 ENTER_HOOK ();
761 APPLY_HOOK ();
762 NEXT;
763 }
764 if (SCM_STRUCTP (x) && SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_PURE_GENERIC)
765 {
766 SCM args = SCM_EOL;
767 int n = nargs;
768 SCM* walk = sp;
769 SYNC_REGISTER ();
770 while (n--)
771 args = scm_cons (*walk--, args);
772 *walk = scm_mcache_compute_cmethod (SCM_GENERIC_METHOD_CACHE (x), args);
773 goto vm_call;
774 }
775 /*
776 * Other interpreted or compiled call
777 */
778 if (!SCM_FALSEP (scm_procedure_p (x)))
779 {
780 SCM args;
781 /* At this point, the stack contains the frame, the procedure and each one
782 of its arguments. */
783 POP_LIST (nargs);
784 POP (args);
785 DROP (); /* drop the procedure */
786 DROP_FRAME ();
787
788 SYNC_REGISTER ();
789 PUSH (scm_apply (x, args, SCM_EOL));
790 NULLSTACK_FOR_NONLOCAL_EXIT ();
791 if (SCM_UNLIKELY (SCM_VALUESP (*sp)))
792 {
793 /* truncate values */
794 SCM values;
795 POP (values);
796 values = scm_struct_ref (values, SCM_INUM0);
797 if (scm_is_null (values))
798 goto vm_error_not_enough_values;
799 PUSH (SCM_CAR (values));
800 }
801 NEXT;
802 }
803
804 program = x;
805 goto vm_error_wrong_type_apply;
806 }
807
808 VM_DEFINE_INSTRUCTION (54, goto_args, "goto/args", 1, -1, 1)
809 {
810 register SCM x;
811 nargs = FETCH ();
812 vm_goto_args:
813 x = sp[-nargs];
814
815 SYNC_REGISTER ();
816 SCM_TICK; /* allow interrupt here */
817
818 /*
819 * Tail call
820 */
821 if (SCM_PROGRAM_P (x))
822 {
823 int i;
824 #ifdef VM_ENABLE_STACK_NULLING
825 SCM *old_sp = sp;
826 CHECK_STACK_LEAK ();
827 #endif
828
829 EXIT_HOOK ();
830
831 /* switch programs */
832 program = x;
833 CACHE_PROGRAM ();
834 /* shuffle down the program and the arguments */
835 for (i = -1, sp = sp - nargs + 1; i < nargs; i++)
836 SCM_FRAME_STACK_ADDRESS (fp)[i] = sp[i];
837
838 sp = fp + i - 1;
839
840 NULLSTACK (old_sp - sp);
841
842 ip = bp->base;
843
844 ENTER_HOOK ();
845 APPLY_HOOK ();
846 NEXT;
847 }
848 if (SCM_STRUCTP (x) && SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_PURE_GENERIC)
849 {
850 SCM args = SCM_EOL;
851 int n = nargs;
852 SCM* walk = sp;
853 SYNC_REGISTER ();
854 while (n--)
855 args = scm_cons (*walk--, args);
856 *walk = scm_mcache_compute_cmethod (SCM_GENERIC_METHOD_CACHE (x), args);
857 goto vm_goto_args;
858 }
859
860 /*
861 * Other interpreted or compiled call
862 */
863 if (!SCM_FALSEP (scm_procedure_p (x)))
864 {
865 SCM args;
866 POP_LIST (nargs);
867 POP (args);
868
869 SYNC_REGISTER ();
870 *sp = scm_apply (x, args, SCM_EOL);
871 NULLSTACK_FOR_NONLOCAL_EXIT ();
872
873 if (SCM_UNLIKELY (SCM_VALUESP (*sp)))
874 {
875 /* multiple values returned to continuation */
876 SCM values;
877 POP (values);
878 values = scm_struct_ref (values, SCM_INUM0);
879 nvalues = scm_ilength (values);
880 PUSH_LIST (values, scm_is_null);
881 goto vm_return_values;
882 }
883 else
884 goto vm_return;
885 }
886
887 program = x;
888
889 goto vm_error_wrong_type_apply;
890 }
891
892 VM_DEFINE_INSTRUCTION (55, goto_nargs, "goto/nargs", 0, 0, 1)
893 {
894 SCM x;
895 POP (x);
896 nargs = scm_to_int (x);
897 /* FIXME: should truncate values? */
898 goto vm_goto_args;
899 }
900
901 VM_DEFINE_INSTRUCTION (56, call_nargs, "call/nargs", 0, 0, 1)
902 {
903 SCM x;
904 POP (x);
905 nargs = scm_to_int (x);
906 /* FIXME: should truncate values? */
907 goto vm_call;
908 }
909
910 VM_DEFINE_INSTRUCTION (57, mv_call, "mv-call", 4, -1, 1)
911 {
912 SCM x;
913 scm_t_int32 offset;
914 scm_t_uint8 *mvra;
915
916 nargs = FETCH ();
917 FETCH_OFFSET (offset);
918 mvra = ip + offset;
919
920 vm_mv_call:
921 x = sp[-nargs];
922
923 /*
924 * Subprogram call
925 */
926 if (SCM_PROGRAM_P (x))
927 {
928 program = x;
929 CACHE_PROGRAM ();
930 fp = sp - nargs + 1;
931 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0);
932 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0);
933 SCM_FRAME_SET_RETURN_ADDRESS (fp, ip);
934 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, mvra);
935 ip = bp->base;
936 ENTER_HOOK ();
937 APPLY_HOOK ();
938 NEXT;
939 }
940 if (SCM_STRUCTP (x) && SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_PURE_GENERIC)
941 {
942 SCM args = SCM_EOL;
943 int n = nargs;
944 SCM* walk = sp;
945 SYNC_REGISTER ();
946 while (n--)
947 args = scm_cons (*walk--, args);
948 *walk = scm_mcache_compute_cmethod (SCM_GENERIC_METHOD_CACHE (x), args);
949 goto vm_mv_call;
950 }
951 /*
952 * Other interpreted or compiled call
953 */
954 if (!SCM_FALSEP (scm_procedure_p (x)))
955 {
956 SCM args;
957 /* At this point, the stack contains the procedure and each one of its
958 arguments. */
959 POP_LIST (nargs);
960 POP (args);
961 DROP (); /* drop the procedure */
962 DROP_FRAME ();
963
964 SYNC_REGISTER ();
965 PUSH (scm_apply (x, args, SCM_EOL));
966 NULLSTACK_FOR_NONLOCAL_EXIT ();
967 if (SCM_VALUESP (*sp))
968 {
969 SCM values, len;
970 POP (values);
971 values = scm_struct_ref (values, SCM_INUM0);
972 len = scm_length (values);
973 PUSH_LIST (values, scm_is_null);
974 PUSH (len);
975 ip = mvra;
976 }
977 NEXT;
978 }
979
980 program = x;
981 goto vm_error_wrong_type_apply;
982 }
983
984 VM_DEFINE_INSTRUCTION (58, apply, "apply", 1, -1, 1)
985 {
986 int len;
987 SCM ls;
988 POP (ls);
989
990 nargs = FETCH ();
991 ASSERT (nargs >= 2);
992
993 len = scm_ilength (ls);
994 if (len < 0)
995 goto vm_error_wrong_type_arg;
996
997 PUSH_LIST (ls, SCM_NULL_OR_NIL_P);
998
999 nargs += len - 2;
1000 goto vm_call;
1001 }
1002
1003 VM_DEFINE_INSTRUCTION (59, goto_apply, "goto/apply", 1, -1, 1)
1004 {
1005 int len;
1006 SCM ls;
1007 POP (ls);
1008
1009 nargs = FETCH ();
1010 ASSERT (nargs >= 2);
1011
1012 len = scm_ilength (ls);
1013 if (len < 0)
1014 goto vm_error_wrong_type_arg;
1015
1016 PUSH_LIST (ls, SCM_NULL_OR_NIL_P);
1017
1018 nargs += len - 2;
1019 goto vm_goto_args;
1020 }
1021
1022 VM_DEFINE_INSTRUCTION (60, call_cc, "call/cc", 0, 1, 1)
1023 {
1024 int first;
1025 SCM proc, cont;
1026 POP (proc);
1027 SYNC_ALL ();
1028 cont = scm_make_continuation (&first);
1029 if (first)
1030 {
1031 PUSH ((SCM)fp); /* dynamic link */
1032 PUSH (0); /* mvra */
1033 PUSH (0); /* ra */
1034 PUSH (proc);
1035 PUSH (cont);
1036 nargs = 1;
1037 goto vm_call;
1038 }
1039 ASSERT (sp == vp->sp);
1040 ASSERT (fp == vp->fp);
1041 else if (SCM_VALUESP (cont))
1042 {
1043 /* multiple values returned to continuation */
1044 SCM values;
1045 values = scm_struct_ref (cont, SCM_INUM0);
1046 if (scm_is_null (values))
1047 goto vm_error_no_values;
1048 /* non-tail context does not accept multiple values? */
1049 PUSH (SCM_CAR (values));
1050 NEXT;
1051 }
1052 else
1053 {
1054 PUSH (cont);
1055 NEXT;
1056 }
1057 }
1058
1059 VM_DEFINE_INSTRUCTION (61, goto_cc, "goto/cc", 0, 1, 1)
1060 {
1061 int first;
1062 SCM proc, cont;
1063 POP (proc);
1064 SYNC_ALL ();
1065 cont = scm_make_continuation (&first);
1066 ASSERT (sp == vp->sp);
1067 ASSERT (fp == vp->fp);
1068 if (first)
1069 {
1070 PUSH (proc);
1071 PUSH (cont);
1072 nargs = 1;
1073 goto vm_goto_args;
1074 }
1075 else if (SCM_VALUESP (cont))
1076 {
1077 /* multiple values returned to continuation */
1078 SCM values;
1079 values = scm_struct_ref (cont, SCM_INUM0);
1080 nvalues = scm_ilength (values);
1081 PUSH_LIST (values, scm_is_null);
1082 goto vm_return_values;
1083 }
1084 else
1085 {
1086 PUSH (cont);
1087 goto vm_return;
1088 }
1089 }
1090
1091 VM_DEFINE_INSTRUCTION (62, return, "return", 0, 1, 1)
1092 {
1093 vm_return:
1094 EXIT_HOOK ();
1095 RETURN_HOOK ();
1096 SYNC_REGISTER ();
1097 SCM_TICK; /* allow interrupt here */
1098 {
1099 SCM ret;
1100
1101 POP (ret);
1102
1103 #ifdef VM_ENABLE_STACK_NULLING
1104 SCM *old_sp = sp;
1105 #endif
1106
1107 /* Restore registers */
1108 sp = SCM_FRAME_LOWER_ADDRESS (fp);
1109 ip = SCM_FRAME_RETURN_ADDRESS (fp);
1110 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1111
1112 #ifdef VM_ENABLE_STACK_NULLING
1113 NULLSTACK (old_sp - sp);
1114 #endif
1115
1116 /* Set return value (sp is already pushed) */
1117 *sp = ret;
1118 }
1119
1120 /* Restore the last program */
1121 program = SCM_FRAME_PROGRAM (fp);
1122 CACHE_PROGRAM ();
1123 CHECK_IP ();
1124 NEXT;
1125 }
1126
1127 VM_DEFINE_INSTRUCTION (63, return_values, "return/values", 1, -1, -1)
1128 {
1129 /* nvalues declared at top level, because for some reason gcc seems to think
1130 that perhaps it might be used without declaration. Fooey to that, I say. */
1131 nvalues = FETCH ();
1132 vm_return_values:
1133 EXIT_HOOK ();
1134 RETURN_HOOK ();
1135
1136 if (nvalues != 1 && SCM_FRAME_MV_RETURN_ADDRESS (fp))
1137 {
1138 /* A multiply-valued continuation */
1139 SCM *vals = sp - nvalues;
1140 int i;
1141 /* Restore registers */
1142 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1143 ip = SCM_FRAME_MV_RETURN_ADDRESS (fp);
1144 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1145
1146 /* Push return values, and the number of values */
1147 for (i = 0; i < nvalues; i++)
1148 *++sp = vals[i+1];
1149 *++sp = SCM_I_MAKINUM (nvalues);
1150
1151 /* Finally null the end of the stack */
1152 NULLSTACK (vals + nvalues - sp);
1153 }
1154 else if (nvalues >= 1)
1155 {
1156 /* Multiple values for a single-valued continuation -- here's where I
1157 break with guile tradition and try and do something sensible. (Also,
1158 this block handles the single-valued return to an mv
1159 continuation.) */
1160 SCM *vals = sp - nvalues;
1161 /* Restore registers */
1162 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1163 ip = SCM_FRAME_RETURN_ADDRESS (fp);
1164 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1165
1166 /* Push first value */
1167 *++sp = vals[1];
1168
1169 /* Finally null the end of the stack */
1170 NULLSTACK (vals + nvalues - sp);
1171 }
1172 else
1173 goto vm_error_no_values;
1174
1175 /* Restore the last program */
1176 program = SCM_FRAME_PROGRAM (fp);
1177 CACHE_PROGRAM ();
1178 CHECK_IP ();
1179 NEXT;
1180 }
1181
1182 VM_DEFINE_INSTRUCTION (64, return_values_star, "return/values*", 1, -1, -1)
1183 {
1184 SCM l;
1185
1186 nvalues = FETCH ();
1187 ASSERT (nvalues >= 1);
1188
1189 nvalues--;
1190 POP (l);
1191 while (scm_is_pair (l))
1192 {
1193 PUSH (SCM_CAR (l));
1194 l = SCM_CDR (l);
1195 nvalues++;
1196 }
1197 if (SCM_UNLIKELY (!SCM_NULL_OR_NIL_P (l))) {
1198 finish_args = scm_list_1 (l);
1199 goto vm_error_improper_list;
1200 }
1201
1202 goto vm_return_values;
1203 }
1204
1205 VM_DEFINE_INSTRUCTION (65, truncate_values, "truncate-values", 2, -1, -1)
1206 {
1207 SCM x;
1208 int nbinds, rest;
1209 POP (x);
1210 nvalues = scm_to_int (x);
1211 nbinds = FETCH ();
1212 rest = FETCH ();
1213
1214 if (rest)
1215 nbinds--;
1216
1217 if (nvalues < nbinds)
1218 goto vm_error_not_enough_values;
1219
1220 if (rest)
1221 POP_LIST (nvalues - nbinds);
1222 else
1223 DROPN (nvalues - nbinds);
1224
1225 NEXT;
1226 }
1227
1228 VM_DEFINE_INSTRUCTION (66, box, "box", 1, 1, 0)
1229 {
1230 SCM val;
1231 POP (val);
1232 SYNC_BEFORE_GC ();
1233 LOCAL_SET (FETCH (), scm_cell (scm_tc7_variable, SCM_UNPACK (val)));
1234 NEXT;
1235 }
1236
1237 /* for letrec:
1238 (let ((a *undef*) (b *undef*) ...)
1239 (set! a (lambda () (b ...)))
1240 ...)
1241 */
1242 VM_DEFINE_INSTRUCTION (67, empty_box, "empty-box", 1, 0, 0)
1243 {
1244 SYNC_BEFORE_GC ();
1245 LOCAL_SET (FETCH (),
1246 scm_cell (scm_tc7_variable, SCM_UNPACK (SCM_UNDEFINED)));
1247 NEXT;
1248 }
1249
1250 VM_DEFINE_INSTRUCTION (68, local_boxed_ref, "local-boxed-ref", 1, 0, 1)
1251 {
1252 SCM v = LOCAL_REF (FETCH ());
1253 ASSERT_BOUND_VARIABLE (v);
1254 PUSH (VARIABLE_REF (v));
1255 NEXT;
1256 }
1257
1258 VM_DEFINE_INSTRUCTION (69, local_boxed_set, "local-boxed-set", 1, 1, 0)
1259 {
1260 SCM v, val;
1261 v = LOCAL_REF (FETCH ());
1262 POP (val);
1263 ASSERT_VARIABLE (v);
1264 VARIABLE_SET (v, val);
1265 NEXT;
1266 }
1267
1268 VM_DEFINE_INSTRUCTION (70, free_ref, "free-ref", 1, 0, 1)
1269 {
1270 scm_t_uint8 idx = FETCH ();
1271
1272 CHECK_FREE_VARIABLE (idx);
1273 PUSH (FREE_VARIABLE_REF (idx));
1274 NEXT;
1275 }
1276
1277 /* no free-set -- if a var is assigned, it should be in a box */
1278
1279 VM_DEFINE_INSTRUCTION (71, free_boxed_ref, "free-boxed-ref", 1, 0, 1)
1280 {
1281 SCM v;
1282 scm_t_uint8 idx = FETCH ();
1283 CHECK_FREE_VARIABLE (idx);
1284 v = FREE_VARIABLE_REF (idx);
1285 ASSERT_BOUND_VARIABLE (v);
1286 PUSH (VARIABLE_REF (v));
1287 NEXT;
1288 }
1289
1290 VM_DEFINE_INSTRUCTION (72, free_boxed_set, "free-boxed-set", 1, 1, 0)
1291 {
1292 SCM v, val;
1293 scm_t_uint8 idx = FETCH ();
1294 POP (val);
1295 CHECK_FREE_VARIABLE (idx);
1296 v = FREE_VARIABLE_REF (idx);
1297 ASSERT_BOUND_VARIABLE (v);
1298 VARIABLE_SET (v, val);
1299 NEXT;
1300 }
1301
1302 VM_DEFINE_INSTRUCTION (73, make_closure, "make-closure", 0, 2, 1)
1303 {
1304 SCM vect;
1305 POP (vect);
1306 SYNC_BEFORE_GC ();
1307 /* fixme underflow */
1308 *sp = scm_double_cell (scm_tc7_program, (scm_t_bits)SCM_PROGRAM_OBJCODE (*sp),
1309 (scm_t_bits)SCM_PROGRAM_OBJTABLE (*sp), (scm_t_bits)vect);
1310 NEXT;
1311 }
1312
1313 VM_DEFINE_INSTRUCTION (74, make_variable, "make-variable", 0, 0, 1)
1314 {
1315 SYNC_BEFORE_GC ();
1316 /* fixme underflow */
1317 PUSH (scm_cell (scm_tc7_variable, SCM_UNPACK (SCM_UNDEFINED)));
1318 NEXT;
1319 }
1320
1321 VM_DEFINE_INSTRUCTION (75, fix_closure, "fix-closure", 2, 0, 1)
1322 {
1323 SCM x, vect;
1324 unsigned int i = FETCH ();
1325 i <<= 8;
1326 i += FETCH ();
1327 POP (vect);
1328 /* FIXME CHECK_LOCAL (i) */
1329 x = LOCAL_REF (i);
1330 /* FIXME ASSERT_PROGRAM (x); */
1331 SCM_SET_CELL_WORD_3 (x, vect);
1332 NEXT;
1333 }
1334
1335 VM_DEFINE_INSTRUCTION (76, define, "define", 0, 0, 2)
1336 {
1337 SCM sym, val;
1338 POP (sym);
1339 POP (val);
1340 SYNC_REGISTER ();
1341 VARIABLE_SET (scm_sym2var (sym, scm_current_module_lookup_closure (),
1342 SCM_BOOL_T),
1343 val);
1344 NEXT;
1345 }
1346
1347 VM_DEFINE_INSTRUCTION (77, make_keyword, "make-keyword", 0, 1, 1)
1348 {
1349 CHECK_UNDERFLOW ();
1350 SYNC_REGISTER ();
1351 *sp = scm_symbol_to_keyword (*sp);
1352 NEXT;
1353 }
1354
1355 VM_DEFINE_INSTRUCTION (78, make_symbol, "make-symbol", 0, 1, 1)
1356 {
1357 CHECK_UNDERFLOW ();
1358 SYNC_REGISTER ();
1359 *sp = scm_string_to_symbol (*sp);
1360 NEXT;
1361 }
1362
1363
1364 /*
1365 (defun renumber-ops ()
1366 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
1367 (interactive "")
1368 (save-excursion
1369 (let ((counter -1)) (goto-char (point-min))
1370 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
1371 (replace-match
1372 (number-to-string (setq counter (1+ counter)))
1373 t t nil 1)))))
1374 */
1375 /*
1376 Local Variables:
1377 c-file-style: "gnu"
1378 End:
1379 */