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 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
28 uint32_t opts_optimization[1 + (COUNT_OPTIMIZATIONS / 32)];
30 /* counter increased in ir.c */
31 unsigned int optimization_count[COUNT_OPTIMIZATIONS];
34 const char *opts_output = "progs.dat";
35 int opts_standard = COMPILER_GMQCC;
36 bool opts_debug = false;
37 bool opts_memchk = false;
38 bool opts_dumpfin = false;
39 bool opts_dump = false;
40 bool opts_forcecrc = false;
41 bool opts_pp_only = false;
42 size_t opts_max_array_size = (1024 << 3);
44 uint16_t opts_forced_crc;
46 static bool opts_output_wasset = false;
48 /* set by the standard */
49 const oper_info *operators = NULL;
50 size_t operator_count = 0;
52 typedef struct { char *filename; int type; } argitem;
53 static argitem *items = NULL;
59 static const char *app_name;
62 con_out("usage: %s [options] [files...]", app_name);
64 " -h, --help show this help message\n"
65 " -debug turns on compiler debug messages\n"
66 " -memchk turns on compiler memory leak check\n");
67 con_out(" -o, --output=file output file, defaults to progs.dat\n"
68 " -a filename add an asm file to be assembled\n"
69 " -s filename add a progs.src file to be used\n");
70 con_out(" -E stop after preprocessing\n");
71 con_out(" -f<flag> enable a flag\n"
72 " -fno-<flag> disable a flag\n"
73 " -std standard select one of the following standards\n"
74 " -std=qcc original QuakeC\n"
75 " -std=fteqcc fteqcc QuakeC\n"
76 " -std=gmqcc this compiler (default)\n");
77 con_out(" -W<warning> enable a warning\n"
78 " -Wno-<warning> disable a warning\n"
79 " -Wall enable all warnings\n"
80 " -Werror treat warnings as errors\n");
81 con_out(" -force-crc=num force a specific checksum into the header\n");
84 " -fadjust-vector-fields\n"
85 " when assigning a vector field, its _y and _z fields also get assigned\n"
90 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
93 for (i = 0; i < listsize; ++i) {
94 if (!strcmp(name, list[i].name)) {
95 longbit lb = list[i].bit;
98 flags[lb.idx] |= (1<<(lb.bit));
100 flags[lb.idx] &= ~(1<<(lb.bit));
105 flags[0] &= ~(1<<(lb));
112 static bool options_setflag(const char *name, bool on) {
113 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
115 static bool options_setwarn(const char *name, bool on) {
116 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
118 static bool options_setoptim(const char *name, bool on) {
119 return options_setflag_all(name, on, opts_optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
122 static bool options_witharg(int *argc_, char ***argv_, char **out) {
124 char **argv = *argv_;
130 /* eat up the next */
131 if (argc < 2) /* no parameter was provided */
140 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
142 char **argv = *argv_;
144 size_t len = strlen(optname);
146 if (strncmp(argv[0]+ds, optname, len))
149 /* it's --optname, check how the parameter is supplied */
150 if (argv[0][ds+len] == '=') {
151 /* using --opt=param */
152 *out = argv[0]+ds+len+1;
156 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
159 /* using --opt param */
165 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
166 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
168 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
169 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
172 void options_set(uint32_t *flags, size_t idx, bool on)
174 longbit lb = LONGBIT(idx);
177 flags[lb.idx] |= (1<<(lb.bit));
179 flags[lb.idx] &= ~(1<<(lb.bit));
182 flags[0] |= (1<<(lb));
184 flags[0] &= ~(1<<(lb));
188 static void set_optimizations(unsigned int level)
191 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
192 options_set(opts_optimization, i, level >= opts_opt_oflag[i]);
195 static bool options_parse(int argc, char **argv) {
199 char *redirout = (char*)stdout;
200 char *redirerr = (char*)stderr;
202 while (!argend && argc > 1) {
209 if (argv[0][0] == '-') {
210 /* All gcc-type long options */
211 if (options_long_gcc("std", &argc, &argv, &argarg)) {
212 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
213 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
214 opts_standard = COMPILER_GMQCC;
215 } else if (!strcmp(argarg, "qcc")) {
216 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
217 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
218 opts_standard = COMPILER_QCC;
219 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
220 options_set(opts_flags, FTEPP, true);
221 options_set(opts_flags, TRANSLATABLE_STRINGS, true);
222 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
223 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
224 options_set(opts_warn, WARN_TERNARY_PRECEDENCE, true);
225 options_set(opts_flags, CORRECT_TERNARY, false);
226 opts_standard = COMPILER_FTEQCC;
227 } else if (!strcmp(argarg, "qccx")) {
228 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
229 opts_standard = COMPILER_QCCX;
231 con_out("Unknown standard: %s\n", argarg);
236 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
237 opts_forcecrc = true;
238 opts_forced_crc = strtol(argarg, NULL, 0);
241 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
242 con_change(redirout, redirerr);
245 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
246 con_change(redirout, redirerr);
250 /* show defaults (like pathscale) */
251 if (!strcmp(argv[0]+1, "show-defaults")) {
254 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
258 memset(buffer, 0, sizeof(buffer));
259 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
261 con_out("-f%s ", buffer);
263 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
267 memset(buffer, 0, sizeof(buffer));
268 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
269 con_out("-W%s ", buffer);
275 if (!strcmp(argv[0]+1, "debug")) {
279 if (!strcmp(argv[0]+1, "dump")) {
283 if (!strcmp(argv[0]+1, "dumpfin")) {
287 if (!strcmp(argv[0]+1, "memchk")) {
291 if (!strcmp(argv[0]+1, "nocolor")) {
296 switch (argv[0][1]) {
297 /* -h, show usage but exit with 0 */
301 /* break; never reached because of exit(0) */
307 /* debug turns on -flno */
309 options_setflag("LNO", true);
312 /* handle all -fflags */
314 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
315 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
316 con_out("Possible flags:\n");
317 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
318 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
319 con_out(" -f%s\n", buffer);
323 else if (!strncmp(argv[0]+2, "NO_", 3)) {
324 if (!options_setflag(argv[0]+5, false)) {
325 con_out("unknown flag: %s\n", argv[0]+2);
329 else if (!options_setflag(argv[0]+2, true)) {
330 con_out("unknown flag: %s\n", argv[0]+2);
335 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
336 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
337 con_out("Possible warnings:\n");
338 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
339 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
340 con_out(" -W%s\n", buffer);
344 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
348 else if (!strcmp(argv[0]+2, "ERROR")) {
352 else if (!strcmp(argv[0]+2, "NONE")) {
353 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
357 else if (!strcmp(argv[0]+2, "ALL")) {
358 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
359 opts_warn[itr] = 0xFFFFFFFFL;
362 if (!strncmp(argv[0]+2, "NO_", 3)) {
363 if (!options_setwarn(argv[0]+5, false)) {
364 con_out("unknown warning: %s\n", argv[0]+2);
368 else if (!options_setwarn(argv[0]+2, true)) {
369 con_out("unknown warning: %s\n", argv[0]+2);
375 if (!options_witharg(&argc, &argv, &argarg)) {
376 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
379 if (isdigit(argarg[0])) {
380 opts_O = atoi(argarg);
381 set_optimizations(opts_O);
383 util_strtocmd(argarg, argarg, strlen(argarg)+1);
384 if (!strcmp(argarg, "HELP")) {
385 con_out("Possible optimizations:\n");
386 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
387 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
388 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
392 else if (!strcmp(argarg, "ALL"))
393 set_optimizations(opts_O = 9999);
394 else if (!strncmp(argarg, "NO_", 3)) {
395 if (!options_setoptim(argarg+3, false)) {
396 con_out("unknown optimization: %s\n", argarg+3);
401 if (!options_setoptim(argarg, true)) {
402 con_out("unknown optimization: %s\n", argarg);
410 if (!options_witharg(&argc, &argv, &argarg)) {
411 con_out("option -o requires an argument: the output file name\n");
414 opts_output = argarg;
415 opts_output_wasset = true;
420 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
421 if (!options_witharg(&argc, &argv, &argarg)) {
422 con_out("option -a requires a filename %s\n",
423 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
426 item.filename = argarg;
427 vec_push(items, item);
432 /* anything following -- is considered a non-option argument */
436 /* All long options without arguments */
437 else if (!strcmp(argv[0]+2, "help")) {
442 /* All long options with arguments */
443 if (options_long_witharg("output", &argc, &argv, &argarg)) {
444 opts_output = argarg;
445 opts_output_wasset = true;
447 con_out("Unknown parameter: %s\n", argv[0]);
454 con_out("Unknown parameter: %s\n", argv[0]);
460 /* it's a QC filename */
461 item.filename = argv[0];
463 vec_push(items, item);
469 /* returns the line number, or -1 on error */
470 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
477 len = util_getline(&line, alen, src);
481 /* start at first non-blank */
482 for (start = line; isspace(*start); ++start) {}
483 /* end at the first non-blank */
484 for (end = start; *end && !isspace(*end); ++end) {}
487 /* move the actual filename to the beginning */
488 while (start != end) {
495 int main(int argc, char **argv) {
498 bool opts_output_free = false;
499 bool operators_free = false;
500 bool progs_src = false;
501 FILE *outfile = NULL;
506 /* default options / warn flags */
507 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
508 options_set(opts_warn, WARN_EXTENSIONS, true);
509 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
510 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
511 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
512 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
513 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
514 options_set(opts_warn, WARN_VOID_VARIABLES, true);
515 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
516 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
517 options_set(opts_warn, WARN_FRAME_MACROS, true);
518 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
519 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
520 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
521 options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
522 options_set(opts_warn, WARN_PREPROCESSOR, true);
523 options_set(opts_warn, WARN_MULTIFILE_IF, true);
524 options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
525 options_set(opts_warn, WARN_CONST_VAR, true);
526 options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
528 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
529 options_set(opts_flags, FTEPP, false);
530 options_set(opts_flags, CORRECT_TERNARY, true);
532 if (!options_parse(argc, argv)) {
536 /* the standard decides which set of operators to use */
537 if (opts_standard == COMPILER_GMQCC) {
538 operators = c_operators;
539 operator_count = c_operator_count;
540 } else if (opts_standard == COMPILER_FTEQCC) {
541 operators = fte_operators;
542 operator_count = fte_operator_count;
544 operators = qcc_operators;
545 operator_count = qcc_operator_count;
548 if (operators == fte_operators) {
550 if (OPTS_FLAG(CORRECT_TERNARY)) {
552 if (operators[operator_count-2].id != opid1(',') ||
553 operators[operator_count-1].id != opid2(':','?'))
555 con_err("internal error: operator precedence table wasn't updated correctly!\n");
558 operators_free = true;
559 newops = mem_a(sizeof(operators[0]) * operator_count);
560 memcpy(newops, operators, sizeof(operators[0]) * operator_count);
561 memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
562 memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
563 newops[operator_count-2].prec = newops[operator_count-1].prec+1;
569 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
570 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
572 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
573 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
575 con_out("output = %s\n", opts_output);
576 con_out("optimization level = %i\n", (int)opts_O);
577 con_out("standard = %i\n", opts_standard);
581 if (opts_output_wasset) {
582 outfile = util_fopen(opts_output, "wb");
584 con_err("failed to open `%s` for writing\n", opts_output);
594 if (!parser_init()) {
595 con_err("failed to initialize parser\n");
600 if (opts_pp_only || OPTS_FLAG(FTEPP)) {
602 con_err("failed to initialize parser\n");
608 util_debug("COM", "starting ...\n");
610 if (!vec_size(items)) {
617 src = util_fopen("progs.src", "rb");
619 con_err("failed to open `progs.src` for reading\n");
625 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
626 con_err("illformatted progs.src file: expected output filename in first line\n");
631 if (!opts_output_wasset) {
632 opts_output = util_strdup(line);
633 opts_output_free = true;
636 while (progs_nextline(&line, &linelen, src)) {
638 if (!line[0] || (line[0] == '/' && line[1] == '/'))
640 item.filename = util_strdup(line);
642 vec_push(items, item);
653 if (vec_size(items)) {
655 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
656 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
658 for (itr = 0; itr < vec_size(items); ++itr) {
660 con_out(" item: %s (%s)\n",
662 ( (items[itr].type == TYPE_QC ? "qc" :
663 (items[itr].type == TYPE_ASM ? "asm" :
664 (items[itr].type == TYPE_SRC ? "progs.src" :
670 if (!ftepp_preprocess_file(items[itr].filename)) {
676 fprintf(outfile, "%s", out);
680 if (OPTS_FLAG(FTEPP)) {
682 if (!ftepp_preprocess_file(items[itr].filename)) {
687 if (vec_size(data)) {
688 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
696 if (!parser_compile_file(items[itr].filename)) {
704 mem_d(items[itr].filename);
705 items[itr].filename = NULL;
711 if (!parser_finish(opts_output)) {
721 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
722 if (optimization_count[itr]) {
723 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
729 util_debug("COM", "cleaning ...\n");
736 if (opts_output_free)
737 mem_d((char*)opts_output);
739 mem_d((void*)operators);