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 printf(" -force-crc=num force a specific checksum into the header\n");
79 " -fdarkplaces-string-table-bug\n"
80 " patch the string table to work with some bugged darkplaces versions\n"
82 " omits certain null-bytes for a smaller output - requires a patched engine\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);
116 static bool options_witharg(int *argc_, char ***argv_, char **out) {
118 char **argv = *argv_;
124 /* eat up the next */
125 if (argc < 2) /* no parameter was provided */
134 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
136 char **argv = *argv_;
138 size_t len = strlen(optname);
140 if (strncmp(argv[0]+ds, optname, len))
143 /* it's --optname, check how the parameter is supplied */
144 if (argv[0][ds+len] == '=') {
145 /* using --opt=param */
146 *out = argv[0]+ds+len+1;
150 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
153 /* using --opt param */
159 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
160 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
162 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
163 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
166 static bool options_parse(int argc, char **argv) {
170 while (!argend && argc > 1) {
177 if (argv[0][0] == '-') {
178 /* All gcc-type long options */
179 if (options_long_gcc("std", &argc, &argv, &argarg)) {
180 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
181 opts_standard = COMPILER_GMQCC;
182 else if (!strcmp(argarg, "qcc"))
183 opts_standard = COMPILER_QCC;
184 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
185 opts_standard = COMPILER_FTEQCC;
186 else if (!strcmp(argarg, "qccx"))
187 opts_standard = COMPILER_QCCX;
189 printf("Unknown standard: %s\n", argarg);
194 if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
195 opts_forcecrc = true;
196 opts_forced_crc = strtol(argarg, NULL, 0);
199 if (!strcmp(argv[0]+1, "debug")) {
203 if (!strcmp(argv[0]+1, "dump")) {
207 if (!strcmp(argv[0]+1, "memchk")) {
211 if (!strcmp(argv[0]+1, "benchmark")) {
212 opts_benchmark = true;
216 switch (argv[0][1]) {
217 /* -h, show usage but exit with 0 */
223 /* handle all -fflags */
225 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
226 if (!strcmp(argv[0]+2, "HELP")) {
227 printf("Possible flags:\n");
228 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
229 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
230 printf(" -f%s\n", buffer);
234 else if (!strncmp(argv[0]+2, "NO_", 3)) {
235 if (!options_setflag(argv[0]+5, false)) {
236 printf("unknown flag: %s\n", argv[0]+2);
240 else if (!options_setflag(argv[0]+2, true)) {
241 printf("unknown flag: %s\n", argv[0]+2);
246 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
247 if (!strcmp(argv[0]+2, "HELP")) {
248 printf("Possible warnings:\n");
249 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
250 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
251 printf(" -W%s\n", buffer);
255 else if (!strcmp(argv[0]+2, "NO_ERROR")) {
259 else if (!strcmp(argv[0]+2, "ERROR")) {
263 else if (!strcmp(argv[0]+2, "NONE")) {
264 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
268 else if (!strcmp(argv[0]+2, "ALL")) {
269 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
270 opts_warn[itr] = 0xFFFFFFFFL;
273 if (!strncmp(argv[0]+2, "NO_", 3)) {
274 if (!options_setwarn(argv[0]+5, false)) {
275 printf("unknown warning: %s\n", argv[0]+2);
279 else if (!options_setwarn(argv[0]+2, true)) {
280 printf("unknown warning: %s\n", argv[0]+2);
286 if (!options_witharg(&argc, &argv, &argarg)) {
287 printf("option -O requires a numerical argument\n");
290 opts_O = atoi(argarg);
294 if (!options_witharg(&argc, &argv, &argarg)) {
295 printf("option -o requires an argument: the output file name\n");
298 opts_output = argarg;
299 opts_output_wasset = true;
304 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
305 if (!options_witharg(&argc, &argv, &argarg)) {
306 printf("option -a requires a filename %s\n",
307 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
310 item.filename = argarg;
316 /* anything following -- is considered a non-option argument */
320 /* All long options without arguments */
321 else if (!strcmp(argv[0]+2, "help")) {
326 /* All long options with arguments */
327 if (options_long_witharg("output", &argc, &argv, &argarg)) {
328 opts_output = argarg;
329 opts_output_wasset = true;
331 printf("Unknown parameter: %s\n", argv[0]);
338 printf("Unknown parameter: %s\n", argv[0]);
344 /* it's a QC filename */
346 item.filename = argv[0];
354 static void options_set(uint32_t *flags, size_t idx, bool on)
356 longbit lb = LONGBIT(idx);
359 flags[lb.idx] |= (1<<(lb.bit));
361 flags[lb.idx] &= ~(1<<(lb.bit));
364 flags[0] |= (1<<(lb));
366 flags[0] &= ~(1<<(lb));
370 /* returns the line number, or -1 on error */
371 static bool progs_nextline(char **out, size_t *alen,FILE *src)
379 len = util_getline(&line, alen, src);
383 /* start at first non-blank */
384 for (start = line; isspace(*start); ++start) {}
385 /* end at the first non-blank */
386 for (end = start; *end && !isspace(*end); ++end) {}
389 /* move the actual filename to the beginning */
390 while (start != end) {
397 int main(int argc, char **argv) {
400 bool opts_output_free = false;
402 struct timespec ta, tb, tc;
406 /* default options / warn flags */
407 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
408 options_set(opts_warn, WARN_EXTENSIONS, true);
409 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
410 options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
411 options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
412 options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
413 options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
414 options_set(opts_warn, WARN_VOID_VARIABLES, true);
415 options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
416 options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
417 options_set(opts_warn, WARN_FRAME_MACROS, true);
418 options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
419 options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, 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);