1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987-1991 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 %d lacks a pattern", insn_index_number);
463 else if (XVEC (split, 2) == 0)
464 fatal ("define_split %d lacks a replacement pattern", insn_index_number);
466 /* Find out how many operands this function has. */
468 max_operand_vec (split, 2);
469 operands = MAX (max_opno, max_dup_opno) + 1;
471 /* Output the function name and argument declarations. */
472 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
476 /* Declare all local variables. */
477 for (i = 0; i < operands; i++)
478 printf (" rtx operand%d;\n", i);
479 printf (" rtx _val;\n");
480 printf (" start_sequence ();\n");
482 /* The fourth operand of DEFINE_SPLIT is some code to be executed
483 before the actual construction. */
486 printf ("%s\n", XSTR (split, 3));
488 /* Output code to copy the arguments back out of `operands' */
489 for (i = 0; i < operands; i++)
490 printf (" operand%d = operands[%d];\n", i, i);
492 /* Output code to construct the rtl for the instruction bodies.
493 Use emit_insn to add them to the sequence being accumulated.
494 But don't do this if the user's code has set `no_more' nonzero. */
496 for (i = 0; i < XVECLEN (split, 2); i++)
498 rtx next = XVECEXP (split, 2, i);
499 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
500 || (GET_CODE (next) == PARALLEL
501 && GET_CODE (XVECEXP (next, 0, 0)) == SET
502 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
503 || GET_CODE (next) == RETURN)
504 printf (" emit_jump_insn (");
505 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
506 || GET_CODE (next) == CALL
507 || (GET_CODE (next) == PARALLEL
508 && GET_CODE (XVECEXP (next, 0, 0)) == SET
509 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
510 || (GET_CODE (next) == PARALLEL
511 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
512 printf (" emit_call_insn (");
513 else if (GET_CODE (next) == CODE_LABEL)
514 printf (" emit_label (");
515 else if (GET_CODE (next) == MATCH_OPERAND
516 || GET_CODE (next) == MATCH_OPERATOR
517 || GET_CODE (next) == MATCH_PARALLEL
518 || GET_CODE (next) == MATCH_OP_DUP
519 || GET_CODE (next) == MATCH_DUP
520 || GET_CODE (next) == PARALLEL)
523 printf (" emit_insn (");
526 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
527 && GET_CODE (SET_SRC (next)) == LABEL_REF)
528 printf (" emit_barrier ();");
531 /* Call `gen_sequence' to make a SEQUENCE out of all the
532 insns emitted within this gen_... function. */
534 printf (" _done:\n");
535 printf (" _val = gen_sequence ();\n");
536 printf (" _fail:\n");
537 printf (" end_sequence ();\n");
538 printf (" return _val;\n}\n\n");
541 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
542 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
543 the end of the vector. */
546 output_add_clobbers ()
548 struct clobber_pat *clobber;
551 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
552 printf (" rtx pattern;\n int insn_code_number;\n");
554 printf (" int i;\n\n");
555 printf (" switch (insn_code_number)\n");
558 for (clobber = clobber_list; clobber; clobber = clobber->next)
560 printf (" case %d:\n", clobber->code_number);
562 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
564 printf (" XVECEXP (pattern, 0, %d) = ", i);
565 gen_exp (XVECEXP (clobber->pattern, 1, i));
569 printf (" break;\n");
572 printf (" default:\n");
573 printf (" abort ();\n");
578 /* Write a function, init_mov_optab, that is called to set up entries
579 in mov_optab for EXTRA_CC_MODES. */
582 output_init_mov_optab ()
584 #ifdef EXTRA_CC_NAMES
585 static char *cc_names[] = { EXTRA_CC_NAMES };
589 printf ("\nvoid\ninit_mov_optab ()\n{\n");
591 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
593 printf ("#ifdef HAVE_mov");
594 for (p = cc_names[i]; *p; p++)
595 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
597 printf (" if (HAVE_mov");
598 for (p = cc_names[i]; *p; p++)
599 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
601 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
603 for (p = cc_names[i]; *p; p++)
604 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
605 printf (";\n#endif\n");
616 register char *val = (char *) malloc (size);
619 fatal ("virtual memory exhausted");
629 char *result = (char *) realloc (ptr, size);
631 fatal ("virtual memory exhausted");
639 fprintf (stderr, "genemit: ");
640 fprintf (stderr, s, a1, a2);
641 fprintf (stderr, "\n");
642 exit (FATAL_EXIT_CODE);
645 /* More 'friendly' abort that prints the line and file.
646 config.h can #define abort fancy_abort if you like that sort of thing. */
651 fatal ("Internal gcc abort.");
661 extern rtx read_rtx ();
664 obstack_init (rtl_obstack);
667 fatal ("No input file name.");
669 infile = fopen (argv[1], "r");
673 exit (FATAL_EXIT_CODE);
678 /* Assign sequential codes to all entries in the machine description
679 in parallel with the tables in insn-output.c. */
681 insn_code_number = 0;
682 insn_index_number = 0;
684 printf ("/* Generated automatically by the program `genemit'\n\
685 from the machine description file `md'. */\n\n");
687 printf ("#include \"config.h\"\n");
688 printf ("#include \"rtl.h\"\n");
689 printf ("#include \"expr.h\"\n");
690 printf ("#include \"real.h\"\n");
691 printf ("#include \"output.h\"\n");
692 printf ("#include \"insn-config.h\"\n\n");
693 printf ("#include \"insn-flags.h\"\n\n");
694 printf ("#include \"insn-codes.h\"\n\n");
695 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
696 printf ("extern rtx recog_operand[];\n");
697 printf ("#define operands emit_operand\n\n");
698 printf ("#define FAIL goto _fail\n\n");
699 printf ("#define DONE goto _done\n\n");
701 /* Read the machine description. */
705 c = read_skip_spaces (infile);
710 desc = read_rtx (infile);
711 if (GET_CODE (desc) == DEFINE_INSN)
716 if (GET_CODE (desc) == DEFINE_EXPAND)
721 if (GET_CODE (desc) == DEFINE_SPLIT)
726 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
733 /* Write out the routine to add CLOBBERs to a pattern. */
734 output_add_clobbers ();
736 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
737 output_init_mov_optab ();
740 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);