2011-06-29 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / gcc / opt-functions.awk
blob945eeb15418c0880d662e7344563f08eb37a1dd6
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 return (" " flags " ") ~ (" " regex " ")
36 # Return STRING if FLAGS contains a flag matching regexp REGEX,
37 # otherwise return the empty string.
38 function test_flag(regex, flags, string)
40 if (flag_set_p(regex, flags))
41 return string
42 return ""
45 # Return a field initializer, with trailing comma, for a field that is
46 # 1 if FLAGS contains a flag matching REGEX and 0 otherwise.
47 function flag_init(regex, flags)
49 if (flag_set_p(regex, flags))
50 return "1 /* " regex " */, "
51 else
52 return "0, "
55 # If FLAGS contains a "NAME(...argument...)" flag, return the value
56 # of the argument. Return the empty string otherwise.
57 function opt_args(name, flags)
59 flags = " " flags
60 if (flags !~ " " name "\\(")
61 return ""
62 sub(".* " name "\\(", "", flags)
63 if (flags ~ "^{")
65 sub ("^{", "", flags)
66 sub("}\\).*", "", flags)
68 else
69 sub("\\).*", "", flags)
71 return flags
74 # Return the Nth comma-separated element of S. Return the empty string
75 # if S does not contain N elements.
76 function nth_arg(n, s)
78 while (n-- > 0) {
79 if (s !~ ",")
80 return ""
81 sub("[^,]*, *", "", s)
83 sub(",.*", "", s)
84 return s
87 # Return a bitmask of CL_* values for option flags FLAGS.
88 function switch_flags (flags)
90 result = "0"
91 for (j = 0; j < n_langs; j++) {
92 regex = langs[j]
93 gsub ( "\\+", "\\+", regex )
94 result = result test_flag(regex, flags, " | " macros[j])
96 result = result \
97 test_flag("Common", flags, " | CL_COMMON") \
98 test_flag("Target", flags, " | CL_TARGET") \
99 test_flag("Driver", flags, " | CL_DRIVER") \
100 test_flag("Joined", flags, " | CL_JOINED") \
101 test_flag("JoinedOrMissing", flags, " | CL_JOINED") \
102 test_flag("Separate", flags, " | CL_SEPARATE") \
103 test_flag("Undocumented", flags, " | CL_UNDOCUMENTED") \
104 test_flag("Warning", flags, " | CL_WARNING") \
105 test_flag("Optimization", flags, " | CL_OPTIMIZATION")
106 sub( "^0 \\| ", "", result )
107 return result
110 # Return bit-field initializers for option flags FLAGS.
111 function switch_bit_fields (flags)
113 result = ""
114 sep_args = opt_args("Args", flags)
115 if (sep_args == "")
116 sep_args = 0
117 else
118 sep_args--
119 result = result sep_args ", "
121 result = result \
122 flag_init("SeparateAlias", flags) \
123 flag_init("NegativeAlias", flags) \
124 flag_init("NoDriverArg", flags) \
125 flag_init("RejectDriver", flags) \
126 flag_init("RejectNegative", flags) \
127 flag_init("JoinedOrMissing", flags) \
128 flag_init("UInteger", flags) \
129 flag_init("ToLower", flags) \
130 flag_init("Report", flags)
132 sub(", $", "", result)
133 return result
136 # If FLAGS includes a Var flag, return the name of the variable it specifies.
137 # Return the empty string otherwise.
138 function var_name(flags)
140 return nth_arg(0, opt_args("Var", flags))
143 # Return true if the option described by FLAGS has a globally-visible state.
144 function global_state_p(flags)
146 return (var_name(flags) != "" \
147 || opt_args("Mask", flags) != "" \
148 || opt_args("InverseMask", flags) != "")
151 # Return true if the option described by FLAGS must have some state
152 # associated with it.
153 function needs_state_p(flags)
155 return (flag_set_p("Target", flags) \
156 && !flag_set_p("Alias.*", flags) \
157 && !flag_set_p("Ignore", flags))
160 # If FLAGS describes an option that needs state without a public
161 # variable name, return the name of that field, minus the initial
162 # "x_", otherwise return "". NAME is the name of the option.
163 function static_var(name, flags)
165 if (global_state_p(flags) || !needs_state_p(flags))
166 return ""
167 gsub ("[^" alnum "]", "_", name)
168 return "VAR_" name
171 # Return the type of variable that should be associated with the given flags.
172 function var_type(flags)
174 if (flag_set_p("Defer", flags))
175 return "void *"
176 else if (flag_set_p("Enum.*", flags)) {
177 en = opt_args("Enum", flags);
178 return enum_type[en] " "
180 else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags))
181 return "int "
182 else if (flag_set_p("UInteger", flags))
183 return "int "
184 else
185 return "const char *"
188 # Return the type of variable that should be associated with the given flags
189 # for use within a structure. Simple variables are changed to signed char
190 # type instead of int to save space.
191 function var_type_struct(flags)
193 if (flag_set_p("UInteger", flags))
194 return "int "
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 if (flag_set_p(".*Mask.*", flags))
201 return "int "
202 else
203 return "signed char "
205 else
206 return "const char *"
209 # Given that an option has flags FLAGS, return an initializer for the
210 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
211 function var_set(flags)
213 if (flag_set_p("Defer", flags))
214 return "0, CLVC_DEFER, 0"
215 s = nth_arg(1, opt_args("Var", flags))
216 if (s != "")
217 return "0, CLVC_EQUAL, " s
218 s = opt_args("Mask", flags);
219 if (s != "") {
220 vn = var_name(flags);
221 if (vn)
222 return "0, CLVC_BIT_SET, OPTION_MASK_" s
223 else
224 return "0, CLVC_BIT_SET, MASK_" s
226 s = nth_arg(0, opt_args("InverseMask", flags));
227 if (s != "") {
228 vn = var_name(flags);
229 if (vn)
230 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
231 else
232 return "0, CLVC_BIT_CLEAR, MASK_" s
234 if (flag_set_p("Enum.*", flags)) {
235 en = opt_args("Enum", flags);
236 return enum_index[en] ", CLVC_ENUM, 0"
238 if (var_type(flags) == "const char *")
239 return "0, CLVC_STRING, 0"
240 return "0, CLVC_BOOLEAN, 0"
243 # Given that an option called NAME has flags FLAGS, return an initializer
244 # for the "flag_var" field of its cl_options[] entry.
245 function var_ref(name, flags)
247 name = var_name(flags) static_var(name, flags)
248 if (name != "")
249 return "offsetof (struct gcc_options, x_" name ")"
250 if (opt_args("Mask", flags) != "")
251 return "offsetof (struct gcc_options, x_target_flags)"
252 if (opt_args("InverseMask", flags) != "")
253 return "offsetof (struct gcc_options, x_target_flags)"
254 return "-1"
257 # Given the option called NAME return a sanitized version of its name.
258 function opt_sanitized_name(name)
260 gsub ("[^" alnum "]", "_", name)
261 return name
264 # Given the option called NAME return the appropriate enum for it.
265 function opt_enum(name)
267 return "OPT_" opt_sanitized_name(name)