]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/optc-gen.awk
2011-07-25 Paolo Carlini <paolo.carlini@oracle.com>
[thirdparty/gcc.git] / gcc / optc-gen.awk
CommitLineData
ecee1b29 1# Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
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 {
30b0f428 33print "/* This file is auto-generated by optc-gen.awk. */"
e8b212b8 34print ""
5c5ccba2 35n_headers = split(header_name, headers, " ")
36for (i = 1; i <= n_headers; i++)
37 print "#include " quote headers[i] quote
e8b212b8 38print "#include " quote "opts.h" quote
6ce616df 39print "#include " quote "intl.h" quote
e8b212b8 40print ""
41
755fa783 42if (n_extra_c_includes > 0) {
43 for (i = 0; i < n_extra_c_includes; i++) {
44 print "#include " quote extra_c_includes[i] quote
45 }
46 print ""
47}
48
d62a5950 49for (i = 0; i < n_enums; i++) {
50 name = enum_names[i]
51 type = enum_type[name]
52 print "static const struct cl_enum_arg cl_enum_" name \
53 "_data[] = "
54 print "{"
55 print enum_data[name] " { NULL, 0, 0 }"
56 print "};"
57 print ""
58 print "static void"
59 print "cl_enum_" name "_set (void *var, int value)"
60 print "{"
61 print " *((" type " *) var) = (" type ") value;"
62 print "}"
63 print ""
64 print "static int"
65 print "cl_enum_" name "_get (const void *var)"
66 print "{"
67 print " return (int) *((const " type " *) var);"
68 print "}"
69 print ""
70}
71
72print "const struct cl_enum cl_enums[] ="
73print "{"
74for (i = 0; i < n_enums; i++) {
75 name = enum_names[i]
76 ehelp = enum_help[name]
77 if (ehelp == "")
78 ehelp = "NULL"
79 else
80 ehelp = quote ehelp quote
81 unknown_error = enum_unknown_error[name]
82 if (unknown_error == "")
83 unknown_error = "NULL"
84 else
85 unknown_error = quote unknown_error quote
86 print " {"
87 print " " ehelp ","
88 print " " unknown_error ","
89 print " cl_enum_" name "_data,"
90 print " sizeof (" enum_type[name] "),"
91 print " cl_enum_" name "_set,"
92 print " cl_enum_" name "_get"
93 print " },"
94}
95print "};"
96print "const unsigned int cl_enums_count = " n_enums ";"
97print ""
98
f3f006ad 99print "const struct gcc_options global_options_init =\n{"
0f8defe5 100for (i = 0; i < n_extra_vars; i++) {
5ae82d58 101 var = extra_vars[i]
102 init = extra_vars[i]
103 if (var ~ "=" ) {
104 sub(".*= *", "", init)
5ae82d58 105 } else {
106 init = "0"
107 }
c3be4b75 108 sub(" *=.*", "", var)
109 name = var
110 sub("^.*[ *]", "", name)
111 sub("\\[.*\\]$", "", name)
112 var_seen[name] = 1
113 print " " init ", /* " name " */"
0f8defe5 114}
e8b212b8 115for (i = 0; i < n_opts; i++) {
116 name = var_name(flags[i]);
117 if (name == "")
118 continue;
119
5ae82d58 120 init = opt_args("Init", flags[i])
121 if (init != "") {
122 if (name in var_init && var_init[name] != init)
123 print "#error multiple initializers for " name
124 var_init[name] = init
a1baa5f1 125 }
5ae82d58 126}
127for (i = 0; i < n_opts; i++) {
128 name = var_name(flags[i]);
129 if (name == "")
130 continue;
131
132 if (name in var_seen)
133 continue;
134
135 if (name in var_init)
136 init = var_init[name]
137 else
138 init = "0"
a150399d 139
5ae82d58 140 print " " init ", /* " name " */"
c5e839cb 141
142 var_seen[name] = 1;
a150399d 143}
d6907cfa 144for (i = 0; i < n_opts; i++) {
145 name = static_var(opts[i], flags[i]);
2c5d2e39 146 if (name != "") {
147 print " 0, /* " name " (private state) */"
148 print "#undef x_" name
149 }
d6907cfa 150}
ecee1b29 151for (i = 0; i < n_opts; i++) {
152 if (flag_set_p("SetByCombined", flags[i]))
153 print " false, /* frontend_set_" var_name(flags[i]) " */"
154}
2c5d2e39 155print "};"
d6907cfa 156print ""
f3f006ad 157print "struct gcc_options global_options;"
f83b64ca 158print "struct gcc_options global_options_set;"
159print ""
e8b212b8 160
161print "const char * const lang_names[] =\n{"
162for (i = 0; i < n_langs; i++) {
163 macros[i] = "CL_" langs[i]
ca40ed4b 164 gsub( "[^" alnum "_]", "X", macros[i] )
e8b212b8 165 s = substr(" ", length (macros[i]))
166 print " " quote langs[i] quote ","
167 }
168
169print " 0\n};\n"
170print "const unsigned int cl_options_count = N_OPTS;\n"
87c75316 171print "const unsigned int cl_lang_count = " n_langs ";\n"
e8b212b8 172
173print "const struct cl_option cl_options[] =\n{"
174
a1baa5f1 175j = 0
176for (i = 0; i < n_opts; i++) {
e8b212b8 177 back_chain[i] = "N_OPTS";
a1baa5f1 178 indices[opts[i]] = j;
179 # Combine the flags of identical switches. Switches
180 # appear many times if they are handled by many front
181 # ends, for example.
182 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
183 flags[i + 1] = flags[i] " " flags[i + 1];
e6a03973 184 if (help[i + 1] == "")
185 help[i + 1] = help[i]
0863a8f4 186 else if (help[i] != "" && help[i + 1] != help[i])
187 print "warning: multiple different help strings for " \
188 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
189 | "cat 1>&2"
a1baa5f1 190 i++;
191 back_chain[i] = "N_OPTS";
192 indices[opts[i]] = j;
193 }
194 j++;
195}
e8b212b8 196
d39ef3aa 197for (i = 0; i < n_opts; i++) {
e6a03973 198 # With identical flags, pick only the last one. The
199 # earlier loop ensured that it has all flags merged,
200 # and a nonempty help text if one of the texts was nonempty.
d39ef3aa 201 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
d39ef3aa 202 i++;
203 }
e8b212b8 204
d39ef3aa 205 len = length (opts[i]);
d4c7816a 206 enum = opt_enum(opts[i])
d39ef3aa 207
208 # If this switch takes joined arguments, back-chain all
209 # subsequent switches to it for which it is a prefix. If
210 # a later switch S is a longer prefix of a switch T, T
211 # will be back-chained to S in a later iteration of this
212 # for() loop, which is what we want.
213 if (flag_set_p("Joined.*", flags[i])) {
214 for (j = i + 1; j < n_opts; j++) {
215 if (substr (opts[j], 1, len) != opts[i])
216 break;
217 back_chain[j] = enum;
e8b212b8 218 }
d39ef3aa 219 }
e8b212b8 220
d39ef3aa 221 s = substr(" ", length (opts[i]))
222 if (i + 1 == n_opts)
223 comma = ""
e8b212b8 224
d39ef3aa 225 if (help[i] == "")
226 hlp = "0"
227 else
228 hlp = quote help[i] quote;
e8b212b8 229
fecf9011 230 missing_arg_error = opt_args("MissingArgError", flags[i])
231 if (missing_arg_error == "")
232 missing_arg_error = "0"
233 else
234 missing_arg_error = quote missing_arg_error quote
235
3b0273a1 236
237 warn_message = opt_args("Warn", flags[i])
238 if (warn_message == "")
239 warn_message = "0"
240 else
241 warn_message = quote warn_message quote
242
67089c6b 243 alias_arg = opt_args("Alias", flags[i])
244 if (alias_arg == "") {
3b0273a1 245 if (flag_set_p("Ignore", flags[i]))
246 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
247 else
248 alias_data = "NULL, NULL, N_OPTS"
67089c6b 249 } else {
250 alias_opt = nth_arg(0, alias_arg)
251 alias_posarg = nth_arg(1, alias_arg)
252 alias_negarg = nth_arg(2, alias_arg)
253
2c5d2e39 254 if (var_ref(opts[i], flags[i]) != "-1")
67089c6b 255 print "#error Alias setting variable"
256
257 if (alias_posarg != "" && alias_negarg == "") {
258 if (!flag_set_p("RejectNegative", flags[i]) \
259 && opts[i] ~ "^[Wfm]")
260 print "#error Alias with single argument " \
261 "allowing negative form"
262 }
4e775b8e 263 if (alias_posarg != "" \
264 && flag_set_p("NegativeAlias", flags[i])) {
265 print "#error Alias with multiple arguments " \
266 "used with NegativeAlias"
267 }
67089c6b 268
269 alias_opt = opt_enum(alias_opt)
270 if (alias_posarg == "")
271 alias_posarg = "NULL"
272 else
273 alias_posarg = quote alias_posarg quote
274 if (alias_negarg == "")
275 alias_negarg = "NULL"
276 else
277 alias_negarg = quote alias_negarg quote
278 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
279 }
280
a1baa5f1 281 neg = opt_args("Negative", flags[i]);
282 if (neg != "")
283 idx = indices[neg]
284 else {
285 if (flag_set_p("RejectNegative", flags[i]))
286 idx = -1;
287 else {
288 if (opts[i] ~ "^[Wfm]")
289 idx = indices[opts[i]];
290 else
291 idx = -1;
292 }
293 }
13c2c394 294 # Split the printf after %u to work around an ia64-hp-hpux11.23
295 # awk bug.
3b0273a1 296 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
297 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
67089c6b 298 alias_data, back_chain[i], len)
13c2c394 299 printf(" %d,\n", idx)
5c5ccba2 300 condition = opt_args("Condition", flags[i])
301 cl_flags = switch_flags(flags[i])
ec840af4 302 cl_bit_fields = switch_bit_fields(flags[i])
303 cl_zero_bit_fields = switch_bit_fields("")
5c5ccba2 304 if (condition != "")
305 printf("#if %s\n" \
306 " %s,\n" \
ec840af4 307 " 0, %s,\n" \
5c5ccba2 308 "#else\n" \
ec840af4 309 " 0,\n" \
310 " 1 /* Disabled. */, %s,\n" \
5c5ccba2 311 "#endif\n",
ec840af4 312 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
5c5ccba2 313 else
ec840af4 314 printf(" %s,\n" \
315 " 0, %s,\n",
316 cl_flags, cl_bit_fields)
d6907cfa 317 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
318 var_set(flags[i]), comma)
e8b212b8 319}
320
321print "};"
46f8e3b0 322
e8b212b8 323}