implement a bare-bones cparser --help
[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 "firm_opt.h"
13 #include <libfirm/firm.h>
14 #include <libfirm/be.h>
15
16 #if defined(_WIN32) || defined(__CYGWIN__)
17 #define DEFAULT_OS OS_SUPPORT_MINGW
18 #elif defined(__APPLE__)
19 #define DEFAULT_OS OS_SUPPORT_MACHO
20 #else
21 #define DEFAULT_OS OS_SUPPORT_LINUX
22 #endif
23
24 /* optimization settings */
25 struct a_firm_opt firm_opt = {
26   /* enabled         = */ TRUE,
27   /* debug_mode      = */ DBG_MODE_NONE,
28   /* const_folding   = */ TRUE,
29   /* cse             = */ TRUE,
30   /* control_flow    = */ TRUE,
31   /* gcse            = */ TRUE,
32   /* confirm         = */ TRUE,
33   /* muls            = */ TRUE,
34   /* divs            = */ TRUE,
35   /* mods            = */ TRUE,
36   /* alias_analysis  = */ TRUE,
37   /* strict_alias    = */ FALSE,
38   /* no_alias        = */ FALSE,
39   /* cc_opt          = */ TRUE,
40   /* freestanding;   = */ FALSE,
41   /* fp_model        = */ fp_model_precise,
42   /* vrfy            = */ FIRM_VERIFICATION_ON,
43   /* check_all       = */ FALSE,
44   /* lower           = */ TRUE,
45   /* os_support      = */ DEFAULT_OS,
46   /* honor_restrict  = */ TRUE,
47   /* lower_bitfields = */ TRUE,
48   /* pic             = */ FALSE,
49   /* clone_threshold = */ DEFAULT_CLONE_THRESHOLD,
50   /* inline_maxsize  = */ 750,
51   /* inline_threshold= */ 0,
52   /* vrfy_edges      = */ FALSE,
53   /* grs_simd_opt    = */ 0,
54   /* grs_create_pattern = */ 0,
55   /* spare_size      = */ 128,
56   /* enable_statev   = */ FALSE,
57   /* statev_filter   = */ "",
58 };
59
60 /* dumping options */
61 struct a_firm_dump firm_dump = {
62   /* debug_print  = */ FALSE,
63   /* all_types    = */ FALSE,
64   /* no_blocks    = */ FALSE,
65   /* extbb        = */ FALSE,
66   /* ir_graph     = */ FALSE,
67   /* all_phases   = */ FALSE,
68   /* edge_labels  = */ FALSE,
69   /* statistic    = */ STAT_NONE,
70   /* stat_pattern = */ 0,
71   /* stat_dag     = */ 0,
72   /* filter       = */ NULL
73 };
74
75 #ifdef FIRM_EXT_GRS
76 struct a_firm_ext_grs firm_ext_grs = {
77   /* simd_opt       = */ FALSE,
78   /* create_pattern = */ FALSE
79 };
80 #endif
81
82 struct a_firm_be_opt firm_be_opt = {
83   /* selection = */ BE_FIRM_BE,
84   /* node_stat = */ 0,
85 };
86
87 #define X(a)    a, sizeof(a)-1
88
89 /** Parameter description structure */
90 static const struct params {
91   const char *option;      /**< name of the option */
92   int        opt_len;      /**< length of the option string */
93   a_byte     *flag;        /**< address of variable to set/reset */
94   a_byte     set;          /**< iff true, variable will be set, else reset */
95   const char *description; /**< description of this option */
96 } firm_options[] = {
97   /* this must be first */
98   { X("help"),                   NULL,                       0, "print FCC related help options" },
99
100   /* firm optimization options */
101   { X("pic"),                    &firm_opt.pic,              1, "firm: generate position independent code" },
102   { X("g0"),                     &firm_opt.debug_mode,       DBG_MODE_BACKSTORE, "firm: Debug Mode: use back stores" },
103   { X("g1"),                     &firm_opt.debug_mode,       DBG_MODE_FULL,      "firm: Debug Mode: no register variables" },
104   { X("no-opt"),                 NULL,                       0, "firm: disable all FIRM optimizations" },
105   { X("cse"),                    &firm_opt.cse,              1, "firm: enable common subexpression elimination" },
106   { X("no-cse"),                 &firm_opt.cse,              0, "firm: disable common subexpression elimination" },
107   { X("const-fold"),             &firm_opt.const_folding,    1, "firm: enable constant folding" },
108   { X("no-const-fold"),          &firm_opt.const_folding,    0, "firm: disable constant folding" },
109   { X("control_flow"),           &firm_opt.control_flow,     1, "firm: enable control flow optimization" },
110   { X("no-control-flow"),        &firm_opt.control_flow,     0, "firm: disable control flow optimization" },
111   { X("gcse"),                   &firm_opt.gcse,             1, "firm: enable global common subexpression elimination" },
112   { X("no-gcse"),                &firm_opt.gcse,             0, "firm: disable global common subexpression elimination" },
113   { X("inline-max-size=<size>"), NULL,                       0, "firm: set maximum size for function inlining" },
114   { X("inline-threshold=<size>"),NULL,                       0, "firm: set benefice threshold for function inlining" },
115   { X("confirm"),                &firm_opt.confirm,          1, "firm: enable Confirm optimization" },
116   { X("no-confirm"),             &firm_opt.confirm,          0, "firm: disable Confirm optimization" },
117   { X("opt-mul"),                &firm_opt.muls,             0, "firm: enable multiplication optimization" },
118   { X("no-opt-mul"),             &firm_opt.muls,             0, "firm: disable multiplication optimization" },
119   { X("opt-div"),                &firm_opt.divs,             0, "firm: enable division optimization" },
120   { X("no-opt-div"),             &firm_opt.divs,             0, "firm: disable division optimization" },
121   { X("opt-mod"),                &firm_opt.mods,             0, "firm: enable remainder optimization" },
122   { X("no-opt-mod"),             &firm_opt.mods,             0, "firm: disable remainder optimization" },
123   { X("opt-alias"),              &firm_opt.alias_analysis,   1, "firm: enable alias analysis" },
124   { X("no-opt-alias"),           &firm_opt.alias_analysis,   0, "firm: disable alias analysis" },
125   { X("alias"),                  &firm_opt.no_alias,         0, "firm: aliasing occurs" },
126   { X("no-alias"),               &firm_opt.no_alias,         1, "firm: no aliasing occurs" },
127   { X("strict-aliasing"),        &firm_opt.strict_alias,     1, "firm: strict alias rules" },
128   { X("no-strict-aliasing"),     &firm_opt.strict_alias,     0, "firm: strict alias rules" },
129   { X("clone-threshold=<value>"),NULL,                       0, "firm: set clone threshold to <value>" },
130   { X("fp-precise"),             &firm_opt.fp_model,         fp_model_precise, "firm: precise fp model" },
131   { X("fp-fast"),                &firm_opt.fp_model,         fp_model_fast,    "firm: fast fp model" },
132   { X("fp-strict"),              &firm_opt.fp_model,         fp_model_strict,  "firm: strict fp model" },
133   { X("opt-cc"),                 &firm_opt.cc_opt,           1, "firm: enable calling conventions optimization" },
134   { X("no-opt-cc"),              &firm_opt.cc_opt,           0, "firm: disable calling conventions optimization" },
135   { X("freestanding"),           &firm_opt.freestanding,     1, "firm: freestanding environment" },
136   { X("hosted"),                 &firm_opt.freestanding,     0, "firm: hosted environment" },
137
138   /* other firm regarding options */
139   { X("restrict"),               &firm_opt.honor_restrict,   1, "firm: honor restrict keyword" },
140   { X("no-restrict"),            &firm_opt.honor_restrict,   1, "firm: restrict keyword is meaningless" },
141   { X("no-lower"),               &firm_opt.lower,            0, "firm: disable lowering" },
142   { X("vrfy-off"),               &firm_opt.vrfy,             FIRM_VERIFICATION_OFF, "firm: disable node verification" },
143   { X("vrfy-on"),                &firm_opt.vrfy,             FIRM_VERIFICATION_ON, "firm: enable node verification" },
144   { X("vrfy-report"),            &firm_opt.vrfy,             FIRM_VERIFICATION_REPORT, "firm: node verification, report only" },
145   { X("check-all"),              &firm_opt.check_all,        1, "firm: enable checking all Firm phases" },
146   { X("no-check-all"),           &firm_opt.check_all,        0, "firm: disable checking all Firm phases" },
147   { X("vrfy-edges-on"),          &firm_opt.vrfy_edges,       1, "firm: enable out edge verification" },
148   { X("vrfy-edges-off"),         &firm_opt.vrfy_edges,       0, "firm: disable out edge verification" },
149
150   /* dumping */
151 #if defined(_DEBUG) || defined(FIRM_DEBUG)
152   { X("debug"),                  &firm_dump.debug_print,     1, "firm: enable debug output" },
153 #endif
154
155   { X("dump-ir"),                &firm_dump.ir_graph,        1, "firm: dump IR graph" },
156   { X("dump-all-types"),         &firm_dump.all_types,       1, "firm: dump graph of all types" },
157   { X("dump-no-blocks"),         &firm_dump.no_blocks,       1, "firm: dump non-blocked graph" },
158   { X("dump-extbb"),             &firm_dump.extbb,           1, "firm: dump extended basic blocks" },
159   { X("dump-all-phases"),        &firm_dump.all_phases,      1, "firm: dump graphs for all optimization phases" },
160   { X("dump-edge-labels"),       &firm_dump.edge_labels,     1, "firm: dump edge labels" },
161
162   /* code generation */
163   { X("no-codegen"),             &firm_be_opt.selection,     BE_NONE, "cg: disable code generator" },
164
165 #ifdef FIRM_EXT_GRS
166   { X("grs-simd-opt"),           &firm_ext_grs.simd_opt,       1, "firm: do simd optimization" },
167   { X("grs-create-pattern"),     &firm_ext_grs.create_pattern, 1, "firm: create patterns for simd optimization" },
168   { X("no-grs-simd-opt"),        &firm_ext_grs.simd_opt,       0, "firm: do simd optimization" },
169   { X("no-grs-create-pattern"),  &firm_ext_grs.create_pattern, 0, "firm: create patterns for simd optimization" },
170 #endif
171
172   { X("be-firm"),                &firm_be_opt.selection,     BE_FIRM_BE, "backend: firm backend facility" },
173 #ifdef FIRM2C_BACKEND
174   { X("be-firm2c"),              &firm_be_opt.selection,     BE_FIRM2C, "backend: firm2C" },
175 #endif /* FIRM2C_BACKEND */
176
177   /* misc */
178   { X("stat-before-opt"),        &firm_dump.statistic,       STAT_BEFORE_OPT,  "misc: Firm statistic output before optimizations" },
179   { X("stat-after-opt"),         &firm_dump.statistic,       STAT_AFTER_OPT,   "misc: Firm statistic output after optimizations" },
180   { X("stat-after-lower"),       &firm_dump.statistic,       STAT_AFTER_LOWER, "misc: Firm statistic output after lowering" },
181   { X("stat-final-ir"),          &firm_dump.statistic,       STAT_FINAL_IR,    "misc: Firm statistic after final optimization" },
182   { X("stat-final"),             &firm_dump.statistic,       STAT_FINAL,       "misc: Firm statistic after code generation" },
183   { X("stat-pattern"),           &firm_dump.stat_pattern,    1, "misc: Firm statistic calculates most used pattern" },
184   { X("stat-dag"),               &firm_dump.stat_dag,        1, "misc: Firm calculates DAG statistics" },
185   { X("win32"),                  &firm_opt.os_support,       OS_SUPPORT_MINGW, "misc: generate MinGW Win32 code" },
186   { X("mac"),                    &firm_opt.os_support,       OS_SUPPORT_MACHO, "misc: generate MacOS code" },
187   { X("linux"),                  &firm_opt.os_support,       OS_SUPPORT_LINUX, "misc: generate Linux-ELF code" },
188
189   /* string options */
190   { X("dump-filter=<string>"),   NULL,                       0, "misc: set dumper filter" },
191 };
192
193 #undef X
194
195 /** A strdup replacement */
196 static char *StrDup(const char *s) {
197   int   l = strlen(s);
198   char *r = malloc(l+1);
199
200   if (r != NULL)
201     memcpy(r, s, l+1);
202   return r;
203 }
204
205 /**
206  * Set a dumper filter.
207  */
208 static void set_dump_filter(const char *filter)
209 {
210   firm_dump.filter = StrDup(filter);
211 }  /* set_dump_filter */
212
213 /** Disable all optimizations. */
214 static void disable_opts(void) {
215   firm_opt.cse             = FALSE;
216   firm_opt.gcse            = FALSE;
217   firm_opt.confirm         = FALSE;
218   firm_opt.muls            = FALSE;
219   firm_opt.divs            = FALSE;
220   firm_opt.mods            = FALSE;
221   firm_opt.alias_analysis  = FALSE;
222   firm_opt.strict_alias    = FALSE;
223   firm_opt.no_alias        = FALSE;
224   firm_opt.cc_opt          = FALSE;
225   firm_opt.freestanding    = TRUE;
226   disable_all_opts();
227 }  /* disable_opts */
228
229 void print_option_help(const char *name, const char *description)
230 {
231         printf("-f %-20s %s\n", name, description);
232 }
233
234 /**
235  * Handles a firm option.
236  */
237 int firm_option(const char *opt)
238 {
239   int i, len    = strlen(opt);
240   const char *p = opt;
241   int res;
242
243   if (strncmp("dump-filter=", opt, 12) == 0) {
244     opt = &opt[12];
245     set_dump_filter(opt);
246     return 1;
247   }
248   else if (strncmp("clone-threshold=", opt, 16) == 0) {
249     sscanf(&opt[16], "%d", &firm_opt.clone_threshold);
250     return 1;
251   }
252   else if (strncmp("inline-max-size=", opt, 16) == 0) {
253     sscanf(&opt[16], "%u", &firm_opt.inline_maxsize);
254     return 1;
255   }
256   else if (strncmp("inline-threshold=", opt, 17) == 0) {
257     sscanf(&opt[17], "%u", &firm_opt.inline_threshold);
258     return 1;
259   }
260   else if (strcmp("no-opt", opt) == 0) {
261     disable_opts();
262     return 1;
263   }
264
265   for (i = sizeof(firm_options) / sizeof(firm_options[0]) - 1; i >= 0; --i) {
266     if (len == firm_options[i].opt_len && strncmp(p, firm_options[i].option, len) == 0) {
267       if (!firm_options[i].flag) {
268         /* help option */
269         print_option_help(firm_options[0].option, firm_options[0].description);
270         firm_opt_option_help();
271         for (i = 1; i < (int) (sizeof(firm_options)/sizeof(firm_options[0])); ++i) {
272           print_option_help(firm_options[i].option, firm_options[i].description);
273         }
274         return -1;
275       }
276       /* statistic options do accumulate */
277       if (firm_options[i].flag == &firm_dump.statistic)
278         *firm_options[i].flag = (a_byte) (*firm_options[i].flag | firm_options[i].set);
279       else
280         *firm_options[i].flag = firm_options[i].set;
281
282       if (firm_options[i].flag == &firm_opt.debug_mode && firm_opt.debug_mode > DBG_MODE_NONE) {
283         if (firm_opt.debug_mode == DBG_MODE_FULL)
284           disable_opts();
285         res = 1;
286         res &= be_parse_arg("omitfp=0");
287         res &= be_parse_arg("stabs");
288         return res;
289       }
290       break;
291     }
292   }
293
294   if (i >= 0)
295     return 1;
296
297   /* maybe this enables/disables an optimisations */
298   if (firm_opt_option(p))
299     return 1;
300
301   return 0;
302 }  /* firm_option */
303
304 /**
305  * prints the firm version number
306  */
307 void print_firm_version(FILE *f) {
308   const char *revision = ir_get_version_revision();
309   const char *build    = ir_get_version_build();
310
311   fprintf(f, "Firm C-Compiler using libFirm (%u.%u",
312           ir_get_version_major(), ir_get_version_minor());
313   if (revision[0] != 0) {
314         fputc(' ', f);
315     fputs(revision, f);
316   }
317    if(build[0] != 0) {
318         fputc(' ', f);
319     fputs(build, f);
320   }
321   fprintf(f, "}\n"
322                      "(C) 2005-2008 Michael Beck\n"
323              "(C) 1995-2008 University of Karlsruhe\n"
324              "Using ");
325 }  /* print_firm_version */