fix bug in ash opcode
[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 (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 (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 (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 (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 /* Right shift, will be a fixnum. */
291 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) >> -SCM_I_INUM (y)));
292 else
293 /* Left shift. See comments in scm_ash. */
294 {
295 long nn, bits_to_shift;
296
297 nn = SCM_I_INUM (x);
298 bits_to_shift = SCM_I_INUM (y);
299
300 if (bits_to_shift < SCM_I_FIXNUM_BIT-1
301 && ((unsigned long)
302 (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
303 <= 1))
304 RETURN (SCM_I_MAKINUM (nn << bits_to_shift));
305 /* fall through */
306 }
307 /* fall through */
308 }
309 SYNC_REGISTER ();
310 RETURN (scm_ash (x, y));
311 }
312
313 VM_DEFINE_FUNCTION (158, logand, "logand", 2)
314 {
315 ARGS2 (x, y);
316 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
317 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) & SCM_I_INUM (y)));
318 SYNC_REGISTER ();
319 RETURN (scm_logand (x, y));
320 }
321
322 VM_DEFINE_FUNCTION (159, logior, "logior", 2)
323 {
324 ARGS2 (x, y);
325 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
326 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) | SCM_I_INUM (y)));
327 SYNC_REGISTER ();
328 RETURN (scm_logior (x, y));
329 }
330
331 VM_DEFINE_FUNCTION (160, logxor, "logxor", 2)
332 {
333 ARGS2 (x, y);
334 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
335 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) ^ SCM_I_INUM (y)));
336 SYNC_REGISTER ();
337 RETURN (scm_logxor (x, y));
338 }
339
340 \f
341 /*
342 * Vectors and arrays
343 */
344
345 VM_DEFINE_FUNCTION (161, vector_ref, "vector-ref", 2)
346 {
347 long i = 0;
348 ARGS2 (vect, idx);
349 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
350 && SCM_I_INUMP (idx)
351 && ((i = SCM_I_INUM (idx)) >= 0)
352 && i < SCM_I_VECTOR_LENGTH (vect)))
353 RETURN (SCM_I_VECTOR_ELTS (vect)[i]);
354 else
355 {
356 SYNC_REGISTER ();
357 RETURN (scm_vector_ref (vect, idx));
358 }
359 }
360
361 VM_DEFINE_INSTRUCTION (162, vector_set, "vector-set", 0, 3, 0)
362 {
363 long i = 0;
364 SCM vect, idx, val;
365 POP (val); POP (idx); POP (vect);
366 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
367 && SCM_I_INUMP (idx)
368 && ((i = SCM_I_INUM (idx)) >= 0)
369 && i < SCM_I_VECTOR_LENGTH (vect)))
370 SCM_I_VECTOR_WELTS (vect)[i] = val;
371 else
372 {
373 SYNC_REGISTER ();
374 scm_vector_set_x (vect, idx, val);
375 }
376 NEXT;
377 }
378
379 VM_DEFINE_INSTRUCTION (163, make_array, "make-array", 3, -1, 1)
380 {
381 scm_t_uint32 len;
382 SCM shape, ret;
383
384 len = FETCH ();
385 len = (len << 8) + FETCH ();
386 len = (len << 8) + FETCH ();
387 POP (shape);
388 SYNC_REGISTER ();
389 ret = scm_from_contiguous_array (shape, sp - len + 1, len);
390 DROPN (len);
391 PUSH (ret);
392 NEXT;
393 }
394
395 \f
396 /*
397 * Structs
398 */
399 #define VM_VALIDATE_STRUCT(obj) \
400 if (SCM_UNLIKELY (!SCM_STRUCTP (obj))) \
401 { \
402 finish_args = (obj); \
403 goto vm_error_not_a_struct; \
404 }
405
406 VM_DEFINE_FUNCTION (164, struct_p, "struct?", 1)
407 {
408 ARGS1 (obj);
409 RETURN (scm_from_bool (SCM_STRUCTP (obj)));
410 }
411
412 VM_DEFINE_FUNCTION (165, struct_vtable, "struct-vtable", 1)
413 {
414 ARGS1 (obj);
415 VM_VALIDATE_STRUCT (obj);
416 RETURN (SCM_STRUCT_VTABLE (obj));
417 }
418
419 VM_DEFINE_INSTRUCTION (166, make_struct, "make-struct", 2, -1, 1)
420 {
421 unsigned h = FETCH ();
422 unsigned l = FETCH ();
423 scm_t_bits n_args = ((h << 8U) + l);
424 SCM vtable = sp[1 - n_args], n_tail = sp[2 - n_args];
425 const SCM *inits = sp - n_args + 3;
426
427 sp -= n_args - 1;
428
429 SYNC_REGISTER ();
430
431 if (SCM_LIKELY (SCM_STRUCTP (vtable)
432 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
433 && SCM_I_INUMP (n_tail)))
434 {
435 scm_t_bits n_inits, len;
436
437 n_inits = SCM_I_INUM (n_tail) + n_args - 2;
438 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
439
440 if (SCM_LIKELY (n_inits == len))
441 {
442 SCM obj;
443
444 obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), n_inits);
445 memcpy (SCM_STRUCT_DATA (obj), inits, n_inits * sizeof (SCM));
446
447 RETURN (obj);
448 }
449 }
450
451 RETURN (scm_c_make_structv (vtable, scm_to_size_t (n_tail),
452 n_args - 2, (scm_t_bits *) inits));
453 }
454
455 VM_DEFINE_FUNCTION (167, struct_ref, "struct-ref", 2)
456 {
457 ARGS2 (obj, pos);
458
459 if (SCM_LIKELY (SCM_STRUCTP (obj)
460 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
461 SCM_VTABLE_FLAG_SIMPLE)
462 && SCM_I_INUMP (pos)))
463 {
464 SCM vtable;
465 scm_t_bits index, len;
466
467 index = SCM_I_INUM (pos);
468 vtable = SCM_STRUCT_VTABLE (obj);
469 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
470
471 if (SCM_LIKELY (index < len))
472 {
473 scm_t_bits *data = SCM_STRUCT_DATA (obj);
474 RETURN (SCM_PACK (data[index]));
475 }
476 }
477
478 SYNC_REGISTER ();
479 RETURN (scm_struct_ref (obj, pos));
480 }
481
482 VM_DEFINE_FUNCTION (168, struct_set, "struct-set", 3)
483 {
484 ARGS3 (obj, pos, val);
485
486 if (SCM_LIKELY (SCM_STRUCTP (obj)
487 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
488 SCM_VTABLE_FLAG_SIMPLE)
489 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
490 SCM_VTABLE_FLAG_SIMPLE_RW)
491 && SCM_I_INUMP (pos)))
492 {
493 SCM vtable;
494 scm_t_bits index, len;
495
496 index = SCM_I_INUM (pos);
497 vtable = SCM_STRUCT_VTABLE (obj);
498 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
499 if (SCM_LIKELY (index < len))
500 {
501 scm_t_bits *data = SCM_STRUCT_DATA (obj);
502 data[index] = SCM_UNPACK (val);
503 RETURN (val);
504 }
505 }
506
507 SYNC_REGISTER ();
508 RETURN (scm_struct_set_x (obj, pos, val));
509 }
510
511 \f
512 /*
513 * GOOPS support
514 */
515 VM_DEFINE_FUNCTION (169, class_of, "class-of", 1)
516 {
517 ARGS1 (obj);
518 RETURN (SCM_INSTANCEP (obj) ? SCM_CLASS_OF (obj) : scm_class_of (obj));
519 }
520
521 VM_DEFINE_FUNCTION (170, slot_ref, "slot-ref", 2)
522 {
523 size_t slot;
524 ARGS2 (instance, idx);
525 slot = SCM_I_INUM (idx);
526 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
527 }
528
529 VM_DEFINE_INSTRUCTION (171, slot_set, "slot-set", 0, 3, 0)
530 {
531 SCM instance, idx, val;
532 size_t slot;
533 POP (val);
534 POP (idx);
535 POP (instance);
536 slot = SCM_I_INUM (idx);
537 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
538 NEXT;
539 }
540
541 \f
542 /*
543 * Bytevectors
544 */
545 #define VM_VALIDATE_BYTEVECTOR(x) \
546 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
547 { finish_args = x; \
548 goto vm_error_not_a_bytevector; \
549 }
550
551 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
552 { \
553 SCM endianness; \
554 POP (endianness); \
555 if (scm_is_eq (endianness, scm_i_native_endianness)) \
556 goto VM_LABEL (bv_##stem##_native_ref); \
557 { \
558 ARGS2 (bv, idx); \
559 SYNC_REGISTER (); \
560 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
561 } \
562 }
563
564 VM_DEFINE_FUNCTION (172, bv_u16_ref, "bv-u16-ref", 3)
565 BV_REF_WITH_ENDIANNESS (u16, u16)
566 VM_DEFINE_FUNCTION (173, bv_s16_ref, "bv-s16-ref", 3)
567 BV_REF_WITH_ENDIANNESS (s16, s16)
568 VM_DEFINE_FUNCTION (174, bv_u32_ref, "bv-u32-ref", 3)
569 BV_REF_WITH_ENDIANNESS (u32, u32)
570 VM_DEFINE_FUNCTION (175, bv_s32_ref, "bv-s32-ref", 3)
571 BV_REF_WITH_ENDIANNESS (s32, s32)
572 VM_DEFINE_FUNCTION (176, bv_u64_ref, "bv-u64-ref", 3)
573 BV_REF_WITH_ENDIANNESS (u64, u64)
574 VM_DEFINE_FUNCTION (177, bv_s64_ref, "bv-s64-ref", 3)
575 BV_REF_WITH_ENDIANNESS (s64, s64)
576 VM_DEFINE_FUNCTION (178, bv_f32_ref, "bv-f32-ref", 3)
577 BV_REF_WITH_ENDIANNESS (f32, ieee_single)
578 VM_DEFINE_FUNCTION (179, bv_f64_ref, "bv-f64-ref", 3)
579 BV_REF_WITH_ENDIANNESS (f64, ieee_double)
580
581 #undef BV_REF_WITH_ENDIANNESS
582
583 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
584 { \
585 long i = 0; \
586 ARGS2 (bv, idx); \
587 VM_VALIDATE_BYTEVECTOR (bv); \
588 if (SCM_LIKELY (SCM_I_INUMP (idx) \
589 && ((i = SCM_I_INUM (idx)) >= 0) \
590 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
591 && (i % size == 0))) \
592 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
593 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
594 else \
595 { \
596 SYNC_REGISTER (); \
597 RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
598 } \
599 }
600
601 #define BV_INT_REF(stem, type, size) \
602 { \
603 long i = 0; \
604 ARGS2 (bv, idx); \
605 VM_VALIDATE_BYTEVECTOR (bv); \
606 if (SCM_LIKELY (SCM_I_INUMP (idx) \
607 && ((i = SCM_I_INUM (idx)) >= 0) \
608 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
609 && (i % size == 0))) \
610 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
611 if (SCM_FIXABLE (x)) \
612 RETURN (SCM_I_MAKINUM (x)); \
613 else \
614 { \
615 SYNC_REGISTER (); \
616 RETURN (scm_from_ ## type (x)); \
617 } \
618 } \
619 else \
620 { \
621 SYNC_REGISTER (); \
622 RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
623 } \
624 }
625
626 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
627 { \
628 long i = 0; \
629 ARGS2 (bv, idx); \
630 VM_VALIDATE_BYTEVECTOR (bv); \
631 SYNC_REGISTER (); \
632 if (SCM_LIKELY (SCM_I_INUMP (idx) \
633 && ((i = SCM_I_INUM (idx)) >= 0) \
634 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
635 && (i % size == 0))) \
636 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
637 else \
638 RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
639 }
640
641 VM_DEFINE_FUNCTION (180, bv_u8_ref, "bv-u8-ref", 2)
642 BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
643 VM_DEFINE_FUNCTION (181, bv_s8_ref, "bv-s8-ref", 2)
644 BV_FIXABLE_INT_REF (s8, s8, int8, 1)
645 VM_DEFINE_FUNCTION (182, bv_u16_native_ref, "bv-u16-native-ref", 2)
646 BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
647 VM_DEFINE_FUNCTION (183, bv_s16_native_ref, "bv-s16-native-ref", 2)
648 BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
649 VM_DEFINE_FUNCTION (184, bv_u32_native_ref, "bv-u32-native-ref", 2)
650 #if SIZEOF_VOID_P > 4
651 BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4)
652 #else
653 BV_INT_REF (u32, uint32, 4)
654 #endif
655 VM_DEFINE_FUNCTION (185, bv_s32_native_ref, "bv-s32-native-ref", 2)
656 #if SIZEOF_VOID_P > 4
657 BV_FIXABLE_INT_REF (s32, s32_native, int32, 4)
658 #else
659 BV_INT_REF (s32, int32, 4)
660 #endif
661 VM_DEFINE_FUNCTION (186, bv_u64_native_ref, "bv-u64-native-ref", 2)
662 BV_INT_REF (u64, uint64, 8)
663 VM_DEFINE_FUNCTION (187, bv_s64_native_ref, "bv-s64-native-ref", 2)
664 BV_INT_REF (s64, int64, 8)
665 VM_DEFINE_FUNCTION (188, bv_f32_native_ref, "bv-f32-native-ref", 2)
666 BV_FLOAT_REF (f32, ieee_single, float, 4)
667 VM_DEFINE_FUNCTION (189, bv_f64_native_ref, "bv-f64-native-ref", 2)
668 BV_FLOAT_REF (f64, ieee_double, double, 8)
669
670 #undef BV_FIXABLE_INT_REF
671 #undef BV_INT_REF
672 #undef BV_FLOAT_REF
673
674
675
676 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
677 { \
678 SCM endianness; \
679 POP (endianness); \
680 if (scm_is_eq (endianness, scm_i_native_endianness)) \
681 goto VM_LABEL (bv_##stem##_native_set); \
682 { \
683 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
684 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
685 NEXT; \
686 } \
687 }
688
689 VM_DEFINE_INSTRUCTION (190, bv_u16_set, "bv-u16-set", 0, 4, 0)
690 BV_SET_WITH_ENDIANNESS (u16, u16)
691 VM_DEFINE_INSTRUCTION (191, bv_s16_set, "bv-s16-set", 0, 4, 0)
692 BV_SET_WITH_ENDIANNESS (s16, s16)
693 VM_DEFINE_INSTRUCTION (192, bv_u32_set, "bv-u32-set", 0, 4, 0)
694 BV_SET_WITH_ENDIANNESS (u32, u32)
695 VM_DEFINE_INSTRUCTION (193, bv_s32_set, "bv-s32-set", 0, 4, 0)
696 BV_SET_WITH_ENDIANNESS (s32, s32)
697 VM_DEFINE_INSTRUCTION (194, bv_u64_set, "bv-u64-set", 0, 4, 0)
698 BV_SET_WITH_ENDIANNESS (u64, u64)
699 VM_DEFINE_INSTRUCTION (195, bv_s64_set, "bv-s64-set", 0, 4, 0)
700 BV_SET_WITH_ENDIANNESS (s64, s64)
701 VM_DEFINE_INSTRUCTION (196, bv_f32_set, "bv-f32-set", 0, 4, 0)
702 BV_SET_WITH_ENDIANNESS (f32, ieee_single)
703 VM_DEFINE_INSTRUCTION (197, bv_f64_set, "bv-f64-set", 0, 4, 0)
704 BV_SET_WITH_ENDIANNESS (f64, ieee_double)
705
706 #undef BV_SET_WITH_ENDIANNESS
707
708 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
709 { \
710 long i = 0, j = 0; \
711 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
712 VM_VALIDATE_BYTEVECTOR (bv); \
713 if (SCM_LIKELY (SCM_I_INUMP (idx) \
714 && ((i = SCM_I_INUM (idx)) >= 0) \
715 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
716 && (i % size == 0) \
717 && (SCM_I_INUMP (val)) \
718 && ((j = SCM_I_INUM (val)) >= min) \
719 && (j <= max))) \
720 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
721 else \
722 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
723 NEXT; \
724 }
725
726 #define BV_INT_SET(stem, type, size) \
727 { \
728 long i = 0; \
729 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
730 VM_VALIDATE_BYTEVECTOR (bv); \
731 if (SCM_LIKELY (SCM_I_INUMP (idx) \
732 && ((i = SCM_I_INUM (idx)) >= 0) \
733 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
734 && (i % size == 0))) \
735 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
736 else \
737 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
738 NEXT; \
739 }
740
741 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
742 { \
743 long i = 0; \
744 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
745 VM_VALIDATE_BYTEVECTOR (bv); \
746 if (SCM_LIKELY (SCM_I_INUMP (idx) \
747 && ((i = SCM_I_INUM (idx)) >= 0) \
748 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
749 && (i % size == 0))) \
750 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
751 else \
752 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
753 NEXT; \
754 }
755
756 VM_DEFINE_INSTRUCTION (198, bv_u8_set, "bv-u8-set", 0, 3, 0)
757 BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
758 VM_DEFINE_INSTRUCTION (199, bv_s8_set, "bv-s8-set", 0, 3, 0)
759 BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
760 VM_DEFINE_INSTRUCTION (200, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
761 BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
762 VM_DEFINE_INSTRUCTION (201, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
763 BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
764 VM_DEFINE_INSTRUCTION (202, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
765 #if SIZEOF_VOID_P > 4
766 BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4)
767 #else
768 BV_INT_SET (u32, uint32, 4)
769 #endif
770 VM_DEFINE_INSTRUCTION (203, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
771 #if SIZEOF_VOID_P > 4
772 BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4)
773 #else
774 BV_INT_SET (s32, int32, 4)
775 #endif
776 VM_DEFINE_INSTRUCTION (204, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
777 BV_INT_SET (u64, uint64, 8)
778 VM_DEFINE_INSTRUCTION (205, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
779 BV_INT_SET (s64, int64, 8)
780 VM_DEFINE_INSTRUCTION (206, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
781 BV_FLOAT_SET (f32, ieee_single, float, 4)
782 VM_DEFINE_INSTRUCTION (207, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
783 BV_FLOAT_SET (f64, ieee_double, double, 8)
784
785 #undef BV_FIXABLE_INT_SET
786 #undef BV_INT_SET
787 #undef BV_FLOAT_SET
788
789 /*
790 (defun renumber-ops ()
791 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
792 (interactive "")
793 (save-excursion
794 (let ((counter 127)) (goto-char (point-min))
795 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
796 (replace-match
797 (number-to-string (setq counter (1+ counter)))
798 t t nil 1)))))
799 */
800
801 /*
802 Local Variables:
803 c-file-style: "gnu"
804 End:
805 */