1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-16 16:50:21 +02:00

callees reserve their own local vars

* libguile/_scm.h (SCM_OBJCODE_MINOR_VERSION): Bump.
* libguile/vm-i-system.c (reserve-locals): New instruction, to reserve
  space for local vars. Other ops renumbered.

* module/language/tree-il/compile-glil.scm (flatten-lambda): Emit
  reserve-locals instructions as necessary.

* test-suite/tests/tree-il.test: Update to expect reserve-locals as
  appropriate.
This commit is contained in:
Andy Wingo 2009-09-27 19:50:06 -04:00
parent 6c6a44390b
commit 55d9bc947e
3 changed files with 52 additions and 35 deletions

View file

@ -519,7 +519,21 @@ VM_DEFINE_INSTRUCTION (40, push_rest_list, "push-rest-list", 2, -1, -1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (41, new_frame, "new-frame", 0, 0, 3) VM_DEFINE_INSTRUCTION (41, reserve_locals, "reserve-locals", 2, -1, -1)
{
scm_t_int32 n;
n = FETCH () << 8;
n += FETCH ();
#if 0
sp += n;
CHECK_OVERFLOW ();
while (n--)
sp[-n] = SCM_UNDEFINED;
#endif
NEXT;
}
VM_DEFINE_INSTRUCTION (42, new_frame, "new-frame", 0, 0, 3)
{ {
/* NB: if you change this, see frames.c:vm-frame-num-locals */ /* NB: if you change this, see frames.c:vm-frame-num-locals */
/* and frames.h, vm-engine.c, etc of course */ /* and frames.h, vm-engine.c, etc of course */
@ -529,7 +543,7 @@ VM_DEFINE_INSTRUCTION (41, new_frame, "new-frame", 0, 0, 3)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (42, call, "call", 1, -1, 1) VM_DEFINE_INSTRUCTION (43, call, "call", 1, -1, 1)
{ {
SCM x; SCM x;
nargs = FETCH (); nargs = FETCH ();
@ -591,7 +605,7 @@ VM_DEFINE_INSTRUCTION (42, call, "call", 1, -1, 1)
goto vm_error_wrong_type_apply; goto vm_error_wrong_type_apply;
} }
VM_DEFINE_INSTRUCTION (43, goto_args, "goto/args", 1, -1, 1) VM_DEFINE_INSTRUCTION (44, goto_args, "goto/args", 1, -1, 1)
{ {
register SCM x; register SCM x;
nargs = FETCH (); nargs = FETCH ();
@ -670,7 +684,7 @@ VM_DEFINE_INSTRUCTION (43, goto_args, "goto/args", 1, -1, 1)
goto vm_error_wrong_type_apply; goto vm_error_wrong_type_apply;
} }
VM_DEFINE_INSTRUCTION (44, goto_nargs, "goto/nargs", 0, 0, 1) VM_DEFINE_INSTRUCTION (45, goto_nargs, "goto/nargs", 0, 0, 1)
{ {
SCM x; SCM x;
POP (x); POP (x);
@ -679,7 +693,7 @@ VM_DEFINE_INSTRUCTION (44, goto_nargs, "goto/nargs", 0, 0, 1)
goto vm_goto_args; goto vm_goto_args;
} }
VM_DEFINE_INSTRUCTION (45, call_nargs, "call/nargs", 0, 0, 1) VM_DEFINE_INSTRUCTION (46, call_nargs, "call/nargs", 0, 0, 1)
{ {
SCM x; SCM x;
POP (x); POP (x);
@ -688,7 +702,7 @@ VM_DEFINE_INSTRUCTION (45, call_nargs, "call/nargs", 0, 0, 1)
goto vm_call; goto vm_call;
} }
VM_DEFINE_INSTRUCTION (46, mv_call, "mv-call", 4, -1, 1) VM_DEFINE_INSTRUCTION (47, mv_call, "mv-call", 4, -1, 1)
{ {
SCM x; SCM x;
scm_t_int32 offset; scm_t_int32 offset;
@ -751,7 +765,7 @@ VM_DEFINE_INSTRUCTION (46, mv_call, "mv-call", 4, -1, 1)
goto vm_error_wrong_type_apply; goto vm_error_wrong_type_apply;
} }
VM_DEFINE_INSTRUCTION (47, apply, "apply", 1, -1, 1) VM_DEFINE_INSTRUCTION (48, apply, "apply", 1, -1, 1)
{ {
int len; int len;
SCM ls; SCM ls;
@ -770,7 +784,7 @@ VM_DEFINE_INSTRUCTION (47, apply, "apply", 1, -1, 1)
goto vm_call; goto vm_call;
} }
VM_DEFINE_INSTRUCTION (48, goto_apply, "goto/apply", 1, -1, 1) VM_DEFINE_INSTRUCTION (49, goto_apply, "goto/apply", 1, -1, 1)
{ {
int len; int len;
SCM ls; SCM ls;
@ -789,7 +803,7 @@ VM_DEFINE_INSTRUCTION (48, goto_apply, "goto/apply", 1, -1, 1)
goto vm_goto_args; goto vm_goto_args;
} }
VM_DEFINE_INSTRUCTION (49, call_cc, "call/cc", 0, 1, 1) VM_DEFINE_INSTRUCTION (50, call_cc, "call/cc", 0, 1, 1)
{ {
int first; int first;
SCM proc, cont; SCM proc, cont;
@ -826,7 +840,7 @@ VM_DEFINE_INSTRUCTION (49, call_cc, "call/cc", 0, 1, 1)
} }
} }
VM_DEFINE_INSTRUCTION (50, goto_cc, "goto/cc", 0, 1, 1) VM_DEFINE_INSTRUCTION (51, goto_cc, "goto/cc", 0, 1, 1)
{ {
int first; int first;
SCM proc, cont; SCM proc, cont;
@ -858,7 +872,7 @@ VM_DEFINE_INSTRUCTION (50, goto_cc, "goto/cc", 0, 1, 1)
} }
} }
VM_DEFINE_INSTRUCTION (51, return, "return", 0, 1, 1) VM_DEFINE_INSTRUCTION (52, return, "return", 0, 1, 1)
{ {
vm_return: vm_return:
EXIT_HOOK (); EXIT_HOOK ();
@ -894,7 +908,7 @@ VM_DEFINE_INSTRUCTION (51, return, "return", 0, 1, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (52, return_values, "return/values", 1, -1, -1) VM_DEFINE_INSTRUCTION (53, return_values, "return/values", 1, -1, -1)
{ {
/* nvalues declared at top level, because for some reason gcc seems to think /* nvalues declared at top level, because for some reason gcc seems to think
that perhaps it might be used without declaration. Fooey to that, I say. */ that perhaps it might be used without declaration. Fooey to that, I say. */
@ -949,7 +963,7 @@ VM_DEFINE_INSTRUCTION (52, return_values, "return/values", 1, -1, -1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (53, return_values_star, "return/values*", 1, -1, -1) VM_DEFINE_INSTRUCTION (54, return_values_star, "return/values*", 1, -1, -1)
{ {
SCM l; SCM l;
@ -972,7 +986,7 @@ VM_DEFINE_INSTRUCTION (53, return_values_star, "return/values*", 1, -1, -1)
goto vm_return_values; goto vm_return_values;
} }
VM_DEFINE_INSTRUCTION (54, truncate_values, "truncate-values", 2, -1, -1) VM_DEFINE_INSTRUCTION (55, truncate_values, "truncate-values", 2, -1, -1)
{ {
SCM x; SCM x;
int nbinds, rest; int nbinds, rest;
@ -995,7 +1009,7 @@ VM_DEFINE_INSTRUCTION (54, truncate_values, "truncate-values", 2, -1, -1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (55, box, "box", 1, 1, 0) VM_DEFINE_INSTRUCTION (56, box, "box", 1, 1, 0)
{ {
SCM val; SCM val;
POP (val); POP (val);
@ -1009,7 +1023,7 @@ VM_DEFINE_INSTRUCTION (55, box, "box", 1, 1, 0)
(set! a (lambda () (b ...))) (set! a (lambda () (b ...)))
...) ...)
*/ */
VM_DEFINE_INSTRUCTION (56, empty_box, "empty-box", 1, 0, 0) VM_DEFINE_INSTRUCTION (57, empty_box, "empty-box", 1, 0, 0)
{ {
SYNC_BEFORE_GC (); SYNC_BEFORE_GC ();
LOCAL_SET (FETCH (), LOCAL_SET (FETCH (),
@ -1017,7 +1031,7 @@ VM_DEFINE_INSTRUCTION (56, empty_box, "empty-box", 1, 0, 0)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (57, local_boxed_ref, "local-boxed-ref", 1, 0, 1) VM_DEFINE_INSTRUCTION (58, local_boxed_ref, "local-boxed-ref", 1, 0, 1)
{ {
SCM v = LOCAL_REF (FETCH ()); SCM v = LOCAL_REF (FETCH ());
ASSERT_BOUND_VARIABLE (v); ASSERT_BOUND_VARIABLE (v);
@ -1025,7 +1039,7 @@ VM_DEFINE_INSTRUCTION (57, local_boxed_ref, "local-boxed-ref", 1, 0, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (58, local_boxed_set, "local-boxed-set", 1, 1, 0) VM_DEFINE_INSTRUCTION (59, local_boxed_set, "local-boxed-set", 1, 1, 0)
{ {
SCM v, val; SCM v, val;
v = LOCAL_REF (FETCH ()); v = LOCAL_REF (FETCH ());
@ -1035,7 +1049,7 @@ VM_DEFINE_INSTRUCTION (58, local_boxed_set, "local-boxed-set", 1, 1, 0)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (59, free_ref, "free-ref", 1, 0, 1) VM_DEFINE_INSTRUCTION (60, free_ref, "free-ref", 1, 0, 1)
{ {
scm_t_uint8 idx = FETCH (); scm_t_uint8 idx = FETCH ();
@ -1046,7 +1060,7 @@ VM_DEFINE_INSTRUCTION (59, free_ref, "free-ref", 1, 0, 1)
/* no free-set -- if a var is assigned, it should be in a box */ /* no free-set -- if a var is assigned, it should be in a box */
VM_DEFINE_INSTRUCTION (60, free_boxed_ref, "free-boxed-ref", 1, 0, 1) VM_DEFINE_INSTRUCTION (61, free_boxed_ref, "free-boxed-ref", 1, 0, 1)
{ {
SCM v; SCM v;
scm_t_uint8 idx = FETCH (); scm_t_uint8 idx = FETCH ();
@ -1057,7 +1071,7 @@ VM_DEFINE_INSTRUCTION (60, free_boxed_ref, "free-boxed-ref", 1, 0, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (61, free_boxed_set, "free-boxed-set", 1, 1, 0) VM_DEFINE_INSTRUCTION (62, free_boxed_set, "free-boxed-set", 1, 1, 0)
{ {
SCM v, val; SCM v, val;
scm_t_uint8 idx = FETCH (); scm_t_uint8 idx = FETCH ();
@ -1069,7 +1083,7 @@ VM_DEFINE_INSTRUCTION (61, free_boxed_set, "free-boxed-set", 1, 1, 0)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (62, make_closure, "make-closure", 0, 2, 1) VM_DEFINE_INSTRUCTION (63, make_closure, "make-closure", 0, 2, 1)
{ {
SCM vect; SCM vect;
POP (vect); POP (vect);
@ -1080,7 +1094,7 @@ VM_DEFINE_INSTRUCTION (62, make_closure, "make-closure", 0, 2, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (63, make_variable, "make-variable", 0, 0, 1) VM_DEFINE_INSTRUCTION (64, make_variable, "make-variable", 0, 0, 1)
{ {
SYNC_BEFORE_GC (); SYNC_BEFORE_GC ();
/* fixme underflow */ /* fixme underflow */
@ -1088,7 +1102,7 @@ VM_DEFINE_INSTRUCTION (63, make_variable, "make-variable", 0, 0, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (64, fix_closure, "fix-closure", 2, 0, 1) VM_DEFINE_INSTRUCTION (65, fix_closure, "fix-closure", 2, 0, 1)
{ {
SCM x, vect; SCM x, vect;
unsigned int i = FETCH (); unsigned int i = FETCH ();
@ -1102,7 +1116,7 @@ VM_DEFINE_INSTRUCTION (64, fix_closure, "fix-closure", 2, 0, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (65, define, "define", 0, 0, 2) VM_DEFINE_INSTRUCTION (66, define, "define", 0, 0, 2)
{ {
SCM sym, val; SCM sym, val;
POP (sym); POP (sym);
@ -1114,7 +1128,7 @@ VM_DEFINE_INSTRUCTION (65, define, "define", 0, 0, 2)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (66, make_keyword, "make-keyword", 0, 1, 1) VM_DEFINE_INSTRUCTION (67, make_keyword, "make-keyword", 0, 1, 1)
{ {
CHECK_UNDERFLOW (); CHECK_UNDERFLOW ();
SYNC_REGISTER (); SYNC_REGISTER ();
@ -1122,7 +1136,7 @@ VM_DEFINE_INSTRUCTION (66, make_keyword, "make-keyword", 0, 1, 1)
NEXT; NEXT;
} }
VM_DEFINE_INSTRUCTION (67, make_symbol, "make-symbol", 0, 1, 1) VM_DEFINE_INSTRUCTION (68, make_symbol, "make-symbol", 0, 1, 1)
{ {
CHECK_UNDERFLOW (); CHECK_UNDERFLOW ();
SYNC_REGISTER (); SYNC_REGISTER ();

View file

@ -194,6 +194,9 @@
(emit-code #f (make-glil-source (lambda-src x)))) (emit-code #f (make-glil-source (lambda-src x))))
;; check arity, potentially consing a rest list ;; check arity, potentially consing a rest list
(emit-code #f (make-glil-arity nargs nrest #f)) (emit-code #f (make-glil-arity nargs nrest #f))
;; reserve space for locals, if necessary
(if (not (zero? nlocs))
(emit-code #f (make-glil-call 'reserve-locals nlocs)))
;; write bindings info ;; write bindings info
(if (not (null? ids)) (if (not (null? ids))
(emit-bindings #f ids vars allocation x emit-code)) (emit-bindings #f ids vars allocation x emit-code))

View file

@ -133,21 +133,21 @@
(with-test-prefix "lexical refs" (with-test-prefix "lexical refs"
(assert-tree-il->glil (assert-tree-il->glil
(let (x) (y) ((const 1)) (lexical x y)) (let (x) (y) ((const 1)) (lexical x y))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #f 0)) (lexical #t #f set 0) (const 1) (bind (x #f 0)) (lexical #t #f set 0)
(lexical #t #f ref 0) (call return 1) (lexical #t #f ref 0) (call return 1)
(unbind))) (unbind)))
(assert-tree-il->glil (assert-tree-il->glil
(let (x) (y) ((const 1)) (begin (lexical x y) (const #f))) (let (x) (y) ((const 1)) (begin (lexical x y) (const #f)))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #f 0)) (lexical #t #f set 0) (const 1) (bind (x #f 0)) (lexical #t #f set 0)
(const #f) (call return 1) (const #f) (call return 1)
(unbind))) (unbind)))
(assert-tree-il->glil (assert-tree-il->glil
(let (x) (y) ((const 1)) (apply (primitive null?) (lexical x y))) (let (x) (y) ((const 1)) (apply (primitive null?) (lexical x y)))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #f 0)) (lexical #t #f set 0) (const 1) (bind (x #f 0)) (lexical #t #f set 0)
(lexical #t #f ref 0) (call null? 1) (call return 1) (lexical #t #f ref 0) (call null? 1) (call return 1)
(unbind)))) (unbind))))
@ -157,7 +157,7 @@
;; unreferenced sets may be optimized away -- make sure they are ref'd ;; unreferenced sets may be optimized away -- make sure they are ref'd
(let (x) (y) ((const 1)) (let (x) (y) ((const 1))
(set! (lexical x y) (apply (primitive 1+) (lexical x y)))) (set! (lexical x y) (apply (primitive 1+) (lexical x y))))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #t 0)) (lexical #t #t box 0) (const 1) (bind (x #t 0)) (lexical #t #t box 0)
(lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0) (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0)
(void) (call return 1) (void) (call return 1)
@ -167,7 +167,7 @@
(let (x) (y) ((const 1)) (let (x) (y) ((const 1))
(begin (set! (lexical x y) (apply (primitive 1+) (lexical x y))) (begin (set! (lexical x y) (apply (primitive 1+) (lexical x y)))
(lexical x y))) (lexical x y)))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #t 0)) (lexical #t #t box 0) (const 1) (bind (x #t 0)) (lexical #t #t box 0)
(lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0) (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0)
(lexical #t #t ref 0) (call return 1) (lexical #t #t ref 0) (call return 1)
@ -177,7 +177,7 @@
(let (x) (y) ((const 1)) (let (x) (y) ((const 1))
(apply (primitive null?) (apply (primitive null?)
(set! (lexical x y) (apply (primitive 1+) (lexical x y))))) (set! (lexical x y) (apply (primitive 1+) (lexical x y)))))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #t 0)) (lexical #t #t box 0) (const 1) (bind (x #t 0)) (lexical #t #t box 0)
(lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0) (void) (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0) (void)
(call null? 1) (call return 1) (call null? 1) (call return 1)
@ -413,7 +413,7 @@
(lexical x y) (lexical x y)
(let (a) (b) ((const 2)) (let (a) (b) ((const 2))
(lexical a b)))) (lexical a b))))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #f 0)) (lexical #t #f set 0) (const 1) (bind (x #f 0)) (lexical #t #f set 0)
(lexical #t #f ref 0) (branch br-if-not ,l1) (lexical #t #f ref 0) (branch br-if-not ,l1)
(lexical #t #f ref 0) (call return 1) (lexical #t #f ref 0) (call return 1)
@ -431,7 +431,7 @@
(lexical x y) (lexical x y)
(let (a) (b) ((const 2)) (let (a) (b) ((const 2))
(lexical x y)))) (lexical x y))))
(program 0 0 1 () (arity 0 0 #f) (program 0 0 1 () (arity 0 0 #f) (call reserve-locals 1)
(const 1) (bind (x #f 0)) (lexical #t #f set 0) (const 1) (bind (x #f 0)) (lexical #t #f set 0)
(lexical #t #f ref 0) (branch br-if-not ,l1) (lexical #t #f ref 0) (branch br-if-not ,l1)
(lexical #t #f ref 0) (call return 1) (lexical #t #f ref 0) (call return 1)