Bump version number for 1.9.9.
[bpt/guile.git] / libguile / vm-i-scheme.c
1 /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19 /* This file is included in vm_engine.c */
20
21 \f
22 /*
23 * Predicates
24 */
25
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);
29
30 #define RETURN(x) do { *sp = x; NEXT; } while (0)
31
32 VM_DEFINE_FUNCTION (128, not, "not", 1)
33 {
34 ARGS1 (x);
35 RETURN (scm_from_bool (scm_is_false_or_nil (x)));
36 }
37
38 VM_DEFINE_FUNCTION (129, not_not, "not-not", 1)
39 {
40 ARGS1 (x);
41 RETURN (scm_from_bool (!scm_is_false_or_nil (x)));
42 }
43
44 VM_DEFINE_FUNCTION (130, eq, "eq?", 2)
45 {
46 ARGS2 (x, y);
47 RETURN (scm_from_bool (scm_is_eq (x, y)));
48 }
49
50 VM_DEFINE_FUNCTION (131, not_eq, "not-eq?", 2)
51 {
52 ARGS2 (x, y);
53 RETURN (scm_from_bool (!scm_is_eq (x, y)));
54 }
55
56 VM_DEFINE_FUNCTION (132, nullp, "null?", 1)
57 {
58 ARGS1 (x);
59 RETURN (scm_from_bool (scm_is_null_or_nil (x)));
60 }
61
62 VM_DEFINE_FUNCTION (133, not_nullp, "not-null?", 1)
63 {
64 ARGS1 (x);
65 RETURN (scm_from_bool (!scm_is_null_or_nil (x)));
66 }
67
68 VM_DEFINE_FUNCTION (134, eqv, "eqv?", 2)
69 {
70 ARGS2 (x, y);
71 if (scm_is_eq (x, y))
72 RETURN (SCM_BOOL_T);
73 if (SCM_IMP (x) || SCM_IMP (y))
74 RETURN (SCM_BOOL_F);
75 SYNC_REGISTER ();
76 RETURN (scm_eqv_p (x, y));
77 }
78
79 VM_DEFINE_FUNCTION (135, equal, "equal?", 2)
80 {
81 ARGS2 (x, y);
82 if (scm_is_eq (x, y))
83 RETURN (SCM_BOOL_T);
84 if (SCM_IMP (x) || SCM_IMP (y))
85 RETURN (SCM_BOOL_F);
86 SYNC_REGISTER ();
87 RETURN (scm_equal_p (x, y));
88 }
89
90 VM_DEFINE_FUNCTION (136, pairp, "pair?", 1)
91 {
92 ARGS1 (x);
93 RETURN (scm_from_bool (scm_is_pair (x)));
94 }
95
96 VM_DEFINE_FUNCTION (137, listp, "list?", 1)
97 {
98 ARGS1 (x);
99 RETURN (scm_from_bool (scm_ilength (x) >= 0));
100 }
101
102 \f
103 /*
104 * Basic data
105 */
106
107 VM_DEFINE_FUNCTION (138, cons, "cons", 2)
108 {
109 ARGS2 (x, y);
110 CONS (x, x, y);
111 RETURN (x);
112 }
113
114 #define VM_VALIDATE_CONS(x) \
115 if (SCM_UNLIKELY (!scm_is_pair (x))) \
116 { finish_args = x; \
117 goto vm_error_not_a_pair; \
118 }
119
120 VM_DEFINE_FUNCTION (139, car, "car", 1)
121 {
122 ARGS1 (x);
123 VM_VALIDATE_CONS (x);
124 RETURN (SCM_CAR (x));
125 }
126
127 VM_DEFINE_FUNCTION (140, cdr, "cdr", 1)
128 {
129 ARGS1 (x);
130 VM_VALIDATE_CONS (x);
131 RETURN (SCM_CDR (x));
132 }
133
134 VM_DEFINE_INSTRUCTION (141, set_car, "set-car!", 0, 2, 0)
135 {
136 SCM x, y;
137 POP (y);
138 POP (x);
139 VM_VALIDATE_CONS (x);
140 SCM_SETCAR (x, y);
141 NEXT;
142 }
143
144 VM_DEFINE_INSTRUCTION (142, set_cdr, "set-cdr!", 0, 2, 0)
145 {
146 SCM x, y;
147 POP (y);
148 POP (x);
149 VM_VALIDATE_CONS (x);
150 SCM_SETCDR (x, y);
151 NEXT;
152 }
153
154 \f
155 /*
156 * Numeric relational tests
157 */
158
159 #undef REL
160 #define REL(crel,srel) \
161 { \
162 ARGS2 (x, y); \
163 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
164 RETURN (scm_from_bool (SCM_I_INUM (x) crel SCM_I_INUM (y))); \
165 SYNC_REGISTER (); \
166 RETURN (srel (x, y)); \
167 }
168
169 VM_DEFINE_FUNCTION (143, ee, "ee?", 2)
170 {
171 REL (==, scm_num_eq_p);
172 }
173
174 VM_DEFINE_FUNCTION (144, lt, "lt?", 2)
175 {
176 REL (<, scm_less_p);
177 }
178
179 VM_DEFINE_FUNCTION (145, le, "le?", 2)
180 {
181 REL (<=, scm_leq_p);
182 }
183
184 VM_DEFINE_FUNCTION (146, gt, "gt?", 2)
185 {
186 REL (>, scm_gr_p);
187 }
188
189 VM_DEFINE_FUNCTION (147, ge, "ge?", 2)
190 {
191 REL (>=, scm_geq_p);
192 }
193
194 \f
195 /*
196 * Numeric functions
197 */
198
199 #undef FUNC2
200 #define FUNC2(CFUNC,SFUNC) \
201 { \
202 ARGS2 (x, y); \
203 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
204 { \
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)); \
208 } \
209 SYNC_REGISTER (); \
210 RETURN (SFUNC (x, y)); \
211 }
212
213 VM_DEFINE_FUNCTION (148, add, "add", 2)
214 {
215 FUNC2 (+, scm_sum);
216 }
217
218 VM_DEFINE_FUNCTION (149, add1, "add1", 1)
219 {
220 ARGS1 (x);
221 if (SCM_I_INUMP (x))
222 {
223 scm_t_int64 n = SCM_I_INUM (x) + 1;
224 if (SCM_FIXABLE (n))
225 RETURN (SCM_I_MAKINUM (n));
226 }
227 SYNC_REGISTER ();
228 RETURN (scm_sum (x, SCM_I_MAKINUM (1)));
229 }
230
231 VM_DEFINE_FUNCTION (150, sub, "sub", 2)
232 {
233 FUNC2 (-, scm_difference);
234 }
235
236 VM_DEFINE_FUNCTION (151, sub1, "sub1", 1)
237 {
238 ARGS1 (x);
239 if (SCM_I_INUMP (x))
240 {
241 scm_t_int64 n = SCM_I_INUM (x) - 1;
242 if (SCM_FIXABLE (n))
243 RETURN (SCM_I_MAKINUM (n));
244 }
245 SYNC_REGISTER ();
246 RETURN (scm_difference (x, SCM_I_MAKINUM (1)));
247 }
248
249 VM_DEFINE_FUNCTION (152, mul, "mul", 2)
250 {
251 ARGS2 (x, y);
252 SYNC_REGISTER ();
253 RETURN (scm_product (x, y));
254 }
255
256 VM_DEFINE_FUNCTION (153, div, "div", 2)
257 {
258 ARGS2 (x, y);
259 SYNC_REGISTER ();
260 RETURN (scm_divide (x, y));
261 }
262
263 VM_DEFINE_FUNCTION (154, quo, "quo", 2)
264 {
265 ARGS2 (x, y);
266 SYNC_REGISTER ();
267 RETURN (scm_quotient (x, y));
268 }
269
270 VM_DEFINE_FUNCTION (155, rem, "rem", 2)
271 {
272 ARGS2 (x, y);
273 SYNC_REGISTER ();
274 RETURN (scm_remainder (x, y));
275 }
276
277 VM_DEFINE_FUNCTION (156, mod, "mod", 2)
278 {
279 ARGS2 (x, y);
280 SYNC_REGISTER ();
281 RETURN (scm_modulo (x, y));
282 }
283
284 VM_DEFINE_FUNCTION (157, ash, "ash", 2)
285 {
286 ARGS2 (x, y);
287 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
288 {
289 if (SCM_I_INUM (y) < 0)
290 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) >> -SCM_I_INUM (y)));
291 else if ((SCM_I_INUM (x) << SCM_I_INUM (y)) >> SCM_I_INUM (y)
292 == SCM_I_INUM (x))
293 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) << SCM_I_INUM (y)));
294 /* fall through */
295 }
296 SYNC_REGISTER ();
297 RETURN (scm_ash (x, y));
298 }
299
300 VM_DEFINE_FUNCTION (158, logand, "logand", 2)
301 {
302 ARGS2 (x, y);
303 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
304 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) & SCM_I_INUM (y)));
305 SYNC_REGISTER ();
306 RETURN (scm_logand (x, y));
307 }
308
309 VM_DEFINE_FUNCTION (159, logior, "logior", 2)
310 {
311 ARGS2 (x, y);
312 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
313 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) | SCM_I_INUM (y)));
314 SYNC_REGISTER ();
315 RETURN (scm_logior (x, y));
316 }
317
318 VM_DEFINE_FUNCTION (160, logxor, "logxor", 2)
319 {
320 ARGS2 (x, y);
321 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
322 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) ^ SCM_I_INUM (y)));
323 SYNC_REGISTER ();
324 RETURN (scm_logxor (x, y));
325 }
326
327 \f
328 /*
329 * Vectors and arrays
330 */
331
332 VM_DEFINE_FUNCTION (161, vector_ref, "vector-ref", 2)
333 {
334 long i = 0;
335 ARGS2 (vect, idx);
336 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
337 && SCM_I_INUMP (idx)
338 && ((i = SCM_I_INUM (idx)) >= 0)
339 && i < SCM_I_VECTOR_LENGTH (vect)))
340 RETURN (SCM_I_VECTOR_ELTS (vect)[i]);
341 else
342 {
343 SYNC_REGISTER ();
344 RETURN (scm_vector_ref (vect, idx));
345 }
346 }
347
348 VM_DEFINE_INSTRUCTION (162, vector_set, "vector-set", 0, 3, 0)
349 {
350 long i = 0;
351 SCM vect, idx, val;
352 POP (val); POP (idx); POP (vect);
353 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
354 && SCM_I_INUMP (idx)
355 && ((i = SCM_I_INUM (idx)) >= 0)
356 && i < SCM_I_VECTOR_LENGTH (vect)))
357 SCM_I_VECTOR_WELTS (vect)[i] = val;
358 else
359 {
360 SYNC_REGISTER ();
361 scm_vector_set_x (vect, idx, val);
362 }
363 NEXT;
364 }
365
366 VM_DEFINE_INSTRUCTION (163, make_array, "make-array", 3, -1, 1)
367 {
368 scm_t_uint32 len;
369 SCM shape, ret;
370
371 len = FETCH ();
372 len = (len << 8) + FETCH ();
373 len = (len << 8) + FETCH ();
374 POP (shape);
375 SYNC_REGISTER ();
376 ret = scm_from_contiguous_array (shape, sp - len + 1, len);
377 DROPN (len);
378 PUSH (ret);
379 NEXT;
380 }
381
382 \f
383 /*
384 * Structs
385 */
386 #define VM_VALIDATE_STRUCT(obj) \
387 if (SCM_UNLIKELY (!SCM_STRUCTP (obj))) \
388 { \
389 finish_args = (obj); \
390 goto vm_error_not_a_struct; \
391 }
392
393 VM_DEFINE_FUNCTION (164, struct_p, "struct?", 1)
394 {
395 ARGS1 (obj);
396 RETURN (scm_from_bool (SCM_STRUCTP (obj)));
397 }
398
399 VM_DEFINE_FUNCTION (165, struct_vtable, "struct-vtable", 1)
400 {
401 ARGS1 (obj);
402 VM_VALIDATE_STRUCT (obj);
403 RETURN (SCM_STRUCT_VTABLE (obj));
404 }
405
406 VM_DEFINE_INSTRUCTION (166, make_struct, "make-struct", 2, -1, 1)
407 {
408 unsigned h = FETCH ();
409 unsigned l = FETCH ();
410 scm_t_bits n_args = ((h << 8U) + l);
411 SCM vtable = sp[1 - n_args], n_tail = sp[2 - n_args];
412 const SCM *inits = sp - n_args + 3;
413
414 sp -= n_args - 1;
415
416 SYNC_REGISTER ();
417
418 if (SCM_LIKELY (SCM_STRUCTP (vtable)
419 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
420 && SCM_I_INUMP (n_tail)))
421 {
422 scm_t_bits n_inits, len;
423
424 n_inits = SCM_I_INUM (n_tail) + n_args - 2;
425 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
426
427 if (SCM_LIKELY (n_inits == len))
428 {
429 SCM obj;
430
431 obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), n_inits);
432 memcpy (SCM_STRUCT_DATA (obj), inits, n_inits * sizeof (SCM));
433
434 RETURN (obj);
435 }
436 }
437
438 RETURN (scm_c_make_structv (vtable, scm_to_size_t (n_tail),
439 n_args - 2, (scm_t_bits *) inits));
440 }
441
442 VM_DEFINE_FUNCTION (167, struct_ref, "struct-ref", 2)
443 {
444 ARGS2 (obj, pos);
445
446 if (SCM_LIKELY (SCM_STRUCTP (obj)
447 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
448 SCM_VTABLE_FLAG_SIMPLE)
449 && SCM_I_INUMP (pos)))
450 {
451 SCM vtable;
452 scm_t_bits index, len;
453
454 index = SCM_I_INUM (pos);
455 vtable = SCM_STRUCT_VTABLE (obj);
456 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
457
458 if (SCM_LIKELY (index < len))
459 {
460 scm_t_bits *data = SCM_STRUCT_DATA (obj);
461 RETURN (SCM_PACK (data[index]));
462 }
463 }
464
465 SYNC_REGISTER ();
466 RETURN (scm_struct_ref (obj, pos));
467 }
468
469 VM_DEFINE_FUNCTION (168, struct_set, "struct-set", 3)
470 {
471 ARGS3 (obj, pos, val);
472
473 if (SCM_LIKELY (SCM_STRUCTP (obj)
474 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
475 SCM_VTABLE_FLAG_SIMPLE)
476 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
477 SCM_VTABLE_FLAG_SIMPLE_RW)
478 && SCM_I_INUMP (pos)))
479 {
480 SCM vtable;
481 scm_t_bits index, len;
482
483 index = SCM_I_INUM (pos);
484 vtable = SCM_STRUCT_VTABLE (obj);
485 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
486 if (SCM_LIKELY (index < len))
487 {
488 scm_t_bits *data = SCM_STRUCT_DATA (obj);
489 data[index] = SCM_UNPACK (val);
490 RETURN (val);
491 }
492 }
493
494 SYNC_REGISTER ();
495 RETURN (scm_struct_set_x (obj, pos, val));
496 }
497
498 \f
499 /*
500 * GOOPS support
501 */
502 VM_DEFINE_FUNCTION (169, class_of, "class-of", 1)
503 {
504 ARGS1 (obj);
505 RETURN (SCM_INSTANCEP (obj) ? SCM_CLASS_OF (obj) : scm_class_of (obj));
506 }
507
508 VM_DEFINE_FUNCTION (170, slot_ref, "slot-ref", 2)
509 {
510 size_t slot;
511 ARGS2 (instance, idx);
512 slot = SCM_I_INUM (idx);
513 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
514 }
515
516 VM_DEFINE_INSTRUCTION (171, slot_set, "slot-set", 0, 3, 0)
517 {
518 SCM instance, idx, val;
519 size_t slot;
520 POP (val);
521 POP (idx);
522 POP (instance);
523 slot = SCM_I_INUM (idx);
524 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
525 NEXT;
526 }
527
528 \f
529 /*
530 * Bytevectors
531 */
532 #define VM_VALIDATE_BYTEVECTOR(x) \
533 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
534 { finish_args = x; \
535 goto vm_error_not_a_bytevector; \
536 }
537
538 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
539 { \
540 SCM endianness; \
541 POP (endianness); \
542 if (scm_is_eq (endianness, scm_i_native_endianness)) \
543 goto VM_LABEL (bv_##stem##_native_ref); \
544 { \
545 ARGS2 (bv, idx); \
546 SYNC_REGISTER (); \
547 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
548 } \
549 }
550
551 VM_DEFINE_FUNCTION (172, bv_u16_ref, "bv-u16-ref", 3)
552 BV_REF_WITH_ENDIANNESS (u16, u16)
553 VM_DEFINE_FUNCTION (173, bv_s16_ref, "bv-s16-ref", 3)
554 BV_REF_WITH_ENDIANNESS (s16, s16)
555 VM_DEFINE_FUNCTION (174, bv_u32_ref, "bv-u32-ref", 3)
556 BV_REF_WITH_ENDIANNESS (u32, u32)
557 VM_DEFINE_FUNCTION (175, bv_s32_ref, "bv-s32-ref", 3)
558 BV_REF_WITH_ENDIANNESS (s32, s32)
559 VM_DEFINE_FUNCTION (176, bv_u64_ref, "bv-u64-ref", 3)
560 BV_REF_WITH_ENDIANNESS (u64, u64)
561 VM_DEFINE_FUNCTION (177, bv_s64_ref, "bv-s64-ref", 3)
562 BV_REF_WITH_ENDIANNESS (s64, s64)
563 VM_DEFINE_FUNCTION (178, bv_f32_ref, "bv-f32-ref", 3)
564 BV_REF_WITH_ENDIANNESS (f32, ieee_single)
565 VM_DEFINE_FUNCTION (179, bv_f64_ref, "bv-f64-ref", 3)
566 BV_REF_WITH_ENDIANNESS (f64, ieee_double)
567
568 #undef BV_REF_WITH_ENDIANNESS
569
570 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
571 { \
572 long i = 0; \
573 ARGS2 (bv, idx); \
574 VM_VALIDATE_BYTEVECTOR (bv); \
575 if (SCM_LIKELY (SCM_I_INUMP (idx) \
576 && ((i = SCM_I_INUM (idx)) >= 0) \
577 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
578 && (i % size == 0))) \
579 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
580 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
581 else \
582 { \
583 SYNC_REGISTER (); \
584 RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
585 } \
586 }
587
588 #define BV_INT_REF(stem, type, size) \
589 { \
590 long i = 0; \
591 ARGS2 (bv, idx); \
592 VM_VALIDATE_BYTEVECTOR (bv); \
593 if (SCM_LIKELY (SCM_I_INUMP (idx) \
594 && ((i = SCM_I_INUM (idx)) >= 0) \
595 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
596 && (i % size == 0))) \
597 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
598 if (SCM_FIXABLE (x)) \
599 RETURN (SCM_I_MAKINUM (x)); \
600 else \
601 { \
602 SYNC_REGISTER (); \
603 RETURN (scm_from_ ## type (x)); \
604 } \
605 } \
606 else \
607 { \
608 SYNC_REGISTER (); \
609 RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
610 } \
611 }
612
613 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
614 { \
615 long i = 0; \
616 ARGS2 (bv, idx); \
617 VM_VALIDATE_BYTEVECTOR (bv); \
618 SYNC_REGISTER (); \
619 if (SCM_LIKELY (SCM_I_INUMP (idx) \
620 && ((i = SCM_I_INUM (idx)) >= 0) \
621 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
622 && (i % size == 0))) \
623 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
624 else \
625 RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
626 }
627
628 VM_DEFINE_FUNCTION (180, bv_u8_ref, "bv-u8-ref", 2)
629 BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
630 VM_DEFINE_FUNCTION (181, bv_s8_ref, "bv-s8-ref", 2)
631 BV_FIXABLE_INT_REF (s8, s8, int8, 1)
632 VM_DEFINE_FUNCTION (182, bv_u16_native_ref, "bv-u16-native-ref", 2)
633 BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
634 VM_DEFINE_FUNCTION (183, bv_s16_native_ref, "bv-s16-native-ref", 2)
635 BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
636 VM_DEFINE_FUNCTION (184, bv_u32_native_ref, "bv-u32-native-ref", 2)
637 #if SIZEOF_VOID_P > 4
638 BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4)
639 #else
640 BV_INT_REF (u32, uint32, 4)
641 #endif
642 VM_DEFINE_FUNCTION (185, bv_s32_native_ref, "bv-s32-native-ref", 2)
643 #if SIZEOF_VOID_P > 4
644 BV_FIXABLE_INT_REF (s32, s32_native, int32, 4)
645 #else
646 BV_INT_REF (s32, int32, 4)
647 #endif
648 VM_DEFINE_FUNCTION (186, bv_u64_native_ref, "bv-u64-native-ref", 2)
649 BV_INT_REF (u64, uint64, 8)
650 VM_DEFINE_FUNCTION (187, bv_s64_native_ref, "bv-s64-native-ref", 2)
651 BV_INT_REF (s64, int64, 8)
652 VM_DEFINE_FUNCTION (188, bv_f32_native_ref, "bv-f32-native-ref", 2)
653 BV_FLOAT_REF (f32, ieee_single, float, 4)
654 VM_DEFINE_FUNCTION (189, bv_f64_native_ref, "bv-f64-native-ref", 2)
655 BV_FLOAT_REF (f64, ieee_double, double, 8)
656
657 #undef BV_FIXABLE_INT_REF
658 #undef BV_INT_REF
659 #undef BV_FLOAT_REF
660
661
662
663 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
664 { \
665 SCM endianness; \
666 POP (endianness); \
667 if (scm_is_eq (endianness, scm_i_native_endianness)) \
668 goto VM_LABEL (bv_##stem##_native_set); \
669 { \
670 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
671 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
672 NEXT; \
673 } \
674 }
675
676 VM_DEFINE_INSTRUCTION (190, bv_u16_set, "bv-u16-set", 0, 4, 0)
677 BV_SET_WITH_ENDIANNESS (u16, u16)
678 VM_DEFINE_INSTRUCTION (191, bv_s16_set, "bv-s16-set", 0, 4, 0)
679 BV_SET_WITH_ENDIANNESS (s16, s16)
680 VM_DEFINE_INSTRUCTION (192, bv_u32_set, "bv-u32-set", 0, 4, 0)
681 BV_SET_WITH_ENDIANNESS (u32, u32)
682 VM_DEFINE_INSTRUCTION (193, bv_s32_set, "bv-s32-set", 0, 4, 0)
683 BV_SET_WITH_ENDIANNESS (s32, s32)
684 VM_DEFINE_INSTRUCTION (194, bv_u64_set, "bv-u64-set", 0, 4, 0)
685 BV_SET_WITH_ENDIANNESS (u64, u64)
686 VM_DEFINE_INSTRUCTION (195, bv_s64_set, "bv-s64-set", 0, 4, 0)
687 BV_SET_WITH_ENDIANNESS (s64, s64)
688 VM_DEFINE_INSTRUCTION (196, bv_f32_set, "bv-f32-set", 0, 4, 0)
689 BV_SET_WITH_ENDIANNESS (f32, ieee_single)
690 VM_DEFINE_INSTRUCTION (197, bv_f64_set, "bv-f64-set", 0, 4, 0)
691 BV_SET_WITH_ENDIANNESS (f64, ieee_double)
692
693 #undef BV_SET_WITH_ENDIANNESS
694
695 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
696 { \
697 long i = 0, j = 0; \
698 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
699 VM_VALIDATE_BYTEVECTOR (bv); \
700 if (SCM_LIKELY (SCM_I_INUMP (idx) \
701 && ((i = SCM_I_INUM (idx)) >= 0) \
702 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
703 && (i % size == 0) \
704 && (SCM_I_INUMP (val)) \
705 && ((j = SCM_I_INUM (val)) >= min) \
706 && (j <= max))) \
707 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
708 else \
709 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
710 NEXT; \
711 }
712
713 #define BV_INT_SET(stem, type, size) \
714 { \
715 long i = 0; \
716 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
717 VM_VALIDATE_BYTEVECTOR (bv); \
718 if (SCM_LIKELY (SCM_I_INUMP (idx) \
719 && ((i = SCM_I_INUM (idx)) >= 0) \
720 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
721 && (i % size == 0))) \
722 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
723 else \
724 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
725 NEXT; \
726 }
727
728 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
729 { \
730 long i = 0; \
731 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
732 VM_VALIDATE_BYTEVECTOR (bv); \
733 if (SCM_LIKELY (SCM_I_INUMP (idx) \
734 && ((i = SCM_I_INUM (idx)) >= 0) \
735 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
736 && (i % size == 0))) \
737 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
738 else \
739 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
740 NEXT; \
741 }
742
743 VM_DEFINE_INSTRUCTION (198, bv_u8_set, "bv-u8-set", 0, 3, 0)
744 BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
745 VM_DEFINE_INSTRUCTION (199, bv_s8_set, "bv-s8-set", 0, 3, 0)
746 BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
747 VM_DEFINE_INSTRUCTION (200, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
748 BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
749 VM_DEFINE_INSTRUCTION (201, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
750 BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
751 VM_DEFINE_INSTRUCTION (202, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
752 #if SIZEOF_VOID_P > 4
753 BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4)
754 #else
755 BV_INT_SET (u32, uint32, 4)
756 #endif
757 VM_DEFINE_INSTRUCTION (203, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
758 #if SIZEOF_VOID_P > 4
759 BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4)
760 #else
761 BV_INT_SET (s32, int32, 4)
762 #endif
763 VM_DEFINE_INSTRUCTION (204, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
764 BV_INT_SET (u64, uint64, 8)
765 VM_DEFINE_INSTRUCTION (205, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
766 BV_INT_SET (s64, int64, 8)
767 VM_DEFINE_INSTRUCTION (206, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
768 BV_FLOAT_SET (f32, ieee_single, float, 4)
769 VM_DEFINE_INSTRUCTION (207, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
770 BV_FLOAT_SET (f64, ieee_double, double, 8)
771
772 #undef BV_FIXABLE_INT_SET
773 #undef BV_INT_SET
774 #undef BV_FLOAT_SET
775
776 /*
777 (defun renumber-ops ()
778 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
779 (interactive "")
780 (save-excursion
781 (let ((counter 127)) (goto-char (point-min))
782 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
783 (replace-match
784 (number-to-string (setq counter (1+ counter)))
785 t t nil 1)))))
786 */
787
788 /*
789 Local Variables:
790 c-file-style: "gnu"
791 End:
792 */