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