Skip asm goto tests on hppa*-*-*.
[official-gcc.git] / gcc / genemit.c
blob89d9e990b735860d40731acc4ad935644e5b47c5
1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
31 /* Data structure for recording the patterns of insns that have CLOBBERs.
32 We use this to output a function that adds these CLOBBERs to a
33 previously-allocated PARALLEL expression. */
35 struct clobber_pat
37 struct clobber_ent *insns;
38 rtx pattern;
39 int first_clobber;
40 struct clobber_pat *next;
41 int has_hard_reg;
42 } *clobber_list;
44 /* Records one insn that uses the clobber list. */
46 struct clobber_ent
48 int code_number; /* Counts only insns. */
49 struct clobber_ent *next;
52 static void output_peephole2_scratches (rtx);
54 /* True for <X>_optab if that optab isn't allowed to fail. */
55 static bool nofail_optabs[NUM_OPTABS];
57 static void
58 print_code (RTX_CODE code)
60 const char *p1;
61 for (p1 = GET_RTX_NAME (code); *p1; p1++)
62 putchar (TOUPPER (*p1));
65 static void
66 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
68 if (subroutine_type == DEFINE_PEEPHOLE2)
70 printf ("operand%d", XINT (x, 0));
72 else
74 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
78 /* Print a C expression to construct an RTX just like X,
79 substituting any operand references appearing within. */
81 static void
82 gen_exp (rtx x, enum rtx_code subroutine_type, char *used, md_rtx_info *info)
84 RTX_CODE code;
85 int i;
86 int len;
87 const char *fmt;
88 const char *sep = "";
90 if (x == 0)
92 printf ("NULL_RTX");
93 return;
96 code = GET_CODE (x);
98 switch (code)
100 case MATCH_OPERAND:
101 case MATCH_DUP:
102 if (used)
104 if (used[XINT (x, 0)])
106 printf ("copy_rtx (operand%d)", XINT (x, 0));
107 return;
109 used[XINT (x, 0)] = 1;
111 printf ("operand%d", XINT (x, 0));
112 return;
114 case MATCH_OP_DUP:
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));
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)));
123 for (i = 0; i < XVECLEN (x, 1); i++)
125 printf (",\n\t\t");
126 gen_exp (XVECEXP (x, 1, i), subroutine_type, used, info);
128 printf (")");
129 return;
131 case MATCH_OPERATOR:
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));
136 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
137 for (i = 0; i < XVECLEN (x, 2); i++)
139 printf (",\n\t\t");
140 gen_exp (XVECEXP (x, 2, i), subroutine_type, used, info);
142 printf (")");
143 return;
145 case MATCH_PARALLEL:
146 case MATCH_PAR_DUP:
147 printf ("operand%d", XINT (x, 0));
148 return;
150 case MATCH_SCRATCH:
151 gen_rtx_scratch (x, subroutine_type);
152 return;
154 case PC:
155 printf ("pc_rtx");
156 return;
157 case RETURN:
158 printf ("ret_rtx");
159 return;
160 case SIMPLE_RETURN:
161 printf ("simple_return_rtx");
162 return;
163 case CLOBBER:
164 if (REG_P (XEXP (x, 0)))
166 printf ("gen_hard_reg_clobber (%smode, %i)",
167 GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
168 REGNO (XEXP (x, 0)));
169 return;
171 break;
172 case CC0:
173 printf ("cc0_rtx");
174 return;
176 case CONST_INT:
177 if (INTVAL (x) == 0)
178 printf ("const0_rtx");
179 else if (INTVAL (x) == 1)
180 printf ("const1_rtx");
181 else if (INTVAL (x) == -1)
182 printf ("constm1_rtx");
183 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
184 && INTVAL (x) <= MAX_SAVED_CONST_INT)
185 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
186 (int) INTVAL (x));
187 else if (INTVAL (x) == STORE_FLAG_VALUE)
188 printf ("const_true_rtx");
189 else
191 printf ("GEN_INT (");
192 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
193 printf (")");
195 return;
197 case CONST_DOUBLE:
198 /* Handle `const_double_zero' rtx. */
199 if (CONST_DOUBLE_REAL_VALUE (x)->cl == rvc_zero)
201 printf ("CONST_DOUBLE_ATOF (\"0\", %smode)",
202 GET_MODE_NAME (GET_MODE (x)));
203 return;
205 /* Fall through. */
206 case CONST_FIXED:
207 case CONST_WIDE_INT:
208 /* These shouldn't be written in MD files. Instead, the appropriate
209 routines in varasm.c should be called. */
210 gcc_unreachable ();
212 default:
213 break;
216 printf ("gen_rtx_");
217 print_code (code);
218 printf (" (");
219 if (!always_void_p (code))
221 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
222 sep = ",\n\t";
225 fmt = GET_RTX_FORMAT (code);
226 len = GET_RTX_LENGTH (code);
227 for (i = 0; i < len; i++)
229 if (fmt[i] == '0')
230 break;
231 fputs (sep, stdout);
232 switch (fmt[i])
234 case 'e': case 'u':
235 gen_exp (XEXP (x, i), subroutine_type, used, info);
236 break;
238 case 'i':
239 printf ("%u", XINT (x, i));
240 break;
242 case 'r':
243 printf ("%u", REGNO (x));
244 break;
246 case 'p':
247 /* We don't have a way of parsing polynomial offsets yet,
248 and hopefully never will. */
249 printf ("%d", SUBREG_BYTE (x).to_constant ());
250 break;
252 case 's':
253 printf ("\"%s\"", XSTR (x, i));
254 break;
256 case 'E':
258 int j;
259 printf ("gen_rtvec (%d", XVECLEN (x, i));
260 for (j = 0; j < XVECLEN (x, i); j++)
262 printf (",\n\t\t");
263 gen_exp (XVECEXP (x, i, j), subroutine_type, used, info);
265 printf (")");
266 break;
269 default:
270 gcc_unreachable ();
272 sep = ",\n\t";
274 printf (")");
277 /* Output code to emit the instruction patterns in VEC, with each element
278 becoming a separate instruction. USED is as for gen_exp. */
280 static void
281 gen_emit_seq (rtvec vec, char *used, md_rtx_info *info)
283 for (int i = 0, len = GET_NUM_ELEM (vec); i < len; ++i)
285 bool last_p = (i == len - 1);
286 rtx next = RTVEC_ELT (vec, i);
287 if (const char *name = get_emit_function (next))
289 printf (" %s (", name);
290 gen_exp (next, DEFINE_EXPAND, used, info);
291 printf (");\n");
292 if (!last_p && needs_barrier_p (next))
293 printf (" emit_barrier ();");
295 else
297 printf (" emit (");
298 gen_exp (next, DEFINE_EXPAND, used, info);
299 printf (", %s);\n", last_p ? "false" : "true");
304 /* Emit the given C code to the output file. The code is allowed to
305 fail if CAN_FAIL_P. NAME describes what we're generating,
306 for use in error messages. */
308 static void
309 emit_c_code (const char *code, bool can_fail_p, const char *name)
311 if (can_fail_p)
312 printf ("#define FAIL return (end_sequence (), _val)\n");
313 else
314 printf ("#define FAIL _Pragma (\"GCC error \\\"%s cannot FAIL\\\"\")"
315 " (void)0\n", name);
316 printf ("#define DONE return (_val = get_insns (), "
317 "end_sequence (), _val)\n");
319 rtx_reader_ptr->print_md_ptr_loc (code);
320 printf ("%s\n", code);
322 printf ("#undef DONE\n");
323 printf ("#undef FAIL\n");
326 /* Generate the `gen_...' function for a DEFINE_INSN. */
328 static void
329 gen_insn (md_rtx_info *info)
331 struct pattern_stats stats;
332 int i;
334 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
335 registers or MATCH_SCRATCHes. If so, store away the information for
336 later. */
338 rtx insn = info->def;
339 if (XVEC (insn, 1))
341 int has_hard_reg = 0;
343 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
345 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
346 break;
348 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
349 has_hard_reg = 1;
350 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
351 break;
354 if (i != XVECLEN (insn, 1) - 1)
356 struct clobber_pat *p;
357 struct clobber_ent *link = XNEW (struct clobber_ent);
358 int j;
360 link->code_number = info->index;
362 /* See if any previous CLOBBER_LIST entry is the same as this
363 one. */
365 for (p = clobber_list; p; p = p->next)
367 if (p->first_clobber != i + 1
368 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
369 continue;
371 for (j = i + 1; j < XVECLEN (insn, 1); j++)
373 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
374 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
376 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
377 of the same mode,
378 or if both are registers of the same mode and number. */
379 if (! (GET_CODE (old_rtx) == GET_CODE (new_rtx)
380 && GET_MODE (old_rtx) == GET_MODE (new_rtx)
381 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
382 && GET_CODE (new_rtx) == MATCH_SCRATCH)
383 || (REG_P (old_rtx) && REG_P (new_rtx)
384 && REGNO (old_rtx) == REGNO (new_rtx)))))
385 break;
388 if (j == XVECLEN (insn, 1))
389 break;
392 if (p == 0)
394 p = XNEW (struct clobber_pat);
396 p->insns = 0;
397 p->pattern = insn;
398 p->first_clobber = i + 1;
399 p->next = clobber_list;
400 p->has_hard_reg = has_hard_reg;
401 clobber_list = p;
404 link->next = p->insns;
405 p->insns = link;
409 /* Don't mention instructions whose names are the null string
410 or begin with '*'. They are in the machine description just
411 to be recognized. */
412 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
413 return;
415 printf ("/* %s:%d */\n", info->loc.filename, info->loc.lineno);
417 /* Find out how many operands this function has. */
418 get_pattern_stats (&stats, XVEC (insn, 1));
419 if (stats.max_dup_opno > stats.max_opno)
420 fatal_at (info->loc, "match_dup operand number has no match_operand");
422 /* Output the function name and argument declarations. */
423 printf ("rtx\ngen_%s (", XSTR (insn, 0));
424 if (stats.num_generator_args)
425 for (i = 0; i < stats.num_generator_args; i++)
426 if (i)
427 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
428 else
429 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
430 else
431 printf ("void");
432 printf (")\n");
433 printf ("{\n");
435 /* Output code to construct and return the rtl for the instruction body. */
437 rtx pattern = add_implicit_parallel (XVEC (insn, 1));
438 /* ??? This is the traditional behavior, but seems suspect. */
439 char *used = (XVECLEN (insn, 1) == 1
440 ? NULL
441 : XCNEWVEC (char, stats.num_generator_args));
442 printf (" return ");
443 gen_exp (pattern, DEFINE_INSN, used, info);
444 printf (";\n}\n\n");
445 XDELETEVEC (used);
448 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
450 static void
451 gen_expand (md_rtx_info *info)
453 struct pattern_stats stats;
454 int i;
455 char *used;
457 rtx expand = info->def;
458 if (strlen (XSTR (expand, 0)) == 0)
459 fatal_at (info->loc, "define_expand lacks a name");
460 if (XVEC (expand, 1) == 0)
461 fatal_at (info->loc, "define_expand for %s lacks a pattern",
462 XSTR (expand, 0));
464 /* Find out how many operands this function has. */
465 get_pattern_stats (&stats, XVEC (expand, 1));
466 if (stats.min_scratch_opno != -1
467 && stats.min_scratch_opno <= MAX (stats.max_opno, stats.max_dup_opno))
468 fatal_at (info->loc, "define_expand for %s needs to have match_scratch "
469 "numbers above all other operands", XSTR (expand, 0));
471 /* Output the function name and argument declarations. */
472 printf ("rtx\ngen_%s (", XSTR (expand, 0));
473 if (stats.num_generator_args)
474 for (i = 0; i < stats.num_generator_args; i++)
475 if (i)
476 printf (",\n\trtx operand%d", i);
477 else
478 printf ("rtx operand%d", i);
479 else
480 printf ("void");
481 printf (")\n");
482 printf ("{\n");
484 /* If we don't have any C code to write, only one insn is being written,
485 and no MATCH_DUPs are present, we can just return the desired insn
486 like we do for a DEFINE_INSN. This saves memory. */
487 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
488 && stats.max_opno >= stats.max_dup_opno
489 && XVECLEN (expand, 1) == 1)
491 printf (" return ");
492 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL, info);
493 printf (";\n}\n\n");
494 return;
497 /* For each operand referred to only with MATCH_DUPs,
498 make a local variable. */
499 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
500 printf (" rtx operand%d;\n", i);
501 printf (" rtx_insn *_val = 0;\n");
502 printf (" start_sequence ();\n");
504 /* The fourth operand of DEFINE_EXPAND is some code to be executed
505 before the actual construction.
506 This code expects to refer to `operands'
507 just as the output-code in a DEFINE_INSN does,
508 but here `operands' is an automatic array.
509 So copy the operand values there before executing it. */
510 if (XSTR (expand, 3) && *XSTR (expand, 3))
512 printf (" {\n");
513 if (stats.num_operand_vars > 0)
514 printf (" rtx operands[%d];\n", stats.num_operand_vars);
516 /* Output code to copy the arguments into `operands'. */
517 for (i = 0; i < stats.num_generator_args; i++)
518 printf (" operands[%d] = operand%d;\n", i, i);
520 /* Output the special code to be executed before the sequence
521 is generated. */
522 optab_pattern p;
523 bool can_fail_p = true;
524 if (find_optab (&p, XSTR (expand, 0)))
526 gcc_assert (p.op < NUM_OPTABS);
527 if (nofail_optabs[p.op])
528 can_fail_p = false;
530 emit_c_code (XSTR (expand, 3), can_fail_p, XSTR (expand, 0));
532 /* Output code to copy the arguments back out of `operands'
533 (unless we aren't going to use them at all). */
534 if (XVEC (expand, 1) != 0)
536 for (i = 0; i <= MAX (stats.max_opno, stats.max_dup_opno); i++)
538 printf (" operand%d = operands[%d];\n", i, i);
539 printf (" (void) operand%d;\n", i);
542 printf (" }\n");
545 used = XCNEWVEC (char, stats.num_operand_vars);
546 gen_emit_seq (XVEC (expand, 1), used, info);
547 XDELETEVEC (used);
549 /* Call `get_insns' to extract the list of all the
550 insns emitted within this gen_... function. */
552 printf (" _val = get_insns ();\n");
553 printf (" end_sequence ();\n");
554 printf (" return _val;\n}\n\n");
557 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
559 static void
560 gen_split (md_rtx_info *info)
562 struct pattern_stats stats;
563 int i;
564 rtx split = info->def;
565 const char *const name =
566 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
567 const char *unused;
568 char *used;
570 if (XVEC (split, 0) == 0)
571 fatal_at (info->loc, "%s lacks a pattern",
572 GET_RTX_NAME (GET_CODE (split)));
573 else if (XVEC (split, 2) == 0)
574 fatal_at (info->loc, "%s lacks a replacement pattern",
575 GET_RTX_NAME (GET_CODE (split)));
577 /* Find out how many operands this function has. */
579 get_pattern_stats (&stats, XVEC (split, 2));
580 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
581 used = XCNEWVEC (char, stats.num_operand_vars);
583 /* Output the prototype, function name and argument declarations. */
584 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
586 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
587 name, info->index);
588 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED,"
589 " rtx *operands%s)\n",
590 name, info->index, unused);
592 else
594 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
595 info->index);
596 printf ("rtx_insn *\ngen_split_%d "
597 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
598 info->index, unused);
600 printf ("{\n");
602 /* Declare all local variables. */
603 for (i = 0; i < stats.num_operand_vars; i++)
604 printf (" rtx operand%d;\n", i);
605 printf (" rtx_insn *_val = NULL;\n");
607 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
608 output_peephole2_scratches (split);
610 const char *fn = info->loc.filename;
611 for (const char *p = fn; *p; p++)
612 if (*p == '/')
613 fn = p + 1;
615 printf (" if (dump_file)\n");
616 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d (%s:%d)\\n\");\n",
617 name, info->index, fn, info->loc.lineno);
619 printf (" start_sequence ();\n");
621 /* The fourth operand of DEFINE_SPLIT is some code to be executed
622 before the actual construction. */
624 if (XSTR (split, 3))
625 emit_c_code (XSTR (split, 3), true, name);
627 /* Output code to copy the arguments back out of `operands' */
628 for (i = 0; i < stats.num_operand_vars; i++)
630 printf (" operand%d = operands[%d];\n", i, i);
631 printf (" (void) operand%d;\n", i);
634 gen_emit_seq (XVEC (split, 2), used, info);
636 /* Call `get_insns' to make a list of all the
637 insns emitted within this gen_... function. */
639 printf (" _val = get_insns ();\n");
640 printf (" end_sequence ();\n");
641 printf (" return _val;\n}\n\n");
643 free (used);
646 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
647 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
648 the end of the vector. */
650 static void
651 output_add_clobbers (md_rtx_info *info)
653 struct clobber_pat *clobber;
654 struct clobber_ent *ent;
655 int i;
657 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
658 printf ("{\n");
659 printf (" switch (insn_code_number)\n");
660 printf (" {\n");
662 for (clobber = clobber_list; clobber; clobber = clobber->next)
664 for (ent = clobber->insns; ent; ent = ent->next)
665 printf (" case %d:\n", ent->code_number);
667 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
669 printf (" XVECEXP (pattern, 0, %d) = ", i);
670 gen_exp (XVECEXP (clobber->pattern, 1, i),
671 GET_CODE (clobber->pattern), NULL, info);
672 printf (";\n");
675 printf (" break;\n\n");
678 printf (" default:\n");
679 printf (" gcc_unreachable ();\n");
680 printf (" }\n");
681 printf ("}\n");
684 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
685 number that will have clobbers added (as indicated by `recog') and returns
686 1 if those include a clobber of a hard reg or 0 if all of them just clobber
687 SCRATCH. */
689 static void
690 output_added_clobbers_hard_reg_p (void)
692 struct clobber_pat *clobber;
693 struct clobber_ent *ent;
694 int clobber_p, used;
696 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
697 printf ("{\n");
698 printf (" switch (insn_code_number)\n");
699 printf (" {\n");
701 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
703 used = 0;
704 for (clobber = clobber_list; clobber; clobber = clobber->next)
705 if (clobber->has_hard_reg == clobber_p)
706 for (ent = clobber->insns; ent; ent = ent->next)
708 printf (" case %d:\n", ent->code_number);
709 used++;
712 if (used)
713 printf (" return %d;\n\n", clobber_p);
716 printf (" default:\n");
717 printf (" gcc_unreachable ();\n");
718 printf (" }\n");
719 printf ("}\n");
722 /* Generate code to invoke find_free_register () as needed for the
723 scratch registers used by the peephole2 pattern in SPLIT. */
725 static void
726 output_peephole2_scratches (rtx split)
728 int i;
729 int insn_nr = 0;
730 bool first = true;
732 for (i = 0; i < XVECLEN (split, 0); i++)
734 rtx elt = XVECEXP (split, 0, i);
735 if (GET_CODE (elt) == MATCH_SCRATCH)
737 int last_insn_nr = insn_nr;
738 int cur_insn_nr = insn_nr;
739 int j;
740 for (j = i + 1; j < XVECLEN (split, 0); j++)
741 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
743 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
744 last_insn_nr = cur_insn_nr;
746 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
747 cur_insn_nr++;
749 if (first)
751 printf (" HARD_REG_SET _regs_allocated;\n");
752 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
753 first = false;
756 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
757 return NULL;\n",
758 XINT (elt, 0),
759 insn_nr, last_insn_nr,
760 XSTR (elt, 1),
761 GET_MODE_NAME (GET_MODE (elt)));
764 else if (GET_CODE (elt) != MATCH_DUP)
765 insn_nr++;
769 /* Print "arg<N>" parameter declarations for each argument N of ONAME. */
771 static void
772 print_overload_arguments (overloaded_name *oname)
774 for (unsigned int i = 0; i < oname->arg_types.length (); ++i)
775 printf ("%s%s arg%d", i == 0 ? "" : ", ", oname->arg_types[i], i);
778 /* Print code to test whether INSTANCE should be chosen, given that
779 argument N of the overload is available as "arg<N>". */
781 static void
782 print_overload_test (overloaded_instance *instance)
784 for (unsigned int i = 0; i < instance->arg_values.length (); ++i)
785 printf ("%sarg%d == %s", i == 0 ? " if (" : "\n && ",
786 i, instance->arg_values[i]);
787 printf (")\n");
790 /* Emit a maybe_code_for_* function for ONAME. */
792 static void
793 handle_overloaded_code_for (overloaded_name *oname)
795 /* Print the function prototype. */
796 printf ("\ninsn_code\nmaybe_code_for_%s (", oname->name);
797 print_overload_arguments (oname);
798 printf (")\n{\n");
800 /* Use a sequence of "if" statements for each instance. */
801 for (overloaded_instance *instance = oname->first_instance;
802 instance; instance = instance->next)
804 print_overload_test (instance);
805 printf (" return CODE_FOR_%s;\n", instance->name);
808 /* Return null if no match was found. */
809 printf (" return CODE_FOR_nothing;\n}\n");
812 /* Emit a maybe_gen_* function for ONAME. */
814 static void
815 handle_overloaded_gen (overloaded_name *oname)
817 unsigned HOST_WIDE_INT seen = 0;
818 /* All patterns must have the same number of operands. */
819 for (overloaded_instance *instance = oname->first_instance->next;
820 instance; instance = instance->next)
822 pattern_stats stats;
823 get_pattern_stats (&stats, XVEC (instance->insn, 1));
824 unsigned HOST_WIDE_INT mask
825 = HOST_WIDE_INT_1U << stats.num_generator_args;
826 if (seen & mask)
827 continue;
829 seen |= mask;
831 /* Print the function prototype. */
832 printf ("\nrtx\nmaybe_gen_%s (", oname->name);
833 print_overload_arguments (oname);
834 for (int i = 0; i < stats.num_generator_args; ++i)
835 printf (", rtx x%d", i);
836 printf (")\n{\n");
838 /* Use maybe_code_for_*, instead of duplicating the selection
839 logic here. */
840 printf (" insn_code code = maybe_code_for_%s (", oname->name);
841 for (unsigned int i = 0; i < oname->arg_types.length (); ++i)
842 printf ("%sarg%d", i == 0 ? "" : ", ", i);
843 printf (");\n"
844 " if (code != CODE_FOR_nothing)\n"
845 " {\n"
846 " gcc_assert (insn_data[code].n_generator_args == %d);\n"
847 " return GEN_FCN (code) (", stats.num_generator_args);
848 for (int i = 0; i < stats.num_generator_args; ++i)
849 printf ("%sx%d", i == 0 ? "" : ", ", i);
850 printf (");\n"
851 " }\n"
852 " else\n"
853 " return NULL_RTX;\n"
854 "}\n");
859 main (int argc, const char **argv)
861 progname = "genemit";
863 if (!init_rtx_reader_args (argc, argv))
864 return (FATAL_EXIT_CODE);
866 #define DEF_INTERNAL_OPTAB_FN(NAME, FLAGS, OPTAB, TYPE) \
867 nofail_optabs[OPTAB##_optab] = true;
868 #include "internal-fn.def"
870 /* Assign sequential codes to all entries in the machine description
871 in parallel with the tables in insn-output.c. */
873 printf ("/* Generated automatically by the program `genemit'\n\
874 from the machine description file `md'. */\n\n");
876 printf ("#define IN_TARGET_CODE 1\n");
877 printf ("#include \"config.h\"\n");
878 printf ("#include \"system.h\"\n");
879 printf ("#include \"coretypes.h\"\n");
880 printf ("#include \"backend.h\"\n");
881 printf ("#include \"predict.h\"\n");
882 printf ("#include \"tree.h\"\n");
883 printf ("#include \"rtl.h\"\n");
884 printf ("#include \"alias.h\"\n");
885 printf ("#include \"varasm.h\"\n");
886 printf ("#include \"stor-layout.h\"\n");
887 printf ("#include \"calls.h\"\n");
888 printf ("#include \"memmodel.h\"\n");
889 printf ("#include \"tm_p.h\"\n");
890 printf ("#include \"flags.h\"\n");
891 printf ("#include \"insn-config.h\"\n");
892 printf ("#include \"expmed.h\"\n");
893 printf ("#include \"dojump.h\"\n");
894 printf ("#include \"explow.h\"\n");
895 printf ("#include \"emit-rtl.h\"\n");
896 printf ("#include \"stmt.h\"\n");
897 printf ("#include \"expr.h\"\n");
898 printf ("#include \"insn-codes.h\"\n");
899 printf ("#include \"optabs.h\"\n");
900 printf ("#include \"dfp.h\"\n");
901 printf ("#include \"output.h\"\n");
902 printf ("#include \"recog.h\"\n");
903 printf ("#include \"df.h\"\n");
904 printf ("#include \"resource.h\"\n");
905 printf ("#include \"reload.h\"\n");
906 printf ("#include \"diagnostic-core.h\"\n");
907 printf ("#include \"regs.h\"\n");
908 printf ("#include \"tm-constrs.h\"\n");
909 printf ("#include \"ggc.h\"\n");
910 printf ("#include \"target.h\"\n\n");
912 /* Read the machine description. */
914 md_rtx_info info;
915 while (read_md_rtx (&info))
916 switch (GET_CODE (info.def))
918 case DEFINE_INSN:
919 gen_insn (&info);
920 break;
922 case DEFINE_EXPAND:
923 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
924 gen_expand (&info);
925 break;
927 case DEFINE_SPLIT:
928 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
929 gen_split (&info);
930 break;
932 case DEFINE_PEEPHOLE2:
933 printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno);
934 gen_split (&info);
935 break;
937 default:
938 break;
941 /* Write out the routines to add CLOBBERs to a pattern and say whether they
942 clobber a hard reg. */
943 output_add_clobbers (&info);
944 output_added_clobbers_hard_reg_p ();
946 for (overloaded_name *oname = rtx_reader_ptr->get_overloads ();
947 oname; oname = oname->next)
949 handle_overloaded_code_for (oname);
950 handle_overloaded_gen (oname);
953 fflush (stdout);
954 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);