Debian Codegraph @

include/cst_val_const.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2001 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: January 2001 */
35 /*************************************************************************/
36 /* */
37 /* Const Vals, and macros to define them */
38 /* */
39 /* Before you give up in disgust bear with me on this. Every single */
40 /* line in this file has been *very* carefully decided on after much */
41 /* thought, experimentation and reading more specs of the C language */
42 /* than most people even thought existed. However inspite of that, the */
43 /* result is still unsatisfying from an elegance point of view but the */
44 /* given all the constraints this is probably the best compromise. */
45 /* */
46 /* This file offers macros for defining const cst_vals. I know many */
47 /* are already laughing at me for wanting runtime types on objects and */
48 /* will use this code as exemplars of why this should be done in C++, I */
49 /* say good luck to them with their 4M footprint while I go for my */
50 /* 50K footprint. But I *will* do cst_vals in 8 bytes and I *will* */
51 /* have them defined const so they are in the text segment */
52 /* */
53 /* The problem here is that there is not yet a standard way to do */
54 /* initialization of unions. There is one in the C99 standard and GCC */
55 /* already supports it, but other compilers do not so I can't use that */
56 /* */
57 /* So I need a way to make an object that will have the right 8 bytes */
58 /* for ints, floats, strings and cons cells that will work on any C */
59 /* compiler and will be of type const cst_val. That unfortunately */
60 /* isn't trivial. */
61 /* */
62 /* For the time being ignoring byte order, and address size, which */
63 /* will be ignored in the particular discuss (though dealt with below) */
64 /* I'd like to do something like */
65 /* */
66 /* const cst_val fredi = { CST_VAL_TYPE_INT,-1, 42 }; */
67 /* const cst_val fredf = { CST_VAL_TYPE_FLOAT,-1, 4.2 }; */
68 /* const cst_val freds = { CST_VAL_TYPE_STRING,-1, "42" }; */
69 /* */
70 /* Even if you accept warnings this isn't going to work, if you add */
71 /* extra {} you can get rid of some warnings but the fval/ival/ *vval */
72 /* part isn't going to work, the compiler *may* try to take the value */
73 /* and assign it using the type of the first field defined in the */
74 /* union. This could be made to work for ints and void* as pointers */
75 /* can be used as ints (even of 64 bit architectures) but the float */
76 /* isn't going to work. Casting will cause the float to be changed to */
77 /* an int by CPP which isn't what you want, what you want is that the */
78 /* four byte region gets filled with the four bytes that represent the */
79 /* float itself. Now you could get the four byte represention of the */
80 /* float and pretend that is an int (0xbfff9a4 for 4.2 on intel), that */
81 /* would work but that doesn't seem satifying and I'd need to have a */
82 /* preprocessor that could convert that. You could make atoms always */
83 /* have a pointer to another piece of memory, but that would take up */
84 /* another 4 bytes not just for these constants but all other cst_vals */
85 /* create */
86 /* */
87 /* So you could do */
88 /* */
89 /* const cst_val_int fredi = { CST_VAL_TYPE_INT,-1, 42 }; */
90 /* const cst_val_float fredf = { CST_VAL_TYPE_FLOAT,-1, 4.2 }; */
91 /* const cst_val_string freds = { CST_VAL_TYPE_STRING,-1, "42" }; */
92 /* */
93 /* Though that's a slippery slope I don't want to have these explicit */
94 /* new types, the first short defines the type perfectly adequately */
95 /* and the whole point of runtime types is that there is one object */
96 /* type. */
97 /* */
98 /* Well just initialize them at runtime, but, that isn't thread safe, */
99 /* slows down startup, requires a instance implicitly in the code and */
100 /* on the heap. As these are const, they should go in ROM. */
101 /* */
102 /* At this moment, I think the second version is the least problematic */
103 /* though it makes defining val_consts more unpleasant than they should */
104 /* be and forces changes elsewhere in the code even when the compiler */
105 /* does support initialization of unions */
106 /* */
107 /* */
108 /*************************************************************************/
109 #ifndef _CST_VAL_CONSTS_H__
110 #define _CST_VAL_CONSTS_H__
111
112 #include "cst_val_defs.h"
113
114 /* There is built-in int to string conversions here for numbers */
115 /* up to 20, note if you make this bigger you have to hand change */
116 /* other things too */
117 #define CST_CONST_INT_MAX 19
118
119 #ifndef NO_UNION_INITIALIZATION
120
121 /* This is the simple way when initialization of unions is supported */
122
123 #define DEF_CONST_VAL_INT(N,V) const cst_val N = {{.a={.type=CST_VAL_TYPE_INT,.ref_count=-1,.v={.ival=V}}}}
124 #define DEF_CONST_VAL_STRING(N,S) const cst_val N = {{.a={.type=CST_VAL_TYPE_STRING,.ref_count=-1,.v={.vval= (void *)S}}}}
125 #define DEF_CONST_VAL_FLOAT(N,F) const cst_val N = {{.a={.type=CST_VAL_TYPE_FLOAT,.ref_count=-1,.v={.fval=F}}}}
126 #define DEF_CONST_VAL_CONS(N,A,D) const cst_val N = {{.cc={.car=(cst_val *)A,.cdr=(cst_val *)D }}}
127
128 extern const cst_val val_int_0;
129 extern const cst_val val_int_1;
130 extern const cst_val val_int_2;
131 extern const cst_val val_int_3;
132 extern const cst_val val_int_4;
133 extern const cst_val val_int_5;
134 extern const cst_val val_int_6;
135 extern const cst_val val_int_7;
136 extern const cst_val val_int_8;
137 extern const cst_val val_int_9;
138 extern const cst_val val_int_10;
139 extern const cst_val val_int_11;
140 extern const cst_val val_int_12;
141 extern const cst_val val_int_13;
142 extern const cst_val val_int_14;
143 extern const cst_val val_int_15;
144 extern const cst_val val_int_16;
145 extern const cst_val val_int_17;
146 extern const cst_val val_int_18;
147 extern const cst_val val_int_19;
148 extern const cst_val val_int_20;
149 extern const cst_val val_int_21;
150 extern const cst_val val_int_22;
151 extern const cst_val val_int_23;
152 extern const cst_val val_int_24;
153
154 extern const cst_val val_string_0;
155 extern const cst_val val_string_1;
156 extern const cst_val val_string_2;
157 extern const cst_val val_string_3;
158 extern const cst_val val_string_4;
159 extern const cst_val val_string_5;
160 extern const cst_val val_string_6;
161 extern const cst_val val_string_7;
162 extern const cst_val val_string_8;
163 extern const cst_val val_string_9;
164 extern const cst_val val_string_10;
165 extern const cst_val val_string_11;
166 extern const cst_val val_string_12;
167 extern const cst_val val_string_13;
168 extern const cst_val val_string_14;
169 extern const cst_val val_string_15;
170 extern const cst_val val_string_16;
171 extern const cst_val val_string_17;
172 extern const cst_val val_string_18;
173 extern const cst_val val_string_19;
174 extern const cst_val val_string_20;
175 extern const cst_val val_string_21;
176 extern const cst_val val_string_22;
177 extern const cst_val val_string_23;
178 extern const cst_val val_string_24;
179
180 #else
181 /* Only GCC seems to currently support the C99 standard for giving */
182 /* explicit names for fields for initializing unions, because we want */
183 /* things to be const, and to be small structures this is really useful */
184 /* thus for compilers not supporting no_union_initization we use other */
185 /* structure that we know (hope ?) are the same size and use agressive */
186 /* casting. The goal here is wholly justified but the method here isn't */
187 /* pretty */
188
189 /* These structures are defined *solely* to get round initialization */
190 /* problems if you need to use these in any code you are using, you are */
191 /* unquestionably doing the wrong thing */
192 typedef struct cst_val_atom_struct_float {
193 #ifdef WORDS_BIGENDIAN
194 short ref_count;
195 short type; /* order is here important */
196 #else
197 #if (defined(__x86_64__) || defined(_M_X64))
198 int type; /* order is here important */
199 int ref_count;
200 #else
201 short type; /* order is here important */
202 short ref_count;
203 #endif
204 #endif
205 #if (defined(__x86_64__) || defined(_M_X64))
206 double fval;
207 #else
208 float fval;
209 #endif
210 } cst_val_float;
211
212 typedef struct cst_val_atom_struct_int {
213 #ifdef WORDS_BIGENDIAN
214 short ref_count;
215 short type; /* order is here important (and unintuitive) */
216 #else
217 #if (defined(__x86_64__) || defined(_M_X64))
218 int type; /* order is here important */
219 int ref_count;
220 #else
221 short type; /* order is here important */
222 short ref_count;
223 #endif
224 #endif
225 #if (defined(__x86_64__) || defined(_M_X64))
226 long long ival;
227 #else
228 int ival;
229 #endif
230 } cst_val_int;
231
232 typedef struct cst_val_atom_struct_void {
233 #ifdef WORDS_BIGENDIAN
234 short ref_count;
235 short type; /* order is here important */
236 #else
237 #if (defined(__x86_64__) || defined(_M_X64))
238 int type; /* order is here important */
239 int ref_count;
240 #else
241 short type; /* order is here important */
242 short ref_count;
243 #endif
244 #endif
245 void *vval;
246 } cst_val_void;
247
248 #ifdef WORDS_BIGENDIAN
249 #define DEF_CONST_VAL_INT(N,V) const cst_val_int N={-1, CST_VAL_TYPE_INT, V}
250 #define DEF_CONST_VAL_STRING(N,S) const cst_val_void N={-1,CST_VAL_TYPE_STRING,(void *)S}
251 #define DEF_CONST_VAL_FLOAT(N,F) const cst_val_float N={-1,CST_VAL_TYPE_FLOAT,(float)F}
252 #else
253 #define DEF_CONST_VAL_INT(N,V) const cst_val_int N={CST_VAL_TYPE_INT,-1,V}
254 #define DEF_CONST_VAL_STRING(N,S) const cst_val_void N={CST_VAL_TYPE_STRING,-1,(void *)S}
255 #define DEF_CONST_VAL_FLOAT(N,F) const cst_val_float N={CST_VAL_TYPE_FLOAT,-1,(float)F}
256 #endif
257 #define DEF_CONST_VAL_CONS(N,A,D) const cst_val_cons N={A,D}
258
259 /* in the non-union intialization version we these consts have to be */
260 /* more typed than need, we'll cast the back later */
261 extern const cst_val_int val_int_0;
262 extern const cst_val_int val_int_1;
263 extern const cst_val_int val_int_2;
264 extern const cst_val_int val_int_3;
265 extern const cst_val_int val_int_4;
266 extern const cst_val_int val_int_5;
267 extern const cst_val_int val_int_6;
268 extern const cst_val_int val_int_7;
269 extern const cst_val_int val_int_8;
270 extern const cst_val_int val_int_9;
271 extern const cst_val_int val_int_10;
272 extern const cst_val_int val_int_11;
273 extern const cst_val_int val_int_12;
274 extern const cst_val_int val_int_13;
275 extern const cst_val_int val_int_14;
276 extern const cst_val_int val_int_15;
277 extern const cst_val_int val_int_16;
278 extern const cst_val_int val_int_17;
279 extern const cst_val_int val_int_18;
280 extern const cst_val_int val_int_19;
281 extern const cst_val_int val_int_20;
282 extern const cst_val_int val_int_21;
283 extern const cst_val_int val_int_22;
284 extern const cst_val_int val_int_23;
285 extern const cst_val_int val_int_24;
286
287 extern const cst_val_void val_string_0;
288 extern const cst_val_void val_string_1;
289 extern const cst_val_void val_string_2;
290 extern const cst_val_void val_string_3;
291 extern const cst_val_void val_string_4;
292 extern const cst_val_void val_string_5;
293 extern const cst_val_void val_string_6;
294 extern const cst_val_void val_string_7;
295 extern const cst_val_void val_string_8;
296 extern const cst_val_void val_string_9;
297 extern const cst_val_void val_string_10;
298 extern const cst_val_void val_string_11;
299 extern const cst_val_void val_string_12;
300 extern const cst_val_void val_string_13;
301 extern const cst_val_void val_string_14;
302 extern const cst_val_void val_string_15;
303 extern const cst_val_void val_string_16;
304 extern const cst_val_void val_string_17;
305 extern const cst_val_void val_string_18;
306 extern const cst_val_void val_string_19;
307 extern const cst_val_void val_string_20;
308 extern const cst_val_void val_string_21;
309 extern const cst_val_void val_string_22;
310 extern const cst_val_void val_string_23;
311 extern const cst_val_void val_string_24;
312
313 #endif
314
315 #define DEF_STATIC_CONST_VAL_INT(N,V) static DEF_CONST_VAL_INT(N,V)
316 #define DEF_STATIC_CONST_VAL_STRING(N,S) static DEF_CONST_VAL_STRING(N,S)
317 #define DEF_STATIC_CONST_VAL_FLOAT(N,F) static DEF_CONST_VAL_FLOAT(N,F)
318 #define DEF_STATIC_CONST_VAL_CONS(N,A,D) static DEF_CONST_VAL_CONS(N,A,D)
319
320 /* Some actual val consts */
321 /* The have casts as in the non-union intialize case the casts are necessary */
322 /* but in the union initial case these casts are harmless */
323
324 #define VAL_INT_0 (cst_val *)&val_int_0
325 #define VAL_INT_1 (cst_val *)&val_int_1
326 #define VAL_INT_2 (cst_val *)&val_int_2
327 #define VAL_INT_3 (cst_val *)&val_int_3
328 #define VAL_INT_4 (cst_val *)&val_int_4
329 #define VAL_INT_5 (cst_val *)&val_int_5
330 #define VAL_INT_6 (cst_val *)&val_int_6
331 #define VAL_INT_7 (cst_val *)&val_int_7
332 #define VAL_INT_8 (cst_val *)&val_int_8
333 #define VAL_INT_9 (cst_val *)&val_int_9
334 #define VAL_INT_10 (cst_val *)&val_int_10
335 #define VAL_INT_11 (cst_val *)&val_int_11
336 #define VAL_INT_12 (cst_val *)&val_int_12
337 #define VAL_INT_13 (cst_val *)&val_int_13
338 #define VAL_INT_14 (cst_val *)&val_int_14
339 #define VAL_INT_15 (cst_val *)&val_int_15
340 #define VAL_INT_16 (cst_val *)&val_int_16
341 #define VAL_INT_17 (cst_val *)&val_int_17
342 #define VAL_INT_18 (cst_val *)&val_int_18
343 #define VAL_INT_19 (cst_val *)&val_int_19
344 #define VAL_INT_20 (cst_val *)&val_int_20
345 #define VAL_INT_21 (cst_val *)&val_int_21
346 #define VAL_INT_22 (cst_val *)&val_int_22
347 #define VAL_INT_23 (cst_val *)&val_int_23
348 #define VAL_INT_24 (cst_val *)&val_int_24
349
350 const cst_val *val_int_n(int n);
351
352 #define VAL_STRING_0 (cst_val *)&val_string_0
353 #define VAL_STRING_1 (cst_val *)&val_string_1
354 #define VAL_STRING_2 (cst_val *)&val_string_2
355 #define VAL_STRING_3 (cst_val *)&val_string_3
356 #define VAL_STRING_4 (cst_val *)&val_string_4
357 #define VAL_STRING_5 (cst_val *)&val_string_5
358 #define VAL_STRING_6 (cst_val *)&val_string_6
359 #define VAL_STRING_7 (cst_val *)&val_string_7
360 #define VAL_STRING_8 (cst_val *)&val_string_8
361 #define VAL_STRING_9 (cst_val *)&val_string_9
362 #define VAL_STRING_10 (cst_val *)&val_string_10
363 #define VAL_STRING_11 (cst_val *)&val_string_11
364 #define VAL_STRING_12 (cst_val *)&val_string_12
365 #define VAL_STRING_13 (cst_val *)&val_string_13
366 #define VAL_STRING_14 (cst_val *)&val_string_14
367 #define VAL_STRING_15 (cst_val *)&val_string_15
368 #define VAL_STRING_16 (cst_val *)&val_string_16
369 #define VAL_STRING_17 (cst_val *)&val_string_17
370 #define VAL_STRING_18 (cst_val *)&val_string_18
371 #define VAL_STRING_19 (cst_val *)&val_string_19
372 #define VAL_STRING_20 (cst_val *)&val_string_20
373 #define VAL_STRING_21 (cst_val *)&val_string_21
374 #define VAL_STRING_22 (cst_val *)&val_string_22
375 #define VAL_STRING_23 (cst_val *)&val_string_23
376 #define VAL_STRING_24 (cst_val *)&val_string_24
377
378 const cst_val *val_string_n(int n);
379
380
381 #endif