xref: /openbsd/gnu/usr.bin/gcc/gcc/genconditions.c (revision c87b03e5)
1 /* Process machine description and calculate constant conditions.
2    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3 
4    This file is part of GNU CC.
5 
6    GNU CC 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    GNU CC 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 GNU CC; 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 "hconfig.h"
30 #include "system.h"
31 #include "rtl.h"
32 #include "errors.h"
33 #include "hashtab.h"
34 #include "gensupport.h"
35 
36 /* so we can include except.h in the generated file */
37 static int saw_eh_return;
38 
39 static htab_t condition_table;
40 
41 static void add_condition	PARAMS ((const char *));
42 static void write_header	PARAMS ((void));
43 static void write_conditions	PARAMS ((void));
44 static int write_one_condition	PARAMS ((PTR *, PTR));
45 
46 extern int main			PARAMS ((int, char **));
47 
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.  */
51 
52 static void
add_condition(expr)53 add_condition (expr)
54      const char *expr;
55 {
56   struct c_test *test;
57 
58   if (expr[0] == 0)
59     return;
60 
61   test = (struct c_test *) xmalloc (sizeof (struct c_test));
62   test->expr = expr;
63 
64   *(htab_find_slot (condition_table, test, INSERT)) = test;
65 }
66 
67 /* Generate the header for insn-conditions.c.  */
68 
69 static void
write_header()70 write_header ()
71 {
72   puts ("\
73 /* Generated automatically by the program `genconditions' from the target\n\
74    machine description file.  */\n\
75 \n\
76 #include \"hconfig.h\"\n\
77 #include \"insn-constants.h\"\n");
78 
79   puts ("\
80 /* Do not allow checking to confuse the issue.  */\n\
81 #undef ENABLE_CHECKING\n\
82 #undef ENABLE_TREE_CHECKING\n\
83 #undef ENABLE_RTL_CHECKING\n\
84 #undef ENABLE_RTL_FLAG_CHECKING\n\
85 #undef ENABLE_GC_CHECKING\n\
86 #undef ENABLE_GC_ALWAYS_COLLECT\n");
87 
88   puts ("\
89 #include \"system.h\"\n\
90 #include \"rtl.h\"\n\
91 #include \"tm_p.h\"\n\
92 #include \"function.h\"\n");
93 
94   puts ("\
95 /* Fake - insn-config.h doesn't exist yet.  */\n\
96 #define MAX_RECOG_OPERANDS 10\n\
97 #define MAX_DUP_OPERANDS 10\n\
98 #define MAX_INSNS_PER_SPLIT 5\n");
99 
100   puts ("\
101 #include \"regs.h\"\n\
102 #include \"recog.h\"\n\
103 #include \"real.h\"\n\
104 #include \"output.h\"\n\
105 #include \"flags.h\"\n\
106 #include \"hard-reg-set.h\"\n\
107 #include \"resource.h\"\n\
108 #include \"toplev.h\"\n\
109 #include \"reload.h\"\n\
110 #include \"gensupport.h\"\n");
111 
112   if (saw_eh_return)
113     puts ("#define HAVE_eh_return 1");
114   puts ("#include \"except.h\"\n");
115 
116   puts ("\
117 /* Dummy external declarations.  */\n\
118 extern rtx insn;\n\
119 extern rtx ins1;\n\
120 extern rtx operands[];\n\
121 extern int next_insn_tests_no_inequality PARAMS ((rtx));\n");
122 
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");
133 }
134 
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) },  */
139 
140 static int
write_one_condition(slot,dummy)141 write_one_condition (slot, dummy)
142      PTR *slot;
143      PTR dummy ATTRIBUTE_UNUSED;
144 {
145   const struct c_test *test = * (const struct c_test **) slot;
146   const char *p;
147 
148   fputs ("  { \"", stdout);
149   for (p = test->expr; *p; p++)
150     {
151       if (*p == '\n')
152 	fputs ("\\n\\\n", stdout);
153       else if (*p == '"')
154 	fputs ("\\\"", stdout);
155       else
156 	putchar (*p);
157     }
158 
159   printf ("\",\n    MAYBE_EVAL (%s) },\n", test->expr);
160   return 1;
161 }
162 
163 /* Write out the complete conditions table, its size, and a flag
164    indicating that gensupport.c can now do insn elision.  */
165 static void
write_conditions()166 write_conditions ()
167 {
168   puts ("\
169 /* This table lists each condition found in the machine description.\n\
170    Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
171    cannot be calculated at compile time. */\n\
172 \n\
173 const struct c_test insn_conditions[] = {");
174 
175   htab_traverse (condition_table, write_one_condition, 0);
176 
177   puts ("};\n");
178 
179   printf ("const size_t n_insn_conditions = %lu;\n",
180 	  (unsigned long) htab_elements (condition_table));
181   puts ("const int insn_elision_unavailable = 0;");
182 }
183 
184 int
main(argc,argv)185 main (argc, argv)
186      int argc;
187      char **argv;
188 {
189   rtx desc;
190   int pattern_lineno; /* not used */
191   int code;
192 
193   progname = "genconditions";
194 
195   if (argc <= 1)
196     fatal ("No input file name.");
197 
198   if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
199     return (FATAL_EXIT_CODE);
200 
201   condition_table = htab_create (1000, hash_c_test, cmp_c_test, NULL);
202 
203   /* Read the machine description.  */
204 
205   while (1)
206     {
207       desc = read_md_rtx (&pattern_lineno, &code);
208       if (desc == NULL)
209 	break;
210 
211       /* N.B. define_insn_and_split, define_cond_exec are handled
212 	 entirely within read_md_rtx; we never see them.  */
213       switch (GET_CODE (desc))
214 	{
215 	default:
216 	  break;
217 
218 	case DEFINE_INSN:
219 	case DEFINE_EXPAND:
220 	  add_condition (XSTR (desc, 2));
221 	  /* except.h needs to know whether there is an eh_return
222 	     pattern in the machine description.  */
223 	  if (!strcmp (XSTR (desc, 0), "eh_return"))
224 	    saw_eh_return = 1;
225 	  break;
226 
227 	case DEFINE_SPLIT:
228 	case DEFINE_PEEPHOLE:
229 	case DEFINE_PEEPHOLE2:
230 	  add_condition (XSTR (desc, 1));
231 	  break;
232 	}
233     }
234 
235   write_header ();
236   write_conditions ();
237 
238   fflush (stdout);
239   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
240 }
241