[gcc]
[official-gcc.git] / gcc / optc-save-gen.awk
blob54c2ed21afce6acaa147b0d284f08ddd9341f2d5
1 # Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>.
19 # This Awk script reads in the option records generated from
20 # opt-gather.awk, combines the flags of duplicate options and generates a
21 # C file.
24 # This program uses functions from opt-functions.awk and code from
25 # opt-read.awk.
27 # Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-save-gen.awk \
28 # [-v header_name=header.h] < inputfile > options-save.c
30 # Dump that array of options into a C file.
31 END {
32 print "/* This file is auto-generated by optc-save-gen.awk. */"
33 print ""
34 n_headers = split(header_name, headers, " ")
35 for (i = 1; i <= n_headers; i++)
36 print "#include " quote headers[i] quote
37 print "#include " quote "opts.h" quote
38 print "#include " quote "intl.h" quote
39 print ""
40 print "#include " quote "flags.h" quote
41 print "#include " quote "target.h" quote
42 print "#include " quote "inchash.h" quote
43 print "#include " quote "tree.h" quote
44 print "#include " quote "tree-ssa-alias.h" quote
45 print "#include " quote "is-a.h" quote
46 print "#include " quote "predict.h" quote
47 print "#include " quote "function.h" quote
48 print "#include " quote "basic-block.h" quote
49 print "#include " quote "gimple-expr.h" quote
50 print "#include " quote "gimple.h" quote
51 print "#include " quote "data-streamer.h" quote
52 print "#include " quote "ipa-ref.h" quote
53 print "#include " quote "cgraph.h" quote
54 print ""
56 if (n_extra_c_includes > 0) {
57 for (i = 0; i < n_extra_c_includes; i++) {
58 print "#include " quote extra_c_includes[i] quote
60 print ""
63 have_save = 0;
64 if (n_extra_target_vars)
65 have_save = 1
67 for (i = 0; i < n_opts; i++) {
68 if (flag_set_p("Save", flags[i]))
69 have_save = 1;
72 print "/* Save optimization variables into a structure. */"
73 print "void";
74 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
75 print "{";
77 n_opt_char = 2;
78 n_opt_short = 0;
79 n_opt_int = 0;
80 n_opt_enum = 1;
81 n_opt_other = 0;
82 var_opt_char[0] = "optimize";
83 var_opt_char[1] = "optimize_size";
84 var_opt_range["optimize"] = "0, 255";
85 var_opt_range["optimize_size"] = "0, 255";
86 var_opt_enum[0] = "flag_fp_contract_mode";
88 # Sort by size to mimic how the structure is laid out to be friendlier to the
89 # cache.
91 for (i = 0; i < n_opts; i++) {
92 if (flag_set_p("Optimization", flags[i])) {
93 name = var_name(flags[i])
94 if(name == "")
95 continue;
97 if(name in var_opt_seen)
98 continue;
100 var_opt_seen[name]++;
101 otype = var_type_struct(flags[i]);
102 if (otype ~ "^((un)?signed +)?int *$")
103 var_opt_int[n_opt_int++] = name;
105 else if (otype ~ "^((un)?signed +)?short *$")
106 var_opt_short[n_opt_short++] = name;
108 else if (otype ~ ("^enum +[_" alnum "]+ *"))
109 var_opt_enum[n_opt_enum++] = name;
111 else if (otype ~ "^((un)?signed +)?char *$") {
112 var_opt_char[n_opt_char++] = name;
113 if (otype ~ "^unsigned +char *$")
114 var_opt_range[name] = "0, 255"
115 else if (otype ~ "^signed +char *$")
116 var_opt_range[name] = "-128, 127"
118 else
119 var_opt_other[n_opt_other++] = name;
123 for (i = 0; i < n_opt_char; i++) {
124 name = var_opt_char[i];
125 if (var_opt_range[name] != "")
126 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
129 print "";
130 for (i = 0; i < n_opt_other; i++) {
131 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
134 for (i = 0; i < n_opt_int; i++) {
135 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
138 for (i = 0; i < n_opt_enum; i++) {
139 print " ptr->x_" var_opt_enum[i] " = opts->x_" var_opt_enum[i] ";";
142 for (i = 0; i < n_opt_short; i++) {
143 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
146 for (i = 0; i < n_opt_char; i++) {
147 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
150 print "}";
152 print "";
153 print "/* Restore optimization options from a structure. */";
154 print "void";
155 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
156 print "{";
158 for (i = 0; i < n_opt_other; i++) {
159 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
162 for (i = 0; i < n_opt_int; i++) {
163 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
166 for (i = 0; i < n_opt_enum; i++) {
167 print " opts->x_" var_opt_enum[i] " = ptr->x_" var_opt_enum[i] ";";
170 for (i = 0; i < n_opt_short; i++) {
171 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
174 for (i = 0; i < n_opt_char; i++) {
175 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
178 print " targetm.override_options_after_change ();";
179 print "}";
181 print "";
182 print "/* Print optimization options from a structure. */";
183 print "void";
184 print "cl_optimization_print (FILE *file,";
185 print " int indent_to,";
186 print " struct cl_optimization *ptr)";
187 print "{";
189 print " fputs (\"\\n\", file);";
190 for (i = 0; i < n_opt_other; i++) {
191 print " if (ptr->x_" var_opt_other[i] ")";
192 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
193 print " indent_to, \"\",";
194 print " \"" var_opt_other[i] "\",";
195 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
196 print "";
199 for (i = 0; i < n_opt_int; i++) {
200 print " if (ptr->x_" var_opt_int[i] ")";
201 print " fprintf (file, \"%*s%s (%#x)\\n\",";
202 print " indent_to, \"\",";
203 print " \"" var_opt_int[i] "\",";
204 print " ptr->x_" var_opt_int[i] ");";
205 print "";
208 for (i = 0; i < n_opt_enum; i++) {
209 print " fprintf (file, \"%*s%s (%#x)\\n\",";
210 print " indent_to, \"\",";
211 print " \"" var_opt_enum[i] "\",";
212 print " (int) ptr->x_" var_opt_enum[i] ");";
213 print "";
216 for (i = 0; i < n_opt_short; i++) {
217 print " if (ptr->x_" var_opt_short[i] ")";
218 print " fprintf (file, \"%*s%s (%#x)\\n\",";
219 print " indent_to, \"\",";
220 print " \"" var_opt_short[i] "\",";
221 print " ptr->x_" var_opt_short[i] ");";
222 print "";
225 for (i = 0; i < n_opt_char; i++) {
226 print " if (ptr->x_" var_opt_char[i] ")";
227 print " fprintf (file, \"%*s%s (%#x)\\n\",";
228 print " indent_to, \"\",";
229 print " \"" var_opt_char[i] "\",";
230 print " ptr->x_" var_opt_char[i] ");";
231 print "";
234 print "}";
236 print "";
237 print "/* Save selected option variables into a structure. */"
238 print "void";
239 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
240 print "{";
242 n_target_char = 0;
243 n_target_short = 0;
244 n_target_int = 0;
245 n_target_enum = 0;
246 n_target_other = 0;
248 if (have_save) {
249 for (i = 0; i < n_opts; i++) {
250 if (flag_set_p("Save", flags[i])) {
251 name = var_name(flags[i])
252 if(name == "")
253 name = "target_flags";
255 if(name in var_save_seen)
256 continue;
258 var_save_seen[name]++;
259 otype = var_type_struct(flags[i])
260 if (otype ~ "^((un)?signed +)?int *$")
261 var_target_int[n_target_int++] = name;
263 else if (otype ~ "^((un)?signed +)?short *$")
264 var_target_short[n_target_short++] = name;
266 else if (otype ~ ("^enum +[_" alnum "]+ *$"))
267 var_target_enum[n_target_enum++] = name;
269 else if (otype ~ "^((un)?signed +)?char *$") {
270 var_target_char[n_target_char++] = name;
271 if (otype ~ "^unsigned +char *$")
272 var_target_range[name] = "0, 255"
273 else if (otype ~ "^signed +char *$")
274 var_target_range[name] = "-128, 127"
275 if (otype == var_type(flags[i]))
276 var_target_range[name] = ""
278 else
279 var_target_other[n_target_other++] = name;
282 } else {
283 var_target_int[n_target_int++] = "target_flags";
286 have_assert = 0;
287 for (i = 0; i < n_target_char; i++) {
288 name = var_target_char[i];
289 if (var_target_range[name] != "") {
290 have_assert = 1;
291 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
295 if (have_assert)
296 print "";
298 print " if (targetm.target_option.save)";
299 print " targetm.target_option.save (ptr, opts);";
300 print "";
302 for (i = 0; i < n_extra_target_vars; i++) {
303 print " ptr->x_" extra_target_vars[i] " = opts->x_" extra_target_vars[i] ";";
306 for (i = 0; i < n_target_other; i++) {
307 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
310 for (i = 0; i < n_target_enum; i++) {
311 print " ptr->x_" var_target_enum[i] " = opts->x_" var_target_enum[i] ";";
314 for (i = 0; i < n_target_int; i++) {
315 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
318 for (i = 0; i < n_target_short; i++) {
319 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
322 for (i = 0; i < n_target_char; i++) {
323 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
326 print "}";
328 print "";
329 print "/* Restore selected current options from a structure. */";
330 print "void";
331 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
332 print "{";
334 for (i = 0; i < n_extra_target_vars; i++) {
335 print " opts->x_" extra_target_vars[i] " = ptr->x_" extra_target_vars[i] ";";
338 for (i = 0; i < n_target_other; i++) {
339 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
342 for (i = 0; i < n_target_enum; i++) {
343 print " opts->x_" var_target_enum[i] " = ptr->x_" var_target_enum[i] ";";
346 for (i = 0; i < n_target_int; i++) {
347 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
350 for (i = 0; i < n_target_short; i++) {
351 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
354 for (i = 0; i < n_target_char; i++) {
355 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
358 # This must occur after the normal variables in case the code depends on those
359 # variables.
360 print "";
361 print " if (targetm.target_option.restore)";
362 print " targetm.target_option.restore (opts, ptr);";
364 print "}";
366 print "";
367 print "/* Print optimization options from a structure. */";
368 print "void";
369 print "cl_target_option_print (FILE *file,";
370 print " int indent,";
371 print " struct cl_target_option *ptr)";
372 print "{";
374 print " fputs (\"\\n\", file);";
375 for (i = 0; i < n_target_other; i++) {
376 print " if (ptr->x_" var_target_other[i] ")";
377 if (host_wide_int[var_target_other[i]] == "yes")
378 print " fprintf (file, \"%*s%s (%#\" HOST_WIDE_INT_PRINT \"x)\\n\",";
379 else
380 print " fprintf (file, \"%*s%s (%#x)\\n\",";
381 print " indent, \"\",";
382 print " \"" var_target_other[i] "\",";
383 if (host_wide_int[var_target_other[i]] == "yes")
384 print " ptr->x_" var_target_other[i] ");";
385 else
386 print " (unsigned long)ptr->x_" var_target_other[i] ");";
387 print "";
390 for (i = 0; i < n_target_enum; i++) {
391 print " if (ptr->x_" var_target_enum[i] ")";
392 print " fprintf (file, \"%*s%s (%#x)\\n\",";
393 print " indent, \"\",";
394 print " \"" var_target_enum[i] "\",";
395 print " ptr->x_" var_target_enum[i] ");";
396 print "";
399 for (i = 0; i < n_target_int; i++) {
400 print " if (ptr->x_" var_target_int[i] ")";
401 print " fprintf (file, \"%*s%s (%#x)\\n\",";
402 print " indent, \"\",";
403 print " \"" var_target_int[i] "\",";
404 print " ptr->x_" var_target_int[i] ");";
405 print "";
408 for (i = 0; i < n_target_short; i++) {
409 print " if (ptr->x_" var_target_short[i] ")";
410 print " fprintf (file, \"%*s%s (%#x)\\n\",";
411 print " indent, \"\",";
412 print " \"" var_target_short[i] "\",";
413 print " ptr->x_" var_target_short[i] ");";
414 print "";
417 for (i = 0; i < n_target_char; i++) {
418 print " if (ptr->x_" var_target_char[i] ")";
419 print " fprintf (file, \"%*s%s (%#x)\\n\",";
420 print " indent, \"\",";
421 print " \"" var_target_char[i] "\",";
422 print " ptr->x_" var_target_char[i] ");";
423 print "";
426 print "";
427 print " if (targetm.target_option.print)";
428 print " targetm.target_option.print (file, indent, ptr);";
430 print "}";
432 print "";
433 print "/* Compare two target options */";
434 print "bool";
435 print "cl_target_option_eq (struct cl_target_option const *ptr1 ATTRIBUTE_UNUSED,";
436 print " struct cl_target_option const *ptr2 ATTRIBUTE_UNUSED)";
437 print "{";
438 n_target_val = 0;
439 n_target_str = 0;
441 for (i = 0; i < n_target_save; i++) {
442 var = target_save_decl[i];
443 sub (" *=.*", "", var);
444 name = var;
445 type = var;
446 sub("^.*[ *]", "", name)
447 sub(" *" name "$", "", type)
448 if (target_save_decl[i] ~ "^const char \\*+[_" alnum "]+$")
449 var_target_str[n_target_str++] = name;
450 else {
451 var_target_val_type[n_target_val] = type;
452 var_target_val[n_target_val++] = name;
455 if (have_save) {
456 for (i = 0; i < n_opts; i++) {
457 if (flag_set_p("Save", flags[i])) {
458 name = var_name(flags[i])
459 if(name == "")
460 name = "target_flags";
462 if(name in var_list_seen)
463 continue;
465 var_list_seen[name]++;
466 otype = var_type_struct(flags[i])
467 if (otype ~ "^const char \\**$")
468 var_target_str[n_target_str++] = "x_" name;
469 else {
470 var_target_val_type[n_target_val] = otype;
471 var_target_val[n_target_val++] = "x_" name;
475 } else {
476 var_target_val_type[n_target_val] = "int";
477 var_target_val[n_target_val++] = "x_target_flags";
480 for (i = 0; i < n_target_str; i++) {
481 name = var_target_str[i]
482 print " if (ptr1->" name" != ptr2->" name;
483 print " && (!ptr1->" name" || !ptr2->" name
484 print " || strcmp (ptr1->" name", ptr2->" name ")))";
485 print " return false;";
487 for (i = 0; i < n_target_val; i++) {
488 name = var_target_val[i]
489 print " if (ptr1->" name" != ptr2->" name ")";
490 print " return false;";
493 print " return true;";
495 print "}";
497 print "";
498 print "/* Hash target options */";
499 print "hashval_t";
500 print "cl_target_option_hash (struct cl_target_option const *ptr ATTRIBUTE_UNUSED)";
501 print "{";
502 print " inchash::hash hstate;";
503 for (i = 0; i < n_target_str; i++) {
504 name = var_target_str[i]
505 print " if (ptr->" name")";
506 print " hstate.add (ptr->" name", strlen (ptr->" name"));";
507 print " else";
508 print " hstate.add_int (0);";
510 for (i = 0; i < n_target_val; i++) {
511 name = var_target_val[i]
512 print " hstate.add_wide_int (ptr->" name");";
514 print " return hstate.end ();";
515 print "}";
517 print "";
518 print "/* Stream out target options */";
519 print "void";
520 print "cl_target_option_stream_out (struct output_block *ob ATTRIBUTE_UNUSED,";
521 print " struct bitpack_d *bp ATTRIBUTE_UNUSED,";
522 print " struct cl_target_option *ptr ATTRIBUTE_UNUSED)";
523 print "{";
524 for (i = 0; i < n_target_str; i++) {
525 name = var_target_str[i]
526 print " bp_pack_string (ob, bp, ptr->" name", true);";
528 for (i = 0; i < n_target_val; i++) {
529 name = var_target_val[i]
530 print " bp_pack_value (bp, ptr->" name", 64);";
532 print "}";
534 print "";
535 print "/* Stream in target options */";
536 print "void";
537 print "cl_target_option_stream_in (struct data_in *data_in ATTRIBUTE_UNUSED,";
538 print " struct bitpack_d *bp ATTRIBUTE_UNUSED,";
539 print " struct cl_target_option *ptr ATTRIBUTE_UNUSED)";
540 print "{";
541 for (i = 0; i < n_target_str; i++) {
542 name = var_target_str[i]
543 print " ptr->" name" = bp_unpack_string (data_in, bp);";
544 print " if (ptr->" name")";
545 print " ptr->" name" = xstrdup (ptr->" name");";
547 for (i = 0; i < n_target_val; i++) {
548 name = var_target_val[i]
549 print " ptr->" name" = (" var_target_val_type[i] ") bp_unpack_value (bp, 64);";
552 print "}";
554 n_opt_val = 2;
555 var_opt_val[0] = "x_optimize"
556 var_opt_val_type[0] = "char "
557 var_opt_val[1] = "x_optimize_size"
558 var_opt_val_type[1] = "char "
559 for (i = 0; i < n_opts; i++) {
560 if (flag_set_p("Optimization", flags[i])) {
561 name = var_name(flags[i])
562 if(name == "")
563 continue;
565 if(name in var_opt_list_seen)
566 continue;
568 var_opt_list_seen[name]++;
570 otype = var_type_struct(flags[i])
571 var_opt_val_type[n_opt_val] = otype;
572 var_opt_val[n_opt_val++] = "x_" name;
575 print "";
576 print "/* Hash optimization options */";
577 print "hashval_t";
578 print "cl_optimization_hash (struct cl_optimization const *ptr ATTRIBUTE_UNUSED)";
579 print "{";
580 print " inchash::hash hstate;";
581 for (i = 0; i < n_opt_val; i++) {
582 name = var_opt_val[i]
583 print " hstate.add_wide_int (ptr->" name");";
585 print " return hstate.end ();";
586 print "}";
588 print "";
589 print "/* Stream out optimization options */";
590 print "void";
591 print "cl_optimization_stream_out (struct bitpack_d *bp,";
592 print " struct cl_optimization *ptr)";
593 print "{";
594 for (i = 0; i < n_opt_val; i++) {
595 name = var_opt_val[i]
596 print " bp_pack_value (bp, ptr->" name", 64);";
598 print "}";
600 print "";
601 print "/* Stream in optimization options */";
602 print "void";
603 print "cl_optimization_stream_in (struct bitpack_d *bp,";
604 print " struct cl_optimization *ptr)";
605 print "{";
606 for (i = 0; i < n_opt_val; i++) {
607 name = var_opt_val[i]
608 print " ptr->" name" = (" var_opt_val_type[i] ") bp_unpack_value (bp, 64);";
610 print "}";