]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/optc-gen.awk
gcc/cp
[thirdparty/gcc.git] / gcc / optc-gen.awk
CommitLineData
fbb6fbd8 1# Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011, 2012
7cf0dbf3 2# Free Software Foundation, Inc.
e8b212b8 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
8c4c00c1 8# Free Software Foundation; either version 3, or (at your option) any
e8b212b8 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
8c4c00c1 17# along with this program; see the file COPYING3. If not see
18# <http://www.gnu.org/licenses/>.
e8b212b8 19
20# This Awk script reads in the option records generated from
63433b97 21# opt-gather.awk, combines the flags of duplicate options and generates a
e8b212b8 22# C file.
23#
1e127302 24
25# This program uses functions from opt-functions.awk and code from
26# opt-read.awk.
e8b212b8 27#
1e127302 28# Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-gen.awk \
e8b212b8 29# [-v header_name=header.h] < inputfile > options.c
30
e8b212b8 31# Dump that array of options into a C file.
32END {
d3b7ee7c 33
34# Record first EnabledBy and LangEnabledBy uses.
35n_enabledby = 0;
36for (i = 0; i < n_langs; i++) {
37 n_enabledby_lang[i] = 0;
38}
39for (i = 0; i < n_opts; i++) {
40 enabledby_arg = opt_args("EnabledBy", flags[i]);
41 if (enabledby_arg != "") {
42 enabledby_name = enabledby_arg;
43 enabledby_index = opt_numbers[enabledby_name];
44 if (enabledby_index == "") {
45 print "#error Enabledby: " enabledby_name
46 } else {
47 if (enables[enabledby_name] == "") {
48 enabledby[n_enabledby] = enabledby_name;
49 n_enabledby++;
50 }
51 enables[enabledby_name] = enables[enabledby_name] opts[i] ",";
52 }
53 }
54
55 enabledby_arg = opt_args("LangEnabledBy", flags[i]);
56 if (enabledby_arg != "") {
57 n_enabledby_arg_langs = split(nth_arg(0, enabledby_arg), enabledby_arg_langs, " ");
58 enabledby_name = nth_arg(1, enabledby_arg);
59 enabledby_index = opt_numbers[enabledby_name];
60 if (enabledby_index == "") {
61 print "#error Enabledby: " enabledby_name
62 } else {
63 for (j = 1; j <= n_enabledby_arg_langs; j++) {
64 lang_name = lang_sanitized_name(enabledby_arg_langs[j]);
65 lang_index = lang_numbers[enabledby_arg_langs[j]];
66 if (enables[lang_name,enabledby_name] == "") {
67 enabledby[lang_name,n_enabledby_lang[lang_index]] = enabledby_name;
68 n_enabledby_lang[lang_index]++;
69 }
70 enables[lang_name,enabledby_name] = enables[lang_name,enabledby_name] opts[i] ",";
71 }
72 }
73 }
74}
75
76
30b0f428 77print "/* This file is auto-generated by optc-gen.awk. */"
e8b212b8 78print ""
5c5ccba2 79n_headers = split(header_name, headers, " ")
80for (i = 1; i <= n_headers; i++)
81 print "#include " quote headers[i] quote
e8b212b8 82print "#include " quote "opts.h" quote
6ce616df 83print "#include " quote "intl.h" quote
19c83f00 84print "#include " quote "insn-attr-common.h" quote
e8b212b8 85print ""
86
755fa783 87if (n_extra_c_includes > 0) {
88 for (i = 0; i < n_extra_c_includes; i++) {
89 print "#include " quote extra_c_includes[i] quote
90 }
91 print ""
92}
93
d62a5950 94for (i = 0; i < n_enums; i++) {
95 name = enum_names[i]
96 type = enum_type[name]
97 print "static const struct cl_enum_arg cl_enum_" name \
98 "_data[] = "
99 print "{"
100 print enum_data[name] " { NULL, 0, 0 }"
101 print "};"
102 print ""
103 print "static void"
104 print "cl_enum_" name "_set (void *var, int value)"
105 print "{"
106 print " *((" type " *) var) = (" type ") value;"
107 print "}"
108 print ""
109 print "static int"
110 print "cl_enum_" name "_get (const void *var)"
111 print "{"
112 print " return (int) *((const " type " *) var);"
113 print "}"
114 print ""
115}
116
117print "const struct cl_enum cl_enums[] ="
118print "{"
119for (i = 0; i < n_enums; i++) {
120 name = enum_names[i]
121 ehelp = enum_help[name]
122 if (ehelp == "")
123 ehelp = "NULL"
124 else
125 ehelp = quote ehelp quote
126 unknown_error = enum_unknown_error[name]
127 if (unknown_error == "")
128 unknown_error = "NULL"
129 else
130 unknown_error = quote unknown_error quote
131 print " {"
132 print " " ehelp ","
133 print " " unknown_error ","
134 print " cl_enum_" name "_data,"
135 print " sizeof (" enum_type[name] "),"
136 print " cl_enum_" name "_set,"
137 print " cl_enum_" name "_get"
138 print " },"
139}
140print "};"
141print "const unsigned int cl_enums_count = " n_enums ";"
142print ""
143
f3f006ad 144print "const struct gcc_options global_options_init =\n{"
0f8defe5 145for (i = 0; i < n_extra_vars; i++) {
5ae82d58 146 var = extra_vars[i]
147 init = extra_vars[i]
148 if (var ~ "=" ) {
149 sub(".*= *", "", init)
5ae82d58 150 } else {
151 init = "0"
152 }
c3be4b75 153 sub(" *=.*", "", var)
154 name = var
155 sub("^.*[ *]", "", name)
156 sub("\\[.*\\]$", "", name)
157 var_seen[name] = 1
158 print " " init ", /* " name " */"
0f8defe5 159}
e8b212b8 160for (i = 0; i < n_opts; i++) {
161 name = var_name(flags[i]);
162 if (name == "")
163 continue;
164
5ae82d58 165 init = opt_args("Init", flags[i])
166 if (init != "") {
167 if (name in var_init && var_init[name] != init)
168 print "#error multiple initializers for " name
169 var_init[name] = init
a1baa5f1 170 }
5ae82d58 171}
172for (i = 0; i < n_opts; i++) {
173 name = var_name(flags[i]);
174 if (name == "")
175 continue;
176
177 if (name in var_seen)
178 continue;
179
180 if (name in var_init)
181 init = var_init[name]
182 else
183 init = "0"
a150399d 184
5ae82d58 185 print " " init ", /* " name " */"
c5e839cb 186
187 var_seen[name] = 1;
a150399d 188}
d6907cfa 189for (i = 0; i < n_opts; i++) {
190 name = static_var(opts[i], flags[i]);
2c5d2e39 191 if (name != "") {
192 print " 0, /* " name " (private state) */"
193 print "#undef x_" name
194 }
d6907cfa 195}
ecee1b29 196for (i = 0; i < n_opts; i++) {
197 if (flag_set_p("SetByCombined", flags[i]))
198 print " false, /* frontend_set_" var_name(flags[i]) " */"
199}
2c5d2e39 200print "};"
d6907cfa 201print ""
f3f006ad 202print "struct gcc_options global_options;"
f83b64ca 203print "struct gcc_options global_options_set;"
204print ""
e8b212b8 205
206print "const char * const lang_names[] =\n{"
207for (i = 0; i < n_langs; i++) {
d3b7ee7c 208 macros[i] = "CL_" lang_sanitized_name(langs[i])
e8b212b8 209 s = substr(" ", length (macros[i]))
210 print " " quote langs[i] quote ","
211 }
212
213print " 0\n};\n"
214print "const unsigned int cl_options_count = N_OPTS;\n"
634b1f85 215print "#if (1U << " n_langs ") > CL_MIN_OPTION_CLASS"
216print " #error the number of languages exceeds the implementation limit"
217print "#endif"
87c75316 218print "const unsigned int cl_lang_count = " n_langs ";\n"
e8b212b8 219
220print "const struct cl_option cl_options[] =\n{"
221
a1baa5f1 222j = 0
223for (i = 0; i < n_opts; i++) {
e8b212b8 224 back_chain[i] = "N_OPTS";
a1baa5f1 225 indices[opts[i]] = j;
226 # Combine the flags of identical switches. Switches
227 # appear many times if they are handled by many front
228 # ends, for example.
229 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
230 flags[i + 1] = flags[i] " " flags[i + 1];
e6a03973 231 if (help[i + 1] == "")
232 help[i + 1] = help[i]
0863a8f4 233 else if (help[i] != "" && help[i + 1] != help[i])
4584d89f 234 print "#error Multiple different help strings for " \
235 opts[i] ":\n\t" help[i] "\n\t" help[i + 1]
236
a1baa5f1 237 i++;
238 back_chain[i] = "N_OPTS";
239 indices[opts[i]] = j;
240 }
241 j++;
242}
e8b212b8 243
d39ef3aa 244for (i = 0; i < n_opts; i++) {
e6a03973 245 # With identical flags, pick only the last one. The
246 # earlier loop ensured that it has all flags merged,
247 # and a nonempty help text if one of the texts was nonempty.
d39ef3aa 248 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
d39ef3aa 249 i++;
250 }
e8b212b8 251
d39ef3aa 252 len = length (opts[i]);
d4c7816a 253 enum = opt_enum(opts[i])
d39ef3aa 254
255 # If this switch takes joined arguments, back-chain all
256 # subsequent switches to it for which it is a prefix. If
257 # a later switch S is a longer prefix of a switch T, T
258 # will be back-chained to S in a later iteration of this
259 # for() loop, which is what we want.
260 if (flag_set_p("Joined.*", flags[i])) {
261 for (j = i + 1; j < n_opts; j++) {
262 if (substr (opts[j], 1, len) != opts[i])
263 break;
264 back_chain[j] = enum;
e8b212b8 265 }
d39ef3aa 266 }
e8b212b8 267
d39ef3aa 268 s = substr(" ", length (opts[i]))
269 if (i + 1 == n_opts)
270 comma = ""
e8b212b8 271
d39ef3aa 272 if (help[i] == "")
273 hlp = "0"
274 else
275 hlp = quote help[i] quote;
e8b212b8 276
fecf9011 277 missing_arg_error = opt_args("MissingArgError", flags[i])
278 if (missing_arg_error == "")
279 missing_arg_error = "0"
280 else
281 missing_arg_error = quote missing_arg_error quote
282
3b0273a1 283
284 warn_message = opt_args("Warn", flags[i])
285 if (warn_message == "")
286 warn_message = "0"
287 else
288 warn_message = quote warn_message quote
289
67089c6b 290 alias_arg = opt_args("Alias", flags[i])
291 if (alias_arg == "") {
3b0273a1 292 if (flag_set_p("Ignore", flags[i]))
293 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
294 else
295 alias_data = "NULL, NULL, N_OPTS"
67089c6b 296 } else {
297 alias_opt = nth_arg(0, alias_arg)
298 alias_posarg = nth_arg(1, alias_arg)
299 alias_negarg = nth_arg(2, alias_arg)
300
2c5d2e39 301 if (var_ref(opts[i], flags[i]) != "-1")
67089c6b 302 print "#error Alias setting variable"
303
304 if (alias_posarg != "" && alias_negarg == "") {
305 if (!flag_set_p("RejectNegative", flags[i]) \
306 && opts[i] ~ "^[Wfm]")
307 print "#error Alias with single argument " \
308 "allowing negative form"
309 }
4e775b8e 310 if (alias_posarg != "" \
311 && flag_set_p("NegativeAlias", flags[i])) {
312 print "#error Alias with multiple arguments " \
313 "used with NegativeAlias"
314 }
67089c6b 315
316 alias_opt = opt_enum(alias_opt)
317 if (alias_posarg == "")
318 alias_posarg = "NULL"
319 else
320 alias_posarg = quote alias_posarg quote
321 if (alias_negarg == "")
322 alias_negarg = "NULL"
323 else
324 alias_negarg = quote alias_negarg quote
325 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
326 }
327
a1baa5f1 328 neg = opt_args("Negative", flags[i]);
329 if (neg != "")
330 idx = indices[neg]
331 else {
332 if (flag_set_p("RejectNegative", flags[i]))
333 idx = -1;
334 else {
335 if (opts[i] ~ "^[Wfm]")
336 idx = indices[opts[i]];
337 else
338 idx = -1;
339 }
340 }
13c2c394 341 # Split the printf after %u to work around an ia64-hp-hpux11.23
342 # awk bug.
3b0273a1 343 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
344 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
67089c6b 345 alias_data, back_chain[i], len)
13c2c394 346 printf(" %d,\n", idx)
5c5ccba2 347 condition = opt_args("Condition", flags[i])
348 cl_flags = switch_flags(flags[i])
ec840af4 349 cl_bit_fields = switch_bit_fields(flags[i])
350 cl_zero_bit_fields = switch_bit_fields("")
5c5ccba2 351 if (condition != "")
352 printf("#if %s\n" \
353 " %s,\n" \
ec840af4 354 " 0, %s,\n" \
5c5ccba2 355 "#else\n" \
ec840af4 356 " 0,\n" \
357 " 1 /* Disabled. */, %s,\n" \
5c5ccba2 358 "#endif\n",
ec840af4 359 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
5c5ccba2 360 else
ec840af4 361 printf(" %s,\n" \
362 " 0, %s,\n",
363 cl_flags, cl_bit_fields)
d6907cfa 364 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
365 var_set(flags[i]), comma)
e8b212b8 366}
367
368print "};"
46f8e3b0 369
fbb6fbd8 370print "\n\n"
371print "bool "
372print "common_handle_option_auto (struct gcc_options *opts, "
373print " struct gcc_options *opts_set, "
374print " const struct cl_decoded_option *decoded, "
375print " unsigned int lang_mask, int kind, "
376print " location_t loc, "
377print " const struct cl_option_handlers *handlers, "
378print " diagnostic_context *dc) "
379print "{ "
380print " size_t scode = decoded->opt_index; "
381print " int value = decoded->value; "
382print " enum opt_code code = (enum opt_code) scode; "
383print " "
384print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
385print " "
386print " switch (code) "
387print " { "
fbb6fbd8 388for (i = 0; i < n_enabledby; i++) {
389 enabledby_name = enabledby[i];
390 print " case " opt_enum(enabledby_name) ":"
391 n_enables = split(enables[enabledby_name], thisenable, ",");
392 for (j = 1; j < n_enables; j++) {
393 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
394 print " if (!opts_set->x_" opt_var_name ")"
395 print " handle_generated_option (opts, opts_set,"
396 print " " opt_enum(thisenable[j]) ", NULL, value,"
397 print " lang_mask, kind, loc, handlers, dc);"
398 }
399 print " break;\n"
fbb6fbd8 400}
401print " default: "
402print " break; "
403print " } "
404print " return true; "
405print "} "
406
d3b7ee7c 407# Handle LangEnabledBy
408for (i = 0; i < n_langs; i++) {
409 lang_name = lang_sanitized_name(langs[i]);
410 mark_unused = " ATTRIBUTE_UNUSED";
411
412 print "\n\n"
413 print "bool "
414 print lang_name "_handle_option_auto (struct gcc_options *opts" mark_unused ", "
415 print " struct gcc_options *opts_set" mark_unused ", "
416 print " size_t scode" mark_unused ", const char *arg" mark_unused ", int value" mark_unused ", "
417 print " unsigned int lang_mask" mark_unused ", int kind" mark_unused ", "
418 print " location_t loc" mark_unused ", "
419 print " const struct cl_option_handlers *handlers" mark_unused ", "
420 print " diagnostic_context *dc" mark_unused ") "
421 print "{ "
422 print " enum opt_code code = (enum opt_code) scode; "
423 print " "
424 print " switch (code) "
425 print " { "
426
427 for (k = 0; k < n_enabledby_lang[i]; k++) {
428 enabledby_name = enabledby[lang_name,k];
429 print " case " opt_enum(enabledby_name) ":"
430 n_enables = split(enables[lang_name,enabledby_name], thisenable, ",");
431 for (j = 1; j < n_enables; j++) {
432 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
433 print " if (!opts_set->x_" opt_var_name ")"
434 print " handle_generated_option (opts, opts_set,"
435 print " " opt_enum(thisenable[j]) ", arg, value,"
436 print " lang_mask, kind, loc, handlers, dc);"
437 }
438 print " break;\n"
439 }
440 print " default: "
441 print " break; "
442 print " } "
443 print " return true; "
444 print "} "
445}
446
e8b212b8 447}