| 1 | /* Copyright (C) 1995,1996, 1997 Free Software Foundation, Inc. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; see the file COPYING. If not, write to |
| 15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 16 | * Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * As a special exception, the Free Software Foundation gives permission |
| 19 | * for additional uses of the text contained in its release of GUILE. |
| 20 | * |
| 21 | * The exception is that, if you link the GUILE library with other files |
| 22 | * to produce an executable, this does not by itself cause the |
| 23 | * resulting executable to be covered by the GNU General Public License. |
| 24 | * Your use of that executable is in no way restricted on account of |
| 25 | * linking the GUILE library code into it. |
| 26 | * |
| 27 | * This exception does not however invalidate any other reasons why |
| 28 | * the executable file might be covered by the GNU General Public License. |
| 29 | * |
| 30 | * This exception applies only to the code released by the |
| 31 | * Free Software Foundation under the name GUILE. If you copy |
| 32 | * code from other Free Software Foundation releases into a copy of |
| 33 | * GUILE, as the General Public License permits, the exception does |
| 34 | * not apply to the code that you add in this way. To avoid misleading |
| 35 | * anyone as to the status of such modified files, you must delete |
| 36 | * this exception notice from them. |
| 37 | * |
| 38 | * If you write modifications of your own for GUILE, it is your choice |
| 39 | * whether to permit this exception to apply to your modifications. |
| 40 | * If you do not wish that, delete this exception notice. */ |
| 41 | \f |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include "_scm.h" |
| 45 | #include "stackchk.h" |
| 46 | #include "dynwind.h" |
| 47 | #include "eval.h" |
| 48 | #include "genio.h" |
| 49 | #include "smob.h" |
| 50 | #include "pairs.h" |
| 51 | #include "throw.h" |
| 52 | #include "fluids.h" |
| 53 | |
| 54 | #include "root.h" |
| 55 | \f |
| 56 | |
| 57 | /* Define this if you want to try out the stack allocation of cwdr's |
| 58 | jumpbuf. It works for me but I'm still worried that the dynwinds |
| 59 | might be able to make a mess. */ |
| 60 | |
| 61 | #undef USE_STACKJMPBUF |
| 62 | |
| 63 | SCM scm_sys_protects[SCM_NUM_PROTECTS]; |
| 64 | |
| 65 | long scm_tc16_root; |
| 66 | |
| 67 | #ifndef USE_THREADS |
| 68 | struct scm_root_state *scm_root; |
| 69 | #endif |
| 70 | |
| 71 | \f |
| 72 | |
| 73 | static SCM mark_root SCM_P ((SCM)); |
| 74 | |
| 75 | static SCM |
| 76 | mark_root (root) |
| 77 | SCM root; |
| 78 | { |
| 79 | scm_root_state *s = SCM_ROOT_STATE (root); |
| 80 | SCM_SETGC8MARK (root); |
| 81 | scm_gc_mark (s->rootcont); |
| 82 | scm_gc_mark (s->dynwinds); |
| 83 | scm_gc_mark (s->continuation_stack); |
| 84 | scm_gc_mark (s->continuation_stack_ptr); |
| 85 | scm_gc_mark (s->progargs); |
| 86 | scm_gc_mark (s->exitval); |
| 87 | scm_gc_mark (s->cur_inp); |
| 88 | scm_gc_mark (s->cur_outp); |
| 89 | scm_gc_mark (s->cur_errp); |
| 90 | scm_gc_mark (s->def_inp); |
| 91 | scm_gc_mark (s->def_outp); |
| 92 | scm_gc_mark (s->def_errp); |
| 93 | scm_gc_mark (s->fluids); |
| 94 | scm_gc_mark (s->top_level_lookup_closure_var); |
| 95 | scm_gc_mark (s->system_transformer); |
| 96 | scm_gc_mark (s->the_last_stack_var); |
| 97 | return SCM_ROOT_STATE (root) -> parent; |
| 98 | } |
| 99 | |
| 100 | static scm_sizet free_root SCM_P ((SCM)); |
| 101 | |
| 102 | static scm_sizet |
| 103 | free_root (root) |
| 104 | SCM root; |
| 105 | { |
| 106 | scm_must_free ((char *) SCM_ROOT_STATE (root)); |
| 107 | return sizeof (scm_root_state); |
| 108 | } |
| 109 | |
| 110 | static int print_root SCM_P ((SCM exp, SCM port, scm_print_state *pstate)); |
| 111 | |
| 112 | static int |
| 113 | print_root (exp, port, pstate) |
| 114 | SCM exp; |
| 115 | SCM port; |
| 116 | scm_print_state *pstate; |
| 117 | { |
| 118 | scm_puts ("#<root ", port); |
| 119 | scm_intprint(SCM_SEQ (SCM_ROOT_STATE (exp) -> rootcont), 16, port); |
| 120 | scm_putc('>', port); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | static scm_smobfuns root_smob = |
| 125 | { |
| 126 | mark_root, |
| 127 | free_root, |
| 128 | print_root, |
| 129 | 0 |
| 130 | }; |
| 131 | |
| 132 | \f |
| 133 | |
| 134 | SCM |
| 135 | scm_make_root (parent) |
| 136 | SCM parent; |
| 137 | { |
| 138 | SCM root; |
| 139 | scm_root_state *root_state; |
| 140 | |
| 141 | root_state = (scm_root_state *) scm_must_malloc (sizeof (scm_root_state), |
| 142 | "scm_make_root"); |
| 143 | if (SCM_NIMP (parent) && SCM_ROOTP (parent)) |
| 144 | { |
| 145 | memcpy (root_state, SCM_ROOT_STATE (parent), sizeof (scm_root_state)); |
| 146 | scm_copy_fluids (root_state); |
| 147 | root_state->parent = parent; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | root_state->parent = SCM_BOOL_F; |
| 152 | } |
| 153 | SCM_NEWCELL (root); |
| 154 | SCM_REDEFER_INTS; |
| 155 | SCM_SETCAR (root, scm_tc16_root); |
| 156 | SCM_SETCDR (root, root_state); |
| 157 | root_state->handle = root; |
| 158 | SCM_REALLOW_INTS; |
| 159 | return root; |
| 160 | } |
| 161 | |
| 162 | /* {call-with-dynamic-root} |
| 163 | * |
| 164 | * Suspending the current thread to evaluate a thunk on the |
| 165 | * same C stack but under a new root. |
| 166 | * |
| 167 | * Calls to call-with-dynamic-root return exactly once (unless |
| 168 | * the process is somehow exitted). */ |
| 169 | |
| 170 | /* Some questions about cwdr: |
| 171 | |
| 172 | Couldn't the body just be a closure? Do we really need to pass |
| 173 | args through to it? |
| 174 | |
| 175 | The semantics are a lot like catch's; in fact, we call |
| 176 | scm_internal_catch to take care of that part of things. Wouldn't |
| 177 | it be cleaner to say that uncaught throws just disappear into the |
| 178 | ether (or print a message to stderr), and let the caller use catch |
| 179 | themselves if they want to? |
| 180 | |
| 181 | -JimB */ |
| 182 | |
| 183 | #if 0 |
| 184 | SCM scm_exitval; /* INUM with return value */ |
| 185 | #endif |
| 186 | static int n_dynamic_roots = 0; |
| 187 | |
| 188 | |
| 189 | /* cwdr fills out both of these structures, and then passes a pointer |
| 190 | to them through scm_internal_catch to the cwdr_body and |
| 191 | cwdr_handler functions, to tell them how to behave and to get |
| 192 | information back from them. |
| 193 | |
| 194 | A cwdr is a lot like a catch, except there is no tag (all |
| 195 | exceptions are caught), and the body procedure takes the arguments |
| 196 | passed to cwdr as A1 and ARGS. The handler is also special since |
| 197 | it is not directly run from scm_internal_catch. It is executed |
| 198 | outside the new dynamic root. */ |
| 199 | |
| 200 | struct cwdr_body_data { |
| 201 | /* Arguments to pass to the cwdr body function. */ |
| 202 | SCM a1, args; |
| 203 | |
| 204 | /* Scheme procedure to use as body of cwdr. */ |
| 205 | SCM body_proc; |
| 206 | }; |
| 207 | |
| 208 | struct cwdr_handler_data { |
| 209 | /* Do we need to run the handler? */ |
| 210 | int run_handler; |
| 211 | |
| 212 | /* The tag and args to pass it. */ |
| 213 | SCM tag, args; |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | /* Invoke the body of a cwdr, assuming that the throw handler has |
| 218 | already been set up. DATA points to a struct set up by cwdr that |
| 219 | says what proc to call, and what args to apply it to. |
| 220 | |
| 221 | With a little thought, we could replace this with scm_body_thunk, |
| 222 | but I don't want to mess with that at the moment. */ |
| 223 | static SCM |
| 224 | cwdr_body (void *data, SCM jmpbuf) |
| 225 | { |
| 226 | struct cwdr_body_data *c = (struct cwdr_body_data *) data; |
| 227 | |
| 228 | return scm_apply (c->body_proc, c->a1, c->args); |
| 229 | } |
| 230 | |
| 231 | /* Record the fact that the body of the cwdr has thrown. Record |
| 232 | enough information to invoke the handler later when the dynamic |
| 233 | root has been deestablished. */ |
| 234 | |
| 235 | static SCM |
| 236 | cwdr_handler (void *data, SCM tag, SCM args) |
| 237 | { |
| 238 | struct cwdr_handler_data *c = (struct cwdr_handler_data *) data; |
| 239 | |
| 240 | c->run_handler = 1; |
| 241 | c->tag = tag; |
| 242 | c->args = args; |
| 243 | return SCM_UNSPECIFIED; |
| 244 | } |
| 245 | |
| 246 | /* This is the basic code for new root creation. |
| 247 | * |
| 248 | * WARNING! The order of actions in this routine is in many ways |
| 249 | * critical. E. g., it is essential that an error doesn't leave Guile |
| 250 | * in a messed up state. */ |
| 251 | |
| 252 | SCM |
| 253 | scm_internal_cwdr (scm_catch_body_t body, void *body_data, |
| 254 | scm_catch_handler_t handler, void *handler_data, |
| 255 | SCM_STACKITEM *stack_start) |
| 256 | { |
| 257 | #ifdef USE_STACKJMPBUF |
| 258 | scm_contregs static_jmpbuf; |
| 259 | #endif |
| 260 | int old_ints_disabled = scm_ints_disabled; |
| 261 | SCM old_rootcont, old_winds; |
| 262 | struct cwdr_handler_data my_handler_data; |
| 263 | SCM answer; |
| 264 | |
| 265 | /* Create a fresh root continuation. */ |
| 266 | { |
| 267 | SCM new_rootcont; |
| 268 | SCM_NEWCELL (new_rootcont); |
| 269 | SCM_REDEFER_INTS; |
| 270 | #ifdef USE_STACKJMPBUF |
| 271 | SCM_SETJMPBUF (new_rootcont, &static_jmpbuf); |
| 272 | #else |
| 273 | SCM_SETJMPBUF (new_rootcont, |
| 274 | scm_must_malloc ((long) sizeof (scm_contregs), |
| 275 | "inferior root continuation")); |
| 276 | #endif |
| 277 | SCM_SETCAR (new_rootcont, scm_tc7_contin); |
| 278 | SCM_DYNENV (new_rootcont) = SCM_EOL; |
| 279 | SCM_BASE (new_rootcont) = stack_start; |
| 280 | SCM_SEQ (new_rootcont) = ++n_dynamic_roots; |
| 281 | #ifdef DEBUG_EXTENSIONS |
| 282 | SCM_DFRAME (new_rootcont) = 0; |
| 283 | #endif |
| 284 | old_rootcont = scm_rootcont; |
| 285 | scm_rootcont = new_rootcont; |
| 286 | SCM_REALLOW_INTS; |
| 287 | } |
| 288 | |
| 289 | /* Exit caller's dynamic state. |
| 290 | */ |
| 291 | old_winds = scm_dynwinds; |
| 292 | scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds)); |
| 293 | #ifdef DEBUG_EXTENSIONS |
| 294 | SCM_DFRAME (old_rootcont) = scm_last_debug_frame; |
| 295 | scm_last_debug_frame = 0; |
| 296 | #endif |
| 297 | |
| 298 | { |
| 299 | my_handler_data.run_handler = 0; |
| 300 | answer = scm_internal_catch (SCM_BOOL_T, |
| 301 | body, body_data, |
| 302 | cwdr_handler, &my_handler_data); |
| 303 | } |
| 304 | |
| 305 | scm_dowinds (old_winds, - scm_ilength (old_winds)); |
| 306 | SCM_REDEFER_INTS; |
| 307 | #ifdef USE_STACKCJMPBUF |
| 308 | SCM_SETJMPBUF (scm_rootcont, NULL); |
| 309 | #endif |
| 310 | #ifdef DEBUG_EXTENSIONS |
| 311 | scm_last_debug_frame = SCM_DFRAME (old_rootcont); |
| 312 | #endif |
| 313 | scm_rootcont = old_rootcont; |
| 314 | SCM_REALLOW_INTS; |
| 315 | scm_ints_disabled = old_ints_disabled; |
| 316 | |
| 317 | /* Now run the real handler iff the body did a throw. */ |
| 318 | if (my_handler_data.run_handler) |
| 319 | return handler (handler_data, my_handler_data.tag, my_handler_data.args); |
| 320 | else |
| 321 | return answer; |
| 322 | } |
| 323 | |
| 324 | /* The original CWDR for invoking Scheme code with a Scheme handler. */ |
| 325 | |
| 326 | static SCM |
| 327 | cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start) |
| 328 | { |
| 329 | struct cwdr_body_data c; |
| 330 | |
| 331 | c.a1 = a1; |
| 332 | c.args = args; |
| 333 | c.body_proc = proc; |
| 334 | |
| 335 | return scm_internal_cwdr (cwdr_body, &c, |
| 336 | scm_handle_by_proc, &handler, |
| 337 | stack_start); |
| 338 | } |
| 339 | |
| 340 | SCM_PROC(s_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0, scm_call_with_dynamic_root); |
| 341 | SCM |
| 342 | scm_call_with_dynamic_root (thunk, handler) |
| 343 | SCM thunk; |
| 344 | SCM handler; |
| 345 | { |
| 346 | SCM_STACKITEM stack_place; |
| 347 | |
| 348 | return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place); |
| 349 | } |
| 350 | |
| 351 | SCM_PROC(s_dynamic_root, "dynamic-root", 0, 0, 0, scm_dynamic_root); |
| 352 | SCM |
| 353 | scm_dynamic_root () |
| 354 | { |
| 355 | return scm_ulong2num (SCM_SEQ (scm_root->rootcont)); |
| 356 | } |
| 357 | |
| 358 | SCM |
| 359 | scm_apply_with_dynamic_root (proc, a1, args, handler) |
| 360 | SCM proc; |
| 361 | SCM a1; |
| 362 | SCM args; |
| 363 | SCM handler; |
| 364 | { |
| 365 | SCM_STACKITEM stack_place; |
| 366 | return cwdr (proc, a1, args, handler, &stack_place); |
| 367 | } |
| 368 | |
| 369 | \f |
| 370 | |
| 371 | /* Call thunk(closure) underneath a top-level error handler. |
| 372 | * If an error occurs, pass the exitval through err_filter and return it. |
| 373 | * If no error occurs, return the value of thunk. |
| 374 | */ |
| 375 | |
| 376 | |
| 377 | #ifdef _UNICOS |
| 378 | typedef int setjmp_type; |
| 379 | #else |
| 380 | typedef long setjmp_type; |
| 381 | #endif |
| 382 | |
| 383 | |
| 384 | |
| 385 | SCM |
| 386 | scm_call_catching_errors (thunk, err_filter, closure) |
| 387 | SCM (*thunk)(); |
| 388 | SCM (*err_filter)(); |
| 389 | void *closure; |
| 390 | { |
| 391 | SCM answer; |
| 392 | setjmp_type i; |
| 393 | #ifdef DEBUG_EXTENSIONS |
| 394 | SCM_DFRAME (scm_rootcont) = scm_last_debug_frame; |
| 395 | #endif |
| 396 | i = setjmp (SCM_JMPBUF (scm_rootcont)); |
| 397 | scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P; |
| 398 | if (!i) |
| 399 | { |
| 400 | scm_gc_heap_lock = 0; |
| 401 | answer = thunk (closure); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | scm_gc_heap_lock = 1; |
| 406 | answer = err_filter (scm_exitval, closure); |
| 407 | } |
| 408 | return answer; |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | scm_init_root () |
| 413 | { |
| 414 | scm_tc16_root = scm_newsmob (&root_smob); |
| 415 | #include "root.x" |
| 416 | } |