Merge branch 'bdw-gc-static-alloc'
[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_EQ_P (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_EQ_P (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 n = FETCH () << 8;
517 n += FETCH ();
518 scm_t_int32 offset;
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 n = FETCH () << 8;
529 n += FETCH ();
530 scm_t_int32 offset;
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 n = FETCH () << 8;
541 n += FETCH ();
542 scm_t_int32 offset;
543 FETCH_OFFSET (offset);
544 if (sp - (fp - 1) > n)
545 ip += offset;
546 NEXT;
547 }
548
549 VM_DEFINE_INSTRUCTION (44, assert_nargs_ee, "assert-nargs-ee", 2, 0, 0)
550 {
551 scm_t_ptrdiff n;
552 n = FETCH () << 8;
553 n += FETCH ();
554 if (sp - (fp - 1) != n)
555 goto vm_error_wrong_num_args;
556 NEXT;
557 }
558
559 VM_DEFINE_INSTRUCTION (45, assert_nargs_ge, "assert-nargs-ge", 2, 0, 0)
560 {
561 scm_t_ptrdiff n;
562 n = FETCH () << 8;
563 n += FETCH ();
564 if (sp - (fp - 1) < n)
565 goto vm_error_wrong_num_args;
566 NEXT;
567 }
568
569 VM_DEFINE_INSTRUCTION (46, bind_optionals, "bind-optionals", 2, -1, -1)
570 {
571 scm_t_ptrdiff n;
572 n = FETCH () << 8;
573 n += FETCH ();
574 while (sp - (fp - 1) < n)
575 PUSH (SCM_UNDEFINED);
576 NEXT;
577 }
578
579 VM_DEFINE_INSTRUCTION (47, bind_optionals_shuffle, "bind-optionals/shuffle", 6, -1, -1)
580 {
581 SCM *walk;
582 scm_t_ptrdiff nreq, nreq_and_opt, ntotal;
583 nreq = FETCH () << 8;
584 nreq += FETCH ();
585 nreq_and_opt = FETCH () << 8;
586 nreq_and_opt += FETCH ();
587 ntotal = FETCH () << 8;
588 ntotal += FETCH ();
589
590 /* look in optionals for first keyword or last positional */
591 /* starting after the last required positional arg */
592 walk = fp + nreq;
593 while (/* while we have args */
594 walk <= sp
595 /* and we still have positionals to fill */
596 && walk - fp < nreq_and_opt
597 /* and we haven't reached a keyword yet */
598 && !scm_is_keyword (*walk))
599 /* bind this optional arg (by leaving it in place) */
600 walk++;
601 /* now shuffle up, from walk to ntotal */
602 {
603 scm_t_ptrdiff nshuf = sp - walk + 1, i;
604 sp = (fp - 1) + ntotal + nshuf;
605 CHECK_OVERFLOW ();
606 for (i = 0; i < nshuf; i++)
607 sp[-i] = walk[nshuf-i-1];
608 }
609 /* and fill optionals & keyword args with SCM_UNDEFINED */
610 while (walk <= (fp - 1) + ntotal)
611 *walk++ = SCM_UNDEFINED;
612
613 NEXT;
614 }
615
616 VM_DEFINE_INSTRUCTION (48, bind_kwargs, "bind-kwargs", 5, 0, 0)
617 {
618 scm_t_uint16 idx;
619 scm_t_ptrdiff nkw;
620 int allow_other_keys_and_rest;
621 SCM kw;
622 idx = FETCH () << 8;
623 idx += FETCH ();
624 nkw = FETCH () << 8;
625 nkw += FETCH ();
626 allow_other_keys_and_rest = FETCH ();
627
628 if (!(allow_other_keys_and_rest & 2)
629 &&(sp - (fp - 1) - nkw) % 2)
630 goto vm_error_kwargs_length_not_even;
631
632 CHECK_OBJECT (idx);
633 kw = OBJECT_REF (idx);
634 /* switch nkw to be a negative index below sp */
635 for (nkw = -(sp - (fp - 1) - nkw) + 1; nkw < 0; nkw += 2)
636 {
637 SCM walk;
638 if (!scm_is_keyword (sp[nkw]))
639 {
640 if (allow_other_keys_and_rest & 2)
641 /* reached the end of keywords, but we have a rest arg; just cut
642 out */
643 break;
644 else
645 goto vm_error_kwargs_invalid_keyword;
646 }
647 for (walk = kw; scm_is_pair (walk); walk = SCM_CDR (walk))
648 {
649 if (scm_is_eq (SCM_CAAR (walk), sp[nkw]))
650 {
651 SCM si = SCM_CDAR (walk);
652 LOCAL_SET (SCM_I_INUMP (si) ? SCM_I_INUM (si) : scm_to_long (si),
653 sp[nkw + 1]);
654 break;
655 }
656 }
657 if (!(allow_other_keys_and_rest & 1) && !scm_is_pair (walk))
658 goto vm_error_kwargs_unrecognized_keyword;
659 }
660
661 NEXT;
662 }
663
664 VM_DEFINE_INSTRUCTION (49, push_rest, "push-rest", 2, -1, -1)
665 {
666 scm_t_ptrdiff n;
667 SCM rest = SCM_EOL;
668 n = FETCH () << 8;
669 n += FETCH ();
670 while (sp - (fp - 1) > n)
671 /* No need to check for underflow. */
672 CONS (rest, *sp--, rest);
673 PUSH (rest);
674 NEXT;
675 }
676
677 VM_DEFINE_INSTRUCTION (50, bind_rest, "bind-rest", 4, -1, -1)
678 {
679 scm_t_ptrdiff n;
680 scm_t_uint32 i;
681 SCM rest = SCM_EOL;
682 n = FETCH () << 8;
683 n += FETCH ();
684 i = FETCH () << 8;
685 i += FETCH ();
686 while (sp - (fp - 1) > n)
687 /* No need to check for underflow. */
688 CONS (rest, *sp--, rest);
689 LOCAL_SET (i, rest);
690 NEXT;
691 }
692
693 VM_DEFINE_INSTRUCTION (51, reserve_locals, "reserve-locals", 2, -1, -1)
694 {
695 SCM *old_sp;
696 scm_t_int32 n;
697 n = FETCH () << 8;
698 n += FETCH ();
699 old_sp = sp;
700 sp = (fp - 1) + n;
701
702 if (old_sp < sp)
703 {
704 CHECK_OVERFLOW ();
705 while (old_sp < sp)
706 *++old_sp = SCM_UNDEFINED;
707 }
708 else
709 NULLSTACK (old_sp - sp);
710
711 NEXT;
712 }
713
714 VM_DEFINE_INSTRUCTION (52, new_frame, "new-frame", 0, 0, 3)
715 {
716 /* NB: if you change this, see frames.c:vm-frame-num-locals */
717 /* and frames.h, vm-engine.c, etc of course */
718 PUSH ((SCM)fp); /* dynamic link */
719 PUSH (0); /* mvra */
720 PUSH (0); /* ra */
721 NEXT;
722 }
723
724 VM_DEFINE_INSTRUCTION (53, call, "call", 1, -1, 1)
725 {
726 SCM x;
727 nargs = FETCH ();
728
729 vm_call:
730 x = sp[-nargs];
731
732 SYNC_REGISTER ();
733 SCM_TICK; /* allow interrupt here */
734
735 /*
736 * Subprogram call
737 */
738 if (SCM_PROGRAM_P (x))
739 {
740 program = x;
741 CACHE_PROGRAM ();
742 fp = sp - nargs + 1;
743 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0);
744 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0);
745 SCM_FRAME_SET_RETURN_ADDRESS (fp, ip);
746 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, 0);
747 ip = bp->base;
748 ENTER_HOOK ();
749 APPLY_HOOK ();
750 NEXT;
751 }
752 /*
753 * Other interpreted or compiled call
754 */
755 if (!SCM_FALSEP (scm_procedure_p (x)))
756 {
757 SCM args;
758 /* At this point, the stack contains the frame, the procedure and each one
759 of its arguments. */
760 POP_LIST (nargs);
761 POP (args);
762 DROP (); /* drop the procedure */
763 DROP_FRAME ();
764
765 SYNC_REGISTER ();
766 PUSH (scm_apply (x, args, SCM_EOL));
767 NULLSTACK_FOR_NONLOCAL_EXIT ();
768 if (SCM_UNLIKELY (SCM_VALUESP (*sp)))
769 {
770 /* truncate values */
771 SCM values;
772 POP (values);
773 values = scm_struct_ref (values, SCM_INUM0);
774 if (scm_is_null (values))
775 goto vm_error_not_enough_values;
776 PUSH (SCM_CAR (values));
777 }
778 NEXT;
779 }
780
781 program = x;
782 goto vm_error_wrong_type_apply;
783 }
784
785 VM_DEFINE_INSTRUCTION (54, goto_args, "goto/args", 1, -1, 1)
786 {
787 register SCM x;
788 nargs = FETCH ();
789 vm_goto_args:
790 x = sp[-nargs];
791
792 SYNC_REGISTER ();
793 SCM_TICK; /* allow interrupt here */
794
795 /*
796 * Tail call
797 */
798 if (SCM_PROGRAM_P (x))
799 {
800 int i;
801 #ifdef VM_ENABLE_STACK_NULLING
802 SCM *old_sp = sp;
803 CHECK_STACK_LEAK ();
804 #endif
805
806 EXIT_HOOK ();
807
808 /* switch programs */
809 program = x;
810 CACHE_PROGRAM ();
811 /* shuffle down the program and the arguments */
812 for (i = -1, sp = sp - nargs + 1; i < nargs; i++)
813 SCM_FRAME_STACK_ADDRESS (fp)[i] = sp[i];
814
815 sp = fp + i - 1;
816
817 NULLSTACK (old_sp - sp);
818
819 ip = bp->base;
820
821 ENTER_HOOK ();
822 APPLY_HOOK ();
823 NEXT;
824 }
825
826 /*
827 * Other interpreted or compiled call
828 */
829 if (!SCM_FALSEP (scm_procedure_p (x)))
830 {
831 SCM args;
832 POP_LIST (nargs);
833 POP (args);
834
835 SYNC_REGISTER ();
836 *sp = scm_apply (x, args, SCM_EOL);
837 NULLSTACK_FOR_NONLOCAL_EXIT ();
838
839 if (SCM_UNLIKELY (SCM_VALUESP (*sp)))
840 {
841 /* multiple values returned to continuation */
842 SCM values;
843 POP (values);
844 values = scm_struct_ref (values, SCM_INUM0);
845 nvalues = scm_ilength (values);
846 PUSH_LIST (values, SCM_NULLP);
847 goto vm_return_values;
848 }
849 else
850 goto vm_return;
851 }
852
853 program = x;
854
855 goto vm_error_wrong_type_apply;
856 }
857
858 VM_DEFINE_INSTRUCTION (55, goto_nargs, "goto/nargs", 0, 0, 1)
859 {
860 SCM x;
861 POP (x);
862 nargs = scm_to_int (x);
863 /* FIXME: should truncate values? */
864 goto vm_goto_args;
865 }
866
867 VM_DEFINE_INSTRUCTION (56, call_nargs, "call/nargs", 0, 0, 1)
868 {
869 SCM x;
870 POP (x);
871 nargs = scm_to_int (x);
872 /* FIXME: should truncate values? */
873 goto vm_call;
874 }
875
876 VM_DEFINE_INSTRUCTION (57, mv_call, "mv-call", 4, -1, 1)
877 {
878 SCM x;
879 scm_t_int32 offset;
880 scm_t_uint8 *mvra;
881
882 nargs = FETCH ();
883 FETCH_OFFSET (offset);
884 mvra = ip + offset;
885
886 x = sp[-nargs];
887
888 /*
889 * Subprogram call
890 */
891 if (SCM_PROGRAM_P (x))
892 {
893 program = x;
894 CACHE_PROGRAM ();
895 fp = sp - nargs + 1;
896 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0);
897 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0);
898 SCM_FRAME_SET_RETURN_ADDRESS (fp, ip);
899 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, mvra);
900 ip = bp->base;
901 ENTER_HOOK ();
902 APPLY_HOOK ();
903 NEXT;
904 }
905 /*
906 * Other interpreted or compiled call
907 */
908 if (!SCM_FALSEP (scm_procedure_p (x)))
909 {
910 SCM args;
911 /* At this point, the stack contains the procedure and each one of its
912 arguments. */
913 POP_LIST (nargs);
914 POP (args);
915 DROP (); /* drop the procedure */
916 DROP_FRAME ();
917
918 SYNC_REGISTER ();
919 PUSH (scm_apply (x, args, SCM_EOL));
920 NULLSTACK_FOR_NONLOCAL_EXIT ();
921 if (SCM_VALUESP (*sp))
922 {
923 SCM values, len;
924 POP (values);
925 values = scm_struct_ref (values, SCM_INUM0);
926 len = scm_length (values);
927 PUSH_LIST (values, SCM_NULLP);
928 PUSH (len);
929 ip = mvra;
930 }
931 NEXT;
932 }
933
934 program = x;
935 goto vm_error_wrong_type_apply;
936 }
937
938 VM_DEFINE_INSTRUCTION (58, apply, "apply", 1, -1, 1)
939 {
940 int len;
941 SCM ls;
942 POP (ls);
943
944 nargs = FETCH ();
945 ASSERT (nargs >= 2);
946
947 len = scm_ilength (ls);
948 if (len < 0)
949 goto vm_error_wrong_type_arg;
950
951 PUSH_LIST (ls, SCM_NULL_OR_NIL_P);
952
953 nargs += len - 2;
954 goto vm_call;
955 }
956
957 VM_DEFINE_INSTRUCTION (59, goto_apply, "goto/apply", 1, -1, 1)
958 {
959 int len;
960 SCM ls;
961 POP (ls);
962
963 nargs = FETCH ();
964 ASSERT (nargs >= 2);
965
966 len = scm_ilength (ls);
967 if (len < 0)
968 goto vm_error_wrong_type_arg;
969
970 PUSH_LIST (ls, SCM_NULL_OR_NIL_P);
971
972 nargs += len - 2;
973 goto vm_goto_args;
974 }
975
976 VM_DEFINE_INSTRUCTION (60, call_cc, "call/cc", 0, 1, 1)
977 {
978 int first;
979 SCM proc, cont;
980 POP (proc);
981 SYNC_ALL ();
982 cont = scm_make_continuation (&first);
983 if (first)
984 {
985 PUSH ((SCM)fp); /* dynamic link */
986 PUSH (0); /* mvra */
987 PUSH (0); /* ra */
988 PUSH (proc);
989 PUSH (cont);
990 nargs = 1;
991 goto vm_call;
992 }
993 ASSERT (sp == vp->sp);
994 ASSERT (fp == vp->fp);
995 else if (SCM_VALUESP (cont))
996 {
997 /* multiple values returned to continuation */
998 SCM values;
999 values = scm_struct_ref (cont, SCM_INUM0);
1000 if (SCM_NULLP (values))
1001 goto vm_error_no_values;
1002 /* non-tail context does not accept multiple values? */
1003 PUSH (SCM_CAR (values));
1004 NEXT;
1005 }
1006 else
1007 {
1008 PUSH (cont);
1009 NEXT;
1010 }
1011 }
1012
1013 VM_DEFINE_INSTRUCTION (61, goto_cc, "goto/cc", 0, 1, 1)
1014 {
1015 int first;
1016 SCM proc, cont;
1017 POP (proc);
1018 SYNC_ALL ();
1019 cont = scm_make_continuation (&first);
1020 ASSERT (sp == vp->sp);
1021 ASSERT (fp == vp->fp);
1022 if (first)
1023 {
1024 PUSH (proc);
1025 PUSH (cont);
1026 nargs = 1;
1027 goto vm_goto_args;
1028 }
1029 else if (SCM_VALUESP (cont))
1030 {
1031 /* multiple values returned to continuation */
1032 SCM values;
1033 values = scm_struct_ref (cont, SCM_INUM0);
1034 nvalues = scm_ilength (values);
1035 PUSH_LIST (values, SCM_NULLP);
1036 goto vm_return_values;
1037 }
1038 else
1039 {
1040 PUSH (cont);
1041 goto vm_return;
1042 }
1043 }
1044
1045 VM_DEFINE_INSTRUCTION (62, return, "return", 0, 1, 1)
1046 {
1047 vm_return:
1048 EXIT_HOOK ();
1049 RETURN_HOOK ();
1050 SYNC_REGISTER ();
1051 SCM_TICK; /* allow interrupt here */
1052 {
1053 SCM ret;
1054
1055 POP (ret);
1056
1057 #ifdef VM_ENABLE_STACK_NULLING
1058 SCM *old_sp = sp;
1059 #endif
1060
1061 /* Restore registers */
1062 sp = SCM_FRAME_LOWER_ADDRESS (fp);
1063 ip = SCM_FRAME_RETURN_ADDRESS (fp);
1064 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1065
1066 #ifdef VM_ENABLE_STACK_NULLING
1067 NULLSTACK (old_sp - sp);
1068 #endif
1069
1070 /* Set return value (sp is already pushed) */
1071 *sp = ret;
1072 }
1073
1074 /* Restore the last program */
1075 program = SCM_FRAME_PROGRAM (fp);
1076 CACHE_PROGRAM ();
1077 CHECK_IP ();
1078 NEXT;
1079 }
1080
1081 VM_DEFINE_INSTRUCTION (63, return_values, "return/values", 1, -1, -1)
1082 {
1083 /* nvalues declared at top level, because for some reason gcc seems to think
1084 that perhaps it might be used without declaration. Fooey to that, I say. */
1085 nvalues = FETCH ();
1086 vm_return_values:
1087 EXIT_HOOK ();
1088 RETURN_HOOK ();
1089
1090 if (nvalues != 1 && SCM_FRAME_MV_RETURN_ADDRESS (fp))
1091 {
1092 /* A multiply-valued continuation */
1093 SCM *vals = sp - nvalues;
1094 int i;
1095 /* Restore registers */
1096 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1097 ip = SCM_FRAME_MV_RETURN_ADDRESS (fp);
1098 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1099
1100 /* Push return values, and the number of values */
1101 for (i = 0; i < nvalues; i++)
1102 *++sp = vals[i+1];
1103 *++sp = SCM_I_MAKINUM (nvalues);
1104
1105 /* Finally null the end of the stack */
1106 NULLSTACK (vals + nvalues - sp);
1107 }
1108 else if (nvalues >= 1)
1109 {
1110 /* Multiple values for a single-valued continuation -- here's where I
1111 break with guile tradition and try and do something sensible. (Also,
1112 this block handles the single-valued return to an mv
1113 continuation.) */
1114 SCM *vals = sp - nvalues;
1115 /* Restore registers */
1116 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1117 ip = SCM_FRAME_RETURN_ADDRESS (fp);
1118 fp = SCM_FRAME_DYNAMIC_LINK (fp);
1119
1120 /* Push first value */
1121 *++sp = vals[1];
1122
1123 /* Finally null the end of the stack */
1124 NULLSTACK (vals + nvalues - sp);
1125 }
1126 else
1127 goto vm_error_no_values;
1128
1129 /* Restore the last program */
1130 program = SCM_FRAME_PROGRAM (fp);
1131 CACHE_PROGRAM ();
1132 CHECK_IP ();
1133 NEXT;
1134 }
1135
1136 VM_DEFINE_INSTRUCTION (64, return_values_star, "return/values*", 1, -1, -1)
1137 {
1138 SCM l;
1139
1140 nvalues = FETCH ();
1141 ASSERT (nvalues >= 1);
1142
1143 nvalues--;
1144 POP (l);
1145 while (SCM_CONSP (l))
1146 {
1147 PUSH (SCM_CAR (l));
1148 l = SCM_CDR (l);
1149 nvalues++;
1150 }
1151 if (SCM_UNLIKELY (!SCM_NULL_OR_NIL_P (l))) {
1152 finish_args = scm_list_1 (l);
1153 goto vm_error_improper_list;
1154 }
1155
1156 goto vm_return_values;
1157 }
1158
1159 VM_DEFINE_INSTRUCTION (65, truncate_values, "truncate-values", 2, -1, -1)
1160 {
1161 SCM x;
1162 int nbinds, rest;
1163 POP (x);
1164 nvalues = scm_to_int (x);
1165 nbinds = FETCH ();
1166 rest = FETCH ();
1167
1168 if (rest)
1169 nbinds--;
1170
1171 if (nvalues < nbinds)
1172 goto vm_error_not_enough_values;
1173
1174 if (rest)
1175 POP_LIST (nvalues - nbinds);
1176 else
1177 DROPN (nvalues - nbinds);
1178
1179 NEXT;
1180 }
1181
1182 VM_DEFINE_INSTRUCTION (66, box, "box", 1, 1, 0)
1183 {
1184 SCM val;
1185 POP (val);
1186 SYNC_BEFORE_GC ();
1187 LOCAL_SET (FETCH (), scm_cell (scm_tc7_variable, SCM_UNPACK (val)));
1188 NEXT;
1189 }
1190
1191 /* for letrec:
1192 (let ((a *undef*) (b *undef*) ...)
1193 (set! a (lambda () (b ...)))
1194 ...)
1195 */
1196 VM_DEFINE_INSTRUCTION (67, empty_box, "empty-box", 1, 0, 0)
1197 {
1198 SYNC_BEFORE_GC ();
1199 LOCAL_SET (FETCH (),
1200 scm_cell (scm_tc7_variable, SCM_UNPACK (SCM_UNDEFINED)));
1201 NEXT;
1202 }
1203
1204 VM_DEFINE_INSTRUCTION (68, local_boxed_ref, "local-boxed-ref", 1, 0, 1)
1205 {
1206 SCM v = LOCAL_REF (FETCH ());
1207 ASSERT_BOUND_VARIABLE (v);
1208 PUSH (VARIABLE_REF (v));
1209 NEXT;
1210 }
1211
1212 VM_DEFINE_INSTRUCTION (69, local_boxed_set, "local-boxed-set", 1, 1, 0)
1213 {
1214 SCM v, val;
1215 v = LOCAL_REF (FETCH ());
1216 POP (val);
1217 ASSERT_VARIABLE (v);
1218 VARIABLE_SET (v, val);
1219 NEXT;
1220 }
1221
1222 VM_DEFINE_INSTRUCTION (70, free_ref, "free-ref", 1, 0, 1)
1223 {
1224 scm_t_uint8 idx = FETCH ();
1225
1226 CHECK_FREE_VARIABLE (idx);
1227 PUSH (FREE_VARIABLE_REF (idx));
1228 NEXT;
1229 }
1230
1231 /* no free-set -- if a var is assigned, it should be in a box */
1232
1233 VM_DEFINE_INSTRUCTION (71, free_boxed_ref, "free-boxed-ref", 1, 0, 1)
1234 {
1235 SCM v;
1236 scm_t_uint8 idx = FETCH ();
1237 CHECK_FREE_VARIABLE (idx);
1238 v = FREE_VARIABLE_REF (idx);
1239 ASSERT_BOUND_VARIABLE (v);
1240 PUSH (VARIABLE_REF (v));
1241 NEXT;
1242 }
1243
1244 VM_DEFINE_INSTRUCTION (72, free_boxed_set, "free-boxed-set", 1, 1, 0)
1245 {
1246 SCM v, val;
1247 scm_t_uint8 idx = FETCH ();
1248 POP (val);
1249 CHECK_FREE_VARIABLE (idx);
1250 v = FREE_VARIABLE_REF (idx);
1251 ASSERT_BOUND_VARIABLE (v);
1252 VARIABLE_SET (v, val);
1253 NEXT;
1254 }
1255
1256 VM_DEFINE_INSTRUCTION (73, make_closure, "make-closure", 0, 2, 1)
1257 {
1258 SCM vect;
1259 POP (vect);
1260 SYNC_BEFORE_GC ();
1261 /* fixme underflow */
1262 *sp = scm_double_cell (scm_tc7_program, (scm_t_bits)SCM_PROGRAM_OBJCODE (*sp),
1263 (scm_t_bits)SCM_PROGRAM_OBJTABLE (*sp), (scm_t_bits)vect);
1264 NEXT;
1265 }
1266
1267 VM_DEFINE_INSTRUCTION (74, make_variable, "make-variable", 0, 0, 1)
1268 {
1269 SYNC_BEFORE_GC ();
1270 /* fixme underflow */
1271 PUSH (scm_cell (scm_tc7_variable, SCM_UNPACK (SCM_UNDEFINED)));
1272 NEXT;
1273 }
1274
1275 VM_DEFINE_INSTRUCTION (75, fix_closure, "fix-closure", 2, 0, 1)
1276 {
1277 SCM x, vect;
1278 unsigned int i = FETCH ();
1279 i <<= 8;
1280 i += FETCH ();
1281 POP (vect);
1282 /* FIXME CHECK_LOCAL (i) */
1283 x = LOCAL_REF (i);
1284 /* FIXME ASSERT_PROGRAM (x); */
1285 SCM_SET_CELL_WORD_3 (x, vect);
1286 NEXT;
1287 }
1288
1289 VM_DEFINE_INSTRUCTION (76, define, "define", 0, 0, 2)
1290 {
1291 SCM sym, val;
1292 POP (sym);
1293 POP (val);
1294 SYNC_REGISTER ();
1295 VARIABLE_SET (scm_sym2var (sym, scm_current_module_lookup_closure (),
1296 SCM_BOOL_T),
1297 val);
1298 NEXT;
1299 }
1300
1301 VM_DEFINE_INSTRUCTION (77, make_keyword, "make-keyword", 0, 1, 1)
1302 {
1303 CHECK_UNDERFLOW ();
1304 SYNC_REGISTER ();
1305 *sp = scm_symbol_to_keyword (*sp);
1306 NEXT;
1307 }
1308
1309 VM_DEFINE_INSTRUCTION (78, make_symbol, "make-symbol", 0, 1, 1)
1310 {
1311 CHECK_UNDERFLOW ();
1312 SYNC_REGISTER ();
1313 *sp = scm_string_to_symbol (*sp);
1314 NEXT;
1315 }
1316
1317
1318 /*
1319 (defun renumber-ops ()
1320 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
1321 (interactive "")
1322 (save-excursion
1323 (let ((counter -1)) (goto-char (point-min))
1324 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
1325 (replace-match
1326 (number-to-string (setq counter (1+ counter)))
1327 t t nil 1)))))
1328 */
1329 /*
1330 Local Variables:
1331 c-file-style: "gnu"
1332 End:
1333 */