]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Remove trailing whitespace from everything
[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
275 //===================================================================
276 //=========================== code.c ================================
277 //===================================================================
278 enum {
279     TYPE_VOID     ,
280     TYPE_STRING   ,
281     TYPE_FLOAT    ,
282     TYPE_VECTOR   ,
283     TYPE_ENTITY   ,
284     TYPE_FIELD    ,
285     TYPE_FUNCTION ,
286     TYPE_POINTER  ,
287     /* TYPE_INTEGER  , */
288     TYPE_VARIANT  ,
289 };
290
291 /*
292  * Each paramater incerements by 3 since vector types hold
293  * 3 components (x,y,z).
294  */
295 #define OFS_NULL      0
296 #define OFS_RETURN    1
297 #define OFS_PARM0     (OFS_RETURN+3)
298 #define OFS_PARM1     (OFS_PARM0 +3)
299 #define OFS_PARM2     (OFS_PARM1 +3)
300 #define OFS_PARM3     (OFS_PARM2 +3)
301 #define OFS_PARM4     (OFS_PARM3 +3)
302 #define OFS_PARM5     (OFS_PARM4 +3)
303 #define OFS_PARM6     (OFS_PARM5 +3)
304 #define OFS_PARM7     (OFS_PARM6 +3)
305
306 typedef struct {
307     uint16_t opcode;
308
309     /* operand 1 */
310     union {
311         int16_t  s1; /* signed   */
312         uint16_t u1; /* unsigned */
313     } o1;
314     /* operand 2 */
315     union {
316         int16_t  s2; /* signed   */
317         uint16_t u2; /* unsigned */
318     } o2;
319     /* operand 3 */
320     union {
321         int16_t  s3; /* signed   */
322         uint16_t u3; /* unsigned */
323     } o3;
324
325     /*
326      * This is the same as the structure in darkplaces
327      * {
328      *     unsigned short op;
329      *     short          a,b,c;
330      * }
331      * But this one is more sane to work with, and the
332      * type sizes are guranteed.
333      */
334 } prog_section_statement;
335
336 typedef struct {
337     /* The types:
338      * 0 = ev_void
339      * 1 = ev_string
340      * 2 = ev_float
341      * 3 = ev_vector
342      * 4 = ev_entity
343      * 5 = ev_field
344      * 6 = ev_function
345      * 7 = ev_pointer -- engine only
346      * 8 = ev_bad     -- engine only
347      */
348     uint16_t type;
349     uint16_t offset;
350     uint32_t name;
351 } prog_section_both;
352 typedef prog_section_both prog_section_def;
353 typedef prog_section_both prog_section_field;
354
355 typedef struct {
356     int32_t   entry;      /* in statement table for instructions  */
357     uint32_t  firstlocal; /* First local in local table           */
358     uint32_t  locals;     /* Total ints of params + locals        */
359     uint32_t  profile;    /* Always zero (engine uses this)       */
360     uint32_t  name;       /* name of function in string table     */
361     uint32_t  file;       /* file of the source file              */
362     uint32_t  nargs;      /* number of arguments                  */
363     uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
364 } prog_section_function;
365
366 /*
367  * Instructions
368  * These are the external instructions supported by the interperter
369  * this is what things compile to (from the C code).
370  */
371 enum {
372     INSTR_DONE,
373     INSTR_MUL_F,
374     INSTR_MUL_V,
375     INSTR_MUL_FV,
376     INSTR_MUL_VF,
377     INSTR_DIV_F,
378     INSTR_ADD_F,
379     INSTR_ADD_V,
380     INSTR_SUB_F,
381     INSTR_SUB_V,
382     INSTR_EQ_F,
383     INSTR_EQ_V,
384     INSTR_EQ_S,
385     INSTR_EQ_E,
386     INSTR_EQ_FNC,
387     INSTR_NE_F,
388     INSTR_NE_V,
389     INSTR_NE_S,
390     INSTR_NE_E,
391     INSTR_NE_FNC,
392     INSTR_LE,
393     INSTR_GE,
394     INSTR_LT,
395     INSTR_GT,
396     INSTR_LOAD_F,
397     INSTR_LOAD_V,
398     INSTR_LOAD_S,
399     INSTR_LOAD_ENT,
400     INSTR_LOAD_FLD,
401     INSTR_LOAD_FNC,
402     INSTR_ADDRESS,
403     INSTR_STORE_F,
404     INSTR_STORE_V,
405     INSTR_STORE_S,
406     INSTR_STORE_ENT,
407     INSTR_STORE_FLD,
408     INSTR_STORE_FNC,
409     INSTR_STOREP_F,
410     INSTR_STOREP_V,
411     INSTR_STOREP_S,
412     INSTR_STOREP_ENT,
413     INSTR_STOREP_FLD,
414     INSTR_STOREP_FNC,
415     INSTR_RETURN,
416     INSTR_NOT_F,
417     INSTR_NOT_V,
418     INSTR_NOT_S,
419     INSTR_NOT_ENT,
420     INSTR_NOT_FNC,
421     INSTR_IF,
422     INSTR_IFNOT,
423     INSTR_CALL0,
424     INSTR_CALL1,
425     INSTR_CALL2,
426     INSTR_CALL3,
427     INSTR_CALL4,
428     INSTR_CALL5,
429     INSTR_CALL6,
430     INSTR_CALL7,
431     INSTR_CALL8,
432     INSTR_STATE,
433     INSTR_GOTO,
434     INSTR_AND,
435     INSTR_OR,
436     INSTR_BITAND,
437     INSTR_BITOR,
438
439     /* Virtual instructions used by the IR
440      * Keep at the end!
441      */
442     VINSTR_PHI,
443     VINSTR_JUMP,
444     VINSTR_COND,
445 };
446
447 /*
448  * The symbols below are created by the following
449  * expanded macros:
450  *
451  * VECTOR_MAKE(prog_section_statement, code_statements);
452  * VECTOR_MAKE(prog_section_def,       code_defs      );
453  * VECTOR_MAKE(prog_section_field,     code_fields    );
454  * VECTOR_MAKE(prog_section_function,  code_functions );
455  * VECTOR_MAKE(int,                    code_globals   );
456  * VECTOR_MAKE(char,                   code_chars     );
457  */
458 int         code_statements_add(prog_section_statement);
459 int         code_defs_add      (prog_section_def);
460 int         code_fields_add    (prog_section_field);
461 int         code_functions_add (prog_section_function);
462 int         code_globals_add   (int);
463 int         code_chars_add     (char);
464 int         code_statements_put(prog_section_statement*, size_t);
465 int         code_defs_put      (prog_section_def*,       size_t);
466 int         code_fields_put    (prog_section_field*,     size_t);
467 int         code_functions_put (prog_section_function*,  size_t);
468 int         code_globals_put   (int*,                    size_t);
469 int         code_chars_put     (char*,                   size_t);
470 extern long code_statements_elements;
471 extern long code_chars_elements;
472 extern long code_globals_elements;
473 extern long code_functions_elements;
474 extern long code_fields_elements;
475 extern long code_defs_elements;
476
477 /*
478  * code_write -- writes out the compiled file
479  * code_init  -- prepares the code file
480  */
481 void code_write ();
482 void code_init  ();
483
484 //===================================================================
485 //========================= assembler.c =============================
486 //===================================================================
487 static const struct {
488     const char  *m; /* menomic     */
489     const size_t o; /* operands    */
490     const size_t l; /* menomic len */
491 } const asm_instr[] = {
492     [INSTR_DONE]       = { "DONE"      , 1, 4 },
493     [INSTR_MUL_F]      = { "MUL_F"     , 3, 5 },
494     [INSTR_MUL_V]      = { "MUL_V"     , 3, 5 },
495     [INSTR_MUL_FV]     = { "MUL_FV"    , 3, 6 },
496     [INSTR_MUL_VF]     = { "MUL_VF"    , 3, 6 },
497     [INSTR_DIV_F]      = { "DIV"       , 0, 3 },
498     [INSTR_ADD_F]      = { "ADD_F"     , 3, 5 },
499     [INSTR_ADD_V]      = { "ADD_V"     , 3, 5 },
500     [INSTR_SUB_F]      = { "SUB_F"     , 3, 5 },
501     [INSTR_SUB_V]      = { "DUB_V"     , 3, 5 },
502     [INSTR_EQ_F]       = { "EQ_F"      , 0, 4 },
503     [INSTR_EQ_V]       = { "EQ_V"      , 0, 4 },
504     [INSTR_EQ_S]       = { "EQ_S"      , 0, 4 },
505     [INSTR_EQ_E]       = { "EQ_E"      , 0, 4 },
506     [INSTR_EQ_FNC]     = { "ES_FNC"    , 0, 6 },
507     [INSTR_NE_F]       = { "NE_F"      , 0, 4 },
508     [INSTR_NE_V]       = { "NE_V"      , 0, 4 },
509     [INSTR_NE_S]       = { "NE_S"      , 0, 4 },
510     [INSTR_NE_E]       = { "NE_E"      , 0, 4 },
511     [INSTR_NE_FNC]     = { "NE_FNC"    , 0, 6 },
512     [INSTR_LE]         = { "LE"        , 0, 2 },
513     [INSTR_GE]         = { "GE"        , 0, 2 },
514     [INSTR_LT]         = { "LT"        , 0, 2 },
515     [INSTR_GT]         = { "GT"        , 0, 2 },
516     [INSTR_LOAD_F]     = { "FIELD_F"   , 0, 7 },
517     [INSTR_LOAD_V]     = { "FIELD_V"   , 0, 7 },
518     [INSTR_LOAD_S]     = { "FIELD_S"   , 0, 7 },
519     [INSTR_LOAD_ENT]   = { "FIELD_ENT" , 0, 9 },
520     [INSTR_LOAD_FLD]   = { "FIELD_FLD" , 0, 9 },
521     [INSTR_LOAD_FNC]   = { "FIELD_FNC" , 0, 9 },
522     [INSTR_ADDRESS]    = { "ADDRESS"   , 0, 7 },
523     [INSTR_STORE_F]    = { "STORE_F"   , 0, 7 },
524     [INSTR_STORE_V]    = { "STORE_V"   , 0, 7 },
525     [INSTR_STORE_S]    = { "STORE_S"   , 0, 7 },
526     [INSTR_STORE_ENT]  = { "STORE_ENT" , 0, 9 },
527     [INSTR_STORE_FLD]  = { "STORE_FLD" , 0, 9 },
528     [INSTR_STORE_FNC]  = { "STORE_FNC" , 0, 9 },
529     [INSTR_STOREP_F]   = { "STOREP_F"  , 0, 8 },
530     [INSTR_STOREP_V]   = { "STOREP_V"  , 0, 8 },
531     [INSTR_STOREP_S]   = { "STOREP_S"  , 0, 8 },
532     [INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
533     [INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
534     [INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
535     [INSTR_RETURN]     = { "RETURN"    , 0, 6 },
536     [INSTR_NOT_F]      = { "NOT_F"     , 0, 5 },
537     [INSTR_NOT_V]      = { "NOT_V"     , 0, 5 },
538     [INSTR_NOT_S]      = { "NOT_S"     , 0, 5 },
539     [INSTR_NOT_ENT]    = { "NOT_ENT"   , 0, 7 },
540     [INSTR_NOT_FNC]    = { "NOT_FNC"   , 0, 7 },
541     [INSTR_IF]         = { "IF"        , 0, 2 },
542     [INSTR_IFNOT]      = { "IFNOT"     , 0, 5 },
543     [INSTR_CALL0]      = { "CALL0"     , 0, 5 },
544     [INSTR_CALL1]      = { "CALL1"     , 0, 5 },
545     [INSTR_CALL2]      = { "CALL2"     , 0, 5 },
546     [INSTR_CALL3]      = { "CALL3"     , 0, 5 },
547     [INSTR_CALL4]      = { "CALL4"     , 0, 5 },
548     [INSTR_CALL5]      = { "CALL5"     , 0, 5 },
549     [INSTR_CALL6]      = { "CALL6"     , 0, 5 },
550     [INSTR_CALL7]      = { "CALL7"     , 0, 5 },
551     [INSTR_CALL8]      = { "CALL8"     , 0, 5 },
552     [INSTR_STATE]      = { "STATE"     , 0, 5 },
553     [INSTR_GOTO]       = { "GOTO"      , 0, 4 },
554     [INSTR_AND]        = { "AND"       , 0, 3 },
555     [INSTR_OR]         = { "OR"        , 0, 2 },
556     [INSTR_BITAND]     = { "BITAND"    , 0, 6 },
557     [INSTR_BITOR]      = { "BITOR"     , 0, 5 }
558 };
559
560 void asm_init (const char *, FILE **);
561 void asm_close(FILE *);
562 void asm_parse(FILE *);
563 //======================================================================
564 //============================= main.c =================================
565 //======================================================================
566 enum {
567     COMPILER_QCC,     /* circa  QuakeC */
568     COMPILER_FTEQCC,  /* fteqcc QuakeC */
569     COMPILER_QCCX,    /* qccx   QuakeC */
570     COMPILER_GMQCC    /* this   QuakeC */
571 };
572 extern bool opts_debug;
573 extern bool opts_memchk;
574 extern bool opts_darkplaces_stringtablebug;
575 extern bool opts_omit_nullcode;
576 extern int  opts_compiler;
577 //======================================================================
578 //============================= ast.c ==================================
579 //======================================================================
580 #define MEM_VECTOR_PROTO(Towner, Tmem, mem)                   \
581     bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem);      \
582     bool GMQCC_WARN Towner##_##mem##_remove(Towner*, size_t)
583
584 #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem)                    \
585     MEM_VECTOR_PROTO(Towner, Tmem, mem);                           \
586     bool GMQCC_WARN Towner##_##mem##_find(Towner*, Tmem, size_t*); \
587     void Towner##_##mem##_clear(Towner*)
588
589 #define MEM_VECTOR_MAKE(Twhat, name) \
590     Twhat  *name;                    \
591     size_t name##_count;             \
592     size_t name##_alloc
593
594 #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem)                          \
595 bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f)            \
596 {                                                                    \
597     Twhat *reall;                                                    \
598     if (self->mem##_count == self->mem##_alloc) {                    \
599         if (!self->mem##_alloc) {                                    \
600             self->mem##_alloc = 16;                                  \
601         } else {                                                     \
602             self->mem##_alloc *= 2;                                  \
603         }                                                            \
604         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc);    \
605         if (!reall) {                                                \
606             return false;                                            \
607         }                                                            \
608         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
609         mem_d(self->mem);                                            \
610         self->mem = reall;                                           \
611     }                                                                \
612     self->mem[self->mem##_count++] = f;                              \
613     return true;                                                     \
614 }
615
616 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)                       \
617 bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx)      \
618 {                                                                    \
619     size_t i;                                                        \
620     Twhat *reall;                                                    \
621     if (idx >= self->mem##_count) {                                  \
622         return true; /* huh... */                                    \
623     }                                                                \
624     for (i = idx; i < self->mem##_count-1; ++i) {                    \
625         self->mem[i] = self->mem[i+1];                               \
626     }                                                                \
627     self->mem##_count--;                                             \
628     if (self->mem##_count < self->mem##_count/2) {                   \
629         self->mem##_alloc /= 2;                                      \
630         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count);    \
631         if (!reall) {                                                \
632             return false;                                            \
633         }                                                            \
634         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
635         mem_d(self->mem);                                            \
636         self->mem = reall;                                           \
637     }                                                                \
638     return true;                                                     \
639 }
640
641 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem)                    \
642 bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
643 {                                                               \
644     size_t i;                                                   \
645     for (i = 0; i < self->mem##_count; ++i) {                   \
646         if (self->mem[i] == obj) {                              \
647             if (idx) {                                          \
648                 *idx = i;                                       \
649             }                                                   \
650             return true;                                        \
651         }                                                       \
652     }                                                           \
653     return false;                                               \
654 }
655
656 #define _MEM_VEC_FUN_CLEAR(Tself, mem)  \
657 void Tself##_##mem##_clear(Tself *self) \
658 {                                       \
659     if (!self->mem)                     \
660         return;                         \
661     mem_d((void*) self->mem);           \
662     self->mem = NULL;                   \
663     self->mem##_count = 0;              \
664     self->mem##_alloc = 0;              \
665 }
666
667 #define MEM_VECTOR_CLEAR(owner, mem)  \
668     if ((owner)->mem)                 \
669         mem_d((void*)((owner)->mem)); \
670     (owner)->mem = NULL;              \
671     (owner)->mem##_count = 0;         \
672     (owner)->mem##_alloc = 0
673
674 #define MEM_VECTOR_INIT(owner, mem) \
675 {                                   \
676     (owner)->mem = NULL;            \
677     (owner)->mem##_count = 0;       \
678     (owner)->mem##_alloc = 0;       \
679 }
680
681 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
682 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
683 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
684
685 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
686 MEM_VEC_FUNCTIONS(Tself, Twhat, mem)             \
687 _MEM_VEC_FUN_CLEAR(Tself, mem)                   \
688 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
689
690 enum store_types {
691     store_global,
692     store_local,  /* local, assignable for now, should get promoted later */
693     store_value,  /* unassignable */
694 };
695
696 typedef struct {
697     float x, y, z;
698 } vector;
699
700 /*
701  * A shallow copy of a lex_file to remember where which ast node
702  * came from.
703  */
704 typedef struct {
705     const char *file;
706     size_t      line;
707 } lex_ctx;
708 #endif