fix typo
[official-gcc.git] / gcc / genrecog.c
blobb955eb0614949f1699496c838caa9b445881009d
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This program is used to produce insn-recog.c, which contains a
24 function called `recog' plus its subroutines. These functions
25 contain a decision tree that recognizes whether an rtx, the
26 argument given to recog, is a valid instruction.
28 recog returns -1 if the rtx is not valid. If the rtx is valid,
29 recog returns a nonnegative number which is the insn code number
30 for the pattern that matched. This is the same as the order in the
31 machine description of the entry that matched. This number can be
32 used as an index into various insn_* tables, such as insn_template,
33 insn_outfun, and insn_n_operands (found in insn-output.c).
35 The third argument to recog is an optional pointer to an int. If
36 present, recog will accept a pattern if it matches except for
37 missing CLOBBER expressions at the end. In that case, the value
38 pointed to by the optional pointer will be set to the number of
39 CLOBBERs that need to be added (it should be initialized to zero by
40 the caller). If it is set nonzero, the caller should allocate a
41 PARALLEL of the appropriate size, copy the initial entries, and
42 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
44 This program also generates the function `split_insns', which
45 returns 0 if the rtl could not be split, or it returns the split
46 rtl in a SEQUENCE.
48 This program also generates the function `peephole2_insns', which
49 returns 0 if the rtl could not be matched. If there was a match,
50 the new rtl is returned in a SEQUENCE, and LAST_INSN will point
51 to the last recognized insn in the old sequence. */
53 #include "hconfig.h"
54 #include "system.h"
55 #include "rtl.h"
56 #include "errors.h"
57 #include "gensupport.h"
60 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
61 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
63 /* Holds an array of names indexed by insn_code_number. */
64 static char **insn_name_ptr = 0;
65 static int insn_name_ptr_size = 0;
67 /* A listhead of decision trees. The alternatives to a node are kept
68 in a doublely-linked list so we can easily add nodes to the proper
69 place when merging. */
71 struct decision_head
73 struct decision *first;
74 struct decision *last;
77 /* A single test. The two accept types aren't tests per-se, but
78 their equality (or lack thereof) does affect tree merging so
79 it is convenient to keep them here. */
81 struct decision_test
83 /* A linked list through the tests attached to a node. */
84 struct decision_test *next;
86 /* These types are roughly in the order in which we'd like to test them. */
87 enum decision_type {
88 DT_mode, DT_code, DT_veclen,
89 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide,
90 DT_dup, DT_pred, DT_c_test,
91 DT_accept_op, DT_accept_insn
92 } type;
94 union
96 enum machine_mode mode; /* Machine mode of node. */
97 RTX_CODE code; /* Code to test. */
99 struct
101 const char *name; /* Predicate to call. */
102 int index; /* Index into `preds' or -1. */
103 enum machine_mode mode; /* Machine mode for node. */
104 } pred;
106 const char *c_test; /* Additional test to perform. */
107 int veclen; /* Length of vector. */
108 int dup; /* Number of operand to compare against. */
109 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
110 int opno; /* Operand number matched. */
112 struct {
113 int code_number; /* Insn number matched. */
114 int lineno; /* Line number of the insn. */
115 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
116 } insn;
117 } u;
120 /* Data structure for decision tree for recognizing legitimate insns. */
122 struct decision
124 struct decision_head success; /* Nodes to test on success. */
125 struct decision *next; /* Node to test on failure. */
126 struct decision *prev; /* Node whose failure tests us. */
127 struct decision *afterward; /* Node to test on success,
128 but failure of successor nodes. */
130 const char *position; /* String denoting position in pattern. */
132 struct decision_test *tests; /* The tests for this node. */
134 int number; /* Node number, used for labels */
135 int subroutine_number; /* Number of subroutine this node starts */
136 int need_label; /* Label needs to be output. */
139 #define SUBROUTINE_THRESHOLD 100
141 static int next_subroutine_number;
143 /* We can write three types of subroutines: One for insn recognition,
144 one to split insns, and one for peephole-type optimizations. This
145 defines which type is being written. */
147 enum routine_type {
148 RECOG, SPLIT, PEEPHOLE2
151 #define IS_SPLIT(X) ((X) != RECOG)
153 /* Next available node number for tree nodes. */
155 static int next_number;
157 /* Next number to use as an insn_code. */
159 static int next_insn_code;
161 /* Similar, but counts all expressions in the MD file; used for
162 error messages. */
164 static int next_index;
166 /* Record the highest depth we ever have so we know how many variables to
167 allocate in each subroutine we make. */
169 static int max_depth;
171 /* The line number of the start of the pattern currently being processed. */
172 static int pattern_lineno;
174 /* Count of errors. */
175 static int error_count;
177 /* This table contains a list of the rtl codes that can possibly match a
178 predicate defined in recog.c. The function `maybe_both_true' uses it to
179 deduce that there are no expressions that can be matches by certain pairs
180 of tree nodes. Also, if a predicate can match only one code, we can
181 hardwire that code into the node testing the predicate. */
183 static struct pred_table
185 const char *name;
186 RTX_CODE codes[NUM_RTX_CODE];
187 } preds[] = {
188 {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
189 LABEL_REF, SUBREG, REG, MEM}},
190 #ifdef PREDICATE_CODES
191 PREDICATE_CODES
192 #endif
193 {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
194 LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
195 {"register_operand", {SUBREG, REG}},
196 {"pmode_register_operand", {SUBREG, REG}},
197 {"scratch_operand", {SCRATCH, REG}},
198 {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
199 LABEL_REF}},
200 {"const_int_operand", {CONST_INT}},
201 {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
202 {"nonimmediate_operand", {SUBREG, REG, MEM}},
203 {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
204 LABEL_REF, SUBREG, REG}},
205 {"push_operand", {MEM}},
206 {"pop_operand", {MEM}},
207 {"memory_operand", {SUBREG, MEM}},
208 {"indirect_operand", {SUBREG, MEM}},
209 {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
210 UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
211 UNLT, LTGT}},
212 {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
213 LABEL_REF, SUBREG, REG, MEM}}
216 #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
218 static const char * special_mode_pred_table[] = {
219 #ifdef SPECIAL_MODE_PREDICATES
220 SPECIAL_MODE_PREDICATES
221 #endif
222 "pmode_register_operand"
225 #define NUM_SPECIAL_MODE_PREDS \
226 (sizeof (special_mode_pred_table) / sizeof (special_mode_pred_table[0]))
228 static struct decision *new_decision
229 PARAMS ((const char *, struct decision_head *));
230 static struct decision_test *new_decision_test
231 PARAMS ((enum decision_type, struct decision_test ***));
232 static rtx find_operand
233 PARAMS ((rtx, int));
234 static void validate_pattern
235 PARAMS ((rtx, rtx, rtx));
236 static struct decision *add_to_sequence
237 PARAMS ((rtx, struct decision_head *, const char *, enum routine_type, int));
239 static int maybe_both_true_2
240 PARAMS ((struct decision_test *, struct decision_test *));
241 static int maybe_both_true_1
242 PARAMS ((struct decision_test *, struct decision_test *));
243 static int maybe_both_true
244 PARAMS ((struct decision *, struct decision *, int));
246 static int nodes_identical_1
247 PARAMS ((struct decision_test *, struct decision_test *));
248 static int nodes_identical
249 PARAMS ((struct decision *, struct decision *));
250 static void merge_accept_insn
251 PARAMS ((struct decision *, struct decision *));
252 static void merge_trees
253 PARAMS ((struct decision_head *, struct decision_head *));
255 static void factor_tests
256 PARAMS ((struct decision_head *));
257 static void simplify_tests
258 PARAMS ((struct decision_head *));
259 static int break_out_subroutines
260 PARAMS ((struct decision_head *, int));
261 static void find_afterward
262 PARAMS ((struct decision_head *, struct decision *));
264 static void change_state
265 PARAMS ((const char *, const char *, struct decision *, const char *));
266 static void print_code
267 PARAMS ((enum rtx_code));
268 static void write_afterward
269 PARAMS ((struct decision *, struct decision *, const char *));
270 static struct decision *write_switch
271 PARAMS ((struct decision *, int));
272 static void write_cond
273 PARAMS ((struct decision_test *, int, enum routine_type));
274 static void write_action
275 PARAMS ((struct decision *, struct decision_test *, int, int,
276 struct decision *, enum routine_type));
277 static int is_unconditional
278 PARAMS ((struct decision_test *, enum routine_type));
279 static int write_node
280 PARAMS ((struct decision *, int, enum routine_type));
281 static void write_tree_1
282 PARAMS ((struct decision_head *, int, enum routine_type));
283 static void write_tree
284 PARAMS ((struct decision_head *, const char *, enum routine_type, int));
285 static void write_subroutine
286 PARAMS ((struct decision_head *, enum routine_type));
287 static void write_subroutines
288 PARAMS ((struct decision_head *, enum routine_type));
289 static void write_header
290 PARAMS ((void));
292 static struct decision_head make_insn_sequence
293 PARAMS ((rtx, enum routine_type));
294 static void process_tree
295 PARAMS ((struct decision_head *, enum routine_type));
297 static void record_insn_name
298 PARAMS ((int, const char *));
300 static void debug_decision_0
301 PARAMS ((struct decision *, int, int));
302 static void debug_decision_1
303 PARAMS ((struct decision *, int));
304 static void debug_decision_2
305 PARAMS ((struct decision_test *));
306 extern void debug_decision
307 PARAMS ((struct decision *));
308 extern void debug_decision_list
309 PARAMS ((struct decision *));
311 /* Create a new node in sequence after LAST. */
313 static struct decision *
314 new_decision (position, last)
315 const char *position;
316 struct decision_head *last;
318 register struct decision *new
319 = (struct decision *) xmalloc (sizeof (struct decision));
321 memset (new, 0, sizeof (*new));
322 new->success = *last;
323 new->position = xstrdup (position);
324 new->number = next_number++;
326 last->first = last->last = new;
327 return new;
330 /* Create a new test and link it in at PLACE. */
332 static struct decision_test *
333 new_decision_test (type, pplace)
334 enum decision_type type;
335 struct decision_test ***pplace;
337 struct decision_test **place = *pplace;
338 struct decision_test *test;
340 test = (struct decision_test *) xmalloc (sizeof (*test));
341 test->next = *place;
342 test->type = type;
343 *place = test;
345 place = &test->next;
346 *pplace = place;
348 return test;
351 /* Search for and return operand N. */
353 static rtx
354 find_operand (pattern, n)
355 rtx pattern;
356 int n;
358 const char *fmt;
359 RTX_CODE code;
360 int i, j, len;
361 rtx r;
363 code = GET_CODE (pattern);
364 if ((code == MATCH_SCRATCH
365 || code == MATCH_INSN
366 || code == MATCH_OPERAND
367 || code == MATCH_OPERATOR
368 || code == MATCH_PARALLEL)
369 && XINT (pattern, 0) == n)
370 return pattern;
372 fmt = GET_RTX_FORMAT (code);
373 len = GET_RTX_LENGTH (code);
374 for (i = 0; i < len; i++)
376 switch (fmt[i])
378 case 'e': case 'u':
379 if ((r = find_operand (XEXP (pattern, i), n)) != NULL_RTX)
380 return r;
381 break;
383 case 'E':
384 for (j = 0; j < XVECLEN (pattern, i); j++)
385 if ((r = find_operand (XVECEXP (pattern, i, j), n)) != NULL_RTX)
386 return r;
387 break;
389 case 'i': case 'w': case '0': case 's':
390 break;
392 default:
393 abort ();
397 return NULL;
400 /* Check for various errors in patterns. SET is nonnull for a destination,
401 and is the complete set pattern. */
403 static void
404 validate_pattern (pattern, insn, set)
405 rtx pattern;
406 rtx insn;
407 rtx set;
409 const char *fmt;
410 RTX_CODE code;
411 size_t i, len;
412 int j;
414 code = GET_CODE (pattern);
415 switch (code)
417 case MATCH_SCRATCH:
418 return;
420 case MATCH_INSN:
421 case MATCH_OPERAND:
422 case MATCH_OPERATOR:
424 const char *pred_name = XSTR (pattern, 1);
425 int allows_non_lvalue = 1, allows_non_const = 1;
426 int special_mode_pred = 0;
427 const char *c_test;
429 if (GET_CODE (insn) == DEFINE_INSN)
430 c_test = XSTR (insn, 2);
431 else
432 c_test = XSTR (insn, 1);
434 if (pred_name[0] != 0)
436 for (i = 0; i < NUM_KNOWN_PREDS; i++)
437 if (! strcmp (preds[i].name, pred_name))
438 break;
440 if (i < NUM_KNOWN_PREDS)
442 int j;
444 allows_non_lvalue = allows_non_const = 0;
445 for (j = 0; preds[i].codes[j] != 0; j++)
447 RTX_CODE c = preds[i].codes[j];
448 if (c != LABEL_REF
449 && c != SYMBOL_REF
450 && c != CONST_INT
451 && c != CONST_DOUBLE
452 && c != CONST
453 && c != HIGH
454 && c != CONSTANT_P_RTX)
455 allows_non_const = 1;
457 if (c != REG
458 && c != SUBREG
459 && c != MEM
460 && c != CONCAT
461 && c != PARALLEL
462 && c != STRICT_LOW_PART)
463 allows_non_lvalue = 1;
466 else
468 #ifdef PREDICATE_CODES
469 /* If the port has a list of the predicates it uses but
470 omits one, warn. */
471 message_with_line (pattern_lineno,
472 "warning: `%s' not in PREDICATE_CODES",
473 pred_name);
474 #endif
477 for (i = 0; i < NUM_SPECIAL_MODE_PREDS; ++i)
478 if (strcmp (pred_name, special_mode_pred_table[i]) == 0)
480 special_mode_pred = 1;
481 break;
485 /* A MATCH_OPERAND that is a SET should have an output reload. */
486 if (set
487 && code == MATCH_OPERAND
488 && XSTR (pattern, 2)[0] != '\0'
489 && XSTR (pattern, 2)[0] != '='
490 && XSTR (pattern, 2)[0] != '+')
492 message_with_line (pattern_lineno,
493 "operand %d missing output reload",
494 XINT (pattern, 0));
495 error_count++;
498 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
499 while not likely to occur at runtime, results in less efficient
500 code from insn-recog.c. */
501 if (set
502 && pred_name[0] != '\0'
503 && allows_non_lvalue)
505 message_with_line (pattern_lineno,
506 "warning: destination operand %d allows non-lvalue",
507 XINT (pattern, 0));
510 /* A modeless MATCH_OPERAND can be handy when we can
511 check for multiple modes in the c_test. In most other cases,
512 it is a mistake. Only DEFINE_INSN is eligible, since SPLIT
513 and PEEP2 can FAIL within the output pattern. Exclude
514 address_operand, since its mode is related to the mode of
515 the memory not the operand. Exclude the SET_DEST of a call
516 instruction, as that is a common idiom. */
518 if (GET_MODE (pattern) == VOIDmode
519 && code == MATCH_OPERAND
520 && GET_CODE (insn) == DEFINE_INSN
521 && allows_non_const
522 && ! special_mode_pred
523 && pred_name[0] != '\0'
524 && strcmp (pred_name, "address_operand") != 0
525 && strstr (c_test, "operands") == NULL
526 && ! (set
527 && GET_CODE (set) == SET
528 && GET_CODE (SET_SRC (set)) == CALL))
530 message_with_line (pattern_lineno,
531 "warning: operand %d missing mode?",
532 XINT (pattern, 0));
534 return;
537 case SET:
539 enum machine_mode dmode, smode;
540 rtx dest, src;
542 dest = SET_DEST (pattern);
543 src = SET_SRC (pattern);
545 /* Find the referant for a DUP. */
547 if (GET_CODE (dest) == MATCH_DUP
548 || GET_CODE (dest) == MATCH_OP_DUP
549 || GET_CODE (dest) == MATCH_PAR_DUP)
550 dest = find_operand (insn, XINT (dest, 0));
552 if (GET_CODE (src) == MATCH_DUP
553 || GET_CODE (src) == MATCH_OP_DUP
554 || GET_CODE (src) == MATCH_PAR_DUP)
555 src = find_operand (insn, XINT (src, 0));
557 /* STRICT_LOW_PART is a wrapper. Its argument is the real
558 destination, and it's mode should match the source. */
559 if (GET_CODE (dest) == STRICT_LOW_PART)
560 dest = XEXP (dest, 0);
562 dmode = GET_MODE (dest);
563 smode = GET_MODE (src);
565 /* The mode of an ADDRESS_OPERAND is the mode of the memory
566 reference, not the mode of the address. */
567 if (GET_CODE (src) == MATCH_OPERAND
568 && ! strcmp (XSTR (src, 1), "address_operand"))
571 /* The operands of a SET must have the same mode unless one
572 is VOIDmode. */
573 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
575 message_with_line (pattern_lineno,
576 "mode mismatch in set: %smode vs %smode",
577 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
578 error_count++;
581 /* If only one of the operands is VOIDmode, and PC or CC0 is
582 not involved, it's probably a mistake. */
583 else if (dmode != smode
584 && GET_CODE (dest) != PC
585 && GET_CODE (dest) != CC0
586 && GET_CODE (src) != PC
587 && GET_CODE (src) != CC0
588 && GET_CODE (src) != CONST_INT)
590 const char *which;
591 which = (dmode == VOIDmode ? "destination" : "source");
592 message_with_line (pattern_lineno,
593 "warning: %s missing a mode?", which);
596 if (dest != SET_DEST (pattern))
597 validate_pattern (dest, insn, pattern);
598 validate_pattern (SET_DEST (pattern), insn, pattern);
599 validate_pattern (SET_SRC (pattern), insn, NULL_RTX);
600 return;
603 case CLOBBER:
604 validate_pattern (SET_DEST (pattern), insn, pattern);
605 return;
607 case LABEL_REF:
608 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
610 message_with_line (pattern_lineno,
611 "operand to label_ref %smode not VOIDmode",
612 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
613 error_count++;
615 break;
617 default:
618 break;
621 fmt = GET_RTX_FORMAT (code);
622 len = GET_RTX_LENGTH (code);
623 for (i = 0; i < len; i++)
625 switch (fmt[i])
627 case 'e': case 'u':
628 validate_pattern (XEXP (pattern, i), insn, NULL_RTX);
629 break;
631 case 'E':
632 for (j = 0; j < XVECLEN (pattern, i); j++)
633 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX);
634 break;
636 case 'i': case 'w': case '0': case 's':
637 break;
639 default:
640 abort ();
645 /* Create a chain of nodes to verify that an rtl expression matches
646 PATTERN.
648 LAST is a pointer to the listhead in the previous node in the chain (or
649 in the calling function, for the first node).
651 POSITION is the string representing the current position in the insn.
653 INSN_TYPE is the type of insn for which we are emitting code.
655 A pointer to the final node in the chain is returned. */
657 static struct decision *
658 add_to_sequence (pattern, last, position, insn_type, top)
659 rtx pattern;
660 struct decision_head *last;
661 const char *position;
662 enum routine_type insn_type;
663 int top;
665 RTX_CODE code;
666 struct decision *this, *sub;
667 struct decision_test *test;
668 struct decision_test **place;
669 char *subpos;
670 register size_t i;
671 register const char *fmt;
672 int depth = strlen (position);
673 int len;
674 enum machine_mode mode;
676 if (depth > max_depth)
677 max_depth = depth;
679 subpos = (char *) alloca (depth + 2);
680 strcpy (subpos, position);
681 subpos[depth + 1] = 0;
683 sub = this = new_decision (position, last);
684 place = &this->tests;
686 restart:
687 mode = GET_MODE (pattern);
688 code = GET_CODE (pattern);
690 switch (code)
692 case PARALLEL:
693 /* Toplevel peephole pattern. */
694 if (insn_type == PEEPHOLE2 && top)
696 /* We don't need the node we just created -- unlink it. */
697 last->first = last->last = NULL;
699 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
701 /* Which insn we're looking at is represented by A-Z. We don't
702 ever use 'A', however; it is always implied. */
704 subpos[depth] = (i > 0 ? 'A' + i : 0);
705 sub = add_to_sequence (XVECEXP (pattern, 0, i),
706 last, subpos, insn_type, 0);
707 last = &sub->success;
709 return sub;
712 /* Else nothing special. */
713 break;
715 case MATCH_OPERAND:
716 case MATCH_SCRATCH:
717 case MATCH_OPERATOR:
718 case MATCH_PARALLEL:
719 case MATCH_INSN:
721 const char *pred_name;
722 RTX_CODE was_code = code;
723 int allows_const_int = 1;
725 if (code == MATCH_SCRATCH)
727 pred_name = "scratch_operand";
728 code = UNKNOWN;
730 else
732 pred_name = XSTR (pattern, 1);
733 if (code == MATCH_PARALLEL)
734 code = PARALLEL;
735 else
736 code = UNKNOWN;
739 /* We know exactly what const_int_operand matches -- any CONST_INT. */
740 if (strcmp ("const_int_operand", pred_name) == 0)
742 code = CONST_INT;
743 mode = VOIDmode;
745 else if (pred_name[0] != 0)
747 test = new_decision_test (DT_pred, &place);
748 test->u.pred.name = pred_name;
749 test->u.pred.mode = mode;
751 /* See if we know about this predicate and save its number. If
752 we do, and it only accepts one code, note that fact. The
753 predicate `const_int_operand' only tests for a CONST_INT, so
754 if we do so we can avoid calling it at all.
756 Finally, if we know that the predicate does not allow
757 CONST_INT, we know that the only way the predicate can match
758 is if the modes match (here we use the kludge of relying on
759 the fact that "address_operand" accepts CONST_INT; otherwise,
760 it would have to be a special case), so we can test the mode
761 (but we need not). This fact should considerably simplify the
762 generated code. */
764 for (i = 0; i < NUM_KNOWN_PREDS; i++)
765 if (! strcmp (preds[i].name, pred_name))
766 break;
768 if (i < NUM_KNOWN_PREDS)
770 int j;
772 test->u.pred.index = i;
774 if (preds[i].codes[1] == 0 && code == UNKNOWN)
775 code = preds[i].codes[0];
777 allows_const_int = 0;
778 for (j = 0; preds[i].codes[j] != 0; j++)
779 if (preds[i].codes[j] == CONST_INT)
781 allows_const_int = 1;
782 break;
785 else
786 test->u.pred.index = -1;
789 /* Can't enforce a mode if we allow const_int. */
790 if (allows_const_int)
791 mode = VOIDmode;
793 /* Accept the operand, ie. record it in `operands'. */
794 test = new_decision_test (DT_accept_op, &place);
795 test->u.opno = XINT (pattern, 0);
797 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
799 char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
800 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
802 subpos[depth] = i + base;
803 sub = add_to_sequence (XVECEXP (pattern, 2, i),
804 &sub->success, subpos, insn_type, 0);
807 goto fini;
810 case MATCH_OP_DUP:
811 code = UNKNOWN;
813 test = new_decision_test (DT_dup, &place);
814 test->u.dup = XINT (pattern, 0);
816 test = new_decision_test (DT_accept_op, &place);
817 test->u.opno = XINT (pattern, 0);
819 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
821 subpos[depth] = i + '0';
822 sub = add_to_sequence (XVECEXP (pattern, 1, i),
823 &sub->success, subpos, insn_type, 0);
825 goto fini;
827 case MATCH_DUP:
828 case MATCH_PAR_DUP:
829 code = UNKNOWN;
831 test = new_decision_test (DT_dup, &place);
832 test->u.dup = XINT (pattern, 0);
833 goto fini;
835 case ADDRESS:
836 pattern = XEXP (pattern, 0);
837 goto restart;
839 default:
840 break;
843 fmt = GET_RTX_FORMAT (code);
844 len = GET_RTX_LENGTH (code);
846 /* Do tests against the current node first. */
847 for (i = 0; i < (size_t) len; i++)
849 if (fmt[i] == 'i')
851 if (i == 0)
853 test = new_decision_test (DT_elt_zero_int, &place);
854 test->u.intval = XINT (pattern, i);
856 else if (i == 1)
858 test = new_decision_test (DT_elt_one_int, &place);
859 test->u.intval = XINT (pattern, i);
861 else
862 abort ();
864 else if (fmt[i] == 'w')
866 if (i != 0)
867 abort ();
869 test = new_decision_test (DT_elt_zero_wide, &place);
870 test->u.intval = XWINT (pattern, i);
872 else if (fmt[i] == 'E')
874 if (i != 0)
875 abort ();
877 test = new_decision_test (DT_veclen, &place);
878 test->u.veclen = XVECLEN (pattern, i);
882 /* Now test our sub-patterns. */
883 for (i = 0; i < (size_t) len; i++)
885 switch (fmt[i])
887 case 'e': case 'u':
888 subpos[depth] = '0' + i;
889 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
890 subpos, insn_type, 0);
891 break;
893 case 'E':
895 register int j;
896 for (j = 0; j < XVECLEN (pattern, i); j++)
898 subpos[depth] = 'a' + j;
899 sub = add_to_sequence (XVECEXP (pattern, i, j),
900 &sub->success, subpos, insn_type, 0);
902 break;
905 case 'i': case 'w':
906 /* Handled above. */
907 break;
908 case '0':
909 break;
911 default:
912 abort ();
916 fini:
917 /* Insert nodes testing mode and code, if they're still relevant,
918 before any of the nodes we may have added above. */
919 if (code != UNKNOWN)
921 place = &this->tests;
922 test = new_decision_test (DT_code, &place);
923 test->u.code = code;
926 if (mode != VOIDmode)
928 place = &this->tests;
929 test = new_decision_test (DT_mode, &place);
930 test->u.mode = mode;
933 /* If we didn't insert any tests or accept nodes, hork. */
934 if (this->tests == NULL)
935 abort ();
937 return sub;
940 /* A subroutine of maybe_both_true; examines only one test.
941 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
943 static int
944 maybe_both_true_2 (d1, d2)
945 struct decision_test *d1, *d2;
947 if (d1->type == d2->type)
949 switch (d1->type)
951 case DT_mode:
952 return d1->u.mode == d2->u.mode;
954 case DT_code:
955 return d1->u.code == d2->u.code;
957 case DT_veclen:
958 return d1->u.veclen == d2->u.veclen;
960 case DT_elt_zero_int:
961 case DT_elt_one_int:
962 case DT_elt_zero_wide:
963 return d1->u.intval == d2->u.intval;
965 default:
966 break;
970 /* If either has a predicate that we know something about, set
971 things up so that D1 is the one that always has a known
972 predicate. Then see if they have any codes in common. */
974 if (d1->type == DT_pred || d2->type == DT_pred)
976 if (d2->type == DT_pred)
978 struct decision_test *tmp;
979 tmp = d1, d1 = d2, d2 = tmp;
982 /* If D2 tests a mode, see if it matches D1. */
983 if (d1->u.pred.mode != VOIDmode)
985 if (d2->type == DT_mode)
987 if (d1->u.pred.mode != d2->u.mode
988 /* The mode of an address_operand predicate is the
989 mode of the memory, not the operand. It can only
990 be used for testing the predicate, so we must
991 ignore it here. */
992 && strcmp (d1->u.pred.name, "address_operand") != 0)
993 return 0;
995 /* Don't check two predicate modes here, because if both predicates
996 accept CONST_INT, then both can still be true even if the modes
997 are different. If they don't accept CONST_INT, there will be a
998 separate DT_mode that will make maybe_both_true_1 return 0. */
1001 if (d1->u.pred.index >= 0)
1003 /* If D2 tests a code, see if it is in the list of valid
1004 codes for D1's predicate. */
1005 if (d2->type == DT_code)
1007 const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
1008 while (*c != 0)
1010 if (*c == d2->u.code)
1011 break;
1012 ++c;
1014 if (*c == 0)
1015 return 0;
1018 /* Otherwise see if the predicates have any codes in common. */
1019 else if (d2->type == DT_pred && d2->u.pred.index >= 0)
1021 const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
1022 int common = 0;
1024 while (*c1 != 0 && !common)
1026 const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
1027 while (*c2 != 0 && !common)
1029 common = (*c1 == *c2);
1030 ++c2;
1032 ++c1;
1035 if (!common)
1036 return 0;
1041 return -1;
1044 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1045 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1047 static int
1048 maybe_both_true_1 (d1, d2)
1049 struct decision_test *d1, *d2;
1051 struct decision_test *t1, *t2;
1053 /* A match_operand with no predicate can match anything. Recognize
1054 this by the existance of a lone DT_accept_op test. */
1055 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1056 return 1;
1058 /* Eliminate pairs of tests while they can exactly match. */
1059 while (d1 && d2 && d1->type == d2->type)
1061 if (maybe_both_true_2 (d1, d2) == 0)
1062 return 0;
1063 d1 = d1->next, d2 = d2->next;
1066 /* After that, consider all pairs. */
1067 for (t1 = d1; t1 ; t1 = t1->next)
1068 for (t2 = d2; t2 ; t2 = t2->next)
1069 if (maybe_both_true_2 (t1, t2) == 0)
1070 return 0;
1072 return -1;
1075 /* Return 0 if we can prove that there is no RTL that can match both
1076 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
1077 can match both or just that we couldn't prove there wasn't such an RTL).
1079 TOPLEVEL is non-zero if we are to only look at the top level and not
1080 recursively descend. */
1082 static int
1083 maybe_both_true (d1, d2, toplevel)
1084 struct decision *d1, *d2;
1085 int toplevel;
1087 struct decision *p1, *p2;
1088 int cmp;
1090 /* Don't compare strings on the different positions in insn. Doing so
1091 is incorrect and results in false matches from constructs like
1093 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1094 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1096 [(set (match_operand:HI "register_operand" "r")
1097 (match_operand:HI "register_operand" "r"))]
1099 If we are presented with such, we are recursing through the remainder
1100 of a node's success nodes (from the loop at the end of this function).
1101 Skip forward until we come to a position that matches.
1103 Due to the way position strings are constructed, we know that iterating
1104 forward from the lexically lower position (e.g. "00") will run into
1105 the lexically higher position (e.g. "1") and not the other way around.
1106 This saves a bit of effort. */
1108 cmp = strcmp (d1->position, d2->position);
1109 if (cmp != 0)
1111 if (toplevel)
1112 abort();
1114 /* If the d2->position was lexically lower, swap. */
1115 if (cmp > 0)
1116 p1 = d1, d1 = d2, d2 = p1;
1118 if (d1->success.first == 0)
1119 return 0;
1120 for (p1 = d1->success.first; p1; p1 = p1->next)
1121 if (maybe_both_true (p1, d2, 0))
1122 return 1;
1124 return 0;
1127 /* Test the current level. */
1128 cmp = maybe_both_true_1 (d1->tests, d2->tests);
1129 if (cmp >= 0)
1130 return cmp;
1132 /* We can't prove that D1 and D2 cannot both be true. If we are only
1133 to check the top level, return 1. Otherwise, see if we can prove
1134 that all choices in both successors are mutually exclusive. If
1135 either does not have any successors, we can't prove they can't both
1136 be true. */
1138 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1139 return 1;
1141 for (p1 = d1->success.first; p1; p1 = p1->next)
1142 for (p2 = d2->success.first; p2; p2 = p2->next)
1143 if (maybe_both_true (p1, p2, 0))
1144 return 1;
1146 return 0;
1149 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1151 static int
1152 nodes_identical_1 (d1, d2)
1153 struct decision_test *d1, *d2;
1155 switch (d1->type)
1157 case DT_mode:
1158 return d1->u.mode == d2->u.mode;
1160 case DT_code:
1161 return d1->u.code == d2->u.code;
1163 case DT_pred:
1164 return (d1->u.pred.mode == d2->u.pred.mode
1165 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1167 case DT_c_test:
1168 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1170 case DT_veclen:
1171 return d1->u.veclen == d2->u.veclen;
1173 case DT_dup:
1174 return d1->u.dup == d2->u.dup;
1176 case DT_elt_zero_int:
1177 case DT_elt_one_int:
1178 case DT_elt_zero_wide:
1179 return d1->u.intval == d2->u.intval;
1181 case DT_accept_op:
1182 return d1->u.opno == d2->u.opno;
1184 case DT_accept_insn:
1185 /* Differences will be handled in merge_accept_insn. */
1186 return 1;
1188 default:
1189 abort ();
1193 /* True iff the two nodes are identical (on one level only). Due
1194 to the way these lists are constructed, we shouldn't have to
1195 consider different orderings on the tests. */
1197 static int
1198 nodes_identical (d1, d2)
1199 struct decision *d1, *d2;
1201 struct decision_test *t1, *t2;
1203 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1205 if (t1->type != t2->type)
1206 return 0;
1207 if (! nodes_identical_1 (t1, t2))
1208 return 0;
1211 /* For success, they should now both be null. */
1212 if (t1 != t2)
1213 return 0;
1215 /* Check that their subnodes are at the same position, as any one set
1216 of sibling decisions must be at the same position. */
1217 if (d1->success.first
1218 && d2->success.first
1219 && strcmp (d1->success.first->position, d2->success.first->position))
1220 return 0;
1222 return 1;
1225 /* A subroutine of merge_trees; given two nodes that have been declared
1226 identical, cope with two insn accept states. If they differ in the
1227 number of clobbers, then the conflict was created by make_insn_sequence
1228 and we can drop the with-clobbers version on the floor. If both
1229 nodes have no additional clobbers, we have found an ambiguity in the
1230 source machine description. */
1232 static void
1233 merge_accept_insn (oldd, addd)
1234 struct decision *oldd, *addd;
1236 struct decision_test *old, *add;
1238 for (old = oldd->tests; old; old = old->next)
1239 if (old->type == DT_accept_insn)
1240 break;
1241 if (old == NULL)
1242 return;
1244 for (add = addd->tests; add; add = add->next)
1245 if (add->type == DT_accept_insn)
1246 break;
1247 if (add == NULL)
1248 return;
1250 /* If one node is for a normal insn and the second is for the base
1251 insn with clobbers stripped off, the second node should be ignored. */
1253 if (old->u.insn.num_clobbers_to_add == 0
1254 && add->u.insn.num_clobbers_to_add > 0)
1256 /* Nothing to do here. */
1258 else if (old->u.insn.num_clobbers_to_add > 0
1259 && add->u.insn.num_clobbers_to_add == 0)
1261 /* In this case, replace OLD with ADD. */
1262 old->u.insn = add->u.insn;
1264 else
1266 message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1267 get_insn_name (add->u.insn.code_number),
1268 get_insn_name (old->u.insn.code_number));
1269 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1270 get_insn_name (old->u.insn.code_number));
1271 error_count++;
1275 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1277 static void
1278 merge_trees (oldh, addh)
1279 struct decision_head *oldh, *addh;
1281 struct decision *next, *add;
1283 if (addh->first == 0)
1284 return;
1285 if (oldh->first == 0)
1287 *oldh = *addh;
1288 return;
1291 /* Trying to merge bits at different positions isn't possible. */
1292 if (strcmp (oldh->first->position, addh->first->position))
1293 abort ();
1295 for (add = addh->first; add ; add = next)
1297 struct decision *old, *insert_before = NULL;
1299 next = add->next;
1301 /* The semantics of pattern matching state that the tests are
1302 done in the order given in the MD file so that if an insn
1303 matches two patterns, the first one will be used. However,
1304 in practice, most, if not all, patterns are unambiguous so
1305 that their order is independent. In that case, we can merge
1306 identical tests and group all similar modes and codes together.
1308 Scan starting from the end of OLDH until we reach a point
1309 where we reach the head of the list or where we pass a
1310 pattern that could also be true if NEW is true. If we find
1311 an identical pattern, we can merge them. Also, record the
1312 last node that tests the same code and mode and the last one
1313 that tests just the same mode.
1315 If we have no match, place NEW after the closest match we found. */
1317 for (old = oldh->last; old; old = old->prev)
1319 if (nodes_identical (old, add))
1321 merge_accept_insn (old, add);
1322 merge_trees (&old->success, &add->success);
1323 goto merged_nodes;
1326 if (maybe_both_true (old, add, 0))
1327 break;
1329 /* Insert the nodes in DT test type order, which is roughly
1330 how expensive/important the test is. Given that the tests
1331 are also ordered within the list, examining the first is
1332 sufficient. */
1333 if (add->tests->type < old->tests->type)
1334 insert_before = old;
1337 if (insert_before == NULL)
1339 add->next = NULL;
1340 add->prev = oldh->last;
1341 oldh->last->next = add;
1342 oldh->last = add;
1344 else
1346 if ((add->prev = insert_before->prev) != NULL)
1347 add->prev->next = add;
1348 else
1349 oldh->first = add;
1350 add->next = insert_before;
1351 insert_before->prev = add;
1354 merged_nodes:;
1358 /* Walk the tree looking for sub-nodes that perform common tests.
1359 Factor out the common test into a new node. This enables us
1360 (depending on the test type) to emit switch statements later. */
1362 static void
1363 factor_tests (head)
1364 struct decision_head *head;
1366 struct decision *first, *next;
1368 for (first = head->first; first && first->next; first = next)
1370 enum decision_type type;
1371 struct decision *new, *old_last;
1373 type = first->tests->type;
1374 next = first->next;
1376 /* Want at least two compatible sequential nodes. */
1377 if (next->tests->type != type)
1378 continue;
1380 /* Don't want all node types, just those we can turn into
1381 switch statements. */
1382 if (type != DT_mode
1383 && type != DT_code
1384 && type != DT_veclen
1385 && type != DT_elt_zero_int
1386 && type != DT_elt_one_int
1387 && type != DT_elt_zero_wide)
1388 continue;
1390 /* If we'd been performing more than one test, create a new node
1391 below our first test. */
1392 if (first->tests->next != NULL)
1394 new = new_decision (first->position, &first->success);
1395 new->tests = first->tests->next;
1396 first->tests->next = NULL;
1399 /* Crop the node tree off after our first test. */
1400 first->next = NULL;
1401 old_last = head->last;
1402 head->last = first;
1404 /* For each compatible test, adjust to perform only one test in
1405 the top level node, then merge the node back into the tree. */
1408 struct decision_head h;
1410 if (next->tests->next != NULL)
1412 new = new_decision (next->position, &next->success);
1413 new->tests = next->tests->next;
1414 next->tests->next = NULL;
1416 new = next;
1417 next = next->next;
1418 new->next = NULL;
1419 h.first = h.last = new;
1421 merge_trees (head, &h);
1423 while (next && next->tests->type == type);
1425 /* After we run out of compatible tests, graft the remaining nodes
1426 back onto the tree. */
1427 if (next)
1429 next->prev = head->last;
1430 head->last->next = next;
1431 head->last = old_last;
1435 /* Recurse. */
1436 for (first = head->first; first; first = first->next)
1437 factor_tests (&first->success);
1440 /* After factoring, try to simplify the tests on any one node.
1441 Tests that are useful for switch statements are recognizable
1442 by having only a single test on a node -- we'll be manipulating
1443 nodes with multiple tests:
1445 If we have mode tests or code tests that are redundant with
1446 predicates, remove them. */
1448 static void
1449 simplify_tests (head)
1450 struct decision_head *head;
1452 struct decision *tree;
1454 for (tree = head->first; tree; tree = tree->next)
1456 struct decision_test *a, *b;
1458 a = tree->tests;
1459 b = a->next;
1460 if (b == NULL)
1461 continue;
1463 /* Find a predicate node. */
1464 while (b && b->type != DT_pred)
1465 b = b->next;
1466 if (b)
1468 /* Due to how these tests are constructed, we don't even need
1469 to check that the mode and code are compatible -- they were
1470 generated from the predicate in the first place. */
1471 while (a->type == DT_mode || a->type == DT_code)
1472 a = a->next;
1473 tree->tests = a;
1477 /* Recurse. */
1478 for (tree = head->first; tree; tree = tree->next)
1479 simplify_tests (&tree->success);
1482 /* Count the number of subnodes of HEAD. If the number is high enough,
1483 make the first node in HEAD start a separate subroutine in the C code
1484 that is generated. */
1486 static int
1487 break_out_subroutines (head, initial)
1488 struct decision_head *head;
1489 int initial;
1491 int size = 0;
1492 struct decision *sub;
1494 for (sub = head->first; sub; sub = sub->next)
1495 size += 1 + break_out_subroutines (&sub->success, 0);
1497 if (size > SUBROUTINE_THRESHOLD && ! initial)
1499 head->first->subroutine_number = ++next_subroutine_number;
1500 size = 1;
1502 return size;
1505 /* For each node p, find the next alternative that might be true
1506 when p is true. */
1508 static void
1509 find_afterward (head, real_afterward)
1510 struct decision_head *head;
1511 struct decision *real_afterward;
1513 struct decision *p, *q, *afterward;
1515 /* We can't propogate alternatives across subroutine boundaries.
1516 This is not incorrect, merely a minor optimization loss. */
1518 p = head->first;
1519 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1521 for ( ; p ; p = p->next)
1523 /* Find the next node that might be true if this one fails. */
1524 for (q = p->next; q ; q = q->next)
1525 if (maybe_both_true (p, q, 1))
1526 break;
1528 /* If we reached the end of the list without finding one,
1529 use the incoming afterward position. */
1530 if (!q)
1531 q = afterward;
1532 p->afterward = q;
1533 if (q)
1534 q->need_label = 1;
1537 /* Recurse. */
1538 for (p = head->first; p ; p = p->next)
1539 if (p->success.first)
1540 find_afterward (&p->success, p->afterward);
1542 /* When we are generating a subroutine, record the real afterward
1543 position in the first node where write_tree can find it, and we
1544 can do the right thing at the subroutine call site. */
1545 p = head->first;
1546 if (p->subroutine_number > 0)
1547 p->afterward = real_afterward;
1550 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1551 actions are necessary to move to NEWPOS. If we fail to move to the
1552 new state, branch to node AFTERWARD if non-zero, otherwise return.
1554 Failure to move to the new state can only occur if we are trying to
1555 match multiple insns and we try to step past the end of the stream. */
1557 static void
1558 change_state (oldpos, newpos, afterward, indent)
1559 const char *oldpos;
1560 const char *newpos;
1561 struct decision *afterward;
1562 const char *indent;
1564 int odepth = strlen (oldpos);
1565 int ndepth = strlen (newpos);
1566 int depth;
1567 int old_has_insn, new_has_insn;
1569 /* Pop up as many levels as necessary. */
1570 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1571 continue;
1573 /* Hunt for the last [A-Z] in both strings. */
1574 for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1575 if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1576 break;
1577 for (new_has_insn = ndepth - 1; new_has_insn >= 0; --new_has_insn)
1578 if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1579 break;
1581 /* Go down to desired level. */
1582 while (depth < ndepth)
1584 /* It's a different insn from the first one. */
1585 if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1587 /* We can only fail if we're moving down the tree. */
1588 if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1590 printf ("%stem = peep2_next_insn (%d);\n",
1591 indent, newpos[depth] - 'A');
1593 else
1595 printf ("%stem = peep2_next_insn (%d);\n",
1596 indent, newpos[depth] - 'A');
1597 printf ("%sif (tem == NULL_RTX)\n", indent);
1598 if (afterward)
1599 printf ("%s goto L%d;\n", indent, afterward->number);
1600 else
1601 printf ("%s goto ret0;\n", indent);
1603 printf ("%sx%d = PATTERN (tem);\n", indent, depth + 1);
1605 else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1606 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1607 indent, depth + 1, depth, newpos[depth] - 'a');
1608 else
1609 printf ("%sx%d = XEXP (x%d, %c);\n",
1610 indent, depth + 1, depth, newpos[depth]);
1611 ++depth;
1615 /* Print the enumerator constant for CODE -- the upcase version of
1616 the name. */
1618 static void
1619 print_code (code)
1620 enum rtx_code code;
1622 register const char *p;
1623 for (p = GET_RTX_NAME (code); *p; p++)
1624 putchar (TOUPPER (*p));
1627 /* Emit code to cross an afterward link -- change state and branch. */
1629 static void
1630 write_afterward (start, afterward, indent)
1631 struct decision *start;
1632 struct decision *afterward;
1633 const char *indent;
1635 if (!afterward || start->subroutine_number > 0)
1636 printf("%sgoto ret0;\n", indent);
1637 else
1639 change_state (start->position, afterward->position, NULL, indent);
1640 printf ("%sgoto L%d;\n", indent, afterward->number);
1644 /* Emit a switch statement, if possible, for an initial sequence of
1645 nodes at START. Return the first node yet untested. */
1647 static struct decision *
1648 write_switch (start, depth)
1649 struct decision *start;
1650 int depth;
1652 struct decision *p = start;
1653 enum decision_type type = p->tests->type;
1655 /* If we have two or more nodes in sequence that test the same one
1656 thing, we may be able to use a switch statement. */
1658 if (!p->next
1659 || p->tests->next
1660 || p->next->tests->type != type
1661 || p->next->tests->next)
1662 return p;
1664 /* DT_code is special in that we can do interesting things with
1665 known predicates at the same time. */
1666 if (type == DT_code)
1668 char codemap[NUM_RTX_CODE];
1669 struct decision *ret;
1670 RTX_CODE code;
1672 memset (codemap, 0, sizeof(codemap));
1674 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1675 code = p->tests->u.code;
1678 printf (" case ");
1679 print_code (code);
1680 printf (":\n goto L%d;\n", p->success.first->number);
1681 p->success.first->need_label = 1;
1683 codemap[code] = 1;
1684 p = p->next;
1686 while (p
1687 && ! p->tests->next
1688 && p->tests->type == DT_code
1689 && ! codemap[code = p->tests->u.code]);
1691 /* If P is testing a predicate that we know about and we haven't
1692 seen any of the codes that are valid for the predicate, we can
1693 write a series of "case" statement, one for each possible code.
1694 Since we are already in a switch, these redundant tests are very
1695 cheap and will reduce the number of predicates called. */
1697 /* Note that while we write out cases for these predicates here,
1698 we don't actually write the test here, as it gets kinda messy.
1699 It is trivial to leave this to later by telling our caller that
1700 we only processed the CODE tests. */
1701 ret = p;
1703 while (p && p->tests->type == DT_pred
1704 && p->tests->u.pred.index >= 0)
1706 const RTX_CODE *c;
1708 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1709 if (codemap[(int) *c] != 0)
1710 goto pred_done;
1712 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1714 printf (" case ");
1715 print_code (*c);
1716 printf (":\n");
1717 codemap[(int) *c] = 1;
1720 printf (" goto L%d;\n", p->number);
1721 p->need_label = 1;
1722 p = p->next;
1725 pred_done:
1726 /* Make the default case skip the predicates we managed to match. */
1728 printf (" default:\n");
1729 if (p != ret)
1731 if (p)
1733 printf (" goto L%d;\n", p->number);
1734 p->need_label = 1;
1736 else
1737 write_afterward (start, start->afterward, " ");
1739 else
1740 printf (" break;\n");
1741 printf (" }\n");
1743 return ret;
1745 else if (type == DT_mode
1746 || type == DT_veclen
1747 || type == DT_elt_zero_int
1748 || type == DT_elt_one_int
1749 || type == DT_elt_zero_wide)
1751 printf (" switch (");
1752 switch (type)
1754 case DT_mode:
1755 printf ("GET_MODE (x%d)", depth);
1756 break;
1757 case DT_veclen:
1758 printf ("XVECLEN (x%d, 0)", depth);
1759 break;
1760 case DT_elt_zero_int:
1761 printf ("XINT (x%d, 0)", depth);
1762 break;
1763 case DT_elt_one_int:
1764 printf ("XINT (x%d, 1)", depth);
1765 break;
1766 case DT_elt_zero_wide:
1767 /* Convert result of XWINT to int for portability since some C
1768 compilers won't do it and some will. */
1769 printf ("(int) XWINT (x%d, 0)", depth);
1770 break;
1771 default:
1772 abort ();
1774 printf (")\n {\n");
1778 printf (" case ");
1779 switch (type)
1781 case DT_mode:
1782 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1783 break;
1784 case DT_veclen:
1785 printf ("%d", p->tests->u.veclen);
1786 break;
1787 case DT_elt_zero_int:
1788 case DT_elt_one_int:
1789 case DT_elt_zero_wide:
1790 printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1791 break;
1792 default:
1793 abort ();
1795 printf (":\n goto L%d;\n", p->success.first->number);
1796 p->success.first->need_label = 1;
1798 p = p->next;
1800 while (p && p->tests->type == type && !p->tests->next);
1802 printf (" default:\n break;\n }\n");
1804 return p;
1806 else
1808 /* None of the other tests are ameanable. */
1809 return p;
1813 /* Emit code for one test. */
1815 static void
1816 write_cond (p, depth, subroutine_type)
1817 struct decision_test *p;
1818 int depth;
1819 enum routine_type subroutine_type;
1821 switch (p->type)
1823 case DT_mode:
1824 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1825 break;
1827 case DT_code:
1828 printf ("GET_CODE (x%d) == ", depth);
1829 print_code (p->u.code);
1830 break;
1832 case DT_veclen:
1833 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1834 break;
1836 case DT_elt_zero_int:
1837 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1838 break;
1840 case DT_elt_one_int:
1841 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1842 break;
1844 case DT_elt_zero_wide:
1845 printf ("XWINT (x%d, 0) == ", depth);
1846 printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1847 break;
1849 case DT_dup:
1850 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1851 break;
1853 case DT_pred:
1854 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1855 GET_MODE_NAME (p->u.pred.mode));
1856 break;
1858 case DT_c_test:
1859 printf ("(%s)", p->u.c_test);
1860 break;
1862 case DT_accept_insn:
1863 switch (subroutine_type)
1865 case RECOG:
1866 if (p->u.insn.num_clobbers_to_add == 0)
1867 abort ();
1868 printf ("pnum_clobbers != NULL");
1869 break;
1871 default:
1872 abort ();
1874 break;
1876 default:
1877 abort ();
1881 /* Emit code for one action. The previous tests have succeeded;
1882 TEST is the last of the chain. In the normal case we simply
1883 perform a state change. For the `accept' tests we must do more work. */
1885 static void
1886 write_action (p, test, depth, uncond, success, subroutine_type)
1887 struct decision *p;
1888 struct decision_test *test;
1889 int depth, uncond;
1890 struct decision *success;
1891 enum routine_type subroutine_type;
1893 const char *indent;
1894 int want_close = 0;
1896 if (uncond)
1897 indent = " ";
1898 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1900 fputs (" {\n", stdout);
1901 indent = " ";
1902 want_close = 1;
1904 else
1905 indent = " ";
1907 if (test->type == DT_accept_op)
1909 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1911 /* Only allow DT_accept_insn to follow. */
1912 if (test->next)
1914 test = test->next;
1915 if (test->type != DT_accept_insn)
1916 abort ();
1920 /* Sanity check that we're now at the end of the list of tests. */
1921 if (test->next)
1922 abort ();
1924 if (test->type == DT_accept_insn)
1926 switch (subroutine_type)
1928 case RECOG:
1929 if (test->u.insn.num_clobbers_to_add != 0)
1930 printf ("%s*pnum_clobbers = %d;\n",
1931 indent, test->u.insn.num_clobbers_to_add);
1932 printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1933 break;
1935 case SPLIT:
1936 printf ("%sreturn gen_split_%d (operands);\n",
1937 indent, test->u.insn.code_number);
1938 break;
1940 case PEEPHOLE2:
1942 int match_len = 0, i;
1944 for (i = strlen (p->position) - 1; i >= 0; --i)
1945 if (p->position[i] >= 'A' && p->position[i] <= 'Z')
1947 match_len = p->position[i] - 'A';
1948 break;
1950 printf ("%s*_pmatch_len = %d;\n", indent, match_len);
1951 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1952 indent, test->u.insn.code_number);
1953 printf ("%sif (tem != 0)\n%s return tem;\n", indent, indent);
1955 break;
1957 default:
1958 abort ();
1961 else
1963 printf("%sgoto L%d;\n", indent, success->number);
1964 success->need_label = 1;
1967 if (want_close)
1968 fputs (" }\n", stdout);
1971 /* Return 1 if the test is always true and has no fallthru path. Return -1
1972 if the test does have a fallthru path, but requires that the condition be
1973 terminated. Otherwise return 0 for a normal test. */
1974 /* ??? is_unconditional is a stupid name for a tri-state function. */
1976 static int
1977 is_unconditional (t, subroutine_type)
1978 struct decision_test *t;
1979 enum routine_type subroutine_type;
1981 if (t->type == DT_accept_op)
1982 return 1;
1984 if (t->type == DT_accept_insn)
1986 switch (subroutine_type)
1988 case RECOG:
1989 return (t->u.insn.num_clobbers_to_add == 0);
1990 case SPLIT:
1991 return 1;
1992 case PEEPHOLE2:
1993 return -1;
1994 default:
1995 abort ();
1999 return 0;
2002 /* Emit code for one node -- the conditional and the accompanying action.
2003 Return true if there is no fallthru path. */
2005 static int
2006 write_node (p, depth, subroutine_type)
2007 struct decision *p;
2008 int depth;
2009 enum routine_type subroutine_type;
2011 struct decision_test *test, *last_test;
2012 int uncond;
2014 last_test = test = p->tests;
2015 uncond = is_unconditional (test, subroutine_type);
2016 if (uncond == 0)
2018 printf (" if (");
2019 write_cond (test, depth, subroutine_type);
2021 while ((test = test->next) != NULL)
2023 int uncond2;
2025 last_test = test;
2026 uncond2 = is_unconditional (test, subroutine_type);
2027 if (uncond2 != 0)
2028 break;
2030 printf ("\n && ");
2031 write_cond (test, depth, subroutine_type);
2034 printf (")\n");
2037 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2039 return uncond > 0;
2042 /* Emit code for all of the sibling nodes of HEAD. */
2044 static void
2045 write_tree_1 (head, depth, subroutine_type)
2046 struct decision_head *head;
2047 int depth;
2048 enum routine_type subroutine_type;
2050 struct decision *p, *next;
2051 int uncond = 0;
2053 for (p = head->first; p ; p = next)
2055 /* The label for the first element was printed in write_tree. */
2056 if (p != head->first && p->need_label)
2057 OUTPUT_LABEL (" ", p->number);
2059 /* Attempt to write a switch statement for a whole sequence. */
2060 next = write_switch (p, depth);
2061 if (p != next)
2062 uncond = 0;
2063 else
2065 /* Failed -- fall back and write one node. */
2066 uncond = write_node (p, depth, subroutine_type);
2067 next = p->next;
2071 /* Finished with this chain. Close a fallthru path by branching
2072 to the afterward node. */
2073 if (! uncond)
2074 write_afterward (head->last, head->last->afterward, " ");
2077 /* Write out the decision tree starting at HEAD. PREVPOS is the
2078 position at the node that branched to this node. */
2080 static void
2081 write_tree (head, prevpos, type, initial)
2082 struct decision_head *head;
2083 const char *prevpos;
2084 enum routine_type type;
2085 int initial;
2087 register struct decision *p = head->first;
2089 putchar ('\n');
2090 if (p->need_label)
2091 OUTPUT_LABEL (" ", p->number);
2093 if (! initial && p->subroutine_number > 0)
2095 static const char * const name_prefix[] = {
2096 "recog", "split", "peephole2"
2099 static const char * const call_suffix[] = {
2100 ", pnum_clobbers", "", ", _pmatch_len"
2103 /* This node has been broken out into a separate subroutine.
2104 Call it, test the result, and branch accordingly. */
2106 if (p->afterward)
2108 printf (" tem = %s_%d (x0, insn%s);\n",
2109 name_prefix[type], p->subroutine_number, call_suffix[type]);
2110 if (IS_SPLIT (type))
2111 printf (" if (tem != 0)\n return tem;\n");
2112 else
2113 printf (" if (tem >= 0)\n return tem;\n");
2115 change_state (p->position, p->afterward->position, NULL, " ");
2116 printf (" goto L%d;\n", p->afterward->number);
2118 else
2120 printf (" return %s_%d (x0, insn%s);\n",
2121 name_prefix[type], p->subroutine_number, call_suffix[type]);
2124 else
2126 int depth = strlen (p->position);
2128 change_state (prevpos, p->position, head->last->afterward, " ");
2129 write_tree_1 (head, depth, type);
2131 for (p = head->first; p; p = p->next)
2132 if (p->success.first)
2133 write_tree (&p->success, p->position, type, 0);
2137 /* Write out a subroutine of type TYPE to do comparisons starting at
2138 node TREE. */
2140 static void
2141 write_subroutine (head, type)
2142 struct decision_head *head;
2143 enum routine_type type;
2145 int subfunction = head->first ? head->first->subroutine_number : 0;
2146 const char *s_or_e;
2147 char extension[32];
2148 int i;
2150 s_or_e = subfunction ? "static " : "";
2152 if (subfunction)
2153 sprintf (extension, "_%d", subfunction);
2154 else if (type == RECOG)
2155 extension[0] = '\0';
2156 else
2157 strcpy (extension, "_insns");
2159 switch (type)
2161 case RECOG:
2162 printf ("%sint recog%s PARAMS ((rtx, rtx, int *));\n", s_or_e, extension);
2163 printf ("%sint\n\
2164 recog%s (x0, insn, pnum_clobbers)\n\
2165 register rtx x0;\n\
2166 rtx insn ATTRIBUTE_UNUSED;\n\
2167 int *pnum_clobbers ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2168 break;
2169 case SPLIT:
2170 printf ("%srtx split%s PARAMS ((rtx, rtx));\n", s_or_e, extension);
2171 printf ("%srtx\n\
2172 split%s (x0, insn)\n\
2173 register rtx x0;\n\
2174 rtx insn ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2175 break;
2176 case PEEPHOLE2:
2177 printf ("%srtx peephole2%s PARAMS ((rtx, rtx, int *));\n",
2178 s_or_e, extension);
2179 printf ("%srtx\n\
2180 peephole2%s (x0, insn, _pmatch_len)\n\
2181 register rtx x0;\n\
2182 rtx insn ATTRIBUTE_UNUSED;\n\
2183 int *_pmatch_len ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2184 break;
2187 printf ("{\n register rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2188 for (i = 1; i <= max_depth; i++)
2189 printf (" register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2191 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2193 if (head->first)
2194 write_tree (head, "", type, 1);
2195 else
2196 printf (" goto ret0;\n");
2198 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2201 /* In break_out_subroutines, we discovered the boundaries for the
2202 subroutines, but did not write them out. Do so now. */
2204 static void
2205 write_subroutines (head, type)
2206 struct decision_head *head;
2207 enum routine_type type;
2209 struct decision *p;
2211 for (p = head->first; p ; p = p->next)
2212 if (p->success.first)
2213 write_subroutines (&p->success, type);
2215 if (head->first->subroutine_number > 0)
2216 write_subroutine (head, type);
2219 /* Begin the output file. */
2221 static void
2222 write_header ()
2224 puts ("\
2225 /* Generated automatically by the program `genrecog' from the target\n\
2226 machine description file. */\n\
2228 #include \"config.h\"\n\
2229 #include \"system.h\"\n\
2230 #include \"rtl.h\"\n\
2231 #include \"tm_p.h\"\n\
2232 #include \"function.h\"\n\
2233 #include \"insn-config.h\"\n\
2234 #include \"recog.h\"\n\
2235 #include \"real.h\"\n\
2236 #include \"output.h\"\n\
2237 #include \"flags.h\"\n\
2238 #include \"hard-reg-set.h\"\n\
2239 #include \"resource.h\"\n\
2240 \n");
2242 puts ("\n\
2243 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2244 X0 is a valid instruction.\n\
2246 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2247 returns a nonnegative number which is the insn code number for the\n\
2248 pattern that matched. This is the same as the order in the machine\n\
2249 description of the entry that matched. This number can be used as an\n\
2250 index into `insn_data' and other tables.\n\
2252 The third argument to recog is an optional pointer to an int. If\n\
2253 present, recog will accept a pattern if it matches except for missing\n\
2254 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2255 the optional pointer will be set to the number of CLOBBERs that need\n\
2256 to be added (it should be initialized to zero by the caller). If it\n\
2257 is set nonzero, the caller should allocate a PARALLEL of the\n\
2258 appropriate size, copy the initial entries, and call add_clobbers\n\
2259 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2262 puts ("\n\
2263 The function split_insns returns 0 if the rtl could not\n\
2264 be split or the split rtl in a SEQUENCE if it can be.\n\
2266 The function peephole2_insns returns 0 if the rtl could not\n\
2267 be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2268 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2269 */\n\n");
2273 /* Construct and return a sequence of decisions
2274 that will recognize INSN.
2276 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2278 static struct decision_head
2279 make_insn_sequence (insn, type)
2280 rtx insn;
2281 enum routine_type type;
2283 rtx x;
2284 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2285 struct decision *last;
2286 struct decision_test *test, **place;
2287 struct decision_head head;
2288 char c_test_pos[2];
2290 record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2292 c_test_pos[0] = '\0';
2293 if (type == PEEPHOLE2)
2295 int i, j;
2297 /* peephole2 gets special treatment:
2298 - X always gets an outer parallel even if it's only one entry
2299 - we remove all traces of outer-level match_scratch and match_dup
2300 expressions here. */
2301 x = rtx_alloc (PARALLEL);
2302 PUT_MODE (x, VOIDmode);
2303 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2304 for (i = j = 0; i < XVECLEN (insn, 0); i++)
2306 rtx tmp = XVECEXP (insn, 0, i);
2307 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2309 XVECEXP (x, 0, j) = tmp;
2310 j++;
2313 XVECLEN (x, 0) = j;
2315 c_test_pos[0] = 'A' + j - 1;
2316 c_test_pos[1] = '\0';
2318 else if (XVECLEN (insn, type == RECOG) == 1)
2319 x = XVECEXP (insn, type == RECOG, 0);
2320 else
2322 x = rtx_alloc (PARALLEL);
2323 XVEC (x, 0) = XVEC (insn, type == RECOG);
2324 PUT_MODE (x, VOIDmode);
2327 validate_pattern (x, insn, NULL_RTX);
2329 memset(&head, 0, sizeof(head));
2330 last = add_to_sequence (x, &head, "", type, 1);
2332 /* Find the end of the test chain on the last node. */
2333 for (test = last->tests; test->next; test = test->next)
2334 continue;
2335 place = &test->next;
2337 if (c_test[0])
2339 /* Need a new node if we have another test to add. */
2340 if (test->type == DT_accept_op)
2342 last = new_decision (c_test_pos, &last->success);
2343 place = &last->tests;
2345 test = new_decision_test (DT_c_test, &place);
2346 test->u.c_test = c_test;
2349 test = new_decision_test (DT_accept_insn, &place);
2350 test->u.insn.code_number = next_insn_code;
2351 test->u.insn.lineno = pattern_lineno;
2352 test->u.insn.num_clobbers_to_add = 0;
2354 switch (type)
2356 case RECOG:
2357 /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2358 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2359 If so, set up to recognize the pattern without these CLOBBERs. */
2361 if (GET_CODE (x) == PARALLEL)
2363 int i;
2365 /* Find the last non-clobber in the parallel. */
2366 for (i = XVECLEN (x, 0); i > 0; i--)
2368 rtx y = XVECEXP (x, 0, i - 1);
2369 if (GET_CODE (y) != CLOBBER
2370 || (GET_CODE (XEXP (y, 0)) != REG
2371 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2372 break;
2375 if (i != XVECLEN (x, 0))
2377 rtx new;
2378 struct decision_head clobber_head;
2380 /* Build a similar insn without the clobbers. */
2381 if (i == 1)
2382 new = XVECEXP (x, 0, 0);
2383 else
2385 int j;
2387 new = rtx_alloc (PARALLEL);
2388 XVEC (new, 0) = rtvec_alloc (i);
2389 for (j = i - 1; j >= 0; j--)
2390 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2393 /* Recognize it. */
2394 memset (&clobber_head, 0, sizeof(clobber_head));
2395 last = add_to_sequence (new, &clobber_head, "", type, 1);
2397 /* Find the end of the test chain on the last node. */
2398 for (test = last->tests; test->next; test = test->next)
2399 continue;
2401 /* We definitely have a new test to add -- create a new
2402 node if needed. */
2403 place = &test->next;
2404 if (test->type == DT_accept_op)
2406 last = new_decision ("", &last->success);
2407 place = &last->tests;
2410 if (c_test[0])
2412 test = new_decision_test (DT_c_test, &place);
2413 test->u.c_test = c_test;
2416 test = new_decision_test (DT_accept_insn, &place);
2417 test->u.insn.code_number = next_insn_code;
2418 test->u.insn.lineno = pattern_lineno;
2419 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2421 merge_trees (&head, &clobber_head);
2424 break;
2426 case SPLIT:
2427 /* Define the subroutine we will call below and emit in genemit. */
2428 printf ("extern rtx gen_split_%d PARAMS ((rtx *));\n", next_insn_code);
2429 break;
2431 case PEEPHOLE2:
2432 /* Define the subroutine we will call below and emit in genemit. */
2433 printf ("extern rtx gen_peephole2_%d PARAMS ((rtx, rtx *));\n",
2434 next_insn_code);
2435 break;
2438 return head;
2441 static void
2442 process_tree (head, subroutine_type)
2443 struct decision_head *head;
2444 enum routine_type subroutine_type;
2446 if (head->first == NULL)
2448 /* We can elide peephole2_insns, but not recog or split_insns. */
2449 if (subroutine_type == PEEPHOLE2)
2450 return;
2452 else
2454 factor_tests (head);
2456 next_subroutine_number = 0;
2457 break_out_subroutines (head, 1);
2458 find_afterward (head, NULL);
2460 /* We run this after find_afterward, because find_afterward needs
2461 the redundant DT_mode tests on predicates to determine whether
2462 two tests can both be true or not. */
2463 simplify_tests(head);
2465 write_subroutines (head, subroutine_type);
2468 write_subroutine (head, subroutine_type);
2471 extern int main PARAMS ((int, char **));
2474 main (argc, argv)
2475 int argc;
2476 char **argv;
2478 rtx desc;
2479 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2481 progname = "genrecog";
2483 memset (&recog_tree, 0, sizeof recog_tree);
2484 memset (&split_tree, 0, sizeof split_tree);
2485 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2487 if (argc <= 1)
2488 fatal ("No input file name.");
2490 if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
2491 return (FATAL_EXIT_CODE);
2493 next_insn_code = 0;
2494 next_index = 0;
2496 write_header ();
2498 /* Read the machine description. */
2500 while (1)
2502 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2503 if (desc == NULL)
2504 break;
2506 if (GET_CODE (desc) == DEFINE_INSN)
2508 h = make_insn_sequence (desc, RECOG);
2509 merge_trees (&recog_tree, &h);
2511 else if (GET_CODE (desc) == DEFINE_SPLIT)
2513 h = make_insn_sequence (desc, SPLIT);
2514 merge_trees (&split_tree, &h);
2516 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2518 h = make_insn_sequence (desc, PEEPHOLE2);
2519 merge_trees (&peephole2_tree, &h);
2522 next_index++;
2525 if (error_count)
2526 return FATAL_EXIT_CODE;
2528 puts ("\n\n");
2530 process_tree (&recog_tree, RECOG);
2531 process_tree (&split_tree, SPLIT);
2532 process_tree (&peephole2_tree, PEEPHOLE2);
2534 fflush (stdout);
2535 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2538 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
2539 const char *
2540 get_insn_name (code)
2541 int code;
2543 if (code < insn_name_ptr_size)
2544 return insn_name_ptr[code];
2545 else
2546 return NULL;
2549 static void
2550 record_insn_name (code, name)
2551 int code;
2552 const char *name;
2554 static const char *last_real_name = "insn";
2555 static int last_real_code = 0;
2556 char *new;
2558 if (insn_name_ptr_size <= code)
2560 int new_size;
2561 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2562 insn_name_ptr =
2563 (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2564 memset (insn_name_ptr + insn_name_ptr_size, 0,
2565 sizeof(char *) * (new_size - insn_name_ptr_size));
2566 insn_name_ptr_size = new_size;
2569 if (!name || name[0] == '\0')
2571 new = xmalloc (strlen (last_real_name) + 10);
2572 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2574 else
2576 last_real_name = new = xstrdup (name);
2577 last_real_code = code;
2580 insn_name_ptr[code] = new;
2583 static void
2584 debug_decision_2 (test)
2585 struct decision_test *test;
2587 switch (test->type)
2589 case DT_mode:
2590 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2591 break;
2592 case DT_code:
2593 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2594 break;
2595 case DT_veclen:
2596 fprintf (stderr, "veclen=%d", test->u.veclen);
2597 break;
2598 case DT_elt_zero_int:
2599 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2600 break;
2601 case DT_elt_one_int:
2602 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2603 break;
2604 case DT_elt_zero_wide:
2605 fprintf (stderr, "elt0_w=");
2606 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2607 break;
2608 case DT_dup:
2609 fprintf (stderr, "dup=%d", test->u.dup);
2610 break;
2611 case DT_pred:
2612 fprintf (stderr, "pred=(%s,%s)",
2613 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2614 break;
2615 case DT_c_test:
2617 char sub[16+4];
2618 strncpy (sub, test->u.c_test, sizeof(sub));
2619 memcpy (sub+16, "...", 4);
2620 fprintf (stderr, "c_test=\"%s\"", sub);
2622 break;
2623 case DT_accept_op:
2624 fprintf (stderr, "A_op=%d", test->u.opno);
2625 break;
2626 case DT_accept_insn:
2627 fprintf (stderr, "A_insn=(%d,%d)",
2628 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2629 break;
2631 default:
2632 abort ();
2636 static void
2637 debug_decision_1 (d, indent)
2638 struct decision *d;
2639 int indent;
2641 int i;
2642 struct decision_test *test;
2644 if (d == NULL)
2646 for (i = 0; i < indent; ++i)
2647 putc (' ', stderr);
2648 fputs ("(nil)\n", stderr);
2649 return;
2652 for (i = 0; i < indent; ++i)
2653 putc (' ', stderr);
2655 putc ('{', stderr);
2656 test = d->tests;
2657 if (test)
2659 debug_decision_2 (test);
2660 while ((test = test->next) != NULL)
2662 fputs (" + ", stderr);
2663 debug_decision_2 (test);
2666 fprintf (stderr, "} %d n %d a %d\n", d->number,
2667 (d->next ? d->next->number : -1),
2668 (d->afterward ? d->afterward->number : -1));
2671 static void
2672 debug_decision_0 (d, indent, maxdepth)
2673 struct decision *d;
2674 int indent, maxdepth;
2676 struct decision *n;
2677 int i;
2679 if (maxdepth < 0)
2680 return;
2681 if (d == NULL)
2683 for (i = 0; i < indent; ++i)
2684 putc (' ', stderr);
2685 fputs ("(nil)\n", stderr);
2686 return;
2689 debug_decision_1 (d, indent);
2690 for (n = d->success.first; n ; n = n->next)
2691 debug_decision_0 (n, indent + 2, maxdepth - 1);
2694 void
2695 debug_decision (d)
2696 struct decision *d;
2698 debug_decision_0 (d, 0, 1000000);
2701 void
2702 debug_decision_list (d)
2703 struct decision *d;
2705 while (d)
2707 debug_decision_0 (d, 0, 0);
2708 d = d->next;