gcc/
[official-gcc.git] / gcc / genconditions.c
blob89d273898a69163ace2eb02ce4156e45e46e4528
1 /* Process machine description and calculate constant conditions.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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. */
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"
37 /* so we can include except.h in the generated file. */
38 static int saw_eh_return;
40 static void write_header (void);
41 static void write_conditions (void);
42 static int write_one_condition (void **, void *);
44 /* Generate the header for insn-conditions.c. */
46 static void
47 write_header (void)
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 #include \"system.h\"\n\
55 \n\
56 /* It is necessary, but not entirely safe, to include the headers below\n\
57 in a generator program. As a defensive measure, don't do so when the\n\
58 table isn't going to have anything in it. */\n\
59 #if GCC_VERSION >= 3001\n\
60 \n\
61 /* Do not allow checking to confuse the issue. */\n\
62 #undef ENABLE_CHECKING\n\
63 #undef ENABLE_TREE_CHECKING\n\
64 #undef ENABLE_RTL_CHECKING\n\
65 #undef ENABLE_RTL_FLAG_CHECKING\n\
66 #undef ENABLE_GC_CHECKING\n\
67 #undef ENABLE_GC_ALWAYS_COLLECT\n\
68 \n\
69 #include \"coretypes.h\"\n\
70 #include \"tm.h\"\n\
71 #include \"insn-constants.h\"\n\
72 #include \"rtl.h\"\n\
73 #include \"tm_p.h\"\n\
74 #include \"hard-reg-set.h\"\n\
75 #include \"function.h\"\n\
76 #include \"emit-rtl.h\"\n\
77 \n\
78 /* Fake - insn-config.h doesn't exist yet. */\n\
79 #define MAX_RECOG_OPERANDS 10\n\
80 #define MAX_DUP_OPERANDS 10\n\
81 #define MAX_INSNS_PER_SPLIT 5\n\
82 \n\
83 #include \"regs.h\"\n\
84 #include \"recog.h\"\n\
85 #include \"output.h\"\n\
86 #include \"flags.h\"\n\
87 #include \"hard-reg-set.h\"\n\
88 #include \"predict.h\"\n\
89 #include \"basic-block.h\"\n\
90 #include \"resource.h\"\n\
91 #include \"diagnostic-core.h\"\n\
92 #include \"reload.h\"\n\
93 #include \"tm-constrs.h\"\n");
95 if (saw_eh_return)
96 puts ("#define HAVE_eh_return 1");
97 puts ("#include \"except.h\"\n");
99 puts ("\
100 /* Dummy external declarations. */\n\
101 extern rtx_insn *insn;\n\
102 extern rtx ins1;\n\
103 extern rtx operands[];\n\
105 #endif /* gcc >= 3.0.1 */\n");
108 /* Write out one entry in the conditions table, using the data pointed
109 to by SLOT. Each entry looks like this:
111 { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
112 __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
113 ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
114 : -1) }, */
116 static int
117 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
119 const struct c_test *test = * (const struct c_test **) slot;
120 const char *p;
122 print_md_ptr_loc (test->expr);
123 fputs (" { \"", stdout);
124 for (p = test->expr; *p; p++)
126 switch (*p)
128 case '\n': fputs ("\\n\\", stdout); break;
129 case '\\':
130 case '\"': putchar ('\\'); break;
131 default: break;
133 putchar (*p);
136 fputs ("\",\n __builtin_constant_p ", stdout);
137 print_c_condition (test->expr);
138 fputs ("\n ? (int) ", stdout);
139 print_c_condition (test->expr);
140 fputs ("\n : -1 },\n", stdout);
141 return 1;
144 /* Write out the complete conditions table, its size, and a flag
145 indicating that gensupport.c can now do insn elision. */
146 static void
147 write_conditions (void)
149 puts ("\
150 /* Structure definition duplicated from gensupport.h rather than\n\
151 drag in that file and its dependencies. */\n\
152 struct c_test\n\
153 {\n\
154 const char *expr;\n\
155 int value;\n\
156 };\n\
158 /* This table lists each condition found in the machine description.\n\
159 Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
160 cannot be calculated at compile time.\n\
161 If we don't have __builtin_constant_p, or it's not acceptable in array\n\
162 initializers, fall back to assuming that all conditions potentially\n\
163 vary at run time. It works in 3.0.1 and later; 3.0 only when not\n\
164 optimizing. */\n\
166 #if GCC_VERSION >= 3001\n\
167 static const struct c_test insn_conditions[] = {\n");
169 traverse_c_tests (write_one_condition, 0);
171 puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
174 /* Emit code which will convert the C-format table to a
175 (define_conditions) form, which the MD reader can understand.
176 The result will be added to the set of files scanned by
177 'downstream' generators. */
178 static void
179 write_writer (void)
181 puts ("int\n"
182 "main(void)\n"
183 "{\n"
184 " unsigned int i;\n"
185 " const char *p;\n"
186 " puts (\"(define_conditions [\");\n"
187 "#if GCC_VERSION >= 3001\n"
188 " for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
189 " {\n"
190 " printf (\" (%d \\\"\", insn_conditions[i].value);\n"
191 " for (p = insn_conditions[i].expr; *p; p++)\n"
192 " {\n"
193 " switch (*p)\n"
194 " {\n"
195 " case '\\\\':\n"
196 " case '\\\"': putchar ('\\\\'); break;\n"
197 " default: break;\n"
198 " }\n"
199 " putchar (*p);\n"
200 " }\n"
201 " puts (\"\\\")\");\n"
202 " }\n"
203 "#endif /* gcc >= 3.0.1 */\n"
204 " puts (\"])\");\n"
205 " fflush (stdout);\n"
206 "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
207 "}");
211 main (int argc, char **argv)
213 rtx desc;
214 int pattern_lineno; /* not used */
215 int code;
217 progname = "genconditions";
219 if (!init_rtx_reader_args (argc, argv))
220 return (FATAL_EXIT_CODE);
222 /* Read the machine description. */
223 while (1)
225 desc = read_md_rtx (&pattern_lineno, &code);
226 if (desc == NULL)
227 break;
229 /* N.B. define_insn_and_split, define_cond_exec are handled
230 entirely within read_md_rtx; we never see them. */
231 switch (GET_CODE (desc))
233 default:
234 break;
236 case DEFINE_INSN:
237 case DEFINE_EXPAND:
238 add_c_test (XSTR (desc, 2), -1);
239 /* except.h needs to know whether there is an eh_return
240 pattern in the machine description. */
241 if (!strcmp (XSTR (desc, 0), "eh_return"))
242 saw_eh_return = 1;
243 break;
245 case DEFINE_SPLIT:
246 case DEFINE_PEEPHOLE:
247 case DEFINE_PEEPHOLE2:
248 add_c_test (XSTR (desc, 1), -1);
249 break;
253 write_header ();
254 write_conditions ();
255 write_writer ();
257 fflush (stdout);
258 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);