* g++.dg/cpp/ucn-1.C: Fix typo.
[official-gcc.git] / gcc / genconditions.c
blobdd5bc92922105c43d1643a01571cff19cc3bb566
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 CHECKING_P\n\
63 #define CHECKING_P 0\n\
64 #undef ENABLE_TREE_CHECKING\n\
65 #undef ENABLE_RTL_CHECKING\n\
66 #undef ENABLE_RTL_FLAG_CHECKING\n\
67 #undef ENABLE_GC_CHECKING\n\
68 #undef ENABLE_GC_ALWAYS_COLLECT\n\
69 \n\
70 #include \"coretypes.h\"\n\
71 #include \"tm.h\"\n\
72 #include \"insn-constants.h\"\n\
73 #include \"rtl.h\"\n\
74 #include \"tm_p.h\"\n\
75 #include \"hard-reg-set.h\"\n\
76 #include \"function.h\"\n\
77 #include \"emit-rtl.h\"\n\
78 \n\
79 /* Fake - insn-config.h doesn't exist yet. */\n\
80 #define MAX_RECOG_OPERANDS 10\n\
81 #define MAX_DUP_OPERANDS 10\n\
82 #define MAX_INSNS_PER_SPLIT 5\n\
83 \n\
84 #include \"regs.h\"\n\
85 #include \"recog.h\"\n\
86 #include \"output.h\"\n\
87 #include \"flags.h\"\n\
88 #include \"hard-reg-set.h\"\n\
89 #include \"predict.h\"\n\
90 #include \"basic-block.h\"\n\
91 #include \"bitmap.h\"\n\
92 #include \"df.h\"\n\
93 #include \"resource.h\"\n\
94 #include \"diagnostic-core.h\"\n\
95 #include \"reload.h\"\n\
96 #include \"tm-constrs.h\"\n");
98 if (saw_eh_return)
99 puts ("#define HAVE_eh_return 1");
100 puts ("#include \"except.h\"\n");
102 puts ("\
103 /* Dummy external declarations. */\n\
104 extern rtx_insn *insn;\n\
105 extern rtx ins1;\n\
106 extern rtx operands[];\n\
108 #endif /* gcc >= 3.0.1 */\n");
111 /* Write out one entry in the conditions table, using the data pointed
112 to by SLOT. Each entry looks like this:
114 { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
115 __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
116 ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
117 : -1) }, */
119 static int
120 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
122 const struct c_test *test = * (const struct c_test **) slot;
123 const char *p;
125 print_md_ptr_loc (test->expr);
126 fputs (" { \"", stdout);
127 for (p = test->expr; *p; p++)
129 switch (*p)
131 case '\n': fputs ("\\n\\", stdout); break;
132 case '\\':
133 case '\"': putchar ('\\'); break;
134 default: break;
136 putchar (*p);
139 fputs ("\",\n __builtin_constant_p ", stdout);
140 print_c_condition (test->expr);
141 fputs ("\n ? (int) ", stdout);
142 print_c_condition (test->expr);
143 fputs ("\n : -1 },\n", stdout);
144 return 1;
147 /* Write out the complete conditions table, its size, and a flag
148 indicating that gensupport.c can now do insn elision. */
149 static void
150 write_conditions (void)
152 puts ("\
153 /* Structure definition duplicated from gensupport.h rather than\n\
154 drag in that file and its dependencies. */\n\
155 struct c_test\n\
156 {\n\
157 const char *expr;\n\
158 int value;\n\
159 };\n\
161 /* This table lists each condition found in the machine description.\n\
162 Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
163 cannot be calculated at compile time.\n\
164 If we don't have __builtin_constant_p, or it's not acceptable in array\n\
165 initializers, fall back to assuming that all conditions potentially\n\
166 vary at run time. It works in 3.0.1 and later; 3.0 only when not\n\
167 optimizing. */\n\
169 #if GCC_VERSION >= 3001\n\
170 static const struct c_test insn_conditions[] = {\n");
172 traverse_c_tests (write_one_condition, 0);
174 puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
177 /* Emit code which will convert the C-format table to a
178 (define_conditions) form, which the MD reader can understand.
179 The result will be added to the set of files scanned by
180 'downstream' generators. */
181 static void
182 write_writer (void)
184 puts ("int\n"
185 "main(void)\n"
186 "{\n"
187 " unsigned int i;\n"
188 " const char *p;\n"
189 " puts (\"(define_conditions [\");\n"
190 "#if GCC_VERSION >= 3001\n"
191 " for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
192 " {\n"
193 " printf (\" (%d \\\"\", insn_conditions[i].value);\n"
194 " for (p = insn_conditions[i].expr; *p; p++)\n"
195 " {\n"
196 " switch (*p)\n"
197 " {\n"
198 " case '\\\\':\n"
199 " case '\\\"': putchar ('\\\\'); break;\n"
200 " default: break;\n"
201 " }\n"
202 " putchar (*p);\n"
203 " }\n"
204 " puts (\"\\\")\");\n"
205 " }\n"
206 "#endif /* gcc >= 3.0.1 */\n"
207 " puts (\"])\");\n"
208 " fflush (stdout);\n"
209 "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
210 "}");
214 main (int argc, char **argv)
216 progname = "genconditions";
218 if (!init_rtx_reader_args (argc, argv))
219 return (FATAL_EXIT_CODE);
221 /* Read the machine description. */
222 md_rtx_info info;
223 while (read_md_rtx (&info))
225 rtx def = info.def;
226 add_c_test (get_c_test (def), -1);
227 switch (GET_CODE (def))
229 case DEFINE_INSN:
230 case DEFINE_EXPAND:
231 /* except.h needs to know whether there is an eh_return
232 pattern in the machine description. */
233 if (!strcmp (XSTR (def, 0), "eh_return"))
234 saw_eh_return = 1;
235 break;
237 default:
238 break;
242 write_header ();
243 write_conditions ();
244 write_writer ();
246 fflush (stdout);
247 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);