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