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