]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Merge branch 'master' into test-suite
[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_dump     = false;
36 bool        opts_werror   = false;
37 bool        opts_forcecrc = false;
38 bool        opts_pp_only  = false;
39 size_t      opts_max_array_size = 1024;
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 static argitem *items = NULL;
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     con_out("usage: %s [options] [files...]", app_name);
60     con_out("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     con_out("  -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     con_out("  -E                     stop after preprocessing\n");
68     con_out("  -f<flag>               enable a flag\n"
69             "  -fno-<flag>            disable a flag\n"
70             "  -std standard          select one of the following standards\n"
71             "       -std=qcc          original QuakeC\n"
72             "       -std=fteqcc       fteqcc QuakeC\n"
73             "       -std=gmqcc        this compiler (default)\n");
74     con_out("  -W<warning>            enable a warning\n"
75             "  -Wno-<warning>         disable a warning\n"
76             "  -Wall                  enable all warnings\n"
77             "  -Werror                treat warnings as errors\n");
78     con_out("  -force-crc=num         force a specific checksum into the header\n");
79     con_out("\n");
80     con_out("flags:\n"
81             "  -fadjust-vector-fields\n"
82             "            when assigning a vector field, its _y and _z fields also get assigned\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 void options_set(uint32_t *flags, size_t idx, bool on)
167 {
168     longbit lb = LONGBIT(idx);
169 #if 0
170     if (on)
171         flags[lb.idx] |= (1<<(lb.bit));
172     else
173         flags[lb.idx] &= ~(1<<(lb.bit));
174 #else
175     if (on)
176         flags[0] |= (1<<(lb));
177     else
178         flags[0] &= ~(1<<(lb));
179 #endif
180 }
181
182 static bool options_parse(int argc, char **argv) {
183     bool argend = false;
184     size_t itr;
185     char  buffer[1024];
186     char *redirout = (char*)stdout;
187     char *redirerr = (char*)stderr;
188
189     while (!argend && argc > 1) {
190         char *argarg;
191         argitem item;
192
193         ++argv;
194         --argc;
195
196         if (argv[0][0] == '-') {
197     /* All gcc-type long options */
198             if (options_long_gcc("std", &argc, &argv, &argarg)) {
199                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
200                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
201                     opts_standard = COMPILER_GMQCC;
202                 } else if (!strcmp(argarg, "qcc")) {
203                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
204                     opts_standard = COMPILER_QCC;
205                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
206                     options_set(opts_flags, FTEPP,                true);
207                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
208                     opts_standard = COMPILER_FTEQCC;
209                 } else if (!strcmp(argarg, "qccx")) {
210                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
211                     opts_standard = COMPILER_QCCX;
212                 } else {
213                     con_out("Unknown standard: %s\n", argarg);
214                     return false;
215                 }
216                 continue;
217             }
218             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
219                 opts_forcecrc = true;
220                 opts_forced_crc = strtol(argarg, NULL, 0);
221                 continue;
222             }
223             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
224                 continue;
225             }
226             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
227                 continue;
228             }
229             
230             con_change(redirout, redirerr);
231
232             if (!strcmp(argv[0]+1, "debug")) {
233                 opts_debug = true;
234                 continue;
235             }
236             if (!strcmp(argv[0]+1, "dump")) {
237                 opts_dump = true;
238                 continue;
239             }
240             if (!strcmp(argv[0]+1, "memchk")) {
241                 opts_memchk = true;
242                 continue;
243             }
244             if (!strcmp(argv[0]+1, "nocolor")) {
245                 con_color(0);
246                 continue;
247             }
248
249             switch (argv[0][1]) {
250                 /* -h, show usage but exit with 0 */
251                 case 'h':
252                     usage();
253                     exit(0);
254                     break;
255
256                 case 'E':
257                     opts_pp_only = true;
258                     break;
259
260                 /* handle all -fflags */
261                 case 'f':
262                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
263                     if (!strcmp(argv[0]+2, "HELP")) {
264                         con_out("Possible flags:\n");
265                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
266                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
267                             con_out(" -f%s\n", buffer);
268                         }
269                         exit(0);
270                     }
271                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
272                         if (!options_setflag(argv[0]+5, false)) {
273                             con_out("unknown flag: %s\n", argv[0]+2);
274                             return false;
275                         }
276                     }
277                     else if (!options_setflag(argv[0]+2, true)) {
278                         con_out("unknown flag: %s\n", argv[0]+2);
279                         return false;
280                     }
281                     break;
282                 case 'W':
283                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
284                     if (!strcmp(argv[0]+2, "HELP")) {
285                         con_out("Possible warnings:\n");
286                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
287                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
288                             con_out(" -W%s\n", buffer);
289                         }
290                         exit(0);
291                     }
292                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
293                         opts_werror = false;
294                         break;
295                     }
296                     else if (!strcmp(argv[0]+2, "ERROR")) {
297                         opts_werror = true;
298                         break;
299                     }
300                     else if (!strcmp(argv[0]+2, "NONE")) {
301                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
302                             opts_warn[itr] = 0;
303                         break;
304                     }
305                     else if (!strcmp(argv[0]+2, "ALL")) {
306                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
307                             opts_warn[itr] = 0xFFFFFFFFL;
308                         break;
309                     }
310                     if (!strncmp(argv[0]+2, "NO_", 3)) {
311                         if (!options_setwarn(argv[0]+5, false)) {
312                             con_out("unknown warning: %s\n", argv[0]+2);
313                             return false;
314                         }
315                     }
316                     else if (!options_setwarn(argv[0]+2, true)) {
317                         con_out("unknown warning: %s\n", argv[0]+2);
318                         return false;
319                     }
320                     break;
321
322                 case 'O':
323                     if (!options_witharg(&argc, &argv, &argarg)) {
324                         con_out("option -O requires a numerical argument\n");
325                         return false;
326                     }
327                     opts_O = atoi(argarg);
328                     break;
329
330                 case 'o':
331                     if (!options_witharg(&argc, &argv, &argarg)) {
332                         con_out("option -o requires an argument: the output file name\n");
333                         return false;
334                     }
335                     opts_output = argarg;
336                     opts_output_wasset = true;
337                     break;
338
339                 case 'a':
340                 case 's':
341                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
342                     if (!options_witharg(&argc, &argv, &argarg)) {
343                         con_out("option -a requires a filename %s\n",
344                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
345                         return false;
346                     }
347                     item.filename = argarg;
348                     vec_push(items, item);
349                     break;
350
351                 case '-':
352                     if (!argv[0][2]) {
353                         /* anything following -- is considered a non-option argument */
354                         argend = true;
355                         break;
356                     }
357             /* All long options without arguments */
358                     else if (!strcmp(argv[0]+2, "help")) {
359                         usage();
360                         exit(0);
361                     }
362                     else {
363             /* All long options with arguments */
364                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
365                             opts_output = argarg;
366                             opts_output_wasset = true;
367                         } else {
368                             con_out("Unknown parameter: %s\n", argv[0]);
369                             return false;
370                         }
371                     }
372                     break;
373
374                 default:
375                     con_out("Unknown parameter: %s\n", argv[0]);
376                     return false;
377             }
378         }
379         else
380         {
381             /* it's a QC filename */
382             argitem item;
383             item.filename = argv[0];
384             item.type     = TYPE_QC;
385             vec_push(items, item);
386         }
387     }
388     return true;
389 }
390
391 /* returns the line number, or -1 on error */
392 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
393     int    len;
394     char  *line;
395     char  *start;
396     char  *end;
397
398     line = *out;
399     len = util_getline(&line, alen, src);
400     if (len == -1)
401         return false;
402
403     /* start at first non-blank */
404     for (start = line; isspace(*start); ++start) {}
405     /* end at the first non-blank */
406     for (end = start;  *end && !isspace(*end);  ++end)   {}
407
408     *out = line;
409     /* move the actual filename to the beginning */
410     while (start != end) {
411         *line++ = *start++;
412     }
413     *line = 0;
414     return true;
415 }
416
417 int main(int argc, char **argv) {
418     size_t itr;
419     int retval = 0;
420     bool opts_output_free = false;
421     bool progs_src = false;
422     FILE *outfile = NULL;
423
424     app_name = argv[0];
425     con_init();
426
427     /* default options / warn flags */
428     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
429     options_set(opts_warn, WARN_EXTENSIONS, true);
430     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
431     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
432     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
433     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
434     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
435     options_set(opts_warn, WARN_VOID_VARIABLES, true);
436     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
437     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
438     options_set(opts_warn, WARN_FRAME_MACROS, true);
439     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
440     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
441     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
442     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
443     options_set(opts_warn, WARN_PREPROCESSOR, true);
444     options_set(opts_warn, WARN_MULTIFILE_IF, true);
445
446     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
447     options_set(opts_flags, FTEPP, false);
448
449     if (!options_parse(argc, argv)) {
450         return usage();
451     }
452
453     /* the standard decides which set of operators to use */
454     if (opts_standard == COMPILER_GMQCC) {
455         operators = c_operators;
456         operator_count = c_operator_count;
457     } else {
458         operators = qcc_operators;
459         operator_count = qcc_operator_count;
460     }
461
462     if (opts_dump) {
463         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
464             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
465         }
466         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
467             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
468         }
469         con_out("output = %s\n", opts_output);
470         con_out("optimization level = %i\n", (int)opts_O);
471         con_out("standard = %i\n", opts_standard);
472     }
473
474     if (opts_pp_only) {
475         if (opts_output_wasset) {
476             outfile = util_fopen(opts_output, "wb");
477             if (!outfile) {
478                 con_err("failed to open `%s` for writing\n", opts_output);
479                 retval = 1;
480                 goto cleanup;
481             }
482         }
483         else
484             outfile = stdout;
485     }
486
487     if (!opts_pp_only) {
488         if (!parser_init()) {
489             con_err("failed to initialize parser\n");
490             retval = 1;
491             goto cleanup;
492         }
493     }
494     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
495         if (!ftepp_init()) {
496             con_err("failed to initialize parser\n");
497             retval = 1;
498             goto cleanup;
499         }
500     }
501
502     util_debug("COM", "starting ...\n");
503
504     if (!vec_size(items)) {
505         FILE *src;
506         char *line;
507         size_t linelen = 0;
508
509         progs_src = true;
510
511         src = util_fopen("progs.src", "rb");
512         if (!src) {
513             con_err("failed to open `progs.src` for reading\n");
514             retval = 1;
515             goto cleanup;
516         }
517
518         line = NULL;
519         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
520             con_err("illformatted progs.src file: expected output filename in first line\n");
521             retval = 1;
522             goto srcdone;
523         }
524
525         if (!opts_output_wasset) {
526             opts_output = util_strdup(line);
527             opts_output_free = true;
528         }
529
530         while (progs_nextline(&line, &linelen, src)) {
531             argitem item;
532             if (!line[0] || (line[0] == '/' && line[1] == '/'))
533                 continue;
534             item.filename = util_strdup(line);
535             item.type     = TYPE_QC;
536             vec_push(items, item);
537         }
538
539 srcdone:
540         fclose(src);
541         mem_d(line);
542     }
543
544     if (retval)
545         goto cleanup;
546
547     if (vec_size(items)) {
548         if (!opts_pp_only) {
549             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
550             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
551         }
552         for (itr = 0; itr < vec_size(items); ++itr) {
553             if (!opts_pp_only) {
554                 con_out("  item: %s (%s)\n",
555                        items[itr].filename,
556                        ( (items[itr].type == TYPE_QC ? "qc" :
557                          (items[itr].type == TYPE_ASM ? "asm" :
558                          (items[itr].type == TYPE_SRC ? "progs.src" :
559                          ("unknown"))))));
560             }
561
562             if (opts_pp_only) {
563                 if (!ftepp_preprocess_file(items[itr].filename)) {
564                     retval = 1;
565                     goto cleanup;
566                 }
567                 fprintf(outfile, "%s", ftepp_get());
568                 ftepp_flush();
569             }
570             else {
571                 if (OPTS_FLAG(FTEPP)) {
572                     const char *data;
573                     if (!ftepp_preprocess_file(items[itr].filename)) {
574                         retval = 1;
575                         goto cleanup;
576                     }
577                     data = ftepp_get();
578                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
579                         retval = 1;
580                         goto cleanup;
581                     }
582                     ftepp_flush();
583                 }
584                 else {
585                     if (!parser_compile_file(items[itr].filename)) {
586                         retval = 1;
587                         goto cleanup;
588                     }
589                 }
590             }
591
592             if (progs_src) {
593                 mem_d(items[itr].filename);
594                 items[itr].filename = NULL;
595             }
596         }
597
598         ftepp_finish();
599         if (!opts_pp_only) {
600             if (!parser_finish(opts_output)) {
601                 retval = 1;
602                 goto cleanup;
603             }
604         }
605     }
606
607     /* stuff */
608
609 cleanup:
610     util_debug("COM", "cleaning ...\n");
611     ftepp_finish();
612     con_close();
613     vec_free(items);
614
615     if (!opts_pp_only)
616         parser_cleanup();
617     if (opts_output_free)
618         mem_d((char*)opts_output);
619
620     lex_cleanup();
621     util_meminfo();
622     return retval;
623 }