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