1/* Process machine description and calculate constant conditions.
2 Copyright (C) 2001-2017 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20/* In a machine description, all of the insn patterns - define_insn,
21 define_expand, define_split, define_peephole, define_peephole2 -
22 contain an optional C expression which makes the final decision
23 about whether or not this pattern is usable. That expression may
24 turn out to be always false when the compiler is built. If it is,
25 most of the programs that generate code from the machine
26 description can simply ignore the entire pattern. */
27
28#include "bconfig.h"
29#include "system.h"
30#include "coretypes.h"
31#include "tm.h"
32#include "rtl.h"
33#include "errors.h"
34#include "read-md.h"
35#include "gensupport.h"
36
37/* so we can include except.h in the generated file. */
38static int saw_eh_return;
39
40static void write_header (void);
41static void write_conditions (void);
42static int write_one_condition (void **, void *);
43
44/* Generate the header for insn-conditions.c. */
45
46static void
47write_header (void)
48{
49 puts ("\
50/* Generated automatically by the program `genconditions' from the target\n\
51 machine description file. */\n\
52\n\
53#include \"bconfig.h\"\n\
54#define INCLUDE_STRING\n\
55#include \"system.h\"\n\
56\n\
57/* It is necessary, but not entirely safe, to include the headers below\n\
58 in a generator program. As a defensive measure, don't do so when the\n\
59 table isn't going to have anything in it. */\n\
60#if GCC_VERSION >= 3001\n\
61\n\
62/* Do not allow checking to confuse the issue. */\n\
63#undef CHECKING_P\n\
64#define CHECKING_P 0\n\
65#undef ENABLE_TREE_CHECKING\n\
66#undef ENABLE_RTL_CHECKING\n\
67#undef ENABLE_RTL_FLAG_CHECKING\n\
68#undef ENABLE_GC_CHECKING\n\
69#undef ENABLE_GC_ALWAYS_COLLECT\n\
70#define USE_ENUM_MODES\n\
71\n\
72#include \"coretypes.h\"\n\
73#include \"tm.h\"\n\
74#include \"insn-constants.h\"\n\
75#include \"rtl.h\"\n\
76#include \"memmodel.h\"\n\
77#include \"tm_p.h\"\n\
78#include \"hard-reg-set.h\"\n\
79#include \"function.h\"\n\
80#include \"emit-rtl.h\"\n\
81\n\
82/* Fake - insn-config.h doesn't exist yet. */\n\
83#define MAX_RECOG_OPERANDS 10\n\
84#define MAX_DUP_OPERANDS 10\n\
85#define MAX_INSNS_PER_SPLIT 5\n\
86\n\
87#include \"regs.h\"\n\
88#include \"recog.h\"\n\
89#include \"output.h\"\n\
90#include \"flags.h\"\n\
91#include \"hard-reg-set.h\"\n\
92#include \"predict.h\"\n\
93#include \"basic-block.h\"\n\
94#include \"bitmap.h\"\n\
95#include \"df.h\"\n\
96#include \"resource.h\"\n\
97#include \"diagnostic-core.h\"\n\
98#include \"reload.h\"\n\
99#include \"tm-constrs.h\"\n");
100
101 if (saw_eh_return)
102 puts ("#define HAVE_eh_return 1");
103 puts ("#include \"except.h\"\n");
104
105 puts ("\
106/* Dummy external declarations. */\n\
107extern rtx_insn *insn;\n\
108extern rtx ins1;\n\
109extern rtx operands[];\n\
110\n\
111#endif /* gcc >= 3.0.1 */\n");
112}
113
114/* Write out one entry in the conditions table, using the data pointed
115 to by SLOT. Each entry looks like this:
116
117 { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
118 __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
119 ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
120 : -1) }, */
121
122static int
123write_one_condition (void **slot, void * ARG_UNUSED (dummy))
124{
125 const struct c_test *test = * (const struct c_test **) slot;
126 const char *p;
127
128 rtx_reader_ptr->print_md_ptr_loc (test->expr);
129 fputs (" { \"", stdout);
130 for (p = test->expr; *p; p++)
131 {
132 switch (*p)
133 {
134 case '\n': fputs ("\\n\\", stdout); break;
135 case '\\':
136 case '\"': putchar ('\\'); break;
137 default: break;
138 }
139 putchar (*p);
140 }
141
142 fputs ("\",\n __builtin_constant_p ", stdout);
143 rtx_reader_ptr->print_c_condition (test->expr);
144 fputs ("\n ? (int) ", stdout);
145 rtx_reader_ptr->print_c_condition (test->expr);
146 fputs ("\n : -1 },\n", stdout);
147 return 1;
148}
149
150/* Write out the complete conditions table, its size, and a flag
151 indicating that gensupport.c can now do insn elision. */
152static void
153write_conditions (void)
154{
155 puts ("\
156/* Structure definition duplicated from gensupport.h rather than\n\
157 drag in that file and its dependencies. */\n\
158struct c_test\n\
159{\n\
160 const char *expr;\n\
161 int value;\n\
162};\n\
163\n\
164/* This table lists each condition found in the machine description.\n\
165 Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
166 cannot be calculated at compile time.\n\
167 If we don't have __builtin_constant_p, or it's not acceptable in array\n\
168 initializers, fall back to assuming that all conditions potentially\n\
169 vary at run time. It works in 3.0.1 and later; 3.0 only when not\n\
170 optimizing. */\n\
171\n\
172#if GCC_VERSION >= 3001\n\
173static const struct c_test insn_conditions[] = {\n");
174
175 traverse_c_tests (write_one_condition, 0);
176
177 puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
178}
179
180/* Emit code which will convert the C-format table to a
181 (define_conditions) form, which the MD reader can understand.
182 The result will be added to the set of files scanned by
183 'downstream' generators. */
184static void
185write_writer (void)
186{
187 puts ("int\n"
188 "main(void)\n"
189 "{\n"
190 " unsigned int i;\n"
191 " const char *p;\n"
192 " puts (\"(define_conditions [\");\n"
193 "#if GCC_VERSION >= 3001\n"
194 " for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
195 " {\n"
196 " printf (\" (%d \\\"\", insn_conditions[i].value);\n"
197 " for (p = insn_conditions[i].expr; *p; p++)\n"
198 " {\n"
199 " switch (*p)\n"
200 " {\n"
201 " case '\\\\':\n"
202 " case '\\\"': putchar ('\\\\'); break;\n"
203 " default: break;\n"
204 " }\n"
205 " putchar (*p);\n"
206 " }\n"
207 " puts (\"\\\")\");\n"
208 " }\n"
209 "#endif /* gcc >= 3.0.1 */\n"
210 " puts (\"])\");\n"
211 " fflush (stdout);\n"
212 "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
213 "}");
214}
215
216int
217main (int argc, const char **argv)
218{
219 progname = "genconditions";
220
221 if (!init_rtx_reader_args (argc, argv))
222 return (FATAL_EXIT_CODE);
223
224 /* Read the machine description. */
225 md_rtx_info info;
226 while (read_md_rtx (&info))
227 {
228 rtx def = info.def;
229 add_c_test (get_c_test (def), -1);
230 switch (GET_CODE (def))
231 {
232 case DEFINE_INSN:
233 case DEFINE_EXPAND:
234 /* except.h needs to know whether there is an eh_return
235 pattern in the machine description. */
236 if (!strcmp (XSTR (def, 0), "eh_return"))
237 saw_eh_return = 1;
238 break;
239
240 default:
241 break;
242 }
243 }
244
245 write_header ();
246 write_conditions ();
247 write_writer ();
248
249 fflush (stdout);
250 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
251}
252