6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * Disable some over protective warnings in visual studio because fixing them is a waste
38 # pragma warning(disable : 4244 ) /* conversion from 'int' to 'float', possible loss of data */
39 # pragma warning(disable : 4018 ) /* signed/unsigned mismatch */
40 # pragma warning(disable : 4996 ) /* This function or variable may be unsafe */
41 # pragma warning(disable : 4700 ) /* uninitialized local variable used */
44 #define GMQCC_VERSION_MAJOR 0
45 #define GMQCC_VERSION_MINOR 2
46 #define GMQCC_VERSION_PATCH 0
47 #define GMQCC_VERSION_BUILD(J,N,P) (((J)<<16)|((N)<<8)|(P))
48 #define GMQCC_VERSION \
49 GMQCC_VERSION_BUILD(GMQCC_VERSION_MAJOR, GMQCC_VERSION_MINOR, GMQCC_VERSION_PATCH)
52 * We cannoy rely on C99 at all, since compilers like MSVC
53 * simply don't support it. We define our own boolean type
54 * as a result (since we cannot include <stdbool.h>). For
55 * compilers that are in 1999 mode (C99 compliant) we can use
56 * the language keyword _Bool which can allow for better code
57 * on GCC and GCC-like compilers, opposed to `int`.
68 # ifdef __STDC_VERSION__
69 # if __STDC_VERSION__ < 199901L && __GNUC__ < 3
76 # endif /* !__STDC_VERSION__ */
77 #endif /* !__cplusplus */
80 * Of some functions which are generated we want to make sure
81 * that the result isn't ignored. To find such function calls,
84 #if defined(__GNUC__) || defined(__CLANG__)
85 # define GMQCC_WARN __attribute__((warn_unused_result))
90 * This is a hack to silent clang regarding empty
93 #define GMQCC_SUPPRESS_EMPTY_BODY do { } while (0)
96 * Inline is not supported in < C90, however some compilers
97 * like gcc and clang might have an inline attribute we can
100 #ifdef __STDC_VERSION__
101 # if __STDC_VERSION__ < 199901L
102 # if defined(__GNUC__) || defined (__CLANG__)
104 # define GMQCC_INLINE
106 # define GMQCC_INLINE __attribute__ ((always_inline))
109 # define GMQCC_INLINE
112 # define GMQCC_INLINE inline
115 * Visual studio has __forcinline we can use. So lets use that
116 * I suspect it also has just __inline of some sort, but our use
117 * of inline is correct (not guessed), WE WANT IT TO BE INLINE
119 #elif defined(_MSC_VER)
120 # define GMQCC_INLINE __forceinline
122 # define GMQCC_INLINE
123 #endif /* !__STDC_VERSION__ */
126 * noreturn is present in GCC and clang
127 * it's required for _ast_node_destory otherwise -Wmissing-noreturn
128 * in clang complains about there being no return since abort() is
131 #if (defined(__GNUC__) && __GNUC__ >= 2) || defined(__CLANG__)
132 # define GMQCC_NORETURN __attribute__ ((noreturn))
134 # define GMQCC_NORETURN
137 /* TODO: visual studiblows work around */
142 /*===================================================================*/
143 /*=========================== util.c ================================*/
144 /*===================================================================*/
145 FILE *util_fopen(const char *filename, const char *mode);
147 void *util_memory_a (size_t, unsigned int, const char *);
148 void util_memory_d (void *, unsigned int, const char *);
149 void *util_memory_r (void *, size_t, unsigned int, const char *);
150 void util_meminfo ();
152 bool util_filexists (const char *);
153 bool util_strupper (const char *);
154 bool util_strdigit (const char *);
155 bool util_strncmpexact (const char *, const char *, size_t);
156 char *util_strdup (const char *);
157 char *util_strrq (const char *);
158 char *util_strrnl (const char *);
159 char *util_strsws (const char *);
160 char *util_strchp (const char *, const char *);
161 void util_debug (const char *, const char *, ...);
162 int util_getline (char **, size_t *, FILE *);
163 void util_endianswap (void *, size_t, unsigned int);
165 size_t util_strtocmd (const char *, char *, size_t);
166 size_t util_strtononcmd (const char *, char *, size_t);
168 uint16_t util_crc16(uint16_t crc, const char *data, size_t len);
169 uint32_t util_crc32(uint32_t crc, const char *data, size_t len);
172 # define mem_a(x) malloc (x)
173 # define mem_d(x) free (x)
174 # define mem_r(x, n) realloc(x, n)
176 # define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
177 # define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
178 # define mem_r(x, n) util_memory_r((x), (n), __LINE__, __FILE__)
182 * TODO: make these safer to use. Currently this only works on
183 * x86 and x86_64, some systems will likely not like this. Such
186 #define FLT2INT(Y) *((int32_t*)&(Y))
187 #define INT2FLT(Y) *((float *)&(Y))
189 /* New flexible vector implementation from Dale */
190 #define _vec_raw(A) (((size_t*)(void*)(A)) - 2)
191 #define _vec_beg(A) (_vec_raw(A)[0])
192 #define _vec_end(A) (_vec_raw(A)[1])
193 #define _vec_needsgrow(A,N) ((!(A)) || (_vec_end(A) + (N) >= _vec_beg(A)))
194 #define _vec_mightgrow(A,N) (_vec_needsgrow((A), (N)) ? (void)_vec_forcegrow((A),(N)) : (void)0)
195 #define _vec_forcegrow(A,N) _util_vec_grow(((void**)&(A)), (N), sizeof(*(A)))
196 #define _vec_remove(A,S,I,N) (memmove((char*)(A)+(I)*(S),(char*)(A)+((I)+(N))*(S),(S)*(_vec_end(A)-(I)-(N))), _vec_end(A)-=(N))
197 void _util_vec_grow(void **a, size_t i, size_t s);
198 /* exposed interface */
199 #define vec_free(A) ((A) ? (mem_d((void*)_vec_raw(A)), (A) = NULL) : 0)
200 #define vec_push(A,V) (_vec_mightgrow((A),1), (A)[_vec_end(A)++] = (V))
201 #define vec_size(A) ((A) ? _vec_end(A) : 0)
202 #define vec_add(A,N) (_vec_mightgrow((A),(N)), _vec_end(A)+=(N), &(A)[_vec_end(A)-(N)])
203 #define vec_last(A) ((A)[_vec_end(A)-1])
204 #define vec_append(A,N,S) memcpy(vec_add((A), (N)), (S), N * sizeof(*(S)))
205 #define vec_remove(A,I,N) _vec_remove((A), sizeof(*(A)), (I), (N))
206 #define vec_pop(A) (_vec_end(A)-=1)
207 /* these are supposed to NOT reallocate */
208 #define vec_shrinkto(A,N) (_vec_end(A) = (N))
209 #define vec_shrinkby(A,N) (_vec_end(A) -= (N))
211 #define vec_upload(X,Y,S) \
220 typedef struct hash_table_t {
222 struct hash_node_t **table;
226 * hashtable implementation:
229 * This was designed for pointers: you manage the life of the object yourself
230 * if you do use this for non-pointers please be warned that the object may not
231 * be valid if the duration of it exceeds (i.e on stack). So you need to allocate
232 * yourself, or put those in global scope to ensure duration is for the whole
235 * util_htnew(size) -- to make a new hashtable
236 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
237 * util_htget(table, key) -- to get something from the table
238 * util_htdel(table) -- to delete the table
242 * ht foo = util_htnew(1024);
244 * char *test = "hello world\n";
245 * util_htset(foo, "foo", (void*)&data);
246 * util_gtset(foo, "bar", (void*)test);
248 * printf("foo: %d, bar %s",
249 * *((int *)util_htget(foo, "foo")),
250 * ((char*)util_htget(foo, "bar"))
255 hash_table_t *util_htnew (size_t size);
256 void util_htset (hash_table_t *ht, const char *key, void *value);
257 void *util_htget (hash_table_t *ht, const char *key);
258 void util_htdel (hash_table_t *ht);
259 size_t util_hthash(hash_table_t *ht, const char *key);
260 void *util_htgeth(hash_table_t *ht, const char *key, size_t hash);
261 void util_htseth(hash_table_t *ht, const char *key, size_t hash, void *value);
262 /*===================================================================*/
263 /*=========================== code.c ================================*/
264 /*===================================================================*/
266 /* Note: if you change the order, fix type_sizeof in ir.c */
285 /* const/var qualifiers */
289 /* magic number to help parsing */
290 #define CV_WRONG 0x8000
292 extern const char *type_name [TYPE_COUNT];
293 extern size_t type_sizeof [TYPE_COUNT];
294 extern uint16_t type_store_instr [TYPE_COUNT];
295 extern uint16_t field_store_instr[TYPE_COUNT];
298 * could use type_store_instr + INSTR_STOREP_F - INSTR_STORE_F
299 * but this breaks when TYPE_INTEGER is added, since with the enhanced
300 * instruction set, the old ones are left untouched, thus the _I instructions
301 * are at a seperate place.
303 extern uint16_t type_storep_instr[TYPE_COUNT];
304 extern uint16_t type_eq_instr [TYPE_COUNT];
305 extern uint16_t type_ne_instr [TYPE_COUNT];
306 extern uint16_t type_not_instr [TYPE_COUNT];
309 uint32_t offset; /* Offset in file of where data begins */
310 uint32_t length; /* Length of section (how many of) */
314 uint32_t version; /* Program version (6) */
318 prog_section statements; /* prog_section_statement */
319 prog_section defs; /* prog_section_def */
320 prog_section fields; /* prog_section_field */
321 prog_section functions; /* prog_section_function */
322 prog_section strings;
323 prog_section globals;
324 uint32_t entfield; /* Number of entity fields */
328 * Each paramater incerements by 3 since vector types hold
329 * 3 components (x,y,z).
333 #define OFS_PARM0 (OFS_RETURN+3)
334 #define OFS_PARM1 (OFS_PARM0 +3)
335 #define OFS_PARM2 (OFS_PARM1 +3)
336 #define OFS_PARM3 (OFS_PARM2 +3)
337 #define OFS_PARM4 (OFS_PARM3 +3)
338 #define OFS_PARM5 (OFS_PARM4 +3)
339 #define OFS_PARM6 (OFS_PARM5 +3)
340 #define OFS_PARM7 (OFS_PARM6 +3)
347 int16_t s1; /* signed */
348 uint16_t u1; /* unsigned */
352 int16_t s1; /* signed */
353 uint16_t u1; /* unsigned */
357 int16_t s1; /* signed */
358 uint16_t u1; /* unsigned */
362 * This is the same as the structure in darkplaces
367 * But this one is more sane to work with, and the
368 * type sizes are guranteed.
370 } prog_section_statement;
382 * 7 = ev_pointer -- engine only
383 * 8 = ev_bad -- engine only
390 typedef prog_section_both prog_section_def;
391 typedef prog_section_both prog_section_field;
393 /* this is ORed to the type */
394 #define DEF_SAVEGLOBAL (1<<15)
395 #define DEF_TYPEMASK ((1<<15)-1)
398 int32_t entry; /* in statement table for instructions */
399 uint32_t firstlocal; /* First local in local table */
400 uint32_t locals; /* Total ints of params + locals */
401 uint32_t profile; /* Always zero (engine uses this) */
402 uint32_t name; /* name of function in string table */
403 uint32_t file; /* file of the source file */
404 int32_t nargs; /* number of arguments */
405 uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
406 } prog_section_function;
410 * These are the external instructions supported by the interperter
411 * this is what things compile to (from the C code).
417 INSTR_MUL_FV, /* NOTE: the float operands must NOT be at the same locations: A != C */
418 INSTR_MUL_VF, /* and here: B != C */
482 * Virtual instructions used by the assembler
483 * keep at the end but before virtual instructions
489 * Virtual instructions used by the IR
495 /* A never returning CALL.
496 * Creating this causes IR blocks to be marked as 'final'.
502 extern prog_section_statement *code_statements;
503 extern int *code_linenums;
504 extern prog_section_def *code_defs;
505 extern prog_section_field *code_fields;
506 extern prog_section_function *code_functions;
507 extern int *code_globals;
508 extern char *code_chars;
509 extern uint16_t code_crc;
511 typedef float qcfloat;
512 typedef int32_t qcint;
515 * code_write -- writes out the compiled file
516 * code_init -- prepares the code file
518 bool code_write (const char *filename, const char *lno);
520 uint32_t code_genstring (const char *string);
521 uint32_t code_cachedstring(const char *string);
522 qcint code_alloc_field (size_t qcsize);
524 /* this function is used to keep statements and linenumbers together */
525 void code_push_statement(prog_section_statement *stmt, int linenum);
526 void code_pop_statement();
529 * A shallow copy of a lex_file to remember where which ast node
537 /*===================================================================*/
538 /*============================ con.c ================================*/
539 /*===================================================================*/
558 void con_vprintmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap);
559 void con_printmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, ...);
560 void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap);
561 void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...);
566 void con_color (int);
567 int con_change(const char *, const char *);
568 int con_verr (const char *, va_list);
569 int con_vout (const char *, va_list);
570 int con_err (const char *, ...);
571 int con_out (const char *, ...);
573 /* error/warning interface */
574 extern size_t compile_errors;
575 extern size_t compile_warnings;
577 void /********/ compile_error (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, ...);
578 void /********/ vcompile_error (lex_ctx ctx, /*LVL_ERROR*/ const char *msg, va_list ap);
579 bool GMQCC_WARN compile_warning (lex_ctx ctx, int warntype, const char *fmt, ...);
580 bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_list ap);
582 /*===================================================================*/
583 /*========================= assembler.c =============================*/
584 /*===================================================================*/
585 static const struct {
586 const char *m; /* menomic */
587 const size_t o; /* operands */
588 const size_t l; /* menomic len */
614 { "FIELD_F" , 0, 7 },
615 { "FIELD_V" , 0, 7 },
616 { "FIELD_S" , 0, 7 },
617 { "FIELD_ENT" , 0, 9 },
618 { "FIELD_FLD" , 0, 9 },
619 { "FIELD_FNC" , 0, 9 },
620 { "ADDRESS" , 0, 7 },
621 { "STORE_F" , 0, 7 },
622 { "STORE_V" , 0, 7 },
623 { "STORE_S" , 0, 7 },
624 { "STORE_ENT" , 0, 9 },
625 { "STORE_FLD" , 0, 9 },
626 { "STORE_FNC" , 0, 9 },
627 { "STOREP_F" , 0, 8 },
628 { "STOREP_V" , 0, 8 },
629 { "STOREP_S" , 0, 8 },
630 { "STOREP_ENT", 0, 10},
631 { "STOREP_FLD", 0, 10},
632 { "STOREP_FNC", 0, 10},
637 { "NOT_ENT" , 0, 7 },
638 { "NOT_FNC" , 0, 7 },
657 { "END" , 0, 3 } /* virtual assembler instruction */
659 /*===================================================================*/
660 /*============================= ir.c ================================*/
661 /*===================================================================*/
665 store_local, /* local, assignable for now, should get promoted later */
666 store_param, /* parameters, they are locals with a fixed position */
667 store_value, /* unassignable */
668 store_return /* unassignable, at OFS_RETURN */
675 vector vec3_add (vector, vector);
676 vector vec3_sub (vector, vector);
677 qcfloat vec3_mulvv(vector, vector);
678 vector vec3_mulvf(vector, float);
680 /*===================================================================*/
681 /*============================= exec.c ==============================*/
682 /*===================================================================*/
685 * Darkplaces has (or will have) a 64 bit prog loader
686 * where the 32 bit qc program is autoconverted on load.
687 * Since we may want to support that as well, let's redefine
688 * float and int here.
700 typedef char qcfloat_size_is_correct [sizeof(qcfloat) == 4 ?1:-1];
701 typedef char qcint_size_is_correct [sizeof(qcint) == 4 ?1:-1];
705 VMERR_TEMPSTRING_ALLOC,
710 #define VM_JUMPS_DEFAULT 1000000
713 #define VMXF_DEFAULT 0x0000 /* default flags - nothing */
714 #define VMXF_TRACE 0x0001 /* trace: print statements before executing */
715 #define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */
719 typedef int (*prog_builtin)(struct qc_program_s *prog);
724 prog_section_function *function;
727 typedef struct qc_program_s {
730 prog_section_statement *code;
731 prog_section_def *defs;
732 prog_section_def *fields;
733 prog_section_function *functions;
739 const char* *function_stack;
743 size_t tempstring_start;
744 size_t tempstring_at;
750 prog_builtin *builtins;
751 size_t builtins_count;
756 bool allowworldwrites;
759 qc_exec_stack *stack;
764 int argc; /* current arg count for debugging */
767 qc_program* prog_load(const char *filename);
768 void prog_delete(qc_program *prog);
770 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps);
772 char* prog_getstring (qc_program *prog, qcint str);
773 prog_section_def* prog_entfield (qc_program *prog, qcint off);
774 prog_section_def* prog_getdef (qc_program *prog, qcint off);
775 qcany* prog_getedict (qc_program *prog, qcint e);
776 qcint prog_tempstring(qc_program *prog, const char *_str);
779 /*===================================================================*/
780 /*===================== parser.c commandline ========================*/
781 /*===================================================================*/
784 bool parser_compile_file (const char *filename);
785 bool parser_compile_string(const char *name, const char *str);
786 bool parser_finish (const char *output);
787 void parser_cleanup ();
788 /* There's really no need to strlen() preprocessed files */
789 bool parser_compile_string_len(const char *name, const char *str, size_t len);
791 /*===================================================================*/
792 /*====================== ftepp.c commandline ========================*/
793 /*===================================================================*/
795 bool ftepp_preprocess_file (const char *filename);
796 bool ftepp_preprocess_string(const char *name, const char *str);
797 void ftepp_finish ();
798 const char *ftepp_get ();
800 void ftepp_add_define (const char *source, const char *name);
801 void ftepp_add_macro (const char *name, const char *value);
803 /*===================================================================*/
804 /*======================= main.c commandline ========================*/
805 /*===================================================================*/
808 /* Helpers to allow for a whole lot of flags. Otherwise we'd limit
809 * to 32 or 64 -f options...
812 size_t idx; /* index into an array of 32 bit words */
813 uint8_t bit; /* index _into_ the 32 bit word, thus just uint8 */
815 #define LONGBIT(bit) { ((bit)/32), ((bit)%32) }
817 typedef uint32_t longbit;
818 #define LONGBIT(bit) (bit)
821 /*===================================================================*/
822 /*============================= opts.c ==============================*/
823 /*===================================================================*/
829 bool opts_setflag (const char *, bool);
830 bool opts_setwarn (const char *, bool);
831 bool opts_setwerror(const char *, bool);
832 bool opts_setoptim (const char *, bool);
834 void opts_init (const char *, int, size_t);
835 void opts_set (uint32_t *, size_t, bool);
836 void opts_setoptimlevel(unsigned int);
837 void opts_ini_init (const char *);
840 # define GMQCC_TYPE_FLAGS
841 # define GMQCC_DEFINE_FLAG(X) X,
845 static const opts_flag_def opts_flag_list[] = {
846 # define GMQCC_TYPE_FLAGS
847 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
853 # define GMQCC_TYPE_WARNS
854 # define GMQCC_DEFINE_FLAG(X) WARN_##X,
858 static const opts_flag_def opts_warn_list[] = {
859 # define GMQCC_TYPE_WARNS
860 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
866 # define GMQCC_TYPE_OPTIMIZATIONS
867 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) OPTIM_##NAME,
871 static const opts_flag_def opts_opt_list[] = {
872 # define GMQCC_TYPE_OPTIMIZATIONS
873 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
877 static const unsigned int opts_opt_oflag[] = {
878 # define GMQCC_TYPE_OPTIMIZATIONS
879 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) MIN_O,
883 extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
887 COMPILER_QCC, /* circa QuakeC */
888 COMPILER_FTEQCC, /* fteqcc QuakeC */
889 COMPILER_QCCX, /* qccx QuakeC */
890 COMPILER_GMQCC /* this QuakeC */
894 uint32_t O; /* -Ox */
895 const char *output; /* -o file */
897 opts_std_t standard; /* -std= */
898 bool debug; /* -debug */
899 bool memchk; /* -memchk */
900 bool dumpfin; /* -dumpfin */
901 bool dump; /* -dump */
902 bool forcecrc; /* --force-crc= */
903 uint16_t forced_crc; /* --force-crc= */
904 bool pp_only; /* -E */
905 size_t max_array_size; /* --max-array= */
907 uint32_t flags [1 + (COUNT_FLAGS / 32)];
908 uint32_t warn [1 + (COUNT_WARNINGS / 32)];
909 uint32_t werror [1 + (COUNT_WARNINGS / 32)];
910 uint32_t optimization[1 + (COUNT_OPTIMIZATIONS / 32)];
913 extern opts_cmd_t opts;
915 /*===================================================================*/
916 #define OPTS_FLAG(i) (!! (opts.flags [(i)/32] & (1<< ((i)%32))))
917 #define OPTS_WARN(i) (!! (opts.warn [(i)/32] & (1<< ((i)%32))))
918 #define OPTS_WERROR(i) (!! (opts.werror [(i)/32] & (1<< ((i)%32))))
919 #define OPTS_OPTIMIZATION(i) (!! (opts.optimization[(i)/32] & (1<< ((i)%32))))