call-with-prompt always compiles to CPS $prompt
[bpt/guile.git] / libguile / control.c
index 9121d17..3f2651c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2010, 2011  Free Software Foundation, Inc.
+/* Copyright (C) 2010, 2011, 2012, 2013  Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 
 \f
 
+#define PROMPT_ESCAPE_P(p)                              \
+  (SCM_DYNSTACK_TAG_FLAGS (SCM_DYNSTACK_TAG (p))        \
+   & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
 
-SCM
-scm_c_make_prompt (SCM k, SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
-                   scm_t_uint8 escape_only_p, scm_t_int64 vm_cookie,
-                   SCM winds)
-{
-  scm_t_bits tag;
-  struct scm_prompt_registers *regs;
-
-  tag = scm_tc7_prompt;
-  if (escape_only_p)
-    tag |= (SCM_F_PROMPT_ESCAPE<<8);
-
-  regs = scm_gc_malloc_pointerless (sizeof (*regs), "prompt registers");
-  regs->fp = fp;
-  regs->sp = sp;
-  regs->ip = abort_ip;
-  regs->cookie = vm_cookie;
+\f
 
-  return scm_double_cell (tag, SCM_UNPACK (k), (scm_t_bits)regs, 
-                          SCM_UNPACK (winds));
-}
 
-/* Only to be called if the SCM_PROMPT_SETJMP returns 1 */
+/* Only to be called if the SCM_I_SETJMP returns 1 */
 SCM
 scm_i_prompt_pop_abort_args_x (SCM vm)
 {
@@ -81,44 +65,43 @@ scm_i_prompt_pop_abort_args_x (SCM vm)
 #define META_HEADER(meta)         meta, 0, 0, 0, 0,      0, 0, 0
 #endif
 
-#define ALIGN_PTR(type,p,align) (type*)(ROUND_UP (((scm_t_bits)p), align))
+#define OBJCODE_TAG SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)
 
-#ifdef SCM_ALIGNED
+#if defined (SCM_ALIGNED)
 #define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)     \
 static const type sym[]
 #define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym)  \
 static SCM_ALIGNED (alignment) const type sym[]
-#else
-#define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)     \
-static type *sym
-#define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym)                  \
-SCM_SNARF_INIT(sym = scm_malloc_pointerless (sizeof(sym##__unaligned); \
-               memcpy (sym, sym##__unaligned, sizeof(sym##__unaligned));) \
-static type *sym = NULL;                                                \
-static const type sym##__unaligned[]
-#endif
-
-#define STATIC_OBJCODE_TAG                                      \
-  SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0))
-
 #define SCM_STATIC_OBJCODE(sym)                                         \
   SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode);      \
   SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = {            \
-    { STATIC_OBJCODE_TAG, SCM_PACK (sym##__bytecode) },                 \
+    { SCM_PACK (OBJCODE_TAG), SCM_PACK (sym##__bytecode) },             \
     { SCM_BOOL_F, SCM_PACK (0) }                                        \
   };                                                                    \
   static const SCM sym = SCM_PACK (sym##__cells);                       \
   SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
+#else
+#define SCM_STATIC_OBJCODE(sym)                                         \
+static SCM sym;                                                         \
+static scm_t_uint8 *sym##_bytecode;                                     \
+SCM_SNARF_INIT(sym##_bytecode = scm_gc_malloc_pointerless (sizeof(sym##_bytecode__unaligned), "partial continuation stub"); \
+               memcpy (sym##_bytecode, sym##_bytecode__unaligned, sizeof(sym##_bytecode__unaligned));) \
+SCM_SNARF_INIT(sym = scm_double_cell (OBJCODE_TAG,                      \
+                                      (scm_t_bits)sym##_bytecode,       \
+                                      SCM_UNPACK (SCM_BOOL_F),          \
+                                      0);)                              \
+static const scm_t_uint8 sym##_bytecode__unaligned[]
+#endif
+
 
-  
 SCM_STATIC_OBJCODE (cont_objcode) = {
   /* Like in continuations.c, but with partial-cont-call. */
   OBJCODE_HEADER (8, 19),
   /* leave args on the stack */
   /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
-  /* 2 */ scm_op_object_ref, 1, /* push internal winds */
-  /* 4 */ scm_op_partial_cont_call, /* and go! */
-  /* 5 */ scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
+  /* 2 */ scm_op_partial_cont_call, /* and go! */
+  /* 3 */ scm_op_nop,
+  /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
   /* 8 */
 
   /* We could put some meta-info to say that this proc is a continuation. Not sure
@@ -126,7 +109,7 @@ SCM_STATIC_OBJCODE (cont_objcode) = {
   META_HEADER (19),
   /* 0 */ scm_op_make_eol, /* bindings */
   /* 1 */ scm_op_make_eol, /* sources */
-  /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 5, /* arity: from ip 0 to ip 7 */
+  /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
   /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
   /* 7 */ scm_op_make_int8_0, /* 0 optionals */
   /* 8 */ scm_op_make_true, /* and a rest arg */
@@ -139,45 +122,41 @@ SCM_STATIC_OBJCODE (cont_objcode) = {
 
 
 static SCM
-reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
-                            scm_t_int64 cookie)
+reify_partial_continuation (SCM vm,
+                            SCM *saved_fp, SCM *saved_sp, scm_t_uint8 *saved_ip,
+                            scm_i_jmp_buf *saved_registers,
+                            scm_t_dynstack *dynstack,
+                            scm_i_jmp_buf *current_registers)
 {
-  SCM vm_cont, dynwinds, intwinds = SCM_EOL, ret;
+  SCM vm_cont, ret;
   scm_t_uint32 flags;
 
-  /* No need to reify if the continuation is never referenced in the handler. */
-  if (SCM_PROMPT_ESCAPE_P (prompt))
-    return SCM_BOOL_F;
-
-  dynwinds = scm_i_dynwinds ();
-  while (!scm_is_eq (dynwinds, extwinds))
-    {
-      intwinds = scm_cons (scm_car (dynwinds), intwinds);
-      dynwinds = scm_cdr (dynwinds);
-    }
-
   flags = SCM_F_VM_CONT_PARTIAL;
-  if (cookie >= 0 && SCM_PROMPT_REGISTERS (prompt)->cookie == cookie)
+  /* If we are aborting to a prompt that has the same registers as those
+     of the abort, it means there are no intervening C frames on the
+     stack, and so the continuation can be relocated elsewhere on the
+     stack: it is rewindable.  */
+  if (saved_registers && saved_registers == current_registers)
     flags |= SCM_F_VM_CONT_REWINDABLE;
 
   /* Since non-escape continuations should begin with a thunk application, the
      first bit of the stack should be a frame, with the saved fp equal to the fp
      that was current when the prompt was made. */
-  if ((SCM*)SCM_UNPACK (SCM_PROMPT_REGISTERS (prompt)->sp[1])
-      != SCM_PROMPT_REGISTERS (prompt)->fp)
+  if ((SCM*)SCM_UNPACK (saved_sp[1]) != saved_fp)
     abort ();
 
   /* Capture from the top of the thunk application frame up to the end. Set an
      MVRA only, as the post-abort code is in an MV context. */
-  vm_cont = scm_i_vm_capture_stack (SCM_PROMPT_REGISTERS (prompt)->sp + 4,
+  vm_cont = scm_i_vm_capture_stack (saved_sp + 4,
                                     SCM_VM_DATA (vm)->fp,
                                     SCM_VM_DATA (vm)->sp,
                                     NULL,
                                     SCM_VM_DATA (vm)->ip,
+                                    dynstack,
                                     flags);
 
   ret = scm_make_program (cont_objcode,
-                          scm_vector (scm_list_2 (vm_cont, intwinds)),
+                          scm_c_make_vector (1, vm_cont),
                           SCM_BOOL_F);
   SCM_SET_CELL_WORD_0 (ret,
                        SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
@@ -185,48 +164,49 @@ reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
 }
 
 void
-scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
+scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv,
+             scm_i_jmp_buf *current_registers)
 {
-  SCM cont, winds, prompt = SCM_BOOL_F;
-  long delta;
+  SCM cont;
+  scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
+  scm_t_bits *prompt;
+  scm_t_dynstack_prompt_flags flags;
+  SCM *fp, *sp;
+  scm_t_uint8 *ip;
+  scm_i_jmp_buf *registers;
   size_t i;
 
-  /* Search the wind list for an appropriate prompt.
-     "Waiter, please bring us the wind list." */
-  for (winds = scm_i_dynwinds (), delta = 0;
-       scm_is_pair (winds);
-       winds = SCM_CDR (winds), delta++)
-    {
-      SCM elt = SCM_CAR (winds);
-      if (SCM_PROMPT_P (elt) && scm_is_eq (SCM_PROMPT_TAG (elt), tag))
-        {
-          prompt = elt;
-          break;
-        }
-    }
-  
-  /* If we didn't find anything, raise an error. */
-  if (scm_is_false (prompt))
+  prompt = scm_dynstack_find_prompt (dynstack, tag,
+                                     &flags, &fp, &sp, &ip, &registers);
+
+  if (!prompt)
     scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
 
-  cont = reify_partial_continuation (vm, prompt, winds, cookie);
+  /* Only reify if the continuation referenced in the handler. */
+  if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
+    cont = SCM_BOOL_F;
+  else
+    {
+      scm_t_dynstack *captured;
 
-  /* Unwind once more, beyond the prompt. */
-  winds = SCM_CDR (winds), delta++;
+      captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
+      cont = reify_partial_continuation (vm, fp, sp, ip, registers, captured,
+                                         current_registers);
+    }
 
-  /* Unwind */
-  scm_dowinds (winds, delta);
+  /* Unwind */
+  scm_dynstack_unwind (dynstack, prompt);
 
   /* Unwinding may have changed the current thread's VM, so use the
      new one.  */
   vm = scm_the_vm ();
 
   /* Restore VM regs */
-  SCM_VM_DATA (vm)->fp = SCM_PROMPT_REGISTERS (prompt)->fp;
-  SCM_VM_DATA (vm)->sp = SCM_PROMPT_REGISTERS (prompt)->sp;
-  SCM_VM_DATA (vm)->ip = SCM_PROMPT_REGISTERS (prompt)->ip;
+  SCM_VM_DATA (vm)->fp = fp;
+  SCM_VM_DATA (vm)->sp = sp;
+  SCM_VM_DATA (vm)->ip = ip;
 
-  /* Since we're jumping down, we should always have enough space */
+  /* Since we're jumping down, we should always have enough space */
   if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
     abort ();
 
@@ -237,25 +217,28 @@ scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
   *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
 
   /* Jump! */
-  SCM_I_LONGJMP (SCM_PROMPT_REGISTERS (prompt)->regs, 1);
+  SCM_I_LONGJMP (*registers, 1);
 
   /* Shouldn't get here */
   abort ();
 }
 
-SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
-            "Abort to the nearest prompt with tag @var{tag}.")
-#define FUNC_NAME s_scm_at_abort
+SCM_DEFINE (scm_abort_to_prompt_star, "abort-to-prompt*", 2, 0, 0,
+            (SCM tag, SCM args),
+            "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
+            "values in the list, @var{args}.")
+#define FUNC_NAME s_scm_abort_to_prompt_star
 {
   SCM *argv;
-  size_t i, n;
+  size_t i;
+  long n;
 
   SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
   argv = alloca (sizeof (SCM)*n);
   for (i = 0; i < n; i++, args = scm_cdr (args))
     argv[i] = scm_car (args);
 
-  scm_c_abort (scm_the_vm (), tag, n, argv, -1);
+  scm_c_abort (scm_the_vm (), tag, n, argv, NULL);
 
   /* Oh, what, you're still here? The abort must have been reinstated. Actually,
      that's quite impossible, given that we're already in C-land here, so...
@@ -265,14 +248,6 @@ SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
 }
 #undef FUNC_NAME
 
-void
-scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
-{
-  scm_puts ("#<prompt ", port);
-  scm_intprint (SCM_UNPACK (exp), 16, port);
-  scm_putc ('>', port);
-}
-
 void
 scm_init_control (void)
 {