stacks.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /* Representation of stack frame debug information
  2. * Copyright (C) 1996, 1997, 2000, 2001, 2004 Free Software Foundation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; see the file COPYING. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301 USA
  18. *
  19. * As a special exception, the Free Software Foundation gives permission
  20. * for additional uses of the text contained in its release of GUILE.
  21. *
  22. * The exception is that, if you link the GUILE library with other files
  23. * to produce an executable, this does not by itself cause the
  24. * resulting executable to be covered by the GNU General Public License.
  25. * Your use of that executable is in no way restricted on account of
  26. * linking the GUILE library code into it.
  27. *
  28. * This exception does not however invalidate any other reasons why
  29. * the executable file might be covered by the GNU General Public License.
  30. *
  31. * This exception applies only to the code released by the
  32. * Free Software Foundation under the name GUILE. If you copy
  33. * code from other Free Software Foundation releases into a copy of
  34. * GUILE, as the General Public License permits, the exception does
  35. * not apply to the code that you add in this way. To avoid misleading
  36. * anyone as to the status of such modified files, you must delete
  37. * this exception notice from them.
  38. *
  39. * If you write modifications of your own for GUILE, it is your choice
  40. * whether to permit this exception to apply to your modifications.
  41. * If you do not wish that, delete this exception notice.
  42. *
  43. * The author can be reached at djurfeldt@nada.kth.se
  44. * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
  45. #include "libguile/_scm.h"
  46. #include "libguile/eval.h"
  47. #include "libguile/debug.h"
  48. #include "libguile/continuations.h"
  49. #include "libguile/struct.h"
  50. #include "libguile/macros.h"
  51. #include "libguile/procprop.h"
  52. #include "libguile/modules.h"
  53. #include "libguile/root.h"
  54. #include "libguile/strings.h"
  55. #include "libguile/validate.h"
  56. #include "libguile/stacks.h"
  57. /* {Frames and stacks}
  58. *
  59. * The debugging evaluator creates debug frames on the stack. These
  60. * are linked from the innermost frame and outwards. The last frame
  61. * created can always be accessed as SCM_LAST_DEBUG_FRAME.
  62. * Continuations contain a pointer to the innermost debug frame on the
  63. * continuation stack.
  64. *
  65. * Each debug frame contains a set of flags and information about one
  66. * or more stack frames. The case of multiple frames occurs due to
  67. * tail recursion. The maximal number of stack frames which can be
  68. * recorded in one debug frame can be set dynamically with the debug
  69. * option FRAMES.
  70. *
  71. * Stack frame information is of two types: eval information (the
  72. * expression being evaluated and its environment) and apply
  73. * information (the procedure being applied and its arguments). A
  74. * stack frame normally corresponds to an eval/apply pair, but macros
  75. * and special forms (which are implemented as macros in Guile) only
  76. * have eval information and apply calls leads to apply only frames.
  77. *
  78. * Since we want to record the total stack information and later
  79. * manipulate this data at the scheme level in the debugger, we need
  80. * to transform it into a new representation. In the following code
  81. * section you'll find the functions implementing this data type.
  82. *
  83. * Representation:
  84. *
  85. * The stack is represented as a struct with an id slot and a tail
  86. * array of scm_t_info_frame structs.
  87. *
  88. * A frame is represented as a pair where the car contains a stack and
  89. * the cdr an inum. The inum is an index to the first SCM value of
  90. * the scm_t_info_frame struct.
  91. *
  92. * Stacks
  93. * Constructor
  94. * make-stack
  95. * Selectors
  96. * stack-id
  97. * stack-ref
  98. * Inspector
  99. * stack-length
  100. *
  101. * Frames
  102. * Constructor
  103. * last-stack-frame
  104. * Selectors
  105. * frame-number
  106. * frame-source
  107. * frame-procedure
  108. * frame-arguments
  109. * frame-previous
  110. * frame-next
  111. * Predicates
  112. * frame-real?
  113. * frame-procedure?
  114. * frame-evaluating-args?
  115. * frame-overflow? */
  116. /* Some auxiliary functions for reading debug frames off the stack.
  117. */
  118. /* Stacks often contain pointers to other items on the stack; for
  119. example, each scm_t_debug_frame structure contains a pointer to the
  120. next frame out. When we capture a continuation, we copy the stack
  121. into the heap, and just leave all the pointers unchanged. This
  122. makes it simple to restore the continuation --- just copy the stack
  123. back! However, if we retrieve a pointer from the heap copy to
  124. another item that was originally on the stack, we have to add an
  125. offset to the pointer to discover the new referent.
  126. If PTR is a pointer retrieved from a continuation, whose original
  127. target was on the stack, and OFFSET is the appropriate offset from
  128. the original stack to the continuation, then RELOC_MUMBLE (PTR,
  129. OFFSET) is a pointer to the copy in the continuation of the
  130. original referent, cast to an scm_debug_MUMBLE *. */
  131. #define RELOC_INFO(ptr, offset) \
  132. ((scm_t_debug_info *) ((SCM_STACKITEM *) (ptr) + (offset)))
  133. #define RELOC_FRAME(ptr, offset) \
  134. ((scm_t_debug_frame *) ((SCM_STACKITEM *) (ptr) + (offset)))
  135. /* Count number of debug info frames on a stack, beginning with
  136. * DFRAME. OFFSET is used for relocation of pointers when the stack
  137. * is read from a continuation.
  138. */
  139. static scm_t_bits
  140. stack_depth (scm_t_debug_frame *dframe, long offset,
  141. SCM *id, int *maxp)
  142. {
  143. long n;
  144. long max_depth = SCM_BACKTRACE_MAXDEPTH;
  145. for (n = 0;
  146. dframe && !SCM_VOIDFRAMEP (*dframe) && n < max_depth;
  147. dframe = RELOC_FRAME (dframe->prev, offset))
  148. {
  149. if (SCM_EVALFRAMEP (*dframe))
  150. {
  151. scm_t_debug_info *info = RELOC_INFO (dframe->info, offset);
  152. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  153. n += (info - vect) / 2 + 1;
  154. /* Data in the apply part of an eval info frame comes from previous
  155. stack frame if the scm_t_debug_info vector is overflowed. */
  156. if ((((info - vect) & 1) == 0)
  157. && SCM_OVERFLOWP (*dframe)
  158. && !SCM_UNBNDP (info[1].a.proc))
  159. ++n;
  160. }
  161. else
  162. ++n;
  163. }
  164. if (dframe && SCM_VOIDFRAMEP (*dframe))
  165. *id = RELOC_INFO(dframe->vect, offset)[0].id;
  166. else if (dframe)
  167. *maxp = 1;
  168. return n;
  169. }
  170. /* Read debug info from DFRAME into IFRAME.
  171. */
  172. static void
  173. read_frame (scm_t_debug_frame *dframe, long offset,
  174. scm_t_info_frame *iframe)
  175. {
  176. scm_t_bits flags = SCM_UNPACK (SCM_INUM0); /* UGh. */
  177. if (SCM_EVALFRAMEP (*dframe))
  178. {
  179. scm_t_debug_info *info = RELOC_INFO (dframe->info, offset);
  180. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  181. if ((info - vect) & 1)
  182. {
  183. /* Debug.vect ends with apply info. */
  184. --info;
  185. if (!SCM_UNBNDP (info[1].a.proc))
  186. {
  187. flags |= SCM_FRAMEF_PROC;
  188. iframe->proc = info[1].a.proc;
  189. iframe->args = info[1].a.args;
  190. if (!SCM_ARGS_READY_P (*dframe))
  191. flags |= SCM_FRAMEF_EVAL_ARGS;
  192. }
  193. }
  194. iframe->source = scm_make_memoized (info[0].e.exp, info[0].e.env);
  195. }
  196. else
  197. {
  198. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  199. flags |= SCM_FRAMEF_PROC;
  200. iframe->proc = vect[0].a.proc;
  201. iframe->args = vect[0].a.args;
  202. }
  203. iframe->flags = flags;
  204. }
  205. /* Look up the first body form of the apply closure. We'll use this
  206. below to prevent it from being displayed.
  207. */
  208. static SCM
  209. get_applybody ()
  210. {
  211. SCM var = scm_sym2var (scm_sym_apply, SCM_BOOL_F, SCM_BOOL_F);
  212. if (SCM_VARIABLEP (var) && SCM_CLOSUREP (SCM_VARIABLE_REF (var)))
  213. return SCM_CADR (SCM_CODE (SCM_VARIABLE_REF (var)));
  214. else
  215. return SCM_UNDEFINED;
  216. }
  217. #define NEXT_FRAME(iframe, n, quit) \
  218. do { \
  219. if (SCM_MEMOIZEDP (iframe->source) \
  220. && SCM_EQ_P (SCM_MEMOIZED_EXP (iframe->source), applybody)) \
  221. { \
  222. iframe->source = SCM_BOOL_F; \
  223. if (SCM_FALSEP (iframe->proc)) \
  224. { \
  225. --iframe; \
  226. ++n; \
  227. } \
  228. } \
  229. ++iframe; \
  230. if (--n == 0) \
  231. goto quit; \
  232. } while (0)
  233. /* Fill the scm_t_info_frame vector IFRAME with data from N stack frames
  234. * starting with the first stack frame represented by debug frame
  235. * DFRAME.
  236. */
  237. static scm_t_bits
  238. read_frames (scm_t_debug_frame *dframe, long offset,
  239. long n, scm_t_info_frame *iframes)
  240. {
  241. scm_t_info_frame *iframe = iframes;
  242. scm_t_debug_info *info, *vect;
  243. static SCM applybody = SCM_UNDEFINED;
  244. /* The value of applybody has to be setup after r4rs.scm has executed. */
  245. if (SCM_UNBNDP (applybody))
  246. applybody = get_applybody ();
  247. for (;
  248. dframe && !SCM_VOIDFRAMEP (*dframe) && n > 0;
  249. dframe = RELOC_FRAME (dframe->prev, offset))
  250. {
  251. read_frame (dframe, offset, iframe);
  252. if (SCM_EVALFRAMEP (*dframe))
  253. {
  254. /* If current frame is a macro during expansion, we should
  255. skip the previously recorded macro transformer
  256. application frame. */
  257. if (SCM_MACROEXPP (*dframe) && iframe > iframes)
  258. {
  259. *(iframe - 1) = *iframe;
  260. --iframe;
  261. }
  262. info = RELOC_INFO (dframe->info, offset);
  263. vect = RELOC_INFO (dframe->vect, offset);
  264. if ((info - vect) & 1)
  265. --info;
  266. /* Data in the apply part of an eval info frame comes from
  267. previous stack frame if the scm_t_debug_info vector is
  268. overflowed. */
  269. else if (SCM_OVERFLOWP (*dframe)
  270. && !SCM_UNBNDP (info[1].a.proc))
  271. {
  272. NEXT_FRAME (iframe, n, quit);
  273. iframe->flags = SCM_UNPACK(SCM_INUM0) | SCM_FRAMEF_PROC;
  274. iframe->proc = info[1].a.proc;
  275. iframe->args = info[1].a.args;
  276. }
  277. if (SCM_OVERFLOWP (*dframe))
  278. iframe->flags |= SCM_FRAMEF_OVERFLOW;
  279. info -= 2;
  280. NEXT_FRAME (iframe, n, quit);
  281. while (info >= vect)
  282. {
  283. if (!SCM_UNBNDP (info[1].a.proc))
  284. {
  285. iframe->flags = SCM_UNPACK(SCM_INUM0) | SCM_FRAMEF_PROC;
  286. iframe->proc = info[1].a.proc;
  287. iframe->args = info[1].a.args;
  288. }
  289. else
  290. iframe->flags = SCM_UNPACK (SCM_INUM0);
  291. iframe->source = scm_make_memoized (info[0].e.exp,
  292. info[0].e.env);
  293. info -= 2;
  294. NEXT_FRAME (iframe, n, quit);
  295. }
  296. }
  297. else if (SCM_EQ_P (iframe->proc, scm_f_gsubr_apply))
  298. /* Skip gsubr apply frames. */
  299. continue;
  300. else
  301. {
  302. NEXT_FRAME (iframe, n, quit);
  303. }
  304. quit:
  305. if (iframe > iframes)
  306. (iframe - 1) -> flags |= SCM_FRAMEF_REAL;
  307. }
  308. return iframe - iframes; /* Number of frames actually read */
  309. }
  310. /* Narrow STACK by cutting away stackframes (mutatingly).
  311. *
  312. * Inner frames (most recent) are cut by advancing the frames pointer.
  313. * Outer frames are cut by decreasing the recorded length.
  314. *
  315. * Cut maximally INNER inner frames and OUTER outer frames using
  316. * the keys INNER_KEY and OUTER_KEY.
  317. *
  318. * Frames are cut away starting at the end points and moving towards
  319. * the center of the stack. The key is normally compared to the
  320. * operator in application frames. Frames up to and including the key
  321. * are cut.
  322. *
  323. * If INNER_KEY is #t a different scheme is used for inner frames:
  324. *
  325. * Frames up to but excluding the first source frame originating from
  326. * a user module are cut, except for possible application frames
  327. * between the user frame and the last system frame previously
  328. * encountered.
  329. */
  330. static void
  331. narrow_stack (SCM stack,long inner,SCM inner_key,long outer,SCM outer_key)
  332. {
  333. scm_t_stack *s = SCM_STACK (stack);
  334. unsigned long int i;
  335. long n = s->length;
  336. /* Cut inner part. */
  337. if (SCM_EQ_P (inner_key, SCM_BOOL_T))
  338. {
  339. /* Cut all frames up to user module code */
  340. for (i = 0; inner; ++i, --inner)
  341. {
  342. SCM m = s->frames[i].source;
  343. if (SCM_MEMOIZEDP (m)
  344. && !SCM_IMP (SCM_MEMOIZED_ENV (m))
  345. && SCM_FALSEP (scm_system_module_env_p (SCM_MEMOIZED_ENV (m))))
  346. {
  347. /* Back up in order to include any non-source frames */
  348. while (i > 0)
  349. {
  350. m = s->frames[i - 1].source;
  351. if (SCM_MEMOIZEDP (m))
  352. break;
  353. m = s->frames[i - 1].proc;
  354. if (!SCM_FALSEP (scm_procedure_p (m))
  355. && !SCM_FALSEP (scm_procedure_property
  356. (m, scm_sym_system_procedure)))
  357. break;
  358. --i;
  359. ++inner;
  360. }
  361. break;
  362. }
  363. }
  364. }
  365. else
  366. /* Use standard cutting procedure. */
  367. {
  368. for (i = 0; inner; --inner)
  369. if (SCM_EQ_P (s->frames[i++].proc, inner_key))
  370. break;
  371. }
  372. s->frames = &s->frames[i];
  373. n -= i;
  374. /* Cut outer part. */
  375. for (; n && outer; --outer)
  376. if (SCM_EQ_P (s->frames[--n].proc, outer_key))
  377. break;
  378. s->length = n;
  379. }
  380. /* Stacks
  381. */
  382. SCM scm_stack_type;
  383. SCM_DEFINE (scm_stack_p, "stack?", 1, 0, 0,
  384. (SCM obj),
  385. "Return @code{#t} if @var{obj} is a calling stack.")
  386. #define FUNC_NAME s_scm_stack_p
  387. {
  388. return SCM_BOOL(SCM_STACKP (obj));
  389. }
  390. #undef FUNC_NAME
  391. SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
  392. (SCM obj, SCM args),
  393. "Create a new stack. If @var{obj} is @code{#t}, the current\n"
  394. "evaluation stack is used for creating the stack frames,\n"
  395. "otherwise the frames are taken from @var{obj} (which must be\n"
  396. "either a debug object or a continuation).\n\n"
  397. "@var{args} should be a list containing any combination of\n"
  398. "integer, procedure and @code{#t} values.\n\n"
  399. "These values specify various ways of cutting away uninteresting\n"
  400. "stack frames from the top and bottom of the stack that\n"
  401. "@code{make-stack} returns. They come in pairs like this:\n"
  402. "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
  403. "@var{outer_cut_2} @dots{})}.\n\n"
  404. "Each @var{inner_cut_N} can be @code{#t}, an integer, or a\n"
  405. "procedure. @code{#t} means to cut away all frames up to but\n"
  406. "excluding the first user module frame. An integer means to cut\n"
  407. "away exactly that number of frames. A procedure means to cut\n"
  408. "away all frames up to but excluding the application frame whose\n"
  409. "procedure matches the specified one.\n\n"
  410. "Each @var{outer_cut_N} can be an integer or a procedure. An\n"
  411. "integer means to cut away that number of frames. A procedure\n"
  412. "means to cut away frames down to but excluding the application\n"
  413. "frame whose procedure matches the specified one.\n\n"
  414. "If the @var{outer_cut_N} of the last pair is missing, it is\n"
  415. "taken as 0.")
  416. #define FUNC_NAME s_scm_make_stack
  417. {
  418. long n, size;
  419. int maxp;
  420. scm_t_debug_frame *dframe;
  421. scm_t_info_frame *iframe;
  422. long offset = 0;
  423. SCM stack, id;
  424. SCM inner_cut, outer_cut;
  425. /* Extract a pointer to the innermost frame of whatever object
  426. scm_make_stack was given. */
  427. if (SCM_EQ_P (obj, SCM_BOOL_T))
  428. {
  429. dframe = scm_last_debug_frame;
  430. }
  431. else if (SCM_DEBUGOBJP (obj))
  432. {
  433. dframe = SCM_DEBUGOBJ_FRAME (obj);
  434. }
  435. else if (SCM_CONTINUATIONP (obj))
  436. {
  437. scm_t_contregs *cont = SCM_CONTREGS (obj);
  438. offset = cont->offset;
  439. dframe = RELOC_FRAME (cont->dframe, offset);
  440. }
  441. else
  442. {
  443. SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
  444. /* not reached */
  445. }
  446. /* Count number of frames. Also get stack id tag and check whether
  447. there are more stackframes than we want to record
  448. (SCM_BACKTRACE_MAXDEPTH). */
  449. id = SCM_BOOL_F;
  450. maxp = 0;
  451. n = stack_depth (dframe, offset, &id, &maxp);
  452. size = n * SCM_FRAME_N_SLOTS;
  453. /* Make the stack object. */
  454. stack = scm_make_struct (scm_stack_type, SCM_MAKINUM (size), SCM_EOL);
  455. SCM_STACK (stack) -> id = id;
  456. iframe = &SCM_STACK (stack) -> tail[0];
  457. SCM_STACK (stack) -> frames = iframe;
  458. /* Translate the current chain of stack frames into debugging information. */
  459. n = read_frames (dframe, offset, n, iframe);
  460. SCM_STACK (stack) -> length = n;
  461. /* Narrow the stack according to the arguments given to scm_make_stack. */
  462. SCM_VALIDATE_REST_ARGUMENT (args);
  463. while (n > 0 && !SCM_NULLP (args))
  464. {
  465. inner_cut = SCM_CAR (args);
  466. args = SCM_CDR (args);
  467. if (SCM_NULLP (args))
  468. {
  469. outer_cut = SCM_INUM0;
  470. }
  471. else
  472. {
  473. outer_cut = SCM_CAR (args);
  474. args = SCM_CDR (args);
  475. }
  476. narrow_stack (stack,
  477. SCM_INUMP (inner_cut) ? SCM_INUM (inner_cut) : n,
  478. SCM_INUMP (inner_cut) ? 0 : inner_cut,
  479. SCM_INUMP (outer_cut) ? SCM_INUM (outer_cut) : n,
  480. SCM_INUMP (outer_cut) ? 0 : outer_cut);
  481. n = SCM_STACK (stack) -> length;
  482. }
  483. if (n > 0)
  484. {
  485. if (maxp)
  486. iframe[n - 1].flags |= SCM_FRAMEF_OVERFLOW;
  487. return stack;
  488. }
  489. else
  490. return SCM_BOOL_F;
  491. }
  492. #undef FUNC_NAME
  493. SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
  494. (SCM stack),
  495. "Return the identifier given to @var{stack} by @code{start-stack}.")
  496. #define FUNC_NAME s_scm_stack_id
  497. {
  498. scm_t_debug_frame *dframe;
  499. long offset = 0;
  500. if (SCM_EQ_P (stack, SCM_BOOL_T))
  501. {
  502. dframe = scm_last_debug_frame;
  503. }
  504. else if (SCM_DEBUGOBJP (stack))
  505. {
  506. dframe = SCM_DEBUGOBJ_FRAME (stack);
  507. }
  508. else if (SCM_CONTINUATIONP (stack))
  509. {
  510. scm_t_contregs *cont = SCM_CONTREGS (stack);
  511. offset = cont->offset;
  512. dframe = RELOC_FRAME (cont->dframe, offset);
  513. }
  514. else if (SCM_STACKP (stack))
  515. {
  516. return SCM_STACK (stack) -> id;
  517. }
  518. else
  519. {
  520. SCM_WRONG_TYPE_ARG (1, stack);
  521. }
  522. while (dframe && !SCM_VOIDFRAMEP (*dframe))
  523. dframe = RELOC_FRAME (dframe->prev, offset);
  524. if (dframe && SCM_VOIDFRAMEP (*dframe))
  525. return RELOC_INFO (dframe->vect, offset)[0].id;
  526. return SCM_BOOL_F;
  527. }
  528. #undef FUNC_NAME
  529. SCM_DEFINE (scm_stack_ref, "stack-ref", 2, 0, 0,
  530. (SCM stack, SCM index),
  531. "Return the @var{index}'th frame from @var{stack}.")
  532. #define FUNC_NAME s_scm_stack_ref
  533. {
  534. unsigned long int c_index;
  535. SCM_VALIDATE_STACK (1, stack);
  536. SCM_VALIDATE_INUM (2, index);
  537. SCM_ASSERT_RANGE (1, index, SCM_INUM (index) >= 0);
  538. c_index = SCM_INUM (index);
  539. SCM_ASSERT_RANGE (1, index, c_index < SCM_STACK_LENGTH (stack));
  540. return scm_cons (stack, index);
  541. }
  542. #undef FUNC_NAME
  543. SCM_DEFINE (scm_stack_length, "stack-length", 1, 0, 0,
  544. (SCM stack),
  545. "Return the length of @var{stack}.")
  546. #define FUNC_NAME s_scm_stack_length
  547. {
  548. SCM_VALIDATE_STACK (1,stack);
  549. return SCM_MAKINUM (SCM_STACK_LENGTH (stack));
  550. }
  551. #undef FUNC_NAME
  552. /* Frames
  553. */
  554. SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
  555. (SCM obj),
  556. "Return @code{#t} if @var{obj} is a stack frame.")
  557. #define FUNC_NAME s_scm_frame_p
  558. {
  559. return SCM_BOOL(SCM_FRAMEP (obj));
  560. }
  561. #undef FUNC_NAME
  562. SCM_DEFINE (scm_last_stack_frame, "last-stack-frame", 1, 0, 0,
  563. (SCM obj),
  564. "Return a stack which consists of a single frame, which is the\n"
  565. "last stack frame for @var{obj}. @var{obj} must be either a\n"
  566. "debug object or a continuation.")
  567. #define FUNC_NAME s_scm_last_stack_frame
  568. {
  569. scm_t_debug_frame *dframe;
  570. long offset = 0;
  571. SCM stack;
  572. if (SCM_DEBUGOBJP (obj))
  573. {
  574. dframe = SCM_DEBUGOBJ_FRAME (obj);
  575. }
  576. else if (SCM_CONTINUATIONP (obj))
  577. {
  578. scm_t_contregs *cont = SCM_CONTREGS (obj);
  579. offset = cont->offset;
  580. dframe = RELOC_FRAME (cont->dframe, offset);
  581. }
  582. else
  583. {
  584. SCM_WRONG_TYPE_ARG (1, obj);
  585. /* not reached */
  586. }
  587. if (!dframe || SCM_VOIDFRAMEP (*dframe))
  588. return SCM_BOOL_F;
  589. stack = scm_make_struct (scm_stack_type, SCM_MAKINUM (SCM_FRAME_N_SLOTS),
  590. SCM_EOL);
  591. SCM_STACK (stack) -> length = 1;
  592. SCM_STACK (stack) -> frames = &SCM_STACK (stack) -> tail[0];
  593. read_frame (dframe, offset,
  594. (scm_t_info_frame *) &SCM_STACK (stack) -> frames[0]);
  595. return scm_cons (stack, SCM_INUM0);
  596. }
  597. #undef FUNC_NAME
  598. SCM_DEFINE (scm_frame_number, "frame-number", 1, 0, 0,
  599. (SCM frame),
  600. "Return the frame number of @var{frame}.")
  601. #define FUNC_NAME s_scm_frame_number
  602. {
  603. SCM_VALIDATE_FRAME (1,frame);
  604. return SCM_MAKINUM (SCM_FRAME_NUMBER (frame));
  605. }
  606. #undef FUNC_NAME
  607. SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
  608. (SCM frame),
  609. "Return the source of @var{frame}.")
  610. #define FUNC_NAME s_scm_frame_source
  611. {
  612. SCM_VALIDATE_FRAME (1,frame);
  613. return SCM_FRAME_SOURCE (frame);
  614. }
  615. #undef FUNC_NAME
  616. SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
  617. (SCM frame),
  618. "Return the procedure for @var{frame}, or @code{#f} if no\n"
  619. "procedure is associated with @var{frame}.")
  620. #define FUNC_NAME s_scm_frame_procedure
  621. {
  622. SCM_VALIDATE_FRAME (1,frame);
  623. return (SCM_FRAME_PROC_P (frame)
  624. ? SCM_FRAME_PROC (frame)
  625. : SCM_BOOL_F);
  626. }
  627. #undef FUNC_NAME
  628. SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
  629. (SCM frame),
  630. "Return the arguments of @var{frame}.")
  631. #define FUNC_NAME s_scm_frame_arguments
  632. {
  633. SCM_VALIDATE_FRAME (1,frame);
  634. return SCM_FRAME_ARGS (frame);
  635. }
  636. #undef FUNC_NAME
  637. SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
  638. (SCM frame),
  639. "Return the previous frame of @var{frame}, or @code{#f} if\n"
  640. "@var{frame} is the first frame in its stack.")
  641. #define FUNC_NAME s_scm_frame_previous
  642. {
  643. unsigned long int n;
  644. SCM_VALIDATE_FRAME (1, frame);
  645. n = SCM_INUM (SCM_CDR (frame)) + 1;
  646. if (n >= SCM_STACK_LENGTH (SCM_CAR (frame)))
  647. return SCM_BOOL_F;
  648. else
  649. return scm_cons (SCM_CAR (frame), SCM_MAKINUM (n));
  650. }
  651. #undef FUNC_NAME
  652. SCM_DEFINE (scm_frame_next, "frame-next", 1, 0, 0,
  653. (SCM frame),
  654. "Return the next frame of @var{frame}, or @code{#f} if\n"
  655. "@var{frame} is the last frame in its stack.")
  656. #define FUNC_NAME s_scm_frame_next
  657. {
  658. unsigned long int n;
  659. SCM_VALIDATE_FRAME (1, frame);
  660. n = SCM_INUM (SCM_CDR (frame));
  661. if (n == 0)
  662. return SCM_BOOL_F;
  663. else
  664. return scm_cons (SCM_CAR (frame), SCM_MAKINUM (n - 1));
  665. }
  666. #undef FUNC_NAME
  667. SCM_DEFINE (scm_frame_real_p, "frame-real?", 1, 0, 0,
  668. (SCM frame),
  669. "Return @code{#t} if @var{frame} is a real frame.")
  670. #define FUNC_NAME s_scm_frame_real_p
  671. {
  672. SCM_VALIDATE_FRAME (1,frame);
  673. return SCM_BOOL(SCM_FRAME_REAL_P (frame));
  674. }
  675. #undef FUNC_NAME
  676. SCM_DEFINE (scm_frame_procedure_p, "frame-procedure?", 1, 0, 0,
  677. (SCM frame),
  678. "Return @code{#t} if a procedure is associated with @var{frame}.")
  679. #define FUNC_NAME s_scm_frame_procedure_p
  680. {
  681. SCM_VALIDATE_FRAME (1,frame);
  682. return SCM_BOOL(SCM_FRAME_PROC_P (frame));
  683. }
  684. #undef FUNC_NAME
  685. SCM_DEFINE (scm_frame_evaluating_args_p, "frame-evaluating-args?", 1, 0, 0,
  686. (SCM frame),
  687. "Return @code{#t} if @var{frame} contains evaluated arguments.")
  688. #define FUNC_NAME s_scm_frame_evaluating_args_p
  689. {
  690. SCM_VALIDATE_FRAME (1,frame);
  691. return SCM_BOOL(SCM_FRAME_EVAL_ARGS_P (frame));
  692. }
  693. #undef FUNC_NAME
  694. SCM_DEFINE (scm_frame_overflow_p, "frame-overflow?", 1, 0, 0,
  695. (SCM frame),
  696. "Return @code{#t} if @var{frame} is an overflow frame.")
  697. #define FUNC_NAME s_scm_frame_overflow_p
  698. {
  699. SCM_VALIDATE_FRAME (1,frame);
  700. return SCM_BOOL(SCM_FRAME_OVERFLOW_P (frame));
  701. }
  702. #undef FUNC_NAME
  703. void
  704. scm_init_stacks ()
  705. {
  706. SCM vtable;
  707. SCM stack_layout
  708. = scm_make_struct_layout (scm_makfrom0str (SCM_STACK_LAYOUT));
  709. vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
  710. scm_stack_type
  711. = scm_permanent_object (scm_make_struct (vtable, SCM_INUM0,
  712. scm_cons (stack_layout,
  713. SCM_EOL)));
  714. scm_set_struct_vtable_name_x (scm_stack_type, scm_str2symbol ("stack"));
  715. #include "libguile/stacks.x"
  716. }
  717. /*
  718. Local Variables:
  719. c-file-style: "gnu"
  720. End:
  721. */