]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Removed even more warnings
[xonotic/gmqcc.git] / gmqcc.h
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler, Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_HDR
24 #define GMQCC_HDR
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30
31
32 #define GMQCC_VERSION_MAJOR 0
33 #define GMQCC_VERSION_MINOR 1
34 #define GMQCC_VERSION_PATCH 0
35 #define GMQCC_VERSION_BUILD(J,N,P) (((J)<<16)|((N)<<8)|(P))
36 #define GMQCC_VERSION \
37     GMQCC_VERSION_BUILD(GMQCC_VERSION_MAJOR, GMQCC_VERSION_MINOR, GMQCC_VERSION_PATCH)
38
39 /*
40  * We cannoy rely on C99 at all, since compilers like MSVC
41  * simply don't support it.  We define our own boolean type
42  * as a result (since we cannot include <stdbool.h>). For
43  * compilers that are in 1999 mode (C99 compliant) we can use
44  * the language keyword _Bool which can allow for better code
45  * on GCC and GCC-like compilers, opposed to `int`.
46  */
47 #ifndef __cplusplus
48 #   ifdef  false
49 #       undef  false
50 #   endif /* !false */
51 #   ifdef  true
52 #       undef true
53 #   endif /* !true  */
54 #   define false (0)
55 #   define true  (1)
56 #   if __STDC_VERSION__ < 199901L && __GNUC__ < 3
57         typedef int  _Bool
58 #   else
59         typedef _Bool bool;
60 #   endif
61 #   endif /* !__cplusplus */
62
63 /*
64  * Of some functions which are generated we want to make sure
65  * that the result isn't ignored. To find such function calls,
66  * we use this macro.
67  */
68 #if defined(__GNUC__) || defined(__CLANG__)
69 #   define GMQCC_WARN __attribute__((warn_unused_result))
70 #else
71 #   define GMQCC_WARN
72 #endif
73 /*
74  * This is a hack to silent clang regarding empty
75  * body if statements.
76  */
77 #define GMQCC_SUPRESS_EMPTY_BODY do { } while (0)
78
79 /*
80  * Inline is not supported in < C90, however some compilers
81  * like gcc and clang might have an inline attribute we can
82  * use if present.
83  */
84 #if __STDC_VERSION__ < 199901L
85 #   if defined(__GNUC__) || defined (__CLANG__)
86 #       if __GNUC__ < 2
87 #           define GMQCC_INLINE
88 #       else
89 #           define GMQCC_INLINE __attribute__ ((always_inline))
90 #       endif
91 #   else
92 #       define GMQCC_INLINE
93 #   endif
94 #else
95 #   define GMQCC_INLINE inline
96 #endif
97
98 /*
99  * noreturn is present in GCC and clang
100  * it's required for _ast_node_destory otherwise -Wmissing-noreturn
101  * in clang complains about there being no return since abort() is
102  * called.
103  */
104 #if (defined(__GNUC__) && __GNUC__ >= 2) || defined(__CLANG__)
105 #    define GMQCC_NORETURN __attribute__ ((noreturn))
106 #else
107 #    define GMQCC_NORETURN
108 #endif
109
110 /*
111  * stdint.h and inttypes.h -less subset
112  * for systems that don't have it, which we must
113  * assume is all systems. (int8_t not required)
114  */
115 #if   CHAR_MIN  == -128
116     typedef unsigned char  uint8_t; /* same as below */
117 #elif SCHAR_MIN == -128
118     typedef unsigned char  uint8_t; /* same as above */
119 #endif
120 #if   SHRT_MAX  == 0x7FFF
121     typedef short          int16_t;
122     typedef unsigned short uint16_t;
123 #elif INT_MAX   == 0x7FFF
124     typedef int            int16_t;
125     typedef unsigned int   uint16_t;
126 #endif
127 #if   INT_MAX   == 0x7FFFFFFF
128     typedef int            int32_t;
129     typedef unsigned int   uint32_t;
130     typedef long           int64_t;
131     typedef unsigned long  uint64_t;
132 #elif LONG_MAX  == 0x7FFFFFFF
133     typedef long           int32_t;
134     typedef unsigned long  uint32_t;
135
136     /*
137      * It's nearly impossible to figure out a 64bit type at
138      * this point without making assumptions about the build
139      * enviroment.  So if clang or gcc is detected use some
140      * compiler builtins to create a 64 signed and unsigned
141      * type.
142      */
143 #   if defined(__GNUC__) || defined (__CLANG__)
144         typedef int          int64_t  __attribute__((__mode__(__DI__)));
145         typedef unsigned int uint64_t __attribute__((__mode__(__DI__)));
146 #   else
147         /*
148          * Incoorectly size the types so static assertions below will
149          * fail.  There is no valid way to get a 64bit type at this point
150          * without making assumptions of too many things.
151          */
152         typedef char         int64_t;
153         typedef char         uint64_t;
154 #   endif
155 #endif
156 #ifdef _LP64 /* long pointer == 64 */
157     typedef unsigned long  uintptr_t;
158     typedef long           intptr_t;
159 #else
160     typedef unsigned int   uintptr_t;
161     typedef int            intptr_t;
162 #endif
163 /* Ensure type sizes are correct: */
164 typedef char uint8_size_is_correct  [sizeof(uint8_t)  == 1?1:-1];
165 typedef char uint16_size_if_correct [sizeof(uint16_t) == 2?1:-1];
166 typedef char uint32_size_is_correct [sizeof(uint32_t) == 4?1:-1];
167 typedef char uint64_size_is_correct [sizeof(uint64_t) == 8?1:-1];
168 typedef char int16_size_if_correct  [sizeof(int16_t)  == 2?1:-1];
169 typedef char int32_size_is_correct  [sizeof(int32_t)  == 4?1:-1];
170 typedef char int64_size_is_correct  [sizeof(int64_t)  == 8?1:-1];
171 /* intptr_t / uintptr_t correct size check */
172 typedef char uintptr_size_is_correct[sizeof(intptr_t) == sizeof(int*)?1:-1];
173 typedef char intptr_size_is_correct [sizeof(uintptr_t)== sizeof(int*)?1:-1];
174
175 /*===================================================================*/
176 /*============================ lex.c ================================*/
177 /*===================================================================*/
178 typedef struct lex_file_t {
179     FILE *file;        /* file handler */
180     char *name;        /* name of file */
181     char  peek  [5];
182     char  lastok[8192];
183
184     int   last;    /* last token                   */
185     int   current; /* current token                */
186     int   length;  /* bytes left to parse          */
187     int   size;    /* never changes (size of file) */
188     int   line;    /* what line are we on?         */
189 } lex_file;
190
191 /*
192  * It's important that this table never exceed 32 keywords, the ascii
193  * table starts at 33 (and we don't want conflicts)
194  */
195 enum {
196     TOKEN_DO       ,
197     TOKEN_ELSE     ,
198     TOKEN_IF       ,
199     TOKEN_WHILE    ,
200     TOKEN_BREAK    ,
201     TOKEN_CONTINUE ,
202     TOKEN_RETURN   ,
203     TOKEN_GOTO     ,
204     TOKEN_FOR      ,   /* extension */
205     TOKEN_TYPEDEF  ,   /* extension */
206
207     /* ensure the token types are out of the  */
208     /* bounds of anyothers that may conflict. */
209     TOKEN_FLOAT    = 110,
210     TOKEN_VECTOR        ,
211     TOKEN_STRING        ,
212     TOKEN_ENTITY        ,
213     TOKEN_VOID
214 };
215
216 /*
217  * Lexer state constants, these are numbers for where exactly in
218  * the lexing the lexer is at. Or where it decided to stop if a lexer
219  * error occurs.  These numbers must be > where the ascii-table ends
220  * and > the last type token which is TOKEN_VOID
221  */
222 enum {
223     LEX_COMMENT = 1128,
224     LEX_CHRLIT        ,
225     LEX_STRLIT        ,
226     LEX_IDENT
227 };
228
229 int       lex_token  (lex_file *);
230 void      lex_reset  (lex_file *);
231 void      lex_close  (lex_file *);
232 void      lex_parse  (lex_file *);
233 lex_file *lex_include(lex_file *, const char *);
234 void      lex_init   (const char *, lex_file **);
235
236 /*===================================================================*/
237 /*========================== error.c ================================*/
238 /*===================================================================*/
239 #define ERROR_LEX      (SHRT_MAX+0)
240 #define ERROR_PARSE    (SHRT_MAX+1)
241 #define ERROR_INTERNAL (SHRT_MAX+2)
242 #define ERROR_COMPILER (SHRT_MAX+3)
243 #define ERROR_PREPRO   (SHRT_MAX+4)
244 int error(lex_file *, int, const char *, ...);
245
246 /*===================================================================*/
247 /*========================== parse.c ================================*/
248 /*===================================================================*/
249 int parse_gen(lex_file *);
250
251 /*===================================================================*/
252 /*========================== typedef.c ==============================*/
253 /*===================================================================*/
254 typedef struct typedef_node_t {
255     char      *name;
256 } typedef_node;
257
258 void          typedef_init();
259 void          typedef_clear();
260 typedef_node *typedef_find(const char *);
261 int           typedef_add (lex_file *file, const char *, const char *);
262
263
264 /*===================================================================*/
265 /*=========================== util.c ================================*/
266 /*===================================================================*/
267 void *util_memory_a      (unsigned int, unsigned int, const char *);
268 void  util_memory_d      (void       *, unsigned int, const char *);
269 void  util_meminfo       ();
270
271 bool  util_strupper      (const char *);
272 bool  util_strdigit      (const char *);
273 bool  util_strncmpexact  (const char *, const char *, size_t);
274 char *util_strdup        (const char *);
275 char *util_strrq         (const char *);
276 char *util_strrnl        (const char *);
277 char *util_strsws        (const char *);
278 char *util_strchp        (const char *, const char *);
279 void  util_debug         (const char *, const char *, ...);
280 int   util_getline       (char **, size_t *, FILE *);
281 void  util_endianswap    (void *,  int, int);
282
283 uint32_t util_crc32(const char *, int, register const short);
284
285 #ifdef NOTRACK
286 #    define mem_a(x) malloc(x)
287 #    define mem_d(x) free  (x)
288 #else
289 #    define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
290 #    define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
291 #endif
292
293 /*
294  * TODO: make these safer to use.  Currently this only works on
295  * x86 and x86_64, some systems will likely not like this. Such
296  * as BE systems.
297  */
298 #define FLT2INT(Y) *((int32_t*)&(Y))
299 #define INT2FLT(Y) *((float  *)&(Y))
300
301 /* Builds vector type (usefull for inside structures) */
302 #define VECTOR_SNAP(X,Y) X ## Y
303 #define VECTOR_FILL(X,Y) VECTOR_SNAP(X,Y)
304 #define VECTOR_TYPE(T,N)                                        \
305     T*     N##_data      = NULL;                                \
306     long   N##_elements  = 0;                                   \
307     long   N##_allocated = 0
308 /* Builds vector add */
309 #define VECTOR_CORE(T,N)                                        \
310     int    N##_add(T element) {                                 \
311         void *temp = NULL;                                      \
312         if (N##_elements == N##_allocated) {                    \
313             if (N##_allocated == 0) {                           \
314                 N##_allocated = 12;                             \
315             } else {                                            \
316                 N##_allocated *= 2;                             \
317             }                                                   \
318             if  (!(temp = mem_a(N##_allocated * sizeof(T)))) {  \
319                 mem_d(temp);                                    \
320                 return -1;                                      \
321             }                                                   \
322             memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
323             mem_d(N##_data);                                    \
324             N##_data = (T*)temp;                                \
325         }                                                       \
326         N##_data[N##_elements] = element;                       \
327         return   N##_elements++;                                \
328     }                                                           \
329     int N##_put(T* elements, size_t len) {                      \
330         len     --;                                             \
331         elements--;                                             \
332         while (N##_add(*++elements) != -1 && len--);            \
333         return N##_elements;                                    \
334     }                                                           \
335     typedef char VECTOR_FILL(extra_semicolon_,__COUNTER__)
336 #define VECTOR_PROT(T,N)                                        \
337     extern T*     N##_data     ;                                \
338     extern long   N##_elements ;                                \
339     extern long   N##_allocated;                                \
340     int           N##_add(T);                                   \
341     int           N##_put(T *, size_t)
342 #define VECTOR_MAKE(T,N) \
343     VECTOR_TYPE(T,N);    \
344     VECTOR_CORE(T,N)
345
346 /*===================================================================*/
347 /*=========================== code.c ================================*/
348 /*===================================================================*/
349 enum {
350     TYPE_VOID     ,
351     TYPE_STRING   ,
352     TYPE_FLOAT    ,
353     TYPE_VECTOR   ,
354     TYPE_ENTITY   ,
355     TYPE_FIELD    ,
356     TYPE_FUNCTION ,
357     TYPE_POINTER  ,
358     /* TYPE_INTEGER  , */
359     TYPE_VARIANT
360 };
361
362 /*
363  * Each paramater incerements by 3 since vector types hold
364  * 3 components (x,y,z).
365  */
366 #define OFS_NULL      0
367 #define OFS_RETURN    1
368 #define OFS_PARM0     (OFS_RETURN+3)
369 #define OFS_PARM1     (OFS_PARM0 +3)
370 #define OFS_PARM2     (OFS_PARM1 +3)
371 #define OFS_PARM3     (OFS_PARM2 +3)
372 #define OFS_PARM4     (OFS_PARM3 +3)
373 #define OFS_PARM5     (OFS_PARM4 +3)
374 #define OFS_PARM6     (OFS_PARM5 +3)
375 #define OFS_PARM7     (OFS_PARM6 +3)
376
377 typedef struct {
378     uint16_t opcode;
379
380     /* operand 1 */
381     union {
382         int16_t  s1; /* signed   */
383         uint16_t u1; /* unsigned */
384     } o1;
385     /* operand 2 */
386     union {
387         int16_t  s1; /* signed   */
388         uint16_t u1; /* unsigned */
389     } o2;
390     /* operand 3 */
391     union {
392         int16_t  s1; /* signed   */
393         uint16_t u1; /* unsigned */
394     } o3;
395
396     /*
397      * This is the same as the structure in darkplaces
398      * {
399      *     unsigned short op;
400      *     short          a,b,c;
401      * }
402      * But this one is more sane to work with, and the
403      * type sizes are guranteed.
404      */
405 } prog_section_statement;
406
407 typedef struct {
408     /* The types:
409      * 0 = ev_void
410      * 1 = ev_string
411      * 2 = ev_float
412      * 3 = ev_vector
413      * 4 = ev_entity
414      * 5 = ev_field
415      * 6 = ev_function
416      * 7 = ev_pointer -- engine only
417      * 8 = ev_bad     -- engine only
418      */
419     uint16_t type;
420     uint16_t offset;
421     uint32_t name;
422 } prog_section_both;
423 typedef prog_section_both prog_section_def;
424 typedef prog_section_both prog_section_field;
425
426 typedef struct {
427     int32_t   entry;      /* in statement table for instructions  */
428     uint32_t  firstlocal; /* First local in local table           */
429     uint32_t  locals;     /* Total ints of params + locals        */
430     uint32_t  profile;    /* Always zero (engine uses this)       */
431     uint32_t  name;       /* name of function in string table     */
432     uint32_t  file;       /* file of the source file              */
433     uint32_t  nargs;      /* number of arguments                  */
434     uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
435 } prog_section_function;
436
437 /*
438  * Instructions
439  * These are the external instructions supported by the interperter
440  * this is what things compile to (from the C code).
441  */
442 enum {
443     INSTR_DONE,
444     INSTR_MUL_F,
445     INSTR_MUL_V,
446     INSTR_MUL_FV,
447     INSTR_MUL_VF,
448     INSTR_DIV_F,
449     INSTR_ADD_F,
450     INSTR_ADD_V,
451     INSTR_SUB_F,
452     INSTR_SUB_V,
453     INSTR_EQ_F,
454     INSTR_EQ_V,
455     INSTR_EQ_S,
456     INSTR_EQ_E,
457     INSTR_EQ_FNC,
458     INSTR_NE_F,
459     INSTR_NE_V,
460     INSTR_NE_S,
461     INSTR_NE_E,
462     INSTR_NE_FNC,
463     INSTR_LE,
464     INSTR_GE,
465     INSTR_LT,
466     INSTR_GT,
467     INSTR_LOAD_F,
468     INSTR_LOAD_V,
469     INSTR_LOAD_S,
470     INSTR_LOAD_ENT,
471     INSTR_LOAD_FLD,
472     INSTR_LOAD_FNC,
473     INSTR_ADDRESS,
474     INSTR_STORE_F,
475     INSTR_STORE_V,
476     INSTR_STORE_S,
477     INSTR_STORE_ENT,
478     INSTR_STORE_FLD,
479     INSTR_STORE_FNC,
480     INSTR_STOREP_F,
481     INSTR_STOREP_V,
482     INSTR_STOREP_S,
483     INSTR_STOREP_ENT,
484     INSTR_STOREP_FLD,
485     INSTR_STOREP_FNC,
486     INSTR_RETURN,
487     INSTR_NOT_F,
488     INSTR_NOT_V,
489     INSTR_NOT_S,
490     INSTR_NOT_ENT,
491     INSTR_NOT_FNC,
492     INSTR_IF,
493     INSTR_IFNOT,
494     INSTR_CALL0,
495     INSTR_CALL1,
496     INSTR_CALL2,
497     INSTR_CALL3,
498     INSTR_CALL4,
499     INSTR_CALL5,
500     INSTR_CALL6,
501     INSTR_CALL7,
502     INSTR_CALL8,
503     INSTR_STATE,
504     INSTR_GOTO,
505     INSTR_AND,
506     INSTR_OR,
507     INSTR_BITAND,
508     INSTR_BITOR,
509
510     /*
511      * Virtual instructions used by the assembler
512      * keep at the end but before virtual instructions
513      * for the IR below.
514      */
515     AINSTR_END,
516
517     /*
518      * Virtual instructions used by the IR
519      * Keep at the end!
520      */
521     VINSTR_PHI,
522     VINSTR_JUMP,
523     VINSTR_COND
524 };
525
526 /*
527  * The symbols below are created by the following
528  * expanded macros:
529  *
530  * VECTOR_MAKE(prog_section_statement, code_statements);
531  * VECTOR_MAKE(prog_section_def,       code_defs      );
532  * VECTOR_MAKE(prog_section_field,     code_fields    );
533  * VECTOR_MAKE(prog_section_function,  code_functions );
534  * VECTOR_MAKE(int,                    code_globals   );
535  * VECTOR_MAKE(char,                   code_chars     );
536  */
537 VECTOR_PROT(prog_section_statement, code_statements);
538 VECTOR_PROT(prog_section_statement, code_statements);
539 VECTOR_PROT(prog_section_def,       code_defs      );
540 VECTOR_PROT(prog_section_field,     code_fields    );
541 VECTOR_PROT(prog_section_function,  code_functions );
542 VECTOR_PROT(int,                    code_globals   );
543 VECTOR_PROT(char,                   code_chars     );
544
545 /*
546  * code_write -- writes out the compiled file
547  * code_init  -- prepares the code file
548  */
549 bool     code_write       (const char *filename);
550 void     code_init        ();
551 uint32_t code_genstring   (const char *string);
552 uint32_t code_cachedstring(const char *string);
553
554 /*===================================================================*/
555 /*========================= assembler.c =============================*/
556 /*===================================================================*/
557 static const struct {
558     const char  *m; /* menomic     */
559     const size_t o; /* operands    */
560     const size_t l; /* menomic len */
561 } asm_instr[] = {
562     { "DONE"      , 1, 4 },
563     { "MUL_F"     , 3, 5 },
564     { "MUL_V"     , 3, 5 },
565     { "MUL_FV"    , 3, 6 },
566     { "MUL_VF"    , 3, 6 },
567     { "DIV"       , 0, 3 },
568     { "ADD_F"     , 3, 5 },
569     { "ADD_V"     , 3, 5 },
570     { "SUB_F"     , 3, 5 },
571     { "DUB_V"     , 3, 5 },
572     { "EQ_F"      , 0, 4 },
573     { "EQ_V"      , 0, 4 },
574     { "EQ_S"      , 0, 4 },
575     { "EQ_E"      , 0, 4 },
576     { "ES_FNC"    , 0, 6 },
577     { "NE_F"      , 0, 4 },
578     { "NE_V"      , 0, 4 },
579     { "NE_S"      , 0, 4 },
580     { "NE_E"      , 0, 4 },
581     { "NE_FNC"    , 0, 6 },
582     { "LE"        , 0, 2 },
583     { "GE"        , 0, 2 },
584     { "LT"        , 0, 2 },
585     { "GT"        , 0, 2 },
586     { "FIELD_F"   , 0, 7 },
587     { "FIELD_V"   , 0, 7 },
588     { "FIELD_S"   , 0, 7 },
589     { "FIELD_ENT" , 0, 9 },
590     { "FIELD_FLD" , 0, 9 },
591     { "FIELD_FNC" , 0, 9 },
592     { "ADDRESS"   , 0, 7 },
593     { "STORE_F"   , 0, 7 },
594     { "STORE_V"   , 0, 7 },
595     { "STORE_S"   , 0, 7 },
596     { "STORE_ENT" , 0, 9 },
597     { "STORE_FLD" , 0, 9 },
598     { "STORE_FNC" , 0, 9 },
599     { "STOREP_F"  , 0, 8 },
600     { "STOREP_V"  , 0, 8 },
601     { "STOREP_S"  , 0, 8 },
602     { "STOREP_ENT", 0, 10},
603     { "STOREP_FLD", 0, 10},
604     { "STOREP_FNC", 0, 10},
605     { "RETURN"    , 0, 6 },
606     { "NOT_F"     , 0, 5 },
607     { "NOT_V"     , 0, 5 },
608     { "NOT_S"     , 0, 5 },
609     { "NOT_ENT"   , 0, 7 },
610     { "NOT_FNC"   , 0, 7 },
611     { "IF"        , 0, 2 },
612     { "IFNOT"     , 0, 5 },
613     { "CALL0"     , 1, 5 },
614     { "CALL1"     , 2, 5 },
615     { "CALL2"     , 3, 5 },
616     { "CALL3"     , 4, 5 },
617     { "CALL4"     , 5, 5 },
618     { "CALL5"     , 6, 5 },
619     { "CALL6"     , 7, 5 },
620     { "CALL7"     , 8, 5 },
621     { "CALL8"     , 9, 5 },
622     { "STATE"     , 0, 5 },
623     { "GOTO"      , 0, 4 },
624     { "AND"       , 0, 3 },
625     { "OR"        , 0, 2 },
626     { "BITAND"    , 0, 6 },
627     { "BITOR"     , 0, 5 },
628     { "END"       , 0, 3 } /* virtual assembler instruction */
629 };
630
631 void asm_init (const char *, FILE **);
632 void asm_close(FILE *);
633 void asm_parse(FILE *);
634 /*===================================================================*/
635 /*============================= main.c ==============================*/
636 /*===================================================================*/
637 enum {
638     COMPILER_QCC,     /* circa  QuakeC */
639     COMPILER_FTEQCC,  /* fteqcc QuakeC */
640     COMPILER_QCCX,    /* qccx   QuakeC */
641     COMPILER_GMQCC    /* this   QuakeC */
642 };
643 extern bool opts_debug;
644 extern bool opts_memchk;
645 extern bool opts_darkplaces_stringtablebug;
646 extern bool opts_omit_nullcode;
647 extern int  opts_compiler;
648 /*===================================================================*/
649 /*============================= ast.c ===============================*/
650 /*===================================================================*/
651 #define MEM_VECTOR_PROTO(Towner, Tmem, mem)                   \
652     bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem);      \
653     bool GMQCC_WARN Towner##_##mem##_remove(Towner*, size_t)
654
655 #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem)                    \
656     MEM_VECTOR_PROTO(Towner, Tmem, mem);                           \
657     bool GMQCC_WARN Towner##_##mem##_find(Towner*, Tmem, size_t*); \
658     void Towner##_##mem##_clear(Towner*)
659
660 #define MEM_VECTOR_MAKE(Twhat, name) \
661     Twhat  *name;                    \
662     size_t name##_count;             \
663     size_t name##_alloc
664
665 #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem)                          \
666 bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f)            \
667 {                                                                    \
668     Twhat *reall;                                                    \
669     if (self->mem##_count == self->mem##_alloc) {                    \
670         if (!self->mem##_alloc) {                                    \
671             self->mem##_alloc = 16;                                  \
672         } else {                                                     \
673             self->mem##_alloc *= 2;                                  \
674         }                                                            \
675         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc);    \
676         if (!reall) {                                                \
677             return false;                                            \
678         }                                                            \
679         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
680         mem_d(self->mem);                                            \
681         self->mem = reall;                                           \
682     }                                                                \
683     self->mem[self->mem##_count++] = f;                              \
684     return true;                                                     \
685 }
686
687 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)                       \
688 bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx)      \
689 {                                                                    \
690     size_t i;                                                        \
691     Twhat *reall;                                                    \
692     if (idx >= self->mem##_count) {                                  \
693         return true; /* huh... */                                    \
694     }                                                                \
695     for (i = idx; i < self->mem##_count-1; ++i) {                    \
696         self->mem[i] = self->mem[i+1];                               \
697     }                                                                \
698     self->mem##_count--;                                             \
699     if (self->mem##_count < self->mem##_count/2) {                   \
700         self->mem##_alloc /= 2;                                      \
701         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count);    \
702         if (!reall) {                                                \
703             return false;                                            \
704         }                                                            \
705         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
706         mem_d(self->mem);                                            \
707         self->mem = reall;                                           \
708     }                                                                \
709     return true;                                                     \
710 }
711
712 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem)                    \
713 bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
714 {                                                               \
715     size_t i;                                                   \
716     for (i = 0; i < self->mem##_count; ++i) {                   \
717         if (self->mem[i] == obj) {                              \
718             if (idx) {                                          \
719                 *idx = i;                                       \
720             }                                                   \
721             return true;                                        \
722         }                                                       \
723     }                                                           \
724     return false;                                               \
725 }
726
727 #define _MEM_VEC_FUN_CLEAR(Tself, mem)  \
728 void Tself##_##mem##_clear(Tself *self) \
729 {                                       \
730     if (!self->mem)                     \
731         return;                         \
732     mem_d((void*) self->mem);           \
733     self->mem = NULL;                   \
734     self->mem##_count = 0;              \
735     self->mem##_alloc = 0;              \
736 }
737
738 #define MEM_VECTOR_CLEAR(owner, mem)  \
739     if ((owner)->mem)                 \
740         mem_d((void*)((owner)->mem)); \
741     (owner)->mem = NULL;              \
742     (owner)->mem##_count = 0;         \
743     (owner)->mem##_alloc = 0
744
745 #define MEM_VECTOR_INIT(owner, mem) \
746 {                                   \
747     (owner)->mem = NULL;            \
748     (owner)->mem##_count = 0;       \
749     (owner)->mem##_alloc = 0;       \
750 }
751
752 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
753 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
754 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
755
756 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
757 MEM_VEC_FUNCTIONS(Tself, Twhat, mem)             \
758 _MEM_VEC_FUN_CLEAR(Tself, mem)                   \
759 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
760
761 enum store_types {
762     store_global,
763     store_local,  /* local, assignable for now, should get promoted later */
764     store_value   /* unassignable */
765 };
766
767 typedef struct {
768     float x, y, z;
769 } vector;
770
771 /*
772  * A shallow copy of a lex_file to remember where which ast node
773  * came from.
774  */
775 typedef struct {
776     const char *file;
777     size_t      line;
778 } lex_ctx;
779 #endif