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 * stdint.h and inttypes.h -less subset
63 * for systems that don't have it, which we must
64 * assume is all systems. (int8_t not required)
67 typedef unsigned char uint8_t; /* same as below */
68 #elif SCHAR_MIN == -128
69 typedef unsigned char uint8_t; /* same as above */
71 #if SHRT_MAX == 0x7FFF
72 typedef short int16_t;
73 typedef unsigned short uint16_t;
74 #elif INT_MAX == 0x7FFF
76 typedef unsigned int uint16_t;
78 #if INT_MAX == 0x7FFFFFFF
80 typedef unsigned int uint32_t;
81 #elif LONG_MAX == 0x7FFFFFFF
83 typedef unsigned long uint32_t;
85 #ifdef _LP64 /* long pointer == 64 */
86 typedef unsigned long uintptr_t;
87 typedef long intptr_t;
89 typedef unsigned int uintptr_t;
92 /* Ensure type sizes are correct: */
93 typedef char uint8_size_is_correct [sizeof(uint8_t) == 1?1:-1];
94 typedef char uint16_size_if_correct [sizeof(uint16_t) == 2?1:-1];
95 typedef char uint32_size_is_correct [sizeof(uint32_t) == 4?1:-1];
96 typedef char int16_size_if_correct [sizeof(int16_t) == 2?1:-1];
97 typedef char int32_size_is_correct [sizeof(int32_t) == 4?1:-1];
98 /* intptr_t / uintptr_t correct size check */
99 typedef char uintptr_size_is_correct[sizeof(intptr_t) == sizeof(int*)?1:-1];
100 typedef char intptr_size_is_correct [sizeof(uintptr_t)== sizeof(int*)?1:-1];
102 //===================================================================
103 //============================ lex.c ================================
104 //===================================================================
106 FILE *file; /* file handler */
107 char *name; /* name of file */
111 int last; /* last token */
112 int current; /* current token */
113 int length; /* bytes left to parse */
114 int size; /* never changes (size of file) */
115 int line; /* what line are we on? */
119 * It's important that this table never exceed 32 keywords, the ascii
120 * table starts at 33 (and we don't want conflicts)
131 TOKEN_FOR , // extension
132 TOKEN_TYPEDEF , // extension
134 // ensure the token types are out of the
135 // bounds of anyothers that may conflict.
144 * Lexer state constants, these are numbers for where exactly in
145 * the lexing the lexer is at. Or where it decided to stop if a lexer
146 * error occurs. These numbers must be > where the ascii-table ends
147 * and > the last type token which is TOKEN_VOID
156 int lex_token (struct lex_file *);
157 void lex_reset (struct lex_file *);
158 void lex_close (struct lex_file *);
159 struct lex_file *lex_include(struct lex_file *, char *);
160 struct lex_file *lex_open (FILE *);
162 //===================================================================
163 //========================== error.c ================================
164 //===================================================================
165 #define ERROR_LEX (SHRT_MAX+0)
166 #define ERROR_PARSE (SHRT_MAX+1)
167 #define ERROR_INTERNAL (SHRT_MAX+2)
168 #define ERROR_COMPILER (SHRT_MAX+3)
169 #define ERROR_PREPRO (SHRT_MAX+4)
170 int error(struct lex_file *, int, const char *, ...);
172 //===================================================================
173 //========================== parse.c ================================
174 //===================================================================
175 int parse_gen(struct lex_file *);
177 //===================================================================
178 //========================== typedef.c ==============================
179 //===================================================================
180 typedef struct typedef_node_t {
185 void typedef_clear();
186 typedef_node *typedef_find(const char *);
187 int typedef_add (struct lex_file *file, const char *, const char *);
190 //===================================================================
191 //=========================== util.c ================================
192 //===================================================================
193 void *util_memory_a (unsigned int, unsigned int, const char *);
194 void util_memory_d (void *, unsigned int, const char *);
195 void util_meminfo ();
197 bool util_strupper (const char *);
198 char *util_strdup (const char *);
199 char *util_strrq (char *);
200 char *util_strrnl (char *);
201 char *util_strsws (const char *);
202 char *util_strchp (const char *, const char *);
203 void util_debug (const char *, const char *, ...);
204 int util_getline (char **, size_t *, FILE *);
205 void util_endianswap (void *, int, int);
207 uint32_t util_crc32(const char *, int, register const short);
210 # define mem_a(x) malloc(x)
211 # define mem_d(x) free (x)
213 # define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
214 # define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
217 /* Builds vector type (usefull for inside structures) */
218 #define VECTOR_TYPE(T,N) \
219 T* N##_data = NULL; \
220 long N##_elements = 0; \
221 long N##_allocated = 0
222 /* Builds vector add */
223 #define VECTOR_CORE(T,N) \
224 int N##_add(T element) { \
225 if (N##_elements == N##_allocated) { \
226 if (N##_allocated == 0) { \
227 N##_allocated = 12; \
229 N##_allocated *= 2; \
231 void *temp = mem_a(N##_allocated * sizeof(T)); \
236 memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
238 N##_data = (T*)temp; \
240 N##_data[N##_elements] = element; \
241 return N##_elements++; \
243 int N##_put(T* elements, size_t len) { \
246 while (N##_add(*++elements) != -1 && len--); \
247 return N##_elements; \
249 /* Builds a full vector inspot */
250 #define VECTOR_MAKE(T,N) \
253 /* Builds a vector add function pointer for inside structures */
254 #define VECTOR_IMPL(T,N) int (*N##_add)(T)
256 //===================================================================
257 //=========================== code.c ================================
258 //===================================================================
271 * Each paramater incerements by 3 since vector types hold
272 * 3 components (x,y,z).
276 #define OFS_PARM0 (OFS_RETURN+3)
277 #define OFS_PARM1 (OFS_PARM0 +3)
278 #define OFS_PARM2 (OFS_PARM1 +3)
279 #define OFS_PARM3 (OFS_PARM2 +3)
280 #define OFS_PARM4 (OFS_PARM3 +3)
281 #define OFS_PARM5 (OFS_PARM4 +3)
282 #define OFS_PARM6 (OFS_PARM5 +3)
283 #define OFS_PARM7 (OFS_PARM6 +3)
290 int16_t s1; /* signed */
291 uint16_t u1; /* unsigned */
295 int16_t s2; /* signed */
296 uint16_t u2; /* unsigned */
300 int16_t s3; /* signed */
301 uint16_t u3; /* unsigned */
305 * This is the same as the structure in darkplaces
310 * But this one is more sane to work with, and the
311 * type sizes are guranteed.
313 } prog_section_statement;
324 * 7 = ev_pointer -- engine only
325 * 8 = ev_bad -- engine only
331 typedef prog_section_both prog_section_def;
332 typedef prog_section_both prog_section_field;
335 int32_t entry; /* in statement table for instructions */
336 uint32_t firstlocal; /* First local in local table */
337 uint32_t locals; /* Total ints of params + locals */
338 uint32_t profile; /* Always zero (engine uses this) */
339 uint32_t name; /* name of function in string table */
340 uint32_t file; /* file of the source file */
341 uint32_t nargs; /* number of arguments */
342 uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
343 } prog_section_function;
347 * These are the external instructions supported by the interperter
348 * this is what things compile to (from the C code).
420 * The symbols below are created by the following
423 * VECTOR_MAKE(prog_section_statement, code_statements);
424 * VECTOR_MAKE(prog_section_def, code_defs );
425 * VECTOR_MAKE(prog_section_field, code_fields );
426 * VECTOR_MAKE(prog_section_function, code_functions );
427 * VECTOR_MAKE(int, code_globals );
428 * VECTOR_MAKE(char, code_chars );
430 int code_statements_add(prog_section_statement);
431 int code_defs_add (prog_section_def);
432 int code_fields_add (prog_section_field);
433 int code_functions_add (prog_section_function);
434 int code_globals_add (int);
435 int code_chars_add (char);
436 int code_statements_put(prog_section_statement*, size_t);
437 int code_defs_put (prog_section_def*, size_t);
438 int code_fields_put (prog_section_field*, size_t);
439 int code_functions_put (prog_section_function*, size_t);
440 int code_globals_put (int*, size_t);
441 int code_chars_put (char*, size_t);
442 extern long code_statements_elements;
443 extern long code_chars_elements;
444 extern long code_globals_elements;
445 extern long code_functions_elements;
446 extern long code_fields_elements;
447 extern long code_defs_elements;
450 * code_write -- writes out the compiled file
451 * code_init -- prepares the code file
456 //===================================================================
457 //========================= assembler.c =============================
458 //===================================================================
459 static const struct {
460 const char *m; /* menomic */
461 const size_t o; /* operands */
462 const size_t l; /* menomic len */
463 } const asm_instr[] = {
464 [INSTR_DONE] = { "DONE" , 1, 4 },
465 [INSTR_MUL_F] = { "MUL_F" , 3, 5 },
466 [INSTR_MUL_V] = { "MUL_V" , 3, 5 },
467 [INSTR_MUL_FV] = { "MUL_FV" , 3, 6 },
468 [INSTR_MUL_VF] = { "MUL_VF" , 3, 6 },
469 [INSTR_DIV_F] = { "DIV" , 0, 3 },
470 [INSTR_ADD_F] = { "ADD_F" , 3, 5 },
471 [INSTR_ADD_V] = { "ADD_V" , 3, 5 },
472 [INSTR_SUB_F] = { "SUB_F" , 3, 5 },
473 [INSTR_SUB_V] = { "DUB_V" , 3, 5 },
474 [INSTR_EQ_F] = { "EQ_F" , 0, 4 },
475 [INSTR_EQ_V] = { "EQ_V" , 0, 4 },
476 [INSTR_EQ_S] = { "EQ_S" , 0, 4 },
477 [INSTR_EQ_E] = { "EQ_E" , 0, 4 },
478 [INSTR_EQ_FNC] = { "ES_FNC" , 0, 6 },
479 [INSTR_NE_F] = { "NE_F" , 0, 4 },
480 [INSTR_NE_V] = { "NE_V" , 0, 4 },
481 [INSTR_NE_S] = { "NE_S" , 0, 4 },
482 [INSTR_NE_E] = { "NE_E" , 0, 4 },
483 [INSTR_NE_FNC] = { "NE_FNC" , 0, 6 },
484 [INSTR_LE] = { "LE" , 0, 2 },
485 [INSTR_GE] = { "GE" , 0, 2 },
486 [INSTR_LT] = { "LT" , 0, 2 },
487 [INSTR_GT] = { "GT" , 0, 2 },
488 [INSTR_LOAD_F] = { "FIELD_F" , 0, 7 },
489 [INSTR_LOAD_V] = { "FIELD_V" , 0, 7 },
490 [INSTR_LOAD_S] = { "FIELD_S" , 0, 7 },
491 [INSTR_LOAD_ENT] = { "FIELD_ENT" , 0, 9 },
492 [INSTR_LOAD_FLD] = { "FIELD_FLD" , 0, 9 },
493 [INSTR_LOAD_FNC] = { "FIELD_FNC" , 0, 9 },
494 [INSTR_ADDRESS] = { "ADDRESS" , 0, 7 },
495 [INSTR_STORE_F] = { "STORE_F" , 0, 7 },
496 [INSTR_STORE_V] = { "STORE_V" , 0, 7 },
497 [INSTR_STORE_S] = { "STORE_S" , 0, 7 },
498 [INSTR_STORE_ENT] = { "STORE_ENT" , 0, 9 },
499 [INSTR_STORE_FLD] = { "STORE_FLD" , 0, 9 },
500 [INSTR_STORE_FNC] = { "STORE_FNC" , 0, 9 },
501 [INSTR_STOREP_F] = { "STOREP_F" , 0, 8 },
502 [INSTR_STOREP_V] = { "STOREP_V" , 0, 8 },
503 [INSTR_STOREP_S] = { "STOREP_S" , 0, 8 },
504 [INSTR_STOREP_ENT] = { "STOREP_ENT", 0, 10},
505 [INSTR_STOREP_FLD] = { "STOREP_FLD", 0, 10},
506 [INSTR_STOREP_FNC] = { "STOREP_FNC", 0, 10},
507 [INSTR_RETURN] = { "RETURN" , 0, 6 },
508 [INSTR_NOT_F] = { "NOT_F" , 0, 5 },
509 [INSTR_NOT_V] = { "NOT_V" , 0, 5 },
510 [INSTR_NOT_S] = { "NOT_S" , 0, 5 },
511 [INSTR_NOT_ENT] = { "NOT_ENT" , 0, 7 },
512 [INSTR_NOT_FNC] = { "NOT_FNC" , 0, 7 },
513 [INSTR_IF] = { "IF" , 0, 2 },
514 [INSTR_IFNOT] = { "IFNOT" , 0, 5 },
515 [INSTR_CALL0] = { "CALL0" , 0, 5 },
516 [INSTR_CALL1] = { "CALL1" , 0, 5 },
517 [INSTR_CALL2] = { "CALL2" , 0, 5 },
518 [INSTR_CALL3] = { "CALL3" , 0, 5 },
519 [INSTR_CALL4] = { "CALL4" , 0, 5 },
520 [INSTR_CALL5] = { "CALL5" , 0, 5 },
521 [INSTR_CALL6] = { "CALL6" , 0, 5 },
522 [INSTR_CALL7] = { "CALL7" , 0, 5 },
523 [INSTR_CALL8] = { "CALL8" , 0, 5 },
524 [INSTR_STATE] = { "STATE" , 0, 5 },
525 [INSTR_GOTO] = { "GOTO" , 0, 4 },
526 [INSTR_AND] = { "AND" , 0, 3 },
527 [INSTR_OR] = { "OR" , 0, 2 },
528 [INSTR_BITAND] = { "BITAND" , 0, 6 },
529 [INSTR_BITOR] = { "BITOR" , 0, 5 }
532 void asm_init (const char *, FILE **);
533 void asm_close(FILE *);
534 void asm_parse(FILE *);
535 //======================================================================
536 //============================= main.c =================================
537 //======================================================================
539 COMPILER_QCC, /* circa QuakeC */
540 COMPILER_FTEQCC, /* fteqcc QuakeC */
541 COMPILER_QCCX, /* qccx QuakeC */
542 COMPILER_GMQCC /* this QuakeC */
544 extern bool opts_debug;
545 extern bool opts_memchk;
546 extern bool opts_darkplaces_stringtablebug;
547 extern bool opts_omit_nullcode;
548 extern int opts_compiler;