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