Arithmetic exception flag and a plethora of tests.
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012, 2013, 2014
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 <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "lexer.h"
29
30 /* TODO: cleanup this whole file .. it's a fuckign mess */
31
32 /* set by the standard */
33 const oper_info *operators      = NULL;
34 size_t           operator_count = 0;
35 static bool      opts_output_wasset = false;
36
37 typedef struct { char *filename; int   type;  } argitem;
38 typedef struct { char *name;     char *value; } ppitem;
39 static argitem *items = NULL;
40 static ppitem  *ppems = NULL;
41
42 #define TYPE_QC  0
43 #define TYPE_ASM 1
44 #define TYPE_SRC 2
45
46 static const char *app_name;
47
48 static void version(void) {
49     con_out("GMQCC %d.%d.%d Built %s %s\n" GMQCC_DEV_VERSION_STRING,
50         GMQCC_VERSION_MAJOR,
51         GMQCC_VERSION_MINOR,
52         GMQCC_VERSION_PATCH,
53         __DATE__,
54         __TIME__
55     );
56 }
57
58 static int usage(void) {
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             "  -s filename            add a progs.src file to be used\n");
66     con_out("  -E                     stop after preprocessing\n");
67     con_out("  -q, --quiet            be less verbose\n");
68     con_out("  -config file           use the specified ini file\n");
69     con_out("  -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("  -f<flag>               enable a flag\n"
74             "  -fno-<flag>            disable a flag\n"
75             "  -fhelp                 list possible flags\n");
76     con_out("  -W<warning>            enable a warning\n"
77             "  -Wno-<warning>         disable a warning\n"
78             "  -Wall                  enable all warnings\n");
79     con_out("  -Werror                treat warnings as errors\n"
80             "  -Werror-<warning>      treat a warning as error\n"
81             "  -Wno-error-<warning>   opposite of the above\n");
82     con_out("  -Whelp                 list possible warnings\n");
83     con_out("  -O<number>             optimization level\n"
84             "  -O<name>               enable specific optimization\n"
85             "  -Ono-<name>            disable specific optimization\n"
86             "  -Ohelp                 list optimizations\n");
87     con_out("  -force-crc=num         force a specific checksum into the header\n");
88     con_out("  -coverage              add coverage support\n");
89     return -1;
90 }
91
92 /* command line parsing */
93 static bool options_witharg(int *argc_, char ***argv_, char **out) {
94     int  argc   = *argc_;
95     char **argv = *argv_;
96
97     if (argv[0][2]) {
98         *out = argv[0]+2;
99         return true;
100     }
101     /* eat up the next */
102     if (argc < 2) /* no parameter was provided */
103         return false;
104
105     *out = argv[1];
106     --*argc_;
107     ++*argv_;
108     return true;
109 }
110
111 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
112     int  argc   = *argc_;
113     char **argv = *argv_;
114
115     size_t len = strlen(optname);
116
117     if (strncmp(argv[0]+ds, optname, len))
118         return false;
119
120     /* it's --optname, check how the parameter is supplied */
121     if (argv[0][ds+len] == '=') {
122         /* using --opt=param */
123         *out = argv[0]+ds+len+1;
124         return true;
125     }
126
127     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
128         return false;
129
130     /* using --opt param */
131     *out = argv[1];
132     --*argc_;
133     ++*argv_;
134     return true;
135 }
136 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
137     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
138 }
139 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
140     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
141 }
142
143 static bool options_parse(int argc, char **argv) {
144     bool argend = false;
145     size_t itr;
146     char  buffer[1024];
147     char *redirout    = NULL;
148     char *redirerr    = NULL;
149     char *config      = NULL;
150     char *memdumpcols = NULL;
151
152     while (!argend && argc > 1) {
153         char *argarg;
154         argitem item;
155         ppitem  macro;
156
157         ++argv;
158         --argc;
159
160         if (argv[0][0] == '-') {
161             /* All gcc-type long options */
162             if (options_long_gcc("std", &argc, &argv, &argarg)) {
163                 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
164
165                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,          true);
166                     opts_set(opts.flags, CORRECT_LOGIC,                 true);
167                     opts_set(opts.flags, SHORT_LOGIC,                   true);
168                     opts_set(opts.flags, UNTYPED_NIL,                   true);
169                     opts_set(opts.flags, VARIADIC_ARGS,                 true);
170                     opts_set(opts.flags, FALSE_EMPTY_STRINGS,           false);
171                     opts_set(opts.flags, TRUE_EMPTY_STRINGS,            true);
172                     opts_set(opts.flags, LOOP_LABELS,                   true);
173                     opts_set(opts.flags, TRANSLATABLE_STRINGS,          true);
174                     opts_set(opts.flags, INITIALIZED_NONCONSTANTS,      true);
175                     opts_set(opts.werror, WARN_INVALID_PARAMETER_COUNT, true);
176                     opts_set(opts.werror, WARN_MISSING_RETURN_VALUES,   true);
177                     opts_set(opts.flags,  EXPRESSIONS_FOR_BUILTINS,     true);
178                     opts_set(opts.warn,   WARN_BREAKDEF,                true);
179
180
181
182                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_GMQCC;
183
184                 } else if (!strcmp(argarg, "qcc")) {
185
186                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
187                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES, true);
188
189                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCC;
190
191                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
192
193                     opts_set(opts.flags, FTEPP,                    true);
194                     opts_set(opts.flags, TRANSLATABLE_STRINGS,     true);
195                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,     false);
196                     opts_set(opts.flags, ASSIGN_FUNCTION_TYPES,    true);
197                     opts_set(opts.flags, CORRECT_TERNARY,          false);
198                     opts_set(opts.warn, WARN_TERNARY_PRECEDENCE,   true);
199                     opts_set(opts.warn, WARN_BREAKDEF,             true);
200
201                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_FTEQCC;
202
203                 } else if (!strcmp(argarg, "qccx")) {
204
205                     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,  false);
206                     OPTS_OPTION_U32(OPTION_STANDARD) = COMPILER_QCCX;
207
208                 } else {
209                     con_out("Unknown standard: %s\n", argarg);
210                     return false;
211                 }
212                 continue;
213             }
214             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
215
216                 OPTS_OPTION_BOOL(OPTION_FORCECRC)   = true;
217                 OPTS_OPTION_U16 (OPTION_FORCED_CRC) = strtol(argarg, NULL, 0);
218                 continue;
219             }
220             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
221                 con_change(redirout, redirerr);
222                 continue;
223             }
224             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
225                 con_change(redirout, redirerr);
226                 continue;
227             }
228             if (options_long_gcc("config", &argc, &argv, &argarg)) {
229                 config = argarg;
230                 continue;
231             }
232             if (options_long_gcc("memdumpcols", &argc, &argv, &memdumpcols)) {
233                 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = (uint16_t)strtol(memdumpcols, NULL, 10);
234                 continue;
235             }
236             if (options_long_gcc("progsrc", &argc, &argv, &argarg)) {
237                 OPTS_OPTION_STR(OPTION_PROGSRC) = argarg;
238                 continue;
239             }
240
241             /* show defaults (like pathscale) */
242             if (!strcmp(argv[0]+1, "show-defaults")) {
243                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
244                     if (!OPTS_FLAG(itr))
245                         continue;
246
247                     memset(buffer, 0, sizeof(buffer));
248                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
249
250                     con_out("-f%s ", buffer);
251                 }
252                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
253                     if (!OPTS_WARN(itr))
254                         continue;
255
256                     memset(buffer, 0, sizeof(buffer));
257                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
258                     con_out("-W%s ", buffer);
259                 }
260                 con_out("\n");
261                 exit(0);
262             }
263
264             if (!strcmp(argv[0]+1, "debug")) {
265                 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
266                 continue;
267             }
268             if (!strcmp(argv[0]+1, "dump")) {
269                 OPTS_OPTION_BOOL(OPTION_DUMP)  = true;
270                 continue;
271             }
272             if (!strcmp(argv[0]+1, "dumpfin")) {
273                 OPTS_OPTION_BOOL(OPTION_DUMPFIN) = true;
274                 continue;
275             }
276             if (!strcmp(argv[0]+1, "memchk")) {
277                 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
278                 continue;
279             }
280             if (!strcmp(argv[0]+1, "nocolor")) {
281                 con_color(0);
282                 continue;
283             }
284             if (!strcmp(argv[0]+1, "coverage")) {
285                 OPTS_OPTION_BOOL(OPTION_COVERAGE) = true;
286                 continue;
287             }
288
289             switch (argv[0][1]) {
290                 /* -h, show usage but exit with 0 */
291                 case 'h':
292                     usage();
293                     exit(0);
294                     /* break; never reached because of exit(0) */
295
296                 case 'v':
297                     version();
298                     exit(0);
299
300                 case 'E':
301                     OPTS_OPTION_BOOL(OPTION_PP_ONLY) = true;
302                     opts_set(opts.flags, FTEPP_PREDEFS, true); /* predefs on for -E */
303                     break;
304
305                 /* debug turns on -flno */
306                 case 'g':
307                     opts_setflag("LNO", true);
308                     OPTS_OPTION_BOOL(OPTION_G) = true;
309                     break;
310
311                 case 'q':
312                     OPTS_OPTION_BOOL(OPTION_QUIET) = true;
313                     break;
314
315                 case 'D':
316                     if (!strlen(argv[0]+2)) {
317                         con_err("expected name after -D\n");
318                         exit(0);
319                     }
320
321                     if (!(argarg = strchr(argv[0] + 2, '='))) {
322                         macro.name  = util_strdup(argv[0]+2);
323                         macro.value = NULL;
324                     } else {
325                         *argarg='\0'; /* terminate for name */
326                         macro.name  = util_strdup(argv[0]+2);
327                         macro.value = util_strdup(argarg+1);
328                     }
329                     vec_push(ppems, macro);
330                     break;
331
332                 /* handle all -fflags */
333                 case 'f':
334                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
335                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
336                         con_out("Possible flags:\n\n");
337                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
338                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
339                             con_out(" -f%s\n", buffer);
340                         }
341                         exit(0);
342                     }
343                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
344                         if (!opts_setflag(argv[0]+5, false)) {
345                             con_out("unknown flag: %s\n", argv[0]+2);
346                             return false;
347                         }
348                     }
349                     else if (!opts_setflag(argv[0]+2, true)) {
350                         con_out("unknown flag: %s\n", argv[0]+2);
351                         return false;
352                     }
353                     break;
354                 case 'W':
355                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
356                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
357                         con_out("Possible warnings:\n");
358                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
359                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
360                             con_out(" -W%s\n", buffer);
361                             if (itr == WARN_DEBUG)
362                                 con_out("   Warnings included by -Wall:\n");
363                         }
364                         exit(0);
365                     }
366                     else if (!strcmp(argv[0]+2, "NO_ERROR") ||
367                              !strcmp(argv[0]+2, "NO_ERROR_ALL"))
368                     {
369                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
370                             opts.werror[itr] = 0;
371                         break;
372                     }
373                     else if (!strcmp(argv[0]+2, "ERROR") ||
374                              !strcmp(argv[0]+2, "ERROR_ALL"))
375                     {
376                         opts_backup_non_Werror_all();
377                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.werror); ++itr)
378                             opts.werror[itr] = 0xFFFFFFFFL;
379                         opts_restore_non_Werror_all();
380                         break;
381                     }
382                     else if (!strcmp(argv[0]+2, "NONE")) {
383                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
384                             opts.warn[itr] = 0;
385                         break;
386                     }
387                     else if (!strcmp(argv[0]+2, "ALL")) {
388                         opts_backup_non_Wall();
389                         for (itr = 0; itr < GMQCC_ARRAY_COUNT(opts.warn); ++itr)
390                             opts.warn[itr] = 0xFFFFFFFFL;
391                         opts_restore_non_Wall();
392                         break;
393                     }
394                     else if (!strncmp(argv[0]+2, "ERROR_", 6)) {
395                         if (!opts_setwerror(argv[0]+8, true)) {
396                             con_out("unknown warning: %s\n", argv[0]+2);
397                             return false;
398                         }
399                     }
400                     else if (!strncmp(argv[0]+2, "NO_ERROR_", 9)) {
401                         if (!opts_setwerror(argv[0]+11, false)) {
402                             con_out("unknown warning: %s\n", argv[0]+2);
403                             return false;
404                         }
405                     }
406                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
407                         if (!opts_setwarn(argv[0]+5, false)) {
408                             con_out("unknown warning: %s\n", argv[0]+2);
409                             return false;
410                         }
411                     }
412                     else if (!opts_setwarn(argv[0]+2, true)) {
413                         con_out("unknown warning: %s\n", argv[0]+2);
414                         return false;
415                     }
416                     break;
417
418                 case 'O':
419                     if (!options_witharg(&argc, &argv, &argarg)) {
420                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
421                         return false;
422                     }
423                     if (util_isdigit(argarg[0])) {
424                         uint32_t val = (uint32_t)strtol(argarg, NULL, 10);
425                         OPTS_OPTION_U32(OPTION_O) = val;
426                         opts_setoptimlevel(val);
427                     } else {
428                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
429                         if (!strcmp(argarg, "HELP")) {
430                             con_out("Possible optimizations:\n");
431                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
432                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
433                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
434                             }
435                             exit(0);
436                         }
437                         else if (!strcmp(argarg, "ALL"))
438                             opts_setoptimlevel(OPTS_OPTION_U32(OPTION_O) = 9999);
439                         else if (!strncmp(argarg, "NO_", 3)) {
440                             /* constant folding cannot be turned off for obvious reasons */
441                             if (!strcmp(argarg, "NO_CONST_FOLD") || !opts_setoptim(argarg+3, false)) {
442                                 con_out("unknown optimization: %s\n", argarg+3);
443                                 return false;
444                             }
445                         }
446                         else {
447                             if (!opts_setoptim(argarg, true)) {
448                                 con_out("unknown optimization: %s\n", argarg);
449                                 return false;
450                             }
451                         }
452                     }
453                     break;
454
455                 case 'o':
456                     if (!options_witharg(&argc, &argv, &argarg)) {
457                         con_out("option -o requires an argument: the output file name\n");
458                         return false;
459                     }
460                     OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
461                     opts_output_wasset = true;
462                     break;
463
464                 case 'a':
465                 case 's':
466                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
467                     if (!options_witharg(&argc, &argv, &argarg)) {
468                         con_out("option -a requires a filename %s\n",
469                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
470                         return false;
471                     }
472                     item.filename = argarg;
473                     vec_push(items, item);
474                     break;
475
476                 case '-':
477                     if (!argv[0][2]) {
478                         /* anything following -- is considered a non-option argument */
479                         argend = true;
480                         break;
481                     }
482             /* All long options without arguments */
483                     else if (!strcmp(argv[0]+2, "help")) {
484                         usage();
485                         exit(0);
486                     }
487                     else if (!strcmp(argv[0]+2, "version")) {
488                         version();
489                         exit(0);
490                     }
491                     else if (!strcmp(argv[0]+2, "quiet")) {
492                         OPTS_OPTION_BOOL(OPTION_QUIET) = true;
493                         break;
494                     }
495                     else if (!strcmp(argv[0]+2, "correct")) {
496                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
497                         break;
498                     }
499                     else if (!strcmp(argv[0]+2, "no-correct")) {
500                         OPTS_OPTION_BOOL(OPTION_CORRECTION) = false;
501                         break;
502                     }
503                     else if (!strcmp(argv[0]+2, "add-info")) {
504                         OPTS_OPTION_BOOL(OPTION_ADD_INFO) = true;
505                         break;
506                     }
507                     else {
508             /* All long options with arguments */
509                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
510                             OPTS_OPTION_STR(OPTION_OUTPUT) = argarg;
511                             opts_output_wasset = true;
512                         } else {
513                             con_out("Unknown parameter: %s\n", argv[0]);
514                             return false;
515                         }
516                     }
517                     break;
518
519                 default:
520                     con_out("Unknown parameter: %s\n", argv[0]);
521                     return false;
522             }
523         }
524         else
525         {
526             /* it's a QC filename */
527             item.filename = argv[0];
528             item.type     = TYPE_QC;
529             vec_push(items, item);
530         }
531     }
532     opts_ini_init(config);
533     return true;
534 }
535
536 /* returns the line number, or -1 on error */
537 static bool progs_nextline(char **out, size_t *alen, fs_file_t *src) {
538     int    len;
539     char  *line;
540     char  *start;
541     char  *end;
542
543     line = *out;
544     len  = fs_file_getline(&line, alen, src);
545     if (len == -1)
546         return false;
547
548     /* start at first non-blank */
549     for (start = line; util_isspace(*start); ++start) {}
550     /* end at the first non-blank */
551     for (end = start; *end && !util_isspace(*end);  ++end)   {}
552
553     *out = line;
554     /* move the actual filename to the beginning */
555     while (start != end) {
556         *line++ = *start++;
557     }
558     *line = 0;
559     return true;
560 }
561
562 int main(int argc, char **argv) {
563     size_t          itr;
564     int             retval           = 0;
565     bool            operators_free   = false;
566     bool            progs_src        = false;
567     fs_file_t       *outfile         = NULL;
568     struct parser_s *parser          = NULL;
569     struct ftepp_s  *ftepp           = NULL;
570
571     app_name = argv[0];
572     con_init ();
573     opts_init("progs.dat", COMPILER_QCC, (1024 << 3));
574
575     util_seed(time(0));
576
577     if (!options_parse(argc, argv)) {
578         return usage();
579     }
580
581     if (OPTS_FLAG(TRUE_EMPTY_STRINGS) && OPTS_FLAG(FALSE_EMPTY_STRINGS)) {
582         con_err("-ftrue-empty-strings and -ffalse-empty-strings are mutually exclusive");
583         exit(EXIT_FAILURE);
584     }
585
586     /* the standard decides which set of operators to use */
587     if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
588         operators      = c_operators;
589         operator_count = GMQCC_ARRAY_COUNT(c_operators);
590     } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
591         operators      = fte_operators;
592         operator_count = GMQCC_ARRAY_COUNT(fte_operators);
593     } else {
594         operators      = qcc_operators;
595         operator_count = GMQCC_ARRAY_COUNT(qcc_operators);
596     }
597
598     if (operators == fte_operators) {
599         /* fix ternary? */
600         if (OPTS_FLAG(CORRECT_TERNARY)) {
601             oper_info *newops;
602             if (operators[operator_count-2].id != opid1(',') ||
603                 operators[operator_count-1].id != opid2(':','?'))
604             {
605                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
606                 exit(EXIT_FAILURE);
607             }
608             operators_free = true;
609             newops = (oper_info*)mem_a(sizeof(operators[0]) * operator_count);
610             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
611             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
612             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
613             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
614             operators = newops;
615         }
616     }
617
618     if (OPTS_OPTION_BOOL(OPTION_DUMP)) {
619         for (itr = 0; itr < COUNT_FLAGS; ++itr)
620             con_out("Flag %s = %i\n",    opts_flag_list[itr].name, OPTS_FLAG(itr));
621         for (itr = 0; itr < COUNT_WARNINGS; ++itr)
622             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
623
624         con_out("output             = %s\n", OPTS_OPTION_STR(OPTION_OUTPUT));
625         con_out("optimization level = %u\n", OPTS_OPTION_U32(OPTION_O));
626         con_out("standard           = %u\n", OPTS_OPTION_U32(OPTION_STANDARD));
627     }
628
629     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
630         if (opts_output_wasset) {
631             outfile = fs_file_open(OPTS_OPTION_STR(OPTION_OUTPUT), "wb");
632             if (!outfile) {
633                 con_err("failed to open `%s` for writing\n", OPTS_OPTION_STR(OPTION_OUTPUT));
634                 retval = 1;
635                 goto cleanup;
636             }
637         }
638         else {
639             outfile = con_default_out();
640         }
641     }
642
643     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
644         if (!(parser = parser_create())) {
645             con_err("failed to initialize parser\n");
646             retval = 1;
647             goto cleanup;
648         }
649     }
650
651     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
652         if (!(ftepp = ftepp_create())) {
653             con_err("failed to initialize parser\n");
654             retval = 1;
655             goto cleanup;
656         }
657     }
658
659     /* add macros */
660     if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
661         for (itr = 0; itr < vec_size(ppems); itr++) {
662             ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
663             mem_d(ppems[itr].name);
664
665             /* can be null */
666             if (ppems[itr].value)
667                 mem_d(ppems[itr].value);
668         }
669     }
670
671     if (!vec_size(items)) {
672         fs_file_t *src;
673         char      *line    = NULL;
674         size_t     linelen = 0;
675         bool       hasline = false;
676
677         progs_src = true;
678
679         src = fs_file_open(OPTS_OPTION_STR(OPTION_PROGSRC), "rb");
680         if (!src) {
681             con_err("failed to open `%s` for reading\n", OPTS_OPTION_STR(OPTION_PROGSRC));
682             retval = 1;
683             goto cleanup;
684         }
685
686         while (progs_nextline(&line, &linelen, src)) {
687             argitem item;
688
689             if (!line[0] || (line[0] == '/' && line[1] == '/'))
690                 continue;
691
692             if (hasline) {
693                 item.filename = util_strdup(line);
694                 item.type     = TYPE_QC;
695                 vec_push(items, item);
696             } else if (!opts_output_wasset) {
697                 OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
698                 hasline                        = true;
699             }
700         }
701
702         fs_file_close(src);
703         mem_d(line);
704     }
705
706     if (vec_size(items)) {
707         if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
708             !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
709         {
710             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
711             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
712         }
713
714         for (itr = 0; itr < vec_size(items); ++itr) {
715             if (!OPTS_OPTION_BOOL(OPTION_QUIET) &&
716                 !OPTS_OPTION_BOOL(OPTION_PP_ONLY))
717             {
718                 con_out("  item: %s (%s)\n",
719                        items[itr].filename,
720                        ( (items[itr].type == TYPE_QC ? "qc" :
721                          (items[itr].type == TYPE_ASM ? "asm" :
722                          (items[itr].type == TYPE_SRC ? "progs.src" :
723                          ("unknown"))))));
724             }
725
726             if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
727                 const char *out;
728                 if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
729                     retval = 1;
730                     goto cleanup;
731                 }
732                 out = ftepp_get(ftepp);
733                 if (out)
734                     fs_file_printf(outfile, "%s", out);
735                 ftepp_flush(ftepp);
736             }
737             else {
738                 if (OPTS_FLAG(FTEPP)) {
739                     const char *data;
740                     if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
741                         retval = 1;
742                         goto cleanup;
743                     }
744                     data = ftepp_get(ftepp);
745                     if (vec_size(data)) {
746                         if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
747                             retval = 1;
748                             goto cleanup;
749                         }
750                     }
751                     ftepp_flush(ftepp);
752                 }
753                 else {
754                     if (!parser_compile_file(parser, items[itr].filename)) {
755                         retval = 1;
756                         goto cleanup;
757                     }
758                 }
759             }
760
761             if (progs_src) {
762                 mem_d(items[itr].filename);
763                 items[itr].filename = NULL;
764             }
765         }
766
767         ftepp_finish(ftepp);
768         ftepp = NULL;
769         if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
770             if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
771                 retval = 1;
772                 goto cleanup;
773             }
774         }
775     }
776
777 cleanup:
778     if (ftepp)
779         ftepp_finish(ftepp);
780     con_close();
781     vec_free(items);
782     vec_free(ppems);
783
784     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
785         if(parser) parser_cleanup(parser);
786
787     /* free allocated option strings */
788     for (itr = 0; itr < OPTION_COUNT; itr++)
789         if (OPTS_OPTION_DUPED(itr))
790             mem_d(OPTS_OPTION_STR(itr));
791
792     if (operators_free)
793         mem_d((void*)operators);
794
795     lex_cleanup();
796     stat_info();
797
798     if (!retval && compile_errors)
799         retval = 1;
800     return retval;
801 }