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;
49 con_out("usage: %s [options] [files...]", app_name);
51 " -h, --help show this help message\n"
52 " -debug turns on compiler debug messages\n"
53 " -memchk turns on compiler memory leak check\n");
54 con_out(" -o, --output=file output file, defaults to progs.dat\n"
55 " -s filename add a progs.src file to be used\n");
56 con_out(" -E stop after preprocessing\n");
57 con_out(" -std=standard select one of the following standards\n"
58 " -std=qcc original QuakeC\n"
59 " -std=fteqcc fteqcc QuakeC\n"
60 " -std=gmqcc this compiler (default)\n");
61 con_out(" -f<flag> enable a flag\n"
62 " -fno-<flag> disable a flag\n"
63 " -fhelp list possible flags\n");
64 con_out(" -W<warning> enable a warning\n"
65 " -Wno-<warning> disable a warning\n"
66 " -Wall enable all warnings\n"
67 " -Werror treat warnings as errors\n");
68 con_out(" -Whelp list possible warnings\n");
69 con_out(" -O<number> optimization level\n"
70 " -O<name> enable specific optimization\n"
71 " -Ono-<name> disable specific optimization\n"
72 " -Ohelp list optimizations\n");
73 con_out(" -force-crc=num force a specific checksum into the header\n");
77 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
80 for (i = 0; i < listsize; ++i) {
81 if (!strcmp(name, list[i].name)) {
82 longbit lb = list[i].bit;
85 flags[lb.idx] |= (1<<(lb.bit));
87 flags[lb.idx] &= ~(1<<(lb.bit));
92 flags[0] &= ~(1<<(lb));
99 static bool options_setflag(const char *name, bool on) {
100 return options_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
102 static bool options_setwarn(const char *name, bool on) {
103 return options_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
105 static bool options_setoptim(const char *name, bool on) {
106 return options_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
109 static bool options_witharg(int *argc_, char ***argv_, char **out) {
111 char **argv = *argv_;
117 /* eat up the next */
118 if (argc < 2) /* no parameter was provided */
127 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
129 char **argv = *argv_;
131 size_t len = strlen(optname);
133 if (strncmp(argv[0]+ds, optname, len))
136 /* it's --optname, check how the parameter is supplied */
137 if (argv[0][ds+len] == '=') {
138 /* using --opt=param */
139 *out = argv[0]+ds+len+1;
143 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
146 /* using --opt param */
152 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
153 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
155 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
156 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
159 void options_set(uint32_t *flags, size_t idx, bool on)
161 longbit lb = LONGBIT(idx);
164 flags[lb.idx] |= (1<<(lb.bit));
166 flags[lb.idx] &= ~(1<<(lb.bit));
169 flags[0] |= (1<<(lb));
171 flags[0] &= ~(1<<(lb));
175 static void set_optimizations(unsigned int level)
178 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
179 options_set(opts.optimization, i, level >= opts_opt_oflag[i]);
182 static bool options_parse(int argc, char **argv) {
186 char *redirout = (char*)stdout;
187 char *redirerr = (char*)stderr;
189 while (!argend && argc > 1) {
197 if (argv[0][0] == '-') {
198 /* All gcc-type long options */
199 if (options_long_gcc("std", &argc, &argv, &argarg)) {
200 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
201 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
202 opts.standard = COMPILER_GMQCC;
203 } else if (!strcmp(argarg, "qcc")) {
204 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
205 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
206 opts.standard = COMPILER_QCC;
207 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
208 options_set(opts.flags, FTEPP, true);
209 options_set(opts.flags, TRANSLATABLE_STRINGS, true);
210 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
211 options_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
212 options_set(opts.warn, WARN_TERNARY_PRECEDENCE, true);
213 options_set(opts.flags, CORRECT_TERNARY, false);
214 opts.standard = COMPILER_FTEQCC;
215 } else if (!strcmp(argarg, "qccx")) {
216 options_set(opts.flags, ADJUST_VECTOR_FIELDS, false);
217 opts.standard = COMPILER_QCCX;
219 con_out("Unknown standard: %s\n", argarg);
224 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
225 opts.forcecrc = true;
226 opts.forced_crc = strtol(argarg, NULL, 0);
229 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
230 con_change(redirout, redirerr);
233 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
234 con_change(redirout, redirerr);
238 /* show defaults (like pathscale) */
239 if (!strcmp(argv[0]+1, "show-defaults")) {
242 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
246 memset(buffer, 0, sizeof(buffer));
247 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
249 con_out("-f%s ", buffer);
251 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
255 memset(buffer, 0, sizeof(buffer));
256 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
257 con_out("-W%s ", buffer);
263 if (!strcmp(argv[0]+1, "debug")) {
267 if (!strcmp(argv[0]+1, "dump")) {
271 if (!strcmp(argv[0]+1, "dumpfin")) {
275 if (!strcmp(argv[0]+1, "memchk")) {
279 if (!strcmp(argv[0]+1, "nocolor")) {
284 switch (argv[0][1]) {
285 /* -h, show usage but exit with 0 */
289 /* break; never reached because of exit(0) */
295 /* debug turns on -flno */
297 options_setflag("LNO", true);
301 if (!(argarg = strchr(argv[0] + 2, '='))) {
302 con_out("missing = in -D\n");
305 *argarg='\0'; /* terminate for name */
306 macro.name = util_strdup(argarg);
307 macro.value = util_strdup(argv[0]+2);
308 vec_push(ppems, macro);
311 /* handle all -fflags */
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 flags:\n");
316 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
317 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
318 con_out(" -f%s\n", buffer);
322 else if (!strncmp(argv[0]+2, "NO_", 3)) {
323 if (!options_setflag(argv[0]+5, false)) {
324 con_out("unknown flag: %s\n", argv[0]+2);
328 else if (!options_setflag(argv[0]+2, true)) {
329 con_out("unknown flag: %s\n", argv[0]+2);
334 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
335 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
336 con_out("Possible warnings:\n");
337 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
338 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
339 con_out(" -W%s\n", buffer);
343 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
347 else if (!strcmp(argv[0]+2, "ERROR")) {
351 else if (!strcmp(argv[0]+2, "NONE")) {
352 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
356 else if (!strcmp(argv[0]+2, "ALL")) {
357 for (itr = 0; itr < sizeof(opts.warn)/sizeof(opts.warn[0]); ++itr)
358 opts.warn[itr] = 0xFFFFFFFFL;
361 if (!strncmp(argv[0]+2, "NO_", 3)) {
362 if (!options_setwarn(argv[0]+5, false)) {
363 con_out("unknown warning: %s\n", argv[0]+2);
367 else if (!options_setwarn(argv[0]+2, true)) {
368 con_out("unknown warning: %s\n", argv[0]+2);
374 if (!options_witharg(&argc, &argv, &argarg)) {
375 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
378 if (isdigit(argarg[0])) {
379 opts.O = atoi(argarg);
380 set_optimizations(opts.O);
382 util_strtocmd(argarg, argarg, strlen(argarg)+1);
383 if (!strcmp(argarg, "HELP")) {
384 con_out("Possible optimizations:\n");
385 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
386 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
387 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
391 else if (!strcmp(argarg, "ALL"))
392 set_optimizations(opts.O = 9999);
393 else if (!strncmp(argarg, "NO_", 3)) {
394 if (!options_setoptim(argarg+3, false)) {
395 con_out("unknown optimization: %s\n", argarg+3);
400 if (!options_setoptim(argarg, true)) {
401 con_out("unknown optimization: %s\n", argarg);
409 if (!options_witharg(&argc, &argv, &argarg)) {
410 con_out("option -o requires an argument: the output file name\n");
413 opts.output = argarg;
414 opts_output_wasset = true;
419 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
420 if (!options_witharg(&argc, &argv, &argarg)) {
421 con_out("option -a requires a filename %s\n",
422 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
425 item.filename = argarg;
426 vec_push(items, item);
431 /* anything following -- is considered a non-option argument */
435 /* All long options without arguments */
436 else if (!strcmp(argv[0]+2, "help")) {
441 /* All long options with arguments */
442 if (options_long_witharg("output", &argc, &argv, &argarg)) {
443 opts.output = argarg;
444 opts_output_wasset = true;
446 con_out("Unknown parameter: %s\n", argv[0]);
453 con_out("Unknown parameter: %s\n", argv[0]);
459 /* it's a QC filename */
460 item.filename = argv[0];
462 vec_push(items, item);
468 /* returns the line number, or -1 on error */
469 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
476 len = util_getline(&line, alen, src);
480 /* start at first non-blank */
481 for (start = line; isspace(*start); ++start) {}
482 /* end at the first non-blank */
483 for (end = start; *end && !isspace(*end); ++end) {}
486 /* move the actual filename to the beginning */
487 while (start != end) {
494 int main(int argc, char **argv) {
497 bool opts_output_free = false;
498 bool operators_free = false;
499 bool progs_src = false;
500 FILE *outfile = NULL;
502 memset(&opts, 0, sizeof(opts));
503 opts.output = "progs.dat";
504 opts.standard = COMPILER_GMQCC;
505 opts.max_array_size = (1024<<3);
510 /* default options / warn flags */
511 options_set(opts.warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
512 options_set(opts.warn, WARN_EXTENSIONS, true);
513 options_set(opts.warn, WARN_FIELD_REDECLARED, true);
514 options_set(opts.warn, WARN_TOO_FEW_PARAMETERS, true);
515 options_set(opts.warn, WARN_MISSING_RETURN_VALUES, true);
516 options_set(opts.warn, WARN_USED_UNINITIALIZED, true);
517 options_set(opts.warn, WARN_LOCAL_CONSTANTS, true);
518 options_set(opts.warn, WARN_VOID_VARIABLES, true);
519 options_set(opts.warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
520 options_set(opts.warn, WARN_VARIADIC_FUNCTION, true);
521 options_set(opts.warn, WARN_FRAME_MACROS, true);
522 options_set(opts.warn, WARN_UNUSED_VARIABLE, true);
523 options_set(opts.warn, WARN_EFFECTLESS_STATEMENT, true);
524 options_set(opts.warn, WARN_END_SYS_FIELDS, true);
525 options_set(opts.warn, WARN_ASSIGN_FUNCTION_TYPES, true);
526 options_set(opts.warn, WARN_PREPROCESSOR, true);
527 options_set(opts.warn, WARN_MULTIFILE_IF, true);
528 options_set(opts.warn, WARN_DOUBLE_DECLARATION, true);
529 options_set(opts.warn, WARN_CONST_VAR, true);
530 options_set(opts.warn, WARN_MULTIBYTE_CHARACTER, true);
532 options_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
533 options_set(opts.flags, FTEPP, false);
534 options_set(opts.flags, CORRECT_TERNARY, true);
536 if (!options_parse(argc, argv)) {
540 /* the standard decides which set of operators to use */
541 if (opts.standard == COMPILER_GMQCC) {
542 operators = c_operators;
543 operator_count = c_operator_count;
544 } else if (opts.standard == COMPILER_FTEQCC) {
545 operators = fte_operators;
546 operator_count = fte_operator_count;
548 operators = qcc_operators;
549 operator_count = qcc_operator_count;
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 = 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;
573 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
574 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.output);
580 con_out("optimization level = %i\n", (int)opts.O);
581 con_out("standard = %i\n", opts.standard);
585 if (opts_output_wasset) {
586 outfile = util_fopen(opts.output, "wb");
588 con_err("failed to open `%s` for writing\n", opts.output);
598 if (!parser_init()) {
599 con_err("failed to initialize parser\n");
605 if (opts.pp_only || OPTS_FLAG(FTEPP)) {
607 con_err("failed to initialize parser\n");
612 for (i = 0; i < vec_size(ppems); ++i) {
613 ftepp_add_macro(ppems[i].name, ppems[i].value);
614 mem_d(ppems[i].name);
615 mem_d(ppems[i].value);
620 util_debug("COM", "starting ...\n");
622 if (!vec_size(items)) {
629 src = util_fopen("progs.src", "rb");
631 con_err("failed to open `progs.src` for reading\n");
637 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
638 con_err("illformatted progs.src file: expected output filename in first line\n");
643 if (!opts_output_wasset) {
644 opts.output = util_strdup(line);
645 opts_output_free = true;
648 while (progs_nextline(&line, &linelen, src)) {
650 if (!line[0] || (line[0] == '/' && line[1] == '/'))
652 item.filename = util_strdup(line);
654 vec_push(items, item);
665 if (vec_size(items)) {
667 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
668 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
670 for (itr = 0; itr < vec_size(items); ++itr) {
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" :
682 if (!ftepp_preprocess_file(items[itr].filename)) {
688 fprintf(outfile, "%s", out);
692 if (OPTS_FLAG(FTEPP)) {
694 if (!ftepp_preprocess_file(items[itr].filename)) {
699 if (vec_size(data)) {
700 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
708 if (!parser_compile_file(items[itr].filename)) {
716 mem_d(items[itr].filename);
717 items[itr].filename = NULL;
723 if (!parser_finish(opts.output)) {
733 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
734 if (optimization_count[itr]) {
735 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
741 util_debug("COM", "cleaning ...\n");
749 if (opts_output_free)
750 mem_d((char*)opts.output);
752 mem_d((void*)operators);