7 /* TODO: cleanup this whole file .. it's a fuckign mess */
9 /* set by the standard */
10 const oper_info *operators = NULL;
11 size_t operator_count = 0;
12 static bool opts_output_wasset = false;
14 typedef struct { char *filename; int type; } argitem;
15 typedef struct { char *name; char *value; } ppitem;
16 static argitem *items = NULL;
17 static ppitem *ppems = NULL;
23 static const char *app_name;
25 static void version(void) {
26 con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
35 static int usage(void) {
36 con_out("usage: %s [options] [files...]", app_name);
38 " -h, --help show this help message\n"
39 " -debug turns on compiler debug messages\n");
40 con_out(" -o, --output=file output file, defaults to progs.dat\n"
41 " -s filename add a progs.src file to be used\n");
42 con_out(" -E stop after preprocessing\n");
43 con_out(" -q, --quiet be less verbose\n");
44 con_out(" -config file use the specified ini file\n");
45 con_out(" -std=standard select one of the following standards\n"
46 " -std=qcc original QuakeC\n"
47 " -std=fteqcc fteqcc QuakeC\n"
48 " -std=gmqcc this compiler (default)\n");
49 con_out(" -f<flag> enable a flag\n"
50 " -fno-<flag> disable a flag\n"
51 " -fhelp list possible flags\n");
52 con_out(" -W<warning> enable a warning\n"
53 " -Wno-<warning> disable a warning\n"
54 " -Wall enable all warnings\n");
55 con_out(" -Werror treat warnings as errors\n"
56 " -Werror-<warning> treat a warning as error\n"
57 " -Wno-error-<warning> opposite of the above\n");
58 con_out(" -Whelp list possible warnings\n");
59 con_out(" -O<number> optimization level\n"
60 " -O<name> enable specific optimization\n"
61 " -Ono-<name> disable specific optimization\n"
62 " -Ohelp list optimizations\n");
63 con_out(" -force-crc=num force a specific checksum into the header\n");
64 con_out(" -state-fps=num emulate OP_STATE with the specified FPS\n");
65 con_out(" -coverage add coverage support\n");
69 /* command line parsing */
70 static bool options_witharg(int *argc_, char ***argv_, char **out) {
79 if (argc < 2) /* no parameter was provided */
88 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
92 size_t len = strlen(optname);
94 if (strncmp(argv[0]+ds, optname, len))
97 /* it's --optname, check how the parameter is supplied */
98 if (argv[0][ds+len] == '=') {
99 /* using --opt=param */
100 *out = argv[0]+ds+len+1;
104 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
107 /* using --opt param */
113 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
114 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
116 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
117 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
120 static bool options_parse(int argc, char **argv) {
126 while (!argend && argc > 1) {
134 if (argv[0][0] == '-') {
135 /* All gcc-type long options */
136 if (options_long_gcc("std", &argc, &argv, &argarg)) {
137 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
139 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
140 opts_set(opts.flags, CORRECT_LOGIC, true);
141 opts_set(opts.flags, SHORT_LOGIC, true);
142 opts_set(opts.flags, UNTYPED_NIL, true);
143 opts_set(opts.flags, VARIADIC_ARGS, true);
144 opts_set(opts.flags, FALSE_EMPTY_STRINGS, false);
145 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
146 opts_set(opts.flags, LOOP_LABELS, true);
147 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
148 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
149 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
150 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
151 opts_set(opts.flags, EXPRESSIONS_FOR_BUILTINS, true);
152 opts_set(opts.warn, WARN_BREAKDEF, true);
156 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
158 } else if (!strcmp(argarg, "qcc")) {
160 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
161 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
163 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
165 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
167 opts_set(opts.flags, FTEPP, true);
168 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
169 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
170 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
171 opts_set(opts.flags, CORRECT_TERNARY, false);
172 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
173 opts_set(opts.warn, WARN_BREAKDEF, true);
175 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
177 } else if (!strcmp(argarg, "qccx")) {
179 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
180 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
183 con_out("Unknown standard: %s\n", argarg);
188 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
190 OPTS_OPTION_BOOL(OPTION_FORCECRC) = true;
191 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
194 if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
195 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
196 opts_set(opts.flags, EMULATE_STATE, true);
199 if (options_long_gcc("config", &argc, &argv, &argarg)) {
203 if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
204 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
208 /* show defaults (like pathscale) */
209 if (!strcmp(argv[0]+1, "show-defaults")) {
210 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
214 memset(buffer, 0, sizeof(buffer));
215 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
217 con_out("-f%s ", buffer);
219 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
223 memset(buffer, 0, sizeof(buffer));
224 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
225 con_out("-W%s ", buffer);
231 if (!strcmp(argv[0]+1, "debug")) {
232 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
235 if (!strcmp(argv[0]+1, "dump")) {
236 OPTS_OPTION_BOOL(OPTION_DUMP) = true;
239 if (!strcmp(argv[0]+1, "dumpfin")) {
240 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
243 if (!strcmp(argv[0]+1, "nocolor")) {
247 if (!strcmp(argv[0]+1, "coverage")) {
248 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
252 switch (argv[0][1]) {
253 /* -h, show usage but exit with 0 */
257 /* break; never reached because of exit(0) */
264 OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
265 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
268 /* debug turns on -flno */
270 opts_setflag("LNO", true);
271 OPTS_OPTION_BOOL(OPTION_G) = true;
275 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
279 if (!strlen(argv[0]+2)) {
280 con_err("expected name after -D\n");
284 if (!(argarg = strchr(argv[0] + 2, '='))) {
285 macro.name = util_strdup(argv[0]+2);
288 *argarg='\0'; /* terminate for name */
289 macro.name = util_strdup(argv[0]+2);
290 macro.value = util_strdup(argarg+1);
292 vec_push(ppems, macro);
295 /* handle all -fflags */
297 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
298 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
299 con_out("Possible flags:\n\n");
300 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
301 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
302 con_out(" -f%s\n", buffer);
306 else if (!strncmp(argv[0]+2, "NO_", 3)) {
307 if (!opts_setflag(argv[0]+5, false)) {
308 con_out("unknown flag: %s\n", argv[0]+2);
312 else if (!opts_setflag(argv[0]+2, true)) {
313 con_out("unknown flag: %s\n", argv[0]+2);
318 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
319 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
320 con_out("Possible warnings:\n");
321 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
322 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
323 con_out(" -W%s\n", buffer);
324 if (itr == WARN_DEBUG)
325 con_out(" Warnings included by -Wall:\n");
329 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
330 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
332 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
333 opts.werror[itr] = 0;
336 else if (!strcmp(argv[0]+2, "ERROR") ||
337 !strcmp(argv[0]+2, "ERROR_ALL"))
339 opts_backup_non_Werror_all();
340 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
341 opts.werror[itr] = 0xFFFFFFFFL;
342 opts_restore_non_Werror_all();
345 else if (!strcmp(argv[0]+2, "NONE")) {
346 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
350 else if (!strcmp(argv[0]+2, "ALL")) {
351 opts_backup_non_Wall();
352 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
353 opts.warn[itr] = 0xFFFFFFFFL;
354 opts_restore_non_Wall();
357 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
358 if (!opts_setwerror(argv[0]+8, true)) {
359 con_out("unknown warning: %s\n", argv[0]+2);
363 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
364 if (!opts_setwerror(argv[0]+11, false)) {
365 con_out("unknown warning: %s\n", argv[0]+2);
369 else if (!strncmp(argv[0]+2, "NO_", 3)) {
370 if (!opts_setwarn(argv[0]+5, false)) {
371 con_out("unknown warning: %s\n", argv[0]+2);
375 else if (!opts_setwarn(argv[0]+2, true)) {
376 con_out("unknown warning: %s\n", argv[0]+2);
382 if (!options_witharg(&argc, &argv, &argarg)) {
383 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
386 if (util_isdigit(argarg[0])) {
387 uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
388 OPTS_OPTION_U32(OPTION_O) = val;
389 opts_setoptimlevel(val);
391 util_strtocmd(argarg, argarg, strlen(argarg)+1);
392 if (!strcmp(argarg, "HELP")) {
393 con_out("Possible optimizations:\n");
394 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
395 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
396 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
400 else if (!strcmp(argarg, "ALL"))
401 opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
402 else if (!strncmp(argarg, "NO_", 3)) {
403 /* constant folding cannot be turned off for obvious reasons */
404 if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
405 con_out("unknown optimization: %s\n", argarg+3);
410 if (!opts_setoptim(argarg, true)) {
411 con_out("unknown optimization: %s\n", argarg);
419 if (!options_witharg(&argc, &argv, &argarg)) {
420 con_out("option -o requires an argument: the output file name\n");
423 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
424 opts_output_wasset = true;
429 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
430 if (!options_witharg(&argc, &argv, &argarg)) {
431 con_out("option -a requires a filename %s\n",
432 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
435 item.filename = argarg;
436 vec_push(items, item);
441 /* anything following -- is considered a non-option argument */
445 /* All long options without arguments */
446 else if (!strcmp(argv[0]+2, "help")) {
450 else if (!strcmp(argv[0]+2, "version")) {
454 else if (!strcmp(argv[0]+2, "quiet")) {
455 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
458 else if (!strcmp(argv[0]+2, "add-info")) {
459 OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
463 /* All long options with arguments */
464 if (options_long_witharg("output", &argc, &argv, &argarg)) {
465 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
466 opts_output_wasset = true;
468 con_out("Unknown parameter: %s\n", argv[0]);
475 con_out("Unknown parameter: %s\n", argv[0]);
481 /* it's a QC filename */
482 item.filename = argv[0];
484 vec_push(items, item);
487 opts_ini_init(config);
491 /* returns the line number, or -1 on error */
492 static bool progs_nextline(char **out, size_t *alen, FILE *src) {
499 len = util_getline(&line, alen, src);
503 /* start at first non-blank */
504 for (start = line; util_isspace(*start); ++start) {}
505 /* end at the first non-blank */
506 for (end = start; *end && !util_isspace(*end); ++end) {}
509 /* move the actual filename to the beginning */
510 while (start != end) {
517 int main(int argc, char **argv) {
520 bool operators_free = false;
521 bool progs_src = false;
522 FILE *outfile = NULL;
523 parser_t *parser = NULL;
524 ftepp_t *ftepp = NULL;
528 opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
532 if (!options_parse(argc, argv)) {
536 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
537 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
541 /* the standard decides which set of operators to use */
542 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
543 operators = c_operators;
544 operator_count = GMQCC_ARRAY_COUNT(c_operators);
545 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
546 operators = fte_operators;
547 operator_count = GMQCC_ARRAY_COUNT(fte_operators);
549 operators = qcc_operators;
550 operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
553 if (operators == fte_operators) {
555 if (OPTS_FLAG(CORRECT_TERNARY)) {
557 if (operators[operator_count-2].id != opid1(',') ||
558 operators[operator_count-1].id != opid2(':','?'))
560 con_err("internal error: operator precedence table wasn't updated correctly!\n");
563 operators_free = true;
564 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
565 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
566 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
567 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
568 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
573 if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
574 for (itr = 0; itr < COUNT_FLAGS; ++itr)
575 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
576 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
577 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
579 con_out("output = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
580 con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
581 con_out("standard = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
584 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
585 if (opts_output_wasset) {
586 outfile = fopen(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
588 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
594 outfile = con_default_out();
598 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
599 if (!(parser = parser_create())) {
600 con_err("failed to initialize parser\n");
606 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
607 if (!(ftepp = ftepp_create())) {
608 con_err("failed to initialize parser\n");
615 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
616 for (itr = 0; itr < vec_size(ppems); itr++) {
617 ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
618 mem_d(ppems[itr].name);
621 if (ppems[itr].value)
622 mem_d(ppems[itr].value);
626 if (!vec_size(items)) {
630 bool hasline = false;
634 src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
636 con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
641 while (progs_nextline(&line, &linelen, src)) {
644 if (!line[0] || (line[0] == '/' && line[1] == '/'))
648 item.filename = util_strdup(line);
650 vec_push(items, item);
651 } else if (!opts_output_wasset) {
652 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
661 if (vec_size(items)) {
662 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
663 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
665 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
666 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
669 for (itr = 0; itr < vec_size(items); ++itr) {
670 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
671 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
673 con_out(" item: %s (%s)\n",
675 ( (items[itr].type == TYPE_QC ? "qc" :
676 (items[itr].type == TYPE_ASM ? "asm" :
677 (items[itr].type == TYPE_SRC ? "progs.src" :
681 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
683 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
687 out = ftepp_get(ftepp);
689 fprintf(outfile, "%s", out);
693 if (OPTS_FLAG(FTEPP)) {
695 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
699 data = ftepp_get(ftepp);
700 if (vec_size(data)) {
701 if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
709 if (!parser_compile_file(parser, items[itr].filename)) {
717 mem_d(items[itr].filename);
718 items[itr].filename = NULL;
724 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
725 if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
739 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
740 if(parser) parser_cleanup(parser);
742 /* free allocated option strings */
743 for (itr = 0; itr < OPTION_COUNT; itr++)
744 if (OPTS_OPTION_DUPED(itr))
745 mem_d(OPTS_OPTION_STR(itr));
748 mem_d((void*)operators);
752 if (!retval && compile_errors)