]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Don't warn about uninitialized stuff in accessors - remove the liferanges calculated...
[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                 /* handle all -fflags */
308                 case 'f':
309                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
310                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
311                         con_out("Possible flags:\n");
312                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
313                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
314                             con_out(" -f%s\n", buffer);
315                         }
316                         exit(0);
317                     }
318                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
319                         if (!options_setflag(argv[0]+5, false)) {
320                             con_out("unknown flag: %s\n", argv[0]+2);
321                             return false;
322                         }
323                     }
324                     else if (!options_setflag(argv[0]+2, true)) {
325                         con_out("unknown flag: %s\n", argv[0]+2);
326                         return false;
327                     }
328                     break;
329                 case 'W':
330                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
331                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
332                         con_out("Possible warnings:\n");
333                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
334                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
335                             con_out(" -W%s\n", buffer);
336                         }
337                         exit(0);
338                     }
339                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
340                         opts_werror = false;
341                         break;
342                     }
343                     else if (!strcmp(argv[0]+2, "ERROR")) {
344                         opts_werror = true;
345                         break;
346                     }
347                     else if (!strcmp(argv[0]+2, "NONE")) {
348                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
349                             opts_warn[itr] = 0;
350                         break;
351                     }
352                     else if (!strcmp(argv[0]+2, "ALL")) {
353                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
354                             opts_warn[itr] = 0xFFFFFFFFL;
355                         break;
356                     }
357                     if (!strncmp(argv[0]+2, "NO_", 3)) {
358                         if (!options_setwarn(argv[0]+5, false)) {
359                             con_out("unknown warning: %s\n", argv[0]+2);
360                             return false;
361                         }
362                     }
363                     else if (!options_setwarn(argv[0]+2, true)) {
364                         con_out("unknown warning: %s\n", argv[0]+2);
365                         return false;
366                     }
367                     break;
368
369                 case 'O':
370                     if (!options_witharg(&argc, &argv, &argarg)) {
371                         con_out("option -O requires a numerical argument, or optimization name with an optional 'no-' prefix\n");
372                         return false;
373                     }
374                     if (isdigit(argarg[0])) {
375                         opts_O = atoi(argarg);
376                         set_optimizations(opts_O);
377                     } else {
378                         util_strtocmd(argarg, argarg, strlen(argarg)+1);
379                         if (!strcmp(argarg, "HELP")) {
380                             con_out("Possible optimizations:\n");
381                             for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
382                                 util_strtononcmd(opts_opt_list[itr].name, buffer, sizeof(buffer));
383                                 con_out(" -O%-20s (-O%u)\n", buffer, opts_opt_oflag[itr]);
384                             }
385                             exit(0);
386                         }
387                         else if (!strcmp(argarg, "ALL"))
388                             set_optimizations(opts_O = 9999);
389                         else if (!strncmp(argarg, "NO_", 3)) {
390                             if (!options_setoptim(argarg+3, false)) {
391                                 con_out("unknown optimization: %s\n", argarg+3);
392                                 return false;
393                             }
394                         }
395                         else {
396                             if (!options_setoptim(argarg, true)) {
397                                 con_out("unknown optimization: %s\n", argarg);
398                                 return false;
399                             }
400                         }
401                     }
402                     break;
403
404                 case 'o':
405                     if (!options_witharg(&argc, &argv, &argarg)) {
406                         con_out("option -o requires an argument: the output file name\n");
407                         return false;
408                     }
409                     opts_output = argarg;
410                     opts_output_wasset = true;
411                     break;
412
413                 case 'a':
414                 case 's':
415                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
416                     if (!options_witharg(&argc, &argv, &argarg)) {
417                         con_out("option -a requires a filename %s\n",
418                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
419                         return false;
420                     }
421                     item.filename = argarg;
422                     vec_push(items, item);
423                     break;
424
425                 case '-':
426                     if (!argv[0][2]) {
427                         /* anything following -- is considered a non-option argument */
428                         argend = true;
429                         break;
430                     }
431             /* All long options without arguments */
432                     else if (!strcmp(argv[0]+2, "help")) {
433                         usage();
434                         exit(0);
435                     }
436                     else {
437             /* All long options with arguments */
438                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
439                             opts_output = argarg;
440                             opts_output_wasset = true;
441                         } else {
442                             con_out("Unknown parameter: %s\n", argv[0]);
443                             return false;
444                         }
445                     }
446                     break;
447
448                 default:
449                     con_out("Unknown parameter: %s\n", argv[0]);
450                     return false;
451             }
452         }
453         else
454         {
455             /* it's a QC filename */
456             item.filename = argv[0];
457             item.type     = TYPE_QC;
458             vec_push(items, item);
459         }
460     }
461     return true;
462 }
463
464 /* returns the line number, or -1 on error */
465 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
466     int    len;
467     char  *line;
468     char  *start;
469     char  *end;
470
471     line = *out;
472     len = util_getline(&line, alen, src);
473     if (len == -1)
474         return false;
475
476     /* start at first non-blank */
477     for (start = line; isspace(*start); ++start) {}
478     /* end at the first non-blank */
479     for (end = start;  *end && !isspace(*end);  ++end)   {}
480
481     *out = line;
482     /* move the actual filename to the beginning */
483     while (start != end) {
484         *line++ = *start++;
485     }
486     *line = 0;
487     return true;
488 }
489
490 int main(int argc, char **argv) {
491     size_t itr;
492     int retval = 0;
493     bool opts_output_free = false;
494     bool operators_free = false;
495     bool progs_src = false;
496     FILE *outfile = NULL;
497
498     app_name = argv[0];
499     con_init();
500
501     /* default options / warn flags */
502     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
503     options_set(opts_warn, WARN_EXTENSIONS, true);
504     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
505     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
506     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
507     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
508     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
509     options_set(opts_warn, WARN_VOID_VARIABLES, true);
510     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
511     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
512     options_set(opts_warn, WARN_FRAME_MACROS, true);
513     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
514     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
515     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
516     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
517     options_set(opts_warn, WARN_PREPROCESSOR, true);
518     options_set(opts_warn, WARN_MULTIFILE_IF, true);
519     options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
520     options_set(opts_warn, WARN_CONST_VAR, true);
521     options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
522
523     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
524     options_set(opts_flags, FTEPP, false);
525     options_set(opts_flags, CORRECT_TERNARY, true);
526
527     if (!options_parse(argc, argv)) {
528         return usage();
529     }
530
531     /* the standard decides which set of operators to use */
532     if (opts_standard == COMPILER_GMQCC) {
533         operators = c_operators;
534         operator_count = c_operator_count;
535     } else if (opts_standard == COMPILER_FTEQCC) {
536         operators = fte_operators;
537         operator_count = fte_operator_count;
538     } else {
539         operators = qcc_operators;
540         operator_count = qcc_operator_count;
541     }
542
543     if (operators == fte_operators) {
544         /* fix ternary? */
545         if (OPTS_FLAG(CORRECT_TERNARY)) {
546             oper_info *newops;
547             if (operators[operator_count-2].id != opid1(',') ||
548                 operators[operator_count-1].id != opid2(':','?'))
549             {
550                 con_err("internal error: operator precedence table wasn't updated correctly!\n");
551                 exit(1);
552             }
553             operators_free = true;
554             newops = mem_a(sizeof(operators[0]) * operator_count);
555             memcpy(newops, operators, sizeof(operators[0]) * operator_count);
556             memcpy(&newops[operator_count-2], &operators[operator_count-1], sizeof(newops[0]));
557             memcpy(&newops[operator_count-1], &operators[operator_count-2], sizeof(newops[0]));
558             newops[operator_count-2].prec = newops[operator_count-1].prec+1;
559             operators = newops;
560         }
561     }
562
563     if (opts_dump) {
564         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
565             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
566         }
567         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
568             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
569         }
570         con_out("output = %s\n", opts_output);
571         con_out("optimization level = %i\n", (int)opts_O);
572         con_out("standard = %i\n", opts_standard);
573     }
574
575     if (opts_pp_only) {
576         if (opts_output_wasset) {
577             outfile = util_fopen(opts_output, "wb");
578             if (!outfile) {
579                 con_err("failed to open `%s` for writing\n", opts_output);
580                 retval = 1;
581                 goto cleanup;
582             }
583         }
584         else
585             outfile = stdout;
586     }
587
588     if (!opts_pp_only) {
589         if (!parser_init()) {
590             con_err("failed to initialize parser\n");
591             retval = 1;
592             goto cleanup;
593         }
594     }
595     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
596         if (!ftepp_init()) {
597             con_err("failed to initialize parser\n");
598             retval = 1;
599             goto cleanup;
600         }
601     }
602
603     util_debug("COM", "starting ...\n");
604
605     if (!vec_size(items)) {
606         FILE *src;
607         char *line;
608         size_t linelen = 0;
609
610         progs_src = true;
611
612         src = util_fopen("progs.src", "rb");
613         if (!src) {
614             con_err("failed to open `progs.src` for reading\n");
615             retval = 1;
616             goto cleanup;
617         }
618
619         line = NULL;
620         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
621             con_err("illformatted progs.src file: expected output filename in first line\n");
622             retval = 1;
623             goto srcdone;
624         }
625
626         if (!opts_output_wasset) {
627             opts_output = util_strdup(line);
628             opts_output_free = true;
629         }
630
631         while (progs_nextline(&line, &linelen, src)) {
632             argitem item;
633             if (!line[0] || (line[0] == '/' && line[1] == '/'))
634                 continue;
635             item.filename = util_strdup(line);
636             item.type     = TYPE_QC;
637             vec_push(items, item);
638         }
639
640 srcdone:
641         fclose(src);
642         mem_d(line);
643     }
644
645     if (retval)
646         goto cleanup;
647
648     if (vec_size(items)) {
649         if (!opts_pp_only) {
650             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
651             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
652         }
653         for (itr = 0; itr < vec_size(items); ++itr) {
654             if (!opts_pp_only) {
655                 con_out("  item: %s (%s)\n",
656                        items[itr].filename,
657                        ( (items[itr].type == TYPE_QC ? "qc" :
658                          (items[itr].type == TYPE_ASM ? "asm" :
659                          (items[itr].type == TYPE_SRC ? "progs.src" :
660                          ("unknown"))))));
661             }
662
663             if (opts_pp_only) {
664                 const char *out;
665                 if (!ftepp_preprocess_file(items[itr].filename)) {
666                     retval = 1;
667                     goto cleanup;
668                 }
669                 out = ftepp_get();
670                 if (out)
671                     fprintf(outfile, "%s", out);
672                 ftepp_flush();
673             }
674             else {
675                 if (OPTS_FLAG(FTEPP)) {
676                     const char *data;
677                     if (!ftepp_preprocess_file(items[itr].filename)) {
678                         retval = 1;
679                         goto cleanup;
680                     }
681                     data = ftepp_get();
682                     if (vec_size(data)) {
683                         if (!parser_compile_string_len(items[itr].filename, data, vec_size(data))) {
684                             retval = 1;
685                             goto cleanup;
686                         }
687                     }
688                     ftepp_flush();
689                 }
690                 else {
691                     if (!parser_compile_file(items[itr].filename)) {
692                         retval = 1;
693                         goto cleanup;
694                     }
695                 }
696             }
697
698             if (progs_src) {
699                 mem_d(items[itr].filename);
700                 items[itr].filename = NULL;
701             }
702         }
703
704         ftepp_finish();
705         if (!opts_pp_only) {
706             if (!parser_finish(opts_output)) {
707                 retval = 1;
708                 goto cleanup;
709             }
710         }
711     }
712
713     /* stuff */
714
715     if (!opts_pp_only) {
716         for (itr = 0; itr < COUNT_OPTIMIZATIONS; ++itr) {
717             if (optimization_count[itr]) {
718                 con_out("%s: %u\n", opts_opt_list[itr].name, (unsigned int)optimization_count[itr]);
719             }
720         }
721     }
722
723 cleanup:
724     util_debug("COM", "cleaning ...\n");
725     ftepp_finish();
726     con_close();
727     vec_free(items);
728
729     if (!opts_pp_only)
730         parser_cleanup();
731     if (opts_output_free)
732         mem_d((char*)opts_output);
733     if (operators_free)
734         mem_d((void*)operators);
735
736     lex_cleanup();
737     util_meminfo();
738     return retval;
739 }