1 | /* Generate from machine description: |
2 | a series of #define statements, one for each constant named in |
3 | a (define_constants ...) pattern. |
4 | |
5 | Copyright (C) 1987-2023 Free Software Foundation, Inc. |
6 | |
7 | This file is part of GCC. |
8 | |
9 | GCC is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by |
11 | the Free Software Foundation; either version 3, or (at your option) |
12 | any later version. |
13 | |
14 | GCC is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ |
22 | |
23 | /* This program does not use gensupport.cc because it does not need to |
24 | look at insn patterns, only (define_constants), and we want to |
25 | minimize dependencies. */ |
26 | |
27 | #include "bconfig.h" |
28 | #include "system.h" |
29 | #include "coretypes.h" |
30 | #include "errors.h" |
31 | #include "statistics.h" |
32 | #include "vec.h" |
33 | #include "read-md.h" |
34 | |
35 | /* Called via traverse_md_constants; emit a #define for |
36 | the current constant definition. */ |
37 | |
38 | static int |
39 | print_md_constant (void **slot, void *info ATTRIBUTE_UNUSED) |
40 | { |
41 | struct md_constant *def = (struct md_constant *) *slot; |
42 | |
43 | if (!def->parent_enum) |
44 | printf (format: "#define %s %s\n" , def->name, def->value); |
45 | return 1; |
46 | } |
47 | |
48 | /* Called via traverse_enums. Emit an enum definition for |
49 | enum_type *SLOT. */ |
50 | |
51 | static int |
52 | print_enum_type (void **slot, void *info ATTRIBUTE_UNUSED) |
53 | { |
54 | struct enum_type *def; |
55 | struct enum_value *value; |
56 | char *value_name; |
57 | |
58 | def = (struct enum_type *) *slot; |
59 | printf (format: "\nenum %s {" , def->name); |
60 | for (value = def->values; value; value = value->next) |
61 | { |
62 | printf (format: "\n %s = %s" , value->def->name, value->def->value); |
63 | if (value->next) |
64 | putc (',', stdout); |
65 | } |
66 | printf (format: "\n};\n" ); |
67 | |
68 | /* Define NUM_<enum>_VALUES to be the largest enum value + 1. */ |
69 | value_name = ACONCAT (("num_" , def->name, "_values" , NULL)); |
70 | upcase_string (value_name); |
71 | printf (format: "#define %s %d\n" , value_name, def->num_values); |
72 | |
73 | /* Declare the array that is generated by genenum. */ |
74 | printf (format: "extern const char *const %s_strings[];\n" , def->name); |
75 | |
76 | return 1; |
77 | } |
78 | |
79 | int |
80 | main (int argc, const char **argv) |
81 | { |
82 | progname = "genconstants" ; |
83 | |
84 | noop_reader reader; |
85 | if (!reader.read_md_files (argc, argv, NULL)) |
86 | return (FATAL_EXIT_CODE); |
87 | |
88 | /* Initializing the MD reader has the side effect of loading up |
89 | the constants table that we wish to scan. */ |
90 | |
91 | puts (s: "/* Generated automatically by the program `genconstants'" ); |
92 | puts (s: " from the machine description file `md'. */\n" ); |
93 | puts (s: "#ifndef GCC_INSN_CONSTANTS_H" ); |
94 | puts (s: "#define GCC_INSN_CONSTANTS_H\n" ); |
95 | |
96 | reader.traverse_md_constants (callback: print_md_constant, info: 0); |
97 | reader.traverse_enum_types (callback: print_enum_type, info: 0); |
98 | |
99 | puts (s: "\n#endif /* GCC_INSN_CONSTANTS_H */" ); |
100 | |
101 | if (ferror (stdout) || fflush (stdout) || fclose (stdout)) |
102 | return FATAL_EXIT_CODE; |
103 | |
104 | return SUCCESS_EXIT_CODE; |
105 | } |
106 | |