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
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.
24 lower =
"abcdefghijklmnopqrstuvwxyz"
25 upper =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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
))
45 # If FLAGS contains a "NAME(...argument...)" flag, return the value
46 # of the argument. Return the empty string otherwise.
47 function opt_args
(name
, flags
)
50 if (flags !~
" " name
"\\(")
52 sub(".* " name
"\\(", "", flags
)
56 sub("}\\).*", "", flags
)
59 sub("\\).*", "", flags
)
64 # Return the Nth comma-separated element of S. Return the empty string
65 # if S does not contain N elements.
66 function nth_arg
(n
, s
)
71 sub("[^,]*, *", "", s
)
77 # Return a bitmask of CL_* values for option flags FLAGS.
78 function switch_flags
(flags
)
81 for (j =
0; j
< n_langs
; j
++) {
83 gsub ( "\\+", "\\+", regex
)
84 result = result test_flag
(regex
, flags
, " | " macros
[j
])
87 test_flag
("Common", flags
, " | CL_COMMON") \
88 test_flag
("Target", flags
, " | CL_TARGET") \
89 test_flag
("Driver", flags
, " | CL_DRIVER") \
90 test_flag
("RejectDriver", flags
, " | CL_REJECT_DRIVER") \
91 test_flag
("NoDriverArg", flags
, " | CL_NO_DRIVER_ARG") \
92 test_flag
("SeparateAlias", flags
, " | CL_SEPARATE_ALIAS") \
93 test_flag
("NegativeAlias", flags
, " | CL_NEGATIVE_ALIAS") \
94 test_flag
("Save", flags
, " | CL_SAVE") \
95 test_flag
("Joined", flags
, " | CL_JOINED") \
96 test_flag
("JoinedOrMissing", flags
, " | CL_JOINED | CL_MISSING_OK") \
97 test_flag
("Separate", flags
, " | CL_SEPARATE") \
98 test_flag
("RejectNegative", flags
, " | CL_REJECT_NEGATIVE") \
99 test_flag
("UInteger", flags
, " | CL_UINTEGER") \
100 test_flag
("Undocumented", flags
, " | CL_UNDOCUMENTED") \
101 test_flag
("Warning", flags
, " | CL_WARNING") \
102 test_flag
("Optimization", flags
, " | CL_OPTIMIZATION") \
103 test_flag
("Report", flags
, " | CL_REPORT")
104 sep_args = opt_args
("Args", flags
)
105 if (sep_args
!= "") {
107 result = result
" | (" sep_args \
108 " << CL_SEPARATE_NARGS_SHIFT)"
110 sub( "^0 \\| ", "", result
)
114 # If FLAGS includes a Var flag, return the name of the variable it specifies.
115 # Return the empty string otherwise.
116 function var_name
(flags
)
118 return nth_arg
(0, opt_args
("Var", flags
))
121 # Return true if the option described by FLAGS has a globally-visible state.
122 function global_state_p
(flags
)
124 return (var_name
(flags
) != "" \
125 || opt_args
("Mask", flags
) != "" \
126 || opt_args
("InverseMask", flags
) != "")
129 # Return true if the option described by FLAGS must have some state
130 # associated with it.
131 function needs_state_p
(flags
)
133 return (flag_set_p
("Target", flags
) \
134 && !flag_set_p
("Alias.*", flags
) \
135 && !flag_set_p
("Ignore", flags
))
138 # If FLAGS describes an option that needs state without a public
139 # variable name, return the name of that field, minus the initial
140 # "x_", otherwise return "". NAME is the name of the option.
141 function static_var
(name
, flags
)
143 if (global_state_p
(flags
) || !needs_state_p
(flags
))
145 gsub ("[^" alnum
"]", "_", name
)
149 # Return the type of variable that should be associated with the given flags.
150 function var_type
(flags
)
152 if (flag_set_p
("Defer", flags
))
154 else if (flag_set_p
("Enum.*", flags
)) {
155 en = opt_args
("Enum", flags
);
156 return enum_type
[en
] " "
158 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
))
160 else if (flag_set_p
("UInteger", flags
))
163 return "const char *"
166 # Return the type of variable that should be associated with the given flags
167 # for use within a structure. Simple variables are changed to signed char
168 # type instead of int to save space.
169 function var_type_struct
(flags
)
171 if (flag_set_p
("UInteger", flags
))
173 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
)) {
174 if (flag_set_p
(".*Mask.*", flags
))
177 return "signed char "
180 return "const char *"
183 # Given that an option has flags FLAGS, return an initializer for the
184 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
185 function var_set
(flags
)
187 if (flag_set_p
("Defer", flags
))
188 return "0, CLVC_DEFER, 0"
189 s = nth_arg
(1, opt_args
("Var", flags
))
191 return "0, CLVC_EQUAL, " s
192 s = opt_args
("Mask", flags
);
194 vn = var_name
(flags
);
196 return "0, CLVC_BIT_SET, OPTION_MASK_" s
198 return "0, CLVC_BIT_SET, MASK_" s
200 s = nth_arg
(0, opt_args
("InverseMask", flags
));
202 vn = var_name
(flags
);
204 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
206 return "0, CLVC_BIT_CLEAR, MASK_" s
208 if (flag_set_p
("Enum.*", flags
)) {
209 en = opt_args
("Enum", flags
);
210 return enum_index
[en
] ", CLVC_ENUM, 0"
212 if (var_type
(flags
) ==
"const char *")
213 return "0, CLVC_STRING, 0"
214 return "0, CLVC_BOOLEAN, 0"
217 # Given that an option called NAME has flags FLAGS, return an initializer
218 # for the "flag_var" field of its cl_options[] entry.
219 function var_ref
(name
, flags
)
221 name = var_name
(flags
) static_var
(name
, flags
)
223 return "offsetof (struct gcc_options, x_" name
")"
224 if (opt_args
("Mask", flags
) != "")
225 return "offsetof (struct gcc_options, x_target_flags)"
226 if (opt_args
("InverseMask", flags
) != "")
227 return "offsetof (struct gcc_options, x_target_flags)"
231 # Given the option called NAME return a sanitized version of its name.
232 function opt_sanitized_name
(name
)
234 gsub ("[^" alnum
"]", "_", name
)
238 # Given the option called NAME return the appropriate enum for it.
239 function opt_enum
(name
)
241 return "OPT_" opt_sanitized_name
(name
)