]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genemit.c
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));
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
;
67 register RTX_CODE code
;
77 if (code
== MATCH_OPERAND
&& XSTR (x
, 2) != 0 && *XSTR (x
, 2) != '\0')
78 register_constraints
= 1;
79 if (code
== MATCH_SCRATCH
&& XSTR (x
, 1) != 0 && *XSTR (x
, 1) != '\0')
80 register_constraints
= 1;
81 if (code
== MATCH_OPERAND
|| code
== MATCH_OPERATOR
82 || code
== MATCH_PARALLEL
)
83 max_opno
= MAX (max_opno
, XINT (x
, 0));
84 if (code
== MATCH_DUP
|| code
== MATCH_OP_DUP
|| code
== MATCH_PAR_DUP
)
85 max_dup_opno
= MAX (max_dup_opno
, XINT (x
, 0));
87 fmt
= GET_RTX_FORMAT (code
);
88 len
= GET_RTX_LENGTH (code
);
89 for (i
= 0; i
< len
; i
++)
91 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
92 max_operand_1 (XEXP (x
, i
));
93 else if (fmt
[i
] == 'E')
96 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
97 max_operand_1 (XVECEXP (x
, i
, j
));
103 max_operand_vec (insn
, arg
)
107 register int len
= XVECLEN (insn
, arg
);
113 for (i
= 0; i
< len
; i
++)
114 max_operand_1 (XVECEXP (insn
, arg
, i
));
124 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
126 if (*p1
>= 'a' && *p1
<= 'z')
127 putchar (*p1
+ 'A' - 'a');
133 /* Print a C expression to construct an RTX just like X,
134 substituting any operand references appearing within. */
140 register RTX_CODE code
;
157 printf ("operand%d", XINT (x
, 0));
161 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
162 XINT (x
, 0), XINT (x
, 0));
163 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
166 gen_exp (XVECEXP (x
, 1, i
));
172 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x
, 0));
173 printf (", %smode", GET_MODE_NAME (GET_MODE (x
)));
174 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
177 gen_exp (XVECEXP (x
, 2, i
));
184 printf ("operand%d", XINT (x
, 0));
188 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x
)));
192 fatal ("ADDRESS expression code used in named instruction pattern");
204 printf ("const0_rtx");
205 else if (INTVAL (x
) == 1)
206 printf ("const1_rtx");
207 else if (INTVAL (x
) == -1)
208 printf ("constm1_rtx");
209 else if (INTVAL (x
) == STORE_FLAG_VALUE
)
210 printf ("const_true_rtx");
213 printf ("GEN_INT (");
214 printf (HOST_WIDE_INT_PRINT_DEC
, INTVAL (x
));
220 /* These shouldn't be written in MD files. Instead, the appropriate
221 routines in varasm.c should be called. */
230 printf (" (%smode", GET_MODE_NAME (GET_MODE (x
)));
232 fmt
= GET_RTX_FORMAT (code
);
233 len
= GET_RTX_LENGTH (code
);
234 for (i
= 0; i
< len
; i
++)
239 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
240 gen_exp (XEXP (x
, i
));
241 else if (fmt
[i
] == 'i')
242 printf ("%u", XINT (x
, i
));
243 else if (fmt
[i
] == 's')
244 printf ("\"%s\"", XSTR (x
, i
));
245 else if (fmt
[i
] == 'E')
248 printf ("gen_rtvec (%d", XVECLEN (x
, i
));
249 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
252 gen_exp (XVECEXP (x
, i
, j
));
262 /* Generate the `gen_...' function for a DEFINE_INSN. */
271 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
272 registers or MATCH_SCRATCHes. If so, store away the information for
277 for (i
= XVECLEN (insn
, 1) - 1; i
> 0; i
--)
278 if (GET_CODE (XVECEXP (insn
, 1, i
)) != CLOBBER
279 || (GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != REG
280 && GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != MATCH_SCRATCH
))
283 if (i
!= XVECLEN (insn
, 1) - 1)
285 register struct clobber_pat
*p
;
286 register struct clobber_ent
*link
287 = (struct clobber_ent
*) xmalloc (sizeof (struct clobber_ent
));
290 link
->code_number
= insn_code_number
;
292 /* See if any previous CLOBBER_LIST entry is the same as this
295 for (p
= clobber_list
; p
; p
= p
->next
)
297 if (p
->first_clobber
!= i
+ 1
298 || XVECLEN (p
->pattern
, 1) != XVECLEN (insn
, 1))
301 for (j
= i
+ 1; j
< XVECLEN (insn
, 1); j
++)
303 rtx old
= XEXP (XVECEXP (p
->pattern
, 1, j
), 0);
304 rtx
new = XEXP (XVECEXP (insn
, 1, j
), 0);
306 /* OLD and NEW are the same if both are to be a SCRATCH
308 or if both are registers of the same mode and number. */
309 if (! (GET_MODE (old
) == GET_MODE (new)
310 && ((GET_CODE (old
) == MATCH_SCRATCH
311 && GET_CODE (new) == MATCH_SCRATCH
)
312 || (GET_CODE (old
) == REG
&& GET_CODE (new) == REG
313 && REGNO (old
) == REGNO (new)))))
317 if (j
== XVECLEN (insn
, 1))
323 p
= (struct clobber_pat
*) xmalloc (sizeof (struct clobber_pat
));
327 p
->first_clobber
= i
+ 1;
328 p
->next
= clobber_list
;
332 link
->next
= p
->insns
;
337 /* Don't mention instructions whose names are the null string
338 or begin with '*'. They are in the machine description just
340 if (XSTR (insn
, 0)[0] == 0 || XSTR (insn
, 0)[0] == '*')
343 /* Find out how many operands this function has,
344 and also whether any of them have register constraints. */
345 register_constraints
= 0;
346 operands
= max_operand_vec (insn
, 1);
347 if (max_dup_opno
>= operands
)
348 fatal ("match_dup operand number has no match_operand");
350 /* Output the function name and argument declarations. */
351 printf ("rtx\ngen_%s (", XSTR (insn
, 0));
352 for (i
= 0; i
< operands
; i
++)
353 printf (i
? ", operand%d" : "operand%d", i
);
355 for (i
= 0; i
< operands
; i
++)
356 printf (" rtx operand%d;\n", i
);
359 /* Output code to construct and return the rtl for the instruction body */
361 if (XVECLEN (insn
, 1) == 1)
364 gen_exp (XVECEXP (insn
, 1, 0));
369 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn
, 1));
370 for (i
= 0; i
< XVECLEN (insn
, 1); i
++)
373 gen_exp (XVECEXP (insn
, 1, i
));
375 printf ("));\n}\n\n");
379 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
388 if (strlen (XSTR (expand
, 0)) == 0)
389 fatal ("define_expand lacks a name");
390 if (XVEC (expand
, 1) == 0)
391 fatal ("define_expand for %s lacks a pattern", XSTR (expand
, 0));
393 /* Find out how many operands this function has,
394 and also whether any of them have register constraints. */
395 register_constraints
= 0;
397 operands
= max_operand_vec (expand
, 1);
399 /* Output the function name and argument declarations. */
400 printf ("rtx\ngen_%s (", XSTR (expand
, 0));
401 for (i
= 0; i
< operands
; i
++)
402 printf (i
? ", operand%d" : "operand%d", i
);
404 for (i
= 0; i
< operands
; i
++)
405 printf (" rtx operand%d;\n", i
);
408 /* If we don't have any C code to write, only one insn is being written,
409 and no MATCH_DUPs are present, we can just return the desired insn
410 like we do for a DEFINE_INSN. This saves memory. */
411 if ((XSTR (expand
, 3) == 0 || *XSTR (expand
, 3) == '\0')
412 && operands
> max_dup_opno
413 && XVECLEN (expand
, 1) == 1)
416 gen_exp (XVECEXP (expand
, 1, 0));
421 /* For each operand referred to only with MATCH_DUPs,
422 make a local variable. */
423 for (i
= operands
; i
<= max_dup_opno
; i
++)
424 printf (" rtx operand%d;\n", i
);
425 if (operands
> 0 || max_dup_opno
>= 0)
426 printf (" rtx operands[%d];\n", MAX (operands
, max_dup_opno
+ 1));
427 printf (" rtx _val = 0;\n");
428 printf (" start_sequence ();\n");
430 /* The fourth operand of DEFINE_EXPAND is some code to be executed
431 before the actual construction.
432 This code expects to refer to `operands'
433 just as the output-code in a DEFINE_INSN does,
434 but here `operands' is an automatic array.
435 So copy the operand values there before executing it. */
436 if (XSTR (expand
, 3) && *XSTR (expand
, 3))
438 /* Output code to copy the arguments into `operands'. */
439 for (i
= 0; i
< operands
; i
++)
440 printf (" operands[%d] = operand%d;\n", i
, i
);
442 /* Output the special code to be executed before the sequence
444 printf ("%s\n", XSTR (expand
, 3));
446 /* Output code to copy the arguments back out of `operands'
447 (unless we aren't going to use them at all). */
448 if (XVEC (expand
, 1) != 0)
450 for (i
= 0; i
< operands
; i
++)
451 printf (" operand%d = operands[%d];\n", i
, i
);
452 for (; i
<= max_dup_opno
; i
++)
453 printf (" operand%d = operands[%d];\n", i
, i
);
457 /* Output code to construct the rtl for the instruction bodies.
458 Use emit_insn to add them to the sequence being accumulated.
459 But don't do this if the user's code has set `no_more' nonzero. */
461 for (i
= 0; i
< XVECLEN (expand
, 1); i
++)
463 rtx next
= XVECEXP (expand
, 1, i
);
464 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
465 || (GET_CODE (next
) == PARALLEL
466 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
467 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
468 || GET_CODE (next
) == RETURN
)
469 printf (" emit_jump_insn (");
470 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
471 || GET_CODE (next
) == CALL
472 || (GET_CODE (next
) == PARALLEL
473 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
474 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
475 || (GET_CODE (next
) == PARALLEL
476 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
477 printf (" emit_call_insn (");
478 else if (GET_CODE (next
) == CODE_LABEL
)
479 printf (" emit_label (");
480 else if (GET_CODE (next
) == MATCH_OPERAND
481 || GET_CODE (next
) == MATCH_OPERATOR
482 || GET_CODE (next
) == MATCH_PARALLEL
483 || GET_CODE (next
) == MATCH_OP_DUP
484 || GET_CODE (next
) == MATCH_DUP
485 || GET_CODE (next
) == PARALLEL
)
488 printf (" emit_insn (");
491 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
492 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
493 printf (" emit_barrier ();");
496 /* Call `gen_sequence' to make a SEQUENCE out of all the
497 insns emitted within this gen_... function. */
499 printf (" _done:\n");
500 printf (" _val = gen_sequence ();\n");
501 printf (" _fail:\n");
502 printf (" end_sequence ();\n");
503 printf (" return _val;\n}\n\n");
506 /* Like gen_expand, but generates a SEQUENCE. */
515 if (XVEC (split
, 0) == 0)
516 fatal ("define_split (definition %d) lacks a pattern", insn_index_number
);
517 else if (XVEC (split
, 2) == 0)
518 fatal ("define_split (definition %d) lacks a replacement pattern",
521 /* Find out how many operands this function has. */
523 max_operand_vec (split
, 2);
524 operands
= MAX (max_opno
, max_dup_opno
) + 1;
526 /* Output the function name and argument declarations. */
527 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
531 /* Declare all local variables. */
532 for (i
= 0; i
< operands
; i
++)
533 printf (" rtx operand%d;\n", i
);
534 printf (" rtx _val = 0;\n");
535 printf (" start_sequence ();\n");
537 /* The fourth operand of DEFINE_SPLIT is some code to be executed
538 before the actual construction. */
541 printf ("%s\n", XSTR (split
, 3));
543 /* Output code to copy the arguments back out of `operands' */
544 for (i
= 0; i
< operands
; i
++)
545 printf (" operand%d = operands[%d];\n", i
, i
);
547 /* Output code to construct the rtl for the instruction bodies.
548 Use emit_insn to add them to the sequence being accumulated.
549 But don't do this if the user's code has set `no_more' nonzero. */
551 for (i
= 0; i
< XVECLEN (split
, 2); i
++)
553 rtx next
= XVECEXP (split
, 2, i
);
554 if ((GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
)
555 || (GET_CODE (next
) == PARALLEL
556 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
557 && GET_CODE (SET_DEST (XVECEXP (next
, 0, 0))) == PC
)
558 || GET_CODE (next
) == RETURN
)
559 printf (" emit_jump_insn (");
560 else if ((GET_CODE (next
) == SET
&& GET_CODE (SET_SRC (next
)) == CALL
)
561 || GET_CODE (next
) == CALL
562 || (GET_CODE (next
) == PARALLEL
563 && GET_CODE (XVECEXP (next
, 0, 0)) == SET
564 && GET_CODE (SET_SRC (XVECEXP (next
, 0, 0))) == CALL
)
565 || (GET_CODE (next
) == PARALLEL
566 && GET_CODE (XVECEXP (next
, 0, 0)) == CALL
))
567 printf (" emit_call_insn (");
568 else if (GET_CODE (next
) == CODE_LABEL
)
569 printf (" emit_label (");
570 else if (GET_CODE (next
) == MATCH_OPERAND
571 || GET_CODE (next
) == MATCH_OPERATOR
572 || GET_CODE (next
) == MATCH_PARALLEL
573 || GET_CODE (next
) == MATCH_OP_DUP
574 || GET_CODE (next
) == MATCH_DUP
575 || GET_CODE (next
) == PARALLEL
)
578 printf (" emit_insn (");
581 if (GET_CODE (next
) == SET
&& GET_CODE (SET_DEST (next
)) == PC
582 && GET_CODE (SET_SRC (next
)) == LABEL_REF
)
583 printf (" emit_barrier ();");
586 /* Call `gen_sequence' to make a SEQUENCE out of all the
587 insns emitted within this gen_... function. */
589 printf (" _done:\n");
590 printf (" _val = gen_sequence ();\n");
591 printf (" _fail:\n");
592 printf (" end_sequence ();\n");
593 printf (" return _val;\n}\n\n");
596 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
597 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
598 the end of the vector. */
601 output_add_clobbers ()
603 struct clobber_pat
*clobber
;
604 struct clobber_ent
*ent
;
607 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
608 printf (" rtx pattern;\n int insn_code_number;\n");
610 printf (" int i;\n\n");
611 printf (" switch (insn_code_number)\n");
614 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
616 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
617 printf (" case %d:\n", ent
->code_number
);
619 for (i
= clobber
->first_clobber
; i
< XVECLEN (clobber
->pattern
, 1); i
++)
621 printf (" XVECEXP (pattern, 0, %d) = ", i
);
622 gen_exp (XVECEXP (clobber
->pattern
, 1, i
));
626 printf (" break;\n\n");
629 printf (" default:\n");
630 printf (" abort ();\n");
635 /* Write a function, init_mov_optab, that is called to set up entries
636 in mov_optab for EXTRA_CC_MODES. */
639 output_init_mov_optab ()
641 #ifdef EXTRA_CC_NAMES
642 static char *cc_names
[] = { EXTRA_CC_NAMES
};
646 printf ("\nvoid\ninit_mov_optab ()\n{\n");
648 for (i
= 0; i
< sizeof cc_names
/ sizeof cc_names
[0]; i
++)
650 printf ("#ifdef HAVE_mov");
651 for (p
= cc_names
[i
]; *p
; p
++)
652 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
654 printf (" if (HAVE_mov");
655 for (p
= cc_names
[i
]; *p
; p
++)
656 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
658 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
660 for (p
= cc_names
[i
]; *p
; p
++)
661 printf ("%c", *p
>= 'A' && *p
<= 'Z' ? *p
- 'A' + 'a' : *p
);
662 printf (";\n#endif\n");
673 register char *val
= (char *) malloc (size
);
676 fatal ("virtual memory exhausted");
686 char *result
= (char *) realloc (ptr
, size
);
688 fatal ("virtual memory exhausted");
696 fprintf (stderr
, "genemit: ");
697 fprintf (stderr
, s
, a1
, a2
);
698 fprintf (stderr
, "\n");
699 exit (FATAL_EXIT_CODE
);
702 /* More 'friendly' abort that prints the line and file.
703 config.h can #define abort fancy_abort if you like that sort of thing. */
708 fatal ("Internal gcc abort.");
720 obstack_init (rtl_obstack
);
723 fatal ("No input file name.");
725 infile
= fopen (argv
[1], "r");
729 exit (FATAL_EXIT_CODE
);
734 /* Assign sequential codes to all entries in the machine description
735 in parallel with the tables in insn-output.c. */
737 insn_code_number
= 0;
738 insn_index_number
= 0;
740 printf ("/* Generated automatically by the program `genemit'\n\
741 from the machine description file `md'. */\n\n");
743 printf ("#include \"config.h\"\n");
744 printf ("#include <stdio.h>\n");
745 printf ("#include \"rtl.h\"\n");
746 printf ("#include \"expr.h\"\n");
747 printf ("#include \"real.h\"\n");
748 printf ("#include \"flags.h\"\n");
749 printf ("#include \"output.h\"\n");
750 printf ("#include \"insn-config.h\"\n\n");
751 printf ("#include \"insn-flags.h\"\n\n");
752 printf ("#include \"insn-codes.h\"\n\n");
753 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
754 printf ("extern rtx recog_operand[];\n");
755 printf ("#define operands emit_operand\n\n");
756 printf ("#define FAIL goto _fail\n\n");
757 printf ("#define DONE goto _done\n\n");
759 /* Read the machine description. */
763 c
= read_skip_spaces (infile
);
768 desc
= read_rtx (infile
);
769 if (GET_CODE (desc
) == DEFINE_INSN
)
774 if (GET_CODE (desc
) == DEFINE_EXPAND
)
779 if (GET_CODE (desc
) == DEFINE_SPLIT
)
784 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
791 /* Write out the routine to add CLOBBERs to a pattern. */
792 output_add_clobbers ();
794 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
795 output_init_mov_optab ();
798 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);