2 * Copyright (C) 2012, 2013, 2014
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
30 #define GMQCC_VERSION_MAJOR 0
31 #define GMQCC_VERSION_MINOR 3
32 #define GMQCC_VERSION_PATCH 6
33 #define GMQCC_VERSION ((GMQCC_VERSION_MAJOR<<16)|(GMQCC_VERSION_MINOR<<8)|GMQCC_VERSION_PATCH)
35 #ifdef GMQCC_VERSION_TYPE_RELEASE
37 # define GMQCC_DEV_VERSION_STRING "git build: " GMQCC_GITINFO "\n"
38 # elif defined(GMQCC_VERSION_TYPE_DEVEL)
39 # define GMQCC_DEV_VERSION_STRING "development build\n"
41 # define GMQCC_DEV_VERSION_STRING
42 # endif /*! GMQCC_GITINGO */
44 # define GMQCC_DEV_VERSION_STRING
47 #define GMQCC_STRINGIFY(x) #x
48 #define GMQCC_IND_STRING(x) GMQCC_STRINGIFY(x)
49 #define GMQCC_FULL_VERSION_STRING \
51 GMQCC_IND_STRING(GMQCC_VERSION_MAJOR) "." \
52 GMQCC_IND_STRING(GMQCC_VERSION_MINOR) "." \
53 GMQCC_IND_STRING(GMQCC_VERSION_PATCH) \
54 " Built " __DATE__ " " __TIME__ \
55 "\n" GMQCC_DEV_VERSION_STRING
58 # define false (unsigned char)(0)
59 # define true (unsigned char)(1)
60 typedef unsigned char bool;
63 #if defined(__GNUC__) || defined(__CLANG__)
65 # if (__GNUC__ >= 2) || defined(__CLANG__)
66 # define GMQCC_NORETURN __attribute__((noreturn))
67 # define GMQCC_FORCEINLINE __attribute__((always_inline))
68 # define GMQCC_INLINE __inline
70 # define GMQCC_LIKELY(X) __builtin_expect((X), 1)
71 # define GMQCC_UNLIKELY(X) __builtin_expect((X), 0)
72 # define GMQCC_WARN __attribute__((warn_unused_result))
73 # define GMQCC_USED __attribute__((used))
74 # define GMQCC_RESTRICT __restrict__
77 /* conversion from 'int' to 'float', possible loss of data */
78 # pragma warning(disable : 4244)
80 typedef unsigned __int8 uint8_t;
81 typedef unsigned __int16 uint16_t;
82 typedef unsigned __int32 uint32_t;
83 typedef unsigned __int64 uint64_t;
84 typedef __int16 int16_t;
85 typedef __int32 int32_t;
86 typedef __int64 int64_t;
87 # define GMQCC_NORETURN __declspec(noreturn)
88 # define GMQCC_FORCEINLINE __forceinline
89 # define GMQCC_INLINE __inline
90 # define GMQCC_RESTRICT __restrict
92 # define GMQCC_NORETURN
93 # define GMQCC_FORCEINLINE
95 # define GMQCC_RESTRICT
97 # define GMQCC_LIKELY(X) (X)
98 # define GMQCC_UNLIKELY(X) (X)
103 #define GMQCC_BYTE_ORDER_LITTLE 1234
104 #define GMQCC_BYTE_ORDER_BIG 4321
106 #if defined (__GNUC__) || defined (__GNU_LIBRARY__)
107 # if defined (__FreeBSD__) || defined (__OpenBSD__)
108 # include <sys/endian.h>
109 # elif defined (BSD) && (BSD >= 199103) || defined (__DJGPP__) || defined (__CYGWIN32__)
110 # include <machine/endian.h>
111 # elif defined (__APPLE__)
112 # if defined (__BIG_ENDIAN__) && !defined(BIG_ENDIAN)
114 # elif defined (__LITTLE_ENDIAN__) && !defined (LITTLE_ENDIAN)
115 # define LITTLE_ENDIAN
116 # endif /*! defined (__BIG_ENDIAN__) && !defined(BIG_ENDIAN) */
117 # elif !defined (__MINGW32__)
119 # if !defined (__BEOS__)
120 # include <byteswap.h>
121 # endif /*! !definde (__BEOS__) */
122 # endif /*! defined (__FreeBSD__) || defined (__OpenBSD__) */
123 #endif /*! defined (__GNUC__) || defined (__GNU_LIBRARY__) */
124 #if !defined(PLATFORM_BYTE_ORDER)
125 # if defined (LITTLE_ENDIAN) || defined (BIG_ENDIAN)
126 # if defined (LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
127 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
128 # elif !defined (LITTLE_ENDIAN) && defined (BIG_ENDIAN)
129 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
130 # elif defined (BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
131 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
132 # elif defined (BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
133 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
134 # endif /*! defined (LITTLE_ENDIAN) && !defined(BIG_ENDIAN) */
135 # elif defined (_LITTLE_ENDIAN) || defined (_BIG_ENDIAN)
136 # if defined (_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
137 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
138 # elif !defined (_LITTLE_ENDIAN) && defined (_BIG_ENDIAN)
139 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
140 # elif defined (_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
141 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
142 # elif defined (_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
143 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
144 # endif /*! defined (_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) */
145 # elif defined (__LITTLE_ENDIAN__) || defined (__BIG_ENDIAN__)
146 # if defined (__LITTLE_ENDIAN__) && !defined (__BIG_ENDIAN__)
147 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
148 # elif !defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
149 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
150 # elif defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
151 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
152 # elif defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
153 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
154 # endif /*! defined (__LITTLE_ENDIAN__) && !defined (__BIG_ENDIAN__) */
155 # endif /*! defined(LITTLE_ENDIAN) || defined (BIG_ENDIAN) */
156 #endif /*! !defined(PLATFORM_BYTE_ORDER) */
157 #if !defined (PLATFORM_BYTE_ORDER)
158 # if defined (__alpha__) || defined (__alpha) || defined (i386) || \
159 defined (__i386__) || defined (_M_I86) || defined (_M_IX86) || \
160 defined (__OS2__) || defined (sun386) || defined (__TURBOC__) || \
161 defined (vax) || defined (vms) || defined (VMS) || \
162 defined (__VMS) || defined (__x86_64__) || defined (_M_IA64) || \
163 defined (_M_X64) || defined (__i386) || defined (__x86_64)
164 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_LITTLE
165 # elif defined (AMIGA) || defined (applec) || defined (__AS400__) || \
166 defined (_CRAY) || defined (__hppa) || defined (__hp9000) || \
167 defined (ibm370) || defined (mc68000) || defined (m68k) || \
168 defined (__MRC__) || defined (__MVS__) || defined (__MWERKS__) || \
169 defined (sparc) || defined (__sparc) || defined (SYMANTEC_C) || \
170 defined (__TANDEM) || defined (THINK_C) || defined (__VMCMS__) || \
171 defined (__PPC__) || defined (__PPC) || defined (PPC)
172 # define PLATFORM_BYTE_ORDER GMQCC_BYTE_ORDER_BIG
174 # define PLATFORM_BYTE_ORDER -1
178 #define GMQCC_ARRAY_COUNT(X) (sizeof(X) / sizeof((X)[0]))
181 void stat_info (void);
182 char *stat_mem_strdup (const char *, size_t, const char *, bool);
183 void *stat_mem_reallocate(void *, size_t, size_t, const char *, const char *);
184 void stat_mem_deallocate(void *);
185 void *stat_mem_allocate (size_t, size_t, const char *, const char *);
187 #define mem_a(SIZE) stat_mem_allocate ((SIZE), __LINE__, __FILE__, #SIZE)
188 #define mem_d(PTRN) stat_mem_deallocate((void*)(PTRN))
189 #define mem_r(PTRN, SIZE) stat_mem_reallocate((void*)(PTRN), (SIZE), __LINE__, __FILE__, #SIZE)
190 #define mem_af(SIZE, FILE, LINE) stat_mem_allocate ((SIZE), (LINE), (FILE), #SIZE)
192 /* TODO: rename to mem variations */
193 #define util_strdup(SRC) stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, false)
194 #define util_strdupe(SRC) stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, true)
199 * Microsoft implements against the spec versions of ctype.h. Which
200 * means what ever the current set locale is will render the actual
201 * results of say isalpha('A') wrong for what ever retarded locale
202 * is used. Simalerly these are also implemented inefficently on
203 * some toolchains and end up becoming actual library calls. Perhaps
204 * this is why tools like yacc provide their own? Regardless implementing
205 * these as functions is equally as silly, the call overhead is not
206 * justified when this could happen on every character from an input
207 * stream. We provide our own as macros for absolute inlinability.
209 #define util_isalpha(a) ((((unsigned)(a)|32)-'a') < 26)
210 #define util_isdigit(a) (((unsigned)(a)-'0') < 10)
211 #define util_islower(a) (((unsigned)(a)-'a') < 26)
212 #define util_isupper(a) (((unsigned)(a)-'A') < 26)
213 #define util_isprint(a) (((unsigned)(a)-0x20) < 0x5F)
214 #define util_isspace(a) (((a) >= 9 && (a) <= 13) || (a) == ' ')
216 bool util_strupper (const char *);
217 bool util_strdigit (const char *);
218 void util_endianswap (void *, size_t, unsigned int);
220 size_t util_strtocmd (const char *, char *, size_t);
221 size_t util_strtononcmd (const char *, char *, size_t);
222 size_t util_optimizationtostr(const char *, char *, size_t);
224 uint16_t util_crc16(uint16_t crc, const char *data, size_t len);
226 void util_seed(uint32_t);
227 uint32_t util_rand(void);
229 int util_asprintf (char **ret, const char *fmt, ...);
230 int util_sscanf (const char *str, const char *format, ...);
231 char *util_strncpy (char *dest, const char *src, size_t n);
232 char *util_strncat (char *dest, const char *src, size_t n);
233 char *util_strcat (char *dest, const char *src);
234 const char *util_strerror(int err);
236 const struct tm *util_localtime(const time_t *timer);
237 const char *util_ctime (const time_t *timer);
239 typedef struct fs_file_s fs_file_t;
241 bool util_isatty(fs_file_t *);
242 size_t hash(const char *key);
245 * A flexible vector implementation: all vector pointers contain some
246 * data about themselfs exactly - sizeof(vector_t) behind the pointer
247 * this data is represented in the structure below. Doing this allows
248 * us to use the array [] to access individual elements from the vector
249 * opposed to using set/get methods.
255 /* can be extended now! whoot */
258 /* hidden interface */
259 void _util_vec_grow(void **a, size_t i, size_t s);
260 #define GMQCC_VEC_WILLGROW(X,Y) ( \
261 ((!(X) || vec_meta(X)->used + Y >= vec_meta(X)->allocated)) ? \
262 (void)_util_vec_grow(((void**)&(X)), (Y), sizeof(*(X))) : \
266 /* exposed interface */
267 #define vec_meta(A) (((vector_t*)((void*)A)) - 1)
268 #define vec_free(A) ((void)((A) ? (mem_d((void*)vec_meta(A)), (A) = NULL) : 0))
269 #define vec_push(A,V) (GMQCC_VEC_WILLGROW((A),1), (A)[vec_meta(A)->used++] = (V))
270 #define vec_size(A) ((A) ? vec_meta(A)->used : 0)
271 #define vec_add(A,N) (GMQCC_VEC_WILLGROW((A),(N)), vec_meta(A)->used += (N), &(A)[vec_meta(A)->used-(N)])
272 #define vec_last(A) ((A)[vec_meta(A)->used - 1])
273 #define vec_pop(A) ((void)(vec_meta(A)->used -= 1))
274 #define vec_shrinkto(A,N) ((void)(vec_meta(A)->used = (N)))
275 #define vec_shrinkby(A,N) ((void)(vec_meta(A)->used -= (N)))
276 #define vec_append(A,N,S) ((void)(memcpy(vec_add((A), (N)), (S), (N) * sizeof(*(S)))))
277 #define vec_remove(A,I,N) ((void)(memmove((A)+(I),(A)+((I)+(N)),sizeof(*(A))*(vec_meta(A)->used-(I)-(N))),vec_meta(A)->used-=(N)))
279 typedef struct hash_table_s {
281 struct hash_node_t **table;
285 * hashtable implementation:
288 * This was designed for pointers: you manage the life of the object yourself
289 * if you do use this for non-pointers please be warned that the object may not
290 * be valid if the duration of it exceeds (i.e on stack). So you need to allocate
291 * yourself, or put those in global scope to ensure duration is for the whole
294 * util_htnew(size) -- to make a new hashtable
295 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
296 * util_htget(table, key) -- to get something from the table
297 * util_htdel(table) -- to delete the table
301 * ht foo = util_htnew(1024);
303 * char *test = "hello world\n";
304 * util_htset(foo, "foo", (void*)&data);
305 * util_gtset(foo, "bar", (void*)test);
307 * printf("foo: %d, bar %s",
308 * *((int *)util_htget(foo, "foo")),
309 * ((char*)util_htget(foo, "bar"))
314 hash_table_t *util_htnew (size_t size);
315 void util_htrem (hash_table_t *ht, void (*callback)(void *data));
316 void util_htset (hash_table_t *ht, const char *key, void *value);
317 void util_htdel (hash_table_t *ht);
318 size_t util_hthash(hash_table_t *ht, const char *key);
319 void util_htseth(hash_table_t *ht, const char *key, size_t hash, void *value);
320 void util_htrmh (hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*));
321 void util_htrm (hash_table_t *ht, const char *key, void (*cb)(void*));
323 void *util_htget (hash_table_t *ht, const char *key);
324 void *util_htgeth(hash_table_t *ht, const char *key, size_t hash);
326 int util_snprintf(char *str, size_t, const char *fmt, ...);
330 #define FS_FILE_SEEK_SET 0
331 #define FS_FILE_SEEK_CUR 1
332 #define FS_FILE_SEEK_END 2
333 #define FS_FILE_EOF -1
335 typedef struct fs_dir_s fs_dir_t;
336 /*typedef struct fs_file_s fs_file_t;*/
337 typedef struct dirent fs_dirent_t;
339 void fs_file_close (fs_file_t *);
340 int fs_file_error (fs_file_t *);
341 int fs_file_getc (fs_file_t *);
342 int fs_file_printf (fs_file_t *, const char *, ...);
343 int fs_file_puts (fs_file_t *, const char *);
344 int fs_file_seek (fs_file_t *, long int, int);
345 long fs_file_tell (fs_file_t *);
346 int fs_file_flush (fs_file_t *);
348 size_t fs_file_read (void *, size_t, size_t, fs_file_t *);
349 size_t fs_file_write (const void *, size_t, size_t, fs_file_t *);
351 fs_file_t *fs_file_open (const char *, const char *);
352 int fs_file_getline(char **, size_t *, fs_file_t *);
354 int fs_dir_make (const char *);
355 fs_dir_t *fs_dir_open (const char *);
356 int fs_dir_close (fs_dir_t *);
357 fs_dirent_t *fs_dir_read (fs_dir_t *);
361 typedef struct correct_trie_s {
363 struct correct_trie_s *entries;
366 correct_trie_t* correct_trie_new(void);
373 void correct_del (correct_trie_t*, size_t **);
374 void correct_add (correct_trie_t*, size_t ***, const char *);
375 char *correct_str (correction_t *, correct_trie_t*, const char *);
376 void correct_init(correction_t *);
377 void correct_free(correction_t *);
382 /* Note: if you change the order, fix type_sizeof in ir.c */
398 TYPE_NIL , /* it's its own type / untyped */
399 TYPE_NOEXPR , /* simply invalid in expressions */
404 /* const/var qualifiers */
408 #define CV_WRONG 0x8000 /* magic number to help parsing */
410 extern const char *type_name [TYPE_COUNT];
411 extern const uint16_t type_store_instr [TYPE_COUNT];
412 extern const uint16_t field_store_instr[TYPE_COUNT];
415 * could use type_store_instr + INSTR_STOREP_F - INSTR_STORE_F
416 * but this breaks when TYPE_INTEGER is added, since with the enhanced
417 * instruction set, the old ones are left untouched, thus the _I instructions
418 * are at a seperate place.
420 extern const uint16_t type_storep_instr[TYPE_COUNT];
421 extern const uint16_t type_eq_instr [TYPE_COUNT];
422 extern const uint16_t type_ne_instr [TYPE_COUNT];
423 extern const uint16_t type_not_instr [TYPE_COUNT];
426 uint32_t offset; /* Offset in file of where data begins */
427 uint32_t length; /* Length of section (how many of) */
431 uint32_t version; /* Program version (6) */
435 prog_section_t statements; /* prog_section_statement */
436 prog_section_t defs; /* prog_section_def */
437 prog_section_t fields; /* prog_section_field */
438 prog_section_t functions; /* prog_section_function */
439 prog_section_t strings;
440 prog_section_t globals;
441 uint32_t entfield; /* Number of entity fields */
445 * Each paramater incerements by 3 since vector types hold
446 * 3 components (x,y,z).
450 #define OFS_PARM0 (OFS_RETURN+3)
451 #define OFS_PARM1 (OFS_PARM0 +3)
452 #define OFS_PARM2 (OFS_PARM1 +3)
453 #define OFS_PARM3 (OFS_PARM2 +3)
454 #define OFS_PARM4 (OFS_PARM3 +3)
455 #define OFS_PARM5 (OFS_PARM4 +3)
456 #define OFS_PARM6 (OFS_PARM5 +3)
457 #define OFS_PARM7 (OFS_PARM6 +3)
464 int16_t s1; /* signed */
465 uint16_t u1; /* unsigned */
469 int16_t s1; /* signed */
470 uint16_t u1; /* unsigned */
474 int16_t s1; /* signed */
475 uint16_t u1; /* unsigned */
479 * This is the same as the structure in darkplaces
484 * But this one is more sane to work with, and the
485 * type sizes are guranteed.
487 } prog_section_statement_t;
499 * 7 = ev_pointer -- engine only
500 * 8 = ev_bad -- engine only
505 } prog_section_both_t;
507 typedef prog_section_both_t prog_section_def_t;
508 typedef prog_section_both_t prog_section_field_t;
510 /* this is ORed to the type */
511 #define DEF_SAVEGLOBAL (1<<15)
512 #define DEF_TYPEMASK ((1<<15)-1)
515 int32_t entry; /* in statement table for instructions */
516 uint32_t firstlocal; /* First local in local table */
517 uint32_t locals; /* Total ints of params + locals */
518 uint32_t profile; /* Always zero (engine uses this) */
519 uint32_t name; /* name of function in string table */
520 uint32_t file; /* file of the source file */
521 int32_t nargs; /* number of arguments */
522 uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
523 } prog_section_function_t;
527 * These are the external instructions supported by the interperter
528 * this is what things compile to (from the C code).
534 INSTR_MUL_FV, /* NOTE: the float operands must NOT be at the same locations: A != C */
535 INSTR_MUL_VF, /* and here: B != C */
599 * Virtual instructions used by the IR
607 /* A never returning CALL.
608 * Creating this causes IR blocks to be marked as 'final'.
613 /* Emulated instructions. */
614 VINSTR_BITAND_V, /* BITAND_V must be the first emulated bitop */
627 extern const char *util_instr_str[VINSTR_END];
630 typedef float qcfloat_t;
631 typedef int32_t qcint_t;
632 typedef uint32_t qcuint_t;
635 prog_section_statement_t *statements;
638 prog_section_def_t *defs;
639 prog_section_field_t *fields;
640 prog_section_function_t *functions;
646 qcint_t string_cached_empty;
650 * A shallow copy of a lex_file to remember where which ast node
660 * code_write -- writes out the compiled file
661 * code_init -- prepares the code file
662 * code_genstrin -- generates string for code
663 * code_alloc_field -- allocated a field
664 * code_push_statement -- keeps statements and linenumbers together
665 * code_pop_statement -- keeps statements and linenumbers together
667 bool code_write (code_t *, const char *filename, const char *lno);
669 code_t *code_init (void);
670 void code_cleanup (code_t *);
671 uint32_t code_genstring (code_t *, const char *string);
672 qcint_t code_alloc_field (code_t *, size_t qcsize);
673 void code_push_statement(code_t *, prog_section_statement_t *stmt, lex_ctx_t ctx);
674 void code_pop_statement (code_t *);
696 fs_file_t *con_default_out(void);
697 fs_file_t *con_default_err(void);
699 void con_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap);
700 void con_printmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...);
701 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap);
702 void con_cprintmsg (lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...);
704 void con_close (void);
705 void con_init (void);
706 void con_reset (void);
707 void con_color (int);
708 int con_change(const char *, const char *);
709 int con_verr (const char *, va_list);
710 int con_vout (const char *, va_list);
711 int con_err (const char *, ...);
712 int con_out (const char *, ...);
714 /* error/warning interface */
715 extern size_t compile_errors;
716 extern size_t compile_Werrors;
717 extern size_t compile_warnings;
719 void /********/ compile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, ...);
720 void /********/ vcompile_error (lex_ctx_t ctx, /*LVL_ERROR*/ const char *msg, va_list ap);
721 bool GMQCC_WARN compile_warning (lex_ctx_t ctx, int warntype, const char *fmt, ...);
722 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap);
723 void compile_show_werrors(void);
729 store_local, /* local, assignable for now, should get promoted later */
730 store_param, /* parameters, they are locals with a fixed position */
731 store_value, /* unassignable */
732 store_return /* unassignable, at OFS_RETURN */
743 * Darkplaces has (or will have) a 64 bit prog loader
744 * where the 32 bit qc program is autoconverted on load.
745 * Since we may want to support that as well, let's redefine
746 * float and int here.
758 typedef char qcfloat_t_size_is_correct [sizeof(qcfloat_t) == 4 ?1:-1];
759 typedef char qcint_t_size_is_correct [sizeof(qcint_t) == 4 ?1:-1];
763 VMERR_TEMPSTRING_ALLOC,
767 #define VM_JUMPS_DEFAULT 1000000
770 #define VMXF_DEFAULT 0x0000 /* default flags - nothing */
771 #define VMXF_TRACE 0x0001 /* trace: print statements before executing */
772 #define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */
775 typedef int (*prog_builtin_t)(struct qc_program_s *prog);
780 prog_section_function_t *function;
783 typedef struct qc_program_s {
785 prog_section_statement_t *code;
786 prog_section_def_t *defs;
787 prog_section_def_t *fields;
788 prog_section_function_t *functions;
794 const char* *function_stack;
798 size_t tempstring_start;
799 size_t tempstring_at;
805 prog_builtin_t *builtins;
806 size_t builtins_count;
811 bool allowworldwrites;
814 qc_exec_stack_t *stack;
819 int argc; /* current arg count for debugging */
822 qc_program_t* prog_load (const char *filename, bool ignoreversion);
823 void prog_delete (qc_program_t *prog);
824 bool prog_exec (qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps);
825 const char* prog_getstring (qc_program_t *prog, qcint_t str);
826 prog_section_def_t* prog_entfield (qc_program_t *prog, qcint_t off);
827 prog_section_def_t* prog_getdef (qc_program_t *prog, qcint_t off);
828 qcany_t* prog_getedict (qc_program_t *prog, qcint_t e);
829 qcint_t prog_tempstring(qc_program_t *prog, const char *_str);
834 struct parser_s *parser_create (void);
835 bool parser_compile_file (struct parser_s *parser, const char *);
836 bool parser_compile_string(struct parser_s *parser, const char *, const char *, size_t);
837 bool parser_finish (struct parser_s *parser, const char *);
838 void parser_cleanup (struct parser_s *parser);
842 struct ftepp_s *ftepp_create (void);
843 bool ftepp_preprocess_file (struct ftepp_s *ftepp, const char *filename);
844 bool ftepp_preprocess_string(struct ftepp_s *ftepp, const char *name, const char *str);
845 void ftepp_finish (struct ftepp_s *ftepp);
846 const char *ftepp_get (struct ftepp_s *ftepp);
847 void ftepp_flush (struct ftepp_s *ftepp);
848 void ftepp_add_define (struct ftepp_s *ftepp, const char *source, const char *name);
849 void ftepp_add_macro (struct ftepp_s *ftepp, const char *name, const char *value);
854 /* Helpers to allow for a whole lot of flags. Otherwise we'd limit
855 * to 32 or 64 -f options...
858 size_t idx; /* index into an array of 32 bit words */
859 uint8_t bit; /* bit index for the 8 bit group idx points to */
861 #define LONGBIT(bit) { ((bit)/32), ((bit)%32) }
862 #define LONGBIT_SET(B, I) ((B).idx = (I)/32, (B).bit = ((I)%32))
864 typedef uint32_t longbit;
865 #define LONGBIT(bit) (bit)
866 #define LONGBIT_SET(B, I) ((B) = (I))
870 typedef long utf8ch_t;
871 int utf8_from(char *, utf8ch_t);
872 int utf8_to(utf8ch_t *, const unsigned char *, size_t);
880 bool opts_setflag (const char *, bool);
881 bool opts_setwarn (const char *, bool);
882 bool opts_setwerror(const char *, bool);
883 bool opts_setoptim (const char *, bool);
885 void opts_init (const char *, int, size_t);
886 void opts_set (uint32_t *, size_t, bool);
887 void opts_setoptimlevel(unsigned int);
888 void opts_ini_init (const char *);
890 /* Saner flag handling */
891 void opts_backup_non_Wall(void);
892 void opts_restore_non_Wall(void);
893 void opts_backup_non_Werror_all(void);
894 void opts_restore_non_Werror_all(void);
898 # define GMQCC_TYPE_FLAGS
899 # define GMQCC_DEFINE_FLAG(X) X,
905 # define GMQCC_TYPE_WARNS
906 # define GMQCC_DEFINE_FLAG(X) WARN_##X,
912 # define GMQCC_TYPE_OPTIMIZATIONS
913 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) OPTIM_##NAME,
919 # define GMQCC_TYPE_OPTIONS
920 # define GMQCC_DEFINE_FLAG(X) OPTION_##X,
925 extern const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1];
926 extern const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1];
927 extern const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1];
928 extern const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1];
929 extern unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
933 COMPILER_QCC, /* circa QuakeC */
934 COMPILER_FTEQCC, /* fteqcc QuakeC */
935 COMPILER_QCCX, /* qccx QuakeC */
936 COMPILER_GMQCC /* this QuakeC */
956 opt_value_t options [OPTION_COUNT];
957 uint32_t flags [1 + (COUNT_FLAGS / 32)];
958 uint32_t warn [1 + (COUNT_WARNINGS / 32)];
959 uint32_t werror [1 + (COUNT_WARNINGS / 32)];
960 uint32_t warn_backup [1 + (COUNT_WARNINGS / 32)];
961 uint32_t werror_backup[1 + (COUNT_WARNINGS / 32)];
962 uint32_t optimization [1 + (COUNT_OPTIMIZATIONS / 32)];
963 bool optimizeoff; /* True when -O0 */
966 extern opts_cmd_t opts;
968 #define OPTS_GENERIC(f,i) (!! (((f)[(i)/32]) & (1<< (unsigned)((i)%32))))
970 #define OPTS_FLAG(i) OPTS_GENERIC(opts.flags, (i))
971 #define OPTS_WARN(i) OPTS_GENERIC(opts.warn, (i))
972 #define OPTS_WERROR(i) OPTS_GENERIC(opts.werror, (i))
973 #define OPTS_OPTIMIZATION(i) OPTS_GENERIC(opts.optimization, (i))
975 #define OPTS_OPTION_DUPED(X) (opts.options[X].allocated)
976 #define OPTS_OPTION_BOOL(X) (opts.options[X].data.b)
977 #define OPTS_OPTION_U16(X) (opts.options[X].data.u16)
978 #define OPTS_OPTION_U32(X) (opts.options[X].data.u32)
979 #define OPTS_OPTION_STR(X) (opts.options[X].data.str.c)
980 #define OPTS_OPTION_DUP(X) *(OPTS_OPTION_DUPED(X)=true, &(opts.options[X].data.str.p))
982 #endif /*! GMQCC_HDR */