summaryrefslogtreecommitdiff
path: root/libguile/vm-engine.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2017-11-05 10:46:13 +0100
committerAndy Wingo <wingo@pobox.com>2017-11-05 15:00:16 +0100
commit17bd5a893835b44acbfc5ee4254813e9a6ceea25 (patch)
treee8838950c55ac1ef9e871e5c5b92b026840c4940 /libguile/vm-engine.c
parentdea84a46b476643ea0abf7133ff4bdf59c46a88e (diff)
downloadguile-17bd5a893835b44acbfc5ee4254813e9a6ceea25.tar.gz
Add lsh, rsh instructions
* libguile/vm-engine.c (lsh, rsh, lsh/immediate, rsh/immediate): New instructions taking unboxed bit counts. * module/language/cps/compile-bytecode.scm (compile-function): * module/language/cps/effects-analysis.scm: * module/language/cps/specialize-numbers.scm (specialize-f64-unop): (specialize-u64-unop): Add ability to specialize add/immediate, etc, and add lsh/immediate as well. (specialize-u64-binop, specialize-u64-shift): Move rsh/lsh specialization to its own procedure, given that the bit count is already unboxed. (specialize-operations): Adapt to support more /immediate instructions. * module/language/cps/type-fold.scm (mul): Reify an lsh/immediate instead of an ash. * module/language/cps/types.scm (compute-ash-range): Add type inferrers for lsh, rsh, and their immediate variants. * module/system/vm/assembler.scm: Export emit-lsh and so on. * module/language/tree-il/compile-cps.scm (convert): Convert "ash" on immediates to rsh/immediate or lsh/immediate.
Diffstat (limited to 'libguile/vm-engine.c')
-rw-r--r--libguile/vm-engine.c142
1 files changed, 138 insertions, 4 deletions
diff --git a/libguile/vm-engine.c b/libguile/vm-engine.c
index ffbe5c1ae..7c0a226b3 100644
--- a/libguile/vm-engine.c
+++ b/libguile/vm-engine.c
@@ -4024,15 +4024,149 @@ VM_NAME (scm_i_thread *thread, struct scm_vm *vp,
VM_DEFINE_OP (249, unused_249, NULL, NOP)
VM_DEFINE_OP (250, unused_250, NULL, NOP)
VM_DEFINE_OP (251, unused_251, NULL, NOP)
- VM_DEFINE_OP (252, unused_252, NULL, NOP)
- VM_DEFINE_OP (253, unused_253, NULL, NOP)
- VM_DEFINE_OP (254, unused_254, NULL, NOP)
- VM_DEFINE_OP (255, unused_255, NULL, NOP)
{
vm_error_bad_instruction (op);
abort (); /* never reached */
}
+ /* Temporary instructions down here, while we incrementally proceed
+ with instruction explosion. */
+
+ /* lsh dst:8 a:8 b:8
+ *
+ * Shift A left by B bits, and place the result in DST. B is a U64
+ * value.
+ */
+ VM_DEFINE_OP (252, lsh, "lsh", OP1 (X8_S8_S8_S8) | OP_DST)
+ {
+ scm_t_uint8 dst, x, y;
+ SCM a, result;
+ scm_t_uint64 b;
+
+ UNPACK_8_8_8 (op, dst, x, y);
+ a = SP_REF (x);
+ b = SP_REF_U64 (y);
+
+ if (SCM_LIKELY (SCM_I_INUMP (a))
+ && b < (scm_t_uint64) (SCM_I_FIXNUM_BIT - 1)
+ && ((scm_t_bits)
+ (SCM_SRS (SCM_I_INUM (a), (SCM_I_FIXNUM_BIT-1 - b)) + 1)
+ <= 1))
+ {
+ scm_t_signed_bits nn = SCM_I_INUM (a);
+ result = SCM_I_MAKINUM (nn < 0 ? -(-nn << b) : (nn << b));
+ }
+ else
+ {
+ SYNC_IP ();
+ /* B has to be a bignum. FIXME: use instruction explosion to
+ ensure that. */
+ result = scm_ash (a, scm_from_uint64 (b));
+ CACHE_SP ();
+ }
+ SP_SET (dst, result);
+ NEXT (1);
+ }
+ /* rsh dst:8 a:8 b:8
+ *
+ * Shift A right by B bits, and place the result in DST. B is a U64
+ * value.
+ */
+ VM_DEFINE_OP (253, rsh, "rsh", OP1 (X8_S8_S8_S8) | OP_DST)
+ {
+ scm_t_uint8 dst, x, y;
+ SCM a, result;
+ scm_t_uint64 b;
+
+ UNPACK_8_8_8 (op, dst, x, y);
+ a = SP_REF (x);
+ b = SP_REF_U64 (y);
+
+ if (SCM_LIKELY (SCM_I_INUMP (a)))
+ {
+ if (b > (scm_t_uint64) (SCM_I_FIXNUM_BIT - 1))
+ b = SCM_I_FIXNUM_BIT - 1;
+ result = SCM_I_MAKINUM (SCM_SRS (SCM_I_INUM (a), b));
+ }
+ else
+ {
+ SYNC_IP ();
+ /* B has to be a bignum. FIXME: use instruction explosion to
+ ensure that. */
+ result = scm_ash (a, scm_difference (SCM_INUM0, scm_from_uint64 (b)));
+ CACHE_SP ();
+ }
+ SP_SET (dst, result);
+ NEXT (1);
+ }
+ /* lsh/immediate dst:8 a:8 b:8
+ *
+ * Shift A left by B bits, and place the result in DST. B is an
+ * immediate unsigned integer.
+ */
+ VM_DEFINE_OP (254, lsh_immediate, "lsh/immediate", OP1 (X8_S8_S8_C8) | OP_DST)
+ {
+ scm_t_uint8 dst, x, y;
+ SCM a, result;
+ unsigned int b;
+
+ UNPACK_8_8_8 (op, dst, x, y);
+ a = SP_REF (x);
+ b = y;
+
+ if (SCM_LIKELY (SCM_I_INUMP (a))
+ && b < (unsigned int) (SCM_I_FIXNUM_BIT - 1)
+ && ((scm_t_bits)
+ (SCM_SRS (SCM_I_INUM (a), (SCM_I_FIXNUM_BIT-1 - b)) + 1)
+ <= 1))
+ {
+ scm_t_signed_bits nn = SCM_I_INUM (a);
+ result = SCM_I_MAKINUM (nn < 0 ? -(-nn << b) : (nn << b));
+ }
+ else
+ {
+ SYNC_IP ();
+ /* B has to be a bignum. FIXME: use instruction explosion to
+ ensure that. */
+ result = scm_ash (a, SCM_I_MAKINUM (b));
+ CACHE_SP ();
+ }
+ SP_SET (dst, result);
+ NEXT (1);
+ }
+ /* rsh dst:8 a:8 b:8
+ *
+ * Shift A right by B bits, and place the result in DST. B is an
+ * immediate unsigned integer.
+ */
+ VM_DEFINE_OP (255, rsh_immediate, "rsh/immediate", OP1 (X8_S8_S8_C8) | OP_DST)
+ {
+ scm_t_uint8 dst, x, y;
+ SCM a, result;
+ int b;
+
+ UNPACK_8_8_8 (op, dst, x, y);
+ a = SP_REF (x);
+ b = y;
+
+ if (SCM_LIKELY (SCM_I_INUMP (a)))
+ {
+ if (b > (int) (SCM_I_FIXNUM_BIT - 1))
+ b = SCM_I_FIXNUM_BIT - 1;
+ result = SCM_I_MAKINUM (SCM_SRS (SCM_I_INUM (a), b));
+ }
+ else
+ {
+ SYNC_IP ();
+ /* B has to be a bignum. FIXME: use instruction explosion to
+ ensure that. */
+ result = scm_ash (a, SCM_I_MAKINUM (-b));
+ CACHE_SP ();
+ }
+ SP_SET (dst, result);
+ NEXT (1);
+ }
+
END_DISPATCH_SWITCH;
}