dynstack.h 8.2 KB

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