]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
remove unimplemented -f flags from --help, add the implemented one
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 #include "lexer.h"
25
26 #include <time.h>
27
28 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
29 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
30
31 uint32_t    opts_O        = 1;
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;
40
41 uint16_t    opts_forced_crc;
42
43 static bool opts_output_wasset = false;
44
45 /* set by the standard */
46 const oper_info *operators      = NULL;
47 size_t           operator_count = 0;
48
49 typedef struct { char *filename; int type; } argitem;
50 VECTOR_MAKE(argitem, items);
51
52 #define TYPE_QC  0
53 #define TYPE_ASM 1
54 #define TYPE_SRC 2
55
56 static const char *app_name;
57
58 static int usage() {
59     printf("usage: %s [options] [files...]", app_name);
60     printf("options:\n"
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");
78     printf("\n");
79     printf("flags:\n"
80            "  -fadjust-vector-fields\n"
81            "            when assigning a vector field, its _y and _z fields also get assigned\n"
82            );
83     return -1;
84 }
85
86 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
87     size_t i;
88
89     for (i = 0; i < listsize; ++i) {
90         if (!strcmp(name, list[i].name)) {
91             longbit lb = list[i].bit;
92 #if 0
93             if (on)
94                 flags[lb.idx] |= (1<<(lb.bit));
95             else
96                 flags[lb.idx] &= ~(1<<(lb.bit));
97 #else
98             if (on)
99                 flags[0] |= (1<<lb);
100             else
101                 flags[0] &= ~(1<<(lb));
102 #endif
103             return true;
104         }
105     }
106     return false;
107 }
108 static bool options_setflag(const char *name, bool on) {
109     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
110 }
111 static bool options_setwarn(const char *name, bool on) {
112     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
113 }
114
115 static bool options_witharg(int *argc_, char ***argv_, char **out) {
116     int  argc   = *argc_;
117     char **argv = *argv_;
118
119     if (argv[0][2]) {
120         *out = argv[0]+2;
121         return true;
122     }
123     /* eat up the next */
124     if (argc < 2) /* no parameter was provided */
125         return false;
126
127     *out = argv[1];
128     --*argc_;
129     ++*argv_;
130     return true;
131 }
132
133 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
134     int  argc   = *argc_;
135     char **argv = *argv_;
136
137     size_t len = strlen(optname);
138
139     if (strncmp(argv[0]+ds, optname, len))
140         return false;
141
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;
146         return true;
147     }
148
149     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
150         return false;
151
152     /* using --opt param */
153     *out = argv[1];
154     --*argc_;
155     ++*argv_;
156     return true;
157 }
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);
160 }
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);
163 }
164
165 static bool options_parse(int argc, char **argv) {
166     bool argend = false;
167     size_t itr;
168     char buffer[1024];
169     while (!argend && argc > 1) {
170         char *argarg;
171         argitem item;
172
173         ++argv;
174         --argc;
175
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;
187                 else {
188                     printf("Unknown standard: %s\n", argarg);
189                     return false;
190                 }
191                 continue;
192             }
193             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
194                 opts_forcecrc = true;
195                 opts_forced_crc = strtol(argarg, NULL, 0);
196                 continue;
197             }
198             if (!strcmp(argv[0]+1, "debug")) {
199                 opts_debug = true;
200                 continue;
201             }
202             if (!strcmp(argv[0]+1, "dump")) {
203                 opts_dump = true;
204                 continue;
205             }
206             if (!strcmp(argv[0]+1, "memchk")) {
207                 opts_memchk = true;
208                 continue;
209             }
210             if (!strcmp(argv[0]+1, "benchmark")) {
211                 opts_benchmark = true;
212                 continue;
213             }
214
215             switch (argv[0][1]) {
216                 /* -h, show usage but exit with 0 */
217                 case 'h':
218                     usage();
219                     exit(0);
220                     break;
221
222                 /* handle all -fflags */
223                 case 'f':
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);
230                         }
231                         exit(0);
232                     }
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);
236                             return false;
237                         }
238                     }
239                     else if (!options_setflag(argv[0]+2, true)) {
240                         printf("unknown flag: %s\n", argv[0]+2);
241                         return false;
242                     }
243                     break;
244                 case 'W':
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);
251                         }
252                         exit(0);
253                     }
254                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
255                         opts_werror = false;
256                         break;
257                     }
258                     else if (!strcmp(argv[0]+2, "ERROR")) {
259                         opts_werror = true;
260                         break;
261                     }
262                     else if (!strcmp(argv[0]+2, "NONE")) {
263                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
264                             opts_warn[itr] = 0;
265                         break;
266                     }
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;
270                         break;
271                     }
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);
275                             return false;
276                         }
277                     }
278                     else if (!options_setwarn(argv[0]+2, true)) {
279                         printf("unknown warning: %s\n", argv[0]+2);
280                         return false;
281                     }
282                     break;
283
284                 case 'O':
285                     if (!options_witharg(&argc, &argv, &argarg)) {
286                         printf("option -O requires a numerical argument\n");
287                         return false;
288                     }
289                     opts_O = atoi(argarg);
290                     break;
291
292                 case 'o':
293                     if (!options_witharg(&argc, &argv, &argarg)) {
294                         printf("option -o requires an argument: the output file name\n");
295                         return false;
296                     }
297                     opts_output = argarg;
298                     opts_output_wasset = true;
299                     break;
300
301                 case 'a':
302                 case 's':
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"));
307                         return false;
308                     }
309                     item.filename = argarg;
310                     items_add(item);
311                     break;
312
313                 case '-':
314                     if (!argv[0][2]) {
315                         /* anything following -- is considered a non-option argument */
316                         argend = true;
317                         break;
318                     }
319             /* All long options without arguments */
320                     else if (!strcmp(argv[0]+2, "help")) {
321                         usage();
322                         exit(0);
323                     }
324                     else {
325             /* All long options with arguments */
326                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
327                             opts_output = argarg;
328                             opts_output_wasset = true;
329                         } else {
330                             printf("Unknown parameter: %s\n", argv[0]);
331                             return false;
332                         }
333                     }
334                     break;
335
336                 default:
337                     printf("Unknown parameter: %s\n", argv[0]);
338                     return false;
339             }
340         }
341         else
342         {
343             /* it's a QC filename */
344             argitem item;
345             item.filename = argv[0];
346             item.type     = TYPE_QC;
347             items_add(item);
348         }
349     }
350     return true;
351 }
352
353 static void options_set(uint32_t *flags, size_t idx, bool on)
354 {
355     longbit lb = LONGBIT(idx);
356 #if 0
357     if (on)
358         flags[lb.idx] |= (1<<(lb.bit));
359     else
360         flags[lb.idx] &= ~(1<<(lb.bit));
361 #else
362     if (on)
363         flags[0] |= (1<<(lb));
364     else
365         flags[0] &= ~(1<<(lb));
366 #endif
367 }
368
369 /* returns the line number, or -1 on error */
370 static bool progs_nextline(char **out, size_t *alen,FILE *src)
371 {
372     int    len;
373     char  *line;
374     char  *start;
375     char  *end;
376
377     line = *out;
378     len = util_getline(&line, alen, src);
379     if (len == -1)
380         return false;
381
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)   {}
386
387     *out = line;
388     /* move the actual filename to the beginning */
389     while (start != end) {
390         *line++ = *start++;
391     }
392     *line = 0;
393     return true;
394 }
395
396 int main(int argc, char **argv) {
397     size_t itr;
398     int retval = 0;
399     bool opts_output_free = false;
400
401     struct timespec ta, tb, tc;
402
403     app_name = argv[0];
404
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
420     if (!options_parse(argc, argv)) {
421         return usage();
422     }
423
424     /* the standard decides which set of operators to use */
425     if (opts_standard == COMPILER_GMQCC) {
426         operators = c_operators;
427         operator_count = c_operator_count;
428     } else {
429         operators = qcc_operators;
430         operator_count = qcc_operator_count;
431     }
432
433     if (opts_dump) {
434         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
435             printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
436         }
437         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
438             printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
439         }
440         printf("output = %s\n", opts_output);
441         printf("optimization level = %i\n", (int)opts_O);
442         printf("standard = %i\n", opts_standard);
443     }
444
445     if (!parser_init()) {
446         printf("failed to initialize parser\n");
447         retval = 1;
448         goto cleanup;
449     }
450
451     util_debug("COM", "starting ...\n");
452
453     if (items_elements) {
454         printf("Mode: manual\n");
455         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
456         if (opts_benchmark)
457             clock_gettime(CLOCK_MONOTONIC, &ta);
458         for (itr = 0; itr < items_elements; ++itr) {
459             printf("  item: %s (%s)\n",
460                    items_data[itr].filename,
461                    ( (items_data[itr].type == TYPE_QC ? "qc" :
462                      (items_data[itr].type == TYPE_ASM ? "asm" :
463                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
464                      ("unknown"))))));
465
466             if (!parser_compile(items_data[itr].filename)) {
467                 retval = 1;
468                 goto cleanup;
469             }
470         }
471
472         if (opts_benchmark)
473             clock_gettime(CLOCK_MONOTONIC, &tb);
474         parser_finish(opts_output);
475         if (opts_benchmark)
476             clock_gettime(CLOCK_MONOTONIC, &tc);
477
478     } else {
479         FILE *src;
480         char *line;
481         size_t linelen = 0;
482
483         printf("Mode: progs.src\n");
484         src = util_fopen("progs.src", "rb");
485         if (!src) {
486             printf("failed to open `progs.src` for reading\n");
487             retval = 1;
488             goto cleanup;
489         }
490
491         line = NULL;
492         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
493             printf("illformatted progs.src file: expected output filename in first line\n");
494             retval = 1;
495             goto srcdone;
496         }
497
498         if (!opts_output_wasset) {
499             opts_output = util_strdup(line);
500             opts_output_free = true;
501         }
502
503         if (opts_benchmark)
504             clock_gettime(CLOCK_MONOTONIC, &ta);
505         while (progs_nextline(&line, &linelen, src)) {
506             if (!line[0] || (line[0] == '/' && line[1] == '/'))
507                 continue;
508             printf("  src: %s\n", line);
509             if (!parser_compile(line)) {
510                 retval = 1;
511                 goto srcdone;
512             }
513         }
514
515         if (opts_benchmark)
516             clock_gettime(CLOCK_MONOTONIC, &tb);
517         parser_finish(opts_output);
518         if (opts_benchmark)
519             clock_gettime(CLOCK_MONOTONIC, &tc);
520
521 srcdone:
522         fclose(src);
523         mem_d(line);
524     }
525     if (opts_benchmark)
526     {
527         printf("started parsing at: %lu:%09lu\n", (unsigned long)ta.tv_sec, (unsigned long)ta.tv_nsec);
528         printf("started codegen at: %lu:%09lu\n", (unsigned long)tb.tv_sec, (unsigned long)tb.tv_nsec);
529         printf("       finished at: %lu:%09lu\n", (unsigned long)tc.tv_sec, (unsigned long)tc.tv_nsec);
530         {
531             size_t sec = tb.tv_sec - ta.tv_sec;
532             size_t nsec = (tb.tv_nsec + sec * 1000000000L) - ta.tv_nsec;
533             printf("Parsing took %lu\n", (unsigned long)nsec);
534         }
535         {
536             size_t sec = tc.tv_sec - tb.tv_sec;
537             size_t nsec = (tc.tv_nsec + sec * 1000000000L) - tb.tv_nsec;
538             printf("Codegen took %lu\n", (unsigned long)nsec);
539         }
540     }
541
542     /* stuff */
543
544 cleanup:
545     util_debug("COM", "cleaning ...\n");
546
547     mem_d(items_data);
548
549     parser_cleanup();
550     if (opts_output_free)
551         mem_d((char*)opts_output);
552
553     lex_cleanup();
554     util_meminfo();
555     return retval;
556 }