opth-gen.awk 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. # Copyright (C) 2003-2015 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. # This Awk script reads in the option records generated from
  19. # opt-gather.awk, combines the flags of duplicate options and generates a
  20. # C header file.
  21. #
  22. # This program uses functions from opt-functions.awk and code from
  23. # opt-read.awk.
  24. # Usage: awk -f opt-functions.awk -f opt-read.awk -f opth-gen.awk \
  25. # < inputfile > options.h
  26. # Dump out an enumeration into a .h file.
  27. # Combine the flags of duplicate options.
  28. END {
  29. print "/* This file is auto-generated by opth-gen.awk. */"
  30. print ""
  31. print "#ifndef OPTIONS_H"
  32. print "#define OPTIONS_H"
  33. print ""
  34. print "#include \"flag-types.h\""
  35. print ""
  36. if (n_extra_h_includes > 0) {
  37. for (i = 0; i < n_extra_h_includes; i++) {
  38. print "#include " quote extra_h_includes[i] quote
  39. }
  40. print ""
  41. }
  42. print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
  43. print "#ifndef GENERATOR_FILE"
  44. print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
  45. print "struct GTY(()) gcc_options"
  46. print "#else"
  47. print "struct gcc_options"
  48. print "#endif"
  49. print "{"
  50. print "#endif"
  51. for (i = 0; i < n_extra_vars; i++) {
  52. var = extra_vars[i]
  53. sub(" *=.*", "", var)
  54. orig_var = var
  55. name = var
  56. type = var
  57. type_after = var
  58. sub("^.*[ *]", "", name)
  59. sub("\\[.*\\]$", "", name)
  60. sub("\\[.*\\]$", "", type)
  61. sub(" *" name "$", "", type)
  62. sub("^.*" name, "", type_after)
  63. var_seen[name] = 1
  64. print "#ifdef GENERATOR_FILE"
  65. print "extern " orig_var ";"
  66. print "#else"
  67. print " " type " x_" name type_after ";"
  68. print "#define " name " global_options.x_" name
  69. print "#endif"
  70. }
  71. for (i = 0; i < n_opts; i++) {
  72. if (flag_set_p("Save", flags[i]))
  73. have_save = 1;
  74. name = var_name(flags[i]);
  75. if (name == "")
  76. continue;
  77. if (name in var_seen)
  78. continue;
  79. var_seen[name] = 1;
  80. print "#ifdef GENERATOR_FILE"
  81. print "extern " var_type(flags[i]) name ";"
  82. print "#else"
  83. print " " var_type(flags[i]) "x_" name ";"
  84. print "#define " name " global_options.x_" name
  85. print "#endif"
  86. }
  87. for (i = 0; i < n_opts; i++) {
  88. name = static_var(opts[i], flags[i]);
  89. if (name != "") {
  90. print "#ifndef GENERATOR_FILE"
  91. print " " var_type(flags[i]) "x_" name ";"
  92. print "#define x_" name " do_not_use"
  93. print "#endif"
  94. }
  95. }
  96. for (i = 0; i < n_opts; i++) {
  97. if (flag_set_p("SetByCombined", flags[i])) {
  98. print "#ifndef GENERATOR_FILE"
  99. print " bool frontend_set_" var_name(flags[i]) ";"
  100. print "#endif"
  101. }
  102. }
  103. print "#ifndef GENERATOR_FILE"
  104. print "};"
  105. print "extern struct gcc_options global_options;"
  106. print "extern const struct gcc_options global_options_init;"
  107. print "extern struct gcc_options global_options_set;"
  108. print "#define target_flags_explicit global_options_set.x_target_flags"
  109. print "#endif"
  110. print "#endif"
  111. print ""
  112. # All of the optimization switches gathered together so they can be saved and restored.
  113. # This will allow attribute((cold)) to turn on space optimization.
  114. # Change the type of normal switches from int to unsigned char to save space.
  115. # Also, order the structure so that pointer fields occur first, then int
  116. # fields, and then char fields to provide the best packing.
  117. print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
  118. print ""
  119. print "/* Structure to save/restore optimization and target specific options. */";
  120. print "struct GTY(()) cl_optimization";
  121. print "{";
  122. n_opt_char = 3;
  123. n_opt_short = 0;
  124. n_opt_int = 0;
  125. n_opt_enum = 0;
  126. n_opt_other = 0;
  127. var_opt_char[0] = "unsigned char x_optimize";
  128. var_opt_char[1] = "unsigned char x_optimize_size";
  129. var_opt_char[2] = "unsigned char x_optimize_debug";
  130. for (i = 0; i < n_opts; i++) {
  131. if (flag_set_p("Optimization", flags[i])) {
  132. name = var_name(flags[i])
  133. if(name == "")
  134. continue;
  135. if(name in var_opt_seen)
  136. continue;
  137. var_opt_seen[name]++;
  138. otype = var_type_struct(flags[i]);
  139. if (otype ~ "^((un)?signed +)?int *$")
  140. var_opt_int[n_opt_int++] = otype "x_" name;
  141. else if (otype ~ "^((un)?signed +)?short *$")
  142. var_opt_short[n_opt_short++] = otype "x_" name;
  143. else if (otype ~ "^((un)?signed +)?char *$")
  144. var_opt_char[n_opt_char++] = otype "x_" name;
  145. else if (otype ~ ("^enum +[_" alnum "]+ *$"))
  146. var_opt_enum[n_opt_enum++] = otype "x_" name;
  147. else
  148. var_opt_other[n_opt_other++] = otype "x_" name;
  149. }
  150. }
  151. for (i = 0; i < n_opt_other; i++) {
  152. print " " var_opt_other[i] ";";
  153. }
  154. for (i = 0; i < n_opt_int; i++) {
  155. print " " var_opt_int[i] ";";
  156. }
  157. for (i = 0; i < n_opt_enum; i++) {
  158. print " " var_opt_enum[i] ";";
  159. }
  160. for (i = 0; i < n_opt_short; i++) {
  161. print " " var_opt_short[i] ";";
  162. }
  163. for (i = 0; i < n_opt_char; i++) {
  164. print " " var_opt_char[i] ";";
  165. }
  166. print "};";
  167. print "";
  168. # Target and optimization save/restore/print functions.
  169. print "/* Structure to save/restore selected target specific options. */";
  170. print "struct GTY(()) cl_target_option";
  171. print "{";
  172. n_target_char = 0;
  173. n_target_short = 0;
  174. n_target_int = 0;
  175. n_target_enum = 0;
  176. n_target_other = 0;
  177. for (i = 0; i < n_target_save; i++) {
  178. if (target_save_decl[i] ~ "^((un)?signed +)?int +[_" alnum "]+$")
  179. var_target_int[n_target_int++] = target_save_decl[i];
  180. else if (target_save_decl[i] ~ "^((un)?signed +)?short +[_" alnum "]+$")
  181. var_target_short[n_target_short++] = target_save_decl[i];
  182. else if (target_save_decl[i] ~ "^((un)?signed +)?char +[_ " alnum "]+$")
  183. var_target_char[n_target_char++] = target_save_decl[i];
  184. else if (target_save_decl[i] ~ ("^enum +[_" alnum "]+ +[_" alnum "]+$")) {
  185. var_target_enum[n_target_enum++] = target_save_decl[i];
  186. }
  187. else
  188. var_target_other[n_target_other++] = target_save_decl[i];
  189. }
  190. if (have_save) {
  191. for (i = 0; i < n_opts; i++) {
  192. if (flag_set_p("Save", flags[i])) {
  193. name = var_name(flags[i])
  194. if(name == "")
  195. name = "target_flags";
  196. if(name in var_save_seen)
  197. continue;
  198. var_save_seen[name]++;
  199. otype = var_type_struct(flags[i])
  200. if (otype ~ "^((un)?signed +)?int *$")
  201. var_target_int[n_target_int++] = otype "x_" name;
  202. else if (otype ~ "^((un)?signed +)?short *$")
  203. var_target_short[n_target_short++] = otype "x_" name;
  204. else if (otype ~ "^((un)?signed +)?char *$")
  205. var_target_char[n_target_char++] = otype "x_" name;
  206. else if (otype ~ ("^enum +[_" alnum "]+ +[_" alnum "]+"))
  207. var_target_enum[n_target_enum++] = otype "x_" name;
  208. else
  209. var_target_other[n_target_other++] = otype "x_" name;
  210. }
  211. }
  212. } else {
  213. var_target_int[n_target_int++] = "int x_target_flags";
  214. }
  215. for (i = 0; i < n_target_other; i++) {
  216. print " " var_target_other[i] ";";
  217. }
  218. for (i = 0; i < n_target_enum; i++) {
  219. print " " var_target_enum[i] ";";
  220. }
  221. for (i = 0; i < n_target_int; i++) {
  222. print " " var_target_int[i] ";";
  223. }
  224. for (i = 0; i < n_target_short; i++) {
  225. print " " var_target_short[i] ";";
  226. }
  227. for (i = 0; i < n_target_char; i++) {
  228. print " " var_target_char[i] ";";
  229. }
  230. print "};";
  231. print "";
  232. print "";
  233. print "/* Save optimization variables into a structure. */"
  234. print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
  235. print "";
  236. print "/* Restore optimization variables from a structure. */";
  237. print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
  238. print "";
  239. print "/* Print optimization variables from a structure. */";
  240. print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
  241. print "";
  242. print "/* Print different optimization variables from structures provided as arguments. */";
  243. print "extern void cl_optimization_print_diff (FILE *, int, cl_optimization *ptr1, cl_optimization *ptr2);";
  244. print "";
  245. print "/* Save selected option variables into a structure. */"
  246. print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
  247. print "";
  248. print "/* Restore selected option variables from a structure. */"
  249. print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
  250. print "";
  251. print "/* Print target option variables from a structure. */";
  252. print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
  253. print "";
  254. print "/* Print different target option variables from structures provided as arguments. */";
  255. print "extern void cl_target_option_print_diff (FILE *, int, cl_target_option *ptr1, cl_target_option *ptr2);";
  256. print "";
  257. print "/* Compare two target option variables from a structure. */";
  258. print "extern bool cl_target_option_eq (const struct cl_target_option *, const struct cl_target_option *);";
  259. print "";
  260. print "/* Hash option variables from a structure. */";
  261. print "extern hashval_t cl_target_option_hash (const struct cl_target_option *);";
  262. print "";
  263. print "/* Hash optimization from a structure. */";
  264. print "extern hashval_t cl_optimization_hash (const struct cl_optimization *);";
  265. print "";
  266. print "/* Anything that includes tm.h, does not necessarily need this. */"
  267. print "#if !defined(GCC_TM_H)"
  268. print "#include \"input.h\" /* for location_t */"
  269. print "bool "
  270. print "common_handle_option_auto (struct gcc_options *opts, "
  271. print " struct gcc_options *opts_set, "
  272. print " const struct cl_decoded_option *decoded, "
  273. print " unsigned int lang_mask, int kind, "
  274. print " location_t loc, "
  275. print " const struct cl_option_handlers *handlers, "
  276. print " diagnostic_context *dc); "
  277. for (i = 0; i < n_langs; i++) {
  278. lang_name = lang_sanitized_name(langs[i]);
  279. print "bool "
  280. print lang_name "_handle_option_auto (struct gcc_options *opts, "
  281. print " struct gcc_options *opts_set, "
  282. print " size_t scode, const char *arg, int value, "
  283. print " unsigned int lang_mask, int kind, "
  284. print " location_t loc, "
  285. print " const struct cl_option_handlers *handlers, "
  286. print " diagnostic_context *dc); "
  287. }
  288. print "void cpp_handle_option_auto (const struct gcc_options * opts, size_t scode,"
  289. print " struct cpp_options * cpp_opts);"
  290. print "void init_global_opts_from_cpp(struct gcc_options * opts, "
  291. print " const struct cpp_options * cpp_opts);"
  292. print "#endif";
  293. print "#endif";
  294. print "";
  295. for (i = 0; i < n_opts; i++) {
  296. name = opt_args("Mask", flags[i])
  297. if (name == "") {
  298. opt = opt_args("InverseMask", flags[i])
  299. if (opt ~ ",")
  300. name = nth_arg(0, opt)
  301. else
  302. name = opt
  303. }
  304. if (name != "" && mask_bits[name] == 0) {
  305. mask_bits[name] = 1
  306. vname = var_name(flags[i])
  307. mask = "MASK_"
  308. mask_1 = "1"
  309. if (vname != "") {
  310. mask = "OPTION_MASK_"
  311. if (host_wide_int[vname] == "yes")
  312. mask_1 = "HOST_WIDE_INT_1"
  313. } else
  314. extra_mask_bits[name] = 1
  315. print "#define " mask name " (" mask_1 " << " masknum[vname]++ ")"
  316. }
  317. }
  318. for (i = 0; i < n_extra_masks; i++) {
  319. if (extra_mask_bits[extra_masks[i]] == 0)
  320. print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
  321. }
  322. for (var in masknum) {
  323. if (var != "" && host_wide_int[var] == "yes") {
  324. print" #if defined(HOST_BITS_PER_WIDE_INT) && " masknum[var] " >= HOST_BITS_PER_WIDE_INT"
  325. print "#error too many masks for " var
  326. print "#endif"
  327. }
  328. else if (masknum[var] > 31) {
  329. if (var == "")
  330. print "#error too many target masks"
  331. else
  332. print "#error too many masks for " var
  333. }
  334. }
  335. print ""
  336. for (i = 0; i < n_opts; i++) {
  337. name = opt_args("Mask", flags[i])
  338. if (name == "") {
  339. opt = opt_args("InverseMask", flags[i])
  340. if (opt ~ ",")
  341. name = nth_arg(0, opt)
  342. else
  343. name = opt
  344. }
  345. if (name != "" && mask_macros[name] == 0) {
  346. mask_macros[name] = 1
  347. vname = var_name(flags[i])
  348. mask = "OPTION_MASK_"
  349. if (vname == "") {
  350. vname = "target_flags"
  351. mask = "MASK_"
  352. extra_mask_macros[name] = 1
  353. }
  354. print "#define TARGET_" name \
  355. " ((" vname " & " mask name ") != 0)"
  356. print "#define TARGET_" name "_P(" vname ")" \
  357. " ((" vname " & " mask name ") != 0)"
  358. }
  359. }
  360. for (i = 0; i < n_extra_masks; i++) {
  361. if (extra_mask_macros[extra_masks[i]] == 0)
  362. print "#define TARGET_" extra_masks[i] \
  363. " ((target_flags & MASK_" extra_masks[i] ") != 0)"
  364. }
  365. print ""
  366. for (i = 0; i < n_opts; i++) {
  367. opt = opt_args("InverseMask", flags[i])
  368. if (opt ~ ",") {
  369. vname = var_name(flags[i])
  370. mask = "OPTION_MASK_"
  371. if (vname == "") {
  372. vname = "target_flags"
  373. mask = "MASK_"
  374. }
  375. print "#define TARGET_" nth_arg(1, opt) \
  376. " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
  377. }
  378. }
  379. print ""
  380. for (i = 0; i < n_langs; i++) {
  381. macros[i] = "CL_" lang_sanitized_name(langs[i])
  382. s = substr(" ", length (macros[i]))
  383. print "#define " macros[i] s " (1U << " i ")"
  384. }
  385. print "#define CL_LANG_ALL ((1U << " n_langs ") - 1)"
  386. print ""
  387. print "enum opt_code"
  388. print "{"
  389. for (i = 0; i < n_opts; i++)
  390. back_chain[i] = "N_OPTS";
  391. enum_value = 0
  392. for (i = 0; i < n_opts; i++) {
  393. # Combine the flags of identical switches. Switches
  394. # appear many times if they are handled by many front
  395. # ends, for example.
  396. while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
  397. flags[i + 1] = flags[i] " " flags[i + 1];
  398. i++;
  399. }
  400. len = length (opts[i]);
  401. enum = opt_enum(opts[i])
  402. enum_string = enum " = " enum_value ","
  403. # Aliases do not get enumeration names.
  404. if ((flag_set_p("Alias.*", flags[i]) \
  405. && !flag_set_p("SeparateAlias", flags[i])) \
  406. || flag_set_p("Ignore", flags[i])) {
  407. enum_string = "/* " enum_string " */"
  408. }
  409. # If this switch takes joined arguments, back-chain all
  410. # subsequent switches to it for which it is a prefix. If
  411. # a later switch S is a longer prefix of a switch T, T
  412. # will be back-chained to S in a later iteration of this
  413. # for() loop, which is what we want.
  414. if (flag_set_p("Joined.*", flags[i])) {
  415. for (j = i + 1; j < n_opts; j++) {
  416. if (substr (opts[j], 1, len) != opts[i])
  417. break;
  418. back_chain[j] = enum;
  419. }
  420. }
  421. s = substr(" ",
  422. length (enum_string))
  423. if (help[i] == "")
  424. hlp = "0"
  425. else
  426. hlp = "N_(\"" help[i] "\")";
  427. print " " enum_string s "/* -" opts[i] " */"
  428. enum_value++
  429. }
  430. print " N_OPTS,"
  431. print " OPT_SPECIAL_unknown,"
  432. print " OPT_SPECIAL_ignore,"
  433. print " OPT_SPECIAL_program_name,"
  434. print " OPT_SPECIAL_input_file"
  435. print "};"
  436. print ""
  437. print "#ifdef GCC_C_COMMON_C"
  438. print "/* Mapping from cpp message reasons to the options that enable them. */"
  439. print "#include <cpplib.h>"
  440. print "struct cpp_reason_option_codes_t"
  441. print "{"
  442. print " const int reason; /* cpplib message reason. */"
  443. print " const int option_code; /* gcc option that controls this message. */"
  444. print "};"
  445. print ""
  446. print "static const struct cpp_reason_option_codes_t cpp_reason_option_codes[] = {"
  447. for (i = 0; i < n_opts; i++) {
  448. # With identical flags, pick only the last one. The
  449. # earlier loop ensured that it has all flags merged,
  450. # and a nonempty help text if one of the texts was nonempty.
  451. while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
  452. i++;
  453. }
  454. cpp_reason = nth_arg(0, opt_args("CppReason", flags[i]));
  455. if (cpp_reason != "") {
  456. cpp_reason = cpp_reason ",";
  457. printf(" {%-40s %s},\n", cpp_reason, opt_enum(opts[i]))
  458. }
  459. }
  460. printf(" {%-40s 0},\n", "CPP_W_NONE,")
  461. print "};"
  462. print "#endif"
  463. print ""
  464. print "#endif /* OPTIONS_H */"
  465. }