frames.c 12 KB

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