1/* Process machine description and calculate constant conditions.
2   Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor,
19   Boston, MA 02110-1301, 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.  */
39static int saw_eh_return;
40
41static htab_t condition_table;
42
43static void add_condition	(const char *);
44static void write_header	(void);
45static void write_conditions	(void);
46static int write_one_condition	(void **, void *);
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
52static void
53add_condition (const char *expr)
54{
55  struct c_test *test;
56
57  if (expr[0] == 0)
58    return;
59
60  test = XNEW (struct c_test);
61  test->expr = expr;
62
63  *(htab_find_slot (condition_table, test, INSERT)) = test;
64}
65
66/* Generate the header for insn-conditions.c.  */
67
68static void
69write_header (void)
70{
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");
77
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");
86
87  puts ("\
88#include \"system.h\"\n\
89/* If we don't have __builtin_constant_p, or it's not acceptable in array\n\
90   initializers, fall back to assuming that all conditions potentially\n\
91   vary at run time.  It works in 3.0.1 and later; 3.0 only when not\n\
92   optimizing.  */\n\
93#if GCC_VERSION < 3001\n\
94#include \"dummy-conditions.c\"\n\
95#else\n\
96#include \"coretypes.h\"\n\
97#include \"tm.h\"\n\
98#include \"rtl.h\"\n\
99#include \"tm_p.h\"\n\
100#include \"function.h\"\n");
101
102  puts ("\
103/* Fake - insn-config.h doesn't exist yet.  */\n\
104#define MAX_RECOG_OPERANDS 10\n\
105#define MAX_DUP_OPERANDS 10\n\
106#define MAX_INSNS_PER_SPLIT 5\n");
107
108  puts ("\
109#include \"regs.h\"\n\
110#include \"recog.h\"\n\
111#include \"real.h\"\n\
112#include \"output.h\"\n\
113#include \"flags.h\"\n\
114#include \"hard-reg-set.h\"\n\
115#include \"resource.h\"\n\
116#include \"toplev.h\"\n\
117#include \"reload.h\"\n\
118#include \"gensupport.h\"\n");
119
120  if (saw_eh_return)
121    puts ("#define HAVE_eh_return 1");
122  puts ("#include \"except.h\"\n");
123
124  puts ("\
125/* Dummy external declarations.  */\n\
126extern rtx insn;\n\
127extern rtx ins1;\n\
128extern rtx operands[];\n");
129}
130
131/* Write out one entry in the conditions table, using the data pointed
132   to by SLOT.  Each entry looks like this:
133
134   { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
135     __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
136     ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
137     : -1) },  */
138
139static int
140write_one_condition (void **slot, void * ARG_UNUSED (dummy))
141{
142  const struct c_test *test = * (const struct c_test **) slot;
143  const char *p;
144
145  print_rtx_ptr_loc (test->expr);
146  fputs ("  { \"", stdout);
147  for (p = test->expr; *p; p++)
148    {
149      if (*p == '\n')
150	fputs ("\\n\\\n", stdout);
151      else if (*p == '"')
152	fputs ("\\\"", stdout);
153      else
154	putchar (*p);
155    }
156
157  printf ("\",\n    __builtin_constant_p ");
158  print_c_condition (test->expr);
159  printf ("\n    ? (int) ");
160  print_c_condition (test->expr);
161  printf ("\n    : -1 },\n");
162  return 1;
163}
164
165/* Write out the complete conditions table, its size, and a flag
166   indicating that gensupport.c can now do insn elision.  */
167static void
168write_conditions (void)
169{
170  puts ("\
171/* This table lists each condition found in the machine description.\n\
172   Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
173   cannot be calculated at compile time. */\n\
174\n\
175const struct c_test insn_conditions[] = {");
176
177  htab_traverse (condition_table, write_one_condition, 0);
178
179  puts ("};\n");
180
181  printf ("const size_t n_insn_conditions = %lu;\n",
182	  (unsigned long) htab_elements (condition_table));
183  puts ("const int insn_elision_unavailable = 0;\n#endif");
184}
185
186int
187main (int argc, char **argv)
188{
189  rtx desc;
190  int pattern_lineno; /* not used */
191  int code;
192
193  progname = "genconditions";
194
195  if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
196    return (FATAL_EXIT_CODE);
197
198  condition_table = htab_create (1000, hash_c_test, cmp_c_test, NULL);
199
200  /* Read the machine description.  */
201
202  while (1)
203    {
204      desc = read_md_rtx (&pattern_lineno, &code);
205      if (desc == NULL)
206	break;
207
208      /* N.B. define_insn_and_split, define_cond_exec are handled
209	 entirely within read_md_rtx; we never see them.  */
210      switch (GET_CODE (desc))
211	{
212	default:
213	  break;
214
215	case DEFINE_INSN:
216	case DEFINE_EXPAND:
217	  add_condition (XSTR (desc, 2));
218	  /* except.h needs to know whether there is an eh_return
219	     pattern in the machine description.  */
220	  if (!strcmp (XSTR (desc, 0), "eh_return"))
221	    saw_eh_return = 1;
222	  break;
223
224	case DEFINE_SPLIT:
225	case DEFINE_PEEPHOLE:
226	case DEFINE_PEEPHOLE2:
227	  add_condition (XSTR (desc, 1));
228	  break;
229	}
230    }
231
232  write_header ();
233  write_conditions ();
234
235  fflush (stdout);
236  return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
237}
238