1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 88, 91, 94, 95, 1997 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. */
31 static struct obstack obstack
;
32 struct obstack
*rtl_obstack
= &obstack
;
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
38 extern rtx
read_rtx ();
45 static int max_dup_opno
;
46 static int register_constraints
;
47 static int insn_code_number
;
48 static int insn_index_number
;
50 /* Data structure for recording the patterns of insns that have CLOBBERs.
51 We use this to output a function that adds these CLOBBERs to a
52 previously-allocated PARALLEL expression. */
56 struct clobber_ent
*insns
;
59 struct clobber_pat
*next
;
62 /* Records one insn that uses the clobber list. */
66 int code_number
; /* Counts only insns. */
67 struct clobber_ent
*next
;
74 register RTX_CODE code
;
84 if (code
== MATCH_OPERAND
&& XSTR (x
, 2) != 0 && *XSTR (x
, 2) != '\0')
85 register_constraints
= 1;
86 if (code
== MATCH_SCRATCH
&& XSTR (x
, 1) != 0 && *XSTR (x
, 1) != '\0')
87 register_constraints
= 1;
88 if (code
== MATCH_OPERAND
|| code
== MATCH_OPERATOR
89 || code
== MATCH_PARALLEL
)
90 max_opno
= MAX (max_opno
, XINT (x
, 0));
91 if (code
== MATCH_DUP
|| code
== MATCH_OP_DUP
|| code
== MATCH_PAR_DUP
)
92 max_dup_opno
= MAX (max_dup_opno
, XINT (x
, 0));
94 fmt
= GET_RTX_FORMAT (code
);
95 len
= GET_RTX_LENGTH (code
);
96 for (i
= 0; i
< len
; i
++)
98 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
99 max_operand_1 (XEXP (x
, i
));
100 else if (fmt
[i
] == 'E')
103 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
104 max_operand_1 (XVECEXP (x
, i
, j
));
110 max_operand_vec (insn
, arg
)
114 register int len
= XVECLEN (insn
, arg
);
120 for (i
= 0; i
< len
; i
++)
121 max_operand_1 (XVECEXP (insn
, arg
, i
));
131 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
133 if (*p1
>= 'a' && *p1
<= 'z')
134 putchar (*p1
+ 'A' - 'a');
140 /* Print a C expression to construct an RTX just like X,
141 substituting any operand references appearing within. */
147 register RTX_CODE code
;
164 printf ("operand%d", XINT (x
, 0));
168 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
169 XINT (x
, 0), XINT (x
, 0));
170 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
173 gen_exp (XVECEXP (x
, 1, i
));
179 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x
, 0));
180 printf (", %smode", GET_MODE_NAME (GET_MODE (x
)));
181 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
184 gen_exp (XVECEXP (x
, 2, i
));
191 printf ("operand%d", XINT (x
, 0));
195 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x
)));
199 fatal ("ADDRESS expression code used in named instruction pattern");
211 printf ("const0_rtx");
212 else if (INTVAL (x
) == 1)
213 printf ("const1_rtx");
214 else if (INTVAL (x
) == -1)
215 printf ("constm1_rtx");
216 else if (INTVAL (x
) == STORE_FLAG_VALUE
)
217 printf ("const_true_rtx");
220 printf ("GEN_INT (");
221 printf (HOST_WIDE_INT_PRINT_DEC
, INTVAL (x
));
227 /* These shouldn't be written in MD files. Instead, the appropriate
228 routines in varasm.c should be called. */
237 printf (" (%smode", GET_MODE_NAME (GET_MODE (x
)));
239 fmt
= GET_RTX_FORMAT (code
);
240 len
= GET_RTX_LENGTH (code
);
241 for (i
= 0; i
< len
; i
++)
246 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
247 gen_exp (XEXP (x
, i
));
248 else if (fmt
[i
] == 'i')
249 printf ("%u", XINT (x
, i
));
250 else if (fmt
[i
] == 's')
251 printf ("\"%s\"", XSTR (x
, i
));
252 else if (fmt
[i
] == 'E')
255 printf ("gen_rtvec (%d", XVECLEN (x
, i
));
256 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
259 gen_exp (XVECEXP (x
, i
, j
));
269 /* Generate the `gen_...' function for a DEFINE_INSN. */
278 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
279 registers or MATCH_SCRATCHes. If so, store away the information for
284 for (i
= XVECLEN (insn
, 1) - 1; i
> 0; i
--)
285 if (GET_CODE (XVECEXP (insn
, 1, i
)) != CLOBBER
286 || (GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != REG
287 && GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != MATCH_SCRATCH
))
290 if (i
!= XVECLEN (insn
, 1) - 1)
292 register struct clobber_pat
*p
;
293 register struct clobber_ent
*link
294 = (struct clobber_ent
*) xmalloc (sizeof (struct clobber_ent
));
297 link
->code_number
= insn_code_number
;
299 /* See if any previous CLOBBER_LIST entry is the same as this
302 for (p
= clobber_list
; p
; p
= p
->next
)
304 if (p
->first_clobber
!= i
+ 1
305 || XVECLEN (p
->pattern
, 1) != XVECLEN (insn
, 1))
308 for (j
= i
+ 1; j
< XVECLEN (insn
, 1); j
++)
310 rtx old
= XEXP (XVECEXP (p
->pattern
, 1, j
), 0);
311 rtx
new = XEXP (XVECEXP (insn
, 1, j
), 0);
313 /* OLD and NEW are the same if both are to be a SCRATCH
315 or if both are registers of the same mode and number. */
316 if (! (GET_MODE (old
) == GET_MODE (new)
317 && ((GET_CODE (old
) == MATCH_SCRATCH
318 && GET_CODE (new) == MATCH_SCRATCH
)
319 || (GET_CODE (old
) == REG
&& GET_CODE (new) == REG
320 && REGNO (old
) == REGNO (new)))))
324 if (j
== XVECLEN (insn
, 1))
330 p
= (struct clobber_pat
*) xmalloc (sizeof (struct clobber_pat
));
334 p
->first_clobber
= i
+ 1;
335 p
->next
= clobber_list
;
339 link
->next
= p
->insns
;
344 /* Don't mention instructions whose names are the null string
345 or begin with '*'. They are in the machine description just
347 if (XSTR (insn
, 0)[0] == 0 || XSTR (insn
, 0)[0] == '*')
350 /* Find out how many operands this function has,
351 and also whether any of them have register constraints. */
352 register_constraints
= 0;
353 operands
= max_operand_vec (insn
, 1);
354 if (max_dup_opno
>= operands
)
355 fatal ("match_dup operand number has no match_operand");
357 /* Output the function name and argument declarations. */
358 printf ("rtx\ngen_%s (", XSTR (insn
, 0));
359 for (i
= 0; i
< operands
; i
++)
360 printf (i
? ", operand%d" : "operand%d", i
);
362 for (i
= 0; i
< operands
; i
++)
363 printf (" rtx operand%d;\n", i
);
366 /* Output code to construct and return the rtl for the instruction body */
368 if (XVECLEN (insn
, 1) == 1)
371 gen_exp (XVECEXP (insn
, 1, 0));
376 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn
, 1));
377 for (i
= 0; i
< XVECLEN (insn
, 1); i
++)
380 gen_exp (XVECEXP (insn
, 1, i
));
382 printf ("));\n}\n\n");
386 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
395 if (strlen (XSTR (expand
, 0)) == 0)
396 fatal ("define_expand lacks a name");
397 if (XVEC (expand
, 1) == 0)
398 fatal ("define_expand for %s lacks a pattern", XSTR (expand
, 0));
400 /* Find out how many operands this function has,
401 and also whether any of them have register constraints. */
402 register_constraints
= 0;
404 operands
= max_operand_vec (expand
, 1);
406 /* Output the function name and argument declarations. */
407 printf ("rtx\ngen_%s (", XSTR (expand
, 0));
408 for (i
= 0; i
< operands
; i
++)
409 printf (i
? ", operand%d" : "operand%d", i
);
411 for (i
= 0; i
< operands
; i
++)
412 printf (" rtx operand%d;\n", i
);
415 /* If we don't have any C code to write, only one insn is being written,
416 and no MATCH_DUPs are present, we can just return the desired insn
417 like we do for a DEFINE_INSN. This saves memory. */
418 if ((XSTR (expand
, 3) == 0 || *XSTR (expand
, 3) == '\0')
419 && operands
> max_dup_opno
420 && XVECLEN (expand
, 1) == 1)
423 gen_exp (XVECEXP (expand
, 1, 0));
428 /* For each operand referred to only with MATCH_DUPs,
429 make a local variable. */
430 for (i
= operands
; i
<= max_dup_opno
; i
++)
431 printf (" rtx operand%d;\n", i
);
432 if (operands
> 0 || max_dup_opno
>= 0)
433 printf (" rtx operands[%d];\n", MAX (operands
, max_dup_opno
+ 1));
434 printf (" rtx _val = 0;\n");
435 printf (" start_sequence ();\n");
437 /* The fourth operand of DEFINE_EXPAND is some code to be executed
438 before the actual construction.
439 This code expects to refer to `operands'
440 just as the output-code in a DEFINE_INSN does,
441 but here `operands' is an automatic array.
442 So copy the operand values there before executing it. */
443 if (XSTR (expand
, 3) && *XSTR (expand
, 3))
445 /* Output code to copy the arguments into `operands'. */
446 for (i
= 0; i
< operands
; i
++)
447 printf (" operands[%d] = operand%d;\n", i
, i
);
449 /* Output the special code to be executed before the sequence
451 printf ("%s\n", XSTR (expand
, 3));
453 /* Output code to copy the arguments back out of `operands'
454 (unless we aren't going to use them at all). */
455 if (XVEC (expand
, 1) != 0)
457 for (i
= 0; i
< operands
; i
++)
458 printf (" operand%d = operands[%d];\n", i
, i
);
459 for (; i
<= max_dup_opno
; i
++)
460 printf (" operand%d = operands[%d];\n", i
, i
);
464 /* Output code to construct the rtl for the instruction bodies.
465 Use emit_insn to add them to the sequence being accumulated.
466 But don't do this if the user's code has set `no_more' nonzero. */
468 for (i
= 0; i
< XVECLEN (expand
, 1); i
++)
470 rtx next
= XVECEXP (expand
, 1, i
);
471 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
472 || (GET_CODE (next
) == PARALLEL
473 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
474 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
475 || GET_CODE (next
) == RETURN
)
476 printf (" emit_jump_insn (");
477 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
478 || GET_CODE (next
) == CALL
479 || (GET_CODE (next
) == PARALLEL
480 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
481 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
482 || (GET_CODE (next
) == PARALLEL
483 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
484 printf (" emit_call_insn (");
485 else if (GET_CODE (next
) == CODE_LABEL
)
486 printf (" emit_label (");
487 else if (GET_CODE (next
) == MATCH_OPERAND
488 || GET_CODE (next
) == MATCH_OPERATOR
489 || GET_CODE (next
) == MATCH_PARALLEL
490 || GET_CODE (next
) == MATCH_OP_DUP
491 || GET_CODE (next
) == MATCH_DUP
492 || GET_CODE (next
) == PARALLEL
)
495 printf (" emit_insn (");
498 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
499 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
500 printf (" emit_barrier ();");
503 /* Call `gen_sequence' to make a SEQUENCE out of all the
504 insns emitted within this gen_... function. */
506 printf (" _done:\n");
507 printf (" _val = gen_sequence ();\n");
508 printf (" _fail:\n");
509 printf (" end_sequence ();\n");
510 printf (" return _val;\n}\n\n");
513 /* Like gen_expand, but generates a SEQUENCE. */
522 if (XVEC (split
, 0) == 0)
523 fatal ("define_split (definition %d) lacks a pattern", insn_index_number
);
524 else if (XVEC (split
, 2) == 0)
525 fatal ("define_split (definition %d) lacks a replacement pattern",
528 /* Find out how many operands this function has. */
530 max_operand_vec (split
, 2);
531 operands
= MAX (max_opno
, max_dup_opno
) + 1;
533 /* Output the function name and argument declarations. */
534 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
538 /* Declare all local variables. */
539 for (i
= 0; i
< operands
; i
++)
540 printf (" rtx operand%d;\n", i
);
541 printf (" rtx _val = 0;\n");
542 printf (" start_sequence ();\n");
544 /* The fourth operand of DEFINE_SPLIT is some code to be executed
545 before the actual construction. */
548 printf ("%s\n", XSTR (split
, 3));
550 /* Output code to copy the arguments back out of `operands' */
551 for (i
= 0; i
< operands
; i
++)
552 printf (" operand%d = operands[%d];\n", i
, i
);
554 /* Output code to construct the rtl for the instruction bodies.
555 Use emit_insn to add them to the sequence being accumulated.
556 But don't do this if the user's code has set `no_more' nonzero. */
558 for (i
= 0; i
< XVECLEN (split
, 2); i
++)
560 rtx next
= XVECEXP (split
, 2, i
);
561 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
562 || (GET_CODE (next
) == PARALLEL
563 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
564 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
565 || GET_CODE (next
) == RETURN
)
566 printf (" emit_jump_insn (");
567 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
568 || GET_CODE (next
) == CALL
569 || (GET_CODE (next
) == PARALLEL
570 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
571 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
572 || (GET_CODE (next
) == PARALLEL
573 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
574 printf (" emit_call_insn (");
575 else if (GET_CODE (next
) == CODE_LABEL
)
576 printf (" emit_label (");
577 else if (GET_CODE (next
) == MATCH_OPERAND
578 || GET_CODE (next
) == MATCH_OPERATOR
579 || GET_CODE (next
) == MATCH_PARALLEL
580 || GET_CODE (next
) == MATCH_OP_DUP
581 || GET_CODE (next
) == MATCH_DUP
582 || GET_CODE (next
) == PARALLEL
)
585 printf (" emit_insn (");
588 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
589 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
590 printf (" emit_barrier ();");
593 /* Call `gen_sequence' to make a SEQUENCE out of all the
594 insns emitted within this gen_... function. */
596 printf (" _done:\n");
597 printf (" _val = gen_sequence ();\n");
598 printf (" _fail:\n");
599 printf (" end_sequence ();\n");
600 printf (" return _val;\n}\n\n");
603 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
604 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
605 the end of the vector. */
608 output_add_clobbers ()
610 struct clobber_pat
*clobber
;
611 struct clobber_ent
*ent
;
614 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
615 printf (" rtx pattern;\n int insn_code_number;\n");
617 printf (" int i;\n\n");
618 printf (" switch (insn_code_number)\n");
621 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
623 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
624 printf (" case %d:\n", ent
->code_number
);
626 for (i
= clobber
->first_clobber
; i
< XVECLEN (clobber
->pattern
, 1); i
++)
628 printf (" XVECEXP (pattern, 0, %d) = ", i
);
629 gen_exp (XVECEXP (clobber
->pattern
, 1, i
));
633 printf (" break;\n\n");
636 printf (" default:\n");
637 printf (" abort ();\n");
642 /* Write a function, init_mov_optab, that is called to set up entries
643 in mov_optab for EXTRA_CC_MODES. */
646 output_init_mov_optab ()
648 #ifdef EXTRA_CC_NAMES
649 static char *cc_names
[] = { EXTRA_CC_NAMES
};
653 printf ("\nvoid\ninit_mov_optab ()\n{\n");
655 for (i
= 0; i
< sizeof cc_names
/ sizeof cc_names
[0]; i
++)
657 printf ("#ifdef HAVE_mov");
658 for (p
= cc_names
[i
]; *p
; p
++)
659 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
661 printf (" if (HAVE_mov");
662 for (p
= cc_names
[i
]; *p
; p
++)
663 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
665 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
667 for (p
= cc_names
[i
]; *p
; p
++)
668 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
669 printf (";\n#endif\n");
680 register char *val
= (char *) malloc (size
);
683 fatal ("virtual memory exhausted");
693 char *result
= (char *) realloc (ptr
, size
);
695 fatal ("virtual memory exhausted");
703 fprintf (stderr
, "genemit: ");
704 fprintf (stderr
, s
, a1
, a2
);
705 fprintf (stderr
, "\n");
706 exit (FATAL_EXIT_CODE
);
709 /* More 'friendly' abort that prints the line and file.
710 config.h can #define abort fancy_abort if you like that sort of thing. */
715 fatal ("Internal gcc abort.");
727 obstack_init (rtl_obstack
);
730 fatal ("No input file name.");
732 infile
= fopen (argv
[1], "r");
736 exit (FATAL_EXIT_CODE
);
741 /* Assign sequential codes to all entries in the machine description
742 in parallel with the tables in insn-output.c. */
744 insn_code_number
= 0;
745 insn_index_number
= 0;
747 printf ("/* Generated automatically by the program `genemit'\n\
748 from the machine description file `md'. */\n\n");
750 printf ("#include \"config.h\"\n");
751 printf ("#include <stdio.h>\n");
752 printf ("#include \"rtl.h\"\n");
753 printf ("#include \"expr.h\"\n");
754 printf ("#include \"real.h\"\n");
755 printf ("#include \"flags.h\"\n");
756 printf ("#include \"output.h\"\n");
757 printf ("#include \"insn-config.h\"\n\n");
758 printf ("#include \"insn-flags.h\"\n\n");
759 printf ("#include \"insn-codes.h\"\n\n");
760 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
761 printf ("extern rtx recog_operand[];\n");
762 printf ("#define operands emit_operand\n\n");
763 printf ("#define FAIL goto _fail\n\n");
764 printf ("#define DONE goto _done\n\n");
766 /* Read the machine description. */
770 c
= read_skip_spaces (infile
);
775 desc
= read_rtx (infile
);
776 if (GET_CODE (desc
) == DEFINE_INSN
)
781 if (GET_CODE (desc
) == DEFINE_EXPAND
)
786 if (GET_CODE (desc
) == DEFINE_SPLIT
)
791 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
798 /* Write out the routine to add CLOBBERs to a pattern. */
799 output_add_clobbers ();
801 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
802 output_init_mov_optab ();
805 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);