frames.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 <stdlib.h>
  22. #include <string.h>
  23. #include "_scm.h"
  24. #include "frames.h"
  25. #include "vm.h"
  26. SCM
  27. scm_c_make_frame (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
  28. {
  29. struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame),
  30. "vmframe");
  31. p->stack_holder = frame->stack_holder;
  32. p->fp_offset = frame->fp_offset;
  33. p->sp_offset = frame->sp_offset;
  34. p->ip = frame->ip;
  35. return scm_cell (scm_tc7_frame | (kind << 8), (scm_t_bits)p);
  36. }
  37. void
  38. scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate)
  39. {
  40. scm_puts ("#<frame ", port);
  41. scm_uintprint (SCM_UNPACK (frame), 16, port);
  42. if (scm_module_system_booted_p)
  43. {
  44. SCM name = scm_frame_procedure_name (frame);
  45. if (scm_is_true (name))
  46. {
  47. scm_putc (' ', port);
  48. scm_write (name, port);
  49. }
  50. }
  51. /* Don't write args, they can be ridiculously long. */
  52. scm_puts (">", port);
  53. }
  54. static union scm_vm_stack_element*
  55. frame_stack_top (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
  56. {
  57. switch (kind)
  58. {
  59. case SCM_VM_FRAME_KIND_CONT:
  60. {
  61. struct scm_vm_cont *cont = frame->stack_holder;
  62. return cont->stack_bottom + cont->stack_size;
  63. }
  64. case SCM_VM_FRAME_KIND_VM:
  65. return ((struct scm_vm *) frame->stack_holder)->stack_top;
  66. default:
  67. abort ();
  68. }
  69. }
  70. union scm_vm_stack_element*
  71. scm_i_frame_stack_top (SCM frame)
  72. #define FUNC_NAME "frame-stack-top"
  73. {
  74. SCM_VALIDATE_VM_FRAME (1, frame);
  75. return frame_stack_top (SCM_VM_FRAME_KIND (frame),
  76. SCM_VM_FRAME_DATA (frame));
  77. }
  78. #undef FUNC_NAME
  79. /* Scheme interface */
  80. SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
  81. (SCM obj),
  82. "")
  83. #define FUNC_NAME s_scm_frame_p
  84. {
  85. return scm_from_bool (SCM_VM_FRAME_P (obj));
  86. }
  87. #undef FUNC_NAME
  88. /* Retrieve the local in slot 0, which may or may not actually be a
  89. procedure, and may or may not actually be the procedure being
  90. applied. If you want the procedure, look it up from the IP. */
  91. SCM
  92. scm_c_frame_closure (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
  93. {
  94. union scm_vm_stack_element *fp, *sp;
  95. fp = frame_stack_top (kind, frame) - frame->fp_offset;
  96. sp = frame_stack_top (kind, frame) - frame->sp_offset;
  97. if (SCM_FRAME_NUM_LOCALS (fp, sp) > 0)
  98. return SCM_FRAME_LOCAL (fp, 0);
  99. return SCM_BOOL_F;
  100. }
  101. static SCM frame_procedure_name_var;
  102. static void
  103. init_frame_procedure_name_var (void)
  104. {
  105. frame_procedure_name_var
  106. = scm_c_private_lookup ("system vm frame", "frame-procedure-name");
  107. }
  108. SCM_DEFINE (scm_frame_procedure_name, "frame-procedure-name", 1, 0, 0,
  109. (SCM frame),
  110. "")
  111. #define FUNC_NAME s_scm_frame_procedure_name
  112. {
  113. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  114. scm_i_pthread_once (&once, init_frame_procedure_name_var);
  115. SCM_VALIDATE_VM_FRAME (1, frame);
  116. return scm_call_1 (scm_variable_ref (frame_procedure_name_var), frame);
  117. }
  118. #undef FUNC_NAME
  119. static SCM frame_arguments_var;
  120. static void
  121. init_frame_arguments_var (void)
  122. {
  123. frame_arguments_var
  124. = scm_c_private_lookup ("system vm frame", "frame-arguments");
  125. }
  126. SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
  127. (SCM frame),
  128. "")
  129. #define FUNC_NAME s_scm_frame_arguments
  130. {
  131. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  132. scm_i_pthread_once (&once, init_frame_arguments_var);
  133. SCM_VALIDATE_VM_FRAME (1, frame);
  134. return scm_call_1 (scm_variable_ref (frame_arguments_var), frame);
  135. }
  136. #undef FUNC_NAME
  137. static SCM frame_call_representation_var;
  138. static void
  139. init_frame_call_representation_var (void)
  140. {
  141. frame_call_representation_var
  142. = scm_c_private_lookup ("system vm frame", "frame-call-representation");
  143. }
  144. SCM scm_frame_call_representation (SCM frame)
  145. #define FUNC_NAME "frame-call-representation"
  146. {
  147. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  148. scm_i_pthread_once (&once, init_frame_call_representation_var);
  149. SCM_VALIDATE_VM_FRAME (1, frame);
  150. return scm_call_1 (scm_variable_ref (frame_call_representation_var), frame);
  151. }
  152. #undef FUNC_NAME
  153. SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
  154. (SCM frame),
  155. "")
  156. #define FUNC_NAME s_scm_frame_source
  157. {
  158. SCM_VALIDATE_VM_FRAME (1, frame);
  159. return scm_find_source_for_addr (scm_frame_instruction_pointer (frame));
  160. }
  161. #undef FUNC_NAME
  162. static const char s_scm_frame_num_locals[] = "frame-num-locals";
  163. static SCM
  164. scm_frame_num_locals (SCM frame)
  165. #define FUNC_NAME s_scm_frame_num_locals
  166. {
  167. union scm_vm_stack_element *fp, *sp;
  168. SCM_VALIDATE_VM_FRAME (1, frame);
  169. fp = SCM_VM_FRAME_FP (frame);
  170. sp = SCM_VM_FRAME_SP (frame);
  171. return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp));
  172. }
  173. #undef FUNC_NAME
  174. enum stack_item_representation
  175. {
  176. STACK_ITEM_SCM = 0,
  177. STACK_ITEM_F64 = 1,
  178. STACK_ITEM_U64 = 2,
  179. STACK_ITEM_S64 = 3
  180. };
  181. static enum stack_item_representation
  182. scm_to_stack_item_representation (SCM x, const char *subr, int pos)
  183. {
  184. if (scm_is_eq (x, scm_from_latin1_symbol ("scm")))
  185. return STACK_ITEM_SCM;
  186. if (scm_is_eq (x, scm_from_latin1_symbol ("f64")))
  187. return STACK_ITEM_F64;
  188. if (scm_is_eq (x, scm_from_latin1_symbol ("u64")))
  189. return STACK_ITEM_U64;
  190. if (scm_is_eq (x, scm_from_latin1_symbol ("s64")))
  191. return STACK_ITEM_S64;
  192. scm_wrong_type_arg (subr, pos, x);
  193. return 0; /* Not reached. */
  194. }
  195. static const char s_scm_frame_local_ref[] = "frame-local-ref";
  196. static SCM
  197. scm_frame_local_ref (SCM frame, SCM index, SCM representation)
  198. #define FUNC_NAME s_scm_frame_local_ref
  199. {
  200. union scm_vm_stack_element *fp, *sp;
  201. unsigned int i;
  202. enum stack_item_representation repr;
  203. SCM_VALIDATE_VM_FRAME (1, frame);
  204. SCM_VALIDATE_UINT_COPY (2, index, i);
  205. repr = scm_to_stack_item_representation (representation, FUNC_NAME, SCM_ARG3);
  206. fp = SCM_VM_FRAME_FP (frame);
  207. sp = SCM_VM_FRAME_SP (frame);
  208. if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
  209. {
  210. union scm_vm_stack_element *item = SCM_FRAME_SLOT (fp, i);
  211. switch (repr)
  212. {
  213. case STACK_ITEM_SCM:
  214. return item->as_scm;
  215. case STACK_ITEM_F64:
  216. return scm_from_double (item->as_f64);
  217. case STACK_ITEM_U64:
  218. return scm_from_uint64 (item->as_u64);
  219. case STACK_ITEM_S64:
  220. return scm_from_int64 (item->as_s64);
  221. default:
  222. abort();
  223. }
  224. }
  225. SCM_OUT_OF_RANGE (SCM_ARG2, index);
  226. }
  227. #undef FUNC_NAME
  228. static const char s_scm_frame_local_set_x[] = "frame-local-set!";
  229. static SCM
  230. scm_frame_local_set_x (SCM frame, SCM index, SCM val, SCM representation)
  231. #define FUNC_NAME s_scm_frame_local_set_x
  232. {
  233. union scm_vm_stack_element *fp, *sp;
  234. unsigned int i;
  235. enum stack_item_representation repr;
  236. SCM_VALIDATE_VM_FRAME (1, frame);
  237. SCM_VALIDATE_UINT_COPY (2, index, i);
  238. repr = scm_to_stack_item_representation (representation, FUNC_NAME, SCM_ARG3);
  239. fp = SCM_VM_FRAME_FP (frame);
  240. sp = SCM_VM_FRAME_SP (frame);
  241. if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
  242. {
  243. union scm_vm_stack_element *item = SCM_FRAME_SLOT (fp, i);
  244. switch (repr)
  245. {
  246. case STACK_ITEM_SCM:
  247. item->as_scm = val;
  248. break;
  249. case STACK_ITEM_F64:
  250. item->as_f64 = scm_to_double (val);
  251. break;
  252. case STACK_ITEM_U64:
  253. item->as_u64 = scm_to_uint64 (val);
  254. break;
  255. case STACK_ITEM_S64:
  256. item->as_s64 = scm_to_int64 (val);
  257. break;
  258. default:
  259. abort();
  260. }
  261. return SCM_UNSPECIFIED;
  262. }
  263. SCM_OUT_OF_RANGE (SCM_ARG2, index);
  264. }
  265. #undef FUNC_NAME
  266. SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
  267. (SCM frame),
  268. "Return the frame pointer for @var{frame}.")
  269. #define FUNC_NAME s_scm_frame_address
  270. {
  271. SCM_VALIDATE_VM_FRAME (1, frame);
  272. return scm_from_ptrdiff_t (SCM_VM_FRAME_FP_OFFSET (frame));
  273. }
  274. #undef FUNC_NAME
  275. SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
  276. (SCM frame),
  277. "")
  278. #define FUNC_NAME s_scm_frame_stack_pointer
  279. {
  280. SCM_VALIDATE_VM_FRAME (1, frame);
  281. return scm_from_ptrdiff_t (SCM_VM_FRAME_SP_OFFSET (frame));
  282. }
  283. #undef FUNC_NAME
  284. SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
  285. (SCM frame),
  286. "")
  287. #define FUNC_NAME s_scm_frame_instruction_pointer
  288. {
  289. SCM_VALIDATE_VM_FRAME (1, frame);
  290. return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame));
  291. }
  292. #undef FUNC_NAME
  293. SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
  294. (SCM frame),
  295. "")
  296. #define FUNC_NAME s_scm_frame_return_address
  297. {
  298. SCM_VALIDATE_VM_FRAME (1, frame);
  299. return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS
  300. (SCM_VM_FRAME_FP (frame))));
  301. }
  302. #undef FUNC_NAME
  303. SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
  304. (SCM frame),
  305. "")
  306. #define FUNC_NAME s_scm_frame_dynamic_link
  307. {
  308. SCM_VALIDATE_VM_FRAME (1, frame);
  309. /* fixme: munge fp if holder is a continuation */
  310. return scm_from_uintptr_t
  311. ((scm_t_uintptr)
  312. SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame)));
  313. }
  314. #undef FUNC_NAME
  315. int
  316. scm_c_frame_previous (enum scm_vm_frame_kind kind, struct scm_frame *frame)
  317. {
  318. union scm_vm_stack_element *this_fp, *new_fp, *new_sp;
  319. union scm_vm_stack_element *stack_top = frame_stack_top (kind, frame);
  320. again:
  321. this_fp = stack_top - frame->fp_offset;
  322. if (this_fp == stack_top)
  323. return 0;
  324. new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
  325. if (new_fp >= stack_top)
  326. return 0;
  327. new_sp = SCM_FRAME_PREVIOUS_SP (this_fp);
  328. frame->fp_offset = stack_top - new_fp;
  329. frame->sp_offset = stack_top - new_sp;
  330. frame->ip = SCM_FRAME_RETURN_ADDRESS (this_fp);
  331. if (scm_i_vm_is_boot_continuation_code (frame->ip))
  332. goto again;
  333. return 1;
  334. }
  335. SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
  336. (SCM frame),
  337. "")
  338. #define FUNC_NAME s_scm_frame_previous
  339. {
  340. enum scm_vm_frame_kind kind;
  341. struct scm_frame tmp;
  342. SCM_VALIDATE_VM_FRAME (1, frame);
  343. kind = SCM_VM_FRAME_KIND (frame);
  344. memcpy (&tmp, SCM_VM_FRAME_DATA (frame), sizeof tmp);
  345. if (!scm_c_frame_previous (SCM_VM_FRAME_KIND (frame), &tmp))
  346. return SCM_BOOL_F;
  347. return scm_c_make_frame (kind, &tmp);
  348. }
  349. #undef FUNC_NAME
  350. static void
  351. scm_init_frames_builtins (void *unused)
  352. {
  353. scm_c_define_gsubr (s_scm_frame_num_locals, 1, 0, 0,
  354. (scm_t_subr) scm_frame_num_locals);
  355. scm_c_define_gsubr (s_scm_frame_local_ref, 3, 0, 0,
  356. (scm_t_subr) scm_frame_local_ref);
  357. scm_c_define_gsubr (s_scm_frame_local_set_x, 4, 0, 0,
  358. (scm_t_subr) scm_frame_local_set_x);
  359. }
  360. void
  361. scm_init_frames (void)
  362. {
  363. #ifndef SCM_MAGIC_SNARFER
  364. #include "libguile/frames.x"
  365. #endif
  366. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  367. "scm_init_frames_builtins",
  368. scm_init_frames_builtins,
  369. NULL);
  370. }
  371. /*
  372. Local Variables:
  373. c-file-style: "gnu"
  374. End:
  375. */