intrinsics.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright 2018-2021, 2023
  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_INTRINSICS_H_
  16. #define _SCM_INTRINSICS_H_
  17. #ifndef BUILDING_LIBGUILE
  18. #error intrinsics.h is private and uninstalled
  19. #endif
  20. #include <setjmp.h>
  21. #include <libguile/scm.h>
  22. typedef SCM (*scm_t_scm_from_scm_scm_intrinsic) (SCM, SCM);
  23. typedef SCM (*scm_t_scm_from_scm_uimm_intrinsic) (SCM, uint8_t);
  24. typedef void (*scm_t_scm_sz_u32_intrinsic) (SCM, size_t, uint32_t);
  25. typedef SCM (*scm_t_scm_from_scm_intrinsic) (SCM);
  26. typedef double (*scm_t_f64_from_scm_intrinsic) (SCM);
  27. typedef SCM (*scm_t_scm_from_scmn_scmn_intrinsic) (SCM, SCM);
  28. /* If we don't have 64-bit registers, the intrinsics will take and
  29. return 64-bit values by reference. */
  30. #if SIZEOF_UINTPTR_T >= 8
  31. #define INDIRECT_INT64_INTRINSICS 0
  32. #else
  33. #define INDIRECT_INT64_INTRINSICS 1
  34. #endif
  35. #if INDIRECT_INT64_INTRINSICS
  36. typedef void (*scm_t_u64_from_scm_intrinsic) (uint64_t*, SCM);
  37. typedef void (*scm_t_s64_from_scm_intrinsic) (int64_t*, SCM);
  38. typedef SCM (*scm_t_scm_from_u64_intrinsic) (uint64_t*);
  39. typedef SCM (*scm_t_scm_from_s64_intrinsic) (int64_t*);
  40. typedef SCM (*scm_t_scm_from_scm_u64_intrinsic) (SCM, uint64_t*);
  41. typedef double (*scm_t_f64_from_s64_intrinsic) (uint64_t*);
  42. #else
  43. typedef uint64_t (*scm_t_u64_from_scm_intrinsic) (SCM);
  44. typedef int64_t (*scm_t_s64_from_scm_intrinsic) (SCM);
  45. typedef SCM (*scm_t_scm_from_u64_intrinsic) (uint64_t);
  46. typedef SCM (*scm_t_scm_from_s64_intrinsic) (int64_t);
  47. typedef SCM (*scm_t_scm_from_scm_u64_intrinsic) (SCM, uint64_t);
  48. typedef double (*scm_t_f64_from_s64_intrinsic) (uint64_t);
  49. #endif
  50. typedef void (*scm_t_thread_intrinsic) (scm_thread*);
  51. typedef void (*scm_t_thread_scm_intrinsic) (scm_thread*, SCM);
  52. typedef void (*scm_t_thread_scm_scm_intrinsic) (scm_thread*, SCM, SCM);
  53. typedef SCM (*scm_t_scm_from_thread_scm_intrinsic) (scm_thread*, SCM);
  54. typedef int (*scm_t_bool_from_scm_scm_intrinsic) (SCM, SCM);
  55. typedef enum scm_compare (*scm_t_compare_from_scm_scm_intrinsic) (SCM, SCM);
  56. typedef void (*scm_t_thread_sp_intrinsic) (scm_thread*, union scm_vm_stack_element*);
  57. typedef SCM (*scm_t_scm_from_thread_u32_intrinsic) (scm_thread*, uint32_t);
  58. typedef uint32_t (*scm_t_u32_from_thread_u32_u32_intrinsic) (scm_thread*, uint32_t, uint32_t);
  59. typedef void (*scm_t_thread_u32_u32_scm_u8_u8_intrinsic) (scm_thread*, uint32_t,
  60. uint32_t, SCM, uint8_t,
  61. uint8_t);
  62. typedef SCM (*scm_t_scm_from_scm_scm_scmp_sp_intrinsic) (SCM, SCM, SCM*,
  63. const union scm_vm_stack_element*);
  64. typedef void (*scm_t_thread_noreturn_intrinsic) (scm_thread*) SCM_NORETURN;
  65. typedef void (*scm_t_thread_scm_noreturn_intrinsic) (scm_thread*, SCM) SCM_NORETURN;
  66. typedef int (*scm_t_int_from_scm_intrinsic) (SCM);
  67. typedef void (*scm_t_scm_scm_noreturn_intrinsic) (SCM, SCM) SCM_NORETURN;
  68. typedef void (*scm_t_noreturn_intrinsic) (void) SCM_NORETURN;
  69. typedef void (*scm_t_scm_noreturn_intrinsic) (SCM) SCM_NORETURN;
  70. typedef void (*scm_t_u32_noreturn_intrinsic) (uint32_t) SCM_NORETURN;
  71. typedef SCM (*scm_t_scm_from_thread_sz_intrinsic) (scm_thread*, size_t);
  72. typedef SCM (*scm_t_scm_from_thread_intrinsic) (scm_thread*);
  73. typedef void (*scm_t_thread_u8_scm_sp_vra_mra_intrinsic) (scm_thread*,
  74. uint8_t, SCM,
  75. const union scm_vm_stack_element*,
  76. uint32_t*, uint8_t*);
  77. typedef void (*scm_t_thread_mra_intrinsic) (scm_thread*, uint8_t*);
  78. typedef uint32_t* (*scm_t_vra_from_thread_intrinsic) (scm_thread*);
  79. typedef uint8_t* (*scm_t_mra_from_thread_scm_intrinsic) (scm_thread*, SCM);
  80. typedef uint8_t* (*scm_t_mra_from_thread_mra_intrinsic) (scm_thread*, uint8_t*);
  81. typedef SCM (*scm_t_scm_from_ptr_intrinsic) (SCM*);
  82. typedef void (*scm_t_ptr_scm_intrinsic) (SCM*, SCM);
  83. typedef SCM (*scm_t_scm_from_ptr_scm_intrinsic) (SCM*, SCM);
  84. typedef SCM (*scm_t_scm_from_ptr_scm_scm_intrinsic) (SCM*, SCM, SCM);
  85. typedef double (*scm_t_f64_from_f64_intrinsic) (double);
  86. typedef double (*scm_t_f64_from_f64_f64_intrinsic) (double, double);
  87. typedef uint32_t* scm_t_vcode_intrinsic;
  88. typedef void (*scm_t_scm_scm_intrinsic) (SCM, SCM);
  89. typedef void (*scm_t_scm_scm_scm_intrinsic) (SCM, SCM, SCM);
  90. typedef void (*scm_t_scm_uimm_scm_intrinsic) (SCM, uint8_t, SCM);
  91. #define SCM_FOR_ALL_VM_INTRINSICS(M) \
  92. M(scm_from_scm_scm, add, "add", ADD) \
  93. M(scm_from_scm_uimm, add_immediate, "add/immediate", ADD_IMMEDIATE) \
  94. M(scm_from_scm_scm, sub, "sub", SUB) \
  95. M(scm_from_scm_uimm, sub_immediate, "sub/immediate", SUB_IMMEDIATE) \
  96. M(scm_from_scm_scm, mul, "mul", MUL) \
  97. M(scm_from_scm_scm, div, "div", DIV) \
  98. M(scm_from_scm_scm, quo, "quo", QUO) \
  99. M(scm_from_scm_scm, rem, "rem", REM) \
  100. M(scm_from_scm_scm, mod, "mod", MOD) \
  101. M(scm_from_scm_scm, logand, "logand", LOGAND) \
  102. M(scm_from_scm_scm, logior, "logior", LOGIOR) \
  103. M(scm_from_scm_scm, logxor, "logxor", LOGXOR) \
  104. M(scm_sz_u32, string_set_x, "string-set!", STRING_SET_X) \
  105. M(scm_from_scm, string_to_number, "string->number", STRING_TO_NUMBER) \
  106. M(scm_from_scm, string_to_symbol, "string->symbol", STRING_TO_SYMBOL) \
  107. M(scm_from_scm, symbol_to_keyword, "symbol->keyword", SYMBOL_TO_KEYWORD) \
  108. M(scm_from_scm, class_of, "class-of", CLASS_OF) \
  109. M(f64_from_scm, scm_to_f64, "scm->f64", SCM_TO_F64) \
  110. M(u64_from_scm, scm_to_u64, "scm->u64", SCM_TO_U64) \
  111. M(u64_from_scm, scm_to_u64_truncate, "scm->u64/truncate", SCM_TO_U64_TRUNCATE) \
  112. M(s64_from_scm, scm_to_s64, "scm->s64", SCM_TO_S64) \
  113. M(scm_from_u64, u64_to_scm, "u64->scm", U64_TO_SCM) \
  114. M(scm_from_s64, s64_to_scm, "s64->scm", S64_TO_SCM) \
  115. M(scm_from_scm_scm, logsub, "logsub", LOGSUB) \
  116. M(thread_scm_scm, wind, "wind", WIND) \
  117. M(thread, unwind, "unwind", UNWIND) \
  118. M(thread_scm_scm, push_fluid, "push-fluid", PUSH_FLUID) \
  119. M(thread, pop_fluid, "pop-fluid", POP_FLUID) \
  120. M(scm_from_thread_scm, fluid_ref, "fluid-ref", FLUID_REF) \
  121. M(thread_scm_scm, fluid_set_x, "fluid-set!", FLUID_SET_X) \
  122. M(thread_scm, push_dynamic_state, "push-dynamic-state", PUSH_DYNAMIC_STATE) \
  123. M(thread, pop_dynamic_state, "pop-dynamic-state", POP_DYNAMIC_STATE) \
  124. M(scm_from_scm_u64, lsh, "lsh", LSH) \
  125. M(scm_from_scm_u64, rsh, "rsh", RSH) \
  126. M(scm_from_scm_uimm, lsh_immediate, "lsh/immediate", LSH_IMMEDIATE) \
  127. M(scm_from_scm_uimm, rsh_immediate, "rsh/immediate", RSH_IMMEDIATE) \
  128. M(bool_from_scm_scm, heap_numbers_equal_p, "heap-numbers-equal?", HEAP_NUMBERS_EQUAL_P) \
  129. M(compare_from_scm_scm, less_p, "<?", LESS_P) \
  130. M(bool_from_scm_scm, numerically_equal_p, "=?", NUMERICALLY_EQUAL_P) \
  131. M(scm_from_scm_uimm, resolve_module, "resolve-module", RESOLVE_MODULE) \
  132. M(scm_from_scm_scm, module_variable, "module-variable", MODULE_VARIABLE) \
  133. M(scm_from_scm_scm, define_x, "define!", DEFINE_X) \
  134. M(thread_sp, expand_stack, "expand-stack", EXPAND_STACK) \
  135. M(scm_from_thread_u32, cons_rest, "cons-rest", CONS_REST) \
  136. M(u32_from_thread_u32_u32, compute_kwargs_npositional, "compute-kwargs-npositional", COMPUTE_KWARGS_NPOSITIONAL) \
  137. M(thread_u32_u32_scm_u8_u8, bind_kwargs, "bind-kwargs", BIND_KWARGS) \
  138. M(thread_mra, push_interrupt_frame, "push-interrupt-frame", PUSH_INTERRUPT_FRAME) \
  139. M(thread_scm_scm, foreign_call, "foreign-call", FOREIGN_CALL) \
  140. M(thread_scm_noreturn, reinstate_continuation_x, "reinstate-continuation!", REINSTATE_CONTINUATION_X) \
  141. M(scm_from_thread, capture_continuation, "capture-continuation", CAPTURE_CONTINUATION) \
  142. M(mra_from_thread_scm, compose_continuation, "compose-continuation", COMPOSE_CONTINUATION) \
  143. M(thread, expand_apply_argument, "expand-apply-argument", EXPAND_APPLY_ARGUMENT) \
  144. M(mra_from_thread_mra, abort_to_prompt, "abort-to-prompt", ABORT_TO_PROMPT) \
  145. M(scm_scm_noreturn, throw_, "throw", THROW) \
  146. M(scm_scm_noreturn, throw_with_value, "throw/value", THROW_WITH_VALUE) \
  147. M(scm_scm_noreturn, throw_with_value_and_data, "throw/value+data", THROW_WITH_VALUE_AND_DATA) \
  148. M(thread_noreturn, error_wrong_num_args, "wrong-num-args", ERROR_WRONG_NUM_ARGS) \
  149. M(noreturn, error_no_values, "no-values", ERROR_NO_VALUES) \
  150. M(noreturn, error_not_enough_values, "not-enough-values", ERROR_NOT_ENOUGH_VALUES) \
  151. M(u32_noreturn, error_wrong_number_of_values, "wrong-number-of-values", ERROR_WRONG_NUMBER_OF_VALUES) \
  152. M(vra_from_thread, get_callee_vcode, "get-callee-vcode", GET_CALLEE_VCODE) \
  153. M(scm_from_thread_sz, allocate_words, "allocate-words", ALLOCATE_WORDS) \
  154. M(scm_from_thread, current_module, "current-module", CURRENT_MODULE) \
  155. M(thread_u8_scm_sp_vra_mra, push_prompt, "push-prompt", PUSH_PROMPT) \
  156. M(thread_scm, unpack_values_object, "unpack-values-object", UNPACK_VALUES_OBJECT) \
  157. M(vcode, handle_interrupt_code, "%handle-interrupt-code", HANDLE_INTERRUPT_CODE) \
  158. M(scm_from_thread_sz, allocate_words_with_freelist, "allocate-words/freelist", ALLOCATE_WORDS_WITH_FREELIST) \
  159. M(scm_from_scm, abs, "abs", ABS) \
  160. M(scm_from_scm, sqrt, "sqrt", SQRT) \
  161. M(f64_from_f64, fabs, "fabs", FABS) \
  162. M(f64_from_f64, fsqrt, "fsqrt", FSQRT) \
  163. M(scm_from_scm, floor, "floor", FLOOR) \
  164. M(scm_from_scm, ceiling, "ceiling", CEILING) \
  165. M(scm_from_scm, sin, "sin", SIN) \
  166. M(scm_from_scm, cos, "cos", COS) \
  167. M(scm_from_scm, tan, "tan", TAN) \
  168. M(scm_from_scm, asin, "asin", ASIN) \
  169. M(scm_from_scm, acos, "acos", ACOS) \
  170. M(scm_from_scm, atan, "atan", ATAN) \
  171. M(scm_from_scm_scm, atan2, "atan2", ATAN2) \
  172. M(f64_from_f64, ffloor, "ffloor", FFLOOR) \
  173. M(f64_from_f64, fceiling, "fceiling", FCEILING) \
  174. M(f64_from_f64, fsin, "fsin", FSIN) \
  175. M(f64_from_f64, fcos, "fcos", FCOS) \
  176. M(f64_from_f64, ftan, "ftan", FTAN) \
  177. M(f64_from_f64, fasin, "fasin", FASIN) \
  178. M(f64_from_f64, facos, "facos", FACOS) \
  179. M(f64_from_f64, fatan, "fatan", FATAN) \
  180. M(f64_from_f64_f64, fatan2, "fatan2", FATAN2) \
  181. M(scm_from_thread_sz, allocate_pointerless_words, "allocate-pointerless-words", ALLOCATE_POINTERLESS_WORDS) \
  182. M(scm_from_thread_sz, allocate_pointerless_words_with_freelist, "allocate-pointerless-words/freelist", ALLOCATE_POINTERLESS_WORDS_WITH_FREELIST) \
  183. M(scm_from_scm, inexact, "inexact", INEXACT) \
  184. M(f64_from_s64, s64_to_f64, "s64->f64", S64_TO_F64) \
  185. M(scm_from_scm, car, "$car", CAR) \
  186. M(scm_from_scm, cdr, "$cdr", CDR) \
  187. M(scm_scm, set_car_x, "$set-car!", SET_CAR_X) \
  188. M(scm_scm, set_cdr_x, "$set-cdr!", SET_CDR_X) \
  189. M(scm_from_scm, variable_ref, "$variable-ref", VARIABLE_REF) \
  190. M(scm_scm, variable_set_x, "$variable-set!", VARIABLE_SET_X) \
  191. M(scm_from_scm, vector_length, "$vector-length", VECTOR_LENGTH) \
  192. M(scm_from_scm_scm, vector_ref, "$vector-ref", VECTOR_REF) \
  193. M(scm_scm_scm, vector_set_x, "$vector-set!", VECTOR_SET_X) \
  194. M(scm_from_scm_uimm, vector_ref_immediate, "$vector-ref/immediate", VECTOR_REF_IMMEDIATE) \
  195. M(scm_uimm_scm, vector_set_x_immediate, "$vector-set!/immediate", VECTOR_SET_X_IMMEDIATE) \
  196. M(scm_from_scm_scm, allocate_struct, "$allocate-struct", ALLOCATE_STRUCT) \
  197. M(scm_from_scm, struct_vtable, "$struct-vtable", STRUCT_VTABLE) \
  198. M(scm_from_scm_scm, struct_ref, "$struct-ref", STRUCT_REF) \
  199. M(scm_scm_scm, struct_set_x, "$struct-set!", STRUCT_SET_X) \
  200. M(scm_from_scm_uimm, struct_ref_immediate, "$struct-ref/immediate", STRUCT_REF_IMMEDIATE) \
  201. M(scm_uimm_scm, struct_set_x_immediate, "$struct-set!/immediate", STRUCT_SET_X_IMMEDIATE) \
  202. M(scm_from_scm_scm, lookup, "lookup", LOOKUP) \
  203. M(scm_from_scm_scm, lookup_bound, "lookup-bound", LOOKUP_BOUND) \
  204. M(scm_from_scmn_scmn, lookup_bound_public, "lookup-bound-public", LOOKUP_BOUND_PUBLIC) \
  205. M(scm_from_scmn_scmn, lookup_bound_private, "lookup-bound-private", LOOKUP_BOUND_PRIVATE) \
  206. M(scm_from_scm, symbol_to_string, "symbol->string", SYMBOL_TO_STRING) \
  207. /* Add new intrinsics here; also update scm_bootstrap_intrinsics. */
  208. /* Intrinsics prefixed with $ are meant to reduce bytecode size,
  209. notably for the baseline compiler. */
  210. enum scm_vm_intrinsic
  211. {
  212. #define DEFINE_ENUM(type, id, name, ID) SCM_VM_INTRINSIC_##ID,
  213. SCM_FOR_ALL_VM_INTRINSICS(DEFINE_ENUM)
  214. #undef DEFINE_ENUM
  215. SCM_VM_INTRINSIC_COUNT
  216. };
  217. SCM_INTERNAL struct scm_vm_intrinsics
  218. {
  219. #define DEFINE_MEMBER(type, id, name, ID) scm_t_##type##_intrinsic id;
  220. SCM_FOR_ALL_VM_INTRINSICS(DEFINE_MEMBER)
  221. #undef DEFINE_MEMBER
  222. } scm_vm_intrinsics;
  223. SCM_INTERNAL SCM scm_intrinsic_list (void);
  224. SCM_INTERNAL void scm_bootstrap_intrinsics (void);
  225. SCM_INTERNAL void scm_init_intrinsics (void);
  226. #endif /* _SCM_INTRINSICS_H_ */