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);
56 #elif defined(GMQCC_VERION_TYPE_DEVEL)
57 con_out("development build\n");
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 " -s filename add a progs.src file to be used\n");
69 con_out(" -E stop after preprocessing\n");
70 con_out(" -q, --quiet be less verbose\n");
71 con_out(" -config file use the specified ini file\n");
72 con_out(" -std=standard select one of the following standards\n"
73 " -std=qcc original QuakeC\n"
74 " -std=fteqcc fteqcc QuakeC\n"
75 " -std=gmqcc this compiler (default)\n");
76 con_out(" -f<flag> enable a flag\n"
77 " -fno-<flag> disable a flag\n"
78 " -fhelp list possible flags\n");
79 con_out(" -W<warning> enable a warning\n"
80 " -Wno-<warning> disable a warning\n"
81 " -Wall enable all warnings\n");
82 con_out(" -Werror treat warnings as errors\n"
83 " -Werror-<warning> treat a warning as error\n"
84 " -Wno-error-<warning> opposite of the above\n");
85 con_out(" -Whelp list possible warnings\n");
86 con_out(" -O<number> optimization level\n"
87 " -O<name> enable specific optimization\n"
88 " -Ono-<name> disable specific optimization\n"
89 " -Ohelp list optimizations\n");
90 con_out(" -force-crc=num force a specific checksum into the header\n");
94 /* command line parsing */
95 static bool options_witharg(int *argc_, char ***argv_, char **out) {
103 /* eat up the next */
104 if (argc < 2) /* no parameter was provided */
113 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
115 char **argv = *argv_;
117 size_t len = strlen(optname);
119 if (strncmp(argv[0]+ds, optname, len))
122 /* it's --optname, check how the parameter is supplied */
123 if (argv[0][ds+len] == '=') {
124 /* using --opt=param */
125 *out = argv[0]+ds+len+1;
129 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
132 /* using --opt param */
138 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
139 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
141 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
142 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
145 static bool options_parse(int argc, char **argv) {
149 char *redirout = NULL;
150 char *redirerr = 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, FALSE_EMPTY_STRINGS, false);
169 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
170 opts_set(opts.flags, LOOP_LABELS, true);
171 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
172 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
173 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
174 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
177 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
179 } else if (!strcmp(argarg, "qcc")) {
181 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
182 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
184 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
186 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
188 opts_set(opts.flags, FTEPP, true);
189 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
190 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
191 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
192 opts_set(opts.flags, CORRECT_TERNARY, false);
193 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
195 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
197 } else if (!strcmp(argarg, "qccx")) {
199 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
200 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
203 con_out("Unknown standard: %s\n", argarg);
208 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
210 OPTS_OPTION_BOOL(OPTION_FORCECRC) = true;
211 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
214 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
215 con_change(redirout, redirerr);
218 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
219 con_change(redirout, redirerr);
222 if (options_long_gcc("config", &argc, &argv, &argarg)) {
227 /* show defaults (like pathscale) */
228 if (!strcmp(argv[0]+1, "show-defaults")) {
229 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
233 memset(buffer, 0, sizeof(buffer));
234 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
236 con_out("-f%s ", buffer);
238 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
242 memset(buffer, 0, sizeof(buffer));
243 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
244 con_out("-W%s ", buffer);
250 if (!strcmp(argv[0]+1, "debug")) {
251 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
254 if (!strcmp(argv[0]+1, "dump")) {
255 OPTS_OPTION_BOOL(OPTION_DUMP) = true;
258 if (!strcmp(argv[0]+1, "dumpfin")) {
259 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
262 if (!strcmp(argv[0]+1, "memchk")) {
263 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
266 if (!strcmp(argv[0]+1, "nocolor")) {
271 switch (argv[0][1]) {
272 /* -h, show usage but exit with 0 */
276 /* break; never reached because of exit(0) */
283 OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
284 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
287 /* debug turns on -flno */
289 opts_setflag("LNO", true);
290 OPTS_OPTION_BOOL(OPTION_G) = true;
294 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
298 if (!strlen(argv[0]+2)) {
299 con_err("expected name after -D\n");
303 if (!(argarg = strchr(argv[0] + 2, '='))) {
304 macro.name = util_strdup(argv[0]+2);
307 *argarg='\0'; /* terminate for name */
308 macro.name = util_strdup(argv[0]+2);
309 macro.value = util_strdup(argarg+1);
311 vec_push(ppems, macro);
314 /* handle all -fflags */
316 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
317 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
318 con_out("Possible flags:\n\n");
319 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
320 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
321 con_out(" -f%s\n", buffer);
325 else if (!strncmp(argv[0]+2, "NO_", 3)) {
326 if (!opts_setflag(argv[0]+5, false)) {
327 con_out("unknown flag: %s\n", argv[0]+2);
331 else if (!opts_setflag(argv[0]+2, true)) {
332 con_out("unknown flag: %s\n", argv[0]+2);
337 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
338 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
339 con_out("Possible warnings:\n");
340 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
341 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
342 con_out(" -W%s\n", buffer);
343 if (itr == WARN_DEBUG)
344 con_out(" Warnings included by -Wall:\n");
348 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
349 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
351 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
352 opts.werror[itr] = 0;
355 else if (!strcmp(argv[0]+2, "ERROR") ||
356 !strcmp(argv[0]+2, "ERROR_ALL"))
358 opts_backup_non_Werror_all();
359 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
360 opts.werror[itr] = 0xFFFFFFFFL;
361 opts_restore_non_Werror_all();
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 opts_backup_non_Wall();
371 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
372 opts.warn[itr] = 0xFFFFFFFFL;
373 opts_restore_non_Wall();
376 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
377 if (!opts_setwerror(argv[0]+8, true)) {
378 con_out("unknown warning: %s\n", argv[0]+2);
382 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
383 if (!opts_setwerror(argv[0]+11, false)) {
384 con_out("unknown warning: %s\n", argv[0]+2);
388 else if (!strncmp(argv[0]+2, "NO_", 3)) {
389 if (!opts_setwarn(argv[0]+5, false)) {
390 con_out("unknown warning: %s\n", argv[0]+2);
394 else if (!opts_setwarn(argv[0]+2, true)) {
395 con_out("unknown warning: %s\n", argv[0]+2);
401 if (!options_witharg(&argc, &argv, &argarg)) {
402 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
405 if (isdigit(argarg[0])) {
406 uint32_t val = atoi(argarg);
407 OPTS_OPTION_U32(OPTION_O) = val;
408 opts_setoptimlevel(val);
410 util_strtocmd(argarg, argarg, strlen(argarg)+1);
411 if (!strcmp(argarg, "HELP")) {
412 con_out("Possible optimizations:\n");
413 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
414 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
415 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
419 else if (!strcmp(argarg, "ALL"))
420 opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
421 else if (!strncmp(argarg, "NO_", 3)) {
422 if (!opts_setoptim(argarg+3, false)) {
423 con_out("unknown optimization: %s\n", argarg+3);
428 if (!opts_setoptim(argarg, true)) {
429 con_out("unknown optimization: %s\n", argarg);
437 if (!options_witharg(&argc, &argv, &argarg)) {
438 con_out("option -o requires an argument: the output file name\n");
441 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
442 opts_output_wasset = true;
447 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
448 if (!options_witharg(&argc, &argv, &argarg)) {
449 con_out("option -a requires a filename %s\n",
450 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
453 item.filename = argarg;
454 vec_push(items, item);
459 /* anything following -- is considered a non-option argument */
463 /* All long options without arguments */
464 else if (!strcmp(argv[0]+2, "help")) {
468 else if (!strcmp(argv[0]+2, "version")) {
472 else if (!strcmp(argv[0]+2, "quiet")) {
473 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
476 else if (!strcmp(argv[0]+2, "correct")) {
477 OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
480 else if (!strcmp(argv[0]+2, "no-correct")) {
481 OPTS_OPTION_BOOL(OPTION_CORRECTION) = false;
484 else if (!strcmp(argv[0]+2, "add-info")) {
485 OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
489 /* All long options with arguments */
490 if (options_long_witharg("output", &argc, &argv, &argarg)) {
491 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
492 opts_output_wasset = true;
494 con_out("Unknown parameter: %s\n", argv[0]);
501 con_out("Unknown parameter: %s\n", argv[0]);
507 /* it's a QC filename */
508 item.filename = argv[0];
510 vec_push(items, item);
513 opts_ini_init(config);
517 /* returns the line number, or -1 on error */
518 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
525 len = fs_file_getline(&line, alen, src);
529 /* start at first non-blank */
530 for (start = line; isspace(*start); ++start) {}
531 /* end at the first non-blank */
532 for (end = start; *end && !isspace(*end); ++end) {}
535 /* move the actual filename to the beginning */
536 while (start != end) {
543 int main(int argc, char **argv) {
546 bool opts_output_free = false;
547 bool operators_free = false;
548 bool progs_src = false;
549 FILE *outfile = NULL;
553 opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
557 if (!options_parse(argc, argv)) {
561 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
562 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
566 /* the standard decides which set of operators to use */
567 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
568 operators = c_operators;
569 operator_count = c_operator_count;
570 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
571 operators = fte_operators;
572 operator_count = fte_operator_count;
574 operators = qcc_operators;
575 operator_count = qcc_operator_count;
578 if (operators == fte_operators) {
580 if (OPTS_FLAG(CORRECT_TERNARY)) {
582 if (operators[operator_count-2].id != opid1(',') ||
583 operators[operator_count-1].id != opid2(':','?'))
585 con_err("internal error: operator precedence table wasn't updated correctly!\n");
588 operators_free = true;
589 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
590 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
591 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
592 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
593 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
598 if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
599 for (itr = 0; itr < COUNT_FLAGS; ++itr)
600 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
601 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
602 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
604 con_out("output = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
605 con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
606 con_out("standard = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
609 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
610 if (opts_output_wasset) {
611 outfile = fs_file_open(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
613 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
619 outfile = con_default_out();
623 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
624 if (!parser_init()) {
625 con_err("failed to initialize parser\n");
631 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
633 con_err("failed to initialize parser\n");
639 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
640 type_not_instr[TYPE_STRING] = INSTR_NOT_F;
642 util_debug("COM", "starting ...\n");
645 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
646 for (itr = 0; itr < vec_size(ppems); itr++) {
647 ftepp_add_macro(ppems[itr].name, ppems[itr].value);
648 mem_d(ppems[itr].name);
651 if (ppems[itr].value)
652 mem_d(ppems[itr].value);
656 if (!vec_size(items)) {
663 src = fs_file_open("progs.src", "rb");
665 con_err("failed to open `progs.src` for reading\n");
671 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
672 con_err("illformatted progs.src file: expected output filename in first line\n");
677 if (!opts_output_wasset) {
678 OPTS_OPTION_STR(OPTION_OUTPUT) = util_strdup(line);
679 opts_output_free = true;
682 while (progs_nextline(&line, &linelen, src)) {
684 if (!line[0] || (line[0] == '/' && line[1] == '/'))
686 item.filename = util_strdup(line);
688 vec_push(items, item);
699 if (vec_size(items)) {
700 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
701 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
703 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
704 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
706 for (itr = 0; itr < vec_size(items); ++itr) {
707 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
708 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
710 con_out(" item: %s (%s)\n",
712 ( (items[itr].type == TYPE_QC ? "qc" :
713 (items[itr].type == TYPE_ASM ? "asm" :
714 (items[itr].type == TYPE_SRC ? "progs.src" :
718 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
720 if (!ftepp_preprocess_file(items[itr].filename)) {
726 fs_file_printf(outfile, "%s", out);
730 if (OPTS_FLAG(FTEPP)) {
732 if (!ftepp_preprocess_file(items[itr].filename)) {
737 if (vec_size(data)) {
738 if (!parser_compile_string(items[itr].filename, data, vec_size(data))) {
746 if (!parser_compile_file(items[itr].filename)) {
754 mem_d(items[itr].filename);
755 items[itr].filename = NULL;
760 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
761 if (!parser_finish(OPTS_OPTION_STR(OPTION_OUTPUT))) {
769 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
770 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
772 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
773 if (opts_optimizationcount[itr]) {
774 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)opts_optimizationcount[itr]);
780 util_debug("COM", "cleaning ...\n");
786 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
788 if (opts_output_free)
789 mem_d(OPTS_OPTION_STR(OPTION_OUTPUT));
791 mem_d((void*)operators);