Fix inline asm of VM numerical operations for x32.
[bpt/guile.git] / libguile / vm-i-scheme.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 VM_DEFINE_FUNCTION (138, symbolp, "symbol?", 1)
103 {
104 ARGS1 (x);
105 RETURN (scm_from_bool (scm_is_symbol (x)));
106 }
107
108 VM_DEFINE_FUNCTION (139, vectorp, "vector?", 1)
109 {
110 ARGS1 (x);
111 RETURN (scm_from_bool (SCM_I_IS_VECTOR (x)));
112 }
113
114 \f
115 /*
116 * Basic data
117 */
118
119 VM_DEFINE_FUNCTION (140, cons, "cons", 2)
120 {
121 ARGS2 (x, y);
122 CONS (x, x, y);
123 RETURN (x);
124 }
125
126 #define VM_VALIDATE_CONS(x, proc) \
127 VM_ASSERT (scm_is_pair (x), vm_error_not_a_pair (proc, x))
128
129 VM_DEFINE_FUNCTION (141, car, "car", 1)
130 {
131 ARGS1 (x);
132 VM_VALIDATE_CONS (x, "car");
133 RETURN (SCM_CAR (x));
134 }
135
136 VM_DEFINE_FUNCTION (142, cdr, "cdr", 1)
137 {
138 ARGS1 (x);
139 VM_VALIDATE_CONS (x, "cdr");
140 RETURN (SCM_CDR (x));
141 }
142
143 VM_DEFINE_INSTRUCTION (143, set_car, "set-car!", 0, 2, 0)
144 {
145 SCM x, y;
146 POP2 (y, x);
147 VM_VALIDATE_CONS (x, "set-car!");
148 SCM_SETCAR (x, y);
149 NEXT;
150 }
151
152 VM_DEFINE_INSTRUCTION (144, set_cdr, "set-cdr!", 0, 2, 0)
153 {
154 SCM x, y;
155 POP2 (y, x);
156 VM_VALIDATE_CONS (x, "set-cdr!");
157 SCM_SETCDR (x, y);
158 NEXT;
159 }
160
161 \f
162 /*
163 * Numeric relational tests
164 */
165
166 #undef REL
167 #define REL(crel,srel) \
168 { \
169 ARGS2 (x, y); \
170 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
171 RETURN (scm_from_bool (((scm_t_signed_bits) SCM_UNPACK (x)) \
172 crel ((scm_t_signed_bits) SCM_UNPACK (y)))); \
173 SYNC_REGISTER (); \
174 RETURN (srel (x, y)); \
175 }
176
177 VM_DEFINE_FUNCTION (145, ee, "ee?", 2)
178 {
179 REL (==, scm_num_eq_p);
180 }
181
182 VM_DEFINE_FUNCTION (146, lt, "lt?", 2)
183 {
184 REL (<, scm_less_p);
185 }
186
187 VM_DEFINE_FUNCTION (147, le, "le?", 2)
188 {
189 REL (<=, scm_leq_p);
190 }
191
192 VM_DEFINE_FUNCTION (148, gt, "gt?", 2)
193 {
194 REL (>, scm_gr_p);
195 }
196
197 VM_DEFINE_FUNCTION (149, ge, "ge?", 2)
198 {
199 REL (>=, scm_geq_p);
200 }
201
202 \f
203 /*
204 * Numeric functions
205 */
206
207 /* The maximum/minimum tagged integers. */
208 #undef INUM_MAX
209 #undef INUM_MIN
210 #undef INUM_STEP
211 #define INUM_MAX \
212 ((scm_t_signed_bits) SCM_UNPACK (SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM)))
213 #define INUM_MIN \
214 ((scm_t_signed_bits) SCM_UNPACK (SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM)))
215 #define INUM_STEP \
216 ((scm_t_signed_bits) SCM_UNPACK (SCM_INUM1) \
217 - (scm_t_signed_bits) SCM_UNPACK (SCM_INUM0))
218
219 #undef FUNC2
220 #define FUNC2(CFUNC,SFUNC) \
221 { \
222 ARGS2 (x, y); \
223 if (SCM_I_INUMP (x) && SCM_I_INUMP (y)) \
224 { \
225 scm_t_int64 n = SCM_I_INUM (x) CFUNC SCM_I_INUM (y);\
226 if (SCM_FIXABLE (n)) \
227 RETURN (SCM_I_MAKINUM (n)); \
228 } \
229 SYNC_REGISTER (); \
230 RETURN (SFUNC (x, y)); \
231 }
232
233 /* Assembly tagged integer arithmetic routines. This code uses the
234 `asm goto' feature introduced in GCC 4.5. */
235
236 #if SCM_GNUC_PREREQ (4, 5) && (defined __x86_64__ || defined __i386__)
237
238 # undef _CX
239 # if SIZEOF_VOID_P == 8
240 # define _CX "rcx"
241 # elif SIZEOF_VOID_P == 4
242 # define _CX "ecx"
243 # else
244 # error unsupported word size
245 # endif
246
247 /* The macros below check the CPU's overflow flag to improve fixnum
248 arithmetic. The _CX register (%rcx or %ecx) is explicitly
249 clobbered because `asm goto' can't have outputs, in which case the
250 `r' constraint could be used to let the register allocator choose a
251 register.
252
253 TODO: Use `cold' label attribute in GCC 4.6.
254 http://gcc.gnu.org/ml/gcc-patches/2010-10/msg01777.html */
255
256 # define ASM_ADD(x, y) \
257 { \
258 asm volatile goto ("mov %1, %%"_CX"; " \
259 "test %[tag], %%cl; je %l[slow_add]; " \
260 "test %[tag], %0; je %l[slow_add]; " \
261 "sub %[tag], %%"_CX"; " \
262 "add %0, %%"_CX"; jo %l[slow_add]; " \
263 "mov %%"_CX", (%[vsp])\n" \
264 : /* no outputs */ \
265 : "r" (x), "r" (y), \
266 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
267 : _CX, "memory", "cc" \
268 : slow_add); \
269 NEXT; \
270 } \
271 slow_add: \
272 do { } while (0)
273
274 # define ASM_SUB(x, y) \
275 { \
276 asm volatile goto ("mov %0, %%"_CX"; " \
277 "test %[tag], %%cl; je %l[slow_sub]; " \
278 "test %[tag], %1; je %l[slow_sub]; " \
279 "sub %1, %%"_CX"; jo %l[slow_sub]; " \
280 "add %[tag], %%"_CX"; " \
281 "mov %%"_CX", (%[vsp])\n" \
282 : /* no outputs */ \
283 : "r" (x), "r" (y), \
284 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
285 : _CX, "memory", "cc" \
286 : slow_sub); \
287 NEXT; \
288 } \
289 slow_sub: \
290 do { } while (0)
291
292 # define ASM_MUL(x, y) \
293 { \
294 scm_t_signed_bits xx = SCM_I_INUM (x); \
295 asm volatile goto ("mov %1, %%"_CX"; " \
296 "test %[tag], %%cl; je %l[slow_mul]; " \
297 "sub %[tag], %%"_CX"; " \
298 "test %[tag], %0; je %l[slow_mul]; " \
299 "imul %2, %%"_CX"; jo %l[slow_mul]; " \
300 "add %[tag], %%"_CX"; " \
301 "mov %%"_CX", (%[vsp])\n" \
302 : /* no outputs */ \
303 : "r" (x), "r" (y), "r" (xx), \
304 [vsp] "r" (sp), [tag] "i" (scm_tc2_int) \
305 : _CX, "memory", "cc" \
306 : slow_mul); \
307 NEXT; \
308 } \
309 slow_mul: \
310 do { } while (0)
311
312 #endif
313
314 #if SCM_GNUC_PREREQ (4, 5) && defined __arm__
315
316 # define ASM_ADD(x, y) \
317 if (SCM_LIKELY (SCM_I_INUMP (x) && SCM_I_INUMP (y))) \
318 { \
319 asm volatile goto ("adds r0, %0, %1; bvs %l[slow_add]; " \
320 "str r0, [%[vsp]]\n" \
321 : /* no outputs */ \
322 : "r" (x), "r" (y - scm_tc2_int), \
323 [vsp] "r" (sp) \
324 : "r0", "memory", "cc" \
325 : slow_add); \
326 NEXT; \
327 } \
328 slow_add: \
329 do { } while (0)
330
331 # define ASM_SUB(x, y) \
332 if (SCM_LIKELY (SCM_I_INUMP (x) && SCM_I_INUMP (y))) \
333 { \
334 asm volatile goto ("subs r0, %0, %1; bvs %l[slow_sub]; " \
335 "str r0, [%[vsp]]\n" \
336 : /* no outputs */ \
337 : "r" (x), "r" (y - scm_tc2_int), \
338 [vsp] "r" (sp) \
339 : "r0", "memory", "cc" \
340 : slow_sub); \
341 NEXT; \
342 } \
343 slow_sub: \
344 do { } while (0)
345
346 # if defined (__ARM_ARCH_3M__) || defined (__ARM_ARCH_4__) \
347 || defined (__ARM_ARCH_4T__) || defined (__ARM_ARCH_5__) \
348 || defined (__ARM_ARCH_5T__) || defined (__ARM_ARCH_5E__) \
349 || defined (__ARM_ARCH_5TE__) || defined (__ARM_ARCH_5TEJ__) \
350 || defined (__ARM_ARCH_6__) || defined (__ARM_ARCH_6J__) \
351 || defined (__ARM_ARCH_6K__) || defined (__ARM_ARCH_6Z__) \
352 || defined (__ARM_ARCH_6ZK__) || defined (__ARM_ARCH_6T2__) \
353 || defined (__ARM_ARCH_6M__) || defined (__ARM_ARCH_7__) \
354 || defined (__ARM_ARCH_7A__) || defined (__ARM_ARCH_7R__) \
355 || defined (__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) \
356 || defined (__ARM_ARCH_8A__)
357
358 /* The ARM architectures listed above support the SMULL instruction */
359
360 # define ASM_MUL(x, y) \
361 if (SCM_LIKELY (SCM_I_INUMP (x) && SCM_I_INUMP (y))) \
362 { \
363 scm_t_signed_bits rlo, rhi; \
364 asm ("smull %0, %1, %2, %3\n" \
365 : "=r" (rlo), "=r" (rhi) \
366 : "r" (SCM_UNPACK (x) - scm_tc2_int), \
367 "r" (SCM_I_INUM (y))); \
368 if (SCM_LIKELY (SCM_SRS (rlo, 31) == rhi)) \
369 RETURN (SCM_PACK (rlo + scm_tc2_int)); \
370 } \
371 do { } while (0)
372
373 # endif
374
375 #endif
376
377 VM_DEFINE_FUNCTION (150, add, "add", 2)
378 {
379 #ifndef ASM_ADD
380 FUNC2 (+, scm_sum);
381 #else
382 ARGS2 (x, y);
383 ASM_ADD (x, y);
384 SYNC_REGISTER ();
385 RETURN (scm_sum (x, y));
386 #endif
387 }
388
389 VM_DEFINE_FUNCTION (151, add1, "add1", 1)
390 {
391 ARGS1 (x);
392
393 /* Check for overflow. We must avoid overflow in the signed
394 addition below, even if X is not an inum. */
395 if (SCM_LIKELY ((scm_t_signed_bits) SCM_UNPACK (x) <= INUM_MAX - INUM_STEP))
396 {
397 SCM result;
398
399 /* Add 1 to the integer without untagging. */
400 result = SCM_PACK ((scm_t_signed_bits) SCM_UNPACK (x) + INUM_STEP);
401
402 if (SCM_LIKELY (SCM_I_INUMP (result)))
403 RETURN (result);
404 }
405
406 SYNC_REGISTER ();
407 RETURN (scm_sum (x, SCM_I_MAKINUM (1)));
408 }
409
410 VM_DEFINE_FUNCTION (152, sub, "sub", 2)
411 {
412 #ifndef ASM_SUB
413 FUNC2 (-, scm_difference);
414 #else
415 ARGS2 (x, y);
416 ASM_SUB (x, y);
417 SYNC_REGISTER ();
418 RETURN (scm_difference (x, y));
419 #endif
420 }
421
422 VM_DEFINE_FUNCTION (153, sub1, "sub1", 1)
423 {
424 ARGS1 (x);
425
426 /* Check for overflow. We must avoid overflow in the signed
427 subtraction below, even if X is not an inum. */
428 if (SCM_LIKELY ((scm_t_signed_bits) SCM_UNPACK (x) >= INUM_MIN + INUM_STEP))
429 {
430 SCM result;
431
432 /* Substract 1 from the integer without untagging. */
433 result = SCM_PACK ((scm_t_signed_bits) SCM_UNPACK (x) - INUM_STEP);
434
435 if (SCM_LIKELY (SCM_I_INUMP (result)))
436 RETURN (result);
437 }
438
439 SYNC_REGISTER ();
440 RETURN (scm_difference (x, SCM_I_MAKINUM (1)));
441 }
442
443 VM_DEFINE_FUNCTION (154, mul, "mul", 2)
444 {
445 ARGS2 (x, y);
446 #ifdef ASM_MUL
447 ASM_MUL (x, y);
448 #endif
449 SYNC_REGISTER ();
450 RETURN (scm_product (x, y));
451 }
452
453 # undef ASM_ADD
454 # undef ASM_SUB
455 # undef ASM_MUL
456
457 VM_DEFINE_FUNCTION (155, div, "div", 2)
458 {
459 ARGS2 (x, y);
460 SYNC_REGISTER ();
461 RETURN (scm_divide (x, y));
462 }
463
464 VM_DEFINE_FUNCTION (156, quo, "quo", 2)
465 {
466 ARGS2 (x, y);
467 SYNC_REGISTER ();
468 RETURN (scm_quotient (x, y));
469 }
470
471 VM_DEFINE_FUNCTION (157, rem, "rem", 2)
472 {
473 ARGS2 (x, y);
474 SYNC_REGISTER ();
475 RETURN (scm_remainder (x, y));
476 }
477
478 VM_DEFINE_FUNCTION (158, mod, "mod", 2)
479 {
480 ARGS2 (x, y);
481 SYNC_REGISTER ();
482 RETURN (scm_modulo (x, y));
483 }
484
485 VM_DEFINE_FUNCTION (159, ash, "ash", 2)
486 {
487 ARGS2 (x, y);
488 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
489 {
490 if (SCM_I_INUM (y) < 0)
491 /* Right shift, will be a fixnum. */
492 RETURN (SCM_I_MAKINUM
493 (SCM_SRS (SCM_I_INUM (x),
494 (-SCM_I_INUM (y) <= SCM_I_FIXNUM_BIT-1)
495 ? -SCM_I_INUM (y) : SCM_I_FIXNUM_BIT-1)));
496 else
497 /* Left shift. See comments in scm_ash. */
498 {
499 scm_t_signed_bits nn, bits_to_shift;
500
501 nn = SCM_I_INUM (x);
502 bits_to_shift = SCM_I_INUM (y);
503
504 if (bits_to_shift < SCM_I_FIXNUM_BIT-1
505 && ((scm_t_bits)
506 (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
507 <= 1))
508 RETURN (SCM_I_MAKINUM (nn << bits_to_shift));
509 /* fall through */
510 }
511 /* fall through */
512 }
513 SYNC_REGISTER ();
514 RETURN (scm_ash (x, y));
515 }
516
517 VM_DEFINE_FUNCTION (160, logand, "logand", 2)
518 {
519 ARGS2 (x, y);
520 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
521 /* Compute bitwise AND without untagging */
522 RETURN (SCM_PACK (SCM_UNPACK (x) & SCM_UNPACK (y)));
523 SYNC_REGISTER ();
524 RETURN (scm_logand (x, y));
525 }
526
527 VM_DEFINE_FUNCTION (161, logior, "logior", 2)
528 {
529 ARGS2 (x, y);
530 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
531 /* Compute bitwise OR without untagging */
532 RETURN (SCM_PACK (SCM_UNPACK (x) | SCM_UNPACK (y)));
533 SYNC_REGISTER ();
534 RETURN (scm_logior (x, y));
535 }
536
537 VM_DEFINE_FUNCTION (162, logxor, "logxor", 2)
538 {
539 ARGS2 (x, y);
540 if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
541 RETURN (SCM_I_MAKINUM (SCM_I_INUM (x) ^ SCM_I_INUM (y)));
542 SYNC_REGISTER ();
543 RETURN (scm_logxor (x, y));
544 }
545
546 \f
547 /*
548 * Vectors and arrays
549 */
550
551 VM_DEFINE_FUNCTION (163, vector_ref, "vector-ref", 2)
552 {
553 scm_t_signed_bits i = 0;
554 ARGS2 (vect, idx);
555 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect)
556 && SCM_I_INUMP (idx)
557 && ((i = SCM_I_INUM (idx)) >= 0)
558 && i < SCM_I_VECTOR_LENGTH (vect)))
559 RETURN (SCM_I_VECTOR_ELTS (vect)[i]);
560 else
561 {
562 SYNC_REGISTER ();
563 RETURN (scm_vector_ref (vect, idx));
564 }
565 }
566
567 VM_DEFINE_INSTRUCTION (164, vector_set, "vector-set", 0, 3, 0)
568 {
569 scm_t_signed_bits i = 0;
570 SCM vect, idx, val;
571 POP3 (val, idx, vect);
572 if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect)
573 && SCM_I_INUMP (idx)
574 && ((i = SCM_I_INUM (idx)) >= 0)
575 && i < SCM_I_VECTOR_LENGTH (vect)))
576 SCM_I_VECTOR_WELTS (vect)[i] = val;
577 else
578 {
579 SYNC_REGISTER ();
580 scm_vector_set_x (vect, idx, val);
581 }
582 NEXT;
583 }
584
585 VM_DEFINE_INSTRUCTION (165, make_array, "make-array", 3, -1, 1)
586 {
587 scm_t_uint32 len;
588 SCM shape, ret;
589
590 len = FETCH ();
591 len = (len << 8) + FETCH ();
592 len = (len << 8) + FETCH ();
593 POP (shape);
594 SYNC_REGISTER ();
595 PRE_CHECK_UNDERFLOW (len);
596 ret = scm_from_contiguous_array (shape, sp - len + 1, len);
597 DROPN (len);
598 PUSH (ret);
599 NEXT;
600 }
601
602 \f
603 /*
604 * Structs
605 */
606 #define VM_VALIDATE_STRUCT(obj, proc) \
607 VM_ASSERT (SCM_STRUCTP (obj), vm_error_not_a_struct (proc, obj))
608
609 VM_DEFINE_FUNCTION (166, struct_p, "struct?", 1)
610 {
611 ARGS1 (obj);
612 RETURN (scm_from_bool (SCM_STRUCTP (obj)));
613 }
614
615 VM_DEFINE_FUNCTION (167, struct_vtable, "struct-vtable", 1)
616 {
617 ARGS1 (obj);
618 VM_VALIDATE_STRUCT (obj, "struct_vtable");
619 RETURN (SCM_STRUCT_VTABLE (obj));
620 }
621
622 VM_DEFINE_INSTRUCTION (168, make_struct, "make-struct", 2, -1, 1)
623 {
624 unsigned h = FETCH ();
625 unsigned l = FETCH ();
626 scm_t_bits n = ((h << 8U) + l);
627 SCM vtable = sp[-(n - 1)];
628 const SCM *inits = sp - n + 2;
629 SCM ret;
630
631 SYNC_REGISTER ();
632
633 if (SCM_LIKELY (SCM_STRUCTP (vtable)
634 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
635 && (SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size) + 1
636 == n)
637 && !SCM_VTABLE_INSTANCE_FINALIZER (vtable)))
638 {
639 /* Verily, we are making a simple struct with the right number of
640 initializers, and no finalizer. */
641 ret = scm_words ((scm_t_bits)SCM_STRUCT_DATA (vtable) | scm_tc3_struct,
642 n + 1);
643 SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2));
644 memcpy (SCM_STRUCT_DATA (ret), inits, (n - 1) * sizeof (SCM));
645 }
646 else
647 ret = scm_c_make_structv (vtable, 0, n - 1, (scm_t_bits *) inits);
648
649 DROPN (n);
650 PUSH (ret);
651
652 NEXT;
653 }
654
655 VM_DEFINE_FUNCTION (169, struct_ref, "struct-ref", 2)
656 {
657 ARGS2 (obj, pos);
658
659 if (SCM_LIKELY (SCM_STRUCTP (obj)
660 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
661 SCM_VTABLE_FLAG_SIMPLE)
662 && SCM_I_INUMP (pos)))
663 {
664 SCM vtable;
665 scm_t_bits index, len;
666
667 /* True, an inum is a signed value, but cast to unsigned it will
668 certainly be more than the length, so we will fall through if
669 index is negative. */
670 index = SCM_I_INUM (pos);
671 vtable = SCM_STRUCT_VTABLE (obj);
672 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
673
674 if (SCM_LIKELY (index < len))
675 {
676 scm_t_bits *data = SCM_STRUCT_DATA (obj);
677 RETURN (SCM_PACK (data[index]));
678 }
679 }
680
681 SYNC_REGISTER ();
682 RETURN (scm_struct_ref (obj, pos));
683 }
684
685 VM_DEFINE_FUNCTION (170, struct_set, "struct-set", 3)
686 {
687 ARGS3 (obj, pos, val);
688
689 if (SCM_LIKELY (SCM_STRUCTP (obj)
690 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
691 SCM_VTABLE_FLAG_SIMPLE)
692 && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
693 SCM_VTABLE_FLAG_SIMPLE_RW)
694 && SCM_I_INUMP (pos)))
695 {
696 SCM vtable;
697 scm_t_bits index, len;
698
699 /* See above regarding index being >= 0. */
700 index = SCM_I_INUM (pos);
701 vtable = SCM_STRUCT_VTABLE (obj);
702 len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
703 if (SCM_LIKELY (index < len))
704 {
705 scm_t_bits *data = SCM_STRUCT_DATA (obj);
706 data[index] = SCM_UNPACK (val);
707 RETURN (val);
708 }
709 }
710
711 SYNC_REGISTER ();
712 RETURN (scm_struct_set_x (obj, pos, val));
713 }
714
715 \f
716 /*
717 * GOOPS support
718 */
719 VM_DEFINE_FUNCTION (171, class_of, "class-of", 1)
720 {
721 ARGS1 (obj);
722 if (SCM_INSTANCEP (obj))
723 RETURN (SCM_CLASS_OF (obj));
724 SYNC_REGISTER ();
725 RETURN (scm_class_of (obj));
726 }
727
728 /* FIXME: No checking whatsoever. */
729 VM_DEFINE_FUNCTION (172, slot_ref, "slot-ref", 2)
730 {
731 size_t slot;
732 ARGS2 (instance, idx);
733 slot = SCM_I_INUM (idx);
734 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
735 }
736
737 /* FIXME: No checking whatsoever. */
738 VM_DEFINE_INSTRUCTION (173, slot_set, "slot-set", 0, 3, 0)
739 {
740 SCM instance, idx, val;
741 size_t slot;
742 POP3 (val, idx, instance);
743 slot = SCM_I_INUM (idx);
744 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
745 NEXT;
746 }
747
748 \f
749 /*
750 * Bytevectors
751 */
752 #define VM_VALIDATE_BYTEVECTOR(x, proc) \
753 VM_ASSERT (SCM_BYTEVECTOR_P (x), vm_error_not_a_bytevector (proc, x))
754
755 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
756 { \
757 SCM endianness; \
758 POP (endianness); \
759 if (scm_is_eq (endianness, scm_i_native_endianness)) \
760 goto VM_LABEL (bv_##stem##_native_ref); \
761 { \
762 ARGS2 (bv, idx); \
763 SYNC_REGISTER (); \
764 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
765 } \
766 }
767
768 /* Return true (non-zero) if PTR has suitable alignment for TYPE. */
769 #define ALIGNED_P(ptr, type) \
770 ((scm_t_uintptr) (ptr) % alignof_type (type) == 0)
771
772 VM_DEFINE_FUNCTION (174, bv_u16_ref, "bv-u16-ref", 3)
773 BV_REF_WITH_ENDIANNESS (u16, u16)
774 VM_DEFINE_FUNCTION (175, bv_s16_ref, "bv-s16-ref", 3)
775 BV_REF_WITH_ENDIANNESS (s16, s16)
776 VM_DEFINE_FUNCTION (176, bv_u32_ref, "bv-u32-ref", 3)
777 BV_REF_WITH_ENDIANNESS (u32, u32)
778 VM_DEFINE_FUNCTION (177, bv_s32_ref, "bv-s32-ref", 3)
779 BV_REF_WITH_ENDIANNESS (s32, s32)
780 VM_DEFINE_FUNCTION (178, bv_u64_ref, "bv-u64-ref", 3)
781 BV_REF_WITH_ENDIANNESS (u64, u64)
782 VM_DEFINE_FUNCTION (179, bv_s64_ref, "bv-s64-ref", 3)
783 BV_REF_WITH_ENDIANNESS (s64, s64)
784 VM_DEFINE_FUNCTION (180, bv_f32_ref, "bv-f32-ref", 3)
785 BV_REF_WITH_ENDIANNESS (f32, ieee_single)
786 VM_DEFINE_FUNCTION (181, bv_f64_ref, "bv-f64-ref", 3)
787 BV_REF_WITH_ENDIANNESS (f64, ieee_double)
788
789 #undef BV_REF_WITH_ENDIANNESS
790
791 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
792 { \
793 scm_t_signed_bits i; \
794 const scm_t_ ## type *int_ptr; \
795 ARGS2 (bv, idx); \
796 \
797 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
798 i = SCM_I_INUM (idx); \
799 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
800 \
801 if (SCM_LIKELY (SCM_I_INUMP (idx) \
802 && (i >= 0) \
803 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
804 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
805 RETURN (SCM_I_MAKINUM (*int_ptr)); \
806 else \
807 { \
808 SYNC_REGISTER (); \
809 RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
810 } \
811 }
812
813 #define BV_INT_REF(stem, type, size) \
814 { \
815 scm_t_signed_bits i; \
816 const scm_t_ ## type *int_ptr; \
817 ARGS2 (bv, idx); \
818 \
819 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
820 i = SCM_I_INUM (idx); \
821 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
822 \
823 if (SCM_LIKELY (SCM_I_INUMP (idx) \
824 && (i >= 0) \
825 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
826 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
827 { \
828 scm_t_ ## type x = *int_ptr; \
829 if (SCM_FIXABLE (x)) \
830 RETURN (SCM_I_MAKINUM (x)); \
831 else \
832 { \
833 SYNC_REGISTER (); \
834 RETURN (scm_from_ ## type (x)); \
835 } \
836 } \
837 else \
838 { \
839 SYNC_REGISTER (); \
840 RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
841 } \
842 }
843
844 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
845 { \
846 scm_t_signed_bits i; \
847 const type *float_ptr; \
848 ARGS2 (bv, idx); \
849 \
850 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
851 i = SCM_I_INUM (idx); \
852 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
853 \
854 SYNC_REGISTER (); \
855 if (SCM_LIKELY (SCM_I_INUMP (idx) \
856 && (i >= 0) \
857 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
858 && (ALIGNED_P (float_ptr, type)))) \
859 RETURN (scm_from_double (*float_ptr)); \
860 else \
861 RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
862 }
863
864 VM_DEFINE_FUNCTION (182, bv_u8_ref, "bv-u8-ref", 2)
865 BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
866 VM_DEFINE_FUNCTION (183, bv_s8_ref, "bv-s8-ref", 2)
867 BV_FIXABLE_INT_REF (s8, s8, int8, 1)
868 VM_DEFINE_FUNCTION (184, bv_u16_native_ref, "bv-u16-native-ref", 2)
869 BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
870 VM_DEFINE_FUNCTION (185, bv_s16_native_ref, "bv-s16-native-ref", 2)
871 BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
872 VM_DEFINE_FUNCTION (186, bv_u32_native_ref, "bv-u32-native-ref", 2)
873 #if SIZEOF_VOID_P > 4
874 BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4)
875 #else
876 BV_INT_REF (u32, uint32, 4)
877 #endif
878 VM_DEFINE_FUNCTION (187, bv_s32_native_ref, "bv-s32-native-ref", 2)
879 #if SIZEOF_VOID_P > 4
880 BV_FIXABLE_INT_REF (s32, s32_native, int32, 4)
881 #else
882 BV_INT_REF (s32, int32, 4)
883 #endif
884 VM_DEFINE_FUNCTION (188, bv_u64_native_ref, "bv-u64-native-ref", 2)
885 BV_INT_REF (u64, uint64, 8)
886 VM_DEFINE_FUNCTION (189, bv_s64_native_ref, "bv-s64-native-ref", 2)
887 BV_INT_REF (s64, int64, 8)
888 VM_DEFINE_FUNCTION (190, bv_f32_native_ref, "bv-f32-native-ref", 2)
889 BV_FLOAT_REF (f32, ieee_single, float, 4)
890 VM_DEFINE_FUNCTION (191, bv_f64_native_ref, "bv-f64-native-ref", 2)
891 BV_FLOAT_REF (f64, ieee_double, double, 8)
892
893 #undef BV_FIXABLE_INT_REF
894 #undef BV_INT_REF
895 #undef BV_FLOAT_REF
896
897
898
899 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
900 { \
901 SCM endianness; \
902 POP (endianness); \
903 if (scm_is_eq (endianness, scm_i_native_endianness)) \
904 goto VM_LABEL (bv_##stem##_native_set); \
905 { \
906 SCM bv, idx, val; POP3 (val, idx, bv); \
907 SYNC_REGISTER (); \
908 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
909 NEXT; \
910 } \
911 }
912
913 VM_DEFINE_INSTRUCTION (192, bv_u16_set, "bv-u16-set", 0, 4, 0)
914 BV_SET_WITH_ENDIANNESS (u16, u16)
915 VM_DEFINE_INSTRUCTION (193, bv_s16_set, "bv-s16-set", 0, 4, 0)
916 BV_SET_WITH_ENDIANNESS (s16, s16)
917 VM_DEFINE_INSTRUCTION (194, bv_u32_set, "bv-u32-set", 0, 4, 0)
918 BV_SET_WITH_ENDIANNESS (u32, u32)
919 VM_DEFINE_INSTRUCTION (195, bv_s32_set, "bv-s32-set", 0, 4, 0)
920 BV_SET_WITH_ENDIANNESS (s32, s32)
921 VM_DEFINE_INSTRUCTION (196, bv_u64_set, "bv-u64-set", 0, 4, 0)
922 BV_SET_WITH_ENDIANNESS (u64, u64)
923 VM_DEFINE_INSTRUCTION (197, bv_s64_set, "bv-s64-set", 0, 4, 0)
924 BV_SET_WITH_ENDIANNESS (s64, s64)
925 VM_DEFINE_INSTRUCTION (198, bv_f32_set, "bv-f32-set", 0, 4, 0)
926 BV_SET_WITH_ENDIANNESS (f32, ieee_single)
927 VM_DEFINE_INSTRUCTION (199, bv_f64_set, "bv-f64-set", 0, 4, 0)
928 BV_SET_WITH_ENDIANNESS (f64, ieee_double)
929
930 #undef BV_SET_WITH_ENDIANNESS
931
932 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
933 { \
934 scm_t_signed_bits i, j = 0; \
935 SCM bv, idx, val; \
936 scm_t_ ## type *int_ptr; \
937 \
938 POP3 (val, idx, bv); \
939 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
940 i = SCM_I_INUM (idx); \
941 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
942 \
943 if (SCM_LIKELY (SCM_I_INUMP (idx) \
944 && (i >= 0) \
945 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
946 && (ALIGNED_P (int_ptr, scm_t_ ## type)) \
947 && (SCM_I_INUMP (val)) \
948 && ((j = SCM_I_INUM (val)) >= min) \
949 && (j <= max))) \
950 *int_ptr = (scm_t_ ## type) j; \
951 else \
952 { \
953 SYNC_REGISTER (); \
954 scm_bytevector_ ## fn_stem ## _set_x (bv, idx, val); \
955 } \
956 NEXT; \
957 }
958
959 #define BV_INT_SET(stem, type, size) \
960 { \
961 scm_t_signed_bits i = 0; \
962 SCM bv, idx, val; \
963 scm_t_ ## type *int_ptr; \
964 \
965 POP3 (val, idx, bv); \
966 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
967 i = SCM_I_INUM (idx); \
968 int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
969 \
970 if (SCM_LIKELY (SCM_I_INUMP (idx) \
971 && (i >= 0) \
972 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
973 && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
974 *int_ptr = scm_to_ ## type (val); \
975 else \
976 { \
977 SYNC_REGISTER (); \
978 scm_bytevector_ ## stem ## _native_set_x (bv, idx, val); \
979 } \
980 NEXT; \
981 }
982
983 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
984 { \
985 scm_t_signed_bits i = 0; \
986 SCM bv, idx, val; \
987 type *float_ptr; \
988 \
989 POP3 (val, idx, bv); \
990 VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
991 i = SCM_I_INUM (idx); \
992 float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
993 \
994 if (SCM_LIKELY (SCM_I_INUMP (idx) \
995 && (i >= 0) \
996 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
997 && (ALIGNED_P (float_ptr, type)))) \
998 *float_ptr = scm_to_double (val); \
999 else \
1000 { \
1001 SYNC_REGISTER (); \
1002 scm_bytevector_ ## fn_stem ## _native_set_x (bv, idx, val); \
1003 } \
1004 NEXT; \
1005 }
1006
1007 VM_DEFINE_INSTRUCTION (200, bv_u8_set, "bv-u8-set", 0, 3, 0)
1008 BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
1009 VM_DEFINE_INSTRUCTION (201, bv_s8_set, "bv-s8-set", 0, 3, 0)
1010 BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
1011 VM_DEFINE_INSTRUCTION (202, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
1012 BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
1013 VM_DEFINE_INSTRUCTION (203, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
1014 BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
1015 VM_DEFINE_INSTRUCTION (204, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
1016 #if SIZEOF_VOID_P > 4
1017 BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4)
1018 #else
1019 BV_INT_SET (u32, uint32, 4)
1020 #endif
1021 VM_DEFINE_INSTRUCTION (205, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
1022 #if SIZEOF_VOID_P > 4
1023 BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4)
1024 #else
1025 BV_INT_SET (s32, int32, 4)
1026 #endif
1027 VM_DEFINE_INSTRUCTION (206, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
1028 BV_INT_SET (u64, uint64, 8)
1029 VM_DEFINE_INSTRUCTION (207, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
1030 BV_INT_SET (s64, int64, 8)
1031 VM_DEFINE_INSTRUCTION (208, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
1032 BV_FLOAT_SET (f32, ieee_single, float, 4)
1033 VM_DEFINE_INSTRUCTION (209, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
1034 BV_FLOAT_SET (f64, ieee_double, double, 8)
1035
1036 #undef BV_FIXABLE_INT_SET
1037 #undef BV_INT_SET
1038 #undef BV_FLOAT_SET
1039
1040 /*
1041 (defun renumber-ops ()
1042 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
1043 (interactive "")
1044 (save-excursion
1045 (let ((counter 127)) (goto-char (point-min))
1046 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
1047 (replace-match
1048 (number-to-string (setq counter (1+ counter)))
1049 t t nil 1)))))
1050 */
1051
1052 /*
1053 Local Variables:
1054 c-file-style: "gnu"
1055 End:
1056 */