OSDN Git Service

* Makefile.in (gengtype): Use $(BUILD_ERRORS).
[pf3gnuchains/gcc-fork.git] / gcc / genattrtab.c
1 /* Generate code from machine description to compute values of attributes.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /* This program handles insn attributes and the DEFINE_DELAY and
24    DEFINE_INSN_RESERVATION definitions.
25
26    It produces a series of functions named `get_attr_...', one for each insn
27    attribute.  Each of these is given the rtx for an insn and returns a member
28    of the enum for the attribute.
29
30    These subroutines have the form of a `switch' on the INSN_CODE (via
31    `recog_memoized').  Each case either returns a constant attribute value
32    or a value that depends on tests on other attributes, the form of
33    operands, or some random C expression (encoded with a SYMBOL_REF
34    expression).
35
36    If the attribute `alternative', or a random C expression is present,
37    `constrain_operands' is called.  If either of these cases of a reference to
38    an operand is found, `extract_insn' is called.
39
40    The special attribute `length' is also recognized.  For this operand,
41    expressions involving the address of an operand or the current insn,
42    (address (pc)), are valid.  In this case, an initial pass is made to
43    set all lengths that do not depend on address.  Those that do are set to
44    the maximum length.  Then each insn that depends on an address is checked
45    and possibly has its length changed.  The process repeats until no further
46    changed are made.  The resulting lengths are saved for use by
47    `get_attr_length'.
48
49    A special form of DEFINE_ATTR, where the expression for default value is a
50    CONST expression, indicates an attribute that is constant for a given run
51    of the compiler.  The subroutine generated for these attributes has no
52    parameters as it does not depend on any particular insn.  Constant
53    attributes are typically used to specify which variety of processor is
54    used.
55
56    Internal attributes are defined to handle DEFINE_DELAY and
57    DEFINE_INSN_RESERVATION.  Special routines are output for these cases.
58
59    This program works by keeping a list of possible values for each attribute.
60    These include the basic attribute choices, default values for attribute, and
61    all derived quantities.
62
63    As the description file is read, the definition for each insn is saved in a
64    `struct insn_def'.   When the file reading is complete, a `struct insn_ent'
65    is created for each insn and chained to the corresponding attribute value,
66    either that specified, or the default.
67
68    An optimization phase is then run.  This simplifies expressions for each
69    insn.  EQ_ATTR tests are resolved, whenever possible, to a test that
70    indicates when the attribute has the specified value for the insn.  This
71    avoids recursive calls during compilation.
72
73    The strategy used when processing DEFINE_DELAY definitions is to create
74    arbitrarily complex expressions and have the optimization simplify them.
75
76    Once optimization is complete, any required routines and definitions
77    will be written.
78
79    An optimization that is not yet implemented is to hoist the constant
80    expressions entirely out of the routines and definitions that are written.
81    A way to do this is to iterate over all possible combinations of values
82    for constant attributes and generate a set of functions for that given
83    combination.  An initialization function would be written that evaluates
84    the attributes and installs the corresponding set of routines and
85    definitions (each would be accessed through a pointer).
86
87    We use the flags in an RTX as follows:
88    `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
89       independent of the insn code.
90    `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
91       for the insn code currently being processed (see optimize_attrs).
92    `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
93       (see attr_rtx).
94    `volatil' (ATTR_EQ_ATTR_P): During simplify_by_exploding the value of an
95       EQ_ATTR rtx is true if !volatil and false if volatil.  */
96
97 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
98 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
99 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
100 #define ATTR_EQ_ATTR_P(RTX) (RTX_FLAG((RTX), volatil))
101
102 #if 0
103 #define strcmp_check(S1, S2) ((S1) == (S2)              \
104                               ? 0                       \
105                               : (gcc_assert (strcmp ((S1), (S2))), 1))
106 #else
107 #define strcmp_check(S1, S2) ((S1) != (S2))
108 #endif
109
110 #include "bconfig.h"
111 #include "system.h"
112 #include "coretypes.h"
113 #include "tm.h"
114 #include "rtl.h"
115 #include "ggc.h"
116 #include "gensupport.h"
117
118 #ifdef HAVE_SYS_RESOURCE_H
119 # include <sys/resource.h>
120 #endif
121
122 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
123    /usr/include/sys/stdtypes.h on Sun OS 4.x.  */
124 #include "obstack.h"
125 #include "errors.h"
126
127 #include "genattrtab.h"
128
129 static struct obstack obstack1, obstack2;
130 struct obstack *hash_obstack = &obstack1;
131 struct obstack *temp_obstack = &obstack2;
132
133 /* enough space to reserve for printing out ints */
134 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
135
136 /* Define structures used to record attributes and values.  */
137
138 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
139    encountered, we store all the relevant information into a
140    `struct insn_def'.  This is done to allow attribute definitions to occur
141    anywhere in the file.  */
142
143 struct insn_def
144 {
145   struct insn_def *next;        /* Next insn in chain.  */
146   rtx def;                      /* The DEFINE_...  */
147   int insn_code;                /* Instruction number.  */
148   int insn_index;               /* Expression numer in file, for errors.  */
149   int lineno;                   /* Line number.  */
150   int num_alternatives;         /* Number of alternatives.  */
151   int vec_idx;                  /* Index of attribute vector in `def'.  */
152 };
153
154 /* Once everything has been read in, we store in each attribute value a list
155    of insn codes that have that value.  Here is the structure used for the
156    list.  */
157
158 struct insn_ent
159 {
160   struct insn_ent *next;        /* Next in chain.  */
161   struct insn_def *def;         /* Instruction definition.  */
162 };
163
164 /* Each value of an attribute (either constant or computed) is assigned a
165    structure which is used as the listhead of the insns that have that
166    value.  */
167
168 struct attr_value
169 {
170   rtx value;                    /* Value of attribute.  */
171   struct attr_value *next;      /* Next attribute value in chain.  */
172   struct insn_ent *first_insn;  /* First insn with this value.  */
173   int num_insns;                /* Number of insns with this value.  */
174   int has_asm_insn;             /* True if this value used for `asm' insns */
175 };
176
177 /* Structure for each attribute.  */
178
179 struct attr_desc
180 {
181   char *name;                   /* Name of attribute.  */
182   struct attr_desc *next;       /* Next attribute.  */
183   struct attr_value *first_value; /* First value of this attribute.  */
184   struct attr_value *default_val; /* Default value for this attribute.  */
185   int lineno : 24;              /* Line number.  */
186   unsigned is_numeric   : 1;    /* Values of this attribute are numeric.  */
187   unsigned negative_ok  : 1;    /* Allow negative numeric values.  */
188   unsigned unsigned_p   : 1;    /* Make the output function unsigned int.  */
189   unsigned is_const     : 1;    /* Attribute value constant for each run.  */
190   unsigned is_special   : 1;    /* Don't call `write_attr_set'.  */
191   unsigned static_p     : 1;    /* Make the output function static.  */
192 };
193
194 #define NULL_ATTR (struct attr_desc *) NULL
195
196 /* Structure for each DEFINE_DELAY.  */
197
198 struct delay_desc
199 {
200   rtx def;                      /* DEFINE_DELAY expression.  */
201   struct delay_desc *next;      /* Next DEFINE_DELAY.  */
202   int num;                      /* Number of DEFINE_DELAY, starting at 1.  */
203   int lineno;                   /* Line number.  */
204 };
205
206 /* Listheads of above structures.  */
207
208 /* This one is indexed by the first character of the attribute name.  */
209 #define MAX_ATTRS_INDEX 256
210 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
211 static struct insn_def *defs;
212 static struct delay_desc *delays;
213
214 /* Other variables.  */
215
216 static int insn_code_number;
217 static int insn_index_number;
218 static int got_define_asm_attributes;
219 static int must_extract;
220 static int must_constrain;
221 static int address_used;
222 static int length_used;
223 static int num_delays;
224 static int have_annul_true, have_annul_false;
225 static int num_insn_ents;
226
227 int num_dfa_decls;
228
229 /* Stores, for each insn code, the number of constraint alternatives.  */
230
231 static int *insn_n_alternatives;
232
233 /* Stores, for each insn code, a bitmap that has bits on for each possible
234    alternative.  */
235
236 static int *insn_alternatives;
237
238 /* If nonzero, assume that the `alternative' attr has this value.
239    This is the hashed, unique string for the numeral
240    whose value is chosen alternative.  */
241
242 static const char *current_alternative_string;
243
244 /* Used to simplify expressions.  */
245
246 static rtx true_rtx, false_rtx;
247
248 /* Used to reduce calls to `strcmp' */
249
250 static char *alternative_name;
251 static const char *length_str;
252 static const char *delay_type_str;
253 static const char *delay_1_0_str;
254 static const char *num_delay_slots_str;
255
256 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
257    called.  */
258
259 int reload_completed = 0;
260
261 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
262    to define it here.  */
263
264 int optimize = 0;
265
266 /* Simplify an expression.  Only call the routine if there is something to
267    simplify.  */
268 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX)     \
269   (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP)  \
270    : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
271
272 /* Simplify (eq_attr ("alternative") ...)
273    when we are working with a particular alternative.  */
274 #define SIMPLIFY_ALTERNATIVE(EXP)                               \
275   if (current_alternative_string                                \
276       && GET_CODE ((EXP)) == EQ_ATTR                            \
277       && XSTR ((EXP), 0) == alternative_name)                   \
278     (EXP) = (XSTR ((EXP), 1) == current_alternative_string      \
279             ? true_rtx : false_rtx);
280
281 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
282
283 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
284    They won't actually be used.  */
285
286 rtx global_rtl[GR_MAX];
287 rtx pic_offset_table_rtx;
288
289 static void attr_hash_add_rtx   (int, rtx);
290 static void attr_hash_add_string (int, char *);
291 static rtx attr_rtx             (enum rtx_code, ...);
292 static rtx attr_rtx_1           (enum rtx_code, va_list);
293 static char *attr_string        (const char *, int);
294 static rtx check_attr_value     (rtx, struct attr_desc *);
295 static rtx convert_set_attr_alternative (rtx, struct insn_def *);
296 static rtx convert_set_attr     (rtx, struct insn_def *);
297 static void check_defs          (void);
298 static rtx make_canonical       (struct attr_desc *, rtx);
299 static struct attr_value *get_attr_value (rtx, struct attr_desc *, int);
300 static rtx copy_rtx_unchanging  (rtx);
301 static rtx copy_boolean         (rtx);
302 static void expand_delays       (void);
303 static void fill_attr           (struct attr_desc *);
304 static rtx substitute_address   (rtx, rtx (*) (rtx), rtx (*) (rtx));
305 static void make_length_attrs   (void);
306 static rtx identity_fn          (rtx);
307 static rtx zero_fn              (rtx);
308 static rtx one_fn               (rtx);
309 static rtx max_fn               (rtx);
310 static void write_length_unit_log (void);
311 static rtx simplify_cond        (rtx, int, int);
312 static void clear_struct_flag (rtx);
313 static void remove_insn_ent  (struct attr_value *, struct insn_ent *);
314 static void insert_insn_ent  (struct attr_value *, struct insn_ent *);
315 static rtx insert_right_side    (enum rtx_code, rtx, rtx, int, int);
316 static rtx make_alternative_compare (int);
317 static int compute_alternative_mask (rtx, enum rtx_code);
318 static rtx evaluate_eq_attr     (rtx, rtx, int, int);
319 static rtx simplify_and_tree    (rtx, rtx *, int, int);
320 static rtx simplify_or_tree     (rtx, rtx *, int, int);
321 static rtx simplify_test_exp    (rtx, int, int);
322 static rtx simplify_test_exp_in_temp (rtx, int, int);
323 static void optimize_attrs      (void);
324 static void gen_attr            (rtx, int);
325 static int count_alternatives   (rtx);
326 static int compares_alternatives_p (rtx);
327 static int contained_in_p       (rtx, rtx);
328 static void gen_insn            (rtx, int);
329 static void gen_delay           (rtx, int);
330 static void write_test_expr     (rtx, int);
331 static int max_attr_value       (rtx, int*);
332 static int or_attr_value        (rtx, int*);
333 static void walk_attr_value     (rtx);
334 static void write_attr_get      (struct attr_desc *);
335 static rtx eliminate_known_true (rtx, rtx, int, int);
336 static void write_attr_set      (struct attr_desc *, int, rtx,
337                                  const char *, const char *, rtx,
338                                  int, int);
339 static void write_attr_case     (struct attr_desc *, struct attr_value *,
340                                  int, const char *, const char *, int, rtx);
341 static void write_attr_valueq   (struct attr_desc *, const char *);
342 static void write_attr_value    (struct attr_desc *, rtx);
343 static void write_upcase        (const char *);
344 static void write_indent        (int);
345 static void write_eligible_delay (const char *);
346 static int write_expr_attr_cache (rtx, struct attr_desc *);
347 static void write_const_num_delay_slots (void);
348 static char *next_comma_elt     (const char **);
349 static struct attr_desc *find_attr (const char **, int);
350 static struct attr_value *find_most_used  (struct attr_desc *);
351 static rtx attr_eq              (const char *, const char *);
352 static const char *attr_numeral (int);
353 static int attr_equal_p         (rtx, rtx);
354 static rtx attr_copy_rtx        (rtx);
355 static int attr_rtx_cost        (rtx);
356 static bool attr_alt_subset_p (rtx, rtx);
357 static bool attr_alt_subset_of_compl_p (rtx, rtx);
358 static rtx attr_alt_intersection (rtx, rtx);
359 static rtx attr_alt_union (rtx, rtx);
360 static rtx attr_alt_complement (rtx);
361 static bool attr_alt_bit_p (rtx, int);
362 static rtx mk_attr_alt (int);
363
364 #define oballoc(size) obstack_alloc (hash_obstack, size)
365
366 /* Hash table for sharing RTL and strings.  */
367
368 /* Each hash table slot is a bucket containing a chain of these structures.
369    Strings are given negative hash codes; RTL expressions are given positive
370    hash codes.  */
371
372 struct attr_hash
373 {
374   struct attr_hash *next;       /* Next structure in the bucket.  */
375   int hashcode;                 /* Hash code of this rtx or string.  */
376   union
377     {
378       char *str;                /* The string (negative hash codes) */
379       rtx rtl;                  /* or the RTL recorded here.  */
380     } u;
381 };
382
383 /* Now here is the hash table.  When recording an RTL, it is added to
384    the slot whose index is the hash code mod the table size.  Note
385    that the hash table is used for several kinds of RTL (see attr_rtx)
386    and for strings.  While all these live in the same table, they are
387    completely independent, and the hash code is computed differently
388    for each.  */
389
390 #define RTL_HASH_SIZE 4093
391 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
392
393 /* Here is how primitive or already-shared RTL's hash
394    codes are made.  */
395 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
396
397 /* Add an entry to the hash table for RTL with hash code HASHCODE.  */
398
399 static void
400 attr_hash_add_rtx (int hashcode, rtx rtl)
401 {
402   struct attr_hash *h;
403
404   h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
405   h->hashcode = hashcode;
406   h->u.rtl = rtl;
407   h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
408   attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
409 }
410
411 /* Add an entry to the hash table for STRING with hash code HASHCODE.  */
412
413 static void
414 attr_hash_add_string (int hashcode, char *str)
415 {
416   struct attr_hash *h;
417
418   h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
419   h->hashcode = -hashcode;
420   h->u.str = str;
421   h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
422   attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
423 }
424
425 /* Generate an RTL expression, but avoid duplicates.
426    Set the ATTR_PERMANENT_P flag for these permanent objects.
427
428    In some cases we cannot uniquify; then we return an ordinary
429    impermanent rtx with ATTR_PERMANENT_P clear.
430
431    Args are as follows:
432
433    rtx attr_rtx (code, [element1, ..., elementn])  */
434
435 static rtx
436 attr_rtx_1 (enum rtx_code code, va_list p)
437 {
438   rtx rt_val = NULL_RTX;/* RTX to return to caller...           */
439   int hashcode;
440   struct attr_hash *h;
441   struct obstack *old_obstack = rtl_obstack;
442
443   /* For each of several cases, search the hash table for an existing entry.
444      Use that entry if one is found; otherwise create a new RTL and add it
445      to the table.  */
446
447   if (GET_RTX_CLASS (code) == RTX_UNARY)
448     {
449       rtx arg0 = va_arg (p, rtx);
450
451       /* A permanent object cannot point to impermanent ones.  */
452       if (! ATTR_PERMANENT_P (arg0))
453         {
454           rt_val = rtx_alloc (code);
455           XEXP (rt_val, 0) = arg0;
456           return rt_val;
457         }
458
459       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
460       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
461         if (h->hashcode == hashcode
462             && GET_CODE (h->u.rtl) == code
463             && XEXP (h->u.rtl, 0) == arg0)
464           return h->u.rtl;
465
466       if (h == 0)
467         {
468           rtl_obstack = hash_obstack;
469           rt_val = rtx_alloc (code);
470           XEXP (rt_val, 0) = arg0;
471         }
472     }
473   else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
474            || GET_RTX_CLASS (code) == RTX_COMM_ARITH
475            || GET_RTX_CLASS (code) == RTX_COMPARE
476            || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
477     {
478       rtx arg0 = va_arg (p, rtx);
479       rtx arg1 = va_arg (p, rtx);
480
481       /* A permanent object cannot point to impermanent ones.  */
482       if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
483         {
484           rt_val = rtx_alloc (code);
485           XEXP (rt_val, 0) = arg0;
486           XEXP (rt_val, 1) = arg1;
487           return rt_val;
488         }
489
490       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
491       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
492         if (h->hashcode == hashcode
493             && GET_CODE (h->u.rtl) == code
494             && XEXP (h->u.rtl, 0) == arg0
495             && XEXP (h->u.rtl, 1) == arg1)
496           return h->u.rtl;
497
498       if (h == 0)
499         {
500           rtl_obstack = hash_obstack;
501           rt_val = rtx_alloc (code);
502           XEXP (rt_val, 0) = arg0;
503           XEXP (rt_val, 1) = arg1;
504         }
505     }
506   else if (GET_RTX_LENGTH (code) == 1
507            && GET_RTX_FORMAT (code)[0] == 's')
508     {
509       char *arg0 = va_arg (p, char *);
510
511       arg0 = DEF_ATTR_STRING (arg0);
512
513       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
514       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
515         if (h->hashcode == hashcode
516             && GET_CODE (h->u.rtl) == code
517             && XSTR (h->u.rtl, 0) == arg0)
518           return h->u.rtl;
519
520       if (h == 0)
521         {
522           rtl_obstack = hash_obstack;
523           rt_val = rtx_alloc (code);
524           XSTR (rt_val, 0) = arg0;
525         }
526     }
527   else if (GET_RTX_LENGTH (code) == 2
528            && GET_RTX_FORMAT (code)[0] == 's'
529            && GET_RTX_FORMAT (code)[1] == 's')
530     {
531       char *arg0 = va_arg (p, char *);
532       char *arg1 = va_arg (p, char *);
533
534       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
535       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
536         if (h->hashcode == hashcode
537             && GET_CODE (h->u.rtl) == code
538             && XSTR (h->u.rtl, 0) == arg0
539             && XSTR (h->u.rtl, 1) == arg1)
540           return h->u.rtl;
541
542       if (h == 0)
543         {
544           rtl_obstack = hash_obstack;
545           rt_val = rtx_alloc (code);
546           XSTR (rt_val, 0) = arg0;
547           XSTR (rt_val, 1) = arg1;
548         }
549     }
550   else if (code == CONST_INT)
551     {
552       HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
553       if (arg0 == 0)
554         return false_rtx;
555       else if (arg0 == 1)
556         return true_rtx;
557       else
558         goto nohash;
559     }
560   else
561     {
562       int i;            /* Array indices...                     */
563       const char *fmt;  /* Current rtx's format...              */
564     nohash:
565       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
566
567       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
568       for (i = 0; i < GET_RTX_LENGTH (code); i++)
569         {
570           switch (*fmt++)
571             {
572             case '0':           /* Unused field.  */
573               break;
574
575             case 'i':           /* An integer?  */
576               XINT (rt_val, i) = va_arg (p, int);
577               break;
578
579             case 'w':           /* A wide integer? */
580               XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
581               break;
582
583             case 's':           /* A string?  */
584               XSTR (rt_val, i) = va_arg (p, char *);
585               break;
586
587             case 'e':           /* An expression?  */
588             case 'u':           /* An insn?  Same except when printing.  */
589               XEXP (rt_val, i) = va_arg (p, rtx);
590               break;
591
592             case 'E':           /* An RTX vector?  */
593               XVEC (rt_val, i) = va_arg (p, rtvec);
594               break;
595
596             default:
597               gcc_unreachable ();
598             }
599         }
600       return rt_val;
601     }
602
603   rtl_obstack = old_obstack;
604   attr_hash_add_rtx (hashcode, rt_val);
605   ATTR_PERMANENT_P (rt_val) = 1;
606   return rt_val;
607 }
608
609 static rtx
610 attr_rtx (enum rtx_code code, ...)
611 {
612   rtx result;
613   va_list p;
614
615   va_start (p, code);
616   result = attr_rtx_1 (code, p);
617   va_end (p);
618   return result;
619 }
620
621 /* Create a new string printed with the printf line arguments into a space
622    of at most LEN bytes:
623
624    rtx attr_printf (len, format, [arg1, ..., argn])  */
625
626 char *
627 attr_printf (unsigned int len, const char *fmt, ...)
628 {
629   char str[256];
630   va_list p;
631
632   va_start (p, fmt);
633
634   gcc_assert (len < sizeof str); /* Leave room for \0.  */
635
636   vsprintf (str, fmt, p);
637   va_end (p);
638
639   return DEF_ATTR_STRING (str);
640 }
641
642 static rtx
643 attr_eq (const char *name, const char *value)
644 {
645   return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
646 }
647
648 static const char *
649 attr_numeral (int n)
650 {
651   return XSTR (make_numeric_value (n), 0);
652 }
653
654 /* Return a permanent (possibly shared) copy of a string STR (not assumed
655    to be null terminated) with LEN bytes.  */
656
657 static char *
658 attr_string (const char *str, int len)
659 {
660   struct attr_hash *h;
661   int hashcode;
662   int i;
663   char *new_str;
664
665   /* Compute the hash code.  */
666   hashcode = (len + 1) * 613 + (unsigned) str[0];
667   for (i = 1; i <= len; i += 2)
668     hashcode = ((hashcode * 613) + (unsigned) str[i]);
669   if (hashcode < 0)
670     hashcode = -hashcode;
671
672   /* Search the table for the string.  */
673   for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
674     if (h->hashcode == -hashcode && h->u.str[0] == str[0]
675         && !strncmp (h->u.str, str, len))
676       return h->u.str;                  /* <-- return if found.  */
677
678   /* Not found; create a permanent copy and add it to the hash table.  */
679   new_str = obstack_alloc (hash_obstack, len + 1);
680   memcpy (new_str, str, len);
681   new_str[len] = '\0';
682   attr_hash_add_string (hashcode, new_str);
683
684   return new_str;                       /* Return the new string.  */
685 }
686
687 /* Check two rtx's for equality of contents,
688    taking advantage of the fact that if both are hashed
689    then they can't be equal unless they are the same object.  */
690
691 static int
692 attr_equal_p (rtx x, rtx y)
693 {
694   return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
695                      && rtx_equal_p (x, y)));
696 }
697
698 /* Copy an attribute value expression,
699    descending to all depths, but not copying any
700    permanent hashed subexpressions.  */
701
702 static rtx
703 attr_copy_rtx (rtx orig)
704 {
705   rtx copy;
706   int i, j;
707   RTX_CODE code;
708   const char *format_ptr;
709
710   /* No need to copy a permanent object.  */
711   if (ATTR_PERMANENT_P (orig))
712     return orig;
713
714   code = GET_CODE (orig);
715
716   switch (code)
717     {
718     case REG:
719     case CONST_INT:
720     case CONST_DOUBLE:
721     case CONST_VECTOR:
722     case SYMBOL_REF:
723     case CODE_LABEL:
724     case PC:
725     case CC0:
726       return orig;
727
728     default:
729       break;
730     }
731
732   copy = rtx_alloc (code);
733   PUT_MODE (copy, GET_MODE (orig));
734   ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
735   ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
736   ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
737   ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
738
739   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
740
741   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
742     {
743       switch (*format_ptr++)
744         {
745         case 'e':
746           XEXP (copy, i) = XEXP (orig, i);
747           if (XEXP (orig, i) != NULL)
748             XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
749           break;
750
751         case 'E':
752         case 'V':
753           XVEC (copy, i) = XVEC (orig, i);
754           if (XVEC (orig, i) != NULL)
755             {
756               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
757               for (j = 0; j < XVECLEN (copy, i); j++)
758                 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
759             }
760           break;
761
762         case 'n':
763         case 'i':
764           XINT (copy, i) = XINT (orig, i);
765           break;
766
767         case 'w':
768           XWINT (copy, i) = XWINT (orig, i);
769           break;
770
771         case 's':
772         case 'S':
773           XSTR (copy, i) = XSTR (orig, i);
774           break;
775
776         default:
777           gcc_unreachable ();
778         }
779     }
780   return copy;
781 }
782
783 /* Given a test expression for an attribute, ensure it is validly formed.
784    IS_CONST indicates whether the expression is constant for each compiler
785    run (a constant expression may not test any particular insn).
786
787    Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
788    and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")).  Do the latter
789    test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
790
791    Update the string address in EQ_ATTR expression to be the same used
792    in the attribute (or `alternative_name') to speed up subsequent
793    `find_attr' calls and eliminate most `strcmp' calls.
794
795    Return the new expression, if any.  */
796
797 rtx
798 check_attr_test (rtx exp, int is_const, int lineno)
799 {
800   struct attr_desc *attr;
801   struct attr_value *av;
802   const char *name_ptr, *p;
803   rtx orexp, newexp;
804
805   switch (GET_CODE (exp))
806     {
807     case EQ_ATTR:
808       /* Handle negation test.  */
809       if (XSTR (exp, 1)[0] == '!')
810         return check_attr_test (attr_rtx (NOT,
811                                           attr_eq (XSTR (exp, 0),
812                                                    &XSTR (exp, 1)[1])),
813                                 is_const, lineno);
814
815       else if (n_comma_elts (XSTR (exp, 1)) == 1)
816         {
817           attr = find_attr (&XSTR (exp, 0), 0);
818           if (attr == NULL)
819             {
820               if (! strcmp (XSTR (exp, 0), "alternative"))
821                 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
822               else
823                 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
824             }
825
826           if (is_const && ! attr->is_const)
827             fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
828                    XSTR (exp, 0));
829
830           /* Copy this just to make it permanent,
831              so expressions using it can be permanent too.  */
832           exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
833
834           /* It shouldn't be possible to simplify the value given to a
835              constant attribute, so don't expand this until it's time to
836              write the test expression.  */
837           if (attr->is_const)
838             ATTR_IND_SIMPLIFIED_P (exp) = 1;
839
840           if (attr->is_numeric)
841             {
842               for (p = XSTR (exp, 1); *p; p++)
843                 if (! ISDIGIT (*p))
844                   fatal ("attribute `%s' takes only numeric values",
845                          XSTR (exp, 0));
846             }
847           else
848             {
849               for (av = attr->first_value; av; av = av->next)
850                 if (GET_CODE (av->value) == CONST_STRING
851                     && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
852                   break;
853
854               if (av == NULL)
855                 fatal ("unknown value `%s' for `%s' attribute",
856                        XSTR (exp, 1), XSTR (exp, 0));
857             }
858         }
859       else
860         {
861           if (! strcmp (XSTR (exp, 0), "alternative"))
862             {
863               int set = 0;
864
865               name_ptr = XSTR (exp, 1);
866               while ((p = next_comma_elt (&name_ptr)) != NULL)
867                 set |= 1 << atoi (p);
868
869               return mk_attr_alt (set);
870             }
871           else
872             {
873               /* Make an IOR tree of the possible values.  */
874               orexp = false_rtx;
875               name_ptr = XSTR (exp, 1);
876               while ((p = next_comma_elt (&name_ptr)) != NULL)
877                 {
878                   newexp = attr_eq (XSTR (exp, 0), p);
879                   orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
880                 }
881
882               return check_attr_test (orexp, is_const, lineno);
883             }
884         }
885       break;
886
887     case ATTR_FLAG:
888       break;
889
890     case CONST_INT:
891       /* Either TRUE or FALSE.  */
892       if (XWINT (exp, 0))
893         return true_rtx;
894       else
895         return false_rtx;
896
897     case IOR:
898     case AND:
899       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
900       XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
901       break;
902
903     case NOT:
904       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
905       break;
906
907     case MATCH_OPERAND:
908       if (is_const)
909         fatal ("RTL operator \"%s\" not valid in constant attribute test",
910                GET_RTX_NAME (GET_CODE (exp)));
911       /* These cases can't be simplified.  */
912       ATTR_IND_SIMPLIFIED_P (exp) = 1;
913       break;
914
915     case LE:  case LT:  case GT:  case GE:
916     case LEU: case LTU: case GTU: case GEU:
917     case NE:  case EQ:
918       if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
919           && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
920         exp = attr_rtx (GET_CODE (exp),
921                         attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
922                         attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
923       /* These cases can't be simplified.  */
924       ATTR_IND_SIMPLIFIED_P (exp) = 1;
925       break;
926
927     case SYMBOL_REF:
928       if (is_const)
929         {
930           /* These cases are valid for constant attributes, but can't be
931              simplified.  */
932           exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
933           ATTR_IND_SIMPLIFIED_P (exp) = 1;
934           break;
935         }
936     default:
937       fatal ("RTL operator \"%s\" not valid in attribute test",
938              GET_RTX_NAME (GET_CODE (exp)));
939     }
940
941   return exp;
942 }
943
944 /* Given an expression, ensure that it is validly formed and that all named
945    attribute values are valid for the given attribute.  Issue a fatal error
946    if not.  If no attribute is specified, assume a numeric attribute.
947
948    Return a perhaps modified replacement expression for the value.  */
949
950 static rtx
951 check_attr_value (rtx exp, struct attr_desc *attr)
952 {
953   struct attr_value *av;
954   const char *p;
955   int i;
956
957   switch (GET_CODE (exp))
958     {
959     case CONST_INT:
960       if (attr && ! attr->is_numeric)
961         {
962           message_with_line (attr->lineno,
963                              "CONST_INT not valid for non-numeric attribute %s",
964                              attr->name);
965           have_error = 1;
966           break;
967         }
968
969       if (INTVAL (exp) < 0 && ! attr->negative_ok)
970         {
971           message_with_line (attr->lineno,
972                              "negative numeric value specified for attribute %s",
973                              attr->name);
974           have_error = 1;
975           break;
976         }
977       break;
978
979     case CONST_STRING:
980       if (! strcmp (XSTR (exp, 0), "*"))
981         break;
982
983       if (attr == 0 || attr->is_numeric)
984         {
985           p = XSTR (exp, 0);
986           if (attr && attr->negative_ok && *p == '-')
987             p++;
988           for (; *p; p++)
989             if (! ISDIGIT (*p))
990               {
991                 message_with_line (attr ? attr->lineno : 0,
992                                    "non-numeric value for numeric attribute %s",
993                                    attr ? attr->name : "internal");
994                 have_error = 1;
995                 break;
996               }
997           break;
998         }
999
1000       for (av = attr->first_value; av; av = av->next)
1001         if (GET_CODE (av->value) == CONST_STRING
1002             && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1003           break;
1004
1005       if (av == NULL)
1006         {
1007           message_with_line (attr->lineno,
1008                              "unknown value `%s' for `%s' attribute",
1009                              XSTR (exp, 0), attr ? attr->name : "internal");
1010           have_error = 1;
1011         }
1012       break;
1013
1014     case IF_THEN_ELSE:
1015       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1016                                        attr ? attr->is_const : 0,
1017                                        attr ? attr->lineno : 0);
1018       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1019       XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1020       break;
1021
1022     case PLUS:
1023     case MINUS:
1024     case MULT:
1025     case DIV:
1026     case MOD:
1027       if (attr && !attr->is_numeric)
1028         {
1029           message_with_line (attr->lineno,
1030                              "invalid operation `%s' for non-numeric attribute value",
1031                              GET_RTX_NAME (GET_CODE (exp)));
1032           have_error = 1;
1033           break;
1034         }
1035       /* Fall through.  */
1036
1037     case IOR:
1038     case AND:
1039       XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1040       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1041       break;
1042
1043     case FFS:
1044     case CLZ:
1045     case CTZ:
1046     case POPCOUNT:
1047     case PARITY:
1048       XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1049       break;
1050
1051     case COND:
1052       if (XVECLEN (exp, 0) % 2 != 0)
1053         {
1054           message_with_line (attr->lineno,
1055                              "first operand of COND must have even length");
1056           have_error = 1;
1057           break;
1058         }
1059
1060       for (i = 0; i < XVECLEN (exp, 0); i += 2)
1061         {
1062           XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1063                                                  attr ? attr->is_const : 0,
1064                                                  attr ? attr->lineno : 0);
1065           XVECEXP (exp, 0, i + 1)
1066             = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1067         }
1068
1069       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1070       break;
1071
1072     case ATTR:
1073       {
1074         struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
1075         if (attr2 == NULL)
1076           {
1077             message_with_line (attr ? attr->lineno : 0,
1078                                "unknown attribute `%s' in ATTR",
1079                                XSTR (exp, 0));
1080             have_error = 1;
1081           }
1082         else if (attr && attr->is_const && ! attr2->is_const)
1083           {
1084             message_with_line (attr->lineno,
1085                 "non-constant attribute `%s' referenced from `%s'",
1086                 XSTR (exp, 0), attr->name);
1087             have_error = 1;
1088           }
1089         else if (attr
1090                  && (attr->is_numeric != attr2->is_numeric
1091                      || (! attr->negative_ok && attr2->negative_ok)))
1092           {
1093             message_with_line (attr->lineno,
1094                 "numeric attribute mismatch calling `%s' from `%s'",
1095                 XSTR (exp, 0), attr->name);
1096             have_error = 1;
1097           }
1098       }
1099       break;
1100
1101     case SYMBOL_REF:
1102       /* A constant SYMBOL_REF is valid as a constant attribute test and
1103          is expanded later by make_canonical into a COND.  In a non-constant
1104          attribute test, it is left be.  */
1105       return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1106
1107     default:
1108       message_with_line (attr ? attr->lineno : 0,
1109                          "invalid operation `%s' for attribute value",
1110                          GET_RTX_NAME (GET_CODE (exp)));
1111       have_error = 1;
1112       break;
1113     }
1114
1115   return exp;
1116 }
1117
1118 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1119    It becomes a COND with each test being (eq_attr "alternative "n") */
1120
1121 static rtx
1122 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1123 {
1124   int num_alt = id->num_alternatives;
1125   rtx condexp;
1126   int i;
1127
1128   if (XVECLEN (exp, 1) != num_alt)
1129     {
1130       message_with_line (id->lineno,
1131                          "bad number of entries in SET_ATTR_ALTERNATIVE");
1132       have_error = 1;
1133       return NULL_RTX;
1134     }
1135
1136   /* Make a COND with all tests but the last.  Select the last value via the
1137      default.  */
1138   condexp = rtx_alloc (COND);
1139   XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1140
1141   for (i = 0; i < num_alt - 1; i++)
1142     {
1143       const char *p;
1144       p = attr_numeral (i);
1145
1146       XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1147       XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1148     }
1149
1150   XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1151
1152   return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1153 }
1154
1155 /* Given a SET_ATTR, convert to the appropriate SET.  If a comma-separated
1156    list of values is given, convert to SET_ATTR_ALTERNATIVE first.  */
1157
1158 static rtx
1159 convert_set_attr (rtx exp, struct insn_def *id)
1160 {
1161   rtx newexp;
1162   const char *name_ptr;
1163   char *p;
1164   int n;
1165
1166   /* See how many alternative specified.  */
1167   n = n_comma_elts (XSTR (exp, 1));
1168   if (n == 1)
1169     return attr_rtx (SET,
1170                      attr_rtx (ATTR, XSTR (exp, 0)),
1171                      attr_rtx (CONST_STRING, XSTR (exp, 1)));
1172
1173   newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1174   XSTR (newexp, 0) = XSTR (exp, 0);
1175   XVEC (newexp, 1) = rtvec_alloc (n);
1176
1177   /* Process each comma-separated name.  */
1178   name_ptr = XSTR (exp, 1);
1179   n = 0;
1180   while ((p = next_comma_elt (&name_ptr)) != NULL)
1181     XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1182
1183   return convert_set_attr_alternative (newexp, id);
1184 }
1185
1186 /* Scan all definitions, checking for validity.  Also, convert any SET_ATTR
1187    and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1188    expressions.  */
1189
1190 static void
1191 check_defs (void)
1192 {
1193   struct insn_def *id;
1194   struct attr_desc *attr;
1195   int i;
1196   rtx value;
1197
1198   for (id = defs; id; id = id->next)
1199     {
1200       if (XVEC (id->def, id->vec_idx) == NULL)
1201         continue;
1202
1203       for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1204         {
1205           value = XVECEXP (id->def, id->vec_idx, i);
1206           switch (GET_CODE (value))
1207             {
1208             case SET:
1209               if (GET_CODE (XEXP (value, 0)) != ATTR)
1210                 {
1211                   message_with_line (id->lineno, "bad attribute set");
1212                   have_error = 1;
1213                   value = NULL_RTX;
1214                 }
1215               break;
1216
1217             case SET_ATTR_ALTERNATIVE:
1218               value = convert_set_attr_alternative (value, id);
1219               break;
1220
1221             case SET_ATTR:
1222               value = convert_set_attr (value, id);
1223               break;
1224
1225             default:
1226               message_with_line (id->lineno, "invalid attribute code %s",
1227                                  GET_RTX_NAME (GET_CODE (value)));
1228               have_error = 1;
1229               value = NULL_RTX;
1230             }
1231           if (value == NULL_RTX)
1232             continue;
1233
1234           if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1235             {
1236               message_with_line (id->lineno, "unknown attribute %s",
1237                                  XSTR (XEXP (value, 0), 0));
1238               have_error = 1;
1239               continue;
1240             }
1241
1242           XVECEXP (id->def, id->vec_idx, i) = value;
1243           XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1244         }
1245     }
1246 }
1247
1248 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1249    expressions by converting them into a COND.  This removes cases from this
1250    program.  Also, replace an attribute value of "*" with the default attribute
1251    value.  */
1252
1253 static rtx
1254 make_canonical (struct attr_desc *attr, rtx exp)
1255 {
1256   int i;
1257   rtx newexp;
1258
1259   switch (GET_CODE (exp))
1260     {
1261     case CONST_INT:
1262       exp = make_numeric_value (INTVAL (exp));
1263       break;
1264
1265     case CONST_STRING:
1266       if (! strcmp (XSTR (exp, 0), "*"))
1267         {
1268           if (attr == 0 || attr->default_val == 0)
1269             fatal ("(attr_value \"*\") used in invalid context");
1270           exp = attr->default_val->value;
1271         }
1272       else
1273         XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1274
1275       break;
1276
1277     case SYMBOL_REF:
1278       if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1279         break;
1280       /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1281          This makes the COND something that won't be considered an arbitrary
1282          expression by walk_attr_value.  */
1283       ATTR_IND_SIMPLIFIED_P (exp) = 1;
1284       exp = check_attr_value (exp, attr);
1285       break;
1286
1287     case IF_THEN_ELSE:
1288       newexp = rtx_alloc (COND);
1289       XVEC (newexp, 0) = rtvec_alloc (2);
1290       XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1291       XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1292
1293       XEXP (newexp, 1) = XEXP (exp, 2);
1294
1295       exp = newexp;
1296       /* Fall through to COND case since this is now a COND.  */
1297
1298     case COND:
1299       {
1300         int allsame = 1;
1301         rtx defval;
1302
1303         /* First, check for degenerate COND.  */
1304         if (XVECLEN (exp, 0) == 0)
1305           return make_canonical (attr, XEXP (exp, 1));
1306         defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1307
1308         for (i = 0; i < XVECLEN (exp, 0); i += 2)
1309           {
1310             XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1311             XVECEXP (exp, 0, i + 1)
1312               = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1313             if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1314               allsame = 0;
1315           }
1316         if (allsame)
1317           return defval;
1318       }
1319       break;
1320
1321     default:
1322       break;
1323     }
1324
1325   return exp;
1326 }
1327
1328 static rtx
1329 copy_boolean (rtx exp)
1330 {
1331   if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1332     return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1333                      copy_boolean (XEXP (exp, 1)));
1334   if (GET_CODE (exp) == MATCH_OPERAND)
1335     {
1336       XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1337       XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1338     }
1339   else if (GET_CODE (exp) == EQ_ATTR)
1340     {
1341       XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1342       XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1343     }
1344
1345   return exp;
1346 }
1347
1348 /* Given a value and an attribute description, return a `struct attr_value *'
1349    that represents that value.  This is either an existing structure, if the
1350    value has been previously encountered, or a newly-created structure.
1351
1352    `insn_code' is the code of an insn whose attribute has the specified
1353    value (-2 if not processing an insn).  We ensure that all insns for
1354    a given value have the same number of alternatives if the value checks
1355    alternatives.  */
1356
1357 static struct attr_value *
1358 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1359 {
1360   struct attr_value *av;
1361   int num_alt = 0;
1362
1363   value = make_canonical (attr, value);
1364   if (compares_alternatives_p (value))
1365     {
1366       if (insn_code < 0 || insn_alternatives == NULL)
1367         fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1368       else
1369         num_alt = insn_alternatives[insn_code];
1370     }
1371
1372   for (av = attr->first_value; av; av = av->next)
1373     if (rtx_equal_p (value, av->value)
1374         && (num_alt == 0 || av->first_insn == NULL
1375             || insn_alternatives[av->first_insn->def->insn_code]))
1376       return av;
1377
1378   av = oballoc (sizeof (struct attr_value));
1379   av->value = value;
1380   av->next = attr->first_value;
1381   attr->first_value = av;
1382   av->first_insn = NULL;
1383   av->num_insns = 0;
1384   av->has_asm_insn = 0;
1385
1386   return av;
1387 }
1388
1389 /* After all DEFINE_DELAYs have been read in, create internal attributes
1390    to generate the required routines.
1391
1392    First, we compute the number of delay slots for each insn (as a COND of
1393    each of the test expressions in DEFINE_DELAYs).  Then, if more than one
1394    delay type is specified, we compute a similar function giving the
1395    DEFINE_DELAY ordinal for each insn.
1396
1397    Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1398    tells whether a given insn can be in that delay slot.
1399
1400    Normal attribute filling and optimization expands these to contain the
1401    information needed to handle delay slots.  */
1402
1403 static void
1404 expand_delays (void)
1405 {
1406   struct delay_desc *delay;
1407   rtx condexp;
1408   rtx newexp;
1409   int i;
1410   char *p;
1411
1412   /* First, generate data for `num_delay_slots' function.  */
1413
1414   condexp = rtx_alloc (COND);
1415   XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1416   XEXP (condexp, 1) = make_numeric_value (0);
1417
1418   for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1419     {
1420       XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1421       XVECEXP (condexp, 0, i + 1)
1422         = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1423     }
1424
1425   make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1426
1427   /* If more than one delay type, do the same for computing the delay type.  */
1428   if (num_delays > 1)
1429     {
1430       condexp = rtx_alloc (COND);
1431       XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1432       XEXP (condexp, 1) = make_numeric_value (0);
1433
1434       for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1435         {
1436           XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1437           XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1438         }
1439
1440       make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1441     }
1442
1443   /* For each delay possibility and delay slot, compute an eligibility
1444      attribute for non-annulled insns and for each type of annulled (annul
1445      if true and annul if false).  */
1446   for (delay = delays; delay; delay = delay->next)
1447     {
1448       for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1449         {
1450           condexp = XVECEXP (delay->def, 1, i);
1451           if (condexp == 0)
1452             condexp = false_rtx;
1453           newexp = attr_rtx (IF_THEN_ELSE, condexp,
1454                              make_numeric_value (1), make_numeric_value (0));
1455
1456           p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1457                            "*delay_%d_%d", delay->num, i / 3);
1458           make_internal_attr (p, newexp, ATTR_SPECIAL);
1459
1460           if (have_annul_true)
1461             {
1462               condexp = XVECEXP (delay->def, 1, i + 1);
1463               if (condexp == 0) condexp = false_rtx;
1464               newexp = attr_rtx (IF_THEN_ELSE, condexp,
1465                                  make_numeric_value (1),
1466                                  make_numeric_value (0));
1467               p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1468                                "*annul_true_%d_%d", delay->num, i / 3);
1469               make_internal_attr (p, newexp, ATTR_SPECIAL);
1470             }
1471
1472           if (have_annul_false)
1473             {
1474               condexp = XVECEXP (delay->def, 1, i + 2);
1475               if (condexp == 0) condexp = false_rtx;
1476               newexp = attr_rtx (IF_THEN_ELSE, condexp,
1477                                  make_numeric_value (1),
1478                                  make_numeric_value (0));
1479               p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1480                                "*annul_false_%d_%d", delay->num, i / 3);
1481               make_internal_attr (p, newexp, ATTR_SPECIAL);
1482             }
1483         }
1484     }
1485 }
1486
1487 /* Once all attributes and insns have been read and checked, we construct for
1488    each attribute value a list of all the insns that have that value for
1489    the attribute.  */
1490
1491 static void
1492 fill_attr (struct attr_desc *attr)
1493 {
1494   struct attr_value *av;
1495   struct insn_ent *ie;
1496   struct insn_def *id;
1497   int i;
1498   rtx value;
1499
1500   /* Don't fill constant attributes.  The value is independent of
1501      any particular insn.  */
1502   if (attr->is_const)
1503     return;
1504
1505   for (id = defs; id; id = id->next)
1506     {
1507       /* If no value is specified for this insn for this attribute, use the
1508          default.  */
1509       value = NULL;
1510       if (XVEC (id->def, id->vec_idx))
1511         for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1512           if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1513                               attr->name))
1514             value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1515
1516       if (value == NULL)
1517         av = attr->default_val;
1518       else
1519         av = get_attr_value (value, attr, id->insn_code);
1520
1521       ie = oballoc (sizeof (struct insn_ent));
1522       ie->def = id;
1523       insert_insn_ent (av, ie);
1524     }
1525 }
1526
1527 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1528    test that checks relative positions of insns (uses MATCH_DUP or PC).
1529    If so, replace it with what is obtained by passing the expression to
1530    ADDRESS_FN.  If not but it is a COND or IF_THEN_ELSE, call this routine
1531    recursively on each value (including the default value).  Otherwise,
1532    return the value returned by NO_ADDRESS_FN applied to EXP.  */
1533
1534 static rtx
1535 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1536                     rtx (*address_fn) (rtx))
1537 {
1538   int i;
1539   rtx newexp;
1540
1541   if (GET_CODE (exp) == COND)
1542     {
1543       /* See if any tests use addresses.  */
1544       address_used = 0;
1545       for (i = 0; i < XVECLEN (exp, 0); i += 2)
1546         walk_attr_value (XVECEXP (exp, 0, i));
1547
1548       if (address_used)
1549         return (*address_fn) (exp);
1550
1551       /* Make a new copy of this COND, replacing each element.  */
1552       newexp = rtx_alloc (COND);
1553       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1554       for (i = 0; i < XVECLEN (exp, 0); i += 2)
1555         {
1556           XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1557           XVECEXP (newexp, 0, i + 1)
1558             = substitute_address (XVECEXP (exp, 0, i + 1),
1559                                   no_address_fn, address_fn);
1560         }
1561
1562       XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1563                                              no_address_fn, address_fn);
1564
1565       return newexp;
1566     }
1567
1568   else if (GET_CODE (exp) == IF_THEN_ELSE)
1569     {
1570       address_used = 0;
1571       walk_attr_value (XEXP (exp, 0));
1572       if (address_used)
1573         return (*address_fn) (exp);
1574
1575       return attr_rtx (IF_THEN_ELSE,
1576                        substitute_address (XEXP (exp, 0),
1577                                            no_address_fn, address_fn),
1578                        substitute_address (XEXP (exp, 1),
1579                                            no_address_fn, address_fn),
1580                        substitute_address (XEXP (exp, 2),
1581                                            no_address_fn, address_fn));
1582     }
1583
1584   return (*no_address_fn) (exp);
1585 }
1586
1587 /* Make new attributes from the `length' attribute.  The following are made,
1588    each corresponding to a function called from `shorten_branches' or
1589    `get_attr_length':
1590
1591    *insn_default_length         This is the length of the insn to be returned
1592                                 by `get_attr_length' before `shorten_branches'
1593                                 has been called.  In each case where the length
1594                                 depends on relative addresses, the largest
1595                                 possible is used.  This routine is also used
1596                                 to compute the initial size of the insn.
1597
1598    *insn_variable_length_p      This returns 1 if the insn's length depends
1599                                 on relative addresses, zero otherwise.
1600
1601    *insn_current_length         This is only called when it is known that the
1602                                 insn has a variable length and returns the
1603                                 current length, based on relative addresses.
1604   */
1605
1606 static void
1607 make_length_attrs (void)
1608 {
1609   static const char *new_names[] =
1610     {
1611       "*insn_default_length",
1612       "*insn_variable_length_p",
1613       "*insn_current_length"
1614     };
1615   static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
1616   static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
1617   size_t i;
1618   struct attr_desc *length_attr, *new_attr;
1619   struct attr_value *av, *new_av;
1620   struct insn_ent *ie, *new_ie;
1621
1622   /* See if length attribute is defined.  If so, it must be numeric.  Make
1623      it special so we don't output anything for it.  */
1624   length_attr = find_attr (&length_str, 0);
1625   if (length_attr == 0)
1626     return;
1627
1628   if (! length_attr->is_numeric)
1629     fatal ("length attribute must be numeric");
1630
1631   length_attr->is_const = 0;
1632   length_attr->is_special = 1;
1633
1634   /* Make each new attribute, in turn.  */
1635   for (i = 0; i < ARRAY_SIZE (new_names); i++)
1636     {
1637       make_internal_attr (new_names[i],
1638                           substitute_address (length_attr->default_val->value,
1639                                               no_address_fn[i], address_fn[i]),
1640                           ATTR_NONE);
1641       new_attr = find_attr (&new_names[i], 0);
1642       for (av = length_attr->first_value; av; av = av->next)
1643         for (ie = av->first_insn; ie; ie = ie->next)
1644           {
1645             new_av = get_attr_value (substitute_address (av->value,
1646                                                          no_address_fn[i],
1647                                                          address_fn[i]),
1648                                      new_attr, ie->def->insn_code);
1649             new_ie = oballoc (sizeof (struct insn_ent));
1650             new_ie->def = ie->def;
1651             insert_insn_ent (new_av, new_ie);
1652           }
1653     }
1654 }
1655
1656 /* Utility functions called from above routine.  */
1657
1658 static rtx
1659 identity_fn (rtx exp)
1660 {
1661   return exp;
1662 }
1663
1664 static rtx
1665 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1666 {
1667   return make_numeric_value (0);
1668 }
1669
1670 static rtx
1671 one_fn (rtx exp ATTRIBUTE_UNUSED)
1672 {
1673   return make_numeric_value (1);
1674 }
1675
1676 static rtx
1677 max_fn (rtx exp)
1678 {
1679   int unknown;
1680   return make_numeric_value (max_attr_value (exp, &unknown));
1681 }
1682
1683 static void
1684 write_length_unit_log (void)
1685 {
1686   struct attr_desc *length_attr = find_attr (&length_str, 0);
1687   struct attr_value *av;
1688   struct insn_ent *ie;
1689   unsigned int length_unit_log, length_or;
1690   int unknown = 0;
1691
1692   if (length_attr == 0)
1693     return;
1694   length_or = or_attr_value (length_attr->default_val->value, &unknown);
1695   for (av = length_attr->first_value; av; av = av->next)
1696     for (ie = av->first_insn; ie; ie = ie->next)
1697       length_or |= or_attr_value (av->value, &unknown);
1698
1699   if (unknown)
1700     length_unit_log = 0;
1701   else
1702     {
1703       length_or = ~length_or;
1704       for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1705         length_unit_log++;
1706     }
1707   printf ("int length_unit_log = %u;\n", length_unit_log);
1708 }
1709
1710 /* Take a COND expression and see if any of the conditions in it can be
1711    simplified.  If any are known true or known false for the particular insn
1712    code, the COND can be further simplified.
1713
1714    Also call ourselves on any COND operations that are values of this COND.
1715
1716    We do not modify EXP; rather, we make and return a new rtx.  */
1717
1718 static rtx
1719 simplify_cond (rtx exp, int insn_code, int insn_index)
1720 {
1721   int i, j;
1722   /* We store the desired contents here,
1723      then build a new expression if they don't match EXP.  */
1724   rtx defval = XEXP (exp, 1);
1725   rtx new_defval = XEXP (exp, 1);
1726   int len = XVECLEN (exp, 0);
1727   rtx *tests = xmalloc (len * sizeof (rtx));
1728   int allsame = 1;
1729   rtx ret;
1730
1731   /* This lets us free all storage allocated below, if appropriate.  */
1732   obstack_finish (rtl_obstack);
1733
1734   memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1735
1736   /* See if default value needs simplification.  */
1737   if (GET_CODE (defval) == COND)
1738     new_defval = simplify_cond (defval, insn_code, insn_index);
1739
1740   /* Simplify the subexpressions, and see what tests we can get rid of.  */
1741
1742   for (i = 0; i < len; i += 2)
1743     {
1744       rtx newtest, newval;
1745
1746       /* Simplify this test.  */
1747       newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1748       tests[i] = newtest;
1749
1750       newval = tests[i + 1];
1751       /* See if this value may need simplification.  */
1752       if (GET_CODE (newval) == COND)
1753         newval = simplify_cond (newval, insn_code, insn_index);
1754
1755       /* Look for ways to delete or combine this test.  */
1756       if (newtest == true_rtx)
1757         {
1758           /* If test is true, make this value the default
1759              and discard this + any following tests.  */
1760           len = i;
1761           defval = tests[i + 1];
1762           new_defval = newval;
1763         }
1764
1765       else if (newtest == false_rtx)
1766         {
1767           /* If test is false, discard it and its value.  */
1768           for (j = i; j < len - 2; j++)
1769             tests[j] = tests[j + 2];
1770           i -= 2;
1771           len -= 2;
1772         }
1773
1774       else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1775         {
1776           /* If this value and the value for the prev test are the same,
1777              merge the tests.  */
1778
1779           tests[i - 2]
1780             = insert_right_side (IOR, tests[i - 2], newtest,
1781                                  insn_code, insn_index);
1782
1783           /* Delete this test/value.  */
1784           for (j = i; j < len - 2; j++)
1785             tests[j] = tests[j + 2];
1786           len -= 2;
1787           i -= 2;
1788         }
1789
1790       else
1791         tests[i + 1] = newval;
1792     }
1793
1794   /* If the last test in a COND has the same value
1795      as the default value, that test isn't needed.  */
1796
1797   while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1798     len -= 2;
1799
1800   /* See if we changed anything.  */
1801   if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1802     allsame = 0;
1803   else
1804     for (i = 0; i < len; i++)
1805       if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1806         {
1807           allsame = 0;
1808           break;
1809         }
1810
1811   if (len == 0)
1812     {
1813       if (GET_CODE (defval) == COND)
1814         ret = simplify_cond (defval, insn_code, insn_index);
1815       else
1816         ret = defval;
1817     }
1818   else if (allsame)
1819     ret = exp;
1820   else
1821     {
1822       rtx newexp = rtx_alloc (COND);
1823
1824       XVEC (newexp, 0) = rtvec_alloc (len);
1825       memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1826       XEXP (newexp, 1) = new_defval;
1827       ret = newexp;
1828     }
1829   free (tests);
1830   return ret;
1831 }
1832
1833 /* Remove an insn entry from an attribute value.  */
1834
1835 static void
1836 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1837 {
1838   struct insn_ent *previe;
1839
1840   if (av->first_insn == ie)
1841     av->first_insn = ie->next;
1842   else
1843     {
1844       for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1845         ;
1846       previe->next = ie->next;
1847     }
1848
1849   av->num_insns--;
1850   if (ie->def->insn_code == -1)
1851     av->has_asm_insn = 0;
1852
1853   num_insn_ents--;
1854 }
1855
1856 /* Insert an insn entry in an attribute value list.  */
1857
1858 static void
1859 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1860 {
1861   ie->next = av->first_insn;
1862   av->first_insn = ie;
1863   av->num_insns++;
1864   if (ie->def->insn_code == -1)
1865     av->has_asm_insn = 1;
1866
1867   num_insn_ents++;
1868 }
1869
1870 /* This is a utility routine to take an expression that is a tree of either
1871    AND or IOR expressions and insert a new term.  The new term will be
1872    inserted at the right side of the first node whose code does not match
1873    the root.  A new node will be created with the root's code.  Its left
1874    side will be the old right side and its right side will be the new
1875    term.
1876
1877    If the `term' is itself a tree, all its leaves will be inserted.  */
1878
1879 static rtx
1880 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1881 {
1882   rtx newexp;
1883
1884   /* Avoid consing in some special cases.  */
1885   if (code == AND && term == true_rtx)
1886     return exp;
1887   if (code == AND && term == false_rtx)
1888     return false_rtx;
1889   if (code == AND && exp == true_rtx)
1890     return term;
1891   if (code == AND && exp == false_rtx)
1892     return false_rtx;
1893   if (code == IOR && term == true_rtx)
1894     return true_rtx;
1895   if (code == IOR && term == false_rtx)
1896     return exp;
1897   if (code == IOR && exp == true_rtx)
1898     return true_rtx;
1899   if (code == IOR && exp == false_rtx)
1900     return term;
1901   if (attr_equal_p (exp, term))
1902     return exp;
1903
1904   if (GET_CODE (term) == code)
1905     {
1906       exp = insert_right_side (code, exp, XEXP (term, 0),
1907                                insn_code, insn_index);
1908       exp = insert_right_side (code, exp, XEXP (term, 1),
1909                                insn_code, insn_index);
1910
1911       return exp;
1912     }
1913
1914   if (GET_CODE (exp) == code)
1915     {
1916       rtx new = insert_right_side (code, XEXP (exp, 1),
1917                                    term, insn_code, insn_index);
1918       if (new != XEXP (exp, 1))
1919         /* Make a copy of this expression and call recursively.  */
1920         newexp = attr_rtx (code, XEXP (exp, 0), new);
1921       else
1922         newexp = exp;
1923     }
1924   else
1925     {
1926       /* Insert the new term.  */
1927       newexp = attr_rtx (code, exp, term);
1928     }
1929
1930   return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1931 }
1932
1933 /* If we have an expression which AND's a bunch of
1934         (not (eq_attrq "alternative" "n"))
1935    terms, we may have covered all or all but one of the possible alternatives.
1936    If so, we can optimize.  Similarly for IOR's of EQ_ATTR.
1937
1938    This routine is passed an expression and either AND or IOR.  It returns a
1939    bitmask indicating which alternatives are mentioned within EXP.  */
1940
1941 static int
1942 compute_alternative_mask (rtx exp, enum rtx_code code)
1943 {
1944   const char *string;
1945   if (GET_CODE (exp) == code)
1946     return compute_alternative_mask (XEXP (exp, 0), code)
1947            | compute_alternative_mask (XEXP (exp, 1), code);
1948
1949   else if (code == AND && GET_CODE (exp) == NOT
1950            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1951            && XSTR (XEXP (exp, 0), 0) == alternative_name)
1952     string = XSTR (XEXP (exp, 0), 1);
1953
1954   else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1955            && XSTR (exp, 0) == alternative_name)
1956     string = XSTR (exp, 1);
1957
1958   else if (GET_CODE (exp) == EQ_ATTR_ALT)
1959     {
1960       if (code == AND && XINT (exp, 1))
1961         return XINT (exp, 0);
1962
1963       if (code == IOR && !XINT (exp, 1))
1964         return XINT (exp, 0);
1965
1966       return 0;
1967     }
1968   else
1969     return 0;
1970
1971   if (string[1] == 0)
1972     return 1 << (string[0] - '0');
1973   return 1 << atoi (string);
1974 }
1975
1976 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1977    attribute with the value represented by that bit.  */
1978
1979 static rtx
1980 make_alternative_compare (int mask)
1981 {
1982   return mk_attr_alt (mask);
1983 }
1984
1985 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1986    of "attr" for this insn code.  From that value, we can compute a test
1987    showing when the EQ_ATTR will be true.  This routine performs that
1988    computation.  If a test condition involves an address, we leave the EQ_ATTR
1989    intact because addresses are only valid for the `length' attribute.
1990
1991    EXP is the EQ_ATTR expression and VALUE is the value of that attribute
1992    for the insn corresponding to INSN_CODE and INSN_INDEX.  */
1993
1994 static rtx
1995 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
1996 {
1997   rtx orexp, andexp;
1998   rtx right;
1999   rtx newexp;
2000   int i;
2001
2002   switch (GET_CODE (value))
2003     {
2004     case CONST_STRING:
2005       if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
2006         newexp = true_rtx;
2007       else
2008         newexp = false_rtx;
2009       break;
2010       
2011     case SYMBOL_REF:
2012       {
2013         char *p;
2014         char string[256];
2015         
2016         gcc_assert (GET_CODE (exp) == EQ_ATTR);
2017         gcc_assert (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2
2018                     <= 256);
2019         
2020         strcpy (string, XSTR (exp, 0));
2021         strcat (string, "_");
2022         strcat (string, XSTR (exp, 1));
2023         for (p = string; *p; p++)
2024           *p = TOUPPER (*p);
2025         
2026         newexp = attr_rtx (EQ, value,
2027                            attr_rtx (SYMBOL_REF,
2028                                      DEF_ATTR_STRING (string)));
2029         break;
2030       }
2031
2032     case COND:
2033       /* We construct an IOR of all the cases for which the
2034          requested attribute value is present.  Since we start with
2035          FALSE, if it is not present, FALSE will be returned.
2036           
2037          Each case is the AND of the NOT's of the previous conditions with the
2038          current condition; in the default case the current condition is TRUE.
2039           
2040          For each possible COND value, call ourselves recursively.
2041           
2042          The extra TRUE and FALSE expressions will be eliminated by another
2043          call to the simplification routine.  */
2044
2045       orexp = false_rtx;
2046       andexp = true_rtx;
2047
2048       if (current_alternative_string)
2049         clear_struct_flag (value);
2050
2051       for (i = 0; i < XVECLEN (value, 0); i += 2)
2052         {
2053           rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2054                                                 insn_code, insn_index);
2055
2056           SIMPLIFY_ALTERNATIVE (this);
2057
2058           right = insert_right_side (AND, andexp, this,
2059                                      insn_code, insn_index);
2060           right = insert_right_side (AND, right,
2061                                      evaluate_eq_attr (exp,
2062                                                        XVECEXP (value, 0,
2063                                                                 i + 1),
2064                                                        insn_code, insn_index),
2065                                      insn_code, insn_index);
2066           orexp = insert_right_side (IOR, orexp, right,
2067                                      insn_code, insn_index);
2068
2069           /* Add this condition into the AND expression.  */
2070           newexp = attr_rtx (NOT, this);
2071           andexp = insert_right_side (AND, andexp, newexp,
2072                                       insn_code, insn_index);
2073         }
2074
2075       /* Handle the default case.  */
2076       right = insert_right_side (AND, andexp,
2077                                  evaluate_eq_attr (exp, XEXP (value, 1),
2078                                                    insn_code, insn_index),
2079                                  insn_code, insn_index);
2080       newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2081       break;
2082
2083     default:
2084       gcc_unreachable ();
2085     }
2086
2087   /* If uses an address, must return original expression.  But set the
2088      ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again.  */
2089
2090   address_used = 0;
2091   walk_attr_value (newexp);
2092
2093   if (address_used)
2094     {
2095       /* This had `&& current_alternative_string', which seems to be wrong.  */
2096       if (! ATTR_IND_SIMPLIFIED_P (exp))
2097         return copy_rtx_unchanging (exp);
2098       return exp;
2099     }
2100   else
2101     return newexp;
2102 }
2103
2104 /* This routine is called when an AND of a term with a tree of AND's is
2105    encountered.  If the term or its complement is present in the tree, it
2106    can be replaced with TRUE or FALSE, respectively.
2107
2108    Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2109    be true and hence are complementary.
2110
2111    There is one special case:  If we see
2112         (and (not (eq_attr "att" "v1"))
2113              (eq_attr "att" "v2"))
2114    this can be replaced by (eq_attr "att" "v2").  To do this we need to
2115    replace the term, not anything in the AND tree.  So we pass a pointer to
2116    the term.  */
2117
2118 static rtx
2119 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2120 {
2121   rtx left, right;
2122   rtx newexp;
2123   rtx temp;
2124   int left_eliminates_term, right_eliminates_term;
2125
2126   if (GET_CODE (exp) == AND)
2127     {
2128       left  = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2129       right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2130       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2131         {
2132           newexp = attr_rtx (AND, left, right);
2133
2134           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2135         }
2136     }
2137
2138   else if (GET_CODE (exp) == IOR)
2139     {
2140       /* For the IOR case, we do the same as above, except that we can
2141          only eliminate `term' if both sides of the IOR would do so.  */
2142       temp = *pterm;
2143       left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2144       left_eliminates_term = (temp == true_rtx);
2145
2146       temp = *pterm;
2147       right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2148       right_eliminates_term = (temp == true_rtx);
2149
2150       if (left_eliminates_term && right_eliminates_term)
2151         *pterm = true_rtx;
2152
2153       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2154         {
2155           newexp = attr_rtx (IOR, left, right);
2156
2157           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2158         }
2159     }
2160
2161   /* Check for simplifications.  Do some extra checking here since this
2162      routine is called so many times.  */
2163
2164   if (exp == *pterm)
2165     return true_rtx;
2166
2167   else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2168     return false_rtx;
2169
2170   else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2171     return false_rtx;
2172
2173   else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2174     {
2175       if (attr_alt_subset_p (*pterm, exp))
2176         return true_rtx;
2177
2178       if (attr_alt_subset_of_compl_p (*pterm, exp))
2179         return false_rtx;
2180
2181       if (attr_alt_subset_p (exp, *pterm))
2182         *pterm = true_rtx;
2183         
2184       return exp;
2185     }
2186
2187   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2188     {
2189       if (XSTR (exp, 0) != XSTR (*pterm, 0))
2190         return exp;
2191
2192       if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2193         return true_rtx;
2194       else
2195         return false_rtx;
2196     }
2197
2198   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2199            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2200     {
2201       if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2202         return exp;
2203
2204       if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2205         return false_rtx;
2206       else
2207         return true_rtx;
2208     }
2209
2210   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2211            && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2212     {
2213       if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2214         return exp;
2215
2216       if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2217         return false_rtx;
2218       else
2219         *pterm = true_rtx;
2220     }
2221
2222   else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2223     {
2224       if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2225         return true_rtx;
2226     }
2227
2228   else if (GET_CODE (exp) == NOT)
2229     {
2230       if (attr_equal_p (XEXP (exp, 0), *pterm))
2231         return false_rtx;
2232     }
2233
2234   else if (GET_CODE (*pterm) == NOT)
2235     {
2236       if (attr_equal_p (XEXP (*pterm, 0), exp))
2237         return false_rtx;
2238     }
2239
2240   else if (attr_equal_p (exp, *pterm))
2241     return true_rtx;
2242
2243   return exp;
2244 }
2245
2246 /* Similar to `simplify_and_tree', but for IOR trees.  */
2247
2248 static rtx
2249 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2250 {
2251   rtx left, right;
2252   rtx newexp;
2253   rtx temp;
2254   int left_eliminates_term, right_eliminates_term;
2255
2256   if (GET_CODE (exp) == IOR)
2257     {
2258       left  = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2259       right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2260       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2261         {
2262           newexp = attr_rtx (GET_CODE (exp), left, right);
2263
2264           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2265         }
2266     }
2267
2268   else if (GET_CODE (exp) == AND)
2269     {
2270       /* For the AND case, we do the same as above, except that we can
2271          only eliminate `term' if both sides of the AND would do so.  */
2272       temp = *pterm;
2273       left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2274       left_eliminates_term = (temp == false_rtx);
2275
2276       temp = *pterm;
2277       right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2278       right_eliminates_term = (temp == false_rtx);
2279
2280       if (left_eliminates_term && right_eliminates_term)
2281         *pterm = false_rtx;
2282
2283       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2284         {
2285           newexp = attr_rtx (GET_CODE (exp), left, right);
2286
2287           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2288         }
2289     }
2290
2291   if (attr_equal_p (exp, *pterm))
2292     return false_rtx;
2293
2294   else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2295     return true_rtx;
2296
2297   else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2298     return true_rtx;
2299
2300   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2301            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2302            && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2303     *pterm = false_rtx;
2304
2305   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2306            && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2307            && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2308     return false_rtx;
2309
2310   return exp;
2311 }
2312
2313 /* Compute approximate cost of the expression.  Used to decide whether
2314    expression is cheap enough for inline.  */
2315 static int
2316 attr_rtx_cost (rtx x)
2317 {
2318   int cost = 0;
2319   enum rtx_code code;
2320   if (!x)
2321     return 0;
2322   code = GET_CODE (x);
2323   switch (code)
2324     {
2325     case MATCH_OPERAND:
2326       if (XSTR (x, 1)[0])
2327         return 10;
2328       else
2329         return 0;
2330
2331     case EQ_ATTR_ALT:
2332       return 0;
2333
2334     case EQ_ATTR:
2335       /* Alternatives don't result into function call.  */
2336       if (!strcmp_check (XSTR (x, 0), alternative_name))
2337         return 0;
2338       else
2339         return 5;
2340     default:
2341       {
2342         int i, j;
2343         const char *fmt = GET_RTX_FORMAT (code);
2344         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2345           {
2346             switch (fmt[i])
2347               {
2348               case 'V':
2349               case 'E':
2350                 for (j = 0; j < XVECLEN (x, i); j++)
2351                   cost += attr_rtx_cost (XVECEXP (x, i, j));
2352                 break;
2353               case 'e':
2354                 cost += attr_rtx_cost (XEXP (x, i));
2355                 break;
2356               }
2357           }
2358       }
2359       break;
2360     }
2361   return cost;
2362 }
2363
2364 /* Simplify test expression and use temporary obstack in order to avoid
2365    memory bloat.  Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2366    and avoid unnecessary copying if possible.  */
2367
2368 static rtx
2369 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2370 {
2371   rtx x;
2372   struct obstack *old;
2373   if (ATTR_IND_SIMPLIFIED_P (exp))
2374     return exp;
2375   old = rtl_obstack;
2376   rtl_obstack = temp_obstack;
2377   x = simplify_test_exp (exp, insn_code, insn_index);
2378   rtl_obstack = old;
2379   if (x == exp || rtl_obstack == temp_obstack)
2380     return x;
2381   return attr_copy_rtx (x);
2382 }
2383
2384 /* Returns true if S1 is a subset of S2.  */
2385
2386 static bool
2387 attr_alt_subset_p (rtx s1, rtx s2)
2388 {
2389   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2390     {
2391     case (0 << 1) | 0:
2392       return !(XINT (s1, 0) &~ XINT (s2, 0));
2393
2394     case (0 << 1) | 1:
2395       return !(XINT (s1, 0) & XINT (s2, 0));
2396
2397     case (1 << 1) | 0:
2398       return false;
2399
2400     case (1 << 1) | 1:
2401       return !(XINT (s2, 0) &~ XINT (s1, 0));
2402
2403     default:
2404       gcc_unreachable ();
2405     }
2406 }
2407
2408 /* Returns true if S1 is a subset of complement of S2.  */
2409
2410 static bool
2411 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2412 {
2413   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2414     {
2415     case (0 << 1) | 0:
2416       return !(XINT (s1, 0) & XINT (s2, 0));
2417
2418     case (0 << 1) | 1:
2419       return !(XINT (s1, 0) & ~XINT (s2, 0));
2420
2421     case (1 << 1) | 0:
2422       return !(XINT (s2, 0) &~ XINT (s1, 0));
2423
2424     case (1 << 1) | 1:
2425       return false;
2426
2427     default:
2428       gcc_unreachable ();
2429     }
2430 }
2431
2432 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2.  */
2433
2434 static rtx
2435 attr_alt_intersection (rtx s1, rtx s2)
2436 {
2437   rtx result = rtx_alloc (EQ_ATTR_ALT);
2438
2439   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2440     {
2441     case (0 << 1) | 0:
2442       XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2443       break;
2444     case (0 << 1) | 1:
2445       XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2446       break;
2447     case (1 << 1) | 0:
2448       XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2449       break;
2450     case (1 << 1) | 1:
2451       XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2452       break;
2453     default:
2454       gcc_unreachable ();
2455     }
2456   XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2457
2458   return result;
2459 }
2460
2461 /* Return EQ_ATTR_ALT expression representing union of S1 and S2.  */
2462
2463 static rtx
2464 attr_alt_union (rtx s1, rtx s2)
2465 {
2466   rtx result = rtx_alloc (EQ_ATTR_ALT);
2467
2468   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2469     {
2470     case (0 << 1) | 0:
2471       XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2472       break;
2473     case (0 << 1) | 1:
2474       XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2475       break;
2476     case (1 << 1) | 0:
2477       XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2478       break;
2479     case (1 << 1) | 1:
2480       XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2481       break;
2482     default:
2483       gcc_unreachable ();
2484     }
2485
2486   XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2487   return result;
2488 }
2489
2490 /* Return EQ_ATTR_ALT expression representing complement of S.  */
2491
2492 static rtx
2493 attr_alt_complement (rtx s)
2494 {
2495   rtx result = rtx_alloc (EQ_ATTR_ALT);
2496
2497   XINT (result, 0) = XINT (s, 0);
2498   XINT (result, 1) = 1 - XINT (s, 1);
2499
2500   return result;
2501 }
2502
2503 /* Tests whether a bit B belongs to the set represented by S.  */
2504
2505 static bool
2506 attr_alt_bit_p (rtx s, int b)
2507 {
2508   return XINT (s, 1) ^ ((XINT (s, 0) >> b) & 1);
2509 }
2510
2511 /* Return EQ_ATTR_ALT expression representing set containing elements set
2512    in E.  */
2513
2514 static rtx
2515 mk_attr_alt (int e)
2516 {
2517   rtx result = rtx_alloc (EQ_ATTR_ALT);
2518
2519   XINT (result, 0) = e;
2520   XINT (result, 1) = 0;
2521
2522   return result;
2523 }
2524
2525 /* Given an expression, see if it can be simplified for a particular insn
2526    code based on the values of other attributes being tested.  This can
2527    eliminate nested get_attr_... calls.
2528
2529    Note that if an endless recursion is specified in the patterns, the
2530    optimization will loop.  However, it will do so in precisely the cases where
2531    an infinite recursion loop could occur during compilation.  It's better that
2532    it occurs here!  */
2533
2534 static rtx
2535 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2536 {
2537   rtx left, right;
2538   struct attr_desc *attr;
2539   struct attr_value *av;
2540   struct insn_ent *ie;
2541   int i;
2542   rtx newexp = exp;
2543   bool left_alt, right_alt;
2544
2545   /* Don't re-simplify something we already simplified.  */
2546   if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2547     return exp;
2548
2549   switch (GET_CODE (exp))
2550     {
2551     case AND:
2552       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2553       SIMPLIFY_ALTERNATIVE (left);
2554       if (left == false_rtx)
2555         return false_rtx;
2556       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2557       SIMPLIFY_ALTERNATIVE (right);
2558       if (left == false_rtx)
2559         return false_rtx;
2560
2561       if (GET_CODE (left) == EQ_ATTR_ALT
2562           && GET_CODE (right) == EQ_ATTR_ALT)
2563         {
2564           exp = attr_alt_intersection (left, right);
2565           return simplify_test_exp (exp, insn_code, insn_index);
2566         }
2567
2568       /* If either side is an IOR and we have (eq_attr "alternative" ..")
2569          present on both sides, apply the distributive law since this will
2570          yield simplifications.  */
2571       if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2572           && compute_alternative_mask (left, IOR)
2573           && compute_alternative_mask (right, IOR))
2574         {
2575           if (GET_CODE (left) == IOR)
2576             {
2577               rtx tem = left;
2578               left = right;
2579               right = tem;
2580             }
2581
2582           newexp = attr_rtx (IOR,
2583                              attr_rtx (AND, left, XEXP (right, 0)),
2584                              attr_rtx (AND, left, XEXP (right, 1)));
2585
2586           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2587         }
2588
2589       /* Try with the term on both sides.  */
2590       right = simplify_and_tree (right, &left, insn_code, insn_index);
2591       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2592         left = simplify_and_tree (left, &right, insn_code, insn_index);
2593
2594       if (left == false_rtx || right == false_rtx)
2595         return false_rtx;
2596       else if (left == true_rtx)
2597         {
2598           return right;
2599         }
2600       else if (right == true_rtx)
2601         {
2602           return left;
2603         }
2604       /* See if all or all but one of the insn's alternatives are specified
2605          in this tree.  Optimize if so.  */
2606
2607       if (GET_CODE (left) == NOT)
2608         left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2609                     && XSTR (XEXP (left, 0), 0) == alternative_name);
2610       else
2611         left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2612                     && XINT (left, 1));
2613
2614       if (GET_CODE (right) == NOT)
2615         right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2616                      && XSTR (XEXP (right, 0), 0) == alternative_name);
2617       else
2618         right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2619                      && XINT (right, 1));
2620
2621       if (insn_code >= 0
2622           && (GET_CODE (left) == AND
2623               || left_alt
2624               || GET_CODE (right) == AND
2625               || right_alt))
2626         {
2627           i = compute_alternative_mask (exp, AND);
2628           if (i & ~insn_alternatives[insn_code])
2629             fatal ("invalid alternative specified for pattern number %d",
2630                    insn_index);
2631
2632           /* If all alternatives are excluded, this is false.  */
2633           i ^= insn_alternatives[insn_code];
2634           if (i == 0)
2635             return false_rtx;
2636           else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2637             {
2638               /* If just one excluded, AND a comparison with that one to the
2639                  front of the tree.  The others will be eliminated by
2640                  optimization.  We do not want to do this if the insn has one
2641                  alternative and we have tested none of them!  */
2642               left = make_alternative_compare (i);
2643               right = simplify_and_tree (exp, &left, insn_code, insn_index);
2644               newexp = attr_rtx (AND, left, right);
2645
2646               return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2647             }
2648         }
2649
2650       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2651         {
2652           newexp = attr_rtx (AND, left, right);
2653           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2654         }
2655       break;
2656
2657     case IOR:
2658       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2659       SIMPLIFY_ALTERNATIVE (left);
2660       if (left == true_rtx)
2661         return true_rtx;
2662       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2663       SIMPLIFY_ALTERNATIVE (right);
2664       if (right == true_rtx)
2665         return true_rtx;
2666
2667       if (GET_CODE (left) == EQ_ATTR_ALT
2668           && GET_CODE (right) == EQ_ATTR_ALT)
2669         {
2670           exp = attr_alt_union (left, right);
2671           return simplify_test_exp (exp, insn_code, insn_index);
2672         }
2673
2674       right = simplify_or_tree (right, &left, insn_code, insn_index);
2675       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2676         left = simplify_or_tree (left, &right, insn_code, insn_index);
2677
2678       if (right == true_rtx || left == true_rtx)
2679         return true_rtx;
2680       else if (left == false_rtx)
2681         {
2682           return right;
2683         }
2684       else if (right == false_rtx)
2685         {
2686           return left;
2687         }
2688
2689       /* Test for simple cases where the distributive law is useful.  I.e.,
2690             convert (ior (and (x) (y))
2691                          (and (x) (z)))
2692             to      (and (x)
2693                          (ior (y) (z)))
2694        */
2695
2696       else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2697                && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2698         {
2699           newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2700
2701           left = XEXP (left, 0);
2702           right = newexp;
2703           newexp = attr_rtx (AND, left, right);
2704           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2705         }
2706
2707       /* See if all or all but one of the insn's alternatives are specified
2708          in this tree.  Optimize if so.  */
2709
2710       else if (insn_code >= 0
2711                && (GET_CODE (left) == IOR
2712                    || (GET_CODE (left) == EQ_ATTR_ALT
2713                        && !XINT (left, 1))
2714                    || (GET_CODE (left) == EQ_ATTR
2715                        && XSTR (left, 0) == alternative_name)
2716                    || GET_CODE (right) == IOR
2717                    || (GET_CODE (right) == EQ_ATTR_ALT
2718                        && !XINT (right, 1))
2719                    || (GET_CODE (right) == EQ_ATTR
2720                        && XSTR (right, 0) == alternative_name)))
2721         {
2722           i = compute_alternative_mask (exp, IOR);
2723           if (i & ~insn_alternatives[insn_code])
2724             fatal ("invalid alternative specified for pattern number %d",
2725                    insn_index);
2726
2727           /* If all alternatives are included, this is true.  */
2728           i ^= insn_alternatives[insn_code];
2729           if (i == 0)
2730             return true_rtx;
2731           else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2732             {
2733               /* If just one excluded, IOR a comparison with that one to the
2734                  front of the tree.  The others will be eliminated by
2735                  optimization.  We do not want to do this if the insn has one
2736                  alternative and we have tested none of them!  */
2737               left = make_alternative_compare (i);
2738               right = simplify_and_tree (exp, &left, insn_code, insn_index);
2739               newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2740
2741               return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2742             }
2743         }
2744
2745       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2746         {
2747           newexp = attr_rtx (IOR, left, right);
2748           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2749         }
2750       break;
2751
2752     case NOT:
2753       if (GET_CODE (XEXP (exp, 0)) == NOT)
2754         {
2755           left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2756                                     insn_code, insn_index);
2757           SIMPLIFY_ALTERNATIVE (left);
2758           return left;
2759         }
2760
2761       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2762       SIMPLIFY_ALTERNATIVE (left);
2763       if (GET_CODE (left) == NOT)
2764         return XEXP (left, 0);
2765
2766       if (left == false_rtx)
2767         return true_rtx;
2768       if (left == true_rtx)
2769         return false_rtx;
2770
2771       if (GET_CODE (left) == EQ_ATTR_ALT)
2772         {
2773           exp = attr_alt_complement (left);
2774           return simplify_test_exp (exp, insn_code, insn_index);
2775         }
2776
2777       /* Try to apply De`Morgan's laws.  */
2778       if (GET_CODE (left) == IOR)
2779         {
2780           newexp = attr_rtx (AND,
2781                              attr_rtx (NOT, XEXP (left, 0)),
2782                              attr_rtx (NOT, XEXP (left, 1)));
2783
2784           newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2785         }
2786       else if (GET_CODE (left) == AND)
2787         {
2788           newexp = attr_rtx (IOR,
2789                              attr_rtx (NOT, XEXP (left, 0)),
2790                              attr_rtx (NOT, XEXP (left, 1)));
2791
2792           newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2793         }
2794       else if (left != XEXP (exp, 0))
2795         {
2796           newexp = attr_rtx (NOT, left);
2797         }
2798       break;
2799
2800     case EQ_ATTR_ALT:
2801       if (current_alternative_string)
2802         return attr_alt_bit_p (exp, atoi (current_alternative_string)) ? true_rtx : false_rtx;
2803
2804       if (!XINT (exp, 0))
2805         return XINT (exp, 1) ? true_rtx : false_rtx;
2806       break;
2807
2808     case EQ_ATTR:
2809       if (current_alternative_string && XSTR (exp, 0) == alternative_name)
2810         return (XSTR (exp, 1) == current_alternative_string
2811                 ? true_rtx : false_rtx);
2812
2813       if (XSTR (exp, 0) == alternative_name)
2814         {
2815           newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2816           break;
2817         }
2818
2819       /* Look at the value for this insn code in the specified attribute.
2820          We normally can replace this comparison with the condition that
2821          would give this insn the values being tested for.  */
2822       if (XSTR (exp, 0) != alternative_name
2823           && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2824         for (av = attr->first_value; av; av = av->next)
2825           for (ie = av->first_insn; ie; ie = ie->next)
2826             if (ie->def->insn_code == insn_code)
2827               {
2828                 rtx x;
2829                 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
2830                 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2831                 if (attr_rtx_cost(x) < 20)
2832                   return x;
2833               }
2834       break;
2835
2836     default:
2837       break;
2838     }
2839
2840   /* We have already simplified this expression.  Simplifying it again
2841      won't buy anything unless we weren't given a valid insn code
2842      to process (i.e., we are canonicalizing something.).  */
2843   if (insn_code != -2 /* Seems wrong: && current_alternative_string.  */
2844       && ! ATTR_IND_SIMPLIFIED_P (newexp))
2845     return copy_rtx_unchanging (newexp);
2846
2847   return newexp;
2848 }
2849
2850 /* Optimize the attribute lists by seeing if we can determine conditional
2851    values from the known values of other attributes.  This will save subroutine
2852    calls during the compilation.  */
2853
2854 static void
2855 optimize_attrs (void)
2856 {
2857   struct attr_desc *attr;
2858   struct attr_value *av;
2859   struct insn_ent *ie;
2860   rtx newexp;
2861   int i;
2862   struct attr_value_list
2863   {
2864     struct attr_value *av;
2865     struct insn_ent *ie;
2866     struct attr_desc *attr;
2867     struct attr_value_list *next;
2868   };
2869   struct attr_value_list **insn_code_values;
2870   struct attr_value_list *ivbuf;
2871   struct attr_value_list *iv;
2872
2873   /* For each insn code, make a list of all the insn_ent's for it,
2874      for all values for all attributes.  */
2875
2876   if (num_insn_ents == 0)
2877     return;
2878
2879   /* Make 2 extra elements, for "code" values -2 and -1.  */
2880   insn_code_values = xcalloc ((insn_code_number + 2),
2881                               sizeof (struct attr_value_list *));
2882
2883   /* Offset the table address so we can index by -2 or -1.  */
2884   insn_code_values += 2;
2885
2886   iv = ivbuf = xmalloc (num_insn_ents * sizeof (struct attr_value_list));
2887
2888   for (i = 0; i < MAX_ATTRS_INDEX; i++)
2889     for (attr = attrs[i]; attr; attr = attr->next)
2890       for (av = attr->first_value; av; av = av->next)
2891         for (ie = av->first_insn; ie; ie = ie->next)
2892           {
2893             iv->attr = attr;
2894             iv->av = av;
2895             iv->ie = ie;
2896             iv->next = insn_code_values[ie->def->insn_code];
2897             insn_code_values[ie->def->insn_code] = iv;
2898             iv++;
2899           }
2900
2901   /* Sanity check on num_insn_ents.  */
2902   gcc_assert (iv == ivbuf + num_insn_ents);
2903
2904   /* Process one insn code at a time.  */
2905   for (i = -2; i < insn_code_number; i++)
2906     {
2907       /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2908          We use it to mean "already simplified for this insn".  */
2909       for (iv = insn_code_values[i]; iv; iv = iv->next)
2910         clear_struct_flag (iv->av->value);
2911
2912       for (iv = insn_code_values[i]; iv; iv = iv->next)
2913         {
2914           struct obstack *old = rtl_obstack;
2915
2916           attr = iv->attr;
2917           av = iv->av;
2918           ie = iv->ie;
2919           if (GET_CODE (av->value) != COND)
2920             continue;
2921
2922           rtl_obstack = temp_obstack;
2923           newexp = av->value;
2924           while (GET_CODE (newexp) == COND)
2925             {
2926               rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2927                                            ie->def->insn_index);
2928               if (newexp2 == newexp)
2929                 break;
2930               newexp = newexp2;
2931             }
2932
2933           rtl_obstack = old;
2934           if (newexp != av->value)
2935             {
2936               newexp = attr_copy_rtx (newexp);
2937               remove_insn_ent (av, ie);
2938               av = get_attr_value (newexp, attr, ie->def->insn_code);
2939               iv->av = av;
2940               insert_insn_ent (av, ie);
2941             }
2942         }
2943     }
2944
2945   free (ivbuf);
2946   free (insn_code_values - 2);
2947 }
2948
2949 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions.  */
2950
2951 static void
2952 clear_struct_flag (rtx x)
2953 {
2954   int i;
2955   int j;
2956   enum rtx_code code;
2957   const char *fmt;
2958
2959   ATTR_CURR_SIMPLIFIED_P (x) = 0;
2960   if (ATTR_IND_SIMPLIFIED_P (x))
2961     return;
2962
2963   code = GET_CODE (x);
2964
2965   switch (code)
2966     {
2967     case REG:
2968     case CONST_INT:
2969     case CONST_DOUBLE:
2970     case CONST_VECTOR:
2971     case SYMBOL_REF:
2972     case CODE_LABEL:
2973     case PC:
2974     case CC0:
2975     case EQ_ATTR:
2976     case ATTR_FLAG:
2977       return;
2978
2979     default:
2980       break;
2981     }
2982
2983   /* Compare the elements.  If any pair of corresponding elements
2984      fail to match, return 0 for the whole things.  */
2985
2986   fmt = GET_RTX_FORMAT (code);
2987   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2988     {
2989       switch (fmt[i])
2990         {
2991         case 'V':
2992         case 'E':
2993           for (j = 0; j < XVECLEN (x, i); j++)
2994             clear_struct_flag (XVECEXP (x, i, j));
2995           break;
2996
2997         case 'e':
2998           clear_struct_flag (XEXP (x, i));
2999           break;
3000         }
3001     }
3002 }
3003
3004 /* Create table entries for DEFINE_ATTR.  */
3005
3006 static void
3007 gen_attr (rtx exp, int lineno)
3008 {
3009   struct attr_desc *attr;
3010   struct attr_value *av;
3011   const char *name_ptr;
3012   char *p;
3013
3014   /* Make a new attribute structure.  Check for duplicate by looking at
3015      attr->default_val, since it is initialized by this routine.  */
3016   attr = find_attr (&XSTR (exp, 0), 1);
3017   if (attr->default_val)
3018     {
3019       message_with_line (lineno, "duplicate definition for attribute %s",
3020                          attr->name);
3021       message_with_line (attr->lineno, "previous definition");
3022       have_error = 1;
3023       return;
3024     }
3025   attr->lineno = lineno;
3026
3027   if (*XSTR (exp, 1) == '\0')
3028     attr->is_numeric = 1;
3029   else
3030     {
3031       name_ptr = XSTR (exp, 1);
3032       while ((p = next_comma_elt (&name_ptr)) != NULL)
3033         {
3034           av = oballoc (sizeof (struct attr_value));
3035           av->value = attr_rtx (CONST_STRING, p);
3036           av->next = attr->first_value;
3037           attr->first_value = av;
3038           av->first_insn = NULL;
3039           av->num_insns = 0;
3040           av->has_asm_insn = 0;
3041         }
3042     }
3043
3044   if (GET_CODE (XEXP (exp, 2)) == CONST)
3045     {
3046       attr->is_const = 1;
3047       if (attr->is_numeric)
3048         {
3049           message_with_line (lineno,
3050                              "constant attributes may not take numeric values");
3051           have_error = 1;
3052         }
3053
3054       /* Get rid of the CONST node.  It is allowed only at top-level.  */
3055       XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3056     }
3057
3058   if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3059     {
3060       message_with_line (lineno,
3061                          "`length' attribute must take numeric values");
3062       have_error = 1;
3063     }
3064
3065   /* Set up the default value.  */
3066   XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3067   attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3068 }
3069
3070 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3071    alternatives in the constraints.  Assume all MATCH_OPERANDs have the same
3072    number of alternatives as this should be checked elsewhere.  */
3073
3074 static int
3075 count_alternatives (rtx exp)
3076 {
3077   int i, j, n;
3078   const char *fmt;
3079
3080   if (GET_CODE (exp) == MATCH_OPERAND)
3081     return n_comma_elts (XSTR (exp, 2));
3082
3083   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3084        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3085     switch (*fmt++)
3086       {
3087       case 'e':
3088       case 'u':
3089         n = count_alternatives (XEXP (exp, i));
3090         if (n)
3091           return n;
3092         break;
3093
3094       case 'E':
3095       case 'V':
3096         if (XVEC (exp, i) != NULL)
3097           for (j = 0; j < XVECLEN (exp, i); j++)
3098             {
3099               n = count_alternatives (XVECEXP (exp, i, j));
3100               if (n)
3101                 return n;
3102             }
3103       }
3104
3105   return 0;
3106 }
3107
3108 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3109    `alternative' attribute.  */
3110
3111 static int
3112 compares_alternatives_p (rtx exp)
3113 {
3114   int i, j;
3115   const char *fmt;
3116
3117   if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3118     return 1;
3119
3120   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3121        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3122     switch (*fmt++)
3123       {
3124       case 'e':
3125       case 'u':
3126         if (compares_alternatives_p (XEXP (exp, i)))
3127           return 1;
3128         break;
3129
3130       case 'E':
3131         for (j = 0; j < XVECLEN (exp, i); j++)
3132           if (compares_alternatives_p (XVECEXP (exp, i, j)))
3133             return 1;
3134         break;
3135       }
3136
3137   return 0;
3138 }
3139
3140 /* Returns nonzero is INNER is contained in EXP.  */
3141
3142 static int
3143 contained_in_p (rtx inner, rtx exp)
3144 {
3145   int i, j;
3146   const char *fmt;
3147
3148   if (rtx_equal_p (inner, exp))
3149     return 1;
3150
3151   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3152        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3153     switch (*fmt++)
3154       {
3155       case 'e':
3156       case 'u':
3157         if (contained_in_p (inner, XEXP (exp, i)))
3158           return 1;
3159         break;
3160
3161       case 'E':
3162         for (j = 0; j < XVECLEN (exp, i); j++)
3163           if (contained_in_p (inner, XVECEXP (exp, i, j)))
3164             return 1;
3165         break;
3166       }
3167
3168   return 0;
3169 }
3170
3171 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES.  */
3172
3173 static void
3174 gen_insn (rtx exp, int lineno)
3175 {
3176   struct insn_def *id;
3177
3178   id = oballoc (sizeof (struct insn_def));
3179   id->next = defs;
3180   defs = id;
3181   id->def = exp;
3182   id->lineno = lineno;
3183
3184   switch (GET_CODE (exp))
3185     {
3186     case DEFINE_INSN:
3187       id->insn_code = insn_code_number;
3188       id->insn_index = insn_index_number;
3189       id->num_alternatives = count_alternatives (exp);
3190       if (id->num_alternatives == 0)
3191         id->num_alternatives = 1;
3192       id->vec_idx = 4;
3193       break;
3194
3195     case DEFINE_PEEPHOLE:
3196       id->insn_code = insn_code_number;
3197       id->insn_index = insn_index_number;
3198       id->num_alternatives = count_alternatives (exp);
3199       if (id->num_alternatives == 0)
3200         id->num_alternatives = 1;
3201       id->vec_idx = 3;
3202       break;
3203
3204     case DEFINE_ASM_ATTRIBUTES:
3205       id->insn_code = -1;
3206       id->insn_index = -1;
3207       id->num_alternatives = 1;
3208       id->vec_idx = 0;
3209       got_define_asm_attributes = 1;
3210       break;
3211
3212     default:
3213       gcc_unreachable ();
3214     }
3215 }
3216
3217 /* Process a DEFINE_DELAY.  Validate the vector length, check if annul
3218    true or annul false is specified, and make a `struct delay_desc'.  */
3219
3220 static void
3221 gen_delay (rtx def, int lineno)
3222 {
3223   struct delay_desc *delay;
3224   int i;
3225
3226   if (XVECLEN (def, 1) % 3 != 0)
3227     {
3228       message_with_line (lineno,
3229                          "number of elements in DEFINE_DELAY must be multiple of three");
3230       have_error = 1;
3231       return;
3232     }
3233
3234   for (i = 0; i < XVECLEN (def, 1); i += 3)
3235     {
3236       if (XVECEXP (def, 1, i + 1))
3237         have_annul_true = 1;
3238       if (XVECEXP (def, 1, i + 2))
3239         have_annul_false = 1;
3240     }
3241
3242   delay = oballoc (sizeof (struct delay_desc));
3243   delay->def = def;
3244   delay->num = ++num_delays;
3245   delay->next = delays;
3246   delay->lineno = lineno;
3247   delays = delay;
3248 }
3249
3250 /* Given a piece of RTX, print a C expression to test its truth value.
3251    We use AND and IOR both for logical and bit-wise operations, so
3252    interpret them as logical unless they are inside a comparison expression.
3253    The first bit of FLAGS will be nonzero in that case.
3254
3255    Set the second bit of FLAGS to make references to attribute values use
3256    a cached local variable instead of calling a function.  */
3257
3258 static void
3259 write_test_expr (rtx exp, int flags)
3260 {
3261   int comparison_operator = 0;
3262   RTX_CODE code;
3263   struct attr_desc *attr;
3264
3265   /* In order not to worry about operator precedence, surround our part of
3266      the expression with parentheses.  */
3267
3268   printf ("(");
3269   code = GET_CODE (exp);
3270   switch (code)
3271     {
3272     /* Binary operators.  */
3273     case GEU: case GTU:
3274     case LEU: case LTU:
3275       printf ("(unsigned) ");
3276       /* Fall through.  */
3277
3278     case EQ: case NE:
3279     case GE: case GT:
3280     case LE: case LT:
3281       comparison_operator = 1;
3282
3283     case PLUS:   case MINUS:  case MULT:     case DIV:      case MOD:
3284     case AND:    case IOR:    case XOR:
3285     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3286       write_test_expr (XEXP (exp, 0), flags | comparison_operator);
3287       switch (code)
3288         {
3289         case EQ:
3290           printf (" == ");
3291           break;
3292         case NE:
3293           printf (" != ");
3294           break;
3295         case GE:
3296           printf (" >= ");
3297           break;
3298         case GT:
3299           printf (" > ");
3300           break;
3301         case GEU:
3302           printf (" >= (unsigned) ");
3303           break;
3304         case GTU:
3305           printf (" > (unsigned) ");
3306           break;
3307         case LE:
3308           printf (" <= ");
3309           break;
3310         case LT:
3311           printf (" < ");
3312           break;
3313         case LEU:
3314           printf (" <= (unsigned) ");
3315           break;
3316         case LTU:
3317           printf (" < (unsigned) ");
3318           break;
3319         case PLUS:
3320           printf (" + ");
3321           break;
3322         case MINUS:
3323           printf (" - ");
3324           break;
3325         case MULT:
3326           printf (" * ");
3327           break;
3328         case DIV:
3329           printf (" / ");
3330           break;
3331         case MOD:
3332           printf (" %% ");
3333           break;
3334         case AND:
3335           if (flags & 1)
3336             printf (" & ");
3337           else
3338             printf (" && ");
3339           break;
3340         case IOR:
3341           if (flags & 1)
3342             printf (" | ");
3343           else
3344             printf (" || ");
3345           break;
3346         case XOR:
3347           printf (" ^ ");
3348           break;
3349         case ASHIFT:
3350           printf (" << ");
3351           break;
3352         case LSHIFTRT:
3353         case ASHIFTRT:
3354           printf (" >> ");
3355           break;
3356         default:
3357           gcc_unreachable ();
3358         }
3359
3360       write_test_expr (XEXP (exp, 1), flags | comparison_operator);
3361       break;
3362
3363     case NOT:
3364       /* Special-case (not (eq_attrq "alternative" "x")) */
3365       if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3366           && XSTR (XEXP (exp, 0), 0) == alternative_name)
3367         {
3368           printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3369           break;
3370         }
3371
3372       /* Otherwise, fall through to normal unary operator.  */
3373
3374     /* Unary operators.  */
3375     case ABS:  case NEG:
3376       switch (code)
3377         {
3378         case NOT:
3379           if (flags & 1)
3380             printf ("~ ");
3381           else
3382             printf ("! ");
3383           break;
3384         case ABS:
3385           printf ("abs ");
3386           break;
3387         case NEG:
3388           printf ("-");
3389           break;
3390         default:
3391           gcc_unreachable ();
3392         }
3393
3394       write_test_expr (XEXP (exp, 0), flags);
3395       break;
3396
3397     case EQ_ATTR_ALT:
3398         {
3399           int set = XINT (exp, 0), bit = 0;
3400
3401           if (flags & 1)
3402             fatal ("EQ_ATTR_ALT not valid inside comparison");
3403
3404           if (!set)
3405             fatal ("Empty EQ_ATTR_ALT should be optimized out");
3406
3407           if (!(set & (set - 1)))
3408             {
3409               if (!(set & 0xffff))
3410                 {
3411                   bit += 16;
3412                   set >>= 16;
3413                 }
3414               if (!(set & 0xff))
3415                 {
3416                   bit += 8;
3417                   set >>= 8;
3418                 }
3419               if (!(set & 0xf))
3420                 {
3421                   bit += 4;
3422                   set >>= 4;
3423                 }
3424               if (!(set & 0x3))
3425                 {
3426                   bit += 2;
3427                   set >>= 2;
3428                 }
3429               if (!(set & 1))
3430                 bit++;
3431
3432               printf ("which_alternative %s= %d",
3433                       XINT (exp, 1) ? "!" : "=", bit);
3434             }
3435           else
3436             {
3437               printf ("%s((1 << which_alternative) & 0x%x)",
3438                       XINT (exp, 1) ? "!" : "", set);
3439             }
3440         }
3441       break;
3442
3443     /* Comparison test of an attribute with a value.  Most of these will
3444        have been removed by optimization.   Handle "alternative"
3445        specially and give error if EQ_ATTR present inside a comparison.  */
3446     case EQ_ATTR:
3447       if (flags & 1)
3448         fatal ("EQ_ATTR not valid inside comparison");
3449
3450       if (XSTR (exp, 0) == alternative_name)
3451         {
3452           printf ("which_alternative == %s", XSTR (exp, 1));
3453           break;
3454         }
3455
3456       attr = find_attr (&XSTR (exp, 0), 0);
3457       gcc_assert (attr);
3458
3459       /* Now is the time to expand the value of a constant attribute.  */
3460       if (attr->is_const)
3461         {
3462           write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
3463                                              -2, -2),
3464                            flags);
3465         }
3466       else
3467         {
3468           if (flags & 2)
3469             printf ("attr_%s", attr->name);
3470           else
3471             printf ("get_attr_%s (insn)", attr->name);
3472           printf (" == ");
3473           write_attr_valueq (attr, XSTR (exp, 1));
3474         }
3475       break;
3476
3477     /* Comparison test of flags for define_delays.  */
3478     case ATTR_FLAG:
3479       if (flags & 1)
3480         fatal ("ATTR_FLAG not valid inside comparison");
3481       printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3482       break;
3483
3484     /* See if an operand matches a predicate.  */
3485     case MATCH_OPERAND:
3486       /* If only a mode is given, just ensure the mode matches the operand.
3487          If neither a mode nor predicate is given, error.  */
3488       if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3489         {
3490           if (GET_MODE (exp) == VOIDmode)
3491             fatal ("null MATCH_OPERAND specified as test");
3492           else
3493             printf ("GET_MODE (operands[%d]) == %smode",
3494                     XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3495         }
3496       else
3497         printf ("%s (operands[%d], %smode)",
3498                 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3499       break;
3500
3501     /* Constant integer.  */
3502     case CONST_INT:
3503       printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3504       break;
3505
3506     /* A random C expression.  */
3507     case SYMBOL_REF:
3508       printf ("%s", XSTR (exp, 0));
3509       break;
3510
3511     /* The address of the branch target.  */
3512     case MATCH_DUP:
3513       printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3514               XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3515       break;
3516
3517     case PC:
3518       /* The address of the current insn.  We implement this actually as the
3519          address of the current insn for backward branches, but the last
3520          address of the next insn for forward branches, and both with
3521          adjustments that account for the worst-case possible stretching of
3522          intervening alignments between this insn and its destination.  */
3523       printf ("insn_current_reference_address (insn)");
3524       break;
3525
3526     case CONST_STRING:
3527       printf ("%s", XSTR (exp, 0));
3528       break;
3529
3530     case IF_THEN_ELSE:
3531       write_test_expr (XEXP (exp, 0), flags & 2);
3532       printf (" ? ");
3533       write_test_expr (XEXP (exp, 1), flags | 1);
3534       printf (" : ");
3535       write_test_expr (XEXP (exp, 2), flags | 1);
3536       break;
3537
3538     default:
3539       fatal ("bad RTX code `%s' in attribute calculation\n",
3540              GET_RTX_NAME (code));
3541     }
3542
3543   printf (")");
3544 }
3545
3546 /* Given an attribute value, return the maximum CONST_STRING argument
3547    encountered.  Set *UNKNOWNP and return INT_MAX if the value is unknown.  */
3548
3549 static int
3550 max_attr_value (rtx exp, int *unknownp)
3551 {
3552   int current_max;
3553   int i, n;
3554
3555   switch (GET_CODE (exp))
3556     {
3557     case CONST_STRING:
3558       current_max = atoi (XSTR (exp, 0));
3559       break;
3560
3561     case COND:
3562       current_max = max_attr_value (XEXP (exp, 1), unknownp);
3563       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3564         {
3565           n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3566           if (n > current_max)
3567             current_max = n;
3568         }
3569       break;
3570
3571     case IF_THEN_ELSE:
3572       current_max = max_attr_value (XEXP (exp, 1), unknownp);
3573       n = max_attr_value (XEXP (exp, 2), unknownp);
3574       if (n > current_max)
3575         current_max = n;
3576       break;
3577
3578     default:
3579       *unknownp = 1;
3580       current_max = INT_MAX;
3581       break;
3582     }
3583
3584   return current_max;
3585 }
3586
3587 /* Given an attribute value, return the result of ORing together all
3588    CONST_STRING arguments encountered.  Set *UNKNOWNP and return -1
3589    if the numeric value is not known.  */
3590
3591 static int
3592 or_attr_value (rtx exp, int *unknownp)
3593 {
3594   int current_or;
3595   int i;
3596
3597   switch (GET_CODE (exp))
3598     {
3599     case CONST_STRING:
3600       current_or = atoi (XSTR (exp, 0));
3601       break;
3602
3603     case COND:
3604       current_or = or_attr_value (XEXP (exp, 1), unknownp);
3605       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3606         current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3607       break;
3608
3609     case IF_THEN_ELSE:
3610       current_or = or_attr_value (XEXP (exp, 1), unknownp);
3611       current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3612       break;
3613
3614     default:
3615       *unknownp = 1;
3616       current_or = -1;
3617       break;
3618     }
3619
3620   return current_or;
3621 }
3622
3623 /* Scan an attribute value, possibly a conditional, and record what actions
3624    will be required to do any conditional tests in it.
3625
3626    Specifically, set
3627         `must_extract'    if we need to extract the insn operands
3628         `must_constrain'  if we must compute `which_alternative'
3629         `address_used'    if an address expression was used
3630         `length_used'     if an (eq_attr "length" ...) was used
3631  */
3632
3633 static void
3634 walk_attr_value (rtx exp)
3635 {
3636   int i, j;
3637   const char *fmt;
3638   RTX_CODE code;
3639
3640   if (exp == NULL)
3641     return;
3642
3643   code = GET_CODE (exp);
3644   switch (code)
3645     {
3646     case SYMBOL_REF:
3647       if (! ATTR_IND_SIMPLIFIED_P (exp))
3648         /* Since this is an arbitrary expression, it can look at anything.
3649            However, constant expressions do not depend on any particular
3650            insn.  */
3651         must_extract = must_constrain = 1;
3652       return;
3653
3654     case MATCH_OPERAND:
3655       must_extract = 1;
3656       return;
3657
3658     case EQ_ATTR_ALT:
3659       must_extract = must_constrain = 1;
3660       break;
3661
3662     case EQ_ATTR:
3663       if (XSTR (exp, 0) == alternative_name)
3664         must_extract = must_constrain = 1;
3665       else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3666         length_used = 1;
3667       return;
3668
3669     case MATCH_DUP:
3670       must_extract = 1;
3671       address_used = 1;
3672       return;
3673
3674     case PC:
3675       address_used = 1;
3676       return;
3677
3678     case ATTR_FLAG:
3679       return;
3680
3681     default:
3682       break;
3683     }
3684
3685   for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3686     switch (*fmt++)
3687       {
3688       case 'e':
3689       case 'u':
3690         walk_attr_value (XEXP (exp, i));
3691         break;
3692
3693       case 'E':
3694         if (XVEC (exp, i) != NULL)
3695           for (j = 0; j < XVECLEN (exp, i); j++)
3696             walk_attr_value (XVECEXP (exp, i, j));
3697         break;
3698       }
3699 }
3700
3701 /* Write out a function to obtain the attribute for a given INSN.  */
3702
3703 static void
3704 write_attr_get (struct attr_desc *attr)
3705 {
3706   struct attr_value *av, *common_av;
3707
3708   /* Find the most used attribute value.  Handle that as the `default' of the
3709      switch we will generate.  */
3710   common_av = find_most_used (attr);
3711
3712   /* Write out start of function, then all values with explicit `case' lines,
3713      then a `default', then the value with the most uses.  */
3714   if (attr->static_p)
3715     printf ("static ");
3716   if (!attr->is_numeric)
3717     printf ("enum attr_%s\n", attr->name);
3718   else if (attr->unsigned_p)
3719     printf ("unsigned int\n");
3720   else
3721     printf ("int\n");
3722
3723   /* If the attribute name starts with a star, the remainder is the name of
3724      the subroutine to use, instead of `get_attr_...'.  */
3725   if (attr->name[0] == '*')
3726     printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3727   else if (attr->is_const == 0)
3728     printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3729   else
3730     {
3731       printf ("get_attr_%s (void)\n", attr->name);
3732       printf ("{\n");
3733
3734       for (av = attr->first_value; av; av = av->next)
3735         if (av->num_insns != 0)
3736           write_attr_set (attr, 2, av->value, "return", ";",
3737                           true_rtx, av->first_insn->def->insn_code,
3738                           av->first_insn->def->insn_index);
3739
3740       printf ("}\n\n");
3741       return;
3742     }
3743
3744   printf ("{\n");
3745   printf ("  switch (recog_memoized (insn))\n");
3746   printf ("    {\n");
3747
3748   for (av = attr->first_value; av; av = av->next)
3749     if (av != common_av)
3750       write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3751
3752   write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3753   printf ("    }\n}\n\n");
3754 }
3755
3756 /* Given an AND tree of known true terms (because we are inside an `if' with
3757    that as the condition or are in an `else' clause) and an expression,
3758    replace any known true terms with TRUE.  Use `simplify_and_tree' to do
3759    the bulk of the work.  */
3760
3761 static rtx
3762 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3763 {
3764   rtx term;
3765
3766   known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3767
3768   if (GET_CODE (known_true) == AND)
3769     {
3770       exp = eliminate_known_true (XEXP (known_true, 0), exp,
3771                                   insn_code, insn_index);
3772       exp = eliminate_known_true (XEXP (known_true, 1), exp,
3773                                   insn_code, insn_index);
3774     }
3775   else
3776     {
3777       term = known_true;
3778       exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3779     }
3780
3781   return exp;
3782 }
3783
3784 /* Write out a series of tests and assignment statements to perform tests and
3785    sets of an attribute value.  We are passed an indentation amount and prefix
3786    and suffix strings to write around each attribute value (e.g., "return"
3787    and ";").  */
3788
3789 static void
3790 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3791                 const char *prefix, const char *suffix, rtx known_true,
3792                 int insn_code, int insn_index)
3793 {
3794   if (GET_CODE (value) == COND)
3795     {
3796       /* Assume the default value will be the default of the COND unless we
3797          find an always true expression.  */
3798       rtx default_val = XEXP (value, 1);
3799       rtx our_known_true = known_true;
3800       rtx newexp;
3801       int first_if = 1;
3802       int i;
3803
3804       for (i = 0; i < XVECLEN (value, 0); i += 2)
3805         {
3806           rtx testexp;
3807           rtx inner_true;
3808
3809           testexp = eliminate_known_true (our_known_true,
3810                                           XVECEXP (value, 0, i),
3811                                           insn_code, insn_index);
3812           newexp = attr_rtx (NOT, testexp);
3813           newexp = insert_right_side (AND, our_known_true, newexp,
3814                                       insn_code, insn_index);
3815
3816           /* If the test expression is always true or if the next `known_true'
3817              expression is always false, this is the last case, so break
3818              out and let this value be the `else' case.  */
3819           if (testexp == true_rtx || newexp == false_rtx)
3820             {
3821               default_val = XVECEXP (value, 0, i + 1);
3822               break;
3823             }
3824
3825           /* Compute the expression to pass to our recursive call as being
3826              known true.  */
3827           inner_true = insert_right_side (AND, our_known_true,
3828                                           testexp, insn_code, insn_index);
3829
3830           /* If this is always false, skip it.  */
3831           if (inner_true == false_rtx)
3832             continue;
3833
3834           write_indent (indent);
3835           printf ("%sif ", first_if ? "" : "else ");
3836           first_if = 0;
3837           write_test_expr (testexp, 0);
3838           printf ("\n");
3839           write_indent (indent + 2);
3840           printf ("{\n");
3841
3842           write_attr_set (attr, indent + 4,
3843                           XVECEXP (value, 0, i + 1), prefix, suffix,
3844                           inner_true, insn_code, insn_index);
3845           write_indent (indent + 2);
3846           printf ("}\n");
3847           our_known_true = newexp;
3848         }
3849
3850       if (! first_if)
3851         {
3852           write_indent (indent);
3853           printf ("else\n");
3854           write_indent (indent + 2);
3855           printf ("{\n");
3856         }
3857
3858       write_attr_set (attr, first_if ? indent : indent + 4, default_val,
3859                       prefix, suffix, our_known_true, insn_code, insn_index);
3860
3861       if (! first_if)
3862         {
3863           write_indent (indent + 2);
3864           printf ("}\n");
3865         }
3866     }
3867   else
3868     {
3869       write_indent (indent);
3870       printf ("%s ", prefix);
3871       write_attr_value (attr, value);
3872       printf ("%s\n", suffix);
3873     }
3874 }
3875
3876 /* Write out the computation for one attribute value.  */
3877
3878 static void
3879 write_attr_case (struct attr_desc *attr, struct attr_value *av,
3880                  int write_case_lines, const char *prefix, const char *suffix,
3881                  int indent, rtx known_true)
3882 {
3883   struct insn_ent *ie;
3884
3885   if (av->num_insns == 0)
3886     return;
3887
3888   if (av->has_asm_insn)
3889     {
3890       write_indent (indent);
3891       printf ("case -1:\n");
3892       write_indent (indent + 2);
3893       printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
3894       write_indent (indent + 2);
3895       printf ("    && asm_noperands (PATTERN (insn)) < 0)\n");
3896       write_indent (indent + 2);
3897       printf ("  fatal_insn_not_found (insn);\n");
3898     }
3899
3900   if (write_case_lines)
3901     {
3902       for (ie = av->first_insn; ie; ie = ie->next)
3903         if (ie->def->insn_code != -1)
3904           {
3905             write_indent (indent);
3906             printf ("case %d:  /* %s */\n",
3907                     ie->def->insn_code, XSTR (ie->def->def, 0));
3908           }
3909     }
3910   else
3911     {
3912       write_indent (indent);
3913       printf ("default:\n");
3914     }
3915
3916   /* See what we have to do to output this value.  */
3917   must_extract = must_constrain = address_used = 0;
3918   walk_attr_value (av->value);
3919
3920   if (must_constrain)
3921     {
3922       write_indent (indent + 2);
3923       printf ("extract_constrain_insn_cached (insn);\n");
3924     }
3925   else if (must_extract)
3926     {
3927       write_indent (indent + 2);
3928       printf ("extract_insn_cached (insn);\n");
3929     }
3930
3931   write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3932                   known_true, av->first_insn->def->insn_code,
3933                   av->first_insn->def->insn_index);
3934
3935   if (strncmp (prefix, "return", 6))
3936     {
3937       write_indent (indent + 2);
3938       printf ("break;\n");
3939     }
3940   printf ("\n");
3941 }
3942
3943 /* Search for uses of non-const attributes and write code to cache them.  */
3944
3945 static int
3946 write_expr_attr_cache (rtx p, struct attr_desc *attr)
3947 {
3948   const char *fmt;
3949   int i, ie, j, je;
3950
3951   if (GET_CODE (p) == EQ_ATTR)
3952     {
3953       if (XSTR (p, 0) != attr->name)
3954         return 0;
3955
3956       if (!attr->is_numeric)
3957         printf ("  enum attr_%s ", attr->name);
3958       else if (attr->unsigned_p)
3959         printf ("  unsigned int ");
3960       else
3961         printf ("  int ");
3962
3963       printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
3964       return 1;
3965     }
3966
3967   fmt = GET_RTX_FORMAT (GET_CODE (p));
3968   ie = GET_RTX_LENGTH (GET_CODE (p));
3969   for (i = 0; i < ie; i++)
3970     {
3971       switch (*fmt++)
3972         {
3973         case 'e':
3974           if (write_expr_attr_cache (XEXP (p, i), attr))
3975             return 1;
3976           break;
3977
3978         case 'E':
3979           je = XVECLEN (p, i);
3980           for (j = 0; j < je; ++j)
3981             if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
3982               return 1;
3983           break;
3984         }
3985     }
3986
3987   return 0;
3988 }
3989
3990 /* Utilities to write in various forms.  */
3991
3992 static void
3993 write_attr_valueq (struct attr_desc *attr, const char *s)
3994 {
3995   if (attr->is_numeric)
3996     {
3997       int num = atoi (s);
3998
3999       printf ("%d", num);
4000
4001       if (num > 9 || num < 0)
4002         printf (" /* 0x%x */", num);
4003     }
4004   else
4005     {
4006       write_upcase (attr->name);
4007       printf ("_");
4008       write_upcase (s);
4009     }
4010 }
4011
4012 static void
4013 write_attr_value (struct attr_desc *attr, rtx value)
4014 {
4015   int op;
4016
4017   switch (GET_CODE (value))
4018     {
4019     case CONST_STRING:
4020       write_attr_valueq (attr, XSTR (value, 0));
4021       break;
4022
4023     case CONST_INT:
4024       printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4025       break;
4026
4027     case SYMBOL_REF:
4028       fputs (XSTR (value, 0), stdout);
4029       break;
4030
4031     case ATTR:
4032       {
4033         struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4034         printf ("get_attr_%s (%s)", attr2->name,
4035                 (attr2->is_const ? "" : "insn"));
4036       }
4037       break;
4038
4039     case PLUS:
4040       op = '+';
4041       goto do_operator;
4042     case MINUS:
4043       op = '-';
4044       goto do_operator;
4045     case MULT:
4046       op = '*';
4047       goto do_operator;
4048     case DIV:
4049       op = '/';
4050       goto do_operator;
4051     case MOD:
4052       op = '%';
4053       goto do_operator;
4054
4055     do_operator:
4056       write_attr_value (attr, XEXP (value, 0));
4057       putchar (' ');
4058       putchar (op);
4059       putchar (' ');
4060       write_attr_value (attr, XEXP (value, 1));
4061       break;
4062
4063     default:
4064       gcc_unreachable ();
4065     }
4066 }
4067
4068 static void
4069 write_upcase (const char *str)
4070 {
4071   while (*str)
4072     {
4073       /* The argument of TOUPPER should not have side effects.  */
4074       putchar (TOUPPER(*str));
4075       str++;
4076     }
4077 }
4078
4079 static void
4080 write_indent (int indent)
4081 {
4082   for (; indent > 8; indent -= 8)
4083     printf ("\t");
4084
4085   for (; indent; indent--)
4086     printf (" ");
4087 }
4088
4089 /* Write a subroutine that is given an insn that requires a delay slot, a
4090    delay slot ordinal, and a candidate insn.  It returns nonzero if the
4091    candidate can be placed in the specified delay slot of the insn.
4092
4093    We can write as many as three subroutines.  `eligible_for_delay'
4094    handles normal delay slots, `eligible_for_annul_true' indicates that
4095    the specified insn can be annulled if the branch is true, and likewise
4096    for `eligible_for_annul_false'.
4097
4098    KIND is a string distinguishing these three cases ("delay", "annul_true",
4099    or "annul_false").  */
4100
4101 static void
4102 write_eligible_delay (const char *kind)
4103 {
4104   struct delay_desc *delay;
4105   int max_slots;
4106   char str[50];
4107   const char *pstr;
4108   struct attr_desc *attr;
4109   struct attr_value *av, *common_av;
4110   int i;
4111
4112   /* Compute the maximum number of delay slots required.  We use the delay
4113      ordinal times this number plus one, plus the slot number as an index into
4114      the appropriate predicate to test.  */
4115
4116   for (delay = delays, max_slots = 0; delay; delay = delay->next)
4117     if (XVECLEN (delay->def, 1) / 3 > max_slots)
4118       max_slots = XVECLEN (delay->def, 1) / 3;
4119
4120   /* Write function prelude.  */
4121
4122   printf ("int\n");
4123   printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4124           kind);
4125   printf ("{\n");
4126   printf ("  rtx insn;\n");
4127   printf ("\n");
4128   printf ("  gcc_assert (slot < %d)\n", max_slots);
4129   printf ("\n");
4130   /* Allow dbr_schedule to pass labels, etc.  This can happen if try_split
4131      converts a compound instruction into a loop.  */
4132   printf ("  if (!INSN_P (candidate_insn))\n");
4133   printf ("    return 0;\n");
4134   printf ("\n");
4135
4136   /* If more than one delay type, find out which type the delay insn is.  */
4137
4138   if (num_delays > 1)
4139     {
4140       attr = find_attr (&delay_type_str, 0);
4141       gcc_assert (attr);
4142       common_av = find_most_used (attr);
4143
4144       printf ("  insn = delay_insn;\n");
4145       printf ("  switch (recog_memoized (insn))\n");
4146       printf ("    {\n");
4147
4148       sprintf (str, " * %d;\n      break;", max_slots);
4149       for (av = attr->first_value; av; av = av->next)
4150         if (av != common_av)
4151           write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4152
4153       write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4154       printf ("    }\n\n");
4155
4156       /* Ensure matched.  Otherwise, shouldn't have been called.  */
4157       printf ("  gcc_assert (slot >= %d);\n\n", max_slots);
4158     }
4159
4160   /* If just one type of delay slot, write simple switch.  */
4161   if (num_delays == 1 && max_slots == 1)
4162     {
4163       printf ("  insn = candidate_insn;\n");
4164       printf ("  switch (recog_memoized (insn))\n");
4165       printf ("    {\n");
4166
4167       attr = find_attr (&delay_1_0_str, 0);
4168       gcc_assert (attr);
4169       common_av = find_most_used (attr);
4170
4171       for (av = attr->first_value; av; av = av->next)
4172         if (av != common_av)
4173           write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4174
4175       write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4176       printf ("    }\n");
4177     }
4178
4179   else
4180     {
4181       /* Write a nested CASE.  The first indicates which condition we need to
4182          test, and the inner CASE tests the condition.  */
4183       printf ("  insn = candidate_insn;\n");
4184       printf ("  switch (slot)\n");
4185       printf ("    {\n");
4186
4187       for (delay = delays; delay; delay = delay->next)
4188         for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4189           {
4190             printf ("    case %d:\n",
4191                     (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4192             printf ("      switch (recog_memoized (insn))\n");
4193             printf ("\t{\n");
4194
4195             sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4196             pstr = str;
4197             attr = find_attr (&pstr, 0);
4198             gcc_assert (attr);
4199             common_av = find_most_used (attr);
4200
4201             for (av = attr->first_value; av; av = av->next)
4202               if (av != common_av)
4203                 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4204
4205             write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4206             printf ("      }\n");
4207           }
4208
4209       printf ("    default:\n");
4210       printf ("      gcc_unreachable ();\n");
4211       printf ("    }\n");
4212     }
4213
4214   printf ("}\n\n");
4215 }
4216
4217 /* This page contains miscellaneous utility routines.  */
4218
4219 /* Given a pointer to a (char *), return a malloc'ed string containing the
4220    next comma-separated element.  Advance the pointer to after the string
4221    scanned, or the end-of-string.  Return NULL if at end of string.  */
4222
4223 static char *
4224 next_comma_elt (const char **pstr)
4225 {
4226   const char *start;
4227
4228   start = scan_comma_elt (pstr);
4229
4230   if (start == NULL)
4231     return NULL;
4232
4233   return attr_string (start, *pstr - start);
4234 }
4235
4236 /* Return a `struct attr_desc' pointer for a given named attribute.  If CREATE
4237    is nonzero, build a new attribute, if one does not exist.  *NAME_P is
4238    replaced by a pointer to a canonical copy of the string.  */
4239
4240 static struct attr_desc *
4241 find_attr (const char **name_p, int create)
4242 {
4243   struct attr_desc *attr;
4244   int index;
4245   const char *name = *name_p;
4246
4247   /* Before we resort to using `strcmp', see if the string address matches
4248      anywhere.  In most cases, it should have been canonicalized to do so.  */
4249   if (name == alternative_name)
4250     return NULL;
4251
4252   index = name[0] & (MAX_ATTRS_INDEX - 1);
4253   for (attr = attrs[index]; attr; attr = attr->next)
4254     if (name == attr->name)
4255       return attr;
4256
4257   /* Otherwise, do it the slow way.  */
4258   for (attr = attrs[index]; attr; attr = attr->next)
4259     if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4260       {
4261         *name_p = attr->name;
4262         return attr;
4263       }
4264
4265   if (! create)
4266     return NULL;
4267
4268   attr = oballoc (sizeof (struct attr_desc));
4269   attr->name = DEF_ATTR_STRING (name);
4270   attr->first_value = attr->default_val = NULL;
4271   attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
4272   attr->unsigned_p = attr->static_p = 0;
4273   attr->next = attrs[index];
4274   attrs[index] = attr;
4275
4276   *name_p = attr->name;
4277
4278   return attr;
4279 }
4280
4281 /* Create internal attribute with the given default value.  */
4282
4283 void
4284 make_internal_attr (const char *name, rtx value, int special)
4285 {
4286   struct attr_desc *attr;
4287
4288   attr = find_attr (&name, 1);
4289   gcc_assert (!attr->default_val);
4290
4291   attr->is_numeric = 1;
4292   attr->is_const = 0;
4293   attr->is_special = (special & ATTR_SPECIAL) != 0;
4294   attr->negative_ok = (special & ATTR_NEGATIVE_OK) != 0;
4295   attr->unsigned_p = (special & ATTR_UNSIGNED) != 0;
4296   attr->static_p = (special & ATTR_STATIC) != 0;
4297   attr->default_val = get_attr_value (value, attr, -2);
4298 }
4299
4300 /* Find the most used value of an attribute.  */
4301
4302 static struct attr_value *
4303 find_most_used (struct attr_desc *attr)
4304 {
4305   struct attr_value *av;
4306   struct attr_value *most_used;
4307   int nuses;
4308
4309   most_used = NULL;
4310   nuses = -1;
4311
4312   for (av = attr->first_value; av; av = av->next)
4313     if (av->num_insns > nuses)
4314       nuses = av->num_insns, most_used = av;
4315
4316   return most_used;
4317 }
4318
4319 /* Return (attr_value "n") */
4320
4321 rtx
4322 make_numeric_value (int n)
4323 {
4324   static rtx int_values[20];
4325   rtx exp;
4326   char *p;
4327
4328   gcc_assert (n >= 0);
4329
4330   if (n < 20 && int_values[n])
4331     return int_values[n];
4332
4333   p = attr_printf (MAX_DIGITS, "%d", n);
4334   exp = attr_rtx (CONST_STRING, p);
4335
4336   if (n < 20)
4337     int_values[n] = exp;
4338
4339   return exp;
4340 }
4341
4342 static rtx
4343 copy_rtx_unchanging (rtx orig)
4344 {
4345   if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4346     return orig;
4347
4348   ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4349   return orig;
4350 }
4351
4352 /* Determine if an insn has a constant number of delay slots, i.e., the
4353    number of delay slots is not a function of the length of the insn.  */
4354
4355 static void
4356 write_const_num_delay_slots (void)
4357 {
4358   struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4359   struct attr_value *av;
4360   struct insn_ent *ie;
4361
4362   if (attr)
4363     {
4364       printf ("int\nconst_num_delay_slots (rtx insn)\n");
4365       printf ("{\n");
4366       printf ("  switch (recog_memoized (insn))\n");
4367       printf ("    {\n");
4368
4369       for (av = attr->first_value; av; av = av->next)
4370         {
4371           length_used = 0;
4372           walk_attr_value (av->value);
4373           if (length_used)
4374             {
4375               for (ie = av->first_insn; ie; ie = ie->next)
4376                 if (ie->def->insn_code != -1)
4377                   printf ("    case %d:  /* %s */\n",
4378                           ie->def->insn_code, XSTR (ie->def->def, 0));
4379               printf ("      return 0;\n");
4380             }
4381         }
4382
4383       printf ("    default:\n");
4384       printf ("      return 1;\n");
4385       printf ("    }\n}\n\n");
4386     }
4387 }
4388
4389 int
4390 main (int argc, char **argv)
4391 {
4392   rtx desc;
4393   struct attr_desc *attr;
4394   struct insn_def *id;
4395   rtx tem;
4396   int i;
4397
4398   progname = "genattrtab";
4399
4400   if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
4401     return (FATAL_EXIT_CODE);
4402
4403   obstack_init (hash_obstack);
4404   obstack_init (temp_obstack);
4405
4406   /* Set up true and false rtx's */
4407   true_rtx = rtx_alloc (CONST_INT);
4408   XWINT (true_rtx, 0) = 1;
4409   false_rtx = rtx_alloc (CONST_INT);
4410   XWINT (false_rtx, 0) = 0;
4411   ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4412   ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4413
4414   alternative_name = DEF_ATTR_STRING ("alternative");
4415   length_str = DEF_ATTR_STRING ("length");
4416   delay_type_str = DEF_ATTR_STRING ("*delay_type");
4417   delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4418   num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4419
4420   printf ("/* Generated automatically by the program `genattrtab'\n\
4421 from the machine description file `md'.  */\n\n");
4422
4423   /* Read the machine description.  */
4424
4425   initiate_automaton_gen (argc, argv);
4426   while (1)
4427     {
4428       int lineno;
4429
4430       desc = read_md_rtx (&lineno, &insn_code_number);
4431       if (desc == NULL)
4432         break;
4433
4434       switch (GET_CODE (desc))
4435         {
4436         case DEFINE_INSN:
4437         case DEFINE_PEEPHOLE:
4438         case DEFINE_ASM_ATTRIBUTES:
4439           gen_insn (desc, lineno);
4440           break;
4441
4442         case DEFINE_ATTR:
4443           gen_attr (desc, lineno);
4444           break;
4445
4446         case DEFINE_DELAY:
4447           gen_delay (desc, lineno);
4448           break;
4449
4450         case DEFINE_CPU_UNIT:
4451           gen_cpu_unit (desc);
4452           break;
4453
4454         case DEFINE_QUERY_CPU_UNIT:
4455           gen_query_cpu_unit (desc);
4456           break;
4457
4458         case DEFINE_BYPASS:
4459           gen_bypass (desc);
4460           break;
4461
4462         case EXCLUSION_SET:
4463           gen_excl_set (desc);
4464           break;
4465
4466         case PRESENCE_SET:
4467           gen_presence_set (desc);
4468           break;
4469
4470         case FINAL_PRESENCE_SET:
4471           gen_final_presence_set (desc);
4472           break;
4473
4474         case ABSENCE_SET:
4475           gen_absence_set (desc);
4476           break;
4477
4478         case FINAL_ABSENCE_SET:
4479           gen_final_absence_set (desc);
4480           break;
4481
4482         case DEFINE_AUTOMATON:
4483           gen_automaton (desc);
4484           break;
4485
4486         case AUTOMATA_OPTION:
4487           gen_automata_option (desc);
4488           break;
4489
4490         case DEFINE_RESERVATION:
4491           gen_reserv (desc);
4492           break;
4493
4494         case DEFINE_INSN_RESERVATION:
4495           gen_insn_reserv (desc);
4496           break;
4497
4498         default:
4499           break;
4500         }
4501       if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4502         insn_index_number++;
4503     }
4504
4505   if (have_error)
4506     return FATAL_EXIT_CODE;
4507
4508   insn_code_number++;
4509
4510   /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one.  */
4511   if (! got_define_asm_attributes)
4512     {
4513       tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4514       XVEC (tem, 0) = rtvec_alloc (0);
4515       gen_insn (tem, 0);
4516     }
4517
4518   /* Expand DEFINE_DELAY information into new attribute.  */
4519   if (num_delays)
4520     expand_delays ();
4521
4522   /* Build DFA, output some functions and expand DFA information
4523      to new attributes.  */
4524   if (num_dfa_decls)
4525     expand_automata ();
4526
4527   printf ("#include \"config.h\"\n");
4528   printf ("#include \"system.h\"\n");
4529   printf ("#include \"coretypes.h\"\n");
4530   printf ("#include \"tm.h\"\n");
4531   printf ("#include \"rtl.h\"\n");
4532   printf ("#include \"tm_p.h\"\n");
4533   printf ("#include \"insn-config.h\"\n");
4534   printf ("#include \"recog.h\"\n");
4535   printf ("#include \"regs.h\"\n");
4536   printf ("#include \"real.h\"\n");
4537   printf ("#include \"output.h\"\n");
4538   printf ("#include \"insn-attr.h\"\n");
4539   printf ("#include \"toplev.h\"\n");
4540   printf ("#include \"flags.h\"\n");
4541   printf ("#include \"function.h\"\n");
4542   printf ("\n");
4543   printf ("#define operands recog_data.operand\n\n");
4544
4545   /* Make `insn_alternatives'.  */
4546   insn_alternatives = oballoc (insn_code_number * sizeof (int));
4547   for (id = defs; id; id = id->next)
4548     if (id->insn_code >= 0)
4549       insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4550
4551   /* Make `insn_n_alternatives'.  */
4552   insn_n_alternatives = oballoc (insn_code_number * sizeof (int));
4553   for (id = defs; id; id = id->next)
4554     if (id->insn_code >= 0)
4555       insn_n_alternatives[id->insn_code] = id->num_alternatives;
4556
4557   /* Prepare to write out attribute subroutines by checking everything stored
4558      away and building the attribute cases.  */
4559
4560   check_defs ();
4561
4562   for (i = 0; i < MAX_ATTRS_INDEX; i++)
4563     for (attr = attrs[i]; attr; attr = attr->next)
4564       attr->default_val->value
4565         = check_attr_value (attr->default_val->value, attr);
4566
4567   if (have_error)
4568     return FATAL_EXIT_CODE;
4569
4570   for (i = 0; i < MAX_ATTRS_INDEX; i++)
4571     for (attr = attrs[i]; attr; attr = attr->next)
4572       fill_attr (attr);
4573
4574   /* Construct extra attributes for `length'.  */
4575   make_length_attrs ();
4576
4577   /* Perform any possible optimizations to speed up compilation.  */
4578   optimize_attrs ();
4579
4580   /* Now write out all the `gen_attr_...' routines.  Do these before the
4581      special routines so that they get defined before they are used.  */
4582
4583   for (i = 0; i < MAX_ATTRS_INDEX; i++)
4584     for (attr = attrs[i]; attr; attr = attr->next)
4585       {
4586         if (! attr->is_special && ! attr->is_const)
4587           {
4588             int insn_alts_p;
4589
4590             insn_alts_p
4591               = (attr->name [0] == '*'
4592                  && strcmp (&attr->name[1], INSN_ALTS_FUNC_NAME) == 0);
4593             if (insn_alts_p)
4594               printf ("\n#if AUTOMATON_ALTS\n");
4595             write_attr_get (attr);
4596             if (insn_alts_p)
4597               printf ("#endif\n\n");
4598           }
4599       }
4600
4601   /* Write out delay eligibility information, if DEFINE_DELAY present.
4602      (The function to compute the number of delay slots will be written
4603      below.)  */
4604   if (num_delays)
4605     {
4606       write_eligible_delay ("delay");
4607       if (have_annul_true)
4608         write_eligible_delay ("annul_true");
4609       if (have_annul_false)
4610         write_eligible_delay ("annul_false");
4611     }
4612
4613   /* Output code for pipeline hazards recognition based on DFA
4614      (deterministic finite-state automata).  */
4615   if (num_dfa_decls)
4616     write_automata ();
4617
4618   /* Write out constant delay slot info.  */
4619   write_const_num_delay_slots ();
4620
4621   write_length_unit_log ();
4622
4623   fflush (stdout);
4624   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
4625 }
4626
4627 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
4628 const char *
4629 get_insn_name (int code ATTRIBUTE_UNUSED)
4630 {
4631   return NULL;
4632 }