1#  Copyright (C) 2003-2021 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
34# Combine the flags of identical switches.  Switches
35# appear many times if they are handled by many front
36# ends, for example.
37for (i = 0; i < n_opts; i++) {
38    merged_flags[i] = flags[i]
39}
40for (i = 0; i < n_opts; i++) {
41    while(i + 1 != n_opts && opts[i] == opts[i + 1] ) {
42	merged_flags[i + 1] = merged_flags[i] " " merged_flags[i + 1];
43	i++;
44    }
45}
46
47# Record EnabledBy and LangEnabledBy uses.
48n_enabledby = 0;
49for (i = 0; i < n_langs; i++) {
50    n_enabledby_lang[i] = 0;
51}
52for (i = 0; i < n_opts; i++) {
53    enabledby_arg = opt_args("EnabledBy", flags[i]);
54    if (enabledby_arg != "") {
55        logical_and = index(enabledby_arg, " && ");
56        if (logical_and != 0) {
57            # EnabledBy(arg1 && arg2)
58            split_sep = " && ";
59        } else {
60            # EnabledBy(arg) or EnabledBy(arg1 || arg2 || arg3)
61            split_sep = " \\|\\| ";
62        }
63        n_enabledby_names = split(enabledby_arg, enabledby_names, split_sep);
64        if (logical_and != 0 && n_enabledby_names > 2) {
65            print "#error " opts[i] " EnabledBy(Wfoo && Wbar && Wbaz) currently not supported"
66        }
67        for (j = 1; j <= n_enabledby_names; j++) {
68            enabledby_name = enabledby_names[j];
69            enabledby_index = opt_numbers[enabledby_name];
70            if (enabledby_index == "") {
71                print "#error " opts[i] " Enabledby(" enabledby_name "), unknown option '" enabledby_name "'"
72            } else if (!flag_set_p("Common", merged_flags[enabledby_index])) {
73		print "#error " opts[i] " Enabledby(" enabledby_name "), '" \
74		    enabledby_name "' must have flag 'Common'"		\
75		    " to use Enabledby(), otherwise use LangEnabledBy()"
76	    } else {
77		condition = "";
78                if (logical_and != 0) {
79                    opt_var_name_1 = search_var_name(enabledby_names[1], opt_numbers, opts, flags, n_opts);
80                    opt_var_name_2 = search_var_name(enabledby_names[2], opt_numbers, opts, flags, n_opts);
81                    if (opt_var_name_1 == "") {
82                        print "#error " enabledby_names[1] " does not have a Var() flag"
83                    }
84                    if (opt_var_name_2 == "") {
85                        print "#error " enabledby_names[2] " does not have a Var() flag"
86                    }
87                    condition = "opts->x_" opt_var_name_1 " && opts->x_" opt_var_name_2;
88                }
89                if (enables[enabledby_name] == "") {
90                    enabledby[n_enabledby] = enabledby_name;
91                    n_enabledby++;
92                }
93                enables[enabledby_name] = enables[enabledby_name] opts[i] ";";
94                enablesif[enabledby_name] = enablesif[enabledby_name] condition ";";
95            }
96        }
97    }
98
99    enabledby_arg = opt_args("LangEnabledBy", flags[i]);
100    if (enabledby_arg != "") {
101        enabledby_langs = nth_arg(0, enabledby_arg);
102        enabledby_name = nth_arg(1, enabledby_arg);
103        enabledby_posarg = nth_arg(2, enabledby_arg);
104	enabledby_negarg = nth_arg(3, enabledby_arg);
105        lang_enabled_by(enabledby_langs, enabledby_name, enabledby_posarg, enabledby_negarg);
106    }
107
108    if (flag_set_p("Param", flags[i]) && !(opts[i] ~ "^-param="))
109      print "#error Parameter option name '" opts[i] "' must start with '-param='"
110}
111
112
113print "/* This file is auto-generated by optc-gen.awk.  */"
114print ""
115n_headers = split(header_name, headers, " ")
116for (i = 1; i <= n_headers; i++)
117	print "#include " quote headers[i] quote
118print "#include " quote "opts.h" quote
119print "#include " quote "intl.h" quote
120print "#include " quote "insn-attr-common.h" quote
121print ""
122
123if (n_extra_c_includes > 0) {
124	for (i = 0; i < n_extra_c_includes; i++) {
125		print "#include " quote extra_c_includes[i] quote
126	}
127	print ""
128}
129
130for (i = 0; i < n_enums; i++) {
131	name = enum_names[i]
132	type = enum_type[name]
133	print "static const struct cl_enum_arg cl_enum_" name \
134	    "_data[] = "
135	print "{"
136	print enum_data[name] "  { NULL, 0, 0 }"
137	print "};"
138	print ""
139	print "static void"
140	print "cl_enum_" name "_set (void *var, int value)"
141	print "{"
142	print "  *((" type " *) var) = (" type ") value;"
143	print "}"
144	print ""
145	print "static int"
146	print "cl_enum_" name "_get (const void *var)"
147	print "{"
148	print "  return (int) *((const " type " *) var);"
149	print "}"
150	print ""
151}
152
153print "const struct cl_enum cl_enums[] ="
154print "{"
155for (i = 0; i < n_enums; i++) {
156	name = enum_names[i]
157	ehelp = enum_help[name]
158	if (ehelp == "")
159		ehelp = "NULL"
160	else
161		ehelp = quote ehelp quote
162	unknown_error = enum_unknown_error[name]
163	if (unknown_error == "")
164		unknown_error = "NULL"
165	else
166		unknown_error = quote unknown_error quote
167	print "  {"
168	print "    " ehelp ","
169	print "    " unknown_error ","
170	print "    cl_enum_" name "_data,"
171	print "    sizeof (" enum_type[name] "),"
172	print "    cl_enum_" name "_set,"
173	print "    cl_enum_" name "_get"
174	print "  },"
175}
176print "};"
177print "const unsigned int cl_enums_count = " n_enums ";"
178print ""
179
180print "const struct gcc_options global_options_init =\n{"
181for (i = 0; i < n_extra_vars; i++) {
182	var = extra_vars[i]
183	init = extra_vars[i]
184	if (var ~ "=" ) {
185		sub(".*= *", "", init)
186	} else {
187		init = "0"
188	}
189	sub(" *=.*", "", var)
190	name = var
191	sub("^.*[ *]", "", name)
192	sub("\\[.*\\]$", "", name)
193	var_seen[name] = 1
194	print "  " init ", /* " name " */"
195}
196for (i = 0; i < n_opts; i++) {
197	name = var_name(flags[i]);
198	if (name == "")
199		continue;
200
201	init = opt_args("Init", flags[i])
202	if (init != "") {
203		if (name in var_init && var_init[name] != init)
204			print "#error multiple initializers for " name
205		var_init[name] = init
206	}
207}
208for (i = 0; i < n_opts; i++) {
209	name = var_name(flags[i]);
210	if (name == "")
211		continue;
212
213	if (name in var_seen)
214		continue;
215
216	if (name in var_init)
217		init = var_init[name]
218	else
219		init = "0"
220
221	print "  " init ", /* " name " */"
222
223	var_seen[name] = 1;
224}
225for (i = 0; i < n_opts; i++) {
226	name = static_var(opts[i], flags[i]);
227	if (name != "") {
228		print "  0, /* " name " (private state) */"
229		print "#undef x_" name
230	}
231}
232for (i = 0; i < n_opts; i++) {
233	if (flag_set_p("SetByCombined", flags[i]))
234		print "  false, /* frontend_set_" var_name(flags[i]) " */"
235}
236print "};"
237print ""
238print "struct gcc_options global_options;"
239print "struct gcc_options global_options_set;"
240print ""
241
242print "const char * const lang_names[] =\n{"
243for (i = 0; i < n_langs; i++) {
244        macros[i] = "CL_" lang_sanitized_name(langs[i])
245	s = substr("         ", length (macros[i]))
246	print "  " quote langs[i] quote ","
247    }
248
249print "  0\n};\n"
250print "const unsigned int cl_options_count = N_OPTS;\n"
251print "#if (1U << " n_langs ") > CL_MIN_OPTION_CLASS"
252print "  #error the number of languages exceeds the implementation limit"
253print "#endif"
254print "const unsigned int cl_lang_count = " n_langs ";\n"
255
256print "const struct cl_option cl_options[] =\n{"
257
258j = 0
259for (i = 0; i < n_opts; i++) {
260	back_chain[i] = "N_OPTS";
261	indices[opts[i]] = j;
262	# Combine the flags of identical switches.  Switches
263	# appear many times if they are handled by many front
264	# ends, for example.
265	while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
266		flags[i + 1] = flags[i] " " flags[i + 1];
267		if (help[i + 1] == "")
268			help[i + 1] = help[i]
269		else if (help[i] != "" && help[i + 1] != help[i])
270			print "#error Multiple different help strings for " \
271				opts[i] ":\n\t" help[i] "\n\t" help[i + 1]
272
273		i++;
274		back_chain[i] = "N_OPTS";
275		indices[opts[i]] = j;
276	}
277	j++;
278}
279
280optindex = 0
281for (i = 0; i < n_opts; i++) {
282	# With identical flags, pick only the last one.  The
283	# earlier loop ensured that it has all flags merged,
284	# and a nonempty help text if one of the texts was nonempty.
285	while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
286		i++;
287	}
288
289	len = length (opts[i]);
290	enum = opt_enum(opts[i])
291
292	# If this switch takes joined arguments, back-chain all
293	# subsequent switches to it for which it is a prefix.  If
294	# a later switch S is a longer prefix of a switch T, T
295	# will be back-chained to S in a later iteration of this
296	# for() loop, which is what we want.
297	if (flag_set_p("Joined.*", flags[i])) {
298		for (j = i + 1; j < n_opts; j++) {
299			if (substr (opts[j], 1, len) != opts[i])
300				break;
301			back_chain[j] = enum;
302		}
303	}
304
305	s = substr("                                  ", length (opts[i]))
306	if (i + 1 == n_opts)
307		comma = ""
308
309	if (help[i] == "")
310		hlp = "NULL"
311	else
312		hlp = quote help[i] quote;
313
314	missing_arg_error = opt_args("MissingArgError", flags[i])
315	if (missing_arg_error == "")
316		missing_arg_error = "NULL"
317	else
318		missing_arg_error = quote missing_arg_error quote
319
320
321	warn_message = opt_args("Warn", flags[i])
322	if (warn_message == "")
323		warn_message = "NULL"
324	else
325		warn_message = quote warn_message quote
326
327	alias_arg = opt_args("Alias", flags[i])
328	if (alias_arg == "") {
329		if (flag_set_p("Ignore", flags[i])) {
330			  alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
331        if (warn_message != "NULL")
332				  print "#error Ignored option with Warn"
333        if (var_name(flags[i]) != "")
334				  print "#error Ignored option with Var"
335      }
336    else if (flag_set_p("Deprecated", flags[i]))
337        print "#error Deprecated was replaced with WarnRemoved"
338    else if (flag_set_p("WarnRemoved", flags[i])) {
339			  alias_data = "NULL, NULL, OPT_SPECIAL_warn_removed"
340        if (warn_message != "NULL")
341				  print "#error WarnRemoved option with Warn"
342      }
343		else
344			alias_data = "NULL, NULL, N_OPTS"
345		if (flag_set_p("Enum.*", flags[i])) {
346			if (!flag_set_p("RejectNegative", flags[i]) \
347			    && opts[i] ~ "^[Wfgm]")
348				print "#error Enum allowing negative form"
349		}
350	} else {
351		alias_opt = nth_arg(0, alias_arg)
352		alias_posarg = nth_arg(1, alias_arg)
353		alias_negarg = nth_arg(2, alias_arg)
354
355		if (var_ref(opts[i], flags[i]) != "(unsigned short) -1")
356			print "#error Alias setting variable"
357
358		if (alias_posarg != "" && alias_negarg == "") {
359			if (!flag_set_p("RejectNegative", flags[i]) \
360			    && opts[i] ~ "^[Wfm]")
361				print "#error Alias with single argument " \
362					"allowing negative form"
363		}
364		if (alias_posarg != "" \
365		    && flag_set_p("NegativeAlias", flags[i])) {
366			print "#error Alias with multiple arguments " \
367				"used with NegativeAlias"
368		}
369
370		alias_opt = opt_enum(alias_opt)
371		if (alias_posarg == "")
372			alias_posarg = "NULL"
373		else
374			alias_posarg = quote alias_posarg quote
375		if (alias_negarg == "")
376			alias_negarg = "NULL"
377		else
378			alias_negarg = quote alias_negarg quote
379		alias_data = alias_posarg ", " alias_negarg ", " alias_opt
380	}
381
382	neg = opt_args("Negative", flags[i]);
383	if (neg != "")
384		idx = indices[neg]
385	else {
386		if (flag_set_p("RejectNegative", flags[i]))
387			idx = -1;
388		else {
389			if (opts[i] ~ "^[Wfgm]")
390				idx = indices[opts[i]];
391			else
392				idx = -1;
393		}
394	}
395	# Split the printf after %u to work around an ia64-hp-hpux11.23
396	# awk bug.
397	printf(" /* [%i] = */ {\n", optindex)
398	printf("    %c-%s%c,\n    %s,\n    %s,\n    %s,\n    %s, %s, %u,",
399	       quote, opts[i], quote, hlp, missing_arg_error, warn_message,
400	       alias_data, back_chain[i], len)
401	printf(" /* .neg_idx = */ %d,\n", idx)
402	condition = opt_args("Condition", flags[i])
403	cl_flags = switch_flags(flags[i])
404	cl_bit_fields = switch_bit_fields(flags[i])
405	cl_zero_bit_fields = switch_bit_fields("")
406	if (condition != "")
407		printf("#if %s\n" \
408		       "    %s,\n" \
409		       "    0, %s,\n" \
410		       "#else\n" \
411		       "    0,\n" \
412		       "    1 /* Disabled.  */, %s,\n" \
413		       "#endif\n",
414		       condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
415	else
416		printf("    %s,\n" \
417		       "    0, %s,\n",
418		       cl_flags, cl_bit_fields)
419	printf("    %s, %s, %s }%s\n", var_ref(opts[i], flags[i]),
420	       var_set(flags[i]), integer_range_info(opt_args("IntegerRange", flags[i]),
421		    opt_args("Init", flags[i]), opts[i]), comma)
422
423	# Bump up the informational option index.
424	++optindex
425 }
426
427print "};"
428
429print "\n\n"
430print "bool                                                                  "
431print "common_handle_option_auto (struct gcc_options *opts,                  "
432print "                           struct gcc_options *opts_set,              "
433print "                           const struct cl_decoded_option *decoded,   "
434print "                           unsigned int lang_mask, int kind,          "
435print "                           location_t loc,                            "
436print "                           const struct cl_option_handlers *handlers, "
437print "                           diagnostic_context *dc)                    "
438print "{                                                                     "
439print "  size_t scode = decoded->opt_index;                                  "
440print "  HOST_WIDE_INT value = decoded->value;                               "
441print "  enum opt_code code = (enum opt_code) scode;                         "
442print "                                                                      "
443print "  gcc_assert (decoded->canonical_option_num_elements <= 2);           "
444print "                                                                      "
445print "  switch (code)                                                       "
446print "    {                                                                 "
447# Handle EnabledBy
448for (i = 0; i < n_enabledby; i++) {
449    enabledby_name = enabledby[i];
450    print "    case " opt_enum(enabledby_name) ":"
451    n_enables = split(enables[enabledby_name], thisenable, ";");
452    n_enablesif = split(enablesif[enabledby_name], thisenableif, ";");
453    if (n_enables != n_enablesif) {
454        print "#error n_enables != n_enablesif: Something went wrong!"
455    }
456    for (j = 1; j < n_enables; j++) {
457        opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
458        if (opt_var_name != "") {
459            condition = "!opts_set->x_" opt_var_name
460            if (thisenableif[j] != "") {
461                value = "(" thisenableif[j] ")"
462            } else {
463                value = "value"
464            }
465            print "      if (" condition ")"
466            print "        handle_generated_option (opts, opts_set,"
467            print "                                 " opt_enum(thisenable[j]) ", NULL, " value ","
468            print "                                 lang_mask, kind, loc, handlers, true, dc);"
469        } else {
470            print "#error " thisenable[j] " does not have a Var() flag"
471        }
472    }
473    print "      break;\n"
474}
475print "    default:    "
476print "      break;    "
477print "    }           "
478print "  return true;  "
479print "}               "
480
481# Handle LangEnabledBy
482for (i = 0; i < n_langs; i++) {
483    lang_name = lang_sanitized_name(langs[i]);
484    mark_unused = " ATTRIBUTE_UNUSED";
485
486    print "\n\n"
487    print "bool                                                                  "
488    print lang_name "_handle_option_auto (struct gcc_options *opts" mark_unused ",              "
489    print "                           struct gcc_options *opts_set" mark_unused ",              "
490    print "                           size_t scode" mark_unused ", const char *arg" mark_unused ", HOST_WIDE_INT value" mark_unused ",  "
491    print "                           unsigned int lang_mask" mark_unused ", int kind" mark_unused ",          "
492    print "                           location_t loc" mark_unused ",                            "
493    print "                           const struct cl_option_handlers *handlers" mark_unused ", "
494    print "                           diagnostic_context *dc" mark_unused ")                    "
495    print "{                                                                     "
496    print "  enum opt_code code = (enum opt_code) scode;                         "
497    print "                                                                      "
498    print "  switch (code)                                                       "
499    print "    {                                                                 "
500
501    for (k = 0; k < n_enabledby_lang[i]; k++) {
502        enabledby_name = enabledby[lang_name,k];
503        print "    case " opt_enum(enabledby_name) ":"
504        n_thisenable = split(enables[lang_name,enabledby_name], thisenable, ";");
505        for (j = 1; j < n_thisenable; j++) {
506            n_thisenable_args = split(thisenable[j], thisenable_args, ",");
507            if (n_thisenable_args == 1) {
508                thisenable_opt = thisenable[j];
509                value = "value";
510            } else {
511                thisenable_opt = thisenable_args[1];
512                with_posarg = thisenable_args[2];
513                with_negarg = thisenable_args[3];
514                value = "value ? " with_posarg " : " with_negarg;
515            }
516            opt_var_name = var_name(flags[opt_numbers[thisenable_opt]]);
517            if (opt_var_name != "") {
518                print "      if (!opts_set->x_" opt_var_name ")"
519                print "        handle_generated_option (opts, opts_set,"
520                print "                                 " opt_enum(thisenable_opt) ", NULL, " value ","
521                print "                                 lang_mask, kind, loc, handlers, true, dc);"
522            } else {
523                print "#error " thisenable_opt " does not have a Var() flag"
524            }
525        }
526        print "      break;\n"
527    }
528    print "    default:    "
529    print "      break;    "
530    print "    }           "
531    print "  return true;  "
532    print "}               "
533}
534
535#Handle CPP()
536print "\n"
537print "#include " quote "cpplib.h" quote;
538print "void"
539print "cpp_handle_option_auto (const struct gcc_options * opts,                   "
540print "                        size_t scode, struct cpp_options * cpp_opts)"
541print "{                                                                     "
542print "  enum opt_code code = (enum opt_code) scode;                         "
543print "                                                                      "
544print "  switch (code)                                                       "
545print "    {                                                                 "
546for (i = 0; i < n_opts; i++) {
547    # With identical flags, pick only the last one.  The
548    # earlier loop ensured that it has all flags merged,
549    # and a nonempty help text if one of the texts was nonempty.
550    while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
551        i++;
552    }
553
554    cpp_option = nth_arg(0, opt_args("CPP", flags[i]));
555    if (cpp_option != "") {
556        opt_var_name = var_name(flags[i]);
557        init = opt_args("Init", flags[i])
558        if (opt_var_name != "" && init != "") {
559            print "    case " opt_enum(opts[i]) ":"
560            print "      cpp_opts->" cpp_option " = opts->x_" opt_var_name ";"
561            print "      break;"
562        } else if (opt_var_name == "" && init == "") {
563            print "#error CPP() requires setting Init() and Var() for " opts[i]
564        } else if (opt_var_name != "") {
565            print "#error CPP() requires setting Init() for " opts[i]
566        } else {
567            print "#error CPP() requires setting Var() for " opts[i]
568        }
569    }
570}
571print "    default:    "
572print "      break;    "
573print "    }           "
574print "}\n"
575print "void"
576print "init_global_opts_from_cpp(struct gcc_options * opts,                   "
577print "                         const struct cpp_options * cpp_opts)"
578print "{                                                                     "
579for (i = 0; i < n_opts; i++) {
580    # With identical flags, pick only the last one.  The
581    # earlier loop ensured that it has all flags merged,
582    # and a nonempty help text if one of the texts was nonempty.
583    while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
584        i++;
585    }
586    cpp_option = nth_arg(0, opt_args("CPP", flags[i]));
587    opt_var_name = var_name(flags[i]);
588    if (cpp_option != "" && opt_var_name != "") {
589        print "  opts->x_" opt_var_name " = cpp_opts->" cpp_option ";"
590    }
591}
592print "}               "
593
594split("", var_seen, ":")
595print "\n#if !defined(GENERATOR_FILE) && defined(ENABLE_PLUGIN)"
596print "DEBUG_VARIABLE const struct cl_var cl_vars[] =\n{"
597
598for (i = 0; i < n_opts; i++) {
599	name = var_name(flags[i]);
600	if (name == "")
601		continue;
602	var_seen[name] = 1;
603}
604
605for (i = 0; i < n_extra_vars; i++) {
606	var = extra_vars[i]
607	sub(" *=.*", "", var)
608	name = var
609	sub("^.*[ *]", "", name)
610	sub("\\[.*\\]$", "", name)
611	if (name in var_seen)
612		continue;
613	print "  { " quote name quote ", offsetof (struct gcc_options, x_" name ") },"
614	var_seen[name] = 1
615}
616
617print "  { NULL, (unsigned short) -1 }\n};\n#endif"
618
619}
620