1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 88, 91, 94, 95, 97, 98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 static struct obstack obstack
;
28 struct obstack
*rtl_obstack
= &obstack
;
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
33 void fatal
PVPROTO ((const char *, ...))
34 ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
35 void fancy_abort
PROTO((void)) ATTRIBUTE_NORETURN
;
37 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
38 char **insn_name_ptr
= 0;
41 static int max_dup_opno
;
42 static int register_constraints
;
43 static int insn_code_number
;
44 static int insn_index_number
;
46 /* Data structure for recording the patterns of insns that have CLOBBERs.
47 We use this to output a function that adds these CLOBBERs to a
48 previously-allocated PARALLEL expression. */
52 struct clobber_ent
*insns
;
55 struct clobber_pat
*next
;
58 /* Records one insn that uses the clobber list. */
62 int code_number
; /* Counts only insns. */
63 struct clobber_ent
*next
;
66 static void max_operand_1
PROTO((rtx
));
67 static int max_operand_vec
PROTO((rtx
, int));
68 static void print_code
PROTO((RTX_CODE
));
69 static void gen_exp
PROTO((rtx
));
70 static void gen_insn
PROTO((rtx
));
71 static void gen_expand
PROTO((rtx
));
72 static void gen_split
PROTO((rtx
));
73 static void output_add_clobbers
PROTO((void));
74 static void output_init_mov_optab
PROTO((void));
81 register RTX_CODE code
;
91 if (code
== MATCH_OPERAND
&& XSTR (x
, 2) != 0 && *XSTR (x
, 2) != '\0')
92 register_constraints
= 1;
93 if (code
== MATCH_SCRATCH
&& XSTR (x
, 1) != 0 && *XSTR (x
, 1) != '\0')
94 register_constraints
= 1;
95 if (code
== MATCH_OPERAND
|| code
== MATCH_OPERATOR
96 || code
== MATCH_PARALLEL
)
97 max_opno
= MAX (max_opno
, XINT (x
, 0));
98 if (code
== MATCH_DUP
|| code
== MATCH_OP_DUP
|| code
== MATCH_PAR_DUP
)
99 max_dup_opno
= MAX (max_dup_opno
, XINT (x
, 0));
101 fmt
= GET_RTX_FORMAT (code
);
102 len
= GET_RTX_LENGTH (code
);
103 for (i
= 0; i
< len
; i
++)
105 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
106 max_operand_1 (XEXP (x
, i
));
107 else if (fmt
[i
] == 'E')
110 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
111 max_operand_1 (XVECEXP (x
, i
, j
));
117 max_operand_vec (insn
, arg
)
121 register int len
= XVECLEN (insn
, arg
);
127 for (i
= 0; i
< len
; i
++)
128 max_operand_1 (XVECEXP (insn
, arg
, i
));
138 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
140 if (*p1
>= 'a' && *p1
<= 'z')
141 putchar (*p1
+ 'A' - 'a');
147 /* Print a C expression to construct an RTX just like X,
148 substituting any operand references appearing within. */
154 register RTX_CODE code
;
171 printf ("operand%d", XINT (x
, 0));
175 printf ("gen_rtx (GET_CODE (operand%d), ", XINT (x
, 0));
176 if (GET_MODE (x
) == VOIDmode
)
177 printf ("GET_MODE (operand%d)", XINT (x
, 0));
179 printf ("%smode", GET_MODE_NAME (GET_MODE (x
)));
180 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
183 gen_exp (XVECEXP (x
, 1, i
));
189 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x
, 0));
190 printf (", %smode", GET_MODE_NAME (GET_MODE (x
)));
191 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
194 gen_exp (XVECEXP (x
, 2, i
));
201 printf ("operand%d", XINT (x
, 0));
205 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x
)));
209 fatal ("ADDRESS expression code used in named instruction pattern");
221 printf ("const0_rtx");
222 else if (INTVAL (x
) == 1)
223 printf ("const1_rtx");
224 else if (INTVAL (x
) == -1)
225 printf ("constm1_rtx");
226 else if (INTVAL (x
) == STORE_FLAG_VALUE
)
227 printf ("const_true_rtx");
230 printf ("GEN_INT (");
231 printf (HOST_WIDE_INT_PRINT_DEC
, INTVAL (x
));
237 /* These shouldn't be written in MD files. Instead, the appropriate
238 routines in varasm.c should be called. */
247 printf (" (%smode", GET_MODE_NAME (GET_MODE (x
)));
249 fmt
= GET_RTX_FORMAT (code
);
250 len
= GET_RTX_LENGTH (code
);
251 for (i
= 0; i
< len
; i
++)
256 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
257 gen_exp (XEXP (x
, i
));
258 else if (fmt
[i
] == 'i')
259 printf ("%u", XINT (x
, i
));
260 else if (fmt
[i
] == 's')
261 printf ("\"%s\"", XSTR (x
, i
));
262 else if (fmt
[i
] == 'E')
265 printf ("gen_rtvec (%d", XVECLEN (x
, i
));
266 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
269 gen_exp (XVECEXP (x
, i
, j
));
279 /* Generate the `gen_...' function for a DEFINE_INSN. */
288 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
289 registers or MATCH_SCRATCHes. If so, store away the information for
294 for (i
= XVECLEN (insn
, 1) - 1; i
> 0; i
--)
295 if (GET_CODE (XVECEXP (insn
, 1, i
)) != CLOBBER
296 || (GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != REG
297 && GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != MATCH_SCRATCH
))
300 if (i
!= XVECLEN (insn
, 1) - 1)
302 register struct clobber_pat
*p
;
303 register struct clobber_ent
*link
304 = (struct clobber_ent
*) xmalloc (sizeof (struct clobber_ent
));
307 link
->code_number
= insn_code_number
;
309 /* See if any previous CLOBBER_LIST entry is the same as this
312 for (p
= clobber_list
; p
; p
= p
->next
)
314 if (p
->first_clobber
!= i
+ 1
315 || XVECLEN (p
->pattern
, 1) != XVECLEN (insn
, 1))
318 for (j
= i
+ 1; j
< XVECLEN (insn
, 1); j
++)
320 rtx old
= XEXP (XVECEXP (p
->pattern
, 1, j
), 0);
321 rtx
new = XEXP (XVECEXP (insn
, 1, j
), 0);
323 /* OLD and NEW are the same if both are to be a SCRATCH
325 or if both are registers of the same mode and number. */
326 if (! (GET_MODE (old
) == GET_MODE (new)
327 && ((GET_CODE (old
) == MATCH_SCRATCH
328 && GET_CODE (new) == MATCH_SCRATCH
)
329 || (GET_CODE (old
) == REG
&& GET_CODE (new) == REG
330 && REGNO (old
) == REGNO (new)))))
334 if (j
== XVECLEN (insn
, 1))
340 p
= (struct clobber_pat
*) xmalloc (sizeof (struct clobber_pat
));
344 p
->first_clobber
= i
+ 1;
345 p
->next
= clobber_list
;
349 link
->next
= p
->insns
;
354 /* Don't mention instructions whose names are the null string
355 or begin with '*'. They are in the machine description just
357 if (XSTR (insn
, 0)[0] == 0 || XSTR (insn
, 0)[0] == '*')
360 /* Find out how many operands this function has,
361 and also whether any of them have register constraints. */
362 register_constraints
= 0;
363 operands
= max_operand_vec (insn
, 1);
364 if (max_dup_opno
>= operands
)
365 fatal ("match_dup operand number has no match_operand");
367 /* Output the function name and argument declarations. */
368 printf ("rtx\ngen_%s (", XSTR (insn
, 0));
369 for (i
= 0; i
< operands
; i
++)
370 printf (i
? ", operand%d" : "operand%d", i
);
372 for (i
= 0; i
< operands
; i
++)
373 printf (" rtx operand%d;\n", i
);
376 /* Output code to construct and return the rtl for the instruction body */
378 if (XVECLEN (insn
, 1) == 1)
381 gen_exp (XVECEXP (insn
, 1, 0));
386 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn
, 1));
387 for (i
= 0; i
< XVECLEN (insn
, 1); i
++)
390 gen_exp (XVECEXP (insn
, 1, i
));
392 printf ("));\n}\n\n");
396 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
405 if (strlen (XSTR (expand
, 0)) == 0)
406 fatal ("define_expand lacks a name");
407 if (XVEC (expand
, 1) == 0)
408 fatal ("define_expand for %s lacks a pattern", XSTR (expand
, 0));
410 /* Find out how many operands this function has,
411 and also whether any of them have register constraints. */
412 register_constraints
= 0;
414 operands
= max_operand_vec (expand
, 1);
416 /* Output the function name and argument declarations. */
417 printf ("rtx\ngen_%s (", XSTR (expand
, 0));
418 for (i
= 0; i
< operands
; i
++)
419 printf (i
? ", operand%d" : "operand%d", i
);
421 for (i
= 0; i
< operands
; i
++)
422 printf (" rtx operand%d;\n", i
);
425 /* If we don't have any C code to write, only one insn is being written,
426 and no MATCH_DUPs are present, we can just return the desired insn
427 like we do for a DEFINE_INSN. This saves memory. */
428 if ((XSTR (expand
, 3) == 0 || *XSTR (expand
, 3) == '\0')
429 && operands
> max_dup_opno
430 && XVECLEN (expand
, 1) == 1)
433 gen_exp (XVECEXP (expand
, 1, 0));
438 /* For each operand referred to only with MATCH_DUPs,
439 make a local variable. */
440 for (i
= operands
; i
<= max_dup_opno
; i
++)
441 printf (" rtx operand%d;\n", i
);
442 if (operands
> 0 || max_dup_opno
>= 0)
443 printf (" rtx operands[%d];\n", MAX (operands
, max_dup_opno
+ 1));
444 printf (" rtx _val = 0;\n");
445 printf (" start_sequence ();\n");
447 /* The fourth operand of DEFINE_EXPAND is some code to be executed
448 before the actual construction.
449 This code expects to refer to `operands'
450 just as the output-code in a DEFINE_INSN does,
451 but here `operands' is an automatic array.
452 So copy the operand values there before executing it. */
453 if (XSTR (expand
, 3) && *XSTR (expand
, 3))
455 /* Output code to copy the arguments into `operands'. */
456 for (i
= 0; i
< operands
; i
++)
457 printf (" operands[%d] = operand%d;\n", i
, i
);
459 /* Output the special code to be executed before the sequence
461 printf ("%s\n", XSTR (expand
, 3));
463 /* Output code to copy the arguments back out of `operands'
464 (unless we aren't going to use them at all). */
465 if (XVEC (expand
, 1) != 0)
467 for (i
= 0; i
< operands
; i
++)
468 printf (" operand%d = operands[%d];\n", i
, i
);
469 for (; i
<= max_dup_opno
; i
++)
470 printf (" operand%d = operands[%d];\n", i
, i
);
474 /* Output code to construct the rtl for the instruction bodies.
475 Use emit_insn to add them to the sequence being accumulated.
476 But don't do this if the user's code has set `no_more' nonzero. */
478 for (i
= 0; i
< XVECLEN (expand
, 1); i
++)
480 rtx next
= XVECEXP (expand
, 1, i
);
481 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
482 || (GET_CODE (next
) == PARALLEL
483 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
484 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
485 || GET_CODE (next
) == RETURN
)
486 printf (" emit_jump_insn (");
487 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
488 || GET_CODE (next
) == CALL
489 || (GET_CODE (next
) == PARALLEL
490 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
491 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
492 || (GET_CODE (next
) == PARALLEL
493 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
494 printf (" emit_call_insn (");
495 else if (GET_CODE (next
) == CODE_LABEL
)
496 printf (" emit_label (");
497 else if (GET_CODE (next
) == MATCH_OPERAND
498 || GET_CODE (next
) == MATCH_OPERATOR
499 || GET_CODE (next
) == MATCH_PARALLEL
500 || GET_CODE (next
) == MATCH_OP_DUP
501 || GET_CODE (next
) == MATCH_DUP
502 || GET_CODE (next
) == PARALLEL
)
505 printf (" emit_insn (");
508 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
509 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
510 printf (" emit_barrier ();");
513 /* Call `gen_sequence' to make a SEQUENCE out of all the
514 insns emitted within this gen_... function. */
516 printf (" _val = gen_sequence ();\n");
517 printf (" end_sequence ();\n");
518 printf (" return _val;\n}\n\n");
521 /* Like gen_expand, but generates a SEQUENCE. */
530 if (XVEC (split
, 0) == 0)
531 fatal ("define_split (definition %d) lacks a pattern", insn_index_number
);
532 else if (XVEC (split
, 2) == 0)
533 fatal ("define_split (definition %d) lacks a replacement pattern",
536 /* Find out how many operands this function has. */
538 max_operand_vec (split
, 2);
539 operands
= MAX (max_opno
, max_dup_opno
) + 1;
541 /* Output the function name and argument declarations. */
542 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
546 /* Declare all local variables. */
547 for (i
= 0; i
< operands
; i
++)
548 printf (" rtx operand%d;\n", i
);
549 printf (" rtx _val = 0;\n");
550 printf (" start_sequence ();\n");
552 /* The fourth operand of DEFINE_SPLIT is some code to be executed
553 before the actual construction. */
556 printf ("%s\n", XSTR (split
, 3));
558 /* Output code to copy the arguments back out of `operands' */
559 for (i
= 0; i
< operands
; i
++)
560 printf (" operand%d = operands[%d];\n", i
, i
);
562 /* Output code to construct the rtl for the instruction bodies.
563 Use emit_insn to add them to the sequence being accumulated.
564 But don't do this if the user's code has set `no_more' nonzero. */
566 for (i
= 0; i
< XVECLEN (split
, 2); i
++)
568 rtx next
= XVECEXP (split
, 2, i
);
569 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
570 || (GET_CODE (next
) == PARALLEL
571 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
572 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
573 || GET_CODE (next
) == RETURN
)
574 printf (" emit_jump_insn (");
575 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
576 || GET_CODE (next
) == CALL
577 || (GET_CODE (next
) == PARALLEL
578 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
579 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
580 || (GET_CODE (next
) == PARALLEL
581 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
582 printf (" emit_call_insn (");
583 else if (GET_CODE (next
) == CODE_LABEL
)
584 printf (" emit_label (");
585 else if (GET_CODE (next
) == MATCH_OPERAND
586 || GET_CODE (next
) == MATCH_OPERATOR
587 || GET_CODE (next
) == MATCH_PARALLEL
588 || GET_CODE (next
) == MATCH_OP_DUP
589 || GET_CODE (next
) == MATCH_DUP
590 || GET_CODE (next
) == PARALLEL
)
593 printf (" emit_insn (");
596 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
597 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
598 printf (" emit_barrier ();");
601 /* Call `gen_sequence' to make a SEQUENCE out of all the
602 insns emitted within this gen_... function. */
604 printf (" _val = gen_sequence ();\n");
605 printf (" end_sequence ();\n");
606 printf (" return _val;\n}\n\n");
609 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
610 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
611 the end of the vector. */
614 output_add_clobbers ()
616 struct clobber_pat
*clobber
;
617 struct clobber_ent
*ent
;
620 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
621 printf (" rtx pattern;\n int insn_code_number;\n");
623 printf (" switch (insn_code_number)\n");
626 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
628 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
629 printf (" case %d:\n", ent
->code_number
);
631 for (i
= clobber
->first_clobber
; i
< XVECLEN (clobber
->pattern
, 1); i
++)
633 printf (" XVECEXP (pattern, 0, %d) = ", i
);
634 gen_exp (XVECEXP (clobber
->pattern
, 1, i
));
638 printf (" break;\n\n");
641 printf (" default:\n");
642 printf (" abort ();\n");
647 /* Write a function, init_mov_optab, that is called to set up entries
648 in mov_optab for EXTRA_CC_MODES. */
651 output_init_mov_optab ()
653 #ifdef EXTRA_CC_NAMES
654 static char *cc_names
[] = { EXTRA_CC_NAMES
};
658 printf ("\nvoid\ninit_mov_optab ()\n{\n");
660 for (i
= 0; i
< sizeof cc_names
/ sizeof cc_names
[0]; i
++)
662 printf ("#ifdef HAVE_mov");
663 for (p
= cc_names
[i
]; *p
; p
++)
664 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
666 printf (" if (HAVE_mov");
667 for (p
= cc_names
[i
]; *p
; p
++)
668 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
670 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
672 for (p
= cc_names
[i
]; *p
; p
++)
673 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
674 printf (";\n#endif\n");
685 register PTR val
= (PTR
) malloc (size
);
688 fatal ("virtual memory exhausted");
700 ptr
= (PTR
) realloc (old
, size
);
702 ptr
= (PTR
) malloc (size
);
704 fatal ("virtual memory exhausted");
709 fatal
VPROTO ((const char *format
, ...))
711 #ifndef ANSI_PROTOTYPES
716 VA_START (ap
, format
);
718 #ifndef ANSI_PROTOTYPES
719 format
= va_arg (ap
, const char *);
722 fprintf (stderr
, "genemit: ");
723 vfprintf (stderr
, format
, ap
);
725 fprintf (stderr
, "\n");
726 exit (FATAL_EXIT_CODE
);
729 /* More 'friendly' abort that prints the line and file.
730 config.h can #define abort fancy_abort if you like that sort of thing. */
735 fatal ("Internal gcc abort.");
747 obstack_init (rtl_obstack
);
750 fatal ("No input file name.");
752 infile
= fopen (argv
[1], "r");
756 exit (FATAL_EXIT_CODE
);
761 /* Assign sequential codes to all entries in the machine description
762 in parallel with the tables in insn-output.c. */
764 insn_code_number
= 0;
765 insn_index_number
= 0;
767 printf ("/* Generated automatically by the program `genemit'\n\
768 from the machine description file `md'. */\n\n");
770 printf ("#include \"config.h\"\n");
771 printf ("#include \"system.h\"\n");
772 printf ("#include \"rtl.h\"\n");
773 printf ("#include \"expr.h\"\n");
774 printf ("#include \"real.h\"\n");
775 printf ("#include \"flags.h\"\n");
776 printf ("#include \"output.h\"\n");
777 printf ("#include \"insn-config.h\"\n");
778 printf ("#include \"insn-flags.h\"\n");
779 printf ("#include \"insn-codes.h\"\n");
780 printf ("#include \"recog.h\"\n");
781 printf ("#include \"reload.h\"\n\n");
782 printf ("extern rtx recog_operand[];\n");
783 printf ("#define operands emit_operand\n\n");
784 printf ("#define FAIL return (end_sequence (), _val)\n");
785 printf ("#define DONE return (_val = gen_sequence (), end_sequence (), _val)\n");
787 /* Read the machine description. */
791 c
= read_skip_spaces (infile
);
796 desc
= read_rtx (infile
);
797 if (GET_CODE (desc
) == DEFINE_INSN
)
802 if (GET_CODE (desc
) == DEFINE_EXPAND
)
807 if (GET_CODE (desc
) == DEFINE_SPLIT
)
812 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
819 /* Write out the routine to add CLOBBERs to a pattern. */
820 output_add_clobbers ();
822 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
823 output_init_mov_optab ();
826 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);