function.h: Flatten file.
[platform/upstream/gcc.git] / gcc / genconditions.c
1 /* Process machine description and calculate constant conditions.
2    Copyright (C) 2001-2014 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 3, 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 COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20 /* In a machine description, all of the insn patterns - define_insn,
21    define_expand, define_split, define_peephole, define_peephole2 -
22    contain an optional C expression which makes the final decision
23    about whether or not this pattern is usable.  That expression may
24    turn out to be always false when the compiler is built.  If it is,
25    most of the programs that generate code from the machine
26    description can simply ignore the entire pattern.  */
27
28 #include "bconfig.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "errors.h"
34 #include "hashtab.h"
35 #include "read-md.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 \"ggc.h\"\n\
74 #include \"rtl.h\"\n\
75 #include \"tm_p.h\"\n\
76 #include \"hashtab.h\"\n\
77 #include \"hash-set.h\"\n\
78 #include \"hard-reg-set.h\"\n\
79 #include \"function.h\"\n\
80 \n\
81 /* Fake - insn-config.h doesn't exist yet.  */\n\
82 #define MAX_RECOG_OPERANDS 10\n\
83 #define MAX_DUP_OPERANDS 10\n\
84 #define MAX_INSNS_PER_SPLIT 5\n\
85 \n\
86 #include \"regs.h\"\n\
87 #include \"recog.h\"\n\
88 #include \"output.h\"\n\
89 #include \"flags.h\"\n\
90 #include \"hard-reg-set.h\"\n\
91 #include \"resource.h\"\n\
92 #include \"diagnostic-core.h\"\n\
93 #include \"reload.h\"\n\
94 #include \"tm-constrs.h\"\n");
95
96   if (saw_eh_return)
97     puts ("#define HAVE_eh_return 1");
98   puts ("#include \"except.h\"\n");
99
100   puts ("\
101 /* Dummy external declarations.  */\n\
102 extern rtx_insn *insn;\n\
103 extern rtx ins1;\n\
104 extern rtx operands[];\n\
105 \n\
106 #endif /* gcc >= 3.0.1 */\n");
107 }
108
109 /* Write out one entry in the conditions table, using the data pointed
110    to by SLOT.  Each entry looks like this:
111
112    { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
113      __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
114      ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
115      : -1) },  */
116
117 static int
118 write_one_condition (void **slot, void * ARG_UNUSED (dummy))
119 {
120   const struct c_test *test = * (const struct c_test **) slot;
121   const char *p;
122
123   print_md_ptr_loc (test->expr);
124   fputs ("  { \"", stdout);
125   for (p = test->expr; *p; p++)
126     {
127       switch (*p)
128         {
129         case '\n': fputs ("\\n\\", stdout); break;
130         case '\\':
131         case '\"': putchar ('\\'); break;
132         default: break;
133         }
134       putchar (*p);
135     }
136
137   fputs ("\",\n    __builtin_constant_p ", stdout);
138   print_c_condition (test->expr);
139   fputs ("\n    ? (int) ", stdout);
140   print_c_condition (test->expr);
141   fputs ("\n    : -1 },\n", stdout);
142   return 1;
143 }
144
145 /* Write out the complete conditions table, its size, and a flag
146    indicating that gensupport.c can now do insn elision.  */
147 static void
148 write_conditions (void)
149 {
150   puts ("\
151 /* Structure definition duplicated from gensupport.h rather than\n\
152    drag in that file and its dependencies.  */\n\
153 struct c_test\n\
154 {\n\
155   const char *expr;\n\
156   int value;\n\
157 };\n\
158 \n\
159 /* This table lists each condition found in the machine description.\n\
160    Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
161    cannot be calculated at compile time.\n\
162    If we don't have __builtin_constant_p, or it's not acceptable in array\n\
163    initializers, fall back to assuming that all conditions potentially\n\
164    vary at run time.  It works in 3.0.1 and later; 3.0 only when not\n\
165    optimizing.  */\n\
166 \n\
167 #if GCC_VERSION >= 3001\n\
168 static const struct c_test insn_conditions[] = {\n");
169
170   traverse_c_tests (write_one_condition, 0);
171
172   puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
173 }
174
175 /* Emit code which will convert the C-format table to a
176    (define_conditions) form, which the MD reader can understand.
177    The result will be added to the set of files scanned by
178    'downstream' generators.  */
179 static void
180 write_writer (void)
181 {
182   puts ("int\n"
183         "main(void)\n"
184         "{\n"
185         "  unsigned int i;\n"
186         "  const char *p;\n"
187         "  puts (\"(define_conditions [\");\n"
188         "#if GCC_VERSION >= 3001\n"
189         "  for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
190         "    {\n"
191         "      printf (\"  (%d \\\"\", insn_conditions[i].value);\n"
192         "      for (p = insn_conditions[i].expr; *p; p++)\n"
193         "        {\n"
194         "          switch (*p)\n"
195         "            {\n"
196         "            case '\\\\':\n"
197         "            case '\\\"': putchar ('\\\\'); break;\n"
198         "            default: break;\n"
199         "            }\n"
200         "          putchar (*p);\n"
201         "        }\n"
202         "      puts (\"\\\")\");\n"
203         "    }\n"
204         "#endif /* gcc >= 3.0.1 */\n"
205         "  puts (\"])\");\n"
206         "  fflush (stdout);\n"
207         "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
208         "}");
209 }
210
211 int
212 main (int argc, char **argv)
213 {
214   rtx desc;
215   int pattern_lineno; /* not used */
216   int code;
217
218   progname = "genconditions";
219
220   if (!init_rtx_reader_args (argc, argv))
221     return (FATAL_EXIT_CODE);
222
223   /* Read the machine description.  */
224   while (1)
225     {
226       desc = read_md_rtx (&pattern_lineno, &code);
227       if (desc == NULL)
228         break;
229
230       /* N.B. define_insn_and_split, define_cond_exec are handled
231          entirely within read_md_rtx; we never see them.  */
232       switch (GET_CODE (desc))
233         {
234         default:
235           break;
236
237         case DEFINE_INSN:
238         case DEFINE_EXPAND:
239           add_c_test (XSTR (desc, 2), -1);
240           /* except.h needs to know whether there is an eh_return
241              pattern in the machine description.  */
242           if (!strcmp (XSTR (desc, 0), "eh_return"))
243             saw_eh_return = 1;
244           break;
245
246         case DEFINE_SPLIT:
247         case DEFINE_PEEPHOLE:
248         case DEFINE_PEEPHOLE2:
249           add_c_test (XSTR (desc, 1), -1);
250           break;
251         }
252     }
253
254   write_header ();
255   write_conditions ();
256   write_writer ();
257
258   fflush (stdout);
259   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
260 }