fold-const.c (fold_binary_loc): Move ~X | X folding ...
[gcc.git] / gcc / genextract.c
1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987-2015 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 it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 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
21 #include "bconfig.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "errors.h"
27 #include "read-md.h"
28 #include "gensupport.h"
29
30 /* This structure contains all the information needed to describe one
31 set of extractions methods. Each method may be used by more than
32 one pattern if the operands are in the same place.
33
34 The string for each operand describes that path to the operand and
35 contains `0' through `9' when going into an expression and `a' through
36 `z' when going into a vector. We assume here that only the first operand
37 of an rtl expression is a vector. genrecog.c makes the same assumption
38 (and uses the same representation) and it is currently true. */
39
40 typedef char *locstr;
41
42 struct extraction
43 {
44 unsigned int op_count;
45 unsigned int dup_count;
46 locstr *oplocs;
47 locstr *duplocs;
48 int *dupnums;
49 struct code_ptr *insns;
50 struct extraction *next;
51 };
52
53 /* Holds a single insn code that uses an extraction method. */
54 struct code_ptr
55 {
56 int insn_code;
57 struct code_ptr *next;
58 };
59
60 /* All extractions needed for this machine description. */
61 static struct extraction *extractions;
62
63 /* All insn codes for old-style peepholes. */
64 static struct code_ptr *peepholes;
65
66 /* This structure is used by gen_insn and walk_rtx to accumulate the
67 data that will be used to produce an extractions structure. */
68
69
70 struct accum_extract
71 {
72 vec<locstr> oplocs;
73 vec<locstr> duplocs;
74 vec<int> dupnums;
75 vec<char> pathstr;
76 };
77
78 int line_no;
79
80 /* Forward declarations. */
81 static void walk_rtx (rtx, struct accum_extract *);
82
83 static void
84 gen_insn (rtx insn, int insn_code_number)
85 {
86 int i;
87 unsigned int op_count, dup_count, j;
88 struct extraction *p;
89 struct code_ptr *link;
90 struct accum_extract acc;
91
92 acc.oplocs.create (10);
93 acc.duplocs.create (10);
94 acc.dupnums.create (10);
95 acc.pathstr.create (20);
96
97 /* Walk the insn's pattern, remembering at all times the path
98 down to the walking point. */
99
100 if (XVECLEN (insn, 1) == 1)
101 walk_rtx (XVECEXP (insn, 1, 0), &acc);
102 else
103 for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
104 {
105 acc.pathstr.safe_push ('a' + i);
106 walk_rtx (XVECEXP (insn, 1, i), &acc);
107 acc.pathstr.pop ();
108 }
109
110 link = XNEW (struct code_ptr);
111 link->insn_code = insn_code_number;
112
113 /* See if we find something that already had this extraction method. */
114
115 op_count = acc.oplocs.length ();
116 dup_count = acc.duplocs.length ();
117 gcc_assert (dup_count == acc.dupnums.length ());
118
119 for (p = extractions; p; p = p->next)
120 {
121 if (p->op_count != op_count || p->dup_count != dup_count)
122 continue;
123
124 for (j = 0; j < op_count; j++)
125 {
126 char *a = p->oplocs[j];
127 char *b = acc.oplocs[j];
128 if (a != b && (!a || !b || strcmp (a, b)))
129 break;
130 }
131
132 if (j != op_count)
133 continue;
134
135 for (j = 0; j < dup_count; j++)
136 if (p->dupnums[j] != acc.dupnums[j]
137 || strcmp (p->duplocs[j], acc.duplocs[j]))
138 break;
139
140 if (j != dup_count)
141 continue;
142
143 /* This extraction is the same as ours. Just link us in. */
144 link->next = p->insns;
145 p->insns = link;
146 goto done;
147 }
148
149 /* Otherwise, make a new extraction method. We stash the arrays
150 after the extraction structure in memory. */
151
152 p = XNEWVAR (struct extraction, sizeof (struct extraction)
153 + op_count*sizeof (char *)
154 + dup_count*sizeof (char *)
155 + dup_count*sizeof (int));
156 p->op_count = op_count;
157 p->dup_count = dup_count;
158 p->next = extractions;
159 extractions = p;
160 p->insns = link;
161 link->next = 0;
162
163 p->oplocs = (char **)((char *)p + sizeof (struct extraction));
164 p->duplocs = p->oplocs + op_count;
165 p->dupnums = (int *)(p->duplocs + dup_count);
166
167 memcpy (p->oplocs, acc.oplocs.address (), op_count * sizeof (locstr));
168 memcpy (p->duplocs, acc.duplocs.address (), dup_count * sizeof (locstr));
169 memcpy (p->dupnums, acc.dupnums.address (), dup_count * sizeof (int));
170
171 done:
172 acc.oplocs.release ();
173 acc.duplocs.release ();
174 acc.dupnums.release ();
175 acc.pathstr.release ();
176 }
177 \f
178 /* Helper subroutine of walk_rtx: given a vec<locstr>, an index, and a
179 string, insert the string at the index, which should either already
180 exist and be NULL, or not yet exist within the vector. In the latter
181 case the vector is enlarged as appropriate. */
182 static void
183 VEC_safe_set_locstr (vec<locstr> *vp, unsigned int ix, char *str)
184 {
185 if (ix < (*vp).length ())
186 {
187 if ((*vp)[ix])
188 {
189 message_with_line (line_no, "repeated operand number %d", ix);
190 have_error = 1;
191 }
192 else
193 (*vp)[ix] = str;
194 }
195 else
196 {
197 while (ix > (*vp).length ())
198 vp->safe_push (NULL);
199 vp->safe_push (str);
200 }
201 }
202
203 /* Another helper subroutine of walk_rtx: given a vec<char>, convert it
204 to a NUL-terminated string in malloc memory. */
205 static char *
206 VEC_char_to_string (vec<char> v)
207 {
208 size_t n = v.length ();
209 char *s = XNEWVEC (char, n + 1);
210 memcpy (s, v.address (), n);
211 s[n] = '\0';
212 return s;
213 }
214
215 static void
216 walk_rtx (rtx x, struct accum_extract *acc)
217 {
218 RTX_CODE code;
219 int i, len, base;
220 const char *fmt;
221
222 if (x == 0)
223 return;
224
225 code = GET_CODE (x);
226 switch (code)
227 {
228 case PC:
229 case CC0:
230 case CONST_INT:
231 case SYMBOL_REF:
232 return;
233
234 case MATCH_OPERAND:
235 case MATCH_SCRATCH:
236 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
237 VEC_char_to_string (acc->pathstr));
238 break;
239
240 case MATCH_OPERATOR:
241 case MATCH_PARALLEL:
242 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
243 VEC_char_to_string (acc->pathstr));
244
245 base = (code == MATCH_OPERATOR ? '0' : 'a');
246 for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
247 {
248 acc->pathstr.safe_push (base + i);
249 walk_rtx (XVECEXP (x, 2, i), acc);
250 acc->pathstr.pop ();
251 }
252 return;
253
254 case MATCH_DUP:
255 case MATCH_PAR_DUP:
256 case MATCH_OP_DUP:
257 acc->duplocs.safe_push (VEC_char_to_string (acc->pathstr));
258 acc->dupnums.safe_push (XINT (x, 0));
259
260 if (code == MATCH_DUP)
261 break;
262
263 base = (code == MATCH_OP_DUP ? '0' : 'a');
264 for (i = XVECLEN (x, 1) - 1; i >= 0; i--)
265 {
266 acc->pathstr.safe_push (base + i);
267 walk_rtx (XVECEXP (x, 1, i), acc);
268 acc->pathstr.pop ();
269 }
270 return;
271
272 default:
273 break;
274 }
275
276 fmt = GET_RTX_FORMAT (code);
277 len = GET_RTX_LENGTH (code);
278 for (i = 0; i < len; i++)
279 {
280 if (fmt[i] == 'e' || fmt[i] == 'u')
281 {
282 acc->pathstr.safe_push ('0' + i);
283 walk_rtx (XEXP (x, i), acc);
284 acc->pathstr.pop ();
285 }
286 else if (fmt[i] == 'E')
287 {
288 int j;
289 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
290 {
291 acc->pathstr.safe_push ('a' + j);
292 walk_rtx (XVECEXP (x, i, j), acc);
293 acc->pathstr.pop ();
294 }
295 }
296 }
297 }
298
299 /* Given a PATH, representing a path down the instruction's
300 pattern from the root to a certain point, output code to
301 evaluate to the rtx at that point. */
302
303 static void
304 print_path (const char *path)
305 {
306 int len = strlen (path);
307 int i;
308
309 if (len == 0)
310 {
311 /* Don't emit "pat", since we may try to take the address of it,
312 which isn't what is intended. */
313 fputs ("PATTERN (insn)", stdout);
314 return;
315 }
316
317 /* We first write out the operations (XEXP or XVECEXP) in reverse
318 order, then write "pat", then the indices in forward order. */
319
320 for (i = len - 1; i >= 0 ; i--)
321 {
322 if (ISLOWER (path[i]))
323 fputs ("XVECEXP (", stdout);
324 else if (ISDIGIT (path[i]))
325 fputs ("XEXP (", stdout);
326 else
327 gcc_unreachable ();
328 }
329
330 fputs ("pat", stdout);
331
332 for (i = 0; i < len; i++)
333 {
334 if (ISLOWER (path[i]))
335 printf (", 0, %d)", path[i] - 'a');
336 else if (ISDIGIT (path[i]))
337 printf (", %d)", path[i] - '0');
338 else
339 gcc_unreachable ();
340 }
341 }
342 \f
343 static void
344 print_header (void)
345 {
346 /* N.B. Code below avoids putting squiggle braces in column 1 inside
347 a string, because this confuses some editors' syntax highlighting
348 engines. */
349
350 puts ("\
351 /* Generated automatically by the program `genextract'\n\
352 from the machine description file `md'. */\n\
353 \n\
354 #include \"config.h\"\n\
355 #include \"system.h\"\n\
356 #include \"coretypes.h\"\n\
357 #include \"tm.h\"\n\
358 #include \"rtl.h\"\n\
359 #include \"insn-config.h\"\n\
360 #include \"recog.h\"\n\
361 #include \"diagnostic-core.h\"\n\
362 \n\
363 /* This variable is used as the \"location\" of any missing operand\n\
364 whose numbers are skipped by a given pattern. */\n\
365 static rtx junk ATTRIBUTE_UNUSED;\n");
366
367 puts ("\
368 void\n\
369 insn_extract (rtx_insn *insn)\n{\n\
370 rtx *ro = recog_data.operand;\n\
371 rtx **ro_loc = recog_data.operand_loc;\n\
372 rtx pat = PATTERN (insn);\n\
373 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
374 \n\
375 #ifdef ENABLE_CHECKING\n\
376 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
377 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
378 #endif\n");
379
380 puts ("\
381 switch (INSN_CODE (insn))\n\
382 {\n\
383 default:\n\
384 /* Control reaches here if insn_extract has been called with an\n\
385 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
386 corresponds to a DEFINE_EXPAND in the machine description;\n\
387 either way, a bug. */\n\
388 if (INSN_CODE (insn) < 0)\n\
389 fatal_insn (\"unrecognizable insn:\", insn);\n\
390 else\n\
391 fatal_insn (\"insn with invalid code number:\", insn);\n");
392 }
393
394 int
395 main (int argc, char **argv)
396 {
397 rtx desc;
398 unsigned int i;
399 struct extraction *p;
400 struct code_ptr *link;
401 const char *name;
402 int insn_code_number;
403
404 progname = "genextract";
405
406 if (!init_rtx_reader_args (argc, argv))
407 return (FATAL_EXIT_CODE);
408
409 /* Read the machine description. */
410
411 while ((desc = read_md_rtx (&line_no, &insn_code_number)) != NULL)
412 {
413 if (GET_CODE (desc) == DEFINE_INSN)
414 gen_insn (desc, insn_code_number);
415
416 else if (GET_CODE (desc) == DEFINE_PEEPHOLE)
417 {
418 struct code_ptr *link = XNEW (struct code_ptr);
419
420 link->insn_code = insn_code_number;
421 link->next = peepholes;
422 peepholes = link;
423 }
424 }
425
426 if (have_error)
427 return FATAL_EXIT_CODE;
428
429 print_header ();
430
431 /* Write out code to handle peepholes and the insn_codes that it should
432 be called for. */
433 if (peepholes)
434 {
435 for (link = peepholes; link; link = link->next)
436 printf (" case %d:\n", link->insn_code);
437
438 /* The vector in the insn says how many operands it has.
439 And all it contains are operands. In fact, the vector was
440 created just for the sake of this function. We need to set the
441 location of the operands for sake of simplifications after
442 extraction, like eliminating subregs. */
443 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
444 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
445 " break;\n");
446 }
447
448 /* Write out all the ways to extract insn operands. */
449 for (p = extractions; p; p = p->next)
450 {
451 for (link = p->insns; link; link = link->next)
452 {
453 i = link->insn_code;
454 name = get_insn_name (i);
455 if (name)
456 printf (" case %d: /* %s */\n", i, name);
457 else
458 printf (" case %d:\n", i);
459 }
460
461 for (i = 0; i < p->op_count; i++)
462 {
463 if (p->oplocs[i] == 0)
464 {
465 printf (" ro[%d] = const0_rtx;\n", i);
466 printf (" ro_loc[%d] = &junk;\n", i);
467 }
468 else
469 {
470 printf (" ro[%d] = *(ro_loc[%d] = &", i, i);
471 print_path (p->oplocs[i]);
472 puts (");");
473 }
474 }
475
476 for (i = 0; i < p->dup_count; i++)
477 {
478 printf (" recog_data.dup_loc[%d] = &", i);
479 print_path (p->duplocs[i]);
480 puts (";");
481 printf (" recog_data.dup_num[%d] = %d;\n", i, p->dupnums[i]);
482 }
483
484 puts (" break;\n");
485 }
486
487 puts (" }\n}");
488 fflush (stdout);
489 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
490 }