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