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