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
28 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
29 uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)];
32 const char *opts_output = "progs.dat";
33 int opts_standard = COMPILER_GMQCC;
34 bool opts_debug = false;
35 bool opts_memchk = false;
36 bool opts_dump = false;
37 bool opts_werror = false;
38 bool opts_forcecrc = false;
39 bool opts_benchmark = false;
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 VECTOR_MAKE(argitem, items);
56 static const char *app_name;
59 printf("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 printf(" -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 printf(" -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 printf(" -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 printf(" -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 bool options_parse(int argc, char **argv) {
169 while (!argend && argc > 1) {
176 if (argv[0][0] == '-') {
177 /* All gcc-type long options */
178 if (options_long_gcc("std", &argc, &argv, &argarg)) {
179 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
180 opts_standard = COMPILER_GMQCC;
181 else if (!strcmp(argarg, "qcc"))
182 opts_standard = COMPILER_QCC;
183 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
184 opts_standard = COMPILER_FTEQCC;
185 else if (!strcmp(argarg, "qccx"))
186 opts_standard = COMPILER_QCCX;
188 printf("Unknown standard: %s\n", argarg);
193 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
194 opts_forcecrc = true;
195 opts_forced_crc = strtol(argarg, NULL, 0);
198 if (!strcmp(argv[0]+1, "debug")) {
202 if (!strcmp(argv[0]+1, "dump")) {
206 if (!strcmp(argv[0]+1, "memchk")) {
210 if (!strcmp(argv[0]+1, "benchmark")) {
211 opts_benchmark = true;
215 switch (argv[0][1]) {
216 /* -h, show usage but exit with 0 */
222 /* handle all -fflags */
224 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
225 if (!strcmp(argv[0]+2, "HELP")) {
226 printf("Possible flags:\n");
227 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
228 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
229 printf(" -f%s\n", buffer);
233 else if (!strncmp(argv[0]+2, "NO_", 3)) {
234 if (!options_setflag(argv[0]+5, false)) {
235 printf("unknown flag: %s\n", argv[0]+2);
239 else if (!options_setflag(argv[0]+2, true)) {
240 printf("unknown flag: %s\n", argv[0]+2);
245 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
246 if (!strcmp(argv[0]+2, "HELP")) {
247 printf("Possible warnings:\n");
248 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
249 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
250 printf(" -W%s\n", buffer);
254 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
258 else if (!strcmp(argv[0]+2, "ERROR")) {
262 else if (!strcmp(argv[0]+2, "NONE")) {
263 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
267 else if (!strcmp(argv[0]+2, "ALL")) {
268 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
269 opts_warn[itr] = 0xFFFFFFFFL;
272 if (!strncmp(argv[0]+2, "NO_", 3)) {
273 if (!options_setwarn(argv[0]+5, false)) {
274 printf("unknown warning: %s\n", argv[0]+2);
278 else if (!options_setwarn(argv[0]+2, true)) {
279 printf("unknown warning: %s\n", argv[0]+2);
285 if (!options_witharg(&argc, &argv, &argarg)) {
286 printf("option -O requires a numerical argument\n");
289 opts_O = atoi(argarg);
293 if (!options_witharg(&argc, &argv, &argarg)) {
294 printf("option -o requires an argument: the output file name\n");
297 opts_output = argarg;
298 opts_output_wasset = true;
303 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
304 if (!options_witharg(&argc, &argv, &argarg)) {
305 printf("option -a requires a filename %s\n",
306 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
309 item.filename = argarg;
315 /* anything following -- is considered a non-option argument */
319 /* All long options without arguments */
320 else if (!strcmp(argv[0]+2, "help")) {
325 /* All long options with arguments */
326 if (options_long_witharg("output", &argc, &argv, &argarg)) {
327 opts_output = argarg;
328 opts_output_wasset = true;
330 printf("Unknown parameter: %s\n", argv[0]);
337 printf("Unknown parameter: %s\n", argv[0]);
343 /* it's a QC filename */
345 item.filename = argv[0];
353 static void options_set(uint32_t *flags, size_t idx, bool on)
355 longbit lb = LONGBIT(idx);
358 flags[lb.idx] |= (1<<(lb.bit));
360 flags[lb.idx] &= ~(1<<(lb.bit));
363 flags[0] |= (1<<(lb));
365 flags[0] &= ~(1<<(lb));
369 /* returns the line number, or -1 on error */
370 static bool progs_nextline(char **out, size_t *alen,FILE *src)
378 len = util_getline(&line, alen, src);
382 /* start at first non-blank */
383 for (start = line; isspace(*start); ++start) {}
384 /* end at the first non-blank */
385 for (end = start; *end && !isspace(*end); ++end) {}
388 /* move the actual filename to the beginning */
389 while (start != end) {
396 int main(int argc, char **argv) {
399 bool opts_output_free = false;
401 struct timespec ta, tb, tc;
405 /* default options / warn flags */
406 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
407 options_set(opts_warn, WARN_EXTENSIONS, true);
408 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
409 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
410 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
411 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
412 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
413 options_set(opts_warn, WARN_VOID_VARIABLES, true);
414 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
415 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
416 options_set(opts_warn, WARN_FRAME_MACROS, true);
417 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
418 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
419 options_set(opts_warn, WARN_END_SYS_FIELDS, true);
421 if (!options_parse(argc, argv)) {
425 /* the standard decides which set of operators to use */
426 if (opts_standard == COMPILER_GMQCC) {
427 operators = c_operators;
428 operator_count = c_operator_count;
430 operators = qcc_operators;
431 operator_count = qcc_operator_count;
435 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
436 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
438 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
439 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
441 printf("output = %s\n", opts_output);
442 printf("optimization level = %i\n", (int)opts_O);
443 printf("standard = %i\n", opts_standard);
446 if (!parser_init()) {
447 printf("failed to initialize parser\n");
452 util_debug("COM", "starting ...\n");
454 if (items_elements) {
455 printf("Mode: manual\n");
456 printf("There are %lu items to compile:\n", (unsigned long)items_elements);
458 clock_gettime(CLOCK_MONOTONIC, &ta);
459 for (itr = 0; itr < items_elements; ++itr) {
460 printf(" item: %s (%s)\n",
461 items_data[itr].filename,
462 ( (items_data[itr].type == TYPE_QC ? "qc" :
463 (items_data[itr].type == TYPE_ASM ? "asm" :
464 (items_data[itr].type == TYPE_SRC ? "progs.src" :
467 if (!parser_compile(items_data[itr].filename)) {
474 clock_gettime(CLOCK_MONOTONIC, &tb);
475 parser_finish(opts_output);
477 clock_gettime(CLOCK_MONOTONIC, &tc);
484 printf("Mode: progs.src\n");
485 src = util_fopen("progs.src", "rb");
487 printf("failed to open `progs.src` for reading\n");
493 if (!progs_nextline(&line, &linelen, src) || !line[0]) {
494 printf("illformatted progs.src file: expected output filename in first line\n");
499 if (!opts_output_wasset) {
500 opts_output = util_strdup(line);
501 opts_output_free = true;
505 clock_gettime(CLOCK_MONOTONIC, &ta);
506 while (progs_nextline(&line, &linelen, src)) {
507 if (!line[0] || (line[0] == '/' && line[1] == '/'))
509 printf(" src: %s\n", line);
510 if (!parser_compile(line)) {
517 clock_gettime(CLOCK_MONOTONIC, &tb);
518 parser_finish(opts_output);
520 clock_gettime(CLOCK_MONOTONIC, &tc);
528 printf("started parsing at: %lu:%09lu\n", (unsigned long)ta.tv_sec, (unsigned long)ta.tv_nsec);
529 printf("started codegen at: %lu:%09lu\n", (unsigned long)tb.tv_sec, (unsigned long)tb.tv_nsec);
530 printf(" finished at: %lu:%09lu\n", (unsigned long)tc.tv_sec, (unsigned long)tc.tv_nsec);
532 size_t sec = tb.tv_sec - ta.tv_sec;
533 size_t nsec = (tb.tv_nsec + sec * 1000000000L) - ta.tv_nsec;
534 printf("Parsing took %lu\n", (unsigned long)nsec);
537 size_t sec = tc.tv_sec - tb.tv_sec;
538 size_t nsec = (tc.tv_nsec + sec * 1000000000L) - tb.tv_nsec;
539 printf("Codegen took %lu\n", (unsigned long)nsec);
546 util_debug("COM", "cleaning ...\n");
551 if (opts_output_free)
552 mem_d((char*)opts_output);