scm.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. #ifndef SCM_SCM_H
  2. #define SCM_SCM_H
  3. /* Copyright 1995-2004,2006-2015,2017-2019,2023
  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. /* This is the central header for Guile that defines how Scheme values
  18. are represented. Enjoy the read! */
  19. #include <stdint.h>
  20. #include "libguile/scmconfig.h"
  21. /* The value of SCM_DEBUG determines the default for most of the not yet
  22. defined debugging options. This allows, for example, to enable most
  23. of the debugging options by simply defining SCM_DEBUG as 1. */
  24. #ifndef SCM_DEBUG
  25. #define SCM_DEBUG 0
  26. #endif
  27. /* If SCM_DEBUG_PAIR_ACCESSES is set to 1, accesses to cons cells will
  28. be exhaustively checked. Note: If this option is enabled, guile
  29. will run slower than normally. */
  30. #ifndef SCM_DEBUG_PAIR_ACCESSES
  31. #define SCM_DEBUG_PAIR_ACCESSES SCM_DEBUG
  32. #endif
  33. /* If SCM_DEBUG_REST_ARGUMENT is set to 1, functions that take rest
  34. arguments will check whether the rest arguments are actually passed
  35. as a proper list. Otherwise, if SCM_DEBUG_REST_ARGUMENT is 0,
  36. functions that take rest arguments will take it for granted that
  37. these are passed as a proper list. */
  38. #ifndef SCM_DEBUG_REST_ARGUMENT
  39. #define SCM_DEBUG_REST_ARGUMENT SCM_DEBUG
  40. #endif
  41. /* The macro SCM_DEBUG_TYPING_STRICTNESS indicates what level of type
  42. checking shall be performed with respect to the use of the SCM
  43. datatype. The macro may be defined to one of the values 0, 1 and 2.
  44. A value of 0 means that there will be no compile time type checking,
  45. since the SCM datatype will be declared as an integral type. This
  46. setting should only be used on systems, where casting from integral
  47. types to pointers may lead to loss of bit information.
  48. A value of 1 means that there will an intermediate level of compile
  49. time type checking, since the SCM datatype will be declared as a
  50. pointer to an undefined struct. This setting is the default, since
  51. it does not cost anything in terms of performance or code size.
  52. A value of 2 provides a maximum level of compile time type checking
  53. since the SCM datatype will be declared as a struct. This setting
  54. should be used for _compile time_ type checking only, since the
  55. compiled result is likely to be quite inefficient. The right way to
  56. make use of this option is to do a 'make clean; make
  57. CFLAGS=-DSCM_DEBUG_TYPING_STRICTNESS=2', fix your errors, and then do
  58. 'make clean; make'. */
  59. #ifndef SCM_DEBUG_TYPING_STRICTNESS
  60. #define SCM_DEBUG_TYPING_STRICTNESS 1
  61. #endif
  62. /* Guile as of today can only work on systems which fulfill at least the
  63. following requirements:
  64. - scm_t_bits and SCM variables have at least 32 bits.
  65. Guile's type system is based on this assumption.
  66. - sizeof (scm_t_bits) >= sizeof (void*) and sizeof (SCM) >= sizeof (void*)
  67. Guile's type system is based on this assumption, since it must be
  68. possible to store pointers to cells on the heap in scm_t_bits and
  69. SCM variables.
  70. - sizeof (scm_t_bits) >= 4 and sizeof (scm_t_bits) is a power of 2.
  71. Guile's type system is based on this assumption. In particular, it
  72. is assumed that cells, i. e. pairs of scm_t_bits variables, are
  73. eight-byte aligned. This is because three bits of a scm_t_bits
  74. variable that is holding a pointer to a cell on the heap must be
  75. available for storing type data.
  76. - sizeof (scm_t_bits) <= sizeof (void*) and sizeof (SCM) <= sizeof (void*)
  77. In some parts of guile, scm_t_bits and SCM variables are passed to
  78. functions as void* arguments. Together with the requirement above,
  79. this requires a one-to-one correspondence between the size of a
  80. void* and the sizes of scm_t_bits and SCM variables.
  81. - numbers are encoded using two's complement.
  82. The implementation of the bitwise Scheme-level operations is based on
  83. this assumption. */
  84. /* In the beginning was the Word:
  85. For the representation of scheme objects and their handling, Guile
  86. provides two types: scm_t_bits and SCM.
  87. - scm_t_bits values can hold bit patterns of non-objects and objects:
  88. Non-objects -- in this case the value may not be changed into a SCM
  89. value in any way.
  90. Objects -- in this case the value may be changed into a SCM value
  91. using the SCM_PACK macro.
  92. - SCM values can hold proper scheme objects only. They can be
  93. changed into a scm_t_bits value using the SCM_UNPACK macro.
  94. When working in the domain of scm_t_bits values, programmers must
  95. keep track of any scm_t_bits value they create that is not a proper
  96. scheme object. This makes sure that in the domain of SCM values
  97. developers can rely on the fact that they are dealing with proper
  98. scheme objects only. Thus, the distinction between scm_t_bits and
  99. SCM values helps to identify those parts of the code where special
  100. care has to be taken not to create bad SCM values. */
  101. /* For dealing with the bit level representation of scheme objects we
  102. define scm_t_bits. */
  103. typedef intptr_t scm_t_signed_bits;
  104. typedef uintptr_t scm_t_bits;
  105. #define SCM_T_SIGNED_BITS_MAX INTPTR_MAX
  106. #define SCM_T_SIGNED_BITS_MIN INTPTR_MIN
  107. #define SCM_T_BITS_MAX UINTPTR_MAX
  108. /* But as external interface, we define SCM, which may, according to the
  109. desired level of type checking, be defined in several ways. */
  110. #if (SCM_DEBUG_TYPING_STRICTNESS == 2)
  111. typedef union SCM { struct { scm_t_bits n; } n; } SCM;
  112. # define SCM_UNPACK(x) ((x).n.n)
  113. # define SCM_PACK(x) ((SCM) { { (scm_t_bits) (x) } })
  114. #elif (SCM_DEBUG_TYPING_STRICTNESS == 1)
  115. /* This is the default, which provides an intermediate level of compile
  116. time type checking while still resulting in very efficient code. */
  117. typedef struct scm_unused_struct { char scm_unused_field; } *SCM;
  118. /* The 0?: constructions makes sure that the code is never executed, and
  119. that there is no performance hit. However, the alternative is
  120. compiled, and does generate a warning when used with the wrong
  121. pointer type. We use a volatile pointer type to avoid warnings from
  122. clang.
  123. The Tru64 and ia64-hp-hpux11.23 compilers fail on `case (0?0=0:x)'
  124. statements, so for them type-checking is disabled. */
  125. # if defined __DECC || defined __HP_cc
  126. # define SCM_UNPACK(x) ((scm_t_bits) (x))
  127. # else
  128. # define SCM_UNPACK(x) ((scm_t_bits) (0? (*(volatile SCM *)0=(x)): x))
  129. # endif
  130. /* There is no typechecking on SCM_PACK, since all kinds of types
  131. (unsigned long, void*) go in SCM_PACK. */
  132. # define SCM_PACK(x) ((SCM) (x))
  133. #else
  134. /* This should be used as a fall back solution for machines on which
  135. casting to a pointer may lead to loss of bit information, e. g. in
  136. the three least significant bits. */
  137. typedef scm_t_bits SCM;
  138. # define SCM_UNPACK(x) (x)
  139. # define SCM_PACK(x) ((SCM) (x))
  140. #endif
  141. /* Packing SCM objects into and out of pointers. */
  142. #define SCM_UNPACK_POINTER(x) ((scm_t_bits *) (SCM_UNPACK (x)))
  143. #define SCM_PACK_POINTER(x) (SCM_PACK ((scm_t_bits) (x)))
  144. /* SCM values can not be compared by using the operator ==. Use the
  145. following macro instead, which is the equivalent of the scheme
  146. predicate 'eq?'. */
  147. #define scm_is_eq(x, y) (SCM_UNPACK (x) == SCM_UNPACK (y))
  148. /* Representation of scheme objects:
  149. Guile's type system is designed to work on systems where scm_t_bits
  150. and SCM variables consist of at least 32 bits. The objects that a
  151. SCM variable can represent belong to one of the following two major
  152. categories:
  153. - Immediates -- meaning that the SCM variable contains an entire
  154. Scheme object. That means, all the object's data (including the
  155. type tagging information that is required to identify the object's
  156. type) must fit into 32 bits.
  157. - Heap objects -- meaning that the SCM variable holds a pointer into
  158. the heap. On systems where a pointer needs more than 32 bits this
  159. means that scm_t_bits and SCM variables need to be large enough to
  160. hold such pointers. In contrast to immediates, the data associated
  161. with a heap object can consume arbitrary amounts of memory.
  162. The 'heap' is the memory area that is under control of Guile's
  163. garbage collector. It holds allocated memory of various sizes. The
  164. impact on the runtime type system is that Guile needs to be able to
  165. determine the type of an object given the pointer. Usually the way
  166. that Guile does this is by storing a "type tag" in the first word of
  167. the object.
  168. Some objects are common enough that they get special treatment.
  169. Since Guile guarantees that the address of a GC-allocated object on
  170. the heap is 8-byte aligned, Guile can play tricks with the lower 3
  171. bits. That is, since heap objects encode a pointer to an
  172. 8-byte-aligned pointer, the three least significant bits of a SCM can
  173. be used to store additional information. The bits are used to store
  174. information about the object's type and thus are called tc3-bits,
  175. where tc stands for type-code.
  176. For a given SCM value, the distinction whether it holds an immediate
  177. or heap object is based on the tc3-bits (see above) of its scm_t_bits
  178. equivalent: If the tc3-bits equal #b000, then the SCM value holds a
  179. heap object, and the scm_t_bits variable's value is just the pointer
  180. to the heap cell.
  181. Summarized, the data of a scheme object that is represented by a SCM
  182. variable consists of a) the SCM variable itself, b) in case of heap
  183. objects memory that the SCM object points to, c) in case of heap
  184. objects potentially additional data outside of the heap (like for
  185. example malloc'ed data), and d) in case of heap objects potentially
  186. additional data inside of the heap, since data stored in b) and c)
  187. may hold references to other cells.
  188. Immediates
  189. Operations on immediate objects can typically be processed faster
  190. than on heap objects. The reason is that the object's data can be
  191. extracted directly from the SCM variable (or rather a corresponding
  192. scm_t_bits variable), instead of having to perform additional memory
  193. accesses to obtain the object's data from the heap. In order to get
  194. the best possible performance frequently used data types should be
  195. realized as immediates. This is, as has been mentioned above, only
  196. possible if the objects can be represented with 32 bits (including
  197. type tagging).
  198. In Guile, the following data types and special objects are realized
  199. as immediates: booleans, characters, small integers (see below), the
  200. empty list, the end of file object, the 'unspecified' object (which
  201. is delivered as a return value by functions for which the return
  202. value is unspecified), a 'nil' object used in the elisp-compatibility
  203. mode and certain other 'special' objects which are only used
  204. internally in Guile.
  205. Integers in Guile can be arbitrarily large. On the other hand,
  206. integers are one of the most frequently used data types. Especially
  207. integers with less than 32 bits are commonly used. Thus, internally
  208. and transparently for application code guile distinguishes between
  209. small and large integers. Whether an integer is a large or a small
  210. integer depends on the number of bits needed to represent its value.
  211. Small integers are those which can be represented as immediates.
  212. Since they don't require more than a fixed number of bits for their
  213. representation, they are also known as 'fixnums'.
  214. The tc3-combinations #b010 and #b110 are used to represent small
  215. integers, which allows to use the most significant bit of the
  216. tc3-bits to be part of the integer value being represented. This
  217. means that all integers with up to 30 bits (including one bit for the
  218. sign) can be represented as immediates. On systems where SCM and
  219. scm_t_bits variables hold more than 32 bits, the amount of bits
  220. usable for small integers will even be larger. The tc3-code #b100 is
  221. shared among booleans, characters and the other special objects
  222. listed above.
  223. Heap Objects
  224. All object types not mentioned above in the list of immediate objects
  225. are represented as heap objects. The amount of memory referenced by
  226. a heap object depends on the object's type, namely on the set of
  227. attributes that have to be stored with objects of that type. Every
  228. heap object type is allowed to define its own layout and
  229. interpretation of the data stored in its cell (with some
  230. restrictions, see below).
  231. One of the design goals of guile's type system is to make it possible
  232. to store a scheme pair with as little memory usage as possible. The
  233. minimum amount of memory that is required to store two scheme objects
  234. (car and cdr of a pair) is the amount of memory required by two
  235. scm_t_bits or SCM variables. Therefore pairs in guile are stored in
  236. two words, and are tagged with a bit pattern in the SCM value, not
  237. with a type tag on the heap.
  238. Garbage collection
  239. During garbage collection, unreachable objects on the heap will be
  240. freed. To determine the set of reachable objects, by default, the GC
  241. just traces all words in all heap objects. It is possible to
  242. register custom tracing ("marking") procedures.
  243. If an object is unreachable, by default, the GC just notes this fact
  244. and moves on. Later allocations will clear out the memory associated
  245. with the object, and re-use it. It is possible to register custom
  246. finalizers, however.
  247. Run-time type introspection
  248. Guile's type system is designed to make it possible to determine a
  249. the type of a heap object from the object's first scm_t_bits
  250. variable. (Given a SCM variable X holding a heap object, the macro
  251. SCM_CELL_TYPE(X) will deliver the corresponding object's first
  252. scm_t_bits variable.)
  253. If the object holds a scheme pair, then we already know that the
  254. first scm_t_bits variable of the cell will hold a scheme object with
  255. one of the following tc3-codes: #b000 (heap object), #b010 (small
  256. integer), #b110 (small integer), #b100 (non-integer immediate). All
  257. these tc3-codes have in common, that their least significant bit is
  258. #b0. This fact is used by the garbage collector to identify cells
  259. that hold pairs. The remaining tc3-codes are assigned as follows:
  260. #b001 (class instance or, more precisely, a struct, of which a class
  261. instance is a special case), #b011 (closure), #b101/#b111 (all
  262. remaining heap object types).
  263. Summary of type codes of scheme objects (SCM variables)
  264. Here is a summary of tagging bits as they might occur in a scheme
  265. object. The notation is as follows: tc stands for type code as
  266. before, tc<n> with n being a number indicates a type code formed by
  267. the n least significant bits of the SCM variables corresponding
  268. scm_t_bits value.
  269. Note that (as has been explained above) tc1==1 can only occur in the
  270. first scm_t_bits variable of a cell belonging to a heap object that
  271. is not a pair. For an explanation of the tc tags with tc1==1, see
  272. the next section with the summary of the type codes on the heap.
  273. tc1:
  274. 0: For scheme objects, tc1==0 must be fulfilled.
  275. (1: This can never be the case for a scheme object.)
  276. tc2:
  277. 00: Either a heap object or some non-integer immediate
  278. (01: This can never be the case for a scheme object.)
  279. 10: Small integer
  280. (11: This can never be the case for a scheme object.)
  281. tc3:
  282. 000: a heap object (pair, closure, class instance etc.)
  283. (001: This can never be the case for a scheme object.)
  284. 010: an even small integer (least significant bit is 0).
  285. (011: This can never be the case for a scheme object.)
  286. 100: Non-integer immediate
  287. (101: This can never be the case for a scheme object.)
  288. 110: an odd small integer (least significant bit is 1).
  289. (111: This can never be the case for a scheme object.)
  290. The remaining bits of the heap objects form the pointer to the heap
  291. cell. The remaining bits of the small integers form the integer's
  292. value and sign. Thus, the only scheme objects for which a further
  293. subdivision is of interest are the ones with tc3==100.
  294. tc8 (for objects with tc3==100):
  295. 00000-100: special objects ('flags')
  296. 00001-100: characters
  297. 00010-100: unused
  298. 00011-100: unused
  299. Summary of type codes on the heap
  300. Here is a summary of tagging in scm_t_bits values as they might occur
  301. in the first scm_t_bits variable of a heap cell.
  302. tc1:
  303. 0: the cell belongs to a pair.
  304. 1: the cell belongs to a non-pair.
  305. tc2:
  306. 00: the cell belongs to a pair with no short integer in its car.
  307. 01: the cell belongs to a non-pair (struct or some other heap object).
  308. 10: the cell belongs to a pair with a short integer in its car.
  309. 11: the cell belongs to a non-pair (closure or some other heap object).
  310. tc3:
  311. 000: the cell belongs to a pair with a heap object in its car.
  312. 001: the cell belongs to a struct
  313. 010: the cell belongs to a pair with an even short integer in its car.
  314. 011: the cell belongs to a closure
  315. 100: the cell belongs to a pair with a non-integer immediate in its car.
  316. 101: the cell belongs to some other heap object.
  317. 110: the cell belongs to a pair with an odd short integer in its car.
  318. 111: the cell belongs to some other heap object.
  319. tc7 (for tc3==1x1):
  320. See below for the list of types. Three special tc7-codes are of
  321. interest: numbers, ports and smobs in fact each represent
  322. collections of types, which are subdivided using tc16-codes.
  323. tc16 (for tc7==scm_tc7_smob):
  324. The largest part of the space of smob types is not subdivided in a
  325. predefined way, since smobs can be added arbitrarily by user C
  326. code. */
  327. /* Checking if a SCM variable holds an immediate or a heap object. This
  328. check can either be performed by checking for tc3==000 or tc3==00x,
  329. since for a SCM variable it is known that tc1==0. */
  330. #define SCM_IMP(x) (6 & SCM_UNPACK (x))
  331. #define SCM_NIMP(x) (!SCM_IMP (x))
  332. #define SCM_HEAP_OBJECT_P(x) (SCM_NIMP (x))
  333. /* Checking if a SCM variable holds an immediate integer: See numbers.h
  334. for the definition of the following macros: SCM_I_FIXNUM_BIT,
  335. SCM_MOST_POSITIVE_FIXNUM, SCM_I_INUMP, SCM_I_MAKINUM, SCM_I_INUM. */
  336. /* Checking if a SCM variable holds a pair (for historical reasons, in
  337. Guile also known as a cons-cell): This is done by first checking that
  338. the SCM variable holds a heap object, and second, by checking that
  339. tc1==0 holds for the SCM_CELL_TYPE of the SCM variable. */
  340. #define SCM_I_CONSP(x) (!SCM_IMP (x) && ((1 & SCM_CELL_TYPE (x)) == 0))
  341. /* Definitions for tc2: */
  342. #define scm_tc2_int 2
  343. /* Definitions for tc3: */
  344. #define SCM_ITAG3(x) (7 & SCM_UNPACK (x))
  345. #define SCM_TYP3(x) (7 & SCM_CELL_TYPE (x))
  346. #define scm_tc3_cons 0
  347. #define scm_tc3_struct 1
  348. #define scm_tc3_int_1 (scm_tc2_int + 0)
  349. #define scm_tc3_unused 3
  350. #define scm_tc3_imm24 4
  351. #define scm_tc3_tc7_1 5
  352. #define scm_tc3_int_2 (scm_tc2_int + 4)
  353. #define scm_tc3_tc7_2 7
  354. /* Definitions for tc7: */
  355. #define SCM_ITAG7(x) (0x7f & SCM_UNPACK (x))
  356. #define SCM_TYP7(x) (0x7f & SCM_CELL_TYPE (x))
  357. #define SCM_HAS_HEAP_TYPE(x, type, tag) \
  358. (SCM_NIMP (x) && type (x) == (tag))
  359. #define SCM_HAS_TYP7(x, tag) (SCM_HAS_HEAP_TYPE (x, SCM_TYP7, tag))
  360. /* These type codes form part of the ABI and cannot be changed in a
  361. stable series. The low bits of each must have the tc3 of a heap
  362. object type code (see above). If you do change them in a development
  363. series, change them also in (system vm assembler) and (system base
  364. types). Bonus points if you change the build to define these tag
  365. values in only one place! */
  366. #define scm_tc7_symbol 0x05
  367. #define scm_tc7_variable 0x07
  368. #define scm_tc7_vector 0x0d
  369. #define scm_tc7_wvect 0x0f
  370. #define scm_tc7_string 0x15
  371. #define scm_tc7_number 0x17
  372. #define scm_tc7_hashtable 0x1d
  373. #define scm_tc7_pointer 0x1f
  374. #define scm_tc7_fluid 0x25
  375. #define scm_tc7_stringbuf 0x27
  376. #define scm_tc7_dynamic_state 0x2d
  377. #define scm_tc7_frame 0x2f
  378. #define scm_tc7_keyword 0x35
  379. #define scm_tc7_atomic_box 0x37
  380. #define scm_tc7_syntax 0x3d
  381. #define scm_tc7_values 0x3f
  382. #define scm_tc7_program 0x45
  383. #define scm_tc7_vm_cont 0x47
  384. #define scm_tc7_bytevector 0x4d
  385. #define scm_tc7_unused_4f 0x4f
  386. #define scm_tc7_weak_set 0x55
  387. #define scm_tc7_weak_table 0x57
  388. #define scm_tc7_array 0x5d
  389. #define scm_tc7_bitvector 0x5f
  390. #define scm_tc7_unused_65 0x65
  391. #define scm_tc7_unused_67 0x67
  392. #define scm_tc7_unused_6d 0x6d
  393. #define scm_tc7_unused_6f 0x6f
  394. #define scm_tc7_unused_75 0x75
  395. #define scm_tc7_smob 0x77
  396. #define scm_tc7_port 0x7d
  397. #define scm_tc7_unused_7f 0x7f
  398. /* Definitions for tc16: */
  399. #define SCM_TYP16(x) (0xffff & SCM_CELL_TYPE (x))
  400. #define SCM_HAS_TYP16(x, tag) (SCM_HAS_HEAP_TYPE (x, SCM_TYP16, tag))
  401. #define SCM_TYP16_PREDICATE(tag, x) (SCM_HAS_TYP16 (x, tag))
  402. /* Immediate values (besides fixnums). */
  403. enum scm_tc8_tags
  404. {
  405. scm_tc8_flag = scm_tc3_imm24 + 0x00, /* special objects ('flags') */
  406. scm_tc8_char = scm_tc3_imm24 + 0x08, /* characters */
  407. scm_tc8_unused_0 = scm_tc3_imm24 + 0x10,
  408. scm_tc8_unused_1 = scm_tc3_imm24 + 0x18
  409. };
  410. #define SCM_ITAG8(X) (SCM_UNPACK (X) & 0xff)
  411. #define SCM_MAKE_ITAG8_BITS(X, TAG) (((X) << 8) + TAG)
  412. #define SCM_MAKE_ITAG8(X, TAG) (SCM_PACK (SCM_MAKE_ITAG8_BITS (X, TAG)))
  413. #define SCM_ITAG8_DATA(X) (SCM_UNPACK (X) >> 8)
  414. /* Flags (special objects). The indices of the flags must agree with
  415. the declarations in print.c: iflagnames. */
  416. #define SCM_IFLAGP(n) (SCM_ITAG8 (n) == scm_tc8_flag)
  417. #define SCM_MAKIFLAG_BITS(n) (SCM_MAKE_ITAG8_BITS ((n), scm_tc8_flag))
  418. #define SCM_IFLAGNUM(n) (SCM_ITAG8_DATA (n))
  419. /*
  420. IMPORTANT NOTE regarding IFLAG numbering!!!
  421. Several macros depend upon careful IFLAG numbering of SCM_BOOL_F,
  422. SCM_BOOL_T, SCM_ELISP_NIL, SCM_EOL, and the two SCM_XXX_*_DONT_USE
  423. constants. In particular:
  424. - SCM_BOOL_F and SCM_BOOL_T must differ in exactly one bit position.
  425. (used to implement scm_is_bool_and_not_nil, aka scm_is_bool)
  426. - SCM_ELISP_NIL and SCM_BOOL_F must differ in exactly one bit
  427. position. (used to implement scm_is_false_or_nil and
  428. scm_is_true_and_not_nil)
  429. - SCM_ELISP_NIL and SCM_EOL must differ in exactly one bit position.
  430. (used to implement scm_is_null_or_nil)
  431. - SCM_ELISP_NIL, SCM_BOOL_F, SCM_EOL,
  432. SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE must all be equal except for
  433. two bit positions. (used to implement scm_is_lisp_false)
  434. - SCM_ELISP_NIL, SCM_BOOL_F, SCM_BOOL_T,
  435. SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0 must all be equal except for two
  436. bit positions. (used to implement scm_is_bool_or_nil)
  437. These properties allow the aforementioned macros to be implemented by
  438. bitwise ANDing with a mask and then comparing with a constant, using
  439. as a common basis the macro SCM_MATCHES_BITS_IN_COMMON, defined
  440. below. The properties are checked at compile-time using `verify'
  441. macros near the top of boolean.c and pairs.c. */
  442. #define SCM_BOOL_F_BITS SCM_MAKIFLAG_BITS (0)
  443. #define SCM_ELISP_NIL_BITS SCM_MAKIFLAG_BITS (1)
  444. #define SCM_BOOL_F SCM_PACK (SCM_BOOL_F_BITS)
  445. #define SCM_ELISP_NIL SCM_PACK (SCM_ELISP_NIL_BITS)
  446. #ifdef BUILDING_LIBGUILE
  447. #define SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE SCM_MAKIFLAG_BITS (2)
  448. #endif
  449. #define SCM_EOL_BITS SCM_MAKIFLAG_BITS (3)
  450. #define SCM_BOOL_T_BITS SCM_MAKIFLAG_BITS (4)
  451. #define SCM_EOL SCM_PACK (SCM_EOL_BITS)
  452. #define SCM_BOOL_T SCM_PACK (SCM_BOOL_T_BITS)
  453. #ifdef BUILDING_LIBGUILE
  454. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0 SCM_MAKIFLAG_BITS (5)
  455. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_1 SCM_MAKIFLAG_BITS (6)
  456. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_2 SCM_MAKIFLAG_BITS (7)
  457. #endif
  458. #define SCM_UNSPECIFIED_BITS SCM_MAKIFLAG_BITS (8)
  459. #define SCM_UNDEFINED_BITS SCM_MAKIFLAG_BITS (9)
  460. #define SCM_EOF_VAL_BITS SCM_MAKIFLAG_BITS (10)
  461. #define SCM_UNSPECIFIED SCM_PACK (SCM_UNSPECIFIED_BITS)
  462. #define SCM_UNDEFINED SCM_PACK (SCM_UNDEFINED_BITS)
  463. #define SCM_EOF_VAL SCM_PACK (SCM_EOF_VAL_BITS)
  464. #define SCM_UNBNDP(x) (scm_is_eq ((x), SCM_UNDEFINED))
  465. /* SCM_MATCHES_BITS_IN_COMMON(x,a,b) returns 1 if and only if x matches
  466. both a and b in every bit position where a and b are equal; otherwise
  467. it returns 0. Bit positions where a and b differ are ignored.
  468. This is used to efficiently compare against two values which differ
  469. in exactly one bit position, or against four values which differ in
  470. exactly two bit positions. It is the basis for the following macros:
  471. scm_is_null_or_nil,
  472. scm_is_false_or_nil,
  473. scm_is_true_and_not_nil,
  474. scm_is_lisp_false,
  475. scm_is_lisp_true,
  476. scm_is_bool_and_not_nil (aka scm_is_bool)
  477. scm_is_bool_or_nil. */
  478. #define SCM_MATCHES_BITS_IN_COMMON(x,a,b) \
  479. ((SCM_UNPACK(x) & ~(SCM_UNPACK(a) ^ SCM_UNPACK(b))) == \
  480. (SCM_UNPACK(a) & SCM_UNPACK(b)))
  481. /* These macros are used for compile-time verification that the
  482. constants have the properties needed for the above macro to work
  483. properly. */
  484. #ifdef BUILDING_LIBGUILE
  485. #define SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED(x) ((x) & ((x)-1))
  486. #define SCM_HAS_EXACTLY_ONE_BIT_SET(x) \
  487. ((x) != 0 && SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x) == 0)
  488. #define SCM_HAS_EXACTLY_TWO_BITS_SET(x) \
  489. (SCM_HAS_EXACTLY_ONE_BIT_SET (SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x)))
  490. #define SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION(a,b) \
  491. (SCM_HAS_EXACTLY_ONE_BIT_SET ((a) ^ (b)))
  492. #define SCM_BITS_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS(a,b,c,d) \
  493. (SCM_HAS_EXACTLY_TWO_BITS_SET (((a) ^ (b)) | \
  494. ((b) ^ (c)) | \
  495. ((c) ^ (d))))
  496. #endif /* BUILDING_LIBGUILE */
  497. /* Dispatching aids:
  498. When switching on SCM_TYP7 of a SCM value, use these fake case
  499. labels to catch types that use fewer than 7 bits for tagging. */
  500. /* Pairs with immediate values in the CAR. */
  501. #define scm_tcs_cons_imcar \
  502. scm_tc2_int + 0: case scm_tc2_int + 4: case scm_tc3_imm24 + 0:\
  503. case scm_tc2_int + 8: case scm_tc2_int + 12: case scm_tc3_imm24 + 8:\
  504. case scm_tc2_int + 16: case scm_tc2_int + 20: case scm_tc3_imm24 + 16:\
  505. case scm_tc2_int + 24: case scm_tc2_int + 28: case scm_tc3_imm24 + 24:\
  506. case scm_tc2_int + 32: case scm_tc2_int + 36: case scm_tc3_imm24 + 32:\
  507. case scm_tc2_int + 40: case scm_tc2_int + 44: case scm_tc3_imm24 + 40:\
  508. case scm_tc2_int + 48: case scm_tc2_int + 52: case scm_tc3_imm24 + 48:\
  509. case scm_tc2_int + 56: case scm_tc2_int + 60: case scm_tc3_imm24 + 56:\
  510. case scm_tc2_int + 64: case scm_tc2_int + 68: case scm_tc3_imm24 + 64:\
  511. case scm_tc2_int + 72: case scm_tc2_int + 76: case scm_tc3_imm24 + 72:\
  512. case scm_tc2_int + 80: case scm_tc2_int + 84: case scm_tc3_imm24 + 80:\
  513. case scm_tc2_int + 88: case scm_tc2_int + 92: case scm_tc3_imm24 + 88:\
  514. case scm_tc2_int + 96: case scm_tc2_int + 100: case scm_tc3_imm24 + 96:\
  515. case scm_tc2_int + 104: case scm_tc2_int + 108: case scm_tc3_imm24 + 104:\
  516. case scm_tc2_int + 112: case scm_tc2_int + 116: case scm_tc3_imm24 + 112:\
  517. case scm_tc2_int + 120: case scm_tc2_int + 124: case scm_tc3_imm24 + 120
  518. /* Pairs with heap objects in the CAR. */
  519. #define scm_tcs_cons_nimcar \
  520. scm_tc3_cons + 0:\
  521. case scm_tc3_cons + 8:\
  522. case scm_tc3_cons + 16:\
  523. case scm_tc3_cons + 24:\
  524. case scm_tc3_cons + 32:\
  525. case scm_tc3_cons + 40:\
  526. case scm_tc3_cons + 48:\
  527. case scm_tc3_cons + 56:\
  528. case scm_tc3_cons + 64:\
  529. case scm_tc3_cons + 72:\
  530. case scm_tc3_cons + 80:\
  531. case scm_tc3_cons + 88:\
  532. case scm_tc3_cons + 96:\
  533. case scm_tc3_cons + 104:\
  534. case scm_tc3_cons + 112:\
  535. case scm_tc3_cons + 120
  536. /* Structs. */
  537. #define scm_tcs_struct \
  538. scm_tc3_struct + 0:\
  539. case scm_tc3_struct + 8:\
  540. case scm_tc3_struct + 16:\
  541. case scm_tc3_struct + 24:\
  542. case scm_tc3_struct + 32:\
  543. case scm_tc3_struct + 40:\
  544. case scm_tc3_struct + 48:\
  545. case scm_tc3_struct + 56:\
  546. case scm_tc3_struct + 64:\
  547. case scm_tc3_struct + 72:\
  548. case scm_tc3_struct + 80:\
  549. case scm_tc3_struct + 88:\
  550. case scm_tc3_struct + 96:\
  551. case scm_tc3_struct + 104:\
  552. case scm_tc3_struct + 112:\
  553. case scm_tc3_struct + 120
  554. /* If SCM_ENABLE_DEPRECATED is set to 1, deprecated code will be
  555. included in Guile, as well as some functions to issue run-time
  556. warnings about uses of deprecated functions. */
  557. #ifndef SCM_ENABLE_DEPRECATED
  558. #define SCM_ENABLE_DEPRECATED 0
  559. #endif
  560. /* SCM_API is a macro prepended to all function and data definitions
  561. which should be exported from libguile. */
  562. #if BUILDING_LIBGUILE && HAVE_VISIBILITY
  563. # define SCM_API extern __attribute__((__visibility__("default")))
  564. #elif BUILDING_LIBGUILE && (defined _WIN32 || defined __CYGWIN__)
  565. # define SCM_API __declspec(dllexport) extern
  566. #elif defined _WIN32 || defined __CYGWIN__
  567. # define SCM_API __declspec(dllimport) extern
  568. #else
  569. # define SCM_API extern
  570. #endif
  571. /* The SCM_INTERNAL macro makes it possible to explicitly declare a
  572. function as having "internal" linkage. However our current tack on
  573. this problem is to use GCC 4's -fvisibility=hidden, making functions
  574. internal by default, and then SCM_API marks them for export. */
  575. #define SCM_INTERNAL extern
  576. /* The SCM_DEPRECATED macro is used in declarations of deprecated
  577. functions or variables. Defining `SCM_BUILDING_DEPRECATED_CODE'
  578. allows deprecated functions to be implemented in terms of deprecated
  579. functions, and allows deprecated functions to be referred to by
  580. `scm_c_define_gsubr ()'. */
  581. #if !defined (SCM_BUILDING_DEPRECATED_CODE) && defined __GNUC__
  582. # define SCM_DEPRECATED SCM_API __attribute__ ((__deprecated__))
  583. #else
  584. # define SCM_DEPRECATED SCM_API
  585. #endif
  586. /* The SCM_NORETURN macro indicates that a function will never return.
  587. Examples:
  588. 1) int foo (char arg) SCM_NORETURN; */
  589. #ifdef __GNUC__
  590. # define SCM_NORETURN __attribute__ ((__noreturn__))
  591. #else
  592. # define SCM_NORETURN
  593. #endif
  594. /* The SCM_UNUSED macro indicates that a function, function argument or
  595. variable may potentially be unused.
  596. Examples:
  597. 1) static int unused_function (char arg) SCM_UNUSED;
  598. 2) int foo (char unused_argument SCM_UNUSED);
  599. 3) int unused_variable SCM_UNUSED; */
  600. #ifdef __GNUC__
  601. # define SCM_UNUSED __attribute__ ((unused))
  602. #else
  603. # define SCM_UNUSED
  604. #endif
  605. /* The SCM_MALLOC macro can be used in function declarations to tell the
  606. compiler that a function may be treated as if any non-NULL pointer it
  607. returns cannot alias any other pointer valid when the function
  608. returns. */
  609. #ifdef __GNUC__
  610. # define SCM_MALLOC __attribute__ ((__malloc__))
  611. #else
  612. # define SCM_MALLOC
  613. #endif
  614. /* The SCM_EXPECT macros provide branch prediction hints to the
  615. compiler. To use only in places where the result of the expression
  616. under "normal" circumstances is known. */
  617. #ifdef __GNUC__
  618. # define SCM_EXPECT __builtin_expect
  619. #else
  620. # define SCM_EXPECT(_expr, _value) (_expr)
  621. #endif
  622. #define SCM_LIKELY(_expr) SCM_EXPECT ((_expr), 1)
  623. #define SCM_UNLIKELY(_expr) SCM_EXPECT ((_expr), 0)
  624. /* The SCM_ALIGNED macro, when defined, can be used to instruct the
  625. compiler to honor the given alignment constraint. Sun Studio
  626. supports alignment since Sun Studio 12. */
  627. #if defined __GNUC__ || (defined( __SUNPRO_C ) && (__SUNPRO_C - 0 >= 0x590))
  628. # define SCM_ALIGNED(x) __attribute__ ((aligned (x)))
  629. #elif defined __INTEL_COMPILER
  630. # define SCM_ALIGNED(x) __declspec (align (x))
  631. #else
  632. # undef SCM_ALIGNED
  633. #endif
  634. /* Thread-local storage (TLS). */
  635. #ifdef SCM_HAVE_THREAD_STORAGE_CLASS
  636. # define SCM_THREAD_LOCAL __thread
  637. #else
  638. # define SCM_THREAD_LOCAL
  639. #endif
  640. /* The type of subrs, i.e., Scheme procedures implemented in C. Empty
  641. function declarators are used internally for pointers to functions of
  642. any arity. However, these are equivalent to `(void)' in C++, are
  643. obsolescent as of C99, and trigger `strict-prototypes' GCC warnings
  644. (bug #23681). */
  645. #ifdef BUILDING_LIBGUILE
  646. typedef SCM (* scm_t_subr) ();
  647. #else
  648. typedef void *scm_t_subr;
  649. #endif
  650. typedef struct scm_dynamic_state scm_t_dynamic_state;
  651. typedef struct scm_print_state scm_print_state;
  652. typedef struct scm_dynstack scm_t_dynstack;
  653. typedef int32_t scm_t_wchar;
  654. struct scm_frame;
  655. struct scm_vm;
  656. union scm_vm_stack_element;
  657. typedef struct scm_thread scm_thread;
  658. #ifdef CHAR_BIT
  659. # define SCM_CHAR_BIT CHAR_BIT
  660. #else
  661. # define SCM_CHAR_BIT 8
  662. #endif
  663. #ifdef LONG_BIT
  664. # define SCM_LONG_BIT LONG_BIT
  665. #else
  666. # define SCM_LONG_BIT (SCM_SIZEOF_LONG * 8)
  667. #endif
  668. /* Cast pointer through (void *) in order to avoid compiler warnings
  669. when strict aliasing is enabled */
  670. typedef long SCM_STACKITEM;
  671. #define SCM_STACK_PTR(ptr) ((SCM_STACKITEM *) (void *) (ptr))
  672. #endif /* SCM_SCM_H */