vm-i-scheme.c 28 KB

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