eval.i.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. /*
  2. * eval.i.c - actual evaluator code for GUILE
  3. *
  4. * Copyright (C) 2002, 03, 04, 05, 06, 07, 09 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #undef RETURN
  22. #undef ENTER_APPLY
  23. #undef PREP_APPLY
  24. #undef CEVAL
  25. #undef SCM_APPLY
  26. #undef EVAL_DEBUGGING_P
  27. #ifdef DEVAL
  28. /*
  29. This code is specific for the debugging support.
  30. */
  31. #define EVAL_DEBUGGING_P 1
  32. #define CEVAL deval /* Substitute all uses of ceval */
  33. #define SCM_APPLY scm_dapply
  34. #define PREP_APPLY(p, l) \
  35. { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
  36. #define ENTER_APPLY \
  37. do { \
  38. SCM_SET_ARGSREADY (debug);\
  39. if (scm_check_apply_p && SCM_TRAPS_P)\
  40. if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && SCM_PROCTRACEP (proc)))\
  41. {\
  42. SCM tmp, tail = scm_from_bool(SCM_TRACED_FRAME_P (debug)); \
  43. SCM_SET_TRACED_FRAME (debug); \
  44. SCM_TRAPS_P = 0;\
  45. tmp = scm_make_debugobj (&debug);\
  46. scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
  47. SCM_TRAPS_P = 1;\
  48. }\
  49. } while (0)
  50. #define RETURN(e) do { proc = (e); goto exit; } while (0)
  51. #ifdef STACK_CHECKING
  52. # ifndef EVAL_STACK_CHECKING
  53. # define EVAL_STACK_CHECKING
  54. # endif /* EVAL_STACK_CHECKING */
  55. #endif /* STACK_CHECKING */
  56. static SCM
  57. deval_args (SCM l, SCM env, SCM proc, SCM *lloc)
  58. {
  59. SCM *results = lloc;
  60. while (scm_is_pair (l))
  61. {
  62. const SCM res = SCM_I_XEVALCAR (l, env, 1);
  63. *lloc = scm_list_1 (res);
  64. lloc = SCM_CDRLOC (*lloc);
  65. l = SCM_CDR (l);
  66. }
  67. if (!scm_is_null (l))
  68. scm_wrong_num_args (proc);
  69. return *results;
  70. }
  71. #else /* DEVAL */
  72. /*
  73. Code is specific to debugging-less support.
  74. */
  75. #define CEVAL ceval
  76. #define SCM_APPLY scm_apply
  77. #define PREP_APPLY(proc, args)
  78. #define ENTER_APPLY
  79. #define RETURN(x) do { return x; } while (0)
  80. #define EVAL_DEBUGGING_P 0
  81. #ifdef STACK_CHECKING
  82. # ifndef NO_CEVAL_STACK_CHECKING
  83. # define EVAL_STACK_CHECKING
  84. # endif
  85. #endif
  86. static void
  87. ceval_letrec_inits (SCM env, SCM init_forms, SCM **init_values_eol)
  88. {
  89. SCM argv[10];
  90. int i = 0, imax = sizeof (argv) / sizeof (SCM);
  91. while (!scm_is_null (init_forms))
  92. {
  93. if (imax == i)
  94. {
  95. ceval_letrec_inits (env, init_forms, init_values_eol);
  96. break;
  97. }
  98. argv[i++] = SCM_I_XEVALCAR (init_forms, env, 0);
  99. init_forms = SCM_CDR (init_forms);
  100. }
  101. for (i--; i >= 0; i--)
  102. {
  103. **init_values_eol = scm_list_1 (argv[i]);
  104. *init_values_eol = SCM_CDRLOC (**init_values_eol);
  105. }
  106. }
  107. static SCM
  108. scm_ceval_args (SCM l, SCM env, SCM proc)
  109. {
  110. SCM results = SCM_EOL, *lloc = &results, res;
  111. while (scm_is_pair (l))
  112. {
  113. res = EVALCAR (l, env);
  114. *lloc = scm_list_1 (res);
  115. lloc = SCM_CDRLOC (*lloc);
  116. l = SCM_CDR (l);
  117. }
  118. if (!scm_is_null (l))
  119. scm_wrong_num_args (proc);
  120. return results;
  121. }
  122. SCM
  123. scm_eval_args (SCM l, SCM env, SCM proc)
  124. {
  125. return scm_ceval_args (l, env, proc);
  126. }
  127. #endif
  128. #define EVAL(x, env) SCM_I_XEVAL(x, env, EVAL_DEBUGGING_P)
  129. #define EVALCAR(x, env) SCM_I_XEVALCAR(x, env, EVAL_DEBUGGING_P)
  130. /* Update the toplevel environment frame ENV so that it refers to the
  131. * current module. */
  132. #define UPDATE_TOPLEVEL_ENV(env) \
  133. do { \
  134. SCM p = scm_current_module_lookup_closure (); \
  135. if (p != SCM_CAR (env)) \
  136. env = scm_top_level_env (p); \
  137. } while (0)
  138. #define SCM_VALIDATE_NON_EMPTY_COMBINATION(x) \
  139. ASSERT_SYNTAX (!scm_is_eq ((x), SCM_EOL), s_empty_combination, x)
  140. /* This is the evaluator. Like any real monster, it has three heads:
  141. *
  142. * ceval is the non-debugging evaluator, deval is the debugging version. Both
  143. * are implemented using a common code base, using the following mechanism:
  144. * CEVAL is a macro, which is either defined to ceval or deval. Thus, there
  145. * is no function CEVAL, but the code for CEVAL actually compiles to either
  146. * ceval or deval. When CEVAL is defined to ceval, it is known that the macro
  147. * DEVAL is not defined. When CEVAL is defined to deval, then the macro DEVAL
  148. * is known to be defined. Thus, in CEVAL parts for the debugging evaluator
  149. * are enclosed within #ifdef DEVAL ... #endif.
  150. *
  151. * All three (ceval, deval and their common implementation CEVAL) take two
  152. * input parameters, x and env: x is a single expression to be evalutated.
  153. * env is the environment in which bindings are searched.
  154. *
  155. * x is known to be a pair. Since x is a single expression, it is necessarily
  156. * in a tail position. If x is just a call to another function like in the
  157. * expression (foo exp1 exp2 ...), the realization of that call therefore
  158. * _must_not_ increase stack usage (the evaluation of exp1, exp2 etc.,
  159. * however, may do so). This is realized by making extensive use of 'goto'
  160. * statements within the evaluator: The gotos replace recursive calls to
  161. * CEVAL, thus re-using the same stack frame that CEVAL was already using.
  162. * If, however, x represents some form that requires to evaluate a sequence of
  163. * expressions like (begin exp1 exp2 ...), then recursive calls to CEVAL are
  164. * performed for all but the last expression of that sequence. */
  165. static SCM
  166. CEVAL (SCM x, SCM env)
  167. {
  168. SCM proc, arg1;
  169. #ifdef DEVAL
  170. scm_t_debug_frame debug;
  171. scm_t_debug_info *debug_info_end;
  172. debug.prev = scm_i_last_debug_frame ();
  173. debug.status = 0;
  174. /*
  175. * The debug.vect contains twice as much scm_t_debug_info frames as the
  176. * user has specified with (debug-set! frames <n>).
  177. *
  178. * Even frames are eval frames, odd frames are apply frames.
  179. */
  180. debug.vect = (scm_t_debug_info *) alloca (scm_debug_eframe_size
  181. * sizeof (scm_t_debug_info));
  182. debug.info = debug.vect;
  183. debug_info_end = debug.vect + scm_debug_eframe_size;
  184. scm_i_set_last_debug_frame (&debug);
  185. #endif
  186. #ifdef EVAL_STACK_CHECKING
  187. if (scm_stack_checking_enabled_p && SCM_STACK_OVERFLOW_P (&proc))
  188. {
  189. #ifdef DEVAL
  190. debug.info->e.exp = x;
  191. debug.info->e.env = env;
  192. #endif
  193. scm_report_stack_overflow ();
  194. }
  195. #endif
  196. #ifdef DEVAL
  197. goto start;
  198. #endif
  199. loop:
  200. #ifdef DEVAL
  201. SCM_CLEAR_ARGSREADY (debug);
  202. if (SCM_OVERFLOWP (debug))
  203. --debug.info;
  204. /*
  205. * In theory, this should be the only place where it is necessary to
  206. * check for space in debug.vect since both eval frames and
  207. * available space are even.
  208. *
  209. * For this to be the case, however, it is necessary that primitive
  210. * special forms which jump back to `loop', `begin' or some similar
  211. * label call PREP_APPLY.
  212. */
  213. else if (++debug.info >= debug_info_end)
  214. {
  215. SCM_SET_OVERFLOW (debug);
  216. debug.info -= 2;
  217. }
  218. start:
  219. debug.info->e.exp = x;
  220. debug.info->e.env = env;
  221. if (scm_check_entry_p && SCM_TRAPS_P)
  222. {
  223. if (SCM_ENTER_FRAME_P
  224. || (SCM_BREAKPOINTS_P && scm_c_source_property_breakpoint_p (x)))
  225. {
  226. SCM stackrep;
  227. SCM tail = scm_from_bool (SCM_TAILRECP (debug));
  228. SCM_SET_TAILREC (debug);
  229. stackrep = scm_make_debugobj (&debug);
  230. SCM_TRAPS_P = 0;
  231. stackrep = scm_call_4 (SCM_ENTER_FRAME_HDLR,
  232. scm_sym_enter_frame,
  233. stackrep,
  234. tail,
  235. unmemoize_expression (x, env));
  236. SCM_TRAPS_P = 1;
  237. if (scm_is_pair (stackrep) &&
  238. scm_is_eq (SCM_CAR (stackrep), sym_instead))
  239. {
  240. /* This gives the possibility for the debugger to modify
  241. the source expression before evaluation. */
  242. x = SCM_CDR (stackrep);
  243. if (SCM_IMP (x))
  244. RETURN (x);
  245. }
  246. }
  247. }
  248. #endif
  249. dispatch:
  250. SCM_TICK;
  251. if (SCM_ISYMP (SCM_CAR (x)))
  252. {
  253. switch (ISYMNUM (SCM_CAR (x)))
  254. {
  255. case (ISYMNUM (SCM_IM_AND)):
  256. x = SCM_CDR (x);
  257. while (!scm_is_null (SCM_CDR (x)))
  258. {
  259. SCM test_result = EVALCAR (x, env);
  260. if (scm_is_false (test_result) || SCM_NILP (test_result))
  261. RETURN (SCM_BOOL_F);
  262. else
  263. x = SCM_CDR (x);
  264. }
  265. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  266. goto carloop;
  267. case (ISYMNUM (SCM_IM_BEGIN)):
  268. x = SCM_CDR (x);
  269. if (scm_is_null (x))
  270. RETURN (SCM_UNSPECIFIED);
  271. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  272. begin:
  273. /* If we are on toplevel with a lookup closure, we need to sync
  274. with the current module. */
  275. if (scm_is_pair (env) && !scm_is_pair (SCM_CAR (env)))
  276. {
  277. UPDATE_TOPLEVEL_ENV (env);
  278. while (!scm_is_null (SCM_CDR (x)))
  279. {
  280. EVALCAR (x, env);
  281. UPDATE_TOPLEVEL_ENV (env);
  282. x = SCM_CDR (x);
  283. }
  284. goto carloop;
  285. }
  286. else
  287. goto nontoplevel_begin;
  288. nontoplevel_begin:
  289. while (!scm_is_null (SCM_CDR (x)))
  290. {
  291. const SCM form = SCM_CAR (x);
  292. if (SCM_IMP (form))
  293. {
  294. if (SCM_ISYMP (form))
  295. {
  296. scm_dynwind_begin (0);
  297. scm_i_dynwind_pthread_mutex_lock (&source_mutex);
  298. /* check for race condition */
  299. if (SCM_ISYMP (SCM_CAR (x)))
  300. m_expand_body (x, env);
  301. scm_dynwind_end ();
  302. goto nontoplevel_begin;
  303. }
  304. else
  305. SCM_VALIDATE_NON_EMPTY_COMBINATION (form);
  306. }
  307. else
  308. (void) EVAL (form, env);
  309. x = SCM_CDR (x);
  310. }
  311. carloop:
  312. {
  313. /* scm_eval last form in list */
  314. const SCM last_form = SCM_CAR (x);
  315. if (scm_is_pair (last_form))
  316. {
  317. /* This is by far the most frequent case. */
  318. x = last_form;
  319. goto loop; /* tail recurse */
  320. }
  321. else if (SCM_IMP (last_form))
  322. RETURN (SCM_I_EVALIM (last_form, env));
  323. else if (SCM_VARIABLEP (last_form))
  324. RETURN (SCM_VARIABLE_REF (last_form));
  325. else if (scm_is_symbol (last_form))
  326. RETURN (*scm_lookupcar (x, env, 1));
  327. else
  328. RETURN (last_form);
  329. }
  330. case (ISYMNUM (SCM_IM_CASE)):
  331. x = SCM_CDR (x);
  332. {
  333. const SCM key = EVALCAR (x, env);
  334. x = SCM_CDR (x);
  335. while (!scm_is_null (x))
  336. {
  337. const SCM clause = SCM_CAR (x);
  338. SCM labels = SCM_CAR (clause);
  339. if (scm_is_eq (labels, SCM_IM_ELSE))
  340. {
  341. x = SCM_CDR (clause);
  342. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  343. goto begin;
  344. }
  345. while (!scm_is_null (labels))
  346. {
  347. const SCM label = SCM_CAR (labels);
  348. if (scm_is_eq (label, key)
  349. || scm_is_true (scm_eqv_p (label, key)))
  350. {
  351. x = SCM_CDR (clause);
  352. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  353. goto begin;
  354. }
  355. labels = SCM_CDR (labels);
  356. }
  357. x = SCM_CDR (x);
  358. }
  359. }
  360. RETURN (SCM_UNSPECIFIED);
  361. case (ISYMNUM (SCM_IM_COND)):
  362. x = SCM_CDR (x);
  363. while (!scm_is_null (x))
  364. {
  365. const SCM clause = SCM_CAR (x);
  366. if (scm_is_eq (SCM_CAR (clause), SCM_IM_ELSE))
  367. {
  368. x = SCM_CDR (clause);
  369. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  370. goto begin;
  371. }
  372. else
  373. {
  374. arg1 = EVALCAR (clause, env);
  375. /* SRFI 61 extended cond */
  376. if (!scm_is_null (SCM_CDR (clause))
  377. && !scm_is_null (SCM_CDDR (clause))
  378. && scm_is_eq (SCM_CADDR (clause), SCM_IM_ARROW))
  379. {
  380. SCM xx, guard_result;
  381. if (SCM_VALUESP (arg1))
  382. arg1 = scm_struct_ref (arg1, SCM_INUM0);
  383. else
  384. arg1 = scm_list_1 (arg1);
  385. xx = SCM_CDR (clause);
  386. proc = EVALCAR (xx, env);
  387. guard_result = SCM_APPLY (proc, arg1, SCM_EOL);
  388. if (scm_is_true (guard_result)
  389. && !SCM_NILP (guard_result))
  390. {
  391. proc = SCM_CDDR (xx);
  392. proc = EVALCAR (proc, env);
  393. PREP_APPLY (proc, arg1);
  394. goto apply_proc;
  395. }
  396. }
  397. else if (scm_is_true (arg1) && !SCM_NILP (arg1))
  398. {
  399. x = SCM_CDR (clause);
  400. if (scm_is_null (x))
  401. RETURN (arg1);
  402. else if (!scm_is_eq (SCM_CAR (x), SCM_IM_ARROW))
  403. {
  404. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  405. goto begin;
  406. }
  407. else
  408. {
  409. proc = SCM_CDR (x);
  410. proc = EVALCAR (proc, env);
  411. PREP_APPLY (proc, scm_list_1 (arg1));
  412. ENTER_APPLY;
  413. goto evap1;
  414. }
  415. }
  416. x = SCM_CDR (x);
  417. }
  418. }
  419. RETURN (SCM_UNSPECIFIED);
  420. case (ISYMNUM (SCM_IM_DO)):
  421. x = SCM_CDR (x);
  422. {
  423. /* Compute the initialization values and the initial environment. */
  424. SCM init_forms = SCM_CAR (x);
  425. SCM init_values = SCM_EOL;
  426. while (!scm_is_null (init_forms))
  427. {
  428. init_values = scm_cons (EVALCAR (init_forms, env), init_values);
  429. init_forms = SCM_CDR (init_forms);
  430. }
  431. x = SCM_CDR (x);
  432. env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
  433. }
  434. x = SCM_CDR (x);
  435. {
  436. SCM test_form = SCM_CAR (x);
  437. SCM body_forms = SCM_CADR (x);
  438. SCM step_forms = SCM_CDDR (x);
  439. SCM test_result = EVALCAR (test_form, env);
  440. while (scm_is_false (test_result) || SCM_NILP (test_result))
  441. {
  442. {
  443. /* Evaluate body forms. */
  444. SCM temp_forms;
  445. for (temp_forms = body_forms;
  446. !scm_is_null (temp_forms);
  447. temp_forms = SCM_CDR (temp_forms))
  448. {
  449. SCM form = SCM_CAR (temp_forms);
  450. /* Dirk:FIXME: We only need to eval forms that may have
  451. * a side effect here. This is only true for forms that
  452. * start with a pair. All others are just constants.
  453. * Since with the current memoizer 'form' may hold a
  454. * constant, we call EVAL here to handle the constant
  455. * cases. In the long run it would make sense to have
  456. * the macro transformer of 'do' eliminate all forms
  457. * that have no sideeffect. Then instead of EVAL we
  458. * could call CEVAL directly here. */
  459. (void) EVAL (form, env);
  460. }
  461. }
  462. {
  463. /* Evaluate the step expressions. */
  464. SCM temp_forms;
  465. SCM step_values = SCM_EOL;
  466. for (temp_forms = step_forms;
  467. !scm_is_null (temp_forms);
  468. temp_forms = SCM_CDR (temp_forms))
  469. {
  470. const SCM value = EVALCAR (temp_forms, env);
  471. step_values = scm_cons (value, step_values);
  472. }
  473. env = SCM_EXTEND_ENV (SCM_CAAR (env),
  474. step_values,
  475. SCM_CDR (env));
  476. }
  477. test_result = EVALCAR (test_form, env);
  478. }
  479. }
  480. x = SCM_CDAR (x);
  481. if (scm_is_null (x))
  482. RETURN (SCM_UNSPECIFIED);
  483. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  484. goto nontoplevel_begin;
  485. case (ISYMNUM (SCM_IM_IF)):
  486. x = SCM_CDR (x);
  487. {
  488. SCM test_result = EVALCAR (x, env);
  489. x = SCM_CDR (x); /* then expression */
  490. if (scm_is_false (test_result) || SCM_NILP (test_result))
  491. {
  492. x = SCM_CDR (x); /* else expression */
  493. if (scm_is_null (x))
  494. RETURN (SCM_UNSPECIFIED);
  495. }
  496. }
  497. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  498. goto carloop;
  499. case (ISYMNUM (SCM_IM_LET)):
  500. x = SCM_CDR (x);
  501. {
  502. SCM init_forms = SCM_CADR (x);
  503. SCM init_values = SCM_EOL;
  504. do
  505. {
  506. init_values = scm_cons (EVALCAR (init_forms, env), init_values);
  507. init_forms = SCM_CDR (init_forms);
  508. }
  509. while (!scm_is_null (init_forms));
  510. env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
  511. }
  512. x = SCM_CDDR (x);
  513. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  514. goto nontoplevel_begin;
  515. case (ISYMNUM (SCM_IM_LETREC)):
  516. x = SCM_CDR (x);
  517. env = SCM_EXTEND_ENV (SCM_CAR (x), undefineds, env);
  518. x = SCM_CDR (x);
  519. {
  520. SCM init_forms = SCM_CAR (x);
  521. SCM init_values = scm_list_1 (SCM_BOOL_T);
  522. SCM *init_values_eol = SCM_CDRLOC (init_values);
  523. ceval_letrec_inits (env, init_forms, &init_values_eol);
  524. SCM_SETCDR (SCM_CAR (env), SCM_CDR (init_values));
  525. }
  526. x = SCM_CDR (x);
  527. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  528. goto nontoplevel_begin;
  529. case (ISYMNUM (SCM_IM_LETSTAR)):
  530. x = SCM_CDR (x);
  531. {
  532. SCM bindings = SCM_CAR (x);
  533. if (!scm_is_null (bindings))
  534. {
  535. do
  536. {
  537. SCM name = SCM_CAR (bindings);
  538. SCM init = SCM_CDR (bindings);
  539. env = SCM_EXTEND_ENV (name, EVALCAR (init, env), env);
  540. bindings = SCM_CDR (init);
  541. }
  542. while (!scm_is_null (bindings));
  543. }
  544. }
  545. x = SCM_CDR (x);
  546. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  547. goto nontoplevel_begin;
  548. case (ISYMNUM (SCM_IM_OR)):
  549. x = SCM_CDR (x);
  550. while (!scm_is_null (SCM_CDR (x)))
  551. {
  552. SCM val = EVALCAR (x, env);
  553. if (scm_is_true (val) && !SCM_NILP (val))
  554. RETURN (val);
  555. else
  556. x = SCM_CDR (x);
  557. }
  558. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  559. goto carloop;
  560. case (ISYMNUM (SCM_IM_LAMBDA)):
  561. RETURN (scm_closure (SCM_CDR (x), env));
  562. case (ISYMNUM (SCM_IM_QUOTE)):
  563. RETURN (SCM_CDR (x));
  564. case (ISYMNUM (SCM_IM_SET_X)):
  565. x = SCM_CDR (x);
  566. {
  567. SCM *location;
  568. SCM variable = SCM_CAR (x);
  569. if (SCM_ILOCP (variable))
  570. location = scm_ilookup (variable, env);
  571. else if (SCM_VARIABLEP (variable))
  572. location = SCM_VARIABLE_LOC (variable);
  573. else
  574. {
  575. /* (scm_is_symbol (variable)) is known to be true */
  576. variable = lazy_memoize_variable (variable, env);
  577. SCM_SETCAR (x, variable);
  578. location = SCM_VARIABLE_LOC (variable);
  579. }
  580. x = SCM_CDR (x);
  581. *location = EVALCAR (x, env);
  582. }
  583. RETURN (SCM_UNSPECIFIED);
  584. case (ISYMNUM (SCM_IM_APPLY)):
  585. /* Evaluate the procedure to be applied. */
  586. x = SCM_CDR (x);
  587. proc = EVALCAR (x, env);
  588. PREP_APPLY (proc, SCM_EOL);
  589. /* Evaluate the argument holding the list of arguments */
  590. x = SCM_CDR (x);
  591. arg1 = EVALCAR (x, env);
  592. apply_proc:
  593. /* Go here to tail-apply a procedure. PROC is the procedure and
  594. * ARG1 is the list of arguments. PREP_APPLY must have been called
  595. * before jumping to apply_proc. */
  596. if (SCM_CLOSUREP (proc))
  597. {
  598. SCM formals = SCM_CLOSURE_FORMALS (proc);
  599. #ifdef DEVAL
  600. debug.info->a.args = arg1;
  601. #endif
  602. if (SCM_UNLIKELY (scm_badargsp (formals, arg1)))
  603. scm_wrong_num_args (proc);
  604. ENTER_APPLY;
  605. /* Copy argument list */
  606. if (SCM_NULL_OR_NIL_P (arg1))
  607. env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
  608. else
  609. {
  610. SCM args = scm_list_1 (SCM_CAR (arg1));
  611. SCM tail = args;
  612. arg1 = SCM_CDR (arg1);
  613. while (!SCM_NULL_OR_NIL_P (arg1))
  614. {
  615. SCM new_tail = scm_list_1 (SCM_CAR (arg1));
  616. SCM_SETCDR (tail, new_tail);
  617. tail = new_tail;
  618. arg1 = SCM_CDR (arg1);
  619. }
  620. env = SCM_EXTEND_ENV (formals, args, SCM_ENV (proc));
  621. }
  622. x = SCM_CLOSURE_BODY (proc);
  623. goto nontoplevel_begin;
  624. }
  625. else
  626. {
  627. ENTER_APPLY;
  628. RETURN (SCM_APPLY (proc, arg1, SCM_EOL));
  629. }
  630. case (ISYMNUM (SCM_IM_CONT)):
  631. {
  632. int first;
  633. SCM val = scm_make_continuation (&first);
  634. if (!first)
  635. RETURN (val);
  636. else
  637. {
  638. arg1 = val;
  639. proc = SCM_CDR (x);
  640. proc = EVALCAR (proc, env);
  641. PREP_APPLY (proc, scm_list_1 (arg1));
  642. ENTER_APPLY;
  643. goto evap1;
  644. }
  645. }
  646. case (ISYMNUM (SCM_IM_DELAY)):
  647. RETURN (scm_make_promise (scm_closure (SCM_CDR (x), env)));
  648. #if 0
  649. /* See futures.h for a comment why futures are not enabled.
  650. */
  651. case (ISYMNUM (SCM_IM_FUTURE)):
  652. RETURN (scm_i_make_future (scm_closure (SCM_CDR (x), env)));
  653. #endif
  654. /* PLACEHOLDER for case (ISYMNUM (SCM_IM_DISPATCH)): The following
  655. code (type_dispatch) is intended to be the tail of the case
  656. clause for the internal macro SCM_IM_DISPATCH. Please don't
  657. remove it from this location without discussing it with Mikael
  658. <djurfeldt@nada.kth.se> */
  659. /* The type dispatch code is duplicated below
  660. * (c.f. objects.c:scm_mcache_compute_cmethod) since that
  661. * cuts down execution time for type dispatch to 50%. */
  662. type_dispatch: /* inputs: x, arg1 */
  663. /* Type dispatch means to determine from the types of the function
  664. * arguments (i. e. the 'signature' of the call), which method from
  665. * a generic function is to be called. This process of selecting
  666. * the right method takes some time. To speed it up, guile uses
  667. * caching: Together with the macro call to dispatch the signatures
  668. * of some previous calls to that generic function from the same
  669. * place are stored (in the code!) in a cache that we call the
  670. * 'method cache'. This is done since it is likely, that
  671. * consecutive calls to dispatch from that position in the code will
  672. * have the same signature. Thus, the type dispatch works as
  673. * follows: First, determine a hash value from the signature of the
  674. * actual arguments. Second, use this hash value as an index to
  675. * find that same signature in the method cache stored at this
  676. * position in the code. If found, you have also found the
  677. * corresponding method that belongs to that signature. If the
  678. * signature is not found in the method cache, you have to perform a
  679. * full search over all signatures stored with the generic
  680. * function. */
  681. {
  682. unsigned long int specializers;
  683. unsigned long int hash_value;
  684. unsigned long int cache_end_pos;
  685. unsigned long int mask;
  686. SCM method_cache;
  687. {
  688. SCM z = SCM_CDDR (x);
  689. SCM tmp = SCM_CADR (z);
  690. specializers = scm_to_ulong (SCM_CAR (z));
  691. /* Compute a hash value for searching the method cache. There
  692. * are two variants for computing the hash value, a (rather)
  693. * complicated one, and a simple one. For the complicated one
  694. * explained below, tmp holds a number that is used in the
  695. * computation. */
  696. if (scm_is_simple_vector (tmp))
  697. {
  698. /* This method of determining the hash value is much
  699. * simpler: Set the hash value to zero and just perform a
  700. * linear search through the method cache. */
  701. method_cache = tmp;
  702. mask = (unsigned long int) ((long) -1);
  703. hash_value = 0;
  704. cache_end_pos = SCM_SIMPLE_VECTOR_LENGTH (method_cache);
  705. }
  706. else
  707. {
  708. /* Use the signature of the actual arguments to determine
  709. * the hash value. This is done as follows: Each class has
  710. * an array of random numbers, that are determined when the
  711. * class is created. The integer 'hashset' is an index into
  712. * that array of random numbers. Now, from all classes that
  713. * are part of the signature of the actual arguments, the
  714. * random numbers at index 'hashset' are taken and summed
  715. * up, giving the hash value. The value of 'hashset' is
  716. * stored at the call to dispatch. This allows to have
  717. * different 'formulas' for calculating the hash value at
  718. * different places where dispatch is called. This allows
  719. * to optimize the hash formula at every individual place
  720. * where dispatch is called, such that hopefully the hash
  721. * value that is computed will directly point to the right
  722. * method in the method cache. */
  723. unsigned long int hashset = scm_to_ulong (tmp);
  724. unsigned long int counter = specializers + 1;
  725. SCM tmp_arg = arg1;
  726. hash_value = 0;
  727. while (!scm_is_null (tmp_arg) && counter != 0)
  728. {
  729. SCM class = scm_class_of (SCM_CAR (tmp_arg));
  730. hash_value += SCM_INSTANCE_HASH (class, hashset);
  731. tmp_arg = SCM_CDR (tmp_arg);
  732. counter--;
  733. }
  734. z = SCM_CDDR (z);
  735. method_cache = SCM_CADR (z);
  736. mask = scm_to_ulong (SCM_CAR (z));
  737. hash_value &= mask;
  738. cache_end_pos = hash_value;
  739. }
  740. }
  741. {
  742. /* Search the method cache for a method with a matching
  743. * signature. Start the search at position 'hash_value'. The
  744. * hashing implementation uses linear probing for conflict
  745. * resolution, that is, if the signature in question is not
  746. * found at the starting index in the hash table, the next table
  747. * entry is tried, and so on, until in the worst case the whole
  748. * cache has been searched, but still the signature has not been
  749. * found. */
  750. SCM z;
  751. do
  752. {
  753. SCM args = arg1; /* list of arguments */
  754. z = SCM_SIMPLE_VECTOR_REF (method_cache, hash_value);
  755. while (!scm_is_null (args))
  756. {
  757. /* More arguments than specifiers => CLASS != ENV */
  758. SCM class_of_arg = scm_class_of (SCM_CAR (args));
  759. if (!scm_is_eq (class_of_arg, SCM_CAR (z)))
  760. goto next_method;
  761. args = SCM_CDR (args);
  762. z = SCM_CDR (z);
  763. }
  764. /* Fewer arguments than specifiers => CAR != CLASS */
  765. if (!scm_is_pair (z))
  766. goto apply_vm_cmethod;
  767. else if (!SCM_CLASSP (SCM_CAR (z))
  768. && !scm_is_symbol (SCM_CAR (z)))
  769. goto apply_memoized_cmethod;
  770. next_method:
  771. hash_value = (hash_value + 1) & mask;
  772. } while (hash_value != cache_end_pos);
  773. /* No appropriate method was found in the cache. */
  774. z = scm_memoize_method (x, arg1);
  775. if (scm_is_pair (z))
  776. goto apply_memoized_cmethod;
  777. apply_vm_cmethod:
  778. proc = z;
  779. PREP_APPLY (proc, arg1);
  780. goto apply_proc;
  781. apply_memoized_cmethod: /* inputs: z, arg1 */
  782. {
  783. SCM formals = SCM_CMETHOD_FORMALS (z);
  784. env = SCM_EXTEND_ENV (formals, arg1, SCM_CMETHOD_ENV (z));
  785. x = SCM_CMETHOD_BODY (z);
  786. goto nontoplevel_begin;
  787. }
  788. }
  789. }
  790. case (ISYMNUM (SCM_IM_SLOT_REF)):
  791. x = SCM_CDR (x);
  792. {
  793. SCM instance = EVALCAR (x, env);
  794. unsigned long int slot = SCM_I_INUM (SCM_CDR (x));
  795. RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
  796. }
  797. case (ISYMNUM (SCM_IM_SLOT_SET_X)):
  798. x = SCM_CDR (x);
  799. {
  800. SCM instance = EVALCAR (x, env);
  801. unsigned long int slot = SCM_I_INUM (SCM_CADR (x));
  802. SCM value = EVALCAR (SCM_CDDR (x), env);
  803. SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (value);
  804. RETURN (SCM_UNSPECIFIED);
  805. }
  806. #if SCM_ENABLE_ELISP
  807. case (ISYMNUM (SCM_IM_NIL_COND)):
  808. {
  809. SCM test_form = SCM_CDR (x);
  810. x = SCM_CDR (test_form);
  811. while (!SCM_NULL_OR_NIL_P (x))
  812. {
  813. SCM test_result = EVALCAR (test_form, env);
  814. if (!(scm_is_false (test_result)
  815. || SCM_NULL_OR_NIL_P (test_result)))
  816. {
  817. if (scm_is_eq (SCM_CAR (x), SCM_UNSPECIFIED))
  818. RETURN (test_result);
  819. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  820. goto carloop;
  821. }
  822. else
  823. {
  824. test_form = SCM_CDR (x);
  825. x = SCM_CDR (test_form);
  826. }
  827. }
  828. x = test_form;
  829. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  830. goto carloop;
  831. }
  832. #endif /* SCM_ENABLE_ELISP */
  833. case (ISYMNUM (SCM_IM_BIND)):
  834. {
  835. SCM vars, exps, vals;
  836. x = SCM_CDR (x);
  837. vars = SCM_CAAR (x);
  838. exps = SCM_CDAR (x);
  839. vals = SCM_EOL;
  840. while (!scm_is_null (exps))
  841. {
  842. vals = scm_cons (EVALCAR (exps, env), vals);
  843. exps = SCM_CDR (exps);
  844. }
  845. scm_swap_bindings (vars, vals);
  846. scm_i_set_dynwinds (scm_acons (vars, vals, scm_i_dynwinds ()));
  847. /* Ignore all but the last evaluation result. */
  848. for (x = SCM_CDR (x); !scm_is_null (SCM_CDR (x)); x = SCM_CDR (x))
  849. {
  850. if (scm_is_pair (SCM_CAR (x)))
  851. CEVAL (SCM_CAR (x), env);
  852. }
  853. proc = EVALCAR (x, env);
  854. scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
  855. scm_swap_bindings (vars, vals);
  856. RETURN (proc);
  857. }
  858. case (ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
  859. {
  860. SCM producer;
  861. x = SCM_CDR (x);
  862. producer = EVALCAR (x, env);
  863. x = SCM_CDR (x);
  864. proc = EVALCAR (x, env); /* proc is the consumer. */
  865. arg1 = SCM_APPLY (producer, SCM_EOL, SCM_EOL);
  866. if (SCM_VALUESP (arg1))
  867. {
  868. /* The list of arguments is not copied. Rather, it is assumed
  869. * that this has been done by the 'values' procedure. */
  870. arg1 = scm_struct_ref (arg1, SCM_INUM0);
  871. }
  872. else
  873. {
  874. arg1 = scm_list_1 (arg1);
  875. }
  876. PREP_APPLY (proc, arg1);
  877. goto apply_proc;
  878. }
  879. default:
  880. break;
  881. }
  882. }
  883. else
  884. {
  885. if (SCM_VARIABLEP (SCM_CAR (x)))
  886. proc = SCM_VARIABLE_REF (SCM_CAR (x));
  887. else if (SCM_ILOCP (SCM_CAR (x)))
  888. proc = *scm_ilookup (SCM_CAR (x), env);
  889. else if (scm_is_pair (SCM_CAR (x)))
  890. proc = CEVAL (SCM_CAR (x), env);
  891. else if (scm_is_symbol (SCM_CAR (x)))
  892. {
  893. SCM orig_sym = SCM_CAR (x);
  894. {
  895. SCM *location = scm_lookupcar1 (x, env, 1);
  896. if (location == NULL)
  897. {
  898. /* we have lost the race, start again. */
  899. goto dispatch;
  900. }
  901. proc = *location;
  902. #ifdef DEVAL
  903. if (scm_check_memoize_p && SCM_TRAPS_P)
  904. {
  905. SCM_CLEAR_TRACED_FRAME (debug);
  906. SCM arg1 = scm_make_debugobj (&debug);
  907. SCM retval = SCM_BOOL_T;
  908. SCM_TRAPS_P = 0;
  909. retval = scm_call_4 (SCM_MEMOIZE_HDLR,
  910. scm_sym_memoize_symbol,
  911. arg1, x, env);
  912. /*
  913. do something with retval?
  914. */
  915. SCM_TRAPS_P = 1;
  916. }
  917. #endif
  918. }
  919. if (SCM_MACROP (proc))
  920. {
  921. SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
  922. lookupcar */
  923. handle_a_macro: /* inputs: x, env, proc */
  924. #ifdef DEVAL
  925. /* Set a flag during macro expansion so that macro
  926. application frames can be deleted from the backtrace. */
  927. SCM_SET_MACROEXP (debug);
  928. #endif
  929. arg1 = SCM_APPLY (SCM_MACRO_CODE (proc), x,
  930. scm_cons (env, scm_listofnull));
  931. #ifdef DEVAL
  932. SCM_CLEAR_MACROEXP (debug);
  933. #endif
  934. switch (SCM_MACRO_TYPE (proc))
  935. {
  936. case 3:
  937. case 2:
  938. if (!scm_is_pair (arg1))
  939. arg1 = scm_list_2 (SCM_IM_BEGIN, arg1);
  940. assert (!scm_is_eq (x, SCM_CAR (arg1))
  941. && !scm_is_eq (x, SCM_CDR (arg1)));
  942. #ifdef DEVAL
  943. if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc)))
  944. {
  945. SCM_CRITICAL_SECTION_START;
  946. SCM_SETCAR (x, SCM_CAR (arg1));
  947. SCM_SETCDR (x, SCM_CDR (arg1));
  948. SCM_CRITICAL_SECTION_END;
  949. goto dispatch;
  950. }
  951. /* Prevent memoizing of debug info expression. */
  952. debug.info->e.exp = scm_cons_source (debug.info->e.exp,
  953. SCM_CAR (x),
  954. SCM_CDR (x));
  955. #endif
  956. SCM_CRITICAL_SECTION_START;
  957. SCM_SETCAR (x, SCM_CAR (arg1));
  958. SCM_SETCDR (x, SCM_CDR (arg1));
  959. SCM_CRITICAL_SECTION_END;
  960. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  961. goto loop;
  962. #if SCM_ENABLE_DEPRECATED == 1
  963. case 1:
  964. x = arg1;
  965. if (SCM_NIMP (x))
  966. {
  967. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  968. goto loop;
  969. }
  970. else
  971. RETURN (arg1);
  972. #endif
  973. case 0:
  974. RETURN (arg1);
  975. }
  976. }
  977. }
  978. else
  979. proc = SCM_CAR (x);
  980. if (SCM_MACROP (proc))
  981. goto handle_a_macro;
  982. }
  983. /* When reaching this part of the code, the following is granted: Variable x
  984. * holds the first pair of an expression of the form (<function> arg ...).
  985. * Variable proc holds the object that resulted from the evaluation of
  986. * <function>. In the following, the arguments (if any) will be evaluated,
  987. * and proc will be applied to them. If proc does not really hold a
  988. * function object, this will be signalled as an error on the scheme
  989. * level. If the number of arguments does not match the number of arguments
  990. * that are allowed to be passed to proc, also an error on the scheme level
  991. * will be signalled. */
  992. PREP_APPLY (proc, SCM_EOL);
  993. if (scm_is_null (SCM_CDR (x))) {
  994. ENTER_APPLY;
  995. evap0:
  996. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  997. switch (SCM_TYP7 (proc))
  998. { /* no arguments given */
  999. case scm_tc7_subr_0:
  1000. RETURN (SCM_SUBRF (proc) ());
  1001. case scm_tc7_subr_1o:
  1002. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
  1003. case scm_tc7_lsubr:
  1004. RETURN (SCM_SUBRF (proc) (SCM_EOL));
  1005. case scm_tc7_rpsubr:
  1006. RETURN (SCM_BOOL_T);
  1007. case scm_tc7_asubr:
  1008. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
  1009. case scm_tc7_smob:
  1010. if (!SCM_SMOB_APPLICABLE_P (proc))
  1011. goto badfun;
  1012. RETURN (SCM_SMOB_APPLY_0 (proc));
  1013. case scm_tc7_gsubr:
  1014. #ifdef DEVAL
  1015. debug.info->a.proc = proc;
  1016. debug.info->a.args = SCM_EOL;
  1017. #endif
  1018. RETURN (scm_i_gsubr_apply (proc, SCM_UNDEFINED));
  1019. case scm_tc7_pws:
  1020. proc = SCM_PROCEDURE (proc);
  1021. #ifdef DEVAL
  1022. debug.info->a.proc = proc;
  1023. #endif
  1024. if (!SCM_CLOSUREP (proc))
  1025. goto evap0;
  1026. /* fallthrough */
  1027. case scm_tcs_closures:
  1028. {
  1029. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  1030. if (SCM_UNLIKELY (scm_is_pair (formals)))
  1031. goto wrongnumargs;
  1032. x = SCM_CLOSURE_BODY (proc);
  1033. env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
  1034. goto nontoplevel_begin;
  1035. }
  1036. case scm_tcs_struct:
  1037. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  1038. {
  1039. x = SCM_ENTITY_PROCEDURE (proc);
  1040. arg1 = SCM_EOL;
  1041. goto type_dispatch;
  1042. }
  1043. else if (SCM_I_OPERATORP (proc))
  1044. {
  1045. arg1 = proc;
  1046. proc = (SCM_I_ENTITYP (proc)
  1047. ? SCM_ENTITY_PROCEDURE (proc)
  1048. : SCM_OPERATOR_PROCEDURE (proc));
  1049. #ifdef DEVAL
  1050. debug.info->a.proc = proc;
  1051. debug.info->a.args = scm_list_1 (arg1);
  1052. #endif
  1053. goto evap1;
  1054. }
  1055. else
  1056. goto badfun;
  1057. case scm_tc7_subr_1:
  1058. case scm_tc7_subr_2:
  1059. case scm_tc7_subr_2o:
  1060. case scm_tc7_dsubr:
  1061. case scm_tc7_cxr:
  1062. case scm_tc7_subr_3:
  1063. case scm_tc7_lsubr_2:
  1064. wrongnumargs:
  1065. scm_wrong_num_args (proc);
  1066. default:
  1067. badfun:
  1068. scm_misc_error (NULL, "Wrong type to apply: ~S", scm_list_1 (proc));
  1069. }
  1070. }
  1071. /* must handle macros by here */
  1072. x = SCM_CDR (x);
  1073. if (SCM_LIKELY (scm_is_pair (x)))
  1074. arg1 = EVALCAR (x, env);
  1075. else
  1076. scm_wrong_num_args (proc);
  1077. #ifdef DEVAL
  1078. debug.info->a.args = scm_list_1 (arg1);
  1079. #endif
  1080. x = SCM_CDR (x);
  1081. {
  1082. SCM arg2;
  1083. if (scm_is_null (x))
  1084. {
  1085. ENTER_APPLY;
  1086. evap1: /* inputs: proc, arg1 */
  1087. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  1088. switch (SCM_TYP7 (proc))
  1089. { /* have one argument in arg1 */
  1090. case scm_tc7_subr_2o:
  1091. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  1092. case scm_tc7_subr_1:
  1093. case scm_tc7_subr_1o:
  1094. RETURN (SCM_SUBRF (proc) (arg1));
  1095. case scm_tc7_dsubr:
  1096. if (SCM_I_INUMP (arg1))
  1097. {
  1098. RETURN (scm_from_double (SCM_DSUBRF (proc) ((double) SCM_I_INUM (arg1))));
  1099. }
  1100. else if (SCM_REALP (arg1))
  1101. {
  1102. RETURN (scm_from_double (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  1103. }
  1104. else if (SCM_BIGP (arg1))
  1105. {
  1106. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
  1107. }
  1108. else if (SCM_FRACTIONP (arg1))
  1109. {
  1110. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
  1111. }
  1112. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  1113. SCM_ARG1,
  1114. scm_i_symbol_chars (SCM_SUBR_NAME (proc)));
  1115. case scm_tc7_cxr:
  1116. RETURN (scm_i_chase_pairs (arg1, (scm_t_bits) SCM_SUBRF (proc)));
  1117. case scm_tc7_rpsubr:
  1118. RETURN (SCM_BOOL_T);
  1119. case scm_tc7_asubr:
  1120. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  1121. case scm_tc7_lsubr:
  1122. #ifdef DEVAL
  1123. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  1124. #else
  1125. RETURN (SCM_SUBRF (proc) (scm_list_1 (arg1)));
  1126. #endif
  1127. case scm_tc7_smob:
  1128. if (!SCM_SMOB_APPLICABLE_P (proc))
  1129. goto badfun;
  1130. RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
  1131. case scm_tc7_gsubr:
  1132. #ifdef DEVAL
  1133. debug.info->a.args = scm_cons (arg1, debug.info->a.args);
  1134. debug.info->a.proc = proc;
  1135. #endif
  1136. RETURN (scm_i_gsubr_apply (proc, arg1, SCM_UNDEFINED));
  1137. case scm_tc7_pws:
  1138. proc = SCM_PROCEDURE (proc);
  1139. #ifdef DEVAL
  1140. debug.info->a.proc = proc;
  1141. #endif
  1142. if (!SCM_CLOSUREP (proc))
  1143. goto evap1;
  1144. /* fallthrough */
  1145. case scm_tcs_closures:
  1146. {
  1147. /* clos1: */
  1148. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  1149. if (scm_is_null (formals)
  1150. || (scm_is_pair (formals) && scm_is_pair (SCM_CDR (formals))))
  1151. goto wrongnumargs;
  1152. x = SCM_CLOSURE_BODY (proc);
  1153. #ifdef DEVAL
  1154. env = SCM_EXTEND_ENV (formals,
  1155. debug.info->a.args,
  1156. SCM_ENV (proc));
  1157. #else
  1158. env = SCM_EXTEND_ENV (formals,
  1159. scm_list_1 (arg1),
  1160. SCM_ENV (proc));
  1161. #endif
  1162. goto nontoplevel_begin;
  1163. }
  1164. case scm_tcs_struct:
  1165. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  1166. {
  1167. x = SCM_ENTITY_PROCEDURE (proc);
  1168. #ifdef DEVAL
  1169. arg1 = debug.info->a.args;
  1170. #else
  1171. arg1 = scm_list_1 (arg1);
  1172. #endif
  1173. goto type_dispatch;
  1174. }
  1175. else if (SCM_I_OPERATORP (proc))
  1176. {
  1177. arg2 = arg1;
  1178. arg1 = proc;
  1179. proc = (SCM_I_ENTITYP (proc)
  1180. ? SCM_ENTITY_PROCEDURE (proc)
  1181. : SCM_OPERATOR_PROCEDURE (proc));
  1182. #ifdef DEVAL
  1183. debug.info->a.args = scm_cons (arg1, debug.info->a.args);
  1184. debug.info->a.proc = proc;
  1185. #endif
  1186. goto evap2;
  1187. }
  1188. else
  1189. goto badfun;
  1190. case scm_tc7_subr_2:
  1191. case scm_tc7_subr_0:
  1192. case scm_tc7_subr_3:
  1193. case scm_tc7_lsubr_2:
  1194. scm_wrong_num_args (proc);
  1195. default:
  1196. goto badfun;
  1197. }
  1198. }
  1199. if (SCM_LIKELY (scm_is_pair (x)))
  1200. arg2 = EVALCAR (x, env);
  1201. else
  1202. scm_wrong_num_args (proc);
  1203. { /* have two or more arguments */
  1204. #ifdef DEVAL
  1205. debug.info->a.args = scm_list_2 (arg1, arg2);
  1206. #endif
  1207. x = SCM_CDR (x);
  1208. if (scm_is_null (x)) {
  1209. ENTER_APPLY;
  1210. evap2:
  1211. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  1212. switch (SCM_TYP7 (proc))
  1213. { /* have two arguments */
  1214. case scm_tc7_subr_2:
  1215. case scm_tc7_subr_2o:
  1216. RETURN (SCM_SUBRF (proc) (arg1, arg2));
  1217. case scm_tc7_lsubr:
  1218. #ifdef DEVAL
  1219. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  1220. #else
  1221. RETURN (SCM_SUBRF (proc) (scm_list_2 (arg1, arg2)));
  1222. #endif
  1223. case scm_tc7_lsubr_2:
  1224. RETURN (SCM_SUBRF (proc) (arg1, arg2, SCM_EOL));
  1225. case scm_tc7_rpsubr:
  1226. case scm_tc7_asubr:
  1227. RETURN (SCM_SUBRF (proc) (arg1, arg2));
  1228. case scm_tc7_smob:
  1229. if (!SCM_SMOB_APPLICABLE_P (proc))
  1230. goto badfun;
  1231. RETURN (SCM_SMOB_APPLY_2 (proc, arg1, arg2));
  1232. case scm_tc7_gsubr:
  1233. #ifdef DEVAL
  1234. RETURN (scm_i_gsubr_apply_list (proc, debug.info->a.args));
  1235. #else
  1236. RETURN (scm_i_gsubr_apply (proc, arg1, arg2, SCM_UNDEFINED));
  1237. #endif
  1238. case scm_tcs_struct:
  1239. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  1240. {
  1241. x = SCM_ENTITY_PROCEDURE (proc);
  1242. #ifdef DEVAL
  1243. arg1 = debug.info->a.args;
  1244. #else
  1245. arg1 = scm_list_2 (arg1, arg2);
  1246. #endif
  1247. goto type_dispatch;
  1248. }
  1249. else if (SCM_I_OPERATORP (proc))
  1250. {
  1251. operatorn:
  1252. #ifdef DEVAL
  1253. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  1254. ? SCM_ENTITY_PROCEDURE (proc)
  1255. : SCM_OPERATOR_PROCEDURE (proc),
  1256. scm_cons (proc, debug.info->a.args),
  1257. SCM_EOL));
  1258. #else
  1259. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  1260. ? SCM_ENTITY_PROCEDURE (proc)
  1261. : SCM_OPERATOR_PROCEDURE (proc),
  1262. scm_cons2 (proc, arg1,
  1263. scm_cons (arg2,
  1264. scm_ceval_args (x,
  1265. env,
  1266. proc))),
  1267. SCM_EOL));
  1268. #endif
  1269. }
  1270. else
  1271. goto badfun;
  1272. case scm_tc7_subr_0:
  1273. case scm_tc7_dsubr:
  1274. case scm_tc7_cxr:
  1275. case scm_tc7_subr_1o:
  1276. case scm_tc7_subr_1:
  1277. case scm_tc7_subr_3:
  1278. scm_wrong_num_args (proc);
  1279. default:
  1280. goto badfun;
  1281. case scm_tc7_pws:
  1282. proc = SCM_PROCEDURE (proc);
  1283. #ifdef DEVAL
  1284. debug.info->a.proc = proc;
  1285. #endif
  1286. if (!SCM_CLOSUREP (proc))
  1287. goto evap2;
  1288. /* fallthrough */
  1289. case scm_tcs_closures:
  1290. {
  1291. /* clos2: */
  1292. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  1293. if (scm_is_null (formals)
  1294. || (scm_is_pair (formals)
  1295. && (scm_is_null (SCM_CDR (formals))
  1296. || (scm_is_pair (SCM_CDR (formals))
  1297. && scm_is_pair (SCM_CDDR (formals))))))
  1298. goto wrongnumargs;
  1299. #ifdef DEVAL
  1300. env = SCM_EXTEND_ENV (formals,
  1301. debug.info->a.args,
  1302. SCM_ENV (proc));
  1303. #else
  1304. env = SCM_EXTEND_ENV (formals,
  1305. scm_list_2 (arg1, arg2),
  1306. SCM_ENV (proc));
  1307. #endif
  1308. x = SCM_CLOSURE_BODY (proc);
  1309. goto nontoplevel_begin;
  1310. }
  1311. }
  1312. }
  1313. if (SCM_UNLIKELY (!scm_is_pair (x)))
  1314. scm_wrong_num_args (proc);
  1315. #ifdef DEVAL
  1316. debug.info->a.args = scm_cons2 (arg1, arg2,
  1317. deval_args (x, env, proc,
  1318. SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
  1319. #endif
  1320. ENTER_APPLY;
  1321. evap3:
  1322. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  1323. switch (SCM_TYP7 (proc))
  1324. { /* have 3 or more arguments */
  1325. #ifdef DEVAL
  1326. case scm_tc7_subr_3:
  1327. if (!scm_is_null (SCM_CDR (x)))
  1328. scm_wrong_num_args (proc);
  1329. else
  1330. RETURN (SCM_SUBRF (proc) (arg1, arg2,
  1331. SCM_CADDR (debug.info->a.args)));
  1332. case scm_tc7_asubr:
  1333. arg1 = SCM_SUBRF(proc)(arg1, arg2);
  1334. arg2 = SCM_CDDR (debug.info->a.args);
  1335. do
  1336. {
  1337. arg1 = SCM_SUBRF(proc)(arg1, SCM_CAR (arg2));
  1338. arg2 = SCM_CDR (arg2);
  1339. }
  1340. while (SCM_NIMP (arg2));
  1341. RETURN (arg1);
  1342. case scm_tc7_rpsubr:
  1343. if (scm_is_false (SCM_SUBRF (proc) (arg1, arg2)))
  1344. RETURN (SCM_BOOL_F);
  1345. arg1 = SCM_CDDR (debug.info->a.args);
  1346. do
  1347. {
  1348. if (scm_is_false (SCM_SUBRF (proc) (arg2, SCM_CAR (arg1))))
  1349. RETURN (SCM_BOOL_F);
  1350. arg2 = SCM_CAR (arg1);
  1351. arg1 = SCM_CDR (arg1);
  1352. }
  1353. while (SCM_NIMP (arg1));
  1354. RETURN (SCM_BOOL_T);
  1355. case scm_tc7_lsubr_2:
  1356. RETURN (SCM_SUBRF (proc) (arg1, arg2,
  1357. SCM_CDDR (debug.info->a.args)));
  1358. case scm_tc7_lsubr:
  1359. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  1360. case scm_tc7_smob:
  1361. if (!SCM_SMOB_APPLICABLE_P (proc))
  1362. goto badfun;
  1363. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
  1364. SCM_CDDR (debug.info->a.args)));
  1365. case scm_tc7_gsubr:
  1366. RETURN (scm_i_gsubr_apply_list (proc, debug.info->a.args));
  1367. case scm_tc7_pws:
  1368. proc = SCM_PROCEDURE (proc);
  1369. debug.info->a.proc = proc;
  1370. if (!SCM_CLOSUREP (proc))
  1371. goto evap3;
  1372. /* fallthrough */
  1373. case scm_tcs_closures:
  1374. {
  1375. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  1376. if (scm_is_null (formals)
  1377. || (scm_is_pair (formals)
  1378. && (scm_is_null (SCM_CDR (formals))
  1379. || (scm_is_pair (SCM_CDR (formals))
  1380. && scm_badargsp (SCM_CDDR (formals), x)))))
  1381. goto wrongnumargs;
  1382. SCM_SET_ARGSREADY (debug);
  1383. env = SCM_EXTEND_ENV (formals,
  1384. debug.info->a.args,
  1385. SCM_ENV (proc));
  1386. x = SCM_CLOSURE_BODY (proc);
  1387. goto nontoplevel_begin;
  1388. }
  1389. #else /* DEVAL */
  1390. case scm_tc7_subr_3:
  1391. if (SCM_UNLIKELY (!scm_is_null (SCM_CDR (x))))
  1392. scm_wrong_num_args (proc);
  1393. else
  1394. RETURN (SCM_SUBRF (proc) (arg1, arg2, EVALCAR (x, env)));
  1395. case scm_tc7_asubr:
  1396. arg1 = SCM_SUBRF (proc) (arg1, arg2);
  1397. do
  1398. {
  1399. arg1 = SCM_SUBRF(proc)(arg1, EVALCAR(x, env));
  1400. x = SCM_CDR(x);
  1401. }
  1402. while (!scm_is_null (x));
  1403. RETURN (arg1);
  1404. case scm_tc7_rpsubr:
  1405. if (scm_is_false (SCM_SUBRF (proc) (arg1, arg2)))
  1406. RETURN (SCM_BOOL_F);
  1407. do
  1408. {
  1409. arg1 = EVALCAR (x, env);
  1410. if (scm_is_false (SCM_SUBRF (proc) (arg2, arg1)))
  1411. RETURN (SCM_BOOL_F);
  1412. arg2 = arg1;
  1413. x = SCM_CDR (x);
  1414. }
  1415. while (!scm_is_null (x));
  1416. RETURN (SCM_BOOL_T);
  1417. case scm_tc7_lsubr_2:
  1418. RETURN (SCM_SUBRF (proc) (arg1, arg2, scm_ceval_args (x, env, proc)));
  1419. case scm_tc7_lsubr:
  1420. RETURN (SCM_SUBRF (proc) (scm_cons2 (arg1,
  1421. arg2,
  1422. scm_ceval_args (x, env, proc))));
  1423. case scm_tc7_smob:
  1424. if (!SCM_SMOB_APPLICABLE_P (proc))
  1425. goto badfun;
  1426. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
  1427. scm_ceval_args (x, env, proc)));
  1428. case scm_tc7_gsubr:
  1429. if (scm_is_null (SCM_CDR (x)))
  1430. /* 3 arguments */
  1431. RETURN (scm_i_gsubr_apply (proc, arg1, arg2, EVALCAR (x, env),
  1432. SCM_UNDEFINED));
  1433. else
  1434. RETURN (scm_i_gsubr_apply_list (proc,
  1435. scm_cons2 (arg1, arg2,
  1436. scm_ceval_args (x, env,
  1437. proc))));
  1438. case scm_tc7_pws:
  1439. proc = SCM_PROCEDURE (proc);
  1440. if (!SCM_CLOSUREP (proc))
  1441. goto evap3;
  1442. /* fallthrough */
  1443. case scm_tcs_closures:
  1444. {
  1445. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  1446. if (scm_is_null (formals)
  1447. || (scm_is_pair (formals)
  1448. && (scm_is_null (SCM_CDR (formals))
  1449. || (scm_is_pair (SCM_CDR (formals))
  1450. && scm_badargsp (SCM_CDDR (formals), x)))))
  1451. goto wrongnumargs;
  1452. env = SCM_EXTEND_ENV (formals,
  1453. scm_cons2 (arg1,
  1454. arg2,
  1455. scm_ceval_args (x, env, proc)),
  1456. SCM_ENV (proc));
  1457. x = SCM_CLOSURE_BODY (proc);
  1458. goto nontoplevel_begin;
  1459. }
  1460. #endif /* DEVAL */
  1461. case scm_tcs_struct:
  1462. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  1463. {
  1464. #ifdef DEVAL
  1465. arg1 = debug.info->a.args;
  1466. #else
  1467. arg1 = scm_cons2 (arg1, arg2, scm_ceval_args (x, env, proc));
  1468. #endif
  1469. x = SCM_ENTITY_PROCEDURE (proc);
  1470. goto type_dispatch;
  1471. }
  1472. else if (SCM_I_OPERATORP (proc))
  1473. goto operatorn;
  1474. else
  1475. goto badfun;
  1476. case scm_tc7_subr_2:
  1477. case scm_tc7_subr_1o:
  1478. case scm_tc7_subr_2o:
  1479. case scm_tc7_subr_0:
  1480. case scm_tc7_dsubr:
  1481. case scm_tc7_cxr:
  1482. case scm_tc7_subr_1:
  1483. scm_wrong_num_args (proc);
  1484. default:
  1485. goto badfun;
  1486. }
  1487. }
  1488. }
  1489. #ifdef DEVAL
  1490. exit:
  1491. if (scm_check_exit_p && SCM_TRAPS_P)
  1492. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  1493. {
  1494. SCM_CLEAR_TRACED_FRAME (debug);
  1495. arg1 = scm_make_debugobj (&debug);
  1496. SCM_TRAPS_P = 0;
  1497. arg1 = scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
  1498. SCM_TRAPS_P = 1;
  1499. if (scm_is_pair (arg1) && scm_is_eq (SCM_CAR (arg1), sym_instead))
  1500. proc = SCM_CDR (arg1);
  1501. }
  1502. scm_i_set_last_debug_frame (debug.prev);
  1503. return proc;
  1504. #endif
  1505. }
  1506. /* Apply a function to a list of arguments.
  1507. This function is exported to the Scheme level as taking two
  1508. required arguments and a tail argument, as if it were:
  1509. (lambda (proc arg1 . args) ...)
  1510. Thus, if you just have a list of arguments to pass to a procedure,
  1511. pass the list as ARG1, and '() for ARGS. If you have some fixed
  1512. args, pass the first as ARG1, then cons any remaining fixed args
  1513. onto the front of your argument list, and pass that as ARGS. */
  1514. SCM
  1515. SCM_APPLY (SCM proc, SCM arg1, SCM args)
  1516. {
  1517. #ifdef DEVAL
  1518. scm_t_debug_frame debug;
  1519. scm_t_debug_info debug_vect_body;
  1520. debug.prev = scm_i_last_debug_frame ();
  1521. debug.status = SCM_APPLYFRAME;
  1522. debug.vect = &debug_vect_body;
  1523. debug.vect[0].a.proc = proc;
  1524. debug.vect[0].a.args = SCM_EOL;
  1525. scm_i_set_last_debug_frame (&debug);
  1526. #else
  1527. if (scm_debug_mode_p)
  1528. return scm_dapply (proc, arg1, args);
  1529. #endif
  1530. SCM_ASRTGO (SCM_NIMP (proc), badproc);
  1531. /* If ARGS is the empty list, then we're calling apply with only two
  1532. arguments --- ARG1 is the list of arguments for PROC. Whatever
  1533. the case, futz with things so that ARG1 is the first argument to
  1534. give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
  1535. rest.
  1536. Setting the debug apply frame args this way is pretty messy.
  1537. Perhaps we should store arg1 and args directly in the frame as
  1538. received, and let scm_frame_arguments unpack them, because that's
  1539. a relatively rare operation. This works for now; if the Guile
  1540. developer archives are still around, see Mikael's post of
  1541. 11-Apr-97. */
  1542. if (scm_is_null (args))
  1543. {
  1544. if (scm_is_null (arg1))
  1545. {
  1546. arg1 = SCM_UNDEFINED;
  1547. #ifdef DEVAL
  1548. debug.vect[0].a.args = SCM_EOL;
  1549. #endif
  1550. }
  1551. else
  1552. {
  1553. #ifdef DEVAL
  1554. debug.vect[0].a.args = arg1;
  1555. #endif
  1556. args = SCM_CDR (arg1);
  1557. arg1 = SCM_CAR (arg1);
  1558. }
  1559. }
  1560. else
  1561. {
  1562. args = scm_nconc2last (args);
  1563. #ifdef DEVAL
  1564. debug.vect[0].a.args = scm_cons (arg1, args);
  1565. #endif
  1566. }
  1567. #ifdef DEVAL
  1568. if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
  1569. {
  1570. SCM tmp = scm_make_debugobj (&debug);
  1571. SCM_TRAPS_P = 0;
  1572. scm_call_2 (SCM_ENTER_FRAME_HDLR, scm_sym_enter_frame, tmp);
  1573. SCM_TRAPS_P = 1;
  1574. }
  1575. ENTER_APPLY;
  1576. #endif
  1577. tail:
  1578. switch (SCM_TYP7 (proc))
  1579. {
  1580. case scm_tc7_subr_2o:
  1581. if (SCM_UNLIKELY (SCM_UNBNDP (arg1)))
  1582. scm_wrong_num_args (proc);
  1583. if (scm_is_null (args))
  1584. args = SCM_UNDEFINED;
  1585. else
  1586. {
  1587. if (SCM_UNLIKELY (! scm_is_null (SCM_CDR (args))))
  1588. scm_wrong_num_args (proc);
  1589. args = SCM_CAR (args);
  1590. }
  1591. RETURN (SCM_SUBRF (proc) (arg1, args));
  1592. case scm_tc7_subr_2:
  1593. if (SCM_UNLIKELY (scm_is_null (args) ||
  1594. !scm_is_null (SCM_CDR (args))))
  1595. scm_wrong_num_args (proc);
  1596. args = SCM_CAR (args);
  1597. RETURN (SCM_SUBRF (proc) (arg1, args));
  1598. case scm_tc7_subr_0:
  1599. if (SCM_UNLIKELY (!SCM_UNBNDP (arg1)))
  1600. scm_wrong_num_args (proc);
  1601. else
  1602. RETURN (SCM_SUBRF (proc) ());
  1603. case scm_tc7_subr_1:
  1604. if (SCM_UNLIKELY (SCM_UNBNDP (arg1)))
  1605. scm_wrong_num_args (proc);
  1606. case scm_tc7_subr_1o:
  1607. if (SCM_UNLIKELY (!scm_is_null (args)))
  1608. scm_wrong_num_args (proc);
  1609. else
  1610. RETURN (SCM_SUBRF (proc) (arg1));
  1611. case scm_tc7_dsubr:
  1612. if (SCM_UNLIKELY (SCM_UNBNDP (arg1) || !scm_is_null (args)))
  1613. scm_wrong_num_args (proc);
  1614. if (SCM_I_INUMP (arg1))
  1615. {
  1616. RETURN (scm_from_double (SCM_DSUBRF (proc) ((double) SCM_I_INUM (arg1))));
  1617. }
  1618. else if (SCM_REALP (arg1))
  1619. {
  1620. RETURN (scm_from_double (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  1621. }
  1622. else if (SCM_BIGP (arg1))
  1623. {
  1624. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
  1625. }
  1626. else if (SCM_FRACTIONP (arg1))
  1627. {
  1628. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
  1629. }
  1630. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  1631. SCM_ARG1, scm_i_symbol_chars (SCM_SUBR_NAME (proc)));
  1632. case scm_tc7_cxr:
  1633. if (SCM_UNLIKELY (SCM_UNBNDP (arg1) || !scm_is_null (args)))
  1634. scm_wrong_num_args (proc);
  1635. RETURN (scm_i_chase_pairs (arg1, (scm_t_bits) SCM_SUBRF (proc)));
  1636. case scm_tc7_subr_3:
  1637. if (SCM_UNLIKELY (scm_is_null (args)
  1638. || scm_is_null (SCM_CDR (args))
  1639. || !scm_is_null (SCM_CDDR (args))))
  1640. scm_wrong_num_args (proc);
  1641. else
  1642. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CADR (args)));
  1643. case scm_tc7_lsubr:
  1644. #ifdef DEVAL
  1645. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args));
  1646. #else
  1647. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)));
  1648. #endif
  1649. case scm_tc7_lsubr_2:
  1650. if (SCM_UNLIKELY (!scm_is_pair (args)))
  1651. scm_wrong_num_args (proc);
  1652. else
  1653. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)));
  1654. case scm_tc7_asubr:
  1655. if (scm_is_null (args))
  1656. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  1657. while (SCM_NIMP (args))
  1658. {
  1659. SCM_ASSERT (scm_is_pair (args), args, SCM_ARG2, "apply");
  1660. arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
  1661. args = SCM_CDR (args);
  1662. }
  1663. RETURN (arg1);
  1664. case scm_tc7_rpsubr:
  1665. if (scm_is_null (args))
  1666. RETURN (SCM_BOOL_T);
  1667. while (SCM_NIMP (args))
  1668. {
  1669. SCM_ASSERT (scm_is_pair (args), args, SCM_ARG2, "apply");
  1670. if (scm_is_false (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
  1671. RETURN (SCM_BOOL_F);
  1672. arg1 = SCM_CAR (args);
  1673. args = SCM_CDR (args);
  1674. }
  1675. RETURN (SCM_BOOL_T);
  1676. case scm_tcs_closures:
  1677. #ifdef DEVAL
  1678. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
  1679. #else
  1680. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
  1681. #endif
  1682. if (SCM_UNLIKELY (scm_badargsp (SCM_CLOSURE_FORMALS (proc), arg1)))
  1683. scm_wrong_num_args (proc);
  1684. /* Copy argument list */
  1685. if (SCM_IMP (arg1))
  1686. args = arg1;
  1687. else
  1688. {
  1689. SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
  1690. for (arg1 = SCM_CDR (arg1); scm_is_pair (arg1); arg1 = SCM_CDR (arg1))
  1691. {
  1692. SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED));
  1693. tl = SCM_CDR (tl);
  1694. }
  1695. SCM_SETCDR (tl, arg1);
  1696. }
  1697. args = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
  1698. args,
  1699. SCM_ENV (proc));
  1700. proc = SCM_CLOSURE_BODY (proc);
  1701. again:
  1702. arg1 = SCM_CDR (proc);
  1703. while (!scm_is_null (arg1))
  1704. {
  1705. if (SCM_IMP (SCM_CAR (proc)))
  1706. {
  1707. if (SCM_ISYMP (SCM_CAR (proc)))
  1708. {
  1709. scm_dynwind_begin (0);
  1710. scm_i_dynwind_pthread_mutex_lock (&source_mutex);
  1711. /* check for race condition */
  1712. if (SCM_ISYMP (SCM_CAR (proc)))
  1713. m_expand_body (proc, args);
  1714. scm_dynwind_end ();
  1715. goto again;
  1716. }
  1717. else
  1718. SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc));
  1719. }
  1720. else
  1721. (void) EVAL (SCM_CAR (proc), args);
  1722. proc = arg1;
  1723. arg1 = SCM_CDR (proc);
  1724. }
  1725. RETURN (EVALCAR (proc, args));
  1726. case scm_tc7_smob:
  1727. if (!SCM_SMOB_APPLICABLE_P (proc))
  1728. goto badproc;
  1729. if (SCM_UNBNDP (arg1))
  1730. RETURN (SCM_SMOB_APPLY_0 (proc));
  1731. else if (scm_is_null (args))
  1732. RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
  1733. else if (scm_is_null (SCM_CDR (args)))
  1734. RETURN (SCM_SMOB_APPLY_2 (proc, arg1, SCM_CAR (args)));
  1735. else
  1736. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, SCM_CAR (args), SCM_CDR (args)));
  1737. case scm_tc7_gsubr:
  1738. #ifdef DEVAL
  1739. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  1740. debug.vect[0].a.proc = proc;
  1741. debug.vect[0].a.args = scm_cons (arg1, args);
  1742. #else
  1743. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  1744. #endif
  1745. RETURN (scm_i_gsubr_apply_list (proc, args));
  1746. case scm_tc7_pws:
  1747. proc = SCM_PROCEDURE (proc);
  1748. #ifdef DEVAL
  1749. debug.vect[0].a.proc = proc;
  1750. #endif
  1751. goto tail;
  1752. case scm_tcs_struct:
  1753. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  1754. {
  1755. #ifdef DEVAL
  1756. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  1757. #else
  1758. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  1759. #endif
  1760. RETURN (scm_apply_generic (proc, args));
  1761. }
  1762. else if (SCM_I_OPERATORP (proc))
  1763. {
  1764. /* operator */
  1765. #ifdef DEVAL
  1766. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  1767. #else
  1768. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  1769. #endif
  1770. arg1 = proc;
  1771. proc = (SCM_I_ENTITYP (proc)
  1772. ? SCM_ENTITY_PROCEDURE (proc)
  1773. : SCM_OPERATOR_PROCEDURE (proc));
  1774. #ifdef DEVAL
  1775. debug.vect[0].a.proc = proc;
  1776. debug.vect[0].a.args = scm_cons (arg1, args);
  1777. #endif
  1778. if (SCM_NIMP (proc))
  1779. goto tail;
  1780. else
  1781. goto badproc;
  1782. }
  1783. else
  1784. goto badproc;
  1785. default:
  1786. badproc:
  1787. scm_wrong_type_arg ("apply", SCM_ARG1, proc);
  1788. }
  1789. #ifdef DEVAL
  1790. exit:
  1791. if (scm_check_exit_p && SCM_TRAPS_P)
  1792. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  1793. {
  1794. SCM_CLEAR_TRACED_FRAME (debug);
  1795. arg1 = scm_make_debugobj (&debug);
  1796. SCM_TRAPS_P = 0;
  1797. arg1 = scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
  1798. SCM_TRAPS_P = 1;
  1799. if (scm_is_pair (arg1) && scm_is_eq (SCM_CAR (arg1), sym_instead))
  1800. proc = SCM_CDR (arg1);
  1801. }
  1802. scm_i_set_last_debug_frame (debug.prev);
  1803. return proc;
  1804. #endif
  1805. }