re PR target/26885 (-m64 -m32 no longer creates 32-bit object)
[gcc.git] / gcc / optc-gen.awk
1 # Copyright (C) 2003,2004 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 2, 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; if not, write to the Free Software
17 # Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
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.
22 #
23 # This program uses functions from opt-functions.awk
24 #
25 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
26 # [-v header_name=header.h] < inputfile > options.c
27
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"
36 }
37
38 # Collect the text and flags of each option into an array
39 {
40 if ($1 == "Language") {
41 langs[n_langs] = $2
42 n_langs++;
43 }
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++;
51 }
52 }
53 }
54
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 "int target_flags;"
66 print ""
67
68 for (i = 0; i < n_opts; i++) {
69 name = var_name(flags[i]);
70 if (name == "")
71 continue;
72
73 if (flag_set_p("VarExists", flags[i])) {
74 # Need it for the gcc driver.
75 if (name in var_seen)
76 continue;
77 init = ""
78 }
79 else {
80 init = opt_args("Init", flags[i])
81 if (init != "")
82 init = " = " init;
83 else if (name in var_seen)
84 continue;
85 }
86
87 print "/* Set by -" opts[i] "."
88 print " " help[i] " */"
89 print var_type(flags[i]) name init ";"
90 print ""
91
92 var_seen[name] = 1;
93 }
94
95 print ""
96 print "/* Local state variables. */"
97 for (i = 0; i < n_opts; i++) {
98 name = static_var(opts[i], flags[i]);
99 if (name != "")
100 print "static " var_type(flags[i]) name ";"
101 }
102 print ""
103
104 print "const char * const lang_names[] =\n{"
105 for (i = 0; i < n_langs; i++) {
106 macros[i] = "CL_" langs[i]
107 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
108 s = substr(" ", length (macros[i]))
109 print " " quote langs[i] quote ","
110 }
111
112 print " 0\n};\n"
113 print "const unsigned int cl_options_count = N_OPTS;\n"
114
115 print "const struct cl_option cl_options[] =\n{"
116
117 j = 0
118 for (i = 0; i < n_opts; i++) {
119 back_chain[i] = "N_OPTS";
120 indices[opts[i]] = j;
121 # Combine the flags of identical switches. Switches
122 # appear many times if they are handled by many front
123 # ends, for example.
124 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
125 flags[i + 1] = flags[i] " " flags[i + 1];
126 i++;
127 back_chain[i] = "N_OPTS";
128 indices[opts[i]] = j;
129 }
130 j++;
131 }
132
133 for (i = 0; i < n_opts; i++) {
134 # Combine the flags of identical switches. Switches
135 # appear many times if they are handled by many front
136 # ends, for example.
137 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
138 flags[i + 1] = flags[i] " " flags[i + 1];
139 i++;
140 }
141
142 len = length (opts[i]);
143 enum = "OPT_" opts[i]
144 if (opts[i] == "finline-limit=")
145 enum = enum "eq"
146 gsub ("[^A-Za-z0-9]", "_", enum)
147
148 # If this switch takes joined arguments, back-chain all
149 # subsequent switches to it for which it is a prefix. If
150 # a later switch S is a longer prefix of a switch T, T
151 # will be back-chained to S in a later iteration of this
152 # for() loop, which is what we want.
153 if (flag_set_p("Joined.*", flags[i])) {
154 for (j = i + 1; j < n_opts; j++) {
155 if (substr (opts[j], 1, len) != opts[i])
156 break;
157 back_chain[j] = enum;
158 }
159 }
160
161 s = substr(" ", length (opts[i]))
162 if (i + 1 == n_opts)
163 comma = ""
164
165 if (help[i] == "")
166 hlp = "0"
167 else
168 hlp = quote help[i] quote;
169
170 neg = opt_args("Negative", flags[i]);
171 if (neg != "")
172 idx = indices[neg]
173 else {
174 if (flag_set_p("RejectNegative", flags[i]))
175 idx = -1;
176 else {
177 if (opts[i] ~ "^[Wfm]")
178 idx = indices[opts[i]];
179 else
180 idx = -1;
181 }
182 }
183 printf(" { %c-%s%c,\n %s,\n %s, %u, %d,\n",
184 quote, opts[i], quote, hlp, back_chain[i], len, idx)
185 condition = opt_args("Condition", flags[i])
186 cl_flags = switch_flags(flags[i])
187 if (condition != "")
188 printf("#if %s\n" \
189 " %s,\n" \
190 "#else\n" \
191 " CL_DISABLED,\n" \
192 "#endif\n",
193 condition, cl_flags, cl_flags)
194 else
195 printf(" %s,\n", cl_flags)
196 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
197 var_set(flags[i]), comma)
198 }
199
200 print "};"
201 }