improve handling of multiple input files
[cparser] / driver / firm_cmdline.c
1 /**
2  * @file firm_cmdline.c -- Additional Firm generating backend parameters
3  *
4  * Compile when BACK_END_IS_CP_FIRM_BE is defined
5  *
6  * (C) 2005  Michael Beck  beck@ipd.info.uni-karlsruhe.de
7  *
8  * $Id$
9  */
10 #include <string.h>
11 #include "firm_cmdline.h"
12 #include <libfirm/firm.h>
13 #include <libfirm/be.h>
14
15 #if defined(_WIN32) || defined(__CYGWIN__)
16 #define DEFAULT_OS OS_SUPPORT_MINGW
17 #elif defined(__APPLE__)
18 #define DEFAULT_OS OS_SUPPORT_MACHO
19 #else
20 #define DEFAULT_OS OS_SUPPORT_LINUX
21 #endif
22
23 /* optimization settings */
24 struct a_firm_opt firm_opt = {
25   /* enabled         = */ TRUE,
26   /* debug_mode      = */ DBG_MODE_NONE,
27   /* const_folding   = */ TRUE,
28   /* reassoc         = */ TRUE,
29   /* cse             = */ TRUE,
30   /* control_flow    = */ TRUE,
31   /* combo           = */ TRUE,
32   /* gcse            = */ TRUE,
33   /* gvn_pre         = */ FALSE,
34   /* cond_eval       = */ FALSE,
35   /* if_conversion   = */ FALSE,
36   /* func_calls      = */ TRUE,
37   /* do_inline       = */ FALSE,
38   /* auto_inline     = */ TRUE,
39   /* tail_rec        = */ TRUE,
40   /* strength_red    = */ TRUE,
41   /* scalar_replace  = */ TRUE,
42   /* confirm         = */ TRUE,
43   /* muls            = */ TRUE,
44   /* divs            = */ TRUE,
45   /* mods            = */ TRUE,
46   /* fragile_ops     = */ TRUE,
47   /* load_store      = */ TRUE,
48   /* load_store_pre  = */ FALSE,
49   /* modes           = */ FALSE,
50   /* precise_exc     = */ FALSE,        /* never needed for C */
51   /* use_DivMod      = */ FALSE,
52   /* remove_unused   = */ TRUE,
53   /* cloning         = */ FALSE,
54   /* auto_sync       = */ FALSE,
55   /* alias_analysis  = */ TRUE,
56   /* strict_alias    = */ FALSE,
57   /* no_alias        = */ FALSE,
58   /* sync            = */ TRUE,
59   /* deconv          = */ FALSE,
60   /* cc_opt          = */ TRUE,
61   /* bool_opt        = */ FALSE,
62   /* shape_blocks    = */ FALSE,
63   /* freestanding;   = */ FALSE,
64   /* fp_model        = */ fp_model_precise,
65   /* lower_ll        = */ FALSE,
66   /* vrfy            = */ FIRM_VERIFICATION_ON,
67   /* check_all       = */ FALSE,
68   /* lower           = */ TRUE,
69   /* os_support      = */ DEFAULT_OS,
70   /* honor_restrict  = */ TRUE,
71   /* lower_bitfields = */ TRUE,
72   /* pic             = */ FALSE,
73   /* clone_threshold = */ DEFAULT_CLONE_THRESHOLD,
74   /* inline_maxsize  = */ 750,
75   /* inline_threshold= */ 0,
76   /* vrfy_edges      = */ FALSE,
77   /* grs_simd_opt    = */ 0,
78   /* grs_create_pattern = */ 0,
79   /* spare_size      = */ 128,
80 };
81
82 /* dumping options */
83 struct a_firm_dump firm_dump = {
84   /* debug_print  = */ FALSE,
85   /* all_types    = */ FALSE,
86   /* no_blocks    = */ FALSE,
87   /* extbb        = */ FALSE,
88   /* ir_graph     = */ FALSE,
89   /* all_phases   = */ FALSE,
90   /* edge_labels  = */ FALSE,
91   /* statistic    = */ STAT_NONE,
92   /* stat_pattern = */ 0,
93   /* stat_dag     = */ 0,
94   /* gen_firm_asm = */ FALSE,
95   /* filter       = */ NULL
96 };
97
98 #ifdef FIRM_EXT_GRS
99 struct a_firm_ext_grs firm_ext_grs = {
100   /* simd_opt       = */ FALSE,
101   /* create_pattern = */ FALSE
102 };
103 #endif
104
105 struct a_firm_be_opt firm_be_opt = {
106   /* selection = */ BE_FIRM_BE,
107   /* node_stat = */ 0,
108 };
109
110 #define X(a)    a, sizeof(a)-1
111
112 /** Parameter description structure */
113 static const struct params {
114   const char *option;      /**< name of the option */
115   int        opt_len;      /**< length of the option string */
116   a_byte     *flag;        /**< address of variable to set/reset */
117   a_byte     set;          /**< iff true, variable will be set, else reset */
118   const char *description; /**< description of this option */
119 } firm_options[] = {
120   /* this must be first */
121   { X("help"),                   NULL,                       0, "print FCC related help options" },
122
123   /* firm optimization options */
124   { X("pic"),                    &firm_opt.pic,              1, "firm: generate position independent code" },
125   { X("g0"),                     &firm_opt.debug_mode,       DBG_MODE_BACKSTORE, "firm: Debug Mode: use back stores" },
126   { X("g1"),                     &firm_opt.debug_mode,       DBG_MODE_FULL,      "firm: Debug Mode: no register variables" },
127   { X("no-opt"),                 NULL,                       0, "firm: disable all FIRM optimizations" },
128   { X("cse"),                    &firm_opt.cse,              1, "firm: enable common subexpression elimination" },
129   { X("no-cse"),                 &firm_opt.cse,              0, "firm: disable common subexpression elimination" },
130   { X("const-fold"),             &firm_opt.const_folding,    1, "firm: enable constant folding" },
131   { X("no-const-fold"),          &firm_opt.const_folding,    0, "firm: disable constant folding" },
132   { X("control_flow"),           &firm_opt.control_flow,     1, "firm: enable control flow optimization" },
133   { X("no-control-flow"),        &firm_opt.control_flow,     0, "firm: disable control flow optimization" },
134   { X("combo"),                  &firm_opt.combo,            1, "firm: enable combined CCE, UCE and GVN" },
135   { X("no-combo"),               &firm_opt.combo,            0, "firm: disable combined CCE, UCE and GVN" },
136   { X("gcse"),                   &firm_opt.gcse,             1, "firm: enable global common subexpression elimination" },
137   { X("no-gcse"),                &firm_opt.gcse,             0, "firm: disable global common subexpression elimination" },
138   { X("gvn-pre"),                &firm_opt.gvn_pre,          1, "firm: enable GVN partial redundancy elimination" },
139   { X("no-gvn-pre"),             &firm_opt.gvn_pre,          0, "firm: disable GVN partial redundancy elimination" },
140   { X("cond-eval"),              &firm_opt.cond_eval,        1, "firm: enable partial condition evaluation optimization" },
141   { X("no-cond-eval"),           &firm_opt.cond_eval,        0, "firm: disable partial condition evaluation optimization" },
142   { X("if-conv"),                &firm_opt.if_conversion,    1, "firm: enable if-conversion optimization" },
143   { X("no-if-conv"),             &firm_opt.if_conversion,    0, "firm: disable if-conversion optimization" },
144   { X("opt-func-call"),          &firm_opt.func_calls,       1, "firm: enable function call optimization" },
145   { X("no-opt-func-call"),       &firm_opt.func_calls,       0, "firm: disable function call optimization" },
146   { X("reassociation"),          &firm_opt.reassoc,          1, "firm: enable reassociation" },
147   { X("no-reassociation"),       &firm_opt.reassoc,          0, "firm: disable reassociation" },
148   { X("inline"),                 &firm_opt.do_inline,        1, "firm: enable FIRM inlining" },
149   { X("no-inline"),              &firm_opt.do_inline,        0, "firm: disable FIRM inlining" },
150   { X("inline-max-size=<size>"), NULL,                       0, "firm: set maximum size for function inlining" },
151   { X("inline-threshold=<size>"),NULL,                       0, "firm: set benefice threshold for function inlining" },
152   { X("tail-rec"),               &firm_opt.tail_rec,         1, "firm: enable tail-recursion optimization" },
153   { X("no-tail-rec"),            &firm_opt.tail_rec,         0, "firm: disable tail-recursion optimization" },
154   { X("strength-red"),           &firm_opt.strength_red,     1, "firm: enable strength reduction for loops" },
155   { X("no-strength-red"),        &firm_opt.strength_red,     0, "firm: disable strength reduction for loops" },
156   { X("scalar-replace"),         &firm_opt.scalar_replace,   1, "firm: enable scalar replacement" },
157   { X("no-scalar-replace"),      &firm_opt.scalar_replace,   0, "firm: disable scalar replacement" },
158   { X("confirm"),                &firm_opt.confirm,          1, "firm: enable Confirm optimization" },
159   { X("no-confirm"),             &firm_opt.confirm,          0, "firm: disable Confirm optimization" },
160   { X("opt-mul"),                &firm_opt.muls,             0, "firm: enable multiplication optimization" },
161   { X("no-opt-mul"),             &firm_opt.muls,             0, "firm: disable multiplication optimization" },
162   { X("opt-div"),                &firm_opt.divs,             0, "firm: enable division optimization" },
163   { X("no-opt-div"),             &firm_opt.divs,             0, "firm: disable division optimization" },
164   { X("opt-mod"),                &firm_opt.mods,             0, "firm: enable remainder optimization" },
165   { X("no-opt-mod"),             &firm_opt.mods,             0, "firm: disable remainder optimization" },
166   { X("opt-fragile-ops"),        &firm_opt.fragile_ops,      1, "firm: enable fragile ops optimization" },
167   { X("no-opt-fragile-ops"),     &firm_opt.fragile_ops,      0, "firm: disable fragile ops optimization" },
168   { X("opt-load-store"),         &firm_opt.load_store,       1, "firm: enable load store optimization" },
169   { X("no-opt-load-store"),      &firm_opt.load_store,       0, "firm: disable load store optimization" },
170   { X("opt-load-store-pre"),     &firm_opt.load_store_pre,   1, "firm: enable load store optimization and PRE" },
171   { X("no-opt-load-store-pre"),  &firm_opt.load_store_pre,   0, "firm: disable load store optimization and PRE" },
172   { X("opt-modes"),              &firm_opt.modes,            1, "firm: optimize integer modes" },
173   { X("no-opt-modes"),           &firm_opt.modes,            0, "firm: disable integer modes optimization" },
174   { X("sync"),                   &firm_opt.auto_sync,        1, "firm: automatically create Sync nodes" },
175   { X("no-sync"),                &firm_opt.auto_sync,        0, "firm: do not create Sync nodes" },
176   { X("opt-alias"),              &firm_opt.alias_analysis,   1, "firm: enable alias analysis" },
177   { X("no-opt-alias"),           &firm_opt.alias_analysis,   0, "firm: disable alias analysis" },
178   { X("alias"),                  &firm_opt.no_alias,         0, "firm: aliasing occurs" },
179   { X("no-alias"),               &firm_opt.no_alias,         1, "firm: no aliasing occurs" },
180   { X("strict-aliasing"),        &firm_opt.strict_alias,     1, "firm: strict alias rules" },
181   { X("no-strict-aliasing"),     &firm_opt.strict_alias,     0, "firm: strict alias rules" },
182   { X("opt-proc-clone"),         &firm_opt.cloning,          1, "firm: enable procedure cloning" },
183   { X("no-opt-proc-clone"),      &firm_opt.cloning,          0, "firm: disable procedure cloning" },
184   { X("clone-threshold=<value>"),NULL,                       0, "firm: set clone threshold to <value>" },
185   { X("DivMod"),                 &firm_opt.use_DivMod,       1, "firm: use DivMod nodes" },
186   { X("no-DivMod"),              &firm_opt.use_DivMod,       0, "firm: don't use DivMod nodes" },
187   { X("precise-except"),         &firm_opt.precise_exc,      1, "firm: precise exception context" },
188   { X("no-precise-except"),      &firm_opt.precise_exc,      0, "firm: no precise exception context" },
189   { X("remove-unused"),          &firm_opt.remove_unused,    1, "firm: remove unused functions" },
190   { X("no-remove-unused"),       &firm_opt.remove_unused,    0, "firm: dont't remove unused functions" },
191   { X("fp-precise"),             &firm_opt.fp_model,         fp_model_precise, "firm: precise fp model" },
192   { X("fp-fast"),                &firm_opt.fp_model,         fp_model_fast,    "firm: fast fp model" },
193   { X("fp-strict"),              &firm_opt.fp_model,         fp_model_strict,  "firm: strict fp model" },
194   { X("sync"),                   &firm_opt.sync,             1, "firm: use Syncs to remove unnecesary memory dependencies" },
195   { X("no-sync"),                &firm_opt.sync,             0, "firm: do not use Syncs to remove unnecesary memory dependencies" },
196   { X("deconv"),                 &firm_opt.deconv,           1, "firm: enable the conv node optimization" },
197   { X("no-deconv"),              &firm_opt.deconv,           0, "firm: disable the conv node optimization" },
198   { X("opt-cc"),                 &firm_opt.cc_opt,           1, "firm: enable calling conventions optimization" },
199   { X("no-opt-cc"),              &firm_opt.cc_opt,           0, "firm: disable calling conventions optimization" },
200   { X("bool"),                   &firm_opt.bool_opt,         1, "firm: enable bool simplification optimization" },
201   { X("no-bool"),                &firm_opt.bool_opt,         0, "firm: disable bool simplification optimization" },
202   { X("shape-blocks"),           &firm_opt.shape_blocks,     1, "firm: enable block shaping" },
203   { X("no-shape-blocks"),        &firm_opt.shape_blocks,     0, "firm: disable block shaping" },
204   { X("freestanding"),           &firm_opt.freestanding,     1, "firm: freestanding environment" },
205   { X("hosted"),                 &firm_opt.freestanding,     0, "firm: hosted environment" },
206
207   /* other firm regarding options */
208   { X("restrict"),               &firm_opt.honor_restrict,   1, "firm: honor restrict keyword" },
209   { X("no-restrict"),            &firm_opt.honor_restrict,   1, "firm: restrict keyword is meaningless" },
210   { X("no-lower"),               &firm_opt.lower,            0, "firm: disable lowering" },
211   { X("vrfy-off"),               &firm_opt.vrfy,             FIRM_VERIFICATION_OFF, "firm: disable node verification" },
212   { X("vrfy-on"),                &firm_opt.vrfy,             FIRM_VERIFICATION_ON, "firm: enable node verification" },
213   { X("vrfy-report"),            &firm_opt.vrfy,             FIRM_VERIFICATION_REPORT, "firm: node verification, report only" },
214   { X("check-all"),              &firm_opt.check_all,        1, "firm: enable checking all Firm phases" },
215   { X("no-check-all"),           &firm_opt.check_all,        0, "firm: disable checking all Firm phases" },
216   { X("vrfy-edges-on"),          &firm_opt.vrfy_edges,       1, "firm: enable out edge verification" },
217   { X("vrfy-edges-off"),         &firm_opt.vrfy_edges,       0, "firm: disable out edge verification" },
218
219   /* dumping */
220 #if defined(_DEBUG) || defined(FIRM_DEBUG)
221   { X("debug"),                  &firm_dump.debug_print,     1, "firm: enable debug output" },
222 #endif
223
224   { X("dump-ir"),                &firm_dump.ir_graph,        1, "firm: dump IR graph" },
225   { X("dump-all-types"),         &firm_dump.all_types,       1, "firm: dump graph of all types" },
226   { X("dump-no-blocks"),         &firm_dump.no_blocks,       1, "firm: dump non-blocked graph" },
227   { X("dump-extbb"),             &firm_dump.extbb,           1, "firm: dump extended basic blocks" },
228   { X("dump-all-phases"),        &firm_dump.all_phases,      1, "firm: dump graphs for all optimization phases" },
229   { X("dump-edge-labels"),       &firm_dump.edge_labels,     1, "firm: dump edge labels" },
230
231   /* code generation */
232   { X("no-codegen"),             &firm_be_opt.selection,     BE_NONE, "cg: disable code generator" },
233
234 #ifdef FIRM_EXT_GRS
235   { X("grs-simd-opt"),           &firm_ext_grs.simd_opt,                1, "firm: do simd optimization" },
236   { X("grs-create-pattern"),     &firm_ext_grs.create_pattern,  1, "firm: create patterns for simd optimization" },
237   { X("no-grs-simd-opt"),        &firm_ext_grs.simd_opt,                0, "firm: do simd optimization" },
238   { X("no-grs-create-pattern"),  &firm_ext_grs.create_pattern,  0, "firm: create patterns for simd optimization" },
239 #endif
240
241 #ifdef FIRM_BACKEND
242   { X("be-firm"),                &firm_be_opt.selection,     BE_FIRM_BE, "backend: firm backend facility" },
243 #endif /* FIRM_BACKEND */
244 #ifdef FIRM2C_BACKEND
245   { X("be-firm2c"),              &firm_be_opt.selection,     BE_FIRM2C, "backend: firm2C" },
246 #endif /* FIRM2C_BACKEND */
247
248   /* misc */
249   { X("stat-before-opt"),        &firm_dump.statistic,       STAT_BEFORE_OPT,  "misc: Firm statistic output before optimizations" },
250   { X("stat-after-opt"),         &firm_dump.statistic,       STAT_AFTER_OPT,   "misc: Firm statistic output after optimizations" },
251   { X("stat-after-lower"),       &firm_dump.statistic,       STAT_AFTER_LOWER, "misc: Firm statistic output after lowering" },
252   { X("stat-final-ir"),          &firm_dump.statistic,       STAT_FINAL_IR,    "misc: Firm statistic after final optimization" },
253   { X("stat-final"),             &firm_dump.statistic,       STAT_FINAL,       "misc: Firm statistic after code generation" },
254   { X("stat-pattern"),           &firm_dump.stat_pattern,    1, "misc: Firm statistic calculates most used pattern" },
255   { X("stat-dag"),               &firm_dump.stat_dag,        1, "misc: Firm calculates DAG statistics" },
256   { X("firm-asm"),               &firm_dump.gen_firm_asm,    1, "misc: output Firm assembler" },
257   { X("win32"),                  &firm_opt.os_support,       OS_SUPPORT_MINGW, "misc: generate MinGW Win32 code" },
258   { X("mac"),                    &firm_opt.os_support,       OS_SUPPORT_MACHO, "misc: generate MacOS code" },
259   { X("linux"),                  &firm_opt.os_support,       OS_SUPPORT_LINUX, "misc: generate Linux-ELF code" },
260
261   /* string options */
262   { X("dump-filter=<string>"),   NULL,                       0, "misc: set dumper filter" },
263 };
264
265 #undef X
266
267 /** A strdup replacement */
268 static char *StrDup(const char *s) {
269   int   l = strlen(s);
270   char *r = malloc(l+1);
271
272   if (r != NULL)
273     memcpy(r, s, l+1);
274   return r;
275 }
276
277 /**
278  * Set a dumper filter.
279  */
280 static void set_dump_filter(const char *filter)
281 {
282   firm_dump.filter = StrDup(filter);
283 }  /* set_dump_filter */
284
285 /** Disable all optimizations. */
286 static void disable_opts(void) {
287   /* firm_opt.const_folding */
288   firm_opt.reassoc         = FALSE;
289   firm_opt.cse             = FALSE;
290   /* firm_opt.control_flow */
291   firm_opt.gcse            = FALSE;
292   firm_opt.gvn_pre         = FALSE;
293   firm_opt.cond_eval       = FALSE;
294   firm_opt.if_conversion   = FALSE;
295   firm_opt.func_calls      = FALSE;
296   firm_opt.do_inline       = FALSE;
297   firm_opt.auto_inline     = FALSE;
298   firm_opt.tail_rec        = FALSE;
299   firm_opt.strength_red    = FALSE;
300   firm_opt.scalar_replace  = FALSE;
301   firm_opt.confirm         = FALSE;
302   firm_opt.muls            = FALSE;
303   firm_opt.divs            = FALSE;
304   firm_opt.mods            = FALSE;
305   firm_opt.fragile_ops     = FALSE;
306   firm_opt.load_store      = FALSE;
307   firm_opt.remove_unused   = FALSE;
308   /* firm_opt.jmp_tbls */
309   firm_opt.cloning         = FALSE;
310   /* firm_opt.auto_sync */
311   firm_opt.alias_analysis  = FALSE;
312   firm_opt.strict_alias    = FALSE;
313   firm_opt.no_alias        = FALSE;
314   firm_opt.sync            = FALSE;
315   firm_opt.deconv          = FALSE;
316   firm_opt.cc_opt          = FALSE;
317   firm_opt.bool_opt        = FALSE;
318   firm_opt.shape_blocks    = FALSE;
319   firm_opt.freestanding    = TRUE;
320 }  /* disable_opts */
321
322 /**
323  * Handles a firm option.
324  */
325 int firm_option(const char *opt)
326 {
327   int i, len    = strlen(opt);
328   const char *p = opt;
329   int res;
330
331   if (strncmp("dump-filter=", opt, 12) == 0) {
332     opt = &opt[12];
333     set_dump_filter(opt);
334     return 1;
335   }
336   else if (strncmp("clone-threshold=", opt, 16) == 0) {
337     sscanf(&opt[16], "%d", &firm_opt.clone_threshold);
338     firm_opt.cloning = TRUE;
339     return 1;
340   }
341   else if (strncmp("inline-max-size=", opt, 16) == 0) {
342     sscanf(&opt[16], "%u", &firm_opt.inline_maxsize);
343     return 1;
344   }
345   else if (strncmp("inline-threshold=", opt, 17) == 0) {
346     sscanf(&opt[17], "%u", &firm_opt.inline_threshold);
347     return 1;
348   }
349   else if (strcmp("no-opt", opt) == 0) {
350     disable_opts();
351     return 1;
352   }
353
354   for (i = sizeof(firm_options) / sizeof(firm_options[0]) - 1; i >= 0; --i) {
355     if (len == firm_options[i].opt_len && strncmp(p, firm_options[i].option, len) == 0) {
356       if (!firm_options[i].flag) {
357         /* help option */
358         for (i = 0; i < (int) (sizeof(firm_options)/sizeof(firm_options[0])); ++i) {
359           printf("-f %-20s %s\n", firm_options[i].option,
360                  firm_options[i].description);
361         }
362         return -1;
363       }
364       /* statistic options do accumulate */
365       if (firm_options[i].flag == &firm_dump.statistic)
366         *firm_options[i].flag = (a_byte) (*firm_options[i].flag | firm_options[i].set);
367       else
368         *firm_options[i].flag = firm_options[i].set;
369
370       if (firm_options[i].flag == &firm_opt.debug_mode && firm_opt.debug_mode > DBG_MODE_NONE) {
371         if (firm_opt.debug_mode == DBG_MODE_FULL)
372           disable_opts();
373         res = 1;
374         res &= firm_be_option("omitfp=0");
375         res &= firm_be_option("stabs");
376         return res;
377       }
378       break;
379     }
380   }
381
382   if (i < 0)
383     return 0;
384   return 1;
385 }  /* firm_option */
386
387 /**
388  * Handles a firm backend option.
389  *
390  * The options are here only checked for validity and later transmitted
391  * to the firm backend (in the hope they do not fail ...)
392  */
393 int firm_be_option(const char *opt) {
394 #ifdef FIRM_BACKEND
395   return be_parse_arg(opt);
396 #else
397   return 0;
398 #endif /* FIRM_BACKEND */
399 }  /* firm_be_option */
400
401 /**
402  * prints the firm version number
403  */
404 void print_firm_version(FILE *f) {
405   const char *revision = ir_get_version_revision();
406   const char *build    = ir_get_version_build();
407
408   fprintf(f, "Firm C-Compiler using libFirm (%u.%u",
409           ir_get_version_major(), ir_get_version_minor());
410   if (revision[0] != 0) {
411         fputc(' ', f);
412     fputs(revision, f);
413   }
414    if(build[0] != 0) {
415         fputc(' ', f);
416     fputs(build, f);
417   }
418   fprintf(f, "}\n"
419                      "(C) 2005-2008 Michael Beck\n"
420              "(C) 1995-2008 University of Karlsruhe\n"
421              "Using ");
422 }  /* print_firm_version */