5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
27 uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)];
30 const char *opts_output = "progs.dat";
31 int opts_standard = COMPILER_GMQCC;
32 bool opts_debug = false;
33 bool opts_memchk = false;
34 bool opts_dump = false;
35 bool opts_werror = false;
36 bool opts_forcecrc = false;
37 bool opts_pp_only = false;
38 size_t opts_max_array_size = 1024;
40 uint16_t opts_forced_crc;
42 static bool opts_output_wasset = false;
44 /* set by the standard */
45 const oper_info *operators = NULL;
46 size_t operator_count = 0;
48 typedef struct { char *filename; int type; } argitem;
49 static argitem *items = NULL;
55 static const char *app_name;
58 con_out("usage: %s [options] [files...]", app_name);
60 " -h, --help show this help message\n"
61 " -debug turns on compiler debug messages\n"
62 " -memchk turns on compiler memory leak check\n");
63 con_out(" -o, --output=file output file, defaults to progs.dat\n"
64 " -a filename add an asm file to be assembled\n"
65 " -s filename add a progs.src file to be used\n");
66 con_out(" -E stop after preprocessing\n");
67 con_out(" -f<flag> enable a flag\n"
68 " -fno-<flag> disable a flag\n"
69 " -std standard select one of the following standards\n"
70 " -std=qcc original QuakeC\n"
71 " -std=fteqcc fteqcc QuakeC\n"
72 " -std=gmqcc this compiler (default)\n");
73 con_out(" -W<warning> enable a warning\n"
74 " -Wno-<warning> disable a warning\n"
75 " -Wall enable all warnings\n"
76 " -Werror treat warnings as errors\n");
77 con_out(" -force-crc=num force a specific checksum into the header\n");
80 " -fadjust-vector-fields\n"
81 " when assigning a vector field, its _y and _z fields also get assigned\n"
86 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
89 for (i = 0; i < listsize; ++i) {
90 if (!strcmp(name, list[i].name)) {
91 longbit lb = list[i].bit;
94 flags[lb.idx] |= (1<<(lb.bit));
96 flags[lb.idx] &= ~(1<<(lb.bit));
101 flags[0] &= ~(1<<(lb));
108 static bool options_setflag(const char *name, bool on) {
109 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
111 static bool options_setwarn(const char *name, bool on) {
112 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
115 static bool options_witharg(int *argc_, char ***argv_, char **out) {
117 char **argv = *argv_;
123 /* eat up the next */
124 if (argc < 2) /* no parameter was provided */
133 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
135 char **argv = *argv_;
137 size_t len = strlen(optname);
139 if (strncmp(argv[0]+ds, optname, len))
142 /* it's --optname, check how the parameter is supplied */
143 if (argv[0][ds+len] == '=') {
144 /* using --opt=param */
145 *out = argv[0]+ds+len+1;
149 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
152 /* using --opt param */
158 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
159 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
161 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
162 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
165 static void options_set(uint32_t *flags, size_t idx, bool on)
167 longbit lb = LONGBIT(idx);
170 flags[lb.idx] |= (1<<(lb.bit));
172 flags[lb.idx] &= ~(1<<(lb.bit));
175 flags[0] |= (1<<(lb));
177 flags[0] &= ~(1<<(lb));
181 static bool options_parse(int argc, char **argv) {
185 char *redirout = (char*)stdout;
186 char *redirerr = (char*)stderr;
188 while (!argend && argc > 1) {
195 if (argv[0][0] == '-') {
196 /* All gcc-type long options */
197 if (options_long_gcc("std", &argc, &argv, &argarg)) {
198 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
199 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
200 opts_standard = COMPILER_GMQCC;
201 } else if (!strcmp(argarg, "qcc")) {
202 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
203 opts_standard = COMPILER_QCC;
204 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
205 options_set(opts_flags, FTEPP, true);
206 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
207 opts_standard = COMPILER_FTEQCC;
208 } else if (!strcmp(argarg, "qccx")) {
209 options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
210 opts_standard = COMPILER_QCCX;
212 con_out("Unknown standard: %s\n", argarg);
217 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
218 opts_forcecrc = true;
219 opts_forced_crc = strtol(argarg, NULL, 0);
222 if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
225 if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
229 if (!strcmp(argv[0]+1, "debug")) {
233 if (!strcmp(argv[0]+1, "dump")) {
237 if (!strcmp(argv[0]+1, "memchk")) {
241 if (!strcmp(argv[0]+1, "nocolor")) {
246 switch (argv[0][1]) {
247 /* -h, show usage but exit with 0 */
257 /* handle all -fflags */
259 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
260 if (!strcmp(argv[0]+2, "HELP")) {
261 con_out("Possible flags:\n");
262 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
263 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
264 con_out(" -f%s\n", buffer);
268 else if (!strncmp(argv[0]+2, "NO_", 3)) {
269 if (!options_setflag(argv[0]+5, false)) {
270 con_out("unknown flag: %s\n", argv[0]+2);
274 else if (!options_setflag(argv[0]+2, true)) {
275 con_out("unknown flag: %s\n", argv[0]+2);
280 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
281 if (!strcmp(argv[0]+2, "HELP")) {
282 con_out("Possible warnings:\n");
283 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
284 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
285 con_out(" -W%s\n", buffer);
289 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
293 else if (!strcmp(argv[0]+2, "ERROR")) {
297 else if (!strcmp(argv[0]+2, "NONE")) {
298 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
302 else if (!strcmp(argv[0]+2, "ALL")) {
303 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
304 opts_warn[itr] = 0xFFFFFFFFL;
307 if (!strncmp(argv[0]+2, "NO_", 3)) {
308 if (!options_setwarn(argv[0]+5, false)) {
309 con_out("unknown warning: %s\n", argv[0]+2);
313 else if (!options_setwarn(argv[0]+2, true)) {
314 con_out("unknown warning: %s\n", argv[0]+2);
320 if (!options_witharg(&argc, &argv, &argarg)) {
321 con_out("option -O requires a numerical argument\n");
324 opts_O = atoi(argarg);
328 if (!options_witharg(&argc, &argv, &argarg)) {
329 con_out("option -o requires an argument: the output file name\n");
332 opts_output = argarg;
333 opts_output_wasset = true;
338 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
339 if (!options_witharg(&argc, &argv, &argarg)) {
340 con_out("option -a requires a filename %s\n",
341 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
344 item.filename = argarg;
345 vec_push(items, item);
350 /* anything following -- is considered a non-option argument */
354 /* All long options without arguments */
355 else if (!strcmp(argv[0]+2, "help")) {
360 /* All long options with arguments */
361 if (options_long_witharg("output", &argc, &argv, &argarg)) {
362 opts_output = argarg;
363 opts_output_wasset = true;
365 con_out("Unknown parameter: %s\n", argv[0]);
372 con_out("Unknown parameter: %s\n", argv[0]);
378 /* it's a QC filename */
380 item.filename = argv[0];
382 vec_push(items, item);
385 con_change(redirout, redirerr);
389 /* returns the line number, or -1 on error */
390 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
397 len = util_getline(&line, alen, src);
401 /* start at first non-blank */
402 for (start = line; isspace(*start); ++start) {}
403 /* end at the first non-blank */
404 for (end = start; *end && !isspace(*end); ++end) {}
407 /* move the actual filename to the beginning */
408 while (start != end) {
415 int main(int argc, char **argv) {
418 bool opts_output_free = false;
419 bool progs_src = false;
420 FILE *outfile = NULL;
425 /* default options / warn flags */
426 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
427 options_set(opts_warn, WARN_EXTENSIONS, true);
428 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
429 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
430 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
431 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
432 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
433 options_set(opts_warn, WARN_VOID_VARIABLES, true);
434 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
435 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
436 options_set(opts_warn, WARN_FRAME_MACROS, true);
437 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
438 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
439 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
440 options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
441 options_set(opts_warn, WARN_PREPROCESSOR, true);
442 options_set(opts_warn, WARN_MULTIFILE_IF, true);
444 options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
445 options_set(opts_flags, FTEPP, false);
447 if (!options_parse(argc, argv)) {
451 /* the standard decides which set of operators to use */
452 if (opts_standard == COMPILER_GMQCC) {
453 operators = c_operators;
454 operator_count = c_operator_count;
456 operators = qcc_operators;
457 operator_count = qcc_operator_count;
461 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
462 con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
464 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
465 con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
467 con_out("output = %s\n", opts_output);
468 con_out("optimization level = %i\n", (int)opts_O);
469 con_out("standard = %i\n", opts_standard);
473 if (opts_output_wasset) {
474 outfile = util_fopen(opts_output, "wb");
476 con_err("failed to open `%s` for writing\n", opts_output);
486 if (!parser_init()) {
487 con_err("failed to initialize parser\n");
492 if (opts_pp_only || OPTS_FLAG(FTEPP)) {
494 con_err("failed to initialize parser\n");
500 util_debug("COM", "starting ...\n");
502 if (!vec_size(items)) {
509 src = util_fopen("progs.src", "rb");
511 con_err("failed to open `progs.src` for reading\n");
517 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
518 con_err("illformatted progs.src file: expected output filename in first line\n");
523 if (!opts_output_wasset) {
524 opts_output = util_strdup(line);
525 opts_output_free = true;
528 while (progs_nextline(&line, &linelen, src)) {
530 if (!line[0] || (line[0] == '/' && line[1] == '/'))
532 item.filename = util_strdup(line);
534 vec_push(items, item);
545 if (vec_size(items)) {
547 con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
548 con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
550 for (itr = 0; itr < vec_size(items); ++itr) {
552 con_out(" item: %s (%s)\n",
554 ( (items[itr].type == TYPE_QC ? "qc" :
555 (items[itr].type == TYPE_ASM ? "asm" :
556 (items[itr].type == TYPE_SRC ? "progs.src" :
561 if (!ftepp_preprocess_file(items[itr].filename)) {
565 fprintf(outfile, "%s", ftepp_get());
569 if (OPTS_FLAG(FTEPP)) {
571 if (!ftepp_preprocess_file(items[itr].filename)) {
576 if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
583 if (!parser_compile_file(items[itr].filename)) {
591 mem_d(items[itr].filename);
592 items[itr].filename = NULL;
598 if (!parser_finish(opts_output)) {
608 util_debug("COM", "cleaning ...\n");
615 if (opts_output_free)
616 mem_d((char*)opts_output);