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

9141 commits

Author SHA1 Message Date
Andy Wingo
02a9e76b57 Fix bug in compile-alloc-frame
* libguile/jit.c (compile_alloc_frame): Fix alloc-frame filling in of
  undefined values for when previous frame size was unknown.
2018-08-24 12:14:29 +02:00
Andy Wingo
5603079995 Clear frame size after subr-call, return-values
* libguile/jit.c (compile_return_values, compile_subr_call): Clear frame
  size.
2018-08-24 11:43:50 +02:00
Andy Wingo
270147cf88 Fix r12 register spec for lightning with sysv x86-64 ABI
* libguile/lightning/lib/jit_x86.c (_rvs): Mark R12 as callee-save.
2018-08-24 11:43:09 +02:00
Andy Wingo
25e9b0f79d 32-bit fix for emit_run_hook
* libguile/jit.c (emit_run_hook): Use sign-extending load, as unsigned
  load doesn't exist on 32-bit targets.
2018-08-23 19:53:17 +02:00
Andy Wingo
7854460fac Minor refactors to jit.c
* libguile/jit.c (emit_direct_tail_call): Add FIXME.
  (emit_mov): Simplify implementation.
  (emit_branch_if_frame_locals_count_less_than):
  (emit_branch_if_frame_locals_count_eq):
  (emit_branch_if_frame_locals_count_greater_than):
  (emit_load_fp_slot):
  (emit_branch_if_immediate):
  (emit_load_heap_object_word):
  (emit_load_heap_object_tc):
  (emit_branch_if_heap_object_has_tc):
  (emit_branch_if_heap_object_not_tc):
  (emit_branch_if_heap_object_not_tc7):
  (emit_entry_trampoline):
  (emit_handle_interrupts_trampoline):
  (initialize_handle_interrupts_trampoline):
  (emit_free_variable_ref): Move later in the file.
2018-08-23 19:45:36 +02:00
Andy Wingo
bf035d9385 Fix load width of get-ip-relative-addr
* libguile/jit.c (emit_get_ip_relative_addr): Fix load width.
2018-08-23 17:19:15 +02:00
Andy Wingo
065efdd101 Fix in-tree builds
* libguile/Makefile.am (AM_CPPFLAGS): Use -iquote for $(builddir)
  instead of -I, to avoid having in-tree builds find libguile/poll.h for
  <poll.h>.
2018-08-23 10:47:00 +02:00
Andy Wingo
fd1deab3c3 Optimizations to subr-call
* libguile/jit.c (compile_subr_call): Optimizations.
2018-08-23 10:27:27 +02:00
Andy Wingo
01a79bed89 Comments in the JIT
* libguile/jit.c: Add some overview comments.
2018-08-23 10:24:30 +02:00
Daniel Llorens
01c14512d6 Add $(builddir) to include path in libguile/Makefile.am 2018-08-21 18:41:34 +02:00
Andy Wingo
77695612f4 Add support for labels, and fix a bug in uadd/imm and usub/imm.
* libguile/jit.c (add_inter_instruction_patch, compile, compute_mcode):
  Add support for labels.
  (compile_uadd_immediate, compile_usub_immediate): Fix cases where we
  were adding the wrong operand as an immediate.
2018-08-20 15:35:47 +02:00
Andy Wingo
d7fcdd9a2e Add out-of-line handle-interrupts trampoline stub
* libguile/jit.c (emit_handle_interrupts_trampoline)
  (initialize_handle_interrupts_trampoline)
  (compile_handle_interrupts): Handle most of the slow case of
  handle-interrupts out-of-line, to avoid code bloat.
2018-08-20 14:57:00 +02:00
Andy Wingo
916c570557 Tweak to instrument-entry
* libguile/vm-engine.c (instrument-entry): Eagerly check if data->mcode
  is already set, and in that case just jump directly without checking
  the counter.
2018-08-20 12:45:07 +02:00
Andy Wingo
9c76a1ad42 Fix frame popping in JIT
* libguile/jit.c (emit_entry_trampoline): Don't bother hackily trying to
  save registers; the "jit_frame" call handles that.
  (compile_return_values, compile_return_from_interrupt): Fix bug when
  computing previous FP: no need to add frame_overhead_slots.
  (emit_load_prev_fp_offset, emit_store_prev_fp_offset): Rename from
  emit_load_prev_frame_size, emit_store_prev_frame_size.
  (emit_push_frame): Adapt to emit_store_prev_frame_size.  Don't
  subtract off the frame_overhead_slots.
  (scm_jit_enter_mcode): Comment out a printf for the time being.
2018-08-20 12:45:00 +02:00
Andy Wingo
d00150303d JIT compilation tweaks
* libguile/jit.c (emit_alloc_frame_for_sp, compile_current_thread): Fix
  some ldxr/ldxi stxr/stxi confusions.
  (compile_alloc_frame): Omit if the frame size is already correct.
  (compile, compute_mcode, scm_sys_jit_compile, scm_jit_enter_mcode):
  Add a bit more debugging.
2018-08-20 11:43:46 +02:00
Andy Wingo
4a9de64f81 Fix default code allocator in Lightning
* libguile/lightning/lib/lightning.c (_jit_emit): The default code
  allocator will simply mmap a code buffer, try to emit into that
  buffer, and if it fails, try again with a larger buffer.  However the
  buffer size starts at 0, for some reason.  Why?  I can't see the
  reason.  Change the default to 4096.  In the future we will need to
  implement our own allocator anyway so that we can pack multiple JIT
  runs in one page.
2018-08-20 08:59:19 +02:00
Andy Wingo
dca1e9d7bd Manual JIT interface via %jit-compile
* libguile/init.c (scm_i_init_guile): Call scm_init_jit ().
* libguile/jit.c (enter_mcode, exit_mcode): New static members; code
  pointers for the JIT trampoline.
  (emit_exit): New helper.  The Lightning tramp/frame mechanism that we
  use needs to exit via a jmp instead of a return.  Adapt callers of
  jit_ret.
  (emit_entry_trampoline): Use the "frame" mechanism to enter the JIT.
  (compile1): Add missing "break" after case statements.  Oops!
  (compile): Add prolog and "tramp" to compiled functions.
  (initialize_jit): New local routine to init the JIT on demand.
  (compute_mcode): New helper, to compile a function.
  (scm_sys_jit_compile): New function, exported to Scheme as
  %jit-compile.
  (scm_jit_compute_mcode): Return the existing mcode if the function is
  at the start.
  (scm_jit_enter_mcode): Call the enter_mcode trampoline.
* libguile/jit.h (struct scm_jit_state): Declare, so we can make
  pointers to it.
* libguile/threads.h (struct scm_thread): Add jit_state member.
* libguile/threads.c (on_thread_exit): Free the jit state.
2018-08-20 08:56:35 +02:00
Andy Wingo
698bff8748 First implementation of a template JIT
* libguile/jit.c: Implement most of a JIT.  Untested and still needs to
  be wired up.
2018-08-19 17:39:16 +02:00
Andy Wingo
110310b3e7 Expose scm_vm_builtin_ref internally
* libguile/vm-builtins.h:
* libguile/vm.c (scm_vm_builtin_ref): Expose internally.
2018-08-19 17:38:59 +02:00
Andy Wingo
cf0270cb4c Add internal function mapping subr index to function
* libguile/gsubr.h:
* libguile/gsubr.c (scm_subr_function_by_index): New internal function.
  (scm_subr_function): Use new function.
2018-08-19 17:38:11 +02:00
Andy Wingo
3827769aff Add instrumentation to VM builtins
* libguile/intrinsics.h: Add "intrinsic" for handle-interrupts code.
  Unlike the other intrinsics, this one isn't a function.
* libguile/programs.c (try_parse_arity): Add cases for instructions used
  in VM builtins.
  (scm_primitive_call_ip): Return #f if call-ip not found.
* libguile/vm-engine.c (handle-interrupts): Get code from intrinsics.
* libguile/vm.c
* libguile/vm.c (instrumented_code, define_vm_builtins): Add
  instrumentation to the builtins, so that they can be JIT-compiled.
  (INIT_BUILTIN): Remove min-arity setting; the fallback min-arity
  interpreter should figure it out.
  (scm_bootstrap_vm): Call the new define_vm_builtins function.
* libguile/gsubr.c (primitive_call_ip): Return 0 if call IP not found.
  (primitive_subr_idx): Interpret call ip == 0 as not-a-subr.
* module/system/vm/program.scm (program-arguments-alist): Allow a #f
  call-ip.
2018-08-17 08:50:33 +02:00
Andy Wingo
e6304fb242 Define intrinsics for atomic ops
* libguile/intrinsics.h:
* libguile/intrinsics.c (atomic_ref_scm, atomic_set_scm):
  (atomic_swap_scm, atomic_compare_and_swap_scm): New intrinsics, given
  that lightning doesn't know atomics.
  (scm_bootstrap_intrinsics): Init new intrinsics.
* libguile/vm-engine.c (atomic-scm-ref/immediate)
  (atomic-scm-set!/immediate, atomic-scm-swap!/immediate)
  (atomic-scm-compare-and-swap!/immediate): Use intrinsics, to be like
  the JIT.
2018-08-13 22:00:19 +02:00
Andy Wingo
0188bd3816 64-bit intrinsic args and return values passed indirectly on 32-bit
* libguile/intrinsics.h (INDIRECT_INT64_INTRINSICS): New definition.  If
  true, int64 args and return values are passed by reference.  Here to
  make JIT easier.
* libguile/intrinsics.c (indirect_scm_to_int64, indirect_scm_to_uint64):
  (indirect_scm_to_uint64_truncate, indirect_scm_from_int64):
  (indirect_scm_from_uint64, indirect_lsh, indirect_rsh): New indirect
  variants.
  (scm_bootstrap_intrinsics): Use indirect variants as appropriate.
* libguile/vm-engine.c: Update to call indirect intrinsics if
  appropriate.
2018-08-13 22:00:15 +02:00
Andy Wingo
d4abe8bbed Avoid needless 64-bit args on 32-bit machines for some intrinsics
* libguile/intrinsics.h:
* libguile/intrinsics.c (string_set_x): Change to take size_t and u32 as
  args.
  (allocate_words): Change to take size_t as arg.
* libguile/vm.c (expand_apply_argument): Rename from rest_arg_length,
  and also handle the stack manipulation.
* libguile/vm-engine.c (expand-apply-argument): Update for intrinsic
  change.
  (call-scm-sz-u32): Rename from call-scm-u64-u64, as it matches its
  uses and will compile better on 32-bit systems.
* module/system/vm/assembler.scm (define-scm-sz-u32-intrinsic):
  (string-set!): Update for new instrinsic call inst.
* libguile/jit.c (compile_call_scm_sz_u32): Adapt.
2018-08-13 14:30:01 +02:00
Andy Wingo
11940f4c72 Update error-wrong-num-args intrinsic prototype
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS):
* libguile/intrinsics.c (error_wrong_num_args): Take the thread as an
  arg, instead of the ostensible callee.
* libguile/vm-engine.c: Update callers of wrong-num-args intrinsic to
  pass a thread instead.
2018-08-13 10:59:09 +02:00
Andy Wingo
5df43b60a9 Adapt JIT calling convention; continuations take mra from stack
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Update prototype of
  capture-continuation.
* libguile/jit.h:
* libguile/jit.c (scm_jit_enter_mcode): Return void, not the vra.
  Instead, we expect the code to set vp->ip for the vra.
* libguile/vm-engine.c (instrument-entry, instrument-loop)
  (return-values, abort): Adapt scm_jit_enter_mcode calling convention.
  (capture-continuation): No need to pass an mra; the intrinsic will
  read it from the stack.
* libguile/vm.c (capture_continuation): Remove mra arg, as we take mra
  from the continuation.
  (scm_call_n): Adapt to scm_jit_enter_mcode change.
2018-08-13 10:32:13 +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
939b1ae23f Rework foreign-call trampoline
* libguile/foreign.c (scm_i_foreign_call): Rename back from
  foreign_call.  Need a new trampoline that's easier to call from JIT,
  until we actually rewrite the FFI in terms of the JIT.
  (scm_register_foreign): Remove foreign_call intrinsic init.
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Foreign-call
  intrinsic sets return directly on stack.
* libguile/vm-engine.c (foreign-call): Adapt to new intrinsic behavior.
* libguile/vm.c (foreign_call, scm_bootstrap_vm): Add new intrinsic
  wrapper.
2018-08-11 14:25:07 +02:00
Andy Wingo
6027027724 Invoke VM hooks through intrinsics
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Make intrinsics for
  the VM hooks.
* libguile/vm-engine.c (RUN_HOOK): Run hooks through the intrinsics
  table.
* libguile/vm.c (invoke_apply_hook, invoke_return_hook):
  (invoke_next_hook, invoke_abort_hook): Rename, remove NOINLINE
  attribute (as we call them through the intrinsics table).
  (scm_bootstrap_vm): Init new intrinsics.
2018-08-11 11:37:03 +02:00
Andy Wingo
ef4c1a5f55 Add unpack-values-object intrinsic
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Add
  unpack-values-object.
* libguile/vm-engine.c (subr-call): If the object is a values object,
  call out to unpack-values-object.  This is to avoid reifying
  allocate-frame code in each jitted subr.
* libguile/vm.c (unpack_values_object, scm_bootstrap_vm): Define new
  intrinsic.
2018-08-11 11:36:58 +02:00
Andy Wingo
c3ff72cb81 Change call/cc inst to capture-continuation
* libguile/jit.c (compile_capture_continuation): Rename from call_cc now
  that the call is elsewhere.
* libguile/vm-engine.c (call, tail-call): Remove needless SYNC_IP before
  get-callee-vcode; the intrinsic can sync the ip if needed from the
  frame.
  (capture-continuation): Rename from call/cc, and leave the call itself
  to tail-call.
* libguile/vm.c (vm_builtin_call_with_current_continuation_code): Update
  to put the continuation in a local and then tail call.
  (get_callee_vcode): Sync vp->ip if we error.
2018-08-08 16:32:18 +02:00
Andy Wingo
926b72f5ac Rework program->ip mapping in VM to always call intrinsic
* libguile/intrinsics.h:
* libguile/vm.c (get_callee_vcode): Rename from apply_non_program, and
  instead return the IP for the callee of a frame.
  (scm_call_n, scm_bootstrap_vm): Adapt to get_callee_vcode change.
* libguile/vm-engine.c (call, tail-call, call/cc): Use
  get_callee_vcode unconditionally.  JIT will do this to avoid so much
  code generation for calls.
2018-08-08 15:22:28 +02:00
Andy Wingo
b7dbc7251f Minor optimization in RESET_FRAME
* libguile/vm-engine.c (RESET_FRAME): Remove update of sp_min_since_gc.
2018-08-08 14:48:32 +02:00
Andy Wingo
0d00164f8a Add missing include to adapt to recent merge from master
* libguile/r6rs-ports.c: Add syscalls.h include for scm_from_off_t.
2018-08-07 12:57:30 +02:00
Andy Wingo
41100f7786 Merge branch 'master' into lightning
This includes a manual cherry-pick of relevant stable-2.2 commits up to
4c91de3e45.
2018-08-07 12:43:25 +02:00
Ludovic Courtès
04aa6d1fb7 r6rs-ports: 'put-bytevector' accepts 64-bit integers.
* libguile/r6rs-ports.c (scm_put_bytevector): Use 'size_t' for c_start,
c_count, and c_len.
2018-08-07 12:34:27 +02:00
Ludovic Courtès
0075b7f4dc r6rs-ports: Accept 'port-position' values greater than 2^32.
Reported by Ricardo Wurmus <rekado@elephly.net>.
Fixes <https://bugs.gnu.org/32161>.

* libguile/r6rs-ports.c (custom_binary_port_seek): Use 'scm_to_off_t'
instead of 'scm_to_int'.
* test-suite/tests/r6rs-ports.test ("8.2.7 Input Ports")["custom binary
input port position, long offset"]: New test.
2018-08-07 12:33:47 +02:00
Ludovic Courtès
8840ee5a3c vm: Fix stack-marking bug in multi-threaded programs.
Fixes <https://bugs.gnu.org/28211>.

* libguile/vm-engine.c (call, call_label, handle_interrupts): Add
'new_fp' variable; set the dynamic link and return address of the frame
at NEW_FP before setting 'vp->fp'.  This fixes a bug whereby, in a
multi-threaded context, the stack-marking code could run after vp->fp
has been set but before its dynamic link has been set, leading the
stack-walking code in 'scm_i_vm_mark_stack' to exit early on.
2018-08-07 12:31:11 +02:00
Ludovic Courtès
6a95c8e77a GDB support: Add 'guile-backtrace' command.
* libguile/libguile-2.2-gdb.scm <top level>: Add 'register-command!' call.
2018-08-07 12:14:05 +02:00
Ludovic Courtès
90fe975188 GDB support: Add note about (gdb frame-filters).
* libguile/libguile-2.2-gdb.scm <top level>: Add comment.
2018-08-07 12:13:58 +02:00
Ludovic Courtès
6e57d0d56e GDB support: Fix 'display-vm-frames'.
Previously 'vm-frame-older' would fail to traverse the chain of frames.

* libguile/libguile-2.2-gdb.scm (uint-type): New variable
(vm-frame): Fix "saved ip" and "saved fp" computation.  The latter had
been broken roughly since commit
72353de77d.
(vm-frame-older): Return #f when IP is zero, not when FP is zero.
(vm-frame-function-name): Wrap 'vm-frame-program-debug-info' in
'false-if-exception'
2018-08-07 12:13:52 +02:00
Ludovic Courtès
ffc1b9f3ac vm: Fix another typo.
Fix typo introduced in efc33cd149.

* libguile/vm.c (return_unused_stack_to_os): Remove extra 'while'.
2018-08-07 12:13:35 +02:00
Ludovic Courtès
25c719b412 Add 'scm_to_stringn' shortcut when converting to UTF-8.
* libguile/strings.c (scm_to_stringn): Call 'scm_to_utf8_stringn' when
ENCODING == "UTF-8".
2018-08-07 12:10:32 +02:00
Ludovic Courtès
edc80bd595 Module import obarrays are accessed in a critical section.
Fixes <https://bugs.gnu.org/31879>.

* libguile/modules.c (import_obarray_mutex): New variable.
(resolve_duplicate_binding, module_imported_variable): Acquire it before
accessing an obarray.
2018-08-07 12:10:05 +02:00
Jan Smydke
4853ca3e6d get-bytevector-n and get-bytevector-n! can now read more than 4 GB
* libguile/r6rs-ports.c (scm_get_bytevector_n, scm_get_bytevector_n_x):
Turn 'c_count' and related variables into a 'size_t', and use
'scm_to_size_t' instead of 'scm_to_uint'.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2018-08-07 12:09:25 +02:00
Ludovic Courtès
4611ba2fcf vm: Fix typo when checking for 'madvise' error code.
* libguile/vm.c (return_unused_stack_to_os): Check for EAGAIN, not -EAGAIN.
2018-08-07 12:09:19 +02:00
Mark H Weaver
a44c2a679f Fix error reporting in 'load-thunk-from-memory'.
Previously 'load-thunk-from-memory' would often throw to 'system-error'
based on a stale value in 'errno', leading to incorrect error messages.

* libguile/loader.c (load_thunk_from_memory): Set 'errno' to 0 before
jumping to cleanup in the ABORT preprocessor macro, and also in the case
when 'process_dynamic_segment' reports an error.
2018-08-07 12:06:19 +02:00
Mark H Weaver
5f7213e12c Fix typo in comment within numbers.c 2018-08-07 11:41:20 +02:00
Ludovic Courtès
08041d216f build: Use 'sed' invocation compatible with BSD sed.
Fixes <https://bugs.gnu.org/30011>.
Reported by ilove zfs <ilovezfs@icloud.com>.

* libguile/Makefile.am (INSTANTIATE): Move '-i' first.
2018-08-07 11:39:35 +02:00
Ludovic Courtès
666f12c871 'select' returns empty sets upon EINTR and EAGAIN.
Fixes <https://bugs.gnu.org/30368>.

* libguile/filesys.c (scm_select): Clear READ_SET, WRITE_SET, and
EXCEPT_SET when RV < 0.
2018-08-07 11:39:11 +02:00