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 /* counter increased in ir.c */
28 unsigned int optimization_count[COUNT_OPTIMIZATIONS];
29 static bool opts_output_wasset = false;
33 /* set by the standard */
34 const oper_info *operators = NULL;
35 size_t operator_count = 0;
37 typedef struct { char *filename; int type; } argitem;
38 typedef struct { char *name; char *value; } ppitem;
39 static argitem *items = NULL;
40 static ppitem *ppems = NULL;
46 static const char *app_name;
49 con_out("usage: %s [options] [files...]", app_name);
51 " -h, --help show this help message\n"
52 " -debug turns on compiler debug messages\n"
53 " -memchk turns on compiler memory leak check\n");
54 con_out(" -o, --output=file output file, defaults to progs.dat\n"
55 " -s filename add a progs.src file to be used\n");
56 con_out(" -E stop after preprocessing\n");
57 con_out(" -std=standard select one of the following standards\n"
58 " -std=qcc original QuakeC\n"
59 " -std=fteqcc fteqcc QuakeC\n"
60 " -std=gmqcc this compiler (default)\n");
61 con_out(" -f<flag> enable a flag\n"
62 " -fno-<flag> disable a flag\n"
63 " -fhelp list possible flags\n");
64 con_out(" -W<warning> enable a warning\n"
65 " -Wno-<warning> disable a warning\n"
66 " -Wall enable all warnings\n"
67 " -Werror treat warnings as errors\n");
68 con_out(" -Whelp list possible warnings\n");
69 con_out(" -O<number> optimization level\n"
70 " -O<name> enable specific optimization\n"
71 " -Ono-<name> disable specific optimization\n"
72 " -Ohelp list optimizations\n");
73 con_out(" -force-crc=num force a specific checksum into the header\n");
77 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
80 for (i = 0; i < listsize; ++i) {
81 if (!strcmp(name, list[i].name)) {
82 longbit lb = list[i].bit;
85 flags[lb.idx] |= (1<<(lb.bit));
87 flags[lb.idx] &= ~(1<<(lb.bit));
92 flags[0] &= ~(1<<(lb));
99 static bool options_setflag(const char *name, bool on) {
100 return options_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
102 static bool options_setwarn(const char *name, bool on) {
103 return options_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
105 static bool options_setoptim(const char *name, bool on) {
106 return options_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
109 static bool options_witharg(int *argc_, char ***argv_, char **out) {
111 char **argv = *argv_;
117 /* eat up the next */
118 if (argc < 2) /* no parameter was provided */
127 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
129 char **argv = *argv_;
131 size_t len = strlen(optname);
133 if (strncmp(argv[0]+ds, optname, len))
136 /* it's --optname, check how the parameter is supplied */
137 if (argv[0][ds+len] == '=') {
138 /* using --opt=param */
139 *out = argv[0]+ds+len+1;
143 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
146 /* using --opt param */
152 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
153 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
155 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
156 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
159 void options_set(uint32_t *flags, size_t idx, bool on)
161 longbit lb = LONGBIT(idx);
164 flags[lb.idx] |= (1<<(lb.bit));
166 flags[lb.idx] &= ~(1<<(lb.bit));
169 flags[0] |= (1<<(lb));
171 flags[0] &= ~(1<<(lb));
175 static void set_optimizations(unsigned int level)
178 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
179 options_set(opts.optimization, i, level >= opts_opt_oflag[i]);
182 static bool options_parse(int argc, char **argv) {
186 char *redirout = (char*)stdout;
187 char *redirerr = (char*)stderr;
189 while (!argend && argc > 1) {
197 if (argv[0][0] == '-') {
198 /* All gcc-type long options */
199 if (options_long_gcc("std", &argc, &argv, &argarg)) {
200 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
201 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
202 opts.standard = COMPILER_GMQCC;
203 } else if (!strcmp(argarg, "qcc")) {
204 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
205 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
206 opts.standard = COMPILER_QCC;
207 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
208 options_set(opts.flags, FTEPP, true);
209 options_set(opts.flags, TRANSLATABLE_STRINGS, true);
210 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
211 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
212 options_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
213 options_set(opts.flags, CORRECT_TERNARY, false);
214 opts.standard = COMPILER_FTEQCC;
215 } else if (!strcmp(argarg, "qccx")) {
216 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
217 opts.standard = COMPILER_QCCX;
219 con_out("Unknown standard: %s\n", argarg);
224 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
225 opts.forcecrc = true;
226 opts.forced_crc = strtol(argarg, NULL, 0);
229 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
230 con_change(redirout, redirerr);
233 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
234 con_change(redirout, redirerr);
238 if (!strcmp(argv[0]+1, "which") ||
239 !strcmp(argv[0]+1, "version")) {
240 con_out("GMQCC %d.%d.%d Built %s %s\n",
251 /* show defaults (like pathscale) */
252 if (!strcmp(argv[0]+1, "show-defaults")) {
255 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
259 memset(buffer, 0, sizeof(buffer));
260 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
262 con_out("-f%s ", buffer);
264 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
268 memset(buffer, 0, sizeof(buffer));
269 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
270 con_out("-W%s ", buffer);
276 if (!strcmp(argv[0]+1, "debug")) {
280 if (!strcmp(argv[0]+1, "dump")) {
284 if (!strcmp(argv[0]+1, "dumpfin")) {
288 if (!strcmp(argv[0]+1, "memchk")) {
292 if (!strcmp(argv[0]+1, "nocolor")) {
297 switch (argv[0][1]) {
298 /* -h, show usage but exit with 0 */
302 /* break; never reached because of exit(0) */
308 /* debug turns on -flno */
310 options_setflag("LNO", true);
314 if (!(argarg = strchr(argv[0] + 2, '='))) {
315 con_out("missing = in -D\n");
318 *argarg='\0'; /* terminate for name */
319 macro.name = util_strdup(argarg);
320 macro.value = util_strdup(argv[0]+2);
321 vec_push(ppems, macro);
324 /* handle all -fflags */
326 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
327 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
328 con_out("Possible flags:\n");
329 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
330 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
331 con_out(" -f%s\n", buffer);
335 else if (!strncmp(argv[0]+2, "NO_", 3)) {
336 if (!options_setflag(argv[0]+5, false)) {
337 con_out("unknown flag: %s\n", argv[0]+2);
341 else if (!options_setflag(argv[0]+2, true)) {
342 con_out("unknown flag: %s\n", argv[0]+2);
347 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
348 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
349 con_out("Possible warnings:\n");
350 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
351 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
352 con_out(" -W%s\n", buffer);
356 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
360 else if (!strcmp(argv[0]+2, "ERROR")) {
364 else if (!strcmp(argv[0]+2, "NONE")) {
365 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
369 else if (!strcmp(argv[0]+2, "ALL")) {
370 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
371 opts.warn[itr] = 0xFFFFFFFFL;
374 if (!strncmp(argv[0]+2, "NO_", 3)) {
375 if (!options_setwarn(argv[0]+5, false)) {
376 con_out("unknown warning: %s\n", argv[0]+2);
380 else if (!options_setwarn(argv[0]+2, true)) {
381 con_out("unknown warning: %s\n", argv[0]+2);
387 if (!options_witharg(&argc, &argv, &argarg)) {
388 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
391 if (isdigit(argarg[0])) {
392 opts.O = atoi(argarg);
393 set_optimizations(opts.O);
395 util_strtocmd(argarg, argarg, strlen(argarg)+1);
396 if (!strcmp(argarg, "HELP")) {
397 con_out("Possible optimizations:\n");
398 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
399 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
400 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
404 else if (!strcmp(argarg, "ALL"))
405 set_optimizations(opts.O = 9999);
406 else if (!strncmp(argarg, "NO_", 3)) {
407 if (!options_setoptim(argarg+3, false)) {
408 con_out("unknown optimization: %s\n", argarg+3);
413 if (!options_setoptim(argarg, true)) {
414 con_out("unknown optimization: %s\n", argarg);
422 if (!options_witharg(&argc, &argv, &argarg)) {
423 con_out("option -o requires an argument: the output file name\n");
426 opts.output = argarg;
427 opts_output_wasset = true;
432 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
433 if (!options_witharg(&argc, &argv, &argarg)) {
434 con_out("option -a requires a filename %s\n",
435 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
438 item.filename = argarg;
439 vec_push(items, item);
444 /* anything following -- is considered a non-option argument */
448 /* All long options without arguments */
449 else if (!strcmp(argv[0]+2, "help")) {
454 /* All long options with arguments */
455 if (options_long_witharg("output", &argc, &argv, &argarg)) {
456 opts.output = argarg;
457 opts_output_wasset = true;
459 con_out("Unknown parameter: %s\n", argv[0]);
466 con_out("Unknown parameter: %s\n", argv[0]);
472 /* it's a QC filename */
473 item.filename = argv[0];
475 vec_push(items, item);
481 /* returns the line number, or -1 on error */
482 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
489 len = util_getline(&line, alen, src);
493 /* start at first non-blank */
494 for (start = line; isspace(*start); ++start) {}
495 /* end at the first non-blank */
496 for (end = start; *end && !isspace(*end); ++end) {}
499 /* move the actual filename to the beginning */
500 while (start != end) {
507 int main(int argc, char **argv) {
510 bool opts_output_free = false;
511 bool operators_free = false;
512 bool progs_src = false;
513 FILE *outfile = NULL;
515 memset(&opts, 0, sizeof(opts));
516 opts.output = "progs.dat";
517 opts.standard = COMPILER_GMQCC;
518 opts.max_array_size = (1024<<3);
523 /* default options / warn flags */
524 options_set(opts.warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
525 options_set(opts.warn, WARN_EXTENSIONS, true);
526 options_set(opts.warn, WARN_FIELD_REDECLARED, true);
527 options_set(opts.warn, WARN_TOO_FEW_PARAMETERS, true);
528 options_set(opts.warn, WARN_MISSING_RETURN_VALUES, true);
529 options_set(opts.warn, WARN_USED_UNINITIALIZED, true);
530 options_set(opts.warn, WARN_LOCAL_CONSTANTS, true);
531 options_set(opts.warn, WARN_VOID_VARIABLES, true);
532 options_set(opts.warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
533 options_set(opts.warn, WARN_VARIADIC_FUNCTION, true);
534 options_set(opts.warn, WARN_FRAME_MACROS, true);
535 options_set(opts.warn, WARN_UNUSED_VARIABLE, true);
536 options_set(opts.warn, WARN_EFFECTLESS_STATEMENT, true);
537 options_set(opts.warn, WARN_END_SYS_FIELDS, true);
538 options_set(opts.warn, WARN_ASSIGN_FUNCTION_TYPES, true);
539 options_set(opts.warn, WARN_PREPROCESSOR, true);
540 options_set(opts.warn, WARN_MULTIFILE_IF, true);
541 options_set(opts.warn, WARN_DOUBLE_DECLARATION, true);
542 options_set(opts.warn, WARN_CONST_VAR, true);
543 options_set(opts.warn, WARN_MULTIBYTE_CHARACTER, true);
545 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
546 options_set(opts.flags, FTEPP, false);
547 options_set(opts.flags, CORRECT_TERNARY, true);
549 if (!options_parse(argc, argv)) {
553 /* the standard decides which set of operators to use */
554 if (opts.standard == COMPILER_GMQCC) {
555 operators = c_operators;
556 operator_count = c_operator_count;
557 } else if (opts.standard == COMPILER_FTEQCC) {
558 operators = fte_operators;
559 operator_count = fte_operator_count;
561 operators = qcc_operators;
562 operator_count = qcc_operator_count;
565 if (operators == fte_operators) {
567 if (OPTS_FLAG(CORRECT_TERNARY)) {
569 if (operators[operator_count-2].id != opid1(',') ||
570 operators[operator_count-1].id != opid2(':','?'))
572 con_err("internal error: operator precedence table wasn't updated correctly!\n");
575 operators_free = true;
576 newops = mem_a(sizeof(operators[0]) * operator_count);
577 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
578 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
579 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
580 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
586 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
587 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
589 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
590 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
592 con_out("output = %s\n", opts.output);
593 con_out("optimization level = %i\n", (int)opts.O);
594 con_out("standard = %i\n", opts.standard);
598 if (opts_output_wasset) {
599 outfile = util_fopen(opts.output, "wb");
601 con_err("failed to open `%s` for writing\n", opts.output);
611 if (!parser_init()) {
612 con_err("failed to initialize parser\n");
618 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
620 con_err("failed to initialize parser\n");
625 for (i = 0; i < vec_size(ppems); ++i) {
626 ftepp_add_macro(ppems[i].name, ppems[i].value);
627 mem_d(ppems[i].name);
628 mem_d(ppems[i].value);
633 util_debug("COM", "starting ...\n");
635 if (!vec_size(items)) {
642 src = util_fopen("progs.src", "rb");
644 con_err("failed to open `progs.src` for reading\n");
650 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
651 con_err("illformatted progs.src file: expected output filename in first line\n");
656 if (!opts_output_wasset) {
657 opts.output = util_strdup(line);
658 opts_output_free = true;
661 while (progs_nextline(&line, &linelen, src)) {
663 if (!line[0] || (line[0] == '/' && line[1] == '/'))
665 item.filename = util_strdup(line);
667 vec_push(items, item);
678 if (vec_size(items)) {
680 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
681 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
683 for (itr = 0; itr < vec_size(items); ++itr) {
685 con_out(" item: %s (%s)\n",
687 ( (items[itr].type == TYPE_QC ? "qc" :
688 (items[itr].type == TYPE_ASM ? "asm" :
689 (items[itr].type == TYPE_SRC ? "progs.src" :
695 if (!ftepp_preprocess_file(items[itr].filename)) {
701 fprintf(outfile, "%s", out);
705 if (OPTS_FLAG(FTEPP)) {
707 if (!ftepp_preprocess_file(items[itr].filename)) {
712 if (vec_size(data)) {
713 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
721 if (!parser_compile_file(items[itr].filename)) {
729 mem_d(items[itr].filename);
730 items[itr].filename = NULL;
736 if (!parser_finish(opts.output)) {
746 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
747 if (optimization_count[itr]) {
748 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
754 util_debug("COM", "cleaning ...\n");
762 if (opts_output_free)
763 mem_d((char*)opts.output);
765 mem_d((void*)operators);