frames.c 12 KB

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