memoize.c 26 KB

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