* recog.c (peephole2_optimize): Make it static.
[official-gcc.git] / gcc / genconditions.c
blobbc4bfd57151e353404c43ba085ee45421eb41d2f
1 /* Process machine description and calculate constant conditions.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
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 "gensupport.h"
38 /* so we can include except.h in the generated file. */
39 static int saw_eh_return;
41 static void write_header (void);
42 static void write_conditions (void);
43 static int write_one_condition (void **, void *);
45 /* Generate the header for insn-conditions.c. */
47 static void
48 write_header (void)
50 puts ("\
51 /* Generated automatically by the program `genconditions' from the target\n\
52 machine description file. */\n\
53 \n\
54 #include \"bconfig.h\"\n\
55 #include \"system.h\"\n");
57 puts ("\
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");
71 puts ("\
72 #include \"coretypes.h\"\n\
73 #include \"tm.h\"\n\
74 #include \"insn-constants.h\"\n\
75 #include \"rtl.h\"\n\
76 #include \"tm_p.h\"\n\
77 #include \"function.h\"\n");
79 puts ("\
80 /* Fake - insn-config.h doesn't exist yet. */\n\
81 #define MAX_RECOG_OPERANDS 10\n\
82 #define MAX_DUP_OPERANDS 10\n\
83 #define MAX_INSNS_PER_SPLIT 5\n");
85 puts ("\
86 #include \"regs.h\"\n\
87 #include \"recog.h\"\n\
88 #include \"real.h\"\n\
89 #include \"output.h\"\n\
90 #include \"flags.h\"\n\
91 #include \"hard-reg-set.h\"\n\
92 #include \"resource.h\"\n\
93 #include \"toplev.h\"\n\
94 #include \"reload.h\"\n");
96 if (saw_eh_return)
97 puts ("#define HAVE_eh_return 1");
98 puts ("#include \"except.h\"\n");
100 puts ("\
101 /* Dummy external declarations. */\n\
102 extern rtx insn;\n\
103 extern rtx ins1;\n\
104 extern rtx operands[];\n\
106 #endif /* gcc >= 3.0.1 */\n");
109 /* Write out one entry in the conditions table, using the data pointed
110 to by SLOT. Each entry looks like this:
112 { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
113 __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
114 ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
115 : -1) }, */
117 static int
118 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
120 const struct c_test *test = * (const struct c_test **) slot;
121 const char *p;
123 print_rtx_ptr_loc (test->expr);
124 fputs (" { \"", stdout);
125 for (p = test->expr; *p; p++)
127 switch (*p)
129 case '\n': fputs ("\\n\\", stdout); break;
130 case '\\':
131 case '\"': putchar ('\\'); break;
132 default: break;
134 putchar (*p);
137 printf ("\",\n __builtin_constant_p ");
138 print_c_condition (test->expr);
139 printf ("\n ? (int) ");
140 print_c_condition (test->expr);
141 printf ("\n : -1 },\n");
142 return 1;
145 /* Write out the complete conditions table, its size, and a flag
146 indicating that gensupport.c can now do insn elision. */
147 static void
148 write_conditions (void)
150 puts ("\
151 /* Structure definition duplicated from gensupport.h rather than\n\
152 drag in that file and its dependencies. */\n\
153 struct c_test\n\
154 {\n\
155 const char *expr;\n\
156 int value;\n\
157 };\n");
159 puts ("\
160 /* This table lists each condition found in the machine description.\n\
161 Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
162 cannot be calculated at compile time.\n\
163 If we don't have __builtin_constant_p, or it's not acceptable in array\n\
164 initializers, fall back to assuming that all conditions potentially\n\
165 vary at run time. It works in 3.0.1 and later; 3.0 only when not\n\
166 optimizing. */\n\
168 static const struct c_test insn_conditions[] = {\n\
169 #if GCC_VERSION >= 3001\n");
171 traverse_c_tests (write_one_condition, 0);
173 puts ("\n#endif /* gcc >= 3.0.1 */\n};\n");
176 /* Emit code which will convert the C-format table to a
177 (define_conditions) form, which the MD reader can understand.
178 The result will be added to the set of files scanned by
179 'downstream' generators. */
180 static void
181 write_writer (void)
183 puts ("int\n"
184 "main(void)\n"
185 "{\n"
186 " unsigned int i;\n"
187 " const char *p;\n"
188 " puts (\"(define_conditions [\");\n"
189 " for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
190 " {\n"
191 " printf (\" (%d \\\"\", insn_conditions[i].value);\n"
192 " for (p = insn_conditions[i].expr; *p; p++)\n"
193 " {\n"
194 " switch (*p)\n"
195 " {\n"
196 " case '\\\\':\n"
197 " case '\\\"': putchar ('\\\\'); break;\n"
198 " default: break;\n"
199 " }\n"
200 " putchar (*p);\n"
201 " }\n"
202 " puts (\"\\\")\");\n"
203 " }");
204 puts (" 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_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
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);