1 /* Copyright (C) 2001, 2009 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 (100, not, "not", 1)
35 RETURN (SCM_BOOL (SCM_FALSEP (x
)));
38 VM_DEFINE_FUNCTION (101, not_not
, "not-not", 1)
41 RETURN (SCM_BOOL (!SCM_FALSEP (x
)));
44 VM_DEFINE_FUNCTION (102, eq
, "eq?", 2)
47 RETURN (SCM_BOOL (SCM_EQ_P (x
, y
)));
50 VM_DEFINE_FUNCTION (103, not_eq, "not-eq?", 2)
53 RETURN (SCM_BOOL (!SCM_EQ_P (x
, y
)));
56 VM_DEFINE_FUNCTION (104, nullp
, "null?", 1)
59 RETURN (SCM_BOOL (SCM_NULLP (x
)));
62 VM_DEFINE_FUNCTION (105, not_nullp
, "not-null?", 1)
65 RETURN (SCM_BOOL (!SCM_NULLP (x
)));
68 VM_DEFINE_FUNCTION (106, eqv
, "eqv?", 2)
73 if (SCM_IMP (x
) || SCM_IMP (y
))
76 RETURN (scm_eqv_p (x
, y
));
79 VM_DEFINE_FUNCTION (107, equal
, "equal?", 2)
84 if (SCM_IMP (x
) || SCM_IMP (y
))
87 RETURN (scm_equal_p (x
, y
));
90 VM_DEFINE_FUNCTION (108, pairp
, "pair?", 1)
93 RETURN (SCM_BOOL (SCM_CONSP (x
)));
96 VM_DEFINE_FUNCTION (109, listp
, "list?", 1)
99 RETURN (SCM_BOOL (scm_ilength (x
) >= 0));
107 VM_DEFINE_FUNCTION (110, cons
, "cons", 2)
114 #define VM_VALIDATE_CONS(x) \
115 if (SCM_UNLIKELY (!scm_is_pair (x))) \
117 goto vm_error_not_a_pair; \
120 VM_DEFINE_FUNCTION (111, car
, "car", 1)
123 VM_VALIDATE_CONS (x
);
124 RETURN (SCM_CAR (x
));
127 VM_DEFINE_FUNCTION (112, cdr
, "cdr", 1)
130 VM_VALIDATE_CONS (x
);
131 RETURN (SCM_CDR (x
));
134 VM_DEFINE_INSTRUCTION (113, set_car
, "set-car!", 0, 2, 0)
139 VM_VALIDATE_CONS (x
);
144 VM_DEFINE_INSTRUCTION (114, set_cdr
, "set-cdr!", 0, 2, 0)
149 VM_VALIDATE_CONS (x
);
156 * Numeric relational tests
160 #define REL(crel,srel) \
163 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
164 RETURN (SCM_BOOL (SCM_I_INUM (x) crel SCM_I_INUM (y))); \
166 RETURN (srel (x, y)); \
169 VM_DEFINE_FUNCTION (115, ee
, "ee?", 2)
171 REL (==, scm_num_eq_p
);
174 VM_DEFINE_FUNCTION (116, lt
, "lt?", 2)
179 VM_DEFINE_FUNCTION (117, le
, "le?", 2)
184 VM_DEFINE_FUNCTION (118, gt
, "gt?", 2)
189 VM_DEFINE_FUNCTION (119, ge
, "ge?", 2)
200 #define FUNC2(CFUNC,SFUNC) \
203 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
205 scm_t_int64 n = SCM_I_INUM (x) CFUNC SCM_I_INUM (y);\
206 if (SCM_FIXABLE (n)) \
207 RETURN (SCM_I_MAKINUM (n)); \
210 RETURN (SFUNC (x, y)); \
213 VM_DEFINE_FUNCTION (120, add
, "add", 2)
218 VM_DEFINE_FUNCTION (167, add1
, "add1", 1)
223 scm_t_int64 n
= SCM_I_INUM (x
) + 1;
225 RETURN (SCM_I_MAKINUM (n
));
228 RETURN (scm_sum (x
, SCM_I_MAKINUM (1)));
231 VM_DEFINE_FUNCTION (121, sub
, "sub", 2)
233 FUNC2 (-, scm_difference
);
236 VM_DEFINE_FUNCTION (168, sub1
, "sub1", 1)
241 scm_t_int64 n
= SCM_I_INUM (x
) - 1;
243 RETURN (SCM_I_MAKINUM (n
));
246 RETURN (scm_difference (x
, SCM_I_MAKINUM (1)));
249 VM_DEFINE_FUNCTION (122, mul
, "mul", 2)
253 RETURN (scm_product (x
, y
));
256 VM_DEFINE_FUNCTION (123, div
, "div", 2)
260 RETURN (scm_divide (x
, y
));
263 VM_DEFINE_FUNCTION (124, quo
, "quo", 2)
267 RETURN (scm_quotient (x
, y
));
270 VM_DEFINE_FUNCTION (125, rem
, "rem", 2)
274 RETURN (scm_remainder (x
, y
));
277 VM_DEFINE_FUNCTION (126, mod
, "mod", 2)
281 RETURN (scm_modulo (x
, y
));
288 VM_DEFINE_FUNCTION (127, slot_ref
, "slot-ref", 2)
291 ARGS2 (instance
, idx
);
292 slot
= SCM_I_INUM (idx
);
293 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance
) [slot
]));
296 VM_DEFINE_INSTRUCTION (128, slot_set
, "slot-set", 0, 3, 0)
298 SCM instance
, idx
, val
;
303 slot
= SCM_I_INUM (idx
);
304 SCM_STRUCT_DATA (instance
) [slot
] = SCM_UNPACK (val
);
308 VM_DEFINE_FUNCTION (129, vector_ref
, "vector-ref", 2)
312 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect
)
314 && ((i
= SCM_I_INUM (idx
)) >= 0)
315 && i
< SCM_I_VECTOR_LENGTH (vect
)))
316 RETURN (SCM_I_VECTOR_ELTS (vect
)[i
]);
320 RETURN (scm_vector_ref (vect
, idx
));
324 VM_DEFINE_INSTRUCTION (130, vector_set
, "vector-set", 0, 3, 0)
328 POP (val
); POP (idx
); POP (vect
);
329 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect
)
331 && ((i
= SCM_I_INUM (idx
)) >= 0)
332 && i
< SCM_I_VECTOR_LENGTH (vect
)))
333 SCM_I_VECTOR_WELTS (vect
)[i
] = val
;
337 scm_vector_set_x (vect
, idx
, val
);
342 #define VM_VALIDATE_BYTEVECTOR(x) \
343 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
345 goto vm_error_not_a_bytevector; \
348 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
352 if (scm_is_eq (endianness, scm_i_native_endianness)) \
353 goto VM_LABEL (bv_##stem##_native_ref); \
356 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
360 VM_DEFINE_FUNCTION (131, bv_u16_ref
, "bv-u16-ref", 3)
361 BV_REF_WITH_ENDIANNESS (u16
, u16
)
362 VM_DEFINE_FUNCTION (132, bv_s16_ref
, "bv-s16-ref", 3)
363 BV_REF_WITH_ENDIANNESS (s16
, s16
)
364 VM_DEFINE_FUNCTION (133, bv_u32_ref
, "bv-u32-ref", 3)
365 BV_REF_WITH_ENDIANNESS (u32
, u32
)
366 VM_DEFINE_FUNCTION (134, bv_s32_ref
, "bv-s32-ref", 3)
367 BV_REF_WITH_ENDIANNESS (s32
, s32
)
368 VM_DEFINE_FUNCTION (135, bv_u64_ref
, "bv-u64-ref", 3)
369 BV_REF_WITH_ENDIANNESS (u64
, u64
)
370 VM_DEFINE_FUNCTION (136, bv_s64_ref
, "bv-s64-ref", 3)
371 BV_REF_WITH_ENDIANNESS (s64
, s64
)
372 VM_DEFINE_FUNCTION (137, bv_f32_ref
, "bv-f32-ref", 3)
373 BV_REF_WITH_ENDIANNESS (f32
, ieee_single
)
374 VM_DEFINE_FUNCTION (138, bv_f64_ref
, "bv-f64-ref", 3)
375 BV_REF_WITH_ENDIANNESS (f64
, ieee_double
)
377 #undef BV_REF_WITH_ENDIANNESS
379 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
383 VM_VALIDATE_BYTEVECTOR (bv); \
384 if (SCM_LIKELY (SCM_I_INUMP (idx) \
385 && ((i = SCM_I_INUM (idx)) >= 0) \
386 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
387 && (i % size == 0))) \
388 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
389 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
391 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx)); \
394 #define BV_INT_REF(stem, type, size) \
398 VM_VALIDATE_BYTEVECTOR (bv); \
399 if (SCM_LIKELY (SCM_I_INUMP (idx) \
400 && ((i = SCM_I_INUM (idx)) >= 0) \
401 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
402 && (i % size == 0))) \
403 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
404 if (SCM_FIXABLE (x)) \
405 RETURN (SCM_I_MAKINUM (x)); \
407 RETURN (scm_from_##type (x)); \
410 RETURN (scm_bytevector_##stem##_native_ref (bv, idx)); \
413 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
417 VM_VALIDATE_BYTEVECTOR (bv); \
418 if (SCM_LIKELY (SCM_I_INUMP (idx) \
419 && ((i = SCM_I_INUM (idx)) >= 0) \
420 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
421 && (i % size == 0))) \
422 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
424 RETURN (scm_bytevector_##fn_stem##_native_ref (bv, idx)); \
427 VM_DEFINE_FUNCTION (139, bv_u8_ref
, "bv-u8-ref", 2)
428 BV_FIXABLE_INT_REF (u8
, u8
, uint8
, 1)
429 VM_DEFINE_FUNCTION (140, bv_s8_ref
, "bv-s8-ref", 2)
430 BV_FIXABLE_INT_REF (s8
, s8
, int8
, 1)
431 VM_DEFINE_FUNCTION (141, bv_u16_native_ref
, "bv-u16-native-ref", 2)
432 BV_FIXABLE_INT_REF (u16
, u16_native
, uint16
, 2)
433 VM_DEFINE_FUNCTION (142, bv_s16_native_ref
, "bv-s16-native-ref", 2)
434 BV_FIXABLE_INT_REF (s16
, s16_native
, int16
, 2)
435 VM_DEFINE_FUNCTION (143, bv_u32_native_ref
, "bv-u32-native-ref", 2)
436 /* FIXME: u32 is always a fixnum on 64-bit builds */
437 BV_INT_REF (u32
, uint32
, 4)
438 VM_DEFINE_FUNCTION (144, bv_s32_native_ref
, "bv-s32-native-ref", 2)
439 BV_INT_REF (s32
, int32
, 4)
440 VM_DEFINE_FUNCTION (145, bv_u64_native_ref
, "bv-u64-native-ref", 2)
441 BV_INT_REF (u64
, uint64
, 8)
442 VM_DEFINE_FUNCTION (146, bv_s64_native_ref
, "bv-s64-native-ref", 2)
443 BV_INT_REF (s64
, int64
, 8)
444 VM_DEFINE_FUNCTION (147, bv_f32_native_ref
, "bv-f32-native-ref", 2)
445 BV_FLOAT_REF (f32
, ieee_single
, float, 4)
446 VM_DEFINE_FUNCTION (148, bv_f64_native_ref
, "bv-f64-native-ref", 2)
447 BV_FLOAT_REF (f64
, ieee_double
, double, 8)
449 #undef BV_FIXABLE_INT_REF
455 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
459 if (scm_is_eq (endianness, scm_i_native_endianness)) \
460 goto VM_LABEL (bv_##stem##_native_set); \
462 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
463 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
468 VM_DEFINE_INSTRUCTION (149, bv_u16_set
, "bv-u16-set", 0, 4, 0)
469 BV_SET_WITH_ENDIANNESS (u16
, u16
)
470 VM_DEFINE_INSTRUCTION (150, bv_s16_set
, "bv-s16-set", 0, 4, 0)
471 BV_SET_WITH_ENDIANNESS (s16
, s16
)
472 VM_DEFINE_INSTRUCTION (151, bv_u32_set
, "bv-u32-set", 0, 4, 0)
473 BV_SET_WITH_ENDIANNESS (u32
, u32
)
474 VM_DEFINE_INSTRUCTION (152, bv_s32_set
, "bv-s32-set", 0, 4, 0)
475 BV_SET_WITH_ENDIANNESS (s32
, s32
)
476 VM_DEFINE_INSTRUCTION (153, bv_u64_set
, "bv-u64-set", 0, 4, 0)
477 BV_SET_WITH_ENDIANNESS (u64
, u64
)
478 VM_DEFINE_INSTRUCTION (154, bv_s64_set
, "bv-s64-set", 0, 4, 0)
479 BV_SET_WITH_ENDIANNESS (s64
, s64
)
480 VM_DEFINE_INSTRUCTION (155, bv_f32_set
, "bv-f32-set", 0, 4, 0)
481 BV_SET_WITH_ENDIANNESS (f32
, ieee_single
)
482 VM_DEFINE_INSTRUCTION (156, bv_f64_set
, "bv-f64-set", 0, 4, 0)
483 BV_SET_WITH_ENDIANNESS (f64
, ieee_double
)
485 #undef BV_SET_WITH_ENDIANNESS
487 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
490 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
491 VM_VALIDATE_BYTEVECTOR (bv); \
492 if (SCM_LIKELY (SCM_I_INUMP (idx) \
493 && ((i = SCM_I_INUM (idx)) >= 0) \
494 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
496 && (SCM_I_INUMP (val)) \
497 && ((j = SCM_I_INUM (val)) >= min) \
499 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
501 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
505 #define BV_INT_SET(stem, type, size) \
508 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
509 VM_VALIDATE_BYTEVECTOR (bv); \
510 if (SCM_LIKELY (SCM_I_INUMP (idx) \
511 && ((i = SCM_I_INUM (idx)) >= 0) \
512 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
513 && (i % size == 0))) \
514 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
516 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
520 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
523 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
524 VM_VALIDATE_BYTEVECTOR (bv); \
525 if (SCM_LIKELY (SCM_I_INUMP (idx) \
526 && ((i = SCM_I_INUM (idx)) >= 0) \
527 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
528 && (i % size == 0))) \
529 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
531 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
535 VM_DEFINE_INSTRUCTION (157, bv_u8_set
, "bv-u8-set", 0, 3, 0)
536 BV_FIXABLE_INT_SET (u8
, u8
, uint8
, 0, SCM_T_UINT8_MAX
, 1)
537 VM_DEFINE_INSTRUCTION (158, bv_s8_set
, "bv-s8-set", 0, 3, 0)
538 BV_FIXABLE_INT_SET (s8
, s8
, int8
, SCM_T_INT8_MIN
, SCM_T_INT8_MAX
, 1)
539 VM_DEFINE_INSTRUCTION (159, bv_u16_native_set
, "bv-u16-native-set", 0, 3, 0)
540 BV_FIXABLE_INT_SET (u16
, u16_native
, uint16
, 0, SCM_T_UINT16_MAX
, 2)
541 VM_DEFINE_INSTRUCTION (160, bv_s16_native_set
, "bv-s16-native-set", 0, 3, 0)
542 BV_FIXABLE_INT_SET (s16
, s16_native
, int16
, SCM_T_INT16_MIN
, SCM_T_INT16_MAX
, 2)
543 VM_DEFINE_INSTRUCTION (161, bv_u32_native_set
, "bv-u32-native-set", 0, 3, 0)
544 /* FIXME: u32 is always a fixnum on 64-bit builds */
545 BV_INT_SET (u32
, uint32
, 4)
546 VM_DEFINE_INSTRUCTION (162, bv_s32_native_set
, "bv-s32-native-set", 0, 3, 0)
547 BV_INT_SET (s32
, int32
, 4)
548 VM_DEFINE_INSTRUCTION (163, bv_u64_native_set
, "bv-u64-native-set", 0, 3, 0)
549 BV_INT_SET (u64
, uint64
, 8)
550 VM_DEFINE_INSTRUCTION (164, bv_s64_native_set
, "bv-s64-native-set", 0, 3, 0)
551 BV_INT_SET (s64
, int64
, 8)
552 VM_DEFINE_INSTRUCTION (165, bv_f32_native_set
, "bv-f32-native-set", 0, 3, 0)
553 BV_FLOAT_SET (f32
, ieee_single
, float, 4)
554 VM_DEFINE_INSTRUCTION (166, bv_f64_native_set
, "bv-f64-native-set", 0, 3, 0)
555 BV_FLOAT_SET (f64
, ieee_double
, double, 8)
557 #undef BV_FIXABLE_INT_SET
562 (defun renumber-ops ()
563 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
566 (let ((counter 99)) (goto-char (point-min))
567 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
569 (number-to-string (setq counter (1+ counter)))