2012-07-06 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / opt-functions.awk
blob8e098c28c2ccebb6c1982254f12a718efbdcba94
1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
2 # Free Software Foundation, Inc.
3 # Contributed by Kelley Cook, June 2004.
4 # Original code from Neil Booth, May 2003.
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
8 # Free Software Foundation; either version 3, or (at your option) any
9 # later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; see the file COPYING3. If not see
18 # <http://www.gnu.org/licenses/>.
20 # Some common subroutines for use by opt[ch]-gen.awk.
22 # Define some helpful character classes, for portability.
23 BEGIN {
24 lower = "abcdefghijklmnopqrstuvwxyz"
25 upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 digit = "0123456789"
27 alnum = lower "" upper "" digit
30 # Return nonzero if FLAGS contains a flag matching REGEX.
31 function flag_set_p(regex, flags)
33 # Ignore the arguments of flags with arguments.
34 gsub ("\\([^)]+\\)", "", flags);
35 return (" " flags " ") ~ (" " regex " ")
38 # Return STRING if FLAGS contains a flag matching regexp REGEX,
39 # otherwise return the empty string.
40 function test_flag(regex, flags, string)
42 if (flag_set_p(regex, flags))
43 return string
44 return ""
47 # Return a field initializer, with trailing comma, for a field that is
48 # 1 if FLAGS contains a flag matching REGEX and 0 otherwise.
49 function flag_init(regex, flags)
51 if (flag_set_p(regex, flags))
52 return "1 /* " regex " */, "
53 else
54 return "0, "
57 # If FLAGS contains a "NAME(...argument...)" flag, return the value
58 # of the argument. Return the empty string otherwise.
59 function opt_args(name, flags)
61 flags = " " flags
62 if (flags !~ " " name "\\(")
63 return ""
64 sub(".* " name "\\(", "", flags)
65 if (flags ~ "^{")
67 sub ("^{", "", flags)
68 sub("}\\).*", "", flags)
70 else
71 sub("\\).*", "", flags)
73 return flags
76 # Return the Nth comma-separated element of S. Return the empty string
77 # if S does not contain N elements.
78 function nth_arg(n, s)
80 while (n-- > 0) {
81 if (s !~ ",")
82 return ""
83 sub("[^,]*, *", "", s)
85 sub(",.*", "", s)
86 return s
89 # Return a bitmask of CL_* values for option flags FLAGS.
90 function switch_flags (flags)
92 result = "0"
93 for (j = 0; j < n_langs; j++) {
94 regex = langs[j]
95 gsub ( "\\+", "\\+", regex )
96 result = result test_flag(regex, flags, " | " macros[j])
98 result = result \
99 test_flag("Common", flags, " | CL_COMMON") \
100 test_flag("Target", flags, " | CL_TARGET") \
101 test_flag("Driver", flags, " | CL_DRIVER") \
102 test_flag("Joined", flags, " | CL_JOINED") \
103 test_flag("JoinedOrMissing", flags, " | CL_JOINED") \
104 test_flag("Separate", flags, " | CL_SEPARATE") \
105 test_flag("Undocumented", flags, " | CL_UNDOCUMENTED") \
106 test_flag("Warning", flags, " | CL_WARNING") \
107 test_flag("Optimization", flags, " | CL_OPTIMIZATION")
108 sub( "^0 \\| ", "", result )
109 return result
112 # Return bit-field initializers for option flags FLAGS.
113 function switch_bit_fields (flags)
115 vn = var_name(flags);
116 if (host_wide_int[vn] == "yes")
117 hwi = "Host_Wide_Int"
118 else
119 hwi = ""
120 result = ""
121 sep_args = opt_args("Args", flags)
122 if (sep_args == "")
123 sep_args = 0
124 else
125 sep_args--
126 result = result sep_args ", "
128 result = result \
129 flag_init("SeparateAlias", flags) \
130 flag_init("NegativeAlias", flags) \
131 flag_init("NoDriverArg", flags) \
132 flag_init("RejectDriver", flags) \
133 flag_init("RejectNegative", flags) \
134 flag_init("JoinedOrMissing", flags) \
135 flag_init("UInteger", flags) \
136 flag_init("Host_Wide_Int", hwi) \
137 flag_init("ToLower", flags) \
138 flag_init("Report", flags)
140 sub(", $", "", result)
141 return result
144 # If FLAGS includes a Var flag, return the name of the variable it specifies.
145 # Return the empty string otherwise.
146 function var_name(flags)
148 return nth_arg(0, opt_args("Var", flags))
151 # Return the name of the variable if FLAGS has a HOST_WIDE_INT variable.
152 # Return the empty string otherwise.
153 function host_wide_int_var_name(flags)
155 split (flags, array, "[ \t]+")
156 if (array[1] == "HOST_WIDE_INT")
157 return array[2]
158 else
159 return ""
162 # Return true if the option described by FLAGS has a globally-visible state.
163 function global_state_p(flags)
165 return (var_name(flags) != "" \
166 || opt_args("Mask", flags) != "" \
167 || opt_args("InverseMask", flags) != "")
170 # Return true if the option described by FLAGS must have some state
171 # associated with it.
172 function needs_state_p(flags)
174 return (flag_set_p("Target", flags) \
175 && !flag_set_p("Alias.*", flags) \
176 && !flag_set_p("Ignore", flags))
179 # If FLAGS describes an option that needs state without a public
180 # variable name, return the name of that field, minus the initial
181 # "x_", otherwise return "". NAME is the name of the option.
182 function static_var(name, flags)
184 if (global_state_p(flags) || !needs_state_p(flags))
185 return ""
186 gsub ("[^" alnum "]", "_", name)
187 return "VAR_" name
190 # Return the type of variable that should be associated with the given flags.
191 function var_type(flags)
193 if (flag_set_p("Defer", flags))
194 return "void *"
195 else if (flag_set_p("Enum.*", flags)) {
196 en = opt_args("Enum", flags);
197 return enum_type[en] " "
199 else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags))
200 return "int "
201 else if (flag_set_p("UInteger", flags))
202 return "int "
203 else
204 return "const char *"
207 # Return the type of variable that should be associated with the given flags
208 # for use within a structure. Simple variables are changed to signed char
209 # type instead of int to save space.
210 function var_type_struct(flags)
212 if (flag_set_p("UInteger", flags))
213 return "int "
214 else if (flag_set_p("Enum.*", flags)) {
215 en = opt_args("Enum", flags);
216 return enum_type[en] " "
218 else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags)) {
219 if (flag_set_p(".*Mask.*", flags)) {
220 if (host_wide_int[var_name(flags)] == "yes")
221 return "HOST_WIDE_INT "
222 else
223 return "int "
225 else
226 return "signed char "
228 else
229 return "const char *"
232 # Given that an option has flags FLAGS, return an initializer for the
233 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
234 function var_set(flags)
236 if (flag_set_p("Defer", flags))
237 return "0, CLVC_DEFER, 0"
238 s = nth_arg(1, opt_args("Var", flags))
239 if (s != "")
240 return "0, CLVC_EQUAL, " s
241 s = opt_args("Mask", flags);
242 if (s != "") {
243 vn = var_name(flags);
244 if (vn)
245 return "0, CLVC_BIT_SET, OPTION_MASK_" s
246 else
247 return "0, CLVC_BIT_SET, MASK_" s
249 s = nth_arg(0, opt_args("InverseMask", flags));
250 if (s != "") {
251 vn = var_name(flags);
252 if (vn)
253 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
254 else
255 return "0, CLVC_BIT_CLEAR, MASK_" s
257 if (flag_set_p("Enum.*", flags)) {
258 en = opt_args("Enum", flags);
259 return enum_index[en] ", CLVC_ENUM, 0"
261 if (var_type(flags) == "const char *")
262 return "0, CLVC_STRING, 0"
263 return "0, CLVC_BOOLEAN, 0"
266 # Given that an option called NAME has flags FLAGS, return an initializer
267 # for the "flag_var" field of its cl_options[] entry.
268 function var_ref(name, flags)
270 name = var_name(flags) static_var(name, flags)
271 if (name != "")
272 return "offsetof (struct gcc_options, x_" name ")"
273 if (opt_args("Mask", flags) != "")
274 return "offsetof (struct gcc_options, x_target_flags)"
275 if (opt_args("InverseMask", flags) != "")
276 return "offsetof (struct gcc_options, x_target_flags)"
277 return "-1"
280 # Given the option called NAME return a sanitized version of its name.
281 function opt_sanitized_name(name)
283 gsub ("[^" alnum "]", "_", name)
284 return name
287 # Given the option called NAME return the appropriate enum for it.
288 function opt_enum(name)
290 return "OPT_" opt_sanitized_name(name)
293 # Given the language called NAME return a sanitized version of its name.
294 function lang_sanitized_name(name)
296 gsub( "[^" alnum "_]", "X", name )
297 return name