]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genemit.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[thirdparty/gcc.git] / gcc / genemit.c
CommitLineData
8d365e72 1/* Generate code from machine description to emit insns as rtl.
8e8f6434 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
8d365e72 3
f12b58b3 4This file is part of GCC.
8d365e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
8d365e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
8d365e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
8d365e72 19
20
805e22b2 21#include "bconfig.h"
5ce88198 22#include "system.h"
805e22b2 23#include "coretypes.h"
24#include "tm.h"
8d365e72 25#include "rtl.h"
04b58880 26#include "errors.h"
960ebfe7 27#include "read-md.h"
c5ddd6b5 28#include "gensupport.h"
8d365e72 29
8d365e72 30
8d365e72 31/* Data structure for recording the patterns of insns that have CLOBBERs.
1a97be37 32 We use this to output a function that adds these CLOBBERs to a
8d365e72 33 previously-allocated PARALLEL expression. */
34
35struct clobber_pat
36{
c39100fe 37 struct clobber_ent *insns;
8d365e72 38 rtx pattern;
39 int first_clobber;
40 struct clobber_pat *next;
388df988 41 int has_hard_reg;
8d365e72 42} *clobber_list;
43
c39100fe 44/* Records one insn that uses the clobber list. */
45
46struct clobber_ent
47{
48 int code_number; /* Counts only insns. */
49 struct clobber_ent *next;
50};
51
1a97be37 52static void output_peephole2_scratches (rtx);
13de78c5 53
592e7d69 54/* True for <X>_optab if that optab isn't allowed to fail. */
55static bool nofail_optabs[NUM_OPTABS];
8d365e72 56\f
57static void
1a97be37 58print_code (RTX_CODE code)
8d365e72 59{
19cb6b50 60 const char *p1;
8d365e72 61 for (p1 = GET_RTX_NAME (code); *p1; p1++)
9af5ce0c 62 putchar (TOUPPER (*p1));
8d365e72 63}
64
82575fa7 65static void
1a97be37 66gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
82575fa7 67{
68 if (subroutine_type == DEFINE_PEEPHOLE2)
69 {
70 printf ("operand%d", XINT (x, 0));
71 }
72 else
73 {
74 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
75 }
76}
77
8d365e72 78/* Print a C expression to construct an RTX just like X,
79 substituting any operand references appearing within. */
80
81static void
ccd6679f 82gen_exp (rtx x, enum rtx_code subroutine_type, char *used, md_rtx_info *info)
8d365e72 83{
19cb6b50 84 RTX_CODE code;
85 int i;
86 int len;
87 const char *fmt;
d1f9b275 88 const char *sep = "";
8d365e72 89
90 if (x == 0)
91 {
9259c67f 92 printf ("NULL_RTX");
8d365e72 93 return;
94 }
95
96 code = GET_CODE (x);
97
98 switch (code)
99 {
100 case MATCH_OPERAND:
101 case MATCH_DUP:
eeddd58f 102 if (used)
103 {
104 if (used[XINT (x, 0)])
105 {
106 printf ("copy_rtx (operand%d)", XINT (x, 0));
107 return;
108 }
109 used[XINT (x, 0)] = 1;
110 }
8d365e72 111 printf ("operand%d", XINT (x, 0));
112 return;
113
114 case MATCH_OP_DUP:
8d7bd4fd 115 printf ("gen_rtx_fmt_");
116 for (i = 0; i < XVECLEN (x, 1); i++)
117 printf ("e");
118 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
904202c3 119 if (GET_MODE (x) == VOIDmode)
120 printf ("GET_MODE (operand%d)", XINT (x, 0));
121 else
122 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
8d365e72 123 for (i = 0; i < XVECLEN (x, 1); i++)
124 {
125 printf (",\n\t\t");
ccd6679f 126 gen_exp (XVECEXP (x, 1, i), subroutine_type, used, info);
8d365e72 127 }
128 printf (")");
129 return;
130
131 case MATCH_OPERATOR:
8d7bd4fd 132 printf ("gen_rtx_fmt_");
133 for (i = 0; i < XVECLEN (x, 2); i++)
134 printf ("e");
135 printf (" (GET_CODE (operand%d)", XINT (x, 0));
8d365e72 136 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
137 for (i = 0; i < XVECLEN (x, 2); i++)
138 {
139 printf (",\n\t\t");
ccd6679f 140 gen_exp (XVECEXP (x, 2, i), subroutine_type, used, info);
8d365e72 141 }
142 printf (")");
143 return;
144
145 case MATCH_PARALLEL:
aaaa1a6e 146 case MATCH_PAR_DUP:
8d365e72 147 printf ("operand%d", XINT (x, 0));
148 return;
149
150 case MATCH_SCRATCH:
82575fa7 151 gen_rtx_scratch (x, subroutine_type);
8d365e72 152 return;
153
8d365e72 154 case PC:
155 printf ("pc_rtx");
156 return;
1a860023 157 case RETURN:
158 printf ("ret_rtx");
159 return;
9cb2517e 160 case SIMPLE_RETURN:
161 printf ("simple_return_rtx");
162 return;
c09425a0 163 case CLOBBER:
164 if (REG_P (XEXP (x, 0)))
165 {
ccd6679f 166 printf ("gen_hard_reg_clobber (%smode, %i)",
167 GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
168 REGNO (XEXP (x, 0)));
c09425a0 169 return;
170 }
171 break;
ccd6679f 172 case CLOBBER_HIGH:
173 if (!REG_P (XEXP (x, 0)))
174 error ("CLOBBER_HIGH argument is not a register expr, at %s:%d",
175 info->loc.filename, info->loc.lineno);
176 printf ("gen_hard_reg_clobber_high (%smode, %i)",
177 GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
178 REGNO (XEXP (x, 0)));
179 return;
180 break;
8d365e72 181 case CC0:
182 printf ("cc0_rtx");
183 return;
184
185 case CONST_INT:
186 if (INTVAL (x) == 0)
9259c67f 187 printf ("const0_rtx");
188 else if (INTVAL (x) == 1)
189 printf ("const1_rtx");
190 else if (INTVAL (x) == -1)
191 printf ("constm1_rtx");
949168fb 192 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
ef17a71a 193 && INTVAL (x) <= MAX_SAVED_CONST_INT)
949168fb 194 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
195 (int) INTVAL (x));
c0999a5d 196 else if (INTVAL (x) == STORE_FLAG_VALUE)
9259c67f 197 printf ("const_true_rtx");
198 else
13a0a8d8 199 {
200 printf ("GEN_INT (");
fe0e3c2a 201 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
13a0a8d8 202 printf (")");
203 }
9259c67f 204 return;
205
206 case CONST_DOUBLE:
e397ad8e 207 case CONST_FIXED:
e913b5cd 208 case CONST_WIDE_INT:
9259c67f 209 /* These shouldn't be written in MD files. Instead, the appropriate
210 routines in varasm.c should be called. */
e0a4c0c2 211 gcc_unreachable ();
99c14947 212
213 default:
214 break;
8d365e72 215 }
216
3ad7bb1c 217 printf ("gen_rtx_");
8d365e72 218 print_code (code);
d1f9b275 219 printf (" (");
220 if (!always_void_p (code))
221 {
222 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
223 sep = ",\n\t";
224 }
8d365e72 225
226 fmt = GET_RTX_FORMAT (code);
227 len = GET_RTX_LENGTH (code);
228 for (i = 0; i < len; i++)
229 {
230 if (fmt[i] == '0')
231 break;
d1f9b275 232 fputs (sep, stdout);
e0a4c0c2 233 switch (fmt[i])
8d365e72 234 {
e0a4c0c2 235 case 'e': case 'u':
ccd6679f 236 gen_exp (XEXP (x, i), subroutine_type, used, info);
e0a4c0c2 237 break;
238
239 case 'i':
240 printf ("%u", XINT (x, i));
241 break;
242
15183fd2 243 case 'r':
244 printf ("%u", REGNO (x));
245 break;
246
9edf7ea8 247 case 'p':
248 /* We don't have a way of parsing polynomial offsets yet,
249 and hopefully never will. */
250 printf ("%d", SUBREG_BYTE (x).to_constant ());
251 break;
252
e0a4c0c2 253 case 's':
254 printf ("\"%s\"", XSTR (x, i));
255 break;
256
257 case 'E':
258 {
259 int j;
260 printf ("gen_rtvec (%d", XVECLEN (x, i));
261 for (j = 0; j < XVECLEN (x, i); j++)
262 {
263 printf (",\n\t\t");
ccd6679f 264 gen_exp (XVECEXP (x, i, j), subroutine_type, used, info);
e0a4c0c2 265 }
266 printf (")");
267 break;
268 }
269
270 default:
271 gcc_unreachable ();
8d365e72 272 }
d1f9b275 273 sep = ",\n\t";
8d365e72 274 }
275 printf (")");
1a97be37 276}
03acb861 277
278/* Output code to emit the instruction patterns in VEC, with each element
279 becoming a separate instruction. USED is as for gen_exp. */
280
281static void
ccd6679f 282gen_emit_seq (rtvec vec, char *used, md_rtx_info *info)
03acb861 283{
284 for (int i = 0, len = GET_NUM_ELEM (vec); i < len; ++i)
285 {
16d83c02 286 bool last_p = (i == len - 1);
03acb861 287 rtx next = RTVEC_ELT (vec, i);
16d83c02 288 if (const char *name = get_emit_function (next))
289 {
290 printf (" %s (", name);
ccd6679f 291 gen_exp (next, DEFINE_EXPAND, used, info);
16d83c02 292 printf (");\n");
293 if (!last_p && needs_barrier_p (next))
294 printf (" emit_barrier ();");
295 }
296 else
297 {
298 printf (" emit (");
ccd6679f 299 gen_exp (next, DEFINE_EXPAND, used, info);
16d83c02 300 printf (", %s);\n", last_p ? "false" : "true");
301 }
03acb861 302 }
303}
8d365e72 304\f
592e7d69 305/* Emit the given C code to the output file. The code is allowed to
306 fail if CAN_FAIL_P. NAME describes what we're generating,
307 for use in error messages. */
308
309static void
310emit_c_code (const char *code, bool can_fail_p, const char *name)
311{
312 if (can_fail_p)
313 printf ("#define FAIL return (end_sequence (), _val)\n");
314 else
315 printf ("#define FAIL _Pragma (\"GCC error \\\"%s cannot FAIL\\\"\")"
316 " (void)0\n", name);
317 printf ("#define DONE return (_val = get_insns (),"
318 "end_sequence (), _val)\n");
319
e1e9159b 320 rtx_reader_ptr->print_md_ptr_loc (code);
592e7d69 321 printf ("%s\n", code);
322
323 printf ("#undef DONE\n");
324 printf ("#undef FAIL\n");
325}
326\f
8d365e72 327/* Generate the `gen_...' function for a DEFINE_INSN. */
328
329static void
c04601c1 330gen_insn (md_rtx_info *info)
8d365e72 331{
22add764 332 struct pattern_stats stats;
19cb6b50 333 int i;
8d365e72 334
335 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
336 registers or MATCH_SCRATCHes. If so, store away the information for
a92771b8 337 later. */
8d365e72 338
c04601c1 339 rtx insn = info->def;
8d365e72 340 if (XVEC (insn, 1))
341 {
388df988 342 int has_hard_reg = 0;
343
8d365e72 344 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
388df988 345 {
ccd6679f 346 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
347 && GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER_HIGH)
388df988 348 break;
349
8ad4c111 350 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
388df988 351 has_hard_reg = 1;
352 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
353 break;
354 }
8d365e72 355
356 if (i != XVECLEN (insn, 1) - 1)
357 {
19cb6b50 358 struct clobber_pat *p;
4c36ffe6 359 struct clobber_ent *link = XNEW (struct clobber_ent);
19cb6b50 360 int j;
c39100fe 361
c04601c1 362 link->code_number = info->index;
c39100fe 363
364 /* See if any previous CLOBBER_LIST entry is the same as this
365 one. */
366
367 for (p = clobber_list; p; p = p->next)
368 {
369 if (p->first_clobber != i + 1
370 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
371 continue;
372
373 for (j = i + 1; j < XVECLEN (insn, 1); j++)
374 {
2657a7d5 375 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
376 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
c39100fe 377
2657a7d5 378 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
1a97be37 379 of the same mode,
c39100fe 380 or if both are registers of the same mode and number. */
ccd6679f 381 if (! (GET_CODE (old_rtx) == GET_CODE (new_rtx)
382 && GET_MODE (old_rtx) == GET_MODE (new_rtx)
2657a7d5 383 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
384 && GET_CODE (new_rtx) == MATCH_SCRATCH)
385 || (REG_P (old_rtx) && REG_P (new_rtx)
386 && REGNO (old_rtx) == REGNO (new_rtx)))))
c39100fe 387 break;
388 }
1a97be37 389
c39100fe 390 if (j == XVECLEN (insn, 1))
391 break;
392 }
393
394 if (p == 0)
395 {
4c36ffe6 396 p = XNEW (struct clobber_pat);
1a97be37 397
c39100fe 398 p->insns = 0;
399 p->pattern = insn;
400 p->first_clobber = i + 1;
401 p->next = clobber_list;
388df988 402 p->has_hard_reg = has_hard_reg;
c39100fe 403 clobber_list = p;
404 }
405
406 link->next = p->insns;
407 p->insns = link;
8d365e72 408 }
409 }
410
a0761610 411 /* Don't mention instructions whose names are the null string
412 or begin with '*'. They are in the machine description just
413 to be recognized. */
414 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
8d365e72 415 return;
416
c04601c1 417 printf ("/* %s:%d */\n", info->loc.filename, info->loc.lineno);
f1c96ec2 418
c18bfb9c 419 /* Find out how many operands this function has. */
22add764 420 get_pattern_stats (&stats, XVEC (insn, 1));
421 if (stats.max_dup_opno > stats.max_opno)
c04601c1 422 fatal_at (info->loc, "match_dup operand number has no match_operand");
8d365e72 423
424 /* Output the function name and argument declarations. */
425 printf ("rtx\ngen_%s (", XSTR (insn, 0));
22add764 426 if (stats.num_generator_args)
427 for (i = 0; i < stats.num_generator_args; i++)
69dc4d00 428 if (i)
429 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
430 else
431 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
432 else
433 printf ("void");
8d365e72 434 printf (")\n");
8d365e72 435 printf ("{\n");
436
8b332087 437 /* Output code to construct and return the rtl for the instruction body. */
8d365e72 438
2ff8d4ae 439 rtx pattern = add_implicit_parallel (XVEC (insn, 1));
440 /* ??? This is the traditional behavior, but seems suspect. */
441 char *used = (XVECLEN (insn, 1) == 1
442 ? NULL
443 : XCNEWVEC (char, stats.num_generator_args));
444 printf (" return ");
ccd6679f 445 gen_exp (pattern, DEFINE_INSN, used, info);
2ff8d4ae 446 printf (";\n}\n\n");
447 XDELETEVEC (used);
8d365e72 448}
449\f
450/* Generate the `gen_...' function for a DEFINE_EXPAND. */
451
452static void
c04601c1 453gen_expand (md_rtx_info *info)
8d365e72 454{
22add764 455 struct pattern_stats stats;
19cb6b50 456 int i;
778725bb 457 char *used;
8d365e72 458
c04601c1 459 rtx expand = info->def;
8d365e72 460 if (strlen (XSTR (expand, 0)) == 0)
c04601c1 461 fatal_at (info->loc, "define_expand lacks a name");
8d365e72 462 if (XVEC (expand, 1) == 0)
c04601c1 463 fatal_at (info->loc, "define_expand for %s lacks a pattern",
464 XSTR (expand, 0));
8d365e72 465
c18bfb9c 466 /* Find out how many operands this function has. */
22add764 467 get_pattern_stats (&stats, XVEC (expand, 1));
0f7b9610 468 if (stats.min_scratch_opno != -1
469 && stats.min_scratch_opno <= MAX (stats.max_opno, stats.max_dup_opno))
470 fatal_at (info->loc, "define_expand for %s needs to have match_scratch "
471 "numbers above all other operands", XSTR (expand, 0));
8d365e72 472
473 /* Output the function name and argument declarations. */
474 printf ("rtx\ngen_%s (", XSTR (expand, 0));
22add764 475 if (stats.num_generator_args)
476 for (i = 0; i < stats.num_generator_args; i++)
69dc4d00 477 if (i)
478 printf (",\n\trtx operand%d", i);
479 else
480 printf ("rtx operand%d", i);
481 else
482 printf ("void");
8d365e72 483 printf (")\n");
8d365e72 484 printf ("{\n");
485
486 /* If we don't have any C code to write, only one insn is being written,
487 and no MATCH_DUPs are present, we can just return the desired insn
488 like we do for a DEFINE_INSN. This saves memory. */
489 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
22add764 490 && stats.max_opno >= stats.max_dup_opno
8d365e72 491 && XVECLEN (expand, 1) == 1)
492 {
493 printf (" return ");
ccd6679f 494 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL, info);
8d365e72 495 printf (";\n}\n\n");
496 return;
497 }
498
499 /* For each operand referred to only with MATCH_DUPs,
500 make a local variable. */
22add764 501 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
8d365e72 502 printf (" rtx operand%d;\n", i);
9ed997be 503 printf (" rtx_insn *_val = 0;\n");
8d365e72 504 printf (" start_sequence ();\n");
505
506 /* The fourth operand of DEFINE_EXPAND is some code to be executed
507 before the actual construction.
508 This code expects to refer to `operands'
509 just as the output-code in a DEFINE_INSN does,
510 but here `operands' is an automatic array.
511 So copy the operand values there before executing it. */
512 if (XSTR (expand, 3) && *XSTR (expand, 3))
513 {
d5197c98 514 printf (" {\n");
22add764 515 if (stats.num_operand_vars > 0)
516 printf (" rtx operands[%d];\n", stats.num_operand_vars);
517
8d365e72 518 /* Output code to copy the arguments into `operands'. */
22add764 519 for (i = 0; i < stats.num_generator_args; i++)
d5197c98 520 printf (" operands[%d] = operand%d;\n", i, i);
8d365e72 521
522 /* Output the special code to be executed before the sequence
523 is generated. */
592e7d69 524 optab_pattern p;
525 bool can_fail_p = true;
526 if (find_optab (&p, XSTR (expand, 0)))
527 {
528 gcc_assert (p.op < NUM_OPTABS);
529 if (nofail_optabs[p.op])
530 can_fail_p = false;
531 }
532 emit_c_code (XSTR (expand, 3), can_fail_p, XSTR (expand, 0));
8d365e72 533
534 /* Output code to copy the arguments back out of `operands'
535 (unless we aren't going to use them at all). */
536 if (XVEC (expand, 1) != 0)
537 {
0f7b9610 538 for (i = 0; i <= MAX (stats.max_opno, stats.max_dup_opno); i++)
4081d362 539 {
540 printf (" operand%d = operands[%d];\n", i, i);
541 printf (" (void) operand%d;\n", i);
542 }
8d365e72 543 }
d5197c98 544 printf (" }\n");
8d365e72 545 }
546
22add764 547 used = XCNEWVEC (char, stats.num_operand_vars);
ccd6679f 548 gen_emit_seq (XVEC (expand, 1), used, info);
778725bb 549 XDELETEVEC (used);
550
31d3e01c 551 /* Call `get_insns' to extract the list of all the
8d365e72 552 insns emitted within this gen_... function. */
553
31d3e01c 554 printf (" _val = get_insns ();\n");
8d365e72 555 printf (" end_sequence ();\n");
556 printf (" return _val;\n}\n\n");
557}
558
31d3e01c 559/* Like gen_expand, but generates insns resulting from splitting SPLIT. */
a92771b8 560
8d365e72 561static void
c04601c1 562gen_split (md_rtx_info *info)
8d365e72 563{
22add764 564 struct pattern_stats stats;
19cb6b50 565 int i;
c04601c1 566 rtx split = info->def;
35823b64 567 const char *const name =
568 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
388df988 569 const char *unused;
eeddd58f 570 char *used;
82575fa7 571
8d365e72 572 if (XVEC (split, 0) == 0)
c04601c1 573 fatal_at (info->loc, "%s lacks a pattern",
574 GET_RTX_NAME (GET_CODE (split)));
8d365e72 575 else if (XVEC (split, 2) == 0)
c04601c1 576 fatal_at (info->loc, "%s lacks a replacement pattern",
577 GET_RTX_NAME (GET_CODE (split)));
8d365e72 578
579 /* Find out how many operands this function has. */
580
22add764 581 get_pattern_stats (&stats, XVEC (split, 2));
582 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
583 used = XCNEWVEC (char, stats.num_operand_vars);
8d365e72 584
82575fa7 585 /* Output the prototype, function name and argument declarations. */
586 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
587 {
58a87a29 588 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
c04601c1 589 name, info->index);
590 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED,"
591 " rtx *operands%s)\n",
592 name, info->index, unused);
82575fa7 593 }
594 else
595 {
58a87a29 596 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
c04601c1 597 info->index);
58a87a29 598 printf ("rtx_insn *\ngen_split_%d "
599 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
c04601c1 600 info->index, unused);
82575fa7 601 }
8d365e72 602 printf ("{\n");
603
604 /* Declare all local variables. */
22add764 605 for (i = 0; i < stats.num_operand_vars; i++)
8d365e72 606 printf (" rtx operand%d;\n", i);
58a87a29 607 printf (" rtx_insn *_val = NULL;\n");
82575fa7 608
609 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
610 output_peephole2_scratches (split);
611
0eb20de5 612 printf (" if (dump_file)\n");
613 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
c04601c1 614 name, info->index);
0eb20de5 615
8d365e72 616 printf (" start_sequence ();\n");
617
618 /* The fourth operand of DEFINE_SPLIT is some code to be executed
619 before the actual construction. */
620
621 if (XSTR (split, 3))
592e7d69 622 emit_c_code (XSTR (split, 3), true, name);
8d365e72 623
624 /* Output code to copy the arguments back out of `operands' */
22add764 625 for (i = 0; i < stats.num_operand_vars; i++)
4081d362 626 {
627 printf (" operand%d = operands[%d];\n", i, i);
628 printf (" (void) operand%d;\n", i);
629 }
8d365e72 630
ccd6679f 631 gen_emit_seq (XVEC (split, 2), used, info);
8d365e72 632
31d3e01c 633 /* Call `get_insns' to make a list of all the
8d365e72 634 insns emitted within this gen_... function. */
635
31d3e01c 636 printf (" _val = get_insns ();\n");
8d365e72 637 printf (" end_sequence ();\n");
638 printf (" return _val;\n}\n\n");
eeddd58f 639
640 free (used);
8d365e72 641}
642\f
643/* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
644 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
645 the end of the vector. */
646
647static void
ccd6679f 648output_add_clobbers (md_rtx_info *info)
8d365e72 649{
650 struct clobber_pat *clobber;
c39100fe 651 struct clobber_ent *ent;
8d365e72 652 int i;
653
69dc4d00 654 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
8d365e72 655 printf ("{\n");
8d365e72 656 printf (" switch (insn_code_number)\n");
657 printf (" {\n");
658
659 for (clobber = clobber_list; clobber; clobber = clobber->next)
660 {
c39100fe 661 for (ent = clobber->insns; ent; ent = ent->next)
662 printf (" case %d:\n", ent->code_number);
8d365e72 663
664 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
665 {
666 printf (" XVECEXP (pattern, 0, %d) = ", i);
82575fa7 667 gen_exp (XVECEXP (clobber->pattern, 1, i),
ccd6679f 668 GET_CODE (clobber->pattern), NULL, info);
8d365e72 669 printf (";\n");
670 }
671
c39100fe 672 printf (" break;\n\n");
8d365e72 673 }
674
675 printf (" default:\n");
e0a4c0c2 676 printf (" gcc_unreachable ();\n");
8d365e72 677 printf (" }\n");
678 printf ("}\n");
679}
680\f
e1b5e428 681/* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
682 number that will have clobbers added (as indicated by `recog') and returns
683 1 if those include a clobber of a hard reg or 0 if all of them just clobber
684 SCRATCH. */
388df988 685
686static void
1a97be37 687output_added_clobbers_hard_reg_p (void)
388df988 688{
689 struct clobber_pat *clobber;
690 struct clobber_ent *ent;
c1caaef5 691 int clobber_p, used;
388df988 692
69dc4d00 693 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
388df988 694 printf ("{\n");
695 printf (" switch (insn_code_number)\n");
696 printf (" {\n");
697
698 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
699 {
c1caaef5 700 used = 0;
388df988 701 for (clobber = clobber_list; clobber; clobber = clobber->next)
702 if (clobber->has_hard_reg == clobber_p)
703 for (ent = clobber->insns; ent; ent = ent->next)
c1caaef5 704 {
705 printf (" case %d:\n", ent->code_number);
706 used++;
707 }
388df988 708
c1caaef5 709 if (used)
710 printf (" return %d;\n\n", clobber_p);
388df988 711 }
712
713 printf (" default:\n");
e0a4c0c2 714 printf (" gcc_unreachable ();\n");
388df988 715 printf (" }\n");
716 printf ("}\n");
717}
718\f
82575fa7 719/* Generate code to invoke find_free_register () as needed for the
aa40f561 720 scratch registers used by the peephole2 pattern in SPLIT. */
82575fa7 721
722static void
1a97be37 723output_peephole2_scratches (rtx split)
82575fa7 724{
725 int i;
726 int insn_nr = 0;
6d86fc31 727 bool first = true;
82575fa7 728
729 for (i = 0; i < XVECLEN (split, 0); i++)
730 {
731 rtx elt = XVECEXP (split, 0, i);
732 if (GET_CODE (elt) == MATCH_SCRATCH)
733 {
734 int last_insn_nr = insn_nr;
735 int cur_insn_nr = insn_nr;
736 int j;
737 for (j = i + 1; j < XVECLEN (split, 0); j++)
738 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
739 {
740 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
741 last_insn_nr = cur_insn_nr;
742 }
743 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
744 cur_insn_nr++;
3e9f1237 745
6d86fc31 746 if (first)
747 {
748 printf (" HARD_REG_SET _regs_allocated;\n");
749 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
750 first = false;
751 }
752
3e9f1237 753 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
1a97be37 754 return NULL;\n",
82575fa7 755 XINT (elt, 0),
3e9f1237 756 insn_nr, last_insn_nr,
82575fa7 757 XSTR (elt, 1),
758 GET_MODE_NAME (GET_MODE (elt)));
759
760 }
761 else if (GET_CODE (elt) != MATCH_DUP)
762 insn_nr++;
763 }
764}
8d365e72 765
60eea7fb 766/* Print "arg<N>" parameter declarations for each argument N of ONAME. */
767
768static void
769print_overload_arguments (overloaded_name *oname)
770{
771 for (unsigned int i = 0; i < oname->arg_types.length (); ++i)
772 printf ("%s%s arg%d", i == 0 ? "" : ", ", oname->arg_types[i], i);
773}
774
4b5b838c 775/* Print code to test whether INSTANCE should be chosen, given that
60eea7fb 776 argument N of the overload is available as "arg<N>". */
777
778static void
779print_overload_test (overloaded_instance *instance)
780{
781 for (unsigned int i = 0; i < instance->arg_values.length (); ++i)
782 printf ("%sarg%d == %s", i == 0 ? " if (" : "\n && ",
783 i, instance->arg_values[i]);
784 printf (")\n");
785}
786
787/* Emit a maybe_code_for_* function for ONAME. */
788
789static void
790handle_overloaded_code_for (overloaded_name *oname)
791{
792 /* Print the function prototype. */
793 printf ("\ninsn_code\nmaybe_code_for_%s (", oname->name);
794 print_overload_arguments (oname);
795 printf (")\n{\n");
796
797 /* Use a sequence of "if" statements for each instance. */
798 for (overloaded_instance *instance = oname->first_instance;
799 instance; instance = instance->next)
800 {
801 print_overload_test (instance);
802 printf (" return CODE_FOR_%s;\n", instance->name);
803 }
804
805 /* Return null if no match was found. */
806 printf (" return CODE_FOR_nothing;\n}\n");
807}
808
809/* Emit a maybe_gen_* function for ONAME. */
810
811static void
812handle_overloaded_gen (overloaded_name *oname)
813{
814 /* All patterns must have the same number of operands. */
815 pattern_stats stats;
816 get_pattern_stats (&stats, XVEC (oname->first_instance->insn, 1));
817 for (overloaded_instance *instance = oname->first_instance->next;
818 instance; instance = instance->next)
819 {
820 pattern_stats stats2;
821 get_pattern_stats (&stats2, XVEC (instance->insn, 1));
822 if (stats.num_generator_args != stats2.num_generator_args)
823 fatal_at (get_file_location (instance->insn),
824 "inconsistent number of operands for '%s'; "
825 "this instance has %d, but previous instances had %d",
826 oname->name, stats2.num_generator_args,
827 stats.num_generator_args);
828 }
829
830 /* Print the function prototype. */
831 printf ("\nrtx\nmaybe_gen_%s (", oname->name);
832 print_overload_arguments (oname);
833 for (int i = 0; i < stats.num_generator_args; ++i)
834 printf (", rtx x%d", i);
835 printf (")\n{\n");
836
837 /* Use maybe_code_for_*, instead of duplicating the selection logic here. */
838 printf (" insn_code code = maybe_code_for_%s (", oname->name);
839 for (unsigned int i = 0; i < oname->arg_types.length (); ++i)
840 printf ("%sarg%d", i == 0 ? "" : ", ", i);
841 printf (");\n"
842 " if (code != CODE_FOR_nothing)\n"
843 " return GEN_FCN (code) (");
844 for (int i = 0; i < stats.num_generator_args; ++i)
845 printf ("%sx%d", i == 0 ? "" : ", ", i);
846 printf (");\n"
847 " else\n"
848 " return NULL_RTX;\n"
849 "}\n");
850}
851
8d365e72 852int
16570c04 853main (int argc, const char **argv)
8d365e72 854{
04b58880 855 progname = "genemit";
8d365e72 856
77ba95d0 857 if (!init_rtx_reader_args (argc, argv))
c5ddd6b5 858 return (FATAL_EXIT_CODE);
8d365e72 859
592e7d69 860#define DEF_INTERNAL_OPTAB_FN(NAME, FLAGS, OPTAB, TYPE) \
861 nofail_optabs[OPTAB##_optab] = true;
862#include "internal-fn.def"
863
8d365e72 864 /* Assign sequential codes to all entries in the machine description
865 in parallel with the tables in insn-output.c. */
866
8d365e72 867 printf ("/* Generated automatically by the program `genemit'\n\
868from the machine description file `md'. */\n\n");
869
785790dc 870 printf ("#define IN_TARGET_CODE 1\n");
8d365e72 871 printf ("#include \"config.h\"\n");
9aaa1fcc 872 printf ("#include \"system.h\"\n");
805e22b2 873 printf ("#include \"coretypes.h\"\n");
9ef16211 874 printf ("#include \"backend.h\"\n");
d040a5b0 875 printf ("#include \"predict.h\"\n");
41a8aa41 876 printf ("#include \"tree.h\"\n");
9ef16211 877 printf ("#include \"rtl.h\"\n");
878 printf ("#include \"alias.h\"\n");
9ed99284 879 printf ("#include \"varasm.h\"\n");
880 printf ("#include \"stor-layout.h\"\n");
881 printf ("#include \"calls.h\"\n");
ad7b10a2 882 printf ("#include \"memmodel.h\"\n");
7953c610 883 printf ("#include \"tm_p.h\"\n");
d53441c8 884 printf ("#include \"flags.h\"\n");
d53441c8 885 printf ("#include \"insn-config.h\"\n");
886 printf ("#include \"expmed.h\"\n");
887 printf ("#include \"dojump.h\"\n");
888 printf ("#include \"explow.h\"\n");
889 printf ("#include \"emit-rtl.h\"\n");
890 printf ("#include \"stmt.h\"\n");
8d365e72 891 printf ("#include \"expr.h\"\n");
34517c64 892 printf ("#include \"insn-codes.h\"\n");
d8fc4d0b 893 printf ("#include \"optabs.h\"\n");
3b55c036 894 printf ("#include \"dfp.h\"\n");
8d365e72 895 printf ("#include \"output.h\"\n");
3e9f1237 896 printf ("#include \"recog.h\"\n");
9ef16211 897 printf ("#include \"df.h\"\n");
82575fa7 898 printf ("#include \"resource.h\"\n");
05806416 899 printf ("#include \"reload.h\"\n");
c18cb818 900 printf ("#include \"diagnostic-core.h\"\n");
fb16c776 901 printf ("#include \"regs.h\"\n");
a014ec0f 902 printf ("#include \"tm-constrs.h\"\n");
fb16c776 903 printf ("#include \"ggc.h\"\n");
e374c627 904 printf ("#include \"target.h\"\n\n");
8d365e72 905
906 /* Read the machine description. */
907
c04601c1 908 md_rtx_info info;
909 while (read_md_rtx (&info))
910 switch (GET_CODE (info.def))
911 {
912 case DEFINE_INSN:
913 gen_insn (&info);
c5ddd6b5 914 break;
82575fa7 915
c04601c1 916 case DEFINE_EXPAND:
917 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
918 gen_expand (&info);
919 break;
f1c96ec2 920
c04601c1 921 case DEFINE_SPLIT:
922 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
923 gen_split (&info);
924 break;
f1c96ec2 925
c04601c1 926 case DEFINE_PEEPHOLE2:
927 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
928 gen_split (&info);
929 break;
f1c96ec2 930
c04601c1 931 default:
932 break;
933 }
8d365e72 934
388df988 935 /* Write out the routines to add CLOBBERs to a pattern and say whether they
936 clobber a hard reg. */
ccd6679f 937 output_add_clobbers (&info);
388df988 938 output_added_clobbers_hard_reg_p ();
8d365e72 939
60eea7fb 940 for (overloaded_name *oname = rtx_reader_ptr->get_overloads ();
941 oname; oname = oname->next)
942 {
943 handle_overloaded_code_for (oname);
944 handle_overloaded_gen (oname);
945 }
946
8d365e72 947 fflush (stdout);
947491b7 948 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
8d365e72 949}