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)];
31 const char *opts_output = "progs.dat";
32 int opts_standard = COMPILER_GMQCC;
33 bool opts_debug = false;
34 bool opts_memchk = false;
35 bool opts_dumpfin = false;
36 bool opts_dump = false;
37 bool opts_forcecrc = false;
38 bool opts_pp_only = false;
39 size_t opts_max_array_size = 1024;
41 uint16_t opts_forced_crc;
43 static bool opts_output_wasset = false;
45 /* set by the standard */
46 const oper_info *operators = NULL;
47 size_t operator_count = 0;
49 typedef struct { char *filename; int type; } argitem;
50 static argitem *items = NULL;
56 static const char *app_name;
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 " -a filename add an asm file to be assembled\n"
66 " -s filename add a progs.src file to be used\n");
67 con_out(" -E stop after preprocessing\n");
68 con_out(" -f<flag> enable a flag\n"
69 " -fno-<flag> disable a flag\n"
70 " -std standard select one of the following standards\n"
71 " -std=qcc original QuakeC\n"
72 " -std=fteqcc fteqcc QuakeC\n"
73 " -std=gmqcc this compiler (default)\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(" -force-crc=num force a specific checksum into the header\n");
81 " -fadjust-vector-fields\n"
82 " when assigning a vector field, its _y and _z fields also get assigned\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 static 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) {
206 if (argv[0][0] == '-') {
207 /* All gcc-type long options */
208 if (options_long_gcc("std", &argc, &argv, &argarg)) {
209 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
210 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
211 opts_standard = COMPILER_GMQCC;
212 } else if (!strcmp(argarg, "qcc")) {
213 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
214 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
215 opts_standard = COMPILER_QCC;
216 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
217 options_set(opts_flags, FTEPP, true);
218 options_set(opts_flags, TRANSLATABLE_STRINGS, true);
219 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
220 options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
221 opts_standard = COMPILER_FTEQCC;
222 } else if (!strcmp(argarg, "qccx")) {
223 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
224 opts_standard = COMPILER_QCCX;
226 con_out("Unknown standard: %s\n", argarg);
231 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
232 opts_forcecrc = true;
233 opts_forced_crc = strtol(argarg, NULL, 0);
236 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
237 con_change(redirout, redirerr);
240 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
241 con_change(redirout, redirerr);
245 /* show defaults (like pathscale) */
246 if (!strcmp(argv[0]+1, "show-defaults")) {
249 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
253 memset(buffer, 0, sizeof(buffer));
254 util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
256 con_out("-f%s ", buffer);
258 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
262 memset(buffer, 0, sizeof(buffer));
263 util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
264 con_out("-W%s ", buffer);
270 if (!strcmp(argv[0]+1, "debug")) {
274 if (!strcmp(argv[0]+1, "dump")) {
278 if (!strcmp(argv[0]+1, "dumpfin")) {
282 if (!strcmp(argv[0]+1, "memchk")) {
286 if (!strcmp(argv[0]+1, "nocolor")) {
291 switch (argv[0][1]) {
292 /* -h, show usage but exit with 0 */
296 /* break; never reached because of exit(0) */
302 /* handle all -fflags */
304 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
305 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
306 con_out("Possible flags:\n");
307 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
308 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
309 con_out(" -f%s\n", buffer);
313 else if (!strncmp(argv[0]+2, "NO_", 3)) {
314 if (!options_setflag(argv[0]+5, false)) {
315 con_out("unknown flag: %s\n", argv[0]+2);
319 else if (!options_setflag(argv[0]+2, true)) {
320 con_out("unknown flag: %s\n", argv[0]+2);
325 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
326 if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
327 con_out("Possible warnings:\n");
328 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
329 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
330 con_out(" -W%s\n", buffer);
334 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
338 else if (!strcmp(argv[0]+2, "ERROR")) {
342 else if (!strcmp(argv[0]+2, "NONE")) {
343 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
347 else if (!strcmp(argv[0]+2, "ALL")) {
348 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
349 opts_warn[itr] = 0xFFFFFFFFL;
352 if (!strncmp(argv[0]+2, "NO_", 3)) {
353 if (!options_setwarn(argv[0]+5, false)) {
354 con_out("unknown warning: %s\n", argv[0]+2);
358 else if (!options_setwarn(argv[0]+2, true)) {
359 con_out("unknown warning: %s\n", argv[0]+2);
365 if (!options_witharg(&argc, &argv, &argarg)) {
366 con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
369 if (isdigit(argarg[0])) {
370 opts_O = atoi(argarg);
371 set_optimizations(opts_O);
373 util_strtocmd(argarg, argarg, strlen(argarg)+1);
374 if (!strcmp(argarg, "HELP")) {
375 con_out("Possible optimizations:\n");
376 for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
377 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
378 con_out(" -O%s\n", buffer);
382 else if (!strcmp(argarg, "ALL"))
383 set_optimizations(opts_O = 9999);
384 else if (!strncmp(argarg, "NO_", 3)) {
385 if (!options_setoptim(argarg+3, false)) {
386 con_out("unknown optimization: %s\n", argarg+3);
391 if (!options_setoptim(argarg, true)) {
392 con_out("unknown optimization: %s\n", argarg);
400 if (!options_witharg(&argc, &argv, &argarg)) {
401 con_out("option -o requires an argument: the output file name\n");
404 opts_output = argarg;
405 opts_output_wasset = true;
410 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
411 if (!options_witharg(&argc, &argv, &argarg)) {
412 con_out("option -a requires a filename %s\n",
413 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
416 item.filename = argarg;
417 vec_push(items, item);
422 /* anything following -- is considered a non-option argument */
426 /* All long options without arguments */
427 else if (!strcmp(argv[0]+2, "help")) {
432 /* All long options with arguments */
433 if (options_long_witharg("output", &argc, &argv, &argarg)) {
434 opts_output = argarg;
435 opts_output_wasset = true;
437 con_out("Unknown parameter: %s\n", argv[0]);
444 con_out("Unknown parameter: %s\n", argv[0]);
450 /* it's a QC filename */
451 item.filename = argv[0];
453 vec_push(items, item);
459 /* returns the line number, or -1 on error */
460 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
467 len = util_getline(&line, alen, src);
471 /* start at first non-blank */
472 for (start = line; isspace(*start); ++start) {}
473 /* end at the first non-blank */
474 for (end = start; *end && !isspace(*end); ++end) {}
477 /* move the actual filename to the beginning */
478 while (start != end) {
485 int main(int argc, char **argv) {
488 bool opts_output_free = false;
489 bool progs_src = false;
490 FILE *outfile = NULL;
495 /* default options / warn flags */
496 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
497 options_set(opts_warn, WARN_EXTENSIONS, true);
498 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
499 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
500 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
501 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
502 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
503 options_set(opts_warn, WARN_VOID_VARIABLES, true);
504 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
505 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
506 options_set(opts_warn, WARN_FRAME_MACROS, true);
507 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
508 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
509 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
510 options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
511 options_set(opts_warn, WARN_PREPROCESSOR, true);
512 options_set(opts_warn, WARN_MULTIFILE_IF, true);
513 options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
514 options_set(opts_warn, WARN_CONST_VAR, true);
515 options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
517 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
518 options_set(opts_flags, FTEPP, false);
520 if (!options_parse(argc, argv)) {
524 /* the standard decides which set of operators to use */
525 if (opts_standard == COMPILER_GMQCC) {
526 operators = c_operators;
527 operator_count = c_operator_count;
528 } else if (opts_standard == COMPILER_FTEQCC) {
529 operators = fte_operators;
530 operator_count = fte_operator_count;
532 operators = qcc_operators;
533 operator_count = qcc_operator_count;
537 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
538 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
540 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
541 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
543 con_out("output = %s\n", opts_output);
544 con_out("optimization level = %i\n", (int)opts_O);
545 con_out("standard = %i\n", opts_standard);
549 if (opts_output_wasset) {
550 outfile = util_fopen(opts_output, "wb");
552 con_err("failed to open `%s` for writing\n", opts_output);
562 if (!parser_init()) {
563 con_err("failed to initialize parser\n");
568 if (opts_pp_only || OPTS_FLAG(FTEPP)) {
570 con_err("failed to initialize parser\n");
576 util_debug("COM", "starting ...\n");
578 if (!vec_size(items)) {
585 src = util_fopen("progs.src", "rb");
587 con_err("failed to open `progs.src` for reading\n");
593 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
594 con_err("illformatted progs.src file: expected output filename in first line\n");
599 if (!opts_output_wasset) {
600 opts_output = util_strdup(line);
601 opts_output_free = true;
604 while (progs_nextline(&line, &linelen, src)) {
606 if (!line[0] || (line[0] == '/' && line[1] == '/'))
608 item.filename = util_strdup(line);
610 vec_push(items, item);
621 if (vec_size(items)) {
623 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
624 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
626 for (itr = 0; itr < vec_size(items); ++itr) {
628 con_out(" item: %s (%s)\n",
630 ( (items[itr].type == TYPE_QC ? "qc" :
631 (items[itr].type == TYPE_ASM ? "asm" :
632 (items[itr].type == TYPE_SRC ? "progs.src" :
638 if (!ftepp_preprocess_file(items[itr].filename)) {
644 fprintf(outfile, "%s", out);
648 if (OPTS_FLAG(FTEPP)) {
650 if (!ftepp_preprocess_file(items[itr].filename)) {
655 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
662 if (!parser_compile_file(items[itr].filename)) {
670 mem_d(items[itr].filename);
671 items[itr].filename = NULL;
677 if (!parser_finish(opts_output)) {
687 util_debug("COM", "cleaning ...\n");
694 if (opts_output_free)
695 mem_d((char*)opts_output);