| 1 | /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc. |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public License |
| 5 | * as published by the Free Software Foundation; either version 3 of |
| 6 | * the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* For mremap(2) on GNU/Linux systems. */ |
| 20 | #define _GNU_SOURCE |
| 21 | |
| 22 | #if HAVE_CONFIG_H |
| 23 | # include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <alloca.h> |
| 28 | #include <alignof.h> |
| 29 | #include <string.h> |
| 30 | #include <stdint.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #ifdef HAVE_SYS_MMAN_H |
| 34 | #include <sys/mman.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "libguile/bdw-gc.h" |
| 38 | #include <gc/gc_mark.h> |
| 39 | |
| 40 | #include "_scm.h" |
| 41 | #include "control.h" |
| 42 | #include "frames.h" |
| 43 | #include "gc-inline.h" |
| 44 | #include "instructions.h" |
| 45 | #include "loader.h" |
| 46 | #include "programs.h" |
| 47 | #include "simpos.h" |
| 48 | #include "vm.h" |
| 49 | #include "vm-builtins.h" |
| 50 | |
| 51 | static int vm_default_engine = SCM_VM_REGULAR_ENGINE; |
| 52 | |
| 53 | /* Unfortunately we can't snarf these: snarfed things are only loaded up from |
| 54 | (system vm vm), which might not be loaded before an error happens. */ |
| 55 | static SCM sym_vm_run; |
| 56 | static SCM sym_vm_error; |
| 57 | static SCM sym_keyword_argument_error; |
| 58 | static SCM sym_regular; |
| 59 | static SCM sym_debug; |
| 60 | |
| 61 | /* The VM has a number of internal assertions that shouldn't normally be |
| 62 | necessary, but might be if you think you found a bug in the VM. */ |
| 63 | /* #define VM_ENABLE_ASSERTIONS */ |
| 64 | |
| 65 | static void vm_expand_stack (struct scm_vm *vp, SCM *new_sp) SCM_NOINLINE; |
| 66 | |
| 67 | /* RESTORE is for the case where we know we have done a PUSH of equal or |
| 68 | greater stack size in the past. Otherwise PUSH is the thing, which |
| 69 | may expand the stack. */ |
| 70 | enum vm_increase_sp_kind { VM_SP_PUSH, VM_SP_RESTORE }; |
| 71 | |
| 72 | static inline void |
| 73 | vm_increase_sp (struct scm_vm *vp, SCM *new_sp, enum vm_increase_sp_kind kind) |
| 74 | { |
| 75 | if (new_sp <= vp->sp_max_since_gc) |
| 76 | { |
| 77 | vp->sp = new_sp; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (kind == VM_SP_PUSH && new_sp >= vp->stack_limit) |
| 82 | vm_expand_stack (vp, new_sp); |
| 83 | else |
| 84 | vp->sp_max_since_gc = vp->sp = new_sp; |
| 85 | } |
| 86 | |
| 87 | static inline void |
| 88 | vm_push_sp (struct scm_vm *vp, SCM *new_sp) |
| 89 | { |
| 90 | vm_increase_sp (vp, new_sp, VM_SP_PUSH); |
| 91 | } |
| 92 | |
| 93 | static inline void |
| 94 | vm_restore_sp (struct scm_vm *vp, SCM *new_sp) |
| 95 | { |
| 96 | vm_increase_sp (vp, new_sp, VM_SP_RESTORE); |
| 97 | } |
| 98 | |
| 99 | \f |
| 100 | /* |
| 101 | * VM Continuation |
| 102 | */ |
| 103 | |
| 104 | void |
| 105 | scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate) |
| 106 | { |
| 107 | scm_puts_unlocked ("#<vm-continuation ", port); |
| 108 | scm_uintprint (SCM_UNPACK (x), 16, port); |
| 109 | scm_puts_unlocked (">", port); |
| 110 | } |
| 111 | |
| 112 | /* Ideally we could avoid copying the C stack if the continuation root |
| 113 | is inside VM code, and call/cc was invoked within that same call to |
| 114 | vm_run. That's currently not implemented. */ |
| 115 | SCM |
| 116 | scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint32 *ra, |
| 117 | scm_t_dynstack *dynstack, scm_t_uint32 flags) |
| 118 | { |
| 119 | struct scm_vm_cont *p; |
| 120 | |
| 121 | p = scm_gc_malloc (sizeof (*p), "capture_vm_cont"); |
| 122 | p->stack_size = sp - stack_base + 1; |
| 123 | p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM), |
| 124 | "capture_vm_cont"); |
| 125 | p->ra = ra; |
| 126 | p->sp = sp; |
| 127 | p->fp = fp; |
| 128 | memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM)); |
| 129 | p->reloc = p->stack_base - stack_base; |
| 130 | p->dynstack = dynstack; |
| 131 | p->flags = flags; |
| 132 | return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p); |
| 133 | } |
| 134 | |
| 135 | struct return_to_continuation_data |
| 136 | { |
| 137 | struct scm_vm_cont *cp; |
| 138 | struct scm_vm *vp; |
| 139 | }; |
| 140 | |
| 141 | /* Called with the GC lock to prevent the stack marker from traversing a |
| 142 | stack in an inconsistent state. */ |
| 143 | static void * |
| 144 | vm_return_to_continuation_inner (void *data_ptr) |
| 145 | { |
| 146 | struct return_to_continuation_data *data = data_ptr; |
| 147 | struct scm_vm *vp = data->vp; |
| 148 | struct scm_vm_cont *cp = data->cp; |
| 149 | scm_t_ptrdiff reloc; |
| 150 | |
| 151 | /* We know that there is enough space for the continuation, because we |
| 152 | captured it in the past. However there may have been an expansion |
| 153 | since the capture, so we may have to re-link the frame |
| 154 | pointers. */ |
| 155 | reloc = (vp->stack_base - (cp->stack_base - cp->reloc)); |
| 156 | vp->fp = cp->fp + reloc; |
| 157 | memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM)); |
| 158 | vm_restore_sp (vp, cp->sp + reloc); |
| 159 | |
| 160 | if (reloc) |
| 161 | { |
| 162 | SCM *fp = vp->fp; |
| 163 | while (fp) |
| 164 | { |
| 165 | SCM *next_fp = SCM_FRAME_DYNAMIC_LINK (fp); |
| 166 | if (next_fp) |
| 167 | { |
| 168 | next_fp += reloc; |
| 169 | SCM_FRAME_SET_DYNAMIC_LINK (fp, next_fp); |
| 170 | } |
| 171 | fp = next_fp; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | vm_return_to_continuation (struct scm_vm *vp, SCM cont, size_t n, SCM *argv) |
| 180 | { |
| 181 | struct scm_vm_cont *cp; |
| 182 | SCM *argv_copy; |
| 183 | struct return_to_continuation_data data; |
| 184 | |
| 185 | argv_copy = alloca (n * sizeof(SCM)); |
| 186 | memcpy (argv_copy, argv, n * sizeof(SCM)); |
| 187 | |
| 188 | cp = SCM_VM_CONT_DATA (cont); |
| 189 | |
| 190 | data.cp = cp; |
| 191 | data.vp = vp; |
| 192 | GC_call_with_alloc_lock (vm_return_to_continuation_inner, &data); |
| 193 | |
| 194 | /* Now we have the continuation properly copied over. We just need to |
| 195 | copy the arguments. It is not guaranteed that there is actually |
| 196 | space for the arguments, though, so we have to bump the SP first. */ |
| 197 | vm_push_sp (vp, vp->sp + 3 + n); |
| 198 | |
| 199 | /* Now copy on an empty frame and the return values, as the |
| 200 | continuation expects. */ |
| 201 | { |
| 202 | SCM *base = vp->sp + 1 - 3 - n; |
| 203 | size_t i; |
| 204 | |
| 205 | for (i = 0; i < 3; i++) |
| 206 | base[i] = SCM_BOOL_F; |
| 207 | |
| 208 | for (i = 0; i < n; i++) |
| 209 | base[i + 3] = argv_copy[i]; |
| 210 | } |
| 211 | |
| 212 | vp->ip = cp->ra; |
| 213 | } |
| 214 | |
| 215 | static struct scm_vm * thread_vm (scm_i_thread *t); |
| 216 | SCM |
| 217 | scm_i_capture_current_stack (void) |
| 218 | { |
| 219 | scm_i_thread *thread; |
| 220 | struct scm_vm *vp; |
| 221 | |
| 222 | thread = SCM_I_CURRENT_THREAD; |
| 223 | vp = thread_vm (thread); |
| 224 | |
| 225 | return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, |
| 226 | scm_dynstack_capture_all (&thread->dynstack), |
| 227 | 0); |
| 228 | } |
| 229 | |
| 230 | static void vm_dispatch_apply_hook (struct scm_vm *vp) SCM_NOINLINE; |
| 231 | static void vm_dispatch_push_continuation_hook (struct scm_vm *vp) SCM_NOINLINE; |
| 232 | static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp) SCM_NOINLINE; |
| 233 | static void vm_dispatch_next_hook (struct scm_vm *vp) SCM_NOINLINE; |
| 234 | static void vm_dispatch_abort_hook (struct scm_vm *vp) SCM_NOINLINE; |
| 235 | |
| 236 | static void |
| 237 | vm_dispatch_hook (struct scm_vm *vp, int hook_num, SCM *argv, int n) |
| 238 | { |
| 239 | SCM hook; |
| 240 | struct scm_frame c_frame; |
| 241 | scm_t_cell *frame; |
| 242 | int saved_trace_level; |
| 243 | |
| 244 | hook = vp->hooks[hook_num]; |
| 245 | |
| 246 | if (SCM_LIKELY (scm_is_false (hook)) |
| 247 | || scm_is_null (SCM_HOOK_PROCEDURES (hook))) |
| 248 | return; |
| 249 | |
| 250 | saved_trace_level = vp->trace_level; |
| 251 | vp->trace_level = 0; |
| 252 | |
| 253 | /* Allocate a frame object on the stack. This is more efficient than calling |
| 254 | `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not |
| 255 | capture frame objects. |
| 256 | |
| 257 | At the same time, procedures such as `frame-procedure' make sense only |
| 258 | while the stack frame represented by the frame object is visible, so it |
| 259 | seems reasonable to limit the lifetime of frame objects. */ |
| 260 | |
| 261 | c_frame.stack_holder = vp; |
| 262 | c_frame.fp_offset = vp->fp - vp->stack_base; |
| 263 | c_frame.sp_offset = vp->sp - vp->stack_base; |
| 264 | c_frame.ip = vp->ip; |
| 265 | |
| 266 | /* Arrange for FRAME to be 8-byte aligned, like any other cell. */ |
| 267 | frame = alloca (sizeof (*frame) + 8); |
| 268 | frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL); |
| 269 | |
| 270 | frame->word_0 = SCM_PACK (scm_tc7_frame | (SCM_VM_FRAME_KIND_VM << 8)); |
| 271 | frame->word_1 = SCM_PACK_POINTER (&c_frame); |
| 272 | |
| 273 | if (n == 0) |
| 274 | { |
| 275 | SCM args[1]; |
| 276 | |
| 277 | args[0] = SCM_PACK_POINTER (frame); |
| 278 | scm_c_run_hookn (hook, args, 1); |
| 279 | } |
| 280 | else if (n == 1) |
| 281 | { |
| 282 | SCM args[2]; |
| 283 | |
| 284 | args[0] = SCM_PACK_POINTER (frame); |
| 285 | args[1] = argv[0]; |
| 286 | scm_c_run_hookn (hook, args, 2); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | SCM args = SCM_EOL; |
| 291 | |
| 292 | while (n--) |
| 293 | args = scm_cons (argv[n], args); |
| 294 | scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args)); |
| 295 | } |
| 296 | |
| 297 | vp->trace_level = saved_trace_level; |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | vm_dispatch_apply_hook (struct scm_vm *vp) |
| 302 | { |
| 303 | return vm_dispatch_hook (vp, SCM_VM_APPLY_HOOK, NULL, 0); |
| 304 | } |
| 305 | static void vm_dispatch_push_continuation_hook (struct scm_vm *vp) |
| 306 | { |
| 307 | return vm_dispatch_hook (vp, SCM_VM_PUSH_CONTINUATION_HOOK, NULL, 0); |
| 308 | } |
| 309 | static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp) |
| 310 | { |
| 311 | return vm_dispatch_hook (vp, SCM_VM_POP_CONTINUATION_HOOK, |
| 312 | &SCM_FRAME_LOCAL (old_fp, 1), |
| 313 | SCM_FRAME_NUM_LOCALS (old_fp, vp->sp) - 1); |
| 314 | } |
| 315 | static void vm_dispatch_next_hook (struct scm_vm *vp) |
| 316 | { |
| 317 | return vm_dispatch_hook (vp, SCM_VM_NEXT_HOOK, NULL, 0); |
| 318 | } |
| 319 | static void vm_dispatch_abort_hook (struct scm_vm *vp) |
| 320 | { |
| 321 | return vm_dispatch_hook (vp, SCM_VM_ABORT_CONTINUATION_HOOK, |
| 322 | &SCM_FRAME_LOCAL (vp->fp, 1), |
| 323 | SCM_FRAME_NUM_LOCALS (vp->fp, vp->sp) - 1); |
| 324 | } |
| 325 | |
| 326 | static void |
| 327 | vm_abort (struct scm_vm *vp, SCM tag, |
| 328 | size_t nstack, SCM *stack_args, SCM tail, SCM *sp, |
| 329 | scm_i_jmp_buf *current_registers) SCM_NORETURN; |
| 330 | |
| 331 | static void |
| 332 | vm_abort (struct scm_vm *vp, SCM tag, |
| 333 | size_t nstack, SCM *stack_args, SCM tail, SCM *sp, |
| 334 | scm_i_jmp_buf *current_registers) |
| 335 | { |
| 336 | size_t i; |
| 337 | ssize_t tail_len; |
| 338 | SCM *argv; |
| 339 | |
| 340 | tail_len = scm_ilength (tail); |
| 341 | if (tail_len < 0) |
| 342 | scm_misc_error ("vm-engine", "tail values to abort should be a list", |
| 343 | scm_list_1 (tail)); |
| 344 | |
| 345 | argv = alloca ((nstack + tail_len) * sizeof (SCM)); |
| 346 | for (i = 0; i < nstack; i++) |
| 347 | argv[i] = stack_args[i]; |
| 348 | for (; i < nstack + tail_len; i++, tail = scm_cdr (tail)) |
| 349 | argv[i] = scm_car (tail); |
| 350 | |
| 351 | vp->sp = sp; |
| 352 | |
| 353 | scm_c_abort (vp, tag, nstack + tail_len, argv, current_registers); |
| 354 | } |
| 355 | |
| 356 | struct vm_reinstate_partial_continuation_data |
| 357 | { |
| 358 | struct scm_vm *vp; |
| 359 | struct scm_vm_cont *cp; |
| 360 | scm_t_ptrdiff reloc; |
| 361 | }; |
| 362 | |
| 363 | static void * |
| 364 | vm_reinstate_partial_continuation_inner (void *data_ptr) |
| 365 | { |
| 366 | struct vm_reinstate_partial_continuation_data *data = data_ptr; |
| 367 | struct scm_vm *vp = data->vp; |
| 368 | struct scm_vm_cont *cp = data->cp; |
| 369 | SCM *base; |
| 370 | scm_t_ptrdiff reloc; |
| 371 | |
| 372 | base = SCM_FRAME_LOCALS_ADDRESS (vp->fp); |
| 373 | reloc = cp->reloc + (base - cp->stack_base); |
| 374 | |
| 375 | memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM)); |
| 376 | |
| 377 | vp->fp = cp->fp + reloc; |
| 378 | vp->ip = cp->ra; |
| 379 | |
| 380 | /* now relocate frame pointers */ |
| 381 | { |
| 382 | SCM *fp; |
| 383 | for (fp = vp->fp; |
| 384 | SCM_FRAME_LOWER_ADDRESS (fp) > base; |
| 385 | fp = SCM_FRAME_DYNAMIC_LINK (fp)) |
| 386 | SCM_FRAME_SET_DYNAMIC_LINK (fp, SCM_FRAME_DYNAMIC_LINK (fp) + reloc); |
| 387 | } |
| 388 | |
| 389 | data->reloc = reloc; |
| 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | static void |
| 395 | vm_reinstate_partial_continuation (struct scm_vm *vp, SCM cont, |
| 396 | size_t n, SCM *argv, |
| 397 | scm_t_dynstack *dynstack, |
| 398 | scm_i_jmp_buf *registers) |
| 399 | { |
| 400 | struct vm_reinstate_partial_continuation_data data; |
| 401 | struct scm_vm_cont *cp; |
| 402 | SCM *argv_copy; |
| 403 | scm_t_ptrdiff reloc; |
| 404 | size_t i; |
| 405 | |
| 406 | argv_copy = alloca (n * sizeof(SCM)); |
| 407 | memcpy (argv_copy, argv, n * sizeof(SCM)); |
| 408 | |
| 409 | cp = SCM_VM_CONT_DATA (cont); |
| 410 | |
| 411 | vm_push_sp (vp, SCM_FRAME_LOCALS_ADDRESS (vp->fp) + cp->stack_size + n - 1); |
| 412 | |
| 413 | data.vp = vp; |
| 414 | data.cp = cp; |
| 415 | GC_call_with_alloc_lock (vm_reinstate_partial_continuation_inner, &data); |
| 416 | reloc = data.reloc; |
| 417 | |
| 418 | /* Push the arguments. */ |
| 419 | for (i = 0; i < n; i++) |
| 420 | vp->sp[i + 1 - n] = argv_copy[i]; |
| 421 | |
| 422 | /* The prompt captured a slice of the dynamic stack. Here we wind |
| 423 | those entries onto the current thread's stack. We also have to |
| 424 | relocate any prompts that we see along the way. */ |
| 425 | { |
| 426 | scm_t_bits *walk; |
| 427 | |
| 428 | for (walk = SCM_DYNSTACK_FIRST (cp->dynstack); |
| 429 | SCM_DYNSTACK_TAG (walk); |
| 430 | walk = SCM_DYNSTACK_NEXT (walk)) |
| 431 | { |
| 432 | scm_t_bits tag = SCM_DYNSTACK_TAG (walk); |
| 433 | |
| 434 | if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT) |
| 435 | scm_dynstack_wind_prompt (dynstack, walk, reloc, registers); |
| 436 | else |
| 437 | scm_dynstack_wind_1 (dynstack, walk); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | \f |
| 443 | /* |
| 444 | * VM Error Handling |
| 445 | */ |
| 446 | |
| 447 | static void vm_error (const char *msg, SCM arg) SCM_NORETURN; |
| 448 | static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE; |
| 449 | static void vm_error_unbound (SCM proc, SCM sym) SCM_NORETURN SCM_NOINLINE; |
| 450 | static void vm_error_unbound_fluid (SCM proc, SCM fluid) SCM_NORETURN SCM_NOINLINE; |
| 451 | static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE; |
| 452 | static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE; |
| 453 | static void vm_error_kwargs_length_not_even (SCM proc) SCM_NORETURN SCM_NOINLINE; |
| 454 | static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE; |
| 455 | static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE; |
| 456 | static void vm_error_too_many_args (int nargs) SCM_NORETURN SCM_NOINLINE; |
| 457 | static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE; |
| 458 | static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE; |
| 459 | static void vm_error_stack_underflow (void) SCM_NORETURN SCM_NOINLINE; |
| 460 | static void vm_error_improper_list (SCM x) SCM_NORETURN SCM_NOINLINE; |
| 461 | static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE; |
| 462 | static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE; |
| 463 | static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE; |
| 464 | static void vm_error_not_a_vector (const char *subr, SCM v) SCM_NORETURN SCM_NOINLINE; |
| 465 | static void vm_error_out_of_range (const char *subr, SCM k) SCM_NORETURN SCM_NOINLINE; |
| 466 | static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE; |
| 467 | static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE; |
| 468 | static void vm_error_wrong_number_of_values (scm_t_uint32 expected) SCM_NORETURN SCM_NOINLINE; |
| 469 | static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE; |
| 470 | static void vm_error_bad_wide_string_length (size_t len) SCM_NORETURN SCM_NOINLINE; |
| 471 | |
| 472 | static void |
| 473 | vm_error (const char *msg, SCM arg) |
| 474 | { |
| 475 | scm_throw (sym_vm_error, |
| 476 | scm_list_3 (sym_vm_run, scm_from_latin1_string (msg), |
| 477 | SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg))); |
| 478 | abort(); /* not reached */ |
| 479 | } |
| 480 | |
| 481 | static void |
| 482 | vm_error_bad_instruction (scm_t_uint32 inst) |
| 483 | { |
| 484 | vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst)); |
| 485 | } |
| 486 | |
| 487 | static void |
| 488 | vm_error_unbound (SCM proc, SCM sym) |
| 489 | { |
| 490 | scm_error_scm (scm_misc_error_key, proc, |
| 491 | scm_from_latin1_string ("Unbound variable: ~s"), |
| 492 | scm_list_1 (sym), SCM_BOOL_F); |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | vm_error_unbound_fluid (SCM proc, SCM fluid) |
| 497 | { |
| 498 | scm_error_scm (scm_misc_error_key, proc, |
| 499 | scm_from_latin1_string ("Unbound fluid: ~s"), |
| 500 | scm_list_1 (fluid), SCM_BOOL_F); |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | vm_error_not_a_variable (const char *func_name, SCM x) |
| 505 | { |
| 506 | scm_error (scm_arg_type_key, func_name, "Not a variable: ~S", |
| 507 | scm_list_1 (x), scm_list_1 (x)); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | vm_error_apply_to_non_list (SCM x) |
| 512 | { |
| 513 | scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S", |
| 514 | scm_list_1 (x), scm_list_1 (x)); |
| 515 | } |
| 516 | |
| 517 | static void |
| 518 | vm_error_kwargs_length_not_even (SCM proc) |
| 519 | { |
| 520 | scm_error_scm (sym_keyword_argument_error, proc, |
| 521 | scm_from_latin1_string ("Odd length of keyword argument list"), |
| 522 | SCM_EOL, SCM_BOOL_F); |
| 523 | } |
| 524 | |
| 525 | static void |
| 526 | vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) |
| 527 | { |
| 528 | scm_error_scm (sym_keyword_argument_error, proc, |
| 529 | scm_from_latin1_string ("Invalid keyword"), |
| 530 | SCM_EOL, scm_list_1 (obj)); |
| 531 | } |
| 532 | |
| 533 | static void |
| 534 | vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) |
| 535 | { |
| 536 | scm_error_scm (sym_keyword_argument_error, proc, |
| 537 | scm_from_latin1_string ("Unrecognized keyword"), |
| 538 | SCM_EOL, scm_list_1 (kw)); |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | vm_error_too_many_args (int nargs) |
| 543 | { |
| 544 | vm_error ("VM: Too many arguments", scm_from_int (nargs)); |
| 545 | } |
| 546 | |
| 547 | static void |
| 548 | vm_error_wrong_num_args (SCM proc) |
| 549 | { |
| 550 | scm_wrong_num_args (proc); |
| 551 | } |
| 552 | |
| 553 | static void |
| 554 | vm_error_wrong_type_apply (SCM proc) |
| 555 | { |
| 556 | scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S", |
| 557 | scm_list_1 (proc), scm_list_1 (proc)); |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | vm_error_stack_underflow (void) |
| 562 | { |
| 563 | vm_error ("VM: Stack underflow", SCM_UNDEFINED); |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | vm_error_improper_list (SCM x) |
| 568 | { |
| 569 | vm_error ("Expected a proper list, but got object with tail ~s", x); |
| 570 | } |
| 571 | |
| 572 | static void |
| 573 | vm_error_not_a_pair (const char *subr, SCM x) |
| 574 | { |
| 575 | scm_wrong_type_arg_msg (subr, 1, x, "pair"); |
| 576 | } |
| 577 | |
| 578 | static void |
| 579 | vm_error_not_a_bytevector (const char *subr, SCM x) |
| 580 | { |
| 581 | scm_wrong_type_arg_msg (subr, 1, x, "bytevector"); |
| 582 | } |
| 583 | |
| 584 | static void |
| 585 | vm_error_not_a_struct (const char *subr, SCM x) |
| 586 | { |
| 587 | scm_wrong_type_arg_msg (subr, 1, x, "struct"); |
| 588 | } |
| 589 | |
| 590 | static void |
| 591 | vm_error_not_a_vector (const char *subr, SCM x) |
| 592 | { |
| 593 | scm_wrong_type_arg_msg (subr, 1, x, "vector"); |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | vm_error_out_of_range (const char *subr, SCM k) |
| 598 | { |
| 599 | scm_to_size_t (k); |
| 600 | scm_out_of_range (subr, k); |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | vm_error_no_values (void) |
| 605 | { |
| 606 | vm_error ("Zero values returned to single-valued continuation", |
| 607 | SCM_UNDEFINED); |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | vm_error_not_enough_values (void) |
| 612 | { |
| 613 | vm_error ("Too few values returned to continuation", SCM_UNDEFINED); |
| 614 | } |
| 615 | |
| 616 | static void |
| 617 | vm_error_wrong_number_of_values (scm_t_uint32 expected) |
| 618 | { |
| 619 | vm_error ("Wrong number of values returned to continuation (expected ~a)", |
| 620 | scm_from_uint32 (expected)); |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | vm_error_continuation_not_rewindable (SCM cont) |
| 625 | { |
| 626 | vm_error ("Unrewindable partial continuation", cont); |
| 627 | } |
| 628 | |
| 629 | static void |
| 630 | vm_error_bad_wide_string_length (size_t len) |
| 631 | { |
| 632 | vm_error ("VM: Bad wide string length: ~S", scm_from_size_t (len)); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | \f |
| 637 | |
| 638 | static SCM vm_boot_continuation; |
| 639 | static SCM vm_builtin_apply; |
| 640 | static SCM vm_builtin_values; |
| 641 | static SCM vm_builtin_abort_to_prompt; |
| 642 | static SCM vm_builtin_call_with_values; |
| 643 | static SCM vm_builtin_call_with_current_continuation; |
| 644 | |
| 645 | static const scm_t_uint32 vm_boot_continuation_code[] = { |
| 646 | SCM_PACK_OP_24 (halt, 0) |
| 647 | }; |
| 648 | |
| 649 | static const scm_t_uint32 vm_builtin_apply_code[] = { |
| 650 | SCM_PACK_OP_24 (assert_nargs_ge, 3), |
| 651 | SCM_PACK_OP_24 (tail_apply, 0), /* proc in r1, args from r2 */ |
| 652 | }; |
| 653 | |
| 654 | static const scm_t_uint32 vm_builtin_values_code[] = { |
| 655 | SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */ |
| 656 | }; |
| 657 | |
| 658 | static const scm_t_uint32 vm_builtin_abort_to_prompt_code[] = { |
| 659 | SCM_PACK_OP_24 (assert_nargs_ge, 2), |
| 660 | SCM_PACK_OP_24 (abort, 0), /* tag in r1, vals from r2 */ |
| 661 | /* FIXME: Partial continuation should capture caller regs. */ |
| 662 | SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */ |
| 663 | }; |
| 664 | |
| 665 | static const scm_t_uint32 vm_builtin_call_with_values_code[] = { |
| 666 | SCM_PACK_OP_24 (assert_nargs_ee, 3), |
| 667 | SCM_PACK_OP_24 (alloc_frame, 7), |
| 668 | SCM_PACK_OP_12_12 (mov, 6, 1), |
| 669 | SCM_PACK_OP_24 (call, 6), SCM_PACK_OP_ARG_8_24 (0, 1), |
| 670 | SCM_PACK_OP_12_12 (mov, 0, 2), |
| 671 | SCM_PACK_OP_24 (tail_call_shuffle, 7) |
| 672 | }; |
| 673 | |
| 674 | static const scm_t_uint32 vm_builtin_call_with_current_continuation_code[] = { |
| 675 | SCM_PACK_OP_24 (assert_nargs_ee, 2), |
| 676 | SCM_PACK_OP_24 (call_cc, 0) |
| 677 | }; |
| 678 | |
| 679 | |
| 680 | static SCM |
| 681 | scm_vm_builtin_ref (unsigned idx) |
| 682 | { |
| 683 | switch (idx) |
| 684 | { |
| 685 | #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \ |
| 686 | case SCM_VM_BUILTIN_##BUILTIN: return vm_builtin_##builtin; |
| 687 | FOR_EACH_VM_BUILTIN(INDEX_TO_NAME) |
| 688 | #undef INDEX_TO_NAME |
| 689 | default: abort(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | SCM scm_sym_apply; |
| 694 | static SCM scm_sym_values; |
| 695 | static SCM scm_sym_abort_to_prompt; |
| 696 | static SCM scm_sym_call_with_values; |
| 697 | static SCM scm_sym_call_with_current_continuation; |
| 698 | |
| 699 | SCM |
| 700 | scm_vm_builtin_name_to_index (SCM name) |
| 701 | #define FUNC_NAME "builtin-name->index" |
| 702 | { |
| 703 | SCM_VALIDATE_SYMBOL (1, name); |
| 704 | |
| 705 | #define NAME_TO_INDEX(builtin, BUILTIN, req, opt, rest) \ |
| 706 | if (scm_is_eq (name, scm_sym_##builtin)) \ |
| 707 | return scm_from_uint (SCM_VM_BUILTIN_##BUILTIN); |
| 708 | FOR_EACH_VM_BUILTIN(NAME_TO_INDEX) |
| 709 | #undef NAME_TO_INDEX |
| 710 | |
| 711 | return SCM_BOOL_F; |
| 712 | } |
| 713 | #undef FUNC_NAME |
| 714 | |
| 715 | SCM |
| 716 | scm_vm_builtin_index_to_name (SCM index) |
| 717 | #define FUNC_NAME "builtin-index->name" |
| 718 | { |
| 719 | unsigned idx; |
| 720 | |
| 721 | SCM_VALIDATE_UINT_COPY (1, index, idx); |
| 722 | |
| 723 | switch (idx) |
| 724 | { |
| 725 | #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \ |
| 726 | case SCM_VM_BUILTIN_##BUILTIN: return scm_sym_##builtin; |
| 727 | FOR_EACH_VM_BUILTIN(INDEX_TO_NAME) |
| 728 | #undef INDEX_TO_NAME |
| 729 | default: return SCM_BOOL_F; |
| 730 | } |
| 731 | } |
| 732 | #undef FUNC_NAME |
| 733 | |
| 734 | static void |
| 735 | scm_init_vm_builtins (void) |
| 736 | { |
| 737 | scm_c_define_gsubr ("builtin-name->index", 1, 0, 0, |
| 738 | scm_vm_builtin_name_to_index); |
| 739 | scm_c_define_gsubr ("builtin-index->name", 1, 0, 0, |
| 740 | scm_vm_builtin_index_to_name); |
| 741 | } |
| 742 | |
| 743 | SCM |
| 744 | scm_i_call_with_current_continuation (SCM proc) |
| 745 | { |
| 746 | return scm_call_1 (vm_builtin_call_with_current_continuation, proc); |
| 747 | } |
| 748 | |
| 749 | \f |
| 750 | /* |
| 751 | * VM |
| 752 | */ |
| 753 | |
| 754 | /* The page size. */ |
| 755 | static size_t page_size; |
| 756 | |
| 757 | /* Initial stack size. Defaults to one page. */ |
| 758 | static size_t initial_stack_size; |
| 759 | |
| 760 | /* Default soft stack limit is 1M words (4 or 8 megabytes). */ |
| 761 | static size_t default_max_stack_size = 1024 * 1024; |
| 762 | |
| 763 | static void |
| 764 | initialize_default_stack_size (void) |
| 765 | { |
| 766 | initial_stack_size = page_size / sizeof (SCM); |
| 767 | |
| 768 | { |
| 769 | int size; |
| 770 | size = scm_getenv_int ("GUILE_STACK_SIZE", (int) default_max_stack_size); |
| 771 | if (size >= initial_stack_size |
| 772 | && (size_t) size < ((size_t) -1) / sizeof(SCM)) |
| 773 | default_max_stack_size = size; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | #define VM_NAME vm_regular_engine |
| 778 | #define VM_USE_HOOKS 0 |
| 779 | #define FUNC_NAME "vm-regular-engine" |
| 780 | #include "vm-engine.c" |
| 781 | #undef FUNC_NAME |
| 782 | #undef VM_USE_HOOKS |
| 783 | #undef VM_NAME |
| 784 | |
| 785 | #define VM_NAME vm_debug_engine |
| 786 | #define VM_USE_HOOKS 1 |
| 787 | #define FUNC_NAME "vm-debug-engine" |
| 788 | #include "vm-engine.c" |
| 789 | #undef FUNC_NAME |
| 790 | #undef VM_USE_HOOKS |
| 791 | #undef VM_NAME |
| 792 | |
| 793 | typedef SCM (*scm_t_vm_engine) (scm_i_thread *current_thread, struct scm_vm *vp, |
| 794 | scm_i_jmp_buf *registers, int resume); |
| 795 | |
| 796 | static const scm_t_vm_engine vm_engines[SCM_VM_NUM_ENGINES] = |
| 797 | { vm_regular_engine, vm_debug_engine }; |
| 798 | |
| 799 | static SCM* |
| 800 | allocate_stack (size_t size) |
| 801 | #define FUNC_NAME "make_vm" |
| 802 | { |
| 803 | void *ret; |
| 804 | |
| 805 | if (size >= ((size_t) -1) / sizeof (SCM)) |
| 806 | abort (); |
| 807 | |
| 808 | size *= sizeof (SCM); |
| 809 | |
| 810 | #if HAVE_SYS_MMAN_H |
| 811 | ret = mmap (NULL, size, PROT_READ | PROT_WRITE, |
| 812 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 813 | if (ret == MAP_FAILED) |
| 814 | ret = NULL; |
| 815 | #else |
| 816 | ret = malloc (size); |
| 817 | #endif |
| 818 | |
| 819 | if (!ret) |
| 820 | { |
| 821 | perror ("allocate_stack failed"); |
| 822 | return NULL; |
| 823 | } |
| 824 | |
| 825 | return (SCM *) ret; |
| 826 | } |
| 827 | #undef FUNC_NAME |
| 828 | |
| 829 | static void |
| 830 | free_stack (SCM *stack, size_t size) |
| 831 | { |
| 832 | size *= sizeof (SCM); |
| 833 | |
| 834 | #if HAVE_SYS_MMAN_H |
| 835 | munmap (stack, size); |
| 836 | #else |
| 837 | free (stack); |
| 838 | #endif |
| 839 | } |
| 840 | |
| 841 | static SCM* |
| 842 | expand_stack (SCM *old_stack, size_t old_size, size_t new_size) |
| 843 | #define FUNC_NAME "expand_stack" |
| 844 | { |
| 845 | #if defined MREMAP_MAYMOVE |
| 846 | void *new_stack; |
| 847 | |
| 848 | if (new_size >= ((size_t) -1) / sizeof (SCM)) |
| 849 | abort (); |
| 850 | |
| 851 | old_size *= sizeof (SCM); |
| 852 | new_size *= sizeof (SCM); |
| 853 | |
| 854 | new_stack = mremap (old_stack, old_size, new_size, MREMAP_MAYMOVE); |
| 855 | if (new_stack == MAP_FAILED) |
| 856 | return NULL; |
| 857 | |
| 858 | return (SCM *) new_stack; |
| 859 | #else |
| 860 | SCM *new_stack; |
| 861 | |
| 862 | new_stack = allocate_stack (new_size); |
| 863 | if (!new_stack) |
| 864 | return NULL; |
| 865 | |
| 866 | memcpy (new_stack, old_stack, old_size * sizeof (SCM)); |
| 867 | free_stack (old_stack, old_size); |
| 868 | |
| 869 | return new_stack; |
| 870 | #endif |
| 871 | } |
| 872 | #undef FUNC_NAME |
| 873 | |
| 874 | static struct scm_vm * |
| 875 | make_vm (void) |
| 876 | #define FUNC_NAME "make_vm" |
| 877 | { |
| 878 | int i; |
| 879 | struct scm_vm *vp; |
| 880 | |
| 881 | vp = scm_gc_malloc (sizeof (struct scm_vm), "vm"); |
| 882 | |
| 883 | vp->stack_size = initial_stack_size; |
| 884 | vp->stack_base = allocate_stack (vp->stack_size); |
| 885 | if (!vp->stack_base) |
| 886 | /* As in expand_stack, we don't have any way to throw an exception |
| 887 | if we can't allocate one measely page -- there's no stack to |
| 888 | handle it. For now, abort. */ |
| 889 | abort (); |
| 890 | vp->stack_limit = vp->stack_base + vp->stack_size; |
| 891 | vp->max_stack_size = default_max_stack_size; |
| 892 | vp->ip = NULL; |
| 893 | vp->sp = vp->stack_base - 1; |
| 894 | vp->fp = NULL; |
| 895 | vp->engine = vm_default_engine; |
| 896 | vp->trace_level = 0; |
| 897 | for (i = 0; i < SCM_VM_NUM_HOOKS; i++) |
| 898 | vp->hooks[i] = SCM_BOOL_F; |
| 899 | |
| 900 | return vp; |
| 901 | } |
| 902 | #undef FUNC_NAME |
| 903 | |
| 904 | static void |
| 905 | return_unused_stack_to_os (struct scm_vm *vp) |
| 906 | { |
| 907 | #if HAVE_SYS_MMAN_H |
| 908 | scm_t_uintptr start = (scm_t_uintptr) (vp->sp + 1); |
| 909 | scm_t_uintptr end = (scm_t_uintptr) vp->stack_limit; |
| 910 | /* The second condition is needed to protect against wrap-around. */ |
| 911 | if (vp->sp_max_since_gc < vp->stack_limit && vp->sp < vp->sp_max_since_gc) |
| 912 | end = (scm_t_uintptr) (vp->sp_max_since_gc + 1); |
| 913 | |
| 914 | start = ((start - 1U) | (page_size - 1U)) + 1U; /* round up */ |
| 915 | end = ((end - 1U) | (page_size - 1U)) + 1U; /* round up */ |
| 916 | |
| 917 | /* Return these pages to the OS. The next time they are paged in, |
| 918 | they will be zeroed. */ |
| 919 | if (start < end) |
| 920 | { |
| 921 | int ret = 0; |
| 922 | |
| 923 | do |
| 924 | ret = madvise ((void *) start, end - start, MADV_DONTNEED); |
| 925 | while (ret && errno == -EAGAIN); |
| 926 | |
| 927 | if (ret) |
| 928 | perror ("madvise failed"); |
| 929 | } |
| 930 | |
| 931 | vp->sp_max_since_gc = vp->sp; |
| 932 | #endif |
| 933 | } |
| 934 | |
| 935 | #define DEAD_SLOT_MAP_CACHE_SIZE 32U |
| 936 | struct dead_slot_map_cache_entry |
| 937 | { |
| 938 | scm_t_uint32 *ip; |
| 939 | const scm_t_uint8 *map; |
| 940 | }; |
| 941 | |
| 942 | struct dead_slot_map_cache |
| 943 | { |
| 944 | struct dead_slot_map_cache_entry entries[DEAD_SLOT_MAP_CACHE_SIZE]; |
| 945 | }; |
| 946 | |
| 947 | static const scm_t_uint8 * |
| 948 | find_dead_slot_map (scm_t_uint32 *ip, struct dead_slot_map_cache *cache) |
| 949 | { |
| 950 | /* The lower two bits should be zero. FIXME: Use a better hash |
| 951 | function; we don't expose scm_raw_hashq currently. */ |
| 952 | size_t slot = (((scm_t_uintptr) ip) >> 2) % DEAD_SLOT_MAP_CACHE_SIZE; |
| 953 | const scm_t_uint8 *map; |
| 954 | |
| 955 | if (cache->entries[slot].ip == ip) |
| 956 | map = cache->entries[slot].map; |
| 957 | else |
| 958 | { |
| 959 | map = scm_find_dead_slot_map_unlocked (ip); |
| 960 | cache->entries[slot].ip = ip; |
| 961 | cache->entries[slot].map = map; |
| 962 | } |
| 963 | |
| 964 | return map; |
| 965 | } |
| 966 | |
| 967 | /* Mark the VM stack region between its base and its current top. */ |
| 968 | struct GC_ms_entry * |
| 969 | scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr, |
| 970 | struct GC_ms_entry *mark_stack_limit) |
| 971 | { |
| 972 | SCM *sp, *fp; |
| 973 | /* The first frame will be marked conservatively (without a dead |
| 974 | slot map). This is because GC can happen at any point within the |
| 975 | hottest activation, due to multiple threads or per-instruction |
| 976 | hooks, and providing dead slot maps for all points in a program |
| 977 | would take a prohibitive amount of space. */ |
| 978 | const scm_t_uint8 *dead_slots = NULL; |
| 979 | scm_t_uintptr upper = (scm_t_uintptr) GC_greatest_plausible_heap_addr; |
| 980 | scm_t_uintptr lower = (scm_t_uintptr) GC_least_plausible_heap_addr; |
| 981 | struct dead_slot_map_cache cache; |
| 982 | |
| 983 | memset (&cache, 0, sizeof (cache)); |
| 984 | |
| 985 | for (fp = vp->fp, sp = vp->sp; fp; fp = SCM_FRAME_DYNAMIC_LINK (fp)) |
| 986 | { |
| 987 | for (; sp >= &SCM_FRAME_LOCAL (fp, 0); sp--) |
| 988 | { |
| 989 | SCM elt = *sp; |
| 990 | if (SCM_NIMP (elt) |
| 991 | && SCM_UNPACK (elt) >= lower && SCM_UNPACK (elt) <= upper) |
| 992 | { |
| 993 | if (dead_slots) |
| 994 | { |
| 995 | size_t slot = sp - &SCM_FRAME_LOCAL (fp, 0); |
| 996 | if (dead_slots[slot / 8U] & (1U << (slot % 8U))) |
| 997 | { |
| 998 | /* This value may become dead as a result of GC, |
| 999 | so we can't just leave it on the stack. */ |
| 1000 | *sp = SCM_UNBOUND; |
| 1001 | continue; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | mark_stack_ptr = GC_mark_and_push ((void *) elt, |
| 1006 | mark_stack_ptr, |
| 1007 | mark_stack_limit, |
| 1008 | NULL); |
| 1009 | } |
| 1010 | } |
| 1011 | sp = SCM_FRAME_PREVIOUS_SP (fp); |
| 1012 | /* Inner frames may have a dead slots map for precise marking. |
| 1013 | Note that there may be other reasons to not have a dead slots |
| 1014 | map, e.g. if all of the frame's slots below the callee frame |
| 1015 | are live. */ |
| 1016 | dead_slots = find_dead_slot_map (SCM_FRAME_RETURN_ADDRESS (fp), &cache); |
| 1017 | } |
| 1018 | |
| 1019 | return_unused_stack_to_os (vp); |
| 1020 | |
| 1021 | return mark_stack_ptr; |
| 1022 | } |
| 1023 | |
| 1024 | /* Free the VM stack, as this thread is exiting. */ |
| 1025 | void |
| 1026 | scm_i_vm_free_stack (struct scm_vm *vp) |
| 1027 | { |
| 1028 | free_stack (vp->stack_base, vp->stack_size); |
| 1029 | vp->stack_base = vp->stack_limit = NULL; |
| 1030 | vp->stack_size = 0; |
| 1031 | } |
| 1032 | |
| 1033 | struct vm_expand_stack_data |
| 1034 | { |
| 1035 | struct scm_vm *vp; |
| 1036 | size_t stack_size; |
| 1037 | SCM *new_sp; |
| 1038 | }; |
| 1039 | |
| 1040 | static void * |
| 1041 | vm_expand_stack_inner (void *data_ptr) |
| 1042 | { |
| 1043 | struct vm_expand_stack_data *data = data_ptr; |
| 1044 | |
| 1045 | struct scm_vm *vp = data->vp; |
| 1046 | SCM *old_stack, *new_stack; |
| 1047 | size_t new_size; |
| 1048 | scm_t_ptrdiff reloc; |
| 1049 | |
| 1050 | new_size = vp->stack_size; |
| 1051 | while (new_size < data->stack_size) |
| 1052 | new_size *= 2; |
| 1053 | old_stack = vp->stack_base; |
| 1054 | |
| 1055 | new_stack = expand_stack (vp->stack_base, vp->stack_size, new_size); |
| 1056 | if (!new_stack) |
| 1057 | return NULL; |
| 1058 | |
| 1059 | vp->stack_base = new_stack; |
| 1060 | vp->stack_size = new_size; |
| 1061 | vp->stack_limit = vp->stack_base + new_size; |
| 1062 | reloc = vp->stack_base - old_stack; |
| 1063 | |
| 1064 | if (reloc) |
| 1065 | { |
| 1066 | SCM *fp; |
| 1067 | if (vp->fp) |
| 1068 | vp->fp += reloc; |
| 1069 | data->new_sp += reloc; |
| 1070 | fp = vp->fp; |
| 1071 | while (fp) |
| 1072 | { |
| 1073 | SCM *next_fp = SCM_FRAME_DYNAMIC_LINK (fp); |
| 1074 | if (next_fp) |
| 1075 | { |
| 1076 | next_fp += reloc; |
| 1077 | SCM_FRAME_SET_DYNAMIC_LINK (fp, next_fp); |
| 1078 | } |
| 1079 | fp = next_fp; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return new_stack; |
| 1084 | } |
| 1085 | |
| 1086 | static void |
| 1087 | vm_expand_stack (struct scm_vm *vp, SCM *new_sp) |
| 1088 | { |
| 1089 | scm_t_ptrdiff stack_size = new_sp + 1 - vp->stack_base; |
| 1090 | |
| 1091 | if (stack_size > vp->stack_size) |
| 1092 | { |
| 1093 | struct vm_expand_stack_data data; |
| 1094 | |
| 1095 | data.vp = vp; |
| 1096 | data.stack_size = stack_size; |
| 1097 | data.new_sp = new_sp; |
| 1098 | |
| 1099 | if (!GC_call_with_alloc_lock (vm_expand_stack_inner, &data)) |
| 1100 | scm_report_stack_overflow (); |
| 1101 | |
| 1102 | new_sp = data.new_sp; |
| 1103 | } |
| 1104 | |
| 1105 | vp->sp_max_since_gc = vp->sp = new_sp; |
| 1106 | |
| 1107 | if (stack_size >= vp->max_stack_size) |
| 1108 | { |
| 1109 | /* Expand the soft limit by 256K entries to give us space to |
| 1110 | handle the error. */ |
| 1111 | vp->max_stack_size += 256 * 1024; |
| 1112 | |
| 1113 | /* If it's still not big enough... it's quite improbable, but go |
| 1114 | ahead and set to the full available stack size. */ |
| 1115 | if (vp->max_stack_size < stack_size) |
| 1116 | vp->max_stack_size = vp->stack_size; |
| 1117 | |
| 1118 | /* Finally, reset the limit, to catch further overflows. */ |
| 1119 | vp->stack_limit = vp->stack_base + vp->max_stack_size; |
| 1120 | |
| 1121 | /* FIXME: Use scm_report_stack_overflow, but in a mode that allows |
| 1122 | pre-unwind handlers to run. */ |
| 1123 | vm_error ("VM: Stack overflow", SCM_UNDEFINED); |
| 1124 | } |
| 1125 | |
| 1126 | /* Otherwise continue, with the new enlarged stack. */ |
| 1127 | } |
| 1128 | |
| 1129 | static struct scm_vm * |
| 1130 | thread_vm (scm_i_thread *t) |
| 1131 | { |
| 1132 | if (SCM_UNLIKELY (!t->vp)) |
| 1133 | t->vp = make_vm (); |
| 1134 | |
| 1135 | return t->vp; |
| 1136 | } |
| 1137 | |
| 1138 | struct scm_vm * |
| 1139 | scm_the_vm (void) |
| 1140 | { |
| 1141 | return thread_vm (SCM_I_CURRENT_THREAD); |
| 1142 | } |
| 1143 | |
| 1144 | SCM |
| 1145 | scm_call_n (SCM proc, SCM *argv, size_t nargs) |
| 1146 | { |
| 1147 | scm_i_thread *thread; |
| 1148 | struct scm_vm *vp; |
| 1149 | SCM *base; |
| 1150 | ptrdiff_t base_frame_size; |
| 1151 | /* Cached variables. */ |
| 1152 | scm_i_jmp_buf registers; /* used for prompts */ |
| 1153 | size_t i; |
| 1154 | |
| 1155 | thread = SCM_I_CURRENT_THREAD; |
| 1156 | vp = thread_vm (thread); |
| 1157 | |
| 1158 | SCM_CHECK_STACK; |
| 1159 | |
| 1160 | /* Check that we have enough space: 3 words for the boot continuation, |
| 1161 | and 3 + nargs for the procedure application. */ |
| 1162 | base_frame_size = 3 + 3 + nargs; |
| 1163 | vm_push_sp (vp, vp->sp + base_frame_size); |
| 1164 | base = vp->sp + 1 - base_frame_size; |
| 1165 | |
| 1166 | /* Since it's possible to receive the arguments on the stack itself, |
| 1167 | shuffle up the arguments first. */ |
| 1168 | for (i = nargs; i > 0; i--) |
| 1169 | base[6 + i - 1] = argv[i - 1]; |
| 1170 | |
| 1171 | /* Push the boot continuation, which calls PROC and returns its |
| 1172 | result(s). */ |
| 1173 | base[0] = SCM_PACK (vp->fp); /* dynamic link */ |
| 1174 | base[1] = SCM_PACK (vp->ip); /* ra */ |
| 1175 | base[2] = vm_boot_continuation; |
| 1176 | vp->fp = &base[2]; |
| 1177 | vp->ip = (scm_t_uint32 *) vm_boot_continuation_code; |
| 1178 | |
| 1179 | /* The pending call to PROC. */ |
| 1180 | base[3] = SCM_PACK (vp->fp); /* dynamic link */ |
| 1181 | base[4] = SCM_PACK (vp->ip); /* ra */ |
| 1182 | base[5] = proc; |
| 1183 | vp->fp = &base[5]; |
| 1184 | |
| 1185 | { |
| 1186 | int resume = SCM_I_SETJMP (registers); |
| 1187 | |
| 1188 | if (SCM_UNLIKELY (resume)) |
| 1189 | /* Non-local return. */ |
| 1190 | vm_dispatch_abort_hook (vp); |
| 1191 | |
| 1192 | return vm_engines[vp->engine](thread, vp, ®isters, resume); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* Scheme interface */ |
| 1197 | |
| 1198 | #define VM_DEFINE_HOOK(n) \ |
| 1199 | { \ |
| 1200 | struct scm_vm *vp; \ |
| 1201 | vp = scm_the_vm (); \ |
| 1202 | if (scm_is_false (vp->hooks[n])) \ |
| 1203 | vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \ |
| 1204 | return vp->hooks[n]; \ |
| 1205 | } |
| 1206 | |
| 1207 | SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 0, 0, 0, |
| 1208 | (void), |
| 1209 | "") |
| 1210 | #define FUNC_NAME s_scm_vm_apply_hook |
| 1211 | { |
| 1212 | VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK); |
| 1213 | } |
| 1214 | #undef FUNC_NAME |
| 1215 | |
| 1216 | SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 0, 0, 0, |
| 1217 | (void), |
| 1218 | "") |
| 1219 | #define FUNC_NAME s_scm_vm_push_continuation_hook |
| 1220 | { |
| 1221 | VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK); |
| 1222 | } |
| 1223 | #undef FUNC_NAME |
| 1224 | |
| 1225 | SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 0, 0, 0, |
| 1226 | (void), |
| 1227 | "") |
| 1228 | #define FUNC_NAME s_scm_vm_pop_continuation_hook |
| 1229 | { |
| 1230 | VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK); |
| 1231 | } |
| 1232 | #undef FUNC_NAME |
| 1233 | |
| 1234 | SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 0, 0, 0, |
| 1235 | (void), |
| 1236 | "") |
| 1237 | #define FUNC_NAME s_scm_vm_next_hook |
| 1238 | { |
| 1239 | VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK); |
| 1240 | } |
| 1241 | #undef FUNC_NAME |
| 1242 | |
| 1243 | SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 0, 0, 0, |
| 1244 | (void), |
| 1245 | "") |
| 1246 | #define FUNC_NAME s_scm_vm_abort_continuation_hook |
| 1247 | { |
| 1248 | VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK); |
| 1249 | } |
| 1250 | #undef FUNC_NAME |
| 1251 | |
| 1252 | SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 0, 0, 0, |
| 1253 | (void), |
| 1254 | "") |
| 1255 | #define FUNC_NAME s_scm_vm_trace_level |
| 1256 | { |
| 1257 | return scm_from_int (scm_the_vm ()->trace_level); |
| 1258 | } |
| 1259 | #undef FUNC_NAME |
| 1260 | |
| 1261 | SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 1, 0, 0, |
| 1262 | (SCM level), |
| 1263 | "") |
| 1264 | #define FUNC_NAME s_scm_set_vm_trace_level_x |
| 1265 | { |
| 1266 | scm_the_vm ()->trace_level = scm_to_int (level); |
| 1267 | return SCM_UNSPECIFIED; |
| 1268 | } |
| 1269 | #undef FUNC_NAME |
| 1270 | |
| 1271 | \f |
| 1272 | /* |
| 1273 | * VM engines |
| 1274 | */ |
| 1275 | |
| 1276 | static int |
| 1277 | symbol_to_vm_engine (SCM engine, const char *FUNC_NAME) |
| 1278 | { |
| 1279 | if (scm_is_eq (engine, sym_regular)) |
| 1280 | return SCM_VM_REGULAR_ENGINE; |
| 1281 | else if (scm_is_eq (engine, sym_debug)) |
| 1282 | return SCM_VM_DEBUG_ENGINE; |
| 1283 | else |
| 1284 | SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine)); |
| 1285 | } |
| 1286 | |
| 1287 | static SCM |
| 1288 | vm_engine_to_symbol (int engine, const char *FUNC_NAME) |
| 1289 | { |
| 1290 | switch (engine) |
| 1291 | { |
| 1292 | case SCM_VM_REGULAR_ENGINE: |
| 1293 | return sym_regular; |
| 1294 | case SCM_VM_DEBUG_ENGINE: |
| 1295 | return sym_debug; |
| 1296 | default: |
| 1297 | /* ? */ |
| 1298 | SCM_MISC_ERROR ("Unknown VM engine: ~a", |
| 1299 | scm_list_1 (scm_from_int (engine))); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | SCM_DEFINE (scm_vm_engine, "vm-engine", 0, 0, 0, |
| 1304 | (void), |
| 1305 | "") |
| 1306 | #define FUNC_NAME s_scm_vm_engine |
| 1307 | { |
| 1308 | return vm_engine_to_symbol (scm_the_vm ()->engine, FUNC_NAME); |
| 1309 | } |
| 1310 | #undef FUNC_NAME |
| 1311 | |
| 1312 | void |
| 1313 | scm_c_set_vm_engine_x (int engine) |
| 1314 | #define FUNC_NAME "set-vm-engine!" |
| 1315 | { |
| 1316 | if (engine < 0 || engine >= SCM_VM_NUM_ENGINES) |
| 1317 | SCM_MISC_ERROR ("Unknown VM engine: ~a", |
| 1318 | scm_list_1 (scm_from_int (engine))); |
| 1319 | |
| 1320 | scm_the_vm ()->engine = engine; |
| 1321 | } |
| 1322 | #undef FUNC_NAME |
| 1323 | |
| 1324 | SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 1, 0, 0, |
| 1325 | (SCM engine), |
| 1326 | "") |
| 1327 | #define FUNC_NAME s_scm_set_vm_engine_x |
| 1328 | { |
| 1329 | scm_c_set_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME)); |
| 1330 | return SCM_UNSPECIFIED; |
| 1331 | } |
| 1332 | #undef FUNC_NAME |
| 1333 | |
| 1334 | void |
| 1335 | scm_c_set_default_vm_engine_x (int engine) |
| 1336 | #define FUNC_NAME "set-default-vm-engine!" |
| 1337 | { |
| 1338 | if (engine < 0 || engine >= SCM_VM_NUM_ENGINES) |
| 1339 | SCM_MISC_ERROR ("Unknown VM engine: ~a", |
| 1340 | scm_list_1 (scm_from_int (engine))); |
| 1341 | |
| 1342 | vm_default_engine = engine; |
| 1343 | } |
| 1344 | #undef FUNC_NAME |
| 1345 | |
| 1346 | SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0, |
| 1347 | (SCM engine), |
| 1348 | "") |
| 1349 | #define FUNC_NAME s_scm_set_default_vm_engine_x |
| 1350 | { |
| 1351 | scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME)); |
| 1352 | return SCM_UNSPECIFIED; |
| 1353 | } |
| 1354 | #undef FUNC_NAME |
| 1355 | |
| 1356 | /* FIXME: This function makes no sense, but we keep it to make sure we |
| 1357 | have a way of switching to the debug or regular VM. */ |
| 1358 | SCM_DEFINE (scm_call_with_vm, "call-with-vm", 1, 0, 1, |
| 1359 | (SCM proc, SCM args), |
| 1360 | "Apply @var{proc} to @var{args} in a dynamic extent in which\n" |
| 1361 | "@var{vm} is the current VM.") |
| 1362 | #define FUNC_NAME s_scm_call_with_vm |
| 1363 | { |
| 1364 | return scm_apply_0 (proc, args); |
| 1365 | } |
| 1366 | #undef FUNC_NAME |
| 1367 | |
| 1368 | \f |
| 1369 | /* |
| 1370 | * Initialize |
| 1371 | */ |
| 1372 | |
| 1373 | SCM |
| 1374 | scm_load_compiled_with_vm (SCM file) |
| 1375 | { |
| 1376 | return scm_call_0 (scm_load_thunk_from_file (file)); |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | void |
| 1381 | scm_init_vm_builtin_properties (void) |
| 1382 | { |
| 1383 | /* FIXME: Seems hacky to do this here, but oh well :/ */ |
| 1384 | scm_sym_apply = scm_from_utf8_symbol ("apply"); |
| 1385 | scm_sym_values = scm_from_utf8_symbol ("values"); |
| 1386 | scm_sym_abort_to_prompt = scm_from_utf8_symbol ("abort-to-prompt"); |
| 1387 | scm_sym_call_with_values = scm_from_utf8_symbol ("call-with-values"); |
| 1388 | scm_sym_call_with_current_continuation = |
| 1389 | scm_from_utf8_symbol ("call-with-current-continuation"); |
| 1390 | |
| 1391 | #define INIT_BUILTIN(builtin, BUILTIN, req, opt, rest) \ |
| 1392 | scm_set_procedure_property_x (vm_builtin_##builtin, scm_sym_name, \ |
| 1393 | scm_sym_##builtin); \ |
| 1394 | scm_set_procedure_minimum_arity_x (vm_builtin_##builtin, \ |
| 1395 | SCM_I_MAKINUM (req), \ |
| 1396 | SCM_I_MAKINUM (opt), \ |
| 1397 | scm_from_bool (rest)); |
| 1398 | FOR_EACH_VM_BUILTIN (INIT_BUILTIN); |
| 1399 | #undef INIT_BUILTIN |
| 1400 | } |
| 1401 | |
| 1402 | void |
| 1403 | scm_bootstrap_vm (void) |
| 1404 | { |
| 1405 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
| 1406 | "scm_init_vm", |
| 1407 | (scm_t_extension_init_func)scm_init_vm, NULL); |
| 1408 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
| 1409 | "scm_init_vm_builtins", |
| 1410 | (scm_t_extension_init_func)scm_init_vm_builtins, |
| 1411 | NULL); |
| 1412 | |
| 1413 | page_size = getpagesize (); |
| 1414 | /* page_size should be a power of two. */ |
| 1415 | if (page_size & (page_size - 1)) |
| 1416 | abort (); |
| 1417 | |
| 1418 | initialize_default_stack_size (); |
| 1419 | |
| 1420 | sym_vm_run = scm_from_latin1_symbol ("vm-run"); |
| 1421 | sym_vm_error = scm_from_latin1_symbol ("vm-error"); |
| 1422 | sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error"); |
| 1423 | sym_regular = scm_from_latin1_symbol ("regular"); |
| 1424 | sym_debug = scm_from_latin1_symbol ("debug"); |
| 1425 | |
| 1426 | vm_boot_continuation = scm_i_make_program (vm_boot_continuation_code); |
| 1427 | SCM_SET_CELL_WORD_0 (vm_boot_continuation, |
| 1428 | (SCM_CELL_WORD_0 (vm_boot_continuation) |
| 1429 | | SCM_F_PROGRAM_IS_BOOT)); |
| 1430 | |
| 1431 | #define DEFINE_BUILTIN(builtin, BUILTIN, req, opt, rest) \ |
| 1432 | vm_builtin_##builtin = scm_i_make_program (vm_builtin_##builtin##_code); |
| 1433 | FOR_EACH_VM_BUILTIN (DEFINE_BUILTIN); |
| 1434 | #undef DEFINE_BUILTIN |
| 1435 | } |
| 1436 | |
| 1437 | void |
| 1438 | scm_init_vm (void) |
| 1439 | { |
| 1440 | #ifndef SCM_MAGIC_SNARFER |
| 1441 | #include "libguile/vm.x" |
| 1442 | #endif |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | Local Variables: |
| 1447 | c-file-style: "gnu" |
| 1448 | End: |
| 1449 | */ |