955813d55d54987900c801310f3f59485d3931f7
[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   /* func_calls      = */ TRUE,
28   /* do_inline       = */ FALSE,
29   /* auto_inline     = */ TRUE,
30   /* tail_rec        = */ TRUE,
31   /* strength_red    = */ TRUE,
32   /* scalar_replace  = */ TRUE,
33   /* confirm         = */ TRUE,
34   /* muls            = */ TRUE,
35   /* divs            = */ TRUE,
36   /* mods            = */ TRUE,
37   /* fragile_ops     = */ TRUE,
38   /* load_store      = */ TRUE,
39   /* modes           = */ FALSE,
40   /* precise_exc     = */ FALSE,        /* never needed for C */
41   /* use_DivMod      = */ FALSE,
42   /* remove_unused   = */ TRUE,
43   /* jmp_tbls        = */ TRUE,
44   /* cloning         = */ FALSE,
45   /* auto_sync       = */ TRUE,
46   /* alias_analysis  = */ TRUE,
47   /* strict_alias    = */ FALSE,
48   /* no_alias        = */ FALSE,
49   /* luffig          = */ FALSE,
50   /* deconv          = */ FALSE,
51   /* cc_opt          = */ TRUE,
52   /* bool_opt        = */ FALSE,
53   /* freestanding;   = */ FALSE,
54   /* fp_model        = */ fp_model_precise,
55   /* lower_ll        = */ FALSE,
56   /* vrfy            = */ FIRM_VERIFICATION_ON,
57   /* check_all       = */ FALSE,
58   /* lower           = */ TRUE,
59   /* os_support      = */ OS_SUPPORT_LINUX,
60   /* honor_restrict  = */ TRUE,
61   /* lower_bitfields = */ TRUE,
62   /* pic             = */ FALSE,
63   /* ycomp_dbg       = */ FALSE,
64   /* ycomp_host      = */ FIRM_YCOMP_DEFAULT_HOST,
65   /* ycomp_port      = */ FIRM_YCOMP_DEFAULT_PORT,
66   /* clone_threshold = */ DEFAULT_CLONE_THRESHOLD,
67   /* vrfy_edges      = */ FALSE,
68   /* grs_simd_opt    = */ 0,
69   /* grs_create_pattern = */ 0,
70   /* spare_size      = */ 128,
71 };
72
73 /* dumping options */
74 struct a_firm_dump firm_dump = {
75   /* debug_print  = */ FALSE,
76   /* all_types    = */ FALSE,
77   /* no_blocks    = */ FALSE,
78   /* extbb        = */ FALSE,
79   /* ir_graph     = */ FALSE,
80   /* all_phases   = */ FALSE,
81   /* edge_labels  = */ FALSE,
82   /* statistic    = */ STAT_NONE,
83   /* stat_pattern = */ 0,
84   /* stat_dag     = */ 0,
85   /* gen_firm_asm = */ FALSE,
86   /* filter       = */ NULL
87 };
88
89 #ifdef FIRM_EXT_GRS
90 struct a_firm_ext_grs firm_ext_grs = {
91   /* simd_opt       = */ FALSE,
92   /* create_pattern = */ FALSE
93 };
94 #endif
95
96 struct a_firm_be_opt firm_be_opt = {
97   /* selection = */ BE_FIRM_BE,
98   /* node_stat = */ 0,
99 };
100
101 #define X(a)    a, sizeof(a)-1
102
103 /** Parameter description structure */
104 static const struct params {
105   const char *option;      /**< name of the option */
106   int        opt_len;      /**< length of the option string */
107   a_byte     *flag;        /**< address of variable to set/reset */
108   a_byte     set;          /**< iff true, variable will be set, else reset */
109   const char *description; /**< description of this option */
110 } firm_options[] = {
111   /* this must be first */
112   { X("help"),                   NULL,                       0, "print FCC related help options" },
113
114   /* firm optimization options */
115   { X("pic"),                    &firm_opt.pic,              1, "firm: generate position independent code" },
116   { X("g0"),                     &firm_opt.debug_mode,       DBG_MODE_BACKSTORE, "firm: Debug Mode: use back stores" },
117   { X("g1"),                     &firm_opt.debug_mode,       DBG_MODE_FULL,      "firm: Debug Mode: no register variables" },
118   { X("no-opt"),                 NULL,                       0, "firm: disable all FIRM optimizations" },
119   { X("cse"),                    &firm_opt.cse,              1, "firm: enable common subexpression elimination" },
120   { X("no-cse"),                 &firm_opt.cse,              0, "firm: disable common subexpression elimination" },
121   { X("const-fold"),             &firm_opt.const_folding,    1, "firm: enable constant folding" },
122   { X("no-const-fold"),          &firm_opt.const_folding,    0, "firm: disable constant folding" },
123   { X("control_flow"),           &firm_opt.control_flow,     1, "firm: enable control flow optimization" },
124   { X("no-control-flow"),        &firm_opt.control_flow,     0, "firm: disable control flow optimization" },
125   { X("code-place"),             &firm_opt.code_place,       1, "firm: enable GCSE and code placement" },
126   { X("no-code-place"),          &firm_opt.code_place,       0, "firm: disable GCSE and code placement" },
127   { X("gvn-pre"),                &firm_opt.gvn_pre,          1, "firm: enable GVN partial redundancy elimination" },
128   { X("no-gvn-pre"),             &firm_opt.gvn_pre,          0, "firm: disable GVN partial redundancy elimination" },
129   { X("cond-eval"),              &firm_opt.cond_eval,        1, "firm: enable partial condition evaluation optimization" },
130   { X("no-cond-eval"),           &firm_opt.cond_eval,        0, "firm: disable partial condition evaluation optimization" },
131   { X("if-conv"),                &firm_opt.if_conversion,    1, "firm: enable if-conversion optimization" },
132   { X("no-if-conv"),             &firm_opt.if_conversion,    0, "firm: disable if-conversion 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.func_calls      = FALSE;
290   firm_opt.do_inline       = FALSE;
291   firm_opt.auto_inline     = FALSE;
292   firm_opt.tail_rec        = FALSE;
293   firm_opt.strength_red    = FALSE;
294   firm_opt.scalar_replace  = FALSE;
295   firm_opt.confirm         = FALSE;
296   firm_opt.muls            = FALSE;
297   firm_opt.divs            = FALSE;
298   firm_opt.mods            = FALSE;
299   firm_opt.fragile_ops     = FALSE;
300   firm_opt.load_store      = FALSE;
301   firm_opt.remove_unused   = FALSE;
302   /* firm_opt.jmp_tbls */
303   firm_opt.cloning         = FALSE;
304   /* firm_opt.auto_sync */
305   firm_opt.alias_analysis  = FALSE;
306   firm_opt.strict_alias    = FALSE;
307   firm_opt.no_alias        = FALSE;
308   firm_opt.luffig          = FALSE;
309   firm_opt.deconv          = FALSE;
310   firm_opt.cc_opt          = FALSE;
311   firm_opt.bool_opt        = FALSE;
312   firm_opt.freestanding    = TRUE;
313 }  /* disable_opts */
314
315 /**
316  * Handles a firm option.
317  */
318 int firm_option(const char *opt)
319 {
320   int i, len    = strlen(opt);
321   const char *p = opt;
322   int res;
323
324   if (strncmp("dump-filter=", opt, 12) == 0) {
325     opt = &opt[12];
326     set_dump_filter(opt);
327     return 1;
328   }
329   else if (strncmp("clone-threshold=", opt, 16) == 0) {
330     sscanf(&opt[16], "%d", &firm_opt.clone_threshold);
331     firm_opt.cloning = TRUE;
332     return 1;
333   }
334   else if (strncmp("ycomp-host=", opt, 11) == 0) {
335     opt = &opt[11];
336     set_ycomp_host(opt);
337     return 1;
338   }
339   else if (strncmp("ycomp-port=", opt, 11) == 0) {
340     sscanf(&opt[11], "%d", &firm_opt.ycomp_port);
341     return 1;
342   }
343   else if (strcmp("no-opt", opt) == 0) {
344     disable_opts();
345     return 1;
346   }
347
348   for (i = sizeof(firm_options) / sizeof(firm_options[0]) - 1; i >= 0; --i) {
349     if (len == firm_options[i].opt_len && strncmp(p, firm_options[i].option, len) == 0) {
350       if (!firm_options[i].flag) {
351         /* help option */
352         for (i = 0; i < (int) (sizeof(firm_options)/sizeof(firm_options[0])); ++i) {
353           printf("-f %-20s %s\n", firm_options[i].option,
354                  firm_options[i].description);
355         }
356         return -1;
357       }
358       /* statistic options do accumulate */
359       if (firm_options[i].flag == &firm_dump.statistic)
360         *firm_options[i].flag = (a_byte) (*firm_options[i].flag | firm_options[i].set);
361       else
362         *firm_options[i].flag = firm_options[i].set;
363
364       if (firm_options[i].flag == &firm_opt.debug_mode && firm_opt.debug_mode > DBG_MODE_NONE) {
365         if (firm_opt.debug_mode == DBG_MODE_FULL)
366           disable_opts();
367         res = 1;
368 #ifdef FIRM_BACKEND
369         res &= firm_be_option("omitfp=0");
370         res &= firm_be_option("stabs");
371 #endif /* FIRM_BACKEND */
372         return res;
373       }
374 #ifdef FIRM_BACKEND
375       /* OS option must be set to the backend */
376       else if (firm_options[i].flag == &firm_opt.os_support)
377         firm_be_option(firm_opt.os_support == OS_SUPPORT_MINGW ?
378           "ia32-gasmode=mingw" : "ia32-gasmode=linux");
379 #endif /* FIRM_BACKEND */
380       break;
381     }
382   }
383
384   if (i < 0)
385     return 0;
386   return 1;
387 }  /* firm_option */
388
389 /**
390  * Handles a firm backend option.
391  *
392  * The options are here only checked for validity and later transmitted
393  * to the firm backend (in the hope they do not fail ...)
394  */
395 int firm_be_option(const char *opt) {
396 #ifdef FIRM_BACKEND
397   return be_parse_arg(opt);
398 #else
399   return 0;
400 #endif /* FIRM_BACKEND */
401 }  /* firm_be_option */
402
403 /**
404  * prints the firm version number
405  */
406 void print_firm_version(FILE *f) {
407   firm_version_t version;
408
409   firm_get_version(&version);
410
411   fprintf(f, "Firm C-Compiler using libFirm (%u.%u", version.major, version.minor);
412   if (version.revision[0] != 0) {
413         fputc(' ', f);
414     fputs(version.revision, f);
415   }
416    if(version.build[0] != 0) {
417         fputc(' ', f);
418     fputs(version.build, f);
419   }
420   fprintf(f, "}\n"
421                      "(C) 2005-2008 Michael Beck\n"
422              "(C) 1995-2008 University of Karlsruhe\n"
423              "Using ");
424 }  /* print_firm_version */