OSDN Git Service

Add testcase for PR43351.
[pf3gnuchains/gcc-fork.git] / gcc / genconditions.c
1 /* Process machine description and calculate constant conditions.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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.  */
28
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"
37
38 /* so we can include except.h in the generated file.  */
39 static int saw_eh_return;
40
41 static void write_header        (void);
42 static void write_conditions    (void);
43 static int write_one_condition  (void **, void *);
44
45 /* Generate the header for insn-conditions.c.  */
46
47 static void
48 write_header (void)
49 {
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\
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 ENABLE_CHECKING\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 \"function.h\"\n\
76 \n\
77 /* Fake - insn-config.h doesn't exist yet.  */\n\
78 #define MAX_RECOG_OPERANDS 10\n\
79 #define MAX_DUP_OPERANDS 10\n\
80 #define MAX_INSNS_PER_SPLIT 5\n\
81 \n\
82 #include \"regs.h\"\n\
83 #include \"recog.h\"\n\
84 #include \"output.h\"\n\
85 #include \"flags.h\"\n\
86 #include \"hard-reg-set.h\"\n\
87 #include \"resource.h\"\n\
88 #include \"toplev.h\"\n\
89 #include \"reload.h\"\n\
90 #include \"tm-constrs.h\"\n");
91
92   if (saw_eh_return)
93     puts ("#define HAVE_eh_return 1");
94   puts ("#include \"except.h\"\n");
95
96   puts ("\
97 /* Dummy external declarations.  */\n\
98 extern rtx insn;\n\
99 extern rtx ins1;\n\
100 extern rtx operands[];\n\
101 \n\
102 #endif /* gcc >= 3.0.1 */\n");
103 }
104
105 /* Write out one entry in the conditions table, using the data pointed
106    to by SLOT.  Each entry looks like this:
107
108    { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
109      __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
110      ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
111      : -1) },  */
112
113 static int
114 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
115 {
116   const struct c_test *test = * (const struct c_test **) slot;
117   const char *p;
118
119   print_rtx_ptr_loc (test->expr);
120   fputs ("  { \"", stdout);
121   for (p = test->expr; *p; p++)
122     {
123       switch (*p)
124         {
125         case '\n': fputs ("\\n\\", stdout); break;
126         case '\\':
127         case '\"': putchar ('\\'); break;
128         default: break;
129         }
130       putchar (*p);
131     }
132
133   fputs ("\",\n    __builtin_constant_p ", stdout);
134   print_c_condition (test->expr);
135   fputs ("\n    ? (int) ", stdout);
136   print_c_condition (test->expr);
137   fputs ("\n    : -1 },\n", stdout);
138   return 1;
139 }
140
141 /* Write out the complete conditions table, its size, and a flag
142    indicating that gensupport.c can now do insn elision.  */
143 static void
144 write_conditions (void)
145 {
146   puts ("\
147 /* Structure definition duplicated from gensupport.h rather than\n\
148    drag in that file and its dependencies.  */\n\
149 struct c_test\n\
150 {\n\
151   const char *expr;\n\
152   int value;\n\
153 };\n\
154 \n\
155 /* This table lists each condition found in the machine description.\n\
156    Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
157    cannot be calculated at compile time.\n\
158    If we don't have __builtin_constant_p, or it's not acceptable in array\n\
159    initializers, fall back to assuming that all conditions potentially\n\
160    vary at run time.  It works in 3.0.1 and later; 3.0 only when not\n\
161    optimizing.  */\n\
162 \n\
163 #if GCC_VERSION >= 3001\n\
164 static const struct c_test insn_conditions[] = {\n");
165
166   traverse_c_tests (write_one_condition, 0);
167
168   puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
169 }
170
171 /* Emit code which will convert the C-format table to a
172    (define_conditions) form, which the MD reader can understand.
173    The result will be added to the set of files scanned by
174    'downstream' generators.  */
175 static void
176 write_writer (void)
177 {
178   puts ("int\n"
179         "main(void)\n"
180         "{\n"
181         "  unsigned int i;\n"
182         "  const char *p;\n"
183         "  puts (\"(define_conditions [\");\n"
184         "#if GCC_VERSION >= 3001\n"
185         "  for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
186         "    {\n"
187         "      printf (\"  (%d \\\"\", insn_conditions[i].value);\n"
188         "      for (p = insn_conditions[i].expr; *p; p++)\n"
189         "        {\n"
190         "          switch (*p)\n"
191         "            {\n"
192         "            case '\\\\':\n"
193         "            case '\\\"': putchar ('\\\\'); break;\n"
194         "            default: break;\n"
195         "            }\n"
196         "          putchar (*p);\n"
197         "        }\n"
198         "      puts (\"\\\")\");\n"
199         "    }\n"
200         "#endif /* gcc >= 3.0.1 */\n"
201         "  puts (\"])\");\n"
202         "  fflush (stdout);\n"
203         "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
204         "}");
205 }
206
207 int
208 main (int argc, char **argv)
209 {
210   rtx desc;
211   int pattern_lineno; /* not used */
212   int code;
213
214   progname = "genconditions";
215
216   if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
217     return (FATAL_EXIT_CODE);
218
219   /* Read the machine description.  */
220   while (1)
221     {
222       desc = read_md_rtx (&pattern_lineno, &code);
223       if (desc == NULL)
224         break;
225
226       /* N.B. define_insn_and_split, define_cond_exec are handled
227          entirely within read_md_rtx; we never see them.  */
228       switch (GET_CODE (desc))
229         {
230         default:
231           break;
232
233         case DEFINE_INSN:
234         case DEFINE_EXPAND:
235           add_c_test (XSTR (desc, 2), -1);
236           /* except.h needs to know whether there is an eh_return
237              pattern in the machine description.  */
238           if (!strcmp (XSTR (desc, 0), "eh_return"))
239             saw_eh_return = 1;
240           break;
241
242         case DEFINE_SPLIT:
243         case DEFINE_PEEPHOLE:
244         case DEFINE_PEEPHOLE2:
245           add_c_test (XSTR (desc, 1), -1);
246           break;
247         }
248     }
249
250   write_header ();
251   write_conditions ();
252   write_writer ();
253
254   fflush (stdout);
255   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
256 }