2 * Copyright (C) 2012, 2013
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
28 /* TODO: cleanup this whole file .. it's a fuckign mess */
30 /* set by the standard */
31 const oper_info *operators = NULL;
32 size_t operator_count = 0;
33 static bool opts_output_wasset = false;
35 typedef struct { char *filename; int type; } argitem;
36 typedef struct { char *name; char *value; } ppitem;
37 static argitem *items = NULL;
38 static ppitem *ppems = NULL;
44 static const char *app_name;
46 static void version() {
47 con_out("GMQCC %d.%d.%d Built %s %s\n",
55 con_out("git build: %s\n", GMQCC_GITINFO);
60 con_out("usage: %s [options] [files...]", app_name);
62 " -h, --help show this help message\n"
63 " -debug turns on compiler debug messages\n"
64 " -memchk turns on compiler memory leak check\n");
65 con_out(" -o, --output=file output file, defaults to progs.dat\n"
66 " -s filename add a progs.src file to be used\n");
67 con_out(" -E stop after preprocessing\n");
68 con_out(" -q, --quiet be less verbose\n");
69 con_out(" -config file use the specified ini file\n");
70 con_out(" -std=standard select one of the following standards\n"
71 " -std=qcc original QuakeC\n"
72 " -std=fteqcc fteqcc QuakeC\n"
73 " -std=gmqcc this compiler (default)\n");
74 con_out(" -f<flag> enable a flag\n"
75 " -fno-<flag> disable a flag\n"
76 " -fhelp list possible flags\n");
77 con_out(" -W<warning> enable a warning\n"
78 " -Wno-<warning> disable a warning\n"
79 " -Wall enable all warnings\n");
80 con_out(" -Werror treat warnings as errors\n"
81 " -Werror-<warning> treat a warning as error\n"
82 " -Wno-error-<warning> opposite of the above\n");
83 con_out(" -Whelp list possible warnings\n");
84 con_out(" -O<number> optimization level\n"
85 " -O<name> enable specific optimization\n"
86 " -Ono-<name> disable specific optimization\n"
87 " -Ohelp list optimizations\n");
88 con_out(" -force-crc=num force a specific checksum into the header\n");
92 /* command line parsing */
93 static bool options_witharg(int *argc_, char ***argv_, char **out) {
101 /* eat up the next */
102 if (argc < 2) /* no parameter was provided */
111 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
113 char **argv = *argv_;
115 size_t len = strlen(optname);
117 if (strncmp(argv[0]+ds, optname, len))
120 /* it's --optname, check how the parameter is supplied */
121 if (argv[0][ds+len] == '=') {
122 /* using --opt=param */
123 *out = argv[0]+ds+len+1;
127 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
130 /* using --opt param */
136 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
137 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
139 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
140 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
143 static bool options_parse(int argc, char **argv) {
147 char *redirout = NULL;
148 char *redirerr = NULL;
151 while (!argend && argc > 1) {
159 if (argv[0][0] == '-') {
160 /* All gcc-type long options */
161 if (options_long_gcc("std", &argc, &argv, &argarg)) {
162 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
164 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
165 opts_set(opts.flags, CORRECT_LOGIC, true);
166 opts_set(opts.flags, FALSE_EMPTY_STRINGS, false);
167 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
168 opts_set(opts.flags, LOOP_LABELS, true);
169 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
170 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
171 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
172 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
173 opts.standard = COMPILER_GMQCC;
175 } else if (!strcmp(argarg, "qcc")) {
177 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
178 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
179 opts.standard = COMPILER_QCC;
181 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
183 opts_set(opts.flags, FTEPP, true);
184 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
185 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
186 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
187 opts_set(opts.flags, CORRECT_TERNARY, false);
188 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
189 opts.standard = COMPILER_FTEQCC;
191 } else if (!strcmp(argarg, "qccx")) {
193 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
194 opts.standard = COMPILER_QCCX;
197 con_out("Unknown standard: %s\n", argarg);
202 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
203 opts.forcecrc = true;
204 opts.forced_crc = strtol(argarg, NULL, 0);
207 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
208 con_change(redirout, redirerr);
211 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
212 con_change(redirout, redirerr);
215 if (options_long_gcc("config", &argc, &argv, &argarg)) {
220 /* show defaults (like pathscale) */
221 if (!strcmp(argv[0]+1, "show-defaults")) {
222 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
226 memset(buffer, 0, sizeof(buffer));
227 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
229 con_out("-f%s ", buffer);
231 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
235 memset(buffer, 0, sizeof(buffer));
236 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
237 con_out("-W%s ", buffer);
243 if (!strcmp(argv[0]+1, "debug")) {
247 if (!strcmp(argv[0]+1, "dump")) {
251 if (!strcmp(argv[0]+1, "dumpfin")) {
255 if (!strcmp(argv[0]+1, "memchk")) {
259 if (!strcmp(argv[0]+1, "nocolor")) {
264 switch (argv[0][1]) {
265 /* -h, show usage but exit with 0 */
269 /* break; never reached because of exit(0) */
277 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
280 /* debug turns on -flno */
282 opts_setflag("LNO", true);
291 if (!strlen(argv[0]+2)) {
292 con_err("expected name after -D\n");
296 if (!(argarg = strchr(argv[0] + 2, '='))) {
297 macro.name = util_strdup(argv[0]+2);
300 *argarg='\0'; /* terminate for name */
301 macro.name = util_strdup(argv[0]+2);
302 macro.value = util_strdup(argarg+1);
304 vec_push(ppems, macro);
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 (!opts_setflag(argv[0]+5, false)) {
320 con_out("unknown flag: %s\n", argv[0]+2);
324 else if (!opts_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") ||
340 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
342 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
343 opts.werror[itr] = 0;
346 else if (!strcmp(argv[0]+2, "ERROR") ||
347 !strcmp(argv[0]+2, "ERROR_ALL"))
349 opts_backup_non_Werror_all();
350 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
351 opts.werror[itr] = 0xFFFFFFFFL;
352 opts_restore_non_Werror_all();
355 else if (!strcmp(argv[0]+2, "NONE")) {
356 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
360 else if (!strcmp(argv[0]+2, "ALL")) {
361 opts_backup_non_Wall();
362 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
363 opts.warn[itr] = 0xFFFFFFFFL;
364 opts_restore_non_Wall();
367 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
368 if (!opts_setwerror(argv[0]+8, true)) {
369 con_out("unknown warning: %s\n", argv[0]+2);
373 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
374 if (!opts_setwerror(argv[0]+11, false)) {
375 con_out("unknown warning: %s\n", argv[0]+2);
379 else if (!strncmp(argv[0]+2, "NO_", 3)) {
380 if (!opts_setwarn(argv[0]+5, false)) {
381 con_out("unknown warning: %s\n", argv[0]+2);
385 else if (!opts_setwarn(argv[0]+2, true)) {
386 con_out("unknown warning: %s\n", argv[0]+2);
392 if (!options_witharg(&argc, &argv, &argarg)) {
393 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
396 if (isdigit(argarg[0])) {
397 opts.O = atoi(argarg);
398 opts_setoptimlevel(opts.O);
400 util_strtocmd(argarg, argarg, strlen(argarg)+1);
401 if (!strcmp(argarg, "HELP")) {
402 con_out("Possible optimizations:\n");
403 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
404 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
405 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
409 else if (!strcmp(argarg, "ALL"))
410 opts_setoptimlevel(opts.O = 9999);
411 else if (!strncmp(argarg, "NO_", 3)) {
412 if (!opts_setoptim(argarg+3, false)) {
413 con_out("unknown optimization: %s\n", argarg+3);
418 if (!opts_setoptim(argarg, true)) {
419 con_out("unknown optimization: %s\n", argarg);
427 if (!options_witharg(&argc, &argv, &argarg)) {
428 con_out("option -o requires an argument: the output file name\n");
431 opts.output = argarg;
432 opts_output_wasset = true;
437 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
438 if (!options_witharg(&argc, &argv, &argarg)) {
439 con_out("option -a requires a filename %s\n",
440 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
443 item.filename = argarg;
444 vec_push(items, item);
449 /* anything following -- is considered a non-option argument */
453 /* All long options without arguments */
454 else if (!strcmp(argv[0]+2, "help")) {
458 else if (!strcmp(argv[0]+2, "version")) {
462 else if (!strcmp(argv[0]+2, "quiet")) {
467 /* All long options with arguments */
468 if (options_long_witharg("output", &argc, &argv, &argarg)) {
469 opts.output = argarg;
470 opts_output_wasset = true;
472 con_out("Unknown parameter: %s\n", argv[0]);
479 con_out("Unknown parameter: %s\n", argv[0]);
485 /* it's a QC filename */
486 item.filename = argv[0];
488 vec_push(items, item);
491 opts_ini_init(config);
495 /* returns the line number, or -1 on error */
496 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
503 len = file_getline(&line, alen, src);
507 /* start at first non-blank */
508 for (start = line; isspace(*start); ++start) {}
509 /* end at the first non-blank */
510 for (end = start; *end && !isspace(*end); ++end) {}
513 /* move the actual filename to the beginning */
514 while (start != end) {
521 int main(int argc, char **argv) {
524 bool opts_output_free = false;
525 bool operators_free = false;
526 bool progs_src = false;
527 FILE *outfile = NULL;
531 opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
535 if (!options_parse(argc, argv)) {
539 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
540 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
544 /* the standard decides which set of operators to use */
545 if (opts.standard == COMPILER_GMQCC) {
546 operators = c_operators;
547 operator_count = c_operator_count;
548 } else if (opts.standard == COMPILER_FTEQCC) {
549 operators = fte_operators;
550 operator_count = fte_operator_count;
552 operators = qcc_operators;
553 operator_count = qcc_operator_count;
556 if (operators == fte_operators) {
558 if (OPTS_FLAG(CORRECT_TERNARY)) {
560 if (operators[operator_count-2].id != opid1(',') ||
561 operators[operator_count-1].id != opid2(':','?'))
563 con_err("internal error: operator precedence table wasn't updated correctly!\n");
566 operators_free = true;
567 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
568 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
569 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
570 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
571 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
577 for (itr = 0; itr < COUNT_FLAGS; ++itr)
578 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
579 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
580 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
582 con_out("output = %s\n", opts.output);
583 con_out("optimization level = %d\n", opts.O);
584 con_out("standard = %i\n", opts.standard);
588 if (opts_output_wasset) {
589 outfile = file_open(opts.output, "wb");
591 con_err("failed to open `%s` for writing\n", opts.output);
597 outfile = con_default_out();
602 if (!parser_init()) {
603 con_err("failed to initialize parser\n");
609 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
611 con_err("failed to initialize parser\n");
617 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
618 type_not_instr[TYPE_STRING] = INSTR_NOT_F;
620 util_debug("COM", "starting ...\n");
623 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
624 for (itr = 0; itr < vec_size(ppems); itr++) {
625 ftepp_add_macro(ppems[itr].name, ppems[itr].value);
626 mem_d(ppems[itr].name);
629 if (ppems[itr].value)
630 mem_d(ppems[itr].value);
634 if (!vec_size(items)) {
641 src = file_open("progs.src", "rb");
643 con_err("failed to open `progs.src` for reading\n");
649 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
650 con_err("illformatted progs.src file: expected output filename in first line\n");
655 if (!opts_output_wasset) {
656 opts.output = util_strdup(line);
657 opts_output_free = true;
660 while (progs_nextline(&line, &linelen, src)) {
662 if (!line[0] || (line[0] == '/' && line[1] == '/'))
664 item.filename = util_strdup(line);
666 vec_push(items, item);
677 if (vec_size(items)) {
678 if (!opts.quiet && !opts.pp_only) {
679 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
680 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
682 for (itr = 0; itr < vec_size(items); ++itr) {
683 if (!opts.quiet && !opts.pp_only) {
684 con_out(" item: %s (%s)\n",
686 ( (items[itr].type == TYPE_QC ? "qc" :
687 (items[itr].type == TYPE_ASM ? "asm" :
688 (items[itr].type == TYPE_SRC ? "progs.src" :
694 if (!ftepp_preprocess_file(items[itr].filename)) {
700 file_printf(outfile, "%s", out);
704 if (OPTS_FLAG(FTEPP)) {
706 if (!ftepp_preprocess_file(items[itr].filename)) {
711 if (vec_size(data)) {
712 if (!parser_compile_string(items[itr].filename, data, vec_size(data))) {
720 if (!parser_compile_file(items[itr].filename)) {
728 mem_d(items[itr].filename);
729 items[itr].filename = NULL;
735 if (!parser_finish(opts.output)) {
743 if (!opts.quiet && !opts.pp_only) {
744 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
745 if (opts_optimizationcount[itr]) {
746 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)opts_optimizationcount[itr]);
752 util_debug("COM", "cleaning ...\n");
760 if (opts_output_free)
761 mem_d((char*)opts.output);
763 mem_d((void*)operators);