tags.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /* classes: h_files */
  2. #ifndef SCM_TAGS_H
  3. #define SCM_TAGS_H
  4. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011
  5. * 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. /** This file defines the format of SCM values and cons pairs.
  23. ** It is here that tag bits are assigned for various purposes.
  24. **/
  25. /* picks up scmconfig.h too */
  26. #include "libguile/__scm.h"
  27. /* In the beginning was the Word:
  28. *
  29. * For the representation of scheme objects and their handling, Guile provides
  30. * two types: scm_t_bits and SCM.
  31. *
  32. * - scm_t_bits values can hold bit patterns of non-objects and objects:
  33. *
  34. * Non-objects -- in this case the value may not be changed into a SCM value
  35. * in any way.
  36. *
  37. * Objects -- in this case the value may be changed into a SCM value using
  38. * the SCM_PACK macro.
  39. *
  40. * - SCM values can hold proper scheme objects only. They can be changed into
  41. * a scm_t_bits value using the SCM_UNPACK macro.
  42. *
  43. * When working in the domain of scm_t_bits values, programmers must keep
  44. * track of any scm_t_bits value they create that is not a proper scheme
  45. * object. This makes sure that in the domain of SCM values developers can
  46. * rely on the fact that they are dealing with proper scheme objects only.
  47. * Thus, the distinction between scm_t_bits and SCM values helps to identify
  48. * those parts of the code where special care has to be taken not to create
  49. * bad SCM values.
  50. */
  51. /* For dealing with the bit level representation of scheme objects we define
  52. * scm_t_bits:
  53. */
  54. typedef scm_t_int64 scm_t_signed_bits;
  55. typedef scm_t_uint64 scm_t_bits;
  56. #define SCM_T_SIGNED_BITS_MAX SCM_T_INT64_MAX
  57. #define SCM_T_SIGNED_BITS_MIN SCM_T_INT64_MIN
  58. #define SCM_T_BITS_MAX SCM_T_UINT64_MAX
  59. /* But as external interface, we define SCM.
  60. */
  61. union SCM
  62. {
  63. scm_t_bits as_bits;
  64. /* If you set a field of the SCM union, it needs to set all the bits.
  65. The following types are not guaranteed to do so, so we put them in
  66. a sub-union, to indicate that they are for read access only. */
  67. union SCM_read_only {
  68. #if SCM_IS_BIG_ENDIAN
  69. struct { scm_t_uint32 high, low; } as_uint32;
  70. #else
  71. struct { scm_t_uint32 low, high; } as_uint32;
  72. #endif
  73. } read;
  74. };
  75. typedef union SCM SCM;
  76. #define SCM_UNPACK(x) ((x).as_bits)
  77. #define SCM_PACK(x) ((SCM) { (scm_t_bits) (x) })
  78. /* SCM values can not be compared by using the operator ==. Use the following
  79. * macro instead, which is the equivalent of the scheme predicate 'eq?'.
  80. */
  81. #define scm_is_eq(x, y) (SCM_UNPACK (x) == SCM_UNPACK (y))
  82. /* Representation of scheme objects:
  83. *
  84. * Guile's type system is designed to work on systems where scm_t_bits
  85. * and SCM values consist of 64 bits. The objects that a SCM value can
  86. * represent belong to one of the following two major categories:
  87. *
  88. * - Immediates -- meaning that the SCM value contains an entire Scheme
  89. * object. That means, all the object's data (including the type
  90. * tagging information that is required to identify the object's type)
  91. * must fit into 64 bits.
  92. *
  93. * - Non-immediates -- meaning that the SCM value contains a pointer
  94. * into the heap, where additional information is stored.
  95. *
  96. * The 'heap' is the memory area that is under control of Guile's
  97. * garbage collector. The size of the pointed-to memory will be at
  98. * least 8 bytes, and its address will be 8-byte aligned. Thus, on a
  99. * 32-bit system, a pointer only has 29 significant bits. On a 64-bit
  100. * system, Guile restricts the address range of the heap to the lower 48
  101. * bits of memory, so a pointer has 45 significant bits.
  102. *
  103. * Guile uses the remaining 19 bits of a SCM value to encode whether the
  104. * object is immediate or non-immediate, and, if it is immediate, to
  105. * encode the payload. For example, in a Scheme character, some of the
  106. * bits of the SCM indicate that the value is a character, and the other
  107. * bits indicate which character it is.
  108. *
  109. * The precise encoding of an SCM uses a technique known as
  110. * "NaN-boxing". The basic idea is that of the (2^53 - 2) possible bit
  111. * patterns for a double-precision IEEE-754 floating point NaN
  112. * (not-a-number), current hardware and software only produces one
  113. * representation. Guile takes advantage of this situation to encode
  114. * values in the remaining NaN representations.
  115. *
  116. * The primary advantage of this scheme is that floating-point numbers
  117. * can be represented as immediate values.
  118. *
  119. * Recall the IEEE-754 double representation:
  120. *
  121. * <- most significant bit (MSB) least significant bit (LSB) ->
  122. *
  123. * sign: 1 bit
  124. * | exponent: 11 bits
  125. * | | mantissa: 52 bits
  126. * ------------------------------------------------------------------
  127. * 0 00000000000 0000000000000000000000000000000000000000000000000000
  128. * #x0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  129. *
  130. * Positive and negative infinity are encoded like this:
  131. *
  132. * +inf.0
  133. * 0 11111111111 0000000000000000000000000000000000000000000000000000
  134. * #x7 F F 0 0 0 0 0 0 0 0 0 0 0 0 0
  135. *
  136. * -inf.0
  137. * 1 11111111111 0000000000000000000000000000000000000000000000000000
  138. * #xF F F 0 0 0 0 0 0 0 0 0 0 0 0 0
  139. *
  140. * And the canonical NaN value is like this:
  141. *
  142. * +nan.0
  143. * 0 11111111111 100000000000000000000000000000000000000000000000000
  144. * #x7 F F 8 0 0 0 0 0 0 0 0 0 0 0 0
  145. *
  146. * Any other bit pattern with all exponent bits set is a non-canonical
  147. * NaN. For simplicity, Guile uses NaN values with the top 13 bits set,
  148. * then uses the next 3 bits as a tag, and the following 48 bits as a
  149. * payload.
  150. *
  151. * 1 11111111111 1TTTPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
  152. *
  153. * Some tag bits will indicate immediate values, some will indicate
  154. * non-immediate values, and some will indicate invalid values. If any
  155. * of the top 13 bits is unset, then the value is a double.
  156. *
  157. * At this point we need to talk about garbage collection. There are
  158. * two cases to consider: 32-bit pointers and 64-bit pointers. They
  159. * present different challenges.
  160. *
  161. * In the 32-bit case, the low 32 bits of the payload may represent a
  162. * pointer directly, and the entire upper 32 bits may be taken as the
  163. * tag. The GC will see the low half of the SCM as a potential pointer,
  164. * and can trace it. However we need to take care that non-pointer bit
  165. * patterns
  166. *
  167. * Guile does one more trick here, which is to rotate the whole tag
  168. * space, subtracting off 0xfff800000000000 from the bit
  169. * representation. This will leave the top 13 bits set to zero, if the
  170. * SCM value is not a double
  171. At this point you have a choice: whether to prefer doubles or pointers
  172. * representation or the
  173. And then subtract off a constant from the tag, so that for pointers,
  174. * the first bits can be zero:
  175. *
  176. * 000000000000000000PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
  177. *
  178. * Then, if the tag indicates that the value is a
  179. *
  180. * The eight-byte alignment means that for a non-immediate -- a heap
  181. * object -- that the three least significant bits of its address will
  182. * be zero. Guile can then use these bits as type tags. These lowest
  183. * three bits are called tc3-bits, where tc stands for type-code.
  184. *
  185. *
  186. The heap is the part of memory that is managed by
  187. * the garbage collector. Guile restricts the heap to a 48-bit
  188. * address range, so this means that some bit representations of SCM
  189. * values encode a 48-bit pointer to the heap.
  190. *
  191. * Most non-immediates use the first word of the heap memory pointed
  192. * to be a non-immediate for extra type information. The notable
  193. * exception to this scheme are pairs, which has space for at least one scm_t_bits value,
  194. * which Guile uses to SCM value pointing to the heap
  195. *
  196. On systems where a pointer needs more than
  197. * 32 bits this means that scm_t_bits and SCM variables need to be
  198. * large enough to hold such pointers. In contrast to immediates, the
  199. * object's data of a non-immediate can consume arbitrary amounts of
  200. * memory: The heap cell being pointed to consists of at least two
  201. * scm_t_bits variables and thus can be used to hold pointers to
  202. * malloc'ed memory of any size.
  203. *
  204. * For a given SCM value, the distinction whether it holds an immediate
  205. * or non-immediate object is based on the tc3-bits of its scm_t_bits
  206. * equivalent: If the tc3-bits equal #b000, then the SCM value holds a
  207. * non-immediate, and the scm_t_bits variable's value is just the
  208. * pointer to the heap cell.
  209. *
  210. * Summarized, the data of a scheme object that is represented by a SCM
  211. * variable consists of a) the SCM variable itself, b) in case of
  212. * non-immediates the heap data that the SCM object points to, c) in
  213. * case of non-immediates potentially additional data outside of the
  214. * heap (like for example malloc'ed data), and d) in case of
  215. * non-immediates potentially additional data inside of the heap, since
  216. * data stored in b) and c) may hold references to other cells.
  217. */
  218. /* Let's take a breather and define some helpers to access Scheme values
  219. from the heap.
  220. Though C99 doesn't specify whether pointers are represented the same
  221. way in memory as integers are, it seems to be universal, besides
  222. UNICOS. We make that assumption, having checked at configure-time
  223. that it does indeed hold true on this platform. */
  224. #if SCM_SIZEOF_VOID_P == 4
  225. #define SCM_TO_POINTER(x) ((void *)((x).read.as_uint32.low))
  226. #elif SCM_SIZEOF_VOID_P == 8
  227. #define SCM_TO_POINTER(x) ((void *)((x).as_bits))
  228. #else
  229. #error Unhandled word size.
  230. #endif
  231. #define SCM_FROM_POINTER(x) SCM_PACK ((scm_t_bits)x)
  232. #define SCM_HEAP_POINTER(x) ((SCM *) SCM_TO_POINTER (x))
  233. #define SCM_HEAP_OBJECT(x, n) \
  234. (SCM_HEAP_POINTER (x) [(n)])
  235. #define SCM_SET_HEAP_OBJECT(x, n, o) \
  236. SCM_HEAP_OBJECT (x, n) = (o)
  237. #define SCM_HEAP_DATA(x, n) \
  238. (SCM_HEAP_OBJECT (x, n).as_bits)
  239. #define SCM_SET_HEAP_DATA(x, n, b) \
  240. SCM_HEAP_DATA (x, n) = (b)
  241. /*
  242. *
  243. * Immediates
  244. *
  245. * Operations on immediate objects can typically be processed faster than on
  246. * non-immediates. The reason is that the object's data can be extracted
  247. * directly from the SCM variable (or rather a corresponding scm_t_bits
  248. * variable), instead of having to perform additional memory accesses to
  249. * obtain the object's data from the heap. In order to get the best possible
  250. * performance frequently used data types should be realized as immediates.
  251. * This is, as has been mentioned above, only possible if the objects can be
  252. * represented with 32 bits (including type tagging).
  253. *
  254. * In Guile, the following data types and special objects are realized as
  255. * immediates: booleans, characters, small integers (see below), the empty
  256. * list, the end of file object, the 'unspecified' object (which is delivered
  257. * as a return value by functions for which the return value is unspecified),
  258. * a 'nil' object used in the elisp-compatibility mode and certain other
  259. * 'special' objects which are only used internally in Guile.
  260. *
  261. * Integers in Guile can be arbitrarily large. On the other hand, integers
  262. * are one of the most frequently used data types. Especially integers with
  263. * less than 32 bits are commonly used. Thus, internally and transparently
  264. * for application code guile distinguishes between small and large integers.
  265. * Whether an integer is a large or a small integer depends on the number of
  266. * bits needed to represent its value. Small integers are those which can be
  267. * represented as immediates. Since they don't require more than a fixed
  268. * number of bits for their representation, they are also known as 'fixnums'.
  269. *
  270. * The tc3-combinations #b010 and #b110 are used to represent small integers,
  271. * which allows to use the most significant bit of the tc3-bits to be part of
  272. * the integer value being represented. This means that all integers with up
  273. * to 30 bits (including one bit for the sign) can be represented as
  274. * immediates. On systems where SCM and scm_t_bits variables hold more than
  275. * 32 bits, the amount of bits usable for small integers will even be larger.
  276. * The tc3-code #b100 is shared among booleans, characters and the other
  277. * special objects listed above.
  278. *
  279. *
  280. * Non-Immediates
  281. *
  282. * All object types not mentioned above in the list of immedate objects are
  283. * represented as non-immediates. Whether a non-immediate scheme object is
  284. * represented by a single-cell or a double-cell depends on the object's type,
  285. * namely on the set of attributes that have to be stored with objects of that
  286. * type. Every non-immediate type is allowed to define its own layout and
  287. * interpretation of the data stored in its cell (with some restrictions, see
  288. * below).
  289. *
  290. * One of the design goals of guile's type system is to make it possible to
  291. * store a scheme pair with as little memory usage as possible. The minimum
  292. * amount of memory that is required to store two scheme objects (car and cdr
  293. * of a pair) is the amount of memory required by two scm_t_bits or SCM
  294. * variables. Therefore pairs in guile are stored in single-cells.
  295. *
  296. * Another design goal for the type system is to store procedure objects
  297. * created by lambda expresssions (closures) and class instances (goops
  298. * objects) with as little memory usage as possible. Closures are represented
  299. * by a reference to the function code and a reference to the closure's
  300. * environment. Class instances are represented by a reference to the
  301. * instance's class definition and a reference to the instance's data. Thus,
  302. * closures as well as class instances also can be stored in single-cells.
  303. *
  304. * Certain other non-immediate types also store their data in single-cells.
  305. * By design decision, the heap is split into areas for single-cells and
  306. * double-cells, but not into areas for single-cells-holding-pairs and areas
  307. * for single-cells-holding-non-pairs. Any single-cell on the heap therefore
  308. * can hold pairs (consisting of two scm_t_bits variables representing two
  309. * scheme objects - the car and cdr of the pair) and non-pairs (consisting of
  310. * two scm_t_bits variables that hold bit patterns as defined by the layout of
  311. * the corresponding object's type).
  312. *
  313. *
  314. * Garbage collection
  315. *
  316. * During garbage collection, unreachable cells on the heap will be freed.
  317. * That is, the garbage collector will detect cells which have no SCM variable
  318. * pointing towards them. In order to properly release all memory belonging
  319. * to the object to which a cell belongs, the gc needs to be able to interpret
  320. * the cell contents in the correct way. That means that the gc needs to be
  321. * able to determine the object type associated with a cell only from the cell
  322. * itself.
  323. *
  324. * Consequently, if the gc detects an unreachable single-cell, those two
  325. * scm_t_bits variables must provide enough information to determine whether
  326. * they belong to a pair (i. e. both scm_t_bits variables represent valid
  327. * scheme objects), to a closure, a class instance or if they belong to any
  328. * other non-immediate. Guile's type system is designed to make it possible
  329. * to determine a the type to which a cell belongs in the majority of cases
  330. * from the cell's first scm_t_bits variable. (Given a SCM variable X holding
  331. * a non-immediate object, the macro SCM_CELL_TYPE(X) will deliver the
  332. * corresponding cell's first scm_t_bits variable.)
  333. *
  334. * If the cell holds a scheme pair, then we already know that the first
  335. * scm_t_bits variable of the cell will hold a scheme object with one of the
  336. * following tc3-codes: #b000 (non-immediate), #b010 (small integer), #b110
  337. * (small integer), #b100 (non-integer immediate). All these tc3-codes have
  338. * in common, that their least significant bit is #b0. This fact is used by
  339. * the garbage collector to identify cells that hold pairs. The remaining
  340. * tc3-codes are assigned as follows: #b001 (class instance or, more
  341. * precisely, a struct, of which a class instance is a special case), #b011
  342. * (closure), #b101/#b111 (all remaining non-immediate types).
  343. *
  344. *
  345. * Summary of type codes of scheme objects (SCM variables)
  346. *
  347. * Here is a summary of tagging bits as they might occur in a scheme object.
  348. * The notation is as follows: tc stands for type code as before, tc<n> with n
  349. * being a number indicates a type code formed by the n least significant bits
  350. * of the SCM variables corresponding scm_t_bits value.
  351. *
  352. * Note that (as has been explained above) tc1==1 can only occur in the first
  353. * scm_t_bits variable of a cell belonging to a non-immediate object that is
  354. * not a pair. For an explanation of the tc tags with tc1==1, see the next
  355. * section with the summary of the type codes on the heap.
  356. *
  357. * tc1:
  358. * 0: For scheme objects, tc1==0 must be fulfilled.
  359. * (1: This can never be the case for a scheme object.)
  360. *
  361. * tc2:
  362. * 00: Either a non-immediate or some non-integer immediate
  363. * (01: This can never be the case for a scheme object.)
  364. * 10: Small integer
  365. * (11: This can never be the case for a scheme object.)
  366. *
  367. * tc3:
  368. * 000: a non-immediate object (pair, closure, class instance etc.)
  369. * (001: This can never be the case for a scheme object.)
  370. * 010: an even small integer (least significant bit is 0).
  371. * (011: This can never be the case for a scheme object.)
  372. * 100: Non-integer immediate
  373. * (101: This can never be the case for a scheme object.)
  374. * 110: an odd small integer (least significant bit is 1).
  375. * (111: This can never be the case for a scheme object.)
  376. *
  377. * The remaining bits of the non-immediate objects form the pointer to the
  378. * heap cell. The remaining bits of the small integers form the integer's
  379. * value and sign. Thus, the only scheme objects for which a further
  380. * subdivision is of interest are the ones with tc3==100.
  381. *
  382. * tc8 (for objects with tc3==100):
  383. * 00000-100: special objects ('flags')
  384. * 00001-100: characters
  385. * 00010-100: unused
  386. * 00011-100: unused
  387. *
  388. *
  389. * Summary of type codes on the heap
  390. *
  391. * Here is a summary of tagging in scm_t_bits values as they might occur in
  392. * the first scm_t_bits variable of a heap cell.
  393. *
  394. * tc1:
  395. * 0: the cell belongs to a pair.
  396. * 1: the cell belongs to a non-pair.
  397. *
  398. * tc2:
  399. * 00: the cell belongs to a pair with no short integer in its car.
  400. * 01: the cell belongs to a non-pair (struct or some other non-immediate).
  401. * 10: the cell belongs to a pair with a short integer in its car.
  402. * 11: the cell belongs to a non-pair (closure or some other non-immediate).
  403. *
  404. * tc3:
  405. * 000: the cell belongs to a pair with a non-immediate in its car.
  406. * 001: the cell belongs to a struct
  407. * 010: the cell belongs to a pair with an even short integer in its car.
  408. * 011: the cell belongs to a closure
  409. * 100: the cell belongs to a pair with a non-integer immediate in its car.
  410. * 101: the cell belongs to some other non-immediate.
  411. * 110: the cell belongs to a pair with an odd short integer in its car.
  412. * 111: the cell belongs to some other non-immediate.
  413. *
  414. * tc7 (for tc3==1x1):
  415. * See below for the list of types. Note the special case of scm_tc7_vector
  416. * and scm_tc7_wvect: vectors and weak vectors are treated the same in many
  417. * cases. Thus, their tc7-codes are chosen to only differ in one bit. This
  418. * makes it possible to check an object at the same time for being a vector
  419. * or a weak vector by comparing its tc7 code with that bit masked (using
  420. * the TYP7S macro). Three more special tc7-codes are of interest:
  421. * numbers, ports and smobs in fact each represent collections of types,
  422. * which are subdivided using tc16-codes.
  423. *
  424. * tc16 (for tc7==scm_tc7_smob):
  425. * The largest part of the space of smob types is not subdivided in a
  426. * predefined way, since smobs can be added arbitrarily by user C code.
  427. */
  428. /* Checking if a SCM variable holds an immediate or a non-immediate object:
  429. * This check can either be performed by checking for tc3==000 or tc3==00x,
  430. * since for a SCM variable it is known that tc1==0. */
  431. #define SCM_IMP(x) (6 & SCM_UNPACK (x))
  432. #define SCM_NIMP(x) (!SCM_IMP (x))
  433. /* Checking if a SCM variable holds an immediate integer: See numbers.h for
  434. * the definition of the following macros: SCM_I_FIXNUM_BIT,
  435. * SCM_MOST_POSITIVE_FIXNUM, SCM_I_INUMP, SCM_I_MAKINUM, SCM_I_INUM. */
  436. /* Checking if a SCM variable holds a pair (for historical reasons, in Guile
  437. * also known as a cons-cell): This is done by first checking that the SCM
  438. * variable holds a non-immediate, and second, by checking that tc1==0 holds
  439. * for the SCM_CELL_TYPE of the SCM variable.
  440. */
  441. #define SCM_I_CONSP(x) (!SCM_IMP (x) && ((1 & SCM_CELL_TYPE (x)) == 0))
  442. /* Definitions for tc2: */
  443. #define scm_tc2_int 2
  444. /* Definitions for tc3: */
  445. #define SCM_ITAG3(x) (7 & SCM_UNPACK (x))
  446. #define SCM_TYP3(x) (7 & SCM_CELL_TYPE (x))
  447. #define scm_tc3_cons 0
  448. #define scm_tc3_struct 1
  449. #define scm_tc3_int_1 (scm_tc2_int + 0)
  450. #define scm_tc3_unused 3
  451. #define scm_tc3_imm24 4
  452. #define scm_tc3_tc7_1 5
  453. #define scm_tc3_int_2 (scm_tc2_int + 4)
  454. #define scm_tc3_tc7_2 7
  455. /* Definitions for tc7: */
  456. #define SCM_ITAG7(x) (127 & SCM_UNPACK (x))
  457. #define SCM_TYP7(x) (0x7f & SCM_CELL_TYPE (x))
  458. #define SCM_TYP7S(x) ((0x7f & ~2) & SCM_CELL_TYPE (x))
  459. #define scm_tc7_symbol 5
  460. #define scm_tc7_variable 7
  461. /* couple */
  462. #define scm_tc7_vector 13
  463. #define scm_tc7_wvect 15
  464. #define scm_tc7_string 21
  465. #define scm_tc7_number 23
  466. #define scm_tc7_stringbuf 39
  467. #define scm_tc7_bytevector 77
  468. #define scm_tc7_pointer 31
  469. #define scm_tc7_hashtable 29
  470. #define scm_tc7_fluid 37
  471. #define scm_tc7_dynamic_state 45
  472. #define scm_tc7_frame 47
  473. #define scm_tc7_objcode 53
  474. #define scm_tc7_vm 55
  475. #define scm_tc7_vm_cont 71
  476. #define scm_tc7_prompt 61
  477. #define scm_tc7_with_fluids 63
  478. #define scm_tc7_unused_19 69
  479. #define scm_tc7_program 79
  480. #define scm_tc7_unused_9 85
  481. #define scm_tc7_unused_10 87
  482. #define scm_tc7_unused_20 93
  483. #define scm_tc7_unused_11 95
  484. #define scm_tc7_unused_12 101
  485. #define scm_tc7_unused_18 103
  486. #define scm_tc7_unused_13 109
  487. #define scm_tc7_unused_14 111
  488. #define scm_tc7_unused_15 117
  489. #define scm_tc7_unused_16 119
  490. /* There are 256 port subtypes. */
  491. #define scm_tc7_port 125
  492. /* There are 256 smob subtypes. [**] If you change scm_tc7_smob, you must
  493. * also change the places it is hard coded in this file and possibly others.
  494. * Dirk:FIXME:: Any hard coded reference to scm_tc7_smob must be replaced by a
  495. * symbolic reference. */
  496. #define scm_tc7_smob 127 /* DO NOT CHANGE [**] */
  497. /* Definitions for tc16: */
  498. #define SCM_TYP16(x) (0xffff & SCM_CELL_TYPE (x))
  499. #define SCM_TYP16_PREDICATE(tag, x) (!SCM_IMP (x) && SCM_TYP16 (x) == (tag))
  500. /* {Immediate Values}
  501. */
  502. enum scm_tc8_tags
  503. {
  504. scm_tc8_flag = scm_tc3_imm24 + 0x00, /* special objects ('flags') */
  505. scm_tc8_char = scm_tc3_imm24 + 0x08, /* characters */
  506. scm_tc8_unused_0 = scm_tc3_imm24 + 0x10,
  507. scm_tc8_unused_1 = scm_tc3_imm24 + 0x18
  508. };
  509. #define SCM_ITAG8(X) (SCM_UNPACK (X) & 0xff)
  510. #define SCM_MAKE_ITAG8_BITS(X, TAG) (((X) << 8) + TAG)
  511. #define SCM_MAKE_ITAG8(X, TAG) (SCM_PACK (SCM_MAKE_ITAG8_BITS (X, TAG)))
  512. #define SCM_ITAG8_DATA(X) (SCM_UNPACK (X) >> 8)
  513. /* Flags (special objects). The indices of the flags must agree with the
  514. * declarations in print.c: iflagnames. */
  515. #define SCM_IFLAGP(n) (SCM_ITAG8 (n) == scm_tc8_flag)
  516. #define SCM_MAKIFLAG_BITS(n) (SCM_MAKE_ITAG8_BITS ((n), scm_tc8_flag))
  517. #define SCM_IFLAGNUM(n) (SCM_ITAG8_DATA (n))
  518. /*
  519. * IMPORTANT NOTE regarding IFLAG numbering!!!
  520. *
  521. * Several macros depend upon careful IFLAG numbering of SCM_BOOL_F,
  522. * SCM_BOOL_T, SCM_ELISP_NIL, SCM_EOL, and the two SCM_XXX_*_DONT_USE
  523. * constants. In particular:
  524. *
  525. * - SCM_BOOL_F and SCM_BOOL_T must differ in exactly one bit position.
  526. * (used to implement scm_is_bool_and_not_nil, aka scm_is_bool)
  527. *
  528. * - SCM_ELISP_NIL and SCM_BOOL_F must differ in exactly one bit position.
  529. * (used to implement scm_is_false_or_nil and
  530. * scm_is_true_and_not_nil)
  531. *
  532. * - SCM_ELISP_NIL and SCM_EOL must differ in exactly one bit position.
  533. * (used to implement scm_is_null_or_nil)
  534. *
  535. * - SCM_ELISP_NIL, SCM_BOOL_F, SCM_EOL, SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE
  536. * must all be equal except for two bit positions.
  537. * (used to implement scm_is_lisp_false)
  538. *
  539. * - SCM_ELISP_NIL, SCM_BOOL_F, SCM_BOOL_T, SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0
  540. * must all be equal except for two bit positions.
  541. * (used to implement scm_is_bool_or_nil)
  542. *
  543. * These properties allow the aforementioned macros to be implemented
  544. * by bitwise ANDing with a mask and then comparing with a constant,
  545. * using as a common basis the macro SCM_MATCHES_BITS_IN_COMMON,
  546. * defined below. The properties are checked at compile-time using
  547. * `verify' macros near the top of boolean.c and pairs.c.
  548. */
  549. #define SCM_BOOL_F_BITS SCM_MAKIFLAG_BITS (0)
  550. #define SCM_ELISP_NIL_BITS SCM_MAKIFLAG_BITS (1)
  551. #define SCM_BOOL_F SCM_PACK (SCM_BOOL_F_BITS)
  552. #define SCM_ELISP_NIL SCM_PACK (SCM_ELISP_NIL_BITS)
  553. #ifdef BUILDING_LIBGUILE
  554. #define SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE SCM_MAKIFLAG_BITS (2)
  555. #endif
  556. #define SCM_EOL_BITS SCM_MAKIFLAG_BITS (3)
  557. #define SCM_BOOL_T_BITS SCM_MAKIFLAG_BITS (4)
  558. #define SCM_EOL SCM_PACK (SCM_EOL_BITS)
  559. #define SCM_BOOL_T SCM_PACK (SCM_BOOL_T_BITS)
  560. #ifdef BUILDING_LIBGUILE
  561. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0 SCM_MAKIFLAG_BITS (5)
  562. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_1 SCM_MAKIFLAG_BITS (6)
  563. #define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_2 SCM_MAKIFLAG_BITS (7)
  564. #endif
  565. #define SCM_UNSPECIFIED_BITS SCM_MAKIFLAG_BITS (8)
  566. #define SCM_UNDEFINED_BITS SCM_MAKIFLAG_BITS (9)
  567. #define SCM_EOF_VAL_BITS SCM_MAKIFLAG_BITS (10)
  568. #define SCM_UNSPECIFIED SCM_PACK (SCM_UNSPECIFIED_BITS)
  569. #define SCM_UNDEFINED SCM_PACK (SCM_UNDEFINED_BITS)
  570. #define SCM_EOF_VAL SCM_PACK (SCM_EOF_VAL_BITS)
  571. /* When a variable is unbound this is marked by the SCM_UNDEFINED
  572. * value. The following is an unbound value which can be handled on
  573. * the Scheme level, i.e., it can be stored in and retrieved from a
  574. * Scheme variable. This value is only intended to mark an unbound
  575. * slot in GOOPS. It is needed now, but we should probably rewrite
  576. * the code which handles this value in C so that SCM_UNDEFINED can be
  577. * used instead. It is not ideal to let this kind of unique and
  578. * strange values loose on the Scheme level. */
  579. #define SCM_UNBOUND_BITS SCM_MAKIFLAG_BITS (11)
  580. #define SCM_UNBOUND SCM_PACK (SCM_UNBOUND_BITS)
  581. #define SCM_UNBNDP(x) (scm_is_eq ((x), SCM_UNDEFINED))
  582. /*
  583. * SCM_MATCHES_BITS_IN_COMMON(x,a,b) returns 1 if and only if x
  584. * matches both a and b in every bit position where a and b are equal;
  585. * otherwise it returns 0. Bit positions where a and b differ are
  586. * ignored.
  587. *
  588. * This is used to efficiently compare against two values which differ
  589. * in exactly one bit position, or against four values which differ in
  590. * exactly two bit positions. It is the basis for the following
  591. * macros:
  592. *
  593. * scm_is_null_or_nil,
  594. * scm_is_false_or_nil,
  595. * scm_is_true_and_not_nil,
  596. * scm_is_lisp_false,
  597. * scm_is_lisp_true,
  598. * scm_is_bool_and_not_nil (aka scm_is_bool)
  599. * scm_is_bool_or_nil.
  600. */
  601. #define SCM_MATCHES_BITS_IN_COMMON(x,a,b) \
  602. ((SCM_UNPACK(x) & ~(SCM_UNPACK(a) ^ SCM_UNPACK(b))) == \
  603. (SCM_UNPACK(a) & SCM_UNPACK(b)))
  604. /*
  605. * These macros are used for compile-time verification that the
  606. * constants have the properties needed for the above macro to work
  607. * properly.
  608. */
  609. #ifdef BUILDING_LIBGUILE
  610. #define SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED(x) ((x) & ((x)-1))
  611. #define SCM_HAS_EXACTLY_ONE_BIT_SET(x) \
  612. ((x) != 0 && SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x) == 0)
  613. #define SCM_HAS_EXACTLY_TWO_BITS_SET(x) \
  614. (SCM_HAS_EXACTLY_ONE_BIT_SET (SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x)))
  615. #define SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION(a,b) \
  616. (SCM_HAS_EXACTLY_ONE_BIT_SET ((a) ^ (b)))
  617. #define SCM_BITS_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS(a,b,c,d) \
  618. (SCM_HAS_EXACTLY_TWO_BITS_SET (((a) ^ (b)) | \
  619. ((b) ^ (c)) | \
  620. ((c) ^ (d))))
  621. #endif /* BUILDING_LIBGUILE */
  622. /* Dispatching aids:
  623. When switching on SCM_TYP7 of a SCM value, use these fake case
  624. labels to catch types that use fewer than 7 bits for tagging. */
  625. /* For cons pairs with immediate values in the CAR
  626. */
  627. #define scm_tcs_cons_imcar \
  628. scm_tc2_int + 0: case scm_tc2_int + 4: case scm_tc3_imm24 + 0:\
  629. case scm_tc2_int + 8: case scm_tc2_int + 12: case scm_tc3_imm24 + 8:\
  630. case scm_tc2_int + 16: case scm_tc2_int + 20: case scm_tc3_imm24 + 16:\
  631. case scm_tc2_int + 24: case scm_tc2_int + 28: case scm_tc3_imm24 + 24:\
  632. case scm_tc2_int + 32: case scm_tc2_int + 36: case scm_tc3_imm24 + 32:\
  633. case scm_tc2_int + 40: case scm_tc2_int + 44: case scm_tc3_imm24 + 40:\
  634. case scm_tc2_int + 48: case scm_tc2_int + 52: case scm_tc3_imm24 + 48:\
  635. case scm_tc2_int + 56: case scm_tc2_int + 60: case scm_tc3_imm24 + 56:\
  636. case scm_tc2_int + 64: case scm_tc2_int + 68: case scm_tc3_imm24 + 64:\
  637. case scm_tc2_int + 72: case scm_tc2_int + 76: case scm_tc3_imm24 + 72:\
  638. case scm_tc2_int + 80: case scm_tc2_int + 84: case scm_tc3_imm24 + 80:\
  639. case scm_tc2_int + 88: case scm_tc2_int + 92: case scm_tc3_imm24 + 88:\
  640. case scm_tc2_int + 96: case scm_tc2_int + 100: case scm_tc3_imm24 + 96:\
  641. case scm_tc2_int + 104: case scm_tc2_int + 108: case scm_tc3_imm24 + 104:\
  642. case scm_tc2_int + 112: case scm_tc2_int + 116: case scm_tc3_imm24 + 112:\
  643. case scm_tc2_int + 120: case scm_tc2_int + 124: case scm_tc3_imm24 + 120
  644. /* For cons pairs with non-immediate values in the SCM_CAR
  645. */
  646. #define scm_tcs_cons_nimcar \
  647. scm_tc3_cons + 0:\
  648. case scm_tc3_cons + 8:\
  649. case scm_tc3_cons + 16:\
  650. case scm_tc3_cons + 24:\
  651. case scm_tc3_cons + 32:\
  652. case scm_tc3_cons + 40:\
  653. case scm_tc3_cons + 48:\
  654. case scm_tc3_cons + 56:\
  655. case scm_tc3_cons + 64:\
  656. case scm_tc3_cons + 72:\
  657. case scm_tc3_cons + 80:\
  658. case scm_tc3_cons + 88:\
  659. case scm_tc3_cons + 96:\
  660. case scm_tc3_cons + 104:\
  661. case scm_tc3_cons + 112:\
  662. case scm_tc3_cons + 120
  663. /* For structs
  664. */
  665. #define scm_tcs_struct \
  666. scm_tc3_struct + 0:\
  667. case scm_tc3_struct + 8:\
  668. case scm_tc3_struct + 16:\
  669. case scm_tc3_struct + 24:\
  670. case scm_tc3_struct + 32:\
  671. case scm_tc3_struct + 40:\
  672. case scm_tc3_struct + 48:\
  673. case scm_tc3_struct + 56:\
  674. case scm_tc3_struct + 64:\
  675. case scm_tc3_struct + 72:\
  676. case scm_tc3_struct + 80:\
  677. case scm_tc3_struct + 88:\
  678. case scm_tc3_struct + 96:\
  679. case scm_tc3_struct + 104:\
  680. case scm_tc3_struct + 112:\
  681. case scm_tc3_struct + 120
  682. #endif /* SCM_TAGS_H */
  683. /*
  684. Local Variables:
  685. c-file-style: "gnu"
  686. End:
  687. */