frames.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef _SCM_FRAMES_H_
  19. #define _SCM_FRAMES_H_
  20. #include <libguile.h>
  21. #include "programs.h"
  22. /*
  23. * VM frames
  24. */
  25. /*
  26. * It's a little confusing, but there are two representations of frames in this
  27. * file: frame pointers and Scheme objects wrapping those frame pointers. The
  28. * former uses the SCM_FRAME_... macro prefix, the latter SCM_VM_FRAME_..
  29. * prefix.
  30. *
  31. * The confusing thing is that only Scheme frame objects have functions that use
  32. * them, and they use the scm_frame_.. prefix. Hysterical raisins.
  33. */
  34. /* VM Frame Layout
  35. ---------------
  36. | ... |
  37. | Intermed. val. 0 | <- fp + nargs + nlocs
  38. +------------------+
  39. | Local variable 1 |
  40. | Local variable 0 | <- fp + nargs
  41. | Argument 1 |
  42. | Argument 0 | <- fp = SCM_FRAME_STACK_ADDRESS (fp)
  43. | Program | <- fp - 1
  44. +==================+
  45. | Return address | <- SCM_FRAME_UPPER_ADDRESS (fp)
  46. | MV return address|
  47. | Dynamic link | <- fp - 4 = SCM_FRAME_DATA_ADDRESS (fp) = SCM_FRAME_LOWER_ADDRESS (fp)
  48. +==================+
  49. | |
  50. As can be inferred from this drawing, it is assumed that
  51. `sizeof (SCM *) == sizeof (SCM)', since pointers (the `link' parts) are
  52. assumed to be as long as SCM objects. */
  53. /* This structure maps to the contents of a VM stack frame. It can
  54. alias a frame directly. */
  55. struct scm_vm_frame
  56. {
  57. SCM *dynamic_link;
  58. scm_t_uint8 *mv_return_address;
  59. scm_t_uint8 *return_address;
  60. SCM program;
  61. SCM stack[1]; /* Variable-length */
  62. };
  63. #define SCM_FRAME_STRUCT(fp) \
  64. ((struct scm_vm_frame *) SCM_FRAME_DATA_ADDRESS (fp))
  65. #define SCM_FRAME_DATA_ADDRESS(fp) (((SCM *) (fp)) - 4)
  66. #define SCM_FRAME_STACK_ADDRESS(fp) (SCM_FRAME_STRUCT (fp)->stack)
  67. #define SCM_FRAME_UPPER_ADDRESS(fp) ((SCM*)&SCM_FRAME_STRUCT (fp)->return_address)
  68. #define SCM_FRAME_LOWER_ADDRESS(fp) ((SCM*)SCM_FRAME_STRUCT (fp))
  69. #define SCM_FRAME_BYTE_CAST(x) ((scm_t_uint8 *) SCM_UNPACK (x))
  70. #define SCM_FRAME_STACK_CAST(x) ((SCM *) SCM_UNPACK (x))
  71. #define SCM_FRAME_RETURN_ADDRESS(fp) \
  72. (SCM_FRAME_STRUCT (fp)->return_address)
  73. #define SCM_FRAME_SET_RETURN_ADDRESS(fp, ra) \
  74. SCM_FRAME_STRUCT (fp)->return_address = (ra)
  75. #define SCM_FRAME_MV_RETURN_ADDRESS(fp) \
  76. (SCM_FRAME_STRUCT (fp)->mv_return_address)
  77. #define SCM_FRAME_SET_MV_RETURN_ADDRESS(fp, mvra) \
  78. SCM_FRAME_STRUCT (fp)->mv_return_address = (mvra)
  79. #define SCM_FRAME_DYNAMIC_LINK(fp) \
  80. (SCM_FRAME_STRUCT (fp)->dynamic_link)
  81. #define SCM_FRAME_SET_DYNAMIC_LINK(fp, dl) \
  82. SCM_FRAME_DYNAMIC_LINK (fp) = (dl)
  83. #define SCM_FRAME_VARIABLE(fp,i) \
  84. (SCM_FRAME_STRUCT (fp)->stack[i])
  85. #define SCM_FRAME_PROGRAM(fp) \
  86. (SCM_FRAME_STRUCT (fp)->program)
  87. /*
  88. * Heap frames
  89. */
  90. struct scm_frame
  91. {
  92. SCM stack_holder;
  93. SCM *fp;
  94. SCM *sp;
  95. scm_t_uint8 *ip;
  96. scm_t_ptrdiff offset;
  97. };
  98. #define SCM_VM_FRAME_P(x) (SCM_HAS_TYP7 (x, scm_tc7_frame))
  99. #define SCM_VM_FRAME_DATA(x) ((struct scm_frame*)SCM_CELL_WORD_1 (x))
  100. #define SCM_VM_FRAME_STACK_HOLDER(f) SCM_VM_FRAME_DATA(f)->stack_holder
  101. #define SCM_VM_FRAME_FP(f) SCM_VM_FRAME_DATA(f)->fp
  102. #define SCM_VM_FRAME_SP(f) SCM_VM_FRAME_DATA(f)->sp
  103. #define SCM_VM_FRAME_IP(f) SCM_VM_FRAME_DATA(f)->ip
  104. #define SCM_VM_FRAME_OFFSET(f) SCM_VM_FRAME_DATA(f)->offset
  105. #define SCM_VALIDATE_VM_FRAME(p,x) SCM_MAKE_VALIDATE (p, x, VM_FRAME_P)
  106. SCM_API SCM scm_c_make_frame (SCM stack_holder, SCM *fp, SCM *sp,
  107. scm_t_uint8 *ip, scm_t_ptrdiff offset);
  108. SCM_API SCM scm_frame_p (SCM obj);
  109. SCM_API SCM scm_frame_procedure (SCM frame);
  110. SCM_API SCM scm_frame_arguments (SCM frame);
  111. SCM_API SCM scm_frame_source (SCM frame);
  112. SCM_API SCM scm_frame_num_locals (SCM frame);
  113. SCM_API SCM scm_frame_local_ref (SCM frame, SCM index);
  114. SCM_API SCM scm_frame_local_set_x (SCM frame, SCM index, SCM val);
  115. SCM_API SCM scm_frame_address (SCM frame);
  116. SCM_API SCM scm_frame_stack_pointer (SCM frame);
  117. SCM_API SCM scm_frame_instruction_pointer (SCM frame);
  118. SCM_API SCM scm_frame_return_address (SCM frame);
  119. SCM_API SCM scm_frame_mv_return_address (SCM frame);
  120. SCM_API SCM scm_frame_dynamic_link (SCM frame);
  121. SCM_API SCM scm_frame_previous (SCM frame);
  122. SCM_INTERNAL void scm_i_frame_print (SCM frame, SCM port,
  123. scm_print_state *pstate);
  124. SCM_INTERNAL void scm_init_frames (void);
  125. #endif /* _SCM_FRAMES_H_ */
  126. /*
  127. Local Variables:
  128. c-file-style: "gnu"
  129. End:
  130. */