]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Removed the qc_type enum and replaced all qc_* by TYPE_*
[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 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 struct lex_file {
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 };
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  (struct lex_file *);
168 void             lex_reset  (struct lex_file *);
169 void             lex_close  (struct lex_file *);
170 struct lex_file *lex_include(struct lex_file *, char *);
171 struct lex_file *lex_open   (FILE *);
172
173 //===================================================================
174 //========================== error.c ================================
175 //===================================================================
176 #define ERROR_LEX      (SHRT_MAX+0)
177 #define ERROR_PARSE    (SHRT_MAX+1)
178 #define ERROR_INTERNAL (SHRT_MAX+2)
179 #define ERROR_COMPILER (SHRT_MAX+3)
180 #define ERROR_PREPRO   (SHRT_MAX+4)
181 int error(struct lex_file *, int, const char *, ...);
182
183 //===================================================================
184 //========================== parse.c ================================
185 //===================================================================
186 int parse_gen(struct lex_file *);
187
188 //===================================================================
189 //========================== typedef.c ==============================
190 //===================================================================
191 typedef struct typedef_node_t {
192     char      *name;
193 } typedef_node;
194
195 void          typedef_init();
196 void          typedef_clear();
197 typedef_node *typedef_find(const char *);
198 int           typedef_add (struct lex_file *file, const char *, const char *);
199
200
201 //===================================================================
202 //=========================== util.c ================================
203 //===================================================================
204 void *util_memory_a      (unsigned int, unsigned int, const char *);
205 void  util_memory_d      (void       *, unsigned int, const char *);
206 void  util_meminfo       ();
207
208 bool  util_strupper      (const char *);
209 bool  util_strdigit      (const char *);
210 char *util_strdup        (const char *);
211 char *util_strrq         (char *);
212 char *util_strrnl        (char *);
213 char *util_strsws        (const char *);
214 char *util_strchp        (const char *, const char *);
215 void  util_debug         (const char *, const char *, ...);
216 int   util_getline       (char **, size_t *, FILE *);
217 void  util_endianswap    (void *,  int, int);
218
219 uint32_t util_crc32(const char *, int, register const short); 
220
221 #ifdef NOTRACK
222 #    define mem_a(x) malloc(x)
223 #    define mem_d(x) free  (x)
224 #else
225 #    define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
226 #    define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
227 #endif
228
229 /* Builds vector type (usefull for inside structures) */
230 #define VECTOR_TYPE(T,N)                                        \
231     T*     N##_data      = NULL;                                \
232     long   N##_elements  = 0;                                   \
233     long   N##_allocated = 0
234 /* Builds vector add */
235 #define VECTOR_CORE(T,N)                                        \
236     int    N##_add(T element) {                                 \
237         if (N##_elements == N##_allocated) {                    \
238             if (N##_allocated == 0) {                           \
239                 N##_allocated = 12;                             \
240             } else {                                            \
241                 N##_allocated *= 2;                             \
242             }                                                   \
243             void *temp = mem_a(N##_allocated * sizeof(T));      \
244             if  (!temp) {                                       \
245                 mem_d(temp);                                    \
246                 return -1;                                      \
247             }                                                   \
248             memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
249             mem_d(N##_data);                                    \
250             N##_data = (T*)temp;                                \
251         }                                                       \
252         N##_data[N##_elements] = element;                       \
253         return   N##_elements++;                                \
254     }                                                           \
255     int N##_put(T* elements, size_t len) {                      \
256         len     --;                                             \
257         elements--;                                             \
258         while (N##_add(*++elements) != -1 && len--);            \
259         return N##_elements;                                    \
260     }
261 /* Builds a full vector inspot */
262 #define VECTOR_MAKE(T,N) \
263     VECTOR_TYPE(T,N);    \
264     VECTOR_CORE(T,N)
265 /* Builds a vector add function pointer for inside structures */
266 #define VECTOR_IMPL(T,N) int (*N##_add)(T)
267
268 //===================================================================
269 //=========================== code.c ================================
270 //===================================================================
271 enum {
272     TYPE_VOID     ,
273     TYPE_STRING   ,
274     TYPE_FLOAT    ,
275     TYPE_VECTOR   ,
276     TYPE_ENTITY   ,
277     TYPE_FIELD    ,
278     TYPE_FUNCTION ,
279     TYPE_POINTER  ,
280     /* TYPE_INTEGER  , */
281     TYPE_VARIANT  ,
282 };
283
284 /*
285  * Each paramater incerements by 3 since vector types hold
286  * 3 components (x,y,z).
287  */
288 #define OFS_NULL      0
289 #define OFS_RETURN    1
290 #define OFS_PARM0     (OFS_RETURN+3)
291 #define OFS_PARM1     (OFS_PARM0 +3)
292 #define OFS_PARM2     (OFS_PARM1 +3)
293 #define OFS_PARM3     (OFS_PARM2 +3)
294 #define OFS_PARM4     (OFS_PARM3 +3)
295 #define OFS_PARM5     (OFS_PARM4 +3)
296 #define OFS_PARM6     (OFS_PARM5 +3)
297 #define OFS_PARM7     (OFS_PARM6 +3)
298
299 typedef struct {
300     uint16_t opcode;
301     
302     /* operand 1 */
303     union {
304         int16_t  s1; /* signed   */
305         uint16_t u1; /* unsigned */
306     };
307     /* operand 2 */
308     union {
309         int16_t  s2; /* signed   */
310         uint16_t u2; /* unsigned */
311     };
312     /* operand 3 */
313     union {
314         int16_t  s3; /* signed   */
315         uint16_t u3; /* unsigned */
316     };
317     
318     /*
319      * This is the same as the structure in darkplaces
320      * {
321      *     unsigned short op;
322      *     short          a,b,c;
323      * }
324      * But this one is more sane to work with, and the
325      * type sizes are guranteed.
326      */
327 } prog_section_statement;
328
329 typedef struct {
330     /* The types:
331      * 0 = ev_void
332      * 1 = ev_string
333      * 2 = ev_float
334      * 3 = ev_vector
335      * 4 = ev_entity
336      * 5 = ev_field
337      * 6 = ev_function
338      * 7 = ev_pointer -- engine only
339      * 8 = ev_bad     -- engine only
340      */
341     uint16_t type;
342     uint16_t offset;
343     uint32_t name;
344 } prog_section_both;
345 typedef prog_section_both prog_section_def;
346 typedef prog_section_both prog_section_field;
347
348 typedef struct {
349     int32_t   entry;      /* in statement table for instructions  */
350     uint32_t  firstlocal; /* First local in local table           */
351     uint32_t  locals;     /* Total ints of params + locals        */
352     uint32_t  profile;    /* Always zero (engine uses this)       */
353     uint32_t  name;       /* name of function in string table     */
354     uint32_t  file;       /* file of the source file              */
355     uint32_t  nargs;      /* number of arguments                  */
356     uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
357 } prog_section_function;
358
359 /* 
360  * Instructions 
361  * These are the external instructions supported by the interperter
362  * this is what things compile to (from the C code).
363  */
364 enum {
365     INSTR_DONE,
366     INSTR_MUL_F,
367     INSTR_MUL_V,
368     INSTR_MUL_FV,
369     INSTR_MUL_VF,
370     INSTR_DIV_F,
371     INSTR_ADD_F,
372     INSTR_ADD_V,
373     INSTR_SUB_F,
374     INSTR_SUB_V,
375     INSTR_EQ_F,
376     INSTR_EQ_V,
377     INSTR_EQ_S,
378     INSTR_EQ_E,
379     INSTR_EQ_FNC,
380     INSTR_NE_F,
381     INSTR_NE_V,
382     INSTR_NE_S,
383     INSTR_NE_E,
384     INSTR_NE_FNC,
385     INSTR_LE,
386     INSTR_GE,
387     INSTR_LT,
388     INSTR_GT,
389     INSTR_LOAD_F,
390     INSTR_LOAD_V,
391     INSTR_LOAD_S,
392     INSTR_LOAD_ENT,
393     INSTR_LOAD_FLD,
394     INSTR_LOAD_FNC,
395     INSTR_ADDRESS,
396     INSTR_STORE_F,
397     INSTR_STORE_V,
398     INSTR_STORE_S,
399     INSTR_STORE_ENT,
400     INSTR_STORE_FLD,
401     INSTR_STORE_FNC,
402     INSTR_STOREP_F,
403     INSTR_STOREP_V,
404     INSTR_STOREP_S,
405     INSTR_STOREP_ENT,
406     INSTR_STOREP_FLD,
407     INSTR_STOREP_FNC,
408     INSTR_RETURN,
409     INSTR_NOT_F,
410     INSTR_NOT_V,
411     INSTR_NOT_S,
412     INSTR_NOT_ENT,
413     INSTR_NOT_FNC,
414     INSTR_IF,
415     INSTR_IFNOT,
416     INSTR_CALL0,
417     INSTR_CALL1,
418     INSTR_CALL2,
419     INSTR_CALL3,
420     INSTR_CALL4,
421     INSTR_CALL5,
422     INSTR_CALL6,
423     INSTR_CALL7,
424     INSTR_CALL8,
425     INSTR_STATE,
426     INSTR_GOTO,
427     INSTR_AND,
428     INSTR_OR,
429     INSTR_BITAND,
430     INSTR_BITOR,
431
432     /* Virtual instructions used by the IR
433      * Keep at the end!
434      */
435     VINSTR_PHI,
436     VINSTR_JUMP,
437     VINSTR_COND,
438 };
439
440 /*
441  * The symbols below are created by the following
442  * expanded macros:
443  * 
444  * VECTOR_MAKE(prog_section_statement, code_statements);
445  * VECTOR_MAKE(prog_section_def,       code_defs      );
446  * VECTOR_MAKE(prog_section_field,     code_fields    );
447  * VECTOR_MAKE(prog_section_function,  code_functions );
448  * VECTOR_MAKE(int,                    code_globals   );
449  * VECTOR_MAKE(char,                   code_chars     );
450  */
451 int         code_statements_add(prog_section_statement);
452 int         code_defs_add      (prog_section_def);
453 int         code_fields_add    (prog_section_field);
454 int         code_functions_add (prog_section_function);
455 int         code_globals_add   (int);
456 int         code_chars_add     (char);
457 int         code_statements_put(prog_section_statement*, size_t);
458 int         code_defs_put      (prog_section_def*,       size_t);
459 int         code_fields_put    (prog_section_field*,     size_t);
460 int         code_functions_put (prog_section_function*,  size_t);
461 int         code_globals_put   (int*,                    size_t);
462 int         code_chars_put     (char*,                   size_t);
463 extern long code_statements_elements;
464 extern long code_chars_elements;
465 extern long code_globals_elements;
466 extern long code_functions_elements;
467 extern long code_fields_elements;
468 extern long code_defs_elements;
469
470 /*
471  * code_write -- writes out the compiled file
472  * code_init  -- prepares the code file
473  */
474 void code_write ();
475 void code_init  ();
476
477 //===================================================================
478 //========================= assembler.c =============================
479 //===================================================================
480 static const struct {
481     const char  *m; /* menomic     */
482     const size_t o; /* operands    */ 
483     const size_t l; /* menomic len */
484 } const asm_instr[] = {
485     [INSTR_DONE]       = { "DONE"      , 1, 4 },
486     [INSTR_MUL_F]      = { "MUL_F"     , 3, 5 },
487     [INSTR_MUL_V]      = { "MUL_V"     , 3, 5 },
488     [INSTR_MUL_FV]     = { "MUL_FV"    , 3, 6 },
489     [INSTR_MUL_VF]     = { "MUL_VF"    , 3, 6 },
490     [INSTR_DIV_F]      = { "DIV"       , 0, 3 },
491     [INSTR_ADD_F]      = { "ADD_F"     , 3, 5 },
492     [INSTR_ADD_V]      = { "ADD_V"     , 3, 5 },
493     [INSTR_SUB_F]      = { "SUB_F"     , 3, 5 },
494     [INSTR_SUB_V]      = { "DUB_V"     , 3, 5 },
495     [INSTR_EQ_F]       = { "EQ_F"      , 0, 4 },
496     [INSTR_EQ_V]       = { "EQ_V"      , 0, 4 },
497     [INSTR_EQ_S]       = { "EQ_S"      , 0, 4 },
498     [INSTR_EQ_E]       = { "EQ_E"      , 0, 4 },
499     [INSTR_EQ_FNC]     = { "ES_FNC"    , 0, 6 },
500     [INSTR_NE_F]       = { "NE_F"      , 0, 4 },
501     [INSTR_NE_V]       = { "NE_V"      , 0, 4 },
502     [INSTR_NE_S]       = { "NE_S"      , 0, 4 },
503     [INSTR_NE_E]       = { "NE_E"      , 0, 4 },
504     [INSTR_NE_FNC]     = { "NE_FNC"    , 0, 6 },
505     [INSTR_LE]         = { "LE"        , 0, 2 },
506     [INSTR_GE]         = { "GE"        , 0, 2 },
507     [INSTR_LT]         = { "LT"        , 0, 2 },
508     [INSTR_GT]         = { "GT"        , 0, 2 },
509     [INSTR_LOAD_F]     = { "FIELD_F"   , 0, 7 },
510     [INSTR_LOAD_V]     = { "FIELD_V"   , 0, 7 },
511     [INSTR_LOAD_S]     = { "FIELD_S"   , 0, 7 },
512     [INSTR_LOAD_ENT]   = { "FIELD_ENT" , 0, 9 },
513     [INSTR_LOAD_FLD]   = { "FIELD_FLD" , 0, 9 },
514     [INSTR_LOAD_FNC]   = { "FIELD_FNC" , 0, 9 },
515     [INSTR_ADDRESS]    = { "ADDRESS"   , 0, 7 },
516     [INSTR_STORE_F]    = { "STORE_F"   , 0, 7 },
517     [INSTR_STORE_V]    = { "STORE_V"   , 0, 7 },
518     [INSTR_STORE_S]    = { "STORE_S"   , 0, 7 },
519     [INSTR_STORE_ENT]  = { "STORE_ENT" , 0, 9 },
520     [INSTR_STORE_FLD]  = { "STORE_FLD" , 0, 9 },
521     [INSTR_STORE_FNC]  = { "STORE_FNC" , 0, 9 },
522     [INSTR_STOREP_F]   = { "STOREP_F"  , 0, 8 },
523     [INSTR_STOREP_V]   = { "STOREP_V"  , 0, 8 },
524     [INSTR_STOREP_S]   = { "STOREP_S"  , 0, 8 },
525     [INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
526     [INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
527     [INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
528     [INSTR_RETURN]     = { "RETURN"    , 0, 6 },
529     [INSTR_NOT_F]      = { "NOT_F"     , 0, 5 },
530     [INSTR_NOT_V]      = { "NOT_V"     , 0, 5 },
531     [INSTR_NOT_S]      = { "NOT_S"     , 0, 5 },
532     [INSTR_NOT_ENT]    = { "NOT_ENT"   , 0, 7 },
533     [INSTR_NOT_FNC]    = { "NOT_FNC"   , 0, 7 },
534     [INSTR_IF]         = { "IF"        , 0, 2 },
535     [INSTR_IFNOT]      = { "IFNOT"     , 0, 5 },
536     [INSTR_CALL0]      = { "CALL0"     , 0, 5 },
537     [INSTR_CALL1]      = { "CALL1"     , 0, 5 },
538     [INSTR_CALL2]      = { "CALL2"     , 0, 5 },
539     [INSTR_CALL3]      = { "CALL3"     , 0, 5 },
540     [INSTR_CALL4]      = { "CALL4"     , 0, 5 },
541     [INSTR_CALL5]      = { "CALL5"     , 0, 5 },
542     [INSTR_CALL6]      = { "CALL6"     , 0, 5 },
543     [INSTR_CALL7]      = { "CALL7"     , 0, 5 },
544     [INSTR_CALL8]      = { "CALL8"     , 0, 5 },
545     [INSTR_STATE]      = { "STATE"     , 0, 5 },
546     [INSTR_GOTO]       = { "GOTO"      , 0, 4 },
547     [INSTR_AND]        = { "AND"       , 0, 3 },
548     [INSTR_OR]         = { "OR"        , 0, 2 },
549     [INSTR_BITAND]     = { "BITAND"    , 0, 6 },
550     [INSTR_BITOR]      = { "BITOR"     , 0, 5 }
551 };
552
553 void asm_init (const char *, FILE **);
554 void asm_close(FILE *);
555 void asm_parse(FILE *);
556 //======================================================================
557 //============================= main.c =================================
558 //======================================================================
559 enum {
560     COMPILER_QCC,     /* circa  QuakeC */
561     COMPILER_FTEQCC,  /* fteqcc QuakeC */
562     COMPILER_QCCX,    /* qccx   QuakeC */
563     COMPILER_GMQCC    /* this   QuakeC */
564 };
565 extern bool opts_debug;
566 extern bool opts_memchk;
567 extern bool opts_darkplaces_stringtablebug;
568 extern bool opts_omit_nullcode;
569 extern int  opts_compiler;
570 #endif