2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / genconditions.c
blobf356519935c1a0479c1deb39991219bb1426352a
1 /* Process machine description and calculate constant conditions.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* In a machine description, all of the insn patterns - define_insn,
22 define_expand, define_split, define_peephole, define_peephole2 -
23 contain an optional C expression which makes the final decision
24 about whether or not this pattern is usable. That expression may
25 turn out to be always false when the compiler is built. If it is,
26 most of the programs that generate code from the machine
27 description can simply ignore the entire pattern. */
29 #include "bconfig.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "errors.h"
35 #include "hashtab.h"
36 #include "read-md.h"
37 #include "gensupport.h"
39 /* so we can include except.h in the generated file. */
40 static int saw_eh_return;
42 static void write_header (void);
43 static void write_conditions (void);
44 static int write_one_condition (void **, void *);
46 /* Generate the header for insn-conditions.c. */
48 static void
49 write_header (void)
51 puts ("\
52 /* Generated automatically by the program `genconditions' from the target\n\
53 machine description file. */\n\
54 \n\
55 #include \"bconfig.h\"\n\
56 #include \"system.h\"\n\
57 \n\
58 /* It is necessary, but not entirely safe, to include the headers below\n\
59 in a generator program. As a defensive measure, don't do so when the\n\
60 table isn't going to have anything in it. */\n\
61 #if GCC_VERSION >= 3001\n\
62 \n\
63 /* Do not allow checking to confuse the issue. */\n\
64 #undef ENABLE_CHECKING\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 \n\
71 #include \"coretypes.h\"\n\
72 #include \"tm.h\"\n\
73 #include \"insn-constants.h\"\n\
74 #include \"rtl.h\"\n\
75 #include \"tm_p.h\"\n\
76 #include \"function.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 \"resource.h\"\n\
89 #include \"diagnostic-core.h\"\n\
90 #include \"toplev.h\"\n\
91 #include \"reload.h\"\n\
92 #include \"tm-constrs.h\"\n");
94 if (saw_eh_return)
95 puts ("#define HAVE_eh_return 1");
96 puts ("#include \"except.h\"\n");
98 puts ("\
99 /* Dummy external declarations. */\n\
100 extern rtx insn;\n\
101 extern rtx ins1;\n\
102 extern rtx operands[];\n\
104 #endif /* gcc >= 3.0.1 */\n");
107 /* Write out one entry in the conditions table, using the data pointed
108 to by SLOT. Each entry looks like this:
110 { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
111 __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
112 ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
113 : -1) }, */
115 static int
116 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
118 const struct c_test *test = * (const struct c_test **) slot;
119 const char *p;
121 print_md_ptr_loc (test->expr);
122 fputs (" { \"", stdout);
123 for (p = test->expr; *p; p++)
125 switch (*p)
127 case '\n': fputs ("\\n\\", stdout); break;
128 case '\\':
129 case '\"': putchar ('\\'); break;
130 default: break;
132 putchar (*p);
135 fputs ("\",\n __builtin_constant_p ", stdout);
136 print_c_condition (test->expr);
137 fputs ("\n ? (int) ", stdout);
138 print_c_condition (test->expr);
139 fputs ("\n : -1 },\n", stdout);
140 return 1;
143 /* Write out the complete conditions table, its size, and a flag
144 indicating that gensupport.c can now do insn elision. */
145 static void
146 write_conditions (void)
148 puts ("\
149 /* Structure definition duplicated from gensupport.h rather than\n\
150 drag in that file and its dependencies. */\n\
151 struct c_test\n\
152 {\n\
153 const char *expr;\n\
154 int value;\n\
155 };\n\
157 /* This table lists each condition found in the machine description.\n\
158 Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
159 cannot be calculated at compile time.\n\
160 If we don't have __builtin_constant_p, or it's not acceptable in array\n\
161 initializers, fall back to assuming that all conditions potentially\n\
162 vary at run time. It works in 3.0.1 and later; 3.0 only when not\n\
163 optimizing. */\n\
165 #if GCC_VERSION >= 3001\n\
166 static const struct c_test insn_conditions[] = {\n");
168 traverse_c_tests (write_one_condition, 0);
170 puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
173 /* Emit code which will convert the C-format table to a
174 (define_conditions) form, which the MD reader can understand.
175 The result will be added to the set of files scanned by
176 'downstream' generators. */
177 static void
178 write_writer (void)
180 puts ("int\n"
181 "main(void)\n"
182 "{\n"
183 " unsigned int i;\n"
184 " const char *p;\n"
185 " puts (\"(define_conditions [\");\n"
186 "#if GCC_VERSION >= 3001\n"
187 " for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
188 " {\n"
189 " printf (\" (%d \\\"\", insn_conditions[i].value);\n"
190 " for (p = insn_conditions[i].expr; *p; p++)\n"
191 " {\n"
192 " switch (*p)\n"
193 " {\n"
194 " case '\\\\':\n"
195 " case '\\\"': putchar ('\\\\'); break;\n"
196 " default: break;\n"
197 " }\n"
198 " putchar (*p);\n"
199 " }\n"
200 " puts (\"\\\")\");\n"
201 " }\n"
202 "#endif /* gcc >= 3.0.1 */\n"
203 " puts (\"])\");\n"
204 " fflush (stdout);\n"
205 "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
206 "}");
210 main (int argc, char **argv)
212 rtx desc;
213 int pattern_lineno; /* not used */
214 int code;
216 progname = "genconditions";
218 if (!init_rtx_reader_args (argc, argv))
219 return (FATAL_EXIT_CODE);
221 /* Read the machine description. */
222 while (1)
224 desc = read_md_rtx (&pattern_lineno, &code);
225 if (desc == NULL)
226 break;
228 /* N.B. define_insn_and_split, define_cond_exec are handled
229 entirely within read_md_rtx; we never see them. */
230 switch (GET_CODE (desc))
232 default:
233 break;
235 case DEFINE_INSN:
236 case DEFINE_EXPAND:
237 add_c_test (XSTR (desc, 2), -1);
238 /* except.h needs to know whether there is an eh_return
239 pattern in the machine description. */
240 if (!strcmp (XSTR (desc, 0), "eh_return"))
241 saw_eh_return = 1;
242 break;
244 case DEFINE_SPLIT:
245 case DEFINE_PEEPHOLE:
246 case DEFINE_PEEPHOLE2:
247 add_c_test (XSTR (desc, 1), -1);
248 break;
252 write_header ();
253 write_conditions ();
254 write_writer ();
256 fflush (stdout);
257 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);