frames.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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. /* Stack frames
  23. ------------
  24. It's a little confusing, but there are two representations of frames
  25. in this file: frame pointers, and Scheme objects wrapping those frame
  26. pointers. The former uses the SCM_FRAME macro prefix, the latter
  27. SCM_VM_FRAME prefix.
  28. The confusing thing is that only Scheme frame objects have functions
  29. that use them, and they use the lower-case scm_frame prefix.
  30. Stack frame layout
  31. ------------------
  32. | ... |
  33. +==================+ <- fp + 2 = SCM_FRAME_PREVIOUS_SP (fp)
  34. | Dynamic link |
  35. +------------------+
  36. | Return address |
  37. +==================+ <- fp
  38. | Local 0 |
  39. +------------------+
  40. | Local 1 |
  41. +------------------+
  42. | ... |
  43. +------------------+
  44. | Local N-1 |
  45. \------------------/ <- sp
  46. The stack grows down.
  47. The calling convention is that a caller prepares a stack frame
  48. consisting of the saved FP and the return address, followed by the
  49. procedure and then the arguments to the call, in order. Thus in the
  50. beginning of a call, the procedure being called is in slot 0, the
  51. first argument is in slot 1, and the SP points to the last argument.
  52. The number of arguments, including the procedure, is thus FP - SP.
  53. After ensuring that the correct number of arguments have been passed,
  54. a function will set the stack pointer to point to the last local
  55. slot. This lets a function allocate the temporary space that it
  56. needs once in the beginning of the call, instead of pushing and
  57. popping the stack pointer during the call's extent.
  58. When a program returns, it returns its values in the slots starting
  59. from local 1, as if the values were arguments to a tail call. We
  60. start from 1 instead of 0 for the convenience of the "values" builtin
  61. function, which can just leave its arguments in place.
  62. The callee resets the stack pointer to point to the last value. In
  63. this way the caller knows how many values there are: it's the number
  64. of words between the stack pointer and the slot at which the caller
  65. placed the procedure.
  66. After checking that the number of values returned is appropriate, the
  67. caller shuffles the values around (if needed), and resets the stack
  68. pointer back to its original value from before the call. */
  69. /* Each element on the stack occupies the same amount of space. */
  70. union scm_vm_stack_element
  71. {
  72. scm_t_uintptr as_uint;
  73. scm_t_uint32 *as_ip;
  74. SCM as_scm;
  75. double as_f64;
  76. scm_t_uint64 as_u64;
  77. scm_t_int64 as_s64;
  78. /* For GC purposes. */
  79. void *as_ptr;
  80. scm_t_bits as_bits;
  81. };
  82. #define SCM_FRAME_PREVIOUS_SP(fp) ((fp) + 2)
  83. #define SCM_FRAME_RETURN_ADDRESS(fp) ((fp)[0].as_ip)
  84. #define SCM_FRAME_SET_RETURN_ADDRESS(fp, ra) ((fp)[0].as_ip = (ra))
  85. #define SCM_FRAME_DYNAMIC_LINK(fp) ((fp) + (fp)[1].as_uint)
  86. #define SCM_FRAME_SET_DYNAMIC_LINK(fp, dl) ((fp)[1].as_uint = ((dl) - (fp)))
  87. #define SCM_FRAME_SLOT(fp,i) ((fp) - (i) - 1)
  88. #define SCM_FRAME_LOCAL(fp,i) (SCM_FRAME_SLOT (fp, i)->as_scm)
  89. #define SCM_FRAME_NUM_LOCALS(fp, sp) ((fp) - (sp))
  90. /*
  91. * Heap frames
  92. */
  93. #ifdef BUILDING_LIBGUILE
  94. struct scm_frame
  95. {
  96. void *stack_holder;
  97. scm_t_ptrdiff fp_offset;
  98. scm_t_ptrdiff sp_offset;
  99. scm_t_uint32 *ip;
  100. };
  101. enum scm_vm_frame_kind
  102. {
  103. SCM_VM_FRAME_KIND_VM,
  104. SCM_VM_FRAME_KIND_CONT
  105. };
  106. #define SCM_VM_FRAME_P(x) (SCM_HAS_TYP7 (x, scm_tc7_frame))
  107. #define SCM_VM_FRAME_KIND(x) ((enum scm_vm_frame_kind) (SCM_CELL_WORD_0 (x) >> 8))
  108. #define SCM_VM_FRAME_DATA(x) ((struct scm_frame *)SCM_CELL_WORD_1 (x))
  109. #define SCM_VM_FRAME_STACK_HOLDER(f) SCM_VM_FRAME_DATA (f)->stack_holder
  110. #define SCM_VM_FRAME_FP_OFFSET(f) SCM_VM_FRAME_DATA (f)->fp_offset
  111. #define SCM_VM_FRAME_SP_OFFSET(f) SCM_VM_FRAME_DATA (f)->sp_offset
  112. #define SCM_VM_FRAME_FP(f) (scm_i_frame_stack_top (f) - SCM_VM_FRAME_FP_OFFSET (f))
  113. #define SCM_VM_FRAME_SP(f) (scm_i_frame_stack_top (f) - SCM_VM_FRAME_SP_OFFSET (f))
  114. #define SCM_VM_FRAME_IP(f) SCM_VM_FRAME_DATA (f)->ip
  115. #define SCM_VM_FRAME_OFFSET(f) scm_i_frame_offset (f)
  116. #define SCM_VALIDATE_VM_FRAME(p,x) SCM_MAKE_VALIDATE (p, x, VM_FRAME_P)
  117. SCM_INTERNAL union scm_vm_stack_element* scm_i_frame_stack_top (SCM frame);
  118. SCM_INTERNAL scm_t_ptrdiff scm_i_frame_offset (SCM frame);
  119. /* See notes in frames.c before using this. */
  120. SCM_INTERNAL SCM scm_c_frame_closure (enum scm_vm_frame_kind kind,
  121. const struct scm_frame *frame);
  122. SCM_INTERNAL SCM scm_c_make_frame (enum scm_vm_frame_kind kind,
  123. const struct scm_frame *frame);
  124. SCM_INTERNAL int scm_c_frame_previous (enum scm_vm_frame_kind kind,
  125. struct scm_frame *frame);
  126. #endif
  127. SCM_API SCM scm_frame_p (SCM obj);
  128. SCM_API SCM scm_frame_procedure_name (SCM frame);
  129. SCM_API SCM scm_frame_call_representation (SCM frame);
  130. SCM_API SCM scm_frame_arguments (SCM frame);
  131. SCM_API SCM scm_frame_source (SCM frame);
  132. SCM_API SCM scm_frame_address (SCM frame);
  133. SCM_API SCM scm_frame_stack_pointer (SCM frame);
  134. SCM_API SCM scm_frame_instruction_pointer (SCM frame);
  135. SCM_API SCM scm_frame_return_address (SCM frame);
  136. SCM_API SCM scm_frame_dynamic_link (SCM frame);
  137. SCM_API SCM scm_frame_previous (SCM frame);
  138. SCM_INTERNAL void scm_i_frame_print (SCM frame, SCM port,
  139. scm_print_state *pstate);
  140. SCM_INTERNAL void scm_init_frames (void);
  141. #endif /* _SCM_FRAMES_H_ */
  142. /*
  143. Local Variables:
  144. c-file-style: "gnu"
  145. End:
  146. */