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