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 " -memchk turns on compiler memory leak check\n");
41 con_out(" -o, --output=file output file, defaults to progs.dat\n"
42 " -s filename add a progs.src file to be used\n");
43 con_out(" -E stop after preprocessing\n");
44 con_out(" -q, --quiet be less verbose\n");
45 con_out(" -config file use the specified ini file\n");
46 con_out(" -std=standard select one of the following standards\n"
47 " -std=qcc original QuakeC\n"
48 " -std=fteqcc fteqcc QuakeC\n"
49 " -std=gmqcc this compiler (default)\n");
50 con_out(" -f<flag> enable a flag\n"
51 " -fno-<flag> disable a flag\n"
52 " -fhelp list possible flags\n");
53 con_out(" -W<warning> enable a warning\n"
54 " -Wno-<warning> disable a warning\n"
55 " -Wall enable all warnings\n");
56 con_out(" -Werror treat warnings as errors\n"
57 " -Werror-<warning> treat a warning as error\n"
58 " -Wno-error-<warning> opposite of the above\n");
59 con_out(" -Whelp list possible warnings\n");
60 con_out(" -O<number> optimization level\n"
61 " -O<name> enable specific optimization\n"
62 " -Ono-<name> disable specific optimization\n"
63 " -Ohelp list optimizations\n");
64 con_out(" -force-crc=num force a specific checksum into the header\n");
65 con_out(" -state-fps=num emulate OP_STATE with the specified FPS\n");
66 con_out(" -coverage add coverage support\n");
70 /* command line parsing */
71 static bool options_witharg(int *argc_, char ***argv_, char **out) {
80 if (argc < 2) /* no parameter was provided */
89 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
93 size_t len = strlen(optname);
95 if (strncmp(argv[0]+ds, optname, len))
98 /* it's --optname, check how the parameter is supplied */
99 if (argv[0][ds+len] == '=') {
100 /* using --opt=param */
101 *out = argv[0]+ds+len+1;
105 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
108 /* using --opt param */
114 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
115 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
117 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
118 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
121 static bool options_parse(int argc, char **argv) {
126 char *memdumpcols = NULL;
128 while (!argend && argc > 1) {
136 if (argv[0][0] == '-') {
137 /* All gcc-type long options */
138 if (options_long_gcc("std", &argc, &argv, &argarg)) {
139 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
141 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
142 opts_set(opts.flags, CORRECT_LOGIC, true);
143 opts_set(opts.flags, SHORT_LOGIC, true);
144 opts_set(opts.flags, UNTYPED_NIL, true);
145 opts_set(opts.flags, VARIADIC_ARGS, true);
146 opts_set(opts.flags, FALSE_EMPTY_STRINGS, false);
147 opts_set(opts.flags, TRUE_EMPTY_STRINGS, true);
148 opts_set(opts.flags, LOOP_LABELS, true);
149 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
150 opts_set(opts.flags, INITIALIZED_NONCONSTANTS, true);
151 opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
152 opts_set(opts.werror, WARN_MISSING_RETURN_VALUES, true);
153 opts_set(opts.flags, EXPRESSIONS_FOR_BUILTINS, true);
154 opts_set(opts.warn, WARN_BREAKDEF, true);
158 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
160 } else if (!strcmp(argarg, "qcc")) {
162 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
163 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
165 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
167 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
169 opts_set(opts.flags, FTEPP, true);
170 opts_set(opts.flags, TRANSLATABLE_STRINGS, true);
171 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
172 opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
173 opts_set(opts.flags, CORRECT_TERNARY, false);
174 opts_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
175 opts_set(opts.warn, WARN_BREAKDEF, true);
177 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
179 } else if (!strcmp(argarg, "qccx")) {
181 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
182 OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
185 con_out("Unknown standard: %s\n", argarg);
190 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
192 OPTS_OPTION_BOOL(OPTION_FORCECRC) = true;
193 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
196 if (options_long_gcc("state-fps", &argc, &argv, &argarg)) {
197 OPTS_OPTION_U32(OPTION_STATE_FPS) = strtol(argarg, NULL, 0);
198 opts_set(opts.flags, EMULATE_STATE, true);
201 if (options_long_gcc("config", &argc, &argv, &argarg)) {
205 if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
206 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
209 if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
210 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
214 /* show defaults (like pathscale) */
215 if (!strcmp(argv[0]+1, "show-defaults")) {
216 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
220 memset(buffer, 0, sizeof(buffer));
221 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
223 con_out("-f%s ", buffer);
225 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
229 memset(buffer, 0, sizeof(buffer));
230 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
231 con_out("-W%s ", buffer);
237 if (!strcmp(argv[0]+1, "debug")) {
238 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
241 if (!strcmp(argv[0]+1, "dump")) {
242 OPTS_OPTION_BOOL(OPTION_DUMP) = true;
245 if (!strcmp(argv[0]+1, "dumpfin")) {
246 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
249 if (!strcmp(argv[0]+1, "memchk")) {
250 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
253 if (!strcmp(argv[0]+1, "nocolor")) {
257 if (!strcmp(argv[0]+1, "coverage")) {
258 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
262 switch (argv[0][1]) {
263 /* -h, show usage but exit with 0 */
267 /* break; never reached because of exit(0) */
274 OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
275 opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
278 /* debug turns on -flno */
280 opts_setflag("LNO", true);
281 OPTS_OPTION_BOOL(OPTION_G) = true;
285 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
289 if (!strlen(argv[0]+2)) {
290 con_err("expected name after -D\n");
294 if (!(argarg = strchr(argv[0] + 2, '='))) {
295 macro.name = util_strdup(argv[0]+2);
298 *argarg='\0'; /* terminate for name */
299 macro.name = util_strdup(argv[0]+2);
300 macro.value = util_strdup(argarg+1);
302 vec_push(ppems, macro);
305 /* handle all -fflags */
307 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
308 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
309 con_out("Possible flags:\n\n");
310 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
311 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
312 con_out(" -f%s\n", buffer);
316 else if (!strncmp(argv[0]+2, "NO_", 3)) {
317 if (!opts_setflag(argv[0]+5, false)) {
318 con_out("unknown flag: %s\n", argv[0]+2);
322 else if (!opts_setflag(argv[0]+2, true)) {
323 con_out("unknown flag: %s\n", argv[0]+2);
328 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
329 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
330 con_out("Possible warnings:\n");
331 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
332 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
333 con_out(" -W%s\n", buffer);
334 if (itr == WARN_DEBUG)
335 con_out(" Warnings included by -Wall:\n");
339 else if (!strcmp(argv[0]+2, "NO_ERROR") ||
340 !strcmp(argv[0]+2, "NO_ERROR_ALL"))
342 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
343 opts.werror[itr] = 0;
346 else if (!strcmp(argv[0]+2, "ERROR") ||
347 !strcmp(argv[0]+2, "ERROR_ALL"))
349 opts_backup_non_Werror_all();
350 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
351 opts.werror[itr] = 0xFFFFFFFFL;
352 opts_restore_non_Werror_all();
355 else if (!strcmp(argv[0]+2, "NONE")) {
356 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
360 else if (!strcmp(argv[0]+2, "ALL")) {
361 opts_backup_non_Wall();
362 for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
363 opts.warn[itr] = 0xFFFFFFFFL;
364 opts_restore_non_Wall();
367 else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
368 if (!opts_setwerror(argv[0]+8, true)) {
369 con_out("unknown warning: %s\n", argv[0]+2);
373 else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
374 if (!opts_setwerror(argv[0]+11, false)) {
375 con_out("unknown warning: %s\n", argv[0]+2);
379 else if (!strncmp(argv[0]+2, "NO_", 3)) {
380 if (!opts_setwarn(argv[0]+5, false)) {
381 con_out("unknown warning: %s\n", argv[0]+2);
385 else if (!opts_setwarn(argv[0]+2, true)) {
386 con_out("unknown warning: %s\n", argv[0]+2);
392 if (!options_witharg(&argc, &argv, &argarg)) {
393 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
396 if (util_isdigit(argarg[0])) {
397 uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
398 OPTS_OPTION_U32(OPTION_O) = val;
399 opts_setoptimlevel(val);
401 util_strtocmd(argarg, argarg, strlen(argarg)+1);
402 if (!strcmp(argarg, "HELP")) {
403 con_out("Possible optimizations:\n");
404 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
405 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
406 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
410 else if (!strcmp(argarg, "ALL"))
411 opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
412 else if (!strncmp(argarg, "NO_", 3)) {
413 /* constant folding cannot be turned off for obvious reasons */
414 if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
415 con_out("unknown optimization: %s\n", argarg+3);
420 if (!opts_setoptim(argarg, true)) {
421 con_out("unknown optimization: %s\n", argarg);
429 if (!options_witharg(&argc, &argv, &argarg)) {
430 con_out("option -o requires an argument: the output file name\n");
433 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
434 opts_output_wasset = true;
439 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
440 if (!options_witharg(&argc, &argv, &argarg)) {
441 con_out("option -a requires a filename %s\n",
442 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
445 item.filename = argarg;
446 vec_push(items, item);
451 /* anything following -- is considered a non-option argument */
455 /* All long options without arguments */
456 else if (!strcmp(argv[0]+2, "help")) {
460 else if (!strcmp(argv[0]+2, "version")) {
464 else if (!strcmp(argv[0]+2, "quiet")) {
465 OPTS_OPTION_BOOL(OPTION_QUIET) = true;
468 else if (!strcmp(argv[0]+2, "add-info")) {
469 OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
473 /* All long options with arguments */
474 if (options_long_witharg("output", &argc, &argv, &argarg)) {
475 OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
476 opts_output_wasset = true;
478 con_out("Unknown parameter: %s\n", argv[0]);
485 con_out("Unknown parameter: %s\n", argv[0]);
491 /* it's a QC filename */
492 item.filename = argv[0];
494 vec_push(items, item);
497 opts_ini_init(config);
501 /* returns the line number, or -1 on error */
502 static bool progs_nextline(char **out, size_t *alen, FILE *src) {
509 len = util_getline(&line, alen, src);
513 /* start at first non-blank */
514 for (start = line; util_isspace(*start); ++start) {}
515 /* end at the first non-blank */
516 for (end = start; *end && !util_isspace(*end); ++end) {}
519 /* move the actual filename to the beginning */
520 while (start != end) {
527 int main(int argc, char **argv) {
530 bool operators_free = false;
531 bool progs_src = false;
532 FILE *outfile = NULL;
533 struct parser_s *parser = NULL;
534 struct ftepp_s *ftepp = NULL;
538 opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
542 if (!options_parse(argc, argv)) {
546 if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
547 con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
551 /* the standard decides which set of operators to use */
552 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
553 operators = c_operators;
554 operator_count = GMQCC_ARRAY_COUNT(c_operators);
555 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
556 operators = fte_operators;
557 operator_count = GMQCC_ARRAY_COUNT(fte_operators);
559 operators = qcc_operators;
560 operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
563 if (operators == fte_operators) {
565 if (OPTS_FLAG(CORRECT_TERNARY)) {
567 if (operators[operator_count-2].id != opid1(',') ||
568 operators[operator_count-1].id != opid2(':','?'))
570 con_err("internal error: operator precedence table wasn't updated correctly!\n");
573 operators_free = true;
574 newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
575 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
576 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
577 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
578 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
583 if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
584 for (itr = 0; itr < COUNT_FLAGS; ++itr)
585 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
586 for (itr = 0; itr < COUNT_WARNINGS; ++itr)
587 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
589 con_out("output = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
590 con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
591 con_out("standard = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
594 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
595 if (opts_output_wasset) {
596 outfile = fopen(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
598 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
604 outfile = con_default_out();
608 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
609 if (!(parser = parser_create())) {
610 con_err("failed to initialize parser\n");
616 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
617 if (!(ftepp = ftepp_create())) {
618 con_err("failed to initialize parser\n");
625 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
626 for (itr = 0; itr < vec_size(ppems); itr++) {
627 ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
628 mem_d(ppems[itr].name);
631 if (ppems[itr].value)
632 mem_d(ppems[itr].value);
636 if (!vec_size(items)) {
640 bool hasline = false;
644 src = fopen(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
646 con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
651 while (progs_nextline(&line, &linelen, src)) {
654 if (!line[0] || (line[0] == '/' && line[1] == '/'))
658 item.filename = util_strdup(line);
660 vec_push(items, item);
661 } else if (!opts_output_wasset) {
662 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
671 if (vec_size(items)) {
672 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
673 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
675 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
676 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
679 for (itr = 0; itr < vec_size(items); ++itr) {
680 if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
681 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
683 con_out(" item: %s (%s)\n",
685 ( (items[itr].type == TYPE_QC ? "qc" :
686 (items[itr].type == TYPE_ASM ? "asm" :
687 (items[itr].type == TYPE_SRC ? "progs.src" :
691 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
693 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
697 out = ftepp_get(ftepp);
699 fprintf(outfile, "%s", out);
703 if (OPTS_FLAG(FTEPP)) {
705 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
709 data = ftepp_get(ftepp);
710 if (vec_size(data)) {
711 if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
719 if (!parser_compile_file(parser, items[itr].filename)) {
727 mem_d(items[itr].filename);
728 items[itr].filename = NULL;
734 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
735 if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
749 if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
750 if(parser) parser_cleanup(parser);
752 /* free allocated option strings */
753 for (itr = 0; itr < OPTION_COUNT; itr++)
754 if (OPTS_OPTION_DUPED(itr))
755 mem_d(OPTS_OPTION_STR(itr));
758 mem_d((void*)operators);
762 if (!retval && compile_errors)