Debian Codegraph @

gcc/genemit.cc

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