vm.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright 2001,2009-2015,2017-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_VM_H_
  16. #define _SCM_VM_H_
  17. #include <setjmp.h>
  18. #include <libguile/gc.h>
  19. #include <libguile/programs.h>
  20. #define SCM_VM_REGULAR_ENGINE 0
  21. #define SCM_VM_DEBUG_ENGINE 1
  22. #define SCM_VM_NUM_ENGINES 2
  23. enum scm_compare {
  24. SCM_F_COMPARE_NONE = 0x0,
  25. SCM_F_COMPARE_EQUAL = 0x1,
  26. SCM_F_COMPARE_LESS_THAN = 0x2,
  27. SCM_F_COMPARE_INVALID = 0x3
  28. };
  29. struct scm_vm {
  30. uint32_t *ip; /* instruction pointer */
  31. union scm_vm_stack_element *sp; /* stack pointer */
  32. union scm_vm_stack_element *fp; /* frame pointer */
  33. union scm_vm_stack_element *stack_limit; /* stack limit address */
  34. uint8_t compare_result; /* flags register: a value from scm_compare */
  35. uint8_t apply_hook_enabled; /* if apply hook is enabled */
  36. uint8_t return_hook_enabled; /* if return hook is enabled */
  37. uint8_t next_hook_enabled; /* if next hook is enabled */
  38. uint8_t abort_hook_enabled; /* if abort hook is enabled */
  39. uint8_t disable_mcode; /* if mcode is disabled (because debugging) */
  40. uint8_t engine; /* which vm engine we're using */
  41. uint8_t unused; /* padding */
  42. size_t stack_size; /* stack size */
  43. union scm_vm_stack_element *stack_bottom; /* lowest address in allocated stack */
  44. SCM apply_hook; /* apply hook */
  45. SCM return_hook; /* return hook */
  46. SCM next_hook; /* next hook */
  47. SCM abort_hook; /* abort hook */
  48. union scm_vm_stack_element *stack_top; /* highest address in allocated stack */
  49. SCM overflow_handler_stack; /* alist of max-stack-size -> thunk */
  50. jmp_buf *registers; /* registers captured at latest vm entry */
  51. uint8_t *mra_after_abort; /* mra to resume after nonlocal exit, or NULL */
  52. int trace_level; /* traces enabled if trace_level > 0 */
  53. };
  54. SCM_API SCM scm_call_with_vm (SCM proc, SCM args);
  55. SCM_API SCM scm_call_with_stack_overflow_handler (SCM limit, SCM thunk,
  56. SCM handler);
  57. SCM_INTERNAL SCM scm_vm_add_apply_hook_x (SCM);
  58. SCM_INTERNAL SCM scm_vm_add_return_hook_x (SCM);
  59. SCM_INTERNAL SCM scm_vm_add_abort_hook_x (SCM);
  60. SCM_INTERNAL SCM scm_vm_add_next_hook_x (SCM);
  61. SCM_INTERNAL SCM scm_vm_remove_apply_hook_x (SCM);
  62. SCM_INTERNAL SCM scm_vm_remove_return_hook_x (SCM);
  63. SCM_INTERNAL SCM scm_vm_remove_abort_hook_x (SCM);
  64. SCM_INTERNAL SCM scm_vm_remove_next_hook_x (SCM);
  65. SCM_API SCM scm_vm_trace_level (void);
  66. SCM_API SCM scm_set_vm_trace_level_x (SCM level);
  67. SCM_API SCM scm_vm_engine (void);
  68. SCM_API SCM scm_set_vm_engine_x (SCM engine);
  69. SCM_API SCM scm_set_default_vm_engine_x (SCM engine);
  70. SCM_API void scm_c_set_vm_engine_x (int engine);
  71. SCM_API void scm_c_set_default_vm_engine_x (int engine);
  72. SCM_INTERNAL void scm_i_vm_prepare_stack (struct scm_vm *vp);
  73. struct GC_ms_entry;
  74. SCM_INTERNAL struct GC_ms_entry * scm_i_vm_mark_stack (struct scm_vm *,
  75. struct GC_ms_entry *,
  76. struct GC_ms_entry *);
  77. SCM_INTERNAL void scm_i_vm_free_stack (struct scm_vm *vp);
  78. #define SCM_F_VM_CONT_PARTIAL 0x1
  79. #define SCM_F_VM_CONT_REWINDABLE 0x2
  80. struct scm_vm_cont {
  81. /* IP of newest frame. */
  82. uint32_t *vra;
  83. /* Machine code corresponding to IP. */
  84. uint8_t *mra;
  85. /* Offset of FP of newest frame, relative to stack top. */
  86. ptrdiff_t fp_offset;
  87. /* Besides being the stack size, this is also the offset of the SP of
  88. the newest frame. */
  89. ptrdiff_t stack_size;
  90. /* Stack bottom, which also keeps saved stack alive for GC. */
  91. union scm_vm_stack_element *stack_bottom;
  92. /* Saved dynamic stack, with prompts relocated to record saved SP/FP
  93. offsets from the stack top of this scm_vm_cont. */
  94. scm_t_dynstack *dynstack;
  95. /* See the continuation is partial and/or rewindable. */
  96. uint32_t flags;
  97. };
  98. #define SCM_VM_CONT_P(OBJ) (SCM_HAS_TYP7 (OBJ, scm_tc7_vm_cont))
  99. #define SCM_VM_CONT_DATA(CONT) ((struct scm_vm_cont *) SCM_CELL_WORD_1 (CONT))
  100. #define SCM_VM_CONT_PARTIAL_P(CONT) (SCM_VM_CONT_DATA (CONT)->flags & SCM_F_VM_CONT_PARTIAL)
  101. #define SCM_VM_CONT_REWINDABLE_P(CONT) (SCM_VM_CONT_DATA (CONT)->flags & SCM_F_VM_CONT_REWINDABLE)
  102. SCM_API SCM scm_load_compiled_with_vm (SCM file);
  103. SCM_INTERNAL SCM scm_i_call_with_current_continuation (SCM proc);
  104. SCM_INTERNAL SCM scm_i_capture_current_stack (void);
  105. SCM_INTERNAL void scm_i_vm_abort (SCM *tag_and_argv, size_t n) SCM_NORETURN;
  106. SCM_INTERNAL void scm_i_vm_emergency_abort (SCM *tag_and_argv, size_t n) SCM_NORETURN;
  107. SCM_INTERNAL int scm_i_vm_cont_to_frame (SCM cont, struct scm_frame *frame);
  108. SCM_INTERNAL void scm_i_vm_cont_print (SCM x, SCM port,
  109. scm_print_state *pstate);
  110. SCM_INTERNAL int scm_i_vm_is_boot_continuation_code (uint32_t *ip);
  111. SCM_INTERNAL void scm_bootstrap_vm (void);
  112. SCM_INTERNAL void scm_init_vm (void);
  113. #endif /* _SCM_VM_H_ */