]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genemit.c
cgraph.c: Add overall comment.
[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,
d9221e01 3 2003, 2004 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
9Software Foundation; either version 2, or (at your option) any later
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
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
65963943
RK
21
22
4977bab6 23#include "bconfig.h"
0b93b64e 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
65963943 27#include "rtl.h"
f8b6598e 28#include "errors.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:
261 /* These shouldn't be written in MD files. Instead, the appropriate
262 routines in varasm.c should be called. */
263 abort ();
1d300e19
KG
264
265 default:
266 break;
65963943
RK
267 }
268
3b80f6ca 269 printf ("gen_rtx_");
65963943 270 print_code (code);
3b80f6ca 271 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
65963943
RK
272
273 fmt = GET_RTX_FORMAT (code);
274 len = GET_RTX_LENGTH (code);
275 for (i = 0; i < len; i++)
276 {
277 if (fmt[i] == '0')
278 break;
a558d864 279 printf (",\n\t");
65963943 280 if (fmt[i] == 'e' || fmt[i] == 'u')
ab55f58c 281 gen_exp (XEXP (x, i), subroutine_type, used);
65963943 282 else if (fmt[i] == 'i')
3d678dca 283 printf ("%u", XINT (x, i));
65963943
RK
284 else if (fmt[i] == 's')
285 printf ("\"%s\"", XSTR (x, i));
286 else if (fmt[i] == 'E')
287 {
288 int j;
289 printf ("gen_rtvec (%d", XVECLEN (x, i));
290 for (j = 0; j < XVECLEN (x, i); j++)
291 {
292 printf (",\n\t\t");
ab55f58c 293 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
65963943
RK
294 }
295 printf (")");
296 }
297 else
298 abort ();
299 }
300 printf (")");
3d7aafde 301}
65963943
RK
302\f
303/* Generate the `gen_...' function for a DEFINE_INSN. */
304
305static void
3d7aafde 306gen_insn (rtx insn, int lineno)
65963943
RK
307{
308 int operands;
b3694847 309 int i;
65963943
RK
310
311 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
312 registers or MATCH_SCRATCHes. If so, store away the information for
0f41302f 313 later. */
65963943
RK
314
315 if (XVEC (insn, 1))
316 {
751aa7cc
RK
317 int has_hard_reg = 0;
318
65963943 319 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
751aa7cc
RK
320 {
321 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
322 break;
323
324 if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) == REG)
325 has_hard_reg = 1;
326 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
327 break;
328 }
65963943
RK
329
330 if (i != XVECLEN (insn, 1) - 1)
331 {
b3694847 332 struct clobber_pat *p;
703ad42b 333 struct clobber_ent *link = xmalloc (sizeof (struct clobber_ent));
b3694847 334 int j;
e5f6a288
RK
335
336 link->code_number = insn_code_number;
337
338 /* See if any previous CLOBBER_LIST entry is the same as this
339 one. */
340
341 for (p = clobber_list; p; p = p->next)
342 {
343 if (p->first_clobber != i + 1
344 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
345 continue;
346
347 for (j = i + 1; j < XVECLEN (insn, 1); j++)
348 {
349 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
350 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
351
352 /* OLD and NEW are the same if both are to be a SCRATCH
3d7aafde 353 of the same mode,
e5f6a288 354 or if both are registers of the same mode and number. */
f5f178e0
RS
355 if (! (GET_MODE (old) == GET_MODE (new)
356 && ((GET_CODE (old) == MATCH_SCRATCH
357 && GET_CODE (new) == MATCH_SCRATCH)
358 || (GET_CODE (old) == REG && GET_CODE (new) == REG
359 && REGNO (old) == REGNO (new)))))
e5f6a288
RK
360 break;
361 }
3d7aafde 362
e5f6a288
RK
363 if (j == XVECLEN (insn, 1))
364 break;
365 }
366
367 if (p == 0)
368 {
703ad42b 369 p = xmalloc (sizeof (struct clobber_pat));
3d7aafde 370
e5f6a288
RK
371 p->insns = 0;
372 p->pattern = insn;
373 p->first_clobber = i + 1;
374 p->next = clobber_list;
751aa7cc 375 p->has_hard_reg = has_hard_reg;
e5f6a288
RK
376 clobber_list = p;
377 }
378
379 link->next = p->insns;
380 p->insns = link;
65963943
RK
381 }
382 }
383
6b6ca844
RK
384 /* Don't mention instructions whose names are the null string
385 or begin with '*'. They are in the machine description just
386 to be recognized. */
387 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
65963943
RK
388 return;
389
821e35ba
RH
390 printf ("/* %s:%d */\n", read_rtx_filename, lineno);
391
85a45cbb 392 /* Find out how many operands this function has. */
65963943
RK
393 operands = max_operand_vec (insn, 1);
394 if (max_dup_opno >= operands)
395 fatal ("match_dup operand number has no match_operand");
396
397 /* Output the function name and argument declarations. */
398 printf ("rtx\ngen_%s (", XSTR (insn, 0));
6906ba40
KG
399 if (operands)
400 for (i = 0; i < operands; i++)
401 if (i)
402 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
403 else
404 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
405 else
406 printf ("void");
65963943 407 printf (")\n");
65963943
RK
408 printf ("{\n");
409
6614fd40 410 /* Output code to construct and return the rtl for the instruction body. */
65963943
RK
411
412 if (XVECLEN (insn, 1) == 1)
413 {
414 printf (" return ");
ab55f58c 415 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
65963943
RK
416 printf (";\n}\n\n");
417 }
418 else
419 {
c5c76735
JL
420 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
421 XVECLEN (insn, 1));
422
65963943
RK
423 for (i = 0; i < XVECLEN (insn, 1); i++)
424 {
425 printf (",\n\t\t");
ab55f58c 426 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, NULL);
65963943
RK
427 }
428 printf ("));\n}\n\n");
429 }
430}
431\f
432/* Generate the `gen_...' function for a DEFINE_EXPAND. */
433
434static void
3d7aafde 435gen_expand (rtx expand)
65963943
RK
436{
437 int operands;
b3694847 438 int i;
65963943
RK
439
440 if (strlen (XSTR (expand, 0)) == 0)
441 fatal ("define_expand lacks a name");
442 if (XVEC (expand, 1) == 0)
443 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
444
85a45cbb 445 /* Find out how many operands this function has. */
65963943
RK
446 operands = max_operand_vec (expand, 1);
447
448 /* Output the function name and argument declarations. */
449 printf ("rtx\ngen_%s (", XSTR (expand, 0));
6906ba40
KG
450 if (operands)
451 for (i = 0; i < operands; i++)
452 if (i)
453 printf (",\n\trtx operand%d", i);
454 else
455 printf ("rtx operand%d", i);
456 else
457 printf ("void");
65963943 458 printf (")\n");
65963943
RK
459 printf ("{\n");
460
461 /* If we don't have any C code to write, only one insn is being written,
462 and no MATCH_DUPs are present, we can just return the desired insn
463 like we do for a DEFINE_INSN. This saves memory. */
464 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
465 && operands > max_dup_opno
466 && XVECLEN (expand, 1) == 1)
467 {
468 printf (" return ");
ab55f58c 469 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
65963943
RK
470 printf (";\n}\n\n");
471 return;
472 }
473
474 /* For each operand referred to only with MATCH_DUPs,
475 make a local variable. */
476 for (i = operands; i <= max_dup_opno; i++)
477 printf (" rtx operand%d;\n", i);
ede7cd44 478 for (; i <= max_scratch_opno; i++)
b3656137 479 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
65963943
RK
480 printf (" rtx _val = 0;\n");
481 printf (" start_sequence ();\n");
482
483 /* The fourth operand of DEFINE_EXPAND is some code to be executed
484 before the actual construction.
485 This code expects to refer to `operands'
486 just as the output-code in a DEFINE_INSN does,
487 but here `operands' is an automatic array.
488 So copy the operand values there before executing it. */
489 if (XSTR (expand, 3) && *XSTR (expand, 3))
490 {
e2bef702
RH
491 printf (" {\n");
492 if (operands > 0 || max_dup_opno >= 0 || max_scratch_opno >= 0)
493 printf (" rtx operands[%d];\n",
494 MAX (operands, MAX (max_scratch_opno, max_dup_opno) + 1));
65963943
RK
495 /* Output code to copy the arguments into `operands'. */
496 for (i = 0; i < operands; i++)
e2bef702 497 printf (" operands[%d] = operand%d;\n", i, i);
65963943
RK
498
499 /* Output the special code to be executed before the sequence
500 is generated. */
501 printf ("%s\n", XSTR (expand, 3));
502
503 /* Output code to copy the arguments back out of `operands'
504 (unless we aren't going to use them at all). */
505 if (XVEC (expand, 1) != 0)
506 {
507 for (i = 0; i < operands; i++)
e2bef702 508 printf (" operand%d = operands[%d];\n", i, i);
65963943 509 for (; i <= max_dup_opno; i++)
e2bef702 510 printf (" operand%d = operands[%d];\n", i, i);
ede7cd44 511 for (; i <= max_scratch_opno; i++)
e2bef702 512 printf (" operand%d = operands[%d];\n", i, i);
65963943 513 }
e2bef702 514 printf (" }\n");
65963943
RK
515 }
516
517 /* Output code to construct the rtl for the instruction bodies.
518 Use emit_insn to add them to the sequence being accumulated.
519 But don't do this if the user's code has set `no_more' nonzero. */
520
521 for (i = 0; i < XVECLEN (expand, 1); i++)
522 {
523 rtx next = XVECEXP (expand, 1, i);
524 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
525 || (GET_CODE (next) == PARALLEL
79cbc1f7
BS
526 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
527 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
528 || GET_CODE (XVECEXP (next, 0, 0)) == RETURN))
65963943
RK
529 || GET_CODE (next) == RETURN)
530 printf (" emit_jump_insn (");
531 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
532 || GET_CODE (next) == CALL
533 || (GET_CODE (next) == PARALLEL
534 && GET_CODE (XVECEXP (next, 0, 0)) == SET
535 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
536 || (GET_CODE (next) == PARALLEL
537 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
538 printf (" emit_call_insn (");
539 else if (GET_CODE (next) == CODE_LABEL)
540 printf (" emit_label (");
541 else if (GET_CODE (next) == MATCH_OPERAND
d3ca5cdd 542 || GET_CODE (next) == MATCH_DUP
65963943 543 || GET_CODE (next) == MATCH_OPERATOR
65963943 544 || GET_CODE (next) == MATCH_OP_DUP
d3ca5cdd
JW
545 || GET_CODE (next) == MATCH_PARALLEL
546 || GET_CODE (next) == MATCH_PAR_DUP
65963943
RK
547 || GET_CODE (next) == PARALLEL)
548 printf (" emit (");
549 else
550 printf (" emit_insn (");
ab55f58c 551 gen_exp (next, DEFINE_EXPAND, NULL);
65963943
RK
552 printf (");\n");
553 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
554 && GET_CODE (SET_SRC (next)) == LABEL_REF)
555 printf (" emit_barrier ();");
556 }
557
2f937369 558 /* Call `get_insns' to extract the list of all the
65963943
RK
559 insns emitted within this gen_... function. */
560
2f937369 561 printf (" _val = get_insns ();\n");
65963943
RK
562 printf (" end_sequence ();\n");
563 printf (" return _val;\n}\n\n");
564}
565
2f937369 566/* Like gen_expand, but generates insns resulting from splitting SPLIT. */
0f41302f 567
65963943 568static void
3d7aafde 569gen_split (rtx split)
65963943 570{
b3694847 571 int i;
65963943 572 int operands;
27c38fbe
KG
573 const char *const name =
574 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
751aa7cc 575 const char *unused;
ab55f58c 576 char *used;
ede7cd44 577
65963943 578 if (XVEC (split, 0) == 0)
ede7cd44
RH
579 fatal ("define_%s (definition %d) lacks a pattern", name,
580 insn_index_number);
65963943 581 else if (XVEC (split, 2) == 0)
ede7cd44 582 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
3462dc45 583 insn_index_number);
65963943
RK
584
585 /* Find out how many operands this function has. */
586
587 max_operand_vec (split, 2);
ede7cd44 588 operands = MAX (max_opno, MAX (max_dup_opno, max_scratch_opno)) + 1;
751aa7cc 589 unused = (operands == 0 ? " ATTRIBUTE_UNUSED" : "");
ab55f58c 590 used = xcalloc (1, operands);
65963943 591
ede7cd44
RH
592 /* Output the prototype, function name and argument declarations. */
593 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
594 {
3d7aafde 595 printf ("extern rtx gen_%s_%d (rtx, rtx *);\n",
ede7cd44 596 name, insn_code_number);
0641776c
AJ
597 printf ("rtx\ngen_%s_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
598 name, insn_code_number, unused);
ede7cd44
RH
599 }
600 else
601 {
3d7aafde 602 printf ("extern rtx gen_split_%d (rtx *);\n", insn_code_number);
0641776c 603 printf ("rtx\ngen_%s_%d (rtx *operands%s)\n", name, insn_code_number, unused);
ede7cd44 604 }
65963943
RK
605 printf ("{\n");
606
607 /* Declare all local variables. */
608 for (i = 0; i < operands; i++)
609 printf (" rtx operand%d;\n", i);
91afd8f5 610 printf (" rtx _val = 0;\n");
ede7cd44
RH
611
612 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
613 output_peephole2_scratches (split);
614
65963943
RK
615 printf (" start_sequence ();\n");
616
617 /* The fourth operand of DEFINE_SPLIT is some code to be executed
618 before the actual construction. */
619
620 if (XSTR (split, 3))
621 printf ("%s\n", XSTR (split, 3));
622
623 /* Output code to copy the arguments back out of `operands' */
624 for (i = 0; i < operands; i++)
625 printf (" operand%d = operands[%d];\n", i, i);
626
627 /* Output code to construct the rtl for the instruction bodies.
628 Use emit_insn to add them to the sequence being accumulated.
629 But don't do this if the user's code has set `no_more' nonzero. */
630
631 for (i = 0; i < XVECLEN (split, 2); i++)
632 {
633 rtx next = XVECEXP (split, 2, i);
634 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
635 || (GET_CODE (next) == PARALLEL
636 && GET_CODE (XVECEXP (next, 0, 0)) == SET
637 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
638 || GET_CODE (next) == RETURN)
639 printf (" emit_jump_insn (");
640 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
641 || GET_CODE (next) == CALL
642 || (GET_CODE (next) == PARALLEL
643 && GET_CODE (XVECEXP (next, 0, 0)) == SET
644 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
645 || (GET_CODE (next) == PARALLEL
646 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
647 printf (" emit_call_insn (");
648 else if (GET_CODE (next) == CODE_LABEL)
649 printf (" emit_label (");
650 else if (GET_CODE (next) == MATCH_OPERAND
651 || GET_CODE (next) == MATCH_OPERATOR
652 || GET_CODE (next) == MATCH_PARALLEL
653 || GET_CODE (next) == MATCH_OP_DUP
654 || GET_CODE (next) == MATCH_DUP
655 || GET_CODE (next) == PARALLEL)
656 printf (" emit (");
657 else
658 printf (" emit_insn (");
ab55f58c 659 gen_exp (next, GET_CODE (split), used);
65963943
RK
660 printf (");\n");
661 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
662 && GET_CODE (SET_SRC (next)) == LABEL_REF)
663 printf (" emit_barrier ();");
664 }
665
2f937369 666 /* Call `get_insns' to make a list of all the
65963943
RK
667 insns emitted within this gen_... function. */
668
2f937369 669 printf (" _val = get_insns ();\n");
65963943
RK
670 printf (" end_sequence ();\n");
671 printf (" return _val;\n}\n\n");
ab55f58c
RH
672
673 free (used);
65963943
RK
674}
675\f
676/* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
677 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
678 the end of the vector. */
679
680static void
7e51717c 681output_add_clobbers (void)
65963943
RK
682{
683 struct clobber_pat *clobber;
e5f6a288 684 struct clobber_ent *ent;
65963943
RK
685 int i;
686
6906ba40 687 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
65963943 688 printf ("{\n");
65963943
RK
689 printf (" switch (insn_code_number)\n");
690 printf (" {\n");
691
692 for (clobber = clobber_list; clobber; clobber = clobber->next)
693 {
e5f6a288
RK
694 for (ent = clobber->insns; ent; ent = ent->next)
695 printf (" case %d:\n", ent->code_number);
65963943
RK
696
697 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
698 {
699 printf (" XVECEXP (pattern, 0, %d) = ", i);
ede7cd44 700 gen_exp (XVECEXP (clobber->pattern, 1, i),
ab55f58c 701 GET_CODE (clobber->pattern), NULL);
65963943
RK
702 printf (";\n");
703 }
704
e5f6a288 705 printf (" break;\n\n");
65963943
RK
706 }
707
708 printf (" default:\n");
709 printf (" abort ();\n");
710 printf (" }\n");
711 printf ("}\n");
712}
713\f
6327d36a
RK
714/* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
715 number that will have clobbers added (as indicated by `recog') and returns
716 1 if those include a clobber of a hard reg or 0 if all of them just clobber
717 SCRATCH. */
751aa7cc
RK
718
719static void
3d7aafde 720output_added_clobbers_hard_reg_p (void)
751aa7cc
RK
721{
722 struct clobber_pat *clobber;
723 struct clobber_ent *ent;
a29b099d 724 int clobber_p, used;
751aa7cc 725
6906ba40 726 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
751aa7cc
RK
727 printf ("{\n");
728 printf (" switch (insn_code_number)\n");
729 printf (" {\n");
730
731 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
732 {
a29b099d 733 used = 0;
751aa7cc
RK
734 for (clobber = clobber_list; clobber; clobber = clobber->next)
735 if (clobber->has_hard_reg == clobber_p)
736 for (ent = clobber->insns; ent; ent = ent->next)
a29b099d
JJ
737 {
738 printf (" case %d:\n", ent->code_number);
739 used++;
740 }
751aa7cc 741
a29b099d
JJ
742 if (used)
743 printf (" return %d;\n\n", clobber_p);
751aa7cc
RK
744 }
745
746 printf (" default:\n");
747 printf (" abort ();\n");
748 printf (" }\n");
749 printf ("}\n");
750}
751\f
ede7cd44 752/* Generate code to invoke find_free_register () as needed for the
dc297297 753 scratch registers used by the peephole2 pattern in SPLIT. */
ede7cd44
RH
754
755static void
3d7aafde 756output_peephole2_scratches (rtx split)
ede7cd44
RH
757{
758 int i;
759 int insn_nr = 0;
760
ede7cd44 761 printf (" HARD_REG_SET _regs_allocated;\n");
ede7cd44
RH
762 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
763
764 for (i = 0; i < XVECLEN (split, 0); i++)
765 {
766 rtx elt = XVECEXP (split, 0, i);
767 if (GET_CODE (elt) == MATCH_SCRATCH)
768 {
769 int last_insn_nr = insn_nr;
770 int cur_insn_nr = insn_nr;
771 int j;
772 for (j = i + 1; j < XVECLEN (split, 0); j++)
773 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
774 {
775 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
776 last_insn_nr = cur_insn_nr;
777 }
778 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
779 cur_insn_nr++;
23280139
RH
780
781 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
3d7aafde 782 return NULL;\n",
ede7cd44 783 XINT (elt, 0),
23280139 784 insn_nr, last_insn_nr,
ede7cd44
RH
785 XSTR (elt, 1),
786 GET_MODE_NAME (GET_MODE (elt)));
787
788 }
789 else if (GET_CODE (elt) != MATCH_DUP)
790 insn_nr++;
791 }
792}
65963943 793
65963943 794int
3d7aafde 795main (int argc, char **argv)
65963943
RK
796{
797 rtx desc;
65963943 798
f8b6598e 799 progname = "genemit";
65963943
RK
800
801 if (argc <= 1)
1f978f5f 802 fatal ("no input file name");
65963943 803
04d8aa70 804 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
c88c0d42 805 return (FATAL_EXIT_CODE);
65963943 806
65963943
RK
807 /* Assign sequential codes to all entries in the machine description
808 in parallel with the tables in insn-output.c. */
809
810 insn_code_number = 0;
811 insn_index_number = 0;
812
813 printf ("/* Generated automatically by the program `genemit'\n\
814from the machine description file `md'. */\n\n");
815
816 printf ("#include \"config.h\"\n");
729da3f5 817 printf ("#include \"system.h\"\n");
4977bab6
ZW
818 printf ("#include \"coretypes.h\"\n");
819 printf ("#include \"tm.h\"\n");
65963943 820 printf ("#include \"rtl.h\"\n");
6baf1cc8 821 printf ("#include \"tm_p.h\"\n");
49ad7cfa 822 printf ("#include \"function.h\"\n");
65963943 823 printf ("#include \"expr.h\"\n");
e78d8e51 824 printf ("#include \"optabs.h\"\n");
65963943 825 printf ("#include \"real.h\"\n");
1d300e19 826 printf ("#include \"flags.h\"\n");
65963943 827 printf ("#include \"output.h\"\n");
7f7f8214 828 printf ("#include \"insn-config.h\"\n");
ede7cd44 829 printf ("#include \"hard-reg-set.h\"\n");
23280139 830 printf ("#include \"recog.h\"\n");
ede7cd44 831 printf ("#include \"resource.h\"\n");
d80eb1e1 832 printf ("#include \"reload.h\"\n");
04f378ce 833 printf ("#include \"toplev.h\"\n");
d80eb1e1 834 printf ("#include \"ggc.h\"\n\n");
53bfe9cd 835 printf ("#define FAIL return (end_sequence (), _val)\n");
2f937369 836 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
65963943
RK
837
838 /* Read the machine description. */
839
840 while (1)
841 {
c88c0d42 842 int line_no;
65963943 843
c88c0d42
CP
844 desc = read_md_rtx (&line_no, &insn_code_number);
845 if (desc == NULL)
846 break;
ede7cd44 847
c88c0d42 848 switch (GET_CODE (desc))
65963943 849 {
821e35ba
RH
850 case DEFINE_INSN:
851 gen_insn (desc, line_no);
852 break;
853
854 case DEFINE_EXPAND:
855 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
856 gen_expand (desc);
857 break;
858
859 case DEFINE_SPLIT:
860 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
861 gen_split (desc);
862 break;
863
864 case DEFINE_PEEPHOLE2:
865 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
866 gen_split (desc);
867 break;
868
869 default:
870 break;
871 }
65963943
RK
872 ++insn_index_number;
873 }
874
751aa7cc
RK
875 /* Write out the routines to add CLOBBERs to a pattern and say whether they
876 clobber a hard reg. */
65963943 877 output_add_clobbers ();
751aa7cc 878 output_added_clobbers_hard_reg_p ();
65963943 879
65963943 880 fflush (stdout);
c1b59dce 881 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
65963943 882}
a995e389
RH
883
884/* Define this so we can link with print-rtl.o to get debug_rtx function. */
885const char *
3d7aafde 886get_insn_name (int code ATTRIBUTE_UNUSED)
a995e389
RH
887{
888 return NULL;
889}