frames.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright 2001,2009-2015,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _SCM_FRAMES_H_
  16. #define _SCM_FRAMES_H_
  17. #include <libguile/gc.h>
  18. #include "programs.h"
  19. /* Stack frames
  20. ------------
  21. It's a little confusing, but there are two representations of frames
  22. in this file: frame pointers, and Scheme objects wrapping those frame
  23. pointers. The former uses the SCM_FRAME macro prefix, the latter
  24. SCM_VM_FRAME prefix.
  25. The confusing thing is that only Scheme frame objects have functions
  26. that use them, and they use the lower-case scm_frame prefix.
  27. Stack frame layout
  28. ------------------
  29. | ... |
  30. +==============================+ <- fp + 3 = SCM_FRAME_PREVIOUS_SP (fp)
  31. | Dynamic link |
  32. +------------------------------+
  33. | Virtual return address (vRA) |
  34. +------------------------------+
  35. | Machine return address (mRA) |
  36. +==============================+ <- fp
  37. | Local 0 |
  38. +------------------------------+
  39. | Local 1 |
  40. +------------------------------+
  41. | ... |
  42. +------------------------------+
  43. | Local N-1 |
  44. \------------------------------/ <- sp
  45. The stack grows down.
  46. The calling convention is that a caller prepares a stack frame
  47. consisting of the saved FP, the saved virtual return address, and the
  48. saved machine return address of the calling function, 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 0. The callee resets the stack pointer to point to the
  60. last value. In this way the caller knows how many values there are:
  61. it's the number of words between the stack pointer and the slot at
  62. which the caller placed the procedure.
  63. After checking that the number of values returned is appropriate, the
  64. caller shuffles the values around (if needed), and resets the stack
  65. pointer back to its original value from before the call. */
  66. /* Each element on the stack occupies the same amount of space. */
  67. union scm_vm_stack_element
  68. {
  69. uintptr_t as_uint;
  70. uint32_t *as_vcode;
  71. uint8_t *as_mcode;
  72. SCM as_scm;
  73. double as_f64;
  74. uint64_t as_u64;
  75. int64_t as_s64;
  76. /* For GC purposes. */
  77. void *as_ptr;
  78. scm_t_bits as_bits;
  79. };
  80. #define SCM_FRAME_PREVIOUS_SP(fp) ((fp) + 3)
  81. #define SCM_FRAME_MACHINE_RETURN_ADDRESS(fp) ((fp)[0].as_mcode)
  82. #define SCM_FRAME_SET_MACHINE_RETURN_ADDRESS(fp, ra) ((fp)[0].as_mcode = (ra))
  83. #define SCM_FRAME_VIRTUAL_RETURN_ADDRESS(fp) ((fp)[1].as_vcode)
  84. #define SCM_FRAME_SET_VIRTUAL_RETURN_ADDRESS(fp, ra) ((fp)[1].as_vcode = (ra))
  85. #define SCM_FRAME_DYNAMIC_LINK(fp) ((fp) + (fp)[2].as_uint)
  86. #define SCM_FRAME_SET_DYNAMIC_LINK(fp, dl) ((fp)[2].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. ptrdiff_t fp_offset;
  98. ptrdiff_t sp_offset;
  99. uint32_t *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_TYP11 (x, scm_tc11_frame))
  107. #define SCM_VM_FRAME_KIND(x) ((enum scm_vm_frame_kind) (SCM_CELL_WORD_0 (x) >> 12))
  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_VALIDATE_VM_FRAME(p,x) SCM_MAKE_VALIDATE (p, x, VM_FRAME_P)
  116. SCM_INTERNAL union scm_vm_stack_element* scm_i_frame_stack_top (SCM frame);
  117. /* See notes in frames.c before using this. */
  118. SCM_INTERNAL SCM scm_c_frame_closure (enum scm_vm_frame_kind kind,
  119. const struct scm_frame *frame);
  120. SCM_INTERNAL SCM scm_c_make_frame (enum scm_vm_frame_kind kind,
  121. const struct scm_frame *frame);
  122. SCM_INTERNAL int scm_c_frame_previous (enum scm_vm_frame_kind kind,
  123. struct scm_frame *frame);
  124. #endif
  125. SCM_API SCM scm_frame_p (SCM obj);
  126. SCM_API SCM scm_frame_procedure_name (SCM frame);
  127. SCM_API SCM scm_frame_call_representation (SCM frame);
  128. SCM_API SCM scm_frame_arguments (SCM frame);
  129. SCM_API SCM scm_frame_source (SCM frame);
  130. SCM_API SCM scm_frame_address (SCM frame);
  131. SCM_API SCM scm_frame_stack_pointer (SCM frame);
  132. SCM_API SCM scm_frame_instruction_pointer (SCM frame);
  133. SCM_API SCM scm_frame_return_address (SCM frame);
  134. SCM_API SCM scm_frame_dynamic_link (SCM frame);
  135. SCM_API SCM scm_frame_previous (SCM frame);
  136. SCM_INTERNAL void scm_i_frame_print (SCM frame, SCM port,
  137. scm_print_state *pstate);
  138. SCM_INTERNAL void scm_init_frames (void);
  139. #endif /* _SCM_FRAMES_H_ */