aboutsummaryrefslogtreecommitdiff
path: root/gcc/opt-functions.awk
blob: 7cf50256558abccefa9ba72eb79f54f54dad1f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#  Copyright (C) 2003-2016 Free Software Foundation, Inc.
#  Contributed by Kelley Cook, June 2004.
#  Original code from Neil Booth, May 2003.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any
# later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

# Some common subroutines for use by opt[ch]-gen.awk.

# Define some helpful character classes, for portability.
BEGIN {
	lower = "abcdefghijklmnopqrstuvwxyz"
	upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	digit = "0123456789"
	alnum = lower "" upper "" digit
}

# Return nonzero if FLAGS contains a flag matching REGEX.
function flag_set_p(regex, flags)
{
    # Ignore the arguments of flags with arguments.
    gsub ("\\([^)]+\\)", "", flags);
    return (" " flags " ") ~ (" " regex " ")
}

# Return STRING if FLAGS contains a flag matching regexp REGEX,
# otherwise return the empty string.
function test_flag(regex, flags, string)
{
	if (flag_set_p(regex, flags))
		return string
	return ""
}

# Return a field initializer, with trailing comma, for a field that is
# 1 if FLAGS contains a flag matching REGEX and 0 otherwise.
function flag_init(regex, flags)
{
	if (flag_set_p(regex, flags))
		return "1 /* " regex " */, "
	else
		return "0, "
}

# If FLAGS contains a "NAME(...argument...)" flag, return the value
# of the argument.  Return the empty string otherwise.
function opt_args(name, flags)
{
	flags = " " flags
	if (flags !~ " " name "\\(")
		return ""
	sub(".* " name "\\(", "", flags)
	if (flags ~ "^{")
	{
		sub ("^{", "", flags)
		sub("}\\).*", "", flags)
	}
	else
		sub("\\).*", "", flags)

	return flags
}

# Return the Nth comma-separated element of S.  Return the empty string
# if S does not contain N elements.
function nth_arg(n, s)
{
	while (n-- > 0) {
		if (s !~ ",")
			return ""
		sub("[^,]*, *", "", s)
	}
	sub(",.*", "", s)
	return s
}

# Return a bitmask of CL_* values for option flags FLAGS.
function switch_flags (flags)
{
	result = "0"
	for (j = 0; j < n_langs; j++) {
		regex = langs[j]
		gsub ( "\\+", "\\+", regex )
		result = result test_flag(regex, flags, " | " macros[j])
	}
	result = result \
	  test_flag("Common", flags, " | CL_COMMON") \
	  test_flag("Target", flags, " | CL_TARGET") \
	  test_flag("PchIgnore", flags, " | CL_PCH_IGNORE") \
	  test_flag("Driver", flags, " | CL_DRIVER") \
	  test_flag("Joined", flags, " | CL_JOINED") \
	  test_flag("JoinedOrMissing", flags, " | CL_JOINED") \
	  test_flag("Separate", flags, " | CL_SEPARATE") \
	  test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \
	  test_flag("NoDWARFRecord", flags,  " | CL_NO_DWARF_RECORD") \
	  test_flag("Warning", flags,  " | CL_WARNING") \
	  test_flag("Optimization", flags,  " | CL_OPTIMIZATION")
	sub( "^0 \\| ", "", result )
	return result
}

# Return bit-field initializers for option flags FLAGS.
function switch_bit_fields (flags)
{
	vn = var_name(flags);
	if (host_wide_int[vn] == "yes")
		hwi = "Host_Wide_Int"
	else
		hwi = ""
	result = ""
	sep_args = opt_args("Args", flags)
	if (sep_args == "")
		sep_args = 0
	else
		sep_args--
	result = result sep_args ", "

	result = result \
	  flag_init("SeparateAlias", flags) \
	  flag_init("NegativeAlias", flags) \
	  flag_init("NoDriverArg", flags) \
	  flag_init("RejectDriver", flags) \
	  flag_init("RejectNegative", flags) \
	  flag_init("JoinedOrMissing", flags) \
	  flag_init("UInteger", flags) \
	  flag_init("Host_Wide_Int", hwi) \
	  flag_init("ToLower", flags) \
	  flag_init("Report", flags)

	sub(", $", "", result)
	return result
}

# If FLAGS includes a Var flag, return the name of the variable it specifies.
# Return the empty string otherwise.
function var_name(flags)
{
	return nth_arg(0, opt_args("Var", flags))
}

# Return the name of the variable if FLAGS has a HOST_WIDE_INT variable. 
# Return the empty string otherwise.
function host_wide_int_var_name(flags)
{
	split (flags, array, "[ \t]+")
	if (array[1] == "HOST_WIDE_INT")
		return array[2]
	else
		return ""
}

# Return true if the option described by FLAGS has a globally-visible state.
function global_state_p(flags)
{
	return (var_name(flags) != "" \
		|| opt_args("Mask", flags) != "" \
		|| opt_args("InverseMask", flags) != "")
}

# Return true if the option described by FLAGS must have some state
# associated with it.
function needs_state_p(flags)
{
	return (flag_set_p("Target", flags) \
		&& !flag_set_p("Alias.*", flags) \
		&& !flag_set_p("Ignore", flags))
}

# If FLAGS describes an option that needs state without a public
# variable name, return the name of that field, minus the initial
# "x_", otherwise return "".  NAME is the name of the option.
function static_var(name, flags)
{
	if (global_state_p(flags) || !needs_state_p(flags))
		return ""
	gsub ("[^" alnum "]", "_", name)
	return "VAR_" name
}

# Return the type of variable that should be associated with the given flags.
function var_type(flags)
{
	if (flag_set_p("Defer", flags))
		return "void *"
	else if (flag_set_p("Enum.*", flags)) {
		en = opt_args("Enum", flags);
		return enum_type[en] " "
	}
	else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags))
		return "int "
	else if (flag_set_p("UInteger", flags))
		return "int "
	else
		return "const char *"
}

# Return the type of variable that should be associated with the given flags
# for use within a structure.  Simple variables are changed to signed char
# type instead of int to save space.
function var_type_struct(flags)
{
	if (flag_set_p("UInteger", flags))
		return "int "
	else if (flag_set_p("Enum.*", flags)) {
		en = opt_args("Enum", flags);
		return enum_type[en] " "
	}
	else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags)) {
		if (flag_set_p(".*Mask.*", flags)) {
			if (host_wide_int[var_name(flags)] == "yes")
				return "HOST_WIDE_INT "
			else
				return "int "
		}
		else
			return "signed char "
	}
	else
		return "const char *"
}

# Given that an option has flags FLAGS, return an initializer for the
# "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
function var_set(flags)
{
	if (flag_set_p("Defer", flags))
		return "0, CLVC_DEFER, 0"
	s = nth_arg(1, opt_args("Var", flags))
	if (s != "")
		return "0, CLVC_EQUAL, " s
	s = opt_args("Mask", flags);
	if (s != "") {
		vn = var_name(flags);
		if (vn)
			return "0, CLVC_BIT_SET, OPTION_MASK_" s
		else
			return "0, CLVC_BIT_SET, MASK_" s
	}
	s = nth_arg(0, opt_args("InverseMask", flags));
	if (s != "") {
		vn = var_name(flags);
		if (vn)
			return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
		else
			return "0, CLVC_BIT_CLEAR, MASK_" s
	}
	if (flag_set_p("Enum.*", flags)) {
		en = opt_args("Enum", flags);
		return enum_index[en] ", CLVC_ENUM, 0"
	}
	if (var_type(flags) == "const char *")
		return "0, CLVC_STRING, 0"
	return "0, CLVC_BOOLEAN, 0"
}

# Given that an option called NAME has flags FLAGS, return an initializer
# for the "flag_var" field of its cl_options[] entry.
function var_ref(name, flags)
{
	name = var_name(flags) static_var(name, flags)
	if (name != "")
		return "offsetof (struct gcc_options, x_" name ")"
	if (opt_args("Mask", flags) != "")
		return "offsetof (struct gcc_options, x_target_flags)"
	if (opt_args("InverseMask", flags) != "")
		return "offsetof (struct gcc_options, x_target_flags)"
	return "-1"
}

# Given the option called NAME return a sanitized version of its name.
function opt_sanitized_name(name)
{
	gsub ("[^" alnum "]", "_", name)
	return name
}

# Given the option called NAME return the appropriate enum for it.
function opt_enum(name)
{
	return "OPT_" opt_sanitized_name(name)
}

# Given the language called NAME return a sanitized version of its name.
function lang_sanitized_name(name)
{
    gsub( "[^" alnum "_]", "X", name )
    return name
}

# Search for a valid var_name among all OPTS equal to option NAME.
# If not found, return "".
function search_var_name(name, opt_numbers, opts, flags, n_opts)
{
    opt_var_name = var_name(flags[opt_numbers[name]]);
    if (opt_var_name != "") {
        return opt_var_name;
    }
    for (k = 0; k < n_opts; k++) {
        if (opts[k] == name && var_name(flags[k]) != "") {
            return var_name(flags[k]);
        }
    }
    return ""
}

# Handle LangEnabledBy(ENABLED_BY_LANGS, ENABLEDBY_NAME, ENABLEDBY_POSARG,
# ENABLEDBY_NEGARG). This function does not return anything.
function lang_enabled_by(enabledby_langs, enabledby_name, enabledby_posarg, enabledby_negarg)
{
    n_enabledby_arg_langs = split(enabledby_langs, enabledby_arg_langs, " ");
    if (enabledby_posarg != "" && enabledby_negarg != "") {
        with_args = "," enabledby_posarg "," enabledby_negarg
    } else if (enabledby_posarg == "" && enabledby_negarg == "") {
        with_args = ""
    } else {
        print "#error " opts[i] " LangEnabledBy("enabledby_langs","enabledby_name", " \
            enabledby_posarg", " enabledby_negargs                  \
            ") with three arguments, it should have either 2 or 4"
    }

    n_enabledby_array = split(enabledby_name, enabledby_array, " \\|\\| ");
    for (k = 1; k <= n_enabledby_array; k++) {
        enabledby_index = opt_numbers[enabledby_array[k]];
        if (enabledby_index == "") {
             print "#error " opts[i] " LangEnabledBy("enabledby_langs","enabledby_name", " \
                 enabledby_posarg", " enabledby_negargs"), unknown option '" enabledby_name "'"
        } else {
            for (j = 1; j <= n_enabledby_arg_langs; j++) {
                 lang_name = lang_sanitized_name(enabledby_arg_langs[j]);
                 lang_index = lang_numbers[enabledby_arg_langs[j]];
                 if (enables[lang_name,enabledby_array[k]] == "") {
                     enabledby[lang_name,n_enabledby_lang[lang_index]] = enabledby_array[k];
                     n_enabledby_lang[lang_index]++;
                 }
                 enables[lang_name,enabledby_array[k]] \
                     = enables[lang_name,enabledby_array[k]] opts[i] with_args ";";
            }
        }
    }
}