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 (80, not, "not", 1)
35 RETURN (SCM_BOOL (SCM_FALSEP (x
)));
38 VM_DEFINE_FUNCTION (81, not_not
, "not-not", 1)
41 RETURN (SCM_BOOL (!SCM_FALSEP (x
)));
44 VM_DEFINE_FUNCTION (82, eq
, "eq?", 2)
47 RETURN (SCM_BOOL (SCM_EQ_P (x
, y
)));
50 VM_DEFINE_FUNCTION (83, not_eq, "not-eq?", 2)
53 RETURN (SCM_BOOL (!SCM_EQ_P (x
, y
)));
56 VM_DEFINE_FUNCTION (84, nullp
, "null?", 1)
59 RETURN (SCM_BOOL (SCM_NULLP (x
)));
62 VM_DEFINE_FUNCTION (85, not_nullp
, "not-null?", 1)
65 RETURN (SCM_BOOL (!SCM_NULLP (x
)));
68 VM_DEFINE_FUNCTION (86, eqv
, "eqv?", 2)
73 if (SCM_IMP (x
) || SCM_IMP (y
))
76 RETURN (scm_eqv_p (x
, y
));
79 VM_DEFINE_FUNCTION (87, equal
, "equal?", 2)
84 if (SCM_IMP (x
) || SCM_IMP (y
))
87 RETURN (scm_equal_p (x
, y
));
90 VM_DEFINE_FUNCTION (88, pairp
, "pair?", 1)
93 RETURN (SCM_BOOL (SCM_CONSP (x
)));
96 VM_DEFINE_FUNCTION (89, listp
, "list?", 1)
99 RETURN (SCM_BOOL (scm_ilength (x
) >= 0));
107 VM_DEFINE_FUNCTION (90, 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 (91, car
, "car", 1)
123 VM_VALIDATE_CONS (x
);
124 RETURN (SCM_CAR (x
));
127 VM_DEFINE_FUNCTION (92, cdr
, "cdr", 1)
130 VM_VALIDATE_CONS (x
);
131 RETURN (SCM_CDR (x
));
134 VM_DEFINE_INSTRUCTION (93, set_car
, "set-car!", 0, 2, 0)
139 VM_VALIDATE_CONS (x
);
144 VM_DEFINE_INSTRUCTION (94, 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 (95, ee
, "ee?", 2)
171 REL (==, scm_num_eq_p
);
174 VM_DEFINE_FUNCTION (96, lt
, "lt?", 2)
179 VM_DEFINE_FUNCTION (97, le
, "le?", 2)
184 VM_DEFINE_FUNCTION (98, gt
, "gt?", 2)
189 VM_DEFINE_FUNCTION (99, 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 (100, add
, "add", 2)
218 VM_DEFINE_FUNCTION (101, sub
, "sub", 2)
220 FUNC2 (-, scm_difference
);
223 VM_DEFINE_FUNCTION (102, mul
, "mul", 2)
227 RETURN (scm_product (x
, y
));
230 VM_DEFINE_FUNCTION (103, div
, "div", 2)
234 RETURN (scm_divide (x
, y
));
237 VM_DEFINE_FUNCTION (104, quo
, "quo", 2)
241 RETURN (scm_quotient (x
, y
));
244 VM_DEFINE_FUNCTION (105, rem
, "rem", 2)
248 RETURN (scm_remainder (x
, y
));
251 VM_DEFINE_FUNCTION (106, mod
, "mod", 2)
255 RETURN (scm_modulo (x
, y
));
262 VM_DEFINE_FUNCTION (107, slot_ref
, "slot-ref", 2)
265 ARGS2 (instance
, idx
);
266 slot
= SCM_I_INUM (idx
);
267 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance
) [slot
]));
270 VM_DEFINE_INSTRUCTION (108, slot_set
, "slot-set", 0, 3, 0)
272 SCM instance
, idx
, val
;
277 slot
= SCM_I_INUM (idx
);
278 SCM_STRUCT_DATA (instance
) [slot
] = SCM_UNPACK (val
);
282 VM_DEFINE_FUNCTION (109, vector_ref
, "vector-ref", 2)
286 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect
)
288 && ((i
= SCM_I_INUM (idx
)) >= 0)
289 && i
< SCM_I_VECTOR_LENGTH (vect
)))
290 RETURN (SCM_I_VECTOR_ELTS (vect
)[i
]);
292 RETURN (scm_vector_ref (vect
, idx
));
295 VM_DEFINE_INSTRUCTION (110, vector_set
, "vector-set", 0, 3, 0)
299 POP (val
); POP (idx
); POP (vect
);
300 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect
)
302 && ((i
= SCM_I_INUM (idx
)) >= 0)
303 && i
< SCM_I_VECTOR_LENGTH (vect
)))
304 SCM_I_VECTOR_WELTS (vect
)[i
] = val
;
306 scm_vector_set_x (vect
, idx
, val
);
310 #define VM_VALIDATE_BYTEVECTOR(x) \
311 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
313 goto vm_error_not_a_bytevector; \
316 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
320 if (scm_is_eq (endianness, scm_i_native_endianness)) \
321 goto VM_LABEL (bv_##stem##_native_ref); \
324 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
328 VM_DEFINE_FUNCTION (111, bv_u16_ref
, "bv-u16-ref", 3)
329 BV_REF_WITH_ENDIANNESS (u16
, u16
)
330 VM_DEFINE_FUNCTION (112, bv_s16_ref
, "bv-s16-ref", 3)
331 BV_REF_WITH_ENDIANNESS (s16
, s16
)
332 VM_DEFINE_FUNCTION (113, bv_u32_ref
, "bv-u32-ref", 3)
333 BV_REF_WITH_ENDIANNESS (u32
, u32
)
334 VM_DEFINE_FUNCTION (114, bv_s32_ref
, "bv-s32-ref", 3)
335 BV_REF_WITH_ENDIANNESS (s32
, s32
)
336 VM_DEFINE_FUNCTION (115, bv_u64_ref
, "bv-u64-ref", 3)
337 BV_REF_WITH_ENDIANNESS (u64
, u64
)
338 VM_DEFINE_FUNCTION (116, bv_s64_ref
, "bv-s64-ref", 3)
339 BV_REF_WITH_ENDIANNESS (s64
, s64
)
340 VM_DEFINE_FUNCTION (117, bv_f32_ref
, "bv-f32-ref", 3)
341 BV_REF_WITH_ENDIANNESS (f32
, ieee_single
)
342 VM_DEFINE_FUNCTION (118, bv_f64_ref
, "bv-f64-ref", 3)
343 BV_REF_WITH_ENDIANNESS (f64
, ieee_double
)
345 #undef BV_REF_WITH_ENDIANNESS
347 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
351 VM_VALIDATE_BYTEVECTOR (bv); \
352 if (SCM_LIKELY (SCM_I_INUMP (idx) \
353 && ((i = SCM_I_INUM (idx)) >= 0) \
354 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
355 && (i % size == 0))) \
356 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
357 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
359 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx)); \
362 #define BV_INT_REF(stem, type, size) \
366 VM_VALIDATE_BYTEVECTOR (bv); \
367 if (SCM_LIKELY (SCM_I_INUMP (idx) \
368 && ((i = SCM_I_INUM (idx)) >= 0) \
369 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
370 && (i % size == 0))) \
371 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
372 if (SCM_FIXABLE (x)) \
373 RETURN (SCM_I_MAKINUM (x)); \
375 RETURN (scm_from_##type (x)); \
378 RETURN (scm_bytevector_##stem##_native_ref (bv, idx)); \
381 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
385 VM_VALIDATE_BYTEVECTOR (bv); \
386 if (SCM_LIKELY (SCM_I_INUMP (idx) \
387 && ((i = SCM_I_INUM (idx)) >= 0) \
388 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
389 && (i % size == 0))) \
390 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
392 RETURN (scm_bytevector_##fn_stem##_native_ref (bv, idx)); \
395 VM_DEFINE_FUNCTION (119, bv_u8_ref
, "bv-u8-ref", 2)
396 BV_FIXABLE_INT_REF (u8
, u8
, uint8
, 1)
397 VM_DEFINE_FUNCTION (120, bv_s8_ref
, "bv-s8-ref", 2)
398 BV_FIXABLE_INT_REF (s8
, s8
, int8
, 1)
399 VM_DEFINE_FUNCTION (121, bv_u16_native_ref
, "bv-u16-native-ref", 2)
400 BV_FIXABLE_INT_REF (u16
, u16_native
, uint16
, 2)
401 VM_DEFINE_FUNCTION (122, bv_s16_native_ref
, "bv-s16-native-ref", 2)
402 BV_FIXABLE_INT_REF (s16
, s16_native
, int16
, 2)
403 VM_DEFINE_FUNCTION (123, bv_u32_native_ref
, "bv-u32-native-ref", 2)
404 /* FIXME: u32 is always a fixnum on 64-bit builds */
405 BV_INT_REF (u32
, uint32
, 4)
406 VM_DEFINE_FUNCTION (124, bv_s32_native_ref
, "bv-s32-native-ref", 2)
407 BV_INT_REF (s32
, int32
, 4)
408 VM_DEFINE_FUNCTION (125, bv_u64_native_ref
, "bv-u64-native-ref", 2)
409 BV_INT_REF (u64
, uint64
, 8)
410 VM_DEFINE_FUNCTION (126, bv_s64_native_ref
, "bv-s64-native-ref", 2)
411 BV_INT_REF (s64
, int64
, 8)
412 VM_DEFINE_FUNCTION (127, bv_f32_native_ref
, "bv-f32-native-ref", 2)
413 BV_FLOAT_REF (f32
, ieee_single
, float, 4)
414 VM_DEFINE_FUNCTION (128, bv_f64_native_ref
, "bv-f64-native-ref", 2)
415 BV_FLOAT_REF (f64
, ieee_double
, double, 8)
417 #undef BV_FIXABLE_INT_REF
423 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
427 if (scm_is_eq (endianness, scm_i_native_endianness)) \
428 goto VM_LABEL (bv_##stem##_native_set); \
430 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
431 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
436 VM_DEFINE_INSTRUCTION (129, bv_u16_set
, "bv-u16-set", 0, 4, 0)
437 BV_SET_WITH_ENDIANNESS (u16
, u16
)
438 VM_DEFINE_INSTRUCTION (130, bv_s16_set
, "bv-s16-set", 0, 4, 0)
439 BV_SET_WITH_ENDIANNESS (s16
, s16
)
440 VM_DEFINE_INSTRUCTION (131, bv_u32_set
, "bv-u32-set", 0, 4, 0)
441 BV_SET_WITH_ENDIANNESS (u32
, u32
)
442 VM_DEFINE_INSTRUCTION (132, bv_s32_set
, "bv-s32-set", 0, 4, 0)
443 BV_SET_WITH_ENDIANNESS (s32
, s32
)
444 VM_DEFINE_INSTRUCTION (133, bv_u64_set
, "bv-u64-set", 0, 4, 0)
445 BV_SET_WITH_ENDIANNESS (u64
, u64
)
446 VM_DEFINE_INSTRUCTION (134, bv_s64_set
, "bv-s64-set", 0, 4, 0)
447 BV_SET_WITH_ENDIANNESS (s64
, s64
)
448 VM_DEFINE_INSTRUCTION (135, bv_f32_set
, "bv-f32-set", 0, 4, 0)
449 BV_SET_WITH_ENDIANNESS (f32
, ieee_single
)
450 VM_DEFINE_INSTRUCTION (136, bv_f64_set
, "bv-f64-set", 0, 4, 0)
451 BV_SET_WITH_ENDIANNESS (f64
, ieee_double
)
453 #undef BV_SET_WITH_ENDIANNESS
455 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
458 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
459 VM_VALIDATE_BYTEVECTOR (bv); \
460 if (SCM_LIKELY (SCM_I_INUMP (idx) \
461 && ((i = SCM_I_INUM (idx)) >= 0) \
462 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
464 && (SCM_I_INUMP (val)) \
465 && ((j = SCM_I_INUM (val)) >= min) \
467 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
469 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
473 #define BV_INT_SET(stem, type, size) \
476 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
477 VM_VALIDATE_BYTEVECTOR (bv); \
478 if (SCM_LIKELY (SCM_I_INUMP (idx) \
479 && ((i = SCM_I_INUM (idx)) >= 0) \
480 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
481 && (i % size == 0))) \
482 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
484 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
488 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
491 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
492 VM_VALIDATE_BYTEVECTOR (bv); \
493 if (SCM_LIKELY (SCM_I_INUMP (idx) \
494 && ((i = SCM_I_INUM (idx)) >= 0) \
495 && (i < SCM_BYTEVECTOR_LENGTH (bv)) \
496 && (i % size == 0))) \
497 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
499 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
503 VM_DEFINE_INSTRUCTION (137, bv_u8_set
, "bv-u8-set", 0, 3, 0)
504 BV_FIXABLE_INT_SET (u8
, u8
, uint8
, 0, SCM_T_UINT8_MAX
, 1)
505 VM_DEFINE_INSTRUCTION (138, bv_s8_set
, "bv-s8-set", 0, 3, 0)
506 BV_FIXABLE_INT_SET (s8
, s8
, int8
, SCM_T_INT8_MIN
, SCM_T_INT8_MAX
, 1)
507 VM_DEFINE_INSTRUCTION (139, bv_u16_native_set
, "bv-u16-native-set", 0, 3, 0)
508 BV_FIXABLE_INT_SET (u16
, u16_native
, uint16
, 0, SCM_T_UINT16_MAX
, 2)
509 VM_DEFINE_INSTRUCTION (140, bv_s16_native_set
, "bv-s16-native-set", 0, 3, 0)
510 BV_FIXABLE_INT_SET (s16
, s16_native
, int16
, SCM_T_INT16_MIN
, SCM_T_INT16_MAX
, 2)
511 VM_DEFINE_INSTRUCTION (141, bv_u32_native_set
, "bv-u32-native-set", 0, 3, 0)
512 /* FIXME: u32 is always a fixnum on 64-bit builds */
513 BV_INT_SET (u32
, uint32
, 4)
514 VM_DEFINE_INSTRUCTION (142, bv_s32_native_set
, "bv-s32-native-set", 0, 3, 0)
515 BV_INT_SET (s32
, int32
, 4)
516 VM_DEFINE_INSTRUCTION (143, bv_u64_native_set
, "bv-u64-native-set", 0, 3, 0)
517 BV_INT_SET (u64
, uint64
, 8)
518 VM_DEFINE_INSTRUCTION (144, bv_s64_native_set
, "bv-s64-native-set", 0, 3, 0)
519 BV_INT_SET (s64
, int64
, 8)
520 VM_DEFINE_INSTRUCTION (145, bv_f32_native_set
, "bv-f32-native-set", 0, 3, 0)
521 BV_FLOAT_SET (f32
, ieee_single
, float, 4)
522 VM_DEFINE_INSTRUCTION (146, bv_f64_native_set
, "bv-f64-native-set", 0, 3, 0)
523 BV_FLOAT_SET (f64
, ieee_double
, double, 8)
525 #undef BV_FIXABLE_INT_SET
530 (defun renumber-ops ()
531 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
534 (let ((counter 79)) (goto-char (point-min))
535 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
537 (number-to-string (setq counter (1+ counter)))