]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genemit.c
Add missing entry.
[thirdparty/gcc.git] / gcc / genemit.c
CommitLineData
65963943 1/* Generate code from machine description to emit insns as rtl.
5624e564 2 Copyright (C) 1987-2015 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
RK
31static int insn_code_number;
32static int insn_index_number;
33
34/* Data structure for recording the patterns of insns that have CLOBBERs.
3d7aafde 35 We use this to output a function that adds these CLOBBERs to a
65963943
RK
36 previously-allocated PARALLEL expression. */
37
38struct clobber_pat
39{
e5f6a288 40 struct clobber_ent *insns;
65963943
RK
41 rtx pattern;
42 int first_clobber;
43 struct clobber_pat *next;
751aa7cc 44 int has_hard_reg;
65963943
RK
45} *clobber_list;
46
e5f6a288
RK
47/* Records one insn that uses the clobber list. */
48
49struct clobber_ent
50{
51 int code_number; /* Counts only insns. */
52 struct clobber_ent *next;
53};
54
3d7aafde
AJ
55static void print_code (RTX_CODE);
56static void gen_exp (rtx, enum rtx_code, char *);
57static void gen_insn (rtx, int);
58static void gen_expand (rtx);
59static void gen_split (rtx);
60static void output_add_clobbers (void);
61static void output_added_clobbers_hard_reg_p (void);
62static void gen_rtx_scratch (rtx, enum rtx_code);
63static void output_peephole2_scratches (rtx);
e009aaf3 64
65963943
RK
65\f
66static void
3d7aafde 67print_code (RTX_CODE code)
65963943 68{
b3694847 69 const char *p1;
65963943 70 for (p1 = GET_RTX_NAME (code); *p1; p1++)
c3284718 71 putchar (TOUPPER (*p1));
65963943
RK
72}
73
ede7cd44 74static void
3d7aafde 75gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
ede7cd44
RH
76{
77 if (subroutine_type == DEFINE_PEEPHOLE2)
78 {
79 printf ("operand%d", XINT (x, 0));
80 }
81 else
82 {
83 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
84 }
85}
86
65963943
RK
87/* Print a C expression to construct an RTX just like X,
88 substituting any operand references appearing within. */
89
90static void
3d7aafde 91gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
65963943 92{
b3694847
SS
93 RTX_CODE code;
94 int i;
95 int len;
96 const char *fmt;
f7df4a84 97 const char *sep = "";
65963943
RK
98
99 if (x == 0)
100 {
3d678dca 101 printf ("NULL_RTX");
65963943
RK
102 return;
103 }
104
105 code = GET_CODE (x);
106
107 switch (code)
108 {
109 case MATCH_OPERAND:
110 case MATCH_DUP:
ab55f58c
RH
111 if (used)
112 {
113 if (used[XINT (x, 0)])
114 {
115 printf ("copy_rtx (operand%d)", XINT (x, 0));
116 return;
117 }
118 used[XINT (x, 0)] = 1;
119 }
65963943
RK
120 printf ("operand%d", XINT (x, 0));
121 return;
122
123 case MATCH_OP_DUP:
600555f5
KH
124 printf ("gen_rtx_fmt_");
125 for (i = 0; i < XVECLEN (x, 1); i++)
126 printf ("e");
127 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
4ceb7595
JC
128 if (GET_MODE (x) == VOIDmode)
129 printf ("GET_MODE (operand%d)", XINT (x, 0));
130 else
131 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
65963943
RK
132 for (i = 0; i < XVECLEN (x, 1); i++)
133 {
134 printf (",\n\t\t");
ab55f58c 135 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
65963943
RK
136 }
137 printf (")");
138 return;
139
140 case MATCH_OPERATOR:
600555f5
KH
141 printf ("gen_rtx_fmt_");
142 for (i = 0; i < XVECLEN (x, 2); i++)
143 printf ("e");
144 printf (" (GET_CODE (operand%d)", XINT (x, 0));
65963943
RK
145 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
146 for (i = 0; i < XVECLEN (x, 2); i++)
147 {
148 printf (",\n\t\t");
ab55f58c 149 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
65963943
RK
150 }
151 printf (")");
152 return;
153
154 case MATCH_PARALLEL:
8fabbfe6 155 case MATCH_PAR_DUP:
65963943
RK
156 printf ("operand%d", XINT (x, 0));
157 return;
158
159 case MATCH_SCRATCH:
ede7cd44 160 gen_rtx_scratch (x, subroutine_type);
65963943
RK
161 return;
162
65963943
RK
163 case PC:
164 printf ("pc_rtx");
165 return;
3810076b
BS
166 case RETURN:
167 printf ("ret_rtx");
168 return;
26898771
BS
169 case SIMPLE_RETURN:
170 printf ("simple_return_rtx");
171 return;
3e89ed8d
JH
172 case CLOBBER:
173 if (REG_P (XEXP (x, 0)))
174 {
175 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
176 REGNO (XEXP (x, 0)));
177 return;
178 }
179 break;
65963943
RK
180
181 case CC0:
182 printf ("cc0_rtx");
183 return;
184
185 case CONST_INT:
186 if (INTVAL (x) == 0)
3d678dca
RS
187 printf ("const0_rtx");
188 else if (INTVAL (x) == 1)
189 printf ("const1_rtx");
190 else if (INTVAL (x) == -1)
191 printf ("constm1_rtx");
11b635fe
KH
192 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
193 && INTVAL (x) <= MAX_SAVED_CONST_INT)
194 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
195 (int) INTVAL (x));
3d678dca
RS
196 else if (INTVAL (x) == STORE_FLAG_VALUE)
197 printf ("const_true_rtx");
198 else
76d31c63
JL
199 {
200 printf ("GEN_INT (");
bb9b3805 201 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
76d31c63
JL
202 printf (")");
203 }
3d678dca
RS
204 return;
205
206 case CONST_DOUBLE:
091a3ac7 207 case CONST_FIXED:
807e902e 208 case CONST_WIDE_INT:
3d678dca
RS
209 /* These shouldn't be written in MD files. Instead, the appropriate
210 routines in varasm.c should be called. */
b2d59f6f 211 gcc_unreachable ();
1d300e19
KG
212
213 default:
214 break;
65963943
RK
215 }
216
3b80f6ca 217 printf ("gen_rtx_");
65963943 218 print_code (code);
f7df4a84
RS
219 printf (" (");
220 if (!always_void_p (code))
221 {
222 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
223 sep = ",\n\t";
224 }
65963943
RK
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;
f7df4a84 232 fputs (sep, stdout);
b2d59f6f 233 switch (fmt[i])
65963943 234 {
b2d59f6f
NS
235 case 'e': case 'u':
236 gen_exp (XEXP (x, i), subroutine_type, used);
237 break;
238
239 case 'i':
240 printf ("%u", XINT (x, i));
241 break;
242
9fccb335
RS
243 case 'r':
244 printf ("%u", REGNO (x));
245 break;
246
b2d59f6f
NS
247 case 's':
248 printf ("\"%s\"", XSTR (x, i));
249 break;
250
251 case 'E':
252 {
253 int j;
254 printf ("gen_rtvec (%d", XVECLEN (x, i));
255 for (j = 0; j < XVECLEN (x, i); j++)
256 {
257 printf (",\n\t\t");
258 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
259 }
260 printf (")");
261 break;
262 }
263
264 default:
265 gcc_unreachable ();
65963943 266 }
f7df4a84 267 sep = ",\n\t";
65963943
RK
268 }
269 printf (")");
3d7aafde 270}
65963943
RK
271\f
272/* Generate the `gen_...' function for a DEFINE_INSN. */
273
274static void
3d7aafde 275gen_insn (rtx insn, int lineno)
65963943 276{
e792559a 277 struct pattern_stats stats;
b3694847 278 int i;
65963943
RK
279
280 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
281 registers or MATCH_SCRATCHes. If so, store away the information for
0f41302f 282 later. */
65963943
RK
283
284 if (XVEC (insn, 1))
285 {
751aa7cc
RK
286 int has_hard_reg = 0;
287
65963943 288 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
751aa7cc
RK
289 {
290 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
291 break;
292
f8cfc6aa 293 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
751aa7cc
RK
294 has_hard_reg = 1;
295 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
296 break;
297 }
65963943
RK
298
299 if (i != XVECLEN (insn, 1) - 1)
300 {
b3694847 301 struct clobber_pat *p;
5ed6ace5 302 struct clobber_ent *link = XNEW (struct clobber_ent);
b3694847 303 int j;
e5f6a288
RK
304
305 link->code_number = insn_code_number;
306
307 /* See if any previous CLOBBER_LIST entry is the same as this
308 one. */
309
310 for (p = clobber_list; p; p = p->next)
311 {
312 if (p->first_clobber != i + 1
313 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
314 continue;
315
316 for (j = i + 1; j < XVECLEN (insn, 1); j++)
317 {
8ad97cfc
KG
318 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
319 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
e5f6a288 320
8ad97cfc 321 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
3d7aafde 322 of the same mode,
e5f6a288 323 or if both are registers of the same mode and number. */
8ad97cfc
KG
324 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
325 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
326 && GET_CODE (new_rtx) == MATCH_SCRATCH)
327 || (REG_P (old_rtx) && REG_P (new_rtx)
328 && REGNO (old_rtx) == REGNO (new_rtx)))))
e5f6a288
RK
329 break;
330 }
3d7aafde 331
e5f6a288
RK
332 if (j == XVECLEN (insn, 1))
333 break;
334 }
335
336 if (p == 0)
337 {
5ed6ace5 338 p = XNEW (struct clobber_pat);
3d7aafde 339
e5f6a288
RK
340 p->insns = 0;
341 p->pattern = insn;
342 p->first_clobber = i + 1;
343 p->next = clobber_list;
751aa7cc 344 p->has_hard_reg = has_hard_reg;
e5f6a288
RK
345 clobber_list = p;
346 }
347
348 link->next = p->insns;
349 p->insns = link;
65963943
RK
350 }
351 }
352
6b6ca844
RK
353 /* Don't mention instructions whose names are the null string
354 or begin with '*'. They are in the machine description just
355 to be recognized. */
356 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
65963943
RK
357 return;
358
d2a3ce4e 359 printf ("/* %s:%d */\n", read_md_filename, lineno);
821e35ba 360
85a45cbb 361 /* Find out how many operands this function has. */
e792559a
RS
362 get_pattern_stats (&stats, XVEC (insn, 1));
363 if (stats.max_dup_opno > stats.max_opno)
65963943
RK
364 fatal ("match_dup operand number has no match_operand");
365
366 /* Output the function name and argument declarations. */
367 printf ("rtx\ngen_%s (", XSTR (insn, 0));
e792559a
RS
368 if (stats.num_generator_args)
369 for (i = 0; i < stats.num_generator_args; i++)
6906ba40
KG
370 if (i)
371 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
372 else
373 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
374 else
375 printf ("void");
65963943 376 printf (")\n");
65963943
RK
377 printf ("{\n");
378
6614fd40 379 /* Output code to construct and return the rtl for the instruction body. */
65963943
RK
380
381 if (XVECLEN (insn, 1) == 1)
382 {
383 printf (" return ");
ab55f58c 384 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
65963943
RK
385 printf (";\n}\n\n");
386 }
387 else
388 {
e792559a 389 char *used = XCNEWVEC (char, stats.num_generator_args);
329b3cc3 390
c5c76735
JL
391 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
392 XVECLEN (insn, 1));
393
65963943
RK
394 for (i = 0; i < XVECLEN (insn, 1); i++)
395 {
396 printf (",\n\t\t");
329b3cc3 397 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
65963943
RK
398 }
399 printf ("));\n}\n\n");
329b3cc3 400 XDELETEVEC (used);
65963943
RK
401 }
402}
403\f
404/* Generate the `gen_...' function for a DEFINE_EXPAND. */
405
406static void
3d7aafde 407gen_expand (rtx expand)
65963943 408{
e792559a 409 struct pattern_stats stats;
b3694847 410 int i;
329b3cc3 411 char *used;
65963943
RK
412
413 if (strlen (XSTR (expand, 0)) == 0)
414 fatal ("define_expand lacks a name");
415 if (XVEC (expand, 1) == 0)
416 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
417
85a45cbb 418 /* Find out how many operands this function has. */
e792559a 419 get_pattern_stats (&stats, XVEC (expand, 1));
65963943
RK
420
421 /* Output the function name and argument declarations. */
422 printf ("rtx\ngen_%s (", XSTR (expand, 0));
e792559a
RS
423 if (stats.num_generator_args)
424 for (i = 0; i < stats.num_generator_args; i++)
6906ba40
KG
425 if (i)
426 printf (",\n\trtx operand%d", i);
427 else
428 printf ("rtx operand%d", i);
429 else
430 printf ("void");
65963943 431 printf (")\n");
65963943
RK
432 printf ("{\n");
433
434 /* If we don't have any C code to write, only one insn is being written,
435 and no MATCH_DUPs are present, we can just return the desired insn
436 like we do for a DEFINE_INSN. This saves memory. */
437 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
e792559a 438 && stats.max_opno >= stats.max_dup_opno
65963943
RK
439 && XVECLEN (expand, 1) == 1)
440 {
441 printf (" return ");
ab55f58c 442 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
65963943
RK
443 printf (";\n}\n\n");
444 return;
445 }
446
447 /* For each operand referred to only with MATCH_DUPs,
448 make a local variable. */
e792559a 449 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
65963943 450 printf (" rtx operand%d;\n", i);
e792559a 451 for (; i <= stats.max_scratch_opno; i++)
b3656137 452 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
65963943
RK
453 printf (" rtx _val = 0;\n");
454 printf (" start_sequence ();\n");
455
456 /* The fourth operand of DEFINE_EXPAND is some code to be executed
457 before the actual construction.
458 This code expects to refer to `operands'
459 just as the output-code in a DEFINE_INSN does,
460 but here `operands' is an automatic array.
461 So copy the operand values there before executing it. */
462 if (XSTR (expand, 3) && *XSTR (expand, 3))
463 {
e2bef702 464 printf (" {\n");
e792559a
RS
465 if (stats.num_operand_vars > 0)
466 printf (" rtx operands[%d];\n", stats.num_operand_vars);
467
65963943 468 /* Output code to copy the arguments into `operands'. */
e792559a 469 for (i = 0; i < stats.num_generator_args; i++)
e2bef702 470 printf (" operands[%d] = operand%d;\n", i, i);
65963943
RK
471
472 /* Output the special code to be executed before the sequence
473 is generated. */
d2a3ce4e 474 print_md_ptr_loc (XSTR (expand, 3));
65963943
RK
475 printf ("%s\n", XSTR (expand, 3));
476
477 /* Output code to copy the arguments back out of `operands'
478 (unless we aren't going to use them at all). */
479 if (XVEC (expand, 1) != 0)
480 {
e792559a 481 for (i = 0; i < stats.num_operand_vars; i++)
60d3aec4
JJ
482 {
483 printf (" operand%d = operands[%d];\n", i, i);
484 printf (" (void) operand%d;\n", i);
485 }
65963943 486 }
e2bef702 487 printf (" }\n");
65963943
RK
488 }
489
490 /* Output code to construct the rtl for the instruction bodies.
491 Use emit_insn to add them to the sequence being accumulated.
492 But don't do this if the user's code has set `no_more' nonzero. */
493
e792559a 494 used = XCNEWVEC (char, stats.num_operand_vars);
329b3cc3 495
65963943
RK
496 for (i = 0; i < XVECLEN (expand, 1); i++)
497 {
498 rtx next = XVECEXP (expand, 1, i);
499 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
500 || (GET_CODE (next) == PARALLEL
79cbc1f7
BS
501 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
502 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
26898771
BS
503 || ANY_RETURN_P (XVECEXP (next, 0, 0))))
504 || ANY_RETURN_P (next))
65963943
RK
505 printf (" emit_jump_insn (");
506 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
507 || GET_CODE (next) == CALL
508 || (GET_CODE (next) == PARALLEL
509 && GET_CODE (XVECEXP (next, 0, 0)) == SET
510 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
511 || (GET_CODE (next) == PARALLEL
512 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
513 printf (" emit_call_insn (");
4b4bf941 514 else if (LABEL_P (next))
65963943
RK
515 printf (" emit_label (");
516 else if (GET_CODE (next) == MATCH_OPERAND
d3ca5cdd 517 || GET_CODE (next) == MATCH_DUP
65963943 518 || GET_CODE (next) == MATCH_OPERATOR
65963943 519 || GET_CODE (next) == MATCH_OP_DUP
d3ca5cdd
JW
520 || GET_CODE (next) == MATCH_PARALLEL
521 || GET_CODE (next) == MATCH_PAR_DUP
65963943
RK
522 || GET_CODE (next) == PARALLEL)
523 printf (" emit (");
524 else
525 printf (" emit_insn (");
329b3cc3 526 gen_exp (next, DEFINE_EXPAND, used);
65963943
RK
527 printf (");\n");
528 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
529 && GET_CODE (SET_SRC (next)) == LABEL_REF)
530 printf (" emit_barrier ();");
531 }
532
329b3cc3
KK
533 XDELETEVEC (used);
534
2f937369 535 /* Call `get_insns' to extract the list of all the
65963943
RK
536 insns emitted within this gen_... function. */
537
2f937369 538 printf (" _val = get_insns ();\n");
65963943
RK
539 printf (" end_sequence ();\n");
540 printf (" return _val;\n}\n\n");
541}
542
2f937369 543/* Like gen_expand, but generates insns resulting from splitting SPLIT. */
0f41302f 544
65963943 545static void
3d7aafde 546gen_split (rtx split)
65963943 547{
e792559a 548 struct pattern_stats stats;
b3694847 549 int i;
27c38fbe
KG
550 const char *const name =
551 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
751aa7cc 552 const char *unused;
ab55f58c 553 char *used;
ede7cd44 554
65963943 555 if (XVEC (split, 0) == 0)
ede7cd44
RH
556 fatal ("define_%s (definition %d) lacks a pattern", name,
557 insn_index_number);
65963943 558 else if (XVEC (split, 2) == 0)
ede7cd44 559 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
3462dc45 560 insn_index_number);
65963943
RK
561
562 /* Find out how many operands this function has. */
563
e792559a
RS
564 get_pattern_stats (&stats, XVEC (split, 2));
565 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
566 used = XCNEWVEC (char, stats.num_operand_vars);
65963943 567
ede7cd44
RH
568 /* Output the prototype, function name and argument declarations. */
569 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
570 {
bb5c4956 571 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
ede7cd44 572 name, insn_code_number);
bb5c4956 573 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
0641776c 574 name, insn_code_number, unused);
ede7cd44
RH
575 }
576 else
577 {
bb5c4956
MM
578 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
579 insn_code_number);
580 printf ("rtx_insn *\ngen_split_%d "
581 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
a406f566 582 insn_code_number, unused);
ede7cd44 583 }
65963943
RK
584 printf ("{\n");
585
586 /* Declare all local variables. */
e792559a 587 for (i = 0; i < stats.num_operand_vars; i++)
65963943 588 printf (" rtx operand%d;\n", i);
bb5c4956 589 printf (" rtx_insn *_val = NULL;\n");
ede7cd44
RH
590
591 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
592 output_peephole2_scratches (split);
593
eddcad13
SB
594 printf (" if (dump_file)\n");
595 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
596 name, insn_code_number);
597
65963943
RK
598 printf (" start_sequence ();\n");
599
600 /* The fourth operand of DEFINE_SPLIT is some code to be executed
601 before the actual construction. */
602
603 if (XSTR (split, 3))
7445392c 604 {
d2a3ce4e 605 print_md_ptr_loc (XSTR (split, 3));
7445392c
RS
606 printf ("%s\n", XSTR (split, 3));
607 }
65963943
RK
608
609 /* Output code to copy the arguments back out of `operands' */
e792559a 610 for (i = 0; i < stats.num_operand_vars; i++)
60d3aec4
JJ
611 {
612 printf (" operand%d = operands[%d];\n", i, i);
613 printf (" (void) operand%d;\n", i);
614 }
65963943
RK
615
616 /* Output code to construct the rtl for the instruction bodies.
617 Use emit_insn to add them to the sequence being accumulated.
618 But don't do this if the user's code has set `no_more' nonzero. */
619
620 for (i = 0; i < XVECLEN (split, 2); i++)
621 {
622 rtx next = XVECEXP (split, 2, i);
623 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
624 || (GET_CODE (next) == PARALLEL
625 && GET_CODE (XVECEXP (next, 0, 0)) == SET
626 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
26898771 627 || ANY_RETURN_P (next))
65963943
RK
628 printf (" emit_jump_insn (");
629 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
630 || GET_CODE (next) == CALL
631 || (GET_CODE (next) == PARALLEL
632 && GET_CODE (XVECEXP (next, 0, 0)) == SET
633 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
634 || (GET_CODE (next) == PARALLEL
635 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
636 printf (" emit_call_insn (");
4b4bf941 637 else if (LABEL_P (next))
65963943
RK
638 printf (" emit_label (");
639 else if (GET_CODE (next) == MATCH_OPERAND
640 || GET_CODE (next) == MATCH_OPERATOR
641 || GET_CODE (next) == MATCH_PARALLEL
642 || GET_CODE (next) == MATCH_OP_DUP
643 || GET_CODE (next) == MATCH_DUP
644 || GET_CODE (next) == PARALLEL)
645 printf (" emit (");
646 else
647 printf (" emit_insn (");
ab55f58c 648 gen_exp (next, GET_CODE (split), used);
65963943
RK
649 printf (");\n");
650 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
651 && GET_CODE (SET_SRC (next)) == LABEL_REF)
652 printf (" emit_barrier ();");
653 }
654
2f937369 655 /* Call `get_insns' to make a list of all the
65963943
RK
656 insns emitted within this gen_... function. */
657
2f937369 658 printf (" _val = get_insns ();\n");
65963943
RK
659 printf (" end_sequence ();\n");
660 printf (" return _val;\n}\n\n");
ab55f58c
RH
661
662 free (used);
65963943
RK
663}
664\f
665/* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
666 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
667 the end of the vector. */
668
669static void
7e51717c 670output_add_clobbers (void)
65963943
RK
671{
672 struct clobber_pat *clobber;
e5f6a288 673 struct clobber_ent *ent;
65963943
RK
674 int i;
675
6906ba40 676 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
65963943 677 printf ("{\n");
65963943
RK
678 printf (" switch (insn_code_number)\n");
679 printf (" {\n");
680
681 for (clobber = clobber_list; clobber; clobber = clobber->next)
682 {
e5f6a288
RK
683 for (ent = clobber->insns; ent; ent = ent->next)
684 printf (" case %d:\n", ent->code_number);
65963943
RK
685
686 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
687 {
688 printf (" XVECEXP (pattern, 0, %d) = ", i);
ede7cd44 689 gen_exp (XVECEXP (clobber->pattern, 1, i),
ab55f58c 690 GET_CODE (clobber->pattern), NULL);
65963943
RK
691 printf (";\n");
692 }
693
e5f6a288 694 printf (" break;\n\n");
65963943
RK
695 }
696
697 printf (" default:\n");
b2d59f6f 698 printf (" gcc_unreachable ();\n");
65963943
RK
699 printf (" }\n");
700 printf ("}\n");
701}
702\f
6327d36a
RK
703/* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
704 number that will have clobbers added (as indicated by `recog') and returns
705 1 if those include a clobber of a hard reg or 0 if all of them just clobber
706 SCRATCH. */
751aa7cc
RK
707
708static void
3d7aafde 709output_added_clobbers_hard_reg_p (void)
751aa7cc
RK
710{
711 struct clobber_pat *clobber;
712 struct clobber_ent *ent;
a29b099d 713 int clobber_p, used;
751aa7cc 714
6906ba40 715 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
751aa7cc
RK
716 printf ("{\n");
717 printf (" switch (insn_code_number)\n");
718 printf (" {\n");
719
720 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
721 {
a29b099d 722 used = 0;
751aa7cc
RK
723 for (clobber = clobber_list; clobber; clobber = clobber->next)
724 if (clobber->has_hard_reg == clobber_p)
725 for (ent = clobber->insns; ent; ent = ent->next)
a29b099d
JJ
726 {
727 printf (" case %d:\n", ent->code_number);
728 used++;
729 }
751aa7cc 730
a29b099d
JJ
731 if (used)
732 printf (" return %d;\n\n", clobber_p);
751aa7cc
RK
733 }
734
735 printf (" default:\n");
b2d59f6f 736 printf (" gcc_unreachable ();\n");
751aa7cc
RK
737 printf (" }\n");
738 printf ("}\n");
739}
740\f
ede7cd44 741/* Generate code to invoke find_free_register () as needed for the
dc297297 742 scratch registers used by the peephole2 pattern in SPLIT. */
ede7cd44
RH
743
744static void
3d7aafde 745output_peephole2_scratches (rtx split)
ede7cd44
RH
746{
747 int i;
748 int insn_nr = 0;
f1ed99cd 749 bool first = true;
ede7cd44
RH
750
751 for (i = 0; i < XVECLEN (split, 0); i++)
752 {
753 rtx elt = XVECEXP (split, 0, i);
754 if (GET_CODE (elt) == MATCH_SCRATCH)
755 {
756 int last_insn_nr = insn_nr;
757 int cur_insn_nr = insn_nr;
758 int j;
759 for (j = i + 1; j < XVECLEN (split, 0); j++)
760 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
761 {
762 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
763 last_insn_nr = cur_insn_nr;
764 }
765 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
766 cur_insn_nr++;
23280139 767
f1ed99cd
JJ
768 if (first)
769 {
770 printf (" HARD_REG_SET _regs_allocated;\n");
771 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
772 first = false;
773 }
774
23280139 775 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
3d7aafde 776 return NULL;\n",
ede7cd44 777 XINT (elt, 0),
23280139 778 insn_nr, last_insn_nr,
ede7cd44
RH
779 XSTR (elt, 1),
780 GET_MODE_NAME (GET_MODE (elt)));
781
782 }
783 else if (GET_CODE (elt) != MATCH_DUP)
784 insn_nr++;
785 }
786}
65963943 787
65963943 788int
3d7aafde 789main (int argc, char **argv)
65963943
RK
790{
791 rtx desc;
65963943 792
f8b6598e 793 progname = "genemit";
65963943 794
600ab3fc 795 if (!init_rtx_reader_args (argc, argv))
c88c0d42 796 return (FATAL_EXIT_CODE);
65963943 797
65963943
RK
798 /* Assign sequential codes to all entries in the machine description
799 in parallel with the tables in insn-output.c. */
800
801 insn_code_number = 0;
802 insn_index_number = 0;
803
804 printf ("/* Generated automatically by the program `genemit'\n\
805from the machine description file `md'. */\n\n");
806
807 printf ("#include \"config.h\"\n");
729da3f5 808 printf ("#include \"system.h\"\n");
4977bab6
ZW
809 printf ("#include \"coretypes.h\"\n");
810 printf ("#include \"tm.h\"\n");
40e23961
MC
811 printf ("#include \"hash-set.h\"\n");
812 printf ("#include \"machmode.h\"\n");
813 printf ("#include \"vec.h\"\n");
814 printf ("#include \"double-int.h\"\n");
815 printf ("#include \"input.h\"\n");
816 printf ("#include \"alias.h\"\n");
817 printf ("#include \"symtab.h\"\n");
818 printf ("#include \"wide-int.h\"\n");
819 printf ("#include \"inchash.h\"\n");
4d648807 820 printf ("#include \"tree.h\"\n");
d8a2d370
DN
821 printf ("#include \"varasm.h\"\n");
822 printf ("#include \"stor-layout.h\"\n");
823 printf ("#include \"calls.h\"\n");
65963943 824 printf ("#include \"rtl.h\"\n");
6baf1cc8 825 printf ("#include \"tm_p.h\"\n");
83685514 826 printf ("#include \"hashtab.h\"\n");
83685514 827 printf ("#include \"hard-reg-set.h\"\n");
49ad7cfa 828 printf ("#include \"function.h\"\n");
36566b39
PK
829 printf ("#include \"flags.h\"\n");
830 printf ("#include \"statistics.h\"\n");
831 printf ("#include \"real.h\"\n");
832 printf ("#include \"fixed-value.h\"\n");
833 printf ("#include \"insn-config.h\"\n");
834 printf ("#include \"expmed.h\"\n");
835 printf ("#include \"dojump.h\"\n");
836 printf ("#include \"explow.h\"\n");
837 printf ("#include \"emit-rtl.h\"\n");
838 printf ("#include \"stmt.h\"\n");
65963943 839 printf ("#include \"expr.h\"\n");
b0710fe1 840 printf ("#include \"insn-codes.h\"\n");
e78d8e51 841 printf ("#include \"optabs.h\"\n");
c413e1b5 842 printf ("#include \"dfp.h\"\n");
65963943 843 printf ("#include \"output.h\"\n");
23280139 844 printf ("#include \"recog.h\"\n");
60393bbc
AM
845 printf ("#include \"predict.h\"\n");
846 printf ("#include \"basic-block.h\"\n");
ede7cd44 847 printf ("#include \"resource.h\"\n");
d80eb1e1 848 printf ("#include \"reload.h\"\n");
79a3f089 849 printf ("#include \"diagnostic-core.h\"\n");
5abc5de9 850 printf ("#include \"regs.h\"\n");
279bb624 851 printf ("#include \"tm-constrs.h\"\n");
5abc5de9
EC
852 printf ("#include \"ggc.h\"\n");
853 printf ("#include \"basic-block.h\"\n");
eddcad13 854 printf ("#include \"dumpfile.h\"\n");
3b1552c1 855 printf ("#include \"target.h\"\n\n");
53bfe9cd 856 printf ("#define FAIL return (end_sequence (), _val)\n");
2f937369 857 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
65963943
RK
858
859 /* Read the machine description. */
860
861 while (1)
862 {
c88c0d42 863 int line_no;
65963943 864
c88c0d42
CP
865 desc = read_md_rtx (&line_no, &insn_code_number);
866 if (desc == NULL)
867 break;
ede7cd44 868
c88c0d42 869 switch (GET_CODE (desc))
65963943 870 {
821e35ba
RH
871 case DEFINE_INSN:
872 gen_insn (desc, line_no);
873 break;
874
875 case DEFINE_EXPAND:
d2a3ce4e 876 printf ("/* %s:%d */\n", read_md_filename, line_no);
821e35ba
RH
877 gen_expand (desc);
878 break;
879
880 case DEFINE_SPLIT:
d2a3ce4e 881 printf ("/* %s:%d */\n", read_md_filename, line_no);
821e35ba
RH
882 gen_split (desc);
883 break;
884
885 case DEFINE_PEEPHOLE2:
d2a3ce4e 886 printf ("/* %s:%d */\n", read_md_filename, line_no);
821e35ba
RH
887 gen_split (desc);
888 break;
889
890 default:
891 break;
892 }
65963943
RK
893 ++insn_index_number;
894 }
895
751aa7cc
RK
896 /* Write out the routines to add CLOBBERs to a pattern and say whether they
897 clobber a hard reg. */
65963943 898 output_add_clobbers ();
751aa7cc 899 output_added_clobbers_hard_reg_p ();
65963943 900
65963943 901 fflush (stdout);
c1b59dce 902 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
65963943 903}