1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
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 # Return nonzero if FLAGS contains a flag matching REGEX.
23 function flag_set_p
(regex
, flags
)
25 return (" " flags
" ") ~
(" " regex
" ")
28 # Return STRING if FLAGS contains a flag matching regexp REGEX,
29 # otherwise return the empty string.
30 function test_flag
(regex
, flags
, string
)
32 if (flag_set_p
(regex
, flags
))
37 # If FLAGS contains a "NAME(...argument...)" flag, return the value
38 # of the argument. Return the empty string otherwise.
39 function opt_args
(name
, flags
)
42 if (flags !~
" " name
"\\(")
44 sub(".* " name
"\\(", "", flags
)
48 sub("}\\).*", "", flags
)
51 sub("\\).*", "", flags
)
56 # Return the Nth comma-separated element of S. Return the empty string
57 # if S does not contain N elements.
58 function nth_arg
(n
, s
)
63 sub("[^,]*, *", "", s
)
69 # Return a bitmask of CL_* values for option flags FLAGS.
70 function switch_flags
(flags
)
73 for (j =
0; j
< n_langs
; j
++) {
75 gsub ( "\\+", "\\+", regex
)
76 result = result test_flag
(regex
, flags
, " | " macros
[j
])
79 test_flag
("Common", flags
, " | CL_COMMON") \
80 test_flag
("Target", flags
, " | CL_TARGET") \
81 test_flag
("Driver", flags
, " | CL_DRIVER") \
82 test_flag
("RejectDriver", flags
, " | CL_REJECT_DRIVER") \
83 test_flag
("NoDriverArg", flags
, " | CL_NO_DRIVER_ARG") \
84 test_flag
("SeparateAlias", flags
, " | CL_SEPARATE_ALIAS") \
85 test_flag
("Save", flags
, " | CL_SAVE") \
86 test_flag
("Joined", flags
, " | CL_JOINED") \
87 test_flag
("JoinedOrMissing", flags
, " | CL_JOINED | CL_MISSING_OK") \
88 test_flag
("Separate", flags
, " | CL_SEPARATE") \
89 test_flag
("RejectNegative", flags
, " | CL_REJECT_NEGATIVE") \
90 test_flag
("UInteger", flags
, " | CL_UINTEGER") \
91 test_flag
("Undocumented", flags
, " | CL_UNDOCUMENTED") \
92 test_flag
("Warning", flags
, " | CL_WARNING") \
93 test_flag
("Optimization", flags
, " | CL_OPTIMIZATION") \
94 test_flag
("Report", flags
, " | CL_REPORT")
95 sub( "^0 \\| ", "", result
)
99 # If FLAGS includes a Var flag, return the name of the variable it specifies.
100 # Return the empty string otherwise.
101 function var_name
(flags
)
103 return nth_arg
(0, opt_args
("Var", flags
))
106 # Return true if the option described by FLAGS has a globally-visible state.
107 function global_state_p
(flags
)
109 return (var_name
(flags
) != "" \
110 || opt_args
("Mask", flags
) != "" \
111 || opt_args
("InverseMask", flags
) != "")
114 # Return true if the option described by FLAGS must have some state
115 # associated with it.
116 function needs_state_p
(flags
)
118 return (flag_set_p
("Target", flags
) \
119 && !flag_set_p
("Alias.*", flags
) \
120 && !flag_set_p
("Ignore", flags
))
123 # If FLAGS describes an option that needs a static state variable,
124 # return the name of that variable, otherwise return "". NAME is
125 # the name of the option.
126 function static_var
(name
, flags
)
128 if (global_state_p
(flags
) || !needs_state_p
(flags
))
130 gsub ("[^A-Za-z0-9]", "_", name
)
134 # Return the type of variable that should be associated with the given flags.
135 function var_type
(flags
)
137 if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
))
139 else if (flag_set_p
("UInteger", flags
))
142 return "const char *"
145 # Return the type of variable that should be associated with the given flags
146 # for use within a structure. Simple variables are changed to signed char
147 # type instead of int to save space.
148 function var_type_struct
(flags
)
150 if (flag_set_p
("UInteger", flags
))
152 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
)) {
153 if (flag_set_p
(".*Mask.*", flags
))
156 return "signed char "
159 return "const char *"
162 # Given that an option has flags FLAGS, return an initializer for the
163 # "var_cond" and "var_value" fields of its cl_options[] entry.
164 function var_set
(flags
)
166 s = nth_arg
(1, opt_args
("Var", flags
))
168 return "CLVC_EQUAL, " s
169 s = opt_args
("Mask", flags
);
171 vn = var_name
(flags
);
173 return "CLVC_BIT_SET, OPTION_MASK_" s
175 return "CLVC_BIT_SET, MASK_" s
177 s = nth_arg
(0, opt_args
("InverseMask", flags
));
179 vn = var_name
(flags
);
181 return "CLVC_BIT_CLEAR, OPTION_MASK_" s
183 return "CLVC_BIT_CLEAR, MASK_" s
185 if (var_type
(flags
) ==
"const char *")
186 return "CLVC_STRING, 0"
187 return "CLVC_BOOLEAN, 0"
190 # Given that an option called NAME has flags FLAGS, return an initializer
191 # for the "flag_var" field of its cl_options[] entry.
192 function var_ref
(name
, flags
)
194 name = var_name
(flags
) static_var
(name
, flags
)
197 if (opt_args
("Mask", flags
) != "")
198 return "&target_flags"
199 if (opt_args
("InverseMask", flags
) != "")
200 return "&target_flags"
204 # Given the option called NAME return a sanitized version of its name.
205 function opt_sanitized_name
(name
)
207 if (name ==
"gdwarf+")
209 gsub ("[^A-Za-z0-9]", "_", name
)
213 # Given the option called NAME return the appropriate enum for it.
214 function opt_enum
(name
)
216 return "OPT_" opt_sanitized_name
(name
)