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 static argitem *items = NULL;
44 static const char *app_name;
47 con_out("usage: %s [options] [files...]", app_name);
49 " -h, --help show this help message\n"
50 " -debug turns on compiler debug messages\n"
51 " -memchk turns on compiler memory leak check\n");
52 con_out(" -o, --output=file output file, defaults to progs.dat\n"
53 " -s filename add a progs.src file to be used\n");
54 con_out(" -E stop after preprocessing\n");
55 con_out(" -std=standard select one of the following standards\n"
56 " -std=qcc original QuakeC\n"
57 " -std=fteqcc fteqcc QuakeC\n"
58 " -std=gmqcc this compiler (default)\n");
59 con_out(" -f<flag> enable a flag\n"
60 " -fno-<flag> disable a flag\n"
61 " -fhelp list possible flags\n");
62 con_out(" -W<warning> enable a warning\n"
63 " -Wno-<warning> disable a warning\n"
64 " -Wall enable all warnings\n"
65 " -Werror treat warnings as errors\n");
66 con_out(" -Whelp list possible warnings\n");
67 con_out(" -O<number> optimization level\n"
68 " -O<name> enable specific optimization\n"
69 " -Ono-<name> disable specific optimization\n"
70 " -Ohelp list optimizations\n");
71 con_out(" -force-crc=num force a specific checksum into the header\n");
75 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
78 for (i = 0; i < listsize; ++i) {
79 if (!strcmp(name, list[i].name)) {
80 longbit lb = list[i].bit;
83 flags[lb.idx] |= (1<<(lb.bit));
85 flags[lb.idx] &= ~(1<<(lb.bit));
90 flags[0] &= ~(1<<(lb));
97 static bool options_setflag(const char *name, bool on) {
98 return options_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
100 static bool options_setwarn(const char *name, bool on) {
101 return options_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
103 static bool options_setoptim(const char *name, bool on) {
104 return options_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
107 static bool options_witharg(int *argc_, char ***argv_, char **out) {
109 char **argv = *argv_;
115 /* eat up the next */
116 if (argc < 2) /* no parameter was provided */
125 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
127 char **argv = *argv_;
129 size_t len = strlen(optname);
131 if (strncmp(argv[0]+ds, optname, len))
134 /* it's --optname, check how the parameter is supplied */
135 if (argv[0][ds+len] == '=') {
136 /* using --opt=param */
137 *out = argv[0]+ds+len+1;
141 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
144 /* using --opt param */
150 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
151 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
153 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
154 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
157 void options_set(uint32_t *flags, size_t idx, bool on)
159 longbit lb = LONGBIT(idx);
162 flags[lb.idx] |= (1<<(lb.bit));
164 flags[lb.idx] &= ~(1<<(lb.bit));
167 flags[0] |= (1<<(lb));
169 flags[0] &= ~(1<<(lb));
173 static void set_optimizations(unsigned int level)
176 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
177 options_set(opts.optimization, i, level >= opts_opt_oflag[i]);
180 static bool options_parse(int argc, char **argv) {
184 char *redirout = (char*)stdout;
185 char *redirerr = (char*)stderr;
187 while (!argend && argc > 1) {
194 if (argv[0][0] == '-') {
195 /* All gcc-type long options */
196 if (options_long_gcc("std", &argc, &argv, &argarg)) {
197 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
198 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
199 opts.standard = COMPILER_GMQCC;
200 } else if (!strcmp(argarg, "qcc")) {
201 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
202 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
203 opts.standard = COMPILER_QCC;
204 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
205 options_set(opts.flags, FTEPP, true);
206 options_set(opts.flags, TRANSLATABLE_STRINGS, true);
207 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
208 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
209 options_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
210 options_set(opts.flags, CORRECT_TERNARY, false);
211 opts.standard = COMPILER_FTEQCC;
212 } else if (!strcmp(argarg, "qccx")) {
213 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
214 opts.standard = COMPILER_QCCX;
216 con_out("Unknown standard: %s\n", argarg);
221 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
222 opts.forcecrc = true;
223 opts.forced_crc = strtol(argarg, NULL, 0);
226 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
227 con_change(redirout, redirerr);
230 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
231 con_change(redirout, redirerr);
235 /* show defaults (like pathscale) */
236 if (!strcmp(argv[0]+1, "show-defaults")) {
239 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
243 memset(buffer, 0, sizeof(buffer));
244 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
246 con_out("-f%s ", buffer);
248 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
252 memset(buffer, 0, sizeof(buffer));
253 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
254 con_out("-W%s ", buffer);
260 if (!strcmp(argv[0]+1, "debug")) {
264 if (!strcmp(argv[0]+1, "dump")) {
268 if (!strcmp(argv[0]+1, "dumpfin")) {
272 if (!strcmp(argv[0]+1, "memchk")) {
276 if (!strcmp(argv[0]+1, "nocolor")) {
281 switch (argv[0][1]) {
282 /* -h, show usage but exit with 0 */
286 /* break; never reached because of exit(0) */
292 /* debug turns on -flno */
294 options_setflag("LNO", true);
297 /* handle all -fflags */
299 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
300 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
301 con_out("Possible flags:\n");
302 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
303 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
304 con_out(" -f%s\n", buffer);
308 else if (!strncmp(argv[0]+2, "NO_", 3)) {
309 if (!options_setflag(argv[0]+5, false)) {
310 con_out("unknown flag: %s\n", argv[0]+2);
314 else if (!options_setflag(argv[0]+2, true)) {
315 con_out("unknown flag: %s\n", argv[0]+2);
320 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
321 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
322 con_out("Possible warnings:\n");
323 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
324 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
325 con_out(" -W%s\n", buffer);
329 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
333 else if (!strcmp(argv[0]+2, "ERROR")) {
337 else if (!strcmp(argv[0]+2, "NONE")) {
338 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
342 else if (!strcmp(argv[0]+2, "ALL")) {
343 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
344 opts.warn[itr] = 0xFFFFFFFFL;
347 if (!strncmp(argv[0]+2, "NO_", 3)) {
348 if (!options_setwarn(argv[0]+5, false)) {
349 con_out("unknown warning: %s\n", argv[0]+2);
353 else if (!options_setwarn(argv[0]+2, true)) {
354 con_out("unknown warning: %s\n", argv[0]+2);
360 if (!options_witharg(&argc, &argv, &argarg)) {
361 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
364 if (isdigit(argarg[0])) {
365 opts.O = atoi(argarg);
366 set_optimizations(opts.O);
368 util_strtocmd(argarg, argarg, strlen(argarg)+1);
369 if (!strcmp(argarg, "HELP")) {
370 con_out("Possible optimizations:\n");
371 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
372 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
373 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
377 else if (!strcmp(argarg, "ALL"))
378 set_optimizations(opts.O = 9999);
379 else if (!strncmp(argarg, "NO_", 3)) {
380 if (!options_setoptim(argarg+3, false)) {
381 con_out("unknown optimization: %s\n", argarg+3);
386 if (!options_setoptim(argarg, true)) {
387 con_out("unknown optimization: %s\n", argarg);
395 if (!options_witharg(&argc, &argv, &argarg)) {
396 con_out("option -o requires an argument: the output file name\n");
399 opts.output = argarg;
400 opts_output_wasset = true;
405 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
406 if (!options_witharg(&argc, &argv, &argarg)) {
407 con_out("option -a requires a filename %s\n",
408 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
411 item.filename = argarg;
412 vec_push(items, item);
417 /* anything following -- is considered a non-option argument */
421 /* All long options without arguments */
422 else if (!strcmp(argv[0]+2, "help")) {
427 /* All long options with arguments */
428 if (options_long_witharg("output", &argc, &argv, &argarg)) {
429 opts.output = argarg;
430 opts_output_wasset = true;
432 con_out("Unknown parameter: %s\n", argv[0]);
439 con_out("Unknown parameter: %s\n", argv[0]);
445 /* it's a QC filename */
446 item.filename = argv[0];
448 vec_push(items, item);
454 /* returns the line number, or -1 on error */
455 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
462 len = util_getline(&line, alen, src);
466 /* start at first non-blank */
467 for (start = line; isspace(*start); ++start) {}
468 /* end at the first non-blank */
469 for (end = start; *end && !isspace(*end); ++end) {}
472 /* move the actual filename to the beginning */
473 while (start != end) {
480 int main(int argc, char **argv) {
483 bool opts_output_free = false;
484 bool operators_free = false;
485 bool progs_src = false;
486 FILE *outfile = NULL;
488 memset(&opts, 0, sizeof(opts));
489 opts.output = "progs.dat";
490 opts.standard = COMPILER_GMQCC;
491 opts.max_array_size = (1024<<3);
496 /* default options / warn flags */
497 options_set(opts.warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
498 options_set(opts.warn, WARN_EXTENSIONS, true);
499 options_set(opts.warn, WARN_FIELD_REDECLARED, true);
500 options_set(opts.warn, WARN_TOO_FEW_PARAMETERS, true);
501 options_set(opts.warn, WARN_MISSING_RETURN_VALUES, true);
502 options_set(opts.warn, WARN_USED_UNINITIALIZED, true);
503 options_set(opts.warn, WARN_LOCAL_CONSTANTS, true);
504 options_set(opts.warn, WARN_VOID_VARIABLES, true);
505 options_set(opts.warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
506 options_set(opts.warn, WARN_VARIADIC_FUNCTION, true);
507 options_set(opts.warn, WARN_FRAME_MACROS, true);
508 options_set(opts.warn, WARN_UNUSED_VARIABLE, true);
509 options_set(opts.warn, WARN_EFFECTLESS_STATEMENT, true);
510 options_set(opts.warn, WARN_END_SYS_FIELDS, true);
511 options_set(opts.warn, WARN_ASSIGN_FUNCTION_TYPES, true);
512 options_set(opts.warn, WARN_PREPROCESSOR, true);
513 options_set(opts.warn, WARN_MULTIFILE_IF, true);
514 options_set(opts.warn, WARN_DOUBLE_DECLARATION, true);
515 options_set(opts.warn, WARN_CONST_VAR, true);
516 options_set(opts.warn, WARN_MULTIBYTE_CHARACTER, true);
518 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
519 options_set(opts.flags, FTEPP, false);
520 options_set(opts.flags, CORRECT_TERNARY, true);
522 if (!options_parse(argc, argv)) {
526 /* the standard decides which set of operators to use */
527 if (opts.standard == COMPILER_GMQCC) {
528 operators = c_operators;
529 operator_count = c_operator_count;
530 } else if (opts.standard == COMPILER_FTEQCC) {
531 operators = fte_operators;
532 operator_count = fte_operator_count;
534 operators = qcc_operators;
535 operator_count = qcc_operator_count;
538 if (operators == fte_operators) {
540 if (OPTS_FLAG(CORRECT_TERNARY)) {
542 if (operators[operator_count-2].id != opid1(',') ||
543 operators[operator_count-1].id != opid2(':','?'))
545 con_err("internal error: operator precedence table wasn't updated correctly!\n");
548 operators_free = true;
549 newops = mem_a(sizeof(operators[0]) * operator_count);
550 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
551 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
552 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
553 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
559 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
560 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
562 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
563 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
565 con_out("output = %s\n", opts.output);
566 con_out("optimization level = %i\n", (int)opts.O);
567 con_out("standard = %i\n", opts.standard);
571 if (opts_output_wasset) {
572 outfile = util_fopen(opts.output, "wb");
574 con_err("failed to open `%s` for writing\n", opts.output);
584 if (!parser_init()) {
585 con_err("failed to initialize parser\n");
590 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
592 con_err("failed to initialize parser\n");
598 util_debug("COM", "starting ...\n");
600 if (!vec_size(items)) {
607 src = util_fopen("progs.src", "rb");
609 con_err("failed to open `progs.src` for reading\n");
615 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
616 con_err("illformatted progs.src file: expected output filename in first line\n");
621 if (!opts_output_wasset) {
622 opts.output = util_strdup(line);
623 opts_output_free = true;
626 while (progs_nextline(&line, &linelen, src)) {
628 if (!line[0] || (line[0] == '/' && line[1] == '/'))
630 item.filename = util_strdup(line);
632 vec_push(items, item);
643 if (vec_size(items)) {
645 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
646 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
648 for (itr = 0; itr < vec_size(items); ++itr) {
650 con_out(" item: %s (%s)\n",
652 ( (items[itr].type == TYPE_QC ? "qc" :
653 (items[itr].type == TYPE_ASM ? "asm" :
654 (items[itr].type == TYPE_SRC ? "progs.src" :
660 if (!ftepp_preprocess_file(items[itr].filename)) {
666 fprintf(outfile, "%s", out);
670 if (OPTS_FLAG(FTEPP)) {
672 if (!ftepp_preprocess_file(items[itr].filename)) {
677 if (vec_size(data)) {
678 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
686 if (!parser_compile_file(items[itr].filename)) {
694 mem_d(items[itr].filename);
695 items[itr].filename = NULL;
701 if (!parser_finish(opts.output)) {
711 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
712 if (optimization_count[itr]) {
713 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
719 util_debug("COM", "cleaning ...\n");
726 if (opts_output_free)
727 mem_d((char*)opts.output);
729 mem_d((void*)operators);