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
27 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
28 uint32_t opts_optimization[1 + (COUNT_OPTIMIZATIONS / 32)];
30 /* counter increased in ir.c */
31 unsigned int optimization_count[COUNT_OPTIMIZATIONS];
34 const char *opts_output = "progs.dat";
35 int opts_standard = COMPILER_GMQCC;
36 bool opts_debug = false;
37 bool opts_memchk = false;
38 bool opts_dumpfin = false;
39 bool opts_dump = false;
40 bool opts_forcecrc = false;
41 bool opts_pp_only = false;
42 size_t opts_max_array_size = (1024 << 3);
44 uint16_t opts_forced_crc;
46 static bool opts_output_wasset = false;
48 /* set by the standard */
49 const oper_info *operators = NULL;
50 size_t operator_count = 0;
52 typedef struct { char *filename; int type; } argitem;
53 static argitem *items = NULL;
59 static const char *app_name;
62 con_out("usage: %s [options] [files...]", app_name);
64 " -h, --help show this help message\n"
65 " -debug turns on compiler debug messages\n"
66 " -memchk turns on compiler memory leak check\n");
67 con_out(" -o, --output=file output file, defaults to progs.dat\n"
68 " -a filename add an asm file to be assembled\n"
69 " -s filename add a progs.src file to be used\n");
70 con_out(" -E stop after preprocessing\n");
71 con_out(" -f<flag> enable a flag\n"
72 " -fno-<flag> disable a flag\n"
73 " -std standard select one of the following standards\n"
74 " -std=qcc original QuakeC\n"
75 " -std=fteqcc fteqcc QuakeC\n"
76 " -std=gmqcc this compiler (default)\n");
77 con_out(" -W<warning> enable a warning\n"
78 " -Wno-<warning> disable a warning\n"
79 " -Wall enable all warnings\n"
80 " -Werror treat warnings as errors\n");
81 con_out(" -force-crc=num force a specific checksum into the header\n");
84 " -fadjust-vector-fields\n"
85 " when assigning a vector field, its _y and _z fields also get assigned\n"
90 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
93 for (i = 0; i < listsize; ++i) {
94 if (!strcmp(name, list[i].name)) {
95 longbit lb = list[i].bit;
98 flags[lb.idx] |= (1<<(lb.bit));
100 flags[lb.idx] &= ~(1<<(lb.bit));
105 flags[0] &= ~(1<<(lb));
112 static bool options_setflag(const char *name, bool on) {
113 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
115 static bool options_setwarn(const char *name, bool on) {
116 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
118 static bool options_setoptim(const char *name, bool on) {
119 return options_setflag_all(name, on, opts_optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
122 static bool options_witharg(int *argc_, char ***argv_, char **out) {
124 char **argv = *argv_;
130 /* eat up the next */
131 if (argc < 2) /* no parameter was provided */
140 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
142 char **argv = *argv_;
144 size_t len = strlen(optname);
146 if (strncmp(argv[0]+ds, optname, len))
149 /* it's --optname, check how the parameter is supplied */
150 if (argv[0][ds+len] == '=') {
151 /* using --opt=param */
152 *out = argv[0]+ds+len+1;
156 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
159 /* using --opt param */
165 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
166 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
168 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
169 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
172 static void options_set(uint32_t *flags, size_t idx, bool on)
174 longbit lb = LONGBIT(idx);
177 flags[lb.idx] |= (1<<(lb.bit));
179 flags[lb.idx] &= ~(1<<(lb.bit));
182 flags[0] |= (1<<(lb));
184 flags[0] &= ~(1<<(lb));
188 static void set_optimizations(unsigned int level)
191 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
192 options_set(opts_optimization, i, level >= opts_opt_oflag[i]);
195 static bool options_parse(int argc, char **argv) {
199 char *redirout = (char*)stdout;
200 char *redirerr = (char*)stderr;
202 while (!argend && argc > 1) {
209 if (argv[0][0] == '-') {
210 /* All gcc-type long options */
211 if (options_long_gcc("std", &argc, &argv, &argarg)) {
212 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
213 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
214 opts_standard = COMPILER_GMQCC;
215 } else if (!strcmp(argarg, "qcc")) {
216 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
217 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
218 opts_standard = COMPILER_QCC;
219 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
220 options_set(opts_flags, FTEPP, true);
221 options_set(opts_flags, TRANSLATABLE_STRINGS, true);
222 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
223 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
224 options_set(opts_warn, WARN_TERNARY_PRECEDENCE, true);
225 options_set(opts_flags, CORRECT_TERNARY, false);
226 opts_standard = COMPILER_FTEQCC;
227 } else if (!strcmp(argarg, "qccx")) {
228 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
229 opts_standard = COMPILER_QCCX;
231 con_out("Unknown standard: %s\n", argarg);
236 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
237 opts_forcecrc = true;
238 opts_forced_crc = strtol(argarg, NULL, 0);
241 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
242 con_change(redirout, redirerr);
245 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
246 con_change(redirout, redirerr);
250 /* show defaults (like pathscale) */
251 if (!strcmp(argv[0]+1, "show-defaults")) {
254 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
258 memset(buffer, 0, sizeof(buffer));
259 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
261 con_out("-f%s ", buffer);
263 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
267 memset(buffer, 0, sizeof(buffer));
268 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
269 con_out("-W%s ", buffer);
275 if (!strcmp(argv[0]+1, "debug")) {
279 if (!strcmp(argv[0]+1, "dump")) {
283 if (!strcmp(argv[0]+1, "dumpfin")) {
287 if (!strcmp(argv[0]+1, "memchk")) {
291 if (!strcmp(argv[0]+1, "nocolor")) {
296 switch (argv[0][1]) {
297 /* -h, show usage but exit with 0 */
301 /* break; never reached because of exit(0) */
307 /* handle all -fflags */
309 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
310 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
311 con_out("Possible flags:\n");
312 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
313 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
314 con_out(" -f%s\n", buffer);
318 else if (!strncmp(argv[0]+2, "NO_", 3)) {
319 if (!options_setflag(argv[0]+5, false)) {
320 con_out("unknown flag: %s\n", argv[0]+2);
324 else if (!options_setflag(argv[0]+2, true)) {
325 con_out("unknown flag: %s\n", argv[0]+2);
330 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
331 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
332 con_out("Possible warnings:\n");
333 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
334 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
335 con_out(" -W%s\n", buffer);
339 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
343 else if (!strcmp(argv[0]+2, "ERROR")) {
347 else if (!strcmp(argv[0]+2, "NONE")) {
348 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
352 else if (!strcmp(argv[0]+2, "ALL")) {
353 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
354 opts_warn[itr] = 0xFFFFFFFFL;
357 if (!strncmp(argv[0]+2, "NO_", 3)) {
358 if (!options_setwarn(argv[0]+5, false)) {
359 con_out("unknown warning: %s\n", argv[0]+2);
363 else if (!options_setwarn(argv[0]+2, true)) {
364 con_out("unknown warning: %s\n", argv[0]+2);
370 if (!options_witharg(&argc, &argv, &argarg)) {
371 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
374 if (isdigit(argarg[0])) {
375 opts_O = atoi(argarg);
376 set_optimizations(opts_O);
378 util_strtocmd(argarg, argarg, strlen(argarg)+1);
379 if (!strcmp(argarg, "HELP")) {
380 con_out("Possible optimizations:\n");
381 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
382 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
383 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
387 else if (!strcmp(argarg, "ALL"))
388 set_optimizations(opts_O = 9999);
389 else if (!strncmp(argarg, "NO_", 3)) {
390 if (!options_setoptim(argarg+3, false)) {
391 con_out("unknown optimization: %s\n", argarg+3);
396 if (!options_setoptim(argarg, true)) {
397 con_out("unknown optimization: %s\n", argarg);
405 if (!options_witharg(&argc, &argv, &argarg)) {
406 con_out("option -o requires an argument: the output file name\n");
409 opts_output = argarg;
410 opts_output_wasset = true;
415 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
416 if (!options_witharg(&argc, &argv, &argarg)) {
417 con_out("option -a requires a filename %s\n",
418 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
421 item.filename = argarg;
422 vec_push(items, item);
427 /* anything following -- is considered a non-option argument */
431 /* All long options without arguments */
432 else if (!strcmp(argv[0]+2, "help")) {
437 /* All long options with arguments */
438 if (options_long_witharg("output", &argc, &argv, &argarg)) {
439 opts_output = argarg;
440 opts_output_wasset = true;
442 con_out("Unknown parameter: %s\n", argv[0]);
449 con_out("Unknown parameter: %s\n", argv[0]);
455 /* it's a QC filename */
456 item.filename = argv[0];
458 vec_push(items, item);
464 /* returns the line number, or -1 on error */
465 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
472 len = util_getline(&line, alen, src);
476 /* start at first non-blank */
477 for (start = line; isspace(*start); ++start) {}
478 /* end at the first non-blank */
479 for (end = start; *end && !isspace(*end); ++end) {}
482 /* move the actual filename to the beginning */
483 while (start != end) {
490 int main(int argc, char **argv) {
493 bool opts_output_free = false;
494 bool operators_free = false;
495 bool progs_src = false;
496 FILE *outfile = NULL;
501 /* default options / warn flags */
502 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
503 options_set(opts_warn, WARN_EXTENSIONS, true);
504 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
505 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
506 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
507 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
508 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
509 options_set(opts_warn, WARN_VOID_VARIABLES, true);
510 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
511 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
512 options_set(opts_warn, WARN_FRAME_MACROS, true);
513 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
514 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
515 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
516 options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
517 options_set(opts_warn, WARN_PREPROCESSOR, true);
518 options_set(opts_warn, WARN_MULTIFILE_IF, true);
519 options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
520 options_set(opts_warn, WARN_CONST_VAR, true);
521 options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
523 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
524 options_set(opts_flags, FTEPP, false);
525 options_set(opts_flags, CORRECT_TERNARY, true);
527 if (!options_parse(argc, argv)) {
531 /* the standard decides which set of operators to use */
532 if (opts_standard == COMPILER_GMQCC) {
533 operators = c_operators;
534 operator_count = c_operator_count;
535 } else if (opts_standard == COMPILER_FTEQCC) {
536 operators = fte_operators;
537 operator_count = fte_operator_count;
539 operators = qcc_operators;
540 operator_count = qcc_operator_count;
543 if (operators == fte_operators) {
545 if (OPTS_FLAG(CORRECT_TERNARY)) {
547 if (operators[operator_count-2].id != opid1(',') ||
548 operators[operator_count-1].id != opid2(':','?'))
550 con_err("internal error: operator precedence table wasn't updated correctly!\n");
553 operators_free = true;
554 newops = mem_a(sizeof(operators[0]) * operator_count);
555 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
556 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
557 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
558 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
564 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
565 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
567 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
568 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
570 con_out("output = %s\n", opts_output);
571 con_out("optimization level = %i\n", (int)opts_O);
572 con_out("standard = %i\n", opts_standard);
576 if (opts_output_wasset) {
577 outfile = util_fopen(opts_output, "wb");
579 con_err("failed to open `%s` for writing\n", opts_output);
589 if (!parser_init()) {
590 con_err("failed to initialize parser\n");
595 if (opts_pp_only || OPTS_FLAG(FTEPP)) {
597 con_err("failed to initialize parser\n");
603 util_debug("COM", "starting ...\n");
605 if (!vec_size(items)) {
612 src = util_fopen("progs.src", "rb");
614 con_err("failed to open `progs.src` for reading\n");
620 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
621 con_err("illformatted progs.src file: expected output filename in first line\n");
626 if (!opts_output_wasset) {
627 opts_output = util_strdup(line);
628 opts_output_free = true;
631 while (progs_nextline(&line, &linelen, src)) {
633 if (!line[0] || (line[0] == '/' && line[1] == '/'))
635 item.filename = util_strdup(line);
637 vec_push(items, item);
648 if (vec_size(items)) {
650 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
651 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
653 for (itr = 0; itr < vec_size(items); ++itr) {
655 con_out(" item: %s (%s)\n",
657 ( (items[itr].type == TYPE_QC ? "qc" :
658 (items[itr].type == TYPE_ASM ? "asm" :
659 (items[itr].type == TYPE_SRC ? "progs.src" :
665 if (!ftepp_preprocess_file(items[itr].filename)) {
671 fprintf(outfile, "%s", out);
675 if (OPTS_FLAG(FTEPP)) {
677 if (!ftepp_preprocess_file(items[itr].filename)) {
682 if (vec_size(data)) {
683 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
691 if (!parser_compile_file(items[itr].filename)) {
699 mem_d(items[itr].filename);
700 items[itr].filename = NULL;
706 if (!parser_finish(opts_output)) {
716 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
717 if (optimization_count[itr]) {
718 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
724 util_debug("COM", "cleaning ...\n");
731 if (opts_output_free)
732 mem_d((char*)opts_output);
734 mem_d((void*)operators);