vm-i-scheme.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. * Strings
  377. */
  378. VM_DEFINE_FUNCTION (163, string_length, "string-length", 1)
  379. {
  380. ARGS1 (str);
  381. if (SCM_LIKELY (scm_is_string (str)))
  382. RETURN (SCM_I_MAKINUM (scm_i_string_length (str)));
  383. else
  384. {
  385. SYNC_REGISTER ();
  386. RETURN (scm_string_length (str));
  387. }
  388. }
  389. VM_DEFINE_FUNCTION (164, string_ref, "string-ref", 2)
  390. {
  391. scm_t_signed_bits i = 0;
  392. ARGS2 (str, idx);
  393. if (SCM_LIKELY (scm_is_string (str)
  394. && SCM_I_INUMP (idx)
  395. && ((i = SCM_I_INUM (idx)) >= 0)
  396. && i < scm_i_string_length (str)))
  397. RETURN (SCM_MAKE_CHAR (scm_i_string_ref (str, i)));
  398. else
  399. {
  400. SYNC_REGISTER ();
  401. RETURN (scm_string_ref (str, idx));
  402. }
  403. }
  404. /* No string-set! instruction, as there is no good fast path there. */
  405. /*
  406. * Vectors and arrays
  407. */
  408. VM_DEFINE_FUNCTION (165, vector_length, "vector-length", 1)
  409. {
  410. ARGS1 (vect);
  411. if (SCM_LIKELY (SCM_I_IS_VECTOR (vect)))
  412. RETURN (SCM_I_MAKINUM (SCM_I_VECTOR_LENGTH (vect)));
  413. else
  414. {
  415. SYNC_REGISTER ();
  416. RETURN (scm_vector_length (vect));
  417. }
  418. }
  419. VM_DEFINE_FUNCTION (166, vector_ref, "vector-ref", 2)
  420. {
  421. scm_t_signed_bits i = 0;
  422. ARGS2 (vect, idx);
  423. if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect)
  424. && SCM_I_INUMP (idx)
  425. && ((i = SCM_I_INUM (idx)) >= 0)
  426. && i < SCM_I_VECTOR_LENGTH (vect)))
  427. RETURN (SCM_I_VECTOR_ELTS (vect)[i]);
  428. else
  429. {
  430. SYNC_REGISTER ();
  431. RETURN (scm_vector_ref (vect, idx));
  432. }
  433. }
  434. VM_DEFINE_INSTRUCTION (167, vector_set, "vector-set", 0, 3, 0)
  435. {
  436. scm_t_signed_bits i = 0;
  437. SCM vect, idx, val;
  438. POP3 (val, idx, vect);
  439. if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect)
  440. && SCM_I_INUMP (idx)
  441. && ((i = SCM_I_INUM (idx)) >= 0)
  442. && i < SCM_I_VECTOR_LENGTH (vect)))
  443. SCM_I_VECTOR_WELTS (vect)[i] = val;
  444. else
  445. {
  446. SYNC_REGISTER ();
  447. scm_vector_set_x (vect, idx, val);
  448. }
  449. NEXT;
  450. }
  451. VM_DEFINE_INSTRUCTION (168, make_array, "make-array", 3, -1, 1)
  452. {
  453. scm_t_uint32 len;
  454. SCM shape, ret;
  455. len = FETCH ();
  456. len = (len << 8) + FETCH ();
  457. len = (len << 8) + FETCH ();
  458. POP (shape);
  459. SYNC_REGISTER ();
  460. PRE_CHECK_UNDERFLOW (len);
  461. ret = scm_from_contiguous_array (shape, sp - len + 1, len);
  462. DROPN (len);
  463. PUSH (ret);
  464. NEXT;
  465. }
  466. /*
  467. * Structs
  468. */
  469. #define VM_VALIDATE_STRUCT(obj, proc) \
  470. if (SCM_UNLIKELY (!SCM_STRUCTP (obj))) \
  471. { \
  472. func_name = proc; \
  473. finish_args = (obj); \
  474. goto vm_error_not_a_struct; \
  475. }
  476. VM_DEFINE_FUNCTION (169, struct_p, "struct?", 1)
  477. {
  478. ARGS1 (obj);
  479. RETURN (scm_from_bool (SCM_STRUCTP (obj)));
  480. }
  481. VM_DEFINE_FUNCTION (170, struct_vtable, "struct-vtable", 1)
  482. {
  483. ARGS1 (obj);
  484. VM_VALIDATE_STRUCT (obj, "struct_vtable");
  485. RETURN (SCM_STRUCT_VTABLE (obj));
  486. }
  487. VM_DEFINE_INSTRUCTION (171, make_struct, "make-struct", 2, -1, 1)
  488. {
  489. unsigned h = FETCH ();
  490. unsigned l = FETCH ();
  491. scm_t_bits n = ((h << 8U) + l);
  492. SCM vtable = sp[-(n - 1)];
  493. const SCM *inits = sp - n + 2;
  494. SCM ret;
  495. SYNC_REGISTER ();
  496. if (SCM_LIKELY (SCM_STRUCTP (vtable)
  497. && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  498. && (SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size) + 1
  499. == n)
  500. && !SCM_VTABLE_INSTANCE_FINALIZER (vtable)))
  501. {
  502. /* Verily, we are making a simple struct with the right number of
  503. initializers, and no finalizer. */
  504. ret = scm_words ((scm_t_bits)SCM_STRUCT_DATA (vtable) | scm_tc3_struct,
  505. n + 1);
  506. SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2));
  507. memcpy (SCM_STRUCT_DATA (ret), inits, (n - 1) * sizeof (SCM));
  508. }
  509. else
  510. ret = scm_c_make_structv (vtable, 0, n - 1, (scm_t_bits *) inits);
  511. DROPN (n);
  512. PUSH (ret);
  513. NEXT;
  514. }
  515. VM_DEFINE_FUNCTION (172, struct_ref, "struct-ref", 2)
  516. {
  517. ARGS2 (obj, pos);
  518. if (SCM_LIKELY (SCM_STRUCTP (obj)
  519. && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
  520. SCM_VTABLE_FLAG_SIMPLE)
  521. && SCM_I_INUMP (pos)))
  522. {
  523. SCM vtable;
  524. scm_t_bits index, len;
  525. /* True, an inum is a signed value, but cast to unsigned it will
  526. certainly be more than the length, so we will fall through if
  527. index is negative. */
  528. index = SCM_I_INUM (pos);
  529. vtable = SCM_STRUCT_VTABLE (obj);
  530. len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
  531. if (SCM_LIKELY (index < len))
  532. {
  533. scm_t_bits *data = SCM_STRUCT_DATA (obj);
  534. RETURN (SCM_PACK (data[index]));
  535. }
  536. }
  537. SYNC_REGISTER ();
  538. RETURN (scm_struct_ref (obj, pos));
  539. }
  540. VM_DEFINE_FUNCTION (173, struct_set, "struct-set", 3)
  541. {
  542. ARGS3 (obj, pos, val);
  543. if (SCM_LIKELY (SCM_STRUCTP (obj)
  544. && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
  545. SCM_VTABLE_FLAG_SIMPLE)
  546. && SCM_STRUCT_VTABLE_FLAG_IS_SET (obj,
  547. SCM_VTABLE_FLAG_SIMPLE_RW)
  548. && SCM_I_INUMP (pos)))
  549. {
  550. SCM vtable;
  551. scm_t_bits index, len;
  552. /* See above regarding index being >= 0. */
  553. index = SCM_I_INUM (pos);
  554. vtable = SCM_STRUCT_VTABLE (obj);
  555. len = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
  556. if (SCM_LIKELY (index < len))
  557. {
  558. scm_t_bits *data = SCM_STRUCT_DATA (obj);
  559. data[index] = SCM_UNPACK (val);
  560. RETURN (val);
  561. }
  562. }
  563. SYNC_REGISTER ();
  564. RETURN (scm_struct_set_x (obj, pos, val));
  565. }
  566. /*
  567. * GOOPS support
  568. */
  569. VM_DEFINE_FUNCTION (174, class_of, "class-of", 1)
  570. {
  571. ARGS1 (obj);
  572. if (SCM_INSTANCEP (obj))
  573. RETURN (SCM_CLASS_OF (obj));
  574. SYNC_REGISTER ();
  575. RETURN (scm_class_of (obj));
  576. }
  577. /* FIXME: No checking whatsoever. */
  578. VM_DEFINE_FUNCTION (175, slot_ref, "slot-ref", 2)
  579. {
  580. size_t slot;
  581. ARGS2 (instance, idx);
  582. slot = SCM_I_INUM (idx);
  583. RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
  584. }
  585. /* FIXME: No checking whatsoever. */
  586. VM_DEFINE_INSTRUCTION (176, slot_set, "slot-set", 0, 3, 0)
  587. {
  588. SCM instance, idx, val;
  589. size_t slot;
  590. POP3 (val, idx, instance);
  591. slot = SCM_I_INUM (idx);
  592. SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (val);
  593. NEXT;
  594. }
  595. /*
  596. * Bytevectors
  597. */
  598. #define VM_VALIDATE_BYTEVECTOR(x, proc) \
  599. do \
  600. { \
  601. if (SCM_UNLIKELY (!SCM_BYTEVECTOR_P (x))) \
  602. { \
  603. func_name = proc; \
  604. finish_args = x; \
  605. goto vm_error_not_a_bytevector; \
  606. } \
  607. } \
  608. while (0)
  609. #define BV_REF_WITH_ENDIANNESS(stem, fn_stem) \
  610. { \
  611. SCM endianness; \
  612. POP (endianness); \
  613. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  614. goto VM_LABEL (bv_##stem##_native_ref); \
  615. { \
  616. ARGS2 (bv, idx); \
  617. SYNC_REGISTER (); \
  618. RETURN (scm_bytevector_##fn_stem##_ref (bv, idx, endianness)); \
  619. } \
  620. }
  621. /* Return true (non-zero) if PTR has suitable alignment for TYPE. */
  622. #define ALIGNED_P(ptr, type) \
  623. ((scm_t_uintptr) (ptr) % alignof_type (type) == 0)
  624. VM_DEFINE_FUNCTION (177, bv_u16_ref, "bv-u16-ref", 3)
  625. BV_REF_WITH_ENDIANNESS (u16, u16)
  626. VM_DEFINE_FUNCTION (178, bv_s16_ref, "bv-s16-ref", 3)
  627. BV_REF_WITH_ENDIANNESS (s16, s16)
  628. VM_DEFINE_FUNCTION (179, bv_u32_ref, "bv-u32-ref", 3)
  629. BV_REF_WITH_ENDIANNESS (u32, u32)
  630. VM_DEFINE_FUNCTION (180, bv_s32_ref, "bv-s32-ref", 3)
  631. BV_REF_WITH_ENDIANNESS (s32, s32)
  632. VM_DEFINE_FUNCTION (181, bv_u64_ref, "bv-u64-ref", 3)
  633. BV_REF_WITH_ENDIANNESS (u64, u64)
  634. VM_DEFINE_FUNCTION (182, bv_s64_ref, "bv-s64-ref", 3)
  635. BV_REF_WITH_ENDIANNESS (s64, s64)
  636. VM_DEFINE_FUNCTION (183, bv_f32_ref, "bv-f32-ref", 3)
  637. BV_REF_WITH_ENDIANNESS (f32, ieee_single)
  638. VM_DEFINE_FUNCTION (184, bv_f64_ref, "bv-f64-ref", 3)
  639. BV_REF_WITH_ENDIANNESS (f64, ieee_double)
  640. #undef BV_REF_WITH_ENDIANNESS
  641. #define BV_FIXABLE_INT_REF(stem, fn_stem, type, size) \
  642. { \
  643. scm_t_signed_bits i; \
  644. const scm_t_ ## type *int_ptr; \
  645. ARGS2 (bv, idx); \
  646. \
  647. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
  648. i = SCM_I_INUM (idx); \
  649. int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  650. \
  651. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  652. && (i >= 0) \
  653. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  654. && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
  655. RETURN (SCM_I_MAKINUM (*int_ptr)); \
  656. else \
  657. { \
  658. SYNC_REGISTER (); \
  659. RETURN (scm_bytevector_ ## fn_stem ## _ref (bv, idx)); \
  660. } \
  661. }
  662. #define BV_INT_REF(stem, type, size) \
  663. { \
  664. scm_t_signed_bits i; \
  665. const scm_t_ ## type *int_ptr; \
  666. ARGS2 (bv, idx); \
  667. \
  668. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
  669. i = SCM_I_INUM (idx); \
  670. int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  671. \
  672. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  673. && (i >= 0) \
  674. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  675. && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
  676. { \
  677. scm_t_ ## type x = *int_ptr; \
  678. if (SCM_FIXABLE (x)) \
  679. RETURN (SCM_I_MAKINUM (x)); \
  680. else \
  681. { \
  682. SYNC_REGISTER (); \
  683. RETURN (scm_from_ ## type (x)); \
  684. } \
  685. } \
  686. else \
  687. { \
  688. SYNC_REGISTER (); \
  689. RETURN (scm_bytevector_ ## stem ## _native_ref (bv, idx)); \
  690. } \
  691. }
  692. #define BV_FLOAT_REF(stem, fn_stem, type, size) \
  693. { \
  694. scm_t_signed_bits i; \
  695. const type *float_ptr; \
  696. ARGS2 (bv, idx); \
  697. \
  698. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-ref"); \
  699. i = SCM_I_INUM (idx); \
  700. float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  701. \
  702. SYNC_REGISTER (); \
  703. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  704. && (i >= 0) \
  705. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  706. && (ALIGNED_P (float_ptr, type)))) \
  707. RETURN (scm_from_double (*float_ptr)); \
  708. else \
  709. RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
  710. }
  711. VM_DEFINE_FUNCTION (185, bv_u8_ref, "bv-u8-ref", 2)
  712. BV_FIXABLE_INT_REF (u8, u8, uint8, 1)
  713. VM_DEFINE_FUNCTION (186, bv_s8_ref, "bv-s8-ref", 2)
  714. BV_FIXABLE_INT_REF (s8, s8, int8, 1)
  715. VM_DEFINE_FUNCTION (187, bv_u16_native_ref, "bv-u16-native-ref", 2)
  716. BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2)
  717. VM_DEFINE_FUNCTION (188, bv_s16_native_ref, "bv-s16-native-ref", 2)
  718. BV_FIXABLE_INT_REF (s16, s16_native, int16, 2)
  719. VM_DEFINE_FUNCTION (189, bv_u32_native_ref, "bv-u32-native-ref", 2)
  720. #if SIZEOF_VOID_P > 4
  721. BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4)
  722. #else
  723. BV_INT_REF (u32, uint32, 4)
  724. #endif
  725. VM_DEFINE_FUNCTION (190, bv_s32_native_ref, "bv-s32-native-ref", 2)
  726. #if SIZEOF_VOID_P > 4
  727. BV_FIXABLE_INT_REF (s32, s32_native, int32, 4)
  728. #else
  729. BV_INT_REF (s32, int32, 4)
  730. #endif
  731. VM_DEFINE_FUNCTION (191, bv_u64_native_ref, "bv-u64-native-ref", 2)
  732. BV_INT_REF (u64, uint64, 8)
  733. VM_DEFINE_FUNCTION (192, bv_s64_native_ref, "bv-s64-native-ref", 2)
  734. BV_INT_REF (s64, int64, 8)
  735. VM_DEFINE_FUNCTION (193, bv_f32_native_ref, "bv-f32-native-ref", 2)
  736. BV_FLOAT_REF (f32, ieee_single, float, 4)
  737. VM_DEFINE_FUNCTION (194, bv_f64_native_ref, "bv-f64-native-ref", 2)
  738. BV_FLOAT_REF (f64, ieee_double, double, 8)
  739. #undef BV_FIXABLE_INT_REF
  740. #undef BV_INT_REF
  741. #undef BV_FLOAT_REF
  742. #define BV_SET_WITH_ENDIANNESS(stem, fn_stem) \
  743. { \
  744. SCM endianness; \
  745. POP (endianness); \
  746. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  747. goto VM_LABEL (bv_##stem##_native_set); \
  748. { \
  749. SCM bv, idx, val; POP3 (val, idx, bv); \
  750. SYNC_REGISTER (); \
  751. scm_bytevector_##fn_stem##_set_x (bv, idx, val, endianness); \
  752. NEXT; \
  753. } \
  754. }
  755. VM_DEFINE_INSTRUCTION (195, bv_u16_set, "bv-u16-set", 0, 4, 0)
  756. BV_SET_WITH_ENDIANNESS (u16, u16)
  757. VM_DEFINE_INSTRUCTION (196, bv_s16_set, "bv-s16-set", 0, 4, 0)
  758. BV_SET_WITH_ENDIANNESS (s16, s16)
  759. VM_DEFINE_INSTRUCTION (197, bv_u32_set, "bv-u32-set", 0, 4, 0)
  760. BV_SET_WITH_ENDIANNESS (u32, u32)
  761. VM_DEFINE_INSTRUCTION (198, bv_s32_set, "bv-s32-set", 0, 4, 0)
  762. BV_SET_WITH_ENDIANNESS (s32, s32)
  763. VM_DEFINE_INSTRUCTION (199, bv_u64_set, "bv-u64-set", 0, 4, 0)
  764. BV_SET_WITH_ENDIANNESS (u64, u64)
  765. VM_DEFINE_INSTRUCTION (200, bv_s64_set, "bv-s64-set", 0, 4, 0)
  766. BV_SET_WITH_ENDIANNESS (s64, s64)
  767. VM_DEFINE_INSTRUCTION (201, bv_f32_set, "bv-f32-set", 0, 4, 0)
  768. BV_SET_WITH_ENDIANNESS (f32, ieee_single)
  769. VM_DEFINE_INSTRUCTION (202, bv_f64_set, "bv-f64-set", 0, 4, 0)
  770. BV_SET_WITH_ENDIANNESS (f64, ieee_double)
  771. #undef BV_SET_WITH_ENDIANNESS
  772. #define BV_FIXABLE_INT_SET(stem, fn_stem, type, min, max, size) \
  773. { \
  774. scm_t_signed_bits i, j = 0; \
  775. SCM bv, idx, val; \
  776. scm_t_ ## type *int_ptr; \
  777. \
  778. POP3 (val, idx, bv); \
  779. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
  780. i = SCM_I_INUM (idx); \
  781. int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  782. \
  783. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  784. && (i >= 0) \
  785. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  786. && (ALIGNED_P (int_ptr, scm_t_ ## type)) \
  787. && (SCM_I_INUMP (val)) \
  788. && ((j = SCM_I_INUM (val)) >= min) \
  789. && (j <= max))) \
  790. *int_ptr = (scm_t_ ## type) j; \
  791. else \
  792. { \
  793. SYNC_REGISTER (); \
  794. scm_bytevector_ ## fn_stem ## _set_x (bv, idx, val); \
  795. } \
  796. NEXT; \
  797. }
  798. #define BV_INT_SET(stem, type, size) \
  799. { \
  800. scm_t_signed_bits i = 0; \
  801. SCM bv, idx, val; \
  802. scm_t_ ## type *int_ptr; \
  803. \
  804. POP3 (val, idx, bv); \
  805. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
  806. i = SCM_I_INUM (idx); \
  807. int_ptr = (scm_t_ ## type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  808. \
  809. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  810. && (i >= 0) \
  811. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  812. && (ALIGNED_P (int_ptr, scm_t_ ## type)))) \
  813. *int_ptr = scm_to_ ## type (val); \
  814. else \
  815. { \
  816. SYNC_REGISTER (); \
  817. scm_bytevector_ ## stem ## _native_set_x (bv, idx, val); \
  818. } \
  819. NEXT; \
  820. }
  821. #define BV_FLOAT_SET(stem, fn_stem, type, size) \
  822. { \
  823. scm_t_signed_bits i = 0; \
  824. SCM bv, idx, val; \
  825. type *float_ptr; \
  826. \
  827. POP3 (val, idx, bv); \
  828. VM_VALIDATE_BYTEVECTOR (bv, "bv-" #stem "-set"); \
  829. i = SCM_I_INUM (idx); \
  830. float_ptr = (type *) (SCM_BYTEVECTOR_CONTENTS (bv) + i); \
  831. \
  832. if (SCM_LIKELY (SCM_I_INUMP (idx) \
  833. && (i >= 0) \
  834. && (i + size <= SCM_BYTEVECTOR_LENGTH (bv)) \
  835. && (ALIGNED_P (float_ptr, type)))) \
  836. *float_ptr = scm_to_double (val); \
  837. else \
  838. { \
  839. SYNC_REGISTER (); \
  840. scm_bytevector_ ## fn_stem ## _native_set_x (bv, idx, val); \
  841. } \
  842. NEXT; \
  843. }
  844. VM_DEFINE_INSTRUCTION (203, bv_u8_set, "bv-u8-set", 0, 3, 0)
  845. BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1)
  846. VM_DEFINE_INSTRUCTION (204, bv_s8_set, "bv-s8-set", 0, 3, 0)
  847. BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1)
  848. VM_DEFINE_INSTRUCTION (205, bv_u16_native_set, "bv-u16-native-set", 0, 3, 0)
  849. BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2)
  850. VM_DEFINE_INSTRUCTION (206, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0)
  851. BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2)
  852. VM_DEFINE_INSTRUCTION (207, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0)
  853. #if SIZEOF_VOID_P > 4
  854. BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4)
  855. #else
  856. BV_INT_SET (u32, uint32, 4)
  857. #endif
  858. VM_DEFINE_INSTRUCTION (208, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0)
  859. #if SIZEOF_VOID_P > 4
  860. BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4)
  861. #else
  862. BV_INT_SET (s32, int32, 4)
  863. #endif
  864. VM_DEFINE_INSTRUCTION (209, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0)
  865. BV_INT_SET (u64, uint64, 8)
  866. VM_DEFINE_INSTRUCTION (210, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0)
  867. BV_INT_SET (s64, int64, 8)
  868. VM_DEFINE_INSTRUCTION (211, bv_f32_native_set, "bv-f32-native-set", 0, 3, 0)
  869. BV_FLOAT_SET (f32, ieee_single, float, 4)
  870. VM_DEFINE_INSTRUCTION (212, bv_f64_native_set, "bv-f64-native-set", 0, 3, 0)
  871. BV_FLOAT_SET (f64, ieee_double, double, 8)
  872. #undef BV_FIXABLE_INT_SET
  873. #undef BV_INT_SET
  874. #undef BV_FLOAT_SET
  875. /*
  876. (defun renumber-ops ()
  877. "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
  878. (interactive "")
  879. (save-excursion
  880. (let ((counter 127)) (goto-char (point-min))
  881. (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
  882. (replace-match
  883. (number-to-string (setq counter (1+ counter)))
  884. t t nil 1)))))
  885. */
  886. /*
  887. Local Variables:
  888. c-file-style: "gnu"
  889. End:
  890. */