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