memoize.c 27 KB

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