1 # Copyright (C) 2003-2018 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
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 # Some common subroutines for use by opt[ch]-gen.awk.
21 # Define some helpful character classes, for portability.
23 lower =
"abcdefghijklmnopqrstuvwxyz"
24 upper =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 alnum = lower
"" upper
"" digit
29 # Return nonzero if FLAGS contains a flag matching REGEX.
30 function flag_set_p
(regex
, flags
)
32 # Ignore the arguments of flags with arguments.
33 gsub ("\\([^)]+\\)", "", flags
);
34 return (" " flags
" ") ~
(" " regex
" ")
37 # Return STRING if FLAGS contains a flag matching regexp REGEX,
38 # otherwise return the empty string.
39 function test_flag
(regex
, flags
, string
)
41 if (flag_set_p
(regex
, flags
))
46 # Return a field initializer, with trailing comma, for a field that is
47 # 1 if FLAGS contains a flag matching REGEX and 0 otherwise.
48 function flag_init
(regex
, flags
)
50 if (flag_set_p
(regex
, flags
))
51 return "1 /* " regex
" */, "
56 # If FLAGS contains a "NAME(...argument...)" flag, return the value
57 # of the argument. Return the empty string otherwise.
58 function opt_args
(name
, flags
)
61 if (flags !~
" " name
"\\(")
63 sub(".* " name
"\\(", "", flags
)
66 sub ("^[{]", "", flags
)
67 sub ("}\\).*", "", flags
)
70 sub("\\).*", "", flags
)
75 # Return the Nth comma-separated element of S. Return the empty string
76 # if S does not contain N elements.
77 function nth_arg
(n
, s
)
82 sub("[^,]*, *", "", s
)
88 # Return a bitmask of CL_* values for option flags FLAGS.
89 function switch_flags
(flags
)
92 for (j =
0; j
< n_langs
; j
++) {
94 gsub ( "\\+", "\\+", regex
)
95 result = result test_flag
(regex
, flags
, " | " macros
[j
])
98 test_flag
("Common", flags
, " | CL_COMMON") \
99 test_flag
("Target", flags
, " | CL_TARGET") \
100 test_flag
("PchIgnore", flags
, " | CL_PCH_IGNORE") \
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
("NoDWARFRecord", flags
, " | CL_NO_DWARF_RECORD") \
107 test_flag
("Warning", flags
, " | CL_WARNING") \
108 test_flag
("(Optimization|PerFunction)", flags
, " | CL_OPTIMIZATION")
109 sub( "^0 \\| ", "", result
)
113 # Return bit-field initializers for option flags FLAGS.
114 function switch_bit_fields
(flags
)
116 vn = var_name
(flags
);
117 if (host_wide_int
[vn
] ==
"yes")
118 hwi =
"Host_Wide_Int"
122 sep_args = opt_args
("Args", flags
)
127 result = result sep_args
", "
130 flag_init
("SeparateAlias", flags
) \
131 flag_init
("NegativeAlias", flags
) \
132 flag_init
("NoDriverArg", flags
) \
133 flag_init
("RejectDriver", flags
) \
134 flag_init
("RejectNegative", flags
) \
135 flag_init
("JoinedOrMissing", flags
) \
136 flag_init
("UInteger", flags
) \
137 flag_init
("Host_Wide_Int", hwi
) \
138 flag_init
("ToLower", flags
) \
139 flag_init
("Report", flags
) \
140 flag_init
("Deprecated", flags
)
142 sub(", $", "", result
)
146 # If FLAGS includes a Var flag, return the name of the variable it specifies.
147 # Return the empty string otherwise.
148 function var_name
(flags
)
150 return nth_arg
(0, opt_args
("Var", flags
))
153 # Return the name of the variable if FLAGS has a HOST_WIDE_INT variable.
154 # Return the empty string otherwise.
155 function host_wide_int_var_name
(flags
)
157 split (flags
, array
, "[ \t]+")
158 if (array
[1] ==
"HOST_WIDE_INT")
164 # Return true if the option described by FLAGS has a globally-visible state.
165 function global_state_p
(flags
)
167 return (var_name
(flags
) != "" \
168 || opt_args
("Mask", flags
) != "" \
169 || opt_args
("InverseMask", flags
) != "")
172 # Return true if the option described by FLAGS must have some state
173 # associated with it.
174 function needs_state_p
(flags
)
176 return (flag_set_p
("Target", flags
) \
177 && !flag_set_p
("Alias.*", flags
) \
178 && !flag_set_p
("Ignore", flags
))
181 # If FLAGS describes an option that needs state without a public
182 # variable name, return the name of that field, minus the initial
183 # "x_", otherwise return "". NAME is the name of the option.
184 function static_var
(name
, flags
)
186 if (global_state_p
(flags
) || !needs_state_p
(flags
))
188 gsub ("[^" alnum
"]", "_", name
)
192 # Return the type of variable that should be associated with the given flags.
193 function var_type
(flags
)
195 if (flag_set_p
("Defer", flags
))
197 else if (flag_set_p
("Enum.*", flags
)) {
198 en = opt_args
("Enum", flags
);
199 return enum_type
[en
] " "
201 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
))
203 else if (flag_set_p
("UInteger", flags
))
206 return "const char *"
209 # Return the type of variable that should be associated with the given flags
210 # for use within a structure. Simple variables are changed to signed char
211 # type instead of int to save space.
212 function var_type_struct
(flags
)
214 if (flag_set_p
("UInteger", flags
))
216 else if (flag_set_p
("Enum.*", flags
)) {
217 en = opt_args
("Enum", flags
);
218 return enum_type
[en
] " "
220 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
)) {
221 if (flag_set_p
(".*Mask.*", flags
)) {
222 if (host_wide_int
[var_name
(flags
)] ==
"yes")
223 return "HOST_WIDE_INT "
228 return "signed char "
231 return "const char *"
234 # Given that an option has flags FLAGS, return an initializer for the
235 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
236 function var_set
(flags
)
238 if (flag_set_p
("Defer", flags
))
239 return "0, CLVC_DEFER, 0"
240 s = nth_arg
(1, opt_args
("Var", flags
))
242 return "0, CLVC_EQUAL, " s
243 s = opt_args
("Mask", flags
);
245 vn = var_name
(flags
);
247 return "0, CLVC_BIT_SET, OPTION_MASK_" s
249 return "0, CLVC_BIT_SET, MASK_" s
251 s = nth_arg
(0, opt_args
("InverseMask", flags
));
253 vn = var_name
(flags
);
255 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
257 return "0, CLVC_BIT_CLEAR, MASK_" s
259 if (flag_set_p
("Enum.*", flags
)) {
260 en = opt_args
("Enum", flags
);
261 return enum_index
[en
] ", CLVC_ENUM, 0"
263 if (var_type
(flags
) ==
"const char *")
264 return "0, CLVC_STRING, 0"
265 return "0, CLVC_BOOLEAN, 0"
268 # Given that an option called NAME has flags FLAGS, return an initializer
269 # for the "flag_var" field of its cl_options[] entry.
270 function var_ref
(name
, flags
)
272 name = var_name
(flags
) static_var
(name
, flags
)
274 return "offsetof (struct gcc_options, x_" name
")"
275 if (opt_args
("Mask", flags
) != "")
276 return "offsetof (struct gcc_options, x_target_flags)"
277 if (opt_args
("InverseMask", flags
) != "")
278 return "offsetof (struct gcc_options, x_target_flags)"
279 return "(unsigned short) -1"
282 # Given the option called NAME return a sanitized version of its name.
283 function opt_sanitized_name
(name
)
285 gsub ("[^" alnum
"]", "_", name
)
289 # Given the option called NAME return the appropriate enum for it.
290 function opt_enum
(name
)
292 return "OPT_" opt_sanitized_name
(name
)
295 # Given the language called NAME return a sanitized version of its name.
296 function lang_sanitized_name
(name
)
298 gsub( "[^" alnum
"_]", "X", name
)
302 # Search for a valid var_name among all OPTS equal to option NAME.
303 # If not found, return "".
304 function search_var_name
(name
, opt_numbers
, opts
, flags
, n_opts
)
306 opt_var_name = var_name
(flags
[opt_numbers
[name
]]);
307 if (opt_var_name
!= "") {
310 for (k =
0; k
< n_opts
; k
++) {
311 if (opts
[k
] == name
&& var_name
(flags
[k
]) != "") {
312 return var_name
(flags
[k
]);
318 function integer_range_info
(range_option
, init
, option
)
320 if (range_option
!= "") {
321 start = nth_arg
(0, range_option
);
322 end = nth_arg
(1, range_option
);
323 if (init
!= "" && init
!= "-1" && (init
< start
|| init
> end))
324 print "#error initial value " init
" of '" option
"' must be in range [" start
"," end "]"
325 return start
", " end
331 # Handle LangEnabledBy(ENABLED_BY_LANGS, ENABLEDBY_NAME, ENABLEDBY_POSARG,
332 # ENABLEDBY_NEGARG). This function does not return anything.
333 function lang_enabled_by
(enabledby_langs
, enabledby_name
, enabledby_posarg
, enabledby_negarg
)
335 n_enabledby_arg_langs =
split(enabledby_langs
, enabledby_arg_langs
, " ");
336 if (enabledby_posarg
!= "" && enabledby_negarg
!= "") {
337 with_args =
"," enabledby_posarg
"," enabledby_negarg
338 } else if (enabledby_posarg ==
"" && enabledby_negarg ==
"") {
341 print "#error " opts
[i
] " LangEnabledBy("enabledby_langs
","enabledby_name
", " \
342 enabledby_posarg
", " enabledby_negargs \
343 ") with three arguments, it should have either 2 or 4"
346 n_enabledby_array =
split(enabledby_name
, enabledby_array
, " \\|\\| ");
347 for (k =
1; k
<= n_enabledby_array
; k
++) {
348 enabledby_index = opt_numbers
[enabledby_array
[k
]];
349 if (enabledby_index ==
"") {
350 print "#error " opts
[i
] " LangEnabledBy("enabledby_langs
","enabledby_name
", " \
351 enabledby_posarg
", " enabledby_negargs
"), unknown option '" enabledby_name
"'"
353 for (j =
1; j
<= n_enabledby_arg_langs
; j
++) {
354 lang_name = lang_sanitized_name
(enabledby_arg_langs
[j
]);
355 lang_index = lang_numbers
[enabledby_arg_langs
[j
]];
356 if (enables
[lang_name
,enabledby_array
[k
]] ==
"") {
357 enabledby
[lang_name
,n_enabledby_lang
[lang_index
]] = enabledby_array
[k
];
358 n_enabledby_lang
[lang_index
]++;
360 enables
[lang_name
,enabledby_array
[k
]] \
361 = enables
[lang_name
,enabledby_array
[k
]] opts
[i
] with_args
";";