OSDN Git Service

PR c++/10549
[pf3gnuchains/gcc-fork.git] / gcc / genconditions.c
1 /* Process machine description and calculate constant conditions.
2    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
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.
10
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.
15
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, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
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 htab_t condition_table;
42
43 static void add_condition       PARAMS ((const char *));
44 static void write_header        PARAMS ((void));
45 static void write_conditions    PARAMS ((void));
46 static int write_one_condition  PARAMS ((PTR *, PTR));
47
48 extern int main                 PARAMS ((int, char **));
49
50 /* Record the C test expression EXPR in the condition_table.
51    Duplicates clobber previous entries, which leaks memory, but
52    we don't care for this application.  */
53
54 static void
55 add_condition (expr)
56      const char *expr;
57 {
58   struct c_test *test;
59
60   if (expr[0] == 0)
61     return;
62
63   test = (struct c_test *) xmalloc (sizeof (struct c_test));
64   test->expr = expr;
65
66   *(htab_find_slot (condition_table, test, INSERT)) = test;
67 }
68
69 /* Generate the header for insn-conditions.c.  */
70
71 static void
72 write_header ()
73 {
74   puts ("\
75 /* Generated automatically by the program `genconditions' from the target\n\
76    machine description file.  */\n\
77 \n\
78 #include \"bconfig.h\"\n\
79 #include \"insn-constants.h\"\n");
80
81   puts ("\
82 /* Do not allow checking to confuse the issue.  */\n\
83 #undef ENABLE_CHECKING\n\
84 #undef ENABLE_TREE_CHECKING\n\
85 #undef ENABLE_RTL_CHECKING\n\
86 #undef ENABLE_RTL_FLAG_CHECKING\n\
87 #undef ENABLE_GC_CHECKING\n\
88 #undef ENABLE_GC_ALWAYS_COLLECT\n");
89
90   puts ("\
91 #include \"system.h\"\n\
92 #include \"coretypes.h\"\n\
93 #include \"tm.h\"\n\
94 #include \"rtl.h\"\n\
95 #include \"tm_p.h\"\n\
96 #include \"function.h\"\n");
97
98   puts ("\
99 /* Fake - insn-config.h doesn't exist yet.  */\n\
100 #define MAX_RECOG_OPERANDS 10\n\
101 #define MAX_DUP_OPERANDS 10\n\
102 #define MAX_INSNS_PER_SPLIT 5\n");
103
104   puts ("\
105 #include \"regs.h\"\n\
106 #include \"recog.h\"\n\
107 #include \"real.h\"\n\
108 #include \"output.h\"\n\
109 #include \"flags.h\"\n\
110 #include \"hard-reg-set.h\"\n\
111 #include \"resource.h\"\n\
112 #include \"toplev.h\"\n\
113 #include \"reload.h\"\n\
114 #include \"gensupport.h\"\n");
115
116   if (saw_eh_return)
117     puts ("#define HAVE_eh_return 1");
118   puts ("#include \"except.h\"\n");
119
120   puts ("\
121 /* Dummy external declarations.  */\n\
122 extern rtx insn;\n\
123 extern rtx ins1;\n\
124 extern rtx operands[];\n\
125 extern int next_insn_tests_no_inequality PARAMS ((rtx));\n");
126
127   puts ("\
128 /* If we don't have __builtin_constant_p, or it's not acceptable in\n\
129    array initializers, fall back to assuming that all conditions\n\
130    potentially vary at run time.  It works in 3.0.1 and later; 3.0\n\
131    only when not optimizing.  */\n\
132 #if (GCC_VERSION >= 3001) || ((GCC_VERSION == 3000) && !__OPTIMIZE__)\n\
133 # define MAYBE_EVAL(expr) (__builtin_constant_p(expr) ? (int) (expr) : -1)\n\
134 #else\n\
135 # define MAYBE_EVAL(expr) -1\n\
136 #endif\n");
137 }
138
139 /* Write out one entry in the conditions table, using the data pointed
140    to by SLOT.  Each entry looks like this:
141   { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
142     MAYBE_EVAL (! optimize_size && ! TARGET_READ_MODIFY_WRITE) },  */
143
144 static int
145 write_one_condition (slot, dummy)
146      PTR *slot;
147      PTR dummy ATTRIBUTE_UNUSED;
148 {
149   const struct c_test *test = * (const struct c_test **) slot;
150   const char *p;
151
152   fputs ("  { \"", stdout);
153   for (p = test->expr; *p; p++)
154     {
155       if (*p == '\n')
156         fputs ("\\n\\\n", stdout);
157       else if (*p == '"')
158         fputs ("\\\"", stdout);
159       else
160         putchar (*p);
161     }
162
163   printf ("\",\n    MAYBE_EVAL (%s) },\n", test->expr);
164   return 1;
165 }
166
167 /* Write out the complete conditions table, its size, and a flag
168    indicating that gensupport.c can now do insn elision.  */
169 static void
170 write_conditions ()
171 {
172   puts ("\
173 /* This table lists each condition found in the machine description.\n\
174    Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
175    cannot be calculated at compile time. */\n\
176 \n\
177 const struct c_test insn_conditions[] = {");
178
179   htab_traverse (condition_table, write_one_condition, 0);
180
181   puts ("};\n");
182
183   printf ("const size_t n_insn_conditions = %lu;\n",
184           (unsigned long) htab_elements (condition_table));
185   puts ("const int insn_elision_unavailable = 0;");
186 }
187
188 int
189 main (argc, argv)
190      int argc;
191      char **argv;
192 {
193   rtx desc;
194   int pattern_lineno; /* not used */
195   int code;
196
197   progname = "genconditions";
198
199   if (argc <= 1)
200     fatal ("No input file name.");
201
202   if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
203     return (FATAL_EXIT_CODE);
204
205   condition_table = htab_create (1000, hash_c_test, cmp_c_test, NULL);
206
207   /* Read the machine description.  */
208
209   while (1)
210     {
211       desc = read_md_rtx (&pattern_lineno, &code);
212       if (desc == NULL)
213         break;
214
215       /* N.B. define_insn_and_split, define_cond_exec are handled
216          entirely within read_md_rtx; we never see them.  */
217       switch (GET_CODE (desc))
218         {
219         default:
220           break;
221
222         case DEFINE_INSN:
223         case DEFINE_EXPAND:
224           add_condition (XSTR (desc, 2));
225           /* except.h needs to know whether there is an eh_return
226              pattern in the machine description.  */
227           if (!strcmp (XSTR (desc, 0), "eh_return"))
228             saw_eh_return = 1;
229           break;
230
231         case DEFINE_SPLIT:
232         case DEFINE_PEEPHOLE:
233         case DEFINE_PEEPHOLE2:
234           add_condition (XSTR (desc, 1));
235           break;
236         }
237     }
238
239   write_header ();
240   write_conditions ();
241
242   fflush (stdout);
243   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
244 }