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 /* counter increased in ir.c */
28 unsigned int optimization_count[COUNT_OPTIMIZATIONS];
29 static bool opts_output_wasset = false;
33 /* set by the standard */
34 const oper_info *operators = NULL;
35 size_t operator_count = 0;
37 typedef struct { char *filename; int type; } argitem;
38 typedef struct { char *name; char *value; } ppitem;
39 static argitem *items = NULL;
40 static ppitem *ppems = NULL;
46 static const char *app_name;
48 static void version() {
49 con_out("GMQCC %d.%d.%d Built %s %s\n",
59 con_out("usage: %s [options] [files...]", app_name);
61 " -h, --help show this help message\n"
62 " -debug turns on compiler debug messages\n"
63 " -memchk turns on compiler memory leak check\n");
64 con_out(" -o, --output=file output file, defaults to progs.dat\n"
65 " -s filename add a progs.src file to be used\n");
66 con_out(" -E stop after preprocessing\n");
67 con_out(" -std=standard select one of the following standards\n"
68 " -std=qcc original QuakeC\n"
69 " -std=fteqcc fteqcc QuakeC\n"
70 " -std=gmqcc this compiler (default)\n");
71 con_out(" -f<flag> enable a flag\n"
72 " -fno-<flag> disable a flag\n"
73 " -fhelp list possible flags\n");
74 con_out(" -W<warning> enable a warning\n"
75 " -Wno-<warning> disable a warning\n"
76 " -Wall enable all warnings\n"
77 " -Werror treat warnings as errors\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 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
90 for (i = 0; i < listsize; ++i) {
91 if (!strcmp(name, list[i].name)) {
92 longbit lb = list[i].bit;
95 flags[lb.idx] |= (1<<(lb.bit));
97 flags[lb.idx] &= ~(1<<(lb.bit));
102 flags[0] &= ~(1<<(lb));
109 static bool options_setflag(const char *name, bool on) {
110 return options_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
112 static bool options_setwarn(const char *name, bool on) {
113 return options_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
115 static bool options_setoptim(const char *name, bool on) {
116 return options_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
119 static bool options_witharg(int *argc_, char ***argv_, char **out) {
121 char **argv = *argv_;
127 /* eat up the next */
128 if (argc < 2) /* no parameter was provided */
137 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
139 char **argv = *argv_;
141 size_t len = strlen(optname);
143 if (strncmp(argv[0]+ds, optname, len))
146 /* it's --optname, check how the parameter is supplied */
147 if (argv[0][ds+len] == '=') {
148 /* using --opt=param */
149 *out = argv[0]+ds+len+1;
153 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
156 /* using --opt param */
162 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
163 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
165 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
166 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
169 void options_set(uint32_t *flags, size_t idx, bool on)
171 longbit lb = LONGBIT(idx);
174 flags[lb.idx] |= (1<<(lb.bit));
176 flags[lb.idx] &= ~(1<<(lb.bit));
179 flags[0] |= (1<<(lb));
181 flags[0] &= ~(1<<(lb));
185 static void set_optimizations(unsigned int level)
188 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
189 options_set(opts.optimization, i, level >= opts_opt_oflag[i]);
192 static bool options_parse(int argc, char **argv) {
196 char *redirout = (char*)stdout;
197 char *redirerr = (char*)stderr;
199 while (!argend && argc > 1) {
207 if (argv[0][0] == '-') {
208 /* All gcc-type long options */
209 if (options_long_gcc("std", &argc, &argv, &argarg)) {
210 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
211 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
212 opts.standard = COMPILER_GMQCC;
213 } else if (!strcmp(argarg, "qcc")) {
214 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
215 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
216 opts.standard = COMPILER_QCC;
217 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
218 options_set(opts.flags, FTEPP, true);
219 options_set(opts.flags, TRANSLATABLE_STRINGS, true);
220 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
221 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
222 options_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
223 options_set(opts.flags, CORRECT_TERNARY, false);
224 opts.standard = COMPILER_FTEQCC;
225 } else if (!strcmp(argarg, "qccx")) {
226 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
227 opts.standard = COMPILER_QCCX;
229 con_out("Unknown standard: %s\n", argarg);
234 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
235 opts.forcecrc = true;
236 opts.forced_crc = strtol(argarg, NULL, 0);
239 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
240 con_change(redirout, redirerr);
243 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
244 con_change(redirout, redirerr);
248 /* show defaults (like pathscale) */
249 if (!strcmp(argv[0]+1, "show-defaults")) {
252 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
256 memset(buffer, 0, sizeof(buffer));
257 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
259 con_out("-f%s ", buffer);
261 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
265 memset(buffer, 0, sizeof(buffer));
266 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
267 con_out("-W%s ", buffer);
273 if (!strcmp(argv[0]+1, "debug")) {
277 if (!strcmp(argv[0]+1, "dump")) {
281 if (!strcmp(argv[0]+1, "dumpfin")) {
285 if (!strcmp(argv[0]+1, "memchk")) {
289 if (!strcmp(argv[0]+1, "nocolor")) {
294 switch (argv[0][1]) {
295 /* -h, show usage but exit with 0 */
299 /* break; never reached because of exit(0) */
309 /* debug turns on -flno */
311 options_setflag("LNO", true);
315 if (!strlen(argv[0]+2)) {
316 con_err("expected name after -D\n");
320 if (!(argarg = strchr(argv[0] + 2, '='))) {
321 macro.name = util_strdup(argv[0]+2);
324 *argarg='\0'; /* terminate for name */
325 macro.name = util_strdup(argv[0]+2);
326 macro.value = util_strdup(argarg+1);
328 vec_push(ppems, macro);
331 /* handle all -fflags */
333 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
334 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
335 con_out("Possible flags:\n");
336 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
337 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
338 con_out(" -f%s\n", buffer);
342 else if (!strncmp(argv[0]+2, "NO_", 3)) {
343 if (!options_setflag(argv[0]+5, false)) {
344 con_out("unknown flag: %s\n", argv[0]+2);
348 else if (!options_setflag(argv[0]+2, true)) {
349 con_out("unknown flag: %s\n", argv[0]+2);
354 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
355 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
356 con_out("Possible warnings:\n");
357 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
358 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
359 con_out(" -W%s\n", buffer);
363 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
367 else if (!strcmp(argv[0]+2, "ERROR")) {
371 else if (!strcmp(argv[0]+2, "NONE")) {
372 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
376 else if (!strcmp(argv[0]+2, "ALL")) {
377 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
378 opts.warn[itr] = 0xFFFFFFFFL;
381 if (!strncmp(argv[0]+2, "NO_", 3)) {
382 if (!options_setwarn(argv[0]+5, false)) {
383 con_out("unknown warning: %s\n", argv[0]+2);
387 else if (!options_setwarn(argv[0]+2, true)) {
388 con_out("unknown warning: %s\n", argv[0]+2);
394 if (!options_witharg(&argc, &argv, &argarg)) {
395 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
398 if (isdigit(argarg[0])) {
399 opts.O = atoi(argarg);
400 set_optimizations(opts.O);
402 util_strtocmd(argarg, argarg, strlen(argarg)+1);
403 if (!strcmp(argarg, "HELP")) {
404 con_out("Possible optimizations:\n");
405 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
406 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
407 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
411 else if (!strcmp(argarg, "ALL"))
412 set_optimizations(opts.O = 9999);
413 else if (!strncmp(argarg, "NO_", 3)) {
414 if (!options_setoptim(argarg+3, false)) {
415 con_out("unknown optimization: %s\n", argarg+3);
420 if (!options_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.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")) {
465 /* All long options with arguments */
466 if (options_long_witharg("output", &argc, &argv, &argarg)) {
467 opts.output = argarg;
468 opts_output_wasset = true;
470 con_out("Unknown parameter: %s\n", argv[0]);
477 con_out("Unknown parameter: %s\n", argv[0]);
483 /* it's a QC filename */
484 item.filename = argv[0];
486 vec_push(items, item);
492 /* returns the line number, or -1 on error */
493 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
500 len = util_getline(&line, alen, src);
504 /* start at first non-blank */
505 for (start = line; isspace(*start); ++start) {}
506 /* end at the first non-blank */
507 for (end = start; *end && !isspace(*end); ++end) {}
510 /* move the actual filename to the beginning */
511 while (start != end) {
518 int main(int argc, char **argv) {
521 bool opts_output_free = false;
522 bool operators_free = false;
523 bool progs_src = false;
524 FILE *outfile = NULL;
526 memset(&opts, 0, sizeof(opts));
527 opts.output = "progs.dat";
528 opts.standard = COMPILER_GMQCC;
529 opts.max_array_size = (1024<<3);
534 /* default options / warn flags */
535 options_set(opts.warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
536 options_set(opts.warn, WARN_EXTENSIONS, true);
537 options_set(opts.warn, WARN_FIELD_REDECLARED, true);
538 options_set(opts.warn, WARN_TOO_FEW_PARAMETERS, true);
539 options_set(opts.warn, WARN_MISSING_RETURN_VALUES, true);
540 options_set(opts.warn, WARN_USED_UNINITIALIZED, true);
541 options_set(opts.warn, WARN_LOCAL_CONSTANTS, true);
542 options_set(opts.warn, WARN_VOID_VARIABLES, true);
543 options_set(opts.warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
544 options_set(opts.warn, WARN_VARIADIC_FUNCTION, true);
545 options_set(opts.warn, WARN_FRAME_MACROS, true);
546 options_set(opts.warn, WARN_UNUSED_VARIABLE, true);
547 options_set(opts.warn, WARN_EFFECTLESS_STATEMENT, true);
548 options_set(opts.warn, WARN_END_SYS_FIELDS, true);
549 options_set(opts.warn, WARN_ASSIGN_FUNCTION_TYPES, true);
550 options_set(opts.warn, WARN_PREPROCESSOR, true);
551 options_set(opts.warn, WARN_MULTIFILE_IF, true);
552 options_set(opts.warn, WARN_DOUBLE_DECLARATION, true);
553 options_set(opts.warn, WARN_CONST_VAR, true);
554 options_set(opts.warn, WARN_MULTIBYTE_CHARACTER, true);
556 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
557 options_set(opts.flags, FTEPP, false);
558 options_set(opts.flags, CORRECT_TERNARY, true);
560 if (!options_parse(argc, argv)) {
564 /* the standard decides which set of operators to use */
565 if (opts.standard == COMPILER_GMQCC) {
566 operators = c_operators;
567 operator_count = c_operator_count;
568 } else if (opts.standard == COMPILER_FTEQCC) {
569 operators = fte_operators;
570 operator_count = fte_operator_count;
572 operators = qcc_operators;
573 operator_count = qcc_operator_count;
576 if (operators == fte_operators) {
578 if (OPTS_FLAG(CORRECT_TERNARY)) {
580 if (operators[operator_count-2].id != opid1(',') ||
581 operators[operator_count-1].id != opid2(':','?'))
583 con_err("internal error: operator precedence table wasn't updated correctly!\n");
586 operators_free = true;
587 newops = mem_a(sizeof(operators[0]) * operator_count);
588 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
589 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
590 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
591 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
597 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
598 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
600 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
601 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
603 con_out("output = %s\n", opts.output);
604 con_out("optimization level = %i\n", (int)opts.O);
605 con_out("standard = %i\n", opts.standard);
609 if (opts_output_wasset) {
610 outfile = util_fopen(opts.output, "wb");
612 con_err("failed to open `%s` for writing\n", opts.output);
622 if (!parser_init()) {
623 con_err("failed to initialize parser\n");
629 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
631 con_err("failed to initialize parser\n");
637 util_debug("COM", "starting ...\n");
640 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
641 for (itr = 0; itr < vec_size(ppems); itr++) {
642 ftepp_add_macro(ppems[itr].name, ppems[itr].value);
643 mem_d(ppems[itr].name);
646 if (ppems[itr].value)
647 mem_d(ppems[itr].value);
651 if (!vec_size(items)) {
658 src = util_fopen("progs.src", "rb");
660 con_err("failed to open `progs.src` for reading\n");
666 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
667 con_err("illformatted progs.src file: expected output filename in first line\n");
672 if (!opts_output_wasset) {
673 opts.output = util_strdup(line);
674 opts_output_free = true;
677 while (progs_nextline(&line, &linelen, src)) {
679 if (!line[0] || (line[0] == '/' && line[1] == '/'))
681 item.filename = util_strdup(line);
683 vec_push(items, item);
694 if (vec_size(items)) {
696 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
697 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
699 for (itr = 0; itr < vec_size(items); ++itr) {
701 con_out(" item: %s (%s)\n",
703 ( (items[itr].type == TYPE_QC ? "qc" :
704 (items[itr].type == TYPE_ASM ? "asm" :
705 (items[itr].type == TYPE_SRC ? "progs.src" :
711 if (!ftepp_preprocess_file(items[itr].filename)) {
717 fprintf(outfile, "%s", out);
721 if (OPTS_FLAG(FTEPP)) {
723 if (!ftepp_preprocess_file(items[itr].filename)) {
728 if (vec_size(data)) {
729 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
737 if (!parser_compile_file(items[itr].filename)) {
745 mem_d(items[itr].filename);
746 items[itr].filename = NULL;
752 if (!parser_finish(opts.output)) {
762 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
763 if (optimization_count[itr]) {
764 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
770 util_debug("COM", "cleaning ...\n");
778 if (opts_output_free)
779 mem_d((char*)opts.output);
781 mem_d((void*)operators);