1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 1988, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 static struct obstack obstack;
27 struct obstack *rtl_obstack = &obstack;
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
39 static int max_dup_opno;
40 static int register_constraints;
41 static int insn_code_number;
42 static int insn_index_number;
44 /* Data structure for recording the patterns of insns that have CLOBBERs.
45 We use this to output a function that adds these CLOBBERs to a
46 previously-allocated PARALLEL expression. */
50 int code_number; /* Counts only insns. */
53 struct clobber_pat *next;
60 register RTX_CODE code;
70 if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
71 register_constraints = 1;
72 if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
73 register_constraints = 1;
74 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
75 || code == MATCH_PARALLEL)
76 max_opno = MAX (max_opno, XINT (x, 0));
77 if (code == MATCH_DUP || code == MATCH_OP_DUP)
78 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
80 fmt = GET_RTX_FORMAT (code);
81 len = GET_RTX_LENGTH (code);
82 for (i = 0; i < len; i++)
84 if (fmt[i] == 'e' || fmt[i] == 'u')
85 max_operand_1 (XEXP (x, i));
86 else if (fmt[i] == 'E')
89 for (j = 0; j < XVECLEN (x, i); j++)
90 max_operand_1 (XVECEXP (x, i, j));
96 max_operand_vec (insn, arg)
100 register int len = XVECLEN (insn, arg);
106 for (i = 0; i < len; i++)
107 max_operand_1 (XVECEXP (insn, arg, i));
117 for (p1 = GET_RTX_NAME (code); *p1; p1++)
119 if (*p1 >= 'a' && *p1 <= 'z')
120 putchar (*p1 + 'A' - 'a');
126 /* Print a C expression to construct an RTX just like X,
127 substituting any operand references appearing within. */
133 register RTX_CODE code;
150 printf ("operand%d", XINT (x, 0));
154 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
155 XINT (x, 0), XINT (x, 0));
156 for (i = 0; i < XVECLEN (x, 1); i++)
159 gen_exp (XVECEXP (x, 1, i));
165 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
166 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
167 for (i = 0; i < XVECLEN (x, 2); i++)
170 gen_exp (XVECEXP (x, 2, i));
176 printf ("operand%d", XINT (x, 0));
180 printf ("gen_rtx (SCRATCH, %smode, 0)", GET_MODE_NAME (GET_MODE (x)));
184 fatal ("ADDRESS expression code used in named instruction pattern");
197 printf ("const0_rtx");
202 printf ("const1_rtx");
205 if (INTVAL (x) == -1)
207 printf ("constm1_rtx");
210 if (INTVAL (x) == STORE_FLAG_VALUE)
212 printf ("const_true_rtx");
217 printf ("gen_rtx (");
219 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
221 fmt = GET_RTX_FORMAT (code);
222 len = GET_RTX_LENGTH (code);
223 for (i = 0; i < len; i++)
228 if (fmt[i] == 'e' || fmt[i] == 'u')
229 gen_exp (XEXP (x, i));
230 else if (fmt[i] == 'i')
231 printf ("%u", (unsigned) XINT (x, i));
232 else if (fmt[i] == 's')
233 printf ("\"%s\"", XSTR (x, i));
234 else if (fmt[i] == 'E')
237 printf ("gen_rtvec (%d", XVECLEN (x, i));
238 for (j = 0; j < XVECLEN (x, i); j++)
241 gen_exp (XVECEXP (x, i, j));
251 /* Generate the `gen_...' function for a DEFINE_INSN. */
260 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
261 registers or MATCH_SCRATCHes. If so, store away the information for
266 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
267 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
268 || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
269 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
272 if (i != XVECLEN (insn, 1) - 1)
274 register struct clobber_pat *new
275 = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
277 new->code_number = insn_code_number;
279 new->first_clobber = i + 1;
280 new->next = clobber_list;
285 /* Don't mention instructions whose names are the null string.
286 They are in the machine description just to be recognized. */
287 if (strlen (XSTR (insn, 0)) == 0)
290 /* Find out how many operands this function has,
291 and also whether any of them have register constraints. */
292 register_constraints = 0;
293 operands = max_operand_vec (insn, 1);
294 if (max_dup_opno >= operands)
295 fatal ("match_dup operand number has no match_operand");
297 /* Output the function name and argument declarations. */
298 printf ("rtx\ngen_%s (", XSTR (insn, 0));
299 for (i = 0; i < operands; i++)
300 printf (i ? ", operand%d" : "operand%d", i);
302 for (i = 0; i < operands; i++)
303 printf (" rtx operand%d;\n", i);
306 /* Output code to construct and return the rtl for the instruction body */
308 if (XVECLEN (insn, 1) == 1)
311 gen_exp (XVECEXP (insn, 1, 0));
316 printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
317 for (i = 0; i < XVECLEN (insn, 1); i++)
320 gen_exp (XVECEXP (insn, 1, i));
322 printf ("));\n}\n\n");
326 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
335 if (strlen (XSTR (expand, 0)) == 0)
336 fatal ("define_expand lacks a name");
337 if (XVEC (expand, 1) == 0)
338 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
340 /* Find out how many operands this function has,
341 and also whether any of them have register constraints. */
342 register_constraints = 0;
344 operands = max_operand_vec (expand, 1);
346 /* Output the function name and argument declarations. */
347 printf ("rtx\ngen_%s (", XSTR (expand, 0));
348 for (i = 0; i < operands; i++)
349 printf (i ? ", operand%d" : "operand%d", i);
351 for (i = 0; i < operands; i++)
352 printf (" rtx operand%d;\n", i);
355 /* If we don't have any C code to write, only one insn is being written,
356 and no MATCH_DUPs are present, we can just return the desired insn
357 like we do for a DEFINE_INSN. This saves memory. */
358 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
359 && operands > max_dup_opno
360 && XVECLEN (expand, 1) == 1)
363 gen_exp (XVECEXP (expand, 1, 0));
368 /* For each operand referred to only with MATCH_DUPs,
369 make a local variable. */
370 for (i = operands; i <= max_dup_opno; i++)
371 printf (" rtx operand%d;\n", i);
372 if (operands > 0 || max_dup_opno >= 0)
373 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
374 printf (" rtx _val = 0;\n");
375 printf (" start_sequence ();\n");
377 /* The fourth operand of DEFINE_EXPAND is some code to be executed
378 before the actual construction.
379 This code expects to refer to `operands'
380 just as the output-code in a DEFINE_INSN does,
381 but here `operands' is an automatic array.
382 So copy the operand values there before executing it. */
383 if (XSTR (expand, 3) && *XSTR (expand, 3))
385 /* Output code to copy the arguments into `operands'. */
386 for (i = 0; i < operands; i++)
387 printf (" operands[%d] = operand%d;\n", i, i);
389 /* Output the special code to be executed before the sequence
391 printf ("%s\n", XSTR (expand, 3));
393 /* Output code to copy the arguments back out of `operands'
394 (unless we aren't going to use them at all). */
395 if (XVEC (expand, 1) != 0)
397 for (i = 0; i < operands; i++)
398 printf (" operand%d = operands[%d];\n", i, i);
399 for (; i <= max_dup_opno; i++)
400 printf (" operand%d = operands[%d];\n", i, i);
404 /* Output code to construct the rtl for the instruction bodies.
405 Use emit_insn to add them to the sequence being accumulated.
406 But don't do this if the user's code has set `no_more' nonzero. */
408 for (i = 0; i < XVECLEN (expand, 1); i++)
410 rtx next = XVECEXP (expand, 1, i);
411 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
412 || (GET_CODE (next) == PARALLEL
413 && GET_CODE (XVECEXP (next, 0, 0)) == SET
414 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
415 || GET_CODE (next) == RETURN)
416 printf (" emit_jump_insn (");
417 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
418 || GET_CODE (next) == CALL
419 || (GET_CODE (next) == PARALLEL
420 && GET_CODE (XVECEXP (next, 0, 0)) == SET
421 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
422 || (GET_CODE (next) == PARALLEL
423 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
424 printf (" emit_call_insn (");
425 else if (GET_CODE (next) == CODE_LABEL)
426 printf (" emit_label (");
427 else if (GET_CODE (next) == MATCH_OPERAND
428 || GET_CODE (next) == MATCH_OPERATOR
429 || GET_CODE (next) == MATCH_PARALLEL
430 || GET_CODE (next) == MATCH_OP_DUP
431 || GET_CODE (next) == MATCH_DUP
432 || GET_CODE (next) == PARALLEL)
435 printf (" emit_insn (");
438 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
439 && GET_CODE (SET_SRC (next)) == LABEL_REF)
440 printf (" emit_barrier ();");
443 /* Call `gen_sequence' to make a SEQUENCE out of all the
444 insns emitted within this gen_... function. */
446 printf (" _done:\n");
447 printf (" _val = gen_sequence ();\n");
448 printf (" _fail:\n");
449 printf (" end_sequence ();\n");
450 printf (" return _val;\n}\n\n");
453 /* Like gen_expand, but generates a SEQUENCE. */
461 if (XVEC (split, 0) == 0)
462 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
463 else if (XVEC (split, 2) == 0)
464 fatal ("define_split (definition %d) lacks a replacement pattern",
467 /* Find out how many operands this function has. */
469 max_operand_vec (split, 2);
470 operands = MAX (max_opno, max_dup_opno) + 1;
472 /* Output the function name and argument declarations. */
473 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
477 /* Declare all local variables. */
478 for (i = 0; i < operands; i++)
479 printf (" rtx operand%d;\n", i);
480 printf (" rtx _val;\n");
481 printf (" start_sequence ();\n");
483 /* The fourth operand of DEFINE_SPLIT is some code to be executed
484 before the actual construction. */
487 printf ("%s\n", XSTR (split, 3));
489 /* Output code to copy the arguments back out of `operands' */
490 for (i = 0; i < operands; i++)
491 printf (" operand%d = operands[%d];\n", i, i);
493 /* Output code to construct the rtl for the instruction bodies.
494 Use emit_insn to add them to the sequence being accumulated.
495 But don't do this if the user's code has set `no_more' nonzero. */
497 for (i = 0; i < XVECLEN (split, 2); i++)
499 rtx next = XVECEXP (split, 2, i);
500 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
501 || (GET_CODE (next) == PARALLEL
502 && GET_CODE (XVECEXP (next, 0, 0)) == SET
503 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
504 || GET_CODE (next) == RETURN)
505 printf (" emit_jump_insn (");
506 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
507 || GET_CODE (next) == CALL
508 || (GET_CODE (next) == PARALLEL
509 && GET_CODE (XVECEXP (next, 0, 0)) == SET
510 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
511 || (GET_CODE (next) == PARALLEL
512 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
513 printf (" emit_call_insn (");
514 else if (GET_CODE (next) == CODE_LABEL)
515 printf (" emit_label (");
516 else if (GET_CODE (next) == MATCH_OPERAND
517 || GET_CODE (next) == MATCH_OPERATOR
518 || GET_CODE (next) == MATCH_PARALLEL
519 || GET_CODE (next) == MATCH_OP_DUP
520 || GET_CODE (next) == MATCH_DUP
521 || GET_CODE (next) == PARALLEL)
524 printf (" emit_insn (");
527 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
528 && GET_CODE (SET_SRC (next)) == LABEL_REF)
529 printf (" emit_barrier ();");
532 /* Call `gen_sequence' to make a SEQUENCE out of all the
533 insns emitted within this gen_... function. */
535 printf (" _done:\n");
536 printf (" _val = gen_sequence ();\n");
537 printf (" _fail:\n");
538 printf (" end_sequence ();\n");
539 printf (" return _val;\n}\n\n");
542 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
543 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
544 the end of the vector. */
547 output_add_clobbers ()
549 struct clobber_pat *clobber;
552 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
553 printf (" rtx pattern;\n int insn_code_number;\n");
555 printf (" int i;\n\n");
556 printf (" switch (insn_code_number)\n");
559 for (clobber = clobber_list; clobber; clobber = clobber->next)
561 printf (" case %d:\n", clobber->code_number);
563 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
565 printf (" XVECEXP (pattern, 0, %d) = ", i);
566 gen_exp (XVECEXP (clobber->pattern, 1, i));
570 printf (" break;\n");
573 printf (" default:\n");
574 printf (" abort ();\n");
579 /* Write a function, init_mov_optab, that is called to set up entries
580 in mov_optab for EXTRA_CC_MODES. */
583 output_init_mov_optab ()
585 #ifdef EXTRA_CC_NAMES
586 static char *cc_names[] = { EXTRA_CC_NAMES };
590 printf ("\nvoid\ninit_mov_optab ()\n{\n");
592 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
594 printf ("#ifdef HAVE_mov");
595 for (p = cc_names[i]; *p; p++)
596 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
598 printf (" if (HAVE_mov");
599 for (p = cc_names[i]; *p; p++)
600 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
602 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
604 for (p = cc_names[i]; *p; p++)
605 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
606 printf (";\n#endif\n");
617 register char *val = (char *) malloc (size);
620 fatal ("virtual memory exhausted");
630 char *result = (char *) realloc (ptr, size);
632 fatal ("virtual memory exhausted");
640 fprintf (stderr, "genemit: ");
641 fprintf (stderr, s, a1, a2);
642 fprintf (stderr, "\n");
643 exit (FATAL_EXIT_CODE);
646 /* More 'friendly' abort that prints the line and file.
647 config.h can #define abort fancy_abort if you like that sort of thing. */
652 fatal ("Internal gcc abort.");
662 extern rtx read_rtx ();
665 obstack_init (rtl_obstack);
668 fatal ("No input file name.");
670 infile = fopen (argv[1], "r");
674 exit (FATAL_EXIT_CODE);
679 /* Assign sequential codes to all entries in the machine description
680 in parallel with the tables in insn-output.c. */
682 insn_code_number = 0;
683 insn_index_number = 0;
685 printf ("/* Generated automatically by the program `genemit'\n\
686 from the machine description file `md'. */\n\n");
688 printf ("#include \"config.h\"\n");
689 printf ("#include \"rtl.h\"\n");
690 printf ("#include \"expr.h\"\n");
691 printf ("#include \"real.h\"\n");
692 printf ("#include \"output.h\"\n");
693 printf ("#include \"insn-config.h\"\n\n");
694 printf ("#include \"insn-flags.h\"\n\n");
695 printf ("#include \"insn-codes.h\"\n\n");
696 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
697 printf ("extern rtx recog_operand[];\n");
698 printf ("#define operands emit_operand\n\n");
699 printf ("#define FAIL goto _fail\n\n");
700 printf ("#define DONE goto _done\n\n");
702 /* Read the machine description. */
706 c = read_skip_spaces (infile);
711 desc = read_rtx (infile);
712 if (GET_CODE (desc) == DEFINE_INSN)
717 if (GET_CODE (desc) == DEFINE_EXPAND)
722 if (GET_CODE (desc) == DEFINE_SPLIT)
727 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
734 /* Write out the routine to add CLOBBERs to a pattern. */
735 output_add_clobbers ();
737 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
738 output_init_mov_optab ();
741 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);