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