intrinsics.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* Copyright 2018-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <math.h>
  19. #include "alist.h"
  20. #include "atomics-internal.h"
  21. #include "boolean.h"
  22. #include "cache-internal.h"
  23. #include "extensions.h"
  24. #include "fluids.h"
  25. #include "frames.h"
  26. #include "gc-inline.h"
  27. #include "goops.h"
  28. #include "gsubr.h"
  29. #include "keywords.h"
  30. #include "modules.h"
  31. #include "numbers.h"
  32. #include "symbols.h"
  33. #include "threads.h"
  34. #include "version.h"
  35. #include "intrinsics.h"
  36. struct scm_vm_intrinsics scm_vm_intrinsics;
  37. SCM_DEFINE (scm_intrinsic_list, "intrinsic-list", 0, 0, 0,
  38. (void),
  39. "")
  40. #define FUNC_NAME s_scm_intrinsic_list
  41. {
  42. SCM list = SCM_EOL;
  43. #define ADD_INTRINSIC(type, id, name, ID) \
  44. if (name) \
  45. list = scm_acons (scm_from_latin1_symbol (name), \
  46. scm_from_int (SCM_VM_INTRINSIC_##ID), \
  47. list);
  48. SCM_FOR_ALL_VM_INTRINSICS (ADD_INTRINSIC);
  49. #undef ADD_INTRINSIC
  50. return list;
  51. }
  52. #undef FUNC_NAME
  53. static SCM
  54. add_immediate (SCM a, uint8_t b)
  55. {
  56. if (SCM_LIKELY (SCM_I_INUMP (a)))
  57. {
  58. scm_t_signed_bits sum = SCM_I_INUM (a) + b;
  59. if (SCM_LIKELY (SCM_POSFIXABLE (sum)))
  60. return SCM_I_MAKINUM (sum);
  61. }
  62. return scm_sum (a, scm_from_uint8 (b));
  63. }
  64. static SCM
  65. sub_immediate (SCM a, uint8_t b)
  66. {
  67. if (SCM_LIKELY (SCM_I_INUMP (a)))
  68. {
  69. scm_t_signed_bits diff = SCM_I_INUM (a) - b;
  70. if (SCM_LIKELY (SCM_NEGFIXABLE (diff)))
  71. return SCM_I_MAKINUM (diff);
  72. }
  73. return scm_difference (a, scm_from_uint8 (b));
  74. }
  75. static void
  76. string_set_x (SCM str, size_t idx, uint32_t ch)
  77. {
  78. str = scm_i_string_start_writing (str);
  79. scm_i_string_set_x (str, idx, ch);
  80. scm_i_string_stop_writing ();
  81. }
  82. static SCM
  83. string_to_number (SCM str)
  84. {
  85. return scm_string_to_number (str, SCM_UNDEFINED /* radix = 10 */);
  86. }
  87. static uint64_t
  88. scm_to_uint64_truncate (SCM x)
  89. {
  90. if (SCM_LIKELY (SCM_I_INUMP (x)))
  91. return (uint64_t) SCM_I_INUM (x);
  92. else
  93. return scm_to_uint64 (scm_logand (x, scm_from_uint64 ((uint64_t) -1)));
  94. }
  95. #if INDIRECT_INT64_INTRINSICS
  96. static void
  97. indirect_scm_to_int64 (int64_t *dst, SCM x)
  98. {
  99. *dst = scm_to_int64 (x);
  100. }
  101. static void
  102. indirect_scm_to_uint64 (uint64_t *dst, SCM x)
  103. {
  104. *dst = scm_to_uint64 (x);
  105. }
  106. static void
  107. indirect_scm_to_uint64_truncate (uint64_t *dst, SCM x)
  108. {
  109. *dst = scm_to_uint64_truncate (x);
  110. }
  111. static SCM
  112. indirect_scm_from_int64 (int64_t *src)
  113. {
  114. return scm_from_int64 (*src);
  115. }
  116. static SCM
  117. indirect_scm_from_uint64 (uint64_t *src)
  118. {
  119. return scm_from_uint64 (*src);
  120. }
  121. #endif
  122. static SCM
  123. logsub (SCM x, SCM y)
  124. {
  125. if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
  126. {
  127. scm_t_signed_bits a, b;
  128. a = SCM_I_INUM (x);
  129. b = SCM_I_INUM (y);
  130. return SCM_I_MAKINUM (a & ~b);
  131. }
  132. return scm_logand (x, scm_lognot (y));
  133. }
  134. static void
  135. wind (scm_thread *thread, SCM winder, SCM unwinder)
  136. {
  137. scm_dynstack_push_dynwind (&thread->dynstack, winder, unwinder);
  138. }
  139. static void
  140. unwind (scm_thread *thread)
  141. {
  142. scm_dynstack_pop (&thread->dynstack);
  143. }
  144. static void
  145. push_fluid (scm_thread *thread, SCM fluid, SCM value)
  146. {
  147. scm_dynstack_push_fluid (&thread->dynstack, fluid, value,
  148. thread->dynamic_state);
  149. }
  150. static void
  151. pop_fluid (scm_thread *thread)
  152. {
  153. scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
  154. }
  155. static SCM
  156. fluid_ref (scm_thread *thread, SCM fluid)
  157. {
  158. struct scm_cache_entry *entry;
  159. /* If we find FLUID in the cache, then it is indeed a fluid. */
  160. entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
  161. if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)
  162. && !SCM_UNBNDP (SCM_PACK (entry->value))))
  163. return SCM_PACK (entry->value);
  164. return scm_fluid_ref (fluid);
  165. }
  166. static void
  167. fluid_set_x (scm_thread *thread, SCM fluid, SCM value)
  168. {
  169. struct scm_cache_entry *entry;
  170. /* If we find FLUID in the cache, then it is indeed a fluid. */
  171. entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
  172. if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)))
  173. entry->value = SCM_UNPACK (value);
  174. else
  175. scm_fluid_set_x (fluid, value);
  176. }
  177. static void
  178. push_dynamic_state (scm_thread *thread, SCM state)
  179. {
  180. scm_dynstack_push_dynamic_state (&thread->dynstack, state,
  181. thread->dynamic_state);
  182. }
  183. static void
  184. pop_dynamic_state (scm_thread *thread)
  185. {
  186. scm_dynstack_unwind_dynamic_state (&thread->dynstack,
  187. thread->dynamic_state);
  188. }
  189. static SCM
  190. lsh (SCM a, uint64_t b)
  191. {
  192. if (SCM_LIKELY (SCM_I_INUMP (a))
  193. && b < (uint64_t) (SCM_I_FIXNUM_BIT - 1)
  194. && ((scm_t_bits)
  195. (SCM_SRS (SCM_I_INUM (a), (SCM_I_FIXNUM_BIT-1 - b)) + 1)
  196. <= 1))
  197. {
  198. scm_t_signed_bits nn = SCM_I_INUM (a);
  199. return SCM_I_MAKINUM (nn < 0 ? -(-nn << b) : (nn << b));
  200. }
  201. else
  202. return scm_ash (a, scm_from_uint64 (b));
  203. }
  204. static SCM
  205. rsh (SCM a, uint64_t b)
  206. {
  207. if (SCM_LIKELY (SCM_I_INUMP (a)))
  208. {
  209. if (b > (uint64_t) (SCM_I_FIXNUM_BIT - 1))
  210. b = SCM_I_FIXNUM_BIT - 1;
  211. return SCM_I_MAKINUM (SCM_SRS (SCM_I_INUM (a), b));
  212. }
  213. else
  214. return scm_ash (a, scm_difference (SCM_INUM0, scm_from_uint64 (b)));
  215. }
  216. #if INDIRECT_INT64_INTRINSICS
  217. static SCM
  218. indirect_lsh (SCM a, uint64_t *b)
  219. {
  220. return lsh (a, *b);
  221. }
  222. static SCM
  223. indirect_rsh (SCM a, uint64_t *b)
  224. {
  225. return rsh (a, *b);
  226. }
  227. #endif
  228. static SCM
  229. lsh_immediate (SCM a, uint8_t b)
  230. {
  231. return lsh (a, b);
  232. }
  233. static SCM
  234. rsh_immediate (SCM a, uint8_t b)
  235. {
  236. return rsh (a, b);
  237. }
  238. static enum scm_compare
  239. less_p (SCM a, SCM b)
  240. {
  241. if (SCM_LIKELY (SCM_I_INUMP (a) && SCM_I_INUMP (b)))
  242. {
  243. scm_t_signed_bits a_bits = SCM_UNPACK (a);
  244. scm_t_signed_bits b_bits = SCM_UNPACK (b);
  245. return a_bits < b_bits ? SCM_F_COMPARE_LESS_THAN : SCM_F_COMPARE_NONE;
  246. }
  247. if (scm_is_true (scm_nan_p (a)) || scm_is_true (scm_nan_p (b)))
  248. return SCM_F_COMPARE_INVALID;
  249. else if (scm_is_true (scm_less_p (a, b)))
  250. return SCM_F_COMPARE_LESS_THAN;
  251. else
  252. return SCM_F_COMPARE_NONE;
  253. }
  254. static int
  255. numerically_equal_p (SCM a, SCM b)
  256. {
  257. if (SCM_LIKELY (SCM_I_INUMP (a) && SCM_I_INUMP (b)))
  258. return scm_is_eq (a, b);
  259. return scm_is_true (scm_num_eq_p (a, b));
  260. }
  261. static SCM
  262. resolve_module (SCM name, uint8_t public_p)
  263. {
  264. SCM mod;
  265. if (!scm_module_system_booted_p)
  266. return SCM_BOOL_F;
  267. mod = scm_maybe_resolve_module (name);
  268. if (scm_is_false (mod))
  269. scm_misc_error (NULL, "Module named ~s does not exist",
  270. scm_list_1 (name));
  271. if (public_p)
  272. {
  273. mod = scm_module_public_interface (mod);
  274. if (scm_is_false (mod))
  275. scm_misc_error (NULL, "Module named ~s has no public interface",
  276. scm_list_1 (name));
  277. }
  278. return mod;
  279. }
  280. static SCM
  281. lookup (SCM module, SCM name)
  282. {
  283. /* If MODULE was captured before modules were booted, use the root
  284. module. Not so nice, but hey... */
  285. if (scm_is_false (module))
  286. module = scm_the_root_module ();
  287. return scm_module_variable (module, name);
  288. }
  289. static void throw_ (SCM key, SCM args) SCM_NORETURN;
  290. static void throw_with_value (SCM val, SCM key_subr_and_message) SCM_NORETURN;
  291. static void throw_with_value_and_data (SCM val, SCM key_subr_and_message) SCM_NORETURN;
  292. static void
  293. throw_ (SCM key, SCM args)
  294. {
  295. scm_throw (key, args);
  296. abort(); /* not reached */
  297. }
  298. static void
  299. throw_with_value (SCM val, SCM key_subr_and_message)
  300. {
  301. SCM key, subr, message, args, data;
  302. key = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 0);
  303. subr = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 1);
  304. message = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 2);
  305. args = scm_list_1 (val);
  306. data = SCM_BOOL_F;
  307. throw_ (key, scm_list_4 (subr, message, args, data));
  308. }
  309. static void
  310. throw_with_value_and_data (SCM val, SCM key_subr_and_message)
  311. {
  312. SCM key, subr, message, args, data;
  313. key = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 0);
  314. subr = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 1);
  315. message = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 2);
  316. args = scm_list_1 (val);
  317. data = args;
  318. throw_ (key, scm_list_4 (subr, message, args, data));
  319. }
  320. static void error_wrong_num_args (scm_thread *) SCM_NORETURN;
  321. static void error_no_values (void) SCM_NORETURN;
  322. static void error_not_enough_values (void) SCM_NORETURN;
  323. static void error_wrong_number_of_values (uint32_t expected) SCM_NORETURN;
  324. static void
  325. error_wrong_num_args (scm_thread *thread)
  326. {
  327. SCM callee = SCM_FRAME_LOCAL (thread->vm.fp, 0);
  328. scm_wrong_num_args (callee);
  329. }
  330. static void
  331. error_no_values (void)
  332. {
  333. scm_misc_error (NULL, "Zero values returned to single-valued continuation",
  334. SCM_EOL);
  335. }
  336. static void
  337. error_not_enough_values (void)
  338. {
  339. scm_misc_error (NULL, "Too few values returned to continuation", SCM_EOL);
  340. }
  341. static void
  342. error_wrong_number_of_values (uint32_t expected)
  343. {
  344. scm_misc_error (NULL,
  345. "Wrong number of values returned to continuation (expected ~a)",
  346. scm_list_1 (scm_from_uint32 (expected)));
  347. }
  348. static SCM
  349. allocate_words (scm_thread *thread, size_t n)
  350. {
  351. return SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, n));
  352. }
  353. static SCM
  354. allocate_words_with_freelist (scm_thread *thread, size_t freelist_idx)
  355. {
  356. return SCM_PACK_POINTER
  357. (scm_inline_gc_alloc (&thread->freelists[freelist_idx],
  358. freelist_idx,
  359. SCM_INLINE_GC_KIND_NORMAL));
  360. }
  361. static SCM
  362. allocate_pointerless_words (scm_thread *thread, size_t n)
  363. {
  364. return SCM_PACK_POINTER (scm_inline_gc_malloc_pointerless_words (thread, n));
  365. }
  366. static SCM
  367. allocate_pointerless_words_with_freelist (scm_thread *thread, size_t freelist_idx)
  368. {
  369. return SCM_PACK_POINTER
  370. (scm_inline_gc_alloc (&thread->pointerless_freelists[freelist_idx],
  371. freelist_idx,
  372. SCM_INLINE_GC_KIND_POINTERLESS));
  373. }
  374. static SCM
  375. current_module (scm_thread *thread)
  376. {
  377. return scm_i_current_module (thread);
  378. }
  379. static void
  380. push_prompt (scm_thread *thread, uint8_t escape_only_p,
  381. SCM tag, const union scm_vm_stack_element *sp, uint32_t *vra,
  382. uint8_t *mra)
  383. {
  384. struct scm_vm *vp = &thread->vm;
  385. scm_t_dynstack_prompt_flags flags;
  386. flags = escape_only_p ? SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY : 0;
  387. scm_dynstack_push_prompt (&thread->dynstack, flags, tag,
  388. vp->stack_top - vp->fp, vp->stack_top - sp,
  389. vra, mra, thread->vm.registers);
  390. }
  391. static SCM
  392. scm_atan1 (SCM x)
  393. {
  394. return scm_atan (x, SCM_UNDEFINED);
  395. }
  396. void
  397. scm_bootstrap_intrinsics (void)
  398. {
  399. scm_vm_intrinsics.add = scm_sum;
  400. scm_vm_intrinsics.add_immediate = add_immediate;
  401. scm_vm_intrinsics.sub = scm_difference;
  402. scm_vm_intrinsics.sub_immediate = sub_immediate;
  403. scm_vm_intrinsics.mul = scm_product;
  404. scm_vm_intrinsics.div = scm_divide;
  405. scm_vm_intrinsics.quo = scm_quotient;
  406. scm_vm_intrinsics.rem = scm_remainder;
  407. scm_vm_intrinsics.mod = scm_modulo;
  408. scm_vm_intrinsics.logand = scm_logand;
  409. scm_vm_intrinsics.logior = scm_logior;
  410. scm_vm_intrinsics.logxor = scm_logxor;
  411. scm_vm_intrinsics.string_set_x = string_set_x;
  412. scm_vm_intrinsics.string_to_number = string_to_number;
  413. scm_vm_intrinsics.string_to_symbol = scm_string_to_symbol;
  414. scm_vm_intrinsics.symbol_to_keyword = scm_symbol_to_keyword;
  415. scm_vm_intrinsics.class_of = scm_class_of;
  416. scm_vm_intrinsics.scm_to_f64 = scm_to_double;
  417. #if INDIRECT_INT64_INTRINSICS
  418. scm_vm_intrinsics.scm_to_u64 = indirect_scm_to_uint64;
  419. scm_vm_intrinsics.scm_to_u64_truncate = indirect_scm_to_uint64_truncate;
  420. scm_vm_intrinsics.scm_to_s64 = indirect_scm_to_int64;
  421. scm_vm_intrinsics.u64_to_scm = indirect_scm_from_uint64;
  422. scm_vm_intrinsics.s64_to_scm = indirect_scm_from_int64;
  423. #else
  424. scm_vm_intrinsics.scm_to_u64 = scm_to_uint64;
  425. scm_vm_intrinsics.scm_to_u64_truncate = scm_to_uint64_truncate;
  426. scm_vm_intrinsics.scm_to_s64 = scm_to_int64;
  427. scm_vm_intrinsics.u64_to_scm = scm_from_uint64;
  428. scm_vm_intrinsics.s64_to_scm = scm_from_int64;
  429. #endif
  430. scm_vm_intrinsics.logsub = logsub;
  431. scm_vm_intrinsics.wind = wind;
  432. scm_vm_intrinsics.unwind = unwind;
  433. scm_vm_intrinsics.push_fluid = push_fluid;
  434. scm_vm_intrinsics.pop_fluid = pop_fluid;
  435. scm_vm_intrinsics.fluid_ref = fluid_ref;
  436. scm_vm_intrinsics.fluid_set_x = fluid_set_x;
  437. scm_vm_intrinsics.push_dynamic_state = push_dynamic_state;
  438. scm_vm_intrinsics.pop_dynamic_state = pop_dynamic_state;
  439. #if INDIRECT_INT64_INTRINSICS
  440. scm_vm_intrinsics.lsh = indirect_lsh;
  441. scm_vm_intrinsics.rsh = indirect_rsh;
  442. #else
  443. scm_vm_intrinsics.lsh = lsh;
  444. scm_vm_intrinsics.rsh = rsh;
  445. #endif
  446. scm_vm_intrinsics.lsh_immediate = lsh_immediate;
  447. scm_vm_intrinsics.rsh_immediate = rsh_immediate;
  448. scm_vm_intrinsics.heap_numbers_equal_p = scm_i_heap_numbers_equal_p;
  449. scm_vm_intrinsics.less_p = less_p;
  450. scm_vm_intrinsics.numerically_equal_p = numerically_equal_p;
  451. scm_vm_intrinsics.resolve_module = resolve_module;
  452. scm_vm_intrinsics.lookup = lookup;
  453. scm_vm_intrinsics.define_x = scm_module_ensure_local_variable;
  454. scm_vm_intrinsics.throw_ = throw_;
  455. scm_vm_intrinsics.throw_with_value = throw_with_value;
  456. scm_vm_intrinsics.throw_with_value_and_data = throw_with_value_and_data;
  457. scm_vm_intrinsics.error_wrong_num_args = error_wrong_num_args;
  458. scm_vm_intrinsics.error_no_values = error_no_values;
  459. scm_vm_intrinsics.error_not_enough_values = error_not_enough_values;
  460. scm_vm_intrinsics.error_wrong_number_of_values = error_wrong_number_of_values;
  461. scm_vm_intrinsics.allocate_words = allocate_words;
  462. scm_vm_intrinsics.current_module = current_module;
  463. scm_vm_intrinsics.push_prompt = push_prompt;
  464. scm_vm_intrinsics.allocate_words_with_freelist = allocate_words_with_freelist;
  465. scm_vm_intrinsics.abs = scm_abs;
  466. scm_vm_intrinsics.sqrt = scm_sqrt;
  467. scm_vm_intrinsics.fabs = fabs;
  468. scm_vm_intrinsics.fsqrt = sqrt;
  469. scm_vm_intrinsics.floor = scm_floor;
  470. scm_vm_intrinsics.ceiling = scm_ceiling;
  471. scm_vm_intrinsics.sin = scm_sin;
  472. scm_vm_intrinsics.cos = scm_cos;
  473. scm_vm_intrinsics.tan = scm_tan;
  474. scm_vm_intrinsics.asin = scm_asin;
  475. scm_vm_intrinsics.acos = scm_acos;
  476. scm_vm_intrinsics.atan = scm_atan1;
  477. scm_vm_intrinsics.atan2 = scm_atan;
  478. scm_vm_intrinsics.ffloor = floor;
  479. scm_vm_intrinsics.fceiling = ceil;
  480. scm_vm_intrinsics.fsin = sin;
  481. scm_vm_intrinsics.fcos = cos;
  482. scm_vm_intrinsics.ftan = tan;
  483. scm_vm_intrinsics.fasin = asin;
  484. scm_vm_intrinsics.facos = acos;
  485. scm_vm_intrinsics.fatan = atan;
  486. scm_vm_intrinsics.fatan2 = atan2;
  487. scm_vm_intrinsics.allocate_pointerless_words = allocate_pointerless_words;
  488. scm_vm_intrinsics.allocate_pointerless_words_with_freelist =
  489. allocate_pointerless_words_with_freelist;
  490. scm_vm_intrinsics.inexact = scm_exact_to_inexact;
  491. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  492. "scm_init_intrinsics",
  493. (scm_t_extension_init_func)scm_init_intrinsics,
  494. NULL);
  495. }
  496. void
  497. scm_init_intrinsics (void)
  498. {
  499. #ifndef SCM_MAGIC_SNARFER
  500. #include "intrinsics.x"
  501. #endif
  502. }