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