programs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include "_scm.h"
  23. #include "instructions.h"
  24. #include "modules.h"
  25. #include "programs.h"
  26. #include "procprop.h" /* scm_sym_name */
  27. #include "srcprop.h" /* scm_sym_filename */
  28. #include "vm.h"
  29. static SCM write_program = SCM_BOOL_F;
  30. SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
  31. (SCM objcode, SCM objtable, SCM free_variables),
  32. "")
  33. #define FUNC_NAME s_scm_make_program
  34. {
  35. SCM_VALIDATE_OBJCODE (1, objcode);
  36. if (SCM_UNLIKELY (SCM_UNBNDP (objtable)))
  37. objtable = SCM_BOOL_F;
  38. else if (scm_is_true (objtable))
  39. SCM_VALIDATE_VECTOR (2, objtable);
  40. if (SCM_UNBNDP (free_variables) || scm_is_false (free_variables))
  41. {
  42. SCM ret = scm_words (scm_tc7_program, 3);
  43. SCM_SET_CELL_OBJECT_1 (ret, objcode);
  44. SCM_SET_CELL_OBJECT_2 (ret, objtable);
  45. return ret;
  46. }
  47. else
  48. {
  49. size_t i, len;
  50. SCM ret;
  51. SCM_VALIDATE_VECTOR (3, free_variables);
  52. len = scm_c_vector_length (free_variables);
  53. if (SCM_UNLIKELY (len >> 16))
  54. SCM_OUT_OF_RANGE (3, free_variables);
  55. ret = scm_words (scm_tc7_program | (len<<16), 3 + len);
  56. SCM_SET_CELL_OBJECT_1 (ret, objcode);
  57. SCM_SET_CELL_OBJECT_2 (ret, objtable);
  58. for (i = 0; i < len; i++)
  59. SCM_SET_CELL_OBJECT (ret, 3+i,
  60. SCM_SIMPLE_VECTOR_REF (free_variables, i));
  61. return ret;
  62. }
  63. }
  64. #undef FUNC_NAME
  65. void
  66. scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
  67. {
  68. static int print_error = 0;
  69. if (scm_is_false (write_program) && scm_module_system_booted_p)
  70. write_program = scm_module_local_variable
  71. (scm_c_resolve_module ("system vm program"),
  72. scm_from_latin1_symbol ("write-program"));
  73. if (SCM_PROGRAM_IS_CONTINUATION (program))
  74. {
  75. /* twingliness */
  76. scm_puts_unlocked ("#<continuation ", port);
  77. scm_uintprint (SCM_UNPACK (program), 16, port);
  78. scm_putc_unlocked ('>', port);
  79. }
  80. else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
  81. {
  82. /* twingliness */
  83. scm_puts_unlocked ("#<partial-continuation ", port);
  84. scm_uintprint (SCM_UNPACK (program), 16, port);
  85. scm_putc_unlocked ('>', port);
  86. }
  87. else if (scm_is_false (write_program) || print_error)
  88. {
  89. scm_puts_unlocked ("#<program ", port);
  90. scm_uintprint (SCM_UNPACK (program), 16, port);
  91. scm_putc_unlocked ('>', port);
  92. }
  93. else
  94. {
  95. print_error = 1;
  96. scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
  97. print_error = 0;
  98. }
  99. }
  100. /*
  101. * Scheme interface
  102. */
  103. SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
  104. (SCM obj),
  105. "")
  106. #define FUNC_NAME s_scm_program_p
  107. {
  108. return scm_from_bool (SCM_PROGRAM_P (obj));
  109. }
  110. #undef FUNC_NAME
  111. SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
  112. (SCM program),
  113. "")
  114. #define FUNC_NAME s_scm_program_base
  115. {
  116. const struct scm_objcode *c_objcode;
  117. SCM_VALIDATE_PROGRAM (1, program);
  118. c_objcode = SCM_PROGRAM_DATA (program);
  119. return scm_from_unsigned_integer ((scm_t_bits) SCM_C_OBJCODE_BASE (c_objcode));
  120. }
  121. #undef FUNC_NAME
  122. SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
  123. (SCM program),
  124. "")
  125. #define FUNC_NAME s_scm_program_objects
  126. {
  127. SCM_VALIDATE_PROGRAM (1, program);
  128. return SCM_PROGRAM_OBJTABLE (program);
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
  132. (SCM program),
  133. "")
  134. #define FUNC_NAME s_scm_program_module
  135. {
  136. SCM objs, mod;
  137. SCM_VALIDATE_PROGRAM (1, program);
  138. objs = SCM_PROGRAM_OBJTABLE (program);
  139. /* If a program is the result of compiling GLIL to assembly, then if
  140. it has an objtable, the first entry will be a module. But some
  141. programs are hand-coded trampolines, like boot programs and
  142. primitives and the like. So if a program happens to have a
  143. non-module in the first slot of the objtable, assume that it is
  144. such a trampoline, and just return #f for the module. */
  145. mod = scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
  146. return SCM_MODULEP (mod) ? mod : SCM_BOOL_F;
  147. }
  148. #undef FUNC_NAME
  149. SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
  150. (SCM program),
  151. "")
  152. #define FUNC_NAME s_scm_program_meta
  153. {
  154. SCM metaobj;
  155. SCM_VALIDATE_PROGRAM (1, program);
  156. metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
  157. if (scm_is_true (metaobj))
  158. return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program),
  159. SCM_BOOL_F);
  160. else
  161. return SCM_BOOL_F;
  162. }
  163. #undef FUNC_NAME
  164. SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
  165. (SCM program),
  166. "")
  167. #define FUNC_NAME s_scm_program_bindings
  168. {
  169. SCM meta;
  170. SCM_VALIDATE_PROGRAM (1, program);
  171. meta = scm_program_meta (program);
  172. if (scm_is_false (meta))
  173. return SCM_BOOL_F;
  174. return scm_car (scm_call_0 (meta));
  175. }
  176. #undef FUNC_NAME
  177. SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
  178. (SCM program),
  179. "")
  180. #define FUNC_NAME s_scm_program_sources
  181. {
  182. SCM meta, sources, ret, filename;
  183. SCM_VALIDATE_PROGRAM (1, program);
  184. meta = scm_program_meta (program);
  185. if (scm_is_false (meta))
  186. return SCM_EOL;
  187. filename = SCM_BOOL_F;
  188. ret = SCM_EOL;
  189. for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
  190. sources = scm_cdr (sources))
  191. {
  192. SCM x = scm_car (sources);
  193. if (scm_is_pair (x))
  194. {
  195. if (scm_is_number (scm_car (x)))
  196. {
  197. SCM addr = scm_car (x);
  198. ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
  199. ret);
  200. }
  201. else if (scm_is_eq (scm_car (x), scm_sym_filename))
  202. filename = scm_cdr (x);
  203. }
  204. }
  205. return scm_reverse_x (ret, SCM_UNDEFINED);
  206. }
  207. #undef FUNC_NAME
  208. SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0,
  209. (SCM program),
  210. "")
  211. #define FUNC_NAME s_scm_program_arities
  212. {
  213. SCM meta;
  214. SCM_VALIDATE_PROGRAM (1, program);
  215. meta = scm_program_meta (program);
  216. if (scm_is_false (meta))
  217. return SCM_BOOL_F;
  218. return scm_caddr (scm_call_0 (meta));
  219. }
  220. #undef FUNC_NAME
  221. SCM
  222. scm_i_program_properties (SCM program)
  223. #define FUNC_NAME "%program-properties"
  224. {
  225. SCM meta;
  226. SCM_VALIDATE_PROGRAM (1, program);
  227. meta = scm_program_meta (program);
  228. if (scm_is_false (meta))
  229. return SCM_EOL;
  230. return scm_cdddr (scm_call_0 (meta));
  231. }
  232. #undef FUNC_NAME
  233. static SCM
  234. program_source (SCM program, size_t ip, SCM sources)
  235. {
  236. SCM source = SCM_BOOL_F;
  237. while (!scm_is_null (sources)
  238. && scm_to_size_t (scm_caar (sources)) <= ip)
  239. {
  240. source = scm_car (sources);
  241. sources = scm_cdr (sources);
  242. }
  243. return source; /* (addr . (filename . (line . column))) */
  244. }
  245. SCM_DEFINE (scm_program_source, "program-source", 2, 1, 0,
  246. (SCM program, SCM ip, SCM sources),
  247. "")
  248. #define FUNC_NAME s_scm_program_source
  249. {
  250. SCM_VALIDATE_PROGRAM (1, program);
  251. if (SCM_UNBNDP (sources))
  252. sources = scm_program_sources (program);
  253. return program_source (program, scm_to_size_t (ip), sources);
  254. }
  255. #undef FUNC_NAME
  256. extern SCM
  257. scm_c_program_source (SCM program, size_t ip)
  258. {
  259. return program_source (program, ip, scm_program_sources (program));
  260. }
  261. SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0,
  262. (SCM program),
  263. "")
  264. #define FUNC_NAME s_scm_program_num_free_variables
  265. {
  266. SCM_VALIDATE_PROGRAM (1, program);
  267. return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program));
  268. }
  269. #undef FUNC_NAME
  270. SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0,
  271. (SCM program, SCM i),
  272. "")
  273. #define FUNC_NAME s_scm_program_free_variable_ref
  274. {
  275. unsigned long idx;
  276. SCM_VALIDATE_PROGRAM (1, program);
  277. SCM_VALIDATE_ULONG_COPY (2, i, idx);
  278. if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
  279. SCM_OUT_OF_RANGE (2, i);
  280. return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx);
  281. }
  282. #undef FUNC_NAME
  283. SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0,
  284. (SCM program, SCM i, SCM x),
  285. "")
  286. #define FUNC_NAME s_scm_program_free_variable_set_x
  287. {
  288. unsigned long idx;
  289. SCM_VALIDATE_PROGRAM (1, program);
  290. SCM_VALIDATE_ULONG_COPY (2, i, idx);
  291. if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
  292. SCM_OUT_OF_RANGE (2, i);
  293. SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
  294. return SCM_UNSPECIFIED;
  295. }
  296. #undef FUNC_NAME
  297. SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
  298. (SCM program),
  299. "Return a @var{program}'s object code.")
  300. #define FUNC_NAME s_scm_program_objcode
  301. {
  302. SCM_VALIDATE_PROGRAM (1, program);
  303. return SCM_PROGRAM_OBJCODE (program);
  304. }
  305. #undef FUNC_NAME
  306. /* procedure-minimum-arity support. */
  307. static void
  308. parse_arity (SCM arity, int *req, int *opt, int *rest)
  309. {
  310. SCM x = scm_cddr (arity);
  311. if (scm_is_pair (x))
  312. {
  313. *req = scm_to_int (scm_car (x));
  314. x = scm_cdr (x);
  315. if (scm_is_pair (x))
  316. {
  317. *opt = scm_to_int (scm_car (x));
  318. x = scm_cdr (x);
  319. if (scm_is_pair (x))
  320. *rest = scm_is_true (scm_car (x));
  321. else
  322. *rest = 0;
  323. }
  324. else
  325. *opt = *rest = 0;
  326. }
  327. else
  328. *req = *opt = *rest = 0;
  329. }
  330. int
  331. scm_i_program_arity (SCM program, int *req, int *opt, int *rest)
  332. {
  333. SCM arities;
  334. arities = scm_program_arities (program);
  335. if (!scm_is_pair (arities))
  336. return 0;
  337. parse_arity (scm_car (arities), req, opt, rest);
  338. arities = scm_cdr (arities);
  339. for (; scm_is_pair (arities); arities = scm_cdr (arities))
  340. {
  341. int thisreq, thisopt, thisrest;
  342. parse_arity (scm_car (arities), &thisreq, &thisopt, &thisrest);
  343. if (thisreq < *req
  344. || (thisreq == *req
  345. && ((thisrest && (!*rest || thisopt > *opt))
  346. || (!thisrest && !*rest && thisopt > *opt))))
  347. {
  348. *req = thisreq;
  349. *opt = thisopt;
  350. *rest = thisrest;
  351. }
  352. }
  353. return 1;
  354. }
  355. void
  356. scm_bootstrap_programs (void)
  357. {
  358. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  359. "scm_init_programs",
  360. (scm_t_extension_init_func)scm_init_programs, NULL);
  361. }
  362. void
  363. scm_init_programs (void)
  364. {
  365. #ifndef SCM_MAGIC_SNARFER
  366. #include "libguile/programs.x"
  367. #endif
  368. }
  369. /*
  370. Local Variables:
  371. c-file-style: "gnu"
  372. End:
  373. */