snarf.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* classes: h_files */
  2. #ifndef SCM_SNARF_H
  3. #define SCM_SNARF_H
  4. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  5. * 2004, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. /* Macros for snarfing initialization actions from C source. */
  23. /* Casting to a function that can take any number of arguments. */
  24. #define SCM_FUNC_CAST_ARBITRARY_ARGS scm_t_subr
  25. #ifdef SCM_ALIGNED
  26. /* We support static allocation of some `SCM' objects. */
  27. # define SCM_SUPPORT_STATIC_ALLOCATION
  28. #endif
  29. /* C preprocessor token concatenation. */
  30. #define scm_i_paste(x, y) x ## y
  31. #define scm_i_paste3(a, b, c) a ## b ## c
  32. /* Generic macros to be used in user macro definitions.
  33. *
  34. * For example, in order to define a macro which creates ints and
  35. * initializes them to the result of foo (), do:
  36. *
  37. * #define SCM_FOO(NAME) \
  38. * SCM_SNARF_HERE (int NAME) \
  39. * SCM_SNARF_INIT (NAME = foo ())
  40. *
  41. * The SCM_SNARF_INIT text goes into the corresponding .x file
  42. * up through the first occurrence of SCM_SNARF_DOC_START on that
  43. * line, if any.
  44. *
  45. * Some debugging options can cause the preprocessor to echo #define
  46. * directives to its output. Keeping the snarfing markers on separate
  47. * lines prevents guile-snarf from inadvertently snarfing the definition
  48. * of SCM_SNARF_INIT if those options are in effect.
  49. */
  50. #ifdef SCM_MAGIC_SNARF_INITS
  51. # define SCM_SNARF_HERE(X)
  52. # define SCM_SNARF_INIT_PREFIX ^^
  53. # define SCM_SNARF_INIT(X) SCM_SNARF_INIT_PREFIX X ^:^
  54. # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  55. #else
  56. # ifdef SCM_MAGIC_SNARF_DOCS
  57. # define SCM_SNARF_HERE(X)
  58. # define SCM_SNARF_INIT(X)
  59. # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING) \
  60. ^^ { \
  61. cname CNAME ^^ \
  62. fname FNAME ^^ \
  63. type TYPE ^^ \
  64. location __FILE__ __LINE__ ^^ \
  65. arglist ARGLIST ^^ \
  66. argsig REQ OPT VAR ^^ \
  67. DOCSTRING ^^ }
  68. # else
  69. # define SCM_SNARF_HERE(X) X
  70. # define SCM_SNARF_INIT(X)
  71. # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  72. # endif
  73. #endif
  74. #define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  75. SCM_SNARF_HERE(\
  76. static const char s_ ## FNAME [] = PRIMNAME; \
  77. SCM FNAME ARGLIST\
  78. )\
  79. SCM_SNARF_INIT(\
  80. scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
  81. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
  82. )\
  83. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  84. #ifdef SCM_SUPPORT_STATIC_ALLOCATION
  85. /* Static subr allocation. */
  86. /* FIXME: how to verify that req + opt + rest < 11, all are positive, etc? */
  87. #define SCM_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  88. SCM_SYMBOL (scm_i_paste (FNAME, __name), PRIMNAME); \
  89. SCM_SNARF_HERE( \
  90. static const char scm_i_paste (s_, FNAME) [] = PRIMNAME; \
  91. SCM_API SCM FNAME ARGLIST; \
  92. SCM_IMMUTABLE_POINTER (scm_i_paste (FNAME, __subr_foreign), \
  93. (scm_t_bits) &FNAME); /* the subr */ \
  94. SCM_STATIC_SUBR_OBJVECT (scm_i_paste (FNAME, __raw_objtable), \
  95. /* FIXME: directly be the foreign */ \
  96. SCM_BOOL_F); \
  97. /* FIXME: be immutable. grr */ \
  98. SCM_STATIC_PROGRAM (scm_i_paste (FNAME, __subr), \
  99. SCM_BOOL_F, \
  100. SCM_PACK (&scm_i_paste (FNAME, __raw_objtable)), \
  101. SCM_BOOL_F); \
  102. SCM FNAME ARGLIST \
  103. ) \
  104. SCM_SNARF_INIT( \
  105. /* Initialize the foreign. */ \
  106. scm_i_paste (FNAME, __raw_objtable)[1] = scm_i_paste (FNAME, __subr_foreign); \
  107. /* Initialize the procedure name (an interned symbol). */ \
  108. scm_i_paste (FNAME, __raw_objtable)[2] = scm_i_paste (FNAME, __name); \
  109. /* Initialize the objcode trampoline. */ \
  110. SCM_SET_CELL_OBJECT (scm_i_paste (FNAME, __subr), 1, \
  111. scm_subr_objcode_trampoline (REQ, OPT, VAR)); \
  112. \
  113. /* Define the subr. */ \
  114. scm_define (scm_i_paste (FNAME, __name), scm_i_paste (FNAME, __subr)); \
  115. ) \
  116. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  117. #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
  118. /* Always use the generic subr case. */
  119. #define SCM_DEFINE SCM_DEFINE_GSUBR
  120. #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
  121. #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  122. SCM_SNARF_HERE(\
  123. static const char s_ ## FNAME [] = PRIMNAME; \
  124. static SCM g_ ## FNAME; \
  125. SCM FNAME ARGLIST\
  126. )\
  127. SCM_SNARF_INIT(\
  128. g_ ## FNAME = SCM_PACK (0); \
  129. scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
  130. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
  131. &g_ ## FNAME); \
  132. )\
  133. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  134. #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
  135. SCM_SNARF_HERE(\
  136. static const char s_ ## FNAME [] = PRIMNAME; \
  137. SCM FNAME ARGLIST\
  138. )\
  139. SCM_SNARF_INIT(\
  140. scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
  141. (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
  142. scm_c_export (s_ ## FNAME, NULL); \
  143. )\
  144. SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
  145. #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
  146. SCM_SNARF_HERE(static const char RANAME[]=STR) \
  147. SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
  148. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
  149. #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
  150. SCM_SNARF_HERE(static const char RANAME[]=STR) \
  151. SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
  152. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
  153. SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
  154. "implemented by the C function \"" #CFN "\"")
  155. #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
  156. SCM_SNARF_HERE(\
  157. static const char RANAME[]=STR;\
  158. static SCM GF \
  159. )SCM_SNARF_INIT(\
  160. GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
  161. scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
  162. (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
  163. )
  164. #ifdef SCM_SUPPORT_STATIC_ALLOCATION
  165. # define SCM_SYMBOL(c_name, scheme_name) \
  166. SCM_SNARF_HERE( \
  167. SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
  168. static SCM c_name) \
  169. SCM_SNARF_INIT( \
  170. c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
  171. )
  172. # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
  173. SCM_SNARF_HERE( \
  174. SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
  175. SCM c_name) \
  176. SCM_SNARF_INIT( \
  177. c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
  178. )
  179. #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
  180. # define SCM_SYMBOL(c_name, scheme_name) \
  181. SCM_SNARF_HERE(static SCM c_name) \
  182. SCM_SNARF_INIT(c_name = scm_from_utf8_symbol (scheme_name))
  183. # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
  184. SCM_SNARF_HERE(SCM c_name) \
  185. SCM_SNARF_INIT(c_name = scm_from_utf8_symbol (scheme_name))
  186. #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
  187. #define SCM_KEYWORD(c_name, scheme_name) \
  188. SCM_SNARF_HERE(static SCM c_name) \
  189. SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
  190. #define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
  191. SCM_SNARF_HERE(SCM c_name) \
  192. SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
  193. #define SCM_VARIABLE(c_name, scheme_name) \
  194. SCM_SNARF_HERE(static SCM c_name) \
  195. SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
  196. #define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
  197. SCM_SNARF_HERE(SCM c_name) \
  198. SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
  199. #define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
  200. SCM_SNARF_HERE(static SCM c_name) \
  201. SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
  202. #define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
  203. SCM_SNARF_HERE(SCM c_name) \
  204. SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
  205. #define SCM_MUTEX(c_name) \
  206. SCM_SNARF_HERE(static scm_t_mutex c_name) \
  207. SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
  208. #define SCM_GLOBAL_MUTEX(c_name) \
  209. SCM_SNARF_HERE(scm_t_mutex c_name) \
  210. SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
  211. #define SCM_REC_MUTEX(c_name) \
  212. SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
  213. SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
  214. #define SCM_GLOBAL_REC_MUTEX(c_name) \
  215. SCM_SNARF_HERE(scm_t_rec_mutex c_name) \
  216. SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
  217. #define SCM_SMOB(tag, scheme_name, size) \
  218. SCM_SNARF_HERE(static scm_t_bits tag) \
  219. SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
  220. #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
  221. SCM_SNARF_HERE(scm_t_bits tag) \
  222. SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
  223. #define SCM_SMOB_MARK(tag, c_name, arg) \
  224. SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
  225. SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
  226. #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
  227. SCM_SNARF_HERE(SCM c_name(SCM arg)) \
  228. SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
  229. #define SCM_SMOB_FREE(tag, c_name, arg) \
  230. SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
  231. SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
  232. #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
  233. SCM_SNARF_HERE(size_t c_name(SCM arg)) \
  234. SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
  235. #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
  236. SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
  237. SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
  238. #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
  239. SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
  240. SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
  241. #define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
  242. SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
  243. SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
  244. #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
  245. SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
  246. SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
  247. #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
  248. SCM_SNARF_HERE(static SCM c_name arglist) \
  249. SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
  250. #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
  251. SCM_SNARF_HERE(SCM c_name arglist) \
  252. SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
  253. /* Low-level snarfing for static memory allocation. */
  254. #ifdef SCM_SUPPORT_STATIC_ALLOCATION
  255. #define SCM_IMMUTABLE_CELL(c_name, car, cdr) \
  256. static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
  257. c_name ## _raw_scell = \
  258. { \
  259. SCM_PACK (car), \
  260. SCM_PACK (cdr) \
  261. }; \
  262. static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_scell)
  263. #define SCM_IMMUTABLE_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
  264. static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
  265. c_name ## _raw_cell [2] = \
  266. { \
  267. { SCM_PACK (car), SCM_PACK (cbr) }, \
  268. { SCM_PACK (ccr), SCM_PACK (cdr) } \
  269. }; \
  270. static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_cell)
  271. #define SCM_STATIC_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
  272. static SCM_ALIGNED (8) SCM_UNUSED scm_t_cell \
  273. c_name ## _raw_cell [2] = \
  274. { \
  275. { SCM_PACK (car), SCM_PACK (cbr) }, \
  276. { SCM_PACK (ccr), SCM_PACK (cdr) } \
  277. }; \
  278. static SCM_UNUSED SCM c_name = SCM_PACK (& c_name ## _raw_cell)
  279. #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
  280. static SCM_UNUSED const \
  281. struct \
  282. { \
  283. scm_t_bits word_0; \
  284. scm_t_bits word_1; \
  285. const char buffer[sizeof (contents)]; \
  286. } \
  287. c_name = \
  288. { \
  289. scm_tc7_stringbuf | SCM_I_STRINGBUF_F_SHARED, \
  290. sizeof (contents) - 1, \
  291. contents \
  292. }
  293. #define SCM_IMMUTABLE_STRING(c_name, contents) \
  294. SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
  295. SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
  296. scm_tc7_ro_string, \
  297. (scm_t_bits) &scm_i_paste (c_name, \
  298. _stringbuf), \
  299. (scm_t_bits) 0, \
  300. (scm_t_bits) (sizeof (contents) - 1))
  301. #define SCM_IMMUTABLE_POINTER(c_name, ptr) \
  302. SCM_IMMUTABLE_CELL (c_name, scm_tc7_pointer, ptr)
  303. /* for primitive-generics, add a foreign to the end */
  304. #define SCM_STATIC_SUBR_OBJVECT(c_name, foreign) \
  305. static SCM_ALIGNED (8) SCM c_name[3] = \
  306. { \
  307. SCM_PACK (scm_tc7_vector | (2 << 8)), \
  308. foreign, \
  309. SCM_BOOL_F /* the name */ \
  310. }
  311. #define SCM_STATIC_PROGRAM(c_name, objcode, objtable, freevars) \
  312. static SCM_ALIGNED (8) SCM_UNUSED SCM \
  313. scm_i_paste (c_name, _raw_cell)[] = \
  314. { \
  315. SCM_PACK (scm_tc7_program | SCM_F_PROGRAM_IS_PRIMITIVE), \
  316. objcode, \
  317. objtable, \
  318. freevars \
  319. }; \
  320. static SCM_UNUSED const SCM c_name = \
  321. SCM_PACK (& scm_i_paste (c_name, _raw_cell))
  322. #endif /* SCM_SUPPORT_STATIC_ALLOCATION */
  323. /* Documentation. */
  324. #ifdef SCM_MAGIC_SNARF_DOCS
  325. #undef SCM_ASSERT
  326. #define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
  327. #endif /* SCM_MAGIC_SNARF_DOCS */
  328. #endif /* SCM_SNARF_H */
  329. /*
  330. Local Variables:
  331. c-file-style: "gnu"
  332. End:
  333. */