1/* params.c - Run-time parameters.
2   Copyright (C) 2001-2015 Free Software Foundation, Inc.
3   Written by Mark Mitchell <mark@codesourcery.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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 "diagnostic-core.h"
27
28/* An array containing the compiler parameters and their current
29   values.  */
30
31param_info *compiler_params;
32
33/* The number of entries in the table.  */
34static size_t num_compiler_params;
35
36/* Whether the parameters have all been initialized and had their
37   default values determined.  */
38static bool params_finished;
39
40static const param_info lang_independent_params[] = {
41#define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
42  { OPTION, DEFAULT, MIN, MAX, HELP },
43#include "params.def"
44#undef DEFPARAM
45  { NULL, 0, 0, 0, NULL }
46};
47
48/* Add the N PARAMS to the current list of compiler parameters.  */
49
50void
51add_params (const param_info params[], size_t n)
52{
53  gcc_assert (!params_finished);
54
55  /* Allocate enough space for the new parameters.  */
56  compiler_params = XRESIZEVEC (param_info, compiler_params,
57				num_compiler_params + n);
58  /* Copy them into the table.  */
59  memcpy (compiler_params + num_compiler_params,
60	  params,
61	  n * sizeof (param_info));
62  /* Keep track of how many parameters we have.  */
63  num_compiler_params += n;
64}
65
66/* Add all parameters and default values that can be set in both the
67   driver and the compiler proper.  */
68
69void
70global_init_params (void)
71{
72  gcc_assert (!params_finished);
73
74  add_params (lang_independent_params, LAST_PARAM);
75  targetm_common.option_default_params ();
76}
77
78/* Note that all parameters have been added and all default values
79   set.  */
80
81void
82finish_params (void)
83{
84  params_finished = true;
85}
86
87/* Reset all state within params.c so that we can rerun the compiler
88   within the same process.  For use by toplev::finalize.  */
89
90void
91params_c_finalize (void)
92{
93  XDELETEVEC (compiler_params);
94  compiler_params = NULL;
95  num_compiler_params = 0;
96  params_finished = false;
97}
98
99/* Set the value of the parameter given by NUM to VALUE in PARAMS and
100   PARAMS_SET.  If EXPLICIT_P, this is being set by the user;
101   otherwise it is being set implicitly by the compiler.  */
102
103static void
104set_param_value_internal (compiler_param num, int value,
105			  int *params, int *params_set,
106			  bool explicit_p)
107{
108  size_t i = (size_t) num;
109
110  gcc_assert (params_finished);
111
112  params[i] = value;
113  if (explicit_p)
114    params_set[i] = true;
115}
116
117/* Set the VALUE associated with the parameter given by NAME in PARAMS
118   and PARAMS_SET.  */
119
120void
121set_param_value (const char *name, int value,
122		 int *params, int *params_set)
123{
124  size_t i;
125
126  /* Make sure nobody tries to set a parameter to an invalid value.  */
127  gcc_assert (value != INVALID_PARAM_VAL);
128
129  /* Scan the parameter table to find a matching entry.  */
130  for (i = 0; i < num_compiler_params; ++i)
131    if (strcmp (compiler_params[i].option, name) == 0)
132      {
133	if (value < compiler_params[i].min_value)
134	  error ("minimum value of parameter %qs is %u",
135		 compiler_params[i].option,
136		 compiler_params[i].min_value);
137	else if (compiler_params[i].max_value > compiler_params[i].min_value
138		 && value > compiler_params[i].max_value)
139	  error ("maximum value of parameter %qs is %u",
140		 compiler_params[i].option,
141		 compiler_params[i].max_value);
142	else
143	  set_param_value_internal ((compiler_param) i, value,
144				    params, params_set, true);
145	return;
146      }
147
148  /* If we didn't find this parameter, issue an error message.  */
149  error ("invalid parameter %qs", name);
150}
151
152/* Set the value of the parameter given by NUM to VALUE in PARAMS and
153   PARAMS_SET, implicitly, if it has not been set explicitly by the
154   user.  */
155
156void
157maybe_set_param_value (compiler_param num, int value,
158		       int *params, int *params_set)
159{
160  if (!params_set[(int) num])
161    set_param_value_internal (num, value, params, params_set, false);
162}
163
164/* Set the default value of a parameter given by NUM to VALUE, before
165   option processing.  */
166
167void
168set_default_param_value (compiler_param num, int value)
169{
170  gcc_assert (!params_finished);
171
172  compiler_params[(int) num].default_value = value;
173}
174
175/* Return the default value of parameter NUM.  */
176
177int
178default_param_value (compiler_param num)
179{
180  return compiler_params[(int) num].default_value;
181}
182
183/* Initialize an array PARAMS with default values of the
184   parameters.  */
185
186void
187init_param_values (int *params)
188{
189  size_t i;
190
191  gcc_assert (params_finished);
192
193  for (i = 0; i < num_compiler_params; i++)
194    params[i] = compiler_params[i].default_value;
195}
196
197/* Return the current value of num_compiler_params, for the benefit of
198   plugins that use parameters as features.  */
199
200size_t
201get_num_compiler_params (void)
202{
203  return num_compiler_params;
204}
205