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