params.c 5.3 KB

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