]> gcc.gnu.org Git - gcc.git/blame - gcc/genrecog.c
rtl.c (dump_and_abort): Remove.
[gcc.git] / gcc / genrecog.c
CommitLineData
ec65fa66 1/* Generate code from machine description to recognize rtl as insns.
efd59a33 2 Copyright (C) 1987, 88, 92-95, 97-98, 1999 Free Software Foundation, Inc.
ec65fa66 3
09051660
RH
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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. */
20
21
22/* This program is used to produce insn-recog.c, which contains a
23 function called `recog' plus its subroutines. These functions
24 contain a decision tree that recognizes whether an rtx, the
25 argument given to recog, is a valid instruction.
26
27 recog returns -1 if the rtx is not valid. If the rtx is valid,
28 recog returns a nonnegative number which is the insn code number
29 for the pattern that matched. This is the same as the order in the
30 machine description of the entry that matched. This number can be
31 used as an index into various insn_* tables, such as insn_template,
32 insn_outfun, and insn_n_operands (found in insn-output.c).
33
34 The third argument to recog is an optional pointer to an int. If
35 present, recog will accept a pattern if it matches except for
ec65fa66
RK
36 missing CLOBBER expressions at the end. In that case, the value
37 pointed to by the optional pointer will be set to the number of
38 CLOBBERs that need to be added (it should be initialized to zero by
39 the caller). If it is set nonzero, the caller should allocate a
09051660
RH
40 PARALLEL of the appropriate size, copy the initial entries, and
41 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
ec65fa66 42
09051660
RH
43 This program also generates the function `split_insns', which
44 returns 0 if the rtl could not be split, or it returns the split
45 rtl in a SEQUENCE.
46
47 This program also generates the function `peephole2_insns', which
48 returns 0 if the rtl could not be matched. If there was a match,
49 the new rtl is returned in a SEQUENCE, and LAST_INSN will point
50 to the last recognized insn in the old sequence. */
ec65fa66 51
20f92396 52#include "hconfig.h"
0b93b64e 53#include "system.h"
ec65fa66
RK
54#include "rtl.h"
55#include "obstack.h"
f8b6598e 56#include "errors.h"
ec65fa66 57
736b02fd
KG
58#define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
59 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
60
ec65fa66
RK
61static struct obstack obstack;
62struct obstack *rtl_obstack = &obstack;
63
64#define obstack_chunk_alloc xmalloc
65#define obstack_chunk_free free
66
8aeba909 67/* Holds an array of names indexed by insn_code_number. */
a995e389
RH
68static char **insn_name_ptr = 0;
69static int insn_name_ptr_size = 0;
4db83042 70
09051660
RH
71/* A listhead of decision trees. The alternatives to a node are kept
72 in a doublely-linked list so we can easily add nodes to the proper
73 place when merging. */
74
75struct decision_head
76{
77 struct decision *first;
78 struct decision *last;
79};
80
81/* A single test. The two accept types aren't tests per-se, but
82 their equality (or lack thereof) does affect tree merging so
83 it is convenient to keep them here. */
84
85struct decision_test
86{
87 /* A linked list through the tests attached to a node. */
88 struct decision_test *next;
89
90 /* These types are roughly in the order in which we'd like to test them. */
91 enum decision_type {
92 DT_mode, DT_code, DT_veclen,
93 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide,
94 DT_dup, DT_pred, DT_c_test,
95 DT_accept_op, DT_accept_insn
96 } type;
97
98 union
99 {
100 enum machine_mode mode; /* Machine mode of node. */
101 RTX_CODE code; /* Code to test. */
e0689256 102
09051660
RH
103 struct
104 {
105 const char *name; /* Predicate to call. */
106 int index; /* Index into `preds' or -1. */
107 enum machine_mode mode; /* Machine mode for node. */
108 } pred;
109
110 const char *c_test; /* Additional test to perform. */
111 int veclen; /* Length of vector. */
112 int dup; /* Number of operand to compare against. */
113 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
114 int opno; /* Operand number matched. */
115
116 struct {
117 int code_number; /* Insn number matched. */
bcdaba58 118 int lineno; /* Line number of the insn. */
09051660
RH
119 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
120 } insn;
121 } u;
122};
e0689256 123
09051660 124/* Data structure for decision tree for recognizing legitimate insns. */
ec65fa66
RK
125
126struct decision
127{
09051660
RH
128 struct decision_head success; /* Nodes to test on success. */
129 struct decision *next; /* Node to test on failure. */
130 struct decision *prev; /* Node whose failure tests us. */
131 struct decision *afterward; /* Node to test on success,
132 but failure of successor nodes. */
133
134 const char *position; /* String denoting position in pattern. */
135
136 struct decision_test *tests; /* The tests for this node. */
137
e0689256 138 int number; /* Node number, used for labels */
e0689256 139 int subroutine_number; /* Number of subroutine this node starts */
09051660 140 int need_label; /* Label needs to be output. */
ec65fa66
RK
141};
142
09051660 143#define SUBROUTINE_THRESHOLD 100
ec65fa66
RK
144
145static int next_subroutine_number;
146
ede7cd44
RH
147/* We can write three types of subroutines: One for insn recognition,
148 one to split insns, and one for peephole-type optimizations. This
149 defines which type is being written. */
ec65fa66 150
09051660
RH
151enum routine_type {
152 RECOG, SPLIT, PEEPHOLE2
153};
ede7cd44 154
09051660 155#define IS_SPLIT(X) ((X) != RECOG)
ec65fa66 156
e0689256 157/* Next available node number for tree nodes. */
ec65fa66 158
e0689256 159static int next_number;
ec65fa66 160
e0689256 161/* Next number to use as an insn_code. */
ec65fa66 162
e0689256 163static int next_insn_code;
ec65fa66 164
e0689256 165/* Similar, but counts all expressions in the MD file; used for
0f41302f 166 error messages. */
ec65fa66 167
e0689256 168static int next_index;
ec65fa66 169
e0689256
RK
170/* Record the highest depth we ever have so we know how many variables to
171 allocate in each subroutine we make. */
ec65fa66 172
e0689256 173static int max_depth;
bcdaba58
RH
174
175/* The line number of the start of the pattern currently being processed. */
176static int pattern_lineno;
177
178/* Count of errors. */
179static int error_count;
e0689256
RK
180\f
181/* This table contains a list of the rtl codes that can possibly match a
09051660 182 predicate defined in recog.c. The function `maybe_both_true' uses it to
e0689256
RK
183 deduce that there are no expressions that can be matches by certain pairs
184 of tree nodes. Also, if a predicate can match only one code, we can
185 hardwire that code into the node testing the predicate. */
ec65fa66 186
e0689256
RK
187static struct pred_table
188{
85fda1eb 189 const char *name;
e0689256 190 RTX_CODE codes[NUM_RTX_CODE];
09051660
RH
191} preds[] = {
192 {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
193 LABEL_REF, SUBREG, REG, MEM}},
e0689256 194#ifdef PREDICATE_CODES
09051660 195 PREDICATE_CODES
e0689256 196#endif
09051660
RH
197 {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
198 LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
199 {"register_operand", {SUBREG, REG}},
200 {"scratch_operand", {SCRATCH, REG}},
201 {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
202 LABEL_REF}},
203 {"const_int_operand", {CONST_INT}},
204 {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
205 {"nonimmediate_operand", {SUBREG, REG, MEM}},
206 {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
207 LABEL_REF, SUBREG, REG}},
208 {"push_operand", {MEM}},
209 {"pop_operand", {MEM}},
210 {"memory_operand", {SUBREG, MEM}},
211 {"indirect_operand", {SUBREG, MEM}},
212 {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU}},
213 {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
214 LABEL_REF, SUBREG, REG, MEM}}
215};
e0689256
RK
216
217#define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
ec65fa66 218
09051660
RH
219static struct decision *new_decision
220 PROTO((const char *, struct decision_head *));
221static struct decision_test *new_decision_test
222 PROTO((enum decision_type, struct decision_test ***));
bcdaba58
RH
223static void validate_pattern
224 PROTO((rtx, int));
09051660
RH
225static struct decision *add_to_sequence
226 PROTO((rtx, struct decision_head *, const char *, enum routine_type, int));
227
228static int maybe_both_true_2
229 PROTO((struct decision_test *, struct decision_test *));
230static int maybe_both_true_1
231 PROTO((struct decision_test *, struct decision_test *));
232static int maybe_both_true
233 PROTO((struct decision *, struct decision *, int));
234
235static int nodes_identical_1
236 PROTO((struct decision_test *, struct decision_test *));
237static int nodes_identical
238 PROTO((struct decision *, struct decision *));
239static void merge_accept_insn
240 PROTO((struct decision *, struct decision *));
241static void merge_trees
242 PROTO((struct decision_head *, struct decision_head *));
243
244static void factor_tests
245 PROTO((struct decision_head *));
246static void simplify_tests
247 PROTO((struct decision_head *));
248static int break_out_subroutines
249 PROTO((struct decision_head *, int));
250static void find_afterward
251 PROTO((struct decision_head *, struct decision *));
252
253static void change_state
254 PROTO((const char *, const char *, struct decision *, const char *));
255static void print_code
256 PROTO((enum rtx_code));
257static void write_afterward
258 PROTO((struct decision *, struct decision *, const char *));
259static struct decision *write_switch
260 PROTO((struct decision *, int));
261static void write_cond
262 PROTO((struct decision_test *, int, enum routine_type));
263static void write_action
264 PROTO((struct decision_test *, int, int, struct decision *,
265 enum routine_type));
266static int is_unconditional
267 PROTO((struct decision_test *, enum routine_type));
268static int write_node
269 PROTO((struct decision *, int, enum routine_type));
270static void write_tree_1
271 PROTO((struct decision_head *, int, enum routine_type));
272static void write_tree
273 PROTO((struct decision_head *, const char *, enum routine_type, int));
274static void write_subroutine
275 PROTO((struct decision_head *, enum routine_type));
276static void write_subroutines
277 PROTO((struct decision_head *, enum routine_type));
278static void write_header
279 PROTO((void));
280
281static struct decision_head make_insn_sequence
282 PROTO((rtx, enum routine_type));
283static void process_tree
284 PROTO((struct decision_head *, enum routine_type));
285
286static void record_insn_name
287 PROTO((int, const char *));
288
289static void debug_decision_1
290 PROTO((struct decision *, int));
291static void debug_decision_2
292 PROTO((struct decision_test *));
293extern void debug_decision
294 PROTO((struct decision *));
ede7cd44 295\f
bcdaba58
RH
296static void
297message_with_line VPROTO ((int lineno, const char *msg, ...))
298{
299#ifndef ANSI_PROTOTYPES
300 int lineno;
301 const char *msg;
302#endif
303 va_list ap;
304
305 VA_START (ap, msg);
306
307#ifndef ANSI_PROTOTYPES
308 lineno = va_arg (ap, int);
309 msg = va_arg (ap, const char *);
310#endif
311
312 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
313 vfprintf (stderr, msg, ap);
314 fputc ('\n', stderr);
315
316 va_end (ap);
317}
318\f
09051660 319/* Create a new node in sequence after LAST. */
e0689256 320
09051660
RH
321static struct decision *
322new_decision (position, last)
323 const char *position;
324 struct decision_head *last;
ec65fa66 325{
09051660
RH
326 register struct decision *new
327 = (struct decision *) xmalloc (sizeof (struct decision));
ec65fa66 328
09051660
RH
329 memset (new, 0, sizeof (*new));
330 new->success = *last;
331 new->position = xstrdup (position);
332 new->number = next_number++;
ec65fa66 333
09051660
RH
334 last->first = last->last = new;
335 return new;
336}
e0689256 337
09051660 338/* Create a new test and link it in at PLACE. */
ec65fa66 339
09051660
RH
340static struct decision_test *
341new_decision_test (type, pplace)
342 enum decision_type type;
343 struct decision_test ***pplace;
344{
345 struct decision_test **place = *pplace;
346 struct decision_test *test;
ec65fa66 347
09051660
RH
348 test = (struct decision_test *) xmalloc (sizeof (*test));
349 test->next = *place;
350 test->type = type;
351 *place = test;
ede7cd44 352
09051660
RH
353 place = &test->next;
354 *pplace = place;
ec65fa66 355
09051660 356 return test;
e0689256 357}
09051660 358
bcdaba58
RH
359/* Check for various errors in patterns. */
360
361static void
362validate_pattern (pattern, set_dest)
363 rtx pattern;
364 int set_dest;
365{
366 const char *fmt;
367 RTX_CODE code;
368 int i, j, len;
369
370 code = GET_CODE (pattern);
371 switch (code)
372 {
373 case MATCH_SCRATCH:
374 case MATCH_INSN:
375 return;
376
377 case MATCH_OPERAND:
378 {
379 const char *pred_name = XSTR (pattern, 1);
380
381 if (pred_name[0] != 0)
382 {
383 /* See if we know about this predicate and save its number. If
384 we do, and it only accepts one code, note that fact. The
385 predicate `const_int_operand' only tests for a CONST_INT, so
386 if we do so we can avoid calling it at all.
387
388 Finally, if we know that the predicate does not allow
389 CONST_INT, we know that the only way the predicate can match
390 is if the modes match (here we use the kludge of relying on
391 the fact that "address_operand" accepts CONST_INT; otherwise,
392 it would have to be a special case), so we can test the mode
393 (but we need not). This fact should considerably simplify the
394 generated code. */
395
396 for (i = 0; i < (int) NUM_KNOWN_PREDS; i++)
397 if (! strcmp (preds[i].name, pred_name))
398 break;
399
400 if (i < (int) NUM_KNOWN_PREDS)
401 {
402 int j, allows_const_int;
403
404 allows_const_int = 0;
405 for (j = 0; preds[i].codes[j] != 0; j++)
406 if (preds[i].codes[j] == CONST_INT)
407 {
408 allows_const_int = 1;
409 break;
410 }
411
412 if (allows_const_int && set_dest)
413 {
414 message_with_line (pattern_lineno,
415 "warning: `%s' accepts const_int,",
416 pred_name);
417 message_with_line (pattern_lineno,
418 " and used as destination of a set");
419 }
420 }
421 else
422 {
423#ifdef PREDICATE_CODES
424 /* If the port has a list of the predicates it uses but
425 omits one, warn. */
426 message_with_line (pattern_lineno, "warning: `%s' not in PREDICATE_CODES", pred_name);
427#endif
428 }
429 }
430
431 return;
432 }
433
434 case SET:
435 /* The operands of a SET must have the same mode unless one
436 is VOIDmode. */
437 if (GET_MODE (SET_SRC (pattern)) != VOIDmode
438 && GET_MODE (SET_DEST (pattern)) != VOIDmode
439 && GET_MODE (SET_SRC (pattern)) != GET_MODE (SET_DEST (pattern))
440 /* The mode of an ADDRESS_OPERAND is the mode of the memory
441 reference, not the mode of the address. */
442 && ! (GET_CODE (SET_SRC (pattern)) == MATCH_OPERAND
443 && ! strcmp (XSTR (SET_SRC (pattern), 1), "address_operand")))
444 {
445 message_with_line (pattern_lineno,
446 "mode mismatch in set: %smode vs %smode",
447 GET_MODE_NAME (GET_MODE (SET_DEST (pattern))),
448 GET_MODE_NAME (GET_MODE (SET_SRC (pattern))));
449 error_count++;
450 }
451
452 validate_pattern (SET_DEST (pattern), 1);
453 validate_pattern (SET_SRC (pattern), 0);
454 return;
455
456 case LABEL_REF:
457 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
458 {
459 message_with_line (pattern_lineno,
460 "operand to label_ref %smode not VOIDmode",
461 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
462 error_count++;
463 }
464 break;
465
466 default:
467 break;
468 }
469
470 fmt = GET_RTX_FORMAT (code);
471 len = GET_RTX_LENGTH (code);
472 for (i = 0; i < len; i++)
473 {
474 switch (fmt[i])
475 {
476 case 'e': case 'u':
477 validate_pattern (XEXP (pattern, i), 0);
478 break;
479
480 case 'E':
481 for (j = 0; j < XVECLEN (pattern, i); j++)
482 validate_pattern (XVECEXP (pattern, i, j), 0);
483 break;
484
485 case 'i': case 'w': case '0': case 's':
486 break;
487
488 default:
489 abort ();
490 }
491 }
492
493}
494
e0689256
RK
495/* Create a chain of nodes to verify that an rtl expression matches
496 PATTERN.
ec65fa66 497
e0689256
RK
498 LAST is a pointer to the listhead in the previous node in the chain (or
499 in the calling function, for the first node).
ec65fa66 500
e0689256 501 POSITION is the string representing the current position in the insn.
ec65fa66 502
ede7cd44
RH
503 INSN_TYPE is the type of insn for which we are emitting code.
504
e0689256 505 A pointer to the final node in the chain is returned. */
ec65fa66
RK
506
507static struct decision *
ede7cd44 508add_to_sequence (pattern, last, position, insn_type, top)
ec65fa66 509 rtx pattern;
e0689256 510 struct decision_head *last;
85fda1eb 511 const char *position;
ede7cd44
RH
512 enum routine_type insn_type;
513 int top;
ec65fa66 514{
09051660
RH
515 RTX_CODE code;
516 struct decision *this, *sub;
517 struct decision_test *test;
518 struct decision_test **place;
519 char *subpos;
85066503 520 register size_t i;
09051660 521 register const char *fmt;
e0689256 522 int depth = strlen (position);
ec65fa66 523 int len;
09051660 524 enum machine_mode mode;
ec65fa66 525
e0689256
RK
526 if (depth > max_depth)
527 max_depth = depth;
528
09051660
RH
529 subpos = (char *) alloca (depth + 2);
530 strcpy (subpos, position);
531 subpos[depth + 1] = 0;
ec65fa66 532
09051660
RH
533 sub = this = new_decision (position, last);
534 place = &this->tests;
ec65fa66
RK
535
536 restart:
09051660
RH
537 mode = GET_MODE (pattern);
538 code = GET_CODE (pattern);
ec65fa66
RK
539
540 switch (code)
541 {
ede7cd44
RH
542 case PARALLEL:
543 /* Toplevel peephole pattern. */
544 if (insn_type == PEEPHOLE2 && top)
545 {
09051660
RH
546 /* We don't need the node we just created -- unlink it. */
547 last->first = last->last = NULL;
ede7cd44
RH
548
549 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
550 {
551 /* Which insn we're looking at is represented by A-Z. We don't
552 ever use 'A', however; it is always implied. */
09051660
RH
553
554 subpos[depth] = (i > 0 ? 'A' + i : 0);
555 sub = add_to_sequence (XVECEXP (pattern, 0, i),
556 last, subpos, insn_type, 0);
557 last = &sub->success;
ede7cd44 558 }
09051660 559 return sub;
ede7cd44 560 }
09051660
RH
561
562 /* Else nothing special. */
ede7cd44 563 break;
09051660 564
ec65fa66 565 case MATCH_OPERAND:
ec65fa66 566 case MATCH_SCRATCH:
ec65fa66 567 case MATCH_OPERATOR:
ec65fa66 568 case MATCH_PARALLEL:
5126c35a 569 case MATCH_INSN:
09051660
RH
570 {
571 const char *pred_name;
572 RTX_CODE was_code = code;
ec1c89e6 573 int allows_const_int = 1;
09051660
RH
574
575 if (code == MATCH_SCRATCH)
576 {
577 pred_name = "scratch_operand";
578 code = UNKNOWN;
579 }
580 else
581 {
582 pred_name = XSTR (pattern, 1);
583 if (code == MATCH_PARALLEL)
584 code = PARALLEL;
585 else
586 code = UNKNOWN;
587 }
588
589 /* We know exactly what const_int_operand matches -- any CONST_INT. */
590 if (strcmp ("const_int_operand", pred_name) == 0)
591 {
592 code = CONST_INT;
593 mode = VOIDmode;
594 }
595 else if (pred_name[0] != 0)
596 {
597 test = new_decision_test (DT_pred, &place);
598 test->u.pred.name = pred_name;
599 test->u.pred.mode = mode;
600
601 /* See if we know about this predicate and save its number. If
602 we do, and it only accepts one code, note that fact. The
603 predicate `const_int_operand' only tests for a CONST_INT, so
604 if we do so we can avoid calling it at all.
605
606 Finally, if we know that the predicate does not allow
607 CONST_INT, we know that the only way the predicate can match
608 is if the modes match (here we use the kludge of relying on
609 the fact that "address_operand" accepts CONST_INT; otherwise,
610 it would have to be a special case), so we can test the mode
611 (but we need not). This fact should considerably simplify the
612 generated code. */
613
614 for (i = 0; i < NUM_KNOWN_PREDS; i++)
615 if (! strcmp (preds[i].name, pred_name))
616 break;
e0689256 617
09051660 618 if (i < NUM_KNOWN_PREDS)
9edd4689 619 {
c3693cb1 620 int j;
e0689256 621
09051660 622 test->u.pred.index = i;
e0689256 623
09051660
RH
624 if (preds[i].codes[1] == 0 && code == UNKNOWN)
625 code = preds[i].codes[0];
e0689256 626
09051660
RH
627 allows_const_int = 0;
628 for (j = 0; preds[i].codes[j] != 0; j++)
9edd4689 629 if (preds[i].codes[j] == CONST_INT)
09051660
RH
630 {
631 allows_const_int = 1;
632 break;
633 }
9edd4689 634 }
09051660 635 else
bcdaba58 636 test->u.pred.index = -1;
09051660 637 }
ec1c89e6
RH
638
639 /* Can't enforce a mode if we allow const_int. */
640 if (allows_const_int)
641 mode = VOIDmode;
e0689256 642
09051660
RH
643 /* Accept the operand, ie. record it in `operands'. */
644 test = new_decision_test (DT_accept_op, &place);
645 test->u.opno = XINT (pattern, 0);
e0689256 646
09051660
RH
647 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
648 {
649 char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
650 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
651 {
652 subpos[depth] = i + base;
653 sub = add_to_sequence (XVECEXP (pattern, 2, i),
654 &sub->success, subpos, insn_type, 0);
655 }
656 }
657 goto fini;
658 }
ec65fa66
RK
659
660 case MATCH_OP_DUP:
09051660
RH
661 code = UNKNOWN;
662
663 test = new_decision_test (DT_dup, &place);
664 test->u.dup = XINT (pattern, 0);
665
666 test = new_decision_test (DT_accept_op, &place);
667 test->u.opno = XINT (pattern, 0);
668
e51712db 669 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
ec65fa66 670 {
09051660
RH
671 subpos[depth] = i + '0';
672 sub = add_to_sequence (XVECEXP (pattern, 1, i),
673 &sub->success, subpos, insn_type, 0);
ec65fa66 674 }
09051660 675 goto fini;
ec65fa66
RK
676
677 case MATCH_DUP:
f582c9d5 678 case MATCH_PAR_DUP:
09051660
RH
679 code = UNKNOWN;
680
681 test = new_decision_test (DT_dup, &place);
682 test->u.dup = XINT (pattern, 0);
683 goto fini;
ec65fa66
RK
684
685 case ADDRESS:
686 pattern = XEXP (pattern, 0);
687 goto restart;
688
76d31c63
JL
689 default:
690 break;
ec65fa66
RK
691 }
692
693 fmt = GET_RTX_FORMAT (code);
694 len = GET_RTX_LENGTH (code);
09051660
RH
695
696 /* Do tests against the current node first. */
e51712db 697 for (i = 0; i < (size_t) len; i++)
ec65fa66 698 {
09051660 699 if (fmt[i] == 'i')
ec65fa66 700 {
09051660
RH
701 if (i == 0)
702 {
703 test = new_decision_test (DT_elt_zero_int, &place);
704 test->u.intval = XINT (pattern, i);
705 }
706 else if (i == 1)
707 {
708 test = new_decision_test (DT_elt_one_int, &place);
709 test->u.intval = XINT (pattern, i);
710 }
711 else
712 abort ();
ec65fa66 713 }
09051660 714 else if (fmt[i] == 'w')
3d678dca 715 {
09051660
RH
716 if (i != 0)
717 abort ();
718
719 test = new_decision_test (DT_elt_zero_wide, &place);
720 test->u.intval = XWINT (pattern, i);
3d678dca 721 }
ec65fa66
RK
722 else if (fmt[i] == 'E')
723 {
ec65fa66
RK
724 if (i != 0)
725 abort ();
09051660
RH
726
727 test = new_decision_test (DT_veclen, &place);
728 test->u.veclen = XVECLEN (pattern, i);
729 }
730 }
731
732 /* Now test our sub-patterns. */
733 for (i = 0; i < (size_t) len; i++)
734 {
735 switch (fmt[i])
736 {
737 case 'e': case 'u':
738 subpos[depth] = '0' + i;
739 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
740 subpos, insn_type, 0);
741 break;
742
743 case 'E':
744 {
745 register int j;
746 for (j = 0; j < XVECLEN (pattern, i); j++)
747 {
748 subpos[depth] = 'a' + j;
749 sub = add_to_sequence (XVECEXP (pattern, i, j),
750 &sub->success, subpos, insn_type, 0);
751 }
752 break;
753 }
754
755 case 'i': case 'w':
756 /* Handled above. */
757 break;
758 case '0':
759 break;
760
761 default:
762 abort ();
763 }
764 }
765
766 fini:
767 /* Insert nodes testing mode and code, if they're still relevant,
768 before any of the nodes we may have added above. */
769 if (code != UNKNOWN)
770 {
771 place = &this->tests;
772 test = new_decision_test (DT_code, &place);
773 test->u.code = code;
774 }
775
776 if (mode != VOIDmode)
777 {
778 place = &this->tests;
779 test = new_decision_test (DT_mode, &place);
780 test->u.mode = mode;
781 }
782
783 /* If we didn't insert any tests or accept nodes, hork. */
784 if (this->tests == NULL)
785 abort ();
786
787 return sub;
788}
789\f
790/* A subroutine of maybe_both_true; examines only one test.
791 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
792
793static int
794maybe_both_true_2 (d1, d2)
795 struct decision_test *d1, *d2;
796{
797 if (d1->type == d2->type)
798 {
799 switch (d1->type)
800 {
801 case DT_mode:
802 return d1->u.mode == d2->u.mode;
803
804 case DT_code:
805 return d1->u.code == d2->u.code;
806
807 case DT_veclen:
808 return d1->u.veclen == d2->u.veclen;
809
810 case DT_elt_zero_int:
811 case DT_elt_one_int:
812 case DT_elt_zero_wide:
813 return d1->u.intval == d2->u.intval;
814
815 default:
816 break;
817 }
818 }
819
820 /* If either has a predicate that we know something about, set
821 things up so that D1 is the one that always has a known
822 predicate. Then see if they have any codes in common. */
823
824 if (d1->type == DT_pred || d2->type == DT_pred)
825 {
826 if (d2->type == DT_pred)
827 {
828 struct decision_test *tmp;
829 tmp = d1, d1 = d2, d2 = tmp;
830 }
831
832 /* If D2 tests a mode, see if it matches D1. */
833 if (d1->u.pred.mode != VOIDmode)
834 {
835 if (d2->type == DT_mode)
836 {
837 if (d1->u.pred.mode != d2->u.mode)
838 return 0;
839 }
840 else if (d2->type == DT_pred)
841 {
842 if (d2->u.pred.mode != VOIDmode
843 && d1->u.pred.mode != d2->u.pred.mode)
844 return 0;
845 }
846 }
847
848 if (d1->u.pred.index >= 0)
849 {
850 /* If D2 tests a code, see if it is in the list of valid
851 codes for D1's predicate. */
852 if (d2->type == DT_code)
853 {
854 const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
855 while (*c != 0)
856 {
857 if (*c == d2->u.code)
858 break;
859 ++c;
860 }
861 if (*c == 0)
862 return 0;
863 }
864
865 /* Otherwise see if the predicates have any codes in common. */
866 else if (d2->type == DT_pred && d2->u.pred.index >= 0)
ec65fa66 867 {
09051660
RH
868 const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
869 int common = 0;
870
871 while (*c1 != 0 && !common)
872 {
873 const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
874 while (*c2 != 0 && !common)
875 {
876 common = (*c1 == *c2);
877 ++c2;
878 }
879 ++c1;
880 }
881
882 if (!common)
883 return 0;
ec65fa66
RK
884 }
885 }
ec65fa66 886 }
09051660
RH
887
888 return -1;
ec65fa66 889}
09051660
RH
890
891/* A subroutine of maybe_both_true; examines all the tests for a given node.
892 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
893
894static int
895maybe_both_true_1 (d1, d2)
896 struct decision_test *d1, *d2;
897{
898 struct decision_test *t1, *t2;
899
900 /* A match_operand with no predicate can match anything. Recognize
901 this by the existance of a lone DT_accept_op test. */
902 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
903 return 1;
904
905 /* Eliminate pairs of tests while they can exactly match. */
906 while (d1 && d2 && d1->type == d2->type)
907 {
908 if (maybe_both_true_2 (d1, d2) == 0)
909 return 0;
910 d1 = d1->next, d2 = d2->next;
911 }
912
913 /* After that, consider all pairs. */
914 for (t1 = d1; t1 ; t1 = t1->next)
915 for (t2 = d2; t2 ; t2 = t2->next)
916 if (maybe_both_true_2 (t1, t2) == 0)
917 return 0;
918
919 return -1;
920}
921
922/* Return 0 if we can prove that there is no RTL that can match both
923 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
e0689256 924 can match both or just that we couldn't prove there wasn't such an RTL).
ec65fa66 925
e0689256
RK
926 TOPLEVEL is non-zero if we are to only look at the top level and not
927 recursively descend. */
ec65fa66 928
e0689256 929static int
09051660 930maybe_both_true (d1, d2, toplevel)
e0689256
RK
931 struct decision *d1, *d2;
932 int toplevel;
ec65fa66 933{
e0689256 934 struct decision *p1, *p2;
00ec6daa
JH
935 int cmp;
936
937 /* Don't compare strings on the different positions in insn. Doing so
938 is incorrect and results in false matches from constructs like
939
940 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
941 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
942 vs
943 [(set (match_operand:HI "register_operand" "r")
944 (match_operand:HI "register_operand" "r"))]
945
946 If we are presented with such, we are recursing through the remainder
947 of a node's success nodes (from the loop at the end of this function).
948 Skip forward until we come to a position that matches.
949
950 Due to the way position strings are constructed, we know that iterating
951 forward from the lexically lower position (e.g. "00") will run into
952 the lexically higher position (e.g. "1") and not the other way around.
953 This saves a bit of effort. */
954
955 cmp = strcmp (d1->position, d2->position);
956 if (cmp != 0)
957 {
958 if (toplevel)
959 abort();
960
961 /* If the d2->position was lexically lower, swap. */
962 if (cmp > 0)
ace91ff1 963 p1 = d1, d1 = d2, d2 = p1;
00ec6daa
JH
964
965 if (d1->success.first == 0)
966 return 0;
967 for (p1 = d1->success.first; p1; p1 = p1->next)
09051660
RH
968 if (maybe_both_true (p1, d2, 0))
969 return 1;
00ec6daa 970
09051660 971 return 0;
00ec6daa 972 }
e0689256 973
09051660
RH
974 /* Test the current level. */
975 cmp = maybe_both_true_1 (d1->tests, d2->tests);
976 if (cmp >= 0)
977 return cmp;
978
979 /* We can't prove that D1 and D2 cannot both be true. If we are only
980 to check the top level, return 1. Otherwise, see if we can prove
981 that all choices in both successors are mutually exclusive. If
982 either does not have any successors, we can't prove they can't both
983 be true. */
984
985 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
e0689256
RK
986 return 1;
987
09051660
RH
988 for (p1 = d1->success.first; p1; p1 = p1->next)
989 for (p2 = d2->success.first; p2; p2 = p2->next)
990 if (maybe_both_true (p1, p2, 0))
991 return 1;
e0689256 992
09051660
RH
993 return 0;
994}
ec65fa66 995
09051660 996/* A subroutine of nodes_identical. Examine two tests for equivalence. */
ec65fa66 997
09051660
RH
998static int
999nodes_identical_1 (d1, d2)
1000 struct decision_test *d1, *d2;
1001{
1002 switch (d1->type)
ec65fa66 1003 {
09051660
RH
1004 case DT_mode:
1005 return d1->u.mode == d2->u.mode;
e0689256 1006
09051660
RH
1007 case DT_code:
1008 return d1->u.code == d2->u.code;
e0689256 1009
09051660
RH
1010 case DT_pred:
1011 return (d1->u.pred.mode == d2->u.pred.mode
1012 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
e0689256 1013
09051660
RH
1014 case DT_c_test:
1015 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
e0689256 1016
09051660
RH
1017 case DT_veclen:
1018 return d1->u.veclen == d2->u.veclen;
e0689256 1019
09051660
RH
1020 case DT_dup:
1021 return d1->u.dup == d2->u.dup;
e0689256 1022
09051660
RH
1023 case DT_elt_zero_int:
1024 case DT_elt_one_int:
1025 case DT_elt_zero_wide:
1026 return d1->u.intval == d2->u.intval;
e0689256 1027
09051660
RH
1028 case DT_accept_op:
1029 return d1->u.opno == d2->u.opno;
1030
1031 case DT_accept_insn:
1032 /* Differences will be handled in merge_accept_insn. */
1033 return 1;
1034
1035 default:
1036 abort ();
ec65fa66 1037 }
09051660 1038}
ec65fa66 1039
09051660
RH
1040/* True iff the two nodes are identical (on one level only). Due
1041 to the way these lists are constructed, we shouldn't have to
1042 consider different orderings on the tests. */
ec65fa66 1043
09051660
RH
1044static int
1045nodes_identical (d1, d2)
1046 struct decision *d1, *d2;
1047{
1048 struct decision_test *t1, *t2;
e0689256 1049
09051660
RH
1050 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1051 {
1052 if (t1->type != t2->type)
1053 return 0;
1054 if (! nodes_identical_1 (t1, t2))
e0689256 1055 return 0;
09051660 1056 }
e0689256 1057
09051660
RH
1058 /* For success, they should now both be null. */
1059 return t1 == t2;
e0689256 1060}
e0689256 1061
09051660
RH
1062/* A subroutine of merge_trees; given two nodes that have been declared
1063 identical, cope with two insn accept states. If they differ in the
1064 number of clobbers, then the conflict was created by make_insn_sequence
1065 and we can drop the with-clobbers version on the floor. If both
1066 nodes have no additional clobbers, we have found an ambiguity in the
1067 source machine description. */
1068
1069static void
1070merge_accept_insn (oldd, addd)
1071 struct decision *oldd, *addd;
ec65fa66 1072{
09051660
RH
1073 struct decision_test *old, *add;
1074
1075 for (old = oldd->tests; old; old = old->next)
1076 if (old->type == DT_accept_insn)
1077 break;
1078 if (old == NULL)
1079 return;
e0689256 1080
09051660
RH
1081 for (add = addd->tests; add; add = add->next)
1082 if (add->type == DT_accept_insn)
1083 break;
1084 if (add == NULL)
1085 return;
e0689256 1086
09051660
RH
1087 /* If one node is for a normal insn and the second is for the base
1088 insn with clobbers stripped off, the second node should be ignored. */
e0689256 1089
09051660
RH
1090 if (old->u.insn.num_clobbers_to_add == 0
1091 && add->u.insn.num_clobbers_to_add > 0)
1092 {
1093 /* Nothing to do here. */
1094 }
1095 else if (old->u.insn.num_clobbers_to_add > 0
1096 && add->u.insn.num_clobbers_to_add == 0)
1097 {
1098 /* In this case, replace OLD with ADD. */
1099 old->u.insn = add->u.insn;
1100 }
1101 else
1102 {
bcdaba58
RH
1103 message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1104 get_insn_name (add->u.insn.code_number),
1105 get_insn_name (old->u.insn.code_number));
1106 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1107 get_insn_name (old->u.insn.code_number));
1108 error_count++;
09051660 1109 }
e0689256 1110}
e0689256 1111
09051660
RH
1112/* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1113
1114static void
e0689256 1115merge_trees (oldh, addh)
09051660 1116 struct decision_head *oldh, *addh;
e0689256 1117{
09051660 1118 struct decision *next, *add;
e0689256 1119
09051660
RH
1120 if (addh->first == 0)
1121 return;
1122 if (oldh->first == 0)
1123 {
1124 *oldh = *addh;
1125 return;
1126 }
ec65fa66 1127
09051660
RH
1128 /* Trying to merge bits at different positions isn't possible. */
1129 if (strcmp (oldh->first->position, addh->first->position))
e0689256
RK
1130 abort ();
1131
09051660 1132 for (add = addh->first; add ; add = next)
ec65fa66 1133 {
09051660 1134 struct decision *old, *insert_before = NULL;
e0689256
RK
1135
1136 next = add->next;
1137
09051660
RH
1138 /* The semantics of pattern matching state that the tests are
1139 done in the order given in the MD file so that if an insn
1140 matches two patterns, the first one will be used. However,
1141 in practice, most, if not all, patterns are unambiguous so
1142 that their order is independent. In that case, we can merge
1143 identical tests and group all similar modes and codes together.
e0689256
RK
1144
1145 Scan starting from the end of OLDH until we reach a point
09051660
RH
1146 where we reach the head of the list or where we pass a
1147 pattern that could also be true if NEW is true. If we find
1148 an identical pattern, we can merge them. Also, record the
1149 last node that tests the same code and mode and the last one
1150 that tests just the same mode.
e0689256
RK
1151
1152 If we have no match, place NEW after the closest match we found. */
09051660
RH
1153
1154 for (old = oldh->last; old; old = old->prev)
ec65fa66 1155 {
09051660 1156 if (nodes_identical (old, add))
e0689256 1157 {
09051660
RH
1158 merge_accept_insn (old, add);
1159 merge_trees (&old->success, &add->success);
1160 goto merged_nodes;
1161 }
e0689256 1162
09051660
RH
1163 if (maybe_both_true (old, add, 0))
1164 break;
e0689256 1165
09051660
RH
1166 /* Insert the nodes in DT test type order, which is roughly
1167 how expensive/important the test is. Given that the tests
1168 are also ordered within the list, examining the first is
1169 sufficient. */
1170 if (add->tests->type < old->tests->type)
1171 insert_before = old;
1172 }
de6a431b 1173
09051660
RH
1174 if (insert_before == NULL)
1175 {
1176 add->next = NULL;
1177 add->prev = oldh->last;
1178 oldh->last->next = add;
1179 oldh->last = add;
1180 }
1181 else
1182 {
1183 if ((add->prev = insert_before->prev) != NULL)
1184 add->prev->next = add;
1185 else
1186 oldh->first = add;
1187 add->next = insert_before;
1188 insert_before->prev = add;
1189 }
1190
1191 merged_nodes:;
1192 }
1193}
1194\f
1195/* Walk the tree looking for sub-nodes that perform common tests.
1196 Factor out the common test into a new node. This enables us
1197 (depending on the test type) to emit switch statements later. */
1198
1199static void
1200factor_tests (head)
1201 struct decision_head *head;
1202{
1203 struct decision *first, *next;
e0689256 1204
09051660
RH
1205 for (first = head->first; first && first->next; first = next)
1206 {
1207 enum decision_type type;
1208 struct decision *new, *old_last;
e0689256 1209
09051660
RH
1210 type = first->tests->type;
1211 next = first->next;
e0689256 1212
09051660
RH
1213 /* Want at least two compatible sequential nodes. */
1214 if (next->tests->type != type)
1215 continue;
ec65fa66 1216
09051660
RH
1217 /* Don't want all node types, just those we can turn into
1218 switch statements. */
1219 if (type != DT_mode
1220 && type != DT_code
1221 && type != DT_veclen
1222 && type != DT_elt_zero_int
1223 && type != DT_elt_one_int
1224 && type != DT_elt_zero_wide)
e0689256 1225 continue;
ec65fa66 1226
09051660
RH
1227 /* If we'd been performing more than one test, create a new node
1228 below our first test. */
1229 if (first->tests->next != NULL)
1230 {
1231 new = new_decision (first->position, &first->success);
1232 new->tests = first->tests->next;
1233 first->tests->next = NULL;
1234 }
1235
1236 /* Crop the node tree off after our first test. */
1237 first->next = NULL;
1238 old_last = head->last;
1239 head->last = first;
1240
1241 /* For each compatible test, adjust to perform only one test in
1242 the top level node, then merge the node back into the tree. */
1243 do
1244 {
1245 struct decision_head h;
1246
1247 if (next->tests->next != NULL)
1248 {
1249 new = new_decision (next->position, &next->success);
1250 new->tests = next->tests->next;
1251 next->tests->next = NULL;
1252 }
1253 new = next;
1254 next = next->next;
1255 new->next = NULL;
1256 h.first = h.last = new;
ec65fa66 1257
09051660
RH
1258 merge_trees (head, &h);
1259 }
1260 while (next && next->tests->type == type);
ec65fa66 1261
09051660
RH
1262 /* After we run out of compatible tests, graft the remaining nodes
1263 back onto the tree. */
1264 if (next)
e0689256 1265 {
09051660
RH
1266 next->prev = head->last;
1267 head->last->next = next;
1268 head->last = old_last;
e0689256 1269 }
09051660 1270 }
ec65fa66 1271
09051660
RH
1272 /* Recurse. */
1273 for (first = head->first; first; first = first->next)
1274 factor_tests (&first->success);
1275}
1276
1277/* After factoring, try to simplify the tests on any one node.
1278 Tests that are useful for switch statements are recognizable
1279 by having only a single test on a node -- we'll be manipulating
1280 nodes with multiple tests:
1281
1282 If we have mode tests or code tests that are redundant with
1283 predicates, remove them. */
1284
1285static void
1286simplify_tests (head)
1287 struct decision_head *head;
1288{
1289 struct decision *tree;
1290
1291 for (tree = head->first; tree; tree = tree->next)
1292 {
1293 struct decision_test *a, *b;
1294
1295 a = tree->tests;
1296 b = a->next;
1297 if (b == NULL)
1298 continue;
1299
1300 /* Find a predicate node. */
1301 while (b && b->type != DT_pred)
1302 b = b->next;
1303 if (b)
e0689256 1304 {
09051660
RH
1305 /* Due to how these tests are constructed, we don't even need
1306 to check that the mode and code are compatible -- they were
1307 generated from the predicate in the first place. */
1308 while (a->type == DT_mode || a->type == DT_code)
1309 a = a->next;
1310 tree->tests = a;
e0689256
RK
1311 }
1312 }
ec65fa66 1313
09051660
RH
1314 /* Recurse. */
1315 for (tree = head->first; tree; tree = tree->next)
1316 simplify_tests (&tree->success);
ec65fa66 1317}
09051660 1318
e0689256
RK
1319/* Count the number of subnodes of HEAD. If the number is high enough,
1320 make the first node in HEAD start a separate subroutine in the C code
09051660 1321 that is generated. */
ec65fa66
RK
1322
1323static int
09051660
RH
1324break_out_subroutines (head, initial)
1325 struct decision_head *head;
e0689256 1326 int initial;
ec65fa66
RK
1327{
1328 int size = 0;
87bd0490 1329 struct decision *sub;
e0689256 1330
09051660
RH
1331 for (sub = head->first; sub; sub = sub->next)
1332 size += 1 + break_out_subroutines (&sub->success, 0);
e0689256
RK
1333
1334 if (size > SUBROUTINE_THRESHOLD && ! initial)
ec65fa66 1335 {
09051660 1336 head->first->subroutine_number = ++next_subroutine_number;
ec65fa66
RK
1337 size = 1;
1338 }
1339 return size;
1340}
09051660
RH
1341
1342/* For each node p, find the next alternative that might be true
1343 when p is true. */
ec65fa66
RK
1344
1345static void
09051660
RH
1346find_afterward (head, real_afterward)
1347 struct decision_head *head;
1348 struct decision *real_afterward;
ec65fa66 1349{
09051660 1350 struct decision *p, *q, *afterward;
69277eec 1351
09051660
RH
1352 /* We can't propogate alternatives across subroutine boundaries.
1353 This is not incorrect, merely a minor optimization loss. */
ec65fa66 1354
09051660
RH
1355 p = head->first;
1356 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
e0689256 1357
09051660 1358 for ( ; p ; p = p->next)
e0689256 1359 {
09051660
RH
1360 /* Find the next node that might be true if this one fails. */
1361 for (q = p->next; q ; q = q->next)
1362 if (maybe_both_true (p, q, 1))
1363 break;
e0689256 1364
09051660
RH
1365 /* If we reached the end of the list without finding one,
1366 use the incoming afterward position. */
1367 if (!q)
1368 q = afterward;
1369 p->afterward = q;
1370 if (q)
1371 q->need_label = 1;
e0689256
RK
1372 }
1373
09051660
RH
1374 /* Recurse. */
1375 for (p = head->first; p ; p = p->next)
1376 if (p->success.first)
1377 find_afterward (&p->success, p->afterward);
1378
1379 /* When we are generating a subroutine, record the real afterward
1380 position in the first node where write_tree can find it, and we
1381 can do the right thing at the subroutine call site. */
1382 p = head->first;
1383 if (p->subroutine_number > 0)
1384 p->afterward = real_afterward;
1385}
1386\f
1387/* Assuming that the state of argument is denoted by OLDPOS, take whatever
1388 actions are necessary to move to NEWPOS. If we fail to move to the
1389 new state, branch to node AFTERWARD if non-zero, otherwise return.
e0689256 1390
09051660
RH
1391 Failure to move to the new state can only occur if we are trying to
1392 match multiple insns and we try to step past the end of the stream. */
e0689256 1393
09051660
RH
1394static void
1395change_state (oldpos, newpos, afterward, indent)
1396 const char *oldpos;
1397 const char *newpos;
1398 struct decision *afterward;
1399 const char *indent;
1400{
1401 int odepth = strlen (oldpos);
1402 int ndepth = strlen (newpos);
1403 int depth;
1404 int old_has_insn, new_has_insn;
e0689256 1405
09051660
RH
1406 /* Pop up as many levels as necessary. */
1407 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1408 continue;
ec65fa66 1409
09051660
RH
1410 /* Hunt for the last [A-Z] in both strings. */
1411 for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1412 if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1413 break;
1414 for (new_has_insn = odepth - 1; new_has_insn >= 0; --new_has_insn)
1415 if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1416 break;
e0689256 1417
09051660
RH
1418 /* Make sure to reset the _last_insn pointer when popping back up. */
1419 if (old_has_insn >= 0 && new_has_insn < 0)
1420 printf ("%s_last_insn = insn;\n", indent);
e0689256 1421
09051660
RH
1422 /* Go down to desired level. */
1423 while (depth < ndepth)
1424 {
1425 /* It's a different insn from the first one. */
1426 if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
ec65fa66 1427 {
09051660
RH
1428 /* We can only fail if we're moving down the tree. */
1429 if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
e0689256 1430 {
09051660
RH
1431 printf ("%s_last_insn = recog_next_insn (insn, %d);\n",
1432 indent, newpos[depth] - 'A');
e0689256
RK
1433 }
1434 else
1435 {
09051660
RH
1436 printf ("%stem = recog_next_insn (insn, %d);\n",
1437 indent, newpos[depth] - 'A');
1438 printf ("%sif (tem == NULL_RTX)\n", indent);
1439 if (afterward)
1440 printf ("%s goto L%d;\n", indent, afterward->number);
e0689256 1441 else
09051660
RH
1442 printf ("%s goto ret0;\n", indent);
1443 printf ("%s_last_insn = tem;\n", indent);
e0689256 1444 }
09051660 1445 printf ("%sx%d = PATTERN (_last_insn);\n", indent, depth + 1);
ec65fa66 1446 }
09051660
RH
1447 else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1448 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1449 indent, depth + 1, depth, newpos[depth] - 'a');
1450 else
1451 printf ("%sx%d = XEXP (x%d, %c);\n",
1452 indent, depth + 1, depth, newpos[depth]);
1453 ++depth;
1454 }
1455}
1456\f
1457/* Print the enumerator constant for CODE -- the upcase version of
1458 the name. */
1459
1460static void
1461print_code (code)
1462 enum rtx_code code;
1463{
1464 register const char *p;
1465 for (p = GET_RTX_NAME (code); *p; p++)
1466 putchar (TOUPPER (*p));
1467}
ec65fa66 1468
09051660 1469/* Emit code to cross an afterward link -- change state and branch. */
ec65fa66 1470
09051660
RH
1471static void
1472write_afterward (start, afterward, indent)
1473 struct decision *start;
1474 struct decision *afterward;
1475 const char *indent;
1476{
1477 if (!afterward || start->subroutine_number > 0)
1478 printf("%sgoto ret0;\n", indent);
1479 else
1480 {
1481 change_state (start->position, afterward->position, NULL, indent);
1482 printf ("%sgoto L%d;\n", indent, afterward->number);
1483 }
1484}
e0689256 1485
09051660
RH
1486/* Emit a switch statement, if possible, for an initial sequence of
1487 nodes at START. Return the first node yet untested. */
e0689256 1488
09051660
RH
1489static struct decision *
1490write_switch (start, depth)
1491 struct decision *start;
1492 int depth;
1493{
1494 struct decision *p = start;
1495 enum decision_type type = p->tests->type;
ec65fa66 1496
09051660
RH
1497 /* If we have two or more nodes in sequence that test the same one
1498 thing, we may be able to use a switch statement. */
e0689256 1499
09051660
RH
1500 if (!p->next
1501 || p->tests->next
1502 || p->next->tests->type != type
1503 || p->next->tests->next)
1504 return p;
e0689256 1505
09051660
RH
1506 /* DT_code is special in that we can do interesting things with
1507 known predicates at the same time. */
1508 if (type == DT_code)
1509 {
1510 char codemap[NUM_RTX_CODE];
1511 struct decision *ret;
ec65fa66 1512
09051660 1513 memset (codemap, 0, sizeof(codemap));
ec65fa66 1514
09051660
RH
1515 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1516 do
ec65fa66 1517 {
09051660
RH
1518 RTX_CODE code = p->tests->u.code;
1519 printf (" case ");
1520 print_code (code);
1521 printf (":\n goto L%d;\n", p->success.first->number);
1522 p->success.first->need_label = 1;
1523
1524 codemap[code] = 1;
1525 p = p->next;
1526 }
1527 while (p && p->tests->type == DT_code && !p->tests->next);
1528
1529 /* If P is testing a predicate that we know about and we haven't
1530 seen any of the codes that are valid for the predicate, we can
1531 write a series of "case" statement, one for each possible code.
1532 Since we are already in a switch, these redundant tests are very
1533 cheap and will reduce the number of predicates called. */
1534
1535 /* Note that while we write out cases for these predicates here,
1536 we don't actually write the test here, as it gets kinda messy.
1537 It is trivial to leave this to later by telling our caller that
1538 we only processed the CODE tests. */
1539 ret = p;
1540
1541 while (p && p->tests->type == DT_pred
1542 && p->tests->u.pred.index >= 0)
1543 {
1544 const RTX_CODE *c;
ec65fa66 1545
09051660
RH
1546 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1547 if (codemap[(int) *c] != 0)
1548 goto pred_done;
e0689256 1549
09051660 1550 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
ec65fa66 1551 {
09051660
RH
1552 printf (" case ");
1553 print_code (*c);
1554 printf (":\n");
1555 codemap[(int) *c] = 1;
ec65fa66 1556 }
e0689256 1557
09051660
RH
1558 printf (" goto L%d;\n", p->number);
1559 p->need_label = 1;
1560 p = p->next;
ec65fa66
RK
1561 }
1562
09051660
RH
1563 pred_done:
1564 /* Make the default case skip the predicates we managed to match. */
e0689256 1565
09051660
RH
1566 printf (" default:\n");
1567 if (p != ret)
ec65fa66 1568 {
09051660 1569 if (p)
b030d598 1570 {
09051660
RH
1571 printf (" goto L%d;\n", p->number);
1572 p->need_label = 1;
b030d598 1573 }
e0689256 1574 else
09051660 1575 write_afterward (start, start->afterward, " ");
ec65fa66 1576 }
ec65fa66 1577 else
09051660
RH
1578 printf (" break;\n");
1579 printf (" }\n");
1580
1581 return ret;
1582 }
1583 else if (type == DT_mode
1584 || type == DT_veclen
1585 || type == DT_elt_zero_int
1586 || type == DT_elt_one_int
1587 || type == DT_elt_zero_wide)
1588 {
1589 const char *str;
ec65fa66 1590
09051660
RH
1591 printf (" switch (");
1592 switch (type)
1593 {
1594 case DT_mode:
1595 str = "GET_MODE (x%d)";
1596 break;
1597 case DT_veclen:
1598 str = "XVECLEN (x%d, 0)";
1599 break;
1600 case DT_elt_zero_int:
1601 str = "XINT (x%d, 0)";
1602 break;
1603 case DT_elt_one_int:
1604 str = "XINT (x%d, 1)";
1605 break;
1606 case DT_elt_zero_wide:
1607 str = "XWINT (x%d, 0)";
1608 break;
1609 default:
1610 abort ();
1611 }
1612 printf (str, depth);
1613 printf (")\n {\n");
cba998bf 1614
09051660 1615 do
e0689256 1616 {
09051660
RH
1617 printf (" case ");
1618 switch (type)
cba998bf 1619 {
09051660
RH
1620 case DT_mode:
1621 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1622 break;
1623 case DT_veclen:
1624 printf ("%d", p->tests->u.veclen);
1625 break;
1626 case DT_elt_zero_int:
1627 case DT_elt_one_int:
1628 case DT_elt_zero_wide:
1629 printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1630 break;
1631 default:
1632 abort ();
cba998bf 1633 }
09051660
RH
1634 printf (":\n goto L%d;\n", p->success.first->number);
1635 p->success.first->need_label = 1;
cba998bf 1636
09051660 1637 p = p->next;
e0689256 1638 }
09051660
RH
1639 while (p && p->tests->type == type && !p->tests->next);
1640
1641 printf (" default:\n break;\n }\n");
ec65fa66 1642
09051660
RH
1643 return p;
1644 }
1645 else
1646 {
1647 /* None of the other tests are ameanable. */
1648 return p;
1649 }
1650}
ec65fa66 1651
09051660 1652/* Emit code for one test. */
e0689256 1653
09051660
RH
1654static void
1655write_cond (p, depth, subroutine_type)
1656 struct decision_test *p;
1657 int depth;
1658 enum routine_type subroutine_type;
1659{
1660 switch (p->type)
1661 {
1662 case DT_mode:
1663 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1664 break;
e0689256 1665
09051660
RH
1666 case DT_code:
1667 printf ("GET_CODE (x%d) == ", depth);
1668 print_code (p->u.code);
1669 break;
1670
1671 case DT_veclen:
1672 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1673 break;
1674
1675 case DT_elt_zero_int:
1676 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1677 break;
1678
1679 case DT_elt_one_int:
1680 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1681 break;
1682
1683 case DT_elt_zero_wide:
1684 printf ("XWINT (x%d, 0) == ", depth);
1685 printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1686 break;
1687
1688 case DT_dup:
1689 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1690 break;
1691
1692 case DT_pred:
1693 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1694 GET_MODE_NAME (p->u.pred.mode));
1695 break;
1696
1697 case DT_c_test:
1698 printf ("(%s)", p->u.c_test);
1699 break;
1700
1701 case DT_accept_insn:
1702 switch (subroutine_type)
1703 {
1704 case RECOG:
1705 if (p->u.insn.num_clobbers_to_add == 0)
1706 abort ();
1707 printf ("pnum_clobbers != NULL");
1708 break;
1709
1710 default:
1711 abort ();
ec65fa66 1712 }
09051660 1713 break;
ec65fa66 1714
09051660
RH
1715 default:
1716 abort ();
e0689256 1717 }
09051660 1718}
ec65fa66 1719
09051660
RH
1720/* Emit code for one action. The previous tests have succeeded;
1721 TEST is the last of the chain. In the normal case we simply
1722 perform a state change. For the `accept' tests we must do more work. */
ec65fa66 1723
09051660
RH
1724static void
1725write_action (test, depth, uncond, success, subroutine_type)
1726 struct decision_test *test;
1727 int depth, uncond;
1728 struct decision *success;
1729 enum routine_type subroutine_type;
1730{
1731 const char *indent;
1732 int want_close = 0;
1733
1734 if (uncond)
1735 indent = " ";
1736 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
e0689256 1737 {
09051660
RH
1738 fputs (" {\n", stdout);
1739 indent = " ";
1740 want_close = 1;
e0689256 1741 }
09051660
RH
1742 else
1743 indent = " ";
ec65fa66 1744
09051660 1745 if (test->type == DT_accept_op)
e0689256 1746 {
09051660
RH
1747 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1748
1749 /* Only allow DT_accept_insn to follow. */
1750 if (test->next)
1751 {
1752 test = test->next;
1753 if (test->type != DT_accept_insn)
1754 abort ();
1755 }
ec65fa66
RK
1756 }
1757
09051660
RH
1758 /* Sanity check that we're now at the end of the list of tests. */
1759 if (test->next)
e0689256 1760 abort ();
ec65fa66 1761
09051660 1762 if (test->type == DT_accept_insn)
ec65fa66 1763 {
09051660
RH
1764 switch (subroutine_type)
1765 {
1766 case RECOG:
1767 if (test->u.insn.num_clobbers_to_add != 0)
1768 printf ("%s*pnum_clobbers = %d;\n",
1769 indent, test->u.insn.num_clobbers_to_add);
1770 printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1771 break;
1772
1773 case SPLIT:
1774 printf ("%sreturn gen_split_%d (operands);\n",
1775 indent, test->u.insn.code_number);
1776 break;
1777
1778 case PEEPHOLE2:
1779 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1780 indent, test->u.insn.code_number);
1781 printf ("%sif (tem != 0)\n%s goto ret1;\n", indent, indent);
1782 break;
1783
1784 default:
1785 abort ();
1786 }
ec65fa66
RK
1787 }
1788 else
09051660
RH
1789 {
1790 printf("%sgoto L%d;\n", indent, success->number);
1791 success->need_label = 1;
1792 }
ec65fa66 1793
09051660
RH
1794 if (want_close)
1795 fputs (" }\n", stdout);
ec65fa66
RK
1796}
1797
09051660
RH
1798/* Return 1 if the test is always true and has no fallthru path. Return -1
1799 if the test does have a fallthru path, but requires that the condition be
1800 terminated. Otherwise return 0 for a normal test. */
1801/* ??? is_unconditional is a stupid name for a tri-state function. */
1802
ec65fa66 1803static int
09051660
RH
1804is_unconditional (t, subroutine_type)
1805 struct decision_test *t;
1806 enum routine_type subroutine_type;
ec65fa66 1807{
09051660
RH
1808 if (t->type == DT_accept_op)
1809 return 1;
ec65fa66 1810
09051660
RH
1811 if (t->type == DT_accept_insn)
1812 {
1813 switch (subroutine_type)
1814 {
1815 case RECOG:
1816 return (t->u.insn.num_clobbers_to_add == 0);
1817 case SPLIT:
1818 return 1;
1819 case PEEPHOLE2:
1820 return -1;
1821 default:
1822 abort ();
1823 }
1824 }
ec65fa66 1825
09051660 1826 return 0;
ec65fa66
RK
1827}
1828
09051660
RH
1829/* Emit code for one node -- the conditional and the accompanying action.
1830 Return true if there is no fallthru path. */
1831
ec65fa66 1832static int
09051660
RH
1833write_node (p, depth, subroutine_type)
1834 struct decision *p;
1835 int depth;
1836 enum routine_type subroutine_type;
ec65fa66 1837{
09051660
RH
1838 struct decision_test *test, *last_test;
1839 int uncond;
ec65fa66 1840
09051660
RH
1841 last_test = test = p->tests;
1842 uncond = is_unconditional (test, subroutine_type);
1843 if (uncond == 0)
1844 {
1845 printf (" if (");
1846 write_cond (test, depth, subroutine_type);
1847
1848 while ((test = test->next) != NULL)
1849 {
1850 int uncond2;
1851
1852 last_test = test;
1853 uncond2 = is_unconditional (test, subroutine_type);
1854 if (uncond2 != 0)
1855 break;
1856
1857 printf ("\n && ");
1858 write_cond (test, depth, subroutine_type);
1859 }
1860
1861 printf (")\n");
1862 }
1863
1864 write_action (last_test, depth, uncond, p->success.first, subroutine_type);
1865
1866 return uncond > 0;
ec65fa66
RK
1867}
1868
09051660
RH
1869/* Emit code for all of the sibling nodes of HEAD. */
1870
ec65fa66 1871static void
09051660
RH
1872write_tree_1 (head, depth, subroutine_type)
1873 struct decision_head *head;
1874 int depth;
1875 enum routine_type subroutine_type;
ec65fa66 1876{
09051660
RH
1877 struct decision *p, *next;
1878 int uncond = 0;
e0689256 1879
09051660
RH
1880 for (p = head->first; p ; p = next)
1881 {
1882 /* The label for the first element was printed in write_tree. */
1883 if (p != head->first && p->need_label)
1884 OUTPUT_LABEL (" ", p->number);
1885
1886 /* Attempt to write a switch statement for a whole sequence. */
1887 next = write_switch (p, depth);
1888 if (p != next)
1889 uncond = 0;
1890 else
1891 {
1892 /* Failed -- fall back and write one node. */
1893 uncond = write_node (p, depth, subroutine_type);
1894 next = p->next;
1895 }
1896 }
e0689256 1897
09051660
RH
1898 /* Finished with this chain. Close a fallthru path by branching
1899 to the afterward node. */
1900 if (! uncond)
1901 write_afterward (head->last, head->last->afterward, " ");
1902}
e0689256 1903
09051660
RH
1904/* Write out the decision tree starting at HEAD. PREVPOS is the
1905 position at the node that branched to this node. */
e0689256
RK
1906
1907static void
09051660
RH
1908write_tree (head, prevpos, type, initial)
1909 struct decision_head *head;
85fda1eb 1910 const char *prevpos;
e0689256 1911 enum routine_type type;
09051660 1912 int initial;
e0689256 1913{
09051660 1914 register struct decision *p = head->first;
e0689256 1915
09051660
RH
1916 putchar ('\n');
1917 if (p->need_label)
1918 OUTPUT_LABEL (" ", p->number);
1919
1920 if (! initial && p->subroutine_number > 0)
e0689256 1921 {
09051660
RH
1922 static const char * const name_prefix[] = {
1923 "recog", "split", "peephole2"
1924 };
1925
1926 static const char * const call_suffix[] = {
1927 ", pnum_clobbers", "", ", _plast_insn"
1928 };
e0689256 1929
09051660
RH
1930 /* This node has been broken out into a separate subroutine.
1931 Call it, test the result, and branch accordingly. */
1932
1933 if (p->afterward)
e0689256
RK
1934 {
1935 printf (" tem = %s_%d (x0, insn%s);\n",
09051660 1936 name_prefix[type], p->subroutine_number, call_suffix[type]);
ede7cd44 1937 if (IS_SPLIT (type))
09051660 1938 printf (" if (tem != 0)\n return tem;\n");
71bde1f3 1939 else
09051660
RH
1940 printf (" if (tem >= 0)\n return tem;\n");
1941
1942 change_state (p->position, p->afterward->position, NULL, " ");
1943 printf (" goto L%d;\n", p->afterward->number);
e0689256
RK
1944 }
1945 else
09051660
RH
1946 {
1947 printf (" return %s_%d (x0, insn%s);\n",
1948 name_prefix[type], p->subroutine_number, call_suffix[type]);
1949 }
e0689256 1950 }
09051660
RH
1951 else
1952 {
1953 int depth = strlen (p->position);
e0689256 1954
09051660
RH
1955 change_state (prevpos, p->position, head->last->afterward, " ");
1956 write_tree_1 (head, depth, type);
e0689256 1957
09051660
RH
1958 for (p = head->first; p; p = p->next)
1959 if (p->success.first)
1960 write_tree (&p->success, p->position, type, 0);
1961 }
e0689256
RK
1962}
1963
09051660
RH
1964/* Write out a subroutine of type TYPE to do comparisons starting at
1965 node TREE. */
ede7cd44 1966
09051660
RH
1967static void
1968write_subroutine (head, type)
1969 struct decision_head *head;
1970 enum routine_type type;
1971{
1972 static const char * const proto_pattern[] = {
1973 "%sint recog%s PROTO ((rtx, rtx, int *));\n",
1974 "%srtx split%s PROTO ((rtx, rtx));\n",
1975 "%srtx peephole2%s PROTO ((rtx, rtx, rtx *));\n"
1976 };
1977
1978 static const char * const decl_pattern[] = {
1979"%sint\n\
1980recog%s (x0, insn, pnum_clobbers)\n\
1981 register rtx x0;\n\
1982 rtx insn ATTRIBUTE_UNUSED;\n\
1983 int *pnum_clobbers ATTRIBUTE_UNUSED;\n",
1984
1985"%srtx\n\
1986split%s (x0, insn)\n\
1987 register rtx x0;\n\
1988 rtx insn ATTRIBUTE_UNUSED;\n",
1989
1990"%srtx\n\
1991peephole2%s (x0, insn, _plast_insn)\n\
1992 register rtx x0;\n\
1993 rtx insn ATTRIBUTE_UNUSED;\n\
1994 rtx *_plast_insn ATTRIBUTE_UNUSED;\n"
1995 };
1996
e8f9b13a 1997 int subfunction = head->first ? head->first->subroutine_number : 0;
09051660
RH
1998 const char *s_or_e;
1999 char extension[32];
2000 int i;
2001
2002 s_or_e = subfunction ? "static " : "";
e0689256 2003
09051660
RH
2004 if (subfunction)
2005 sprintf (extension, "_%d", subfunction);
2006 else if (type == RECOG)
2007 extension[0] = '\0';
2008 else
2009 strcpy (extension, "_insns");
2010
2011 printf (proto_pattern[type], s_or_e, extension);
2012 printf (decl_pattern[type], s_or_e, extension);
2013
2014 printf ("{\n register rtx * const operands = &recog_data.operand[0];\n");
2015 for (i = 1; i <= max_depth; i++)
2016 printf (" register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2017
2018 if (type == PEEPHOLE2)
2019 printf (" register rtx _last_insn = insn;\n");
2020 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2021
e8f9b13a
RH
2022 if (head->first)
2023 write_tree (head, "", type, 1);
2024 else
2025 printf (" goto ret0;\n");
09051660
RH
2026
2027 if (type == PEEPHOLE2)
2028 printf (" ret1:\n *_plast_insn = _last_insn;\n return tem;\n");
2029 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2030}
2031
2032/* In break_out_subroutines, we discovered the boundaries for the
2033 subroutines, but did not write them out. Do so now. */
e0689256 2034
ec65fa66 2035static void
09051660
RH
2036write_subroutines (head, type)
2037 struct decision_head *head;
2038 enum routine_type type;
ec65fa66 2039{
09051660 2040 struct decision *p;
ec65fa66 2041
09051660
RH
2042 for (p = head->first; p ; p = p->next)
2043 if (p->success.first)
2044 write_subroutines (&p->success, type);
ec65fa66 2045
09051660
RH
2046 if (head->first->subroutine_number > 0)
2047 write_subroutine (head, type);
2048}
ede7cd44 2049
09051660 2050/* Begin the output file. */
ede7cd44 2051
09051660
RH
2052static void
2053write_header ()
2054{
2055 puts ("\
2056/* Generated automatically by the program `genrecog' from the target\n\
2057 machine description file. */\n\
2058\n\
2059#include \"config.h\"\n\
2060#include \"system.h\"\n\
2061#include \"rtl.h\"\n\
2062#include \"tm_p.h\"\n\
2063#include \"function.h\"\n\
2064#include \"insn-config.h\"\n\
2065#include \"recog.h\"\n\
2066#include \"real.h\"\n\
2067#include \"output.h\"\n\
2068#include \"flags.h\"\n\
b1afd7f4
KG
2069#include \"hard-reg-set.h\"\n\
2070#include \"resource.h\"\n\
09051660
RH
2071\n");
2072
2073 puts ("\n\
2074/* `recog' contains a decision tree that recognizes whether the rtx\n\
2075 X0 is a valid instruction.\n\
2076\n\
2077 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2078 returns a nonnegative number which is the insn code number for the\n\
2079 pattern that matched. This is the same as the order in the machine\n\
2080 description of the entry that matched. This number can be used as an\n\
2081 index into `insn_data' and other tables.\n\
2082\n\
2083 The third argument to recog is an optional pointer to an int. If\n\
2084 present, recog will accept a pattern if it matches except for missing\n\
2085 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2086 the optional pointer will be set to the number of CLOBBERs that need\n\
2087 to be added (it should be initialized to zero by the caller). If it\n\
2088 is set nonzero, the caller should allocate a PARALLEL of the\n\
2089 appropriate size, copy the initial entries, and call add_clobbers\n\
2090 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2091");
2092
2093 puts ("\n\
2094 The function split_insns returns 0 if the rtl could not\n\
2095 be split or the split rtl in a SEQUENCE if it can be.\n\
2096\n\
2097 The function peephole2_insns returns 0 if the rtl could not\n\
2098 be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2099 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2100*/\n\n");
2101}
ec65fa66 2102
09051660
RH
2103\f
2104/* Construct and return a sequence of decisions
2105 that will recognize INSN.
ec65fa66 2106
09051660
RH
2107 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2108
2109static struct decision_head
2110make_insn_sequence (insn, type)
2111 rtx insn;
2112 enum routine_type type;
2113{
2114 rtx x;
2115 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2116 struct decision *last;
2117 struct decision_test *test, **place;
2118 struct decision_head head;
2119
2120 record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2121
2122 if (type == PEEPHOLE2)
ec65fa66 2123 {
09051660
RH
2124 int i, j;
2125
2126 /* peephole2 gets special treatment:
2127 - X always gets an outer parallel even if it's only one entry
2128 - we remove all traces of outer-level match_scratch and match_dup
2129 expressions here. */
2130 x = rtx_alloc (PARALLEL);
2131 PUT_MODE (x, VOIDmode);
2132 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2133 for (i = j = 0; i < XVECLEN (insn, 0); i++)
ede7cd44 2134 {
09051660
RH
2135 rtx tmp = XVECEXP (insn, 0, i);
2136 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2137 {
2138 XVECEXP (x, 0, j) = tmp;
2139 j++;
2140 }
2141 }
2142 XVECLEN (x, 0) = j;
2143 }
2144 else if (XVECLEN (insn, type == RECOG) == 1)
2145 x = XVECEXP (insn, type == RECOG, 0);
2146 else
2147 {
2148 x = rtx_alloc (PARALLEL);
2149 XVEC (x, 0) = XVEC (insn, type == RECOG);
2150 PUT_MODE (x, VOIDmode);
2151 }
2152
bcdaba58
RH
2153 validate_pattern (x, 0);
2154
09051660
RH
2155 memset(&head, 0, sizeof(head));
2156 last = add_to_sequence (x, &head, "", type, 1);
2157
2158 /* Find the end of the test chain on the last node. */
2159 for (test = last->tests; test->next; test = test->next)
2160 continue;
2161 place = &test->next;
2162
2163 if (c_test[0])
2164 {
2165 /* Need a new node if we have another test to add. */
2166 if (test->type == DT_accept_op)
2167 {
2168 last = new_decision ("", &last->success);
2169 place = &last->tests;
2170 }
2171 test = new_decision_test (DT_c_test, &place);
2172 test->u.c_test = c_test;
2173 }
2174
2175 test = new_decision_test (DT_accept_insn, &place);
2176 test->u.insn.code_number = next_insn_code;
bcdaba58 2177 test->u.insn.lineno = pattern_lineno;
09051660
RH
2178 test->u.insn.num_clobbers_to_add = 0;
2179
2180 switch (type)
2181 {
2182 case RECOG:
2183 /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2184 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2185 If so, set up to recognize the pattern without these CLOBBERs. */
2186
2187 if (GET_CODE (x) == PARALLEL)
2188 {
2189 int i;
2190
2191 /* Find the last non-clobber in the parallel. */
2192 for (i = XVECLEN (x, 0); i > 0; i--)
ede7cd44 2193 {
09051660
RH
2194 rtx y = XVECEXP (x, 0, i - 1);
2195 if (GET_CODE (y) != CLOBBER
2196 || (GET_CODE (XEXP (y, 0)) != REG
2197 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2198 break;
ede7cd44 2199 }
09051660
RH
2200
2201 if (i != XVECLEN (x, 0))
ede7cd44 2202 {
09051660
RH
2203 rtx new;
2204 struct decision_head clobber_head;
ede7cd44 2205
09051660
RH
2206 /* Build a similar insn without the clobbers. */
2207 if (i == 1)
2208 new = XVECEXP (x, 0, 0);
ede7cd44 2209 else
09051660
RH
2210 {
2211 int j;
2212
2213 new = rtx_alloc (PARALLEL);
2214 XVEC (new, 0) = rtvec_alloc (i);
2215 for (j = i - 1; j >= 0; j--)
2216 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2217 }
2218
2219 /* Recognize it. */
2220 memset (&clobber_head, 0, sizeof(clobber_head));
2221 last = add_to_sequence (new, &clobber_head, "", type, 1);
ede7cd44 2222
09051660
RH
2223 /* Find the end of the test chain on the last node. */
2224 for (test = last->tests; test->next; test = test->next)
2225 continue;
2226
2227 /* We definitely have a new test to add -- create a new
2228 node if needed. */
2229 place = &test->next;
2230 if (test->type == DT_accept_op)
2231 {
2232 last = new_decision ("", &last->success);
2233 place = &last->tests;
2234 }
2235
2236 if (c_test[0])
2237 {
2238 test = new_decision_test (DT_c_test, &place);
2239 test->u.c_test = c_test;
2240 }
2241
2242 test = new_decision_test (DT_accept_insn, &place);
2243 test->u.insn.code_number = next_insn_code;
bcdaba58 2244 test->u.insn.lineno = pattern_lineno;
09051660
RH
2245 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2246
2247 merge_trees (&head, &clobber_head);
ede7cd44 2248 }
ede7cd44 2249 }
09051660
RH
2250 break;
2251
2252 case SPLIT:
2253 /* Define the subroutine we will call below and emit in genemit. */
2254 printf ("extern rtx gen_split_%d PROTO ((rtx *));\n", next_insn_code);
2255 break;
2256
2257 case PEEPHOLE2:
2258 /* Define the subroutine we will call below and emit in genemit. */
2259 printf ("extern rtx gen_peephole2_%d PROTO ((rtx, rtx *));\n",
2260 next_insn_code);
2261 break;
ec65fa66 2262 }
09051660 2263 next_insn_code++;
e0689256 2264
09051660 2265 return head;
ec65fa66
RK
2266}
2267
09051660
RH
2268static void
2269process_tree (head, subroutine_type)
2270 struct decision_head *head;
2271 enum routine_type subroutine_type;
ec65fa66 2272{
e8f9b13a
RH
2273 if (head->first != NULL)
2274 {
2275 factor_tests (head);
2276 simplify_tests (head);
ec65fa66 2277
e8f9b13a
RH
2278 next_subroutine_number = 0;
2279 break_out_subroutines (head, 1);
2280 find_afterward (head, NULL);
c1b59dce 2281
e8f9b13a
RH
2282 write_subroutines (head, subroutine_type);
2283 }
09051660
RH
2284 write_subroutine (head, subroutine_type);
2285}
2286\f
ec65fa66
RK
2287int
2288main (argc, argv)
2289 int argc;
2290 char **argv;
2291{
2292 rtx desc;
09051660 2293 struct decision_head recog_tree, split_tree, peephole2_tree, h;
ec65fa66 2294 FILE *infile;
ec65fa66
RK
2295 register int c;
2296
f8b6598e 2297 progname = "genrecog";
ec65fa66 2298 obstack_init (rtl_obstack);
09051660
RH
2299
2300 memset (&recog_tree, 0, sizeof recog_tree);
2301 memset (&split_tree, 0, sizeof split_tree);
2302 memset (&peephole2_tree, 0, sizeof peephole2_tree);
ec65fa66
RK
2303
2304 if (argc <= 1)
2305 fatal ("No input file name.");
2306
2307 infile = fopen (argv[1], "r");
2308 if (infile == 0)
2309 {
2310 perror (argv[1]);
09051660 2311 return FATAL_EXIT_CODE;
ec65fa66 2312 }
bcdaba58 2313 read_rtx_filename = argv[1];
ec65fa66 2314
ec65fa66
RK
2315 next_insn_code = 0;
2316 next_index = 0;
2317
09051660 2318 write_header ();
ec65fa66
RK
2319
2320 /* Read the machine description. */
2321
2322 while (1)
2323 {
2324 c = read_skip_spaces (infile);
2325 if (c == EOF)
2326 break;
2327 ungetc (c, infile);
bcdaba58 2328 pattern_lineno = read_rtx_lineno;
ec65fa66
RK
2329
2330 desc = read_rtx (infile);
2331 if (GET_CODE (desc) == DEFINE_INSN)
09051660
RH
2332 {
2333 h = make_insn_sequence (desc, RECOG);
2334 merge_trees (&recog_tree, &h);
2335 }
ec65fa66 2336 else if (GET_CODE (desc) == DEFINE_SPLIT)
09051660
RH
2337 {
2338 h = make_insn_sequence (desc, SPLIT);
2339 merge_trees (&split_tree, &h);
2340 }
ede7cd44 2341 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
09051660
RH
2342 {
2343 h = make_insn_sequence (desc, PEEPHOLE2);
2344 merge_trees (&peephole2_tree, &h);
2345 }
2346
ec65fa66
RK
2347 if (GET_CODE (desc) == DEFINE_PEEPHOLE
2348 || GET_CODE (desc) == DEFINE_EXPAND)
2349 next_insn_code++;
2350 next_index++;
2351 }
2352
bcdaba58
RH
2353 if (error_count)
2354 return FATAL_EXIT_CODE;
2355
09051660 2356 puts ("\n\n");
ec65fa66 2357
09051660
RH
2358 process_tree (&recog_tree, RECOG);
2359 process_tree (&split_tree, SPLIT);
2360 process_tree (&peephole2_tree, PEEPHOLE2);
ede7cd44 2361
ec65fa66 2362 fflush (stdout);
c1b59dce 2363 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
ec65fa66 2364}
09051660 2365\f
a995e389
RH
2366/* Define this so we can link with print-rtl.o to get debug_rtx function. */
2367const char *
2368get_insn_name (code)
2369 int code;
2370{
2371 if (code < insn_name_ptr_size)
2372 return insn_name_ptr[code];
2373 else
2374 return NULL;
2375}
09051660
RH
2376
2377static void
2378record_insn_name (code, name)
2379 int code;
2380 const char *name;
2381{
2382 static const char *last_real_name = "insn";
2383 static int last_real_code = 0;
2384 char *new;
2385
2386 if (insn_name_ptr_size <= code)
2387 {
2388 int new_size;
2389 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2390 insn_name_ptr =
2391 (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2392 memset (insn_name_ptr + insn_name_ptr_size, 0,
2393 sizeof(char *) * (new_size - insn_name_ptr_size));
2394 insn_name_ptr_size = new_size;
2395 }
2396
2397 if (!name || name[0] == '\0')
2398 {
2399 new = xmalloc (strlen (last_real_name) + 10);
2400 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2401 }
2402 else
2403 {
2404 last_real_name = new = xstrdup (name);
2405 last_real_code = code;
2406 }
2407
2408 insn_name_ptr[code] = new;
2409}
2410\f
2411char *
2412xstrdup (input)
2413 const char *input;
2414{
2415 register size_t len = strlen (input) + 1;
2416 register char *output = xmalloc (len);
2417 memcpy (output, input, len);
2418 return output;
2419}
2420
2421PTR
2422xrealloc (old, size)
2423 PTR old;
2424 size_t size;
2425{
2426 register PTR ptr;
2427 if (old)
2428 ptr = (PTR) realloc (old, size);
2429 else
2430 ptr = (PTR) malloc (size);
2431 if (!ptr)
2432 fatal ("virtual memory exhausted");
2433 return ptr;
2434}
2435
2436PTR
2437xmalloc (size)
2438 size_t size;
2439{
2440 register PTR val = (PTR) malloc (size);
2441
2442 if (val == 0)
2443 fatal ("virtual memory exhausted");
2444 return val;
2445}
2446\f
2447static void
2448debug_decision_2 (test)
2449 struct decision_test *test;
2450{
2451 switch (test->type)
2452 {
2453 case DT_mode:
2454 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2455 break;
2456 case DT_code:
2457 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2458 break;
2459 case DT_veclen:
2460 fprintf (stderr, "veclen=%d", test->u.veclen);
2461 break;
2462 case DT_elt_zero_int:
2463 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2464 break;
2465 case DT_elt_one_int:
2466 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2467 break;
2468 case DT_elt_zero_wide:
2469 fprintf (stderr, "elt0_w=");
2470 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2471 break;
2472 case DT_dup:
2473 fprintf (stderr, "dup=%d", test->u.dup);
2474 break;
2475 case DT_pred:
2476 fprintf (stderr, "pred=(%s,%s)",
2477 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2478 break;
2479 case DT_c_test:
2480 {
2481 char sub[16+4];
2482 strncpy (sub, test->u.c_test, sizeof(sub));
2483 memcpy (sub+16, "...", 4);
2484 fprintf (stderr, "c_test=\"%s\"", sub);
2485 }
2486 break;
2487 case DT_accept_op:
2488 fprintf (stderr, "A_op=%d", test->u.opno);
2489 break;
2490 case DT_accept_insn:
2491 fprintf (stderr, "A_insn=(%d,%d)",
2492 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2493 break;
2494
2495 default:
2496 abort ();
2497 }
2498}
2499
2500static void
2501debug_decision_1 (d, indent)
2502 struct decision *d;
2503 int indent;
2504{
2505 int i;
2506 struct decision_test *test;
2507
2508 if (d == NULL)
2509 {
2510 for (i = 0; i < indent; ++i)
2511 putc (' ', stderr);
2512 fputs ("(nil)\n", stderr);
2513 return;
2514 }
2515
2516 for (i = 0; i < indent; ++i)
2517 putc (' ', stderr);
2518
2519 putc ('{', stderr);
2520 test = d->tests;
2521 if (test)
2522 {
2523 debug_decision_2 (test);
2524 while ((test = test->next) != NULL)
2525 {
2526 fputs (" + ", stderr);
2527 debug_decision_2 (test);
2528 }
2529 }
2530 fprintf (stderr, "} %d\n", d->number);
2531}
2532
2533static void
2534debug_decision_0 (d, indent, maxdepth)
2535 struct decision *d;
2536 int indent, maxdepth;
2537{
2538 struct decision *n;
2539 int i;
2540
2541 if (maxdepth < 0)
2542 return;
2543 if (d == NULL)
2544 {
2545 for (i = 0; i < indent; ++i)
2546 putc (' ', stderr);
2547 fputs ("(nil)\n", stderr);
2548 return;
2549 }
2550
2551 debug_decision_1 (d, indent);
2552 for (n = d->success.first; n ; n = n->next)
2553 debug_decision_0 (n, indent + 2, maxdepth - 1);
2554}
2555
2556void
2557debug_decision (d)
2558 struct decision *d;
2559{
2560 debug_decision_0 (d, 0, 1000000);
2561}
ec1c89e6
RH
2562
2563void
2564debug_decision_list (d)
2565 struct decision *d;
2566{
2567 while (d)
2568 {
2569 debug_decision_0 (d, 0, 0);
2570 d = d->next;
2571 }
2572}
This page took 1.683976 seconds and 5 git commands to generate.