1 /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 /* This file is included in vm_engine.c */
26 #define ARGS1(a1) SCM a1 = sp[0];
27 #define ARGS2(a1,a2) SCM a1 = sp[-1], a2 = sp[0]; sp--; NULLSTACK (1);
28 #define ARGS3(a1,a2,a3) SCM a1 = sp[-2], a2 = sp[-1], a3 = sp[0]; sp -= 2; NULLSTACK (2);
30 #define RETURN(x) do { *sp = x; NEXT; } while (0)
32 VM_DEFINE_FUNCTION (128, not, "not", 1)
35 RETURN (scm_from_bool (scm_is_false (x
)));
38 VM_DEFINE_FUNCTION (129, not_not
, "not-not", 1)
41 RETURN (scm_from_bool (!scm_is_false (x
)));
44 VM_DEFINE_FUNCTION (130, eq
, "eq?", 2)
47 RETURN (scm_from_bool (scm_is_eq (x
, y
)));
50 VM_DEFINE_FUNCTION (131, not_eq, "not-eq?", 2)
53 RETURN (scm_from_bool (!scm_is_eq (x
, y
)));
56 VM_DEFINE_FUNCTION (132, nullp
, "null?", 1)
59 RETURN (scm_from_bool (scm_is_null (x
)));
62 VM_DEFINE_FUNCTION (133, not_nullp
, "not-null?", 1)
65 RETURN (scm_from_bool (!scm_is_null (x
)));
68 VM_DEFINE_FUNCTION (134, eqv
, "eqv?", 2)
73 if (SCM_IMP (x
) || SCM_IMP (y
))
76 RETURN (scm_eqv_p (x
, y
));
79 VM_DEFINE_FUNCTION (135, equal
, "equal?", 2)
84 if (SCM_IMP (x
) || SCM_IMP (y
))
87 RETURN (scm_equal_p (x
, y
));
90 VM_DEFINE_FUNCTION (136, pairp
, "pair?", 1)
93 RETURN (scm_from_bool (scm_is_pair (x
)));
96 VM_DEFINE_FUNCTION (137, listp
, "list?", 1)
99 RETURN (scm_from_bool (scm_ilength (x
) >= 0));
102 VM_DEFINE_FUNCTION (138, symbolp
, "symbol?", 1)
105 RETURN (scm_from_bool (scm_is_symbol (x
)));
108 VM_DEFINE_FUNCTION (139, vectorp
, "vector?", 1)
111 RETURN (scm_from_bool (SCM_I_IS_VECTOR (x
)));
119 VM_DEFINE_FUNCTION (140, cons
, "cons", 2)
126 #define VM_VALIDATE_CONS(x, proc) \
127 if (SCM_UNLIKELY (!scm_is_pair (x))) \
128 { func_name = proc; \
130 goto vm_error_not_a_pair; \
133 VM_DEFINE_FUNCTION (141, car
, "car", 1)
136 VM_VALIDATE_CONS (x
, "car");
137 RETURN (SCM_CAR (x
));
140 VM_DEFINE_FUNCTION (142, cdr
, "cdr", 1)
143 VM_VALIDATE_CONS (x
, "cdr");
144 RETURN (SCM_CDR (x
));
147 VM_DEFINE_INSTRUCTION (143, set_car
, "set-car!", 0, 2, 0)
152 VM_VALIDATE_CONS (x
, "set-car!");
157 VM_DEFINE_INSTRUCTION (144, set_cdr
, "set-cdr!", 0, 2, 0)
162 VM_VALIDATE_CONS (x
, "set-cdr!");
169 * Numeric relational tests
173 #define REL(crel,srel) \
176 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
177 RETURN (scm_from_bool ((scm_t_signed_bits) (x) \
178 crel (scm_t_signed_bits) (y))); \
180 RETURN (srel (x, y)); \
183 VM_DEFINE_FUNCTION (145, ee
, "ee?", 2)
185 REL (==, scm_num_eq_p
);
188 VM_DEFINE_FUNCTION (146, lt
, "lt?", 2)
193 VM_DEFINE_FUNCTION (147, le
, "le?", 2)
198 VM_DEFINE_FUNCTION (148, gt
, "gt?", 2)
203 VM_DEFINE_FUNCTION (149, ge
, "ge?", 2)
213 /* The maximum/minimum tagged integers. */
216 #define INUM_MAX (INTPTR_MAX - 1)
217 #define INUM_MIN (INTPTR_MIN + scm_tc2_int)
220 #define FUNC2(CFUNC,SFUNC) \
223 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
225 scm_t_int64 n = SCM_I_INUM (x) CFUNC SCM_I_INUM (y);\
226 if (SCM_FIXABLE (n)) \
227 RETURN (SCM_I_MAKINUM (n)); \
230 RETURN (SFUNC (x, y)); \
233 /* Assembly tagged integer arithmetic routines. This code uses the
234 `asm goto' feature introduced in GCC 4.5. */
236 #if defined __x86_64__ && SCM_GNUC_PREREQ (4, 5)
238 /* The macros below check the CPU's overflow flag to improve fixnum
239 arithmetic. The %rcx register is explicitly clobbered because `asm
240 goto' can't have outputs, in which case the `r' constraint could be
241 used to let the register allocator choose a register.
243 TODO: Use `cold' label attribute in GCC 4.6.
244 http://gcc.gnu.org/ml/gcc-patches/2010-10/msg01777.html */
246 # define ASM_ADD(x, y) \
248 asm volatile goto ("mov %1, %%rcx; " \
249 "test %[tag], %%cl; je %l[slow_add]; " \
250 "test %[tag], %0; je %l[slow_add]; " \
251 "add %0, %%rcx; jo %l[slow_add]; " \
252 "sub %[tag], %%rcx; " \
253 "mov %%rcx, (%[vsp])\n" \
255 : "r" (x), "r" (y), \
256 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
264 # define ASM_SUB(x, y) \
266 asm volatile goto ("mov %0, %%rcx; " \
267 "test %[tag], %%cl; je %l[slow_sub]; " \
268 "test %[tag], %1; je %l[slow_sub]; " \
269 "sub %1, %%rcx; jo %l[slow_sub]; " \
270 "add %[tag], %%rcx; " \
271 "mov %%rcx, (%[vsp])\n" \
273 : "r" (x), "r" (y), \
274 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
285 VM_DEFINE_FUNCTION (150, add
, "add", 2)
293 RETURN (scm_sum (x
, y
));
297 VM_DEFINE_FUNCTION (151, add1
, "add1", 1)
301 /* Check for overflow. */
302 if (SCM_LIKELY ((scm_t_intptr
) x
< INUM_MAX
))
306 /* Add the integers without untagging. */
307 result
= SCM_PACK ((scm_t_intptr
) x
308 + (scm_t_intptr
) SCM_I_MAKINUM (1)
311 if (SCM_LIKELY (SCM_I_INUMP (result
)))
316 RETURN (scm_sum (x
, SCM_I_MAKINUM (1)));
319 VM_DEFINE_FUNCTION (152, sub
, "sub", 2)
322 FUNC2 (-, scm_difference
);
327 RETURN (scm_difference (x
, y
));
331 VM_DEFINE_FUNCTION (153, sub1
, "sub1", 1)
335 /* Check for underflow. */
336 if (SCM_LIKELY ((scm_t_intptr
) x
> INUM_MIN
))
340 /* Substract the integers without untagging. */
341 result
= SCM_PACK ((scm_t_intptr
) x
342 - (scm_t_intptr
) SCM_I_MAKINUM (1)
345 if (SCM_LIKELY (SCM_I_INUMP (result
)))
350 RETURN (scm_difference (x
, SCM_I_MAKINUM (1)));
356 VM_DEFINE_FUNCTION (154, mul
, "mul", 2)
360 RETURN (scm_product (x
, y
));
363 VM_DEFINE_FUNCTION (155, div
, "div", 2)
367 RETURN (scm_divide (x
, y
));
370 VM_DEFINE_FUNCTION (156, quo
, "quo", 2)
374 RETURN (scm_quotient (x
, y
));
377 VM_DEFINE_FUNCTION (157, rem
, "rem", 2)
381 RETURN (scm_remainder (x
, y
));
384 VM_DEFINE_FUNCTION (158, mod
, "mod", 2)
388 RETURN (scm_modulo (x
, y
));
391 VM_DEFINE_FUNCTION (159, ash
, "ash", 2)
394 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
396 if (SCM_I_INUM (y
) < 0)
397 /* Right shift, will be a fixnum. */
398 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) >> -SCM_I_INUM (y
)));
400 /* Left shift. See comments in scm_ash. */
402 scm_t_signed_bits nn
, bits_to_shift
;
405 bits_to_shift
= SCM_I_INUM (y
);
407 if (bits_to_shift
< SCM_I_FIXNUM_BIT
-1
409 (SCM_SRS (nn
, (SCM_I_FIXNUM_BIT
-1 - bits_to_shift
)) + 1)
411 RETURN (SCM_I_MAKINUM (nn
<< bits_to_shift
));
417 RETURN (scm_ash (x
, y
));
420 VM_DEFINE_FUNCTION (160, logand
, "logand", 2)
423 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
424 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) & SCM_I_INUM (y
)));
426 RETURN (scm_logand (x
, y
));
429 VM_DEFINE_FUNCTION (161, logior
, "logior", 2)
432 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
433 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) | SCM_I_INUM (y
)));
435 RETURN (scm_logior (x
, y
));
438 VM_DEFINE_FUNCTION (162, logxor
, "logxor", 2)
441 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
442 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) ^ SCM_I_INUM (y
)));
444 RETURN (scm_logxor (x
, y
));
452 VM_DEFINE_FUNCTION (163, vector_ref
, "vector-ref", 2)
454 scm_t_signed_bits i
= 0;
456 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect
)
458 && ((i
= SCM_I_INUM (idx
)) >= 0)
459 && i
< SCM_I_VECTOR_LENGTH (vect
)))
460 RETURN (SCM_I_VECTOR_ELTS (vect
)[i
]);
464 RETURN (scm_vector_ref (vect
, idx
));
468 VM_DEFINE_INSTRUCTION (164, vector_set
, "vector-set", 0, 3, 0)
470 scm_t_signed_bits i
= 0;
472 POP (val
); POP (idx
); POP (vect
);
473 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect
)
475 && ((i
= SCM_I_INUM (idx
)) >= 0)
476 && i
< SCM_I_VECTOR_LENGTH (vect
)))
477 SCM_I_VECTOR_WELTS (vect
)[i
] = val
;
481 scm_vector_set_x (vect
, idx
, val
);
486 VM_DEFINE_INSTRUCTION (165, make_array
, "make-array", 3, -1, 1)
492 len
= (len
<< 8) + FETCH ();
493 len
= (len
<< 8) + FETCH ();
496 PRE_CHECK_UNDERFLOW (len
);
497 ret
= scm_from_contiguous_array (shape
, sp
- len
+ 1, len
);
507 #define VM_VALIDATE_STRUCT(obj, proc) \
508 if (SCM_UNLIKELY (!SCM_STRUCTP (obj))) \
511 finish_args = (obj); \
512 goto vm_error_not_a_struct; \
515 VM_DEFINE_FUNCTION (166, struct_p
, "struct?", 1)
518 RETURN (scm_from_bool (SCM_STRUCTP (obj
)));
521 VM_DEFINE_FUNCTION (167, struct_vtable
, "struct-vtable", 1)
524 VM_VALIDATE_STRUCT (obj
, "struct_vtable");
525 RETURN (SCM_STRUCT_VTABLE (obj
));
528 VM_DEFINE_INSTRUCTION (168, make_struct
, "make-struct", 2, -1, 1)
530 unsigned h
= FETCH ();
531 unsigned l
= FETCH ();
532 scm_t_bits n
= ((h
<< 8U) + l
);
533 SCM vtable
= sp
[-(n
- 1)];
534 const SCM
*inits
= sp
- n
+ 2;
539 if (SCM_LIKELY (SCM_STRUCTP (vtable
)
540 && SCM_VTABLE_FLAG_IS_SET (vtable
, SCM_VTABLE_FLAG_SIMPLE
)
541 && (SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
) + 1
543 && !SCM_VTABLE_INSTANCE_FINALIZER (vtable
)))
545 /* Verily, we are making a simple struct with the right number of
546 initializers, and no finalizer. */
547 ret
= scm_words ((scm_t_bits
)SCM_STRUCT_DATA (vtable
) | scm_tc3_struct
,
549 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)SCM_CELL_OBJECT_LOC (ret
, 2));
550 memcpy (SCM_STRUCT_DATA (ret
), inits
, (n
- 1) * sizeof (SCM
));
553 ret
= scm_c_make_structv (vtable
, 0, n
- 1, (scm_t_bits
*) inits
);
561 VM_DEFINE_FUNCTION (169, struct_ref
, "struct-ref", 2)
565 if (SCM_LIKELY (SCM_STRUCTP (obj
)
566 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
567 SCM_VTABLE_FLAG_SIMPLE
)
568 && SCM_I_INUMP (pos
)))
571 scm_t_bits index
, len
;
573 /* True, an inum is a signed value, but cast to unsigned it will
574 certainly be more than the length, so we will fall through if
575 index is negative. */
576 index
= SCM_I_INUM (pos
);
577 vtable
= SCM_STRUCT_VTABLE (obj
);
578 len
= SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
);
580 if (SCM_LIKELY (index
< len
))
582 scm_t_bits
*data
= SCM_STRUCT_DATA (obj
);
583 RETURN (SCM_PACK (data
[index
]));
588 RETURN (scm_struct_ref (obj
, pos
));
591 VM_DEFINE_FUNCTION (170, struct_set
, "struct-set", 3)
593 ARGS3 (obj
, pos
, val
);
595 if (SCM_LIKELY (SCM_STRUCTP (obj
)
596 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
597 SCM_VTABLE_FLAG_SIMPLE
)
598 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
599 SCM_VTABLE_FLAG_SIMPLE_RW
)
600 && SCM_I_INUMP (pos
)))
603 scm_t_bits index
, len
;
605 /* See above regarding index being >= 0. */
606 index
= SCM_I_INUM (pos
);
607 vtable
= SCM_STRUCT_VTABLE (obj
);
608 len
= SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
);
609 if (SCM_LIKELY (index
< len
))
611 scm_t_bits
*data
= SCM_STRUCT_DATA (obj
);
612 data
[index
] = SCM_UNPACK (val
);
618 RETURN (scm_struct_set_x (obj
, pos
, val
));
625 VM_DEFINE_FUNCTION (171, class_of
, "class-of", 1)
628 if (SCM_INSTANCEP (obj
))
629 RETURN (SCM_CLASS_OF (obj
));
631 RETURN (scm_class_of (obj
));
634 /* FIXME: No checking whatsoever. */
635 VM_DEFINE_FUNCTION (172, slot_ref
, "slot-ref", 2)
638 ARGS2 (instance
, idx
);
639 slot
= SCM_I_INUM (idx
);
640 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance
) [slot
]));
643 /* FIXME: No checking whatsoever. */
644 VM_DEFINE_INSTRUCTION (173, slot_set
, "slot-set", 0, 3, 0)
646 SCM instance
, idx
, val
;
651 slot
= SCM_I_INUM (idx
);
652 SCM_STRUCT_DATA (instance
) [slot
] = SCM_UNPACK (val
);
660 #define VM_VALIDATE_BYTEVECTOR(x, proc) \
663 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
667 goto vm_error_not_a_bytevector; \
672 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
676 if (scm_is_eq (endianness, scm_i_native_endianness)) \
677 goto VM_LABEL (bv_##stem##_native_ref); \
681 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
685 /* Return true (non-zero) if PTR has suitable alignment for TYPE. */
686 #define ALIGNED_P(ptr, type) \
687 ((scm_t_uintptr) (ptr) % alignof (type) == 0)
689 VM_DEFINE_FUNCTION (174, bv_u16_ref
, "bv-u16-ref", 3)
690 BV_REF_WITH_ENDIANNESS (u16
, u16
)
691 VM_DEFINE_FUNCTION (175, bv_s16_ref
, "bv-s16-ref", 3)
692 BV_REF_WITH_ENDIANNESS (s16
, s16
)
693 VM_DEFINE_FUNCTION (176, bv_u32_ref
, "bv-u32-ref", 3)
694 BV_REF_WITH_ENDIANNESS (u32
, u32
)
695 VM_DEFINE_FUNCTION (177, bv_s32_ref
, "bv-s32-ref", 3)
696 BV_REF_WITH_ENDIANNESS (s32
, s32
)
697 VM_DEFINE_FUNCTION (178, bv_u64_ref
, "bv-u64-ref", 3)
698 BV_REF_WITH_ENDIANNESS (u64
, u64
)
699 VM_DEFINE_FUNCTION (179, bv_s64_ref
, "bv-s64-ref", 3)
700 BV_REF_WITH_ENDIANNESS (s64
, s64
)
701 VM_DEFINE_FUNCTION (180, bv_f32_ref
, "bv-f32-ref", 3)
702 BV_REF_WITH_ENDIANNESS (f32
, ieee_single
)
703 VM_DEFINE_FUNCTION (181, bv_f64_ref
, "bv-f64-ref", 3)
704 BV_REF_WITH_ENDIANNESS (f64
, ieee_double
)
706 #undef BV_REF_WITH_ENDIANNESS
708 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
710 scm_t_signed_bits i; \
711 const scm_t_ ## type *int_ptr; \
714 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
715 i = SCM_I_INUM (idx); \
716 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
718 if (SCM_LIKELY (SCM_I_INUMP (idx) \
720 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
721 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
722 RETURN (SCM_I_MAKINUM (*int_ptr)); \
726 RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
730 #define BV_INT_REF(stem, type, size) \
732 scm_t_signed_bits i; \
733 const scm_t_ ## type *int_ptr; \
736 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
737 i = SCM_I_INUM (idx); \
738 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
740 if (SCM_LIKELY (SCM_I_INUMP (idx) \
742 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
743 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
745 scm_t_ ## type x = *int_ptr; \
746 if (SCM_FIXABLE (x)) \
747 RETURN (SCM_I_MAKINUM (x)); \
751 RETURN (scm_from_ ## type (x)); \
757 RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
761 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
763 scm_t_signed_bits i; \
764 const type *float_ptr; \
767 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
768 i = SCM_I_INUM (idx); \
769 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
772 if (SCM_LIKELY (SCM_I_INUMP (idx) \
774 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
775 && (ALIGNED_P (float_ptr, type)))) \
776 RETURN (scm_from_double (*float_ptr)); \
778 RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
781 VM_DEFINE_FUNCTION (182, bv_u8_ref
, "bv-u8-ref", 2)
782 BV_FIXABLE_INT_REF (u8
, u8
, uint8
, 1)
783 VM_DEFINE_FUNCTION (183, bv_s8_ref
, "bv-s8-ref", 2)
784 BV_FIXABLE_INT_REF (s8
, s8
, int8
, 1)
785 VM_DEFINE_FUNCTION (184, bv_u16_native_ref
, "bv-u16-native-ref", 2)
786 BV_FIXABLE_INT_REF (u16
, u16_native
, uint16
, 2)
787 VM_DEFINE_FUNCTION (185, bv_s16_native_ref
, "bv-s16-native-ref", 2)
788 BV_FIXABLE_INT_REF (s16
, s16_native
, int16
, 2)
789 VM_DEFINE_FUNCTION (186, bv_u32_native_ref
, "bv-u32-native-ref", 2)
790 #if SIZEOF_VOID_P > 4
791 BV_FIXABLE_INT_REF (u32
, u32_native
, uint32
, 4)
793 BV_INT_REF (u32
, uint32
, 4)
795 VM_DEFINE_FUNCTION (187, bv_s32_native_ref
, "bv-s32-native-ref", 2)
796 #if SIZEOF_VOID_P > 4
797 BV_FIXABLE_INT_REF (s32
, s32_native
, int32
, 4)
799 BV_INT_REF (s32
, int32
, 4)
801 VM_DEFINE_FUNCTION (188, bv_u64_native_ref
, "bv-u64-native-ref", 2)
802 BV_INT_REF (u64
, uint64
, 8)
803 VM_DEFINE_FUNCTION (189, bv_s64_native_ref
, "bv-s64-native-ref", 2)
804 BV_INT_REF (s64
, int64
, 8)
805 VM_DEFINE_FUNCTION (190, bv_f32_native_ref
, "bv-f32-native-ref", 2)
806 BV_FLOAT_REF (f32
, ieee_single
, float, 4)
807 VM_DEFINE_FUNCTION (191, bv_f64_native_ref
, "bv-f64-native-ref", 2)
808 BV_FLOAT_REF (f64
, ieee_double
, double, 8)
810 #undef BV_FIXABLE_INT_REF
816 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
820 if (scm_is_eq (endianness, scm_i_native_endianness)) \
821 goto VM_LABEL (bv_##stem##_native_set); \
823 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
825 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
830 VM_DEFINE_INSTRUCTION (192, bv_u16_set
, "bv-u16-set", 0, 4, 0)
831 BV_SET_WITH_ENDIANNESS (u16
, u16
)
832 VM_DEFINE_INSTRUCTION (193, bv_s16_set
, "bv-s16-set", 0, 4, 0)
833 BV_SET_WITH_ENDIANNESS (s16
, s16
)
834 VM_DEFINE_INSTRUCTION (194, bv_u32_set
, "bv-u32-set", 0, 4, 0)
835 BV_SET_WITH_ENDIANNESS (u32
, u32
)
836 VM_DEFINE_INSTRUCTION (195, bv_s32_set
, "bv-s32-set", 0, 4, 0)
837 BV_SET_WITH_ENDIANNESS (s32
, s32
)
838 VM_DEFINE_INSTRUCTION (196, bv_u64_set
, "bv-u64-set", 0, 4, 0)
839 BV_SET_WITH_ENDIANNESS (u64
, u64
)
840 VM_DEFINE_INSTRUCTION (197, bv_s64_set
, "bv-s64-set", 0, 4, 0)
841 BV_SET_WITH_ENDIANNESS (s64
, s64
)
842 VM_DEFINE_INSTRUCTION (198, bv_f32_set
, "bv-f32-set", 0, 4, 0)
843 BV_SET_WITH_ENDIANNESS (f32
, ieee_single
)
844 VM_DEFINE_INSTRUCTION (199, bv_f64_set
, "bv-f64-set", 0, 4, 0)
845 BV_SET_WITH_ENDIANNESS (f64
, ieee_double
)
847 #undef BV_SET_WITH_ENDIANNESS
849 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
851 scm_t_signed_bits i, j = 0; \
853 scm_t_ ## type *int_ptr; \
855 POP (val); POP (idx); POP (bv); \
856 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
857 i = SCM_I_INUM (idx); \
858 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
860 if (SCM_LIKELY (SCM_I_INUMP (idx) \
862 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
863 && (ALIGNED_P (int_ptr, scm_t_ ## type)) \
864 && (SCM_I_INUMP (val)) \
865 && ((j = SCM_I_INUM (val)) >= min) \
867 *int_ptr = (scm_t_ ## type) j; \
871 scm_bytevector_ ## fn_stem ## _set_x (bv, idx, val); \
876 #define BV_INT_SET(stem, type, size) \
878 scm_t_signed_bits i = 0; \
880 scm_t_ ## type *int_ptr; \
882 POP (val); POP (idx); POP (bv); \
883 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
884 i = SCM_I_INUM (idx); \
885 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
887 if (SCM_LIKELY (SCM_I_INUMP (idx) \
889 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
890 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
891 *int_ptr = scm_to_ ## type (val); \
895 scm_bytevector_ ## stem ## _native_set_x (bv, idx, val); \
900 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
902 scm_t_signed_bits i = 0; \
906 POP (val); POP (idx); POP (bv); \
907 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
908 i = SCM_I_INUM (idx); \
909 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
911 if (SCM_LIKELY (SCM_I_INUMP (idx) \
913 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
914 && (ALIGNED_P (float_ptr, type)))) \
915 *float_ptr = scm_to_double (val); \
919 scm_bytevector_ ## fn_stem ## _native_set_x (bv, idx, val); \
924 VM_DEFINE_INSTRUCTION (200, bv_u8_set
, "bv-u8-set", 0, 3, 0)
925 BV_FIXABLE_INT_SET (u8
, u8
, uint8
, 0, SCM_T_UINT8_MAX
, 1)
926 VM_DEFINE_INSTRUCTION (201, bv_s8_set
, "bv-s8-set", 0, 3, 0)
927 BV_FIXABLE_INT_SET (s8
, s8
, int8
, SCM_T_INT8_MIN
, SCM_T_INT8_MAX
, 1)
928 VM_DEFINE_INSTRUCTION (202, bv_u16_native_set
, "bv-u16-native-set", 0, 3, 0)
929 BV_FIXABLE_INT_SET (u16
, u16_native
, uint16
, 0, SCM_T_UINT16_MAX
, 2)
930 VM_DEFINE_INSTRUCTION (203, bv_s16_native_set
, "bv-s16-native-set", 0, 3, 0)
931 BV_FIXABLE_INT_SET (s16
, s16_native
, int16
, SCM_T_INT16_MIN
, SCM_T_INT16_MAX
, 2)
932 VM_DEFINE_INSTRUCTION (204, bv_u32_native_set
, "bv-u32-native-set", 0, 3, 0)
933 #if SIZEOF_VOID_P > 4
934 BV_FIXABLE_INT_SET (u32
, u32_native
, uint32
, 0, SCM_T_UINT32_MAX
, 4)
936 BV_INT_SET (u32
, uint32
, 4)
938 VM_DEFINE_INSTRUCTION (205, bv_s32_native_set
, "bv-s32-native-set", 0, 3, 0)
939 #if SIZEOF_VOID_P > 4
940 BV_FIXABLE_INT_SET (s32
, s32_native
, int32
, SCM_T_INT32_MIN
, SCM_T_INT32_MAX
, 4)
942 BV_INT_SET (s32
, int32
, 4)
944 VM_DEFINE_INSTRUCTION (206, bv_u64_native_set
, "bv-u64-native-set", 0, 3, 0)
945 BV_INT_SET (u64
, uint64
, 8)
946 VM_DEFINE_INSTRUCTION (207, bv_s64_native_set
, "bv-s64-native-set", 0, 3, 0)
947 BV_INT_SET (s64
, int64
, 8)
948 VM_DEFINE_INSTRUCTION (208, bv_f32_native_set
, "bv-f32-native-set", 0, 3, 0)
949 BV_FLOAT_SET (f32
, ieee_single
, float, 4)
950 VM_DEFINE_INSTRUCTION (209, bv_f64_native_set
, "bv-f64-native-set", 0, 3, 0)
951 BV_FLOAT_SET (f64
, ieee_double
, double, 8)
953 #undef BV_FIXABLE_INT_SET
958 (defun renumber-ops ()
959 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
962 (let ((counter 127)) (goto-char (point-min))
963 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
965 (number-to-string (setq counter (1+ counter)))