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

214 commits

Author SHA1 Message Date
Andy Wingo
68f13adaaf Better errors for odd-length keyword args
* libguile/vm-engine.c (bind-kwargs):
* libguile/vm.c (vm_error_kwargs_missing_value):
* libguile/eval.c (error_missing_value)
  (prepare_boot_closure_env_for_apply): Adapt to mirror VM behavior.
* libguile/keywords.c (scm_c_bind_keyword_arguments): Likewise.
* module/ice-9/eval.scm (primitive-eval): Update to error on (foo #:kw)
  with a "Keyword argument has no value" instead of the horrible "odd
  argument list length".  Also adapts to the expected args format for
  the keyword-argument-error exception printer in all cases.  Matches
  1.8 optargs behavior also.
* test-suite/standalone/test-scm-c-bind-keyword-arguments.c (test_missing_value):
  (missing_value_error_handler): Update test.
* test-suite/tests/optargs.test: Add tests.
2017-02-28 22:01:20 +01:00
Daniel Llorens
a0028723da Fix bug #25492
* libguile/vm-engine.c (BR_F64_ARITHMETIC): Fix type.
2017-02-07 12:16:16 +01:00
David Thompson
35a9059250 Add unboxed floating point comparison instructions.
* libguile/vm-engine.c (BR_F64_ARITHMETIC): New preprocessor macro.
(br_if_f64_ee, br_if_f64_lt, br_if_f64_le, br_if_f64_gt, br_if_f64_ge):
New VM instructions.
* doc/ref/vm.texi ("Unboxed Floating-Point Arithmetic"): Document them.
* module/language/cps/compile-bytecode.scm (compile-function): Emit f64
comparison instructions.
* module/language/cps/effects-analysis.scm: Define effects for f64
primcalls.
* module/language/cps/primitives.scm (*branching-primcall-arities*): Add
arities for f64 primcalls.
* module/language/cps/specialize-numbers.scm (specialize-f64-comparison):
New procedure.
(specialize-operations): Specialize f64 comparisons.
* module/system/vm/assembler.scm (emit-br-if-f64-=, emit-br-if-f64-<)
(emit-br-if-f64-<=, emit-br-if-f64->, emit-br-if-f64->=): Export.
* module/system/vm/disassembler.scm (code-annotation): Add annotations
for f64 comparison instructions.
2017-01-12 09:27:43 -05:00
Mark H Weaver
a396e14cb1 FFI: Add support for functions that set 'errno'.
Implements wishlist item <https://debbugs.gnu.org/18592>.
Requested by Frank Terbeck <ft@bewatermyfriend.org>.
Based on a proposed patch by Nala Ginrut <nalaginrut@gmail.com>.
Patch ported to 2.2 by Andy Wingo <wingo@pobox.com>.

* libguile/foreign.c (cif_to_procedure): Add 'with_errno' argument.
  If true, truncate result to only one return value.
  (scm_i_foreign_call): Separate the arguments.  Always return errno.
  (pointer_to_procedure): New static function.
  (scm_pointer_to_procedure_with_errno): New C API function, implemented
  in terms of 'pointer_to_procedure'.
  (scm_pointer_to_procedure): Reimplement in terms of
  'pointer_to_procedure', no longer bound to "pointer->procedure".  See
  below.
  (scm_i_pointer_to_procedure): New C function bound to
  "pointer->procedure" which now accepts the optional #:return-errno?
  keyword argument, implemented in terms of 'pointer_to_procedure'.
  (k_return_errno): New keyword #:return-errno?.
* libguile/foreign.h (scm_pointer_to_procedure_with_errno): Add prototype.
* doc/ref/api-foreign.texi (Dynamic FFI): Adjust documentation.
* libguile/vm-engine.c (foreign-call): Return two values.
2016-12-18 23:05:15 +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
8bd5dae8c3 Compile fluid-set! to VM opcode
* libguile/vm-engine.c (fluid-set!): Fix name of opcode to correspond
  with name of Tree-IL primitive.  Fixes compilation of fluid-set! to
  actually use the fluid-set! opcode.
* doc/ref/vm.texi (Dynamic Environment Instructions): Update.
* module/language/cps/compile-bytecode.scm (compile-function): Add
  fluid-set! case.
* module/system/vm/assembler.scm: Update export name for
  emit-fluid-set!.
2016-11-27 21:55:42 +01:00
Andy Wingo
08584310ee Inline interrupts
* libguile/async.c:
* libguile/async.h (scm_i_async_push, scm_i_async_pop): Make internally
  available.
* libguile/vm-engine.c (vm_engine): Invoke interrupts inline.  Add
  return-from-interrupt instruction.
* libguile/vm.c (vm_handle_interrupt_code): New "builtin".
2016-11-19 13:32:45 +01:00
Andy Wingo
4985ef13e6 Explicit interrupt handling in VM
* libguile/foreign.c (CODE, get_foreign_stub_code): Add explicit
  handle-interrupts and return-values calls, as foreign-call will fall
  through.
* libguile/gsubr.c (A, B, C, AB, AC, BC, ABC, SUBR_STUB_CODE)
  (scm_i_primitive_call_ip): Same.
* libguile/vm-engine.c (VM_HANDLE_INTERRUPTS): Inline into
  handle-interrupts.
  (RETURN_ONE_VALUE, RETURN_VALUE_LIST): Inline into callers, and fall
  through instead of returning.
  (BR_BINARY, BR_UNARY, BR_ARITHMETIC, BR_U64_ARITHMETIC): Remove
  conditional VM_HANDLE_INTERRUPTS, as the compiler already inserted the
  handle-interrupts calls if needed.
  (vm_engine): Remove VM_HANDLE_INTERRUPTS invocations except in the
  handle-interrupts instruction.
2016-11-17 22:13:53 +01:00
Andy Wingo
ca74e3fae5 Add handle-interrupts inst and compiler pass
* libguile/vm-engine.c (vm_engine): Remove initial VM_HANDLE_INTERRUPTS
  call; surely our caller already handled interrupts.  Add
  handle-interrupts opcode.
* am/bootstrap.am (SOURCES):
* module/Makefile.am (SOURCES): Add handle-interrupts.scm.
* module/system/vm/assembler.scm (system):
* module/language/cps/compile-bytecode.scm (compile-function):
  (lower-cps): Add handle-interrupts support.
* module/language/cps/handle-interrupts.scm: New file.
2016-11-16 22:55:45 +01:00
Andy Wingo
c957ec7ab0 Use atomics for async interrupts
* libguile/__scm.h (SCM_TICK): Always define as scm_async_tick().
* libguile/error.c (scm_syserror, scm_syserror_msg):
* libguile/fports.c (fport_read, fport_write):
* libguile/_scm.h (SCM_SYSCALL): Replace SCM_ASYNC_TICK with
  scm_async_tick ().
  (SCM_ASYNC_TICK, SCM_ASYNC_TICK_WITH_CODE)
  (SCM_ASYNC_TICK_WITH_GUARD_CODE): Remove internal definitions.  We
  inline into vm-engine.c, the only place where it matters.
* libguile/async.h:
* libguile/async.c (scm_async_tick, scm_i_setup_sleep):
  (scm_i_reset_sleep, scm_system_async_mark_for_thread):
* libguile/threads.h (struct scm_thread_wake_data):
* libguile/threads.h (scm_i_thread):
* libguile/threads.c (block_self, guilify_self_1, scm_std_select):
  Rewrite to use sequentially-consistent atomic references.
* libguile/atomics-internal.h (scm_atomic_set_pointer):
  (scm_atomic_ref_pointer): New definitions.
* libguile/finalizers.c (queue_finalizer_async): We can allocate, so
  just use scm_system_async_mark_for_thread instead of the set-cdr!
  shenanigans.
* libguile/scmsigs.c (take_signal):
* libguile/gc.c (queue_after_gc_hook): Adapt to new asyncs mechanism.
  Can't allocate but we're just manipulating the current thread when no
  other threads are running so we should be good.
* libguile/vm-engine.c (VM_HANDLE_INTERRUPTS): Inline the async_tick
  business.
2016-10-26 22:50:26 +02:00
Andy Wingo
32f309d5ce Compiler support for atomics
* doc/ref/vm.texi (Inlined Atomic Instructions): New section.
* libguile/vm-engine.c (VM_VALIDATE_ATOMIC_BOX, make-atomic-box)
  (atomic-box-ref, atomic-box-set!, atomic-box-swap!)
  (atomic-box-compare-and-swap!): New instructions.
* libguile/vm.c: Include atomic and atomics-internal.h.
  (vm_error_not_a_atomic_box): New function.
* module/ice-9/atomic.scm: Register primitives with the compiler.
* module/language/cps/compile-bytecode.scm (compile-function): Add
  support for atomic ops.
* module/language/cps/effects-analysis.scm: Add comment about why no
  effects analysis needed.
* module/language/cps/reify-primitives.scm (primitive-module): Add case
  for (ice-9 atomic).
* module/language/tree-il/primitives.scm (*effect-free-primitives*):
  (*effect+exception-free-primitives*): Add atomic-box?.
* module/system/vm/assembler.scm: Add new instructions.

* test-suite/tests/atomic.test: Test with compilation and
  interpretation.
2016-09-06 12:18:35 +02:00
Andy Wingo
49d77b1243 Add unboxed logxor on u64 values
* libguile/vm-engine.c (ulogxor): New instruction.
* module/language/cps/effects-analysis.scm (ulogxor):
* module/language/cps/slot-allocation.scm (compute-var-representations):
* module/language/cps/types.scm (ulogxor):
* module/system/vm/assembler.scm (emit-ulogxor): Add support for new
  instruction.
* doc/ref/vm.texi (Unboxed Integer Arithmetic): Document ulogxor.
2016-09-01 10:53:59 +02:00
Andy Wingo
f1c0434403 `define!' instruction returns the variable
* doc/ref/vm.texi (Top-Level Environment Instructions): Update
  documentation.
* libguile/_scm.h (SCM_OBJCODE_MINOR_VERSION): Bump, sadly.
* module/system/vm/assembler.scm (*bytecode-minor-version*): Bump.
* libguile/vm-engine.c (define!): Change to store variable in dst slot.
* module/language/tree-il/compile-cps.scm (convert):
* module/language/cps/compile-bytecode.scm (compile-function): Adapt to
  define! change.
* module/language/cps/effects-analysis.scm (current-module): Fix define!
  effects.  Incidentally here was the bug: in Guile 2.2 you can't have
  effects on different object kinds in one instruction, without
  reverting to &unknown-memory-kinds.
* test-suite/tests/compiler.test ("regression tests"): Add a test.
2016-06-21 22:40:31 +02:00
Andy Wingo
be6194e32a Fix shuffling of unboxed stack elements on 32-bit systems
* libguile/vm-engine.c (SP_REF_SLOT, SP_SET_SLOT): New defines.
  (push, pop, mov, long-mov): Move full slots.  Fixes 32-bit with
  unboxed 64-bit stack values; before when shuffling these values
  around, we were only shuffling the lower 32 bits on 32-bit platforms.
2016-06-11 14:44:59 +02:00
Andy Wingo
100b048097 VM type checking refactor
* libguile/vm-engine.c (VM_VALIDATE): Refactor some type-related
  assertions to use a common macro.
  (vector-length, vector-set!/immediate): Fix the proc mentioned in the
  error message.
2016-06-11 13:03:20 +02:00
Andy Wingo
ddce05e819 vm: Make sure IP is stored before potentially GCing.
* libguile/vm-engine.c: Add a number of SYNC_IP calls that were missing
  before calls that could GC.
2016-06-11 12:26:26 +02:00
Andy Wingo
7e502d57e0 Fix bad backtraces
* libguile/vm-engine.c (BV_REF, BV_BOUNDED_SET, BV_SET, integer->char)
  (char->integer): Use VM_ASSERT so that we save the IP before erroring
  out.
2016-06-10 15:33:09 +02:00
Andy Wingo
d1b99ea2ae Minor VM fixes
* libguile/vm-engine.c (string-ref): Unpack the index into a 64-bit
  integer.
  (br-if-u64-<-scm): Tighten up the fast path.
2016-06-10 08:23:04 +02:00
Andy Wingo
f5b9a53bd0 Add integer->char and char->integer opcodes
* libguile/vm-engine.c (integer_to_char, char_to_integer): New opcodes.
* libguile/vm.c (vm_error_not_a_char): New error case.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/slot-allocation.scm (compute-var-representations):
* module/language/cps/types.scm:
* module/language/tree-il/compile-cps.scm (convert):
* doc/ref/vm.texi (Inlined Scheme Instructions):
* module/system/vm/assembler.scm: Add support for new opcodes.
2016-05-04 12:36:41 +02:00
Andy Wingo
1d4b4ec39c Add support for comparing u64 values with SCM values
* libguile/vm-engine.c (BR_U64_SCM_COMPARISON): New helper.
  (br-if-u64-<=-scm, br-if-u64-<-scm, br-if-u64-=-scm)
  (br-if-u64->-scm, br-if-u64->=-scm): New instructions, to compare an
  untagged u64 with a tagged SCM.  Avoids many u64->scm operations.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm:
* module/language/cps/type-fold.scm:
* module/system/vm/assembler.scm:
* module/system/vm/disassembler.scm (code-annotation, compute-labels):
* module/language/cps/primitives.scm (*branching-primcall-arities*): Add
  support for new opcodes.
* module/language/cps/specialize-numbers.scm
  (specialize-u64-scm-comparison): New helper.
* module/language/cps/specialize-numbers.scm (specialize-operations):
  Specialize u64 comparisons.
* module/language/cps/types.scm (true-comparison-restrictions): New helper.
  (define-comparison-inferrer): Use the new helper.  Add support for
  u64-<-scm et al.
2015-12-03 09:01:24 +01:00
Andy Wingo
97755a1ade Small VM cleanups
* libguile/vm-engine.c (BR_U64_ARITHMETIC): No need for a second
  argument.  Adapt callers.
  (scm->u64/truncate): Remove extra SYNC_IP.
2015-12-03 08:28:22 +01:00
Andy Wingo
9514dc7b95 Add ursh/immediate and ulsh/immediate ops
* libguile/vm-engine.c (ursh/immediate, ulsh/immediate): New ops.
* module/language/cps/effects-analysis.scm:
* module/language/cps/slot-allocation.scm (compute-var-representations)
  (compute-needs-slot):
* module/language/cps/specialize-primcalls.scm (specialize-primcalls):
* module/language/cps/compile-bytecode.scm (compile-function):
* module/system/vm/assembler.scm:
* module/language/cps/types.scm: Add support for new ops, and specialize
  ursh and ulsh.
2015-12-02 22:11:19 +01:00
Andy Wingo
3d6dd2f81c Add untagged bitwise operations
* libguile/vm-engine.c (ulogand, ulogior, ulogsub, ulsh, ursh)
  (scm->u64/truncate): New ops.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm:
* module/language/cps/slot-allocation.scm (compute-var-representations):
* module/language/cps/specialize-primcalls.scm (specialize-primcalls):
* module/language/cps/types.scm:
* module/language/cps/utils.scm (compute-constant-values):
* module/system/vm/assembler.scm: Wire up support for the new ops.
2015-12-02 08:45:30 +01:00
Andy Wingo
82085252ec Add logsub op.
* libguile/vm-engine.c (logsub): New op.
* module/language/cps/effects-analysis.scm (logsub):
* module/language/cps/types.scm (logsub):
* module/system/vm/assembler.scm (system): Add support for the new op.

* module/language/tree-il/compile-cps.scm (canonicalize):
  Rewrite (logand x (lognot y)) to (logsub x y).
2015-12-01 15:42:24 +01:00
Andy Wingo
8c75a5eb1b Add current-thread VM op
* libguile/vm-engine.c (current-thread): New op.
* module/language/cps/effects-analysis.scm (&thread): New memory kind.

* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm (current-thread):
* module/language/cps/types.scm (current-thread):
* module/language/tree-il/primitives.scm (*interesting-primitive-names*):
* module/system/vm/assembler.scm (emit-current-thread): Wire up the new
  op.
2015-12-01 15:42:24 +01:00
Andy Wingo
c3240d09b2 Unbox indexes of vectors, strings, and structs
* libguile/vm-engine.c (string-length, string-ref)
  (make-vector, vector-ref, vector-set!)
  (allocate-struct, struct-ref, struct-set!): Take indexes and return
  lengths as untagged u64 values.

* libguile/vm.c (vm_error_not_a_string): New helper.

* module/language/tree-il/compile-cps.scm (convert):
* module/language/cps/constructors.scm (inline-vector):
* module/language/cps/closure-conversion.scm (convert-one): Untag
  arguments to {string,vector,struct}-{ref,set!}, make-vector, and
  allocate-struct.  Tag return values from {string,vector}-length.

* module/language/cps/slot-allocation.scm (compute-var-representations):
  vector-length and string-length define u64 slots.

* module/language/cps/effects-analysis.scm: make-vector no longer causes
  a &type-check effect.

* module/language/cps/types.scm: Update to expect &u64 values for
  lengths and indexes.
2015-12-01 15:42:24 +01:00
Andy Wingo
a08b3d40f8 Untag values and indexes for all bytevector instructions
* libguile/vm-engine.c (bv-s8-ref, bv-s16-ref, bv-s32-ref, bv-s64-ref):
  Unbox index and return unboxed S32 value.
  (bv-s8-set!, bv-s16-set!, bv-s32-set!, bv-s64-set!): Unbox index and
  take unboxed S32 value.
  (bv-u8-ref, bv-u16-ref, bv-u32-ref, bv-u64-ref)
  (bv-s8-set!, bv-s16-set!, bv-s32-set!, bv-s64-set!): Likewise, but
  with unsigned values.
  (bv-f32-ref, bv-f32-set!, bv-f64-ref, bv-f64-set!): Use memcpy to
  access the value so we don't have to think about alignment.  GCC will
  inline this to a single instruction on architectures that support
  unaligned access.
* libguile/vm.c (vm_error_out_of_range_uint64)
  (vm_error_out_of_range_int64): New helpers.

* module/language/cps/slot-allocation.scm (compute-var-representations):
  All bytevector ref operations produce untagged values.

* module/language/cps/types.scm (define-bytevector-accessors): Update
  for bytevector untagged indices and values.

* module/language/cps/utils.scm (compute-constant-values): Fix s64
  case.

* module/language/tree-il/compile-cps.scm (convert): Box results of all
  bytevector accesses, and unbox incoming indices and values.
2015-12-01 15:42:24 +01:00
Andy Wingo
8bf77f7192 Add support for unboxed s64 values
* libguile/frames.c (enum stack_item_representation):
  (scm_to_stack_item_representation):
  (scm_frame_local_ref, scm_frame_local_set_x): Support for S64
  representations.

* libguile/frames.h (union scm_vm_stack_element): Add signed 64-bit
  integer field.

* libguile/vm-engine.c (scm->s64, s64->scm, load-s64): New
  instructions.

* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/cse.scm (compute-equivalent-subexpressions):
* module/language/cps/effects-analysis.scm:
* module/language/cps/slot-allocation.scm (compute-var-representations)
  (compute-needs-slot, allocate-slots):
* module/language/cps/utils.scm (compute-constant-values):
* module/language/cps/specialize-primcalls.scm (specialize-primcalls):
  Add support for new primcalls.

* module/language/cps/types.scm (&s64): New type.
  (&s64-min, &s64-max, &u64-max): New convenience definitions.
  (&range-min, &range-max): Use &s64-min and &u64-max names.
  (scm->s64, load-s64, s64->scm): Add support for new primcalls.

* module/system/vm/assembler.scm (emit-scm->s64, emit-s64->scm)
  (emit-load-s64): New exports.
* module/system/vm/assembler.scm (write-arities): Support for s64
  slots.

* module/system/vm/debug.scm (arity-definitions): Support for s64
  slots.
2015-12-01 15:42:19 +01:00
Andy Wingo
f34688ad25 New instructions load-f64, load-u64
* libguile/instructions.c (FOR_EACH_INSTRUCTION_WORD_TYPE): Add word
  types for immediate f64 and u64 values.
  (TYPE_WIDTH): Bump up by a bit, now that we have 32 word types.
  (NOP, parse_instruction): Use 64-bit meta type.

* libguile/vm-engine.c (load-f64, load-u64): New instructions.

* module/language/bytecode.scm (compute-instruction-arity): Add parser
  for new instruction word types.

* module/language/cps/compile-bytecode.scm (compile-function): Add
  special-cased assemblers for new instructions, and also for scm->u64
  and u64->scm which I missed before.

* module/language/cps/effects-analysis.scm (load-f64, load-u64): New
  instructions.

* module/language/cps/slot-allocation.scm (compute-needs-slot): load-f64
  and load-u64 don't need slots.
  (compute-var-representations): Update for new instructions.

* module/language/cps/specialize-primcalls.scm (specialize-primcalls):
  Specialize scm->f64 and scm->u64 to make-f64 and make-u64.

* module/language/cps/types.scm (load-f64, load-u64): Wire up to type
  inference, though currently type inference only runs before
  specialization.

* module/language/cps/utils.scm (compute-defining-expressions): For some
  reason I don't understand, it's possible to see two definitions that
  are equal but not equal? here.  Allow for now.
  (compute-constant-values): Punch through type conversions to get
  constant u64/f64 values.

* module/system/vm/assembler.scm (assembler): Support for new word
  types.  Export the new assemblers.
2015-12-01 11:30:55 +01:00
Andy Wingo
bdfa1c1b42 Add tagged and untagged arithmetic ops with immediate operands
* libguile/vm-engine.c (add/immediate, sub/immediate)
  (uadd/immediate, usub/immediate, umul/immediate): New instructions.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/slot-allocation.scm (compute-needs-slot):
* module/language/cps/types.scm:
* module/system/vm/assembler.scm (system):
* module/language/cps/effects-analysis.scm: Support
  for new instructions.

* module/language/cps/optimize.scm (optimize-first-order-cps): Move
  primcall specialization to the last step -- the only benefit of doing
  it earlier was easier reasoning about side effects, and we're already
  doing that in a more general way with (language cps types).
* module/language/cps/specialize-primcalls.scm (specialize-primcalls):
  Specialize add and sub to add/immediate and sub/immediate, and
  specialize u64 addition as well.  U64 specialization doesn't work now
  though because computing constant values doesn't work for U64s; oh
  well.
2015-12-01 11:30:55 +01:00
Andy Wingo
8f18b71b7a Remove add1 and sub1
* libguile/vm-engine.c: Remove add1 and sub1 instructions.  Will replace
  with add/immediate and sub/immediate.
* module/language/tree-il/peval.scm (peval): If we reify a new
  <primcall>, expand it.  Removes 1- and similar primcalls.
* module/language/tree-il/primitives.scm: Don't specialize (+ x 1) to 1+.
  (expand-primcall): New export, does a single primcall expansion.
  (expand-primitives): Use the new helper.
* module/language/cps/effects-analysis.scm:
* module/language/cps/primitives.scm:
* module/language/cps/types.scm:
* module/system/vm/assembler.scm: Remove support for add1 and sub1 CPS
  primitives.
* test-suite/tests/peval.test ("partial evaluation"): Adapt tests that
  expect 1+/1- to expect +/-.
2015-12-01 11:30:55 +01:00
Andy Wingo
d294d5d1e1 Add unsigned 64-bit arithmetic operators: uadd, usub, umul
* libguile/vm-engine.c (uadd, usub, umul): New ops.
* module/language/cps/effects-analysis.scm (uadd, usub, umul): Add
  effects analysis.
* module/language/cps/slot-allocation.scm (compute-var-representations):
  The new ops define 'u64 values.
* module/language/cps/types.scm (uadd, usub, umul): Add type checkers
  and inferrers.
* module/system/vm/assembler.scm (emit-uadd, emit-usub, emit-umul): New
  assemblers.
2015-12-01 11:30:55 +01:00
Andy Wingo
07607f66b8 Add instructions to branch on u64 comparisons
* libguile/vm-engine.c (BR_U64_ARITHMETIC): New helper.
  (br-if-u64-=, br-if-u64-<, br-if-u64->=): New instructions.

* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm:
* module/language/cps/primitives.scm (*branching-primcall-arities*):
* module/language/cps/type-fold.scm:
* module/language/cps/types.scm (u64-=, infer-u64-comparison-ranges):
  (define-u64-comparison-inferrer, u64-<, u64-<=, u64->=, u64->):
* module/system/vm/assembler.scm:
* module/system/vm/disassembler.scm (code-annotation):
  (compute-labels): Compiler and toolchain support for the new
  instructions.
2015-12-01 11:30:54 +01:00
Andy Wingo
87cc8b0f97 bv-f{32,64}-{ref,set!} take unboxed u64 index
* module/language/tree-il/compile-cps.scm (convert): bv-f32-ref,
  bv-f32-set!, bv-f64-ref, and bv-f64-set! take the index as an untagged
  u64 value.
* module/language/cps/types.scm (define-bytevector-uaccessors): New
  helper, used while migrating bytevectors to take unboxed indexes.
  Adapt f32/f64 accessors to use this definition helper.
* libguile/vm-engine.c (BV_FLOAT_REF, BV_FLOAT_SET): The index is
  unboxed.
2015-12-01 11:30:54 +01:00
Andy Wingo
8464cc576c Add bv-length instruction
* libguile/vm-engine.c (bv-length): New instruction.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm (bv-length):
* module/language/cps/primitives.scm (*instruction-aliases*):
* module/language/cps/slot-allocation.scm (compute-var-representations):
* module/language/cps/types.scm (bv-length):
* module/language/tree-il/compile-cps.scm (convert): Add support for
  bv-length.
* module/system/vm/assembler.scm: Export emit-bv-length.
2015-12-01 11:30:54 +01:00
Andy Wingo
dfbe869e24 Add low-level support for unboxed 64-bit unsigned ints
* libguile/frames.c (enum stack_item_representation)
* libguile/frames.c (scm_to_stack_item_representation):
  (scm_frame_local_ref, scm_frame_local_set_x): Support 'u64 slots.
* libguile/frames.h (union scm_vm_stack_element): Add as_u64 member.

* libguile/vm-engine.c (SP_REF_U64, SP_SET_U64): New helpers.
  (scm->u64, u64->scm): New instructions.

* module/language/cps/cse.scm (compute-equivalent-subexpressions):
  Scalar replacement for u64->scm and scm->u64.

* module/language/cps/effects-analysis.scm (scm->u64, u64->scm): Add
  cases.

* module/language/cps/slot-allocation.scm (compute-var-representations):
  (allocate-slots): Represent the result of scm->u64 as a "u64" slot.

* module/language/cps/types.scm (&u64): New type.
  (scm->u64, u64->scm): Add support for these ops.

* module/system/vm/assembler.scm (write-arities):
* module/system/vm/debug.scm (arity-definitions): Support u64
  representations.
2015-12-01 11:30:54 +01:00
Andy Wingo
3b3405e504 Apply of non-programs has IP that is not from prev frame
* libguile/vm-engine.c (vm_engine)
* libguile/vm.c (vm_apply_non_program_code): Arrange so that the code to
  apply a non-program has its own IP, so that frame-instruction-pointer
  for a non-program application doesn't point into the previously active
  frame.
2015-12-01 11:30:45 +01:00
Andy Wingo
d729a0dc75 Remove br-if-equal opcode
* libguile/vm-engine.c (br-if-equal): Remove opcode.
2015-11-12 21:23:09 +01:00
Andy Wingo
3b4941f3a9 Add fadd, fsub, fmul, fdiv instructions
* libguile/vm-engine.c (fadd, fsub, fmul, fdiv): New instructions.

* module/language/cps/effects-analysis.scm:
* module/language/cps/types.scm: Wire up support for new instructions.

* module/system/vm/assembler.scm: Export emit-fadd and friends.
2015-11-11 10:21:28 +01:00
Andy Wingo
b1ac8d68b5 bv-{f32,f64}-{ref,set!} operate on raw f64 values
* module/language/tree-il/compile-cps.scm (convert): Box results of
  bv-f32-ref and bv-f64-ref.  Unbox the argument to bv-f32-set! and
  bv-f64-set!.

* libguile/vm-engine.c (bv-f32-ref, bv-f64-ref): Results are raw.
  (bv-f32-set!, bv-f64-set!): Take unboxed arguments.

* module/system/vm/assembler.scm (emit-scm->f64, emit-f64->scm):
  Export.

* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm: Add support for scm->f64 and
  f64->scm.

* module/language/cps/slot-allocation.scm (compute-var-representations):
  Add cases for primops returning raw values.

* module/language/cps/types.scm (bv-f32-ref, bv-f32-set!)
  (bv-f64-ref, bv-f64-set!): Deal in &f64 values instead of reals.
2015-11-11 10:20:12 +01:00
Andy Wingo
5bbc47b06d Add VM ops to pack and unpack raw f64 values.
* libguile/vm-engine.c (scm->f64, f64->scm): New ops.
2015-10-28 17:58:30 +00:00
Andy Wingo
95855087ec Remove return opcode
* libguile/vm-engine.c (return): Remove opcode.
2015-10-28 13:11:40 +00:00
Andy Wingo
7aee3c74f5 return-values opcode resets the frame
* libguile/vm-engine.c (return-values): Change to also reset the frame,
  if nlocals is nonzero.

* doc/ref/vm.texi (Procedure Call and Return Instructions): Updated
  docs.

* module/language/cps/compile-bytecode.scm (compile-function): Adapt to
  call emit-return-values with the right number of arguments.
2015-10-28 10:47:18 +00:00
Andy Wingo
8832e8b68c Small subr-call refactor
* libguile/gsubr.c (scm_apply_subr): New internal helper.
* libguile/vm-engine.c (subr-call): Call out to scm_apply_subr.
* doc/ref/vm.texi (subr-call): Don't specify how the foreign pointer is
  obtained.
2015-10-22 12:13:37 +00:00
Andy Wingo
9144f50c31 subr-call implementation simplification
* libguile/vm-engine.c (subr-call): Reference args from SP, not FP.
2015-10-22 11:35:07 +00:00
Andy Wingo
70c317ab51 SP-relative local addressing
* libguile/vm-engine.c: S24/S12/S8 operands addressed relative to the
  SP, not the FP.  Cache the SP instead of a FP-relative locals
  pointer.  Further cleanups to follow.

* libguile/vm.c (vm_builtin_call_with_values_code): Adapt to mov operand
  addresing change.

* module/language/cps/compile-bytecode.scm (compile-function): Reify
  SP-relative local indexes where appropriate.

* module/system/vm/assembler.scm (emit-fmov*): New helper, exported as
  emit-fmov.
  (shuffling-assembler, define-shuffling-assembler): Rewrite to shuffle
  via push/pop/drop.
  (standard-prelude, opt-prelude, kw-prelude): No need to provide for
  shuffling args.

* test-suite/tests/rtl.test: Update.

* module/language/cps/slot-allocation.scm: Don't reserve slots 253-255.
2015-10-21 11:49:20 +02:00
Andy Wingo
0da0308b84 Prepare for SP-addressed locals
* libguile/vm-engine.c: Renumber opcodes, and take the opportunity to
  fold recent additions into more logical places.  Be more precise when
  describing the encoding of operands, to shuffle local references only
  and not constants, immediates, or other such values.
  (SP_REF, SP_SET): New helpers.
  (BR_BINARY, BR_ARITHMETIC): Take full 24-bit operands.  Our shuffle
  strategy is to emit push when needed to bring far locals near, then
  pop afterwards, shuffling away far destination values as needed; but
  that doesn't work for conditionals, unless we introduce a trampoline.
  Let's just do the simple thing for now.  Native compilation will use
  condition codes.
  (push, pop, drop): Back from the dead!  We'll only use these for
  temporary shuffling though, when an opcode can't address the full
  24-bit range.
  (long-fmov): New instruction, like long-mov but relative to the frame
  pointer.
  (load-typed-array, make-array): Don't use a compressed encoding so
  that we can avoid the shuffling case.  It would be a pain, given that
  they have so many operands already.

* module/language/bytecode.scm (compute-instruction-arity): Update for
  new instrution word encodings.

* module/system/vm/assembler.scm: Update to expose some opcodes
  directly, without the need for shuffling wrappers.  Adapt to
  instruction word encodings change.

* module/system/vm/disassembler.scm (disassembler): Adapt to instruction
  coding change.
2015-10-21 11:49:20 +02:00
Andy Wingo
8f027385db Rename union scm_vm_stack_element members
* libguile/frames.h (union scm_vm_stack_element): Rename members from
  scm, ip, etc to as_scm, as_ip, etc.  Adapt users.
2015-10-21 11:49:20 +02:00
Andy Wingo
aa9f6b0082 VM caches address of local 0 instead of FP
* libguile/vm-engine.c (vm_engine): Cache the address of local 0 instead
  of the FP.  This makes locals access a bit cheaper, but we still have
  to negate the index.  The right fix is to index relative to the SP
  instead.  That's a more involved change, so we punt until later.
2015-10-21 11:49:20 +02:00