frames.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2001, 2009, 2010, 2011 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. /*
  23. * VM frames
  24. */
  25. /*
  26. * It's a little confusing, but there are two representations of frames in this
  27. * file: frame pointers and Scheme objects wrapping those frame pointers. The
  28. * former uses the SCM_FRAME_... macro prefix, the latter SCM_VM_FRAME_..
  29. * prefix.
  30. *
  31. * The confusing thing is that only Scheme frame objects have functions that use
  32. * them, and they use the scm_frame_.. prefix. Hysterical raisins.
  33. */
  34. /* VM Frame Layout
  35. ---------------
  36. | ... |
  37. | Intermed. val. 0 | <- fp + nargs + nlocs
  38. +------------------+
  39. | Local variable 1 |
  40. | Local variable 0 | <- fp + nargs
  41. | Argument 1 |
  42. | Argument 0 | <- fp = SCM_FRAME_STACK_ADDRESS (fp)
  43. | Program | <- fp - 1
  44. +==================+
  45. | Return address | <- SCM_FRAME_UPPER_ADDRESS (fp)
  46. | MV return address|
  47. | Dynamic link | <- fp - 4 = SCM_FRAME_DATA_ADDRESS (fp) = SCM_FRAME_LOWER_ADDRESS (fp)
  48. +==================+
  49. | |
  50. As can be inferred from this drawing, it is assumed that
  51. `sizeof (SCM *) == sizeof (SCM)', since pointers (the `link' parts) are
  52. assumed to be as long as SCM objects. */
  53. #define SCM_FRAME_DATA_ADDRESS(fp) (fp - 4)
  54. #define SCM_FRAME_STACK_ADDRESS(fp) (fp)
  55. #define SCM_FRAME_UPPER_ADDRESS(fp) (fp - 2)
  56. #define SCM_FRAME_LOWER_ADDRESS(fp) (fp - 4)
  57. #define SCM_FRAME_BYTE_CAST(x) ((scm_t_uint8 *) SCM_UNPACK (x))
  58. #define SCM_FRAME_STACK_CAST(x) ((SCM *) SCM_UNPACK (x))
  59. #define SCM_FRAME_RETURN_ADDRESS(fp) \
  60. (SCM_FRAME_BYTE_CAST (SCM_FRAME_DATA_ADDRESS (fp)[2]))
  61. #define SCM_FRAME_SET_RETURN_ADDRESS(fp, ra) \
  62. ((SCM_FRAME_DATA_ADDRESS (fp)[2])) = SCM_PACK (ra)
  63. #define SCM_FRAME_MV_RETURN_ADDRESS(fp) \
  64. (SCM_FRAME_BYTE_CAST (SCM_FRAME_DATA_ADDRESS (fp)[1]))
  65. #define SCM_FRAME_SET_MV_RETURN_ADDRESS(fp, mvra) \
  66. ((SCM_FRAME_DATA_ADDRESS (fp)[1])) = SCM_PACK (mvra)
  67. #define SCM_FRAME_DYNAMIC_LINK(fp) \
  68. (SCM_FRAME_STACK_CAST (SCM_FRAME_DATA_ADDRESS (fp)[0]))
  69. #define SCM_FRAME_SET_DYNAMIC_LINK(fp, dl) \
  70. ((SCM_FRAME_DATA_ADDRESS (fp)[0])) = SCM_PACK (dl)
  71. #define SCM_FRAME_VARIABLE(fp,i) SCM_FRAME_STACK_ADDRESS (fp)[i]
  72. #define SCM_FRAME_PROGRAM(fp) SCM_FRAME_STACK_ADDRESS (fp)[-1]
  73. /*
  74. * Heap frames
  75. */
  76. struct scm_frame
  77. {
  78. SCM stack_holder;
  79. SCM *fp;
  80. SCM *sp;
  81. scm_t_uint8 *ip;
  82. scm_t_ptrdiff offset;
  83. };
  84. #define SCM_VM_FRAME_P(x) (SCM_NIMP (x) && SCM_TYP7 (x) == scm_tc7_frame)
  85. #define SCM_VM_FRAME_DATA(x) ((struct scm_frame*)SCM_CELL_WORD_1 (x))
  86. #define SCM_VM_FRAME_STACK_HOLDER(f) SCM_VM_FRAME_DATA(f)->stack_holder
  87. #define SCM_VM_FRAME_FP(f) SCM_VM_FRAME_DATA(f)->fp
  88. #define SCM_VM_FRAME_SP(f) SCM_VM_FRAME_DATA(f)->sp
  89. #define SCM_VM_FRAME_IP(f) SCM_VM_FRAME_DATA(f)->ip
  90. #define SCM_VM_FRAME_OFFSET(f) SCM_VM_FRAME_DATA(f)->offset
  91. #define SCM_VALIDATE_VM_FRAME(p,x) SCM_MAKE_VALIDATE (p, x, VM_FRAME_P)
  92. SCM_API SCM scm_c_make_frame (SCM stack_holder, SCM *fp, SCM *sp,
  93. scm_t_uint8 *ip, scm_t_ptrdiff offset);
  94. SCM_API SCM scm_frame_p (SCM obj);
  95. SCM_API SCM scm_frame_procedure (SCM frame);
  96. SCM_API SCM scm_frame_arguments (SCM frame);
  97. SCM_API SCM scm_frame_source (SCM frame);
  98. SCM_API SCM scm_frame_num_locals (SCM frame);
  99. SCM_API SCM scm_frame_local_ref (SCM frame, SCM index);
  100. SCM_API SCM scm_frame_local_set_x (SCM frame, SCM index, SCM val);
  101. SCM_API SCM scm_frame_address (SCM frame);
  102. SCM_API SCM scm_frame_stack_pointer (SCM frame);
  103. SCM_API SCM scm_frame_instruction_pointer (SCM frame);
  104. SCM_API SCM scm_frame_return_address (SCM frame);
  105. SCM_API SCM scm_frame_mv_return_address (SCM frame);
  106. SCM_API SCM scm_frame_dynamic_link (SCM frame);
  107. SCM_API SCM scm_frame_previous (SCM frame);
  108. SCM_INTERNAL void scm_i_frame_print (SCM frame, SCM port,
  109. scm_print_state *pstate);
  110. SCM_INTERNAL void scm_init_frames (void);
  111. #endif /* _SCM_FRAMES_H_ */
  112. /*
  113. Local Variables:
  114. c-file-style: "gnu"
  115. End:
  116. */