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