1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 97, 98 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. */
22 /* This program is used to produce insn-recog.c, which contains
23 a function called `recog' plus its subroutines.
24 These functions contain a decision tree
25 that recognizes whether an rtx, the argument given to recog,
26 is a valid instruction.
28 recog returns -1 if the rtx is not valid.
29 If the rtx is valid, recog returns a nonnegative number
30 which is the insn code number for the pattern that matched.
31 This is the same as the order in the machine description of the
32 entry that matched. This number can be used as an index into various
33 insn_* tables, such as insn_template, insn_outfun, and insn_n_operands
34 (found in insn-output.c).
36 The third argument to recog is an optional pointer to an int.
37 If present, recog will accept a pattern if it matches except for
38 missing CLOBBER expressions at the end. In that case, the value
39 pointed to by the optional pointer will be set to the number of
40 CLOBBERs that need to be added (it should be initialized to zero by
41 the caller). If it is set nonzero, the caller should allocate a
42 PARALLEL of the appropriate size, copy the initial entries, and call
43 add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
45 This program also generates the function `split_insns',
46 which returns 0 if the rtl could not be split, or
47 it returns the split rtl in a SEQUENCE. */
59 static struct obstack obstack
;
60 struct obstack
*rtl_obstack
= &obstack
;
62 #define obstack_chunk_alloc xmalloc
63 #define obstack_chunk_free free
65 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
66 char **insn_name_ptr
= 0;
68 /* Data structure for a listhead of decision trees. The alternatives
69 to a node are kept in a doublely-linked list so we can easily add nodes
70 to the proper place when merging. */
72 struct decision_head
{ struct decision
*first
, *last
; };
74 /* Data structure for decision tree for recognizing
75 legitimate instructions. */
79 int number
; /* Node number, used for labels */
80 char *position
; /* String denoting position in pattern */
81 RTX_CODE code
; /* Code to test for or UNKNOWN to suppress */
82 char ignore_code
; /* If non-zero, need not test code */
83 char ignore_mode
; /* If non-zero, need not test mode */
84 int veclen
; /* Length of vector, if nonzero */
85 enum machine_mode mode
; /* Machine mode of node */
86 char enforce_mode
; /* If non-zero, test `mode' */
87 char retest_code
, retest_mode
; /* See write_tree_1 */
88 int test_elt_zero_int
; /* Nonzero if should test XINT (rtl, 0) */
89 int elt_zero_int
; /* Required value for XINT (rtl, 0) */
90 int test_elt_one_int
; /* Nonzero if should test XINT (rtl, 1) */
91 int elt_one_int
; /* Required value for XINT (rtl, 1) */
92 int test_elt_zero_wide
; /* Nonzero if should test XWINT (rtl, 0) */
93 HOST_WIDE_INT elt_zero_wide
; /* Required value for XWINT (rtl, 0) */
94 char *tests
; /* If nonzero predicate to call */
95 int pred
; /* `preds' index of predicate or -1 */
96 char *c_test
; /* Additional test to perform */
97 struct decision_head success
; /* Nodes to test on success */
98 int insn_code_number
; /* Insn number matched, if success */
99 int num_clobbers_to_add
; /* Number of CLOBBERs to be added to pattern */
100 struct decision
*next
; /* Node to test on failure */
101 struct decision
*prev
; /* Node whose failure tests us */
102 struct decision
*afterward
; /* Node to test on success, but failure of
104 int opno
; /* Operand number, if >= 0 */
105 int dupno
; /* Number of operand to compare against */
106 int label_needed
; /* Nonzero if label needed when writing tree */
107 int subroutine_number
; /* Number of subroutine this node starts */
110 #define SUBROUTINE_THRESHOLD 50
112 static int next_subroutine_number
;
114 /* We can write two types of subroutines: One for insn recognition and
115 one to split insns. This defines which type is being written. */
117 enum routine_type
{RECOG
, SPLIT
};
119 /* Next available node number for tree nodes. */
121 static int next_number
;
123 /* Next number to use as an insn_code. */
125 static int next_insn_code
;
127 /* Similar, but counts all expressions in the MD file; used for
130 static int next_index
;
132 /* Record the highest depth we ever have so we know how many variables to
133 allocate in each subroutine we make. */
135 static int max_depth
;
137 /* This table contains a list of the rtl codes that can possibly match a
138 predicate defined in recog.c. The function `not_both_true' uses it to
139 deduce that there are no expressions that can be matches by certain pairs
140 of tree nodes. Also, if a predicate can match only one code, we can
141 hardwire that code into the node testing the predicate. */
143 static struct pred_table
146 RTX_CODE codes
[NUM_RTX_CODE
];
148 = {{"general_operand", {CONST_INT
, CONST_DOUBLE
, CONST
, SYMBOL_REF
,
149 LABEL_REF
, SUBREG
, REG
, MEM
}},
150 #ifdef PREDICATE_CODES
153 {"address_operand", {CONST_INT
, CONST_DOUBLE
, CONST
, SYMBOL_REF
,
154 LABEL_REF
, SUBREG
, REG
, MEM
, PLUS
, MINUS
, MULT
}},
155 {"register_operand", {SUBREG
, REG
}},
156 {"scratch_operand", {SCRATCH
, REG
}},
157 {"immediate_operand", {CONST_INT
, CONST_DOUBLE
, CONST
, SYMBOL_REF
,
159 {"const_int_operand", {CONST_INT
}},
160 {"const_double_operand", {CONST_INT
, CONST_DOUBLE
}},
161 {"nonimmediate_operand", {SUBREG
, REG
, MEM
}},
162 {"nonmemory_operand", {CONST_INT
, CONST_DOUBLE
, CONST
, SYMBOL_REF
,
163 LABEL_REF
, SUBREG
, REG
}},
164 {"push_operand", {MEM
}},
165 {"memory_operand", {SUBREG
, MEM
}},
166 {"indirect_operand", {SUBREG
, MEM
}},
167 {"comparison_operator", {EQ
, NE
, LE
, LT
, GE
, GT
, LEU
, LTU
, GEU
, GTU
}},
168 {"mode_independent_operand", {CONST_INT
, CONST_DOUBLE
, CONST
, SYMBOL_REF
,
169 LABEL_REF
, SUBREG
, REG
, MEM
}}};
171 #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
173 static struct decision_head make_insn_sequence
PROTO((rtx
, enum routine_type
));
174 static struct decision
*add_to_sequence
PROTO((rtx
, struct decision_head
*,
176 static int not_both_true
PROTO((struct decision
*, struct decision
*,
178 static int position_merit
PROTO((struct decision
*, enum machine_mode
,
180 static struct decision_head merge_trees
PROTO((struct decision_head
,
181 struct decision_head
));
182 static int break_out_subroutines
PROTO((struct decision_head
,
183 enum routine_type
, int));
184 static void write_subroutine
PROTO((struct decision
*, enum routine_type
));
185 static void write_tree_1
PROTO((struct decision
*, char *,
186 struct decision
*, enum routine_type
));
187 static void print_code
PROTO((enum rtx_code
));
188 static int same_codes
PROTO((struct decision
*, enum rtx_code
));
189 static void clear_codes
PROTO((struct decision
*));
190 static int same_modes
PROTO((struct decision
*, enum machine_mode
));
191 static void clear_modes
PROTO((struct decision
*));
192 static void write_tree
PROTO((struct decision
*, char *,
193 struct decision
*, int,
195 static void change_state
PROTO((char *, char *, int));
196 static char *copystr
PROTO((char *));
197 static void mybzero
PROTO((char *, unsigned));
198 static void mybcopy
PROTO((char *, char *, unsigned));
199 static void fatal
PVPROTO((char *, ...)) ATTRIBUTE_PRINTF_1
;
200 char *xrealloc
PROTO((char *, unsigned));
201 char *xmalloc
PROTO((unsigned));
202 void fancy_abort
PROTO((void));
204 /* Construct and return a sequence of decisions
205 that will recognize INSN.
207 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
209 static struct decision_head
210 make_insn_sequence (insn
, type
)
212 enum routine_type type
;
215 char *c_test
= XSTR (insn
, type
== RECOG
? 2 : 1);
216 struct decision
*last
;
217 struct decision_head head
;
219 if (XVECLEN (insn
, type
== RECOG
) == 1)
220 x
= XVECEXP (insn
, type
== RECOG
, 0);
223 x
= rtx_alloc (PARALLEL
);
224 XVEC (x
, 0) = XVEC (insn
, type
== RECOG
);
225 PUT_MODE (x
, VOIDmode
);
228 last
= add_to_sequence (x
, &head
, "");
231 last
->c_test
= c_test
;
232 last
->insn_code_number
= next_insn_code
;
233 last
->num_clobbers_to_add
= 0;
235 /* If this is not a DEFINE_SPLIT and X is a PARALLEL, see if it ends with a
236 group of CLOBBERs of (hard) registers or MATCH_SCRATCHes. If so, set up
237 to recognize the pattern without these CLOBBERs. */
239 if (type
== RECOG
&& GET_CODE (x
) == PARALLEL
)
243 for (i
= XVECLEN (x
, 0); i
> 0; i
--)
244 if (GET_CODE (XVECEXP (x
, 0, i
- 1)) != CLOBBER
245 || (GET_CODE (XEXP (XVECEXP (x
, 0, i
- 1), 0)) != REG
246 && GET_CODE (XEXP (XVECEXP (x
, 0, i
- 1), 0)) != MATCH_SCRATCH
))
249 if (i
!= XVECLEN (x
, 0))
252 struct decision_head clobber_head
;
255 new = XVECEXP (x
, 0, 0);
260 new = rtx_alloc (PARALLEL
);
261 XVEC (new, 0) = rtvec_alloc (i
);
262 for (j
= i
- 1; j
>= 0; j
--)
263 XVECEXP (new, 0, j
) = XVECEXP (x
, 0, j
);
266 last
= add_to_sequence (new, &clobber_head
, "");
269 last
->c_test
= c_test
;
270 last
->insn_code_number
= next_insn_code
;
271 last
->num_clobbers_to_add
= XVECLEN (x
, 0) - i
;
273 head
= merge_trees (head
, clobber_head
);
280 /* Define the subroutine we will call below and emit in genemit. */
281 printf ("extern rtx gen_split_%d ();\n", last
->insn_code_number
);
286 /* Create a chain of nodes to verify that an rtl expression matches
289 LAST is a pointer to the listhead in the previous node in the chain (or
290 in the calling function, for the first node).
292 POSITION is the string representing the current position in the insn.
294 A pointer to the final node in the chain is returned. */
296 static struct decision
*
297 add_to_sequence (pattern
, last
, position
)
299 struct decision_head
*last
;
302 register RTX_CODE code
;
303 register struct decision
*new
304 = (struct decision
*) xmalloc (sizeof (struct decision
));
305 struct decision
*this;
309 int depth
= strlen (position
);
312 if (depth
> max_depth
)
315 new->number
= next_number
++;
316 new->position
= copystr (position
);
317 new->ignore_code
= 0;
318 new->ignore_mode
= 0;
319 new->enforce_mode
= 1;
320 new->retest_code
= new->retest_mode
= 0;
322 new->test_elt_zero_int
= 0;
323 new->test_elt_one_int
= 0;
324 new->test_elt_zero_wide
= 0;
325 new->elt_zero_int
= 0;
326 new->elt_one_int
= 0;
327 new->elt_zero_wide
= 0;
331 new->success
.first
= new->success
.last
= 0;
332 new->insn_code_number
= -1;
333 new->num_clobbers_to_add
= 0;
339 new->label_needed
= 0;
340 new->subroutine_number
= 0;
344 last
->first
= last
->last
= new;
346 newpos
= (char *) alloca (depth
+ 2);
347 strcpy (newpos
, position
);
348 newpos
[depth
+ 1] = 0;
352 new->mode
= GET_MODE (pattern
);
353 new->code
= code
= GET_CODE (pattern
);
362 new->opno
= XINT (pattern
, 0);
363 new->code
= (code
== MATCH_PARALLEL
? PARALLEL
: UNKNOWN
);
364 new->enforce_mode
= 0;
366 if (code
== MATCH_SCRATCH
)
367 new->tests
= "scratch_operand";
369 new->tests
= XSTR (pattern
, 1);
371 if (*new->tests
== 0)
374 /* See if we know about this predicate and save its number. If we do,
375 and it only accepts one code, note that fact. The predicate
376 `const_int_operand' only tests for a CONST_INT, so if we do so we
377 can avoid calling it at all.
379 Finally, if we know that the predicate does not allow CONST_INT, we
380 know that the only way the predicate can match is if the modes match
381 (here we use the kludge of relying on the fact that "address_operand"
382 accepts CONST_INT; otherwise, it would have to be a special case),
383 so we can test the mode (but we need not). This fact should
384 considerably simplify the generated code. */
388 for (i
= 0; i
< NUM_KNOWN_PREDS
; i
++)
389 if (! strcmp (preds
[i
].name
, new->tests
))
392 int allows_const_int
= 0;
396 if (preds
[i
].codes
[1] == 0 && new->code
== UNKNOWN
)
398 new->code
= preds
[i
].codes
[0];
399 if (! strcmp ("const_int_operand", new->tests
))
400 new->tests
= 0, new->pred
= -1;
403 for (j
= 0; j
< NUM_RTX_CODE
&& preds
[i
].codes
[j
] != 0; j
++)
404 if (preds
[i
].codes
[j
] == CONST_INT
)
405 allows_const_int
= 1;
407 if (! allows_const_int
)
408 new->enforce_mode
= new->ignore_mode
= 1;
413 #ifdef PREDICATE_CODES
414 /* If the port has a list of the predicates it uses but omits
416 if (i
== NUM_KNOWN_PREDS
)
417 fprintf (stderr
, "Warning: `%s' not in PREDICATE_CODES\n",
422 if (code
== MATCH_OPERATOR
|| code
== MATCH_PARALLEL
)
424 for (i
= 0; i
< XVECLEN (pattern
, 2); i
++)
426 newpos
[depth
] = i
+ (code
== MATCH_OPERATOR
? '0': 'a');
427 new = add_to_sequence (XVECEXP (pattern
, 2, i
),
428 &new->success
, newpos
);
435 new->opno
= XINT (pattern
, 0);
436 new->dupno
= XINT (pattern
, 0);
439 for (i
= 0; i
< XVECLEN (pattern
, 1); i
++)
441 newpos
[depth
] = i
+ '0';
442 new = add_to_sequence (XVECEXP (pattern
, 1, i
),
443 &new->success
, newpos
);
449 new->dupno
= XINT (pattern
, 0);
451 new->enforce_mode
= 0;
455 pattern
= XEXP (pattern
, 0);
459 /* The operands of a SET must have the same mode unless one is VOIDmode. */
460 if (GET_MODE (SET_SRC (pattern
)) != VOIDmode
461 && GET_MODE (SET_DEST (pattern
)) != VOIDmode
462 && GET_MODE (SET_SRC (pattern
)) != GET_MODE (SET_DEST (pattern
))
463 /* The mode of an ADDRESS_OPERAND is the mode of the memory reference,
464 not the mode of the address. */
465 && ! (GET_CODE (SET_SRC (pattern
)) == MATCH_OPERAND
466 && ! strcmp (XSTR (SET_SRC (pattern
), 1), "address_operand")))
468 print_rtl (stderr
, pattern
);
469 fputc ('\n', stderr
);
470 fatal ("mode mismatch in SET");
473 new = add_to_sequence (SET_DEST (pattern
), &new->success
, newpos
);
474 this->success
.first
->enforce_mode
= 1;
476 new = add_to_sequence (SET_SRC (pattern
), &new->success
, newpos
);
478 /* If set are setting CC0 from anything other than a COMPARE, we
479 must enforce the mode so that we do not produce ambiguous insns. */
480 if (GET_CODE (SET_DEST (pattern
)) == CC0
481 && GET_CODE (SET_SRC (pattern
)) != COMPARE
)
482 this->success
.first
->enforce_mode
= 1;
487 case STRICT_LOW_PART
:
489 new = add_to_sequence (XEXP (pattern
, 0), &new->success
, newpos
);
490 this->success
.first
->enforce_mode
= 1;
494 this->test_elt_one_int
= 1;
495 this->elt_one_int
= XINT (pattern
, 1);
497 new = add_to_sequence (XEXP (pattern
, 0), &new->success
, newpos
);
498 this->success
.first
->enforce_mode
= 1;
504 new = add_to_sequence (XEXP (pattern
, 0), &new->success
, newpos
);
505 this->success
.first
->enforce_mode
= 1;
507 new = add_to_sequence (XEXP (pattern
, 1), &new->success
, newpos
);
509 new = add_to_sequence (XEXP (pattern
, 2), &new->success
, newpos
);
512 case EQ
: case NE
: case LE
: case LT
: case GE
: case GT
:
513 case LEU
: case LTU
: case GEU
: case GTU
:
514 /* If the first operand is (cc0), we don't have to do anything
516 if (GET_CODE (XEXP (pattern
, 0)) == CC0
)
519 /* ... fall through ... */
522 /* Enforce the mode on the first operand to avoid ambiguous insns. */
524 new = add_to_sequence (XEXP (pattern
, 0), &new->success
, newpos
);
525 this->success
.first
->enforce_mode
= 1;
527 new = add_to_sequence (XEXP (pattern
, 1), &new->success
, newpos
);
534 fmt
= GET_RTX_FORMAT (code
);
535 len
= GET_RTX_LENGTH (code
);
536 for (i
= 0; i
< len
; i
++)
538 newpos
[depth
] = '0' + i
;
539 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
540 new = add_to_sequence (XEXP (pattern
, i
), &new->success
, newpos
);
541 else if (fmt
[i
] == 'i' && i
== 0)
543 this->test_elt_zero_int
= 1;
544 this->elt_zero_int
= XINT (pattern
, i
);
546 else if (fmt
[i
] == 'i' && i
== 1)
548 this->test_elt_one_int
= 1;
549 this->elt_one_int
= XINT (pattern
, i
);
551 else if (fmt
[i
] == 'w' && i
== 0)
553 this->test_elt_zero_wide
= 1;
554 this->elt_zero_wide
= XWINT (pattern
, i
);
556 else if (fmt
[i
] == 'E')
559 /* We do not handle a vector appearing as other than
560 the first item, just because nothing uses them
561 and by handling only the special case
562 we can use one element in newpos for either
563 the item number of a subexpression
564 or the element number in a vector. */
567 this->veclen
= XVECLEN (pattern
, i
);
568 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
570 newpos
[depth
] = 'a' + j
;
571 new = add_to_sequence (XVECEXP (pattern
, i
, j
),
572 &new->success
, newpos
);
575 else if (fmt
[i
] != '0')
581 /* Return 1 if we can prove that there is no RTL that can match both
582 D1 and D2. Otherwise, return 0 (it may be that there is an RTL that
583 can match both or just that we couldn't prove there wasn't such an RTL).
585 TOPLEVEL is non-zero if we are to only look at the top level and not
586 recursively descend. */
589 not_both_true (d1
, d2
, toplevel
)
590 struct decision
*d1
, *d2
;
593 struct decision
*p1
, *p2
;
595 /* If they are both to test modes and the modes are different, they aren't
596 both true. Similarly for codes, integer elements, and vector lengths. */
598 if ((d1
->enforce_mode
&& d2
->enforce_mode
599 && d1
->mode
!= VOIDmode
&& d2
->mode
!= VOIDmode
&& d1
->mode
!= d2
->mode
)
600 || (d1
->code
!= UNKNOWN
&& d2
->code
!= UNKNOWN
&& d1
->code
!= d2
->code
)
601 || (d1
->test_elt_zero_int
&& d2
->test_elt_zero_int
602 && d1
->elt_zero_int
!= d2
->elt_zero_int
)
603 || (d1
->test_elt_one_int
&& d2
->test_elt_one_int
604 && d1
->elt_one_int
!= d2
->elt_one_int
)
605 || (d1
->test_elt_zero_wide
&& d2
->test_elt_zero_wide
606 && d1
->elt_zero_wide
!= d2
->elt_zero_wide
)
607 || (d1
->veclen
&& d2
->veclen
&& d1
->veclen
!= d2
->veclen
))
610 /* If either is a wild-card MATCH_OPERAND without a predicate, it can match
611 absolutely anything, so we can't say that no intersection is possible.
612 This case is detected by having a zero TESTS field with a code of
615 if ((d1
->tests
== 0 && d1
->code
== UNKNOWN
)
616 || (d2
->tests
== 0 && d2
->code
== UNKNOWN
))
619 /* If either has a predicate that we know something about, set things up so
620 that D1 is the one that always has a known predicate. Then see if they
621 have any codes in common. */
623 if (d1
->pred
>= 0 || d2
->pred
>= 0)
628 p1
= d1
, d1
= d2
, d2
= p1
;
630 /* If D2 tests an explicit code, see if it is in the list of valid codes
631 for D1's predicate. */
632 if (d2
->code
!= UNKNOWN
)
634 for (i
= 0; i
< NUM_RTX_CODE
&& preds
[d1
->pred
].codes
[i
] != 0; i
++)
635 if (preds
[d1
->pred
].codes
[i
] == d2
->code
)
638 if (preds
[d1
->pred
].codes
[i
] == 0)
642 /* Otherwise see if the predicates have any codes in common. */
644 else if (d2
->pred
>= 0)
646 for (i
= 0; i
< NUM_RTX_CODE
&& preds
[d1
->pred
].codes
[i
] != 0; i
++)
648 for (j
= 0; j
< NUM_RTX_CODE
; j
++)
649 if (preds
[d2
->pred
].codes
[j
] == 0
650 || preds
[d2
->pred
].codes
[j
] == preds
[d1
->pred
].codes
[i
])
653 if (preds
[d2
->pred
].codes
[j
] != 0)
657 if (preds
[d1
->pred
].codes
[i
] == 0)
662 /* If we got here, we can't prove that D1 and D2 cannot both be true.
663 If we are only to check the top level, return 0. Otherwise, see if
664 we can prove that all choices in both successors are mutually
665 exclusive. If either does not have any successors, we can't prove
666 they can't both be true. */
668 if (toplevel
|| d1
->success
.first
== 0 || d2
->success
.first
== 0)
671 for (p1
= d1
->success
.first
; p1
; p1
= p1
->next
)
672 for (p2
= d2
->success
.first
; p2
; p2
= p2
->next
)
673 if (! not_both_true (p1
, p2
, 0))
679 /* Assuming that we can reorder all the alternatives at a specific point in
680 the tree (see discussion in merge_trees), we would prefer an ordering of
681 nodes where groups of consecutive nodes test the same mode and, within each
682 mode, groups of nodes test the same code. With this order, we can
683 construct nested switch statements, the inner one to test the code and
684 the outer one to test the mode.
686 We would like to list nodes testing for specific codes before those
687 that test predicates to avoid unnecessary function calls. Similarly,
688 tests for specific modes should precede nodes that allow any mode.
690 This function returns the merit (with 0 being the best) of inserting
691 a test involving the specified MODE and CODE after node P. If P is
692 zero, we are to determine the merit of inserting the test at the front
696 position_merit (p
, mode
, code
)
698 enum machine_mode mode
;
701 enum machine_mode p_mode
;
703 /* The only time the front of the list is anything other than the worst
704 position is if we are testing a mode that isn't VOIDmode. */
706 return mode
== VOIDmode
? 3 : 2;
708 p_mode
= p
->enforce_mode
? p
->mode
: VOIDmode
;
710 /* The best case is if the codes and modes both match. */
711 if (p_mode
== mode
&& p
->code
== code
)
714 /* If the codes don't match, the next best case is if the modes match.
715 In that case, the best position for this node depends on whether
716 we are testing for a specific code or not. If we are, the best place
717 is after some other test for an explicit code and our mode or after
718 the last test in the previous mode if every test in our mode is for
721 If we are testing for UNKNOWN, then the next best case is at the end of
725 && ((p_mode
== mode
&& p
->code
!= UNKNOWN
)
726 || (p_mode
!= mode
&& p
->next
727 && (p
->next
->enforce_mode
? p
->next
->mode
: VOIDmode
) == mode
728 && (p
->next
->code
== UNKNOWN
))))
729 || (code
== UNKNOWN
&& p_mode
== mode
731 || (p
->next
->enforce_mode
? p
->next
->mode
: VOIDmode
) != mode
)))
734 /* The third best case occurs when nothing is testing MODE. If MODE
735 is not VOIDmode, then the third best case is after something of any
736 mode that is not VOIDmode. If we are testing VOIDmode, the third best
737 place is the end of the list. */
740 && ((mode
!= VOIDmode
&& p_mode
!= VOIDmode
)
741 || (mode
== VOIDmode
&& p
->next
== 0)))
744 /* Otherwise, we have the worst case. */
748 /* Merge two decision tree listheads OLDH and ADDH,
749 modifying OLDH destructively, and return the merged tree. */
751 static struct decision_head
752 merge_trees (oldh
, addh
)
753 register struct decision_head oldh
, addh
;
755 struct decision
*add
, *next
;
763 /* If we are adding things at different positions, something is wrong. */
764 if (strcmp (oldh
.first
->position
, addh
.first
->position
))
767 for (add
= addh
.first
; add
; add
= next
)
769 enum machine_mode add_mode
= add
->enforce_mode
? add
->mode
: VOIDmode
;
770 struct decision
*best_position
= 0;
772 struct decision
*old
;
776 /* The semantics of pattern matching state that the tests are done in
777 the order given in the MD file so that if an insn matches two
778 patterns, the first one will be used. However, in practice, most,
779 if not all, patterns are unambiguous so that their order is
780 independent. In that case, we can merge identical tests and
781 group all similar modes and codes together.
783 Scan starting from the end of OLDH until we reach a point
784 where we reach the head of the list or where we pass a pattern
785 that could also be true if NEW is true. If we find an identical
786 pattern, we can merge them. Also, record the last node that tests
787 the same code and mode and the last one that tests just the same mode.
789 If we have no match, place NEW after the closest match we found. */
791 for (old
= oldh
.last
; old
; old
= old
->prev
)
795 /* If we don't have anything to test except an additional test,
796 do not consider the two nodes equal. If we did, the test below
797 would cause an infinite recursion. */
798 if (old
->tests
== 0 && old
->test_elt_zero_int
== 0
799 && old
->test_elt_one_int
== 0 && old
->veclen
== 0
800 && old
->test_elt_zero_wide
== 0
801 && old
->dupno
== -1 && old
->mode
== VOIDmode
802 && old
->code
== UNKNOWN
803 && (old
->c_test
!= 0 || add
->c_test
!= 0))
806 else if ((old
->tests
== add
->tests
807 || (old
->pred
>= 0 && old
->pred
== add
->pred
)
808 || (old
->tests
&& add
->tests
809 && !strcmp (old
->tests
, add
->tests
)))
810 && old
->test_elt_zero_int
== add
->test_elt_zero_int
811 && old
->elt_zero_int
== add
->elt_zero_int
812 && old
->test_elt_one_int
== add
->test_elt_one_int
813 && old
->elt_one_int
== add
->elt_one_int
814 && old
->test_elt_zero_wide
== add
->test_elt_zero_wide
815 && old
->elt_zero_wide
== add
->elt_zero_wide
816 && old
->veclen
== add
->veclen
817 && old
->dupno
== add
->dupno
818 && old
->opno
== add
->opno
819 && old
->code
== add
->code
820 && old
->enforce_mode
== add
->enforce_mode
821 && old
->mode
== add
->mode
)
823 /* If the additional test is not the same, split both nodes
824 into nodes that just contain all things tested before the
825 additional test and nodes that contain the additional test
826 and actions when it is true. This optimization is important
827 because of the case where we have almost identical patterns
828 with different tests on target flags. */
830 if (old
->c_test
!= add
->c_test
831 && ! (old
->c_test
&& add
->c_test
832 && !strcmp (old
->c_test
, add
->c_test
)))
834 if (old
->insn_code_number
>= 0 || old
->opno
>= 0)
836 struct decision
*split
837 = (struct decision
*) xmalloc (sizeof (struct decision
));
839 mybcopy ((char *) old
, (char *) split
,
840 sizeof (struct decision
));
842 old
->success
.first
= old
->success
.last
= split
;
845 old
->insn_code_number
= -1;
846 old
->num_clobbers_to_add
= 0;
848 split
->number
= next_number
++;
849 split
->next
= split
->prev
= 0;
850 split
->mode
= VOIDmode
;
851 split
->code
= UNKNOWN
;
853 split
->test_elt_zero_int
= 0;
854 split
->test_elt_one_int
= 0;
855 split
->test_elt_zero_wide
= 0;
861 if (add
->insn_code_number
>= 0 || add
->opno
>= 0)
863 struct decision
*split
864 = (struct decision
*) xmalloc (sizeof (struct decision
));
866 mybcopy ((char *) add
, (char *) split
,
867 sizeof (struct decision
));
869 add
->success
.first
= add
->success
.last
= split
;
872 add
->insn_code_number
= -1;
873 add
->num_clobbers_to_add
= 0;
875 split
->number
= next_number
++;
876 split
->next
= split
->prev
= 0;
877 split
->mode
= VOIDmode
;
878 split
->code
= UNKNOWN
;
880 split
->test_elt_zero_int
= 0;
881 split
->test_elt_one_int
= 0;
882 split
->test_elt_zero_wide
= 0;
889 if (old
->insn_code_number
>= 0 && add
->insn_code_number
>= 0)
891 /* If one node is for a normal insn and the second is
892 for the base insn with clobbers stripped off, the
893 second node should be ignored. */
895 if (old
->num_clobbers_to_add
== 0
896 && add
->num_clobbers_to_add
> 0)
897 /* Nothing to do here. */
899 else if (old
->num_clobbers_to_add
> 0
900 && add
->num_clobbers_to_add
== 0)
902 /* In this case, replace OLD with ADD. */
903 old
->insn_code_number
= add
->insn_code_number
;
904 old
->num_clobbers_to_add
= 0;
907 fatal ("Two actions at one point in tree");
910 if (old
->insn_code_number
== -1)
911 old
->insn_code_number
= add
->insn_code_number
;
912 old
->success
= merge_trees (old
->success
, add
->success
);
917 /* Unless we have already found the best possible insert point,
918 see if this position is better. If so, record it. */
921 && ((our_merit
= position_merit (old
, add_mode
, add
->code
))
923 best_merit
= our_merit
, best_position
= old
;
925 if (! not_both_true (old
, add
, 0))
929 /* If ADD was duplicate, we are done. */
933 /* Otherwise, find the best place to insert ADD. Normally this is
934 BEST_POSITION. However, if we went all the way to the top of
935 the list, it might be better to insert at the top. */
937 if (best_position
== 0)
941 && position_merit (NULL_PTR
, add_mode
, add
->code
) < best_merit
)
944 add
->next
= oldh
.first
;
945 oldh
.first
->prev
= add
;
951 add
->prev
= best_position
;
952 add
->next
= best_position
->next
;
953 best_position
->next
= add
;
954 if (best_position
== oldh
.last
)
957 add
->next
->prev
= add
;
964 /* Count the number of subnodes of HEAD. If the number is high enough,
965 make the first node in HEAD start a separate subroutine in the C code
968 TYPE gives the type of routine we are writing.
970 INITIAL is non-zero if this is the highest-level node. We never write
974 break_out_subroutines (head
, type
, initial
)
975 struct decision_head head
;
976 enum routine_type type
;
980 struct decision
*sub
;
982 for (sub
= head
.first
; sub
; sub
= sub
->next
)
983 size
+= 1 + break_out_subroutines (sub
->success
, type
, 0);
985 if (size
> SUBROUTINE_THRESHOLD
&& ! initial
)
987 head
.first
->subroutine_number
= ++next_subroutine_number
;
988 write_subroutine (head
.first
, type
);
994 /* Write out a subroutine of type TYPE to do comparisons starting at node
998 write_subroutine (tree
, type
)
999 struct decision
*tree
;
1000 enum routine_type type
;
1005 printf ("rtx\nsplit");
1007 printf ("int\nrecog");
1009 if (tree
!= 0 && tree
->subroutine_number
> 0)
1010 printf ("_%d", tree
->subroutine_number
);
1011 else if (type
== SPLIT
)
1014 printf (" (x0, insn");
1016 printf (", pnum_clobbers");
1019 printf (" register rtx x0;\n rtx insn ATTRIBUTE_UNUSED;\n");
1021 printf (" int *pnum_clobbers ATTRIBUTE_UNUSED;\n");
1024 printf (" register rtx *ro = &recog_operand[0];\n");
1026 printf (" register rtx ");
1027 for (i
= 1; i
< max_depth
; i
++)
1028 printf ("x%d ATTRIBUTE_UNUSED, ", i
);
1030 printf ("x%d ATTRIBUTE_UNUSED;\n", max_depth
);
1031 printf (" %s tem ATTRIBUTE_UNUSED;\n", type
== SPLIT
? "rtx" : "int");
1032 write_tree (tree
, "", NULL_PTR
, 1, type
);
1033 printf (" ret0: return %d;\n}\n\n", type
== SPLIT
? 0 : -1);
1036 /* This table is used to indent the recog_* functions when we are inside
1037 conditions or switch statements. We only support small indentations
1038 and always indent at least two spaces. */
1040 static char *indents
[]
1041 = {" ", " ", " ", " ", " ", " ", " ", " ",
1042 "\t", "\t ", "\t ", "\t ", "\t ", "\t ", "\t ",
1043 "\t\t", "\t\t ", "\t\t ", "\t\t ", "\t\t ", "\t\t "};
1045 /* Write out C code to perform the decisions in TREE for a subroutine of
1046 type TYPE. If all of the choices fail, branch to node AFTERWARD, if
1047 non-zero, otherwise return. PREVPOS is the position of the node that
1048 branched to this test.
1050 When we merged all alternatives, we tried to set up a convenient order.
1051 Specifically, tests involving the same mode are all grouped together,
1052 followed by a group that does not contain a mode test. Within each group
1053 of the same mode, we also group tests with the same code, followed by a
1054 group that does not test a code.
1056 Occasionally, we cannot arbitrarily reorder the tests so that multiple
1057 sequence of groups as described above are present.
1059 We generate two nested switch statements, the outer statement for
1060 testing modes, and the inner switch for testing RTX codes. It is
1061 not worth optimizing cases when only a small number of modes or
1062 codes is tested, since the compiler can do that when compiling the
1063 resulting function. We do check for when every test is the same mode
1067 write_tree_1 (tree
, prevpos
, afterward
, type
)
1068 struct decision
*tree
;
1070 struct decision
*afterward
;
1071 enum routine_type type
;
1073 register struct decision
*p
, *p1
;
1074 register int depth
= tree
? strlen (tree
->position
) : 0;
1075 enum machine_mode switch_mode
= VOIDmode
;
1076 RTX_CODE switch_code
= UNKNOWN
;
1078 char modemap
[NUM_MACHINE_MODES
];
1079 char codemap
[NUM_RTX_CODE
];
1083 /* One tricky area is what is the exact state when we branch to a
1084 node's label. There are two cases where we branch: when looking at
1085 successors to a node, or when a set of tests fails.
1087 In the former case, we are always branching to the first node in a
1088 decision list and we want all required tests to be performed. We
1089 put the labels for such nodes in front of any switch or test statements.
1090 These branches are done without updating the position to that of the
1093 In the latter case, we are branching to a node that is not the first
1094 node in a decision list. We have already checked that it is possible
1095 for both the node we originally tested at this level and the node we
1096 are branching to to both match some pattern. That means that they
1097 usually will be testing the same mode and code. So it is normally safe
1098 for such labels to be inside switch statements, since the tests done
1099 by virtue of arriving at that label will usually already have been
1100 done. The exception is a branch from a node that does not test a
1101 mode or code to one that does. In such cases, we set the `retest_mode'
1102 or `retest_code' flags. That will ensure that we start a new switch
1103 at that position and put the label before the switch.
1105 The branches in the latter case must set the position to that of the
1110 if (tree
&& tree
->subroutine_number
== 0)
1112 printf (" L%d:\n", tree
->number
);
1113 tree
->label_needed
= 0;
1118 change_state (prevpos
, tree
->position
, 2);
1119 prevpos
= tree
->position
;
1122 for (p
= tree
; p
; p
= p
->next
)
1124 enum machine_mode mode
= p
->enforce_mode
? p
->mode
: VOIDmode
;
1126 int wrote_bracket
= 0;
1129 if (p
->success
.first
== 0 && p
->insn_code_number
< 0)
1132 /* Find the next alternative to p that might be true when p is true.
1133 Test that one next if p's successors fail. */
1135 for (p1
= p
->next
; p1
&& not_both_true (p
, p1
, 1); p1
= p1
->next
)
1141 if (mode
== VOIDmode
&& p1
->enforce_mode
&& p1
->mode
!= VOIDmode
)
1142 p1
->retest_mode
= 1;
1143 if (p
->code
== UNKNOWN
&& p1
->code
!= UNKNOWN
)
1144 p1
->retest_code
= 1;
1145 p1
->label_needed
= 1;
1148 /* If we have a different code or mode than the last node and
1149 are in a switch on codes, we must either end the switch or
1150 go to another case. We must also end the switch if this
1151 node needs a label and to retest either the mode or code. */
1153 if (switch_code
!= UNKNOWN
1154 && (switch_code
!= p
->code
|| switch_mode
!= mode
1155 || (p
->label_needed
&& (p
->retest_mode
|| p
->retest_code
))))
1157 enum rtx_code code
= p
->code
;
1159 /* If P is testing a predicate that we know about and we haven't
1160 seen any of the codes that are valid for the predicate, we
1161 can write a series of "case" statement, one for each possible
1162 code. Since we are already in a switch, these redundant tests
1163 are very cheap and will reduce the number of predicate called. */
1167 for (i
= 0; i
< NUM_RTX_CODE
&& preds
[p
->pred
].codes
[i
] != 0; i
++)
1168 if (codemap
[(int) preds
[p
->pred
].codes
[i
]])
1171 if (preds
[p
->pred
].codes
[i
] == 0)
1172 code
= MATCH_OPERAND
;
1175 if (code
== UNKNOWN
|| codemap
[(int) code
]
1176 || switch_mode
!= mode
1177 || (p
->label_needed
&& (p
->retest_mode
|| p
->retest_code
)))
1179 printf ("%s}\n", indents
[indent
- 2]);
1180 switch_code
= UNKNOWN
;
1186 printf ("%sbreak;\n", indents
[indent
]);
1188 if (code
== MATCH_OPERAND
)
1190 for (i
= 0; i
< NUM_RTX_CODE
&& preds
[p
->pred
].codes
[i
] != 0; i
++)
1192 printf ("%scase ", indents
[indent
- 2]);
1193 print_code (preds
[p
->pred
].codes
[i
]);
1195 codemap
[(int) preds
[p
->pred
].codes
[i
]] = 1;
1200 printf ("%scase ", indents
[indent
- 2]);
1203 codemap
[(int) p
->code
] = 1;
1212 /* If we were previously in a switch on modes and now have a different
1213 mode, end at least the case, and maybe end the switch if we are
1214 not testing a mode or testing a mode whose case we already saw. */
1216 if (switch_mode
!= VOIDmode
1217 && (switch_mode
!= mode
|| (p
->label_needed
&& p
->retest_mode
)))
1219 if (mode
== VOIDmode
|| modemap
[(int) mode
]
1220 || (p
->label_needed
&& p
->retest_mode
))
1222 printf ("%s}\n", indents
[indent
- 2]);
1223 switch_mode
= VOIDmode
;
1229 printf (" break;\n");
1230 printf (" case %smode:\n", GET_MODE_NAME (mode
));
1232 modemap
[(int) mode
] = 1;
1238 /* If we are about to write dead code, something went wrong. */
1239 if (! p
->label_needed
&& uncond
)
1242 /* If we need a label and we will want to retest the mode or code at
1243 that label, write the label now. We have already ensured that
1244 things will be valid for the test. */
1246 if (p
->label_needed
&& (p
->retest_mode
|| p
->retest_code
))
1248 printf ("%sL%d:\n", indents
[indent
- 2], p
->number
);
1249 p
->label_needed
= 0;
1254 /* If we are not in any switches, see if we can shortcut things
1255 by checking for identical modes and codes. */
1257 if (switch_mode
== VOIDmode
&& switch_code
== UNKNOWN
)
1259 /* If p and its alternatives all want the same mode,
1260 reject all others at once, first, then ignore the mode. */
1262 if (mode
!= VOIDmode
&& p
->next
&& same_modes (p
, mode
))
1264 printf (" if (GET_MODE (x%d) != %smode)\n",
1265 depth
, GET_MODE_NAME (p
->mode
));
1269 change_state (p
->position
, afterward
->position
, 6);
1270 printf (" goto L%d;\n }\n", afterward
->number
);
1273 printf (" goto ret0;\n");
1278 /* If p and its alternatives all want the same code,
1279 reject all others at once, first, then ignore the code. */
1281 if (p
->code
!= UNKNOWN
&& p
->next
&& same_codes (p
, p
->code
))
1283 printf (" if (GET_CODE (x%d) != ", depth
);
1284 print_code (p
->code
);
1289 change_state (p
->position
, afterward
->position
, indent
+ 4);
1290 printf (" goto L%d;\n }\n", afterward
->number
);
1293 printf (" goto ret0;\n");
1298 /* If we are not in a mode switch and we are testing for a specific
1299 mode, start a mode switch unless we have just one node or the next
1300 node is not testing a mode (we have already tested for the case of
1301 more than one mode, but all of the same mode). */
1303 if (switch_mode
== VOIDmode
&& mode
!= VOIDmode
&& p
->next
!= 0
1304 && p
->next
->enforce_mode
&& p
->next
->mode
!= VOIDmode
)
1306 mybzero (modemap
, sizeof modemap
);
1307 printf ("%sswitch (GET_MODE (x%d))\n", indents
[indent
], depth
);
1308 printf ("%s{\n", indents
[indent
+ 2]);
1310 printf ("%sdefault:\n%sbreak;\n", indents
[indent
- 2],
1312 printf ("%scase %smode:\n", indents
[indent
- 2],
1313 GET_MODE_NAME (mode
));
1314 modemap
[(int) mode
] = 1;
1318 /* Similarly for testing codes. */
1320 if (switch_code
== UNKNOWN
&& p
->code
!= UNKNOWN
&& ! p
->ignore_code
1321 && p
->next
!= 0 && p
->next
->code
!= UNKNOWN
)
1323 mybzero (codemap
, sizeof codemap
);
1324 printf ("%sswitch (GET_CODE (x%d))\n", indents
[indent
], depth
);
1325 printf ("%s{\n", indents
[indent
+ 2]);
1327 printf ("%sdefault:\n%sbreak;\n", indents
[indent
- 2],
1329 printf ("%scase ", indents
[indent
- 2]);
1330 print_code (p
->code
);
1332 codemap
[(int) p
->code
] = 1;
1333 switch_code
= p
->code
;
1336 /* Now that most mode and code tests have been done, we can write out
1337 a label for an inner node, if we haven't already. */
1338 if (p
->label_needed
)
1339 printf ("%sL%d:\n", indents
[indent
- 2], p
->number
);
1341 inner_indent
= indent
;
1343 /* The only way we can have to do a mode or code test here is if
1344 this node needs such a test but is the only node to be tested.
1345 In that case, we won't have started a switch. Note that this is
1346 the only way the switch and test modes can disagree. */
1348 if ((mode
!= switch_mode
&& ! p
->ignore_mode
)
1349 || (p
->code
!= switch_code
&& p
->code
!= UNKNOWN
&& ! p
->ignore_code
)
1350 || p
->test_elt_zero_int
|| p
->test_elt_one_int
1351 || p
->test_elt_zero_wide
|| p
->veclen
1352 || p
->dupno
>= 0 || p
->tests
|| p
->num_clobbers_to_add
)
1354 printf ("%sif (", indents
[indent
]);
1356 if (mode
!= switch_mode
&& ! p
->ignore_mode
)
1357 printf ("GET_MODE (x%d) == %smode && ",
1358 depth
, GET_MODE_NAME (mode
));
1359 if (p
->code
!= switch_code
&& p
->code
!= UNKNOWN
&& ! p
->ignore_code
)
1361 printf ("GET_CODE (x%d) == ", depth
);
1362 print_code (p
->code
);
1366 if (p
->test_elt_zero_int
)
1367 printf ("XINT (x%d, 0) == %d && ", depth
, p
->elt_zero_int
);
1368 if (p
->test_elt_one_int
)
1369 printf ("XINT (x%d, 1) == %d && ", depth
, p
->elt_one_int
);
1370 if (p
->test_elt_zero_wide
)
1372 /* Set offset to 1 iff the number might get propagated to
1373 unsigned long by ANSI C rules, else 0.
1374 Prospective hosts are required to have at least 32 bit
1375 ints, and integer constants in machine descriptions
1376 must fit in 32 bit, thus it suffices to check only
1378 HOST_WIDE_INT offset
= p
->elt_zero_wide
== -2147483647 - 1;
1379 printf ("XWINT (x%d, 0) == ", depth
);
1380 printf (HOST_WIDE_INT_PRINT_DEC
, p
->elt_zero_wide
+ offset
);
1381 printf ("%s && ", offset
? "-1" : "");
1384 printf ("XVECLEN (x%d, 0) == %d && ", depth
, p
->veclen
);
1386 printf ("rtx_equal_p (x%d, ro[%d]) && ", depth
, p
->dupno
);
1387 if (p
->num_clobbers_to_add
)
1388 printf ("pnum_clobbers != 0 && ");
1390 printf ("%s (x%d, %smode)", p
->tests
, depth
,
1391 GET_MODE_NAME (p
->mode
));
1401 need_bracket
= ! uncond
;
1407 printf ("%s{\n", indents
[inner_indent
]);
1413 printf ("%sro[%d] = x%d;\n", indents
[inner_indent
], p
->opno
, depth
);
1418 printf ("%sif (%s)\n", indents
[inner_indent
], p
->c_test
);
1424 if (p
->insn_code_number
>= 0)
1427 printf ("%sreturn gen_split_%d (operands);\n",
1428 indents
[inner_indent
], p
->insn_code_number
);
1431 if (p
->num_clobbers_to_add
)
1435 printf ("%s{\n", indents
[inner_indent
]);
1439 printf ("%s*pnum_clobbers = %d;\n",
1440 indents
[inner_indent
], p
->num_clobbers_to_add
);
1441 printf ("%sreturn %d;\n",
1442 indents
[inner_indent
], p
->insn_code_number
);
1447 printf ("%s}\n", indents
[inner_indent
]);
1451 printf ("%sreturn %d;\n",
1452 indents
[inner_indent
], p
->insn_code_number
);
1456 printf ("%sgoto L%d;\n", indents
[inner_indent
],
1457 p
->success
.first
->number
);
1460 printf ("%s}\n", indents
[inner_indent
- 2]);
1463 /* We have now tested all alternatives. End any switches we have open
1464 and branch to the alternative node unless we know that we can't fall
1465 through to the branch. */
1467 if (switch_code
!= UNKNOWN
)
1469 printf ("%s}\n", indents
[indent
- 2]);
1474 if (switch_mode
!= VOIDmode
)
1476 printf ("%s}\n", indents
[indent
- 2]);
1489 change_state (prevpos
, afterward
->position
, 2);
1490 printf (" goto L%d;\n", afterward
->number
);
1493 printf (" goto ret0;\n");
1501 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
1503 if (*p1
>= 'a' && *p1
<= 'z')
1504 putchar (*p1
+ 'A' - 'a');
1511 same_codes (p
, code
)
1512 register struct decision
*p
;
1513 register enum rtx_code code
;
1515 for (; p
; p
= p
->next
)
1516 if (p
->code
!= code
)
1524 register struct decision
*p
;
1526 for (; p
; p
= p
->next
)
1531 same_modes (p
, mode
)
1532 register struct decision
*p
;
1533 register enum machine_mode mode
;
1535 for (; p
; p
= p
->next
)
1536 if ((p
->enforce_mode
? p
->mode
: VOIDmode
) != mode
)
1544 register struct decision
*p
;
1546 for (; p
; p
= p
->next
)
1547 p
->enforce_mode
= 0;
1550 /* Write out the decision tree starting at TREE for a subroutine of type TYPE.
1552 PREVPOS is the position at the node that branched to this node.
1554 INITIAL is nonzero if this is the first node we are writing in a subroutine.
1556 If all nodes are false, branch to the node AFTERWARD. */
1559 write_tree (tree
, prevpos
, afterward
, initial
, type
)
1560 struct decision
*tree
;
1562 struct decision
*afterward
;
1564 enum routine_type type
;
1566 register struct decision
*p
;
1567 char *name_prefix
= (type
== SPLIT
? "split" : "recog");
1568 char *call_suffix
= (type
== SPLIT
? "" : ", pnum_clobbers");
1570 if (! initial
&& tree
->subroutine_number
> 0)
1572 printf (" L%d:\n", tree
->number
);
1576 printf (" tem = %s_%d (x0, insn%s);\n",
1577 name_prefix
, tree
->subroutine_number
, call_suffix
);
1579 printf (" if (tem != 0) return tem;\n");
1581 printf (" if (tem >= 0) return tem;\n");
1582 change_state (tree
->position
, afterward
->position
, 2);
1583 printf (" goto L%d;\n", afterward
->number
);
1586 printf (" return %s_%d (x0, insn%s);\n",
1587 name_prefix
, tree
->subroutine_number
, call_suffix
);
1591 write_tree_1 (tree
, prevpos
, afterward
, type
);
1593 for (p
= tree
; p
; p
= p
->next
)
1594 if (p
->success
.first
)
1595 write_tree (p
->success
.first
, p
->position
,
1596 p
->afterward
? p
->afterward
: afterward
, 0, type
);
1600 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1601 actions are necessary to move to NEWPOS.
1603 INDENT says how many blanks to place at the front of lines. */
1606 change_state (oldpos
, newpos
, indent
)
1611 int odepth
= strlen (oldpos
);
1613 int ndepth
= strlen (newpos
);
1615 /* Pop up as many levels as necessary. */
1617 while (strncmp (oldpos
, newpos
, depth
))
1620 /* Go down to desired level. */
1622 while (depth
< ndepth
)
1624 if (newpos
[depth
] >= 'a' && newpos
[depth
] <= 'z')
1625 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1626 indents
[indent
], depth
+ 1, depth
, newpos
[depth
] - 'a');
1628 printf ("%sx%d = XEXP (x%d, %c);\n",
1629 indents
[indent
], depth
+ 1, depth
, newpos
[depth
]);
1643 tem
= (char *) xmalloc (strlen (s1
) + 1);
1652 register unsigned length
;
1654 while (length
-- > 0)
1659 mybcopy (in
, out
, length
)
1660 register char *in
, *out
;
1661 register unsigned length
;
1663 while (length
-- > 0)
1668 xrealloc (ptr
, size
)
1672 char *result
= (char *) realloc (ptr
, size
);
1674 fatal ("virtual memory exhausted");
1682 register char *val
= (char *) malloc (size
);
1685 fatal ("virtual memory exhausted");
1690 fatal
VPROTO ((char *format
, ...))
1697 VA_START (ap
, format
);
1700 format
= va_arg (ap
, char *);
1703 fprintf (stderr
, "genrecog: ");
1704 vfprintf (stderr
, format
, ap
);
1706 fprintf (stderr
, "\n");
1707 fprintf (stderr
, "after %d definitions\n", next_index
);
1708 exit (FATAL_EXIT_CODE
);
1711 /* More 'friendly' abort that prints the line and file.
1712 config.h can #define abort fancy_abort if you like that sort of thing. */
1717 fatal ("Internal gcc abort.");
1726 struct decision_head recog_tree
;
1727 struct decision_head split_tree
;
1731 obstack_init (rtl_obstack
);
1732 recog_tree
.first
= recog_tree
.last
= split_tree
.first
= split_tree
.last
= 0;
1735 fatal ("No input file name.");
1737 infile
= fopen (argv
[1], "r");
1741 exit (FATAL_EXIT_CODE
);
1748 printf ("/* Generated automatically by the program `genrecog'\n\
1749 from the machine description file `md'. */\n\n");
1751 printf ("#include \"config.h\"\n");
1752 printf ("#include \"system.h\"\n");
1753 printf ("#include \"rtl.h\"\n");
1754 printf ("#include \"insn-config.h\"\n");
1755 printf ("#include \"recog.h\"\n");
1756 printf ("#include \"real.h\"\n");
1757 printf ("#include \"output.h\"\n");
1758 printf ("#include \"flags.h\"\n");
1761 /* Read the machine description. */
1765 c
= read_skip_spaces (infile
);
1770 desc
= read_rtx (infile
);
1771 if (GET_CODE (desc
) == DEFINE_INSN
)
1772 recog_tree
= merge_trees (recog_tree
,
1773 make_insn_sequence (desc
, RECOG
));
1774 else if (GET_CODE (desc
) == DEFINE_SPLIT
)
1775 split_tree
= merge_trees (split_tree
,
1776 make_insn_sequence (desc
, SPLIT
));
1777 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
1778 || GET_CODE (desc
) == DEFINE_EXPAND
)
1784 /* `recog' contains a decision tree\n\
1785 that recognizes whether the rtx X0 is a valid instruction.\n\
1787 recog returns -1 if the rtx is not valid.\n\
1788 If the rtx is valid, recog returns a nonnegative number\n\
1789 which is the insn code number for the pattern that matched.\n");
1790 printf (" This is the same as the order in the machine description of\n\
1791 the entry that matched. This number can be used as an index into various\n\
1792 insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands\n\
1793 (found in insn-output.c).\n\n");
1794 printf (" The third argument to recog is an optional pointer to an int.\n\
1795 If present, recog will accept a pattern if it matches except for\n\
1796 missing CLOBBER expressions at the end. In that case, the value\n\
1797 pointed to by the optional pointer will be set to the number of\n\
1798 CLOBBERs that need to be added (it should be initialized to zero by\n\
1799 the caller). If it is set nonzero, the caller should allocate a\n\
1800 PARALLEL of the appropriate size, copy the initial entries, and call\n\
1801 add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
1803 if (split_tree
.first
)
1804 printf ("\n\n The function split_insns returns 0 if the rtl could not\n\
1805 be split or the split rtl in a SEQUENCE if it can be.");
1809 printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
1810 printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
1811 printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
1812 printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
1813 printf ("#define operands recog_operand\n\n");
1815 next_subroutine_number
= 0;
1816 break_out_subroutines (recog_tree
, RECOG
, 1);
1817 write_subroutine (recog_tree
.first
, RECOG
);
1819 next_subroutine_number
= 0;
1820 break_out_subroutines (split_tree
, SPLIT
, 1);
1821 write_subroutine (split_tree
.first
, SPLIT
);
1824 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);