]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Oh my god microsoft you suck
[xonotic/gmqcc.git] / gmqcc.h
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef GMQCC_HDR
25 #define GMQCC_HDR
26 #include <stdarg.h>
27 #include <stdio.h> /* TODO: remove this */
28
29 /*
30  * Disable some over protective warnings in visual studio because fixing them is a waste
31  * of my time.
32  */
33 #ifdef _MSC_VER
34 #   pragma warning(disable : 4244 ) /* conversion from 'int' to 'float', possible loss of data */
35 #endif /*! _MSC_VER */
36
37 #define GMQCC_VERSION_MAJOR 0
38 #define GMQCC_VERSION_MINOR 3
39 #define GMQCC_VERSION_PATCH 0
40 #define GMQCC_VERSION_BUILD(J,N,P) (((J)<<16)|((N)<<8)|(P))
41 #define GMQCC_VERSION \
42     GMQCC_VERSION_BUILD(GMQCC_VERSION_MAJOR, GMQCC_VERSION_MINOR, GMQCC_VERSION_PATCH)
43 /* Undefine the following on a release-tag: */
44 #define GMQCC_VERSION_TYPE_DEVEL
45
46 /* Full version string in case we need it */
47 #ifdef GMQCC_VERSION_TYPE_DEVEL
48 #    ifdef GMQCC_GITINFO
49 #        define GMQCC_DEV_VERSION_STRING "git build: " GMQCC_GITINFO "\n"
50 #    elif defined(GMQCC_VERSION_TYPE_DEVEL)
51 #        define GMQCC_DEV_VERSION_STRING "development build\n"
52 #    else
53 #        define GMQCC_DEV_VERSION_STRING
54 #    endif /*! GMQCC_GITINGO */
55 #else
56 #    define GMQCC_DEV_VERSION_STRING
57 #endif
58
59 #define GMQCC_STRINGIFY(x) #x
60 #define GMQCC_IND_STRING(x) GMQCC_STRINGIFY(x)
61 #define GMQCC_FULL_VERSION_STRING \
62 "GMQCC " \
63 GMQCC_IND_STRING(GMQCC_VERSION_MAJOR) "." \
64 GMQCC_IND_STRING(GMQCC_VERSION_MINOR) "." \
65 GMQCC_IND_STRING(GMQCC_VERSION_PATCH) \
66 " Built " __DATE__ " " __TIME__ \
67 "\n" GMQCC_DEV_VERSION_STRING
68
69 /*
70  * We cannot rely on C99 at all, since compilers like MSVC
71  * simply don't support it.  We define our own boolean type
72  * as a result (since we cannot include <stdbool.h>). For
73  * compilers that are in 1999 mode (C99 compliant) we can use
74  * the language keyword _Bool which can allow for better code
75  * on GCC and GCC-like compilers, opposed to `int`.
76  */
77 #ifndef __cplusplus
78 #   ifdef  false
79 #       undef  false
80 #   endif /*! false */
81 #   ifdef  true
82 #       undef true
83 #   endif /*! true  */
84 #   define false (0)
85 #   define true  (1)
86 #   ifdef __STDC_VERSION__
87 #       if __STDC_VERSION__ < 199901L && __GNUC__ < 3
88             typedef int  bool;
89 #       else
90             typedef _Bool bool;
91 #       endif /*! __STDC_VERSION__ < 199901L && __GNUC__ < 3 */
92 #   else
93         typedef int bool;
94 #   endif /*! __STDC_VERSION__ */
95 #endif /*! __cplusplus      */
96
97 /*
98  * Of some functions which are generated we want to make sure
99  * that the result isn't ignored. To find such function calls,
100  * we use this macro.
101  */
102 #if defined(__GNUC__) || defined(__CLANG__)
103 #   define GMQCC_WARN __attribute__((warn_unused_result))
104 #   define GMQCC_USED __attribute__((used))
105 #else
106 #   define GMQCC_WARN
107 #   define GMQCC_USED
108 #endif /*! defined(__GNUC__) || defined (__CLANG__) */
109 /*
110  * This is a hack to silent clang regarding empty
111  * body if statements.
112  */
113 #define GMQCC_SUPPRESS_EMPTY_BODY do { } while (0)
114
115 /*
116  * Inline is not supported in < C90, however some compilers
117  * like gcc and clang might have an inline attribute we can
118  * use if present.
119  */
120 #ifdef __STDC_VERSION__
121 #    if __STDC_VERSION__ < 199901L
122 #       if defined(__GNUC__) || defined (__CLANG__)
123 #           if __GNUC__ < 2
124 #               define GMQCC_INLINE
125 #           else
126 #               define GMQCC_INLINE __attribute__ ((always_inline))
127 #           endif /*! __GNUC__ < 2 */
128 #       else
129 #           define GMQCC_INLINE
130 #       endif /*! defined(__GNUC__) || defined (__CLANG__) */
131 #    else
132 #       define GMQCC_INLINE inline
133 #    endif /*! __STDC_VERSION < 199901L */
134 /*
135  * Visual studio has __forcinline we can use.  So lets use that
136  * I suspect it also has just __inline of some sort, but our use
137  * of inline is correct (not guessed), WE WANT IT TO BE INLINE
138  */
139 #elif defined(_MSC_VER)
140 #    define GMQCC_INLINE __forceinline
141 #else
142 #    define GMQCC_INLINE
143 #endif /*! __STDC_VERSION__ */
144
145 /*
146  * noreturn is present in GCC and clang
147  * it's required for _ast_node_destory otherwise -Wmissing-noreturn
148  * in clang complains about there being no return since abort() is
149  * called.
150  */
151 #if (defined(__GNUC__) && __GNUC__ >= 2) || defined(__CLANG__)
152 #    define GMQCC_NORETURN __attribute__ ((noreturn))
153 #else
154 #    define GMQCC_NORETURN
155 #endif /*! (defined(__GNUC__) && __GNUC__ >= 2) || defined (__CLANG__) */
156
157 #ifndef _MSC_VER
158 #   include <stdint.h>
159 #else
160     typedef unsigned __int8  uint8_t;
161     typedef unsigned __int16 uint16_t;
162     typedef unsigned __int32 uint32_t;
163     typedef unsigned __int64 uint64_t;
164
165     typedef __int16          int16_t;
166     typedef __int32          int32_t;
167     typedef __int64          int64_t;
168 #endif /*! _MSC_VER */
169
170 /*
171  * Very roboust way at determining endianess at compile time: this handles
172  * almost every possible situation.  Otherwise a runtime check has to be
173  * performed.
174  */
175 #define GMQCC_BYTE_ORDER_LITTLE 1234
176 #define GMQCC_BYTE_ORDER_BIG    4321
177
178 #if defined (__GNUC__) || defined (__GNU_LIBRARY__)
179 #   if defined (__FreeBSD__) || defined (__OpenBSD__)
180 #       include <sys/endian.h>
181 #   elif defined (BSD) && (BSD >= 199103) || defined (__DJGPP__) || defined (__CYGWIN32__)
182 #       include <machine/endian.h>
183 #   elif defined (__APPLE__)
184 #       if defined (__BIG_ENDIAN__) && !defined(BIG_ENDIAN)
185 #           define BIG_ENDIAN
186 #       elif defined (__LITTLE_ENDIAN__) && !defined (LITTLE_ENDIAN)
187 #           define LITTLE_ENDIAN
188 #       endif /*! defined (__BIG_ENDIAN__) && !defined(BIG_ENDIAN) */
189 #   elif !defined (__MINGW32__)
190 #       include <endian.h>
191 #       if !defined (__BEOS__)
192 #           include <byteswap.h>
193 #       endif /*! !definde (__BEOS__) */
194 #   endif /*! defined (__FreeBSD__) || defined (__OpenBSD__) */
195 #endif /*! defined (__GNUC__) || defined (__GNU_LIBRARY__) */
196 #if !defined(PLATFORM_BYTE_ORDER)
197 #   if defined (LITTLE_ENDIAN) || defined (BIG_ENDIAN)
198 #       if defined (LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
199 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
200 #       elif !defined (LITTLE_ENDIAN) && defined (BIG_ENDIAN)
201 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
202 #       elif defined (BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
203 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
204 #       elif defined (BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
205 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
206 #       endif /*! defined (LITTLE_ENDIAN) && !defined(BIG_ENDIAN) */
207 #   elif defined (_LITTLE_ENDIAN) || defined (_BIG_ENDIAN)
208 #       if defined (_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
209 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
210 #       elif !defined (_LITTLE_ENDIAN) && defined (_BIG_ENDIAN)
211 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
212 #       elif defined (_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
213 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
214 #       elif defined (_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
215 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
216 #       endif /*! defined (_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) */
217 #   elif defined (__LITTLE_ENDIAN__) || defined (__BIG_ENDIAN__)
218 #       if defined (__LITTLE_ENDIAN__) && !defined (__BIG_ENDIAN__)
219 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
220 #       elif !defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
221 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
222 #       elif defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
223 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
224 #       elif defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
225 #           define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
226 #       endif /*! defined (__LITTLE_ENDIAN__) && !defined (__BIG_ENDIAN__) */
227 #   endif /*! defined(LITTLE_ENDIAN) || defined (BIG_ENDIAN) */
228 #endif /*! !defined(PLATFORM_BYTE_ORDER) */
229 #if !defined (PLATFORM_BYTE_ORDER)
230 #   if   defined (__alpha__) || defined (__alpha)    || defined (i386)       || \
231          defined (__i386__)  || defined (_M_I86)     || defined (_M_IX86)    || \
232          defined (__OS2__)   || defined (sun386)     || defined (__TURBOC__) || \
233          defined (vax)       || defined (vms)        || defined (VMS)        || \
234          defined (__VMS)     || defined (__x86_64__) || defined (_M_IA64)    || \
235          defined (_M_X64)    || defined (__i386)     || defined (__x86_64)
236 #       define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
237 #   elif defined (AMIGA)     || defined (applec)     || defined (__AS400__)  || \
238          defined (_CRAY)     || defined (__hppa)     || defined (__hp9000)   || \
239          defined (ibm370)    || defined (mc68000)    || defined (m68k)       || \
240          defined (__MRC__)   || defined (__MVS__)    || defined (__MWERKS__) || \
241          defined (sparc)     || defined (__sparc)    || defined (SYMANTEC_C) || \
242          defined (__TANDEM)  || defined (THINK_C)    || defined (__VMCMS__)  || \
243          defined (__PPC__)   || defined (__PPC)      || defined (PPC)
244 #       define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
245 #   else
246 #       define PLATFORM_BYTE_ORDER -1
247 #   endif
248 #endif /*! !defined (PLATFORM_BYTE_ORDER) */
249
250 /*
251  * On windows systems where we're not compiling with MING32 we need a
252  * little extra help on dependinces for implementing our own dirent.h
253  * in fs.c.
254  */
255 #if defined(_WIN32) && !defined(__MINGW32__)
256 #   define _WIN32_LEAN_AND_MEAN
257 #   include <windows.h>
258 #   include <io.h>
259 #   include <fcntl.h>
260
261     struct dirent {
262         long d_ino;
263         unsigned short     d_reclen;
264         unsigned short     d_namlen;
265         char               d_name[FILENAME_MAX];
266     };
267
268     typedef struct {
269         struct _finddata_t dd_dta;
270         struct dirent      dd_dir;
271         long               dd_handle;
272         int                dd_stat;
273         char               dd_name[1];
274     } DIR;
275     /*
276      * Visual studio also lacks S_ISDIR for sys/stat.h, so we emulate this as well
277      * which is not hard at all.
278      */
279 #    ifdef S_ISDIR
280 #        undef  S_ISDIR
281 #    endif /*! S_ISDIR */
282 #   define S_ISDIR(X) ((X)&_S_IFDIR)
283 #else
284 #   include <dirent.h>
285 #endif /*! _WIN32 && !defined(__MINGW32__) */
286
287 /*===================================================================*/
288 /*=========================== stat.c ================================*/
289 /*===================================================================*/
290 void  stat_info          (void);
291 char *stat_mem_strdup    (const char *, size_t,         const char *, bool);
292 void *stat_mem_reallocate(void *,       size_t, size_t, const char *);
293 void  stat_mem_deallocate(void *);
294 void *stat_mem_allocate  (size_t, size_t, const char *);
295
296 #define mem_a(SIZE)              stat_mem_allocate  ((SIZE), __LINE__, __FILE__)
297 #define mem_d(PTRN)              stat_mem_deallocate((void*)(PTRN))
298 #define mem_r(PTRN, SIZE)        stat_mem_reallocate((void*)(PTRN), (SIZE), __LINE__, __FILE__)
299 #define mem_af(SIZE, FILE, LINE) stat_mem_allocate  ((SIZE), (LINE), (FILE))
300
301 /* TODO: rename to mem variations */
302 #define util_strdup(SRC)         stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, false)
303 #define util_strdupe(SRC)        stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, true)
304
305 /*===================================================================*/
306 /*=========================== util.c ================================*/
307 /*===================================================================*/
308
309 /*
310  * Microsoft implements against the spec versions of ctype.h. Which
311  * means what ever the current set locale is will render the actual
312  * results of say isalpha('A') wrong for what ever retarded locale
313  * is used. Simalerly these are also implemented inefficently on
314  * some toolchains and end up becoming actual library calls. Perhaps
315  * this is why tools like yacc provide their own? Regardless implementing
316  * these as functions is equally as silly, the call overhead is not
317  * justified when this could happen on every character from an input
318  * stream. We provide our own as macros for absolute inlinability.
319  */
320 #define util_isupper(C) ((C) >= 'A' && (C) <= 'Z')
321 #define util_islower(C) ((C) >= 'a' && (C) <= 'z')
322 #define util_isdigit(C) ((C) >= '0' && (C) <= '9')
323 #define util_isprint(C) ((C) >= 32  && (C) <= 126)
324 #define util_isspace(C) ((C) == ' ' || (C) == '\f' || \
325                          (C) == '\n'|| (C) == '\r' || \
326                          (C) == '\t'|| (C) == '\v')
327
328 #define util_isalpha(C) (util_islower(C) || util_isupper(C))
329
330 bool  util_filexists     (const char *);
331 bool  util_strupper      (const char *);
332 bool  util_strdigit      (const char *);
333 void  util_debug         (const char *, const char *, ...);
334 void  util_endianswap    (void *,  size_t, unsigned int);
335
336 size_t util_strtocmd    (const char *, char *, size_t);
337 size_t util_strtononcmd (const char *, char *, size_t);
338
339 uint16_t util_crc16(uint16_t crc, const char *data, size_t len);
340
341 void     util_seed(uint32_t);
342 uint32_t util_rand(void);
343
344 /*
345  * String functions (formatting, copying, concatenating, errors). These are wrapped
346  * to use the MSVC _safe_ versions when using MSVC, plus some implementations of
347  * these are non-conformant or don't exist such as asprintf and snprintf, which are
348  * not supported in C90, but do exist in C99.
349  */
350 int         util_vasprintf(char **ret, const char *fmt, va_list);
351 int         util_asprintf (char **ret, const char *fmt, ...);
352 int         util_snprintf (char *src,  size_t bytes, const char *format, ...);
353 char       *util_strcat   (char *dest, const char *src);
354 char       *util_strncpy  (char *dest, const char *src, size_t num);
355 const char *util_strerror (int num);
356
357 /*
358  * A flexible vector implementation: all vector pointers contain some
359  * data about themselfs exactly - sizeof(vector_t) behind the pointer
360  * this data is represented in the structure below.  Doing this allows
361  * us to use the array [] to access individual elements from the vector
362  * opposed to using set/get methods.
363  */
364 typedef struct {
365     size_t  allocated;
366     size_t  used;
367
368     /* can be extended now! whoot */
369 } vector_t;
370
371 /* hidden interface */
372 void _util_vec_grow(void **a, size_t i, size_t s);
373 #define GMQCC_VEC_WILLGROW(X,Y) ( \
374     ((!(X) || vec_meta(X)->used + Y >= vec_meta(X)->allocated)) ? \
375         (void)_util_vec_grow(((void**)&(X)), (Y), sizeof(*(X))) : \
376         (void)0                                                   \
377 )
378
379 /* exposed interface */
380 #define vec_meta(A)       (((vector_t*)((void*)A)) - 1)
381 #define vec_free(A)       ((void)((A) ? (mem_d((void*)vec_meta(A)), (A) = NULL) : 0))
382 #define vec_push(A,V)     (GMQCC_VEC_WILLGROW((A),1), (A)[vec_meta(A)->used++] = (V))
383 #define vec_size(A)       ((A) ? vec_meta(A)->used : 0)
384 #define vec_add(A,N)      (GMQCC_VEC_WILLGROW((A),(N)), vec_meta(A)->used += (N), &(A)[vec_meta(A)->used-(N)])
385 #define vec_last(A)       ((A)[vec_meta(A)->used - 1])
386 #define vec_pop(A)        ((void)(vec_meta(A)->used -= 1))
387 #define vec_shrinkto(A,N) ((void)(vec_meta(A)->used  = (N)))
388 #define vec_shrinkby(A,N) ((void)(vec_meta(A)->used -= (N)))
389 #define vec_append(A,N,S) ((void)(memcpy(vec_add((A), (N)), (S), (N) * sizeof(*(S)))))
390 #define vec_upload(X,Y,S) ((void)(memcpy(vec_add((X), (S) * sizeof(*(Y))), (Y), (S) * sizeof(*(Y)))))
391 #define vec_remove(A,I,N) ((void)(memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N)))
392
393 typedef struct trie_s {
394     void          *value;
395     struct trie_s *entries;
396 } correct_trie_t;
397
398 correct_trie_t* correct_trie_new(void);
399
400 typedef struct hash_table_t {
401     size_t                size;
402     struct hash_node_t **table;
403 } hash_table_t, *ht;
404
405 /*
406  * hashtable implementation:
407  *
408  * Note:
409  *      This was designed for pointers:  you manage the life of the object yourself
410  *      if you do use this for non-pointers please be warned that the object may not
411  *      be valid if the duration of it exceeds (i.e on stack).  So you need to allocate
412  *      yourself, or put those in global scope to ensure duration is for the whole
413  *      runtime.
414  *
415  * util_htnew(size)                             -- to make a new hashtable
416  * util_htset(table, key, value, sizeof(value)) -- to set something in the table
417  * util_htget(table, key)                       -- to get something from the table
418  * util_htdel(table)                            -- to delete the table
419  *
420  * example of use:
421  *
422  * ht    foo  = util_htnew(1024);
423  * int   data = 100;
424  * char *test = "hello world\n";
425  * util_htset(foo, "foo", (void*)&data);
426  * util_gtset(foo, "bar", (void*)test);
427  *
428  * printf("foo: %d, bar %s",
429  *     *((int *)util_htget(foo, "foo")),
430  *      ((char*)util_htget(foo, "bar"))
431  * );
432  *
433  * util_htdel(foo);
434  */
435 hash_table_t *util_htnew (size_t size);
436 void          util_htrem (hash_table_t *ht, void (*callback)(void *data));
437 void          util_htset (hash_table_t *ht, const char *key, void *value);
438 void          util_htdel (hash_table_t *ht);
439 size_t        util_hthash(hash_table_t *ht, const char *key);
440 void          util_htseth(hash_table_t *ht, const char *key, size_t hash, void *value);
441 void          util_htrmh (hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*));
442 void          util_htrm  (hash_table_t *ht, const char *key, void (*cb)(void*));
443
444 void         *util_htget (hash_table_t *ht, const char *key);
445 void         *util_htgeth(hash_table_t *ht, const char *key, size_t hash);
446
447 /*===================================================================*/
448 /*============================ file.c ===============================*/
449 /*===================================================================*/
450 /* file handling */
451 void           fs_file_close  (FILE *);
452 int            fs_file_error  (FILE *);
453 int            fs_file_getc   (FILE *);
454 int            fs_file_printf (FILE *, const char *, ...);
455 int            fs_file_puts   (FILE *, const char *);
456 int            fs_file_seek   (FILE *, long int, int);
457 long int       fs_file_tell   (FILE *);
458
459 size_t         fs_file_read   (void *,        size_t, size_t, FILE *);
460 size_t         fs_file_write  (const void *,  size_t, size_t, FILE *);
461
462 FILE          *fs_file_open   (const char *, const char *);
463 int            fs_file_getline(char  **, size_t *, FILE *);
464
465 /* directory handling */
466 int            fs_dir_make    (const char *);
467 DIR           *fs_dir_open    (const char *);
468 int            fs_dir_close   (DIR *);
469 struct dirent *fs_dir_read    (DIR *);
470
471
472 /*===================================================================*/
473 /*=========================== correct.c =============================*/
474 /*===================================================================*/
475 typedef struct {
476     char   ***edits;
477     size_t  **lens;
478 } correction_t;
479
480 void  correct_del (correct_trie_t*, size_t **);
481 void  correct_add (correct_trie_t*, size_t ***, const char *);
482 char *correct_str (correction_t *, correct_trie_t*, const char *);
483 void  correct_init(correction_t *);
484 void  correct_free(correction_t *);
485
486 /*===================================================================*/
487 /*=========================== code.c ================================*/
488 /*===================================================================*/
489
490 /* TODO: cleanup */
491 /* Note: if you change the order, fix type_sizeof in ir.c */
492 enum {
493     TYPE_VOID     ,
494     TYPE_STRING   ,
495     TYPE_FLOAT    ,
496     TYPE_VECTOR   ,
497     TYPE_ENTITY   ,
498     TYPE_FIELD    ,
499     TYPE_FUNCTION ,
500     TYPE_POINTER  ,
501     TYPE_INTEGER  ,
502     TYPE_VARIANT  ,
503     TYPE_STRUCT   ,
504     TYPE_UNION    ,
505     TYPE_ARRAY    ,
506
507     TYPE_NIL      , /* it's its own type / untyped */
508     TYPE_NOEXPR   , /* simply invalid in expressions */
509
510     TYPE_COUNT
511 };
512
513 /* const/var qualifiers */
514 #define CV_NONE   0
515 #define CV_CONST  1
516 #define CV_VAR   -1
517 #define CV_WRONG  0x8000 /* magic number to help parsing */
518
519 extern const char    *type_name        [TYPE_COUNT];
520 extern const uint16_t type_store_instr [TYPE_COUNT];
521 extern const uint16_t field_store_instr[TYPE_COUNT];
522
523 /*
524  * could use type_store_instr + INSTR_STOREP_F - INSTR_STORE_F
525  * but this breaks when TYPE_INTEGER is added, since with the enhanced
526  * instruction set, the old ones are left untouched, thus the _I instructions
527  * are at a seperate place.
528  */
529 extern const uint16_t type_storep_instr[TYPE_COUNT];
530 extern const uint16_t type_eq_instr    [TYPE_COUNT];
531 extern const uint16_t type_ne_instr    [TYPE_COUNT];
532 extern const uint16_t type_not_instr   [TYPE_COUNT];
533
534 typedef struct {
535     uint32_t offset;      /* Offset in file of where data begins  */
536     uint32_t length;      /* Length of section (how many of)      */
537 } prog_section;
538
539 typedef struct {
540     uint32_t     version;      /* Program version (6)     */
541     uint16_t     crc16;
542     uint16_t     skip;
543
544     prog_section statements;   /* prog_section_statement  */
545     prog_section defs;         /* prog_section_def        */
546     prog_section fields;       /* prog_section_field      */
547     prog_section functions;    /* prog_section_function   */
548     prog_section strings;
549     prog_section globals;
550     uint32_t     entfield;     /* Number of entity fields */
551 } prog_header;
552
553 /*
554  * Each paramater incerements by 3 since vector types hold
555  * 3 components (x,y,z).
556  */
557 #define OFS_NULL      0
558 #define OFS_RETURN    1
559 #define OFS_PARM0     (OFS_RETURN+3)
560 #define OFS_PARM1     (OFS_PARM0 +3)
561 #define OFS_PARM2     (OFS_PARM1 +3)
562 #define OFS_PARM3     (OFS_PARM2 +3)
563 #define OFS_PARM4     (OFS_PARM3 +3)
564 #define OFS_PARM5     (OFS_PARM4 +3)
565 #define OFS_PARM6     (OFS_PARM5 +3)
566 #define OFS_PARM7     (OFS_PARM6 +3)
567
568 typedef struct {
569     uint16_t opcode;
570
571     /* operand 1 */
572     union {
573         int16_t  s1; /* signed   */
574         uint16_t u1; /* unsigned */
575     } o1;
576     /* operand 2 */
577     union {
578         int16_t  s1; /* signed   */
579         uint16_t u1; /* unsigned */
580     } o2;
581     /* operand 3 */
582     union {
583         int16_t  s1; /* signed   */
584         uint16_t u1; /* unsigned */
585     } o3;
586
587     /*
588      * This is the same as the structure in darkplaces
589      * {
590      *     unsigned short op;
591      *     short          a,b,c;
592      * }
593      * But this one is more sane to work with, and the
594      * type sizes are guranteed.
595      */
596 } prog_section_statement;
597
598 typedef struct {
599     /*
600      * The types:
601      * 0 = ev_void
602      * 1 = ev_string
603      * 2 = ev_float
604      * 3 = ev_vector
605      * 4 = ev_entity
606      * 5 = ev_field
607      * 6 = ev_function
608      * 7 = ev_pointer -- engine only
609      * 8 = ev_bad     -- engine only
610      */
611     uint16_t type;
612     uint16_t offset;
613     uint32_t name;
614 } prog_section_both;
615
616 typedef prog_section_both prog_section_def;
617 typedef prog_section_both prog_section_field;
618
619 /* this is ORed to the type */
620 #define DEF_SAVEGLOBAL (1<<15)
621 #define DEF_TYPEMASK   ((1<<15)-1)
622
623 typedef struct {
624     int32_t   entry;      /* in statement table for instructions  */
625     uint32_t  firstlocal; /* First local in local table           */
626     uint32_t  locals;     /* Total ints of params + locals        */
627     uint32_t  profile;    /* Always zero (engine uses this)       */
628     uint32_t  name;       /* name of function in string table     */
629     uint32_t  file;       /* file of the source file              */
630     int32_t   nargs;      /* number of arguments                  */
631     uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
632 } prog_section_function;
633
634 /*
635  * Instructions
636  * These are the external instructions supported by the interperter
637  * this is what things compile to (from the C code).
638  */
639 enum {
640     INSTR_DONE,
641     INSTR_MUL_F,
642     INSTR_MUL_V,
643     INSTR_MUL_FV, /* NOTE: the float operands must NOT be at the same locations: A != C */
644     INSTR_MUL_VF, /* and here: B != C */
645     INSTR_DIV_F,
646     INSTR_ADD_F,
647     INSTR_ADD_V,
648     INSTR_SUB_F,
649     INSTR_SUB_V,
650     INSTR_EQ_F,
651     INSTR_EQ_V,
652     INSTR_EQ_S,
653     INSTR_EQ_E,
654     INSTR_EQ_FNC,
655     INSTR_NE_F,
656     INSTR_NE_V,
657     INSTR_NE_S,
658     INSTR_NE_E,
659     INSTR_NE_FNC,
660     INSTR_LE,
661     INSTR_GE,
662     INSTR_LT,
663     INSTR_GT,
664     INSTR_LOAD_F,
665     INSTR_LOAD_V,
666     INSTR_LOAD_S,
667     INSTR_LOAD_ENT,
668     INSTR_LOAD_FLD,
669     INSTR_LOAD_FNC,
670     INSTR_ADDRESS,
671     INSTR_STORE_F,
672     INSTR_STORE_V,
673     INSTR_STORE_S,
674     INSTR_STORE_ENT,
675     INSTR_STORE_FLD,
676     INSTR_STORE_FNC,
677     INSTR_STOREP_F,
678     INSTR_STOREP_V,
679     INSTR_STOREP_S,
680     INSTR_STOREP_ENT,
681     INSTR_STOREP_FLD,
682     INSTR_STOREP_FNC,
683     INSTR_RETURN,
684     INSTR_NOT_F,
685     INSTR_NOT_V,
686     INSTR_NOT_S,
687     INSTR_NOT_ENT,
688     INSTR_NOT_FNC,
689     INSTR_IF,
690     INSTR_IFNOT,
691     INSTR_CALL0,
692     INSTR_CALL1,
693     INSTR_CALL2,
694     INSTR_CALL3,
695     INSTR_CALL4,
696     INSTR_CALL5,
697     INSTR_CALL6,
698     INSTR_CALL7,
699     INSTR_CALL8,
700     INSTR_STATE,
701     INSTR_GOTO,
702     INSTR_AND,
703     INSTR_OR,
704     INSTR_BITAND,
705     INSTR_BITOR,
706
707     /*
708      * Virtual instructions used by the IR
709      * Keep at the end!
710      */
711     VINSTR_END,
712     VINSTR_PHI,
713     VINSTR_JUMP,
714     VINSTR_COND,
715     /* A never returning CALL.
716      * Creating this causes IR blocks to be marked as 'final'.
717      * No-Return-Call
718      */
719     VINSTR_NRCALL
720 };
721
722 /* uhh? */
723 typedef float   qcfloat;
724 typedef int32_t qcint;
725
726 typedef struct {
727     prog_section_statement *statements;
728     int                    *linenums;
729     prog_section_def       *defs;
730     prog_section_field     *fields;
731     prog_section_function  *functions;
732     int                    *globals;
733     char                   *chars;
734     uint16_t                crc;
735     uint32_t                entfields;
736     ht                      string_cache;
737     qcint                   string_cached_empty;
738 } code_t;
739
740 /*
741  * code_write          -- writes out the compiled file
742  * code_init           -- prepares the code file
743  * code_genstrin       -- generates string for code
744  * code_alloc_field    -- allocated a field
745  * code_push_statement -- keeps statements and linenumbers together
746  * code_pop_statement  -- keeps statements and linenumbers together
747  */
748 bool      code_write         (code_t *, const char *filename, const char *lno);
749 GMQCC_WARN
750 code_t   *code_init          (void);
751 void      code_cleanup       (code_t *);
752 uint32_t  code_genstring     (code_t *, const char *string);
753 qcint     code_alloc_field   (code_t *, size_t qcsize);
754 void      code_push_statement(code_t *, prog_section_statement *stmt, int linenum);
755 void      code_pop_statement (code_t *);
756
757 /*
758  * A shallow copy of a lex_file to remember where which ast node
759  * came from.
760  */
761 typedef struct {
762     const char *file;
763     size_t      line;
764     size_t      column;
765 } lex_ctx;
766
767 /*===================================================================*/
768 /*============================ con.c ================================*/
769 /*===================================================================*/
770 enum {
771     CON_BLACK   = 30,
772     CON_RED,
773     CON_GREEN,
774     CON_BROWN,
775     CON_BLUE,
776     CON_MAGENTA,
777     CON_CYAN ,
778     CON_WHITE
779 };
780
781 /* message level */
782 enum {
783     LVL_MSG,
784     LVL_WARNING,
785     LVL_ERROR
786 };
787
788 FILE *con_default_out(void);
789 FILE *con_default_err(void);
790
791 void con_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap);
792 void con_printmsg  (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...);
793 void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap);
794 void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...);
795
796 void con_close (void);
797 void con_init  (void);
798 void con_reset (void);
799 void con_color (int);
800 int  con_change(const char *, const char *);
801 int  con_verr  (const char *, va_list);
802 int  con_vout  (const char *, va_list);
803 int  con_err   (const char *, ...);
804 int  con_out   (const char *, ...);
805
806 /* error/warning interface */
807 extern size_t compile_errors;
808 extern size_t compile_Werrors;
809 extern size_t compile_warnings;
810
811 void /********/ compile_error   (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, ...);
812 void /********/ vcompile_error  (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, va_list ap);
813 bool GMQCC_WARN compile_warning (lex_ctx ctx, int warntype, const char *fmt, ...);
814 bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap);
815 void            compile_show_werrors(void);
816
817 /*===================================================================*/
818 /*========================= assembler.c =============================*/
819 /*===================================================================*/
820 /* TODO: remove this ... */
821 static const struct {
822     const char  *m; /* menomic     */
823     const size_t o; /* operands    */
824     const size_t l; /* menomic len */
825 } asm_instr[] = {
826     { "DONE"      , 1, 4 },
827     { "MUL_F"     , 3, 5 },
828     { "MUL_V"     , 3, 5 },
829     { "MUL_FV"    , 3, 6 },
830     { "MUL_VF"    , 3, 6 },
831     { "DIV"       , 0, 3 },
832     { "ADD_F"     , 3, 5 },
833     { "ADD_V"     , 3, 5 },
834     { "SUB_F"     , 3, 5 },
835     { "SUB_V"     , 3, 5 },
836     { "EQ_F"      , 0, 4 },
837     { "EQ_V"      , 0, 4 },
838     { "EQ_S"      , 0, 4 },
839     { "EQ_E"      , 0, 4 },
840     { "EQ_FNC"    , 0, 6 },
841     { "NE_F"      , 0, 4 },
842     { "NE_V"      , 0, 4 },
843     { "NE_S"      , 0, 4 },
844     { "NE_E"      , 0, 4 },
845     { "NE_FNC"    , 0, 6 },
846     { "LE"        , 0, 2 },
847     { "GE"        , 0, 2 },
848     { "LT"        , 0, 2 },
849     { "GT"        , 0, 2 },
850     { "FIELD_F"   , 0, 7 },
851     { "FIELD_V"   , 0, 7 },
852     { "FIELD_S"   , 0, 7 },
853     { "FIELD_ENT" , 0, 9 },
854     { "FIELD_FLD" , 0, 9 },
855     { "FIELD_FNC" , 0, 9 },
856     { "ADDRESS"   , 0, 7 },
857     { "STORE_F"   , 0, 7 },
858     { "STORE_V"   , 0, 7 },
859     { "STORE_S"   , 0, 7 },
860     { "STORE_ENT" , 0, 9 },
861     { "STORE_FLD" , 0, 9 },
862     { "STORE_FNC" , 0, 9 },
863     { "STOREP_F"  , 0, 8 },
864     { "STOREP_V"  , 0, 8 },
865     { "STOREP_S"  , 0, 8 },
866     { "STOREP_ENT", 0, 10},
867     { "STOREP_FLD", 0, 10},
868     { "STOREP_FNC", 0, 10},
869     { "RETURN"    , 0, 6 },
870     { "NOT_F"     , 0, 5 },
871     { "NOT_V"     , 0, 5 },
872     { "NOT_S"     , 0, 5 },
873     { "NOT_ENT"   , 0, 7 },
874     { "NOT_FNC"   , 0, 7 },
875     { "IF"        , 0, 2 },
876     { "IFNOT"     , 0, 5 },
877     { "CALL0"     , 1, 5 },
878     { "CALL1"     , 2, 5 },
879     { "CALL2"     , 3, 5 },
880     { "CALL3"     , 4, 5 },
881     { "CALL4"     , 5, 5 },
882     { "CALL5"     , 6, 5 },
883     { "CALL6"     , 7, 5 },
884     { "CALL7"     , 8, 5 },
885     { "CALL8"     , 9, 5 },
886     { "STATE"     , 0, 5 },
887     { "GOTO"      , 0, 4 },
888     { "AND"       , 0, 3 },
889     { "OR"        , 0, 2 },
890     { "BITAND"    , 0, 6 },
891     { "BITOR"     , 0, 5 },
892
893     { "END"       , 0, 3 } /* virtual assembler instruction */
894 };
895 /*===================================================================*/
896 /*============================= ir.c ================================*/
897 /*===================================================================*/
898
899 enum store_types {
900     store_global,
901     store_local,  /* local, assignable for now, should get promoted later */
902     store_param,  /* parameters, they are locals with a fixed position */
903     store_value,  /* unassignable */
904     store_return  /* unassignable, at OFS_RETURN */
905 };
906
907 typedef struct {
908     qcfloat x, y, z;
909 } vector;
910
911 vector  vec3_add  (vector, vector);
912 vector  vec3_sub  (vector, vector);
913 qcfloat vec3_mulvv(vector, vector);
914 vector  vec3_mulvf(vector, float);
915
916 /*===================================================================*/
917 /*============================= exec.c ==============================*/
918 /*===================================================================*/
919
920 /* TODO: cleanup */
921 /*
922  * Darkplaces has (or will have) a 64 bit prog loader
923  * where the 32 bit qc program is autoconverted on load.
924  * Since we may want to support that as well, let's redefine
925  * float and int here.
926  */
927 typedef union {
928     qcint   _int;
929     qcint    string;
930     qcint    function;
931     qcint    edict;
932     qcfloat _float;
933     qcfloat vector[3];
934     qcint   ivector[3];
935 } qcany;
936
937 typedef char qcfloat_size_is_correct [sizeof(qcfloat) == 4 ?1:-1];
938 typedef char qcint_size_is_correct   [sizeof(qcint)   == 4 ?1:-1];
939
940 enum {
941     VMERR_OK,
942     VMERR_TEMPSTRING_ALLOC,
943
944     VMERR_END
945 };
946
947 #define VM_JUMPS_DEFAULT 1000000
948
949 /* execute-flags */
950 #define VMXF_DEFAULT 0x0000     /* default flags - nothing */
951 #define VMXF_TRACE   0x0001     /* trace: print statements before executing */
952 #define VMXF_PROFILE 0x0002     /* profile: increment the profile counters */
953
954 struct qc_program_s;
955
956 typedef int (*prog_builtin)(struct qc_program_s *prog);
957
958 typedef struct {
959     qcint                  stmt;
960     size_t                 localsp;
961     prog_section_function *function;
962 } qc_exec_stack;
963
964 typedef struct qc_program_s {
965     char           *filename;
966
967     prog_section_statement *code;
968     prog_section_def       *defs;
969     prog_section_def       *fields;
970     prog_section_function  *functions;
971     char                   *strings;
972     qcint                  *globals;
973     qcint                  *entitydata;
974     bool                   *entitypool;
975
976     const char*            *function_stack;
977
978     uint16_t crc16;
979
980     size_t tempstring_start;
981     size_t tempstring_at;
982
983     qcint  vmerror;
984
985     size_t *profile;
986
987     prog_builtin *builtins;
988     size_t        builtins_count;
989
990     /* size_t ip; */
991     qcint  entities;
992     size_t entityfields;
993     bool   allowworldwrites;
994
995     qcint         *localstack;
996     qc_exec_stack *stack;
997     size_t statement;
998
999     size_t xflags;
1000
1001     int    argc; /* current arg count for debugging */
1002 } qc_program;
1003
1004 qc_program* prog_load(const char *filename, bool ignoreversion);
1005 void        prog_delete(qc_program *prog);
1006
1007 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps);
1008
1009 const char*       prog_getstring (qc_program *prog, qcint str);
1010 prog_section_def* prog_entfield  (qc_program *prog, qcint off);
1011 prog_section_def* prog_getdef    (qc_program *prog, qcint off);
1012 qcany*            prog_getedict  (qc_program *prog, qcint e);
1013 qcint             prog_tempstring(qc_program *prog, const char *_str);
1014
1015
1016 /*===================================================================*/
1017 /*===================== parser.c commandline ========================*/
1018 /*===================================================================*/
1019 struct parser_s;
1020 struct parser_s *parser_create        (void);
1021 bool             parser_compile_file  (struct parser_s *parser, const char *);
1022 bool             parser_compile_string(struct parser_s *parser, const char *, const char *, size_t);
1023 bool             parser_finish        (struct parser_s *parser, const char *);
1024 void             parser_cleanup       (struct parser_s *parser);
1025
1026 /*===================================================================*/
1027 /*====================== ftepp.c commandline ========================*/
1028 /*===================================================================*/
1029 struct ftepp_s;
1030 struct ftepp_s *ftepp_create           (void);
1031 bool            ftepp_preprocess_file  (struct ftepp_s *ftepp, const char *filename);
1032 bool            ftepp_preprocess_string(struct ftepp_s *ftepp, const char *name, const char *str);
1033 void            ftepp_finish           (struct ftepp_s *ftepp);
1034 const char     *ftepp_get              (struct ftepp_s *ftepp);
1035 void            ftepp_flush            (struct ftepp_s *ftepp);
1036 void            ftepp_add_define       (struct ftepp_s *ftepp, const char *source, const char *name);
1037 void            ftepp_add_macro        (struct ftepp_s *ftepp, const char *name,   const char *value);
1038
1039 /*===================================================================*/
1040 /*======================= main.c commandline ========================*/
1041 /*===================================================================*/
1042
1043 #if 1
1044 /* Helpers to allow for a whole lot of flags. Otherwise we'd limit
1045  * to 32 or 64 -f options...
1046  */
1047 typedef struct {
1048     size_t  idx; /* index into an array of 32 bit words */
1049     uint8_t bit; /* bit index for the 8 bit group idx points to */
1050 } longbit;
1051 #define LONGBIT(bit) { ((bit)/32), ((bit)%32) }
1052 #define LONGBIT_SET(B, I) ((B).idx = (I)/32, (B).bit = ((I)%32))
1053 #else
1054 typedef uint32_t longbit;
1055 #define LONGBIT(bit) (bit)
1056 #define LONGBIT_SET(B, I) ((B) = (I))
1057 #endif
1058
1059 /*===================================================================*/
1060 /*=========================== utf8lib.c =============================*/
1061 /*===================================================================*/
1062 typedef uint32_t uchar_t;
1063
1064 bool    u8_analyze (const char *_s, size_t *_start, size_t *_len, uchar_t *_ch, size_t _maxlen);
1065 size_t  u8_strlen  (const char*);
1066 size_t  u8_strnlen (const char*, size_t);
1067 uchar_t u8_getchar (const char*, const char**);
1068 uchar_t u8_getnchar(const char*, const char**, size_t);
1069 int     u8_fromchar(uchar_t w,   char *to,     size_t maxlen);
1070
1071 /*===================================================================*/
1072 /*============================= opts.c ==============================*/
1073 /*===================================================================*/
1074 typedef struct {
1075     const char *name;
1076     longbit     bit;
1077 } opts_flag_def;
1078
1079 bool opts_setflag  (const char *, bool);
1080 bool opts_setwarn  (const char *, bool);
1081 bool opts_setwerror(const char *, bool);
1082 bool opts_setoptim (const char *, bool);
1083
1084 void opts_init         (const char *, int, size_t);
1085 void opts_set          (uint32_t   *, size_t, bool);
1086 void opts_setoptimlevel(unsigned int);
1087 void opts_ini_init     (const char *);
1088
1089 /* Saner flag handling */
1090 void opts_backup_non_Wall(void);
1091 void opts_restore_non_Wall(void);
1092 void opts_backup_non_Werror_all(void);
1093 void opts_restore_non_Werror_all(void);
1094
1095 enum {
1096 # define GMQCC_TYPE_FLAGS
1097 # define GMQCC_DEFINE_FLAG(X) X,
1098 #  include "opts.def"
1099     COUNT_FLAGS
1100 };
1101 static const opts_flag_def opts_flag_list[] = {
1102 # define GMQCC_TYPE_FLAGS
1103 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
1104 #  include "opts.def"
1105     { NULL, LONGBIT(0) }
1106 };
1107
1108 enum {
1109 # define GMQCC_TYPE_WARNS
1110 # define GMQCC_DEFINE_FLAG(X) WARN_##X,
1111 #  include "opts.def"
1112     COUNT_WARNINGS
1113 };
1114 static const opts_flag_def opts_warn_list[] = {
1115 # define GMQCC_TYPE_WARNS
1116 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
1117 #  include "opts.def"
1118     { NULL, LONGBIT(0) }
1119 };
1120
1121 enum {
1122 # define GMQCC_TYPE_OPTIMIZATIONS
1123 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) OPTIM_##NAME,
1124 #  include "opts.def"
1125     COUNT_OPTIMIZATIONS
1126 };
1127 static const opts_flag_def opts_opt_list[] = {
1128 # define GMQCC_TYPE_OPTIMIZATIONS
1129 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
1130 #  include "opts.def"
1131     { NULL, LONGBIT(0) }
1132 };
1133 static const unsigned int opts_opt_oflag[] = {
1134 # define GMQCC_TYPE_OPTIMIZATIONS
1135 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) MIN_O,
1136 #  include "opts.def"
1137     0
1138 };
1139
1140 enum {
1141 #   define GMQCC_TYPE_OPTIONS
1142 #   define GMQCC_DEFINE_FLAG(X) OPTION_##X,
1143 #   include "opts.def"
1144     OPTION_COUNT
1145 };
1146
1147 extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
1148
1149 /* other options: */
1150 typedef enum {
1151     COMPILER_QCC,     /* circa  QuakeC */
1152     COMPILER_FTEQCC,  /* fteqcc QuakeC */
1153     COMPILER_QCCX,    /* qccx   QuakeC */
1154     COMPILER_GMQCC    /* this   QuakeC */
1155 } opts_std_t;
1156
1157 typedef union {
1158     bool     B;
1159     uint16_t U16;
1160     uint32_t U32;
1161     char    *STR;
1162 } opt_value_t;
1163
1164
1165 typedef struct {
1166     opt_value_t  options      [OPTION_COUNT];
1167     uint32_t     flags        [1 + (COUNT_FLAGS         / 32)];
1168     uint32_t     warn         [1 + (COUNT_WARNINGS      / 32)];
1169     uint32_t     werror       [1 + (COUNT_WARNINGS      / 32)];
1170     uint32_t     warn_backup  [1 + (COUNT_WARNINGS      / 32)];
1171     uint32_t     werror_backup[1 + (COUNT_WARNINGS      / 32)];
1172     uint32_t     optimization [1 + (COUNT_OPTIMIZATIONS / 32)];
1173     bool         optimizeoff; /* True when -O0 */
1174 } opts_cmd_t;
1175
1176 extern opts_cmd_t opts;
1177
1178 #define OPTS_GENERIC(f,i)    (!! (((f)[(i)/32]) & (1<< (unsigned)((i)%32))))
1179 #define OPTS_FLAG(i)         OPTS_GENERIC(opts.flags,        (i))
1180 #define OPTS_WARN(i)         OPTS_GENERIC(opts.warn,         (i))
1181 #define OPTS_WERROR(i)       OPTS_GENERIC(opts.werror,       (i))
1182 #define OPTS_OPTIMIZATION(i) OPTS_GENERIC(opts.optimization, (i))
1183 #define OPTS_OPTION_BOOL(X) (opts.options[X].B)
1184 #define OPTS_OPTION_U16(X)  (opts.options[X].U16)
1185 #define OPTS_OPTION_U32(X)  (opts.options[X].U32)
1186 #define OPTS_OPTION_STR(X)  (opts.options[X].STR)
1187
1188 #endif /*! GMQCC_HDR */