gsubr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* Copyright 1995-2001,2006,2008-2011,2013,2015,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. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <flexmember.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include "foreign.h"
  23. #include "frames.h"
  24. #include "instructions.h"
  25. #include "jit.h"
  26. #include "modules.h"
  27. #include "numbers.h"
  28. #include "private-options.h"
  29. #include "programs.h"
  30. #include "srfi-4.h"
  31. #include "symbols.h"
  32. #include "threads.h"
  33. #include "gsubr.h"
  34. /*
  35. * gsubr.c
  36. * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
  37. * and rest arguments.
  38. */
  39. /* In July 2018 there were 1140 subrs defined in stock Guile. */
  40. static const size_t expected_subr_count = 1500;
  41. static scm_i_pthread_mutex_t admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  42. static void **subrs = NULL;
  43. static uint32_t next_subr_idx = 0;
  44. static uint32_t subrs_array_size = 0;
  45. static uint32_t
  46. alloc_subr_idx (void *subr)
  47. {
  48. uint32_t idx;
  49. scm_i_pthread_mutex_lock (&admin_mutex);
  50. idx = next_subr_idx++;
  51. if (idx > 0xffffff) abort ();
  52. if (idx >= subrs_array_size)
  53. {
  54. void **new_subrs;
  55. if (subrs_array_size)
  56. subrs_array_size *= 2;
  57. else
  58. subrs_array_size = expected_subr_count;
  59. /* Leak this allocation, as code lives as long as the program
  60. does. In the likely case, we only make one malloc for the
  61. program; in the general case it's still O(n) in number of subrs
  62. because of the geometric factor. Use malloc instead of GC
  63. allocations because it's not traceable and not collectable. */
  64. new_subrs = malloc (subrs_array_size * sizeof (void*));
  65. memcpy (new_subrs, subrs, idx * sizeof (void*));
  66. subrs = new_subrs;
  67. }
  68. subrs[idx] = subr;
  69. scm_i_pthread_mutex_unlock (&admin_mutex);
  70. return idx;
  71. }
  72. static SCM *names = NULL;
  73. static uint32_t names_array_size = 0;
  74. static void
  75. record_subr_name (uint32_t idx, SCM name)
  76. {
  77. scm_i_pthread_mutex_lock (&admin_mutex);
  78. if (idx >= names_array_size)
  79. {
  80. SCM *new_names;
  81. uint32_t new_size;
  82. if (names_array_size)
  83. new_size = names_array_size * 2;
  84. else
  85. new_size = expected_subr_count;
  86. new_names = SCM_GC_MALLOC (new_size * sizeof (SCM));
  87. memcpy (new_names, names, names_array_size * sizeof (SCM));
  88. while (names_array_size < new_size)
  89. new_names[names_array_size++] = SCM_BOOL_F;
  90. names = new_names;
  91. names_array_size = new_size;
  92. }
  93. names[idx] = name;
  94. scm_i_pthread_mutex_unlock (&admin_mutex);
  95. }
  96. struct code_arena {
  97. struct code_arena *next;
  98. size_t size;
  99. size_t used;
  100. char data[FLEXIBLE_ARRAY_MEMBER];
  101. };
  102. static struct code_arena *code_arena = NULL;
  103. static size_t
  104. round_up_power_of_two (size_t n, size_t m)
  105. {
  106. return (n + (m-1)) & ~(m-1);
  107. }
  108. static struct code_arena *
  109. alloc_chunk (size_t size, struct code_arena *next)
  110. {
  111. /* Leak the allocation, as we currently don't allow code to be
  112. collected. */
  113. struct code_arena *ret = malloc (FLEXSIZEOF (struct code_arena, data, size));
  114. if (!ret) abort ();
  115. ret->next = next;
  116. ret->size = size;
  117. ret->used = 0;
  118. return ret;
  119. }
  120. static char *
  121. alloc (size_t byte_size)
  122. {
  123. char *ret;
  124. byte_size = round_up_power_of_two (byte_size, sizeof (void *));
  125. scm_i_pthread_mutex_lock (&admin_mutex);
  126. if (code_arena == NULL || code_arena->size - code_arena->used < byte_size)
  127. {
  128. size_t chunk_size;
  129. size_t avg_code_size = 6 * sizeof(uint32_t);
  130. avg_code_size += sizeof (struct scm_jit_function_data);
  131. chunk_size = expected_subr_count * avg_code_size;
  132. if (chunk_size < byte_size)
  133. chunk_size = byte_size;
  134. code_arena = alloc_chunk (chunk_size, code_arena);
  135. }
  136. ret = &code_arena->data[code_arena->used];
  137. code_arena->used += byte_size;
  138. scm_i_pthread_mutex_unlock (&admin_mutex);
  139. memset (ret, 0, byte_size);
  140. return ret;
  141. }
  142. uint32_t *
  143. scm_i_alloc_primitive_code_with_instrumentation (size_t uint32_count,
  144. uint32_t **write_ptr)
  145. {
  146. char *ptr;
  147. uint32_t *ret;
  148. struct scm_jit_function_data *data;
  149. size_t byte_size, padded_byte_size;
  150. /* Reserve space for instrument-entry. */
  151. byte_size = (2 + uint32_count) * sizeof (uint32_t);
  152. padded_byte_size = round_up_power_of_two (byte_size, sizeof (void *));
  153. /* Reserve space for jit data. */
  154. ptr = alloc (padded_byte_size + sizeof (struct scm_jit_function_data));
  155. ret = (uint32_t *) ptr;
  156. data = (struct scm_jit_function_data*) (ptr + padded_byte_size);
  157. ret[0] = SCM_PACK_OP_24 (instrument_entry, 0);
  158. ret[1] = padded_byte_size / 4;
  159. *write_ptr = ret + 2;
  160. data->mcode = NULL;
  161. data->counter = 0;
  162. data->start = -padded_byte_size;
  163. data->end = -(padded_byte_size - byte_size);
  164. return ret;
  165. }
  166. static int
  167. is_primitive_code (const void *ptr)
  168. {
  169. const char *cptr = ptr;
  170. int ret = 0;
  171. struct code_arena *arena;
  172. scm_i_pthread_mutex_lock (&admin_mutex);
  173. for (arena = code_arena; arena; arena = arena->next)
  174. if (&arena->data[0] <= cptr && cptr < &arena->data[arena->used])
  175. {
  176. ret = 1;
  177. break;
  178. }
  179. scm_i_pthread_mutex_unlock (&admin_mutex);
  180. return ret;
  181. }
  182. static uint32_t *
  183. alloc_subr_code (uint32_t subr_idx, uint32_t code[], size_t code_size)
  184. {
  185. uint32_t post[3] = { SCM_PACK_OP_24 (subr_call, subr_idx),
  186. SCM_PACK_OP_24 (handle_interrupts, 0),
  187. SCM_PACK_OP_24 (return_values, 0) };
  188. uint32_t *ret, *write;
  189. ret = scm_i_alloc_primitive_code_with_instrumentation (code_size + 3, &write);
  190. memcpy (write, code, code_size * sizeof (uint32_t));
  191. write += code_size;
  192. memcpy (write, post, 3 * sizeof (uint32_t));
  193. return ret;
  194. }
  195. enum arity_kind {
  196. NULLARY = 0,
  197. REQ = 1,
  198. OPT = 2,
  199. REST = 4,
  200. REQ_OPT = REQ + OPT,
  201. REQ_REST = REQ + REST,
  202. OPT_REST = OPT + REST,
  203. REQ_OPT_REST = REQ + OPT + REST
  204. };
  205. static uint32_t*
  206. get_subr_stub_code (uint32_t subr_idx,
  207. unsigned int nreq, unsigned int nopt, unsigned int rest)
  208. {
  209. enum arity_kind kind = NULLARY;
  210. if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 10))
  211. scm_out_of_range ("make-subr", scm_from_uint (nreq + nopt + rest));
  212. if (nreq) kind += REQ;
  213. if (nopt) kind += OPT;
  214. if (rest) kind += REST;
  215. switch (kind)
  216. {
  217. case NULLARY:
  218. case REQ:
  219. {
  220. uint32_t code[1] = { SCM_PACK_OP_24 (assert_nargs_ee, nreq + 1) };
  221. return alloc_subr_code (subr_idx, code, 1);
  222. }
  223. case OPT:
  224. {
  225. uint32_t code[2] = { SCM_PACK_OP_24 (assert_nargs_le, nopt + 1),
  226. SCM_PACK_OP_24 (bind_optionals, nopt + 1) };
  227. return alloc_subr_code (subr_idx, code, 2);
  228. }
  229. case REST:
  230. {
  231. uint32_t code[1] = { SCM_PACK_OP_24 (bind_rest, 1) };
  232. return alloc_subr_code (subr_idx, code, 1);
  233. }
  234. case REQ_OPT:
  235. {
  236. uint32_t code[3] = { SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1),
  237. SCM_PACK_OP_24 (assert_nargs_le, nreq + nopt + 1),
  238. SCM_PACK_OP_24 (bind_optionals, nreq + nopt + 1) };
  239. return alloc_subr_code (subr_idx, code, 3);
  240. }
  241. case REQ_REST:
  242. {
  243. uint32_t code[2] = { SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1),
  244. SCM_PACK_OP_24 (bind_rest, nreq + 1) };
  245. return alloc_subr_code (subr_idx, code, 2);
  246. }
  247. case OPT_REST:
  248. {
  249. uint32_t code[2] = { SCM_PACK_OP_24 (bind_optionals, nopt + 1),
  250. SCM_PACK_OP_24 (bind_rest, nopt + 1) };
  251. return alloc_subr_code (subr_idx, code, 2);
  252. }
  253. case REQ_OPT_REST:
  254. {
  255. uint32_t code[3] = { SCM_PACK_OP_24 (assert_nargs_ge, nreq + 1),
  256. SCM_PACK_OP_24 (bind_optionals, nreq + nopt + 1),
  257. SCM_PACK_OP_24 (bind_rest, nreq + nopt + 1) };
  258. return alloc_subr_code (subr_idx, code, 3);
  259. }
  260. default:
  261. abort ();
  262. }
  263. }
  264. static SCM
  265. create_subr (int define, const char *name,
  266. unsigned int nreq, unsigned int nopt, unsigned int rest,
  267. void *fcn, SCM *generic_loc)
  268. {
  269. SCM ret, sname;
  270. uint32_t idx;
  271. scm_t_bits flags;
  272. scm_t_bits nfree = generic_loc ? 1 : 0;
  273. idx = alloc_subr_idx (fcn);
  274. sname = scm_from_utf8_symbol (name);
  275. flags = SCM_F_PROGRAM_IS_PRIMITIVE;
  276. flags |= generic_loc ? SCM_F_PROGRAM_IS_PRIMITIVE_GENERIC : 0;
  277. ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
  278. SCM_SET_CELL_WORD_1 (ret, get_subr_stub_code (idx, nreq, nopt, rest));
  279. record_subr_name (idx, sname);
  280. if (generic_loc)
  281. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0,
  282. scm_from_pointer (generic_loc, NULL));
  283. if (define)
  284. scm_define (sname, ret);
  285. return ret;
  286. }
  287. int
  288. scm_i_primitive_code_p (const uint32_t *code)
  289. {
  290. return is_primitive_code (code);
  291. }
  292. static uintptr_t
  293. primitive_call_ip (const uint32_t *code)
  294. {
  295. int direction = 0;
  296. while (1)
  297. {
  298. switch (code[0] & 0xff)
  299. {
  300. case scm_op_instrument_entry:
  301. if (direction < 0) abort ();
  302. direction = 1;
  303. code += 2;
  304. break;
  305. case scm_op_assert_nargs_ee:
  306. case scm_op_assert_nargs_le:
  307. case scm_op_assert_nargs_ge:
  308. case scm_op_bind_optionals:
  309. case scm_op_bind_rest:
  310. case scm_op_alloc_frame:
  311. if (direction < 0) abort ();
  312. direction = 1;
  313. code += 1;
  314. break;
  315. case scm_op_subr_call:
  316. case scm_op_foreign_call:
  317. return (uintptr_t) code;
  318. case scm_op_return_values:
  319. case scm_op_handle_interrupts:
  320. /* Going back isn't possible for instruction streams where we
  321. don't know the length of the preceding instruction, but for
  322. the code we emit, these particular opcodes are only ever
  323. preceded by 4-byte instructions. */
  324. if (direction > 0) abort ();
  325. direction = -1;
  326. code -= 1;
  327. break;
  328. default:
  329. return 0;
  330. }
  331. }
  332. }
  333. static const uint32_t NOT_A_SUBR_CALL = 0xffffffff;
  334. static uint32_t
  335. primitive_subr_idx (const uint32_t *code)
  336. {
  337. uint32_t word;
  338. uintptr_t call_ip = primitive_call_ip (code);
  339. if (call_ip == 0)
  340. return NOT_A_SUBR_CALL;
  341. word = ((uint32_t *) call_ip)[0];
  342. if ((word & 0xff) == scm_op_subr_call)
  343. {
  344. uint32_t idx = word >> 8;
  345. if (idx >= next_subr_idx) abort();
  346. return idx;
  347. }
  348. else
  349. return NOT_A_SUBR_CALL;
  350. }
  351. uintptr_t
  352. scm_i_primitive_call_ip (SCM subr)
  353. {
  354. return primitive_call_ip (SCM_PROGRAM_CODE (subr));
  355. }
  356. SCM
  357. scm_i_primitive_name (const uint32_t *code)
  358. {
  359. uint32_t idx = primitive_subr_idx (code);
  360. if (idx == NOT_A_SUBR_CALL)
  361. return SCM_BOOL_F;
  362. return names[idx];
  363. }
  364. scm_t_subr
  365. scm_subr_function_by_index (uint32_t idx)
  366. {
  367. if (idx == NOT_A_SUBR_CALL)
  368. abort ();
  369. return subrs[idx];
  370. }
  371. scm_t_subr
  372. scm_subr_function (SCM subr)
  373. {
  374. uint32_t idx = primitive_subr_idx (SCM_PROGRAM_CODE (subr));
  375. return scm_subr_function_by_index (idx);
  376. }
  377. SCM
  378. scm_subr_name (SCM subr)
  379. {
  380. return scm_i_primitive_name (SCM_PROGRAM_CODE (subr));
  381. }
  382. SCM
  383. scm_apply_subr (union scm_vm_stack_element *sp, uint32_t idx, ptrdiff_t nslots)
  384. {
  385. SCM (*subr)() = subrs[idx];
  386. #define ARG(i) (sp[i].as_scm)
  387. switch (nslots - 1)
  388. {
  389. case 0:
  390. return subr ();
  391. case 1:
  392. return subr (ARG (0));
  393. case 2:
  394. return subr (ARG (1), ARG (0));
  395. case 3:
  396. return subr (ARG (2), ARG (1), ARG (0));
  397. case 4:
  398. return subr (ARG (3), ARG (2), ARG (1), ARG (0));
  399. case 5:
  400. return subr (ARG (4), ARG (3), ARG (2), ARG (1), ARG (0));
  401. case 6:
  402. return subr (ARG (5), ARG (4), ARG (3), ARG (2), ARG (1),
  403. ARG (0));
  404. case 7:
  405. return subr (ARG (6), ARG (5), ARG (4), ARG (3), ARG (2),
  406. ARG (1), ARG (0));
  407. case 8:
  408. return subr (ARG (7), ARG (6), ARG (5), ARG (4), ARG (3),
  409. ARG (2), ARG (1), ARG (0));
  410. case 9:
  411. return subr (ARG (8), ARG (7), ARG (6), ARG (5), ARG (4),
  412. ARG (3), ARG (2), ARG (1), ARG (0));
  413. case 10:
  414. return subr (ARG (9), ARG (8), ARG (7), ARG (6), ARG (5),
  415. ARG (4), ARG (3), ARG (2), ARG (1), ARG (0));
  416. default:
  417. abort ();
  418. }
  419. #undef ARG
  420. }
  421. SCM
  422. scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
  423. {
  424. return create_subr (0, name, req, opt, rst, fcn, NULL);
  425. }
  426. SCM
  427. scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
  428. {
  429. return create_subr (1, name, req, opt, rst, fcn, NULL);
  430. }
  431. SCM
  432. scm_c_make_gsubr_with_generic (const char *name,
  433. int req,
  434. int opt,
  435. int rst,
  436. SCM (*fcn)(),
  437. SCM *gf)
  438. {
  439. return create_subr (0, name, req, opt, rst, fcn, gf);
  440. }
  441. SCM
  442. scm_c_define_gsubr_with_generic (const char *name,
  443. int req,
  444. int opt,
  445. int rst,
  446. SCM (*fcn)(),
  447. SCM *gf)
  448. {
  449. return create_subr (1, name, req, opt, rst, fcn, gf);
  450. }
  451. void
  452. scm_init_gsubr()
  453. {
  454. #include "gsubr.x"
  455. }