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_warn [1 + (COUNT_WARNINGS / 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_werror = false;
38 bool opts_forcecrc = false;
39 bool opts_pp_only = false;
40 size_t opts_max_array_size = 1024;
42 uint16_t opts_forced_crc;
44 static bool opts_output_wasset = false;
46 /* set by the standard */
47 const oper_info *operators = NULL;
48 size_t operator_count = 0;
50 typedef struct { char *filename; int type; } argitem;
51 static argitem *items = NULL;
57 static const char *app_name;
60 con_out("usage: %s [options] [files...]", app_name);
62 " -h, --help show this help message\n"
63 " -debug turns on compiler debug messages\n"
64 " -memchk turns on compiler memory leak check\n");
65 con_out(" -o, --output=file output file, defaults to progs.dat\n"
66 " -a filename add an asm file to be assembled\n"
67 " -s filename add a progs.src file to be used\n");
68 con_out(" -E stop after preprocessing\n");
69 con_out(" -f<flag> enable a flag\n"
70 " -fno-<flag> disable a flag\n"
71 " -std standard select one of the following standards\n"
72 " -std=qcc original QuakeC\n"
73 " -std=fteqcc fteqcc QuakeC\n"
74 " -std=gmqcc this compiler (default)\n");
75 con_out(" -W<warning> enable a warning\n"
76 " -Wno-<warning> disable a warning\n"
77 " -Wall enable all warnings\n"
78 " -Werror treat warnings as errors\n");
79 con_out(" -force-crc=num force a specific checksum into the header\n");
82 " -fadjust-vector-fields\n"
83 " when assigning a vector field, its _y and _z fields also get assigned\n"
88 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
91 for (i = 0; i < listsize; ++i) {
92 if (!strcmp(name, list[i].name)) {
93 longbit lb = list[i].bit;
96 flags[lb.idx] |= (1<<(lb.bit));
98 flags[lb.idx] &= ~(1<<(lb.bit));
103 flags[0] &= ~(1<<(lb));
110 static bool options_setflag(const char *name, bool on) {
111 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
113 static bool options_setwarn(const char *name, bool on) {
114 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
117 static bool options_witharg(int *argc_, char ***argv_, char **out) {
119 char **argv = *argv_;
125 /* eat up the next */
126 if (argc < 2) /* no parameter was provided */
135 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
137 char **argv = *argv_;
139 size_t len = strlen(optname);
141 if (strncmp(argv[0]+ds, optname, len))
144 /* it's --optname, check how the parameter is supplied */
145 if (argv[0][ds+len] == '=') {
146 /* using --opt=param */
147 *out = argv[0]+ds+len+1;
151 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
154 /* using --opt param */
160 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
161 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
163 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
164 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
167 static void options_set(uint32_t *flags, size_t idx, bool on)
169 longbit lb = LONGBIT(idx);
172 flags[lb.idx] |= (1<<(lb.bit));
174 flags[lb.idx] &= ~(1<<(lb.bit));
177 flags[0] |= (1<<(lb));
179 flags[0] &= ~(1<<(lb));
183 static bool options_parse(int argc, char **argv) {
187 char *redirout = (char*)stdout;
188 char *redirerr = (char*)stderr;
190 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 opts_standard = COMPILER_QCC;
206 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
207 options_set(opts_flags, FTEPP, true);
208 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
209 opts_standard = COMPILER_FTEQCC;
210 } else if (!strcmp(argarg, "qccx")) {
211 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
212 opts_standard = COMPILER_QCCX;
214 con_out("Unknown standard: %s\n", argarg);
219 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
220 opts_forcecrc = true;
221 opts_forced_crc = strtol(argarg, NULL, 0);
224 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
225 con_change(redirout, redirerr);
228 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
229 con_change(redirout, redirerr);
233 if (!strcmp(argv[0]+1, "debug")) {
237 if (!strcmp(argv[0]+1, "dump")) {
241 if (!strcmp(argv[0]+1, "dumpfin")) {
245 if (!strcmp(argv[0]+1, "memchk")) {
249 if (!strcmp(argv[0]+1, "nocolor")) {
254 switch (argv[0][1]) {
255 /* -h, show usage but exit with 0 */
259 /* break; never reached because of exit(0) */
265 /* handle all -fflags */
267 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
268 if (!strcmp(argv[0]+2, "HELP")) {
269 con_out("Possible flags:\n");
270 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
271 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
272 con_out(" -f%s\n", buffer);
276 else if (!strncmp(argv[0]+2, "NO_", 3)) {
277 if (!options_setflag(argv[0]+5, false)) {
278 con_out("unknown flag: %s\n", argv[0]+2);
282 else if (!options_setflag(argv[0]+2, true)) {
283 con_out("unknown flag: %s\n", argv[0]+2);
288 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
289 if (!strcmp(argv[0]+2, "HELP")) {
290 con_out("Possible warnings:\n");
291 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
292 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
293 con_out(" -W%s\n", buffer);
297 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
301 else if (!strcmp(argv[0]+2, "ERROR")) {
305 else if (!strcmp(argv[0]+2, "NONE")) {
306 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
310 else if (!strcmp(argv[0]+2, "ALL")) {
311 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
312 opts_warn[itr] = 0xFFFFFFFFL;
315 if (!strncmp(argv[0]+2, "NO_", 3)) {
316 if (!options_setwarn(argv[0]+5, false)) {
317 con_out("unknown warning: %s\n", argv[0]+2);
321 else if (!options_setwarn(argv[0]+2, true)) {
322 con_out("unknown warning: %s\n", argv[0]+2);
328 if (!options_witharg(&argc, &argv, &argarg)) {
329 con_out("option -O requires a numerical argument\n");
332 opts_O = atoi(argarg);
336 if (!options_witharg(&argc, &argv, &argarg)) {
337 con_out("option -o requires an argument: the output file name\n");
340 opts_output = argarg;
341 opts_output_wasset = true;
346 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
347 if (!options_witharg(&argc, &argv, &argarg)) {
348 con_out("option -a requires a filename %s\n",
349 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
352 item.filename = argarg;
353 vec_push(items, item);
358 /* anything following -- is considered a non-option argument */
362 /* All long options without arguments */
363 else if (!strcmp(argv[0]+2, "help")) {
368 /* All long options with arguments */
369 if (options_long_witharg("output", &argc, &argv, &argarg)) {
370 opts_output = argarg;
371 opts_output_wasset = true;
373 con_out("Unknown parameter: %s\n", argv[0]);
380 con_out("Unknown parameter: %s\n", argv[0]);
386 /* it's a QC filename */
387 item.filename = argv[0];
389 vec_push(items, item);
395 /* returns the line number, or -1 on error */
396 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
403 len = util_getline(&line, alen, src);
407 /* start at first non-blank */
408 for (start = line; isspace(*start); ++start) {}
409 /* end at the first non-blank */
410 for (end = start; *end && !isspace(*end); ++end) {}
413 /* move the actual filename to the beginning */
414 while (start != end) {
421 int main(int argc, char **argv) {
424 bool opts_output_free = false;
425 bool progs_src = false;
426 FILE *outfile = NULL;
431 /* default options / warn flags */
432 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
433 options_set(opts_warn, WARN_EXTENSIONS, true);
434 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
435 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
436 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
437 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
438 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
439 options_set(opts_warn, WARN_VOID_VARIABLES, true);
440 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
441 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
442 options_set(opts_warn, WARN_FRAME_MACROS, true);
443 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
444 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
445 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
446 options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
447 options_set(opts_warn, WARN_PREPROCESSOR, true);
448 options_set(opts_warn, WARN_MULTIFILE_IF, true);
450 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
451 options_set(opts_flags, FTEPP, false);
453 if (!options_parse(argc, argv)) {
457 /* the standard decides which set of operators to use */
458 if (opts_standard == COMPILER_GMQCC) {
459 operators = c_operators;
460 operator_count = c_operator_count;
461 } else if (opts_standard == COMPILER_FTEQCC) {
462 operators = fte_operators;
463 operator_count = fte_operator_count;
465 operators = qcc_operators;
466 operator_count = qcc_operator_count;
470 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
471 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
473 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
474 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
476 con_out("output = %s\n", opts_output);
477 con_out("optimization level = %i\n", (int)opts_O);
478 con_out("standard = %i\n", opts_standard);
482 if (opts_output_wasset) {
483 outfile = util_fopen(opts_output, "wb");
485 con_err("failed to open `%s` for writing\n", opts_output);
495 if (!parser_init()) {
496 con_err("failed to initialize parser\n");
501 if (opts_pp_only || OPTS_FLAG(FTEPP)) {
503 con_err("failed to initialize parser\n");
509 util_debug("COM", "starting ...\n");
511 if (!vec_size(items)) {
518 src = util_fopen("progs.src", "rb");
520 con_err("failed to open `progs.src` for reading\n");
526 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
527 con_err("illformatted progs.src file: expected output filename in first line\n");
532 if (!opts_output_wasset) {
533 opts_output = util_strdup(line);
534 opts_output_free = true;
537 while (progs_nextline(&line, &linelen, src)) {
539 if (!line[0] || (line[0] == '/' && line[1] == '/'))
541 item.filename = util_strdup(line);
543 vec_push(items, item);
554 if (vec_size(items)) {
556 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
557 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
559 for (itr = 0; itr < vec_size(items); ++itr) {
561 con_out(" item: %s (%s)\n",
563 ( (items[itr].type == TYPE_QC ? "qc" :
564 (items[itr].type == TYPE_ASM ? "asm" :
565 (items[itr].type == TYPE_SRC ? "progs.src" :
571 if (!ftepp_preprocess_file(items[itr].filename)) {
577 fprintf(outfile, "%s", out);
581 if (OPTS_FLAG(FTEPP)) {
583 if (!ftepp_preprocess_file(items[itr].filename)) {
588 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
595 if (!parser_compile_file(items[itr].filename)) {
603 mem_d(items[itr].filename);
604 items[itr].filename = NULL;
610 if (!parser_finish(opts_output)) {
620 util_debug("COM", "cleaning ...\n");
627 if (opts_output_free)
628 mem_d((char*)opts_output);