3 * Dale Weiler, Wolfgang Bumiller
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
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)
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`.
56 # if __STDC_VERSION__ < 199901L && __GNUC__ < 3
59 # endif /* !__cplusplus */
62 * Of some functions which are generated we want to make sure
63 * that the result isn't ignored. To find such function calls,
66 #if defined(__GNUC__) || defined(__CLANG__)
67 # define GMQCC_WARN __attribute__((warn_unused_result))
72 * This is a hack to silent clang regarding empty
75 #define GMQCC_SUPRESS_EMPTY_BODY do { } while (0)
78 * stdint.h and inttypes.h -less subset
79 * for systems that don't have it, which we must
80 * assume is all systems. (int8_t not required)
83 typedef unsigned char uint8_t; /* same as below */
84 #elif SCHAR_MIN == -128
85 typedef unsigned char uint8_t; /* same as above */
87 #if SHRT_MAX == 0x7FFF
88 typedef short int16_t;
89 typedef unsigned short uint16_t;
90 #elif INT_MAX == 0x7FFF
92 typedef unsigned int uint16_t;
94 #if INT_MAX == 0x7FFFFFFF
96 typedef unsigned int uint32_t;
97 #elif LONG_MAX == 0x7FFFFFFF
99 typedef unsigned long uint32_t;
101 #ifdef _LP64 /* long pointer == 64 */
102 typedef unsigned long uintptr_t;
103 typedef long intptr_t;
105 typedef unsigned int uintptr_t;
106 typedef int intptr_t;
108 /* Ensure type sizes are correct: */
109 typedef char uint8_size_is_correct [sizeof(uint8_t) == 1?1:-1];
110 typedef char uint16_size_if_correct [sizeof(uint16_t) == 2?1:-1];
111 typedef char uint32_size_is_correct [sizeof(uint32_t) == 4?1:-1];
112 typedef char int16_size_if_correct [sizeof(int16_t) == 2?1:-1];
113 typedef char int32_size_is_correct [sizeof(int32_t) == 4?1:-1];
114 /* intptr_t / uintptr_t correct size check */
115 typedef char uintptr_size_is_correct[sizeof(intptr_t) == sizeof(int*)?1:-1];
116 typedef char intptr_size_is_correct [sizeof(uintptr_t)== sizeof(int*)?1:-1];
118 //===================================================================
119 //============================ lex.c ================================
120 //===================================================================
121 typedef struct lex_file_t {
122 FILE *file; /* file handler */
123 char *name; /* name of file */
127 int last; /* last token */
128 int current; /* current token */
129 int length; /* bytes left to parse */
130 int size; /* never changes (size of file) */
131 int line; /* what line are we on? */
135 * It's important that this table never exceed 32 keywords, the ascii
136 * table starts at 33 (and we don't want conflicts)
147 TOKEN_FOR , // extension
148 TOKEN_TYPEDEF , // extension
150 // ensure the token types are out of the
151 // bounds of anyothers that may conflict.
160 * Lexer state constants, these are numbers for where exactly in
161 * the lexing the lexer is at. Or where it decided to stop if a lexer
162 * error occurs. These numbers must be > where the ascii-table ends
163 * and > the last type token which is TOKEN_VOID
172 int lex_token (lex_file *);
173 void lex_reset (lex_file *);
174 void lex_close (lex_file *);
175 void lex_parse (lex_file *);
176 lex_file *lex_include(lex_file *, const char *);
177 void lex_init (const char *, lex_file **);
179 //===================================================================
180 //========================== error.c ================================
181 //===================================================================
182 #define ERROR_LEX (SHRT_MAX+0)
183 #define ERROR_PARSE (SHRT_MAX+1)
184 #define ERROR_INTERNAL (SHRT_MAX+2)
185 #define ERROR_COMPILER (SHRT_MAX+3)
186 #define ERROR_PREPRO (SHRT_MAX+4)
187 int error(lex_file *, int, const char *, ...);
189 //===================================================================
190 //========================== parse.c ================================
191 //===================================================================
192 int parse_gen(lex_file *);
194 //===================================================================
195 //========================== typedef.c ==============================
196 //===================================================================
197 typedef struct typedef_node_t {
202 void typedef_clear();
203 typedef_node *typedef_find(const char *);
204 int typedef_add (lex_file *file, const char *, const char *);
207 //===================================================================
208 //=========================== util.c ================================
209 //===================================================================
210 void *util_memory_a (unsigned int, unsigned int, const char *);
211 void util_memory_d (void *, unsigned int, const char *);
212 void util_meminfo ();
214 bool util_strupper (const char *);
215 bool util_strdigit (const char *);
216 char *util_strdup (const char *);
217 char *util_strrq (const char *);
218 char *util_strrnl (const char *);
219 char *util_strsws (const char *);
220 char *util_strchp (const char *, const char *);
221 void util_debug (const char *, const char *, ...);
222 int util_getline (char **, size_t *, FILE *);
223 void util_endianswap (void *, int, int);
225 uint32_t util_crc32(const char *, int, register const short);
228 # define mem_a(x) malloc(x)
229 # define mem_d(x) free (x)
231 # define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
232 # define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
235 /* Builds vector type (usefull for inside structures) */
236 #define VECTOR_SNAP(X,Y) X ## Y
237 #define VECTOR_FILL(X,Y) VECTOR_SNAP(X,Y)
238 #define VECTOR_TYPE(T,N) \
239 T* N##_data = NULL; \
240 long N##_elements = 0; \
241 long N##_allocated = 0
242 /* Builds vector add */
243 #define VECTOR_CORE(T,N) \
244 int N##_add(T element) { \
245 if (N##_elements == N##_allocated) { \
246 if (N##_allocated == 0) { \
247 N##_allocated = 12; \
249 N##_allocated *= 2; \
251 void *temp = mem_a(N##_allocated * sizeof(T)); \
256 memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
258 N##_data = (T*)temp; \
260 N##_data[N##_elements] = element; \
261 return N##_elements++; \
263 int N##_put(T* elements, size_t len) { \
266 while (N##_add(*++elements) != -1 && len--); \
267 return N##_elements; \
269 typedef char VECTOR_FILL(extra_semicolon_,__COUNTER__)
270 /* Builds a full vector inspot */
271 #define VECTOR_MAKE(T,N) \
274 /* Builds a vector add function pointer for inside structures */
275 #define VECTOR_IMPL(T,N) int (*N##_add)(T)
277 //===================================================================
278 //=========================== code.c ================================
279 //===================================================================
294 * Each paramater incerements by 3 since vector types hold
295 * 3 components (x,y,z).
299 #define OFS_PARM0 (OFS_RETURN+3)
300 #define OFS_PARM1 (OFS_PARM0 +3)
301 #define OFS_PARM2 (OFS_PARM1 +3)
302 #define OFS_PARM3 (OFS_PARM2 +3)
303 #define OFS_PARM4 (OFS_PARM3 +3)
304 #define OFS_PARM5 (OFS_PARM4 +3)
305 #define OFS_PARM6 (OFS_PARM5 +3)
306 #define OFS_PARM7 (OFS_PARM6 +3)
313 int16_t s1; /* signed */
314 uint16_t u1; /* unsigned */
318 int16_t s2; /* signed */
319 uint16_t u2; /* unsigned */
323 int16_t s3; /* signed */
324 uint16_t u3; /* unsigned */
328 * This is the same as the structure in darkplaces
333 * But this one is more sane to work with, and the
334 * type sizes are guranteed.
336 } prog_section_statement;
347 * 7 = ev_pointer -- engine only
348 * 8 = ev_bad -- engine only
354 typedef prog_section_both prog_section_def;
355 typedef prog_section_both prog_section_field;
358 int32_t entry; /* in statement table for instructions */
359 uint32_t firstlocal; /* First local in local table */
360 uint32_t locals; /* Total ints of params + locals */
361 uint32_t profile; /* Always zero (engine uses this) */
362 uint32_t name; /* name of function in string table */
363 uint32_t file; /* file of the source file */
364 uint32_t nargs; /* number of arguments */
365 uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
366 } prog_section_function;
370 * These are the external instructions supported by the interperter
371 * this is what things compile to (from the C code).
441 /* Virtual instructions used by the IR
450 * The symbols below are created by the following
453 * VECTOR_MAKE(prog_section_statement, code_statements);
454 * VECTOR_MAKE(prog_section_def, code_defs );
455 * VECTOR_MAKE(prog_section_field, code_fields );
456 * VECTOR_MAKE(prog_section_function, code_functions );
457 * VECTOR_MAKE(int, code_globals );
458 * VECTOR_MAKE(char, code_chars );
460 int code_statements_add(prog_section_statement);
461 int code_defs_add (prog_section_def);
462 int code_fields_add (prog_section_field);
463 int code_functions_add (prog_section_function);
464 int code_globals_add (int);
465 int code_chars_add (char);
466 int code_statements_put(prog_section_statement*, size_t);
467 int code_defs_put (prog_section_def*, size_t);
468 int code_fields_put (prog_section_field*, size_t);
469 int code_functions_put (prog_section_function*, size_t);
470 int code_globals_put (int*, size_t);
471 int code_chars_put (char*, size_t);
472 extern long code_statements_elements;
473 extern long code_chars_elements;
474 extern long code_globals_elements;
475 extern long code_functions_elements;
476 extern long code_fields_elements;
477 extern long code_defs_elements;
480 * code_write -- writes out the compiled file
481 * code_init -- prepares the code file
486 //===================================================================
487 //========================= assembler.c =============================
488 //===================================================================
489 static const struct {
490 const char *m; /* menomic */
491 const size_t o; /* operands */
492 const size_t l; /* menomic len */
493 } const asm_instr[] = {
494 [INSTR_DONE] = { "DONE" , 1, 4 },
495 [INSTR_MUL_F] = { "MUL_F" , 3, 5 },
496 [INSTR_MUL_V] = { "MUL_V" , 3, 5 },
497 [INSTR_MUL_FV] = { "MUL_FV" , 3, 6 },
498 [INSTR_MUL_VF] = { "MUL_VF" , 3, 6 },
499 [INSTR_DIV_F] = { "DIV" , 0, 3 },
500 [INSTR_ADD_F] = { "ADD_F" , 3, 5 },
501 [INSTR_ADD_V] = { "ADD_V" , 3, 5 },
502 [INSTR_SUB_F] = { "SUB_F" , 3, 5 },
503 [INSTR_SUB_V] = { "DUB_V" , 3, 5 },
504 [INSTR_EQ_F] = { "EQ_F" , 0, 4 },
505 [INSTR_EQ_V] = { "EQ_V" , 0, 4 },
506 [INSTR_EQ_S] = { "EQ_S" , 0, 4 },
507 [INSTR_EQ_E] = { "EQ_E" , 0, 4 },
508 [INSTR_EQ_FNC] = { "ES_FNC" , 0, 6 },
509 [INSTR_NE_F] = { "NE_F" , 0, 4 },
510 [INSTR_NE_V] = { "NE_V" , 0, 4 },
511 [INSTR_NE_S] = { "NE_S" , 0, 4 },
512 [INSTR_NE_E] = { "NE_E" , 0, 4 },
513 [INSTR_NE_FNC] = { "NE_FNC" , 0, 6 },
514 [INSTR_LE] = { "LE" , 0, 2 },
515 [INSTR_GE] = { "GE" , 0, 2 },
516 [INSTR_LT] = { "LT" , 0, 2 },
517 [INSTR_GT] = { "GT" , 0, 2 },
518 [INSTR_LOAD_F] = { "FIELD_F" , 0, 7 },
519 [INSTR_LOAD_V] = { "FIELD_V" , 0, 7 },
520 [INSTR_LOAD_S] = { "FIELD_S" , 0, 7 },
521 [INSTR_LOAD_ENT] = { "FIELD_ENT" , 0, 9 },
522 [INSTR_LOAD_FLD] = { "FIELD_FLD" , 0, 9 },
523 [INSTR_LOAD_FNC] = { "FIELD_FNC" , 0, 9 },
524 [INSTR_ADDRESS] = { "ADDRESS" , 0, 7 },
525 [INSTR_STORE_F] = { "STORE_F" , 0, 7 },
526 [INSTR_STORE_V] = { "STORE_V" , 0, 7 },
527 [INSTR_STORE_S] = { "STORE_S" , 0, 7 },
528 [INSTR_STORE_ENT] = { "STORE_ENT" , 0, 9 },
529 [INSTR_STORE_FLD] = { "STORE_FLD" , 0, 9 },
530 [INSTR_STORE_FNC] = { "STORE_FNC" , 0, 9 },
531 [INSTR_STOREP_F] = { "STOREP_F" , 0, 8 },
532 [INSTR_STOREP_V] = { "STOREP_V" , 0, 8 },
533 [INSTR_STOREP_S] = { "STOREP_S" , 0, 8 },
534 [INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
535 [INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
536 [INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
537 [INSTR_RETURN] = { "RETURN" , 0, 6 },
538 [INSTR_NOT_F] = { "NOT_F" , 0, 5 },
539 [INSTR_NOT_V] = { "NOT_V" , 0, 5 },
540 [INSTR_NOT_S] = { "NOT_S" , 0, 5 },
541 [INSTR_NOT_ENT] = { "NOT_ENT" , 0, 7 },
542 [INSTR_NOT_FNC] = { "NOT_FNC" , 0, 7 },
543 [INSTR_IF] = { "IF" , 0, 2 },
544 [INSTR_IFNOT] = { "IFNOT" , 0, 5 },
545 [INSTR_CALL0] = { "CALL0" , 0, 5 },
546 [INSTR_CALL1] = { "CALL1" , 0, 5 },
547 [INSTR_CALL2] = { "CALL2" , 0, 5 },
548 [INSTR_CALL3] = { "CALL3" , 0, 5 },
549 [INSTR_CALL4] = { "CALL4" , 0, 5 },
550 [INSTR_CALL5] = { "CALL5" , 0, 5 },
551 [INSTR_CALL6] = { "CALL6" , 0, 5 },
552 [INSTR_CALL7] = { "CALL7" , 0, 5 },
553 [INSTR_CALL8] = { "CALL8" , 0, 5 },
554 [INSTR_STATE] = { "STATE" , 0, 5 },
555 [INSTR_GOTO] = { "GOTO" , 0, 4 },
556 [INSTR_AND] = { "AND" , 0, 3 },
557 [INSTR_OR] = { "OR" , 0, 2 },
558 [INSTR_BITAND] = { "BITAND" , 0, 6 },
559 [INSTR_BITOR] = { "BITOR" , 0, 5 }
562 void asm_init (const char *, FILE **);
563 void asm_close(FILE *);
564 void asm_parse(FILE *);
565 //======================================================================
566 //============================= main.c =================================
567 //======================================================================
569 COMPILER_QCC, /* circa QuakeC */
570 COMPILER_FTEQCC, /* fteqcc QuakeC */
571 COMPILER_QCCX, /* qccx QuakeC */
572 COMPILER_GMQCC /* this QuakeC */
574 extern bool opts_debug;
575 extern bool opts_memchk;
576 extern bool opts_darkplaces_stringtablebug;
577 extern bool opts_omit_nullcode;
578 extern int opts_compiler;
579 //======================================================================
580 //============================= ast.c ==================================
581 //======================================================================
582 #define MEM_VECTOR_PROTO(Towner, Tmem, mem) \
583 bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem); \
584 bool GMQCC_WARN Towner##_##mem##_remove(Towner*, size_t)
586 #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem) \
587 MEM_VECTOR_PROTO(Towner, Tmem, mem); \
588 bool GMQCC_WARN Towner##_##mem##_find(Towner*, Tmem, size_t*); \
589 void Towner##_##mem##_clear(Towner*)
591 #define MEM_VECTOR_MAKE(Twhat, name) \
593 size_t name##_count; \
596 #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem) \
597 bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f) \
600 if (self->mem##_count == self->mem##_alloc) { \
601 if (!self->mem##_alloc) { \
602 self->mem##_alloc = 16; \
604 self->mem##_alloc *= 2; \
606 reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc); \
610 memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
614 self->mem[self->mem##_count++] = f; \
618 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
619 bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx) \
623 if (idx >= self->mem##_count) { \
624 return true; /* huh... */ \
626 for (i = idx; i < self->mem##_count-1; ++i) { \
627 self->mem[i] = self->mem[i+1]; \
629 self->mem##_count--; \
630 if (self->mem##_count < self->mem##_count/2) { \
631 self->mem##_alloc /= 2; \
632 reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count); \
636 memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
643 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem) \
644 bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
647 for (i = 0; i < self->mem##_count; ++i) { \
648 if (self->mem[i] == obj) { \
658 #define _MEM_VEC_FUN_CLEAR(Tself, mem) \
659 void Tself##_##mem##_clear(Tself *self) \
663 mem_d((void*) self->mem); \
665 self->mem##_count = 0; \
666 self->mem##_alloc = 0; \
669 #define MEM_VECTOR_CLEAR(owner, mem) \
671 mem_d((void*)((owner)->mem)); \
672 (owner)->mem = NULL; \
673 (owner)->mem##_count = 0; \
674 (owner)->mem##_alloc = 0
676 #define MEM_VECTOR_INIT(owner, mem) \
678 (owner)->mem = NULL; \
679 (owner)->mem##_count = 0; \
680 (owner)->mem##_alloc = 0; \
683 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
684 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
685 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
687 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
688 MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
689 _MEM_VEC_FUN_CLEAR(Tself, mem) \
690 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
694 store_local, /* local, assignable for now, should get promoted later */
695 store_value, /* unassignable */
703 * A shallow copy of a lex_file to remember where which ast node