Initial revision
[bpt/guile.git] / libguile / stacks.c
index 63bbda0..508b2e2 100644 (file)
@@ -1,5 +1,5 @@
 /* Representation of stack frame debug information
- * Copyright (C) 1996,1997, 2000 Free Software Foundation
+ * Copyright (C) 1996,1997,2000,2001 Free Software Foundation
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,8 +43,6 @@
  * The author can be reached at djurfeldt@nada.kth.se
  * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
 
-/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
-   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
 
 \f
 
  * Representation:
  *
  * The stack is represented as a struct with an id slot and a tail
- * array of scm_info_frame structs.
+ * array of scm_t_info_frame structs.
  *
  * A frame is represented as a pair where the car contains a stack and
  * the cdr an inum.  The inum is an index to the first SCM value of
- * the scm_info_frame struct.
+ * the scm_t_info_frame struct.
  *
  * Stacks
  *   Constructor
  */
 
 /* Stacks often contain pointers to other items on the stack; for
-   example, each scm_debug_frame structure contains a pointer to the
+   example, each scm_t_debug_frame structure contains a pointer to the
    next frame out.  When we capture a continuation, we copy the stack
    into the heap, and just leave all the pointers unchanged.  This
    makes it simple to restore the continuation --- just copy the stack
    OFFSET) is a pointer to the copy in the continuation of the
    original referent, cast to an scm_debug_MUMBLE *.  */
 #define RELOC_INFO(ptr, offset) \
-  ((scm_debug_info *) ((SCM_STACKITEM *) (ptr) + (offset)))
+  ((scm_t_debug_info *) ((SCM_STACKITEM *) (ptr) + (offset)))
 #define RELOC_FRAME(ptr, offset) \
-  ((scm_debug_frame *) ((SCM_STACKITEM *) (ptr) + (offset)))
+  ((scm_t_debug_frame *) ((SCM_STACKITEM *) (ptr) + (offset)))
 
 
 /* Count number of debug info frames on a stack, beginning with
  * DFRAME.  OFFSET is used for relocation of pointers when the stack
  * is read from a continuation.
  */
-static int
-stack_depth (scm_debug_frame *dframe,long offset,SCM *id,int *maxp)
+static scm_t_bits
+stack_depth (scm_t_debug_frame *dframe,long offset,SCM *id,int *maxp)
 {
-  int n;
-  int max_depth = SCM_BACKTRACE_MAXDEPTH;
+  long n;
+  long max_depth = SCM_BACKTRACE_MAXDEPTH;
   for (n = 0;
        dframe && !SCM_VOIDFRAMEP (*dframe) && n < max_depth;
        dframe = RELOC_FRAME (dframe->prev, offset))
     {
       if (SCM_EVALFRAMEP (*dframe))
        {
-         scm_debug_info * info = RELOC_INFO (dframe->info, offset);
+         scm_t_debug_info * info = RELOC_INFO (dframe->info, offset);
          n += (info - dframe->vect) / 2 + 1;
          /* Data in the apply part of an eval info frame comes from previous
-            stack frame if the scm_debug_info vector is overflowed. */
+            stack frame if the scm_t_debug_info vector is overflowed. */
          if ((((info - dframe->vect) & 1) == 0)
              && SCM_OVERFLOWP (*dframe)
              && !SCM_UNBNDP (info[1].a.proc))
@@ -185,12 +183,12 @@ stack_depth (scm_debug_frame *dframe,long offset,SCM *id,int *maxp)
 /* Read debug info from DFRAME into IFRAME.
  */
 static void
-read_frame (scm_debug_frame *dframe,long offset,scm_info_frame *iframe)
+read_frame (scm_t_debug_frame *dframe,long offset,scm_t_info_frame *iframe)
 {
-  scm_bits_t flags = SCM_UNPACK (SCM_INUM0); /* UGh. */
+  scm_t_bits flags = SCM_UNPACK (SCM_INUM0); /* UGh. */
   if (SCM_EVALFRAMEP (*dframe))
     {
-      scm_debug_info * info = RELOC_INFO (dframe->info, offset);
+      scm_t_debug_info * info = RELOC_INFO (dframe->info, offset);
       if ((info - dframe->vect) & 1)
        {
          /* Debug.vect ends with apply info. */
@@ -223,14 +221,14 @@ get_applybody ()
 {
   SCM var = scm_sym2var (scm_sym_apply, SCM_BOOL_F, SCM_BOOL_F);
   if (SCM_VARIABLEP (var) && SCM_CLOSUREP (SCM_VARIABLE_REF (var)))
-    return SCM_CADR (SCM_CODE (SCM_VARIABLE_REF (var)));
+    return SCM_CAR (SCM_CLOSURE_BODY (SCM_VARIABLE_REF (var)));
   else
     return SCM_UNDEFINED;
 }
 
 #define NEXT_FRAME(iframe, n, quit) \
 do { \
-  if (SCM_NIMP (iframe->source) \
+  if (SCM_MEMOIZEDP (iframe->source) \
       && SCM_EQ_P (SCM_MEMOIZED_EXP (iframe->source), applybody)) \
     { \
       iframe->source = SCM_BOOL_F; \
@@ -246,16 +244,16 @@ do { \
 } while (0)
 
 
-/* Fill the scm_info_frame vector IFRAME with data from N stack frames
+/* Fill the scm_t_info_frame vector IFRAME with data from N stack frames
  * starting with the first stack frame represented by debug frame
  * DFRAME.
  */
 
-static int
-read_frames (scm_debug_frame *dframe,long offset,int n,scm_info_frame *iframes)
+static scm_t_bits
+read_frames (scm_t_debug_frame *dframe,long offset,long n,scm_t_info_frame *iframes)
 {
-  scm_info_frame *iframe = iframes;
-  scm_debug_info *info;
+  scm_t_info_frame *iframe = iframes;
+  scm_t_debug_info *info;
   static SCM applybody = SCM_UNDEFINED;
   
   /* The value of applybody has to be setup after r4rs.scm has executed. */
@@ -280,7 +278,8 @@ read_frames (scm_debug_frame *dframe,long offset,int n,scm_info_frame *iframes)
          if ((info - dframe->vect) & 1)
            --info;
          /* Data in the apply part of an eval info frame comes from
-            previous stack frame if the scm_debug_info vector is overflowed. */
+            previous stack frame if the scm_t_debug_info vector is
+            overflowed. */
          else if (SCM_OVERFLOWP (*dframe)
                   && !SCM_UNBNDP (info[1].a.proc))
            {
@@ -345,31 +344,36 @@ read_frames (scm_debug_frame *dframe,long offset,int n,scm_info_frame *iframes)
  */
 
 static void
-narrow_stack (SCM stack,int inner,SCM inner_key,int outer,SCM outer_key)
+narrow_stack (SCM stack,long inner,SCM inner_key,long outer,SCM outer_key)
 {
-  scm_stack *s = SCM_STACK (stack);
-  int i;
-  int n = s->length;
+  scm_t_stack *s = SCM_STACK (stack);
+  unsigned long int i;
+  long n = s->length;
   
   /* Cut inner part. */
   if (SCM_EQ_P (inner_key, SCM_BOOL_T))
-    /* Cut all frames up to user module code */
     {
+      /* Cut all frames up to user module code */
       for (i = 0; inner; ++i, --inner)
        {
          SCM m = s->frames[i].source;
-         if (   SCM_MEMOIZEDP (m)
-             && SCM_NIMP (SCM_MEMOIZED_ENV (m))
+         if (SCM_MEMOIZEDP (m)
+             && !SCM_IMP (SCM_MEMOIZED_ENV (m))
              && SCM_FALSEP (scm_system_module_env_p (SCM_MEMOIZED_ENV (m))))
            {
              /* Back up in order to include any non-source frames */
-             while (i > 0
-                    && !((m = s->frames[i - 1].source, SCM_MEMOIZEDP (m))
-                         || (SCM_NIMP (m = s->frames[i - 1].proc)
-                             && SCM_NFALSEP (scm_procedure_p (m))
-                             && SCM_NFALSEP (scm_procedure_property
-                                             (m, scm_sym_system_procedure)))))
+             while (i > 0)
                {
+                 m = s->frames[i - 1].source;
+                 if (SCM_MEMOIZEDP (m))
+                   break;
+
+                 m = s->frames[i - 1].proc;
+                 if (!SCM_FALSEP (scm_procedure_p (m))
+                     && !SCM_FALSEP (scm_procedure_property
+                                     (m, scm_sym_system_procedure)))
+                   break;
+
                  --i;
                  ++inner;
                }
@@ -416,41 +420,59 @@ SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
            "Create a new stack. If @var{obj} is @code{#t}, the current\n"
            "evaluation stack is used for creating the stack frames,\n"
            "otherwise the frames are taken from @var{obj} (which must be\n"
-           "either a debug object or a continuation).\n"
-           "@var{args} must be a list of integers and specifies how the\n"
-           "resulting stack will be narrowed.")
+           "either a debug object or a continuation).\n\n"
+           "@var{args} should be a list containing any combination of\n"
+           "integer, procedure and @code{#t} values.\n\n"
+           "These values specify various ways of cutting away uninteresting\n"
+           "stack frames from the top and bottom of the stack that\n"
+           "@code{make-stack} returns.  They come in pairs like this:\n"
+           "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
+           "@var{outer_cut_2} @dots{})}.\n\n"
+           "Each @var{inner_cut_N} can be @code{#t}, an integer, or a\n"
+           "procedure.  @code{#t} means to cut away all frames up to but\n"
+           "excluding the first user module frame.  An integer means to cut\n"
+           "away exactly that number of frames.  A procedure means to cut\n"
+           "away all frames up to but excluding the application frame whose\n"
+           "procedure matches the specified one.\n\n"
+           "Each @var{outer_cut_N} can be an integer or a procedure.  An\n"
+           "integer means to cut away that number of frames.  A procedure\n"
+           "means to cut away frames down to but excluding the application\n"
+           "frame whose procedure matches the specified one.\n\n"
+           "If the @var{outer_cut_N} of the last pair is missing, it is\n"
+           "taken as 0.")
 #define FUNC_NAME s_scm_make_stack
 {
-  int n, maxp, size;
-  scm_debug_frame *dframe = scm_last_debug_frame;
-  scm_info_frame *iframe;
+  long n, size;
+  int maxp;
+  scm_t_debug_frame *dframe;
+  scm_t_info_frame *iframe;
   long offset = 0;
   SCM stack, id;
   SCM inner_cut, outer_cut;
 
   /* Extract a pointer to the innermost frame of whatever object
      scm_make_stack was given.  */
-  /* just use dframe == scm_last_debug_frame 
-     (from initialization of dframe, above) if obj is #t */
-  if (!SCM_EQ_P (obj, SCM_BOOL_T))
+  if (SCM_EQ_P (obj, SCM_BOOL_T))
     {
-      SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, FUNC_NAME);
-      if (SCM_DEBUGOBJP (obj))
-       dframe = (scm_debug_frame *) SCM_DEBUGOBJ_FRAME (obj);
-      else if (SCM_CONTINUATIONP (obj))
-       {
-         offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (obj) + sizeof (scm_contregs))
-                   - SCM_BASE (obj));
+      dframe = scm_last_debug_frame;
+    }
+  else if (SCM_DEBUGOBJP (obj))
+    {
+      dframe = SCM_DEBUGOBJ_FRAME (obj);
+    }
+  else if (SCM_CONTINUATIONP (obj))
+    {
+      offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (obj) + sizeof (scm_t_contregs))
+               - SCM_BASE (obj));
 #ifndef STACK_GROWS_UP
-         offset += SCM_CONTINUATION_LENGTH (obj);
+      offset += SCM_CONTINUATION_LENGTH (obj);
 #endif
-         dframe = RELOC_FRAME (SCM_DFRAME (obj), offset);
-       }
-      else
-       {
-         SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
-         /* not reached */
-       }
+      dframe = RELOC_FRAME (SCM_DFRAME (obj), offset);
+    }
+  else
+    {
+      SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
+      /* not reached */
     }
 
   /* Count number of frames.  Also get stack id tag and check whether
@@ -479,7 +501,7 @@ SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
       args = SCM_CDR (args);
       if (SCM_NULLP (args)) 
        {
-       outer_cut = SCM_INUM0;
+         outer_cut = SCM_INUM0;
        } 
       else
        {
@@ -512,29 +534,34 @@ SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
            "Return the identifier given to @var{stack} by @code{start-stack}.")
 #define FUNC_NAME s_scm_stack_id
 {
-  scm_debug_frame *dframe;
+  scm_t_debug_frame *dframe;
   long offset = 0;
   if (SCM_EQ_P (stack, SCM_BOOL_T))
-    dframe = scm_last_debug_frame;
-  else
     {
-      SCM_VALIDATE_NIM (1,stack);
-      if (SCM_DEBUGOBJP (stack))
-       dframe = (scm_debug_frame *) SCM_DEBUGOBJ_FRAME (stack);
-      else if (SCM_CONTINUATIONP (stack))
-       {
-         offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (stack) + sizeof (scm_contregs))
-                   - SCM_BASE (stack));
+      dframe = scm_last_debug_frame;
+    }
+  else if (SCM_DEBUGOBJP (stack))
+    {
+      dframe = SCM_DEBUGOBJ_FRAME (stack);
+    }
+  else if (SCM_CONTINUATIONP (stack))
+    {
+      offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (stack) + sizeof (scm_t_contregs))
+               - SCM_BASE (stack));
 #ifndef STACK_GROWS_UP
-         offset += SCM_CONTINUATION_LENGTH (stack);
+      offset += SCM_CONTINUATION_LENGTH (stack);
 #endif
-         dframe = RELOC_FRAME (SCM_DFRAME (stack), offset);
-       }
-      else if (SCM_STACKP (stack))
-       return SCM_STACK (stack) -> id;
-      else
-       SCM_WRONG_TYPE_ARG (1, stack);
+      dframe = RELOC_FRAME (SCM_DFRAME (stack), offset);
     }
+  else if (SCM_STACKP (stack))
+    {
+      return SCM_STACK (stack) -> id;
+    }
+  else
+    {
+      SCM_WRONG_TYPE_ARG (1, stack);
+    }
+
   while (dframe && !SCM_VOIDFRAMEP (*dframe))
     dframe = RELOC_FRAME (dframe->prev, offset);
   if (dframe && SCM_VOIDFRAMEP (*dframe))
@@ -544,16 +571,18 @@ SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_stack_ref, "stack-ref", 2, 0, 0,
-            (SCM stack, SCM i),
-           "Return the @var{i}'th frame from @var{stack}.")
+            (SCM stack, SCM index),
+           "Return the @var{index}'th frame from @var{stack}.")
 #define FUNC_NAME s_scm_stack_ref
 {
-  SCM_VALIDATE_STACK (1,stack);
-  SCM_VALIDATE_INUM (2,i);
-  SCM_ASSERT_RANGE (1,i,
-                    SCM_INUM (i) >= 0 && 
-                    SCM_INUM (i) < SCM_STACK_LENGTH (stack));
-  return scm_cons (stack, i);
+  unsigned long int c_index;
+
+  SCM_VALIDATE_STACK (1, stack);
+  SCM_VALIDATE_INUM (2, index);
+  SCM_ASSERT_RANGE (1, index, SCM_INUM (index) >= 0);
+  c_index = SCM_INUM (index);
+  SCM_ASSERT_RANGE (1, index, c_index < SCM_STACK_LENGTH (stack));
+  return scm_cons (stack, index);
 }
 #undef FUNC_NAME
 
@@ -586,16 +615,17 @@ SCM_DEFINE (scm_last_stack_frame, "last-stack-frame", 1, 0, 0,
            "debug object or a continuation.")
 #define FUNC_NAME s_scm_last_stack_frame
 {
-  scm_debug_frame *dframe;
+  scm_t_debug_frame *dframe;
   long offset = 0;
   SCM stack;
   
-  SCM_VALIDATE_NIM (1,obj);
   if (SCM_DEBUGOBJP (obj))
-    dframe = (scm_debug_frame *) SCM_DEBUGOBJ_FRAME (obj);
+    {
+      dframe = SCM_DEBUGOBJ_FRAME (obj);
+    }
   else if (SCM_CONTINUATIONP (obj))
     {
-      offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (obj) + sizeof (scm_contregs))
+      offset = ((SCM_STACKITEM *) ((char *) SCM_CONTREGS (obj) + sizeof (scm_t_contregs))
                - SCM_BASE (obj));
 #ifndef STACK_GROWS_UP
       offset += SCM_CONTINUATION_LENGTH (obj);
@@ -616,9 +646,9 @@ SCM_DEFINE (scm_last_stack_frame, "last-stack-frame", 1, 0, 0,
   SCM_STACK (stack) -> length = 1;
   SCM_STACK (stack) -> frames = &SCM_STACK (stack) -> tail[0];
   read_frame (dframe, offset,
-             (scm_info_frame *) &SCM_STACK (stack) -> frames[0]);
+             (scm_t_info_frame *) &SCM_STACK (stack) -> frames[0]);
   
-  return scm_cons (stack, SCM_INUM0);;
+  return scm_cons (stack, SCM_INUM0);
 }
 #undef FUNC_NAME
 
@@ -671,8 +701,8 @@ SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
            "@var{frame} is the first frame in its stack.")
 #define FUNC_NAME s_scm_frame_previous
 {
-  int n;
-  SCM_VALIDATE_FRAME (1,frame);
+  unsigned long int n;
+  SCM_VALIDATE_FRAME (1, frame);
   n = SCM_INUM (SCM_CDR (frame)) + 1;
   if (n >= SCM_STACK_LENGTH (SCM_CAR (frame)))
     return SCM_BOOL_F;
@@ -687,13 +717,13 @@ SCM_DEFINE (scm_frame_next, "frame-next", 1, 0, 0,
            "@var{frame} is the last frame in its stack.")
 #define FUNC_NAME s_scm_frame_next
 {
-  int n;
-  SCM_VALIDATE_FRAME (1,frame);
-  n = SCM_INUM (SCM_CDR (frame)) - 1;
-  if (n < 0)
+  unsigned long int n;
+  SCM_VALIDATE_FRAME (1, frame);
+  n = SCM_INUM (SCM_CDR (frame));
+  if (n == 0)
     return SCM_BOOL_F;
   else
-    return scm_cons (SCM_CAR (frame), SCM_MAKINUM (n));
+    return scm_cons (SCM_CAR (frame), SCM_MAKINUM (n - 1));
 }
 #undef FUNC_NAME