OSDN Git Service

PR target/46430
[pf3gnuchains/gcc-fork.git] / gcc / opt-functions.awk
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.
5 #
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.
10
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.
15
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/>.
19
20 # Some common subroutines for use by opt[ch]-gen.awk.
21
22 # Define some helpful character classes, for portability.
23 BEGIN {
24         lower = "abcdefghijklmnopqrstuvwxyz"
25         upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26         digit = "0123456789"
27         alnum = lower "" upper "" digit
28 }
29
30 # Return nonzero if FLAGS contains a flag matching REGEX.
31 function flag_set_p(regex, flags)
32 {
33         return (" " flags " ") ~ (" " regex " ")
34 }
35
36 # Return STRING if FLAGS contains a flag matching regexp REGEX,
37 # otherwise return the empty string.
38 function test_flag(regex, flags, string)
39 {
40         if (flag_set_p(regex, flags))
41                 return string
42         return ""
43 }
44
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)
48 {
49         flags = " " flags
50         if (flags !~ " " name "\\(")
51                 return ""
52         sub(".* " name "\\(", "", flags)
53         if (flags ~ "^{")
54         {
55                 sub ("^{", "", flags)
56                 sub("}\\).*", "", flags)
57         }
58         else
59                 sub("\\).*", "", flags)
60
61         return flags
62 }
63
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)
67 {
68         while (n-- > 0) {
69                 if (s !~ ",")
70                         return ""
71                 sub("[^,]*, *", "", s)
72         }
73         sub(",.*", "", s)
74         return s
75 }
76
77 # Return a bitmask of CL_* values for option flags FLAGS.
78 function switch_flags (flags)
79 {
80         result = "0"
81         for (j = 0; j < n_langs; j++) {
82                 regex = langs[j]
83                 gsub ( "\\+", "\\+", regex )
84                 result = result test_flag(regex, flags, " | " macros[j])
85         }
86         result = result \
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("Save", flags, " | CL_SAVE") \
94           test_flag("Joined", flags, " | CL_JOINED") \
95           test_flag("JoinedOrMissing", flags, " | CL_JOINED | CL_MISSING_OK") \
96           test_flag("Separate", flags, " | CL_SEPARATE") \
97           test_flag("RejectNegative", flags, " | CL_REJECT_NEGATIVE") \
98           test_flag("UInteger", flags, " | CL_UINTEGER") \
99           test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \
100           test_flag("Warning", flags,  " | CL_WARNING") \
101           test_flag("Optimization", flags,  " | CL_OPTIMIZATION") \
102           test_flag("Report", flags, " | CL_REPORT")
103         sep_args = opt_args("Args", flags)
104         if (sep_args != "") {
105                 sep_args--
106                 result = result " | (" sep_args \
107                     " << CL_SEPARATE_NARGS_SHIFT)"
108         }
109         sub( "^0 \\| ", "", result )
110         return result
111 }
112
113 # If FLAGS includes a Var flag, return the name of the variable it specifies.
114 # Return the empty string otherwise.
115 function var_name(flags)
116 {
117         return nth_arg(0, opt_args("Var", flags))
118 }
119
120 # Return true if the option described by FLAGS has a globally-visible state.
121 function global_state_p(flags)
122 {
123         return (var_name(flags) != "" \
124                 || opt_args("Mask", flags) != "" \
125                 || opt_args("InverseMask", flags) != "")
126 }
127
128 # Return true if the option described by FLAGS must have some state
129 # associated with it.
130 function needs_state_p(flags)
131 {
132         return (flag_set_p("Target", flags) \
133                 && !flag_set_p("Alias.*", flags) \
134                 && !flag_set_p("Ignore", flags))
135 }
136
137 # If FLAGS describes an option that needs state without a public
138 # variable name, return the name of that field, minus the initial
139 # "x_", otherwise return "".  NAME is the name of the option.
140 function static_var(name, flags)
141 {
142         if (global_state_p(flags) || !needs_state_p(flags))
143                 return ""
144         gsub ("[^" alnum "]", "_", name)
145         return "VAR_" name
146 }
147
148 # Return the type of variable that should be associated with the given flags.
149 function var_type(flags)
150 {
151         if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags))
152                 return "int "
153         else if (flag_set_p("UInteger", flags))
154                 return "int "
155         else
156                 return "const char *"
157 }
158
159 # Return the type of variable that should be associated with the given flags
160 # for use within a structure.  Simple variables are changed to signed char
161 # type instead of int to save space.
162 function var_type_struct(flags)
163 {
164         if (flag_set_p("UInteger", flags))
165                 return "int "
166         else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags)) {
167                 if (flag_set_p(".*Mask.*", flags))
168                         return "int "
169                 else
170                         return "signed char "
171         }
172         else
173                 return "const char *"
174 }
175
176 # Given that an option has flags FLAGS, return an initializer for the
177 # "var_cond" and "var_value" fields of its cl_options[] entry.
178 function var_set(flags)
179 {
180         s = nth_arg(1, opt_args("Var", flags))
181         if (s != "")
182                 return "CLVC_EQUAL, " s
183         s = opt_args("Mask", flags);
184         if (s != "") {
185                 vn = var_name(flags);
186                 if (vn)
187                         return "CLVC_BIT_SET, OPTION_MASK_" s
188                 else
189                         return "CLVC_BIT_SET, MASK_" s
190         }
191         s = nth_arg(0, opt_args("InverseMask", flags));
192         if (s != "") {
193                 vn = var_name(flags);
194                 if (vn)
195                         return "CLVC_BIT_CLEAR, OPTION_MASK_" s
196                 else
197                         return "CLVC_BIT_CLEAR, MASK_" s
198         }
199         if (var_type(flags) == "const char *")
200                 return "CLVC_STRING, 0"
201         return "CLVC_BOOLEAN, 0"
202 }
203
204 # Given that an option called NAME has flags FLAGS, return an initializer
205 # for the "flag_var" field of its cl_options[] entry.
206 function var_ref(name, flags)
207 {
208         name = var_name(flags) static_var(name, flags)
209         if (name != "")
210                 return "offsetof (struct gcc_options, x_" name ")"
211         if (opt_args("Mask", flags) != "")
212                 return "offsetof (struct gcc_options, x_target_flags)"
213         if (opt_args("InverseMask", flags) != "")
214                 return "offsetof (struct gcc_options, x_target_flags)"
215         return "-1"
216 }
217
218 # Given the option called NAME return a sanitized version of its name.
219 function opt_sanitized_name(name)
220 {
221         gsub ("[^" alnum "]", "_", name)
222         return name
223 }
224
225 # Given the option called NAME return the appropriate enum for it.
226 function opt_enum(name)
227 {
228         return "OPT_" opt_sanitized_name(name)
229 }