7 /* TODO: cleanup this whole file .. it's a fuckign mess */
9 /* set by the standard */
10 const oper_info *operators = nullptr;
11 size_t operator_count = 0;
12 static bool opts_output_wasset = false;
13 struct argitem { char *filename; int type; };
14 struct ppitem { char *name; char *value; };
15 static argitem *items = nullptr;
16 static ppitem *ppems = nullptr;
22 static const char *app_name;
24 static void version(void) {
25 con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
34 static int usage(void) {
35 con_out("usage: %s [options] [files...]", app_name);
37 " -h, --help show this help message\n"
38 " -debug turns on compiler debug messages\n");
39 con_out(" -o, --output=file output file, defaults to progs.dat\n"
40 " -s filename add a progs.src file to be used\n");
41 con_out(" -E stop after preprocessing\n");
42 con_out(" -q, --quiet be less verbose\n");
43 con_out(" -config file use the specified ini file\n");
44 con_out(" -std=standard select one of the following standards\n"
45 " -std=qcc original QuakeC\n"
46 " -std=fteqcc fteqcc QuakeC\n"
47 " -std=gmqcc this compiler (default)\n");
48 con_out(" -f<flag> enable a flag\n"
49 " -fno-<flag> disable a flag\n"
50 " -fhelp list possible flags\n");
51 con_out(" -W<warning> enable a warning\n"
52 " -Wno-<warning> disable a warning\n"
53 " -Wall enable all warnings\n");
54 con_out(" -Werror treat warnings as errors\n"
55 " -Werror-<warning> treat a warning as error\n"
56 " -Wno-error-<warning> opposite of the above\n");
57 con_out(" -Whelp list possible warnings\n");
58 con_out(" -O<number> optimization level\n"
59 " -O<name> enable specific optimization\n"
60 " -Ono-<name> disable specific optimization\n"
61 " -Ohelp list optimizations\n");
62 con_out(" -force-crc=num force a specific checksum into the header\n");
63 con_out(" -state-fps=num emulate OP_STATE with the specified FPS\n");
64 con_out(" -coverage add coverage support\n");
68 /* command line parsing */
69 static bool options_witharg(int *argc_, char ***argv_, char **out) {
78 if (argc < 2) /* no parameter was provided */
87 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
91 size_t len = strlen(optname);
93 if (strncmp(argv[0]+ds, optname, len))
96 /* it's --optname, check how the parameter is supplied */
97 if (argv[0][ds+len] == '=') {
98 /* using --opt=param */
99 *out = argv[0]+ds+len+1;
103 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
106 /* using --opt param */
112 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
113 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
115 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
116 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
119 static bool options_parse(int argc, char **argv) {
123 char *config = nullptr;
125 while (!argend && argc > 1) {
133 if (argv[0][0] == '-') {
134 /* All gcc-type long options */
135 if (options_long_gcc("std", &argc, &argv, &argarg)) {
136 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
138 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
139 opts_set(opts.flags, CORRECT_LOGIC, true);
140 opts_set(opts.flags, SHORT_LOGIC, true);
141 opts_set(opts.flags, UNTYPED_NIL, true);
142 opts_set(opts.flags, VARIADIC_ARGS, true);
143 opts_set(opts.flags, FALSE_EMPTY_STRINGS, false);
144 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
145 opts_set(opts.flags, LOOP_LABELS, true);
146 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
147 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
148 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
149 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
150 opts_set(opts.flags, EXPRESSIONS_FOR_BUILTINS, true);
151 opts_set(opts.warn, WARN_BREAKDEF, true);
155 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
157 } else if (!strcmp(argarg, "qcc")) {
159 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
160 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
162 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
164 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
166 opts_set(opts.flags, FTEPP, true);
167 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
168 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
169 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
170 opts_set(opts.flags, CORRECT_TERNARY, false);
171 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
172 opts_set(opts.warn, WARN_BREAKDEF, true);
174 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
176 } else if (!strcmp(argarg, "qccx")) {
178 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
179 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
182 con_out("Unknown standard: %s\n", argarg);
187 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
189 OPTS_OPTION_BOOL(OPTION_FORCECRC) = true;
190 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, nullptr, 0);
193 if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
194 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, nullptr, 0);
195 opts_set(opts.flags, EMULATE_STATE, true);
198 if (options_long_gcc("config", &argc, &argv, &argarg)) {
202 if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
203 OPTS_OPTION_STR(OPTION_PROGSRC) = 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")) {
231 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
234 if (!strcmp(argv[0]+1, "dump")) {
235 OPTS_OPTION_BOOL(OPTION_DUMP) = true;
238 if (!strcmp(argv[0]+1, "dumpfin")) {
239 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
242 if (!strcmp(argv[0]+1, "nocolor")) {
246 if (!strcmp(argv[0]+1, "coverage")) {
247 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
251 switch (argv[0][1]) {
252 /* -h, show usage but exit with 0 */
256 /* break; never reached because of exit(0) */
263 OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
264 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
267 /* debug turns on -flno */
269 opts_setflag("LNO", true);
270 OPTS_OPTION_BOOL(OPTION_G) = true;
274 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
278 if (!strlen(argv[0]+2)) {
279 con_err("expected name after -D\n");
283 if (!(argarg = strchr(argv[0] + 2, '='))) {
284 macro.name = util_strdup(argv[0]+2);
285 macro.value = nullptr;
287 *argarg='\0'; /* terminate for name */
288 macro.name = util_strdup(argv[0]+2);
289 macro.value = util_strdup(argarg+1);
291 vec_push(ppems, macro);
294 /* handle all -fflags */
296 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
297 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
298 con_out("Possible flags:\n\n");
299 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
300 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
301 con_out(" -f%s\n", buffer);
305 else if (!strncmp(argv[0]+2, "NO_", 3)) {
306 if (!opts_setflag(argv[0]+5, false)) {
307 con_out("unknown flag: %s\n", argv[0]+2);
311 else if (!opts_setflag(argv[0]+2, true)) {
312 con_out("unknown flag: %s\n", argv[0]+2);
317 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
318 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
319 con_out("Possible warnings:\n");
320 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
321 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
322 con_out(" -W%s\n", buffer);
323 if (itr == WARN_DEBUG)
324 con_out(" Warnings included by -Wall:\n");
328 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
329 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
331 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
332 opts.werror[itr] = 0;
335 else if (!strcmp(argv[0]+2, "ERROR") ||
336 !strcmp(argv[0]+2, "ERROR_ALL"))
338 opts_backup_non_Werror_all();
339 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
340 opts.werror[itr] = 0xFFFFFFFFL;
341 opts_restore_non_Werror_all();
344 else if (!strcmp(argv[0]+2, "NONE")) {
345 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
349 else if (!strcmp(argv[0]+2, "ALL")) {
350 opts_backup_non_Wall();
351 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
352 opts.warn[itr] = 0xFFFFFFFFL;
353 opts_restore_non_Wall();
356 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
357 if (!opts_setwerror(argv[0]+8, true)) {
358 con_out("unknown warning: %s\n", argv[0]+2);
362 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
363 if (!opts_setwerror(argv[0]+11, false)) {
364 con_out("unknown warning: %s\n", argv[0]+2);
368 else if (!strncmp(argv[0]+2, "NO_", 3)) {
369 if (!opts_setwarn(argv[0]+5, false)) {
370 con_out("unknown warning: %s\n", argv[0]+2);
374 else if (!opts_setwarn(argv[0]+2, true)) {
375 con_out("unknown warning: %s\n", argv[0]+2);
381 if (!options_witharg(&argc, &argv, &argarg)) {
382 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
385 if (util_isdigit(argarg[0])) {
386 uint32_t val = (uint32_t)strtol(argarg, nullptr, 10);
387 OPTS_OPTION_U32(OPTION_O) = val;
388 opts_setoptimlevel(val);
390 util_strtocmd(argarg, argarg, strlen(argarg)+1);
391 if (!strcmp(argarg, "HELP")) {
392 con_out("Possible optimizations:\n");
393 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
394 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
395 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
399 else if (!strcmp(argarg, "ALL"))
400 opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
401 else if (!strncmp(argarg, "NO_", 3)) {
402 /* constant folding cannot be turned off for obvious reasons */
403 if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
404 con_out("unknown optimization: %s\n", argarg+3);
409 if (!opts_setoptim(argarg, true)) {
410 con_out("unknown optimization: %s\n", argarg);
418 if (!options_witharg(&argc, &argv, &argarg)) {
419 con_out("option -o requires an argument: the output file name\n");
422 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
423 opts_output_wasset = true;
428 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
429 if (!options_witharg(&argc, &argv, &argarg)) {
430 con_out("option -a requires a filename %s\n",
431 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
434 item.filename = argarg;
435 vec_push(items, item);
440 /* anything following -- is considered a non-option argument */
444 /* All long options without arguments */
445 else if (!strcmp(argv[0]+2, "help")) {
449 else if (!strcmp(argv[0]+2, "version")) {
453 else if (!strcmp(argv[0]+2, "quiet")) {
454 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
457 else if (!strcmp(argv[0]+2, "add-info")) {
458 OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
462 /* All long options with arguments */
463 if (options_long_witharg("output", &argc, &argv, &argarg)) {
464 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
465 opts_output_wasset = true;
467 con_out("Unknown parameter: %s\n", argv[0]);
474 con_out("Unknown parameter: %s\n", argv[0]);
480 /* it's a QC filename */
481 item.filename = argv[0];
483 vec_push(items, item);
486 opts_ini_init(config);
490 /* returns the line number, or -1 on error */
491 static bool progs_nextline(char **out, size_t *alen, FILE *src) {
498 len = util_getline(&line, alen, src);
502 /* start at first non-blank */
503 for (start = line; util_isspace(*start); ++start) {}
504 /* end at the first non-blank */
505 for (end = start; *end && !util_isspace(*end); ++end) {}
508 /* move the actual filename to the beginning */
509 while (start != end) {
516 int main(int argc, char **argv) {
519 bool operators_free = false;
520 bool progs_src = false;
521 FILE *outfile = nullptr;
522 parser_t *parser = nullptr;
523 ftepp_t *ftepp = nullptr;
527 opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
531 if (!options_parse(argc, argv)) {
535 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
536 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
540 /* the standard decides which set of operators to use */
541 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
542 operators = c_operators;
543 operator_count = GMQCC_ARRAY_COUNT(c_operators);
544 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
545 operators = fte_operators;
546 operator_count = GMQCC_ARRAY_COUNT(fte_operators);
548 operators = qcc_operators;
549 operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
552 if (operators == fte_operators) {
554 if (OPTS_FLAG(CORRECT_TERNARY)) {
556 if (operators[operator_count-2].id != opid1(',') ||
557 operators[operator_count-1].id != opid2(':','?'))
559 con_err("internal error: operator precedence table wasn't updated correctly!\n");
562 operators_free = true;
563 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
564 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
565 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
566 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
567 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
572 if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
573 for (itr = 0; itr < COUNT_FLAGS; ++itr)
574 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
575 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
576 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
578 con_out("output = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
579 con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
580 con_out("standard = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
583 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
584 if (opts_output_wasset) {
585 outfile = fopen(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
587 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
593 outfile = con_default_out();
597 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
598 if (!(parser = parser_create())) {
599 con_err("failed to initialize parser\n");
605 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
606 if (!(ftepp = ftepp_create())) {
607 con_err("failed to initialize parser\n");
614 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
615 for (itr = 0; itr < vec_size(ppems); itr++) {
616 ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
617 mem_d(ppems[itr].name);
620 if (ppems[itr].value)
621 mem_d(ppems[itr].value);
625 if (!vec_size(items)) {
627 char *line = nullptr;
629 bool hasline = false;
633 src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
635 con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
640 while (progs_nextline(&line, &linelen, src)) {
643 if (!line[0] || (line[0] == '/' && line[1] == '/'))
647 item.filename = util_strdup(line);
649 vec_push(items, item);
650 } else if (!opts_output_wasset) {
651 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
660 if (vec_size(items)) {
661 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
662 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
664 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
665 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
668 for (itr = 0; itr < vec_size(items); ++itr) {
669 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
670 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
672 con_out(" item: %s (%s)\n",
674 ( (items[itr].type == TYPE_QC ? "qc" :
675 (items[itr].type == TYPE_ASM ? "asm" :
676 (items[itr].type == TYPE_SRC ? "progs.src" :
680 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
682 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
686 out = ftepp_get(ftepp);
688 fprintf(outfile, "%s", out);
692 if (OPTS_FLAG(FTEPP)) {
694 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
698 data = ftepp_get(ftepp);
699 if (vec_size(data)) {
700 if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
708 if (!parser_compile_file(parser, items[itr].filename)) {
716 mem_d(items[itr].filename);
717 items[itr].filename = nullptr;
723 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
724 if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
738 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
739 if(parser) parser_cleanup(parser);
741 /* free allocated option strings */
742 for (itr = 0; itr < OPTION_COUNT; itr++)
743 if (OPTS_OPTION_DUPED(itr))
744 mem_d(OPTS_OPTION_STR(itr));
747 mem_d((void*)operators);
751 if (!retval && compile_errors)