1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 VM_ASSERT (scm_is_pair (x), vm_error_not_a_pair (proc, x))
129 VM_DEFINE_FUNCTION (141, car
, "car", 1)
132 VM_VALIDATE_CONS (x
, "car");
133 RETURN (SCM_CAR (x
));
136 VM_DEFINE_FUNCTION (142, cdr
, "cdr", 1)
139 VM_VALIDATE_CONS (x
, "cdr");
140 RETURN (SCM_CDR (x
));
143 VM_DEFINE_INSTRUCTION (143, set_car
, "set-car!", 0, 2, 0)
147 VM_VALIDATE_CONS (x
, "set-car!");
152 VM_DEFINE_INSTRUCTION (144, set_cdr
, "set-cdr!", 0, 2, 0)
156 VM_VALIDATE_CONS (x
, "set-cdr!");
163 * Numeric relational tests
167 #define REL(crel,srel) \
170 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
171 RETURN (scm_from_bool (((scm_t_signed_bits) SCM_UNPACK (x)) \
172 crel ((scm_t_signed_bits) SCM_UNPACK (y)))); \
174 RETURN (srel (x, y)); \
177 VM_DEFINE_FUNCTION (145, ee
, "ee?", 2)
179 REL (==, scm_num_eq_p
);
182 VM_DEFINE_FUNCTION (146, lt
, "lt?", 2)
187 VM_DEFINE_FUNCTION (147, le
, "le?", 2)
192 VM_DEFINE_FUNCTION (148, gt
, "gt?", 2)
197 VM_DEFINE_FUNCTION (149, ge
, "ge?", 2)
207 /* The maximum/minimum tagged integers. */
210 #define INUM_MAX (INTPTR_MAX - 1)
211 #define INUM_MIN (INTPTR_MIN + scm_tc2_int)
214 #define FUNC2(CFUNC,SFUNC) \
217 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
219 scm_t_int64 n = SCM_I_INUM (x) CFUNC SCM_I_INUM (y);\
220 if (SCM_FIXABLE (n)) \
221 RETURN (SCM_I_MAKINUM (n)); \
224 RETURN (SFUNC (x, y)); \
227 /* Assembly tagged integer arithmetic routines. This code uses the
228 `asm goto' feature introduced in GCC 4.5. */
230 #if defined __x86_64__ && SCM_GNUC_PREREQ (4, 5)
232 /* The macros below check the CPU's overflow flag to improve fixnum
233 arithmetic. The %rcx register is explicitly clobbered because `asm
234 goto' can't have outputs, in which case the `r' constraint could be
235 used to let the register allocator choose a register.
237 TODO: Use `cold' label attribute in GCC 4.6.
238 http://gcc.gnu.org/ml/gcc-patches/2010-10/msg01777.html */
240 # define ASM_ADD(x, y) \
242 asm volatile goto ("mov %1, %%rcx; " \
243 "test %[tag], %%cl; je %l[slow_add]; " \
244 "test %[tag], %0; je %l[slow_add]; " \
245 "add %0, %%rcx; jo %l[slow_add]; " \
246 "sub %[tag], %%rcx; " \
247 "mov %%rcx, (%[vsp])\n" \
249 : "r" (x), "r" (y), \
250 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
258 # define ASM_SUB(x, y) \
260 asm volatile goto ("mov %0, %%rcx; " \
261 "test %[tag], %%cl; je %l[slow_sub]; " \
262 "test %[tag], %1; je %l[slow_sub]; " \
263 "sub %1, %%rcx; jo %l[slow_sub]; " \
264 "add %[tag], %%rcx; " \
265 "mov %%rcx, (%[vsp])\n" \
267 : "r" (x), "r" (y), \
268 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
279 VM_DEFINE_FUNCTION (150, add
, "add", 2)
287 RETURN (scm_sum (x
, y
));
291 VM_DEFINE_FUNCTION (151, add1
, "add1", 1)
295 /* Check for overflow. */
296 if (SCM_LIKELY ((scm_t_intptr
) SCM_UNPACK (x
) < INUM_MAX
))
300 /* Add the integers without untagging. */
301 result
= SCM_PACK ((scm_t_intptr
) SCM_UNPACK (x
)
302 + (scm_t_intptr
) SCM_UNPACK (SCM_I_MAKINUM (1))
305 if (SCM_LIKELY (SCM_I_INUMP (result
)))
310 RETURN (scm_sum (x
, SCM_I_MAKINUM (1)));
313 VM_DEFINE_FUNCTION (152, sub
, "sub", 2)
316 FUNC2 (-, scm_difference
);
321 RETURN (scm_difference (x
, y
));
325 VM_DEFINE_FUNCTION (153, sub1
, "sub1", 1)
329 /* Check for underflow. */
330 if (SCM_LIKELY ((scm_t_intptr
) SCM_UNPACK (x
) > INUM_MIN
))
334 /* Substract the integers without untagging. */
335 result
= SCM_PACK ((scm_t_intptr
) SCM_UNPACK (x
)
336 - (scm_t_intptr
) SCM_UNPACK (SCM_I_MAKINUM (1))
339 if (SCM_LIKELY (SCM_I_INUMP (result
)))
344 RETURN (scm_difference (x
, SCM_I_MAKINUM (1)));
350 VM_DEFINE_FUNCTION (154, mul
, "mul", 2)
354 RETURN (scm_product (x
, y
));
357 VM_DEFINE_FUNCTION (155, div
, "div", 2)
361 RETURN (scm_divide (x
, y
));
364 VM_DEFINE_FUNCTION (156, quo
, "quo", 2)
368 RETURN (scm_quotient (x
, y
));
371 VM_DEFINE_FUNCTION (157, rem
, "rem", 2)
375 RETURN (scm_remainder (x
, y
));
378 VM_DEFINE_FUNCTION (158, mod
, "mod", 2)
382 RETURN (scm_modulo (x
, y
));
385 VM_DEFINE_FUNCTION (159, ash
, "ash", 2)
388 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
390 if (SCM_I_INUM (y
) < 0)
392 /* Right shift, will be a fixnum. */
393 if (SCM_I_INUM (y
) > -SCM_I_FIXNUM_BIT
)
394 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) >> -SCM_I_INUM (y
)));
398 /* Left shift. See comments in scm_ash. */
400 scm_t_signed_bits nn
, bits_to_shift
;
403 bits_to_shift
= SCM_I_INUM (y
);
405 if (bits_to_shift
< SCM_I_FIXNUM_BIT
-1
407 (SCM_SRS (nn
, (SCM_I_FIXNUM_BIT
-1 - bits_to_shift
)) + 1)
409 RETURN (SCM_I_MAKINUM (nn
<< bits_to_shift
));
415 RETURN (scm_ash (x
, y
));
418 VM_DEFINE_FUNCTION (160, logand
, "logand", 2)
421 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
422 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) & SCM_I_INUM (y
)));
424 RETURN (scm_logand (x
, y
));
427 VM_DEFINE_FUNCTION (161, logior
, "logior", 2)
430 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
431 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) | SCM_I_INUM (y
)));
433 RETURN (scm_logior (x
, y
));
436 VM_DEFINE_FUNCTION (162, logxor
, "logxor", 2)
439 if (SCM_I_INUMP (x
) && SCM_I_INUMP (y
))
440 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x
) ^ SCM_I_INUM (y
)));
442 RETURN (scm_logxor (x
, y
));
450 VM_DEFINE_FUNCTION (163, vector_ref
, "vector-ref", 2)
452 scm_t_signed_bits i
= 0;
454 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect
)
456 && ((i
= SCM_I_INUM (idx
)) >= 0)
457 && i
< SCM_I_VECTOR_LENGTH (vect
)))
458 RETURN (SCM_I_VECTOR_ELTS (vect
)[i
]);
462 RETURN (scm_vector_ref (vect
, idx
));
466 VM_DEFINE_INSTRUCTION (164, vector_set
, "vector-set", 0, 3, 0)
468 scm_t_signed_bits i
= 0;
470 POP3 (val
, idx
, vect
);
471 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect
)
473 && ((i
= SCM_I_INUM (idx
)) >= 0)
474 && i
< SCM_I_VECTOR_LENGTH (vect
)))
475 SCM_I_VECTOR_WELTS (vect
)[i
] = val
;
479 scm_vector_set_x (vect
, idx
, val
);
484 VM_DEFINE_INSTRUCTION (165, make_array
, "make-array", 3, -1, 1)
490 len
= (len
<< 8) + FETCH ();
491 len
= (len
<< 8) + FETCH ();
494 PRE_CHECK_UNDERFLOW (len
);
495 ret
= scm_from_contiguous_array (shape
, sp
- len
+ 1, len
);
505 #define VM_VALIDATE_STRUCT(obj, proc) \
506 VM_ASSERT (SCM_STRUCTP (obj), vm_error_not_a_struct (proc, obj))
508 VM_DEFINE_FUNCTION (166, struct_p
, "struct?", 1)
511 RETURN (scm_from_bool (SCM_STRUCTP (obj
)));
514 VM_DEFINE_FUNCTION (167, struct_vtable
, "struct-vtable", 1)
517 VM_VALIDATE_STRUCT (obj
, "struct_vtable");
518 RETURN (SCM_STRUCT_VTABLE (obj
));
521 VM_DEFINE_INSTRUCTION (168, make_struct
, "make-struct", 2, -1, 1)
523 unsigned h
= FETCH ();
524 unsigned l
= FETCH ();
525 scm_t_bits n
= ((h
<< 8U) + l
);
526 SCM vtable
= sp
[-(n
- 1)];
527 const SCM
*inits
= sp
- n
+ 2;
532 if (SCM_LIKELY (SCM_STRUCTP (vtable
)
533 && SCM_VTABLE_FLAG_IS_SET (vtable
, SCM_VTABLE_FLAG_SIMPLE
)
534 && (SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
) + 1
536 && !SCM_VTABLE_INSTANCE_FINALIZER (vtable
)))
538 /* Verily, we are making a simple struct with the right number of
539 initializers, and no finalizer. */
540 ret
= scm_words ((scm_t_bits
)SCM_STRUCT_DATA (vtable
) | scm_tc3_struct
,
542 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)SCM_CELL_OBJECT_LOC (ret
, 2));
543 memcpy (SCM_STRUCT_DATA (ret
), inits
, (n
- 1) * sizeof (SCM
));
546 ret
= scm_c_make_structv (vtable
, 0, n
- 1, (scm_t_bits
*) inits
);
554 VM_DEFINE_FUNCTION (169, struct_ref
, "struct-ref", 2)
558 if (SCM_LIKELY (SCM_STRUCTP (obj
)
559 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
560 SCM_VTABLE_FLAG_SIMPLE
)
561 && SCM_I_INUMP (pos
)))
564 scm_t_bits index
, len
;
566 /* True, an inum is a signed value, but cast to unsigned it will
567 certainly be more than the length, so we will fall through if
568 index is negative. */
569 index
= SCM_I_INUM (pos
);
570 vtable
= SCM_STRUCT_VTABLE (obj
);
571 len
= SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
);
573 if (SCM_LIKELY (index
< len
))
575 scm_t_bits
*data
= SCM_STRUCT_DATA (obj
);
576 RETURN (SCM_PACK (data
[index
]));
581 RETURN (scm_struct_ref (obj
, pos
));
584 VM_DEFINE_FUNCTION (170, struct_set
, "struct-set", 3)
586 ARGS3 (obj
, pos
, val
);
588 if (SCM_LIKELY (SCM_STRUCTP (obj
)
589 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
590 SCM_VTABLE_FLAG_SIMPLE
)
591 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj
,
592 SCM_VTABLE_FLAG_SIMPLE_RW
)
593 && SCM_I_INUMP (pos
)))
596 scm_t_bits index
, len
;
598 /* See above regarding index being >= 0. */
599 index
= SCM_I_INUM (pos
);
600 vtable
= SCM_STRUCT_VTABLE (obj
);
601 len
= SCM_STRUCT_DATA_REF (vtable
, scm_vtable_index_size
);
602 if (SCM_LIKELY (index
< len
))
604 scm_t_bits
*data
= SCM_STRUCT_DATA (obj
);
605 data
[index
] = SCM_UNPACK (val
);
611 RETURN (scm_struct_set_x (obj
, pos
, val
));
618 VM_DEFINE_FUNCTION (171, class_of
, "class-of", 1)
621 if (SCM_INSTANCEP (obj
))
622 RETURN (SCM_CLASS_OF (obj
));
624 RETURN (scm_class_of (obj
));
627 /* FIXME: No checking whatsoever. */
628 VM_DEFINE_FUNCTION (172, slot_ref
, "slot-ref", 2)
631 ARGS2 (instance
, idx
);
632 slot
= SCM_I_INUM (idx
);
633 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance
) [slot
]));
636 /* FIXME: No checking whatsoever. */
637 VM_DEFINE_INSTRUCTION (173, slot_set
, "slot-set", 0, 3, 0)
639 SCM instance
, idx
, val
;
641 POP3 (val
, idx
, instance
);
642 slot
= SCM_I_INUM (idx
);
643 SCM_STRUCT_DATA (instance
) [slot
] = SCM_UNPACK (val
);
651 #define VM_VALIDATE_BYTEVECTOR(x, proc) \
652 VM_ASSERT (SCM_BYTEVECTOR_P (x), vm_error_not_a_bytevector (proc, x))
654 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
658 if (scm_is_eq (endianness, scm_i_native_endianness)) \
659 goto VM_LABEL (bv_##stem##_native_ref); \
663 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
667 /* Return true (non-zero) if PTR has suitable alignment for TYPE. */
668 #define ALIGNED_P(ptr, type) \
669 ((scm_t_uintptr) (ptr) % alignof_type (type) == 0)
671 VM_DEFINE_FUNCTION (174, bv_u16_ref
, "bv-u16-ref", 3)
672 BV_REF_WITH_ENDIANNESS (u16
, u16
)
673 VM_DEFINE_FUNCTION (175, bv_s16_ref
, "bv-s16-ref", 3)
674 BV_REF_WITH_ENDIANNESS (s16
, s16
)
675 VM_DEFINE_FUNCTION (176, bv_u32_ref
, "bv-u32-ref", 3)
676 BV_REF_WITH_ENDIANNESS (u32
, u32
)
677 VM_DEFINE_FUNCTION (177, bv_s32_ref
, "bv-s32-ref", 3)
678 BV_REF_WITH_ENDIANNESS (s32
, s32
)
679 VM_DEFINE_FUNCTION (178, bv_u64_ref
, "bv-u64-ref", 3)
680 BV_REF_WITH_ENDIANNESS (u64
, u64
)
681 VM_DEFINE_FUNCTION (179, bv_s64_ref
, "bv-s64-ref", 3)
682 BV_REF_WITH_ENDIANNESS (s64
, s64
)
683 VM_DEFINE_FUNCTION (180, bv_f32_ref
, "bv-f32-ref", 3)
684 BV_REF_WITH_ENDIANNESS (f32
, ieee_single
)
685 VM_DEFINE_FUNCTION (181, bv_f64_ref
, "bv-f64-ref", 3)
686 BV_REF_WITH_ENDIANNESS (f64
, ieee_double
)
688 #undef BV_REF_WITH_ENDIANNESS
690 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
692 scm_t_signed_bits i; \
693 const scm_t_ ## type *int_ptr; \
696 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
697 i = SCM_I_INUM (idx); \
698 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
700 if (SCM_LIKELY (SCM_I_INUMP (idx) \
702 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
703 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
704 RETURN (SCM_I_MAKINUM (*int_ptr)); \
708 RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
712 #define BV_INT_REF(stem, type, size) \
714 scm_t_signed_bits i; \
715 const scm_t_ ## type *int_ptr; \
718 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
719 i = SCM_I_INUM (idx); \
720 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
722 if (SCM_LIKELY (SCM_I_INUMP (idx) \
724 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
725 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
727 scm_t_ ## type x = *int_ptr; \
728 if (SCM_FIXABLE (x)) \
729 RETURN (SCM_I_MAKINUM (x)); \
733 RETURN (scm_from_ ## type (x)); \
739 RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
743 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
745 scm_t_signed_bits i; \
746 const type *float_ptr; \
749 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
750 i = SCM_I_INUM (idx); \
751 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
754 if (SCM_LIKELY (SCM_I_INUMP (idx) \
756 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
757 && (ALIGNED_P (float_ptr, type)))) \
758 RETURN (scm_from_double (*float_ptr)); \
760 RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
763 VM_DEFINE_FUNCTION (182, bv_u8_ref
, "bv-u8-ref", 2)
764 BV_FIXABLE_INT_REF (u8
, u8
, uint8
, 1)
765 VM_DEFINE_FUNCTION (183, bv_s8_ref
, "bv-s8-ref", 2)
766 BV_FIXABLE_INT_REF (s8
, s8
, int8
, 1)
767 VM_DEFINE_FUNCTION (184, bv_u16_native_ref
, "bv-u16-native-ref", 2)
768 BV_FIXABLE_INT_REF (u16
, u16_native
, uint16
, 2)
769 VM_DEFINE_FUNCTION (185, bv_s16_native_ref
, "bv-s16-native-ref", 2)
770 BV_FIXABLE_INT_REF (s16
, s16_native
, int16
, 2)
771 VM_DEFINE_FUNCTION (186, bv_u32_native_ref
, "bv-u32-native-ref", 2)
772 #if SIZEOF_VOID_P > 4
773 BV_FIXABLE_INT_REF (u32
, u32_native
, uint32
, 4)
775 BV_INT_REF (u32
, uint32
, 4)
777 VM_DEFINE_FUNCTION (187, bv_s32_native_ref
, "bv-s32-native-ref", 2)
778 #if SIZEOF_VOID_P > 4
779 BV_FIXABLE_INT_REF (s32
, s32_native
, int32
, 4)
781 BV_INT_REF (s32
, int32
, 4)
783 VM_DEFINE_FUNCTION (188, bv_u64_native_ref
, "bv-u64-native-ref", 2)
784 BV_INT_REF (u64
, uint64
, 8)
785 VM_DEFINE_FUNCTION (189, bv_s64_native_ref
, "bv-s64-native-ref", 2)
786 BV_INT_REF (s64
, int64
, 8)
787 VM_DEFINE_FUNCTION (190, bv_f32_native_ref
, "bv-f32-native-ref", 2)
788 BV_FLOAT_REF (f32
, ieee_single
, float, 4)
789 VM_DEFINE_FUNCTION (191, bv_f64_native_ref
, "bv-f64-native-ref", 2)
790 BV_FLOAT_REF (f64
, ieee_double
, double, 8)
792 #undef BV_FIXABLE_INT_REF
798 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
802 if (scm_is_eq (endianness, scm_i_native_endianness)) \
803 goto VM_LABEL (bv_##stem##_native_set); \
805 SCM bv, idx, val; POP3 (val, idx, bv); \
807 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
812 VM_DEFINE_INSTRUCTION (192, bv_u16_set
, "bv-u16-set", 0, 4, 0)
813 BV_SET_WITH_ENDIANNESS (u16
, u16
)
814 VM_DEFINE_INSTRUCTION (193, bv_s16_set
, "bv-s16-set", 0, 4, 0)
815 BV_SET_WITH_ENDIANNESS (s16
, s16
)
816 VM_DEFINE_INSTRUCTION (194, bv_u32_set
, "bv-u32-set", 0, 4, 0)
817 BV_SET_WITH_ENDIANNESS (u32
, u32
)
818 VM_DEFINE_INSTRUCTION (195, bv_s32_set
, "bv-s32-set", 0, 4, 0)
819 BV_SET_WITH_ENDIANNESS (s32
, s32
)
820 VM_DEFINE_INSTRUCTION (196, bv_u64_set
, "bv-u64-set", 0, 4, 0)
821 BV_SET_WITH_ENDIANNESS (u64
, u64
)
822 VM_DEFINE_INSTRUCTION (197, bv_s64_set
, "bv-s64-set", 0, 4, 0)
823 BV_SET_WITH_ENDIANNESS (s64
, s64
)
824 VM_DEFINE_INSTRUCTION (198, bv_f32_set
, "bv-f32-set", 0, 4, 0)
825 BV_SET_WITH_ENDIANNESS (f32
, ieee_single
)
826 VM_DEFINE_INSTRUCTION (199, bv_f64_set
, "bv-f64-set", 0, 4, 0)
827 BV_SET_WITH_ENDIANNESS (f64
, ieee_double
)
829 #undef BV_SET_WITH_ENDIANNESS
831 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
833 scm_t_signed_bits i, j = 0; \
835 scm_t_ ## type *int_ptr; \
837 POP3 (val, idx, bv); \
838 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
839 i = SCM_I_INUM (idx); \
840 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
842 if (SCM_LIKELY (SCM_I_INUMP (idx) \
844 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
845 && (ALIGNED_P (int_ptr, scm_t_ ## type)) \
846 && (SCM_I_INUMP (val)) \
847 && ((j = SCM_I_INUM (val)) >= min) \
849 *int_ptr = (scm_t_ ## type) j; \
853 scm_bytevector_ ## fn_stem ## _set_x (bv, idx, val); \
858 #define BV_INT_SET(stem, type, size) \
860 scm_t_signed_bits i = 0; \
862 scm_t_ ## type *int_ptr; \
864 POP3 (val, idx, bv); \
865 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
866 i = SCM_I_INUM (idx); \
867 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
869 if (SCM_LIKELY (SCM_I_INUMP (idx) \
871 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
872 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
873 *int_ptr = scm_to_ ## type (val); \
877 scm_bytevector_ ## stem ## _native_set_x (bv, idx, val); \
882 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
884 scm_t_signed_bits i = 0; \
888 POP3 (val, idx, bv); \
889 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
890 i = SCM_I_INUM (idx); \
891 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
893 if (SCM_LIKELY (SCM_I_INUMP (idx) \
895 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
896 && (ALIGNED_P (float_ptr, type)))) \
897 *float_ptr = scm_to_double (val); \
901 scm_bytevector_ ## fn_stem ## _native_set_x (bv, idx, val); \
906 VM_DEFINE_INSTRUCTION (200, bv_u8_set
, "bv-u8-set", 0, 3, 0)
907 BV_FIXABLE_INT_SET (u8
, u8
, uint8
, 0, SCM_T_UINT8_MAX
, 1)
908 VM_DEFINE_INSTRUCTION (201, bv_s8_set
, "bv-s8-set", 0, 3, 0)
909 BV_FIXABLE_INT_SET (s8
, s8
, int8
, SCM_T_INT8_MIN
, SCM_T_INT8_MAX
, 1)
910 VM_DEFINE_INSTRUCTION (202, bv_u16_native_set
, "bv-u16-native-set", 0, 3, 0)
911 BV_FIXABLE_INT_SET (u16
, u16_native
, uint16
, 0, SCM_T_UINT16_MAX
, 2)
912 VM_DEFINE_INSTRUCTION (203, bv_s16_native_set
, "bv-s16-native-set", 0, 3, 0)
913 BV_FIXABLE_INT_SET (s16
, s16_native
, int16
, SCM_T_INT16_MIN
, SCM_T_INT16_MAX
, 2)
914 VM_DEFINE_INSTRUCTION (204, bv_u32_native_set
, "bv-u32-native-set", 0, 3, 0)
915 #if SIZEOF_VOID_P > 4
916 BV_FIXABLE_INT_SET (u32
, u32_native
, uint32
, 0, SCM_T_UINT32_MAX
, 4)
918 BV_INT_SET (u32
, uint32
, 4)
920 VM_DEFINE_INSTRUCTION (205, bv_s32_native_set
, "bv-s32-native-set", 0, 3, 0)
921 #if SIZEOF_VOID_P > 4
922 BV_FIXABLE_INT_SET (s32
, s32_native
, int32
, SCM_T_INT32_MIN
, SCM_T_INT32_MAX
, 4)
924 BV_INT_SET (s32
, int32
, 4)
926 VM_DEFINE_INSTRUCTION (206, bv_u64_native_set
, "bv-u64-native-set", 0, 3, 0)
927 BV_INT_SET (u64
, uint64
, 8)
928 VM_DEFINE_INSTRUCTION (207, bv_s64_native_set
, "bv-s64-native-set", 0, 3, 0)
929 BV_INT_SET (s64
, int64
, 8)
930 VM_DEFINE_INSTRUCTION (208, bv_f32_native_set
, "bv-f32-native-set", 0, 3, 0)
931 BV_FLOAT_SET (f32
, ieee_single
, float, 4)
932 VM_DEFINE_INSTRUCTION (209, bv_f64_native_set
, "bv-f64-native-set", 0, 3, 0)
933 BV_FLOAT_SET (f64
, ieee_double
, double, 8)
935 #undef BV_FIXABLE_INT_SET
940 (defun renumber-ops ()
941 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
944 (let ((counter 127)) (goto-char (point-min))
945 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
947 (number-to-string (setq counter (1+ counter)))