PR target/65604
[official-gcc.git] / gcc / params.c
blob41660b47393a37bb8e9f2fecc476b51a75d3fa69
1 /* params.c - Run-time parameters.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "common/common-target.h"
25 #include "params.h"
26 #include "params-enum.h"
27 #include "diagnostic-core.h"
29 /* An array containing the compiler parameters and their current
30 values. */
32 param_info *compiler_params;
34 /* The number of entries in the table. */
35 static size_t num_compiler_params;
37 /* Whether the parameters have all been initialized and had their
38 default values determined. */
39 static bool params_finished;
41 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX)
42 #define DEFPARAMENUM5(ENUM, OPTION, HELP, DEFAULT, V0, V1, V2, V3, V4) \
43 static const char *values_ ## ENUM [] = { #V0, #V1, #V2, #V3, #V4, NULL };
44 #include "params.def"
45 #undef DEFPARAMENUM5
46 #undef DEFPARAM
48 static const param_info lang_independent_params[] = {
49 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
50 { OPTION, DEFAULT, MIN, MAX, HELP, NULL },
51 #define DEFPARAMENUM5(ENUM, OPTION, HELP, DEFAULT, \
52 V0, V1, V2, V3, V4) \
53 { OPTION, (int)ENUM ## _KIND_ ## DEFAULT, 0, 4, HELP, values_ ## ENUM },
54 #include "params.def"
55 #undef DEFPARAM
56 #undef DEFPARAMENUM5
57 { NULL, 0, 0, 0, NULL, NULL }
60 /* Add the N PARAMS to the current list of compiler parameters. */
62 void
63 add_params (const param_info params[], size_t n)
65 gcc_assert (!params_finished);
67 /* Allocate enough space for the new parameters. */
68 compiler_params = XRESIZEVEC (param_info, compiler_params,
69 num_compiler_params + n);
70 /* Copy them into the table. */
71 memcpy (compiler_params + num_compiler_params,
72 params,
73 n * sizeof (param_info));
74 /* Keep track of how many parameters we have. */
75 num_compiler_params += n;
78 /* Add all parameters and default values that can be set in both the
79 driver and the compiler proper. */
81 void
82 global_init_params (void)
84 gcc_assert (!params_finished);
86 add_params (lang_independent_params, LAST_PARAM);
87 targetm_common.option_default_params ();
90 /* Note that all parameters have been added and all default values
91 set. */
93 void
94 finish_params (void)
96 params_finished = true;
99 /* Reset all state within params.c so that we can rerun the compiler
100 within the same process. For use by toplev::finalize. */
102 void
103 params_c_finalize (void)
105 XDELETEVEC (compiler_params);
106 compiler_params = NULL;
107 num_compiler_params = 0;
108 params_finished = false;
111 /* Set the value of the parameter given by NUM to VALUE in PARAMS and
112 PARAMS_SET. If EXPLICIT_P, this is being set by the user;
113 otherwise it is being set implicitly by the compiler. */
115 static void
116 set_param_value_internal (compiler_param num, int value,
117 int *params, int *params_set,
118 bool explicit_p)
120 size_t i = (size_t) num;
122 gcc_assert (params_finished);
124 params[i] = value;
125 if (explicit_p)
126 params_set[i] = true;
129 /* Return true if it can find the matching entry for NAME in the parameter
130 table, and assign the entry index to INDEX. Return false otherwise. */
132 bool
133 find_param (const char *name, enum compiler_param *index)
135 for (size_t i = 0; i < num_compiler_params; ++i)
136 if (strcmp (compiler_params[i].option, name) == 0)
138 *index = (enum compiler_param) i;
139 return true;
142 return false;
145 /* Return true if param with entry index INDEX should be defined using strings.
146 If so, return the value corresponding to VALUE_NAME in *VALUE_P. */
148 bool
149 param_string_value_p (enum compiler_param index, const char *value_name,
150 int *value_p)
152 param_info *entry = &compiler_params[(int) index];
153 if (entry->value_names == NULL)
154 return false;
156 *value_p = -1;
158 for (int i = 0; entry->value_names[i] != NULL; ++i)
159 if (strcmp (entry->value_names[i], value_name) == 0)
161 *value_p = i;
162 return true;
165 return true;
168 /* Set the VALUE associated with the parameter given by NAME in PARAMS
169 and PARAMS_SET. */
171 void
172 set_param_value (const char *name, int value,
173 int *params, int *params_set)
175 size_t i;
177 /* Make sure nobody tries to set a parameter to an invalid value. */
178 gcc_assert (value != INVALID_PARAM_VAL);
180 enum compiler_param index;
181 if (!find_param (name, &index))
183 /* If we didn't find this parameter, issue an error message. */
184 error ("invalid parameter %qs", name);
185 return;
187 i = (size_t)index;
189 if (value < compiler_params[i].min_value)
190 error ("minimum value of parameter %qs is %u",
191 compiler_params[i].option,
192 compiler_params[i].min_value);
193 else if (compiler_params[i].max_value > compiler_params[i].min_value
194 && value > compiler_params[i].max_value)
195 error ("maximum value of parameter %qs is %u",
196 compiler_params[i].option,
197 compiler_params[i].max_value);
198 else
199 set_param_value_internal ((compiler_param) i, value,
200 params, params_set, true);
203 /* Set the value of the parameter given by NUM to VALUE in PARAMS and
204 PARAMS_SET, implicitly, if it has not been set explicitly by the
205 user. */
207 void
208 maybe_set_param_value (compiler_param num, int value,
209 int *params, int *params_set)
211 if (!params_set[(int) num])
212 set_param_value_internal (num, value, params, params_set, false);
215 /* Set the default value of a parameter given by NUM to VALUE, before
216 option processing. */
218 void
219 set_default_param_value (compiler_param num, int value)
221 gcc_assert (!params_finished);
223 compiler_params[(int) num].default_value = value;
226 /* Return the default value of parameter NUM. */
229 default_param_value (compiler_param num)
231 return compiler_params[(int) num].default_value;
234 /* Initialize an array PARAMS with default values of the
235 parameters. */
237 void
238 init_param_values (int *params)
240 size_t i;
242 gcc_assert (params_finished);
244 for (i = 0; i < num_compiler_params; i++)
245 params[i] = compiler_params[i].default_value;
248 /* Return the current value of num_compiler_params, for the benefit of
249 plugins that use parameters as features. */
251 size_t
252 get_num_compiler_params (void)
254 return num_compiler_params;