]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
alignment fix
[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     printf("  -force-crc=num         force a specific checksum into the header\n");
77     printf("\n");
78     printf("flags:\n"
79            "  -fdarkplaces-string-table-bug\n"
80            "            patch the string table to work with some bugged darkplaces versions\n"
81            "  -fomit-nullbytes\n"
82            "            omits certain null-bytes for a smaller output - requires a patched engine\n"
83            );
84     return -1;
85 }
86
87 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
88     size_t i;
89
90     for (i = 0; i < listsize; ++i) {
91         if (!strcmp(name, list[i].name)) {
92             longbit lb = list[i].bit;
93 #if 0
94             if (on)
95                 flags[lb.idx] |= (1<<(lb.bit));
96             else
97                 flags[lb.idx] &= ~(1<<(lb.bit));
98 #else
99             if (on)
100                 flags[0] |= (1<<lb);
101             else
102                 flags[0] &= ~(1<<(lb));
103 #endif
104             return true;
105         }
106     }
107     return false;
108 }
109 static bool options_setflag(const char *name, bool on) {
110     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
111 }
112 static bool options_setwarn(const char *name, bool on) {
113     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
114 }
115
116 static bool options_witharg(int *argc_, char ***argv_, char **out) {
117     int  argc   = *argc_;
118     char **argv = *argv_;
119
120     if (argv[0][2]) {
121         *out = argv[0]+2;
122         return true;
123     }
124     /* eat up the next */
125     if (argc < 2) /* no parameter was provided */
126         return false;
127
128     *out = argv[1];
129     --*argc_;
130     ++*argv_;
131     return true;
132 }
133
134 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
135     int  argc   = *argc_;
136     char **argv = *argv_;
137
138     size_t len = strlen(optname);
139
140     if (strncmp(argv[0]+ds, optname, len))
141         return false;
142
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;
147         return true;
148     }
149
150     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
151         return false;
152
153     /* using --opt param */
154     *out = argv[1];
155     --*argc_;
156     ++*argv_;
157     return true;
158 }
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);
161 }
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);
164 }
165
166 static bool options_parse(int argc, char **argv) {
167     bool argend = false;
168     size_t itr;
169     char buffer[1024];
170     while (!argend && argc > 1) {
171         char *argarg;
172         argitem item;
173
174         ++argv;
175         --argc;
176
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;
188                 else {
189                     printf("Unknown standard: %s\n", argarg);
190                     return false;
191                 }
192                 continue;
193             }
194             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
195                 opts_forcecrc = true;
196                 opts_forced_crc = strtol(argarg, NULL, 0);
197                 continue;
198             }
199             if (!strcmp(argv[0]+1, "debug")) {
200                 opts_debug = true;
201                 continue;
202             }
203             if (!strcmp(argv[0]+1, "dump")) {
204                 opts_dump = true;
205                 continue;
206             }
207             if (!strcmp(argv[0]+1, "memchk")) {
208                 opts_memchk = true;
209                 continue;
210             }
211             if (!strcmp(argv[0]+1, "benchmark")) {
212                 opts_benchmark = true;
213                 continue;
214             }
215
216             switch (argv[0][1]) {
217                 /* -h, show usage but exit with 0 */
218                 case 'h':
219                     usage();
220                     exit(0);
221                     break;
222
223                 /* handle all -fflags */
224                 case 'f':
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);
231                         }
232                         exit(0);
233                     }
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);
237                             return false;
238                         }
239                     }
240                     else if (!options_setflag(argv[0]+2, true)) {
241                         printf("unknown flag: %s\n", argv[0]+2);
242                         return false;
243                     }
244                     break;
245                 case 'W':
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);
252                         }
253                         exit(0);
254                     }
255                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
256                         opts_werror = false;
257                         break;
258                     }
259                     else if (!strcmp(argv[0]+2, "ERROR")) {
260                         opts_werror = true;
261                         break;
262                     }
263                     else if (!strcmp(argv[0]+2, "NONE")) {
264                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
265                             opts_warn[itr] = 0;
266                         break;
267                     }
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;
271                         break;
272                     }
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);
276                             return false;
277                         }
278                     }
279                     else if (!options_setwarn(argv[0]+2, true)) {
280                         printf("unknown warning: %s\n", argv[0]+2);
281                         return false;
282                     }
283                     break;
284
285                 case 'O':
286                     if (!options_witharg(&argc, &argv, &argarg)) {
287                         printf("option -O requires a numerical argument\n");
288                         return false;
289                     }
290                     opts_O = atoi(argarg);
291                     break;
292
293                 case 'o':
294                     if (!options_witharg(&argc, &argv, &argarg)) {
295                         printf("option -o requires an argument: the output file name\n");
296                         return false;
297                     }
298                     opts_output = argarg;
299                     opts_output_wasset = true;
300                     break;
301
302                 case 'a':
303                 case 's':
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"));
308                         return false;
309                     }
310                     item.filename = argarg;
311                     items_add(item);
312                     break;
313
314                 case '-':
315                     if (!argv[0][2]) {
316                         /* anything following -- is considered a non-option argument */
317                         argend = true;
318                         break;
319                     }
320             /* All long options without arguments */
321                     else if (!strcmp(argv[0]+2, "help")) {
322                         usage();
323                         exit(0);
324                     }
325                     else {
326             /* All long options with arguments */
327                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
328                             opts_output = argarg;
329                             opts_output_wasset = true;
330                         } else {
331                             printf("Unknown parameter: %s\n", argv[0]);
332                             return false;
333                         }
334                     }
335                     break;
336
337                 default:
338                     printf("Unknown parameter: %s\n", argv[0]);
339                     return false;
340             }
341         }
342         else
343         {
344             /* it's a QC filename */
345             argitem item;
346             item.filename = argv[0];
347             item.type     = TYPE_QC;
348             items_add(item);
349         }
350     }
351     return true;
352 }
353
354 static void options_set(uint32_t *flags, size_t idx, bool on)
355 {
356     longbit lb = LONGBIT(idx);
357 #if 0
358     if (on)
359         flags[lb.idx] |= (1<<(lb.bit));
360     else
361         flags[lb.idx] &= ~(1<<(lb.bit));
362 #else
363     if (on)
364         flags[0] |= (1<<(lb));
365     else
366         flags[0] &= ~(1<<(lb));
367 #endif
368 }
369
370 /* returns the line number, or -1 on error */
371 static bool progs_nextline(char **out, size_t *alen,FILE *src)
372 {
373     int    len;
374     char  *line;
375     char  *start;
376     char  *end;
377
378     line = *out;
379     len = util_getline(&line, alen, src);
380     if (len == -1)
381         return false;
382
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)   {}
387
388     *out = line;
389     /* move the actual filename to the beginning */
390     while (start != end) {
391         *line++ = *start++;
392     }
393     *line = 0;
394     return true;
395 }
396
397 int main(int argc, char **argv) {
398     size_t itr;
399     int retval = 0;
400     bool opts_output_free = false;
401
402     struct timespec ta, tb, tc;
403
404     app_name = argv[0];
405
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);
420
421     if (!options_parse(argc, argv)) {
422         return usage();
423     }
424
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;
429     } else {
430         operators = qcc_operators;
431         operator_count = qcc_operator_count;
432     }
433
434     if (opts_dump) {
435         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
436             printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
437         }
438         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
439             printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
440         }
441         printf("output = %s\n", opts_output);
442         printf("optimization level = %i\n", (int)opts_O);
443         printf("standard = %i\n", opts_standard);
444     }
445
446     if (!parser_init()) {
447         printf("failed to initialize parser\n");
448         retval = 1;
449         goto cleanup;
450     }
451
452     util_debug("COM", "starting ...\n");
453
454     if (items_elements) {
455         printf("Mode: manual\n");
456         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
457         if (opts_benchmark)
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" :
465                      ("unknown"))))));
466
467             if (!parser_compile(items_data[itr].filename)) {
468                 retval = 1;
469                 goto cleanup;
470             }
471         }
472
473         if (opts_benchmark)
474             clock_gettime(CLOCK_MONOTONIC, &tb);
475         parser_finish(opts_output);
476         if (opts_benchmark)
477             clock_gettime(CLOCK_MONOTONIC, &tc);
478
479     } else {
480         FILE *src;
481         char *line;
482         size_t linelen = 0;
483
484         printf("Mode: progs.src\n");
485         src = util_fopen("progs.src", "rb");
486         if (!src) {
487             printf("failed to open `progs.src` for reading\n");
488             retval = 1;
489             goto cleanup;
490         }
491
492         line = NULL;
493         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
494             printf("illformatted progs.src file: expected output filename in first line\n");
495             retval = 1;
496             goto srcdone;
497         }
498
499         if (!opts_output_wasset) {
500             opts_output = util_strdup(line);
501             opts_output_free = true;
502         }
503
504         if (opts_benchmark)
505             clock_gettime(CLOCK_MONOTONIC, &ta);
506         while (progs_nextline(&line, &linelen, src)) {
507             if (!line[0] || (line[0] == '/' && line[1] == '/'))
508                 continue;
509             printf("  src: %s\n", line);
510             if (!parser_compile(line)) {
511                 retval = 1;
512                 goto srcdone;
513             }
514         }
515
516         if (opts_benchmark)
517             clock_gettime(CLOCK_MONOTONIC, &tb);
518         parser_finish(opts_output);
519         if (opts_benchmark)
520             clock_gettime(CLOCK_MONOTONIC, &tc);
521
522 srcdone:
523         fclose(src);
524         mem_d(line);
525     }
526     if (opts_benchmark)
527     {
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);
531         {
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);
535         }
536         {
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);
540         }
541     }
542
543     /* stuff */
544
545 cleanup:
546     util_debug("COM", "cleaning ...\n");
547
548     mem_d(items_data);
549
550     parser_cleanup();
551     if (opts_output_free)
552         mem_d((char*)opts_output);
553
554     lex_cleanup();
555     util_meminfo();
556     return retval;
557 }