test the build version
[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   /* 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   a_byte     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-func-call"),          &firm_opt.func_calls,       1, "firm: enable function call optimization" },
132   { X("no-opt-func-call"),       &firm_opt.func_calls,       0, "firm: disable function call optimization" },
133   { X("reassociation"),          &firm_opt.reassoc,          1, "firm: enable reassociation" },
134   { X("no-reassociation"),       &firm_opt.reassoc,          0, "firm: disable reassociation" },
135   { X("inline"),                 &firm_opt.do_inline,        1, "firm: enable FIRM inlining" },
136   { X("no-inline"),              &firm_opt.do_inline,        0, "firm: disable FIRM inlining" },
137   { X("tail-rec"),               &firm_opt.tail_rec,         1, "firm: enable tail-recursion optimization" },
138   { X("no-tail-rec"),            &firm_opt.tail_rec,         0, "firm: disable tail-recursion optimization" },
139   { X("strength-red"),           &firm_opt.strength_red,     1, "firm: enable strength reduction for loops" },
140   { X("no-strength-red"),        &firm_opt.strength_red,     0, "firm: disable strength reduction for loops" },
141   { X("scalar-replace"),         &firm_opt.scalar_replace,   1, "firm: enable scalar replacement" },
142   { X("no-scalar-replace"),      &firm_opt.scalar_replace,   0, "firm: disable scalar replacement" },
143   { X("confirm"),                &firm_opt.confirm,          1, "firm: enable Confirm optimization" },
144   { X("no-confirm"),             &firm_opt.confirm,          0, "firm: disable Confirm optimization" },
145   { X("opt-mul"),                &firm_opt.muls,             0, "firm: enable multiplication optimization" },
146   { X("no-opt-mul"),             &firm_opt.muls,             0, "firm: disable multiplication optimization" },
147   { X("opt-div"),                &firm_opt.divs,             0, "firm: enable division optimization" },
148   { X("no-opt-div"),             &firm_opt.divs,             0, "firm: disable division optimization" },
149   { X("opt-mod"),                &firm_opt.mods,             0, "firm: enable remainder optimization" },
150   { X("no-opt-mod"),             &firm_opt.mods,             0, "firm: disable remainder optimization" },
151   { X("opt-fragile-ops"),        &firm_opt.fragile_ops,      1, "firm: enable fragile ops optimization" },
152   { X("no-opt-fragile-ops"),     &firm_opt.fragile_ops,      0, "firm: disable fragile ops optimization" },
153   { X("opt-load-store"),         &firm_opt.load_store,       1, "firm: enable load store optimization" },
154   { X("no-opt-load-store"),      &firm_opt.load_store,       0, "firm: disable load store optimization" },
155   { X("opt-modes"),              &firm_opt.modes,            1, "firm: optimize integer modes" },
156   { X("no-opt-modes"),           &firm_opt.modes,            0, "firm: disable integer modes optimization" },
157   { X("jmptbls"),                &firm_opt.jmp_tbls,         1, "firm: create jump table for switch" },
158   { X("no-jmptbls"),             &firm_opt.jmp_tbls,         0, "firm: do not create jump table for switch" },
159   { X("sync"),                   &firm_opt.auto_sync,        1, "firm: automatically create Sync nodes" },
160   { X("no-sync"),                &firm_opt.auto_sync,        0, "firm: do not create Sync nodes" },
161   { X("opt-alias"),              &firm_opt.alias_analysis,   1, "firm: enable alias analysis" },
162   { X("no-opt-alias"),           &firm_opt.alias_analysis,   0, "firm: disable alias analysis" },
163   { X("alias"),                  &firm_opt.no_alias,         0, "firm: aliasing occurs" },
164   { X("no-alias"),               &firm_opt.no_alias,         1, "firm: no aliasing occurs" },
165   { X("strict-aliasing"),        &firm_opt.strict_alias,     1, "firm: strict alias rules" },
166   { X("no-strict-aliasing"),     &firm_opt.strict_alias,     0, "firm: strict alias rules" },
167   { X("opt-proc-clone"),         &firm_opt.cloning,          1, "firm: enable procedure cloning" },
168   { X("no-opt-proc-clone"),      &firm_opt.cloning,          0, "firm: disable procedure cloning" },
169   { X("clone-threshold=<value>"),NULL,                       0, "firm: set clone threshold to <value>" },
170   { X("DivMod"),                 &firm_opt.use_DivMod,       1, "firm: use DivMod nodes" },
171   { X("no-DivMod"),              &firm_opt.use_DivMod,       0, "firm: don't use DivMod nodes" },
172   { X("precise-except"),         &firm_opt.precise_exc,      1, "firm: precise exception context" },
173   { X("no-precise-except"),      &firm_opt.precise_exc,      0, "firm: no precise exception context" },
174   { X("remove-unused"),          &firm_opt.remove_unused,    1, "firm: remove unused functions" },
175   { X("no-remove-unused"),       &firm_opt.remove_unused,    0, "firm: dont't remove unused functions" },
176   { X("fp-precise"),             &firm_opt.fp_model,         fp_model_precise, "firm: precise fp model" },
177   { X("fp-fast"),                &firm_opt.fp_model,         fp_model_fast,    "firm: fast fp model" },
178   { X("fp-strict"),              &firm_opt.fp_model,         fp_model_strict,  "firm: strict fp model" },
179   { X("luffig"),                 &firm_opt.luffig,           1, "firm: enable the fluffy load/store optimization" },
180   { X("no-luffig"),              &firm_opt.luffig,           0, "firm: disable the fluffy load/store optimization" },
181   { X("deconv"),                 &firm_opt.deconv,           1, "firm: enable the conv node optimization" },
182   { X("no-deconv"),              &firm_opt.deconv,           0, "firm: disable the conv node optimization" },
183   { X("opt-cc"),                 &firm_opt.cc_opt,           1, "firm: enable calling conventions optimization" },
184   { X("no-opt-cc"),              &firm_opt.cc_opt,           0, "firm: disable calling conventions optimization" },
185   { X("bool"),                   &firm_opt.bool_opt,         1, "firm: enable bool simplification optimization" },
186   { X("no-bool"),                &firm_opt.bool_opt,         0, "firm: disable bool simplification optimization" },
187   { X("freestanding"),           &firm_opt.freestanding,     1, "firm: freestanding environment" },
188   { X("hosted"),                 &firm_opt.freestanding,     0, "firm: hosted environment" },
189
190   /* other firm regarding options */
191   { X("restrict"),               &firm_opt.honor_restrict,   1, "firm: honor restrict keyword" },
192   { X("no-restrict"),            &firm_opt.honor_restrict,   1, "firm: restrict keyword is meaningless" },
193   { X("no-lower"),               &firm_opt.lower,            0, "firm: disable lowering" },
194   { X("vrfy-off"),               &firm_opt.vrfy,             FIRM_VERIFICATION_OFF, "firm: disable node verification" },
195   { X("vrfy-on"),                &firm_opt.vrfy,             FIRM_VERIFICATION_ON, "firm: enable node verification" },
196   { X("vrfy-report"),            &firm_opt.vrfy,             FIRM_VERIFICATION_REPORT, "firm: node verification, report only" },
197   { X("check-all"),              &firm_opt.check_all,        1, "firm: enable checking all Firm phases" },
198   { X("no-check-all"),           &firm_opt.check_all,        0, "firm: disable checking all Firm phases" },
199   { X("vrfy-edges-on"),          &firm_opt.vrfy_edges,       1, "firm: enable out edge verification" },
200   { X("vrfy-edges-off"),         &firm_opt.vrfy_edges,       0, "firm: disable out edge verification" },
201
202   /* dumping */
203 #if defined(_DEBUG) || defined(FIRM_DEBUG)
204   { X("debug"),                  &firm_dump.debug_print,     1, "firm: enable debug output" },
205 #endif
206
207   { X("dump-ir"),                &firm_dump.ir_graph,        1, "firm: dump IR graph" },
208   { X("dump-all-types"),         &firm_dump.all_types,       1, "firm: dump graph of all types" },
209   { X("dump-no-blocks"),         &firm_dump.no_blocks,       1, "firm: dump non-blocked graph" },
210   { X("dump-extbb"),             &firm_dump.extbb,           1, "firm: dump extended basic blocks" },
211   { X("dump-all-phases"),        &firm_dump.all_phases,      1, "firm: dump graphs for all optimization phases" },
212   { X("dump-edge-labels"),       &firm_dump.edge_labels,     1, "firm: dump edge labels" },
213
214   /* code generation */
215   { X("no-codegen"),             &firm_be_opt.selection,     BE_NONE, "cg: disable code generator" },
216
217 #ifdef FIRM_EXT_GRS
218   { X("grs-simd-opt"),           &firm_ext_grs.simd_opt,                1, "firm: do simd optimization" },
219   { X("grs-create-pattern"),     &firm_ext_grs.create_pattern,  1, "firm: create patterns for simd optimization" },
220   { X("no-grs-simd-opt"),        &firm_ext_grs.simd_opt,                0, "firm: do simd optimization" },
221   { X("no-grs-create-pattern"),  &firm_ext_grs.create_pattern,  0, "firm: create patterns for simd optimization" },
222 #endif
223
224 #ifdef FIRM_BACKEND
225   { X("be-firm"),                &firm_be_opt.selection,     BE_FIRM_BE, "backend: firm backend facility" },
226 #endif /* FIRM_BACKEND */
227 #ifdef FIRM2C_BACKEND
228   { X("be-firm2c"),              &firm_be_opt.selection,     BE_FIRM2C, "backend: firm2C" },
229 #endif /* FIRM2C_BACKEND */
230
231   /* misc */
232   { X("stat-before-opt"),        &firm_dump.statistic,       STAT_BEFORE_OPT,  "misc: Firm statistic output before optimizations" },
233   { X("stat-after-opt"),         &firm_dump.statistic,       STAT_AFTER_OPT,   "misc: Firm statistic output after optimizations" },
234   { X("stat-after-lower"),       &firm_dump.statistic,       STAT_AFTER_LOWER, "misc: Firm statistic output after lowering" },
235   { X("stat-final-ir"),          &firm_dump.statistic,       STAT_FINAL_IR,    "misc: Firm statistic after final optimization" },
236   { X("stat-final"),             &firm_dump.statistic,       STAT_FINAL,       "misc: Firm statistic after code generation" },
237   { X("stat-pattern"),           &firm_dump.stat_pattern,    1, "misc: Firm statistic calculates most used pattern" },
238   { X("stat-dag"),               &firm_dump.stat_dag,        1, "misc: Firm calculates DAG statistics" },
239   { X("firm-asm"),               &firm_dump.gen_firm_asm,    1, "misc: output Firm assembler" },
240   { X("win32"),                  &firm_opt.os_support,       OS_SUPPORT_MINGW, "misc: generate MinGW code" },
241   { X("ycomp"),                  &firm_opt.ycomp_dbg,        1, "misc: enable yComp debugger extension" },
242   { X("ycomp-host=<hostname>"),  NULL,                       0, "misc: yComp host" },
243   { X("ycomp-port=<port>"),      NULL,                       0, "misc: yComp port" },
244
245   /* string options */
246   { X("dump-filter=<string>"),   NULL,                       0, "misc: set dumper filter" },
247 };
248
249 #undef X
250
251 /** A strdup replacement */
252 static char *StrDup(const char *s) {
253   int   l = strlen(s);
254   char *r = malloc(l+1);
255
256   if (r != NULL)
257     memcpy(r, s, l+1);
258   return r;
259 }
260
261 /**
262  * Set a dumper filter.
263  */
264 static void set_dump_filter(const char *filter)
265 {
266   firm_dump.filter = StrDup(filter);
267 }  /* set_dump_filter */
268
269 /**
270  * Set ycomp host
271  */
272 static void set_ycomp_host(const char *host)
273 {
274   firm_opt.ycomp_host = StrDup(host);
275 }  /* set_ycomp_host */
276
277 /** Disable all optimizations. */
278 static void disable_opts(void) {
279   /* firm_opt.const_folding */
280   firm_opt.reassoc         = FALSE;
281   firm_opt.cse             = FALSE;
282   /* firm_opt.control_flow */
283   firm_opt.code_place      = FALSE;
284   firm_opt.gvn_pre         = FALSE;
285   firm_opt.cond_eval       = FALSE;
286   firm_opt.if_conversion   = FALSE;
287   firm_opt.func_calls      = FALSE;
288   firm_opt.do_inline       = FALSE;
289   firm_opt.auto_inline     = FALSE;
290   firm_opt.tail_rec        = FALSE;
291   firm_opt.strength_red    = FALSE;
292   firm_opt.scalar_replace  = FALSE;
293   firm_opt.confirm         = FALSE;
294   firm_opt.muls            = FALSE;
295   firm_opt.divs            = FALSE;
296   firm_opt.mods            = FALSE;
297   firm_opt.fragile_ops     = FALSE;
298   firm_opt.load_store      = FALSE;
299   firm_opt.remove_unused   = FALSE;
300   /* firm_opt.jmp_tbls */
301   firm_opt.cloning         = FALSE;
302   /* firm_opt.auto_sync */
303   firm_opt.alias_analysis  = FALSE;
304   firm_opt.strict_alias    = FALSE;
305   firm_opt.no_alias        = FALSE;
306   firm_opt.luffig          = FALSE;
307   firm_opt.deconv          = FALSE;
308   firm_opt.cc_opt          = FALSE;
309   firm_opt.bool_opt        = FALSE;
310   firm_opt.freestanding    = TRUE;
311 }  /* disable_opts */
312
313 /**
314  * Handles a firm option.
315  */
316 int firm_option(const char *opt)
317 {
318   int i, len    = strlen(opt);
319   const char *p = opt;
320   int res;
321
322   if (strncmp("dump-filter=", opt, 12) == 0) {
323     opt = &opt[12];
324     set_dump_filter(opt);
325     return 1;
326   }
327   else if (strncmp("clone-threshold=", opt, 16) == 0) {
328     sscanf(&opt[16], "%d", &firm_opt.clone_threshold);
329     firm_opt.cloning = TRUE;
330     return 1;
331   }
332   else if (strncmp("ycomp-host=", opt, 11) == 0) {
333     opt = &opt[11];
334     set_ycomp_host(opt);
335     return 1;
336   }
337   else if (strncmp("ycomp-port=", opt, 11) == 0) {
338     sscanf(&opt[11], "%d", &firm_opt.ycomp_port);
339     return 1;
340   }
341   else if (strcmp("no-opt", opt) == 0) {
342     disable_opts();
343     return 1;
344   }
345
346   for (i = sizeof(firm_options) / sizeof(firm_options[0]) - 1; i >= 0; --i) {
347     if (len == firm_options[i].opt_len && strncmp(p, firm_options[i].option, len) == 0) {
348       if (!firm_options[i].flag) {
349         /* help option */
350         for (i = 0; i < (int) (sizeof(firm_options)/sizeof(firm_options[0])); ++i) {
351           printf("-f %-20s %s\n", firm_options[i].option,
352                  firm_options[i].description);
353         }
354         return -1;
355       }
356       /* statistic options do accumulate */
357       if (firm_options[i].flag == &firm_dump.statistic)
358         *firm_options[i].flag = (a_byte) (*firm_options[i].flag | firm_options[i].set);
359       else
360         *firm_options[i].flag = firm_options[i].set;
361
362       if (firm_options[i].flag == &firm_opt.debug_mode && firm_opt.debug_mode > DBG_MODE_NONE) {
363         if (firm_opt.debug_mode == DBG_MODE_FULL)
364           disable_opts();
365         res = 1;
366 #ifdef FIRM_BACKEND
367         res &= firm_be_option("omitfp=0");
368         res &= firm_be_option("stabs");
369 #endif /* FIRM_BACKEND */
370         return res;
371       }
372 #ifdef FIRM_BACKEND
373       /* OS option must be set to the backend */
374       else if (firm_options[i].flag == &firm_opt.os_support)
375         firm_be_option(firm_opt.os_support == OS_SUPPORT_MINGW ?
376           "ia32-gasmode=mingw" : "ia32-gasmode=linux");
377 #endif /* FIRM_BACKEND */
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   firm_version_t version;
406
407   firm_get_version(&version);
408
409   fprintf(f, "Firm C-Compiler using libFirm (%u.%u", version.major, version.minor);
410   if(version.revision[0] != 0) {
411         fputc(' ', f);
412     fputs(version.revision, f);
413   }
414    if(version.build[0] != 0) {
415         fputc(' ', f);
416     fputs(version.build, f);
417   }
418   fputs(")\n", f);
419   fprintf(f, "(C) 2006-2007 Michael Beck\n"
420              "(C) 1995-2007 University of Karlsruhe\n"
421              "Using ");
422 }  /* print_firm_version */