1 /* params.c - Run-time parameters.
2    Copyright (C) 2001-2016 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4 
5 This file is part of GCC.
6 
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.
11 
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.
16 
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/>.  */
20 
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"
28 
29 /* An array containing the compiler parameters and their current
30    values.  */
31 
32 param_info *compiler_params;
33 
34 /* The number of entries in the table.  */
35 static size_t num_compiler_params;
36 
37 /* Whether the parameters have all been initialized and had their
38    default values determined.  */
39 static bool params_finished;
40 
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
47 
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 }
58 };
59 
60 /* Add the N PARAMS to the current list of compiler parameters.  */
61 
62 void
add_params(const param_info params[],size_t n)63 add_params (const param_info params[], size_t n)
64 {
65   gcc_assert (!params_finished);
66 
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;
76 }
77 
78 /* Add all parameters and default values that can be set in both the
79    driver and the compiler proper.  */
80 
81 void
global_init_params(void)82 global_init_params (void)
83 {
84   gcc_assert (!params_finished);
85 
86   add_params (lang_independent_params, LAST_PARAM);
87   targetm_common.option_default_params ();
88 }
89 
90 /* Note that all parameters have been added and all default values
91    set.  */
92 
93 void
finish_params(void)94 finish_params (void)
95 {
96   params_finished = true;
97 }
98 
99 /* Reset all state within params.c so that we can rerun the compiler
100    within the same process.  For use by toplev::finalize.  */
101 
102 void
params_c_finalize(void)103 params_c_finalize (void)
104 {
105   XDELETEVEC (compiler_params);
106   compiler_params = NULL;
107   num_compiler_params = 0;
108   params_finished = false;
109 }
110 
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.  */
114 
115 static void
set_param_value_internal(compiler_param num,int value,int * params,int * params_set,bool explicit_p)116 set_param_value_internal (compiler_param num, int value,
117 			  int *params, int *params_set,
118 			  bool explicit_p)
119 {
120   size_t i = (size_t) num;
121 
122   gcc_assert (params_finished);
123 
124   params[i] = value;
125   if (explicit_p)
126     params_set[i] = true;
127 }
128 
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.  */
131 
132 bool
find_param(const char * name,enum compiler_param * index)133 find_param (const char *name, enum compiler_param *index)
134 {
135   for (size_t i = 0; i < num_compiler_params; ++i)
136     if (strcmp (compiler_params[i].option, name) == 0)
137       {
138 	*index = (enum compiler_param) i;
139 	return true;
140       }
141 
142   return false;
143 }
144 
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.  */
147 
148 bool
param_string_value_p(enum compiler_param index,const char * value_name,int * value_p)149 param_string_value_p (enum compiler_param index, const char *value_name,
150 		      int *value_p)
151 {
152   param_info *entry = &compiler_params[(int) index];
153   if (entry->value_names == NULL)
154     return false;
155 
156   *value_p = -1;
157 
158   for (int i = 0; entry->value_names[i] != NULL; ++i)
159     if (strcmp (entry->value_names[i], value_name) == 0)
160       {
161 	*value_p = i;
162 	return true;
163       }
164 
165   return true;
166 }
167 
168 /* Set the VALUE associated with the parameter given by NAME in PARAMS
169    and PARAMS_SET.  */
170 
171 void
set_param_value(const char * name,int value,int * params,int * params_set)172 set_param_value (const char *name, int value,
173 		 int *params, int *params_set)
174 {
175   size_t i;
176 
177   /* Make sure nobody tries to set a parameter to an invalid value.  */
178   gcc_assert (value != INVALID_PARAM_VAL);
179 
180   enum compiler_param index;
181   if (!find_param (name, &index))
182     {
183       /* If we didn't find this parameter, issue an error message.  */
184       error ("invalid parameter %qs", name);
185       return;
186     }
187   i = (size_t)index;
188 
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);
201 }
202 
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.  */
206 
207 void
maybe_set_param_value(compiler_param num,int value,int * params,int * params_set)208 maybe_set_param_value (compiler_param num, int value,
209 		       int *params, int *params_set)
210 {
211   if (!params_set[(int) num])
212     set_param_value_internal (num, value, params, params_set, false);
213 }
214 
215 /* Set the default value of a parameter given by NUM to VALUE, before
216    option processing.  */
217 
218 void
set_default_param_value(compiler_param num,int value)219 set_default_param_value (compiler_param num, int value)
220 {
221   gcc_assert (!params_finished);
222 
223   compiler_params[(int) num].default_value = value;
224 }
225 
226 /* Return the default value of parameter NUM.  */
227 
228 int
default_param_value(compiler_param num)229 default_param_value (compiler_param num)
230 {
231   return compiler_params[(int) num].default_value;
232 }
233 
234 /* Initialize an array PARAMS with default values of the
235    parameters.  */
236 
237 void
init_param_values(int * params)238 init_param_values (int *params)
239 {
240   size_t i;
241 
242   gcc_assert (params_finished);
243 
244   for (i = 0; i < num_compiler_params; i++)
245     params[i] = compiler_params[i].default_value;
246 }
247 
248 /* Return the current value of num_compiler_params, for the benefit of
249    plugins that use parameters as features.  */
250 
251 size_t
get_num_compiler_params(void)252 get_num_compiler_params (void)
253 {
254   return num_compiler_params;
255 }
256