renumber VM opcodes
[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 if (SCM_LIKELY (SCM_STRUCTP (vtable)
417 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
418 && SCM_I_INUMP (n_tail)))
419 {
420 scm_t_bits n_inits, len;
421
422 n_inits = SCM_I_INUM (n_tail) + n_args - 2;
423 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
424
425 if (SCM_LIKELY (n_inits == len))
426 {
427 SCM obj;
428
429 obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), n_inits);
430 memcpy (SCM_STRUCT_DATA (obj), inits, n_inits * sizeof (SCM));
431
432 RETURN (obj);
433 }
434 }
435
436 SYNC_REGISTER ();
437 RETURN (scm_c_make_structv (vtable, scm_to_size_t (n_tail),
438 n_args - 2, (scm_t_bits *) inits));
439 }
440
441 VM_DEFINE_FUNCTION (167, struct_ref, "struct-ref", 2)
442 {
443 ARGS2 (obj, pos);
444
445 if (SCM_LIKELY (SCM_STRUCTP (obj)
446 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
447 SCM_VTABLE_FLAG_SIMPLE)
448 && SCM_I_INUMP (pos)))
449 {
450 SCM vtable;
451 scm_t_bits index, len;
452
453 index = SCM_I_INUM (pos);
454 vtable = SCM_STRUCT_VTABLE (obj);
455 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
456
457 if (SCM_LIKELY (index < len))
458 {
459 scm_t_bits *data = SCM_STRUCT_DATA (obj);
460 RETURN (SCM_PACK (data[index]));
461 }
462 }
463
464 RETURN (scm_struct_ref (obj, pos));
465 }
466
467 VM_DEFINE_FUNCTION (168, struct_set, "struct-set", 3)
468 {
469 ARGS3 (obj, pos, val);
470
471 if (SCM_LIKELY (SCM_STRUCTP (obj)
472 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
473 SCM_VTABLE_FLAG_SIMPLE)
474 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
475 SCM_VTABLE_FLAG_SIMPLE_RW)
476 && SCM_I_INUMP (pos)))
477 {
478 SCM vtable;
479 scm_t_bits index, len;
480
481 index = SCM_I_INUM (pos);
482 vtable = SCM_STRUCT_VTABLE (obj);
483 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
484 if (SCM_LIKELY (index < len))
485 {
486 scm_t_bits *data = SCM_STRUCT_DATA (obj);
487 data[index] = SCM_UNPACK (val);
488 RETURN (val);
489 }
490 }
491
492 RETURN (scm_struct_set_x (obj, pos, val));
493 }
494
495 \f
496 /*
497 * GOOPS support
498 */
499 VM_DEFINE_FUNCTION (169, class_of, "class-of", 1)
500 {
501 ARGS1 (obj);
502 RETURN (SCM_INSTANCEP (obj) ? SCM_CLASS_OF (obj) : scm_class_of (obj));
503 }
504
505 VM_DEFINE_FUNCTION (170, slot_ref, "slot-ref", 2)
506 {
507 size_t slot;
508 ARGS2 (instance, idx);
509 slot = SCM_I_INUM (idx);
510 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
511 }
512
513 VM_DEFINE_INSTRUCTION (171, slot_set, "slot-set", 0, 3, 0)
514 {
515 SCM instance, idx, val;
516 size_t slot;
517 POP (val);
518 POP (idx);
519 POP (instance);
520 slot = SCM_I_INUM (idx);
521 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
522 NEXT;
523 }
524
525 \f
526 /*
527 * Bytevectors
528 */
529 #define VM_VALIDATE_BYTEVECTOR(x) \
530 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
531 { finish_args = x; \
532 goto vm_error_not_a_bytevector; \
533 }
534
535 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
536 { \
537 SCM endianness; \
538 POP (endianness); \
539 if (scm_is_eq (endianness, scm_i_native_endianness)) \
540 goto VM_LABEL (bv_##stem##_native_ref); \
541 { \
542 ARGS2 (bv, idx); \
543 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
544 } \
545 }
546
547 VM_DEFINE_FUNCTION (172, bv_u16_ref, "bv-u16-ref", 3)
548 BV_REF_WITH_ENDIANNESS (u16, u16)
549 VM_DEFINE_FUNCTION (173, bv_s16_ref, "bv-s16-ref", 3)
550 BV_REF_WITH_ENDIANNESS (s16, s16)
551 VM_DEFINE_FUNCTION (174, bv_u32_ref, "bv-u32-ref", 3)
552 BV_REF_WITH_ENDIANNESS (u32, u32)
553 VM_DEFINE_FUNCTION (175, bv_s32_ref, "bv-s32-ref", 3)
554 BV_REF_WITH_ENDIANNESS (s32, s32)
555 VM_DEFINE_FUNCTION (176, bv_u64_ref, "bv-u64-ref", 3)
556 BV_REF_WITH_ENDIANNESS (u64, u64)
557 VM_DEFINE_FUNCTION (177, bv_s64_ref, "bv-s64-ref", 3)
558 BV_REF_WITH_ENDIANNESS (s64, s64)
559 VM_DEFINE_FUNCTION (178, bv_f32_ref, "bv-f32-ref", 3)
560 BV_REF_WITH_ENDIANNESS (f32, ieee_single)
561 VM_DEFINE_FUNCTION (179, bv_f64_ref, "bv-f64-ref", 3)
562 BV_REF_WITH_ENDIANNESS (f64, ieee_double)
563
564 #undef BV_REF_WITH_ENDIANNESS
565
566 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
567 { \
568 long i = 0; \
569 ARGS2 (bv, idx); \
570 VM_VALIDATE_BYTEVECTOR (bv); \
571 if (SCM_LIKELY (SCM_I_INUMP (idx) \
572 && ((i = SCM_I_INUM (idx)) >= 0) \
573 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
574 && (i % size == 0))) \
575 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
576 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
577 else \
578 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx)); \
579 }
580
581 #define BV_INT_REF(stem, type, size) \
582 { \
583 long i = 0; \
584 ARGS2 (bv, idx); \
585 VM_VALIDATE_BYTEVECTOR (bv); \
586 if (SCM_LIKELY (SCM_I_INUMP (idx) \
587 && ((i = SCM_I_INUM (idx)) >= 0) \
588 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
589 && (i % size == 0))) \
590 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
591 if (SCM_FIXABLE (x)) \
592 RETURN (SCM_I_MAKINUM (x)); \
593 else \
594 RETURN (scm_from_##type (x)); \
595 } \
596 else \
597 RETURN (scm_bytevector_##stem##_native_ref (bv, idx)); \
598 }
599
600 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
601 { \
602 long i = 0; \
603 ARGS2 (bv, idx); \
604 VM_VALIDATE_BYTEVECTOR (bv); \
605 if (SCM_LIKELY (SCM_I_INUMP (idx) \
606 && ((i = SCM_I_INUM (idx)) >= 0) \
607 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
608 && (i % size == 0))) \
609 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
610 else \
611 RETURN (scm_bytevector_##fn_stem##_native_ref (bv, idx)); \
612 }
613
614 VM_DEFINE_FUNCTION (180, bv_u8_ref, "bv-u8-ref", 2)
615 BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
616 VM_DEFINE_FUNCTION (181, bv_s8_ref, "bv-s8-ref", 2)
617 BV_FIXABLE_INT_REF (s8, s8, int8, 1)
618 VM_DEFINE_FUNCTION (182, bv_u16_native_ref, "bv-u16-native-ref", 2)
619 BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
620 VM_DEFINE_FUNCTION (183, bv_s16_native_ref, "bv-s16-native-ref", 2)
621 BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
622 VM_DEFINE_FUNCTION (184, bv_u32_native_ref, "bv-u32-native-ref", 2)
623 #if SIZEOF_VOID_P > 4
624 BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4)
625 #else
626 BV_INT_REF (u32, uint32, 4)
627 #endif
628 VM_DEFINE_FUNCTION (185, bv_s32_native_ref, "bv-s32-native-ref", 2)
629 #if SIZEOF_VOID_P > 4
630 BV_FIXABLE_INT_REF (s32, s32_native, int32, 4)
631 #else
632 BV_INT_REF (s32, int32, 4)
633 #endif
634 VM_DEFINE_FUNCTION (186, bv_u64_native_ref, "bv-u64-native-ref", 2)
635 BV_INT_REF (u64, uint64, 8)
636 VM_DEFINE_FUNCTION (187, bv_s64_native_ref, "bv-s64-native-ref", 2)
637 BV_INT_REF (s64, int64, 8)
638 VM_DEFINE_FUNCTION (188, bv_f32_native_ref, "bv-f32-native-ref", 2)
639 BV_FLOAT_REF (f32, ieee_single, float, 4)
640 VM_DEFINE_FUNCTION (189, bv_f64_native_ref, "bv-f64-native-ref", 2)
641 BV_FLOAT_REF (f64, ieee_double, double, 8)
642
643 #undef BV_FIXABLE_INT_REF
644 #undef BV_INT_REF
645 #undef BV_FLOAT_REF
646
647
648
649 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
650 { \
651 SCM endianness; \
652 POP (endianness); \
653 if (scm_is_eq (endianness, scm_i_native_endianness)) \
654 goto VM_LABEL (bv_##stem##_native_set); \
655 { \
656 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
657 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
658 NEXT; \
659 } \
660 }
661
662 VM_DEFINE_INSTRUCTION (190, bv_u16_set, "bv-u16-set", 0, 4, 0)
663 BV_SET_WITH_ENDIANNESS (u16, u16)
664 VM_DEFINE_INSTRUCTION (191, bv_s16_set, "bv-s16-set", 0, 4, 0)
665 BV_SET_WITH_ENDIANNESS (s16, s16)
666 VM_DEFINE_INSTRUCTION (192, bv_u32_set, "bv-u32-set", 0, 4, 0)
667 BV_SET_WITH_ENDIANNESS (u32, u32)
668 VM_DEFINE_INSTRUCTION (193, bv_s32_set, "bv-s32-set", 0, 4, 0)
669 BV_SET_WITH_ENDIANNESS (s32, s32)
670 VM_DEFINE_INSTRUCTION (194, bv_u64_set, "bv-u64-set", 0, 4, 0)
671 BV_SET_WITH_ENDIANNESS (u64, u64)
672 VM_DEFINE_INSTRUCTION (195, bv_s64_set, "bv-s64-set", 0, 4, 0)
673 BV_SET_WITH_ENDIANNESS (s64, s64)
674 VM_DEFINE_INSTRUCTION (196, bv_f32_set, "bv-f32-set", 0, 4, 0)
675 BV_SET_WITH_ENDIANNESS (f32, ieee_single)
676 VM_DEFINE_INSTRUCTION (197, bv_f64_set, "bv-f64-set", 0, 4, 0)
677 BV_SET_WITH_ENDIANNESS (f64, ieee_double)
678
679 #undef BV_SET_WITH_ENDIANNESS
680
681 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
682 { \
683 long i = 0, j = 0; \
684 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
685 VM_VALIDATE_BYTEVECTOR (bv); \
686 if (SCM_LIKELY (SCM_I_INUMP (idx) \
687 && ((i = SCM_I_INUM (idx)) >= 0) \
688 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
689 && (i % size == 0) \
690 && (SCM_I_INUMP (val)) \
691 && ((j = SCM_I_INUM (val)) >= min) \
692 && (j <= max))) \
693 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
694 else \
695 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
696 NEXT; \
697 }
698
699 #define BV_INT_SET(stem, type, size) \
700 { \
701 long i = 0; \
702 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
703 VM_VALIDATE_BYTEVECTOR (bv); \
704 if (SCM_LIKELY (SCM_I_INUMP (idx) \
705 && ((i = SCM_I_INUM (idx)) >= 0) \
706 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
707 && (i % size == 0))) \
708 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
709 else \
710 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
711 NEXT; \
712 }
713
714 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
715 { \
716 long i = 0; \
717 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
718 VM_VALIDATE_BYTEVECTOR (bv); \
719 if (SCM_LIKELY (SCM_I_INUMP (idx) \
720 && ((i = SCM_I_INUM (idx)) >= 0) \
721 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
722 && (i % size == 0))) \
723 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
724 else \
725 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
726 NEXT; \
727 }
728
729 VM_DEFINE_INSTRUCTION (198, bv_u8_set, "bv-u8-set", 0, 3, 0)
730 BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
731 VM_DEFINE_INSTRUCTION (199, bv_s8_set, "bv-s8-set", 0, 3, 0)
732 BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
733 VM_DEFINE_INSTRUCTION (200, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
734 BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
735 VM_DEFINE_INSTRUCTION (201, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
736 BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
737 VM_DEFINE_INSTRUCTION (202, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
738 #if SIZEOF_VOID_P > 4
739 BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4)
740 #else
741 BV_INT_SET (u32, uint32, 4)
742 #endif
743 VM_DEFINE_INSTRUCTION (203, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
744 #if SIZEOF_VOID_P > 4
745 BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4)
746 #else
747 BV_INT_SET (s32, int32, 4)
748 #endif
749 VM_DEFINE_INSTRUCTION (204, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
750 BV_INT_SET (u64, uint64, 8)
751 VM_DEFINE_INSTRUCTION (205, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
752 BV_INT_SET (s64, int64, 8)
753 VM_DEFINE_INSTRUCTION (206, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
754 BV_FLOAT_SET (f32, ieee_single, float, 4)
755 VM_DEFINE_INSTRUCTION (207, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
756 BV_FLOAT_SET (f64, ieee_double, double, 8)
757
758 #undef BV_FIXABLE_INT_SET
759 #undef BV_INT_SET
760 #undef BV_FLOAT_SET
761
762 /*
763 (defun renumber-ops ()
764 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
765 (interactive "")
766 (save-excursion
767 (let ((counter 127)) (goto-char (point-min))
768 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
769 (replace-match
770 (number-to-string (setq counter (1+ counter)))
771 t t nil 1)))))
772 */
773
774 /*
775 Local Variables:
776 c-file-style: "gnu"
777 End:
778 */