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