]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Replaced it all...
[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 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
27 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
28
29 uint32_t    opts_O        = 1;
30 const char *opts_output   = "progs.dat";
31 int         opts_standard = COMPILER_GMQCC;
32 bool        opts_debug    = false;
33 bool        opts_memchk   = false;
34 bool        opts_dump     = false;
35 bool        opts_werror   = false;
36 bool        opts_forcecrc = false;
37 bool        opts_pp_only  = false;
38 size_t      opts_max_array_size = 1024;
39
40 uint16_t    opts_forced_crc;
41
42 static bool opts_output_wasset = false;
43
44 /* set by the standard */
45 const oper_info *operators      = NULL;
46 size_t           operator_count = 0;
47
48 typedef struct { char *filename; int type; } argitem;
49 static argitem *items = NULL;
50
51 #define TYPE_QC  0
52 #define TYPE_ASM 1
53 #define TYPE_SRC 2
54
55 static const char *app_name;
56
57 static int usage() {
58     con_out("usage: %s [options] [files...]", app_name);
59     con_out("options:\n"
60            "  -h, --help             show this help message\n"
61            "  -debug                 turns on compiler debug messages\n"
62            "  -memchk                turns on compiler memory leak check\n");
63     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
64            "  -a filename            add an asm file to be assembled\n"
65            "  -s filename            add a progs.src file to be used\n");
66     con_out("  -E                     stop after preprocessing\n");
67     con_out("  -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     con_out("  -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     con_out("  -force-crc=num         force a specific checksum into the header\n");
78     con_out("\n");
79     con_out("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 void options_set(uint32_t *flags, size_t idx, bool on)
166 {
167     longbit lb = LONGBIT(idx);
168 #if 0
169     if (on)
170         flags[lb.idx] |= (1<<(lb.bit));
171     else
172         flags[lb.idx] &= ~(1<<(lb.bit));
173 #else
174     if (on)
175         flags[0] |= (1<<(lb));
176     else
177         flags[0] &= ~(1<<(lb));
178 #endif
179 }
180
181 static bool options_parse(int argc, char **argv) {
182     bool argend = false;
183     size_t itr;
184     char  buffer[1024];
185     char *redirout = (char*)stdout;
186     char *redirerr = (char*)stderr;
187
188     while (!argend && argc > 1) {
189         char *argarg;
190         argitem item;
191
192         ++argv;
193         --argc;
194
195         if (argv[0][0] == '-') {
196     /* All gcc-type long options */
197             if (options_long_gcc("std", &argc, &argv, &argarg)) {
198                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
199                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
200                     opts_standard = COMPILER_GMQCC;
201                 } else if (!strcmp(argarg, "qcc")) {
202                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
203                     opts_standard = COMPILER_QCC;
204                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
205                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
206                     opts_standard = COMPILER_FTEQCC;
207                 } else if (!strcmp(argarg, "qccx")) {
208                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
209                     opts_standard = COMPILER_QCCX;
210                 } else {
211                     con_out("Unknown standard: %s\n", argarg);
212                     return false;
213                 }
214                 continue;
215             }
216             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
217                 opts_forcecrc = true;
218                 opts_forced_crc = strtol(argarg, NULL, 0);
219                 continue;
220             }
221             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
222                 continue;
223             }
224             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
225                 continue;
226             }
227
228             if (!strcmp(argv[0]+1, "debug")) {
229                 opts_debug = true;
230                 continue;
231             }
232             if (!strcmp(argv[0]+1, "dump")) {
233                 opts_dump = true;
234                 continue;
235             }
236             if (!strcmp(argv[0]+1, "memchk")) {
237                 opts_memchk = true;
238                 continue;
239             }
240             if (!strcmp(argv[0]+1, "nocolor")) {
241                 con_color(0);
242                 continue;
243             }
244
245             switch (argv[0][1]) {
246                 /* -h, show usage but exit with 0 */
247                 case 'h':
248                     usage();
249                     exit(0);
250                     break;
251
252                 case 'E':
253                     opts_pp_only = true;
254                     break;
255
256                 /* handle all -fflags */
257                 case 'f':
258                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
259                     if (!strcmp(argv[0]+2, "HELP")) {
260                         con_out("Possible flags:\n");
261                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
262                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
263                             con_out(" -f%s\n", buffer);
264                         }
265                         exit(0);
266                     }
267                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
268                         if (!options_setflag(argv[0]+5, false)) {
269                             con_out("unknown flag: %s\n", argv[0]+2);
270                             return false;
271                         }
272                     }
273                     else if (!options_setflag(argv[0]+2, true)) {
274                         con_out("unknown flag: %s\n", argv[0]+2);
275                         return false;
276                     }
277                     break;
278                 case 'W':
279                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
280                     if (!strcmp(argv[0]+2, "HELP")) {
281                         con_out("Possible warnings:\n");
282                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
283                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
284                             con_out(" -W%s\n", buffer);
285                         }
286                         exit(0);
287                     }
288                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
289                         opts_werror = false;
290                         break;
291                     }
292                     else if (!strcmp(argv[0]+2, "ERROR")) {
293                         opts_werror = true;
294                         break;
295                     }
296                     else if (!strcmp(argv[0]+2, "NONE")) {
297                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
298                             opts_warn[itr] = 0;
299                         break;
300                     }
301                     else if (!strcmp(argv[0]+2, "ALL")) {
302                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
303                             opts_warn[itr] = 0xFFFFFFFFL;
304                         break;
305                     }
306                     if (!strncmp(argv[0]+2, "NO_", 3)) {
307                         if (!options_setwarn(argv[0]+5, false)) {
308                             con_out("unknown warning: %s\n", argv[0]+2);
309                             return false;
310                         }
311                     }
312                     else if (!options_setwarn(argv[0]+2, true)) {
313                         con_out("unknown warning: %s\n", argv[0]+2);
314                         return false;
315                     }
316                     break;
317
318                 case 'O':
319                     if (!options_witharg(&argc, &argv, &argarg)) {
320                         con_out("option -O requires a numerical argument\n");
321                         return false;
322                     }
323                     opts_O = atoi(argarg);
324                     break;
325
326                 case 'o':
327                     if (!options_witharg(&argc, &argv, &argarg)) {
328                         con_out("option -o requires an argument: the output file name\n");
329                         return false;
330                     }
331                     opts_output = argarg;
332                     opts_output_wasset = true;
333                     break;
334
335                 case 'a':
336                 case 's':
337                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
338                     if (!options_witharg(&argc, &argv, &argarg)) {
339                         con_out("option -a requires a filename %s\n",
340                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
341                         return false;
342                     }
343                     item.filename = argarg;
344                     vec_push(items, item);
345                     break;
346
347                 case '-':
348                     if (!argv[0][2]) {
349                         /* anything following -- is considered a non-option argument */
350                         argend = true;
351                         break;
352                     }
353             /* All long options without arguments */
354                     else if (!strcmp(argv[0]+2, "help")) {
355                         usage();
356                         exit(0);
357                     }
358                     else {
359             /* All long options with arguments */
360                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
361                             opts_output = argarg;
362                             opts_output_wasset = true;
363                         } else {
364                             con_out("Unknown parameter: %s\n", argv[0]);
365                             return false;
366                         }
367                     }
368                     break;
369
370                 default:
371                     con_out("Unknown parameter: %s\n", argv[0]);
372                     return false;
373             }
374         }
375         else
376         {
377             /* it's a QC filename */
378             argitem item;
379             item.filename = argv[0];
380             item.type     = TYPE_QC;
381             vec_push(items, item);
382         }
383     }
384     con_change(redirout, redirerr);
385     return true;
386 }
387
388 /* returns the line number, or -1 on error */
389 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
390     int    len;
391     char  *line;
392     char  *start;
393     char  *end;
394
395     line = *out;
396     len = util_getline(&line, alen, src);
397     if (len == -1)
398         return false;
399
400     /* start at first non-blank */
401     for (start = line; isspace(*start); ++start) {}
402     /* end at the first non-blank */
403     for (end = start;  *end && !isspace(*end);  ++end)   {}
404
405     *out = line;
406     /* move the actual filename to the beginning */
407     while (start != end) {
408         *line++ = *start++;
409     }
410     *line = 0;
411     return true;
412 }
413
414 int main(int argc, char **argv) {
415     size_t itr;
416     int retval = 0;
417     bool opts_output_free = false;
418
419     app_name = argv[0];
420     con_init();
421
422     /* default options / warn flags */
423     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
424     options_set(opts_warn, WARN_EXTENSIONS, true);
425     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
426     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
427     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
428     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
429     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
430     options_set(opts_warn, WARN_VOID_VARIABLES, true);
431     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
432     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
433     options_set(opts_warn, WARN_FRAME_MACROS, true);
434     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
435     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
436     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
437     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
438
439     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
440
441     if (!options_parse(argc, argv)) {
442         return usage();
443     }
444
445     /* the standard decides which set of operators to use */
446     if (opts_standard == COMPILER_GMQCC) {
447         operators = c_operators;
448         operator_count = c_operator_count;
449     } else {
450         operators = qcc_operators;
451         operator_count = qcc_operator_count;
452     }
453
454     if (opts_dump) {
455         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
456             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
457         }
458         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
459             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
460         }
461         con_out("output = %s\n", opts_output);
462         con_out("optimization level = %i\n", (int)opts_O);
463         con_out("standard = %i\n", opts_standard);
464     }
465
466     if (!parser_init()) {
467         con_out("failed to initialize parser\n");
468         retval = 1;
469         goto cleanup;
470     }
471
472     util_debug("COM", "starting ...\n");
473
474     if (vec_size(items)) {
475         con_out("Mode: manual\n");
476         con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
477         for (itr = 0; itr < vec_size(items); ++itr) {
478             con_out("  item: %s (%s)\n",
479                    items[itr].filename,
480                    ( (items[itr].type == TYPE_QC ? "qc" :
481                      (items[itr].type == TYPE_ASM ? "asm" :
482                      (items[itr].type == TYPE_SRC ? "progs.src" :
483                      ("unknown"))))));
484
485             if (!parser_compile_file(items[itr].filename))
486             {
487                     retval = 1;
488                     goto cleanup;
489             }
490         }
491
492         if (!parser_finish(opts_output)) {
493             retval = 1;
494             goto cleanup;
495         }
496
497     } else {
498         FILE *src;
499         char *line;
500         size_t linelen = 0;
501
502         con_out("Mode: progs.src\n");
503         src = util_fopen("progs.src", "rb");
504         if (!src) {
505             con_out("failed to open `progs.src` for reading\n");
506             retval = 1;
507             goto cleanup;
508         }
509
510         line = NULL;
511         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
512             con_out("illformatted progs.src file: expected output filename in first line\n");
513             retval = 1;
514             goto srcdone;
515         }
516
517         if (!opts_output_wasset) {
518             opts_output = util_strdup(line);
519             opts_output_free = true;
520         }
521
522         while (progs_nextline(&line, &linelen, src)) {
523             if (!line[0] || (line[0] == '/' && line[1] == '/'))
524                 continue;
525             con_out("  src: %s\n", line);
526             if (!parser_compile_file(line)) {
527                 retval = 1;
528                 goto srcdone;
529             }
530         }
531
532         parser_finish(opts_output);
533
534 srcdone:
535         fclose(src);
536         mem_d(line);
537     }
538
539     /* stuff */
540
541 cleanup:
542     util_debug("COM", "cleaning ...\n");
543     con_close();
544     vec_free(items);
545
546     parser_cleanup();
547     if (opts_output_free)
548         mem_d((char*)opts_output);
549
550     lex_cleanup();
551     util_meminfo();
552     return retval;
553 }