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