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