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 /* TODO: cleanup this whole file .. it's a fuckign mess */
29 /* set by the standard */
30 const oper_info *operators = NULL;
31 size_t operator_count = 0;
32 static bool opts_output_wasset = false;
34 typedef struct { char *filename; int type; } argitem;
35 typedef struct { char *name; char *value; } ppitem;
36 static argitem *items = NULL;
37 static ppitem *ppems = NULL;
43 static const char *app_name;
45 static void version() {
46 con_out("GMQCC %d.%d.%d Built %s %s\n",
56 con_out("usage: %s [options] [files...]", app_name);
58 " -h, --help show this help message\n"
59 " -debug turns on compiler debug messages\n"
60 " -memchk turns on compiler memory leak check\n");
61 con_out(" -o, --output=file output file, defaults to progs.dat\n"
62 " -s filename add a progs.src file to be used\n");
63 con_out(" -E stop after preprocessing\n");
64 con_out(" -config file use the specified ini file\n");
65 con_out(" -std=standard select one of the following standards\n"
66 " -std=qcc original QuakeC\n"
67 " -std=fteqcc fteqcc QuakeC\n"
68 " -std=gmqcc this compiler (default)\n");
69 con_out(" -f<flag> enable a flag\n"
70 " -fno-<flag> disable a flag\n"
71 " -fhelp list possible flags\n");
72 con_out(" -W<warning> enable a warning\n"
73 " -Wno-<warning> disable a warning\n"
74 " -Wall enable all warnings\n");
75 con_out(" -Werror treat warnings as errors\n"
76 " -Werror-<warning> treat a warning as error\n"
77 " -Wno-error-<warning> opposite of the above\n");
78 con_out(" -Whelp list possible warnings\n");
79 con_out(" -O<number> optimization level\n"
80 " -O<name> enable specific optimization\n"
81 " -Ono-<name> disable specific optimization\n"
82 " -Ohelp list optimizations\n");
83 con_out(" -force-crc=num force a specific checksum into the header\n");
87 /* command line parsing */
88 static bool options_witharg(int *argc_, char ***argv_, char **out) {
97 if (argc < 2) /* no parameter was provided */
106 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
108 char **argv = *argv_;
110 size_t len = strlen(optname);
112 if (strncmp(argv[0]+ds, optname, len))
115 /* it's --optname, check how the parameter is supplied */
116 if (argv[0][ds+len] == '=') {
117 /* using --opt=param */
118 *out = argv[0]+ds+len+1;
122 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
125 /* using --opt param */
131 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
132 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
134 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
135 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
138 static bool options_parse(int argc, char **argv) {
142 char *redirout = NULL;
143 char *redirerr = NULL;
146 while (!argend && argc > 1) {
154 if (argv[0][0] == '-') {
155 /* All gcc-type long options */
156 if (options_long_gcc("std", &argc, &argv, &argarg)) {
157 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
159 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
160 opts.standard = COMPILER_GMQCC;
162 } else if (!strcmp(argarg, "qcc")) {
164 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
165 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
166 opts.standard = COMPILER_QCC;
168 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
170 opts_set(opts.flags, FTEPP, true);
171 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
172 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
173 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
174 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
175 opts_set(opts.flags, CORRECT_TERNARY, false);
176 opts.standard = COMPILER_FTEQCC;
178 } else if (!strcmp(argarg, "qccx")) {
180 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
181 opts.standard = COMPILER_QCCX;
184 con_out("Unknown standard: %s\n", argarg);
189 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
190 opts.forcecrc = true;
191 opts.forced_crc = strtol(argarg, NULL, 0);
194 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
195 con_change(redirout, redirerr);
198 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
199 con_change(redirout, redirerr);
202 if (options_long_gcc("config", &argc, &argv, &argarg)) {
207 /* show defaults (like pathscale) */
208 if (!strcmp(argv[0]+1, "show-defaults")) {
209 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
213 memset(buffer, 0, sizeof(buffer));
214 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
216 con_out("-f%s ", buffer);
218 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
222 memset(buffer, 0, sizeof(buffer));
223 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
224 con_out("-W%s ", buffer);
230 if (!strcmp(argv[0]+1, "debug")) {
234 if (!strcmp(argv[0]+1, "dump")) {
238 if (!strcmp(argv[0]+1, "dumpfin")) {
242 if (!strcmp(argv[0]+1, "memchk")) {
246 if (!strcmp(argv[0]+1, "nocolor")) {
251 switch (argv[0][1]) {
252 /* -h, show usage but exit with 0 */
256 /* break; never reached because of exit(0) */
264 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
267 /* debug turns on -flno */
269 opts_setflag("LNO", true);
274 if (!strlen(argv[0]+2)) {
275 con_err("expected name after -D\n");
279 if (!(argarg = strchr(argv[0] + 2, '='))) {
280 macro.name = util_strdup(argv[0]+2);
283 *argarg='\0'; /* terminate for name */
284 macro.name = util_strdup(argv[0]+2);
285 macro.value = util_strdup(argarg+1);
287 vec_push(ppems, macro);
290 /* handle all -fflags */
292 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
293 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
294 con_out("Possible flags:\n");
295 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
296 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
297 con_out(" -f%s\n", buffer);
301 else if (!strncmp(argv[0]+2, "NO_", 3)) {
302 if (!opts_setflag(argv[0]+5, false)) {
303 con_out("unknown flag: %s\n", argv[0]+2);
307 else if (!opts_setflag(argv[0]+2, true)) {
308 con_out("unknown flag: %s\n", argv[0]+2);
313 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
314 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
315 con_out("Possible warnings:\n");
316 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
317 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
318 con_out(" -W%s\n", buffer);
322 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
323 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
325 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
326 opts.werror[itr] = 0;
329 else if (!strcmp(argv[0]+2, "ERROR") ||
330 !strcmp(argv[0]+2, "ERROR_ALL"))
332 for (itr = 0; itr < sizeof(opts.werror)/sizeof(opts.werror[0]); ++itr)
333 opts.werror[itr] = 0xFFFFFFFFL;
336 else if (!strcmp(argv[0]+2, "NONE")) {
337 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
341 else if (!strcmp(argv[0]+2, "ALL")) {
342 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
343 opts.warn[itr] = 0xFFFFFFFFL;
346 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
347 if (!opts_setwerror(argv[0]+8, true)) {
348 con_out("unknown warning: %s\n", argv[0]+2);
352 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
353 if (!opts_setwerror(argv[0]+11, false)) {
354 con_out("unknown warning: %s\n", argv[0]+2);
358 else if (!strncmp(argv[0]+2, "NO_", 3)) {
359 if (!opts_setwarn(argv[0]+5, false)) {
360 con_out("unknown warning: %s\n", argv[0]+2);
364 else if (!opts_setwarn(argv[0]+2, true)) {
365 con_out("unknown warning: %s\n", argv[0]+2);
371 if (!options_witharg(&argc, &argv, &argarg)) {
372 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
375 if (isdigit(argarg[0])) {
376 opts.O = atoi(argarg);
377 opts_setoptimlevel(opts.O);
379 util_strtocmd(argarg, argarg, strlen(argarg)+1);
380 if (!strcmp(argarg, "HELP")) {
381 con_out("Possible optimizations:\n");
382 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
383 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
384 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
388 else if (!strcmp(argarg, "ALL"))
389 opts_setoptimlevel(opts.O = 9999);
390 else if (!strncmp(argarg, "NO_", 3)) {
391 if (!opts_setoptim(argarg+3, false)) {
392 con_out("unknown optimization: %s\n", argarg+3);
397 if (!opts_setoptim(argarg, true)) {
398 con_out("unknown optimization: %s\n", argarg);
406 if (!options_witharg(&argc, &argv, &argarg)) {
407 con_out("option -o requires an argument: the output file name\n");
410 opts.output = argarg;
411 opts_output_wasset = true;
416 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
417 if (!options_witharg(&argc, &argv, &argarg)) {
418 con_out("option -a requires a filename %s\n",
419 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
422 item.filename = argarg;
423 vec_push(items, item);
428 /* anything following -- is considered a non-option argument */
432 /* All long options without arguments */
433 else if (!strcmp(argv[0]+2, "help")) {
437 else if (!strcmp(argv[0]+2, "version")) {
442 /* All long options with arguments */
443 if (options_long_witharg("output", &argc, &argv, &argarg)) {
444 opts.output = argarg;
445 opts_output_wasset = true;
447 con_out("Unknown parameter: %s\n", argv[0]);
454 con_out("Unknown parameter: %s\n", argv[0]);
460 /* it's a QC filename */
461 item.filename = argv[0];
463 vec_push(items, item);
466 opts_ini_init(config);
470 /* returns the line number, or -1 on error */
471 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
478 len = file_getline(&line, alen, src);
482 /* start at first non-blank */
483 for (start = line; isspace(*start); ++start) {}
484 /* end at the first non-blank */
485 for (end = start; *end && !isspace(*end); ++end) {}
488 /* move the actual filename to the beginning */
489 while (start != end) {
496 int main(int argc, char **argv) {
499 bool opts_output_free = false;
500 bool operators_free = false;
501 bool progs_src = false;
502 FILE *outfile = NULL;
506 opts_init("progs.dat", COMPILER_GMQCC, (1024 << 3));
508 if (!options_parse(argc, argv)) {
512 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
513 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
517 /* the standard decides which set of operators to use */
518 if (opts.standard == COMPILER_GMQCC) {
519 operators = c_operators;
520 operator_count = c_operator_count;
521 } else if (opts.standard == COMPILER_FTEQCC) {
522 operators = fte_operators;
523 operator_count = fte_operator_count;
525 operators = qcc_operators;
526 operator_count = qcc_operator_count;
529 if (operators == fte_operators) {
531 if (OPTS_FLAG(CORRECT_TERNARY)) {
533 if (operators[operator_count-2].id != opid1(',') ||
534 operators[operator_count-1].id != opid2(':','?'))
536 con_err("internal error: operator precedence table wasn't updated correctly!\n");
539 operators_free = true;
540 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
541 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
542 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
543 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
544 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
550 for (itr = 0; itr < COUNT_FLAGS; ++itr)
551 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
552 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
553 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
555 con_out("output = %s\n", opts.output);
556 con_out("optimization level = %d\n", opts.O);
557 con_out("standard = %i\n", opts.standard);
561 if (opts_output_wasset) {
562 outfile = file_open(opts.output, "wb");
564 con_err("failed to open `%s` for writing\n", opts.output);
570 outfile = con_default_out();
575 if (!parser_init()) {
576 con_err("failed to initialize parser\n");
582 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
584 con_err("failed to initialize parser\n");
590 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
591 type_not_instr[TYPE_STRING] = INSTR_NOT_F;
593 util_debug("COM", "starting ...\n");
596 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
597 for (itr = 0; itr < vec_size(ppems); itr++) {
598 ftepp_add_macro(ppems[itr].name, ppems[itr].value);
599 mem_d(ppems[itr].name);
602 if (ppems[itr].value)
603 mem_d(ppems[itr].value);
607 if (!vec_size(items)) {
614 src = file_open("progs.src", "rb");
616 con_err("failed to open `progs.src` for reading\n");
622 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
623 con_err("illformatted progs.src file: expected output filename in first line\n");
628 if (!opts_output_wasset) {
629 opts.output = util_strdup(line);
630 opts_output_free = true;
633 while (progs_nextline(&line, &linelen, src)) {
635 if (!line[0] || (line[0] == '/' && line[1] == '/'))
637 item.filename = util_strdup(line);
639 vec_push(items, item);
650 if (vec_size(items)) {
652 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
653 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
655 for (itr = 0; itr < vec_size(items); ++itr) {
657 con_out(" item: %s (%s)\n",
659 ( (items[itr].type == TYPE_QC ? "qc" :
660 (items[itr].type == TYPE_ASM ? "asm" :
661 (items[itr].type == TYPE_SRC ? "progs.src" :
667 if (!ftepp_preprocess_file(items[itr].filename)) {
673 file_printf(outfile, "%s", out);
677 if (OPTS_FLAG(FTEPP)) {
679 if (!ftepp_preprocess_file(items[itr].filename)) {
684 if (vec_size(data)) {
685 if (!parser_compile_string(items[itr].filename, data, vec_size(data))) {
693 if (!parser_compile_file(items[itr].filename)) {
701 mem_d(items[itr].filename);
702 items[itr].filename = NULL;
708 if (!parser_finish(opts.output)) {
717 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
718 if (opts_optimizationcount[itr]) {
719 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)opts_optimizationcount[itr]);
725 util_debug("COM", "cleaning ...\n");
733 if (opts_output_free)
734 mem_d((char*)opts.output);
736 mem_d((void*)operators);