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. */
28 static struct obstack obstack;
29 struct obstack *rtl_obstack = &obstack;
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free free
34 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
35 char **insn_name_ptr = 0;
38 static int max_dup_opno;
39 static int register_constraints;
40 static int insn_code_number;
41 static int insn_index_number;
43 /* Data structure for recording the patterns of insns that have CLOBBERs.
44 We use this to output a function that adds these CLOBBERs to a
45 previously-allocated PARALLEL expression. */
49 struct clobber_ent *insns;
52 struct clobber_pat *next;
55 /* Records one insn that uses the clobber list. */
59 int code_number; /* Counts only insns. */
60 struct clobber_ent *next;
63 static void max_operand_1 PROTO((rtx));
64 static int max_operand_vec PROTO((rtx, int));
65 static void print_code PROTO((RTX_CODE));
66 static void gen_exp PROTO((rtx));
67 static void gen_insn PROTO((rtx));
68 static void gen_expand PROTO((rtx));
69 static void gen_split PROTO((rtx));
70 static void output_add_clobbers PROTO((void));
71 static void output_init_mov_optab PROTO((void));
78 register RTX_CODE code;
81 register const char *fmt;
88 if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
89 register_constraints = 1;
90 if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
91 register_constraints = 1;
92 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
93 || code == MATCH_PARALLEL)
94 max_opno = MAX (max_opno, XINT (x, 0));
95 if (code == MATCH_DUP || code == MATCH_OP_DUP || code == MATCH_PAR_DUP)
96 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
98 fmt = GET_RTX_FORMAT (code);
99 len = GET_RTX_LENGTH (code);
100 for (i = 0; i < len; i++)
102 if (fmt[i] == 'e' || fmt[i] == 'u')
103 max_operand_1 (XEXP (x, i));
104 else if (fmt[i] == 'E')
107 for (j = 0; j < XVECLEN (x, i); j++)
108 max_operand_1 (XVECEXP (x, i, j));
114 max_operand_vec (insn, arg)
118 register int len = XVECLEN (insn, arg);
124 for (i = 0; i < len; i++)
125 max_operand_1 (XVECEXP (insn, arg, i));
134 register const char *p1;
135 for (p1 = GET_RTX_NAME (code); *p1; p1++)
137 if (*p1 >= 'a' && *p1 <= 'z')
138 putchar (*p1 + 'A' - 'a');
144 /* Print a C expression to construct an RTX just like X,
145 substituting any operand references appearing within. */
151 register RTX_CODE code;
154 register const char *fmt;
168 printf ("operand%d", XINT (x, 0));
172 printf ("gen_rtx (GET_CODE (operand%d), ", XINT (x, 0));
173 if (GET_MODE (x) == VOIDmode)
174 printf ("GET_MODE (operand%d)", XINT (x, 0));
176 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
177 for (i = 0; i < XVECLEN (x, 1); i++)
180 gen_exp (XVECEXP (x, 1, i));
186 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
187 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
188 for (i = 0; i < XVECLEN (x, 2); i++)
191 gen_exp (XVECEXP (x, 2, i));
198 printf ("operand%d", XINT (x, 0));
202 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
206 fatal ("ADDRESS expression code used in named instruction pattern");
218 printf ("const0_rtx");
219 else if (INTVAL (x) == 1)
220 printf ("const1_rtx");
221 else if (INTVAL (x) == -1)
222 printf ("constm1_rtx");
223 else if (INTVAL (x) == STORE_FLAG_VALUE)
224 printf ("const_true_rtx");
227 printf ("GEN_INT (");
228 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
234 /* These shouldn't be written in MD files. Instead, the appropriate
235 routines in varasm.c should be called. */
244 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
246 fmt = GET_RTX_FORMAT (code);
247 len = GET_RTX_LENGTH (code);
248 for (i = 0; i < len; i++)
253 if (fmt[i] == 'e' || fmt[i] == 'u')
254 gen_exp (XEXP (x, i));
255 else if (fmt[i] == 'i')
256 printf ("%u", XINT (x, i));
257 else if (fmt[i] == 's')
258 printf ("\"%s\"", XSTR (x, i));
259 else if (fmt[i] == 'E')
262 printf ("gen_rtvec (%d", XVECLEN (x, i));
263 for (j = 0; j < XVECLEN (x, i); j++)
266 gen_exp (XVECEXP (x, i, j));
276 /* Generate the `gen_...' function for a DEFINE_INSN. */
285 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
286 registers or MATCH_SCRATCHes. If so, store away the information for
291 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
292 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
293 || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
294 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
297 if (i != XVECLEN (insn, 1) - 1)
299 register struct clobber_pat *p;
300 register struct clobber_ent *link
301 = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
304 link->code_number = insn_code_number;
306 /* See if any previous CLOBBER_LIST entry is the same as this
309 for (p = clobber_list; p; p = p->next)
311 if (p->first_clobber != i + 1
312 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
315 for (j = i + 1; j < XVECLEN (insn, 1); j++)
317 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
318 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
320 /* OLD and NEW are the same if both are to be a SCRATCH
322 or if both are registers of the same mode and number. */
323 if (! (GET_MODE (old) == GET_MODE (new)
324 && ((GET_CODE (old) == MATCH_SCRATCH
325 && GET_CODE (new) == MATCH_SCRATCH)
326 || (GET_CODE (old) == REG && GET_CODE (new) == REG
327 && REGNO (old) == REGNO (new)))))
331 if (j == XVECLEN (insn, 1))
337 p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
341 p->first_clobber = i + 1;
342 p->next = clobber_list;
346 link->next = p->insns;
351 /* Don't mention instructions whose names are the null string
352 or begin with '*'. They are in the machine description just
354 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
357 /* Find out how many operands this function has,
358 and also whether any of them have register constraints. */
359 register_constraints = 0;
360 operands = max_operand_vec (insn, 1);
361 if (max_dup_opno >= operands)
362 fatal ("match_dup operand number has no match_operand");
364 /* Output the function name and argument declarations. */
365 printf ("rtx\ngen_%s (", XSTR (insn, 0));
366 for (i = 0; i < operands; i++)
367 printf (i ? ", operand%d" : "operand%d", i);
369 for (i = 0; i < operands; i++)
370 printf (" rtx operand%d;\n", i);
373 /* Output code to construct and return the rtl for the instruction body */
375 if (XVECLEN (insn, 1) == 1)
378 gen_exp (XVECEXP (insn, 1, 0));
383 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
384 for (i = 0; i < XVECLEN (insn, 1); i++)
387 gen_exp (XVECEXP (insn, 1, i));
389 printf ("));\n}\n\n");
393 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
402 if (strlen (XSTR (expand, 0)) == 0)
403 fatal ("define_expand lacks a name");
404 if (XVEC (expand, 1) == 0)
405 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
407 /* Find out how many operands this function has,
408 and also whether any of them have register constraints. */
409 register_constraints = 0;
411 operands = max_operand_vec (expand, 1);
413 /* Output the function name and argument declarations. */
414 printf ("rtx\ngen_%s (", XSTR (expand, 0));
415 for (i = 0; i < operands; i++)
416 printf (i ? ", operand%d" : "operand%d", i);
418 for (i = 0; i < operands; i++)
419 printf (" rtx operand%d;\n", i);
422 /* If we don't have any C code to write, only one insn is being written,
423 and no MATCH_DUPs are present, we can just return the desired insn
424 like we do for a DEFINE_INSN. This saves memory. */
425 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
426 && operands > max_dup_opno
427 && XVECLEN (expand, 1) == 1)
430 gen_exp (XVECEXP (expand, 1, 0));
435 /* For each operand referred to only with MATCH_DUPs,
436 make a local variable. */
437 for (i = operands; i <= max_dup_opno; i++)
438 printf (" rtx operand%d;\n", i);
439 if (operands > 0 || max_dup_opno >= 0)
440 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
441 printf (" rtx _val = 0;\n");
442 printf (" start_sequence ();\n");
444 /* The fourth operand of DEFINE_EXPAND is some code to be executed
445 before the actual construction.
446 This code expects to refer to `operands'
447 just as the output-code in a DEFINE_INSN does,
448 but here `operands' is an automatic array.
449 So copy the operand values there before executing it. */
450 if (XSTR (expand, 3) && *XSTR (expand, 3))
452 /* Output code to copy the arguments into `operands'. */
453 for (i = 0; i < operands; i++)
454 printf (" operands[%d] = operand%d;\n", i, i);
456 /* Output the special code to be executed before the sequence
458 printf ("%s\n", XSTR (expand, 3));
460 /* Output code to copy the arguments back out of `operands'
461 (unless we aren't going to use them at all). */
462 if (XVEC (expand, 1) != 0)
464 for (i = 0; i < operands; i++)
465 printf (" operand%d = operands[%d];\n", i, i);
466 for (; i <= max_dup_opno; i++)
467 printf (" operand%d = operands[%d];\n", i, i);
471 /* Output code to construct the rtl for the instruction bodies.
472 Use emit_insn to add them to the sequence being accumulated.
473 But don't do this if the user's code has set `no_more' nonzero. */
475 for (i = 0; i < XVECLEN (expand, 1); i++)
477 rtx next = XVECEXP (expand, 1, i);
478 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
479 || (GET_CODE (next) == PARALLEL
480 && GET_CODE (XVECEXP (next, 0, 0)) == SET
481 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
482 || GET_CODE (next) == RETURN)
483 printf (" emit_jump_insn (");
484 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
485 || GET_CODE (next) == CALL
486 || (GET_CODE (next) == PARALLEL
487 && GET_CODE (XVECEXP (next, 0, 0)) == SET
488 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
489 || (GET_CODE (next) == PARALLEL
490 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
491 printf (" emit_call_insn (");
492 else if (GET_CODE (next) == CODE_LABEL)
493 printf (" emit_label (");
494 else if (GET_CODE (next) == MATCH_OPERAND
495 || GET_CODE (next) == MATCH_DUP
496 || GET_CODE (next) == MATCH_OPERATOR
497 || GET_CODE (next) == MATCH_OP_DUP
498 || GET_CODE (next) == MATCH_PARALLEL
499 || GET_CODE (next) == MATCH_PAR_DUP
500 || GET_CODE (next) == PARALLEL)
503 printf (" emit_insn (");
506 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
507 && GET_CODE (SET_SRC (next)) == LABEL_REF)
508 printf (" emit_barrier ();");
511 /* Call `gen_sequence' to make a SEQUENCE out of all the
512 insns emitted within this gen_... function. */
514 printf (" _val = gen_sequence ();\n");
515 printf (" end_sequence ();\n");
516 printf (" return _val;\n}\n\n");
519 /* Like gen_expand, but generates a SEQUENCE. */
528 if (XVEC (split, 0) == 0)
529 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
530 else if (XVEC (split, 2) == 0)
531 fatal ("define_split (definition %d) lacks a replacement pattern",
534 /* Find out how many operands this function has. */
536 max_operand_vec (split, 2);
537 operands = MAX (max_opno, max_dup_opno) + 1;
539 /* Output the prototype, the function name and argument declarations. */
540 printf ("extern rtx gen_split_%d PROTO ((rtx *));\n", insn_code_number);
541 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
545 /* Declare all local variables. */
546 for (i = 0; i < operands; i++)
547 printf (" rtx operand%d;\n", i);
548 printf (" rtx _val = 0;\n");
549 printf (" start_sequence ();\n");
551 /* The fourth operand of DEFINE_SPLIT is some code to be executed
552 before the actual construction. */
555 printf ("%s\n", XSTR (split, 3));
557 /* Output code to copy the arguments back out of `operands' */
558 for (i = 0; i < operands; i++)
559 printf (" operand%d = operands[%d];\n", i, i);
561 /* Output code to construct the rtl for the instruction bodies.
562 Use emit_insn to add them to the sequence being accumulated.
563 But don't do this if the user's code has set `no_more' nonzero. */
565 for (i = 0; i < XVECLEN (split, 2); i++)
567 rtx next = XVECEXP (split, 2, i);
568 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
569 || (GET_CODE (next) == PARALLEL
570 && GET_CODE (XVECEXP (next, 0, 0)) == SET
571 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
572 || GET_CODE (next) == RETURN)
573 printf (" emit_jump_insn (");
574 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
575 || GET_CODE (next) == CALL
576 || (GET_CODE (next) == PARALLEL
577 && GET_CODE (XVECEXP (next, 0, 0)) == SET
578 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
579 || (GET_CODE (next) == PARALLEL
580 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
581 printf (" emit_call_insn (");
582 else if (GET_CODE (next) == CODE_LABEL)
583 printf (" emit_label (");
584 else if (GET_CODE (next) == MATCH_OPERAND
585 || GET_CODE (next) == MATCH_OPERATOR
586 || GET_CODE (next) == MATCH_PARALLEL
587 || GET_CODE (next) == MATCH_OP_DUP
588 || GET_CODE (next) == MATCH_DUP
589 || GET_CODE (next) == PARALLEL)
592 printf (" emit_insn (");
595 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
596 && GET_CODE (SET_SRC (next)) == LABEL_REF)
597 printf (" emit_barrier ();");
600 /* Call `gen_sequence' to make a SEQUENCE out of all the
601 insns emitted within this gen_... function. */
603 printf (" _val = gen_sequence ();\n");
604 printf (" end_sequence ();\n");
605 printf (" return _val;\n}\n\n");
608 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
609 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
610 the end of the vector. */
613 output_add_clobbers ()
615 struct clobber_pat *clobber;
616 struct clobber_ent *ent;
619 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
620 printf (" rtx pattern;\n int insn_code_number;\n");
622 printf (" switch (insn_code_number)\n");
625 for (clobber = clobber_list; clobber; clobber = clobber->next)
627 for (ent = clobber->insns; ent; ent = ent->next)
628 printf (" case %d:\n", ent->code_number);
630 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
632 printf (" XVECEXP (pattern, 0, %d) = ", i);
633 gen_exp (XVECEXP (clobber->pattern, 1, i));
637 printf (" break;\n\n");
640 printf (" default:\n");
641 printf (" abort ();\n");
646 /* Write a function, init_mov_optab, that is called to set up entries
647 in mov_optab for EXTRA_CC_MODES. */
650 output_init_mov_optab ()
652 #ifdef EXTRA_CC_NAMES
653 static char *cc_names[] = { EXTRA_CC_NAMES };
657 printf ("\nvoid\ninit_mov_optab ()\n{\n");
659 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
661 printf ("#ifdef HAVE_mov");
662 for (p = cc_names[i]; *p; p++)
663 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
665 printf (" if (HAVE_mov");
666 for (p = cc_names[i]; *p; p++)
667 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
669 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
671 for (p = cc_names[i]; *p; p++)
672 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
673 printf (";\n#endif\n");
684 register PTR val = (PTR) malloc (size);
687 fatal ("virtual memory exhausted");
699 ptr = (PTR) realloc (old, size);
701 ptr = (PTR) malloc (size);
703 fatal ("virtual memory exhausted");
716 progname = "genemit";
717 obstack_init (rtl_obstack);
720 fatal ("No input file name.");
722 infile = fopen (argv[1], "r");
726 exit (FATAL_EXIT_CODE);
731 /* Assign sequential codes to all entries in the machine description
732 in parallel with the tables in insn-output.c. */
734 insn_code_number = 0;
735 insn_index_number = 0;
737 printf ("/* Generated automatically by the program `genemit'\n\
738 from the machine description file `md'. */\n\n");
740 printf ("#include \"config.h\"\n");
741 printf ("#include \"system.h\"\n");
742 printf ("#include \"rtl.h\"\n");
743 printf ("#include \"function.h\"\n");
744 printf ("#include \"expr.h\"\n");
745 printf ("#include \"real.h\"\n");
746 printf ("#include \"flags.h\"\n");
747 printf ("#include \"output.h\"\n");
748 printf ("#include \"insn-config.h\"\n");
749 printf ("#include \"insn-flags.h\"\n");
750 printf ("#include \"insn-codes.h\"\n");
751 printf ("#include \"recog.h\"\n");
752 printf ("#include \"reload.h\"\n\n");
753 printf ("extern rtx recog_operand[];\n");
754 printf ("#define operands emit_operand\n\n");
755 printf ("#define FAIL return (end_sequence (), _val)\n");
756 printf ("#define DONE return (_val = gen_sequence (), end_sequence (), _val)\n");
758 /* Read the machine description. */
762 c = read_skip_spaces (infile);
767 desc = read_rtx (infile);
768 if (GET_CODE (desc) == DEFINE_INSN)
773 if (GET_CODE (desc) == DEFINE_EXPAND)
778 if (GET_CODE (desc) == DEFINE_SPLIT)
783 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
790 /* Write out the routine to add CLOBBERs to a pattern. */
791 output_add_clobbers ();
793 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
794 output_init_mov_optab ();
797 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);