1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-29 19:30:36 +02:00
Commit graph

25 commits

Author SHA1 Message Date
Michael Käppler
08e26836f1
Fix setjmp/longjmp-related crashes on Windows
* libguile/Makefile.am: add new header file setjump-win.h
* libguile/continuations.h, libguile/dynstack.c, libguile/dynstack.h,
  libguile/intrinsics.h, libguile/vm.h:
  supply custom `setjmp` macro on Windows

Mingw implements `setjmp (env)` as a macro that expands to

 _setjmp (env, faddr)

where `faddr` is set to the current frame address.

This address is then stored as first element in the jump buffer `env`.
When `longjmp` is called, it tries to unwind the stack up
to the saved address by calling `RtlUnwindEx` from MSVCRT,
which will fail, if the stack frames are interwoven with
JIT-generated code, that violate the Windows x64 calling conventions.

Thus implement the macro ourselves as

_setjmp (env, NULL)

which will toggle a code path in `longjmp` that does no unwinding.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2024-10-20 12:14:21 +02:00
Andy Wingo
a20feea43e Continuations capture machine code address
* libguile/continuations.c (scm_i_continuation_to_frame): Adapt to vra
  field renaming.
  (scm_i_reinstate_continuation, grow_stack, copy_stack_and_call)
  (scm_dynthrow): Take mra of continuation.  Set on the vp before the
  longjmp.
* libguile/continuations.h: Update scm_i_reinstate_continuation
  prototype.
* libguile/dynstack.h:
* libguile/control.c (scm_suspendable_continuation_p):
* libguile/dynstack.c (PROMPT_WORDS, PROMPT_VRA, PROMPT_MRA):
  (PROMPT_JMPBUF, scm_dynstack_push_prompt, scm_dynstack_find_prompt)
  (scm_dynstack_wind_prompt): Store both virtual and machine return
  addresses on the dynstack, for prompts.
* libguile/eval.c (eval): Pass NULL for mra.
* libguile/intrinsics.c (push_prompt): Add mra arg, and pass it to the
  dynstack.
* libguile/intrinsics.h: Update prototypes so that continuation-related
  intrinsics can save and restore the MRA.
* libguile/jit.h:
* libguile/jit.c: Return VRA when JIT code needs to tier down.
* libguile/stacks.c (find_prompt, scm_make_stack)
* libguile/throw.c (catch): Adapt find-prompt calls.
* libguile/vm-engine.c (instrument-entry, instrument-loop): Add logic to
  continue with vcode after the mcode finishes.
  (compose-continuation, capture-continuation, abort, prompt): Add logic
  to pass NULL as captured MRA, but continue with mcode from new
  continuations, if appropriate.
* libguile/vm.c (scm_i_vm_cont_to_frame, capture_stack)
  (scm_i_capture_current_stack, reinstate_continuation_x)
  (capture_continuation, compose_continuation_inner, compose_continuation)
  (capture_delimited_continuation, abort_to_prompt): Adapt to plumb
  around machine code continuations.
  (scm_call_n): Check "mra_after_abort" field for machine code
  continuation, if any.
* libguile/vm.h (struct scm_vm): Add "mra_after_abort" field.
  (struct scm_vm_cont): Rename "ra" field to "vra" and add "mra" field.
2018-08-12 15:57:53 +02:00
Andy Wingo
5804c977d7 Rename scm_i_thread to scm_thread
* libguile/scm.h (struct scm_thread, scm_thread): Rename from
  scm_i_thread.
* libguile/deprecated.h (scm_i_thread): Add deprecated typedef.
* libguile/threads.h: Adapt to renaming.
* libguile/intrinsics.h:
* libguile/scmsigs.h:
* libguile/cache-internal.h: Remove threads.h includes; unnecessary with
  the forward decl.
* libguile/continuations.h:
* libguile/gc-inline.h:
* libguile/async.h: Adapt scm_thread type name change.
* libguile/async.c:
* libguile/continuations.c:
* libguile/control.c:
* libguile/dynstack.c:
* libguile/dynwind.c:
* libguile/eval.c:
* libguile/finalizers.c:
* libguile/fluids.c:
* libguile/gc.c:
* libguile/intrinsics.c:
* libguile/load.c:
* libguile/memoize.c:
* libguile/print.c:
* libguile/read.c:
* libguile/scmsigs.c:
* libguile/script.c:
* libguile/stackchk.c:
* libguile/stacks.c:
* libguile/symbols.c:
* libguile/threads.c:
* libguile/throw.c:
* libguile/vm-engine.c:
* libguile/vm.c: Adapt to type name change, and add additional includes
  as needed.
2018-06-26 11:40:22 +02:00
Andy Wingo
6ad1fc9ce0 Use ptrdiff_t instead of scm_t_ptrdiff
* libguile/continuations.h:
* libguile/control.c:
* libguile/control.h:
* libguile/dynstack.c:
* libguile/dynstack.h:
* libguile/eval.c:
* libguile/foreign.c:
* libguile/frames.h:
* libguile/gen-scmconfig.c:
* libguile/gsubr.c:
* libguile/gsubr.h:
* libguile/loader.c:
* libguile/numbers.h:
* libguile/stacks.c:
* libguile/throw.c:
* libguile/vm-engine.c:
* libguile/vm.c:
* libguile/vm.h: Use ptrdiff_t from stddef.h.
2018-06-21 20:20:01 +02:00
Andy Wingo
16879cabed Replace uses of scm_t_int8, scm_t_uintmax, etc with stdint types
* libguile/bitvectors.c:
* libguile/bitvectors.h:
* libguile/bytevectors.c:
* libguile/bytevectors.h:
* libguile/chars.c:
* libguile/continuations.c:
* libguile/control.c:
* libguile/conv-integer.i.c:
* libguile/conv-uinteger.i.c:
* libguile/dynstack.c:
* libguile/dynstack.h:
* libguile/foreign.c:
* libguile/frames.c:
* libguile/frames.h:
* libguile/gc-inline.h:
* libguile/gc.h:
* libguile/gsubr.c:
* libguile/gsubr.h:
* libguile/hash.c:
* libguile/i18n.c:
* libguile/instructions.c:
* libguile/intrinsics.c:
* libguile/intrinsics.h:
* libguile/loader.c:
* libguile/loader.h:
* libguile/numbers.c:
* libguile/numbers.h:
* libguile/pairs.c:
* libguile/ports-internal.h:
* libguile/ports.c:
* libguile/ports.h:
* libguile/posix.c:
* libguile/print.c:
* libguile/print.h:
* libguile/programs.c:
* libguile/programs.h:
* libguile/r6rs-ports.c:
* libguile/random.c:
* libguile/random.h:
* libguile/scm.h:
* libguile/socket.c:
* libguile/srfi-4.c:
* libguile/srfi-4.h:
* libguile/stacks.c:
* libguile/stime.c:
* libguile/strings.c:
* libguile/struct.c:
* libguile/struct.h:
* libguile/symbols.c:
* libguile/threads.c:
* libguile/threads.h:
* libguile/uniform.c:
* libguile/vm-engine.c:
* libguile/vm.c:
* libguile/vm.h:
* libguile/vports.c:
* test-suite/standalone/test-conversion.c:
* test-suite/standalone/test-ffi-lib.c:
* test-suite/standalone/test-scm-take-u8vector.c:
* test-suite/standalone/test-srfi-4.c: Replace e.g. scm_t_uint8 with
  uint8_t.
2018-06-21 20:18:54 +02:00
Andy Wingo
1234bb1850 Update license notices in all C files
Update to newest recommended license notices from the FSF.  Everything
stays LGPLv3+ except guile-readline which is GPLv3+.
2018-06-20 20:07:34 +02:00
Andy Wingo
791eb0c01a Remove (C) from copyright statements
As the FSF advises, 'There is no legal significance to using the
three-character sequence “(C)”, but it does no harm.'  It does take up
space though!  For that reason, we remove it here from our C files.
2018-06-20 19:17:06 +02:00
Andy Wingo
dd8add2fa0 Make .c copyright headers uniform 2018-06-20 18:48:30 +02:00
Andy Wingo
b8d757732f Rationalize include order in C files
Include config.h first, then system includes, then libguile includes, in
alphabetical order, then the include for the file in question.
2018-06-20 18:31:24 +02:00
Andy Wingo
3068bc7384 Make libguile header inclusion consistent within libguile c files
Change from '#include "libguile/foo.h"' and '#include <libguile/foo.h>'
to '#include "foo.h"'.
2018-06-20 17:19:31 +02:00
Andy Wingo
c495b44714 Remove Emacs local variables comments in Guile source
The .dir-locals.el file in the repository is sufficient for Emacs
users.

* libguile/__scm.h:
* libguile/alist.c:
* libguile/alist.h:
* libguile/array-handle.c:
* libguile/array-handle.h:
* libguile/array-map.c:
* libguile/array-map.h:
* libguile/arrays.c:
* libguile/arrays.h:
* libguile/async.c:
* libguile/async.h:
* libguile/backtrace.c:
* libguile/backtrace.h:
* libguile/bitvectors.c:
* libguile/bitvectors.h:
* libguile/boolean.c:
* libguile/boolean.h:
* libguile/chars.c:
* libguile/chars.h:
* libguile/chooks.c:
* libguile/chooks.h:
* libguile/continuations.c:
* libguile/continuations.h:
* libguile/control.c:
* libguile/conv-integer.i.c:
* libguile/debug-malloc.h:
* libguile/debug.c:
* libguile/debug.h:
* libguile/deprecation.c:
* libguile/deprecation.h:
* libguile/dynl.c:
* libguile/dynl.h:
* libguile/dynstack.c:
* libguile/dynstack.h:
* libguile/dynwind.c:
* libguile/dynwind.h:
* libguile/eq.c:
* libguile/eq.h:
* libguile/error.c:
* libguile/error.h:
* libguile/eval.c:
* libguile/eval.h:
* libguile/evalext.c:
* libguile/evalext.h:
* libguile/expand.c:
* libguile/expand.h:
* libguile/extensions.c:
* libguile/extensions.h:
* libguile/feature.c:
* libguile/feature.h:
* libguile/filesys.c:
* libguile/filesys.h:
* libguile/fluids.c:
* libguile/fluids.h:
* libguile/foreign.c:
* libguile/fports.c:
* libguile/fports.h:
* libguile/frames.c:
* libguile/frames.h:
* libguile/gc-inline.h:
* libguile/gc.c:
* libguile/gc.h:
* libguile/generalized-arrays.c:
* libguile/generalized-arrays.h:
* libguile/generalized-vectors.c:
* libguile/generalized-vectors.h:
* libguile/gettext.c:
* libguile/gettext.h:
* libguile/goops.c:
* libguile/goops.h:
* libguile/gsubr.c:
* libguile/gsubr.h:
* libguile/guardians.c:
* libguile/guardians.h:
* libguile/guile.c:
* libguile/hash.c:
* libguile/hash.h:
* libguile/hashtab.c:
* libguile/hashtab.h:
* libguile/hooks.c:
* libguile/hooks.h:
* libguile/i18n.c:
* libguile/i18n.h:
* libguile/init.c:
* libguile/init.h:
* libguile/instructions.c:
* libguile/instructions.h:
* libguile/intrinsics.c:
* libguile/intrinsics.h:
* libguile/ioext.c:
* libguile/ioext.h:
* libguile/iselect.h:
* libguile/keywords.c:
* libguile/keywords.h:
* libguile/list.c:
* libguile/list.h:
* libguile/load.c:
* libguile/load.h:
* libguile/loader.c:
* libguile/loader.h:
* libguile/macros.c:
* libguile/macros.h:
* libguile/mallocs.c:
* libguile/mallocs.h:
* libguile/memmove.c:
* libguile/memoize.c:
* libguile/memoize.h:
* libguile/modules.c:
* libguile/modules.h:
* libguile/net_db.c:
* libguile/net_db.h:
* libguile/null-threads.c:
* libguile/null-threads.h:
* libguile/numbers.c:
* libguile/numbers.h:
* libguile/objprop.c:
* libguile/objprop.h:
* libguile/options.c:
* libguile/options.h:
* libguile/pairs.c:
* libguile/pairs.h:
* libguile/poll.c:
* libguile/poll.h:
* libguile/ports.c:
* libguile/ports.h:
* libguile/posix.c:
* libguile/posix.h:
* libguile/print.c:
* libguile/print.h:
* libguile/procprop.c:
* libguile/procprop.h:
* libguile/procs.c:
* libguile/procs.h:
* libguile/programs.c:
* libguile/programs.h:
* libguile/promises.c:
* libguile/promises.h:
* libguile/pthread-threads.h:
* libguile/random.c:
* libguile/random.h:
* libguile/rdelim.c:
* libguile/rdelim.h:
* libguile/read.c:
* libguile/read.h:
* libguile/regex-posix.c:
* libguile/regex-posix.h:
* libguile/rw.c:
* libguile/rw.h:
* libguile/scmsigs.c:
* libguile/scmsigs.h:
* libguile/script.c:
* libguile/script.h:
* libguile/simpos.c:
* libguile/simpos.h:
* libguile/smob.c:
* libguile/smob.h:
* libguile/snarf.h:
* libguile/socket.c:
* libguile/socket.h:
* libguile/sort.c:
* libguile/sort.h:
* libguile/srcprop.c:
* libguile/srcprop.h:
* libguile/stackchk.c:
* libguile/stackchk.h:
* libguile/stacks.c:
* libguile/stacks.h:
* libguile/stime.c:
* libguile/stime.h:
* libguile/strerror.c:
* libguile/strings.c:
* libguile/strings.h:
* libguile/strorder.c:
* libguile/strorder.h:
* libguile/strports.c:
* libguile/strports.h:
* libguile/struct.c:
* libguile/struct.h:
* libguile/symbols.c:
* libguile/symbols.h:
* libguile/syntax.c:
* libguile/syscalls.h:
* libguile/tags.h:
* libguile/threads.c:
* libguile/threads.h:
* libguile/throw.c:
* libguile/throw.h:
* libguile/trees.h:
* libguile/unicode.c:
* libguile/unicode.h:
* libguile/uniform.c:
* libguile/uniform.h:
* libguile/values.c:
* libguile/values.h:
* libguile/variable.c:
* libguile/variable.h:
* libguile/vectors.c:
* libguile/vectors.h:
* libguile/version.c:
* libguile/vm-engine.c:
* libguile/vm-expand.h:
* libguile/vm.c:
* libguile/vm.h:
* libguile/vports.c:
* libguile/vports.h:
* libguile/weak-list.h:
* libguile/weak-set.c:
* libguile/weak-set.h:
* libguile/weak-table.c:
* libguile/weak-table.h:
* libguile/weak-vector.c:
* libguile/weak-vector.h: Remove needless trailing comments.
2018-06-20 14:32:28 +02:00
Andy Wingo
40ade0886e Remove _scm.h
* libguile/_scm.h: Remove.  An internal header, never installed.
* libguile/__scm.h: Remove horrible documentation.
* libguile/Makefile.am (EXTRA_libguile_@GUILE_EFFECTIVE_VERSION@_la_SOURCES):
  Remove _scm.h.
* libguile/alist.c:
* libguile/array-handle.c:
* libguile/array-map.c:
* libguile/arrays.c:
* libguile/async.c:
* libguile/atomic.c:
* libguile/backtrace.c:
* libguile/bitvectors.c:
* libguile/boolean.c:
* libguile/bytevectors.c:
* libguile/chars.c:
* libguile/continuations.c:
* libguile/control.c:
* libguile/debug-malloc.c:
* libguile/debug.c:
* libguile/deprecated.c:
* libguile/deprecation.c:
* libguile/dynl.c:
* libguile/dynstack.c:
* libguile/dynwind.c:
* libguile/eq.c:
* libguile/error.c:
* libguile/eval.c:
* libguile/evalext.c:
* libguile/expand.c:
* libguile/extensions.c:
* libguile/fdes-finalizers.c:
* libguile/feature.c:
* libguile/filesys.c:
* libguile/finalizers.c:
* libguile/fluids.c:
* libguile/foreign-object.c:
* libguile/foreign.c:
* libguile/fports.c:
* libguile/frames.c:
* libguile/gc-malloc.c:
* libguile/gc.c:
* libguile/gen-scmconfig.c:
* libguile/generalized-arrays.c:
* libguile/generalized-vectors.c:
* libguile/gettext.c:
* libguile/goops.c:
* libguile/gsubr.c:
* libguile/guardians.c:
* libguile/hash.c:
* libguile/hashtab.c:
* libguile/hooks.c:
* libguile/i18n.c:
* libguile/init.c:
* libguile/instructions.c:
* libguile/intrinsics.c:
* libguile/ioext.c:
* libguile/keywords.c:
* libguile/list.c:
* libguile/load.c:
* libguile/loader.c:
* libguile/macros.c:
* libguile/mallocs.c:
* libguile/memoize.c:
* libguile/modules.c:
* libguile/net_db.c:
* libguile/null-threads.c:
* libguile/numbers.c:
* libguile/objprop.c:
* libguile/options.c:
* libguile/pairs.c:
* libguile/poll.c:
* libguile/ports-internal.h:
* libguile/ports.c:
* libguile/posix.c:
* libguile/print.c:
* libguile/procprop.c:
* libguile/procs.c:
* libguile/programs.c:
* libguile/promises.c:
* libguile/r6rs-ports.c:
* libguile/random.c:
* libguile/rdelim.c:
* libguile/read.c:
* libguile/regex-posix.c:
* libguile/rw.c:
* libguile/scmsigs.c:
* libguile/script.c:
* libguile/simpos.c:
* libguile/smob.c:
* libguile/socket.c:
* libguile/sort.c:
* libguile/srcprop.c:
* libguile/srfi-1.c:
* libguile/srfi-13.c:
* libguile/srfi-14.c:
* libguile/srfi-4.c:
* libguile/srfi-60.c:
* libguile/stackchk.c:
* libguile/stacks.c:
* libguile/stime.c:
* libguile/strings.c:
* libguile/strorder.c:
* libguile/strports.c:
* libguile/struct.c:
* libguile/symbols.c:
* libguile/syntax.c:
* libguile/threads.c:
* libguile/throw.c:
* libguile/trees.c:
* libguile/unicode.c:
* libguile/uniform.c:
* libguile/values.c:
* libguile/variable.c:
* libguile/vectors.c:
* libguile/version.c:
* libguile/vm.c:
* libguile/vports.c:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Remove _scm.h includes.
2018-06-20 14:26:01 +02:00
Andy Wingo
3c48c87784 Devolve variable.h from _scm.h.
* libguile/_scm.h: Remove variable.h.
* libguile/backtrace.c:
* libguile/debug.c:
* libguile/dynstack.c:
* libguile/dynwind.c:
* libguile/eval.c:
* libguile/evalext.c:
* libguile/expand.c:
* libguile/feature.c:
* libguile/fluids.c:
* libguile/foreign-object.c:
* libguile/fports.c:
* libguile/frames.c:
* libguile/goops.c:
* libguile/i18n.c:
* libguile/load.c:
* libguile/macros.c:
* libguile/memoize.c:
* libguile/ports.c:
* libguile/print.c:
* libguile/programs.c:
* libguile/random.c:
* libguile/read.c:
* libguile/scmsigs.c:
* libguile/srfi-4.c:
* libguile/strports.c:
* libguile/syntax.c:
* libguile/threads.c:
* libguile/throw.c: Add variable.h.
2018-06-20 11:00:29 +02:00
Andy Wingo
f84ce5442e Refactor handling of auxiliary stacks and setjmp/longjmp
* libguile/__scm.h (scm_i_jmp_buf): Remove definition, which was a shim
  for ia64.  Instead, always use setjmp/longjmp and jmp_buf.
* libguile/_scm.h (SCM_I_SETJMP, SCM_I_LONGJMP): Remove; instead use
  setjmp and longjmp.
* libguile/continuations.c (capture_auxiliary_stack):
  (restore_auxiliary_stack): New helpers.
  (scm_i_make_continuation): Use capture_auxiliary_stack.
  (copy_stack_and_call): Use restore_auxiliary_stack.  No need to stash
  the aux stack on the thread, either.
* libguile/continuations.h (scm_t_contregs): Use
  SCM_HAVE_AUXILIARY_STACK to flag when to have an auxiliary_stack
  member.
* libguile/control.h:
* libguile/control.c (reify_partial_continuation, scm_c_abort):
  (scm_suspendable_continuation_p): Adapt to use setjmp/longjmp
  directly.
* libguile/deprecated.h: Add deprecated scm_i_jmp_buf define.
* libguile/dynstack.h:
* libguile/dynstack.c (PROMPT_JMPBUF):
  (scm_dynstack_push_prompt, scm_dynstack_find_prompt):
  (scm_dynstack_wind_prompt): Adapt to jmp_buf type.
* libguile/eval.c (eval): Use jmp_buf and setjmp directly.
* libguile/gc-malloc.c: No need for ia64-specific things.
* libguile/gc.c: No need for ia64-specific things.
* libguile/gc.h: No need to declare scm_ia64_ar_bsp.
* libguile/init.c: Remove typedef of setjmp_type for Cray, unused.
* libguile/threads.c (guilify_self_1): No more pending_rbs_continuation
  in scm_i_thread, and register_backing_store_base is handled by libgc.
  (scm_ia64_ar_bsp): Remove definitions; inlined into continuations.c's
  capture_auxiliary_stack.
* libguile/threads.h (scm_i_thread): jmpbuf member is plain jmp_buf.
* libguile/throw.c (catch): Just use jmp_buf and setjmp.
* libguile/vm-engine.c (VM_NAME): Adapt prototype to take jmp_buf
  pointer.
* libguile/vm.c (vm_abort): Adapt jmp_buf types.
  (scm_call_n): Use setjmp.
2018-06-17 13:41:03 +02:00
Andy Wingo
00ed4043c2 VM continuations store FP/SP by offset
* libguile/continuations.c (scm_i_continuation_to_frame):
* libguile/stacks.c (scm_make_stack):
* libguile/vm.c (scm_i_vm_cont_to_frame, scm_i_vm_capture_stack):
  (vm_return_to_continuation_inner)
  (struct vm_reinstate_partial_continuation_data):
  (vm_reinstate_partial_continuation_inner):
  (vm_reinstate_partial_continuation):
* libguile/vm.h (sstruct scm_vm_cont): Simplify VM continuations by
  recording the top FP by offset, not value + reloc.
* libguile/frames.c (frame_offset, scm_i_vm_frame_offset): Remove unused
  functions.
* libguile/frames.h (SCM_VALIDATE_VM_FRAME, scm_i_vm_frame_offset):
  Remove.
* libguile/control.c (reify_partial_continuation): Once we know the
  base_fp, relocate the dynamic stack.
* libguile/dynstack.h:
* libguile/dynstack.c (scm_dynstack_relocate_prompts): New function.
  (scm_dynstack_wind_prompt): Adapt to add new fp offset.
2017-02-12 20:31:14 +01:00
Andy Wingo
5048a8afbc Avoid reading garbage from dynstack
* libguile/dynstack.c (push_dynstack_entry_unchecked): Reset top
  dynstack tag to prevent garbage.
2017-02-12 18:02:53 +01:00
Andy Wingo
498f3f9568 Avoid stacks in dynamically-bound values
* libguile/dynstack.h:
* libguile/dynstack.c (scm_dynstack_find_old_fluid_value): New
  function.
* libguile/fluids.c (saved_dynamic_state_ref): New helper.
  (scm_fluid_ref): Fix docstring.
  (scm_fluid_ref_star): New function allowing access to previous values
  for a fluid.
  (scm_dynamic_state_ref): New internal function.
* libguile/fluids.h: Add scm_fluid_ref_star and scm_dynamic_state_ref.
* libguile/stacks.c (scm_stack_id): Adapt to %stacks not being a chain.
* libguile/throw.c (catch, throw_without_pre_unwind): Adapt to
  %exception-handlers not being a chain.
* module/ice-9/boot-9.scm (catch, dispatch-exception): Instead of having
  %exception-handlers be a chain, use fluid-ref* to access the chain
  that is in place at the time the exception is thrown.  Prevents
  unintended undelimited capture of the current exception handler stack
  by a delimited "catch".
  (%start-stack): Similarly, don't be a chain.
* module/system/repl/debug.scm (frame->stack-vector):
* module/system/repl/error-handling.scm (call-with-error-handling):
* module/ice-9/save-stack.scm (save-stack): Adapt to %stacks not being a
  chain.
* test-suite/tests/exceptions.test ("delimited exception handlers"): Add
  tests.
* doc/ref/api-control.texi (Fluids and Dynamic States): Add docs.
2017-02-07 09:57:55 +01:00
Andy Wingo
7184c176b4 with-dynamic-state compiler and VM support
* libguile/dynstack.h (SCM_DYNSTACK_TYPE_DYNAMIC_STATE):
* libguile/dynstack.c (DYNAMIC_STATE_WORDS, DYNAMIC_STATE_STATE_BOX):
  (scm_dynstack_push_dynamic_state):
  (scm_dynstack_unwind_dynamic_state): New definitions.
  (scm_dynstack_unwind_1, scm_dynstack_wind_1): Add with-dynamic-state
  cases.
* libguile/memoize.c (push_dynamic_state, pop_dynamic_state)
  (do_push_dynamic_state, do_pop_dynamic_state): New definitions.
  (memoize, scm_init_memoize): Handle push-dynamic-state and
  pop-dynamic-state.
* libguile/vm-engine.c (push-dynamic-state, pop-dynamic-state): New
  opcodes.
* module/ice-9/boot-9.scm (with-dynamic-state): New definition in Scheme
  so that the push-dynamic-state and pop-dynamic-state always run in the
  VM.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm:
* module/language/cps/types.scm:
* module/language/tree-il/effects.scm (make-effects-analyzer):
* module/language/tree-il/peval.scm (peval):
* module/language/tree-il/primitives.scm (*interesting-primitive-names*):
* module/system/vm/assembler.scm: Add support for with-dynamic-state to
  the compiler.
* test-suite/tests/fluids.test ("dynamic states"): Add basic tests.
* doc/ref/vm.texi (Dynamic Environment Instructions): Update.
2016-12-05 22:57:29 +01:00
Andy Wingo
aa84489d18 Reimplement dynamic states
There are two goals: one, to use less memory per dynamic state in order
to allow millions of dynamic states to be allocated in light-weight
threading scenarios.  The second goal is to prevent dynamic states from
being actively mutated in two threads at once.  This second goal does
mean that dynamic states object that escape into scheme are now copies
that won't receive further updates; an incompatible change, but one
which we hope doesn't affect anyone.

* libguile/cache-internal.h: New file.
* libguile/fluids.c (is_dynamic_state, get_dynamic_state)
  (save_dynamic_state, restore_dynamic_state, add_entry)
  (copy_value_table): New functions.
  (scm_i_fluid_print, scm_i_dynamic_state_print): Move up.
  (new_fluid): No need for a number.
  (scm_fluid_p: scm_is_fluid): Inline IS_FLUID uses.
  (fluid_set_x, fluid_ref): Adapt to dynamic state changes.
  (scm_fluid_set_x, scm_fluid_unset_x): Call fluid_set_x.
  (scm_swap_fluid): Rewrite in terms of fluid_ref and fluid_set.
  (swap_fluid): Use internal fluid_set_x.
  (scm_i_make_initial_dynamic_state): Adapt to dynamic state
  representation change.
  (scm_dynamic_state_p, scm_is_dynamic_state): Use new accessors.
  (scm_current_dynamic_state): Use make_dynamic_state.
  (scm_dynwind_current_dynamic_state): Use new accessor.
* libguile/fluids.h: Remove internal definitions.  Add new struct
  definition.
* libguile/threads.h (scm_i_thread): Use scm_t_dynamic_state for dynamic
  state.
* libguile/threads.c (guilify_self_1, guilify_self_2):
  (scm_i_init_thread_for_guile, scm_init_guile):
  (scm_call_with_new_thread):
  (scm_init_threads, scm_init_threads_default_dynamic_state): Adapt to
  scm_i_thread change.
  (scm_i_with_guile, with_guile): Remove "and parent" suffix.
  (scm_i_reset_fluid): Remove unneeded function.
* doc/ref/api-scheduling.texi (Fluids and Dynamic States): Remove
  scm_make_dynamic_state docs.  Update current-dynamic-state docs.
* libguile/vm-engine.c (vm_engine): Update fluid-ref and fluid-set!
  inlined fast paths for dynamic state changes.
* libguile/vm.c (vm_error_unbound_fluid): Remove now-unused function.
* NEWS: Update.
* module/ice-9/deprecated.scm (make-dynamic-state): New definition.
* libguile/deprecated.h:
* libguile/deprecated.c (scm_make_dynamic_state): Move here.
* libguile/__scm.h (scm_t_dynamic_state): New typedef.
* libguile/dynstack.h:
* libguile/dynstack.c (scm_dynstack_push_fluid):
  (scm_dynstack_unwind_fluid): Take raw dynstate in these internal
  functions.
* libguile/throw.c (catch): Adapt to dynstack changes.
2016-12-05 21:46:37 +01:00
Andy Wingo
0007507340 VM stack grows downward
Adapt VM stack to grow downward.  This will make native compilation look
more like the VM code, as we will be able to use native CALL
instructions, taking proper advantage of the return address buffer.

* libguile/continuations.c (scm_i_continuation_to_frame): Record offsets
  from stack top.

* libguile/control.c (scm_i_prompt_pop_abort_args_x): Adapt for reversed
  order of arguments, and instead of relying on the abort to push on the
  number of arguments, make the caller save the stack depth, which
  allows us to compute the number of arguments ourselves.
  (reify_partial_continuation, scm_c_abort): Adapt to reversed stack
  order.

* libguile/dynstack.c (scm_dynstack_wind_prompt): Since we wind the
  stack in a downward direction, subtract the reloc instead of adding
  it.

* libguile/dynstack.h (SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY): Remove flag;
  instead rely on prompt-establishing code to save the stack depth.

* libguile/eval.c (eval): Remove extraneous "volatile" declarations for
  variables that are not re-set between the setjmp and any longjmp.
  Adapt to save stack depth before instating the prompt.

* libguile/foreign.c (scm_i_foreign_call): Adapt to receive arguments in
  reverse order.

* libguile/frames.c (frame_stack_top, scm_i_frame_stack_top): Adapt to
  compute stack top instead of stack bottom.
  (scm_c_frame_closure): Adapt to stack growth change.
  (scm_frame_num_locals, scm_frame_local_ref, scm_frame_set_x): Use
  union data type to access stack.
  (RELOC): Reformat.
  (scm_c_frame_previous): Adapt to stack growth change.

* libguile/frames.h: Adapt stack diagram to indicate that the stack
  grows up.
  (union scm_vm_stack_element): New data type used to access items on
  the stack.
  (SCM_FRAME_PREVIOUS_SP)
  (SCM_FRAME_RETURN_ADDRESS, SCM_FRAME_SET_RETURN_ADDRESS)
  (SCM_FRAME_DYNAMIC_LINK, SCM_FRAME_SET_DYNAMIC_LINK)
  (SCM_FRAME_LOCAL, SCM_FRAME_NUM_LOCALS): Adapt to stack representation
  change.
  (SCM_FRAME_SLOT): New helper.
  (SCM_VM_FRAME_FP, SCM_VM_FRAME_SP): Adapt to stack growth change.

* libguile/stacks.c (scm_make_stack): Record offsets from top of stack.

* libguile/throw.c (catch): Adapt to scm_i_prompt_pop_abort_args_x
  change.

* libguile/vm-engine.c (ALLOC_FRAME, RESET_FRAME):
  (FRAME_LOCALS_COUNT_FROM): Adapt to stack growth change.
  (LOCAL_ADDRESS): Use SCM_FRAME_SLOT to get the address as the proper
  data type.
  (RETURN_ONE_VALUE, RETURN_VALUE_LIST): Adapt to stack growth change.
  (apply): Shuffling up the SMOB apply args can cause the stack to
  expand, so use ALLOC_FRAME instead of RESET_FRAME.
  (vm_engine): Adapt for stack growth change.

* libguile/vm.c (vm_increase_sp, vm_push_sp, vm_restore_sp): Adapt to
  stack representation change.
  (scm_i_vm_cont_to_frame): Adapt to take offsets from the top.
  (scm_i_vm_capture_stack): Adapt to capture from the top.
  (vm_return_to_continuation_inner): Adapt for data type changes.
  (vm_return_to_continuation): Likewise, and instead of looping, just
  splat the saved arguments on with memcpy.
  (vm_dispatch_hook): Adapt to receive arguments in the reverse order.
  Adapt callers.
  (vm_abort): There is never a tail argument.  Adapt to stack
  representation change.
  (vm_reinstate_partial_continuation)
  (vm_reinstate_partial_continuation_inner): Adapt to stack growth
  change.
  (allocate_stack, free_stack): Adapt to data type change.
  (expand_stack): Don't try to mremap(), as you can't grow a mapping
  from the bottom.  Without knowing that there's a free mapping space
  right below the old stack, which there usually isn't on Linux, we have
  to copy.  We can't use MAP_GROWSDOWN because Linux is buggy.
  (make_vm): Adapt to stack representation changes.
  (return_unused_stack_to_os): Round down instead of up, as the stack
  grows down.
  (scm_i_vm_mark_stack): Adapt to walk up the stack.
  (scm_i_vm_free_stack): Adapt to scm_vm changes.
  (vm_expand_stack_inner, reset_stack_limit, vm_expand_stack): Adapt to
  the stack growing down.
  (scm_call_n): Adapt to the stack growing down.  Don't allow argv to
  point into the stack.

* libguile/vm.h (struct scm_vm, struct scm_vm_cont): Adapt to hold the
  stack top and bottom.
2015-10-21 11:49:20 +02:00
Andy Wingo
0bca90aac9 The dynamic stack records SP and FP values as offsets
* libguile/dynstack.h:
* libguile/dynstack.c (PROMPT_FP, PROMPT_SP):
  (scm_dynstack_push_prompt, scm_dynstack_find_prompt): Prompts on the
  dynstack are recorded as offsets from the base stack address in this
  thread.

* libguile/control.c (scm_c_abort):
* libguile/eval.c (eval):
* libguile/stacks.c (find_prompt, narrow_stack):
* libguile/throw.c (pre_init_catch):
* libguile/vm-engine.c (prompt): Adapt.
2013-11-21 16:51:15 +01:00
Andy Wingo
9121d9f1ac All instruction pointers are now scm_t_uint32*
* libguile/frames.h (SCM_FRAME_RTL_RETURN_ADDRESS)
  (SCM_FRAME_SET_RTL_RETURN_ADDRESS): Remove.  The variants without _RTL
  now work fine.
  (struct scm_vm_frame): Change the return address to be a
  scm_t_uint32*.
  (struct scm_frame): Change IP to be scm_t_uint32*.

* libguile/frames.c (scm_c_make_frame)
* libguile/control.c (reify_partial_continuation, scm_c_abort)
* libguile/dynstack.c (PROMPT_IP)
  (scm_dynstack_push_prompt, scm_dynstack_find_prompt)
* libguile/dynstack.h:
* libguile/vm-engine.c (SYNC_IP, RETURN_ONE_VALUE)
  (call, return-values, prompt): Adapt.
2013-11-19 18:09:34 +01:00
Andy Wingo
98eaef1b50 Simplify dynstack API to only wind one fluid at a time
* libguile/dynstack.h (SCM_DYNSTACK_TYPE_WITH_FLUID): Rename from
  with-fluids.
* libguile/dynstack.c (scm_dynstack_push_fluid):
  (scm_dynstack_unwind_fluid): Change API to only wind/unwind one
  fluid binding.
  (WITH_FLUID_WORDS): New define, always 2 words (fluid and value box).
  (WITH_FLUID_FLUID, WITH_FLUID_VALUE_BOX): New macros to get offsets of
  fluid and value box.
  (scm_dynstack_push_rewinder, scm_dynstack_push_unwinder): Use
  WINDER_WORDS.
  (scm_dynstack_push_dynwind): Use DYNWIND_WORDS.
  (scm_dynstack_wind_1): Update for scm_swap_fluid API change.

* libguile/fluids.h:
* libguile/fluids.c (scm_prepare_fluids): Remove; no longer needed.
  (scm_swap_fluid): Update to just swap one fluid binding.
  (scm_c_with_fluids, scm_c_with_fluid): Update to use
  scm_dynstack_push_fluid.

* libguile/memoize.c (do_push_fluid, do_pop_fluid): Adapt to API
  change.
* libguile/vm-engine.c (rtl_vm_engine): Change wind-fluids / unwind-fluids
  to push-fluid / pop-fluid, and actually enable.  Woo!

* libguile/vm-i-system.c (push-fluid, pop-fluid): Update to new API.
2013-06-28 20:01:35 +02:00
Andy Wingo
9d381ba478 dynstack: pushing a prompt no longer allocates memory
* libguile/control.h: Remove scm_t_prompt_registers and
  scm_c_make_prompt_registers.
  (scm_c_abort): Take a pointer to a jmpbuf instead of a cookie.  It
  will serve the same purpose.
* libguile/control.c (reify_partial_continuation, scm_at_abort): Adapt
  to new prompt representation.

* libguile/dynstack.h:
* libguile/dynstack.c (scm_dynstack_push_prompt): Prompts now have 5
  words instead of 2, as they now push the fp, sp, ip, and jmpbuf on the
  stack separately.  This avoids allocation.
  (scm_dynstack_find_prompt): Likewise, add return values for fp, sp,
  etc.
  (scm_dynstack_wind_prompt): Replaces scm_dynstack_relocate_prompt.

* libguile/eval.c (eval):
* libguile/stacks.c (find_prompt):
* libguile/throw.c (pre_init_catch): Adapt to the new prompt mechanism.

* libguile/vm-engine.c (vm_engine): Setjmp an on-stack jmpbuf every time
  the VM enters.  We can then re-use that jmpbuf for all prompts in that
  invocation.

* libguile/vm-i-system.c (partial_cont_call): Adapt to change in prompt
  representation.  We don't need to wind here any more, since we pass in
  the prompt's jmpbuf.
  (prompt): Adapt to scm_dynstack_push_prompt change.
  (abort): Adapt to vm_abort change.

* libguile/vm.h (struct scm_vm): No more cookie.

* libguile/vm.c (vm_abort): Adapt to scm_c_abort change.
  (vm_reinstate_partial_continuation): Rewind the dynamic stack here,
  now that we do have a valid jmpbuf.
  (make_vm): No need to initialize a cookie.
2012-03-07 10:27:16 +01:00
Andy Wingo
9ede013f68 the dynamic stack is really a stack now, instead of a list
* libguile/dynstack.h:
* libguile/dynstack.c: New files, implementing the dynamic stack as a
  true stack instead of a linked list.  This lowers the cost of
  dynwinds: frames, winders, prompts, with-fluids, and dynamic-wind.
  For the most part, we allocate these items directly on the stack.

* libguile/dynwinds.h:
* libguile/dynwinds.c: Adapt all manipulators of the wind stack to use
  interfaces from dynstack.c.  Remove heap-allocated winder and frame
  object types.
  (scm_dowinds, scm_i_dowinds): Remove these.  The first was exported,
  but it was not a public interface.

* libguile/continuations.c:
* libguile/continuations.h (scm_t_contregs): Continuation objects
  reference scm_t_dynstack* values now.  Adapt to the new interfaces.

* libguile/control.c:
* libguile/control.h: There is no longer a scm_tc7_prompt kind of object
  that can be allocated on the heap.  Instead, the prompt flags, key,
  and registers are pushed on the dynwind stack.  (The registers are
  still on the heap.)  Also, since the vm_cont will reference the
  dynwinds, make the partial continuation stub take just one extra arg,
  instead of storing the intwinds separately in the object table.

* libguile/fluids.c:
* libguile/fluids.h: No more with-fluids objects; instead, the fluids go
  on the dynstack.  The values still have to be on the heap, though.
  (scm_prepare_fluids, scm_swap_fluids): New internal functions,
  replacing scm_i_make_with_fluids and scm_i_swap_with_fluids.

* libguile/print.c: Remove prompt and with-fluids printers.

* libguile/tags.h: Revert prompt and with-fluids tc7 values to what they
  were before they were allocated.

* libguile/vm-i-system.c (partial_cont_call): Just pop the vmcont, the
  intwinds will not be passed as a second arg.  Rewind the dynamic stack
  from within the VM, so that any rewinder sees valid prompt entries.
  (call_cc, tail_call_cc): Adapt to pass the dynstack to
  scm_i_vm_capture_stack.
  (prompt, wind, unwind, wind_fluids, unwind_fluids): Adapt to the new
  interfaces.

* libguile/vm.h (scm_i_capture_current_stack): Rename from
  scm_i_vm_capture_continuation.
  (scm_i_vm_capture_stack): Take a dynstack as an argument.
* libguile/vm.c (vm_reinstate_partial_continuation): Don't wind here, as
  that could result in winders seeing invalid prompts.

* libguile/eval.c:
* libguile/root.c:
* libguile/stacks.c:
* libguile/threads.c:
* libguile/threads.h:
* libguile/throw.c: Adapt other users of dynwinds to use the dynstack.
2012-03-03 17:06:02 +01:00