dynstack.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_t_dynstack_item_type;
  64. #define SCM_DYNSTACK_TAG_TYPE_MASK 0xf
  65. #define SCM_DYNSTACK_TAG_FLAGS_MASK 0xf0
  66. #define SCM_DYNSTACK_TAG_FLAGS_SHIFT 4
  67. #define SCM_DYNSTACK_TAG_LEN_SHIFT 8
  68. #define SCM_MAKE_DYNSTACK_TAG(type, flags, len) \
  69. ((type) | (flags) | ((len) << SCM_DYNSTACK_TAG_LEN_SHIFT))
  70. #define SCM_DYNSTACK_TAG_TYPE(tag) \
  71. ((tag) & SCM_DYNSTACK_TAG_TYPE_MASK)
  72. #define SCM_DYNSTACK_TAG_FLAGS(tag) \
  73. ((tag) & SCM_DYNSTACK_TAG_FLAGS_MASK)
  74. #define SCM_DYNSTACK_TAG_LEN(tag) \
  75. ((tag) >> SCM_DYNSTACK_TAG_LEN_SHIFT)
  76. #define SCM_DYNSTACK_PREV(top) \
  77. (SCM_DYNSTACK_PREV_OFFSET (top) \
  78. ? ((top) - SCM_DYNSTACK_PREV_OFFSET (top)) : NULL)
  79. #define SCM_DYNSTACK_NEXT(top) \
  80. (SCM_DYNSTACK_TAG (top) \
  81. ? ((top) + SCM_DYNSTACK_TAG_LEN (SCM_DYNSTACK_TAG (top)) \
  82. + SCM_DYNSTACK_HEADER_LEN) \
  83. : NULL)
  84. #define SCM_DYNSTACK_FIRST(dynstack) \
  85. ((dynstack)->base + SCM_DYNSTACK_HEADER_LEN)
  86. #define SCM_DYNSTACK_CAPACITY(dynstack) \
  87. ((dynstack)->limit - (dynstack)->base)
  88. #define SCM_DYNSTACK_SPACE(dynstack) \
  89. ((dynstack)->limit - (dynstack)->top)
  90. #define SCM_DYNSTACK_HEIGHT(dynstack) \
  91. ((dynstack)->top - (dynstack)->base)
  92. #define SCM_DYNSTACK_HAS_SPACE(dynstack, n) \
  93. (SCM_DYNSTACK_SPACE (dynstack) >= n + SCM_DYNSTACK_HEADER_LEN)
  94. typedef enum {
  95. SCM_F_DYNSTACK_FRAME_REWINDABLE = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  96. } scm_t_dynstack_frame_flags;
  97. typedef enum {
  98. SCM_F_DYNSTACK_WINDER_EXPLICIT = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  99. } scm_t_dynstack_winder_flags;
  100. typedef enum {
  101. SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY = (1 << SCM_DYNSTACK_TAG_FLAGS_SHIFT)
  102. } scm_t_dynstack_prompt_flags;
  103. typedef void (*scm_t_guard) (void *);
  104. /* Pushing and popping entries on the dynamic stack. */
  105. SCM_INTERNAL void scm_dynstack_push_frame (scm_t_dynstack *,
  106. scm_t_dynstack_frame_flags);
  107. SCM_INTERNAL void scm_dynstack_push_rewinder (scm_t_dynstack *,
  108. scm_t_dynstack_winder_flags,
  109. scm_t_guard, void *);
  110. SCM_INTERNAL void scm_dynstack_push_unwinder (scm_t_dynstack *,
  111. scm_t_dynstack_winder_flags,
  112. scm_t_guard, void *);
  113. SCM_INTERNAL void scm_dynstack_push_fluid (scm_t_dynstack *,
  114. SCM fluid, SCM value,
  115. SCM dynamic_state);
  116. SCM_INTERNAL void scm_dynstack_push_prompt (scm_t_dynstack *,
  117. scm_t_dynstack_prompt_flags,
  118. SCM key,
  119. scm_t_ptrdiff fp_offset,
  120. scm_t_ptrdiff sp_offset,
  121. scm_t_uint32 *ip,
  122. scm_i_jmp_buf *registers);
  123. SCM_INTERNAL void scm_dynstack_push_dynwind (scm_t_dynstack *,
  124. SCM enter, SCM leave);
  125. SCM_INTERNAL void scm_dynstack_pop (scm_t_dynstack *);
  126. /* Capturing, winding, and unwinding. */
  127. SCM_INTERNAL scm_t_dynstack* scm_dynstack_capture_all (scm_t_dynstack *dynstack);
  128. SCM_INTERNAL scm_t_dynstack* scm_dynstack_capture (scm_t_dynstack *dynstack,
  129. scm_t_bits *item);
  130. SCM_INTERNAL void scm_dynstack_wind_1 (scm_t_dynstack *, scm_t_bits *);
  131. SCM_INTERNAL scm_t_bits scm_dynstack_unwind_1 (scm_t_dynstack *);
  132. SCM_INTERNAL void scm_dynstack_wind (scm_t_dynstack *, scm_t_bits *);
  133. SCM_INTERNAL void scm_dynstack_unwind (scm_t_dynstack *, scm_t_bits *);
  134. /* Miscellany. */
  135. SCM_INTERNAL scm_t_bits* scm_dynstack_unwind_fork (scm_t_dynstack *,
  136. scm_t_dynstack *);
  137. SCM_INTERNAL void scm_dynstack_unwind_frame (scm_t_dynstack *);
  138. SCM_INTERNAL void scm_dynstack_unwind_fluid (scm_t_dynstack *dynstack,
  139. SCM dynamic_state);
  140. SCM_INTERNAL scm_t_bits* scm_dynstack_find_prompt (scm_t_dynstack *, SCM,
  141. scm_t_dynstack_prompt_flags *,
  142. scm_t_ptrdiff *,
  143. scm_t_ptrdiff *,
  144. scm_t_uint32 **,
  145. scm_i_jmp_buf **);
  146. SCM_INTERNAL void scm_dynstack_wind_prompt (scm_t_dynstack *, scm_t_bits *,
  147. scm_t_ptrdiff, scm_i_jmp_buf *);
  148. #endif /* SCM_DYNSTACK_H */
  149. /*
  150. Local Variables:
  151. c-file-style: "gnu"
  152. End:
  153. */