gsubr.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef SCM_GSUBR_H
  2. #define SCM_GSUBR_H
  3. /* Copyright 1995-1996,1998,2000-2001,2006,2008,2009-2011,2013,2015,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/snarf.h"
  18. /* Subrs
  19. */
  20. /* Max number of args to the C procedure backing a gsubr */
  21. #define SCM_GSUBR_MAX 10
  22. #define SCM_PRIMITIVE_P(x) (SCM_PROGRAM_P (x) && SCM_PROGRAM_IS_PRIMITIVE (x))
  23. #define SCM_PRIMITIVE_GENERIC_P(x) (SCM_PROGRAM_P (x) && SCM_PROGRAM_IS_PRIMITIVE_GENERIC (x))
  24. #define SCM_SUBRF(x) scm_subr_function (x)
  25. #define SCM_SUBR_NAME(x) scm_subr_name (x)
  26. #define SCM_SUBR_GENERIC(x) \
  27. ((SCM *) SCM_POINTER_VALUE (SCM_PROGRAM_FREE_VARIABLE_REF (x, 0)))
  28. #define SCM_SET_SUBR_GENERIC(x, g) \
  29. (*SCM_SUBR_GENERIC (x) = (g))
  30. SCM_INTERNAL uint32_t *
  31. scm_i_alloc_primitive_code_with_instrumentation (size_t uint32_count,
  32. uint32_t **write_ptr);
  33. SCM_INTERNAL int scm_i_primitive_code_p (const uint32_t *code);
  34. SCM_INTERNAL uintptr_t scm_i_primitive_call_ip (SCM subr);
  35. SCM_INTERNAL SCM scm_i_primitive_name (const uint32_t *code);
  36. SCM_API scm_t_subr scm_subr_function (SCM subr);
  37. SCM_INTERNAL scm_t_subr scm_subr_function_by_index (uint32_t subr_idx);
  38. SCM_API SCM scm_subr_name (SCM subr);
  39. SCM_INTERNAL SCM scm_apply_subr (union scm_vm_stack_element *sp,
  40. uint32_t subr_idx, ptrdiff_t nargs);
  41. SCM_API SCM scm_c_make_gsubr (const char *name,
  42. int req, int opt, int rst, scm_t_subr fcn);
  43. SCM_API SCM scm_c_make_gsubr_with_generic (const char *name,
  44. int req, int opt, int rst,
  45. scm_t_subr fcn, SCM *gf);
  46. SCM_API SCM scm_c_define_gsubr (const char *name,
  47. int req, int opt, int rst, scm_t_subr fcn);
  48. SCM_API SCM scm_c_define_gsubr_with_generic (const char *name,
  49. int req, int opt, int rst,
  50. scm_t_subr fcn, SCM *gf);
  51. /* Casting to a function that can take any number of arguments. */
  52. #define SCM_FUNC_CAST_ARBITRARY_ARGS scm_t_subr
  53. #define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  54. SCM_SNARF_HERE(\
  55. SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
  56. SCM FNAME ARGLIST\
  57. )\
  58. SCM_SNARF_INIT(\
  59. scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
  60. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
  61. )\
  62. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  63. /* Always use the generic subr case. */
  64. #define SCM_DEFINE SCM_DEFINE_GSUBR
  65. #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  66. SCM_SNARF_HERE(\
  67. SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
  68. static SCM g_ ## FNAME; \
  69. SCM FNAME ARGLIST\
  70. )\
  71. SCM_SNARF_INIT(\
  72. g_ ## FNAME = SCM_PACK (0); \
  73. scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
  74. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
  75. &g_ ## FNAME); \
  76. )\
  77. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  78. #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  79. SCM_SNARF_HERE(\
  80. SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
  81. SCM FNAME ARGLIST\
  82. )\
  83. SCM_SNARF_INIT(\
  84. scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
  85. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
  86. scm_c_export (s_ ## FNAME, NULL); \
  87. )\
  88. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  89. #define SCM_DEFINE_STATIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  90. SCM_SNARF_HERE(\
  91. static const char s_ ## FNAME [] = PRIMNAME; \
  92. static SCM FNAME ARGLIST\
  93. )\
  94. SCM_SNARF_INIT(\
  95. scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
  96. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
  97. )\
  98. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  99. #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
  100. SCM_SNARF_HERE(SCM_UNUSED static const char RANAME[]=STR) \
  101. SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
  102. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
  103. #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
  104. SCM_SNARF_HERE(SCM_UNUSED static const char RANAME[]=STR) \
  105. SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
  106. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
  107. SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
  108. "implemented by the C function \"" #CFN "\"")
  109. #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
  110. SCM_SNARF_HERE(\
  111. SCM_UNUSED static const char RANAME[]=STR;\
  112. static SCM GF \
  113. )SCM_SNARF_INIT(\
  114. GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
  115. scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
  116. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
  117. )
  118. SCM_INTERNAL void scm_init_gsubr (void);
  119. #endif /* SCM_GSUBR_H */