2007-10-18 David S. Miller <davem@davemloft.net>
[official-gcc.git] / gcc / optc-gen.awk
blob17ed35d5a7d49918a26b3d62f34dd120cf22bf83
1 # Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
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.
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/>.
19 # This Awk script reads in the option records generated from
20 # opt-gather.awk, combines the flags of duplicat options and generates a
21 # C file.
23 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
26 # [-v header_name=header.h] < inputfile > options.c
28 BEGIN {
29 n_opts = 0
30 n_langs = 0
31 quote = "\042"
32 comma = ","
33 FS=SUBSEP
34 # Default the name of header created from opth-gen.awk to options.h
35 if (header_name == "") header_name="options.h"
38 # Collect the text and flags of each option into an array
40 if ($1 == "Language") {
41 langs[n_langs] = $2
42 n_langs++;
44 else {
45 name = opt_args("Mask", $1)
46 if (name == "") {
47 opts[n_opts] = $1
48 flags[n_opts] = $2
49 help[n_opts] = $3
50 n_opts++;
55 # Dump that array of options into a C file.
56 END {
57 print "/* This file is auto-generated by opts.sh. */"
58 print ""
59 n_headers = split(header_name, headers, " ")
60 for (i = 1; i <= n_headers; i++)
61 print "#include " quote headers[i] quote
62 print "#include " quote "opts.h" quote
63 print "#include " quote "intl.h" quote
64 print ""
65 print "#ifdef GCC_DRIVER"
66 print "int target_flags;"
67 print "#endif /* GCC_DRIVER */"
68 print ""
70 for (i = 0; i < n_opts; i++) {
71 name = var_name(flags[i]);
72 if (name == "")
73 continue;
75 if (flag_set_p("VarExists", flags[i])) {
76 # Need it for the gcc driver.
77 if (name in var_seen)
78 continue;
79 init = ""
80 gcc_driver = 1
82 else {
83 init = opt_args("Init", flags[i])
84 if (init != "")
85 init = " = " init;
86 else if (name in var_seen)
87 continue;
88 gcc_driver = 0
91 if (gcc_driver == 1)
92 print "#ifdef GCC_DRIVER"
93 print "/* Set by -" opts[i] "."
94 print " " help[i] " */"
95 print var_type(flags[i]) name init ";"
96 if (gcc_driver == 1)
97 print "#endif /* GCC_DRIVER */"
98 print ""
100 var_seen[name] = 1;
103 print ""
104 print "/* Local state variables. */"
105 for (i = 0; i < n_opts; i++) {
106 name = static_var(opts[i], flags[i]);
107 if (name != "")
108 print "static " var_type(flags[i]) name ";"
110 print ""
112 print "const char * const lang_names[] =\n{"
113 for (i = 0; i < n_langs; i++) {
114 macros[i] = "CL_" langs[i]
115 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
116 s = substr(" ", length (macros[i]))
117 print " " quote langs[i] quote ","
120 print " 0\n};\n"
121 print "const unsigned int cl_options_count = N_OPTS;\n"
122 print "const unsigned int cl_lang_count = " n_langs ";\n"
124 print "const struct cl_option cl_options[] =\n{"
126 j = 0
127 for (i = 0; i < n_opts; i++) {
128 back_chain[i] = "N_OPTS";
129 indices[opts[i]] = j;
130 # Combine the flags of identical switches. Switches
131 # appear many times if they are handled by many front
132 # ends, for example.
133 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
134 flags[i + 1] = flags[i] " " flags[i + 1];
135 i++;
136 back_chain[i] = "N_OPTS";
137 indices[opts[i]] = j;
139 j++;
142 for (i = 0; i < n_opts; i++) {
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++;
151 len = length (opts[i]);
152 enum = "OPT_" opts[i]
153 if (opts[i] == "finline-limit=")
154 enum = enum "eq"
155 gsub ("[^A-Za-z0-9]", "_", enum)
157 # If this switch takes joined arguments, back-chain all
158 # subsequent switches to it for which it is a prefix. If
159 # a later switch S is a longer prefix of a switch T, T
160 # will be back-chained to S in a later iteration of this
161 # for() loop, which is what we want.
162 if (flag_set_p("Joined.*", flags[i])) {
163 for (j = i + 1; j < n_opts; j++) {
164 if (substr (opts[j], 1, len) != opts[i])
165 break;
166 back_chain[j] = enum;
170 s = substr(" ", length (opts[i]))
171 if (i + 1 == n_opts)
172 comma = ""
174 if (help[i] == "")
175 hlp = "0"
176 else
177 hlp = quote help[i] quote;
179 neg = opt_args("Negative", flags[i]);
180 if (neg != "")
181 idx = indices[neg]
182 else {
183 if (flag_set_p("RejectNegative", flags[i]))
184 idx = -1;
185 else {
186 if (opts[i] ~ "^[Wfm]")
187 idx = indices[opts[i]];
188 else
189 idx = -1;
192 printf(" { %c-%s%c,\n %s,\n %s, %u, %d,\n",
193 quote, opts[i], quote, hlp, back_chain[i], len, idx)
194 condition = opt_args("Condition", flags[i])
195 cl_flags = switch_flags(flags[i])
196 if (condition != "")
197 printf("#if %s\n" \
198 " %s,\n" \
199 "#else\n" \
200 " CL_DISABLED,\n" \
201 "#endif\n",
202 condition, cl_flags, cl_flags)
203 else
204 printf(" %s,\n", cl_flags)
205 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
206 var_set(flags[i]), comma)
209 print "};"