programs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* Copyright (C) 2001, 2009 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 "vm-bootstrap.h"
  24. #include "instructions.h"
  25. #include "modules.h"
  26. #include "programs.h"
  27. #include "procprop.h" // scm_sym_name
  28. #include "srcprop.h" // scm_sym_filename
  29. #include "vm.h"
  30. scm_t_bits scm_tc16_program;
  31. static SCM write_program = SCM_BOOL_F;
  32. SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
  33. (SCM objcode, SCM objtable, SCM free_variables),
  34. "")
  35. #define FUNC_NAME s_scm_make_program
  36. {
  37. SCM_VALIDATE_OBJCODE (1, objcode);
  38. if (SCM_UNLIKELY (SCM_UNBNDP (objtable)))
  39. objtable = SCM_BOOL_F;
  40. else if (scm_is_true (objtable))
  41. SCM_VALIDATE_VECTOR (2, objtable);
  42. if (SCM_UNLIKELY (SCM_UNBNDP (free_variables)))
  43. free_variables = SCM_BOOL_F;
  44. else if (free_variables != SCM_BOOL_F)
  45. SCM_VALIDATE_VECTOR (3, free_variables);
  46. SCM_RETURN_NEWSMOB3 (scm_tc16_program, objcode, objtable, free_variables);
  47. }
  48. #undef FUNC_NAME
  49. static SCM
  50. program_mark (SCM obj)
  51. {
  52. if (scm_is_true (SCM_PROGRAM_OBJTABLE (obj)))
  53. scm_gc_mark (SCM_PROGRAM_OBJTABLE (obj));
  54. if (scm_is_true (SCM_PROGRAM_FREE_VARIABLES (obj)))
  55. scm_gc_mark (SCM_PROGRAM_FREE_VARIABLES (obj));
  56. return SCM_PROGRAM_OBJCODE (obj);
  57. }
  58. static SCM
  59. program_apply (SCM program, SCM args)
  60. {
  61. return scm_vm_apply (scm_the_vm (), program, args);
  62. }
  63. static SCM
  64. program_apply_0 (SCM program)
  65. {
  66. return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
  67. }
  68. static SCM
  69. program_apply_1 (SCM program, SCM a)
  70. {
  71. return scm_c_vm_run (scm_the_vm (), program, &a, 1);
  72. }
  73. static SCM
  74. program_apply_2 (SCM program, SCM a, SCM b)
  75. {
  76. SCM args[2];
  77. args[0] = a;
  78. args[1] = b;
  79. return scm_c_vm_run (scm_the_vm (), program, args, 2);
  80. }
  81. static int
  82. program_print (SCM program, SCM port, scm_print_state *pstate)
  83. {
  84. static int print_error = 0;
  85. if (SCM_FALSEP (write_program) && scm_module_system_booted_p)
  86. write_program = scm_module_local_variable
  87. (scm_c_resolve_module ("system vm program"),
  88. scm_from_locale_symbol ("write-program"));
  89. if (SCM_FALSEP (write_program) || print_error)
  90. return scm_smob_print (program, port, pstate);
  91. print_error = 1;
  92. scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
  93. print_error = 0;
  94. return 1;
  95. }
  96. /*
  97. * Scheme interface
  98. */
  99. SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
  100. (SCM obj),
  101. "")
  102. #define FUNC_NAME s_scm_program_p
  103. {
  104. return SCM_BOOL (SCM_PROGRAM_P (obj));
  105. }
  106. #undef FUNC_NAME
  107. SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
  108. (SCM program),
  109. "")
  110. #define FUNC_NAME s_scm_program_base
  111. {
  112. SCM_VALIDATE_PROGRAM (1, program);
  113. return scm_from_ulong ((unsigned long) SCM_PROGRAM_DATA (program)->base);
  114. }
  115. #undef FUNC_NAME
  116. SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
  117. (SCM program),
  118. "")
  119. #define FUNC_NAME s_scm_program_arity
  120. {
  121. struct scm_objcode *p;
  122. SCM_VALIDATE_PROGRAM (1, program);
  123. p = SCM_PROGRAM_DATA (program);
  124. return scm_list_3 (SCM_I_MAKINUM (p->nargs),
  125. SCM_I_MAKINUM (p->nrest),
  126. SCM_I_MAKINUM (p->nlocs));
  127. }
  128. #undef FUNC_NAME
  129. SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
  130. (SCM program),
  131. "")
  132. #define FUNC_NAME s_scm_program_objects
  133. {
  134. SCM_VALIDATE_PROGRAM (1, program);
  135. return SCM_PROGRAM_OBJTABLE (program);
  136. }
  137. #undef FUNC_NAME
  138. SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
  139. (SCM program),
  140. "")
  141. #define FUNC_NAME s_scm_program_module
  142. {
  143. SCM objs;
  144. SCM_VALIDATE_PROGRAM (1, program);
  145. objs = SCM_PROGRAM_OBJTABLE (program);
  146. return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : 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_BOOL_F, SCM_BOOL_F);
  159. else
  160. return SCM_BOOL_F;
  161. }
  162. #undef FUNC_NAME
  163. SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
  164. (SCM program),
  165. "")
  166. #define FUNC_NAME s_scm_program_bindings
  167. {
  168. SCM meta;
  169. SCM_VALIDATE_PROGRAM (1, program);
  170. meta = scm_program_meta (program);
  171. if (scm_is_false (meta))
  172. return SCM_BOOL_F;
  173. return scm_car (scm_call_0 (meta));
  174. }
  175. #undef FUNC_NAME
  176. SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
  177. (SCM program),
  178. "")
  179. #define FUNC_NAME s_scm_program_sources
  180. {
  181. SCM meta, sources, ret, filename;
  182. SCM_VALIDATE_PROGRAM (1, program);
  183. meta = scm_program_meta (program);
  184. if (scm_is_false (meta))
  185. return SCM_EOL;
  186. filename = SCM_BOOL_F;
  187. ret = SCM_EOL;
  188. for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
  189. sources = scm_cdr (sources))
  190. {
  191. SCM x = scm_car (sources);
  192. if (scm_is_pair (x))
  193. {
  194. if (scm_is_number (scm_car (x)))
  195. {
  196. SCM addr = scm_car (x);
  197. ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
  198. ret);
  199. }
  200. else if (scm_is_eq (scm_car (x), scm_sym_filename))
  201. filename = scm_cdr (x);
  202. }
  203. }
  204. return scm_reverse_x (ret, SCM_UNDEFINED);
  205. }
  206. #undef FUNC_NAME
  207. SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0,
  208. (SCM program),
  209. "")
  210. #define FUNC_NAME s_scm_program_properties
  211. {
  212. SCM meta;
  213. SCM_VALIDATE_PROGRAM (1, program);
  214. meta = scm_program_meta (program);
  215. if (scm_is_false (meta))
  216. return SCM_EOL;
  217. return scm_cddr (scm_call_0 (meta));
  218. }
  219. #undef FUNC_NAME
  220. SCM_DEFINE (scm_program_name, "program-name", 1, 0, 0,
  221. (SCM program),
  222. "")
  223. #define FUNC_NAME s_scm_program_name
  224. {
  225. SCM_VALIDATE_PROGRAM (1, program);
  226. return scm_assq_ref (scm_program_properties (program), scm_sym_name);
  227. }
  228. #undef FUNC_NAME
  229. SCM_DEFINE (scm_program_source, "program-source", 2, 0, 0,
  230. (SCM program, SCM ip),
  231. "")
  232. #define FUNC_NAME s_scm_program_source
  233. {
  234. SCM_VALIDATE_PROGRAM (1, program);
  235. return scm_c_program_source (program, scm_to_size_t (ip));
  236. }
  237. #undef FUNC_NAME
  238. extern SCM
  239. scm_c_program_source (SCM program, size_t ip)
  240. {
  241. SCM sources, source = SCM_BOOL_F;
  242. for (sources = scm_program_sources (program);
  243. !scm_is_null (sources)
  244. && scm_to_size_t (scm_caar (sources)) <= ip;
  245. sources = scm_cdr (sources))
  246. source = scm_car (sources);
  247. return source; /* (addr . (filename . (line . column))) */
  248. }
  249. SCM_DEFINE (scm_program_free_variables, "program-free-variables", 1, 0, 0,
  250. (SCM program),
  251. "")
  252. #define FUNC_NAME s_scm_program_free_variables
  253. {
  254. SCM_VALIDATE_PROGRAM (1, program);
  255. return SCM_PROGRAM_FREE_VARIABLES (program);
  256. }
  257. #undef FUNC_NAME
  258. SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
  259. (SCM program),
  260. "Return a @var{program}'s object code.")
  261. #define FUNC_NAME s_scm_program_objcode
  262. {
  263. SCM_VALIDATE_PROGRAM (1, program);
  264. return SCM_PROGRAM_OBJCODE (program);
  265. }
  266. #undef FUNC_NAME
  267. void
  268. scm_bootstrap_programs (void)
  269. {
  270. scm_tc16_program = scm_make_smob_type ("program", 0);
  271. scm_set_smob_mark (scm_tc16_program, program_mark);
  272. scm_set_smob_apply (scm_tc16_program, program_apply, 0, 0, 1);
  273. scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_0 = program_apply_0;
  274. scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_1 = program_apply_1;
  275. scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_2 = program_apply_2;
  276. scm_set_smob_print (scm_tc16_program, program_print);
  277. scm_c_register_extension ("libguile", "scm_init_programs",
  278. (scm_t_extension_init_func)scm_init_programs, NULL);
  279. }
  280. void
  281. scm_init_programs (void)
  282. {
  283. scm_bootstrap_vm ();
  284. #ifndef SCM_MAGIC_SNARFER
  285. #include "libguile/programs.x"
  286. #endif
  287. }
  288. /*
  289. Local Variables:
  290. c-file-style: "gnu"
  291. End:
  292. */