Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / gcc / opt-functions.awk
1 #  Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
2 #  Contributed by Kelley Cook, June 2004.
3 #  Original code from Neil Booth, May 2003.
4 #
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
8 # later version.
9
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.
14
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/>.
18
19 # Some common subroutines for use by opt[ch]-gen.awk.
20
21 # Return nonzero if FLAGS contains a flag matching REGEX.
22 function flag_set_p(regex, flags)
23 {
24         return (" " flags " ") ~ (" " regex " ")
25 }
26
27 # Return STRING if FLAGS contains a flag matching regexp REGEX,
28 # otherwise return the empty string.
29 function test_flag(regex, flags, string)
30 {
31         if (flag_set_p(regex, flags))
32                 return string
33         return ""
34 }
35
36 # If FLAGS contains a "NAME(...argument...)" flag, return the value
37 # of the argument.  Return the empty string otherwise.
38 function opt_args(name, flags)
39 {
40         flags = " " flags
41         if (flags !~ " " name "\\(")
42                 return ""
43         sub(".* " name "\\(", "", flags)
44         sub("\\).*", "", flags)
45
46         return flags
47 }
48
49 # Return the Nth comma-separated element of S.  Return the empty string
50 # if S does not contain N elements.
51 function nth_arg(n, s)
52 {
53         while (n-- > 0) {
54                 if (s !~ ",")
55                         return ""
56                 sub("[^,]*, *", "", s)
57         }
58         sub(",.*", "", s)
59         return s
60 }
61
62 # Return a bitmask of CL_* values for option flags FLAGS.
63 function switch_flags (flags)
64 {
65         result = "0"
66         for (j = 0; j < n_langs; j++) {
67                 regex = langs[j]
68                 gsub ( "\\+", "\\+", regex )
69                 result = result test_flag(regex, flags, " | " macros[j])
70         }
71         result = result \
72           test_flag("Common", flags, " | CL_COMMON") \
73           test_flag("Target", flags, " | CL_TARGET") \
74           test_flag("Save", flags, " | CL_SAVE") \
75           test_flag("Joined", flags, " | CL_JOINED") \
76           test_flag("JoinedOrMissing", flags, " | CL_JOINED | CL_MISSING_OK") \
77           test_flag("Separate", flags, " | CL_SEPARATE") \
78           test_flag("RejectNegative", flags, " | CL_REJECT_NEGATIVE") \
79           test_flag("UInteger", flags, " | CL_UINTEGER") \
80           test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \
81           test_flag("Warning", flags,  " | CL_WARNING") \
82           test_flag("Optimization", flags,  " | CL_OPTIMIZATION") \
83           test_flag("Report", flags, " | CL_REPORT")
84         sub( "^0 \\| ", "", result )
85         return result
86 }
87
88 # If FLAGS includes a Var flag, return the name of the variable it specifies.
89 # Return the empty string otherwise.
90 function var_name(flags)
91 {
92         return nth_arg(0, opt_args("Var", flags))
93 }
94
95 # Return true if the option described by FLAGS has a globally-visible state.
96 function global_state_p(flags)
97 {
98         return (var_name(flags) != "" \
99                 || opt_args("Mask", flags) != "" \
100                 || opt_args("InverseMask", flags) != "")
101 }
102
103 # Return true if the option described by FLAGS must have some state
104 # associated with it.
105 function needs_state_p(flags)
106 {
107         return flag_set_p("Target", flags)
108 }
109
110 # If FLAGS describes an option that needs a static state variable,
111 # return the name of that variable, otherwise return "".  NAME is
112 # the name of the option.
113 function static_var(name, flags)
114 {
115         if (global_state_p(flags) || !needs_state_p(flags))
116                 return ""
117         gsub ("[^A-Za-z0-9]", "_", name)
118         return "VAR_" name
119 }
120
121 # Return the type of variable that should be associated with the given flags.
122 function var_type(flags)
123 {
124         if (!flag_set_p("Joined.*", flags))
125                 return "int "
126         else if (flag_set_p("UInteger", flags))
127                 return "int "
128         else
129                 return "const char *"
130 }
131
132 # Return the type of variable that should be associated with the given flags
133 # for use within a structure.  Simple variables are changed to unsigned char
134 # type instead of int to save space.
135 function var_type_struct(flags)
136 {
137         if (flag_set_p("UInteger", flags))
138                 return "int "
139         else if (!flag_set_p("Joined.*", flags)) {
140                 if (flag_set_p(".*Mask.*", flags))
141                         return "int "
142                 else
143                         return "unsigned char "
144         }
145         else
146                 return "const char *"
147 }
148
149 # Given that an option has flags FLAGS, return an initializer for the
150 # "var_cond" and "var_value" fields of its cl_options[] entry.
151 function var_set(flags)
152 {
153         s = nth_arg(1, opt_args("Var", flags))
154         if (s != "")
155                 return "CLVC_EQUAL, " s
156         s = opt_args("Mask", flags);
157         if (s != "") {
158                 vn = var_name(flags);
159                 if (vn)
160                         return "CLVC_BIT_SET, OPTION_MASK_" s
161                 else
162                         return "CLVC_BIT_SET, MASK_" s
163         }
164         s = nth_arg(0, opt_args("InverseMask", flags));
165         if (s != "") {
166                 vn = var_name(flags);
167                 if (vn)
168                         return "CLVC_BIT_CLEAR, OPTION_MASK_" s
169                 else
170                         return "CLVC_BIT_CLEAR, MASK_" s
171         }
172         if (var_type(flags) == "const char *")
173                 return "CLVC_STRING, 0"
174         return "CLVC_BOOLEAN, 0"
175 }
176
177 # Given that an option called NAME has flags FLAGS, return an initializer
178 # for the "flag_var" field of its cl_options[] entry.
179 function var_ref(name, flags)
180 {
181         name = var_name(flags) static_var(name, flags)
182         if (name != "")
183                 return "&" name
184         if (opt_args("Mask", flags) != "")
185                 return "&target_flags"
186         if (opt_args("InverseMask", flags) != "")
187                 return "&target_flags"
188         return "0"
189 }