memoize.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /* Copyright 1995-2016,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 <string.h>
  19. #include <stdio.h>
  20. #include "alist.h"
  21. #include "boolean.h"
  22. #include "continuations.h"
  23. #include "dynstack.h"
  24. #include "eq.h"
  25. #include "expand.h"
  26. #include "gsubr.h"
  27. #include "list.h"
  28. #include "macros.h"
  29. #include "modules.h"
  30. #include "numbers.h"
  31. #include "pairs.h"
  32. #include "ports.h"
  33. #include "print.h"
  34. #include "srcprop.h"
  35. #include "strings.h"
  36. #include "symbols.h"
  37. #include "threads.h"
  38. #include "throw.h"
  39. #include "variable.h"
  40. #include "vectors.h"
  41. #include "memoize.h"
  42. #define CAR(x) SCM_CAR(x)
  43. #define CDR(x) SCM_CDR(x)
  44. #define CAAR(x) SCM_CAAR(x)
  45. #define CADR(x) SCM_CADR(x)
  46. #define CDAR(x) SCM_CDAR(x)
  47. #define CDDR(x) SCM_CDDR(x)
  48. #define CADDR(x) SCM_CADDR(x)
  49. #define CDDDR(x) SCM_CDDDR(x)
  50. #define CADDDR(x) SCM_CADDDR(x)
  51. #define VECTOR_REF(v, i) (SCM_SIMPLE_VECTOR_REF (v, i))
  52. #define VECTOR_SET(v, i, x) (SCM_SIMPLE_VECTOR_SET (v, i, x))
  53. #define VECTOR_LENGTH(v) (SCM_SIMPLE_VECTOR_LENGTH (v))
  54. SCM_SYMBOL (sym_case_lambda_star, "case-lambda*");
  55. /* Primitives not exposed to general Scheme. */
  56. static SCM wind;
  57. static SCM unwind;
  58. static SCM push_fluid;
  59. static SCM pop_fluid;
  60. static SCM push_dynamic_state;
  61. static SCM pop_dynamic_state;
  62. static SCM
  63. do_wind (SCM in, SCM out)
  64. {
  65. scm_dynstack_push_dynwind (&SCM_I_CURRENT_THREAD->dynstack, in, out);
  66. return SCM_UNSPECIFIED;
  67. }
  68. static SCM
  69. do_unwind (void)
  70. {
  71. scm_dynstack_pop (&SCM_I_CURRENT_THREAD->dynstack);
  72. return SCM_UNSPECIFIED;
  73. }
  74. static SCM
  75. do_push_fluid (SCM fluid, SCM val)
  76. {
  77. scm_thread *thread = SCM_I_CURRENT_THREAD;
  78. scm_dynstack_push_fluid (&thread->dynstack, fluid, val,
  79. thread->dynamic_state);
  80. return SCM_UNSPECIFIED;
  81. }
  82. static SCM
  83. do_pop_fluid (void)
  84. {
  85. scm_thread *thread = SCM_I_CURRENT_THREAD;
  86. scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
  87. return SCM_UNSPECIFIED;
  88. }
  89. static SCM
  90. do_push_dynamic_state (SCM state)
  91. {
  92. scm_thread *thread = SCM_I_CURRENT_THREAD;
  93. scm_dynstack_push_dynamic_state (&thread->dynstack, state,
  94. thread->dynamic_state);
  95. return SCM_UNSPECIFIED;
  96. }
  97. static SCM
  98. do_pop_dynamic_state (void)
  99. {
  100. scm_thread *thread = SCM_I_CURRENT_THREAD;
  101. scm_dynstack_unwind_dynamic_state (&thread->dynstack,
  102. thread->dynamic_state);
  103. return SCM_UNSPECIFIED;
  104. }
  105. /* {Evaluator memoized expressions}
  106. */
  107. #define MAKMEMO(n, src, args) \
  108. (scm_cons (SCM_I_MAKINUM (n), scm_cons (src, args)))
  109. #define MAKMEMO_SEQ(src, head, tail) \
  110. MAKMEMO (SCM_M_SEQ, src, scm_cons (head, tail))
  111. #define MAKMEMO_IF(src, test, then, else_) \
  112. MAKMEMO (SCM_M_IF, src, scm_cons (test, scm_cons (then, else_)))
  113. #define FIXED_ARITY(nreq) \
  114. scm_list_1 (SCM_I_MAKINUM (nreq))
  115. #define REST_ARITY(nreq, rest) \
  116. scm_list_2 (SCM_I_MAKINUM (nreq), rest)
  117. #define FULL_ARITY(nreq, rest, nopt, kw, ninits, unbound, alt) \
  118. scm_list_n (SCM_I_MAKINUM (nreq), rest, SCM_I_MAKINUM (nopt), kw, \
  119. SCM_I_MAKINUM (ninits), unbound, alt, SCM_UNDEFINED)
  120. #define MAKMEMO_LAMBDA(src, body, arity, meta) \
  121. MAKMEMO (SCM_M_LAMBDA, src, \
  122. scm_cons (body, scm_cons (meta, arity)))
  123. #define MAKMEMO_CAPTURE_ENV(src, vars, body) \
  124. MAKMEMO (SCM_M_CAPTURE_ENV, src, scm_cons (vars, body))
  125. #define MAKMEMO_LET(src, inits, body) \
  126. MAKMEMO (SCM_M_LET, src, scm_cons (inits, body))
  127. #define MAKMEMO_QUOTE(src, exp) \
  128. MAKMEMO (SCM_M_QUOTE, src, exp)
  129. #define MAKMEMO_CAPTURE_MODULE(src, exp) \
  130. MAKMEMO (SCM_M_CAPTURE_MODULE, src, exp)
  131. #define MAKMEMO_APPLY(src, proc, args) \
  132. MAKMEMO (SCM_M_APPLY, src, scm_list_2 (proc, args))
  133. #define MAKMEMO_CONT(src, proc) \
  134. MAKMEMO (SCM_M_CONT, src, proc)
  135. #define MAKMEMO_CALL_WITH_VALUES(src, prod, cons) \
  136. MAKMEMO (SCM_M_CALL_WITH_VALUES, src, scm_cons (prod, cons))
  137. #define MAKMEMO_CALL(src, proc, args) \
  138. MAKMEMO (SCM_M_CALL, src, scm_cons (proc, args))
  139. #define MAKMEMO_LEX_REF(src, pos) \
  140. MAKMEMO (SCM_M_LEXICAL_REF, src, pos)
  141. #define MAKMEMO_LEX_SET(src, pos, val) \
  142. MAKMEMO (SCM_M_LEXICAL_SET, src, scm_cons (pos, val))
  143. #define MAKMEMO_BOX_REF(src, box) \
  144. MAKMEMO (SCM_M_BOX_REF, src, box)
  145. #define MAKMEMO_BOX_SET(src, box, val) \
  146. MAKMEMO (SCM_M_BOX_SET, src, scm_cons (box, val))
  147. #define MAKMEMO_TOP_BOX(src, mode, var) \
  148. MAKMEMO (SCM_M_RESOLVE, src, scm_cons (SCM_I_MAKINUM (mode), var))
  149. #define MAKMEMO_MOD_BOX(src, mode, mod, var, public) \
  150. MAKMEMO (SCM_M_RESOLVE, src, \
  151. scm_cons (SCM_I_MAKINUM (mode), \
  152. scm_cons (mod, scm_cons (var, public))))
  153. #define MAKMEMO_CALL_WITH_PROMPT(src, tag, thunk, handler) \
  154. MAKMEMO (SCM_M_CALL_WITH_PROMPT, src, scm_cons (tag, scm_cons (thunk, handler)))
  155. /* This table must agree with the list of M_ constants in memoize.h */
  156. static const char *const memoized_tags[] =
  157. {
  158. "seq",
  159. "if",
  160. "lambda",
  161. "capture-env",
  162. "let",
  163. "quote",
  164. "capture-module",
  165. "apply",
  166. "call/cc",
  167. "call-with-values",
  168. "call",
  169. "lexical-ref",
  170. "lexical-set!",
  171. "box-ref",
  172. "box-set!",
  173. "resolve",
  174. "call-with-prompt",
  175. };
  176. /* Memoization-time environments mirror the structure of eval-time
  177. environments. Each link in the chain at memoization-time corresponds
  178. to a link at eval-time.
  179. env := module | (link, env)
  180. module := #f | #t
  181. link := flat-link . nested-link
  182. flat-link := (#t . ((var . pos) ...))
  183. nested-link := (#f . #(var ...))
  184. A module of #f indicates that the current module has not yet been
  185. captured. Memoizing a capture-module expression will capture the
  186. module.
  187. Flat environments copy the values for a set of free variables into a
  188. flat environment, via the capture-env expression. During memoization
  189. a flat link collects the values of free variables, along with their
  190. resolved outer locations. We are able to copy values because the
  191. incoming expression has already been assignment-converted. Flat
  192. environments prevent closures from hanging on to too much memory.
  193. Nested environments have a rib of "let" bindings, and link to an
  194. outer environment.
  195. */
  196. static int
  197. try_lookup_rib (SCM x, SCM rib)
  198. {
  199. int idx = 0;
  200. for (; idx < VECTOR_LENGTH (rib); idx++)
  201. if (scm_is_eq (x, VECTOR_REF (rib, idx)))
  202. return idx; /* bound */
  203. return -1;
  204. }
  205. static int
  206. lookup_rib (SCM x, SCM rib)
  207. {
  208. int idx = try_lookup_rib (x, rib);
  209. if (idx < 0)
  210. abort ();
  211. return idx;
  212. }
  213. static SCM
  214. make_pos (int depth, int width)
  215. {
  216. return scm_cons (SCM_I_MAKINUM (depth), SCM_I_MAKINUM (width));
  217. }
  218. static SCM
  219. push_nested_link (SCM vars, SCM env)
  220. {
  221. return scm_acons (SCM_BOOL_F, vars, env);
  222. }
  223. static SCM
  224. push_flat_link (SCM env)
  225. {
  226. return scm_acons (SCM_BOOL_T, SCM_EOL, env);
  227. }
  228. static int
  229. env_link_is_flat (SCM env_link)
  230. {
  231. return scm_is_true (CAR (env_link));
  232. }
  233. static SCM
  234. env_link_vars (SCM env_link)
  235. {
  236. return CDR (env_link);
  237. }
  238. static void
  239. env_link_add_flat_var (SCM env_link, SCM var, SCM pos)
  240. {
  241. SCM vars = env_link_vars (env_link);
  242. if (scm_is_false (scm_assq (var, vars)))
  243. scm_set_cdr_x (env_link, scm_acons (var, pos, vars));
  244. }
  245. static SCM
  246. lookup (SCM x, SCM env)
  247. {
  248. int d = 0;
  249. for (; scm_is_pair (env); env = CDR (env), d++)
  250. {
  251. SCM link = CAR (env);
  252. if (env_link_is_flat (link))
  253. {
  254. int w;
  255. SCM vars;
  256. for (vars = env_link_vars (link), w = scm_ilength (vars) - 1;
  257. scm_is_pair (vars);
  258. vars = CDR (vars), w--)
  259. if (scm_is_eq (x, (CAAR (vars))))
  260. return make_pos (d, w);
  261. env_link_add_flat_var (link, x, lookup (x, CDR (env)));
  262. return make_pos (d, scm_ilength (env_link_vars (link)) - 1);
  263. }
  264. else
  265. {
  266. int w = try_lookup_rib (x, env_link_vars (link));
  267. if (w < 0)
  268. continue;
  269. return make_pos (d, w);
  270. }
  271. }
  272. abort ();
  273. }
  274. static SCM
  275. capture_flat_env (SCM src, SCM lambda, SCM env)
  276. {
  277. int nenv;
  278. SCM vars, link, locs;
  279. link = CAR (env);
  280. vars = env_link_vars (link);
  281. nenv = scm_ilength (vars);
  282. locs = scm_c_make_vector (nenv, SCM_BOOL_F);
  283. for (; scm_is_pair (vars); vars = CDR (vars))
  284. scm_c_vector_set_x (locs, --nenv, CDAR (vars));
  285. return MAKMEMO_CAPTURE_ENV (src, locs, lambda);
  286. }
  287. /* Abbreviate SCM_EXPANDED_REF. Copied because I'm not sure about symbol pasting */
  288. #define REF(x,type,field) \
  289. (scm_struct_ref (x, SCM_I_MAKINUM (SCM_EXPANDED_##type##_##field)))
  290. #define SRC(x) \
  291. (scm_struct_ref (x, SCM_INUM0)) /* WARNING: this assumes that every
  292. expanded structure starts with
  293. its source. */
  294. static SCM list_of_guile = SCM_BOOL_F;
  295. static SCM memoize (SCM exp, SCM env);
  296. static SCM
  297. memoize_exps (SCM exps, SCM env)
  298. {
  299. SCM ret;
  300. for (ret = SCM_EOL; scm_is_pair (exps); exps = CDR (exps))
  301. ret = scm_cons (memoize (CAR (exps), env), ret);
  302. return scm_reverse_x (ret, SCM_UNDEFINED);
  303. }
  304. static SCM
  305. capture_env (SCM env)
  306. {
  307. if (scm_is_false (env))
  308. return SCM_BOOL_T;
  309. return env;
  310. }
  311. static SCM
  312. maybe_makmemo_capture_module (SCM src, SCM exp, SCM env)
  313. {
  314. if (scm_is_false (env))
  315. return MAKMEMO_CAPTURE_MODULE (src, exp);
  316. return exp;
  317. }
  318. static SCM
  319. memoize (SCM exp, SCM env)
  320. {
  321. SCM src;
  322. if (!SCM_EXPANDED_P (exp))
  323. abort ();
  324. src = SRC (exp);
  325. switch (SCM_EXPANDED_TYPE (exp))
  326. {
  327. case SCM_EXPANDED_VOID:
  328. return MAKMEMO_QUOTE (src, SCM_UNSPECIFIED);
  329. case SCM_EXPANDED_CONST:
  330. return MAKMEMO_QUOTE (src, REF (exp, CONST, EXP));
  331. case SCM_EXPANDED_PRIMITIVE_REF:
  332. if (scm_is_eq (scm_current_module (), scm_the_root_module ()))
  333. return maybe_makmemo_capture_module
  334. (src,
  335. MAKMEMO_BOX_REF (src,
  336. MAKMEMO_TOP_BOX (src,
  337. SCM_EXPANDED_TOPLEVEL_REF,
  338. REF (exp, PRIMITIVE_REF, NAME))),
  339. env);
  340. else
  341. return MAKMEMO_BOX_REF (src,
  342. MAKMEMO_MOD_BOX (src,
  343. SCM_EXPANDED_MODULE_REF,
  344. list_of_guile,
  345. REF (exp, PRIMITIVE_REF, NAME),
  346. SCM_BOOL_F));
  347. case SCM_EXPANDED_LEXICAL_REF:
  348. return MAKMEMO_LEX_REF (src,
  349. lookup (REF (exp, LEXICAL_REF, GENSYM), env));
  350. case SCM_EXPANDED_LEXICAL_SET:
  351. return MAKMEMO_LEX_SET (src,
  352. lookup (REF (exp, LEXICAL_SET, GENSYM), env),
  353. memoize (REF (exp, LEXICAL_SET, EXP), env));
  354. case SCM_EXPANDED_MODULE_REF:
  355. return MAKMEMO_BOX_REF (src,
  356. MAKMEMO_MOD_BOX
  357. (src,
  358. SCM_EXPANDED_MODULE_REF,
  359. REF (exp, MODULE_REF, MOD),
  360. REF (exp, MODULE_REF, NAME),
  361. REF (exp, MODULE_REF, PUBLIC)));
  362. case SCM_EXPANDED_MODULE_SET:
  363. return MAKMEMO_BOX_SET (src,
  364. MAKMEMO_MOD_BOX
  365. (src,
  366. SCM_EXPANDED_MODULE_SET,
  367. REF (exp, MODULE_SET, MOD),
  368. REF (exp, MODULE_SET, NAME),
  369. REF (exp, MODULE_SET, PUBLIC)),
  370. memoize (REF (exp, MODULE_SET, EXP), env));
  371. case SCM_EXPANDED_TOPLEVEL_REF:
  372. return maybe_makmemo_capture_module
  373. (src,
  374. MAKMEMO_BOX_REF (src,
  375. MAKMEMO_TOP_BOX (src,
  376. SCM_EXPANDED_TOPLEVEL_REF,
  377. REF (exp, TOPLEVEL_REF, NAME))),
  378. env);
  379. case SCM_EXPANDED_TOPLEVEL_SET:
  380. return maybe_makmemo_capture_module
  381. (src,
  382. MAKMEMO_BOX_SET (src,
  383. MAKMEMO_TOP_BOX (src,
  384. SCM_EXPANDED_TOPLEVEL_SET,
  385. REF (exp, TOPLEVEL_SET, NAME)),
  386. memoize (REF (exp, TOPLEVEL_SET, EXP),
  387. capture_env (env))),
  388. env);
  389. case SCM_EXPANDED_TOPLEVEL_DEFINE:
  390. return maybe_makmemo_capture_module
  391. (src,
  392. MAKMEMO_BOX_SET (src,
  393. MAKMEMO_TOP_BOX (src,
  394. SCM_EXPANDED_TOPLEVEL_DEFINE,
  395. REF (exp, TOPLEVEL_DEFINE, NAME)),
  396. memoize (REF (exp, TOPLEVEL_DEFINE, EXP),
  397. capture_env (env))),
  398. env);
  399. case SCM_EXPANDED_CONDITIONAL:
  400. return MAKMEMO_IF (src,
  401. memoize (REF (exp, CONDITIONAL, TEST), env),
  402. memoize (REF (exp, CONDITIONAL, CONSEQUENT), env),
  403. memoize (REF (exp, CONDITIONAL, ALTERNATE), env));
  404. case SCM_EXPANDED_CALL:
  405. {
  406. SCM proc, args;
  407. proc = REF (exp, CALL, PROC);
  408. args = memoize_exps (REF (exp, CALL, ARGS), env);
  409. return MAKMEMO_CALL (src, memoize (proc, env), args);
  410. }
  411. case SCM_EXPANDED_PRIMCALL:
  412. {
  413. SCM name, args;
  414. int nargs;
  415. name = REF (exp, PRIMCALL, NAME);
  416. args = memoize_exps (REF (exp, PRIMCALL, ARGS), env);
  417. nargs = scm_ilength (args);
  418. if (nargs == 3
  419. && scm_is_eq (name, scm_from_latin1_symbol ("call-with-prompt")))
  420. return MAKMEMO_CALL_WITH_PROMPT (src,
  421. CAR (args),
  422. CADR (args),
  423. CADDR (args));
  424. else if (nargs == 2
  425. && scm_is_eq (name, scm_from_latin1_symbol ("apply")))
  426. return MAKMEMO_APPLY (src, CAR (args), CADR (args));
  427. else if (nargs == 1
  428. && scm_is_eq (name,
  429. scm_from_latin1_symbol
  430. ("call-with-current-continuation")))
  431. return MAKMEMO_CONT (src, CAR (args));
  432. else if (nargs == 2
  433. && scm_is_eq (name,
  434. scm_from_latin1_symbol ("call-with-values")))
  435. return MAKMEMO_CALL_WITH_VALUES (src, CAR (args), CADR (args));
  436. else if (nargs == 1
  437. && scm_is_eq (name,
  438. scm_from_latin1_symbol ("variable-ref")))
  439. return MAKMEMO_BOX_REF (src, CAR (args));
  440. else if (nargs == 2
  441. && scm_is_eq (name,
  442. scm_from_latin1_symbol ("variable-set!")))
  443. return MAKMEMO_BOX_SET (src, CAR (args), CADR (args));
  444. else if (nargs == 2
  445. && scm_is_eq (name, scm_from_latin1_symbol ("wind")))
  446. return MAKMEMO_CALL (src, MAKMEMO_QUOTE (src, wind), args);
  447. else if (nargs == 0
  448. && scm_is_eq (name, scm_from_latin1_symbol ("unwind")))
  449. return MAKMEMO_CALL (src, MAKMEMO_QUOTE (src, unwind), SCM_EOL);
  450. else if (nargs == 2
  451. && scm_is_eq (name, scm_from_latin1_symbol ("push-fluid")))
  452. return MAKMEMO_CALL (src, MAKMEMO_QUOTE (src, push_fluid), args);
  453. else if (nargs == 0
  454. && scm_is_eq (name, scm_from_latin1_symbol ("pop-fluid")))
  455. return MAKMEMO_CALL (src, MAKMEMO_QUOTE (src, pop_fluid), SCM_EOL);
  456. else if (nargs == 1
  457. && scm_is_eq (name,
  458. scm_from_latin1_symbol ("push-dynamic-state")))
  459. return MAKMEMO_CALL (src,
  460. MAKMEMO_QUOTE (src, push_dynamic_state),
  461. args);
  462. else if (nargs == 0
  463. && scm_is_eq (name,
  464. scm_from_latin1_symbol ("pop-dynamic-state")))
  465. return MAKMEMO_CALL (src,
  466. MAKMEMO_QUOTE (src, pop_dynamic_state),
  467. SCM_EOL);
  468. else if (scm_is_eq (scm_current_module (), scm_the_root_module ()))
  469. return MAKMEMO_CALL (src,
  470. maybe_makmemo_capture_module
  471. (src,
  472. MAKMEMO_BOX_REF
  473. (src,
  474. MAKMEMO_TOP_BOX (src,
  475. SCM_EXPANDED_TOPLEVEL_REF,
  476. name)),
  477. env),
  478. args);
  479. else
  480. return MAKMEMO_CALL (src,
  481. MAKMEMO_BOX_REF
  482. (src,
  483. MAKMEMO_MOD_BOX (src,
  484. SCM_EXPANDED_MODULE_REF,
  485. list_of_guile,
  486. name,
  487. SCM_BOOL_F)),
  488. args);
  489. }
  490. case SCM_EXPANDED_SEQ:
  491. return MAKMEMO_SEQ (src,
  492. memoize (REF (exp, SEQ, HEAD), env),
  493. memoize (REF (exp, SEQ, TAIL), env));
  494. case SCM_EXPANDED_LAMBDA:
  495. /* The body will be a lambda-case. */
  496. {
  497. SCM meta, body, proc, new_env;
  498. meta = REF (exp, LAMBDA, META);
  499. body = REF (exp, LAMBDA, BODY);
  500. new_env = push_flat_link (capture_env (env));
  501. proc = memoize (body, new_env);
  502. SCM_SETCAR (SCM_CDR (SCM_MEMOIZED_ARGS (proc)), meta);
  503. return maybe_makmemo_capture_module (src,
  504. capture_flat_env (src,
  505. proc,
  506. new_env),
  507. env);
  508. }
  509. case SCM_EXPANDED_LAMBDA_CASE:
  510. {
  511. SCM req, rest, opt, kw, inits, vars, body, alt;
  512. SCM unbound, arity, rib, new_env;
  513. int nreq, nopt, ninits;
  514. req = REF (exp, LAMBDA_CASE, REQ);
  515. rest = scm_not (scm_not (REF (exp, LAMBDA_CASE, REST)));
  516. opt = REF (exp, LAMBDA_CASE, OPT);
  517. kw = REF (exp, LAMBDA_CASE, KW);
  518. inits = REF (exp, LAMBDA_CASE, INITS);
  519. vars = REF (exp, LAMBDA_CASE, GENSYMS);
  520. body = REF (exp, LAMBDA_CASE, BODY);
  521. alt = REF (exp, LAMBDA_CASE, ALTERNATE);
  522. nreq = scm_ilength (req);
  523. nopt = scm_is_pair (opt) ? scm_ilength (opt) : 0;
  524. ninits = scm_ilength (inits);
  525. /* This relies on assignment conversion turning inits into a
  526. sequence of CONST expressions whose values are a unique
  527. "unbound" token. */
  528. unbound = ninits ? REF (CAR (inits), CONST, EXP) : SCM_BOOL_F;
  529. rib = scm_vector (vars);
  530. new_env = push_nested_link (rib, env);
  531. if (scm_is_true (kw))
  532. {
  533. /* (aok? (kw name sym) ...) -> (aok? (kw . index) ...) */
  534. SCM aok = CAR (kw), indices = SCM_EOL;
  535. for (kw = CDR (kw); scm_is_pair (kw); kw = CDR (kw))
  536. {
  537. SCM k;
  538. int idx;
  539. k = CAR (CAR (kw));
  540. idx = lookup_rib (CADDR (CAR (kw)), rib);
  541. indices = scm_acons (k, SCM_I_MAKINUM (idx), indices);
  542. }
  543. kw = scm_cons (aok, scm_reverse_x (indices, SCM_UNDEFINED));
  544. }
  545. if (scm_is_false (alt) && scm_is_false (kw) && scm_is_false (opt))
  546. {
  547. if (scm_is_false (rest))
  548. arity = FIXED_ARITY (nreq);
  549. else
  550. arity = REST_ARITY (nreq, SCM_BOOL_T);
  551. }
  552. else if (scm_is_true (alt))
  553. arity = FULL_ARITY (nreq, rest, nopt, kw, ninits, unbound,
  554. SCM_MEMOIZED_ARGS (memoize (alt, env)));
  555. else
  556. arity = FULL_ARITY (nreq, rest, nopt, kw, ninits, unbound,
  557. SCM_BOOL_F);
  558. return MAKMEMO_LAMBDA (src,
  559. memoize (body, new_env), arity,
  560. SCM_EOL /* meta, filled in later */);
  561. }
  562. case SCM_EXPANDED_LET:
  563. {
  564. SCM vars, exps, body, varsv, inits, new_env;
  565. int i;
  566. vars = REF (exp, LET, GENSYMS);
  567. exps = REF (exp, LET, VALS);
  568. body = REF (exp, LET, BODY);
  569. varsv = scm_vector (vars);
  570. inits = scm_c_make_vector (VECTOR_LENGTH (varsv),
  571. SCM_BOOL_F);
  572. new_env = push_nested_link (varsv, capture_env (env));
  573. for (i = 0; scm_is_pair (exps); exps = CDR (exps), i++)
  574. VECTOR_SET (inits, i, memoize (CAR (exps), env));
  575. return maybe_makmemo_capture_module
  576. (src, MAKMEMO_LET (src, inits, memoize (body, new_env)), env);
  577. }
  578. default:
  579. abort ();
  580. }
  581. }
  582. SCM_DEFINE (scm_memoize_expression, "memoize-expression", 1, 0, 0,
  583. (SCM exp),
  584. "Memoize the expression @var{exp}.")
  585. #define FUNC_NAME s_scm_memoize_expression
  586. {
  587. SCM_ASSERT_TYPE (SCM_EXPANDED_P (exp), exp, 1, FUNC_NAME, "expanded");
  588. return memoize (scm_convert_assignment (exp), SCM_BOOL_F);
  589. }
  590. #undef FUNC_NAME
  591. SCM_SYMBOL (sym_placeholder, "_");
  592. static SCM unmemoize (SCM expr);
  593. static SCM
  594. unmemoize_exprs (SCM exprs)
  595. {
  596. SCM ret, tail;
  597. if (scm_is_null (exprs))
  598. return SCM_EOL;
  599. ret = scm_list_1 (unmemoize (CAR (exprs)));
  600. tail = ret;
  601. for (exprs = CDR (exprs); !scm_is_null (exprs); exprs = CDR (exprs))
  602. {
  603. SCM_SETCDR (tail, scm_list_1 (unmemoize (CAR (exprs))));
  604. tail = CDR (tail);
  605. }
  606. return ret;
  607. }
  608. static SCM
  609. unmemoize_bindings (SCM inits)
  610. {
  611. SCM ret = SCM_EOL;
  612. int n = scm_c_vector_length (inits);
  613. while (n--)
  614. ret = scm_cons (unmemoize (scm_c_vector_ref (inits, n)), ret);
  615. return ret;
  616. }
  617. static SCM
  618. unmemoize_lexical (SCM n)
  619. {
  620. char buf[32];
  621. buf[31] = 0;
  622. snprintf (buf, 31, "<%u,%u>", scm_to_uint32 (CAR (n)),
  623. scm_to_uint32 (CDR (n)));
  624. return scm_from_utf8_symbol (buf);
  625. }
  626. static SCM
  627. unmemoize (const SCM expr)
  628. {
  629. SCM args;
  630. args = SCM_MEMOIZED_ARGS (expr);
  631. switch (SCM_MEMOIZED_TAG (expr))
  632. {
  633. case SCM_M_APPLY:
  634. return scm_cons (scm_from_latin1_symbol ("apply"),
  635. unmemoize_exprs (args));
  636. case SCM_M_SEQ:
  637. return scm_list_3 (scm_sym_begin, unmemoize (CAR (args)),
  638. unmemoize (CDR (args)));
  639. case SCM_M_CALL:
  640. return unmemoize_exprs (args);
  641. case SCM_M_CONT:
  642. return scm_list_2 (scm_from_latin1_symbol
  643. ("call-with-current_continuation"),
  644. unmemoize (args));
  645. case SCM_M_CALL_WITH_VALUES:
  646. return scm_list_3 (scm_from_latin1_symbol ("call-with-values"),
  647. unmemoize (CAR (args)), unmemoize (CDR (args)));
  648. case SCM_M_CAPTURE_MODULE:
  649. return scm_list_2 (scm_from_latin1_symbol ("capture-module"),
  650. unmemoize (args));
  651. case SCM_M_IF:
  652. return scm_list_4 (scm_sym_if, unmemoize (scm_car (args)),
  653. unmemoize (scm_cadr (args)), unmemoize (scm_cddr (args)));
  654. case SCM_M_LAMBDA:
  655. {
  656. SCM body = CAR (args), spec = CDDR (args);
  657. if (scm_is_null (CDR (spec)))
  658. return scm_list_3 (scm_sym_lambda,
  659. scm_make_list (CAR (spec), sym_placeholder),
  660. unmemoize (CAR (args)));
  661. else if (scm_is_null (SCM_CDDR (spec)))
  662. {
  663. SCM formals = scm_make_list (CAR (spec), sym_placeholder);
  664. return scm_list_3 (scm_sym_lambda,
  665. scm_is_true (CADR (spec))
  666. ? scm_cons_star (sym_placeholder, formals)
  667. : formals,
  668. unmemoize (CAR (args)));
  669. }
  670. else
  671. {
  672. SCM alt, tail;
  673. alt = CADDDR (CDDDR (spec));
  674. if (scm_is_true (alt))
  675. tail = CDR (unmemoize (alt));
  676. else
  677. tail = SCM_EOL;
  678. return scm_cons
  679. (sym_case_lambda_star,
  680. scm_cons (scm_list_2 (scm_list_5 (CAR (spec),
  681. CADR (spec),
  682. CADDR (spec),
  683. CADDDR (spec),
  684. CADR (CDDDR (spec))),
  685. unmemoize (body)),
  686. tail));
  687. }
  688. }
  689. case SCM_M_CAPTURE_ENV:
  690. return scm_list_3 (scm_from_latin1_symbol ("capture-env"),
  691. CAR (args),
  692. unmemoize (CDR (args)));
  693. case SCM_M_LET:
  694. return scm_list_3 (scm_sym_let,
  695. unmemoize_bindings (CAR (args)),
  696. unmemoize (CDR (args)));
  697. case SCM_M_QUOTE:
  698. return scm_list_2 (scm_sym_quote, args);
  699. case SCM_M_LEXICAL_REF:
  700. return unmemoize_lexical (args);
  701. case SCM_M_LEXICAL_SET:
  702. return scm_list_3 (scm_sym_set_x, unmemoize_lexical (CAR (args)),
  703. unmemoize (CDR (args)));
  704. case SCM_M_BOX_REF:
  705. return scm_list_2 (scm_from_latin1_symbol ("variable-ref"),
  706. unmemoize (args));
  707. case SCM_M_BOX_SET:
  708. return scm_list_3 (scm_from_latin1_symbol ("variable-set!"),
  709. unmemoize (CAR (args)),
  710. unmemoize (CDR (args)));
  711. case SCM_M_RESOLVE:
  712. if (SCM_VARIABLEP (args))
  713. return args;
  714. else if (scm_is_symbol (CDR (args)))
  715. return CDR (args);
  716. else
  717. return scm_list_3
  718. (scm_is_true (CDDDR (args)) ? scm_sym_at : scm_sym_atat,
  719. scm_i_finite_list_copy (CADR (args)),
  720. CADDR (args));
  721. case SCM_M_CALL_WITH_PROMPT:
  722. return scm_list_4 (scm_from_latin1_symbol ("call-with-prompt"),
  723. unmemoize (CAR (args)),
  724. unmemoize (CADR (args)),
  725. unmemoize (CDDR (args)));
  726. default:
  727. abort ();
  728. }
  729. }
  730. SCM_DEFINE (scm_unmemoize_expression, "unmemoize-expression", 1, 0, 0,
  731. (SCM m),
  732. "Unmemoize the memoized expression @var{m}.")
  733. #define FUNC_NAME s_scm_unmemoize_expression
  734. {
  735. return unmemoize (m);
  736. }
  737. #undef FUNC_NAME
  738. SCM_DEFINE (scm_memoized_typecode, "memoized-typecode", 1, 0, 0,
  739. (SCM sym),
  740. "Return the memoized typecode corresponding to the symbol @var{sym}.")
  741. #define FUNC_NAME s_scm_memoized_typecode
  742. {
  743. int i;
  744. SCM_VALIDATE_SYMBOL (1, sym);
  745. for (i = 0; i < sizeof(memoized_tags)/sizeof(const char*); i++)
  746. if (strcmp (scm_i_symbol_chars (sym), memoized_tags[i]) == 0)
  747. return scm_from_int32 (i);
  748. return SCM_BOOL_F;
  749. }
  750. #undef FUNC_NAME
  751. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  752. static void error_unbound_variable (SCM symbol) SCM_NORETURN;
  753. static void error_unbound_variable (SCM symbol)
  754. {
  755. scm_error (scm_unbound_variable_key, NULL, "Unbound variable: ~S",
  756. scm_list_1 (symbol), SCM_BOOL_F);
  757. }
  758. SCM_DEFINE (scm_sys_resolve_variable, "%resolve-variable", 2, 0, 0,
  759. (SCM loc, SCM mod),
  760. "Look up and return the variable for @var{loc}.")
  761. #define FUNC_NAME s_scm_sys_resolve_variable
  762. {
  763. int mode;
  764. if (scm_is_false (mod))
  765. mod = scm_the_root_module ();
  766. mode = scm_to_int (scm_car (loc));
  767. loc = scm_cdr (loc);
  768. switch (mode)
  769. {
  770. case SCM_EXPANDED_TOPLEVEL_REF:
  771. case SCM_EXPANDED_TOPLEVEL_SET:
  772. {
  773. SCM var = scm_module_variable (mod, loc);
  774. if (scm_is_false (var)
  775. || (mode == SCM_EXPANDED_TOPLEVEL_REF
  776. && scm_is_false (scm_variable_bound_p (var))))
  777. error_unbound_variable (loc);
  778. return var;
  779. }
  780. case SCM_EXPANDED_TOPLEVEL_DEFINE:
  781. {
  782. return scm_module_ensure_local_variable (mod, loc);
  783. }
  784. case SCM_EXPANDED_MODULE_REF:
  785. case SCM_EXPANDED_MODULE_SET:
  786. {
  787. SCM var;
  788. mod = scm_resolve_module (scm_car (loc));
  789. if (scm_is_true (scm_cddr (loc)))
  790. mod = scm_module_public_interface (mod);
  791. var = scm_module_lookup (mod, scm_cadr (loc));
  792. if (mode == SCM_EXPANDED_MODULE_SET
  793. && scm_is_false (scm_variable_bound_p (var)))
  794. error_unbound_variable (scm_cadr (loc));
  795. return var;
  796. }
  797. default:
  798. scm_wrong_type_arg (FUNC_NAME, 1, loc);
  799. return SCM_BOOL_F;
  800. }
  801. }
  802. #undef FUNC_NAME
  803. void
  804. scm_init_memoize ()
  805. {
  806. #include "memoize.x"
  807. wind = scm_c_make_gsubr ("wind", 2, 0, 0, do_wind);
  808. unwind = scm_c_make_gsubr ("unwind", 0, 0, 0, do_unwind);
  809. push_fluid = scm_c_make_gsubr ("push-fluid", 2, 0, 0, do_push_fluid);
  810. pop_fluid = scm_c_make_gsubr ("pop-fluid", 0, 0, 0, do_pop_fluid);
  811. push_dynamic_state = scm_c_make_gsubr ("push-dynamic_state", 1, 0, 0,
  812. do_push_dynamic_state);
  813. pop_dynamic_state = scm_c_make_gsubr ("pop-dynamic_state", 0, 0, 0,
  814. do_pop_dynamic_state);
  815. list_of_guile = scm_list_1 (scm_from_latin1_symbol ("guile"));
  816. }