1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 88, 91, 94, 95, 97, 1998 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 char *xmalloc PROTO((unsigned));
35 void fancy_abort PROTO((void));
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), GET_MODE (operand%d)",
176 XINT (x, 0), XINT (x, 0));
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_OPERATOR
496 || GET_CODE (next) == MATCH_PARALLEL
497 || GET_CODE (next) == MATCH_OP_DUP
498 || GET_CODE (next) == MATCH_DUP
499 || GET_CODE (next) == PARALLEL)
502 printf (" emit_insn (");
505 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
506 && GET_CODE (SET_SRC (next)) == LABEL_REF)
507 printf (" emit_barrier ();");
510 /* Call `gen_sequence' to make a SEQUENCE out of all the
511 insns emitted within this gen_... function. */
513 printf (" _val = gen_sequence ();\n");
514 printf (" end_sequence ();\n");
515 printf (" return _val;\n}\n\n");
518 /* Like gen_expand, but generates a SEQUENCE. */
527 if (XVEC (split, 0) == 0)
528 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
529 else if (XVEC (split, 2) == 0)
530 fatal ("define_split (definition %d) lacks a replacement pattern",
533 /* Find out how many operands this function has. */
535 max_operand_vec (split, 2);
536 operands = MAX (max_opno, max_dup_opno) + 1;
538 /* Output the function name and argument declarations. */
539 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
543 /* Declare all local variables. */
544 for (i = 0; i < operands; i++)
545 printf (" rtx operand%d;\n", i);
546 printf (" rtx _val = 0;\n");
547 printf (" start_sequence ();\n");
549 /* The fourth operand of DEFINE_SPLIT is some code to be executed
550 before the actual construction. */
553 printf ("%s\n", XSTR (split, 3));
555 /* Output code to copy the arguments back out of `operands' */
556 for (i = 0; i < operands; i++)
557 printf (" operand%d = operands[%d];\n", i, i);
559 /* Output code to construct the rtl for the instruction bodies.
560 Use emit_insn to add them to the sequence being accumulated.
561 But don't do this if the user's code has set `no_more' nonzero. */
563 for (i = 0; i < XVECLEN (split, 2); i++)
565 rtx next = XVECEXP (split, 2, i);
566 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
567 || (GET_CODE (next) == PARALLEL
568 && GET_CODE (XVECEXP (next, 0, 0)) == SET
569 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
570 || GET_CODE (next) == RETURN)
571 printf (" emit_jump_insn (");
572 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
573 || GET_CODE (next) == CALL
574 || (GET_CODE (next) == PARALLEL
575 && GET_CODE (XVECEXP (next, 0, 0)) == SET
576 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
577 || (GET_CODE (next) == PARALLEL
578 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
579 printf (" emit_call_insn (");
580 else if (GET_CODE (next) == CODE_LABEL)
581 printf (" emit_label (");
582 else if (GET_CODE (next) == MATCH_OPERAND
583 || GET_CODE (next) == MATCH_OPERATOR
584 || GET_CODE (next) == MATCH_PARALLEL
585 || GET_CODE (next) == MATCH_OP_DUP
586 || GET_CODE (next) == MATCH_DUP
587 || GET_CODE (next) == PARALLEL)
590 printf (" emit_insn (");
593 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
594 && GET_CODE (SET_SRC (next)) == LABEL_REF)
595 printf (" emit_barrier ();");
598 /* Call `gen_sequence' to make a SEQUENCE out of all the
599 insns emitted within this gen_... function. */
601 printf (" _val = gen_sequence ();\n");
602 printf (" end_sequence ();\n");
603 printf (" return _val;\n}\n\n");
606 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
607 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
608 the end of the vector. */
611 output_add_clobbers ()
613 struct clobber_pat *clobber;
614 struct clobber_ent *ent;
617 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
618 printf (" rtx pattern;\n int insn_code_number;\n");
620 printf (" int i;\n\n");
621 printf (" switch (insn_code_number)\n");
624 for (clobber = clobber_list; clobber; clobber = clobber->next)
626 for (ent = clobber->insns; ent; ent = ent->next)
627 printf (" case %d:\n", ent->code_number);
629 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
631 printf (" XVECEXP (pattern, 0, %d) = ", i);
632 gen_exp (XVECEXP (clobber->pattern, 1, i));
636 printf (" break;\n\n");
639 printf (" default:\n");
640 printf (" abort ();\n");
645 /* Write a function, init_mov_optab, that is called to set up entries
646 in mov_optab for EXTRA_CC_MODES. */
649 output_init_mov_optab ()
651 #ifdef EXTRA_CC_NAMES
652 static char *cc_names[] = { EXTRA_CC_NAMES };
656 printf ("\nvoid\ninit_mov_optab ()\n{\n");
658 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
660 printf ("#ifdef HAVE_mov");
661 for (p = cc_names[i]; *p; p++)
662 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
664 printf (" if (HAVE_mov");
665 for (p = cc_names[i]; *p; p++)
666 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
668 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
670 for (p = cc_names[i]; *p; p++)
671 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
672 printf (";\n#endif\n");
683 register char *val = (char *) malloc (size);
686 fatal ("virtual memory exhausted");
696 char *result = (char *) realloc (ptr, size);
698 fatal ("virtual memory exhausted");
706 fprintf (stderr, "genemit: ");
707 fprintf (stderr, s, a1, a2);
708 fprintf (stderr, "\n");
709 exit (FATAL_EXIT_CODE);
712 /* More 'friendly' abort that prints the line and file.
713 config.h can #define abort fancy_abort if you like that sort of thing. */
718 fatal ("Internal gcc abort.");
730 obstack_init (rtl_obstack);
733 fatal ("No input file name.");
735 infile = fopen (argv[1], "r");
739 exit (FATAL_EXIT_CODE);
744 /* Assign sequential codes to all entries in the machine description
745 in parallel with the tables in insn-output.c. */
747 insn_code_number = 0;
748 insn_index_number = 0;
750 printf ("/* Generated automatically by the program `genemit'\n\
751 from the machine description file `md'. */\n\n");
753 printf ("#include \"config.h\"\n");
754 printf ("#include \"system.h\"\n");
755 printf ("#include \"rtl.h\"\n");
756 printf ("#include \"expr.h\"\n");
757 printf ("#include \"real.h\"\n");
758 printf ("#include \"flags.h\"\n");
759 printf ("#include \"output.h\"\n");
760 printf ("#include \"insn-config.h\"\n\n");
761 printf ("#include \"insn-flags.h\"\n\n");
762 printf ("#include \"insn-codes.h\"\n\n");
763 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
764 printf ("extern rtx recog_operand[];\n");
765 printf ("#define operands emit_operand\n\n");
766 printf ("#define FAIL do {end_sequence (); return _val;} while (0)\n");
767 printf ("#define DONE do {_val = gen_sequence (); end_sequence (); return _val;} while (0)\n");
769 /* Read the machine description. */
773 c = read_skip_spaces (infile);
778 desc = read_rtx (infile);
779 if (GET_CODE (desc) == DEFINE_INSN)
784 if (GET_CODE (desc) == DEFINE_EXPAND)
789 if (GET_CODE (desc) == DEFINE_SPLIT)
794 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
801 /* Write out the routine to add CLOBBERs to a pattern. */
802 output_add_clobbers ();
804 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
805 output_init_mov_optab ();
808 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);