OSDN Git Service

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