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