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