Merge branch 'master' of git://git.savannah.gnu.org/guile into elisp
[bpt/guile.git] / libguile / vm-i-scheme.c
1 /* Copyright (C) 2001, 2009 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 (100, not, "not", 1)
33 {
34 ARGS1 (x);
35 RETURN (SCM_BOOL (SCM_FALSEP (x)));
36 }
37
38 VM_DEFINE_FUNCTION (101, not_not, "not-not", 1)
39 {
40 ARGS1 (x);
41 RETURN (SCM_BOOL (!SCM_FALSEP (x)));
42 }
43
44 VM_DEFINE_FUNCTION (102, eq, "eq?", 2)
45 {
46 ARGS2 (x, y);
47 RETURN (SCM_BOOL (SCM_EQ_P (x, y)));
48 }
49
50 VM_DEFINE_FUNCTION (103, not_eq, "not-eq?", 2)
51 {
52 ARGS2 (x, y);
53 RETURN (SCM_BOOL (!SCM_EQ_P (x, y)));
54 }
55
56 VM_DEFINE_FUNCTION (104, nullp, "null?", 1)
57 {
58 ARGS1 (x);
59 RETURN (SCM_BOOL (SCM_NULLP (x)));
60 }
61
62 VM_DEFINE_FUNCTION (105, not_nullp, "not-null?", 1)
63 {
64 ARGS1 (x);
65 RETURN (SCM_BOOL (!SCM_NULLP (x)));
66 }
67
68 VM_DEFINE_FUNCTION (106, eqv, "eqv?", 2)
69 {
70 ARGS2 (x, y);
71 if (SCM_EQ_P (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 (107, equal, "equal?", 2)
80 {
81 ARGS2 (x, y);
82 if (SCM_EQ_P (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 (108, pairp, "pair?", 1)
91 {
92 ARGS1 (x);
93 RETURN (SCM_BOOL (SCM_CONSP (x)));
94 }
95
96 VM_DEFINE_FUNCTION (109, listp, "list?", 1)
97 {
98 ARGS1 (x);
99 RETURN (SCM_BOOL (scm_ilength (x) >= 0));
100 }
101
102 \f
103 /*
104 * Basic data
105 */
106
107 VM_DEFINE_FUNCTION (110, 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 (111, car, "car", 1)
121 {
122 ARGS1 (x);
123 VM_VALIDATE_CONS (x);
124 RETURN (SCM_CAR (x));
125 }
126
127 VM_DEFINE_FUNCTION (112, cdr, "cdr", 1)
128 {
129 ARGS1 (x);
130 VM_VALIDATE_CONS (x);
131 RETURN (SCM_CDR (x));
132 }
133
134 VM_DEFINE_INSTRUCTION (113, 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 (114, 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_BOOL (SCM_I_INUM (x) crel SCM_I_INUM (y))); \
165 SYNC_REGISTER (); \
166 RETURN (srel (x, y)); \
167 }
168
169 VM_DEFINE_FUNCTION (115, ee, "ee?", 2)
170 {
171 REL (==, scm_num_eq_p);
172 }
173
174 VM_DEFINE_FUNCTION (116, lt, "lt?", 2)
175 {
176 REL (<, scm_less_p);
177 }
178
179 VM_DEFINE_FUNCTION (117, le, "le?", 2)
180 {
181 REL (<=, scm_leq_p);
182 }
183
184 VM_DEFINE_FUNCTION (118, gt, "gt?", 2)
185 {
186 REL (>, scm_gr_p);
187 }
188
189 VM_DEFINE_FUNCTION (119, 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 (120, add, "add", 2)
214 {
215 FUNC2 (+, scm_sum);
216 }
217
218 VM_DEFINE_FUNCTION (121, sub, "sub", 2)
219 {
220 FUNC2 (-, scm_difference);
221 }
222
223 VM_DEFINE_FUNCTION (122, mul, "mul", 2)
224 {
225 ARGS2 (x, y);
226 SYNC_REGISTER ();
227 RETURN (scm_product (x, y));
228 }
229
230 VM_DEFINE_FUNCTION (123, div, "div", 2)
231 {
232 ARGS2 (x, y);
233 SYNC_REGISTER ();
234 RETURN (scm_divide (x, y));
235 }
236
237 VM_DEFINE_FUNCTION (124, quo, "quo", 2)
238 {
239 ARGS2 (x, y);
240 SYNC_REGISTER ();
241 RETURN (scm_quotient (x, y));
242 }
243
244 VM_DEFINE_FUNCTION (125, rem, "rem", 2)
245 {
246 ARGS2 (x, y);
247 SYNC_REGISTER ();
248 RETURN (scm_remainder (x, y));
249 }
250
251 VM_DEFINE_FUNCTION (126, mod, "mod", 2)
252 {
253 ARGS2 (x, y);
254 SYNC_REGISTER ();
255 RETURN (scm_modulo (x, y));
256 }
257
258 \f
259 /*
260 * GOOPS support
261 */
262 VM_DEFINE_FUNCTION (127, slot_ref, "slot-ref", 2)
263 {
264 size_t slot;
265 ARGS2 (instance, idx);
266 slot = SCM_I_INUM (idx);
267 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
268 }
269
270 VM_DEFINE_INSTRUCTION (128, slot_set, "slot-set", 0, 3, 0)
271 {
272 SCM instance, idx, val;
273 size_t slot;
274 POP (val);
275 POP (idx);
276 POP (instance);
277 slot = SCM_I_INUM (idx);
278 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
279 NEXT;
280 }
281
282 VM_DEFINE_FUNCTION (129, vector_ref, "vector-ref", 2)
283 {
284 long i = 0;
285 ARGS2 (vect, idx);
286 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
287 && SCM_I_INUMP (idx)
288 && ((i = SCM_I_INUM (idx)) >= 0)
289 && i < SCM_I_VECTOR_LENGTH (vect)))
290 RETURN (SCM_I_VECTOR_ELTS (vect)[i]);
291 else
292 RETURN (scm_vector_ref (vect, idx));
293 }
294
295 VM_DEFINE_INSTRUCTION (130, vector_set, "vector-set", 0, 3, 0)
296 {
297 long i = 0;
298 SCM vect, idx, val;
299 POP (val); POP (idx); POP (vect);
300 if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)
301 && SCM_I_INUMP (idx)
302 && ((i = SCM_I_INUM (idx)) >= 0)
303 && i < SCM_I_VECTOR_LENGTH (vect)))
304 SCM_I_VECTOR_WELTS (vect)[i] = val;
305 else
306 scm_vector_set_x (vect, idx, val);
307 NEXT;
308 }
309
310 #define VM_VALIDATE_BYTEVECTOR(x) \
311 if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
312 { finish_args = x; \
313 goto vm_error_not_a_bytevector; \
314 }
315
316 #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
317 { \
318 SCM endianness; \
319 POP (endianness); \
320 if (scm_is_eq (endianness, scm_i_native_endianness)) \
321 goto VM_LABEL (bv_##stem##_native_ref); \
322 { \
323 ARGS2 (bv, idx); \
324 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
325 } \
326 }
327
328 VM_DEFINE_FUNCTION (131, bv_u16_ref, "bv-u16-ref", 3)
329 BV_REF_WITH_ENDIANNESS (u16, u16)
330 VM_DEFINE_FUNCTION (132, bv_s16_ref, "bv-s16-ref", 3)
331 BV_REF_WITH_ENDIANNESS (s16, s16)
332 VM_DEFINE_FUNCTION (133, bv_u32_ref, "bv-u32-ref", 3)
333 BV_REF_WITH_ENDIANNESS (u32, u32)
334 VM_DEFINE_FUNCTION (134, bv_s32_ref, "bv-s32-ref", 3)
335 BV_REF_WITH_ENDIANNESS (s32, s32)
336 VM_DEFINE_FUNCTION (135, bv_u64_ref, "bv-u64-ref", 3)
337 BV_REF_WITH_ENDIANNESS (u64, u64)
338 VM_DEFINE_FUNCTION (136, bv_s64_ref, "bv-s64-ref", 3)
339 BV_REF_WITH_ENDIANNESS (s64, s64)
340 VM_DEFINE_FUNCTION (137, bv_f32_ref, "bv-f32-ref", 3)
341 BV_REF_WITH_ENDIANNESS (f32, ieee_single)
342 VM_DEFINE_FUNCTION (138, bv_f64_ref, "bv-f64-ref", 3)
343 BV_REF_WITH_ENDIANNESS (f64, ieee_double)
344
345 #undef BV_REF_WITH_ENDIANNESS
346
347 #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
348 { \
349 long i = 0; \
350 ARGS2 (bv, idx); \
351 VM_VALIDATE_BYTEVECTOR (bv); \
352 if (SCM_LIKELY (SCM_I_INUMP (idx) \
353 && ((i = SCM_I_INUM (idx)) >= 0) \
354 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
355 && (i % size == 0))) \
356 RETURN (SCM_I_MAKINUM (*(scm_t_##type*) \
357 (SCM_BYTEVECTOR_CONTENTS (bv) + i))); \
358 else \
359 RETURN (scm_bytevector_##fn_stem##_ref (bv, idx)); \
360 }
361
362 #define BV_INT_REF(stem, type, size) \
363 { \
364 long i = 0; \
365 ARGS2 (bv, idx); \
366 VM_VALIDATE_BYTEVECTOR (bv); \
367 if (SCM_LIKELY (SCM_I_INUMP (idx) \
368 && ((i = SCM_I_INUM (idx)) >= 0) \
369 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
370 && (i % size == 0))) \
371 { scm_t_##type x = (*(scm_t_##type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)); \
372 if (SCM_FIXABLE (x)) \
373 RETURN (SCM_I_MAKINUM (x)); \
374 else \
375 RETURN (scm_from_##type (x)); \
376 } \
377 else \
378 RETURN (scm_bytevector_##stem##_native_ref (bv, idx)); \
379 }
380
381 #define BV_FLOAT_REF(stem, fn_stem, type, size) \
382 { \
383 long i = 0; \
384 ARGS2 (bv, idx); \
385 VM_VALIDATE_BYTEVECTOR (bv); \
386 if (SCM_LIKELY (SCM_I_INUMP (idx) \
387 && ((i = SCM_I_INUM (idx)) >= 0) \
388 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
389 && (i % size == 0))) \
390 RETURN (scm_from_double ((*(type*)(SCM_BYTEVECTOR_CONTENTS (bv) + i)))); \
391 else \
392 RETURN (scm_bytevector_##fn_stem##_native_ref (bv, idx)); \
393 }
394
395 VM_DEFINE_FUNCTION (139, bv_u8_ref, "bv-u8-ref", 2)
396 BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
397 VM_DEFINE_FUNCTION (140, bv_s8_ref, "bv-s8-ref", 2)
398 BV_FIXABLE_INT_REF (s8, s8, int8, 1)
399 VM_DEFINE_FUNCTION (141, bv_u16_native_ref, "bv-u16-native-ref", 2)
400 BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
401 VM_DEFINE_FUNCTION (142, bv_s16_native_ref, "bv-s16-native-ref", 2)
402 BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
403 VM_DEFINE_FUNCTION (143, bv_u32_native_ref, "bv-u32-native-ref", 2)
404 /* FIXME: u32 is always a fixnum on 64-bit builds */
405 BV_INT_REF (u32, uint32, 4)
406 VM_DEFINE_FUNCTION (144, bv_s32_native_ref, "bv-s32-native-ref", 2)
407 BV_INT_REF (s32, int32, 4)
408 VM_DEFINE_FUNCTION (145, bv_u64_native_ref, "bv-u64-native-ref", 2)
409 BV_INT_REF (u64, uint64, 8)
410 VM_DEFINE_FUNCTION (146, bv_s64_native_ref, "bv-s64-native-ref", 2)
411 BV_INT_REF (s64, int64, 8)
412 VM_DEFINE_FUNCTION (147, bv_f32_native_ref, "bv-f32-native-ref", 2)
413 BV_FLOAT_REF (f32, ieee_single, float, 4)
414 VM_DEFINE_FUNCTION (148, bv_f64_native_ref, "bv-f64-native-ref", 2)
415 BV_FLOAT_REF (f64, ieee_double, double, 8)
416
417 #undef BV_FIXABLE_INT_REF
418 #undef BV_INT_REF
419 #undef BV_FLOAT_REF
420
421
422
423 #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
424 { \
425 SCM endianness; \
426 POP (endianness); \
427 if (scm_is_eq (endianness, scm_i_native_endianness)) \
428 goto VM_LABEL (bv_##stem##_native_set); \
429 { \
430 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
431 scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
432 NEXT; \
433 } \
434 }
435
436 VM_DEFINE_INSTRUCTION (149, bv_u16_set, "bv-u16-set", 0, 4, 0)
437 BV_SET_WITH_ENDIANNESS (u16, u16)
438 VM_DEFINE_INSTRUCTION (150, bv_s16_set, "bv-s16-set", 0, 4, 0)
439 BV_SET_WITH_ENDIANNESS (s16, s16)
440 VM_DEFINE_INSTRUCTION (151, bv_u32_set, "bv-u32-set", 0, 4, 0)
441 BV_SET_WITH_ENDIANNESS (u32, u32)
442 VM_DEFINE_INSTRUCTION (152, bv_s32_set, "bv-s32-set", 0, 4, 0)
443 BV_SET_WITH_ENDIANNESS (s32, s32)
444 VM_DEFINE_INSTRUCTION (153, bv_u64_set, "bv-u64-set", 0, 4, 0)
445 BV_SET_WITH_ENDIANNESS (u64, u64)
446 VM_DEFINE_INSTRUCTION (154, bv_s64_set, "bv-s64-set", 0, 4, 0)
447 BV_SET_WITH_ENDIANNESS (s64, s64)
448 VM_DEFINE_INSTRUCTION (155, bv_f32_set, "bv-f32-set", 0, 4, 0)
449 BV_SET_WITH_ENDIANNESS (f32, ieee_single)
450 VM_DEFINE_INSTRUCTION (156, bv_f64_set, "bv-f64-set", 0, 4, 0)
451 BV_SET_WITH_ENDIANNESS (f64, ieee_double)
452
453 #undef BV_SET_WITH_ENDIANNESS
454
455 #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
456 { \
457 long i = 0, j = 0; \
458 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
459 VM_VALIDATE_BYTEVECTOR (bv); \
460 if (SCM_LIKELY (SCM_I_INUMP (idx) \
461 && ((i = SCM_I_INUM (idx)) >= 0) \
462 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
463 && (i % size == 0) \
464 && (SCM_I_INUMP (val)) \
465 && ((j = SCM_I_INUM (val)) >= min) \
466 && (j <= max))) \
467 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = (scm_t_##type)j; \
468 else \
469 scm_bytevector_##fn_stem##_set_x (bv, idx, val); \
470 NEXT; \
471 }
472
473 #define BV_INT_SET(stem, type, size) \
474 { \
475 long i = 0; \
476 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
477 VM_VALIDATE_BYTEVECTOR (bv); \
478 if (SCM_LIKELY (SCM_I_INUMP (idx) \
479 && ((i = SCM_I_INUM (idx)) >= 0) \
480 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
481 && (i % size == 0))) \
482 *(scm_t_##type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_##type (val); \
483 else \
484 scm_bytevector_##stem##_native_set_x (bv, idx, val); \
485 NEXT; \
486 }
487
488 #define BV_FLOAT_SET(stem, fn_stem, type, size) \
489 { \
490 long i = 0; \
491 SCM bv, idx, val; POP (val); POP (idx); POP (bv); \
492 VM_VALIDATE_BYTEVECTOR (bv); \
493 if (SCM_LIKELY (SCM_I_INUMP (idx) \
494 && ((i = SCM_I_INUM (idx)) >= 0) \
495 && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
496 && (i % size == 0))) \
497 *(type*) (SCM_BYTEVECTOR_CONTENTS (bv) + i) = scm_to_double (val); \
498 else \
499 scm_bytevector_##fn_stem##_native_set_x (bv, idx, val); \
500 NEXT; \
501 }
502
503 VM_DEFINE_INSTRUCTION (157, bv_u8_set, "bv-u8-set", 0, 3, 0)
504 BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
505 VM_DEFINE_INSTRUCTION (158, bv_s8_set, "bv-s8-set", 0, 3, 0)
506 BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
507 VM_DEFINE_INSTRUCTION (159, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
508 BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
509 VM_DEFINE_INSTRUCTION (160, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
510 BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
511 VM_DEFINE_INSTRUCTION (161, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
512 /* FIXME: u32 is always a fixnum on 64-bit builds */
513 BV_INT_SET (u32, uint32, 4)
514 VM_DEFINE_INSTRUCTION (162, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
515 BV_INT_SET (s32, int32, 4)
516 VM_DEFINE_INSTRUCTION (163, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
517 BV_INT_SET (u64, uint64, 8)
518 VM_DEFINE_INSTRUCTION (164, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
519 BV_INT_SET (s64, int64, 8)
520 VM_DEFINE_INSTRUCTION (165, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
521 BV_FLOAT_SET (f32, ieee_single, float, 4)
522 VM_DEFINE_INSTRUCTION (166, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
523 BV_FLOAT_SET (f64, ieee_double, double, 8)
524
525 #undef BV_FIXABLE_INT_SET
526 #undef BV_INT_SET
527 #undef BV_FLOAT_SET
528
529 /*
530 (defun renumber-ops ()
531 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
532 (interactive "")
533 (save-excursion
534 (let ((counter 99)) (goto-char (point-min))
535 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
536 (replace-match
537 (number-to-string (setq counter (1+ counter)))
538 t t nil 1)))))
539 */
540
541 /*
542 Local Variables:
543 c-file-style: "gnu"
544 End:
545 */