intrinsics.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 "alist.h"
  19. #include "atomics-internal.h"
  20. #include "boolean.h"
  21. #include "cache-internal.h"
  22. #include "extensions.h"
  23. #include "fluids.h"
  24. #include "frames.h"
  25. #include "gc-inline.h"
  26. #include "goops.h"
  27. #include "gsubr.h"
  28. #include "keywords.h"
  29. #include "modules.h"
  30. #include "numbers.h"
  31. #include "symbols.h"
  32. #include "threads.h"
  33. #include "version.h"
  34. #include "intrinsics.h"
  35. struct scm_vm_intrinsics scm_vm_intrinsics;
  36. SCM_DEFINE (scm_intrinsic_list, "intrinsic-list", 0, 0, 0,
  37. (void),
  38. "")
  39. #define FUNC_NAME s_scm_intrinsic_list
  40. {
  41. SCM list = SCM_EOL;
  42. #define ADD_INTRINSIC(type, id, name, ID) \
  43. if (name) \
  44. list = scm_acons (scm_from_latin1_symbol (name), \
  45. scm_from_int (SCM_VM_INTRINSIC_##ID), \
  46. list);
  47. SCM_FOR_ALL_VM_INTRINSICS (ADD_INTRINSIC);
  48. #undef ADD_INTRINSIC
  49. return list;
  50. }
  51. #undef FUNC_NAME
  52. static SCM
  53. add_immediate (SCM a, uint8_t b)
  54. {
  55. if (SCM_LIKELY (SCM_I_INUMP (a)))
  56. {
  57. scm_t_signed_bits sum = SCM_I_INUM (a) + b;
  58. if (SCM_LIKELY (SCM_POSFIXABLE (sum)))
  59. return SCM_I_MAKINUM (sum);
  60. }
  61. return scm_sum (a, scm_from_uint8 (b));
  62. }
  63. static SCM
  64. sub_immediate (SCM a, uint8_t b)
  65. {
  66. if (SCM_LIKELY (SCM_I_INUMP (a)))
  67. {
  68. scm_t_signed_bits diff = SCM_I_INUM (a) - b;
  69. if (SCM_LIKELY (SCM_NEGFIXABLE (diff)))
  70. return SCM_I_MAKINUM (diff);
  71. }
  72. return scm_difference (a, scm_from_uint8 (b));
  73. }
  74. static void
  75. string_set_x (SCM str, size_t idx, uint32_t ch)
  76. {
  77. str = scm_i_string_start_writing (str);
  78. scm_i_string_set_x (str, idx, ch);
  79. scm_i_string_stop_writing ();
  80. }
  81. static SCM
  82. string_to_number (SCM str)
  83. {
  84. return scm_string_to_number (str, SCM_UNDEFINED /* radix = 10 */);
  85. }
  86. static uint64_t
  87. scm_to_uint64_truncate (SCM x)
  88. {
  89. if (SCM_LIKELY (SCM_I_INUMP (x)))
  90. return (uint64_t) SCM_I_INUM (x);
  91. else
  92. return scm_to_uint64 (scm_logand (x, scm_from_uint64 ((uint64_t) -1)));
  93. }
  94. #if INDIRECT_INT64_INTRINSICS
  95. static void
  96. indirect_scm_to_int64 (int64_t *dst, SCM x)
  97. {
  98. *dst = scm_to_int64 (x);
  99. }
  100. static void
  101. indirect_scm_to_uint64 (uint64_t *dst, SCM x)
  102. {
  103. *dst = scm_to_uint64 (x);
  104. }
  105. static void
  106. indirect_scm_to_uint64_truncate (uint64_t *dst, SCM x)
  107. {
  108. *dst = scm_to_uint64_truncate (x);
  109. }
  110. static SCM
  111. indirect_scm_from_int64 (int64_t *src)
  112. {
  113. return scm_from_int64 (*src);
  114. }
  115. static SCM
  116. indirect_scm_from_uint64 (uint64_t *src)
  117. {
  118. return scm_from_uint64 (*src);
  119. }
  120. #endif
  121. static SCM
  122. logsub (SCM x, SCM y)
  123. {
  124. if (SCM_I_INUMP (x) && SCM_I_INUMP (y))
  125. {
  126. scm_t_signed_bits a, b;
  127. a = SCM_I_INUM (x);
  128. b = SCM_I_INUM (y);
  129. return SCM_I_MAKINUM (a & ~b);
  130. }
  131. return scm_logand (x, scm_lognot (y));
  132. }
  133. static void
  134. wind (scm_thread *thread, SCM winder, SCM unwinder)
  135. {
  136. scm_dynstack_push_dynwind (&thread->dynstack, winder, unwinder);
  137. }
  138. static void
  139. unwind (scm_thread *thread)
  140. {
  141. scm_dynstack_pop (&thread->dynstack);
  142. }
  143. static void
  144. push_fluid (scm_thread *thread, SCM fluid, SCM value)
  145. {
  146. scm_dynstack_push_fluid (&thread->dynstack, fluid, value,
  147. thread->dynamic_state);
  148. }
  149. static void
  150. pop_fluid (scm_thread *thread)
  151. {
  152. scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
  153. }
  154. static SCM
  155. fluid_ref (scm_thread *thread, SCM fluid)
  156. {
  157. struct scm_cache_entry *entry;
  158. /* If we find FLUID in the cache, then it is indeed a fluid. */
  159. entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
  160. if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)
  161. && !SCM_UNBNDP (SCM_PACK (entry->value))))
  162. return SCM_PACK (entry->value);
  163. return scm_fluid_ref (fluid);
  164. }
  165. static void
  166. fluid_set_x (scm_thread *thread, SCM fluid, SCM value)
  167. {
  168. struct scm_cache_entry *entry;
  169. /* If we find FLUID in the cache, then it is indeed a fluid. */
  170. entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
  171. if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)))
  172. entry->value = SCM_UNPACK (value);
  173. else
  174. scm_fluid_set_x (fluid, value);
  175. }
  176. static void
  177. push_dynamic_state (scm_thread *thread, SCM state)
  178. {
  179. scm_dynstack_push_dynamic_state (&thread->dynstack, state,
  180. thread->dynamic_state);
  181. }
  182. static void
  183. pop_dynamic_state (scm_thread *thread)
  184. {
  185. scm_dynstack_unwind_dynamic_state (&thread->dynstack,
  186. thread->dynamic_state);
  187. }
  188. static SCM
  189. lsh (SCM a, uint64_t b)
  190. {
  191. if (SCM_LIKELY (SCM_I_INUMP (a))
  192. && b < (uint64_t) (SCM_I_FIXNUM_BIT - 1)
  193. && ((scm_t_bits)
  194. (SCM_SRS (SCM_I_INUM (a), (SCM_I_FIXNUM_BIT-1 - b)) + 1)
  195. <= 1))
  196. {
  197. scm_t_signed_bits nn = SCM_I_INUM (a);
  198. return SCM_I_MAKINUM (nn < 0 ? -(-nn << b) : (nn << b));
  199. }
  200. else
  201. return scm_ash (a, scm_from_uint64 (b));
  202. }
  203. static SCM
  204. rsh (SCM a, uint64_t b)
  205. {
  206. if (SCM_LIKELY (SCM_I_INUMP (a)))
  207. {
  208. if (b > (uint64_t) (SCM_I_FIXNUM_BIT - 1))
  209. b = SCM_I_FIXNUM_BIT - 1;
  210. return SCM_I_MAKINUM (SCM_SRS (SCM_I_INUM (a), b));
  211. }
  212. else
  213. return scm_ash (a, scm_difference (SCM_INUM0, scm_from_uint64 (b)));
  214. }
  215. #if INDIRECT_INT64_INTRINSICS
  216. static SCM
  217. indirect_lsh (SCM a, uint64_t *b)
  218. {
  219. return lsh (a, *b);
  220. }
  221. static SCM
  222. indirect_rsh (SCM a, uint64_t *b)
  223. {
  224. return rsh (a, *b);
  225. }
  226. #endif
  227. static SCM
  228. lsh_immediate (SCM a, uint8_t b)
  229. {
  230. return lsh (a, b);
  231. }
  232. static SCM
  233. rsh_immediate (SCM a, uint8_t b)
  234. {
  235. return rsh (a, b);
  236. }
  237. static enum scm_compare
  238. less_p (SCM a, SCM b)
  239. {
  240. if (SCM_LIKELY (SCM_I_INUMP (a) && SCM_I_INUMP (b)))
  241. {
  242. scm_t_signed_bits a_bits = SCM_UNPACK (a);
  243. scm_t_signed_bits b_bits = SCM_UNPACK (b);
  244. return a_bits < b_bits ? SCM_F_COMPARE_LESS_THAN : SCM_F_COMPARE_NONE;
  245. }
  246. if (scm_is_true (scm_nan_p (a)) || scm_is_true (scm_nan_p (b)))
  247. return SCM_F_COMPARE_INVALID;
  248. else if (scm_is_true (scm_less_p (a, b)))
  249. return SCM_F_COMPARE_LESS_THAN;
  250. else
  251. return SCM_F_COMPARE_NONE;
  252. }
  253. static int
  254. numerically_equal_p (SCM a, SCM b)
  255. {
  256. if (SCM_LIKELY (SCM_I_INUMP (a) && SCM_I_INUMP (b)))
  257. return scm_is_eq (a, b);
  258. return scm_is_true (scm_num_eq_p (a, b));
  259. }
  260. static SCM
  261. resolve_module (SCM name, uint8_t public_p)
  262. {
  263. SCM mod;
  264. if (!scm_module_system_booted_p)
  265. return SCM_BOOL_F;
  266. mod = scm_maybe_resolve_module (name);
  267. if (scm_is_false (mod))
  268. scm_misc_error (NULL, "Module named ~s does not exist",
  269. scm_list_1 (name));
  270. if (public_p)
  271. {
  272. mod = scm_module_public_interface (mod);
  273. if (scm_is_false (mod))
  274. scm_misc_error (NULL, "Module named ~s has no public interface",
  275. scm_list_1 (name));
  276. }
  277. return mod;
  278. }
  279. static SCM
  280. lookup (SCM module, SCM name)
  281. {
  282. /* If MODULE was captured before modules were booted, use the root
  283. module. Not so nice, but hey... */
  284. if (scm_is_false (module))
  285. module = scm_the_root_module ();
  286. return scm_module_variable (module, name);
  287. }
  288. static void throw_ (SCM key, SCM args) SCM_NORETURN;
  289. static void throw_with_value (SCM val, SCM key_subr_and_message) SCM_NORETURN;
  290. static void throw_with_value_and_data (SCM val, SCM key_subr_and_message) SCM_NORETURN;
  291. static void
  292. throw_ (SCM key, SCM args)
  293. {
  294. scm_throw (key, args);
  295. abort(); /* not reached */
  296. }
  297. static void
  298. throw_with_value (SCM val, SCM key_subr_and_message)
  299. {
  300. SCM key, subr, message, args, data;
  301. key = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 0);
  302. subr = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 1);
  303. message = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 2);
  304. args = scm_list_1 (val);
  305. data = SCM_BOOL_F;
  306. throw_ (key, scm_list_4 (subr, message, args, data));
  307. }
  308. static void
  309. throw_with_value_and_data (SCM val, SCM key_subr_and_message)
  310. {
  311. SCM key, subr, message, args, data;
  312. key = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 0);
  313. subr = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 1);
  314. message = SCM_SIMPLE_VECTOR_REF (key_subr_and_message, 2);
  315. args = scm_list_1 (val);
  316. data = args;
  317. throw_ (key, scm_list_4 (subr, message, args, data));
  318. }
  319. static void error_wrong_num_args (scm_thread *) SCM_NORETURN;
  320. static void error_no_values (void) SCM_NORETURN;
  321. static void error_not_enough_values (void) SCM_NORETURN;
  322. static void error_wrong_number_of_values (uint32_t expected) SCM_NORETURN;
  323. static void
  324. error_wrong_num_args (scm_thread *thread)
  325. {
  326. SCM callee = SCM_FRAME_LOCAL (thread->vm.fp, 0);
  327. scm_wrong_num_args (callee);
  328. }
  329. static void
  330. error_no_values (void)
  331. {
  332. scm_misc_error (NULL, "Zero values returned to single-valued continuation",
  333. SCM_EOL);
  334. }
  335. static void
  336. error_not_enough_values (void)
  337. {
  338. scm_misc_error (NULL, "Too few values returned to continuation", SCM_EOL);
  339. }
  340. static void
  341. error_wrong_number_of_values (uint32_t expected)
  342. {
  343. scm_misc_error (NULL,
  344. "Wrong number of values returned to continuation (expected ~a)",
  345. scm_list_1 (scm_from_uint32 (expected)));
  346. }
  347. static SCM
  348. allocate_words (scm_thread *thread, size_t n)
  349. {
  350. return SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, n));
  351. }
  352. static SCM
  353. current_module (scm_thread *thread)
  354. {
  355. return scm_i_current_module (thread);
  356. }
  357. static void
  358. push_prompt (scm_thread *thread, uint8_t escape_only_p,
  359. SCM tag, const union scm_vm_stack_element *sp, uint32_t *vra,
  360. uint8_t *mra)
  361. {
  362. struct scm_vm *vp = &thread->vm;
  363. scm_t_dynstack_prompt_flags flags;
  364. flags = escape_only_p ? SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY : 0;
  365. scm_dynstack_push_prompt (&thread->dynstack, flags, tag,
  366. vp->stack_top - vp->fp, vp->stack_top - sp,
  367. vra, mra, thread->vm.registers);
  368. }
  369. void
  370. scm_bootstrap_intrinsics (void)
  371. {
  372. scm_vm_intrinsics.add = scm_sum;
  373. scm_vm_intrinsics.add_immediate = add_immediate;
  374. scm_vm_intrinsics.sub = scm_difference;
  375. scm_vm_intrinsics.sub_immediate = sub_immediate;
  376. scm_vm_intrinsics.mul = scm_product;
  377. scm_vm_intrinsics.div = scm_divide;
  378. scm_vm_intrinsics.quo = scm_quotient;
  379. scm_vm_intrinsics.rem = scm_remainder;
  380. scm_vm_intrinsics.mod = scm_modulo;
  381. scm_vm_intrinsics.logand = scm_logand;
  382. scm_vm_intrinsics.logior = scm_logior;
  383. scm_vm_intrinsics.logxor = scm_logxor;
  384. scm_vm_intrinsics.string_set_x = string_set_x;
  385. scm_vm_intrinsics.string_to_number = string_to_number;
  386. scm_vm_intrinsics.string_to_symbol = scm_string_to_symbol;
  387. scm_vm_intrinsics.symbol_to_keyword = scm_symbol_to_keyword;
  388. scm_vm_intrinsics.class_of = scm_class_of;
  389. scm_vm_intrinsics.scm_to_f64 = scm_to_double;
  390. scm_vm_intrinsics.f64_to_scm = scm_from_double;
  391. #if INDIRECT_INT64_INTRINSICS
  392. scm_vm_intrinsics.scm_to_u64 = indirect_scm_to_uint64;
  393. scm_vm_intrinsics.scm_to_u64_truncate = indirect_scm_to_uint64_truncate;
  394. scm_vm_intrinsics.scm_to_s64 = indirect_scm_to_int64;
  395. scm_vm_intrinsics.u64_to_scm = indirect_scm_from_uint64;
  396. scm_vm_intrinsics.s64_to_scm = indirect_scm_from_int64;
  397. #else
  398. scm_vm_intrinsics.scm_to_u64 = scm_to_uint64;
  399. scm_vm_intrinsics.scm_to_u64_truncate = scm_to_uint64_truncate;
  400. scm_vm_intrinsics.scm_to_s64 = scm_to_int64;
  401. scm_vm_intrinsics.u64_to_scm = scm_from_uint64;
  402. scm_vm_intrinsics.s64_to_scm = scm_from_int64;
  403. #endif
  404. scm_vm_intrinsics.logsub = logsub;
  405. scm_vm_intrinsics.wind = wind;
  406. scm_vm_intrinsics.unwind = unwind;
  407. scm_vm_intrinsics.push_fluid = push_fluid;
  408. scm_vm_intrinsics.pop_fluid = pop_fluid;
  409. scm_vm_intrinsics.fluid_ref = fluid_ref;
  410. scm_vm_intrinsics.fluid_set_x = fluid_set_x;
  411. scm_vm_intrinsics.push_dynamic_state = push_dynamic_state;
  412. scm_vm_intrinsics.pop_dynamic_state = pop_dynamic_state;
  413. #if INDIRECT_INT64_INTRINSICS
  414. scm_vm_intrinsics.lsh = indirect_lsh;
  415. scm_vm_intrinsics.rsh = indirect_rsh;
  416. #else
  417. scm_vm_intrinsics.lsh = lsh;
  418. scm_vm_intrinsics.rsh = rsh;
  419. #endif
  420. scm_vm_intrinsics.lsh_immediate = lsh_immediate;
  421. scm_vm_intrinsics.rsh_immediate = rsh_immediate;
  422. scm_vm_intrinsics.heap_numbers_equal_p = scm_i_heap_numbers_equal_p;
  423. scm_vm_intrinsics.less_p = less_p;
  424. scm_vm_intrinsics.numerically_equal_p = numerically_equal_p;
  425. scm_vm_intrinsics.resolve_module = resolve_module;
  426. scm_vm_intrinsics.lookup = lookup;
  427. scm_vm_intrinsics.define_x = scm_module_ensure_local_variable;
  428. scm_vm_intrinsics.throw_ = throw_;
  429. scm_vm_intrinsics.throw_with_value = throw_with_value;
  430. scm_vm_intrinsics.throw_with_value_and_data = throw_with_value_and_data;
  431. scm_vm_intrinsics.error_wrong_num_args = error_wrong_num_args;
  432. scm_vm_intrinsics.error_no_values = error_no_values;
  433. scm_vm_intrinsics.error_not_enough_values = error_not_enough_values;
  434. scm_vm_intrinsics.error_wrong_number_of_values = error_wrong_number_of_values;
  435. scm_vm_intrinsics.allocate_words = allocate_words;
  436. scm_vm_intrinsics.current_module = current_module;
  437. scm_vm_intrinsics.push_prompt = push_prompt;
  438. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  439. "scm_init_intrinsics",
  440. (scm_t_extension_init_func)scm_init_intrinsics,
  441. NULL);
  442. }
  443. void
  444. scm_init_intrinsics (void)
  445. {
  446. #ifndef SCM_MAGIC_SNARFER
  447. #include "intrinsics.x"
  448. #endif
  449. }