opt-functions.awk (static_var): Update comment.
[gcc.git] / gcc / optc-gen.awk
1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
2 # Free Software Foundation, Inc.
3 # Contributed by Kelley Cook, June 2004.
4 # Original code from Neil Booth, May 2003.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 3, or (at your option) any
9 # later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; see the file COPYING3. If not see
18 # <http://www.gnu.org/licenses/>.
19
20 # This Awk script reads in the option records generated from
21 # opt-gather.awk, combines the flags of duplicate options and generates a
22 # C file.
23 #
24 # This program uses functions from opt-functions.awk
25 #
26 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
27 # [-v header_name=header.h] < inputfile > options.c
28
29 BEGIN {
30 n_opts = 0
31 n_langs = 0
32 n_target_save = 0
33 n_extra_vars = 0
34 quote = "\042"
35 comma = ","
36 FS=SUBSEP
37 # Default the name of header created from opth-gen.awk to options.h
38 if (header_name == "") header_name="options.h"
39 }
40
41 # Collect the text and flags of each option into an array
42 {
43 if ($1 == "Language") {
44 langs[n_langs] = $2
45 n_langs++;
46 }
47 else if ($1 == "TargetSave") {
48 # Make sure the declarations are put in source order
49 target_save_decl[n_target_save] = $2
50 n_target_save++
51 }
52 else if ($1 == "Variable") {
53 extra_vars[n_extra_vars] = $2
54 n_extra_vars++
55 }
56 else {
57 name = opt_args("Mask", $1)
58 if (name == "") {
59 opts[n_opts] = $1
60 flags[n_opts] = $2
61 help[n_opts] = $3
62 for (i = 4; i <= NF; i++)
63 help[n_opts] = help[n_opts] " " $i
64 n_opts++;
65 }
66 }
67 }
68
69 # Dump that array of options into a C file.
70 END {
71 print "/* This file is auto-generated by optc-gen.awk. */"
72 print ""
73 n_headers = split(header_name, headers, " ")
74 for (i = 1; i <= n_headers; i++)
75 print "#include " quote headers[i] quote
76 print "#include " quote "opts.h" quote
77 print "#include " quote "intl.h" quote
78 print ""
79 print "#ifdef GCC_DRIVER"
80 print "int target_flags_explicit;"
81 print "#else"
82 print "#include " quote "flags.h" quote
83 print "#include " quote "target.h" quote
84 print "#endif /* GCC_DRIVER */"
85 print ""
86
87 have_save = 0;
88 print "struct gcc_options global_options =\n{"
89 for (i = 0; i < n_extra_vars; i++) {
90 var = extra_vars[i]
91 init = extra_vars[i]
92 if (var ~ "=" ) {
93 sub(".*= *", "", init)
94 sub(" *=.*", "", var)
95 sub("^.*[ *]", "", var)
96 } else {
97 init = "0"
98 }
99 var_seen[var] = 1
100 print " " init ", /* " var " */"
101 }
102 for (i = 0; i < n_opts; i++) {
103 if (flag_set_p("Save", flags[i]))
104 have_save = 1;
105
106 name = var_name(flags[i]);
107 if (name == "")
108 continue;
109
110 init = opt_args("Init", flags[i])
111 if (init != "") {
112 if (name in var_init && var_init[name] != init)
113 print "#error multiple initializers for " name
114 var_init[name] = init
115 }
116 }
117 for (i = 0; i < n_opts; i++) {
118 name = var_name(flags[i]);
119 if (name == "")
120 continue;
121
122 if (name in var_seen)
123 continue;
124
125 if (name in var_init)
126 init = var_init[name]
127 else
128 init = "0"
129
130 print " " init ", /* " name " */"
131
132 var_seen[name] = 1;
133 }
134 for (i = 0; i < n_opts; i++) {
135 name = static_var(opts[i], flags[i]);
136 if (name != "") {
137 print " 0, /* " name " (private state) */"
138 print "#undef x_" name
139 }
140 }
141 print "};"
142 print ""
143
144 print "const char * const lang_names[] =\n{"
145 for (i = 0; i < n_langs; i++) {
146 macros[i] = "CL_" langs[i]
147 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
148 s = substr(" ", length (macros[i]))
149 print " " quote langs[i] quote ","
150 }
151
152 print " 0\n};\n"
153 print "const unsigned int cl_options_count = N_OPTS;\n"
154 print "const unsigned int cl_lang_count = " n_langs ";\n"
155
156 print "const struct cl_option cl_options[] =\n{"
157
158 j = 0
159 for (i = 0; i < n_opts; i++) {
160 back_chain[i] = "N_OPTS";
161 indices[opts[i]] = j;
162 # Combine the flags of identical switches. Switches
163 # appear many times if they are handled by many front
164 # ends, for example.
165 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
166 flags[i + 1] = flags[i] " " flags[i + 1];
167 if (help[i + 1] == "")
168 help[i + 1] = help[i]
169 else if (help[i] != "" && help[i + 1] != help[i])
170 print "warning: multiple different help strings for " \
171 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
172 | "cat 1>&2"
173 i++;
174 back_chain[i] = "N_OPTS";
175 indices[opts[i]] = j;
176 }
177 j++;
178 }
179
180 for (i = 0; i < n_opts; i++) {
181 # With identical flags, pick only the last one. The
182 # earlier loop ensured that it has all flags merged,
183 # and a nonempty help text if one of the texts was nonempty.
184 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
185 i++;
186 }
187
188 len = length (opts[i]);
189 enum = opt_enum(opts[i])
190
191 # If this switch takes joined arguments, back-chain all
192 # subsequent switches to it for which it is a prefix. If
193 # a later switch S is a longer prefix of a switch T, T
194 # will be back-chained to S in a later iteration of this
195 # for() loop, which is what we want.
196 if (flag_set_p("Joined.*", flags[i])) {
197 for (j = i + 1; j < n_opts; j++) {
198 if (substr (opts[j], 1, len) != opts[i])
199 break;
200 back_chain[j] = enum;
201 }
202 }
203
204 s = substr(" ", length (opts[i]))
205 if (i + 1 == n_opts)
206 comma = ""
207
208 if (help[i] == "")
209 hlp = "0"
210 else
211 hlp = quote help[i] quote;
212
213 missing_arg_error = opt_args("MissingArgError", flags[i])
214 if (missing_arg_error == "")
215 missing_arg_error = "0"
216 else
217 missing_arg_error = quote missing_arg_error quote
218
219
220 warn_message = opt_args("Warn", flags[i])
221 if (warn_message == "")
222 warn_message = "0"
223 else
224 warn_message = quote warn_message quote
225
226 alias_arg = opt_args("Alias", flags[i])
227 if (alias_arg == "") {
228 if (flag_set_p("Ignore", flags[i]))
229 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
230 else
231 alias_data = "NULL, NULL, N_OPTS"
232 } else {
233 alias_opt = nth_arg(0, alias_arg)
234 alias_posarg = nth_arg(1, alias_arg)
235 alias_negarg = nth_arg(2, alias_arg)
236
237 if (var_ref(opts[i], flags[i]) != "-1")
238 print "#error Alias setting variable"
239
240 if (alias_posarg != "" && alias_negarg == "") {
241 if (!flag_set_p("RejectNegative", flags[i]) \
242 && opts[i] ~ "^[Wfm]")
243 print "#error Alias with single argument " \
244 "allowing negative form"
245 }
246
247 alias_opt = opt_enum(alias_opt)
248 if (alias_posarg == "")
249 alias_posarg = "NULL"
250 else
251 alias_posarg = quote alias_posarg quote
252 if (alias_negarg == "")
253 alias_negarg = "NULL"
254 else
255 alias_negarg = quote alias_negarg quote
256 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
257 }
258
259 neg = opt_args("Negative", flags[i]);
260 if (neg != "")
261 idx = indices[neg]
262 else {
263 if (flag_set_p("RejectNegative", flags[i]))
264 idx = -1;
265 else {
266 if (opts[i] ~ "^[Wfm]")
267 idx = indices[opts[i]];
268 else
269 idx = -1;
270 }
271 }
272 # Split the printf after %u to work around an ia64-hp-hpux11.23
273 # awk bug.
274 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
275 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
276 alias_data, back_chain[i], len)
277 printf(" %d,\n", idx)
278 condition = opt_args("Condition", flags[i])
279 cl_flags = switch_flags(flags[i])
280 if (condition != "")
281 printf("#if %s\n" \
282 " %s,\n" \
283 "#else\n" \
284 " CL_DISABLED,\n" \
285 "#endif\n",
286 condition, cl_flags, cl_flags)
287 else
288 printf(" %s,\n", cl_flags)
289 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
290 var_set(flags[i]), comma)
291 }
292
293 print "};"
294
295 print "";
296 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
297 print "";
298 print "/* Save optimization variables into a structure. */"
299 print "void";
300 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
301 print "{";
302
303 n_opt_char = 2;
304 n_opt_short = 0;
305 n_opt_int = 0;
306 n_opt_other = 0;
307 var_opt_char[0] = "optimize";
308 var_opt_char[1] = "optimize_size";
309 var_opt_range["optimize"] = "0, 255";
310 var_opt_range["optimize_size"] = "0, 255";
311
312 # Sort by size to mimic how the structure is laid out to be friendlier to the
313 # cache.
314
315 for (i = 0; i < n_opts; i++) {
316 if (flag_set_p("Optimization", flags[i])) {
317 name = var_name(flags[i])
318 if(name == "")
319 continue;
320
321 if(name in var_opt_seen)
322 continue;
323
324 var_opt_seen[name]++;
325 otype = var_type_struct(flags[i]);
326 if (otype ~ "^((un)?signed +)?int *$")
327 var_opt_int[n_opt_int++] = name;
328
329 else if (otype ~ "^((un)?signed +)?short *$")
330 var_opt_short[n_opt_short++] = name;
331
332 else if (otype ~ "^((un)?signed +)?char *$") {
333 var_opt_char[n_opt_char++] = name;
334 if (otype ~ "^unsigned +char *$")
335 var_opt_range[name] = "0, 255"
336 else if (otype ~ "^signed +char *$")
337 var_opt_range[name] = "-128, 127"
338 }
339 else
340 var_opt_other[n_opt_other++] = name;
341 }
342 }
343
344 for (i = 0; i < n_opt_char; i++) {
345 name = var_opt_char[i];
346 if (var_opt_range[name] != "")
347 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
348 }
349
350 print "";
351 for (i = 0; i < n_opt_other; i++) {
352 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
353 }
354
355 for (i = 0; i < n_opt_int; i++) {
356 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
357 }
358
359 for (i = 0; i < n_opt_short; i++) {
360 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
361 }
362
363 for (i = 0; i < n_opt_char; i++) {
364 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
365 }
366
367 print "}";
368
369 print "";
370 print "/* Restore optimization options from a structure. */";
371 print "void";
372 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
373 print "{";
374
375 for (i = 0; i < n_opt_other; i++) {
376 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
377 }
378
379 for (i = 0; i < n_opt_int; i++) {
380 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
381 }
382
383 for (i = 0; i < n_opt_short; i++) {
384 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
385 }
386
387 for (i = 0; i < n_opt_char; i++) {
388 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
389 }
390
391 print " targetm.override_options_after_change ();";
392 print "}";
393
394 print "";
395 print "/* Print optimization options from a structure. */";
396 print "void";
397 print "cl_optimization_print (FILE *file,";
398 print " int indent_to,";
399 print " struct cl_optimization *ptr)";
400 print "{";
401
402 print " fputs (\"\\n\", file);";
403 for (i = 0; i < n_opt_other; i++) {
404 print " if (ptr->x_" var_opt_other[i] ")";
405 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
406 print " indent_to, \"\",";
407 print " \"" var_opt_other[i] "\",";
408 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
409 print "";
410 }
411
412 for (i = 0; i < n_opt_int; i++) {
413 print " if (ptr->x_" var_opt_int[i] ")";
414 print " fprintf (file, \"%*s%s (%#x)\\n\",";
415 print " indent_to, \"\",";
416 print " \"" var_opt_int[i] "\",";
417 print " ptr->x_" var_opt_int[i] ");";
418 print "";
419 }
420
421 for (i = 0; i < n_opt_short; i++) {
422 print " if (ptr->x_" var_opt_short[i] ")";
423 print " fprintf (file, \"%*s%s (%#x)\\n\",";
424 print " indent_to, \"\",";
425 print " \"" var_opt_short[i] "\",";
426 print " ptr->x_" var_opt_short[i] ");";
427 print "";
428 }
429
430 for (i = 0; i < n_opt_char; i++) {
431 print " if (ptr->x_" var_opt_char[i] ")";
432 print " fprintf (file, \"%*s%s (%#x)\\n\",";
433 print " indent_to, \"\",";
434 print " \"" var_opt_char[i] "\",";
435 print " ptr->x_" var_opt_char[i] ");";
436 print "";
437 }
438
439 print "}";
440
441 print "";
442 print "/* Save selected option variables into a structure. */"
443 print "void";
444 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
445 print "{";
446
447 n_target_char = 0;
448 n_target_short = 0;
449 n_target_int = 0;
450 n_target_other = 0;
451
452 if (have_save) {
453 for (i = 0; i < n_opts; i++) {
454 if (flag_set_p("Save", flags[i])) {
455 name = var_name(flags[i])
456 if(name == "")
457 name = "target_flags";
458
459 if(name in var_save_seen)
460 continue;
461
462 var_save_seen[name]++;
463 otype = var_type_struct(flags[i])
464 if (otype ~ "^((un)?signed +)?int *$")
465 var_target_int[n_target_int++] = name;
466
467 else if (otype ~ "^((un)?signed +)?short *$")
468 var_target_short[n_target_short++] = name;
469
470 else if (otype ~ "^((un)?signed +)?char *$") {
471 var_target_char[n_target_char++] = name;
472 if (otype ~ "^unsigned +char *$")
473 var_target_range[name] = "0, 255"
474 else if (otype ~ "^signed +char *$")
475 var_target_range[name] = "-128, 127"
476 }
477 else
478 var_target_other[n_target_other++] = name;
479 }
480 }
481 } else {
482 var_target_int[n_target_int++] = "target_flags";
483 }
484
485 have_assert = 0;
486 for (i = 0; i < n_target_char; i++) {
487 name = var_target_char[i];
488 if (var_target_range[name] != "") {
489 have_assert = 1;
490 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
491 }
492 }
493
494 if (have_assert)
495 print "";
496
497 print " if (targetm.target_option.save)";
498 print " targetm.target_option.save (ptr);";
499 print "";
500
501 for (i = 0; i < n_target_other; i++) {
502 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
503 }
504
505 for (i = 0; i < n_target_int; i++) {
506 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
507 }
508
509 for (i = 0; i < n_target_short; i++) {
510 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
511 }
512
513 for (i = 0; i < n_target_char; i++) {
514 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
515 }
516
517 print "}";
518
519 print "";
520 print "/* Restore selected current options from a structure. */";
521 print "void";
522 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
523 print "{";
524
525 for (i = 0; i < n_target_other; i++) {
526 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
527 }
528
529 for (i = 0; i < n_target_int; i++) {
530 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
531 }
532
533 for (i = 0; i < n_target_short; i++) {
534 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
535 }
536
537 for (i = 0; i < n_target_char; i++) {
538 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
539 }
540
541 # This must occur after the normal variables in case the code depends on those
542 # variables.
543 print "";
544 print " if (targetm.target_option.restore)";
545 print " targetm.target_option.restore (ptr);";
546
547 print "}";
548
549 print "";
550 print "/* Print optimization options from a structure. */";
551 print "void";
552 print "cl_target_option_print (FILE *file,";
553 print " int indent,";
554 print " struct cl_target_option *ptr)";
555 print "{";
556
557 print " fputs (\"\\n\", file);";
558 for (i = 0; i < n_target_other; i++) {
559 print " if (ptr->x_" var_target_other[i] ")";
560 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
561 print " indent, \"\",";
562 print " \"" var_target_other[i] "\",";
563 print " (unsigned long)ptr->x_" var_target_other[i] ");";
564 print "";
565 }
566
567 for (i = 0; i < n_target_int; i++) {
568 print " if (ptr->x_" var_target_int[i] ")";
569 print " fprintf (file, \"%*s%s (%#x)\\n\",";
570 print " indent, \"\",";
571 print " \"" var_target_int[i] "\",";
572 print " ptr->x_" var_target_int[i] ");";
573 print "";
574 }
575
576 for (i = 0; i < n_target_short; i++) {
577 print " if (ptr->x_" var_target_short[i] ")";
578 print " fprintf (file, \"%*s%s (%#x)\\n\",";
579 print " indent, \"\",";
580 print " \"" var_target_short[i] "\",";
581 print " ptr->x_" var_target_short[i] ");";
582 print "";
583 }
584
585 for (i = 0; i < n_target_char; i++) {
586 print " if (ptr->x_" var_target_char[i] ")";
587 print " fprintf (file, \"%*s%s (%#x)\\n\",";
588 print " indent, \"\",";
589 print " \"" var_target_char[i] "\",";
590 print " ptr->x_" var_target_char[i] ");";
591 print "";
592 }
593
594 print "";
595 print " if (targetm.target_option.print)";
596 print " targetm.target_option.print (file, indent, ptr);";
597
598 print "}";
599 print "#endif";
600
601 }