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