2 * Copyright (C) 2012, 2013, 2014, 2015
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 /* TODO: cleanup this whole file .. it's a fuckign mess */
32 /* set by the standard */
33 const oper_info *operators = NULL;
34 size_t operator_count = 0;
35 static bool opts_output_wasset = false;
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;
48 static void version(void) {
49 con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
58 static int usage(void) {
59 con_out("usage: %s [options] [files...]", app_name);
61 " -h, --help show this help message\n"
62 " -debug turns on compiler debug messages\n"
63 " -memchk turns on compiler memory leak check\n");
64 con_out(" -o, --output=file output file, defaults to progs.dat\n"
65 " -s filename add a progs.src file to be used\n");
66 con_out(" -E stop after preprocessing\n");
67 con_out(" -q, --quiet be less verbose\n");
68 con_out(" -config file use the specified ini file\n");
69 con_out(" -std=standard select one of the following standards\n"
70 " -std=qcc original QuakeC\n"
71 " -std=fteqcc fteqcc QuakeC\n"
72 " -std=gmqcc this compiler (default)\n");
73 con_out(" -f<flag> enable a flag\n"
74 " -fno-<flag> disable a flag\n"
75 " -fhelp list possible flags\n");
76 con_out(" -W<warning> enable a warning\n"
77 " -Wno-<warning> disable a warning\n"
78 " -Wall enable all warnings\n");
79 con_out(" -Werror treat warnings as errors\n"
80 " -Werror-<warning> treat a warning as error\n"
81 " -Wno-error-<warning> opposite of the above\n");
82 con_out(" -Whelp list possible warnings\n");
83 con_out(" -O<number> optimization level\n"
84 " -O<name> enable specific optimization\n"
85 " -Ono-<name> disable specific optimization\n"
86 " -Ohelp list optimizations\n");
87 con_out(" -force-crc=num force a specific checksum into the header\n");
88 con_out(" -state-fps=num emulate OP_STATE with the specified FPS\n");
89 con_out(" -coverage add coverage support\n");
93 /* command line parsing */
94 static bool options_witharg(int *argc_, char ***argv_, char **out) {
102 /* eat up the next */
103 if (argc < 2) /* no parameter was provided */
112 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
114 char **argv = *argv_;
116 size_t len = strlen(optname);
118 if (strncmp(argv[0]+ds, optname, len))
121 /* it's --optname, check how the parameter is supplied */
122 if (argv[0][ds+len] == '=') {
123 /* using --opt=param */
124 *out = argv[0]+ds+len+1;
128 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
131 /* using --opt param */
137 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
138 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
140 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
141 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
144 static bool options_parse(int argc, char **argv) {
148 char *redirout = NULL;
149 char *redirerr = NULL;
151 char *memdumpcols = NULL;
153 while (!argend && argc > 1) {
161 if (argv[0][0] == '-') {
162 /* All gcc-type long options */
163 if (options_long_gcc("std", &argc, &argv, &argarg)) {
164 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
166 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
167 opts_set(opts.flags, CORRECT_LOGIC, true);
168 opts_set(opts.flags, SHORT_LOGIC, true);
169 opts_set(opts.flags, UNTYPED_NIL, true);
170 opts_set(opts.flags, VARIADIC_ARGS, true);
171 opts_set(opts.flags, FALSE_EMPTY_STRINGS, false);
172 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
173 opts_set(opts.flags, LOOP_LABELS, true);
174 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
175 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
176 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
177 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
178 opts_set(opts.flags, EXPRESSIONS_FOR_BUILTINS, true);
179 opts_set(opts.warn, WARN_BREAKDEF, true);
183 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
185 } else if (!strcmp(argarg, "qcc")) {
187 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
188 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
190 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
192 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
194 opts_set(opts.flags, FTEPP, true);
195 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
196 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
197 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
198 opts_set(opts.flags, CORRECT_TERNARY, false);
199 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
200 opts_set(opts.warn, WARN_BREAKDEF, true);
202 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
204 } else if (!strcmp(argarg, "qccx")) {
206 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
207 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
210 con_out("Unknown standard: %s\n", argarg);
215 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
217 OPTS_OPTION_BOOL(OPTION_FORCECRC) = true;
218 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
221 if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
222 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
223 opts_set(opts.flags, EMULATE_STATE, true);
226 if (options_long_gcc("config", &argc, &argv, &argarg)) {
230 if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
231 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
234 if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
235 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
239 /* show defaults (like pathscale) */
240 if (!strcmp(argv[0]+1, "show-defaults")) {
241 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
245 memset(buffer, 0, sizeof(buffer));
246 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
248 con_out("-f%s ", buffer);
250 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
254 memset(buffer, 0, sizeof(buffer));
255 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
256 con_out("-W%s ", buffer);
262 if (!strcmp(argv[0]+1, "debug")) {
263 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
266 if (!strcmp(argv[0]+1, "dump")) {
267 OPTS_OPTION_BOOL(OPTION_DUMP) = true;
270 if (!strcmp(argv[0]+1, "dumpfin")) {
271 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
274 if (!strcmp(argv[0]+1, "memchk")) {
275 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
278 if (!strcmp(argv[0]+1, "nocolor")) {
282 if (!strcmp(argv[0]+1, "coverage")) {
283 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
287 switch (argv[0][1]) {
288 /* -h, show usage but exit with 0 */
292 /* break; never reached because of exit(0) */
299 OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
300 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
303 /* debug turns on -flno */
305 opts_setflag("LNO", true);
306 OPTS_OPTION_BOOL(OPTION_G) = true;
310 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
314 if (!strlen(argv[0]+2)) {
315 con_err("expected name after -D\n");
319 if (!(argarg = strchr(argv[0] + 2, '='))) {
320 macro.name = util_strdup(argv[0]+2);
323 *argarg='\0'; /* terminate for name */
324 macro.name = util_strdup(argv[0]+2);
325 macro.value = util_strdup(argarg+1);
327 vec_push(ppems, macro);
330 /* handle all -fflags */
332 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
333 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
334 con_out("Possible flags:\n\n");
335 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
336 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
337 con_out(" -f%s\n", buffer);
341 else if (!strncmp(argv[0]+2, "NO_", 3)) {
342 if (!opts_setflag(argv[0]+5, false)) {
343 con_out("unknown flag: %s\n", argv[0]+2);
347 else if (!opts_setflag(argv[0]+2, true)) {
348 con_out("unknown flag: %s\n", argv[0]+2);
353 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
354 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
355 con_out("Possible warnings:\n");
356 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
357 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
358 con_out(" -W%s\n", buffer);
359 if (itr == WARN_DEBUG)
360 con_out(" Warnings included by -Wall:\n");
364 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
365 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
367 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
368 opts.werror[itr] = 0;
371 else if (!strcmp(argv[0]+2, "ERROR") ||
372 !strcmp(argv[0]+2, "ERROR_ALL"))
374 opts_backup_non_Werror_all();
375 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
376 opts.werror[itr] = 0xFFFFFFFFL;
377 opts_restore_non_Werror_all();
380 else if (!strcmp(argv[0]+2, "NONE")) {
381 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
385 else if (!strcmp(argv[0]+2, "ALL")) {
386 opts_backup_non_Wall();
387 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
388 opts.warn[itr] = 0xFFFFFFFFL;
389 opts_restore_non_Wall();
392 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
393 if (!opts_setwerror(argv[0]+8, true)) {
394 con_out("unknown warning: %s\n", argv[0]+2);
398 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
399 if (!opts_setwerror(argv[0]+11, false)) {
400 con_out("unknown warning: %s\n", argv[0]+2);
404 else if (!strncmp(argv[0]+2, "NO_", 3)) {
405 if (!opts_setwarn(argv[0]+5, false)) {
406 con_out("unknown warning: %s\n", argv[0]+2);
410 else if (!opts_setwarn(argv[0]+2, true)) {
411 con_out("unknown warning: %s\n", argv[0]+2);
417 if (!options_witharg(&argc, &argv, &argarg)) {
418 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
421 if (util_isdigit(argarg[0])) {
422 uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
423 OPTS_OPTION_U32(OPTION_O) = val;
424 opts_setoptimlevel(val);
426 util_strtocmd(argarg, argarg, strlen(argarg)+1);
427 if (!strcmp(argarg, "HELP")) {
428 con_out("Possible optimizations:\n");
429 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
430 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
431 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
435 else if (!strcmp(argarg, "ALL"))
436 opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
437 else if (!strncmp(argarg, "NO_", 3)) {
438 /* constant folding cannot be turned off for obvious reasons */
439 if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
440 con_out("unknown optimization: %s\n", argarg+3);
445 if (!opts_setoptim(argarg, true)) {
446 con_out("unknown optimization: %s\n", argarg);
454 if (!options_witharg(&argc, &argv, &argarg)) {
455 con_out("option -o requires an argument: the output file name\n");
458 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
459 opts_output_wasset = true;
464 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
465 if (!options_witharg(&argc, &argv, &argarg)) {
466 con_out("option -a requires a filename %s\n",
467 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
470 item.filename = argarg;
471 vec_push(items, item);
476 /* anything following -- is considered a non-option argument */
480 /* All long options without arguments */
481 else if (!strcmp(argv[0]+2, "help")) {
485 else if (!strcmp(argv[0]+2, "version")) {
489 else if (!strcmp(argv[0]+2, "quiet")) {
490 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
493 else if (!strcmp(argv[0]+2, "add-info")) {
494 OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
498 /* All long options with arguments */
499 if (options_long_witharg("output", &argc, &argv, &argarg)) {
500 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
501 opts_output_wasset = true;
503 con_out("Unknown parameter: %s\n", argv[0]);
510 con_out("Unknown parameter: %s\n", argv[0]);
516 /* it's a QC filename */
517 item.filename = argv[0];
519 vec_push(items, item);
522 opts_ini_init(config);
526 /* returns the line number, or -1 on error */
527 static bool progs_nextline(char **out, size_t *alen, FILE *src) {
534 len = util_getline(&line, alen, src);
538 /* start at first non-blank */
539 for (start = line; util_isspace(*start); ++start) {}
540 /* end at the first non-blank */
541 for (end = start; *end && !util_isspace(*end); ++end) {}
544 /* move the actual filename to the beginning */
545 while (start != end) {
552 int main(int argc, char **argv) {
555 bool operators_free = false;
556 bool progs_src = false;
557 FILE *outfile = NULL;
558 struct parser_s *parser = NULL;
559 struct ftepp_s *ftepp = NULL;
563 opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
567 if (!options_parse(argc, argv)) {
571 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
572 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
576 /* the standard decides which set of operators to use */
577 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
578 operators = c_operators;
579 operator_count = GMQCC_ARRAY_COUNT(c_operators);
580 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
581 operators = fte_operators;
582 operator_count = GMQCC_ARRAY_COUNT(fte_operators);
584 operators = qcc_operators;
585 operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
588 if (operators == fte_operators) {
590 if (OPTS_FLAG(CORRECT_TERNARY)) {
592 if (operators[operator_count-2].id != opid1(',') ||
593 operators[operator_count-1].id != opid2(':','?'))
595 con_err("internal error: operator precedence table wasn't updated correctly!\n");
598 operators_free = true;
599 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
600 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
601 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
602 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
603 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
608 if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
609 for (itr = 0; itr < COUNT_FLAGS; ++itr)
610 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
611 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
612 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
614 con_out("output = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
615 con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
616 con_out("standard = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
619 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
620 if (opts_output_wasset) {
621 outfile = fopen(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
623 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
629 outfile = con_default_out();
633 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
634 if (!(parser = parser_create())) {
635 con_err("failed to initialize parser\n");
641 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
642 if (!(ftepp = ftepp_create())) {
643 con_err("failed to initialize parser\n");
650 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
651 for (itr = 0; itr < vec_size(ppems); itr++) {
652 ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
653 mem_d(ppems[itr].name);
656 if (ppems[itr].value)
657 mem_d(ppems[itr].value);
661 if (!vec_size(items)) {
665 bool hasline = false;
669 src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
671 con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
676 while (progs_nextline(&line, &linelen, src)) {
679 if (!line[0] || (line[0] == '/' && line[1] == '/'))
683 item.filename = util_strdup(line);
685 vec_push(items, item);
686 } else if (!opts_output_wasset) {
687 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
696 if (vec_size(items)) {
697 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
698 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
700 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
701 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
704 for (itr = 0; itr < vec_size(items); ++itr) {
705 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
706 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
708 con_out(" item: %s (%s)\n",
710 ( (items[itr].type == TYPE_QC ? "qc" :
711 (items[itr].type == TYPE_ASM ? "asm" :
712 (items[itr].type == TYPE_SRC ? "progs.src" :
716 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
718 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
722 out = ftepp_get(ftepp);
724 fprintf(outfile, "%s", out);
728 if (OPTS_FLAG(FTEPP)) {
730 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
734 data = ftepp_get(ftepp);
735 if (vec_size(data)) {
736 if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
744 if (!parser_compile_file(parser, items[itr].filename)) {
752 mem_d(items[itr].filename);
753 items[itr].filename = NULL;
759 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
760 if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
774 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
775 if(parser) parser_cleanup(parser);
777 /* free allocated option strings */
778 for (itr = 0; itr < OPTION_COUNT; itr++)
779 if (OPTS_OPTION_DUPED(itr))
780 mem_d(OPTS_OPTION_STR(itr));
783 mem_d((void*)operators);
787 if (!retval && compile_errors)