]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Remove trailing whitespace for Blub.
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "gmqcc.h"
25 #include "lexer.h"
26
27 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
28 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
29
30 uint32_t    opts_O        = 1;
31 const char *opts_output   = "progs.dat";
32 int         opts_standard = COMPILER_GMQCC;
33 bool        opts_debug    = false;
34 bool        opts_memchk   = false;
35 bool        opts_dumpfin  = false;
36 bool        opts_dump     = false;
37 bool        opts_werror   = false;
38 bool        opts_forcecrc = false;
39 bool        opts_pp_only  = false;
40 size_t      opts_max_array_size = 1024;
41
42 uint16_t    opts_forced_crc;
43
44 static bool opts_output_wasset = false;
45
46 /* set by the standard */
47 const oper_info *operators      = NULL;
48 size_t           operator_count = 0;
49
50 typedef struct { char *filename; int type; } argitem;
51 static argitem *items = NULL;
52
53 #define TYPE_QC  0
54 #define TYPE_ASM 1
55 #define TYPE_SRC 2
56
57 static const char *app_name;
58
59 static int usage() {
60     con_out("usage: %s [options] [files...]", app_name);
61     con_out("options:\n"
62             "  -h, --help             show this help message\n"
63             "  -debug                 turns on compiler debug messages\n"
64             "  -memchk                turns on compiler memory leak check\n");
65     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
66             "  -a filename            add an asm file to be assembled\n"
67             "  -s filename            add a progs.src file to be used\n");
68     con_out("  -E                     stop after preprocessing\n");
69     con_out("  -f<flag>               enable a flag\n"
70             "  -fno-<flag>            disable a flag\n"
71             "  -std standard          select one of the following standards\n"
72             "       -std=qcc          original QuakeC\n"
73             "       -std=fteqcc       fteqcc QuakeC\n"
74             "       -std=gmqcc        this compiler (default)\n");
75     con_out("  -W<warning>            enable a warning\n"
76             "  -Wno-<warning>         disable a warning\n"
77             "  -Wall                  enable all warnings\n"
78             "  -Werror                treat warnings as errors\n");
79     con_out("  -force-crc=num         force a specific checksum into the header\n");
80     con_out("\n");
81     con_out("flags:\n"
82             "  -fadjust-vector-fields\n"
83             "            when assigning a vector field, its _y and _z fields also get assigned\n"
84            );
85     return -1;
86 }
87
88 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
89     size_t i;
90
91     for (i = 0; i < listsize; ++i) {
92         if (!strcmp(name, list[i].name)) {
93             longbit lb = list[i].bit;
94 #if 0
95             if (on)
96                 flags[lb.idx] |= (1<<(lb.bit));
97             else
98                 flags[lb.idx] &= ~(1<<(lb.bit));
99 #else
100             if (on)
101                 flags[0] |= (1<<lb);
102             else
103                 flags[0] &= ~(1<<(lb));
104 #endif
105             return true;
106         }
107     }
108     return false;
109 }
110 static bool options_setflag(const char *name, bool on) {
111     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
112 }
113 static bool options_setwarn(const char *name, bool on) {
114     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
115 }
116
117 static bool options_witharg(int *argc_, char ***argv_, char **out) {
118     int  argc   = *argc_;
119     char **argv = *argv_;
120
121     if (argv[0][2]) {
122         *out = argv[0]+2;
123         return true;
124     }
125     /* eat up the next */
126     if (argc < 2) /* no parameter was provided */
127         return false;
128
129     *out = argv[1];
130     --*argc_;
131     ++*argv_;
132     return true;
133 }
134
135 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
136     int  argc   = *argc_;
137     char **argv = *argv_;
138
139     size_t len = strlen(optname);
140
141     if (strncmp(argv[0]+ds, optname, len))
142         return false;
143
144     /* it's --optname, check how the parameter is supplied */
145     if (argv[0][ds+len] == '=') {
146         /* using --opt=param */
147         *out = argv[0]+ds+len+1;
148         return true;
149     }
150
151     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
152         return false;
153
154     /* using --opt param */
155     *out = argv[1];
156     --*argc_;
157     ++*argv_;
158     return true;
159 }
160 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
161     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
162 }
163 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
164     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
165 }
166
167 static void options_set(uint32_t *flags, size_t idx, bool on)
168 {
169     longbit lb = LONGBIT(idx);
170 #if 0
171     if (on)
172         flags[lb.idx] |= (1<<(lb.bit));
173     else
174         flags[lb.idx] &= ~(1<<(lb.bit));
175 #else
176     if (on)
177         flags[0] |= (1<<(lb));
178     else
179         flags[0] &= ~(1<<(lb));
180 #endif
181 }
182
183 static bool options_parse(int argc, char **argv) {
184     bool argend = false;
185     size_t itr;
186     char  buffer[1024];
187     char *redirout = (char*)stdout;
188     char *redirerr = (char*)stderr;
189
190     while (!argend && argc > 1) {
191         char *argarg;
192         argitem item;
193
194         ++argv;
195         --argc;
196
197         if (argv[0][0] == '-') {
198     /* All gcc-type long options */
199             if (options_long_gcc("std", &argc, &argv, &argarg)) {
200                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
201                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
202                     opts_standard = COMPILER_GMQCC;
203                 } else if (!strcmp(argarg, "qcc")) {
204                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
205                     options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
206                     opts_standard = COMPILER_QCC;
207                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
208                     options_set(opts_flags, FTEPP,                true);
209                     options_set(opts_flags, TRANSLATABLE_STRINGS, true);
210                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
211                     options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
212                     opts_standard = COMPILER_FTEQCC;
213                 } else if (!strcmp(argarg, "qccx")) {
214                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
215                     opts_standard = COMPILER_QCCX;
216                 } else {
217                     con_out("Unknown standard: %s\n", argarg);
218                     return false;
219                 }
220                 continue;
221             }
222             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
223                 opts_forcecrc = true;
224                 opts_forced_crc = strtol(argarg, NULL, 0);
225                 continue;
226             }
227             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
228                 con_change(redirout, redirerr);
229                 continue;
230             }
231             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
232                 con_change(redirout, redirerr);
233                 continue;
234             }
235
236             /* show defaults (like pathscale) */
237             if (!strcmp(argv[0]+1, "show-defaults")) {
238                 size_t itr;
239                 char   buffer[1024];
240                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
241                     if (!OPTS_FLAG(itr))
242                         continue;
243
244                     memset(buffer, 0, sizeof(buffer));
245                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
246
247                     con_out("-f%s ", buffer);
248                 }
249                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
250                     if (!OPTS_WARN(itr))
251                         continue;
252
253                     memset(buffer, 0, sizeof(buffer));
254                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
255                     con_out("-W%s ", buffer);
256                 }
257                 con_out("\n");
258                 exit(0);
259             }
260
261             if (!strcmp(argv[0]+1, "debug")) {
262                 opts_debug = true;
263                 continue;
264             }
265             if (!strcmp(argv[0]+1, "dump")) {
266                 opts_dump = true;
267                 continue;
268             }
269             if (!strcmp(argv[0]+1, "dumpfin")) {
270                 opts_dumpfin = true;
271                 continue;
272             }
273             if (!strcmp(argv[0]+1, "memchk")) {
274                 opts_memchk = true;
275                 continue;
276             }
277             if (!strcmp(argv[0]+1, "nocolor")) {
278                 con_color(0);
279                 continue;
280             }
281
282             switch (argv[0][1]) {
283                 /* -h, show usage but exit with 0 */
284                 case 'h':
285                     usage();
286                     exit(0);
287                     /* break; never reached because of exit(0) */
288
289                 case 'E':
290                     opts_pp_only = true;
291                     break;
292
293                 /* handle all -fflags */
294                 case 'f':
295                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
296                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
297                         con_out("Possible flags:\n");
298                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
299                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
300                             con_out(" -f%s\n", buffer);
301                         }
302                         exit(0);
303                     }
304                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
305                         if (!options_setflag(argv[0]+5, false)) {
306                             con_out("unknown flag: %s\n", argv[0]+2);
307                             return false;
308                         }
309                     }
310                     else if (!options_setflag(argv[0]+2, true)) {
311                         con_out("unknown flag: %s\n", argv[0]+2);
312                         return false;
313                     }
314                     break;
315                 case 'W':
316                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
317                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
318                         con_out("Possible warnings:\n");
319                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
320                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
321                             con_out(" -W%s\n", buffer);
322                         }
323                         exit(0);
324                     }
325                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
326                         opts_werror = false;
327                         break;
328                     }
329                     else if (!strcmp(argv[0]+2, "ERROR")) {
330                         opts_werror = true;
331                         break;
332                     }
333                     else if (!strcmp(argv[0]+2, "NONE")) {
334                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
335                             opts_warn[itr] = 0;
336                         break;
337                     }
338                     else if (!strcmp(argv[0]+2, "ALL")) {
339                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
340                             opts_warn[itr] = 0xFFFFFFFFL;
341                         break;
342                     }
343                     if (!strncmp(argv[0]+2, "NO_", 3)) {
344                         if (!options_setwarn(argv[0]+5, false)) {
345                             con_out("unknown warning: %s\n", argv[0]+2);
346                             return false;
347                         }
348                     }
349                     else if (!options_setwarn(argv[0]+2, true)) {
350                         con_out("unknown warning: %s\n", argv[0]+2);
351                         return false;
352                     }
353                     break;
354
355                 case 'O':
356                     if (!options_witharg(&argc, &argv, &argarg)) {
357                         con_out("option -O requires a numerical argument\n");
358                         return false;
359                     }
360                     opts_O = atoi(argarg);
361                     break;
362
363                 case 'o':
364                     if (!options_witharg(&argc, &argv, &argarg)) {
365                         con_out("option -o requires an argument: the output file name\n");
366                         return false;
367                     }
368                     opts_output = argarg;
369                     opts_output_wasset = true;
370                     break;
371
372                 case 'a':
373                 case 's':
374                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
375                     if (!options_witharg(&argc, &argv, &argarg)) {
376                         con_out("option -a requires a filename %s\n",
377                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
378                         return false;
379                     }
380                     item.filename = argarg;
381                     vec_push(items, item);
382                     break;
383
384                 case '-':
385                     if (!argv[0][2]) {
386                         /* anything following -- is considered a non-option argument */
387                         argend = true;
388                         break;
389                     }
390             /* All long options without arguments */
391                     else if (!strcmp(argv[0]+2, "help")) {
392                         usage();
393                         exit(0);
394                     }
395                     else {
396             /* All long options with arguments */
397                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
398                             opts_output = argarg;
399                             opts_output_wasset = true;
400                         } else {
401                             con_out("Unknown parameter: %s\n", argv[0]);
402                             return false;
403                         }
404                     }
405                     break;
406
407                 default:
408                     con_out("Unknown parameter: %s\n", argv[0]);
409                     return false;
410             }
411         }
412         else
413         {
414             /* it's a QC filename */
415             item.filename = argv[0];
416             item.type     = TYPE_QC;
417             vec_push(items, item);
418         }
419     }
420     return true;
421 }
422
423 /* returns the line number, or -1 on error */
424 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
425     int    len;
426     char  *line;
427     char  *start;
428     char  *end;
429
430     line = *out;
431     len = util_getline(&line, alen, src);
432     if (len == -1)
433         return false;
434
435     /* start at first non-blank */
436     for (start = line; isspace(*start); ++start) {}
437     /* end at the first non-blank */
438     for (end = start;  *end && !isspace(*end);  ++end)   {}
439
440     *out = line;
441     /* move the actual filename to the beginning */
442     while (start != end) {
443         *line++ = *start++;
444     }
445     *line = 0;
446     return true;
447 }
448
449 int main(int argc, char **argv) {
450     size_t itr;
451     int retval = 0;
452     bool opts_output_free = false;
453     bool progs_src = false;
454     FILE *outfile = NULL;
455
456     app_name = argv[0];
457     con_init();
458
459     /* default options / warn flags */
460     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
461     options_set(opts_warn, WARN_EXTENSIONS, true);
462     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
463     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
464     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
465     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
466     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
467     options_set(opts_warn, WARN_VOID_VARIABLES, true);
468     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
469     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
470     options_set(opts_warn, WARN_FRAME_MACROS, true);
471     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
472     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
473     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
474     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
475     options_set(opts_warn, WARN_PREPROCESSOR, true);
476     options_set(opts_warn, WARN_MULTIFILE_IF, true);
477     options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
478     options_set(opts_warn, WARN_CONST_VAR, true);
479     options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
480
481     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
482     options_set(opts_flags, FTEPP, false);
483
484     if (!options_parse(argc, argv)) {
485         return usage();
486     }
487
488     /* the standard decides which set of operators to use */
489     if (opts_standard == COMPILER_GMQCC) {
490         operators = c_operators;
491         operator_count = c_operator_count;
492     } else if (opts_standard == COMPILER_FTEQCC) {
493         operators = fte_operators;
494         operator_count = fte_operator_count;
495     } else {
496         operators = qcc_operators;
497         operator_count = qcc_operator_count;
498     }
499
500     if (opts_dump) {
501         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
502             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
503         }
504         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
505             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
506         }
507         con_out("output = %s\n", opts_output);
508         con_out("optimization level = %i\n", (int)opts_O);
509         con_out("standard = %i\n", opts_standard);
510     }
511
512     if (opts_pp_only) {
513         if (opts_output_wasset) {
514             outfile = util_fopen(opts_output, "wb");
515             if (!outfile) {
516                 con_err("failed to open `%s` for writing\n", opts_output);
517                 retval = 1;
518                 goto cleanup;
519             }
520         }
521         else
522             outfile = stdout;
523     }
524
525     if (!opts_pp_only) {
526         if (!parser_init()) {
527             con_err("failed to initialize parser\n");
528             retval = 1;
529             goto cleanup;
530         }
531     }
532     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
533         if (!ftepp_init()) {
534             con_err("failed to initialize parser\n");
535             retval = 1;
536             goto cleanup;
537         }
538     }
539
540     util_debug("COM", "starting ...\n");
541
542     if (!vec_size(items)) {
543         FILE *src;
544         char *line;
545         size_t linelen = 0;
546
547         progs_src = true;
548
549         src = util_fopen("progs.src", "rb");
550         if (!src) {
551             con_err("failed to open `progs.src` for reading\n");
552             retval = 1;
553             goto cleanup;
554         }
555
556         line = NULL;
557         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
558             con_err("illformatted progs.src file: expected output filename in first line\n");
559             retval = 1;
560             goto srcdone;
561         }
562
563         if (!opts_output_wasset) {
564             opts_output = util_strdup(line);
565             opts_output_free = true;
566         }
567
568         while (progs_nextline(&line, &linelen, src)) {
569             argitem item;
570             if (!line[0] || (line[0] == '/' && line[1] == '/'))
571                 continue;
572             item.filename = util_strdup(line);
573             item.type     = TYPE_QC;
574             vec_push(items, item);
575         }
576
577 srcdone:
578         fclose(src);
579         mem_d(line);
580     }
581
582     if (retval)
583         goto cleanup;
584
585     if (vec_size(items)) {
586         if (!opts_pp_only) {
587             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
588             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
589         }
590         for (itr = 0; itr < vec_size(items); ++itr) {
591             if (!opts_pp_only) {
592                 con_out("  item: %s (%s)\n",
593                        items[itr].filename,
594                        ( (items[itr].type == TYPE_QC ? "qc" :
595                          (items[itr].type == TYPE_ASM ? "asm" :
596                          (items[itr].type == TYPE_SRC ? "progs.src" :
597                          ("unknown"))))));
598             }
599
600             if (opts_pp_only) {
601                 const char *out;
602                 if (!ftepp_preprocess_file(items[itr].filename)) {
603                     retval = 1;
604                     goto cleanup;
605                 }
606                 out = ftepp_get();
607                 if (out)
608                     fprintf(outfile, "%s", out);
609                 ftepp_flush();
610             }
611             else {
612                 if (OPTS_FLAG(FTEPP)) {
613                     const char *data;
614                     if (!ftepp_preprocess_file(items[itr].filename)) {
615                         retval = 1;
616                         goto cleanup;
617                     }
618                     data = ftepp_get();
619                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
620                         retval = 1;
621                         goto cleanup;
622                     }
623                     ftepp_flush();
624                 }
625                 else {
626                     if (!parser_compile_file(items[itr].filename)) {
627                         retval = 1;
628                         goto cleanup;
629                     }
630                 }
631             }
632
633             if (progs_src) {
634                 mem_d(items[itr].filename);
635                 items[itr].filename = NULL;
636             }
637         }
638
639         ftepp_finish();
640         if (!opts_pp_only) {
641             if (!parser_finish(opts_output)) {
642                 retval = 1;
643                 goto cleanup;
644             }
645         }
646     }
647
648     /* stuff */
649
650 cleanup:
651     util_debug("COM", "cleaning ...\n");
652     ftepp_finish();
653     con_close();
654     vec_free(items);
655
656     if (!opts_pp_only)
657         parser_cleanup();
658     if (opts_output_free)
659         mem_d((char*)opts_output);
660
661     lex_cleanup();
662     util_meminfo();
663     return retval;
664 }