dynstack.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* classes: h_files */
  2. #ifndef SCM_DYNSTACK_H
  3. #define SCM_DYNSTACK_H
  4. /* Copyright (C) 2012, 2013 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include "libguile/__scm.h"
  22. typedef struct
  23. {
  24. scm_t_bits *base;
  25. scm_t_bits *top;
  26. scm_t_bits *limit;
  27. } scm_t_dynstack;
  28. /* Items on the dynstack are preceded by two-word headers, giving the
  29. offset of the preceding item (or 0 if there is none) and the type,
  30. flags, and length of the following dynstack entry, in words. In
  31. addition, there is a "null header" at the top of the stack,
  32. indicating the length of the previous item, but with a tag of zero.
  33. For example, consider an empty dynstack, with a capacity of 6 words:
  34. +----------+----------+ +
  35. |prev=0 |tag=0 | |
  36. +----------+----------+ +
  37. ^base ^top limit^
  38. Now we evaluate (dynamic-wind enter thunk leave). That will result
  39. in a dynstack of:
  40. / the len=2 words \
  41. +----------+----------+----------+----------+----------+----------+
  42. |prev=0 |tag:len=2 |enter |leave |prev=4 |tag=0 |
  43. +----------+----------+----------+----------+----------+----------+
  44. ^base top,limit^
  45. The tag is a combination of the type of the dynstack item, some flags
  46. associated with the item, and the length of the item. See
  47. SCM_MAKE_DYNSTACK_TAG below for the details.
  48. This arrangement makes it possible to have variable-length dynstack
  49. items, and yet be able to traverse them forwards or backwards. */
  50. #define SCM_DYNSTACK_HEADER_LEN 2
  51. #define SCM_DYNSTACK_PREV_OFFSET(top) ((top)[-2])
  52. #define SCM_DYNSTACK_SET_PREV_OFFSET(top, offset) (top)[-2] = (offset)
  53. #define SCM_DYNSTACK_TAG(top) ((top)[-1])
  54. #define SCM_DYNSTACK_SET_TAG(top, tag) (top)[-1] = (tag)
  55. typedef enum {
  56. SCM_DYNSTACK_TYPE_NONE = 0,
  57. SCM_DYNSTACK_TYPE_FRAME,
  58. SCM_DYNSTACK_TYPE_UNWINDER,
  59. SCM_DYNSTACK_TYPE_REWINDER,
  60. SCM_DYNSTACK_TYPE_WITH_FLUID,
  61. SCM_DYNSTACK_TYPE_PROMPT,
  62. SCM_DYNSTACK_TYPE_DYNWIND,
  63. SCM_DYNSTACK_TYPE_DYNAMIC_STATE,
  64. } scm_t_dynstack_item_type;
  65. #define SCM_DYNSTACK_TAG_TYPE_MASK 0xf
  66. #define SCM_DYNSTACK_TAG_FLAGS_MASK 0xf0
  67. #define SCM_DYNSTACK_TAG_FLAGS_SHIFT 4
  68. #define SCM_DYNSTACK_TAG_LEN_SHIFT 8
  69. #define SCM_MAKE_DYNSTACK_TAG(type, flags, len) \
  70. ((type) | (flags) | ((len) << SCM_DYNSTACK_TAG_LEN_SHIFT))
  71. #define SCM_DYNSTACK_TAG_TYPE(tag) \
  72. ((tag) & SCM_DYNSTACK_TAG_TYPE_MASK)
  73. #define SCM_DYNSTACK_TAG_FLAGS(tag) \
  74. ((tag) & SCM_DYNSTACK_TAG_FLAGS_MASK)
  75. #define SCM_DYNSTACK_TAG_LEN(tag) \
  76. ((tag) >> SCM_DYNSTACK_TAG_LEN_SHIFT)
  77. #define SCM_DYNSTACK_PREV(top) \
  78. (SCM_DYNSTACK_PREV_OFFSET (top) \
  79. ? ((top) - SCM_DYNSTACK_PREV_OFFSET (top)) : NULL)
  80. #define SCM_DYNSTACK_NEXT(top) \
  81. (SCM_DYNSTACK_TAG (top) \
  82. ? ((top) + SCM_DYNSTACK_TAG_LEN (SCM_DYNSTACK_TAG (top)) \
  83. + SCM_DYNSTACK_HEADER_LEN) \
  84. : NULL)
  85. #define SCM_DYNSTACK_FIRST(dynstack) \
  86. ((dynstack)->base + SCM_DYNSTACK_HEADER_LEN)
  87. #define SCM_DYNSTACK_CAPACITY(dynstack) \
  88. ((dynstack)->limit - (dynstack)->base)
  89. #define SCM_DYNSTACK_SPACE(dynstack) \
  90. ((dynstack)->limit - (dynstack)->top)
  91. #define SCM_DYNSTACK_HEIGHT(dynstack) \
  92. ((dynstack)->top - (dynstack)->base)
  93. #define SCM_DYNSTACK_HAS_SPACE(dynstack, n) \
  94. (SCM_DYNSTACK_SPACE (dynstack) >= n + SCM_DYNSTACK_HEADER_LEN)
  95. typedef enum {
  96. SCM_F_DYNSTACK_FRAME_REWINDABLE = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  97. } scm_t_dynstack_frame_flags;
  98. typedef enum {
  99. SCM_F_DYNSTACK_WINDER_EXPLICIT = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  100. } scm_t_dynstack_winder_flags;
  101. typedef enum {
  102. SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  103. } scm_t_dynstack_prompt_flags;
  104. typedef void (*scm_t_guard) (void *);
  105. /* Pushing and popping entries on the dynamic stack. */
  106. SCM_INTERNAL void scm_dynstack_push_frame (scm_t_dynstack *,
  107. scm_t_dynstack_frame_flags);
  108. SCM_INTERNAL void scm_dynstack_push_rewinder (scm_t_dynstack *,
  109. scm_t_dynstack_winder_flags,
  110. scm_t_guard, void *);
  111. SCM_INTERNAL void scm_dynstack_push_unwinder (scm_t_dynstack *,
  112. scm_t_dynstack_winder_flags,
  113. scm_t_guard, void *);
  114. SCM_INTERNAL void scm_dynstack_push_fluid (
  115. scm_t_dynstack *, SCM fluid, SCM value,
  116. scm_t_dynamic_state *dynamic_state);
  117. SCM_INTERNAL void scm_dynstack_push_dynamic_state (scm_t_dynstack *, SCM,
  118. scm_t_dynamic_state *);
  119. SCM_INTERNAL void scm_dynstack_push_prompt (scm_t_dynstack *,
  120. scm_t_dynstack_prompt_flags,
  121. SCM key,
  122. scm_t_ptrdiff fp_offset,
  123. scm_t_ptrdiff sp_offset,
  124. scm_t_uint32 *ip,
  125. scm_i_jmp_buf *registers);
  126. SCM_INTERNAL void scm_dynstack_push_dynwind (scm_t_dynstack *,
  127. SCM enter, SCM leave);
  128. SCM_INTERNAL void scm_dynstack_pop (scm_t_dynstack *);
  129. /* Capturing, winding, and unwinding. */
  130. SCM_INTERNAL scm_t_dynstack* scm_dynstack_capture_all (scm_t_dynstack *dynstack);
  131. SCM_INTERNAL scm_t_dynstack* scm_dynstack_capture (scm_t_dynstack *dynstack,
  132. scm_t_bits *item);
  133. SCM_INTERNAL void scm_dynstack_wind_1 (scm_t_dynstack *, scm_t_bits *);
  134. SCM_INTERNAL scm_t_bits scm_dynstack_unwind_1 (scm_t_dynstack *);
  135. SCM_INTERNAL void scm_dynstack_wind (scm_t_dynstack *, scm_t_bits *);
  136. SCM_INTERNAL void scm_dynstack_unwind (scm_t_dynstack *, scm_t_bits *);
  137. /* Miscellany. */
  138. SCM_INTERNAL scm_t_bits* scm_dynstack_unwind_fork (scm_t_dynstack *,
  139. scm_t_dynstack *);
  140. SCM_INTERNAL void scm_dynstack_unwind_frame (scm_t_dynstack *);
  141. SCM_INTERNAL void scm_dynstack_unwind_fluid
  142. (scm_t_dynstack *dynstack, scm_t_dynamic_state *dynamic_state);
  143. SCM_INTERNAL void scm_dynstack_unwind_dynamic_state
  144. (scm_t_dynstack *dynstack, scm_t_dynamic_state *dynamic_state);
  145. SCM_INTERNAL scm_t_bits* scm_dynstack_find_prompt (scm_t_dynstack *, SCM,
  146. scm_t_dynstack_prompt_flags *,
  147. scm_t_ptrdiff *,
  148. scm_t_ptrdiff *,
  149. scm_t_uint32 **,
  150. scm_i_jmp_buf **);
  151. SCM_INTERNAL SCM scm_dynstack_find_old_fluid_value (scm_t_dynstack *,
  152. SCM, size_t, SCM);
  153. SCM_INTERNAL void scm_dynstack_relocate_prompts (scm_t_dynstack *,
  154. scm_t_ptrdiff);
  155. SCM_INTERNAL void scm_dynstack_wind_prompt (scm_t_dynstack *, scm_t_bits *,
  156. scm_t_ptrdiff, scm_i_jmp_buf *);
  157. #endif /* SCM_DYNSTACK_H */
  158. /*
  159. Local Variables:
  160. c-file-style: "gnu"
  161. End:
  162. */