home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / genattrtab.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  98KB  |  3,595 lines

  1. /* Generate code from machine description to compute values of attributes.
  2.    Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  3.    Contributed by Richard Kenner (kenner@nyu.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This program handles insn attribues and the DEFINE_DELAY and
  22.    DEFINE_FUNCTION_UNIT definitions.
  23.  
  24.    It produces a series of functions call `get_attr_...', one for each
  25.    attribute.  Each of these is given the rtx for an insn and returns a member
  26.    of the enum for the attribute.
  27.  
  28.    These subroutines have the form of a `switch' on the INSN_CODE (via
  29.    `recog_memoized').  Each case either returns a constant attribute value
  30.    or a value that depends on tests on other attributes, the form of
  31.    operands, or some random C expression (encoded with a SYMBOL_REF
  32.    expression).
  33.  
  34.    If the attribute `alternative', or a random C expression is present,
  35.    `constrain_operands' is called.  If either of these cases of a reference to
  36.    an operand is found, `insn_extract' is called.
  37.  
  38.    The special attribute `length' is also recognized.  For this operand, 
  39.    expressions involving the address of an operand or the current insn,
  40.    (address (pc)), are valid.  In this case, an initial pass is made to
  41.    set all lengths that do not depend on address.  Those that do are set to
  42.    the maximum length.  Then each insn that depends on an address is checked
  43.    and possibly has its length changed.  The process repeats until no further
  44.    changed are made.  The resulting lengths are saved for use by
  45.    `get_attr_length'.
  46.  
  47.    Internal attributes are defined to handle DEFINE_DELAY and
  48.    DEFINE_FUNCTION_UNIT.  Special routines are output for these cases.
  49.  
  50.    This program works by keeping a list of possible values for each attribute.
  51.    These include the basic attribute choices, default values for attribute, and
  52.    all derived quantities.
  53.  
  54.    As the description file is read, the definition for each insn is saved in a
  55.    `struct insn_def'.   When the file reading is complete, a `struct insn_ent'
  56.    is created for each insn and chained to the corresponding attribute value,
  57.    either that specified, or the default.
  58.  
  59.    An optimization phase is then run.  This simplifies expressions for each
  60.    insn.  EQ_ATTR tests are resolved, whenever possible, to a test that
  61.    indicates when the attribute has the specified value for the insn.  This
  62.    avoids recursive calls during compilation.
  63.  
  64.    The strategy used when processing DEFINE_DELAY and DEFINE_FUNCTION_UNIT
  65.    definitions is to create arbitrarily complex expressions and have the
  66.    optimization simplify them.
  67.  
  68.    Once optimization is complete, any required routines and definitions
  69.    will be written.  */
  70.  
  71. #include <stdio.h>
  72. #include "config.h"
  73. #include "rtl.h"
  74. #include "obstack.h"
  75. #include "insn-config.h"    /* For REGISTER_CONSTRAINTS */
  76.  
  77. struct obstack obstack;
  78. struct obstack *rtl_obstack = &obstack;
  79.  
  80. #define obstack_chunk_alloc xmalloc
  81. #define obstack_chunk_free free
  82. extern int xmalloc ();
  83. extern void free ();
  84.  
  85. void fatal ();
  86. void fancy_abort ();
  87.  
  88. /* Define structures used to record attributes and values.  */
  89.  
  90. /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
  91.    encountered, we store all the relevant information into a
  92.    `struct insn_def'.  This is done to allow attribute definitions to occur
  93.    anywhere in the file.  */
  94.  
  95. struct insn_def
  96. {
  97.   int insn_code;        /* Instruction number. */
  98.   int insn_index;        /* Expression numer in file, for errors. */
  99.   struct insn_def *next;    /* Next insn in chain. */
  100.   rtx def;            /* The DEFINE_... */
  101.   int num_alternatives;        /* Number of alternatives.  */
  102.   int vec_idx;            /* Index of attribute vector in `def'. */
  103. };
  104.  
  105. /* Once everything has been read in, we store in each attribute value a list
  106.    of insn codes that have that value.  Here is the structure used for the
  107.    list.  */
  108.  
  109. struct insn_ent
  110. {
  111.   int insn_code;        /* Instruction number.  */
  112.   int insn_index;        /* Index of definition in file */
  113.   struct insn_ent *next;    /* Next in chain.  */
  114. };
  115.  
  116. /* Each value of an attribute (either constant or computed) is assigned a
  117.    structure which is used as the listhead of the insns that have that
  118.    value.  */
  119.  
  120. struct attr_value
  121. {
  122.   rtx value;            /* Value of attribute.  */
  123.   struct attr_value *next;    /* Next attribute value in chain.  */
  124.   struct insn_ent *first_insn;    /* First insn with this value.  */
  125.   int num_insns;        /* Number of insns with this value.  */
  126.   int has_asm_insn;        /* True if this value used for `asm' insns */
  127. };
  128.  
  129. /* Structure for each attribute.  */
  130.  
  131. struct attr_desc
  132. {
  133.   char *name;            /* Name of attribute. */
  134.   struct attr_desc *next;    /* Next attribute. */
  135.   int is_numeric;        /* Values of this attribute are numeric. */
  136.   int is_special;        /* Don't call `write_attr_set'. */
  137.   struct attr_value *first_value; /* First value of this attribute. */
  138.   struct attr_value *default_val; /* Default value for this attribute. */
  139. };
  140.  
  141. /* Structure for each DEFINE_DELAY.  */
  142.  
  143. struct delay_desc
  144. {
  145.   rtx def;            /* DEFINE_DELAY expression.  */
  146.   struct delay_desc *next;    /* Next DEFINE_DELAY. */
  147.   int num;            /* Number of DEFINE_DELAY, starting at 1.  */
  148. };
  149.  
  150. /* Record information about each DEFINE_FUNCTION_UNIT.  */
  151.  
  152. struct function_unit_op
  153. {
  154.   rtx condexp;            /* Expression TRUE for applicable insn.  */
  155.   struct function_unit_op *next; /* Next operation for this function unit.  */
  156.   int num;            /* Ordinal for this operation type in unit.  */
  157.   int ready;            /* Cost until data is ready.  */
  158.   rtx busyexp;            /* Expression computing conflict cost.  */
  159. };
  160.  
  161. /* Record information about each function unit mentioned in a
  162.    DEFINE_FUNCTION_UNIT.  */
  163.  
  164. struct function_unit
  165. {
  166.   char *name;            /* Function unit name.  */
  167.   struct function_unit *next;    /* Next function unit.  */
  168.   int num;            /* Ordinal of this unit type.  */
  169.   int multiplicity;        /* Number of units of this type.  */
  170.   int simultaneity;        /* Maximum number of simultaneous insns
  171.                    on this function unit or 0 if unlimited.  */
  172.   rtx condexp;            /* Expression TRUE for insn needing unit. */
  173.   rtx costexp;            /* Worst-case cost as function of insn. */
  174.   int num_opclasses;        /* Number of different operation types.  */
  175.   struct function_unit_op *ops;    /* Pointer to first operation type.  */
  176.   int needs_conflict_function;    /* Nonzero if a conflict function required.  */
  177.   rtx default_cost;        /* Conflict cost, if constant.  */
  178. };
  179.  
  180. /* Listheads of above structures.  */
  181.  
  182. struct attr_desc *attrs;
  183. struct insn_def *defs;
  184. struct delay_desc *delays;
  185. struct function_unit *units;
  186.  
  187. /* Other variables. */
  188.  
  189. int insn_code_number;
  190. int insn_index_number;
  191. int got_define_asm_attributes;
  192. int must_extract;
  193. int must_constrain;
  194. int address_used;
  195. int num_delays;
  196. int have_annul_true, have_annul_false;
  197. int num_units;
  198.  
  199. /* Used as operand to `operate_exp':  */
  200.  
  201. enum operator {PLUS_OP, MINUS_OP, OR_OP, MAX_OP};
  202.  
  203. /* Stores, for each insn code, a bitmap that has bits on for each possible
  204.    alternative.  */
  205.  
  206. int *insn_alternatives;
  207.  
  208. /* Used to simplify expressions.  */
  209.  
  210. rtx true_rtx, false_rtx;
  211.  
  212. /* Used to reduce calls to `strcmp' */
  213.  
  214. char *alternative_name = "alternative";
  215.  
  216. /* Simplify an expression.  Only call the routine if there is something to
  217.    simplify.  */
  218. #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX)    \
  219.   (RTX_UNCHANGING_P (EXP) ? (EXP)            \
  220.    : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
  221.   
  222. rtx check_attr_test ();
  223. void check_attr_value ();
  224. rtx convert_set_attr_alternative ();
  225. rtx convert_set_attr ();
  226. void check_defs ();
  227. rtx make_canonical ();
  228. struct attr_value *get_attr_value ();
  229. void expand_delays ();
  230. rtx operate_exp ();
  231. void expand_units ();
  232. void fill_attr ();
  233. rtx substitute_address ();
  234. void make_length_attrs ();
  235. rtx identity_fn ();
  236. rtx zero_fn ();
  237. rtx one_fn ();
  238. rtx max_fn ();
  239. rtx simplify_cond ();
  240. void remove_insn_ent ();
  241. void insert_insn_ent ();
  242. rtx insert_right_side ();
  243. rtx make_alternative_compare ();
  244. int compute_alternative_mask ();
  245. rtx evaluate_eq_attr ();
  246. rtx simplify_and_tree ();
  247. rtx simplify_or_tree ();
  248. rtx simplify_test_exp ();
  249. void optimize_attrs ();
  250. void gen_attr ();
  251. int count_alternatives ();
  252. int compares_alternatives_p ();
  253. int contained_in_p ();
  254. void gen_insn ();
  255. void gen_delay ();
  256. void gen_unit ();
  257. void write_test_expr ();
  258. int max_attr_value ();
  259. void walk_attr_value ();
  260. void write_attr_get ();
  261. rtx eliminate_known_true ();
  262. void write_attr_set ();
  263. void write_attr_case ();
  264. void write_attr_value ();
  265. void write_attr_valueq ();
  266. void write_upcase ();
  267. void write_indent ();
  268. void write_eligible_delay ();
  269. void write_function_unit_info ();
  270. int n_comma_elts ();
  271. char *next_comma_elt ();
  272. struct attr_desc *find_attr ();
  273. void make_internal_attr ();
  274. struct attr_value *find_most_used ();
  275. rtx find_single_value ();
  276. rtx make_numeric_value ();
  277. int xrealloc ();
  278. int xmalloc ();
  279. void fatal ();
  280.  
  281. /* Given a test expression for an attribute, ensure it is validly formed.
  282.    Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
  283.    and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")).  Do the latter
  284.    test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
  285.  
  286.    Update the string address in EQ_ATTR expression to be the same used
  287.    in the attribute (or `alternative_name') to speed up subsequent
  288.    `find_attr' calls and eliminate most `strcmp' calls.
  289.  
  290.    Return the new expression, if any.   */
  291.  
  292. rtx
  293. check_attr_test (exp)
  294.      rtx exp;
  295. {
  296.   struct attr_desc *attr;
  297.   struct attr_value *av;
  298.   char *name_ptr, *p;
  299.   rtx orexp, newexp;
  300.  
  301.   switch (GET_CODE (exp))
  302.     {
  303.     case EQ_ATTR:
  304.       /* Handle negation test.  */
  305.       if (XSTR (exp, 1)[0] == '!')
  306.     {
  307.       XSTR(exp, 1) = &XSTR(exp, 1)[1];
  308.       newexp = rtx_alloc (NOT);
  309.       XEXP (newexp, 0) = exp;
  310.  
  311.       return check_attr_test (newexp);
  312.     }
  313.  
  314.       else if (n_comma_elts (XSTR (exp, 1)) == 1)
  315.     {
  316.       attr = find_attr (XEXP (exp, 0), 0);
  317.       if (attr == NULL)
  318.         {
  319.           if (! strcmp (XSTR (exp, 0), "alternative"))
  320.         {
  321.           XSTR (exp, 0) = alternative_name;
  322.           /* This can't be simplified any further.  */
  323.           RTX_UNCHANGING_P (exp) = 1;
  324.           return exp;
  325.         }
  326.         else
  327.         fatal ("Unknown attribute `%s' in EQ_ATTR", XEXP (exp, 0));
  328.         }
  329.  
  330.       XSTR (exp, 0) = attr->name;
  331.  
  332.       if (attr->is_numeric)
  333.         {
  334.           for (p = XSTR (exp, 1); *p; p++)
  335.         if (*p < '0' || *p > '9')
  336.            fatal ("Attribute `%s' takes only numeric values", 
  337.               XEXP (exp, 0));
  338.         }
  339.       else
  340.         {
  341.           for (av = attr->first_value; av; av = av->next)
  342.         if (GET_CODE (av->value) == CONST_STRING
  343.             && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
  344.           break;
  345.  
  346.           if (av == NULL)
  347.         fatal ("Unknown value `%s' for `%s' attribute",
  348.                XEXP (exp, 1), XEXP (exp, 0));
  349.         }
  350.     }
  351.       else
  352.     {
  353.       /* Make an IOR tree of the possible values.  */
  354.       orexp = false_rtx;
  355.       name_ptr = XSTR (exp, 1);
  356.       while ((p = next_comma_elt (&name_ptr)) != NULL)
  357.         {
  358.           newexp = rtx_alloc (EQ_ATTR);
  359.           XSTR (newexp, 0) = XSTR (exp, 0);
  360.           XSTR (newexp, 1) = p;
  361.           orexp = insert_right_side (IOR, orexp, newexp, -2);
  362.         }
  363.  
  364.       return check_attr_test (orexp);
  365.     }
  366.       break;
  367.  
  368.     case CONST_INT:
  369.       /* Either TRUE or FALSE.  */
  370.       if (XINT (exp, 0))
  371.     return true_rtx;
  372.       else
  373.     return false_rtx;
  374.  
  375.     case IOR:
  376.     case AND:
  377.       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0));
  378.       XEXP (exp, 1) = check_attr_test (XEXP (exp, 1));
  379.       break;
  380.  
  381.     case NOT:
  382.       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0));
  383.       break;
  384.  
  385.     case MATCH_OPERAND:
  386.     case LE:  case LT:  case GT:  case GE:
  387.     case LEU: case LTU: case GTU: case GEU:
  388.     case NE:  case EQ:
  389.       /* These cases can't be simplified.  */
  390.       RTX_UNCHANGING_P (exp) = 1;
  391.       break;
  392.  
  393.     default:
  394.       fatal ("RTL operator \"%s\" not valid in attribute test",
  395.          GET_RTX_NAME (GET_CODE (exp)));
  396.     }
  397.  
  398.   return exp;
  399. }
  400.  
  401. /* Given an expression, ensure that it is validly formed and that all named
  402.    attribute values are valid for the given attribute.  Issue a fatal error
  403.    if not.  If no attribute is specified, assume a numeric attribute.  */
  404.  
  405. void
  406. check_attr_value (exp, attr)
  407.      rtx exp;
  408.      struct attr_desc *attr;
  409. {
  410.   struct attr_value *av;
  411.   char *p;
  412.   int i;
  413.  
  414.   switch (GET_CODE (exp))
  415.     {
  416.     case CONST_INT:
  417.       if (attr && ! attr->is_numeric)
  418.     fatal ("CONST_INT not valid for non-numeric `%s' attribute",
  419.            attr->name);
  420.  
  421.       if (INTVAL (exp) < 0)
  422.     fatal ("Negative numeric value specified for `%s' attribute",
  423.            attr->name);
  424.  
  425.       break;
  426.  
  427.     case CONST_STRING:
  428.       if (! strcmp (XSTR (exp, 0), "*"))
  429.     break;
  430.  
  431.       if (attr == 0 || attr->is_numeric)
  432.     {
  433.       for (p = XSTR (exp, 0); *p; p++)
  434.         if (*p > '9' || *p < '0')
  435.           fatal ("Non-numeric value for numeric `%s' attribute",
  436.              attr ? "internal" : attr->name);
  437.       break;
  438.     }
  439.  
  440.       for (av = attr->first_value; av; av = av->next)
  441.     if (GET_CODE (av->value) == CONST_STRING
  442.         && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
  443.       break;
  444.  
  445.       if (av == NULL)
  446.     fatal ("Unknown value `%s' for `%s' attribute",
  447.            XSTR (exp, 0), attr ? "internal" : attr->name);
  448.  
  449.       return;
  450.  
  451.     case IF_THEN_ELSE:
  452.       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0));
  453.       check_attr_value (XEXP (exp, 1), attr);
  454.       check_attr_value (XEXP (exp, 2), attr);
  455.       return;
  456.  
  457.     case COND:
  458.       if (XVECLEN (exp, 0) % 2 != 0)
  459.     fatal ("First operand of COND must have even length");
  460.  
  461.       for (i = 0; i < XVECLEN (exp, 0); i += 2)
  462.     {
  463.       XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i));
  464.       check_attr_value (XVECEXP (exp, 0, i + 1), attr);
  465.     }
  466.  
  467.       check_attr_value (XEXP (exp, 1), attr);
  468.       return;
  469.  
  470.     default:
  471.       fatal ("Illegal operation `%s' for attribute value",
  472.          GET_RTX_NAME (GET_CODE (exp)));
  473.     }
  474. }
  475.  
  476. /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
  477.    It becomes a COND with each test being (eq_attr "alternative "n") */
  478.  
  479. rtx
  480. convert_set_attr_alternative (exp, num_alt, insn_code, insn_index)
  481.      rtx exp;
  482.      int num_alt;
  483.      int insn_code, insn_index;
  484. {
  485.   rtx newexp;
  486.   rtx condexp;
  487.   int i;
  488.  
  489.   if (XVECLEN (exp, 1) != num_alt)
  490.     fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
  491.        insn_index);
  492.  
  493.   /* Make a COND with all tests but the last.  Select the last value via the
  494.      default.  */
  495.   condexp = rtx_alloc (COND);
  496.   XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
  497.  
  498.   for (i = 0; i < num_alt - 1; i++)
  499.     {
  500.       XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
  501.       XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
  502.       XSTR (XVECEXP (condexp, 0, 2 * i), 1) = (char *) xmalloc (3);
  503.       sprintf (XSTR (XVECEXP (condexp, 0, 2 * i), 1), "%d", i);
  504.       XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
  505.     }
  506.  
  507.   XEXP (condexp, 1) = XVECEXP (exp, 1, i);
  508.  
  509.   newexp = rtx_alloc (SET);
  510.   XEXP (newexp, 0) = rtx_alloc (ATTR);
  511.   XSTR (XEXP (newexp, 0), 0) = XSTR (exp, 0);
  512.   XEXP (newexp, 1) = condexp;
  513.  
  514.   return newexp;
  515. }
  516.  
  517. /* Given a SET_ATTR, convert to the appropriate SET.  If a comma-separated
  518.    list of values is given, convert to SET_ATTR_ALTERNATIVE first.  */
  519.  
  520. rtx
  521. convert_set_attr (exp, num_alt, insn_code, insn_index)
  522.      rtx exp;
  523.      int num_alt;
  524.      int insn_code, insn_index;
  525. {
  526.   rtx newexp;
  527.   char *name_ptr;
  528.   char *p;
  529.   int n;
  530.  
  531.   /* See how many alternative specified.  */
  532.   n = n_comma_elts (XSTR (exp, 1));
  533.   if (n == 1)
  534.     {
  535.       newexp = rtx_alloc (SET);
  536.       XEXP (newexp, 0) = rtx_alloc (ATTR);
  537.       XSTR (XEXP (newexp, 0), 0) = XSTR (exp, 0);
  538.       XEXP (newexp, 1) = rtx_alloc (CONST_STRING);
  539.       XSTR (XEXP (newexp, 1), 0) = XSTR (exp, 1);
  540.  
  541.       return newexp;
  542.     }
  543.  
  544.   newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
  545.   XSTR (newexp, 0) = XSTR (exp, 0);
  546.   XVEC (newexp, 1) = rtvec_alloc (n);
  547.  
  548.   /* Process each comma-separated name.  */
  549.   name_ptr = XSTR (exp, 1);
  550.   n = 0;
  551.   while ((p = next_comma_elt (&name_ptr)) != NULL)
  552.     {
  553.       XVECEXP (newexp, 1, n) = rtx_alloc (CONST_STRING);
  554.       XSTR (XVECEXP (newexp, 1, n++), 0) = p;
  555.     }
  556.  
  557.   return convert_set_attr_alternative (newexp, num_alt, insn_code, insn_index);
  558. }
  559.  
  560. /* Scan all definitions, checking for validity.  Also, convert any SET_ATTR
  561.    and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
  562.    expressions. */
  563.  
  564. void
  565. check_defs ()
  566. {
  567.   struct insn_def *id;
  568.   struct attr_desc *attr;
  569.   int i;
  570.   rtx value;
  571.  
  572.   for (id = defs; id; id = id->next)
  573.     {
  574.       if (XVEC (id->def, id->vec_idx) == NULL)
  575.     continue;
  576.  
  577.       for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
  578.     {
  579.       value = XVECEXP (id->def, id->vec_idx, i);
  580.       switch (GET_CODE (value))
  581.         {
  582.         case SET:
  583.           if (GET_CODE (XEXP (value, 0)) != ATTR)
  584.         fatal ("Bad attribute set in pattern %d", id->insn_index);
  585.           break;
  586.  
  587.         case SET_ATTR_ALTERNATIVE:
  588.           value = convert_set_attr_alternative (value,
  589.                             id->num_alternatives,
  590.                             id->insn_code,
  591.                             id->insn_index);
  592.           break;
  593.  
  594.         case SET_ATTR:
  595.           value = convert_set_attr (value, id->num_alternatives,
  596.                     id->insn_code, id->insn_index);
  597.           break;
  598.  
  599.         default:
  600.           fatal ("Invalid attribute code `%s' for pattern %d",
  601.              GET_RTX_NAME (GET_CODE (value)), id->insn_index);
  602.         }
  603.  
  604.       if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
  605.         fatal ("Unknown attribute `%s' for pattern number %d",
  606.            XSTR (XEXP (value, 0), 0), id->insn_index);
  607.  
  608.       XVECEXP (id->def, id->vec_idx, i) = value;
  609.       check_attr_value (XEXP (value, 1), attr);
  610.     }
  611.     }
  612. }
  613.  
  614. /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
  615.    expressions by converting them into a COND.  This removes cases from this
  616.    program.  Also, replace an attribute value of "*" with the default attribute
  617.    value.  */
  618.  
  619. rtx
  620. make_canonical (attr, exp)
  621.      struct attr_desc *attr;
  622.      rtx exp;
  623. {
  624.   int i;
  625.   rtx newexp;
  626.  
  627.   switch (GET_CODE (exp))
  628.     {
  629.     case CONST_INT:
  630.       exp = make_numeric_value (INTVAL (exp));
  631.       break;
  632.  
  633.     case CONST_STRING:
  634.       if (! strcmp (XSTR (exp, 0), "*"))
  635.     {
  636.       if (attr == 0 || attr->default_val == 0)
  637.         fatal ("(attr_value \"*\") used in invalid context.");
  638.       exp = attr->default_val->value;
  639.     }
  640.  
  641.       break;
  642.  
  643.     case IF_THEN_ELSE:
  644.       newexp = rtx_alloc (COND);
  645.       XVEC (newexp, 0) = rtvec_alloc (2);
  646.       XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
  647.       XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
  648.  
  649.       XEXP (newexp, 1) = XEXP (exp, 2);
  650.  
  651.       exp = newexp;
  652.       /* Fall through to COND case since this is now a COND.  */
  653.  
  654.     case COND:
  655.       /* First, check for degenerate COND. */
  656.       if (XVECLEN (exp, 0) == 0)
  657.     return make_canonical (attr, XEXP (exp, 1));
  658.  
  659.       for (i = 0; i < XVECLEN (exp, 0); i += 2)
  660.     XVECEXP (exp, 0, i + 1)
  661.         = make_canonical (attr, XVECEXP (exp, 0, i + 1));
  662.  
  663.       XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
  664.       break;
  665.     }
  666.  
  667.   return exp;
  668. }
  669.  
  670. /* Given a value and an attribute description, return a `struct attr_value *'
  671.    that represents that value.  This is either an existing structure, if the
  672.    value has been previously encountered, or a newly-created structure.
  673.  
  674.    `insn_code' is the code of an insn whose attribute has the specified
  675.    value (-2 if not processing an insn).  We ensure that all insns for
  676.    a given value have the same number of alternatives if the value checks
  677.    alternatives.  */
  678.  
  679. struct attr_value *
  680. get_attr_value (value, attr, insn_code)
  681.      rtx value;
  682.      struct attr_desc *attr;
  683.      int insn_code;
  684. {
  685.   struct attr_value *av;
  686.   int num_alt = 0;
  687.  
  688.   value = make_canonical (attr, value);
  689.   if (compares_alternatives_p (value))
  690.     {
  691.       if (insn_code < 0 || insn_alternatives == NULL)
  692.     fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
  693.       else
  694.     num_alt = insn_alternatives[insn_code];
  695.     }
  696.  
  697.   for (av = attr->first_value; av; av = av->next)
  698.     if (rtx_equal_p (value, av->value)
  699.     && (num_alt == 0 || av->first_insn == NULL
  700.         || insn_alternatives[av->first_insn->insn_code]))
  701.       return av;
  702.  
  703.   av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
  704.   av->value = value;
  705.   av->next = attr->first_value;
  706.   attr->first_value = av;
  707.   av->first_insn = NULL;
  708.   av->num_insns = 0;
  709.   av->has_asm_insn = 0;
  710.  
  711.   return av;
  712. }
  713.  
  714. /* After all DEFINE_DELAYs have been read in, create internal attributes
  715.    to generate the required routines.
  716.  
  717.    First, we compute the number of delay slots for each insn (as a COND of
  718.    each of the test expressions in DEFINE_DELAYs).  Then, if more than one
  719.    delay type is specified, we compute a similar function giving the
  720.    DEFINE_DELAY ordinal for each insn.
  721.  
  722.    Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
  723.    tells whether a given insn can be in that delay slot.
  724.  
  725.    Normal attrbute filling and optimization expands these to contain the
  726.    information needed to handle delay slots.  */
  727.  
  728. void
  729. expand_delays ()
  730. {
  731.   struct delay_desc *delay;
  732.   rtx condexp;
  733.   rtx newexp;
  734.   int i;
  735.   char *p;
  736.  
  737.   /* First, generate data for `num_delay_slots' function.  */
  738.  
  739.   condexp = rtx_alloc (COND);
  740.   XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
  741.   XEXP (condexp, 1) = make_numeric_value (0);
  742.  
  743.   for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
  744.     {
  745.       XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
  746.       XVECEXP (condexp, 0, i + 1)
  747.     = make_numeric_value (XVECLEN (delay->def, 1) / 3);
  748.     }
  749.  
  750.   make_internal_attr ("*num_delay_slots", condexp, 0);
  751.  
  752.   /* If more than one delay type, do the same for computing the delay type.  */
  753.   if (num_delays > 1)
  754.     {
  755.       condexp = rtx_alloc (COND);
  756.       XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
  757.       XEXP (condexp, 1) = make_numeric_value (0);
  758.  
  759.       for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
  760.     {
  761.       XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
  762.       XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
  763.     }
  764.  
  765.       make_internal_attr ("*delay_type", condexp, 1);
  766.     }
  767.  
  768.   /* For each delay possibility and delay slot, compute an eligability
  769.      attribute for non-anulled insns and for each type of annulled (annul
  770.      if true and annul if false).  */
  771.  for (delay = delays; delay; delay = delay->next)
  772.    {
  773.      for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
  774.        {
  775.      newexp = rtx_alloc (IF_THEN_ELSE);
  776.      condexp = XVECEXP (delay->def, 1, i);
  777.      if (condexp == 0) condexp = false_rtx;
  778.      XEXP (newexp, 0) = condexp;
  779.      XEXP (newexp, 1) = make_numeric_value (1);
  780.      XEXP (newexp, 2) = make_numeric_value (0);
  781.  
  782.      p = (char *) xmalloc (13);
  783.      sprintf (p, "*delay_%d_%d", delay->num, i / 3);
  784.      make_internal_attr (p, newexp, 1);
  785.  
  786.      if (have_annul_true)
  787.        {
  788.          newexp = rtx_alloc (IF_THEN_ELSE);
  789.          condexp = XVECEXP (delay->def, 1, i + 1);
  790.          if (condexp == 0) condexp = false_rtx;
  791.          XEXP (newexp, 0) = condexp;
  792.          XEXP (newexp, 1) = make_numeric_value (1);
  793.          XEXP (newexp, 2) = make_numeric_value (0);
  794.          p = (char *) xmalloc (18);
  795.          sprintf (p, "*annul_true_%d_%d", delay->num, i / 3);
  796.          make_internal_attr (p, newexp, 1);
  797.        }
  798.  
  799.      if (have_annul_false)
  800.        {
  801.          newexp = rtx_alloc (IF_THEN_ELSE);
  802.          condexp = XVECEXP (delay->def, 1, i + 2);
  803.          if (condexp == 0) condexp = false_rtx;
  804.          XEXP (newexp, 0) = condexp;
  805.          XEXP (newexp, 1) = make_numeric_value (1);
  806.          XEXP (newexp, 2) = make_numeric_value (0);
  807.          p = (char *) xmalloc (18);
  808.          sprintf (p, "*annul_false_%d_%d", delay->num, i / 3);
  809.          make_internal_attr (p, newexp, 1);
  810.        }
  811.        }
  812.    }
  813. }
  814.  
  815. /* This function is given a left and right side expression and an operator.
  816.    Each side is a conditional expression, each alternative of which has a
  817.    numerical value.  The function returns another conditional expression
  818.    which, for every possible set of condition values, returns a value that is
  819.    the operator applied to the values of the two sides.
  820.  
  821.    Since this is called early, it must also support IF_THEN_ELSE.  */
  822.  
  823. rtx
  824. operate_exp (op, left, right)
  825.      enum operator op;
  826.      rtx left, right;
  827. {
  828.   int left_value, right_value;
  829.   rtx newexp;
  830.   int i;
  831.  
  832.   /* If left is a string, apply operator to it and the right side.  */
  833.   if (GET_CODE (left) == CONST_STRING)
  834.     {
  835.       /* If right is also a string, just perform the operation.  */
  836.       if (GET_CODE (right) == CONST_STRING)
  837.     {
  838.       left_value = atoi (XSTR (left, 0));
  839.       right_value = atoi (XSTR (right, 0));
  840.       switch (op)
  841.         {
  842.         case PLUS_OP:
  843.           i = left_value + right_value;
  844.           break;
  845.  
  846.         case MINUS_OP:
  847.           i = left_value - right_value;
  848.           break;
  849.  
  850.         case OR_OP:
  851.           i = left_value | right_value;
  852.           break;
  853.  
  854.         case MAX_OP:
  855.           if (left_value > right_value)
  856.         i = left_value;
  857.           else
  858.         i = right_value;
  859.           break;
  860.  
  861.         default:
  862.           abort ();
  863.         }
  864.  
  865.       return make_numeric_value (i);
  866.     }
  867.       else if (GET_CODE (right) == IF_THEN_ELSE)
  868.     {
  869.       /* Apply recursively to all values within.  */
  870.       newexp = rtx_alloc (IF_THEN_ELSE);
  871.       XEXP (newexp, 0) = XEXP (right, 0);
  872.       XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
  873.       XEXP (newexp, 2) = operate_exp (op, left, XEXP (right, 2));
  874.  
  875.       return newexp;
  876.     }
  877.       else if (GET_CODE (right) == COND)
  878.     {
  879.       newexp = rtx_alloc (COND);
  880.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
  881.       for (i = 0; i < XVECLEN (right, 0); i += 2)
  882.         {
  883.           XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
  884.           XVECEXP (newexp, 0, i + 1)
  885.         = operate_exp (op, left, XVECEXP (right, 0, i + 1));
  886.         }
  887.  
  888.       XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
  889.  
  890.       return newexp;
  891.     }
  892.       else
  893.     fatal ("Badly formed attribute value");
  894.     }
  895.  
  896.   /* Otherwise, do recursion the other way.  */
  897.   else if (GET_CODE (left) == IF_THEN_ELSE)
  898.     {
  899.       newexp = rtx_alloc (IF_THEN_ELSE);
  900.       XEXP (newexp, 0) = XEXP (left, 0);
  901.       XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
  902.       XEXP (newexp, 2) = operate_exp (op, XEXP (left, 2), right);
  903.  
  904.       return newexp;
  905.     }
  906.  
  907.   else if (GET_CODE (left) == COND)
  908.     {
  909.       newexp = rtx_alloc (COND);
  910.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
  911.       for (i = 0; i < XVECLEN (left, 0); i += 2)
  912.     {
  913.       XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
  914.       XVECEXP (newexp, 0, i + 1)
  915.         = operate_exp (op, XVECEXP (left, 0, i + 1), right);
  916.     }
  917.  
  918.       XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
  919.  
  920.       return newexp;
  921.     }
  922.  
  923.   else
  924.     fatal ("Badly formed attribute value.");
  925.  
  926. }
  927.  
  928. /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
  929.    construct a number of attributes.
  930.  
  931.    The first produces a function `function_units_used' which is given an
  932.    insn and produces a mask showing which function units are required for
  933.    the execution of that insn.
  934.  
  935.    The second produces a function `result_ready_cost' which is used to
  936.    determine the time that the result of an insn will be ready and hence
  937.    a worst-case schedule.
  938.  
  939.    Both of these produce quite complex expressions which are then set as the
  940.    default value of internal attributes.  Normal attribute simplification
  941.    should produce reasonable expressions.
  942.  
  943.    For each unit, a `<name>_unit_ready_cost' function will take an
  944.    insn and give the delay until that unit will be ready with the result
  945.    and a `<name>_unit_busy_delay' function is given an insn already
  946.    executing on the unit and a candidate to execute and will give the
  947.    cost from the time the executing insn started until the candidate
  948.    can start (ignore limitations on the number of simultaneous insns).  */
  949.  
  950. void
  951. expand_units ()
  952. {
  953.   struct function_unit *unit;
  954.   struct function_unit_op *op;
  955.   rtx unitsmask;
  956.   rtx readycost;
  957.   rtx newexp;
  958.   char *str;
  959.  
  960.   /* Initially, cost and masks are zero.  */
  961.   unitsmask = readycost = make_numeric_value (0);
  962.  
  963.   /* Set up a conditional for costs and unit mask.  */
  964.   newexp = rtx_alloc (IF_THEN_ELSE);
  965.   XEXP (newexp, 2) = make_numeric_value (0);
  966.  
  967.   /* For each unit, insert its contribution to the above three values.  */
  968.   for (unit = units; unit; unit = unit->next)
  969.     {
  970.       /* An expression that computes the ready cost for this unit.  */
  971.       rtx readyexp = rtx_alloc (COND);
  972.       /* An expression that maps insns to operation number for conflicts.  */
  973.       rtx caseexp = rtx_alloc (COND);
  974.  
  975.       XVEC (readyexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
  976.       XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
  977.  
  978.       for (op = unit->ops; op; op = op->next)
  979.     {
  980.       str = (char *) xmalloc (strlen (unit->name) + 11);
  981.  
  982.       /* Validate the expressions we were given for the conditions
  983.          and busy cost.  Then make an attribute for use in the conflict
  984.          function.  */
  985.       op->condexp = check_attr_test (op->condexp);
  986.       check_attr_value (op->busyexp, 0);
  987.       sprintf (str, "*%s_case_%d", unit->name, op->num);
  988.       make_internal_attr (str, make_canonical (0, op->busyexp));
  989.  
  990.       /* Make our adjustment to the two COND's being computed.  If we are
  991.          the last operation class, place our values into the default of
  992.          the COND.  */
  993.       if (op->num == unit->num_opclasses - 1)
  994.         {
  995.           XEXP (readyexp, 1) = make_numeric_value (op->ready);
  996.           XEXP (caseexp, 1) = make_numeric_value (op->num);
  997.         }
  998.       else
  999.         {
  1000.           XVECEXP (readyexp, 0, op->num * 2) = op->condexp;
  1001.           XVECEXP (readyexp, 0, op->num * 2 + 1)
  1002.         = make_numeric_value (op->ready);
  1003.           XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
  1004.           XVECEXP (caseexp, 0, op->num * 2 + 1)
  1005.         = make_numeric_value (op->num);
  1006.         }
  1007.     }
  1008.  
  1009.       /* Make an attribute for the case number and ready delay.  */
  1010.       str = (char *) xmalloc (strlen (unit->name) + 8);
  1011.       sprintf (str, "*%s_cases", unit->name);
  1012.       make_internal_attr (str, caseexp, 1);
  1013.  
  1014.       str = (char *) xmalloc (strlen (unit->name) + 20);
  1015.       sprintf (str, "*%s_unit_ready_cost", unit->name);
  1016.       make_internal_attr (str, readyexp, 0);
  1017.  
  1018.       /* Merge this function unit into the ready cost and unit mask
  1019.      attributes.  */
  1020.       XEXP (newexp, 0) = check_attr_test (unit->condexp);
  1021.       XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
  1022.       unitsmask = operate_exp (OR_OP, unitsmask, newexp);
  1023.  
  1024.       XEXP (newexp, 1) = readyexp;
  1025.       readycost = operate_exp (MAX_OP, readycost, newexp);
  1026.     }
  1027.  
  1028.   make_internal_attr ("*function_units_used", unitsmask, 0);
  1029.   make_internal_attr ("*result_ready_cost", readycost, 0);
  1030. }
  1031.  
  1032. /* Once all attributes and insns have been read and checked, we construct for
  1033.    each attribute value a list of all the insns that have that value for
  1034.    the attribute.  */
  1035.  
  1036. void
  1037. fill_attr (attr)
  1038.      struct attr_desc *attr;
  1039. {
  1040.   struct attr_value *av;
  1041.   struct insn_ent *ie;
  1042.   struct insn_def *id;
  1043.   int i;
  1044.   rtx value;
  1045.  
  1046.   for (id = defs; id; id = id->next)
  1047.     {
  1048.       /* If no value is specified for this insn for this attribute, use the
  1049.      default.  */
  1050.       value = NULL;
  1051.       if (XVEC (id->def, id->vec_idx))
  1052.     for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
  1053.       if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0), 
  1054.             attr->name))
  1055.         value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
  1056.  
  1057.       if (value == NULL)
  1058.     av = attr->default_val;
  1059.       else
  1060.     av = get_attr_value (value, attr, id->insn_code);
  1061.  
  1062.       ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
  1063.       ie->insn_code = id->insn_code;
  1064.       ie->insn_index = id->insn_code;
  1065.       insert_insn_ent (av, ie);
  1066.     }
  1067. }
  1068.  
  1069. /* Given an expression EXP, see if it is a COND that has a test that checks
  1070.    relative positions of insns (uses MATCH_DUP or PC).  If so, replace it
  1071.    with what is obtained by passing the expression to ADDRESS_FN.  If not
  1072.    but it is a COND, call this routine recursively on each value (including
  1073.    the default value).  Otherwise, return the value returned by NO_ADDRESS_FN
  1074.    applied to EXP.  */
  1075.  
  1076. rtx
  1077. substitute_address (exp, no_address_fn, address_fn)
  1078.      rtx exp;
  1079.      rtx (*no_address_fn) ();
  1080.      rtx (*address_fn) ();
  1081. {
  1082.   int i;
  1083.   rtx newexp;
  1084.  
  1085.   if (GET_CODE (exp) != COND)
  1086.     return (*no_address_fn) (exp);
  1087.  
  1088.   /* See if any tests use addresses.  */
  1089.   address_used = 0;
  1090.   for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1091.     walk_attr_value (XVECEXP (exp, 0, i));
  1092.  
  1093.   if (address_used)
  1094.     return (*address_fn) (exp);
  1095.  
  1096.   /* Make a new copy of this COND, replacing each element.  */
  1097.   newexp = rtx_alloc (COND);
  1098.   XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
  1099.   for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1100.     {
  1101.       XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
  1102.       XVECEXP (newexp, 0, i + 1) = substitute_address (XVECEXP (exp, 0, i + 1),
  1103.                                no_address_fn,
  1104.                                address_fn);
  1105.     }
  1106.  
  1107.   XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
  1108.                      no_address_fn, address_fn);
  1109.  
  1110.   return newexp;
  1111. }
  1112.  
  1113. /* Make new attributes from the `length' attribute.  The following are made,
  1114.    each corresponding to a function called from `shorten_branches' or
  1115.    `get_attr_length':
  1116.  
  1117.    *insn_default_length        This is the length of the insn to be returned
  1118.                 by `get_attr_length' before `shorten_branches'
  1119.                 has been called.  In each case where the length
  1120.                 depends on relative addresses, the largest
  1121.                 possible is used.  This routine is also used
  1122.                 to compute the initial size of the insn.
  1123.  
  1124.    *insn_variable_length_p    This returns 1 if the insn's length depends
  1125.                 on relative addresses, zero otherwise.
  1126.  
  1127.    *insn_current_length        This is only called when it is known that the
  1128.                 insn has a variable length and returns the
  1129.                 current length, based on relative addresses.
  1130.   */
  1131.  
  1132. void
  1133. make_length_attrs ()
  1134. {
  1135.   static char *new_names[] = {"*insn_default_length",
  1136.                   "*insn_variable_length_p",
  1137.                   "*insn_current_length"};
  1138.   static rtx (*no_address_fn[]) () = {identity_fn, zero_fn, zero_fn};
  1139.   static rtx (*address_fn[]) () = {max_fn, one_fn, identity_fn};
  1140.   int i;
  1141.   struct attr_desc *length_attr, *new_attr;
  1142.   struct attr_value *av, *new_av;
  1143.   struct insn_ent *ie, *new_ie;
  1144.  
  1145.   /* See if length attribute is defined.  If so, it must be numeric.  Make
  1146.      it special so we don't output anything for it.  */
  1147.   length_attr = find_attr ("length", 0);
  1148.   if (length_attr == 0)
  1149.     return;
  1150.  
  1151.   if (! length_attr->is_numeric)
  1152.     fatal ("length attribute must be numeric.");
  1153.  
  1154.   length_attr->is_special = 1;
  1155.  
  1156.   /* Make each new attribute, in turn.  */
  1157.   for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
  1158.     {
  1159.       make_internal_attr (new_names[i],
  1160.               substitute_address (length_attr->default_val->value,
  1161.                           no_address_fn[i], address_fn[i]),
  1162.               0);
  1163.       new_attr = find_attr (new_names[i], 0);
  1164.       for (av = length_attr->first_value; av; av = av->next)
  1165.     for (ie = av->first_insn; ie; ie = ie->next)
  1166.       {
  1167.         new_av = get_attr_value (substitute_address (av->value,
  1168.                              no_address_fn[i],
  1169.                              address_fn[i]),
  1170.                      new_attr, ie->insn_code);
  1171.         new_ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
  1172.         new_ie->insn_code = ie->insn_code;
  1173.         new_ie->insn_index = ie->insn_index;
  1174.         insert_insn_ent (new_av, new_ie);
  1175.       }
  1176.     }
  1177. }
  1178.  
  1179. /* Utility functions called from above routine.  */
  1180.  
  1181. rtx
  1182. identity_fn (exp)
  1183.      rtx exp;
  1184. {
  1185.   return exp;
  1186. }
  1187.  
  1188. rtx
  1189. zero_fn (exp)
  1190.      rtx exp;
  1191. {
  1192.   return make_numeric_value (0);
  1193. }
  1194.  
  1195. rtx
  1196. one_fn (exp)
  1197.      rtx exp;
  1198. {
  1199.   return make_numeric_value (1);
  1200. }
  1201.  
  1202. rtx
  1203. max_fn (exp)
  1204.      rtx exp;
  1205. {
  1206.   return make_numeric_value (max_attr_value (exp));
  1207. }
  1208.  
  1209. /* Take a COND expression and see if any of the conditions in it can be
  1210.    simplified.  If any are known true or known false for the particular insn
  1211.    code, the COND can be further simplified.
  1212.  
  1213.    Also call ourselves on any COND operations that are values of this COND.
  1214.  
  1215.    We only do the first replacement found directly and call ourselves
  1216.    recursively for subsequent replacements.  */
  1217.  
  1218. rtx
  1219. simplify_cond (exp, insn_code, insn_index)
  1220.      rtx exp;
  1221.      int insn_code, insn_index;
  1222. {
  1223.   int i, j;
  1224.   rtx newtest;
  1225.   rtx value;
  1226.   rtx newexp = exp;
  1227.  
  1228.   for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1229.     {
  1230.       newtest = SIMPLIFY_TEST_EXP (XVECEXP (exp, 0, i), insn_code, insn_index);
  1231.       if (newtest == true_rtx)
  1232.     {
  1233.       /* Make a new COND with any previous conditions and the value for
  1234.          this pair as the default value.  */
  1235.       newexp = rtx_alloc (COND);
  1236.       XVEC (newexp, 0) = rtvec_alloc (i);
  1237.       for (j = 0; j < i; j++)
  1238.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1239.  
  1240.       XEXP (newexp, 1) = XVECEXP (exp, 0, i + 1);
  1241.       break;
  1242.     }
  1243.  
  1244.       else if (newtest == false_rtx)
  1245.     {
  1246.       /* Build a new COND without this test.  */
  1247.       newexp = rtx_alloc (COND);
  1248.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
  1249.       for (j = 0; j < i; j++)
  1250.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1251.  
  1252.       for (j = i; j < XVECLEN (newexp, 0); j++)
  1253.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j + 2);
  1254.  
  1255.       XEXP (newexp, 1) = XEXP (exp, 1);
  1256.       break;
  1257.     }
  1258.  
  1259.       else if (newtest != XVECEXP (exp, 0, i))
  1260.     {
  1261.       newexp = rtx_alloc (COND);
  1262.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
  1263.       for (j = 0; j < XVECLEN (exp, 0); j++)
  1264.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1265.       XEXP (newexp, 1) = XEXP (exp, 1);
  1266.  
  1267.       XVECEXP (newexp, 0, i) = newtest;
  1268.       break;
  1269.     }
  1270.  
  1271.       /* See if this value may need simplification.  */
  1272.       if (GET_CODE (XVECEXP (exp, 0, i + 1)) == COND)
  1273.     {
  1274.       value = simplify_cond (XVECEXP (exp, 0, i + 1),
  1275.                  insn_code, insn_index);
  1276.       if (value != XVECEXP (exp, 0, i + 1))
  1277.         {
  1278.           newexp = rtx_alloc (COND);
  1279.           XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
  1280.           for (j = 0; j < XVECLEN (exp, 0); j++)
  1281.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1282.           XEXP (newexp, 1) = XEXP (exp, 1);
  1283.  
  1284.           XVECEXP (newexp, 0, i + 1) = value;
  1285.           break;
  1286.         }
  1287.     }
  1288.  
  1289.       /* If this is the last condition in a COND and our value is the same
  1290.      as the default value, our test isn't needed.  */
  1291.       if (i == XVECLEN (exp, 0) - 2
  1292.       && rtx_equal_p (XVECEXP (exp, 0, i + 1), XEXP (exp, 1)))
  1293.     {
  1294.       newexp = rtx_alloc (COND);
  1295.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
  1296.       for (j = 0; j < i; j++)
  1297.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1298.       XEXP (newexp, 1) = XEXP (exp, 1);
  1299.       break;
  1300.     }
  1301.  
  1302.       /* If this value and the value for the next test are the same, merge the
  1303.          tests.  */
  1304.       else if (i != XVECLEN (exp, 0) - 2
  1305.            && rtx_equal_p (XVECEXP (exp, 0, i + 1),
  1306.                    XVECEXP (exp, 0, i + 3)))
  1307.     {
  1308.       newexp = rtx_alloc (COND);
  1309.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
  1310.       for (j = 0; j < i; j++)
  1311.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1312.  
  1313.       XVECEXP (newexp, 0, j)
  1314.         = insert_right_side (IOR, XVECEXP (exp, 0, i),
  1315.                  XVECEXP (exp, 0, i + 2),
  1316.                  insn_code, insn_index);
  1317.       XVECEXP (newexp, 0, j + 1) = XVECEXP (exp, 0, i + 1);
  1318.  
  1319.       for (j = i + 2; j < XVECLEN (newexp, 0); j++)
  1320.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j + 2);
  1321.  
  1322.       XEXP (newexp, 1) = XEXP (exp, 1);
  1323.       break;
  1324.     }
  1325.     }
  1326.  
  1327.   /* See if default value needs simplification.  */
  1328.   if (GET_CODE (XEXP (exp, 1)) == COND)
  1329.     {
  1330.       value = simplify_cond (XEXP (exp, 1), insn_code, insn_index);
  1331.       if (value != XEXP (exp, 1))
  1332.     {
  1333.       newexp = rtx_alloc (COND);
  1334.       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
  1335.       for (j = 0; j < XVECLEN (exp, 0); j++)
  1336.         XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
  1337.       XEXP (newexp, 1) = value;
  1338.     }
  1339.     }
  1340.   
  1341.   if (exp == newexp)
  1342.     return exp;
  1343.   else if (XVECLEN (newexp, 0) == 1)
  1344.     return XVECEXP (newexp, 0, 0);
  1345.   else
  1346.     return simplify_cond (newexp, insn_code, insn_index);
  1347. }
  1348.  
  1349. /* Remove an insn entry from an attribute value.  */
  1350.  
  1351. void
  1352. remove_insn_ent (av, ie)
  1353.      struct attr_value *av;
  1354.      struct insn_ent *ie;
  1355. {
  1356.   struct insn_ent *previe;
  1357.  
  1358.   if (av->first_insn == ie)
  1359.     av->first_insn = ie->next;
  1360.   else
  1361.     {
  1362.       for (previe = av->first_insn; previe->next != ie; previe = previe->next)
  1363.     ;
  1364.       previe->next = ie->next;
  1365.     }
  1366.  
  1367.   av->num_insns--;
  1368.   if (ie->insn_code == -1)
  1369.     av->has_asm_insn = 0;
  1370. }
  1371.  
  1372. /* Insert an insn entry in an attribute value list.  */
  1373.  
  1374. void
  1375. insert_insn_ent (av, ie)
  1376.      struct attr_value *av;
  1377.      struct insn_ent *ie;
  1378. {
  1379.   ie->next = av->first_insn;
  1380.   av->first_insn = ie;
  1381.   av->num_insns++;
  1382.   if (ie->insn_code == -1)
  1383.     av->has_asm_insn = 1;
  1384. }
  1385.  
  1386. /* This is a utility routine to take an expression that is a tree of either
  1387.    AND or IOR expressions and insert a new term.  The new term will be
  1388.    inserted at the right side of the first node whose code does not match
  1389.    the root.  A new node will be created with the root's code.  Its left
  1390.    side will be the old right side and its right side will be the new
  1391.    term.
  1392.  
  1393.    If the `term' is itself a tree, all its leaves will be inserted.  */
  1394.  
  1395. rtx
  1396. insert_right_side (code, exp, term, insn_code, insn_index)
  1397.      RTX_CODE code;
  1398.      rtx exp;
  1399.      rtx term;
  1400.      int insn_code, insn_index;
  1401. {
  1402.   rtx newexp;
  1403.  
  1404.   if (GET_CODE (term) == code)
  1405.     {
  1406.       exp = insert_right_side (code, exp, XEXP (term, 0),
  1407.                    insn_code, insn_index);
  1408.       exp = insert_right_side (code, exp, XEXP (term, 1),
  1409.                    insn_code, insn_index);
  1410.  
  1411.       return exp;
  1412.     }
  1413.  
  1414.   if (GET_CODE (exp) == code)
  1415.     {
  1416.       /* Make a copy of this expression and call recursively.  */
  1417.       newexp = rtx_alloc (code);
  1418.       XEXP (newexp, 0) = XEXP (exp, 0);
  1419.       XEXP (newexp, 1) = insert_right_side (code, XEXP (exp, 1),
  1420.                         term, insn_code, insn_index);
  1421.     }
  1422.   else
  1423.     {
  1424.       /* Insert the new term.  */
  1425.       newexp = rtx_alloc (code);
  1426.       XEXP (newexp, 0) = exp;
  1427.       XEXP (newexp, 1) = term;
  1428.       }
  1429.  
  1430.   return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1431. }
  1432.  
  1433. /* If we have an expression which AND's a bunch of
  1434.     (not (eq_attrq "alternative" "n"))
  1435.    terms, we may have covered all or all but one of the possible alternatives.
  1436.    If so, we can optimize.  Similarly for IOR's of EQ_ATTR.
  1437.  
  1438.    This routine is passed an expression and either AND or IOR.  It returns a
  1439.    bitmask indicating which alternatives are present.  */
  1440.  
  1441. int
  1442. compute_alternative_mask (exp, code)
  1443.      rtx exp;
  1444.      RTX_CODE code;
  1445. {
  1446.   if (GET_CODE (exp) == code)
  1447.     return compute_alternative_mask (XEXP (exp, 0), code)
  1448.        | compute_alternative_mask (XEXP (exp, 1), code);
  1449.  
  1450.   else if (code == AND && GET_CODE (exp) == NOT
  1451.        && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  1452.        && XSTR (XEXP (exp, 0), 0) == alternative_name)
  1453.     return 1 << atoi (XSTR (XEXP (exp, 0), 1));
  1454.  
  1455.   else if (code == IOR && GET_CODE (exp) == EQ_ATTR
  1456.        && XSTR (exp, 0) == alternative_name)
  1457.     return 1 << atoi (XSTR (exp, 1));
  1458.  
  1459.   else
  1460.     return 0;
  1461. }
  1462.  
  1463. /* Given I, a single-bit mask, return RTX to compare the `alternative'
  1464.    attribute with the value represented by that bit.  */
  1465.  
  1466. rtx
  1467. make_alternative_compare (mask)
  1468.      int mask;
  1469. {
  1470.   rtx newexp;
  1471.   int i;
  1472.   char *alternative;
  1473.  
  1474.   /* Find the bit.  */
  1475.   for (i = 0; (mask & (1 << i)) == 0; i++)
  1476.     ;
  1477.  
  1478.   alternative = (char *) xmalloc (3);
  1479.   sprintf (alternative, "%d", i);
  1480.  
  1481.   newexp = rtx_alloc (EQ_ATTR);
  1482.   XSTR (newexp, 0) = alternative_name;
  1483.   XSTR (newexp, 1) = alternative;
  1484.   RTX_UNCHANGING_P (newexp) = 1;
  1485.  
  1486.   return newexp;
  1487. }
  1488.  
  1489. /* If we are processing an (eq_attr "attr" "value") test, we find the value
  1490.    of "attr" for this insn code.  From that value, we can compute a test
  1491.    showing when the EQ_ATTR will be true.  This routine performs that
  1492.    computation.  If a test condition involves an address, we leave the EQ_ATTR
  1493.    intact because addresses are only valid for the `length' attribute.  */
  1494.  
  1495. rtx
  1496. evaluate_eq_attr (exp, value, insn_code, insn_index)
  1497.      rtx exp;
  1498.      rtx value;
  1499.      int insn_code, insn_index;
  1500. {
  1501.   rtx orexp, andexp;
  1502.   rtx right;
  1503.   rtx newexp;
  1504.   int i;
  1505.  
  1506.   if (GET_CODE (value) == CONST_STRING)
  1507.     {
  1508.       if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
  1509.     newexp = true_rtx;
  1510.       else
  1511.     newexp = false_rtx;
  1512.     }
  1513.   else if (GET_CODE (value) == COND)
  1514.     {
  1515.       /* We construct an IOR of all the cases for which the requested attribute
  1516.      value is present.  Since we start with FALSE, if it is not present,
  1517.      FALSE will be returned.
  1518.  
  1519.      Each case is the AND of the NOT's of the previous conditions with the
  1520.      current condition; in the default case the current condition is TRUE. 
  1521.  
  1522.      For each possible COND value, call ourselves recursively.
  1523.  
  1524.      The extra TRUE and FALSE expressions will be eliminated by another
  1525.      call to the simplification routine. */
  1526.  
  1527.       orexp = false_rtx;
  1528.       andexp = true_rtx;
  1529.  
  1530.       for (i = 0; i < XVECLEN (value, 0); i += 2)
  1531.     {
  1532.       right = insert_right_side (AND, andexp,
  1533.                      XVECEXP (value, 0, i),
  1534.                      insn_code, insn_index);
  1535.       right = insert_right_side (AND, right,
  1536.             evaluate_eq_attr (exp, XVECEXP (value, 0, i + 1),
  1537.                        insn_code, insn_index),
  1538.                      insn_code, insn_index);
  1539.       orexp = insert_right_side (IOR, orexp, right,
  1540.                      insn_code, insn_index);
  1541.  
  1542.       /* Add this condition into the AND expression.  */
  1543.       newexp = rtx_alloc (NOT);
  1544.       XEXP (newexp, 0) = XVECEXP (value, 0, i);
  1545.       andexp = insert_right_side (AND, andexp, newexp,
  1546.                       insn_code, insn_index);
  1547.     }
  1548.  
  1549.       /* Handle the default case.  */
  1550.       right = insert_right_side (AND, andexp,
  1551.                  evaluate_eq_attr (exp, XEXP (value, 1),
  1552.                             insn_code, insn_index),
  1553.                  insn_code, insn_index);
  1554.       newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
  1555.     }
  1556.   else
  1557.     abort ();
  1558.  
  1559.   /* If uses an address, must return original expression.  */
  1560.  
  1561.   address_used = 0;
  1562.   walk_attr_value (newexp);
  1563.  
  1564.   if (address_used)
  1565.     return exp;
  1566.   else
  1567.     return newexp;
  1568. }
  1569.  
  1570. /* This routine is called when an AND of a term with a tree of AND's is
  1571.    encountered.  If the term or its complement is present in the tree, it
  1572.    can be replaced with TRUE or FALSE, respectively.
  1573.  
  1574.    Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
  1575.    be true and hence are complementary.  
  1576.  
  1577.    There is one special case:  If we see
  1578.     (and (not (eq_attr "att" "v1"))
  1579.          (eq_attr "att" "v2"))
  1580.    this can be replaced by (eq_attr "att" "v2").  To do this we need to
  1581.    replace the term, not anything in the AND tree.  So we pass a pointer to
  1582.    the term.  */
  1583.  
  1584. rtx
  1585. simplify_and_tree (exp, pterm, insn_code, insn_index)
  1586.      rtx exp;
  1587.      rtx *pterm;
  1588.      int insn_code, insn_index;
  1589. {
  1590.   rtx left, right;
  1591.   rtx newexp;
  1592.   rtx temp;
  1593.   int left_eliminates_term, right_eliminates_term;
  1594.  
  1595.   if (GET_CODE (exp) == AND)
  1596.     {
  1597.       left = simplify_and_tree (XEXP (exp, 0), pterm,  insn_code, insn_index);
  1598.       right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
  1599.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1600.     {
  1601.       newexp = rtx_alloc (GET_CODE (exp));
  1602.       XEXP (newexp, 0) = left;
  1603.       XEXP (newexp, 1) = right;
  1604.  
  1605.       exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1606.     }
  1607.     }
  1608.  
  1609.   else if (GET_CODE (exp) == IOR)
  1610.     {
  1611.       /* For the IOR case, we do the same as above, except that we can
  1612.          only eliminate `term' if both sides of the IOR would do so.  */
  1613.       temp = *pterm;
  1614.       left = simplify_and_tree (XEXP (exp, 0), &temp,  insn_code, insn_index);
  1615.       left_eliminates_term = (temp == true_rtx);
  1616.  
  1617.       temp = *pterm;
  1618.       right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
  1619.       right_eliminates_term = (temp == true_rtx);
  1620.  
  1621.       if (left_eliminates_term && right_eliminates_term)
  1622.     *pterm = true_rtx;
  1623.  
  1624.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1625.     {
  1626.       newexp = rtx_alloc (GET_CODE (exp));
  1627.       XEXP (newexp, 0) = left;
  1628.       XEXP (newexp, 1) = right;
  1629.  
  1630.       exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1631.     }
  1632.     }
  1633.  
  1634.   /* Check for simplifications.  Do some extra checking here since this
  1635.      routine is called so many times.  */
  1636.  
  1637.   if (exp == *pterm)
  1638.     return true_rtx;
  1639.  
  1640.   else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
  1641.     return false_rtx;
  1642.  
  1643.   else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
  1644.     return false_rtx;
  1645.  
  1646.   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
  1647.     {
  1648.       if (XSTR (exp, 0) != XSTR (*pterm, 0))
  1649.     return exp;
  1650.  
  1651.       if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
  1652.     return true_rtx;
  1653.       else
  1654.     return false_rtx;
  1655.     }
  1656.  
  1657.   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
  1658.        && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
  1659.     {
  1660.       if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
  1661.     return exp;
  1662.  
  1663.       if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
  1664.     return false_rtx;
  1665.       else
  1666.     return true_rtx;
  1667.     }
  1668.  
  1669.   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
  1670.        && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
  1671.     {
  1672.       if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
  1673.     return exp;
  1674.  
  1675.       if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
  1676.     return false_rtx;
  1677.       else
  1678.     *pterm = true_rtx;
  1679.     }
  1680.  
  1681.   else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
  1682.     {
  1683.       if (rtx_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
  1684.     return true_rtx;
  1685.     }
  1686.  
  1687.   else if (GET_CODE (exp) == NOT)
  1688.     {
  1689.       if (rtx_equal_p (XEXP (exp, 0), *pterm))
  1690.     return false_rtx;
  1691.     }
  1692.  
  1693.   else if (GET_CODE (*pterm) == NOT)
  1694.     {
  1695.       if (rtx_equal_p (XEXP (*pterm, 0), exp))
  1696.     return false_rtx;
  1697.     }
  1698.  
  1699.   else if (rtx_equal_p (exp, *pterm))
  1700.     return true_rtx;
  1701.  
  1702.   return exp;
  1703. }
  1704.  
  1705. /* Similiar to `simplify_and_tree', but for IOR trees.  */
  1706.  
  1707. rtx
  1708. simplify_or_tree (exp, pterm, insn_code, insn_index)
  1709.      rtx exp;
  1710.      rtx *pterm;
  1711.      int insn_code, insn_index;
  1712. {
  1713.   rtx left, right;
  1714.   rtx newexp;
  1715.   rtx temp;
  1716.   int left_eliminates_term, right_eliminates_term;
  1717.  
  1718.   if (GET_CODE (exp) == IOR)
  1719.     {
  1720.       left = simplify_or_tree (XEXP (exp, 0), pterm,  insn_code, insn_index);
  1721.       right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
  1722.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1723.     {
  1724.       newexp = rtx_alloc (GET_CODE (exp));
  1725.       XEXP (newexp, 0) = left;
  1726.       XEXP (newexp, 1) = right;
  1727.  
  1728.       exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1729.     }
  1730.     }
  1731.  
  1732.   else if (GET_CODE (exp) == AND)
  1733.     {
  1734.       /* For the AND case, we do the same as above, except that we can
  1735.          only eliminate `term' if both sides of the AND would do so.  */
  1736.       temp = *pterm;
  1737.       left = simplify_or_tree (XEXP (exp, 0), &temp,  insn_code, insn_index);
  1738.       left_eliminates_term = (temp == false_rtx);
  1739.  
  1740.       temp = *pterm;
  1741.       right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
  1742.       right_eliminates_term = (temp == false_rtx);
  1743.  
  1744.       if (left_eliminates_term && right_eliminates_term)
  1745.     *pterm = false_rtx;
  1746.  
  1747.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1748.     {
  1749.       newexp = rtx_alloc (GET_CODE (exp));
  1750.       XEXP (newexp, 0) = left;
  1751.       XEXP (newexp, 1) = right;
  1752.  
  1753.       exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1754.     }
  1755.     }
  1756.  
  1757.   if (rtx_equal_p (exp, *pterm))
  1758.     return false_rtx;
  1759.  
  1760.   else if (GET_CODE (exp) == NOT && rtx_equal_p (XEXP (exp, 0), *pterm))
  1761.     return true_rtx;
  1762.  
  1763.   else if (GET_CODE (*pterm) == NOT && rtx_equal_p (XEXP (*pterm, 0), exp))
  1764.     return true_rtx;
  1765.  
  1766.   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
  1767.        && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  1768.        && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
  1769.     *pterm = false_rtx;
  1770.  
  1771.   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
  1772.        && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
  1773.        && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
  1774.     return false_rtx;
  1775.  
  1776.   return exp;
  1777. }
  1778.  
  1779. /* Given an expression, see if it can be simplified for a particular insn
  1780.    code based on the values of other attributes being tested.  This can
  1781.    eliminate nested get_attr_... calls.
  1782.  
  1783.    Note that if an endless recursion is specified in the patterns, the 
  1784.    optimization will loop.  However, it will do so in precisely the cases where
  1785.    an infinite recursion loop could occur during compilation.  It's better that
  1786.    it occurs here!  */
  1787.  
  1788. rtx
  1789. simplify_test_exp (exp, insn_code, insn_index)
  1790.      rtx exp;
  1791.      int insn_code, insn_index;
  1792. {
  1793.   rtx left, right;
  1794.   struct attr_desc *attr;
  1795.   struct attr_value *av;
  1796.   struct insn_ent *ie;
  1797.   int i;
  1798.   rtx newexp = exp;
  1799.  
  1800.   switch (GET_CODE (exp))
  1801.     {
  1802.     case AND:
  1803.       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  1804.       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
  1805.  
  1806.       /* If either side is an IOR and we have (eq_attr "alternative" ..")
  1807.      present on both sides, apply the distributive law since this will
  1808.      yield simplications.  */
  1809.       if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
  1810.       && compute_alternative_mask (left, IOR)
  1811.       && compute_alternative_mask (right, IOR))
  1812.     {
  1813.       if (GET_CODE (left) == IOR)
  1814.         {
  1815.           rtx tem = left;
  1816.           left = right;
  1817.           right = tem;
  1818.         }
  1819.  
  1820.       newexp = rtx_alloc (IOR);
  1821.       XEXP (newexp, 0) = rtx_alloc (AND);
  1822.       XEXP (newexp, 1) = rtx_alloc (AND);
  1823.       XEXP (XEXP (newexp, 0), 0) = XEXP (XEXP (newexp, 1), 0) = left;
  1824.       XEXP (XEXP (newexp, 0), 1) = XEXP (right, 0);
  1825.       XEXP (XEXP (newexp, 1), 1) = XEXP (right, 1);
  1826.  
  1827.       return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1828.     }
  1829.  
  1830.       /* Try with the term on both sides.  */
  1831.       right = simplify_and_tree (right, &left, insn_code, insn_index);
  1832.       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
  1833.     left = simplify_and_tree (left, &right, insn_code, insn_index);
  1834.  
  1835.       if (left == false_rtx || right == false_rtx)
  1836.     return false_rtx;
  1837.       else if (left == true_rtx)
  1838.     return right;
  1839.       else if (right == true_rtx)
  1840.     return left;
  1841.  
  1842.       /* See if all or all but one of the insn's alternatives are specified
  1843.      in this tree.  Optimize if so.  */
  1844.  
  1845.       else if (insn_code >= 0
  1846.            && (GET_CODE (left) == AND
  1847.            || (GET_CODE (left) == NOT
  1848.                && GET_CODE (XEXP (left, 0)) == EQ_ATTR
  1849.                && XSTR (XEXP (left, 0), 0) == alternative_name)
  1850.            || GET_CODE (right) == AND
  1851.            || (GET_CODE (right) == NOT
  1852.                && GET_CODE (XEXP (right, 0)) == EQ_ATTR
  1853.                && XSTR (XEXP (right, 0), 0) == alternative_name)))
  1854.     {
  1855.       i = compute_alternative_mask (exp, AND);
  1856.       if (i & ~insn_alternatives[insn_code])
  1857.         fatal ("Illegal alternative specified for pattern number %d",
  1858.            insn_index);
  1859.  
  1860.       /* If all alternatives are excluded, this is false. */
  1861.       i ^= insn_alternatives[insn_code];
  1862.       if (i == 0)
  1863.         return false_rtx;
  1864.       else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
  1865.         {
  1866.           /* If just one excluded, AND a comparison with that one to the
  1867.          front of the tree.  The others will be eliminated by
  1868.          optimization.  We do not want to do this if the insn has one
  1869.          alternative and we have tested none of them!  */
  1870.           left = make_alternative_compare (i);
  1871.           right = simplify_and_tree (exp, &left, insn_code, insn_index);
  1872.           newexp = rtx_alloc (AND);
  1873.           XEXP (newexp, 0) = left;
  1874.           XEXP (newexp, 1) = right;
  1875.  
  1876.           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1877.         }
  1878.     }
  1879.  
  1880.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1881.     {
  1882.       newexp = rtx_alloc (AND);
  1883.       XEXP (newexp, 0) = left;
  1884.       XEXP (newexp, 1) = right;
  1885.       return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1886.     }
  1887.       break;
  1888.  
  1889.     case IOR:
  1890.       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  1891.       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
  1892.  
  1893.       right = simplify_or_tree (right, &left, insn_code, insn_index);
  1894.       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
  1895.     left = simplify_or_tree (left, &right, insn_code, insn_index);
  1896.  
  1897.       if (right == true_rtx || left == true_rtx)
  1898.     return true_rtx;
  1899.       else if (left == false_rtx)
  1900.     return right;
  1901.       else if (right == false_rtx)
  1902.     return left;
  1903.  
  1904.       /* Test for simple cases where the distributive law is useful.  I.e.,
  1905.         convert (ior (and (x) (y))
  1906.              (and (x) (z)))
  1907.         to      (and (x)
  1908.              (ior (y) (z)))
  1909.        */
  1910.  
  1911.       else if (GET_CODE (left) == AND && GET_CODE (right) == AND
  1912.       && rtx_equal_p (XEXP (left, 0), XEXP (right, 0)))
  1913.     {
  1914.       newexp = rtx_alloc (IOR);
  1915.       XEXP (newexp, 0) = XEXP (left, 1);
  1916.       XEXP (newexp, 1) = XEXP (right, 1);
  1917.  
  1918.       left = XEXP (left, 0);
  1919.       right = newexp;
  1920.       newexp = rtx_alloc (AND);
  1921.       XEXP (newexp, 0) = left;
  1922.       XEXP (newexp, 1) = right;
  1923.       return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1924.     }
  1925.  
  1926.       /* See if all or all but one of the insn's alternatives are specified
  1927.      in this tree.  Optimize if so.  */
  1928.  
  1929.       else if (insn_code >= 0
  1930.       && (GET_CODE (left) == IOR
  1931.           || (GET_CODE (left) == EQ_ATTR
  1932.           && XSTR (left, 0) == alternative_name)
  1933.           || GET_CODE (right) == IOR
  1934.           || (GET_CODE (right) == EQ_ATTR
  1935.           && XSTR (right, 0) == alternative_name)))
  1936.     {
  1937.       i = compute_alternative_mask (exp, IOR);
  1938.       if (i & ~insn_alternatives[insn_code])
  1939.         fatal ("Illegal alternative specified for pattern number %d",
  1940.            insn_index);
  1941.  
  1942.       /* If all alternatives are included, this is true. */
  1943.       i ^= insn_alternatives[insn_code];
  1944.       if (i == 0)
  1945.         return true_rtx;
  1946.       else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
  1947.         {
  1948.           /* If just one excluded, IOR a comparison with that one to the
  1949.          front of the tree.  The others will be eliminated by
  1950.          optimization.  We do not want to do this if the insn has one
  1951.          alternative and we have tested none of them!  */
  1952.           left = make_alternative_compare (i);
  1953.           right = simplify_and_tree (exp, &left, insn_code, insn_index);
  1954.           newexp = rtx_alloc (IOR);
  1955.           XEXP (newexp, 0) = rtx_alloc (NOT);
  1956.           XEXP (XEXP (newexp, 0), 0) = left;
  1957.           XEXP (newexp, 1) = right;
  1958.  
  1959.           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1960.         }
  1961.     }
  1962.  
  1963.       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1964.     {
  1965.       newexp = rtx_alloc (IOR);
  1966.       XEXP (newexp, 0) = left;
  1967.       XEXP (newexp, 1) = right;
  1968.       return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1969.     }
  1970.       break;
  1971.  
  1972.     case NOT:
  1973.       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  1974.       if (GET_CODE (left) == NOT)
  1975.     return XEXP (left, 0);
  1976.  
  1977.       if (left == false_rtx)
  1978.     return true_rtx;
  1979.       else if (left == true_rtx)
  1980.     return false_rtx;
  1981.  
  1982.       /* Try to apply De`Morgan's laws.  */
  1983.       else if (GET_CODE (left) == IOR)
  1984.     {
  1985.       newexp = rtx_alloc (AND);
  1986.       XEXP (newexp, 0) = rtx_alloc (NOT);
  1987.       XEXP (XEXP (newexp, 0), 0) = XEXP (left, 0);
  1988.       XEXP (newexp, 1) = rtx_alloc (NOT);
  1989.       XEXP (XEXP (newexp, 1), 0) = XEXP (left, 1);
  1990.  
  1991.       newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  1992.     }
  1993.       else if (GET_CODE (left) == AND)
  1994.     {
  1995.       newexp = rtx_alloc (IOR);
  1996.       XEXP (newexp, 0) = rtx_alloc (NOT);
  1997.       XEXP (XEXP (newexp, 0), 0) = XEXP (left, 0);
  1998.       XEXP (newexp, 1) = rtx_alloc (NOT);
  1999.       XEXP (XEXP (newexp, 1), 0) = XEXP (left, 1);
  2000.  
  2001.       newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2002.     }
  2003.       else if (left != XEXP (exp, 0))
  2004.     {
  2005.       newexp = rtx_alloc (NOT);
  2006.       XEXP (newexp, 0) = left;
  2007.     }
  2008.       break;
  2009.  
  2010.     case EQ_ATTR:
  2011.       /* Look at the value for this insn code in the specified attribute.
  2012.      We normally can replace this comparison with the condition that
  2013.      would give this insn the values being tested for.   */
  2014.       if (XSTR (exp, 0) != alternative_name
  2015.       && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
  2016.     for (av = attr->first_value; av; av = av->next)
  2017.       for (ie = av->first_insn; ie; ie = ie->next)
  2018.         if (ie->insn_code == insn_code)
  2019.           return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
  2020.     }
  2021.  
  2022.   /* We have already simplified this expression.  Simplifying it again
  2023.      won't buy anything unless we weren't given a valid insn code
  2024.      to process (i.e., we are canonicalizing something.).  */
  2025.   if (insn_code != -2)
  2026.     RTX_UNCHANGING_P (newexp) = 1;
  2027.  
  2028.   return newexp;
  2029. }
  2030.  
  2031. /* Optimize the attribute lists by seeing if we can determine conditional
  2032.    values from the known values of other attributes.  This will save subroutine
  2033.    calls during the compilation.  */
  2034.  
  2035. void
  2036. optimize_attrs ()
  2037. {
  2038.   struct attr_desc *attr;
  2039.   struct attr_value *av;
  2040.   struct insn_ent *ie, *nextie;
  2041.   rtx newexp;
  2042.   int something_changed = 1;
  2043.  
  2044.   /* Loop until nothing changes for one iteration.  */
  2045.   while (something_changed)
  2046.     {
  2047.       something_changed = 0;
  2048.       for (attr = attrs; attr; attr = attr->next)
  2049.     for (av = attr->first_value; av; av = av->next)
  2050.         for (ie = av->first_insn; ie; ie = nextie)
  2051.           {
  2052.         nextie = ie->next;
  2053.         if (GET_CODE (av->value) != COND)
  2054.           continue;
  2055.  
  2056.         newexp = simplify_cond (av->value, ie->insn_code,
  2057.                     ie->insn_index);
  2058.         if (newexp != av->value)
  2059.           {
  2060.             remove_insn_ent (av, ie);
  2061.             insert_insn_ent (get_attr_value (newexp, attr,
  2062.                              ie->insn_code), ie);
  2063.             something_changed = 1;
  2064.           }
  2065.           }
  2066.     }
  2067. }
  2068.  
  2069. /* Create table entries for DEFINE_ATTR.  */
  2070.  
  2071. void
  2072. gen_attr (exp)
  2073.      rtx exp;
  2074. {
  2075.   struct attr_desc *attr;
  2076.   struct attr_value *av;
  2077.   char *name_ptr;
  2078.   char *p;
  2079.  
  2080.   /* Make a new attribute structure.  Check for duplicate by looking at
  2081.      attr->default_val, since it is initialized by this routine.  */
  2082.   attr = find_attr (XSTR (exp, 0), 1);
  2083.   if (attr->default_val)
  2084.     fatal ("Duplicate definition for `%s' attribute", attr->name);
  2085.  
  2086.   if (*XSTR (exp, 1) == '\0')
  2087.       attr->is_numeric = 1;
  2088.   else
  2089.     {
  2090.       name_ptr = XSTR (exp, 1);
  2091.       while ((p = next_comma_elt (&name_ptr)) != NULL)
  2092.     {
  2093.       av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
  2094.       av->value = rtx_alloc (CONST_STRING);
  2095.       XSTR (av->value, 0) = p;
  2096.       av->next = attr->first_value;
  2097.       attr->first_value = av;
  2098.       av->first_insn = NULL;
  2099.       av->num_insns = 0;
  2100.       av->has_asm_insn = 0;
  2101.     }
  2102.     }
  2103.  
  2104.   if (! strcmp (attr->name, "length") && ! attr->is_numeric)
  2105.     fatal ("`length' attribute must take numeric values");
  2106.  
  2107.   /* Set up the default value. */
  2108.   check_attr_value (XEXP (exp, 2), attr);
  2109.   attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
  2110. }
  2111.  
  2112. /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
  2113.    alternatives in the constraints.  Assume all MATCH_OPERANDs have the same
  2114.    number of alternatives as this should be checked elsewhere.  */
  2115.  
  2116. int
  2117. count_alternatives (exp)
  2118.      rtx exp;
  2119. {
  2120.   int i, j, n;
  2121.   char *fmt;
  2122.   
  2123.   if (GET_CODE (exp) == MATCH_OPERAND)
  2124.     return n_comma_elts (XSTR (exp, 2));
  2125.  
  2126.   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
  2127.        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  2128.     switch (*fmt++)
  2129.       {
  2130.       case 'e':
  2131.       case 'u':
  2132.     n = count_alternatives (XEXP (exp, i));
  2133.     if (n)
  2134.       return n;
  2135.     break;
  2136.  
  2137.       case 'E':
  2138.       case 'V':
  2139.     if (XVEC (exp, i) != NULL)
  2140.       for (j = 0; j < XVECLEN (exp, i); j++)
  2141.         {
  2142.           n = count_alternatives (XVECEXP (exp, i, j));
  2143.           if (n)
  2144.         return n;
  2145.         }
  2146.       }
  2147.  
  2148.   return 0;
  2149. }
  2150.  
  2151. /* Returns non-zero if the given expression contains an EQ_ATTR with the
  2152.    `alternative' attribute.  */
  2153.  
  2154. int
  2155. compares_alternatives_p (exp)
  2156.      rtx exp;
  2157. {
  2158.   int i, j;
  2159.   char *fmt;
  2160.  
  2161.   if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
  2162.     return 1;
  2163.  
  2164.   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
  2165.        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  2166.     switch (*fmt++)
  2167.       {
  2168.       case 'e':
  2169.       case 'u':
  2170.     if (compares_alternatives_p (XEXP (exp, i)))
  2171.       return 1;
  2172.     break;
  2173.  
  2174.       case 'E':
  2175.     for (j = 0; j < XVECLEN (exp, i); j++)
  2176.       if (compares_alternatives_p (XVECEXP (exp, i, j)))
  2177.         return 1;
  2178.     break;
  2179.       }
  2180.  
  2181.   return 0;
  2182. }
  2183.  
  2184. /* Returns non-zero is INNER is contained in EXP.  */
  2185.  
  2186. int
  2187. contained_in_p (inner, exp)
  2188.      rtx inner;
  2189.      rtx exp;
  2190. {
  2191.   int i, j;
  2192.   char *fmt;
  2193.  
  2194.   if (rtx_equal_p (inner, exp))
  2195.     return 1;
  2196.  
  2197.   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
  2198.        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  2199.     switch (*fmt++)
  2200.       {
  2201.       case 'e':
  2202.       case 'u':
  2203.     if (contained_in_p (inner, XEXP (exp, i)))
  2204.       return 1;
  2205.     break;
  2206.  
  2207.       case 'E':
  2208.     for (j = 0; j < XVECLEN (exp, i); j++)
  2209.       if (contained_in_p (inner, XVECEXP (exp, i, j)))
  2210.         return 1;
  2211.     break;
  2212.       }
  2213.  
  2214.   return 0;
  2215. }
  2216.     
  2217. /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES.  */
  2218.  
  2219. void
  2220. gen_insn (exp)
  2221.      rtx exp;
  2222. {
  2223.   struct insn_def *id;
  2224.  
  2225.   id = (struct insn_def *) xmalloc (sizeof (struct insn_def));
  2226.   id->next = defs;
  2227.   defs = id;
  2228.   id->def = exp;
  2229.  
  2230.   switch (GET_CODE (exp))
  2231.     {
  2232.     case DEFINE_INSN:
  2233.       id->insn_code = insn_code_number++;
  2234.       id->insn_index = insn_index_number++;
  2235.       id->num_alternatives = count_alternatives (exp);
  2236.       if (id->num_alternatives == 0)
  2237.     id->num_alternatives = 1;
  2238.       id->vec_idx = 4;
  2239.       break;
  2240.  
  2241.     case DEFINE_PEEPHOLE:
  2242.       id->insn_code = insn_code_number++;
  2243.       id->insn_index = insn_index_number++;
  2244.       id->num_alternatives = count_alternatives (exp);
  2245.       if (id->num_alternatives == 0)
  2246.     id->num_alternatives = 1;
  2247.       id->vec_idx = 3;
  2248.       break;
  2249.  
  2250.     case DEFINE_ASM_ATTRIBUTES:
  2251.       id->insn_code = -1;
  2252.       id->insn_index = -1;
  2253.       id->num_alternatives = 1;
  2254.       id->vec_idx = 0;
  2255.       got_define_asm_attributes = 1;
  2256.       break;
  2257.     }
  2258. }
  2259.  
  2260. /* Process a DEFINE_DELAY.  Validate the vector length, check if annul
  2261.    true or annul false is specified, and make a `struct delay_desc'.  */
  2262.  
  2263. void
  2264. gen_delay (def)
  2265.      rtx def;
  2266. {
  2267.   struct delay_desc *delay;
  2268.   int i;
  2269.  
  2270.   if (XVECLEN (def, 1) % 3 != 0)
  2271.     fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
  2272.  
  2273.   for (i = 0; i < XVECLEN (def, 1); i += 3)
  2274.     {
  2275.       if (XVECEXP (def, 1, i + 1))
  2276.     have_annul_true = 1;
  2277.       if (XVECEXP (def, 1, i + 2))
  2278.     have_annul_false = 1;
  2279.     }
  2280.   
  2281.   delay = (struct delay_desc *) xmalloc (sizeof (struct delay_desc));
  2282.   delay->def = def;
  2283.   delay->num = ++num_delays;
  2284.   delay->next = delays;
  2285.   delays = delay;
  2286. }
  2287.  
  2288. /* Process a DEFINE_FUNCTION_UNIT.  
  2289.  
  2290.    This gives information about a function unit contained in the CPU.
  2291.    We fill in a `struct function_unit_op' and a `struct function_unit'
  2292.    with information used later by `expand_unit'.  */
  2293.  
  2294. void
  2295. gen_unit (def)
  2296.      rtx def;
  2297. {
  2298.   struct function_unit *unit;
  2299.   struct function_unit_op *op;
  2300.  
  2301.   /* See if we have already seen this function unit.  If so, check that
  2302.      the multipicity and simultaneity values are the same.  If not, make
  2303.      a structure for this function unit.  */
  2304.   for (unit = units; unit; unit = unit->next)
  2305.     if (! strcmp (unit->name, XSTR (def, 0)))
  2306.       {
  2307.     if (unit->multiplicity != XINT (def, 1)
  2308.         || unit->simultaneity != XINT (def, 2))
  2309.       fatal ("Differing specifications given for `%s' function unit.",
  2310.          unit->name);
  2311.     break;
  2312.       }
  2313.  
  2314.   if (unit == 0)
  2315.     {
  2316.       unit = (struct function_unit *) xmalloc (sizeof (struct function_unit));
  2317.       unit->name = XSTR (def, 0);
  2318.       unit->multiplicity = XINT (def, 1);
  2319.       unit->simultaneity = XINT (def, 2);
  2320.       unit->num = num_units++;
  2321.       unit->num_opclasses = 0;
  2322.       unit->condexp = false_rtx;
  2323.       unit->ops = 0;
  2324.       unit->next = units;
  2325.       units = unit;
  2326.     }
  2327.  
  2328.   /* Make a new operation class structure entry and initialize it.  */
  2329.   op = (struct function_unit_op *) xmalloc (sizeof (struct function_unit_op));
  2330.   op->condexp = XEXP (def, 3);
  2331.   op->num = unit->num_opclasses++;
  2332.   op->ready = XINT (def, 4);
  2333.   op->next = unit->ops;
  2334.   unit->ops = op;
  2335.  
  2336.   /* Set our busy expression based on whether or not an optional conflict
  2337.      vector was specified.  */
  2338.   if (XVEC (def, 6))
  2339.     {
  2340.       /* Compute the IOR of all the specified expressions.  */
  2341.       rtx orexp = false_rtx;
  2342.       int i;
  2343.  
  2344.       for (i = 0; i < XVECLEN (def, 6); i++)
  2345.     orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2);
  2346.  
  2347.       op->busyexp = rtx_alloc (IF_THEN_ELSE);
  2348.       XEXP (op->busyexp, 0) = orexp;
  2349.       XEXP (op->busyexp, 1) = make_numeric_value (XINT (def, 5));
  2350.       XEXP (op->busyexp, 2) = make_numeric_value (0);
  2351.     }
  2352.   else
  2353.     op->busyexp = make_numeric_value (XINT (def, 5));
  2354.  
  2355.   /* Merge our conditional into that of the function unit so we can determine
  2356.      which insns are used by the function unit.  */
  2357.   unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2);
  2358. }
  2359.  
  2360. /* Given a piece of RTX, print a C expression to test it's truth value.
  2361.    We use AND and IOR both for logical and bit-wise operations, so 
  2362.    interpret them as logical unless they are inside a comparison expression.
  2363.    The second operand of this function will be non-zero in that case.  */
  2364.  
  2365. void
  2366. write_test_expr (exp, in_comparison)
  2367.      rtx exp;
  2368.      int in_comparison;
  2369. {
  2370.   int comparison_operator = 0;
  2371.   RTX_CODE code;
  2372.   struct attr_desc *attr;
  2373.  
  2374.   /* In order not to worry about operator precedence, surround our part of
  2375.      the expression with parentheses.  */
  2376.  
  2377.   printf ("(");
  2378.   code = GET_CODE (exp);
  2379.   switch (code)
  2380.     {
  2381.     /* Binary operators.  */
  2382.     case EQ: case NE:
  2383.     case GE: case GT: case GEU: case GTU:
  2384.     case LE: case LT: case LEU: case LTU:
  2385.       comparison_operator = 1;
  2386.  
  2387.     case PLUS:   case MINUS:  case MULT:     case DIV:      case MOD:
  2388.     case AND:    case IOR:    case XOR:
  2389.     case LSHIFT: case ASHIFT: case LSHIFTRT: case ASHIFTRT:
  2390.       write_test_expr (XEXP (exp, 0), in_comparison || comparison_operator);
  2391.       switch (code)
  2392.         {
  2393.     case EQ:
  2394.       printf (" == ");
  2395.       break;
  2396.     case NE:
  2397.       printf (" != ");
  2398.       break;
  2399.     case GE:
  2400.       printf (" >= ");
  2401.       break;
  2402.     case GT:
  2403.       printf (" > ");
  2404.       break;
  2405.     case GEU:
  2406.       printf (" >= (unsigned) ");
  2407.       break;
  2408.     case GTU:
  2409.       printf (" > (unsigned) ");
  2410.       break;
  2411.     case LE:
  2412.       printf (" <= ");
  2413.       break;
  2414.     case LT:
  2415.       printf (" < ");
  2416.       break;
  2417.     case LEU:
  2418.       printf (" <= (unsigned) ");
  2419.       break;
  2420.     case LTU:
  2421.       printf (" < (unsigned) ");
  2422.       break;
  2423.     case PLUS:
  2424.       printf (" + ");
  2425.       break;
  2426.     case MINUS:
  2427.       printf (" - ");
  2428.       break;
  2429.     case MULT:
  2430.       printf (" * ");
  2431.       break;
  2432.     case DIV:
  2433.       printf (" / ");
  2434.       break;
  2435.     case MOD:
  2436.       printf (" % ");
  2437.       break;
  2438.     case AND:
  2439.       if (in_comparison)
  2440.         printf (" & ");
  2441.       else
  2442.         printf (" && ");
  2443.       break;
  2444.     case IOR:
  2445.       if (in_comparison)
  2446.         printf (" | ");
  2447.       else
  2448.         printf (" || ");
  2449.       break;
  2450.     case XOR:
  2451.       printf (" ^ ");
  2452.       break;
  2453.     case LSHIFT:
  2454.     case ASHIFT:
  2455.       printf (" << ");
  2456.       break;
  2457.     case LSHIFTRT:
  2458.     case ASHIFTRT:
  2459.       printf (" >> ");
  2460.       break;
  2461.         }
  2462.  
  2463.       write_test_expr (XEXP (exp, 1), in_comparison || comparison_operator);
  2464.       break;
  2465.  
  2466.     case NOT:
  2467.       /* Special-case (not (eq_attrq "alternative" "x")) */
  2468.       if (! in_comparison && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  2469.       && XSTR (XEXP (exp, 0), 0) == alternative_name)
  2470.     {
  2471.       printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
  2472.       break;
  2473.     }
  2474.  
  2475.       /* Otherwise, fall through to normal unary operator.  */
  2476.  
  2477.     /* Unary operators.  */   
  2478.     case ABS:  case NEG:
  2479.       switch (code)
  2480.     {
  2481.     case NOT:
  2482.       if (in_comparison)
  2483.         printf ("~ ");
  2484.       else
  2485.         printf ("! ");
  2486.       break;
  2487.     case ABS:
  2488.       printf ("abs ");
  2489.       break;
  2490.     case NEG:
  2491.       printf ("-");
  2492.       break;
  2493.     }
  2494.  
  2495.       write_test_expr (XEXP (exp, 0), in_comparison);
  2496.       break;
  2497.  
  2498.     /* Comparison test of an attribute with a value.  Most of these will
  2499.        have been removed by optimization.   Handle "alternative"
  2500.        specially and give error if EQ_ATTR present inside a comparison.  */
  2501.     case EQ_ATTR:
  2502.       if (in_comparison)
  2503.     fatal ("EQ_ATTR not valid inside comparison");
  2504.  
  2505.       if (XSTR (exp, 0) == alternative_name)
  2506.     {
  2507.       printf ("which_alternative == %s", XSTR (exp, 1));
  2508.       break;
  2509.     }
  2510.  
  2511.       attr = find_attr (XSTR (exp, 0), 0);
  2512.       if (! attr) abort ();
  2513.       printf ("get_attr_%s (insn) == ", attr->name);
  2514.       write_attr_valueq (attr, XSTR (exp, 1)); 
  2515.       break;
  2516.  
  2517.     /* See if an operand matches a predicate.  */
  2518.     case MATCH_OPERAND:
  2519.       /* If only a mode is given, just ensure the mode matches the operand.
  2520.      If neither a mode nor predicate is given, error.  */
  2521.      if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
  2522.     {
  2523.       if (GET_MODE (exp) == VOIDmode)
  2524.         fatal ("Null MATCH_OPERAND specified as test");
  2525.       else
  2526.         printf ("GET_MODE (operands[%d]) == %smode",
  2527.             XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
  2528.     }
  2529.       else
  2530.     printf ("%s (operands[%d], %smode)",
  2531.         XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
  2532.       break;
  2533.  
  2534.     /* Constant integer. */
  2535.     case CONST_INT:
  2536.       printf ("%d", XINT (exp, 0));
  2537.       break;
  2538.  
  2539.     /* A random C expression. */
  2540.     case SYMBOL_REF:
  2541.       printf ("%s", XSTR (exp, 0));
  2542.       break;
  2543.  
  2544.     /* The address of the branch target.  */
  2545.     case MATCH_DUP:
  2546.       printf ("insn_addresses[INSN_UID (JUMP_LABEL (insn))]");
  2547.       break;
  2548.  
  2549.     /* The address of the current insn.  It would be more consistent with
  2550.        other usage to make this the address of the NEXT insn, but this gets
  2551.        too confusing because of the ambiguity regarding the length of the
  2552.        current insn.  */
  2553.     case PC:
  2554.       printf ("insn_current_address");
  2555.       break;
  2556.  
  2557.     default:
  2558.       fatal ("bad RTX code `%s' in attribute calculation\n",
  2559.          GET_RTX_NAME (code));
  2560.     }
  2561.  
  2562.   printf (")");
  2563. }
  2564.  
  2565. /* Given an attribute value, return the maximum CONST_STRING argument
  2566.    encountered.  It is assumed that they are all numeric.  */
  2567.  
  2568. int
  2569. max_attr_value (exp)
  2570.      rtx exp;
  2571. {
  2572.   int current_max = 0;
  2573.   int n;
  2574.   int i;
  2575.  
  2576.   if (GET_CODE (exp) == CONST_STRING)
  2577.     return atoi (XSTR (exp, 0));
  2578.  
  2579.   else if (GET_CODE (exp) == COND)
  2580.     {
  2581.       for (i = 0; i < XVECLEN (exp, 0); i += 2)
  2582.     {
  2583.       n = max_attr_value (XVECEXP (exp, 0, i + 1));
  2584.       if (n > current_max)
  2585.         current_max = n;
  2586.     }
  2587.  
  2588.       n = max_attr_value (XEXP (exp, 1));
  2589.       if (n > current_max)
  2590.     current_max = n;
  2591.     }
  2592.  
  2593.   else
  2594.     abort ();
  2595.  
  2596.   return current_max;
  2597. }
  2598.  
  2599. /* Scan an attribute value, possibly a conditional, and record what actions
  2600.    will be required to do any conditional tests in it.
  2601.  
  2602.    Specifically, set
  2603.     `must_extract'      if we need to extract the insn operands
  2604.     `must_constrain'  if we must compute `which_alternative'
  2605.     `address_used'      if an address expression was used
  2606.  */
  2607.  
  2608. void
  2609. walk_attr_value (exp)
  2610.      rtx exp;
  2611. {
  2612.   register int i, j;
  2613.   register char *fmt;
  2614.   RTX_CODE code;
  2615.  
  2616.   if (exp == NULL)
  2617.     return;
  2618.  
  2619.   code = GET_CODE (exp);
  2620.   switch (code)
  2621.     {
  2622.     case SYMBOL_REF:
  2623.       /* Since this is an arbitrary expression, it can look at anything. */
  2624.       must_extract = must_constrain = 1;
  2625.       return;
  2626.  
  2627.     case MATCH_OPERAND:
  2628.       must_extract = 1;
  2629.       return;
  2630.  
  2631.     case EQ_ATTR:
  2632.       if (XSTR (exp, 0) == alternative_name)
  2633.     must_extract = must_constrain = 1;
  2634.       return;
  2635.  
  2636.     case MATCH_DUP:
  2637.     case PC:
  2638.       address_used = 1;
  2639.       return;
  2640.     }
  2641.  
  2642.   for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
  2643.     switch (*fmt++)
  2644.       {
  2645.       case 'e':
  2646.       case 'u':
  2647.     walk_attr_value (XEXP (exp, i));
  2648.     break;
  2649.  
  2650.       case 'E':
  2651.     if (XVEC (exp, i) != NULL)
  2652.       for (j = 0; j < XVECLEN (exp, i); j++)
  2653.         walk_attr_value (XVECEXP (exp, i, j));
  2654.     break;
  2655.       }
  2656. }
  2657.  
  2658. /* Write out a function to obtain the attribute for a given INSN.  */
  2659.  
  2660. void
  2661. write_attr_get (attr)
  2662.      struct attr_desc *attr;
  2663. {
  2664.   struct attr_value *av, *common_av;
  2665.  
  2666.   /* Find the most used attribute value.  Handle that as the `default' of the
  2667.      switch we will generate. */
  2668.   common_av = find_most_used (attr);
  2669.  
  2670.   /* Write out start of function, then all values with explicit `case' lines,
  2671.      then a `default', then the value with the most uses.  */
  2672.   if (attr->is_numeric)
  2673.     printf ("int\n");
  2674.   else
  2675.     printf ("enum attr_%s\n", attr->name);
  2676.  
  2677.   /* If the attribute name starts with a star, the remainder is the name of
  2678.      the subroutine to use, instead of `get_attr_...'.  */
  2679.   if (attr->name[0] == '*')
  2680.     printf ("%s (insn)\n", &attr->name[1]);
  2681.   else
  2682.     printf ("get_attr_%s (insn)\n", attr->name);
  2683.   printf ("     rtx insn;\n");
  2684.   printf ("{\n");
  2685.   printf ("  switch (recog_memoized (insn))\n");
  2686.   printf ("    {\n");
  2687.  
  2688.   for (av = attr->first_value; av; av = av->next)
  2689.     if (av != common_av)
  2690.       write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
  2691.  
  2692.   write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
  2693.   printf ("    }\n}\n\n");
  2694. }
  2695.  
  2696. /* Given an AND tree of known true terms (because we are inside an `if' with
  2697.    that as the condition or are in an `else' clause) and an expression,
  2698.    replace any known true terms with TRUE.  Use `simplify_and_tree' to do
  2699.    the bulk of the work.  */
  2700.  
  2701. rtx
  2702. eliminate_known_true (known_true, exp, insn_code, insn_index)
  2703.      rtx known_true;
  2704.      rtx exp;
  2705.      int insn_code, insn_index;
  2706. {
  2707.   rtx term;
  2708.  
  2709.   known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
  2710.  
  2711.   if (GET_CODE (known_true) == AND)
  2712.     {
  2713.       exp = eliminate_known_true (XEXP (known_true, 0), exp,
  2714.                   insn_code, insn_index);
  2715.       exp = eliminate_known_true (XEXP (known_true, 1), exp,
  2716.                   insn_code, insn_index);
  2717.     }
  2718.   else
  2719.     {
  2720.       term = known_true;
  2721.       exp = simplify_and_tree (exp, &term, insn_code, insn_index);
  2722.     }
  2723.  
  2724.   return exp;
  2725. }
  2726.  
  2727. /* Write out a series of tests and assignment statements to perform tests and
  2728.    sets of an attribute value.  We are passed an indentation amount and prefix
  2729.    and suffix strings to write around each attribute value (e.g., "return"
  2730.    and ";").  */
  2731.  
  2732. void
  2733. write_attr_set (attr, indent, value, prefix, suffix, known_true,
  2734.         insn_code, insn_index)
  2735.      struct attr_desc *attr;
  2736.      int indent;
  2737.      rtx value;
  2738.      char *prefix;
  2739.      char *suffix;
  2740.      rtx known_true;
  2741.      int insn_code, insn_index;
  2742. {
  2743.   if (GET_CODE (value) == CONST_STRING)
  2744.     {
  2745.       write_indent (indent);
  2746.       printf ("%s ", prefix);
  2747.       write_attr_value (attr, value);
  2748.       printf ("%s\n", suffix);
  2749.     }
  2750.   else if (GET_CODE (value) == COND)
  2751.     {
  2752.       /* Assume the default value will be the default of the COND unless we
  2753.      find an always true expression.  */
  2754.       rtx default_val = XEXP (value, 1);
  2755.       rtx our_known_true = known_true;
  2756.       rtx newexp;
  2757.       int first_if = 1;
  2758.       int i;
  2759.  
  2760.       for (i = 0; i < XVECLEN (value, 0); i += 2)
  2761.     {
  2762.       rtx testexp;
  2763.       rtx inner_true;
  2764.  
  2765.       testexp = eliminate_known_true (our_known_true,
  2766.                       XVECEXP (value, 0, i),
  2767.                       insn_code, insn_index);
  2768.       newexp = rtx_alloc (NOT);
  2769.       XEXP (newexp, 0) = testexp;
  2770.       newexp  = insert_right_side (AND, our_known_true, newexp,
  2771.                        insn_code, insn_index);
  2772.  
  2773.       /* If the test expression is always true or if the next `known_true'
  2774.          expression is always false, this is the last case, so break
  2775.          out and let this value be the `else' case.  */
  2776.       if (testexp == true_rtx || newexp == false_rtx)
  2777.         {
  2778.           default_val = XVECEXP (value, 0, i + 1);
  2779.           break;
  2780.         }
  2781.  
  2782.       /* Compute the expression to pass to our recursive call as being
  2783.          known true.  */
  2784.       inner_true = insert_right_side (AND, our_known_true,
  2785.                       testexp, insn_code, insn_index);
  2786.  
  2787.       /* If this is always false, skip it.  */
  2788.       if (inner_true == false_rtx)
  2789.         continue;
  2790.  
  2791.       write_indent (indent);
  2792.       printf ("%sif ", first_if ? "" : "else ");
  2793.       first_if = 0;
  2794.       write_test_expr (testexp, 0);
  2795.       printf ("\n");
  2796.       write_indent (indent + 2);
  2797.       printf ("{\n");
  2798.  
  2799.       write_attr_set (attr, indent + 4,  
  2800.               XVECEXP (value, 0, i + 1), prefix, suffix,
  2801.               inner_true, insn_code, insn_index);
  2802.       write_indent (indent + 2);
  2803.       printf ("}\n");
  2804.       our_known_true = newexp;
  2805.     }
  2806.  
  2807.       if (! first_if)
  2808.     {
  2809.       write_indent (indent);
  2810.       printf ("else\n");
  2811.       write_indent (indent + 2);
  2812.       printf ("{\n");
  2813.     }
  2814.  
  2815.       write_attr_set (attr, first_if ? indent : indent + 4, default_val,
  2816.               prefix, suffix, our_known_true, insn_code, insn_index);
  2817.  
  2818.       if (! first_if)
  2819.     {
  2820.       write_indent (indent + 2);
  2821.       printf ("}\n");
  2822.     }
  2823.     }
  2824.   else
  2825.     abort ();
  2826. }
  2827.  
  2828. /* Write out the computation for one attribute value.  */
  2829.  
  2830. void
  2831. write_attr_case (attr, av, write_case_lines, prefix, suffix, indent, known_true)
  2832.      struct attr_desc *attr;
  2833.      struct attr_value *av;
  2834.      int write_case_lines;
  2835.      char *prefix, *suffix;
  2836.      int indent;
  2837.      rtx known_true;
  2838. {
  2839.   struct insn_ent *ie;
  2840.  
  2841.   if (av->num_insns == 0)
  2842.     return;
  2843.  
  2844.   if (av->has_asm_insn)
  2845.     {
  2846.       write_indent (indent);
  2847.       printf ("case -1:\n");
  2848.       write_indent (indent + 2);
  2849.       printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
  2850.       write_indent (indent + 2);
  2851.       printf ("    && asm_noperands (PATTERN (insn)) < 0)\n");
  2852.       write_indent (indent + 2);
  2853.       printf ("  fatal_insn_not_found (insn);\n");
  2854.     }
  2855.  
  2856.   if (write_case_lines)
  2857.     {
  2858.       for (ie = av->first_insn; ie; ie = ie->next)
  2859.     if (ie->insn_code != -1)
  2860.       {
  2861.         write_indent (indent);
  2862.         printf ("case %d:\n", ie->insn_code);
  2863.       }
  2864.     }
  2865.   else
  2866.     {
  2867.       write_indent (indent);
  2868.       printf ("default:\n");
  2869.     }
  2870.  
  2871.   /* See what we have to do to handle output this value.  */
  2872.   must_extract = must_constrain = address_used = 0;
  2873.   walk_attr_value (av->value);
  2874.  
  2875.   if (must_extract)
  2876.     {
  2877.       write_indent (indent + 2);
  2878.       printf ("insn_extract (insn);\n");
  2879.     }
  2880.  
  2881.   if (must_constrain)
  2882.     {
  2883. #ifdef REGISTER_CONSTRAINTS
  2884.       write_indent (indent + 2);
  2885.       printf ("if (! constrain_operands (INSN_CODE (insn), reload_completed))\n");
  2886.       write_indent (indent + 2);
  2887.       printf ("  abort ();\n");
  2888. #endif
  2889.     }
  2890.  
  2891.   write_attr_set (attr, indent + 2, av->value, prefix, suffix,
  2892.           known_true, av->first_insn->insn_code,
  2893.           av->first_insn->insn_index);
  2894.  
  2895.   if (strncmp (prefix, "return", 6))
  2896.     {
  2897.       write_indent (indent + 2);
  2898.       printf ("break;\n");
  2899.     }
  2900.   printf ("\n");
  2901. }
  2902.  
  2903. /* Utilities to write names in various forms.  */
  2904.  
  2905. void
  2906. write_attr_valueq (attr, s)
  2907.      struct attr_desc *attr;
  2908.      char *s;
  2909. {
  2910.   if (attr->is_numeric)
  2911.     printf ("%s", s);
  2912.   else
  2913.     {
  2914.       write_upcase (attr->name);
  2915.       printf ("_");
  2916.       write_upcase (s);
  2917.     }
  2918. }
  2919.  
  2920. void
  2921. write_attr_value (attr, value)
  2922.      struct attr_desc *attr;
  2923.      rtx value;
  2924. {
  2925.   if (GET_CODE (value) != CONST_STRING)
  2926.     abort ();
  2927.  
  2928.   write_attr_valueq (attr, XSTR (value, 0));
  2929. }
  2930.  
  2931. void
  2932. write_upcase (str)
  2933.      char *str;
  2934. {
  2935.   while (*str)
  2936.     if (*str < 'a' || *str > 'z')
  2937.       printf ("%c", *str++);
  2938.     else
  2939.       printf ("%c", *str++ - 'a' + 'A');
  2940. }
  2941.  
  2942. void
  2943. write_indent (indent)
  2944.      int indent;
  2945. {
  2946.   for (; indent > 8; indent -= 8)
  2947.     printf ("\t");
  2948.  
  2949.   for (; indent; indent--)
  2950.     printf (" ");
  2951. }
  2952.  
  2953. /* Write a subroutine that is given an insn that requires a delay slot, a
  2954.    delay slot ordinal, and a candidate insn.  It returns non-zero if the
  2955.    candidate can be placed in the specified delay slot of the insn.
  2956.  
  2957.    We can write as many as three subroutines.  `eligible_for_delay'
  2958.    handles normal delay slots, `eligible_for_annul_true' indicates that
  2959.    the specified insn can be annulled if the branch is true, and likewise
  2960.    for `eligible_for_annul_false'.
  2961.  
  2962.    KIND is a string distingushing these three cases ("delay", "annul_true",
  2963.    or "annul_false").  */
  2964.  
  2965. void
  2966. write_eligible_delay (kind)
  2967.      char *kind;
  2968. {
  2969.   struct delay_desc *delay;
  2970.   int max_slots;
  2971.   char str[50];
  2972.   struct attr_desc *attr;
  2973.   struct attr_value *av, *common_av;
  2974.   int i;
  2975.  
  2976.   /* Compute the maximum number of delay slots required.  We use the delay
  2977.      ordinal times this number plus one, plus the slot number as an index into
  2978.      the appropriate predicate to test.  */
  2979.  
  2980.   for (delay = delays, max_slots = 0; delay; delay = delay->next)
  2981.     if (XVECLEN (delay->def, 1) / 3 > max_slots)
  2982.       max_slots = XVECLEN (delay->def, 1) / 3;
  2983.  
  2984.   /* Write function prelude.  */
  2985.  
  2986.   printf ("int\n");
  2987.   printf ("eligible_for_%s (delay_insn, slot, candidate_insn)\n", kind);
  2988.   printf ("     rtx delay_insn;\n");
  2989.   printf ("     int slot;\n");
  2990.   printf ("     rtx candidate_insn;\n");
  2991.   printf ("{\n");
  2992.   printf ("  rtx insn;\n");
  2993.   printf ("\n");
  2994.   printf ("  if (slot >= %d)\n", max_slots);
  2995.   printf ("    abort ();\n");
  2996.   printf ("\n");
  2997.  
  2998.   /* If more than one delay type, find out which type the delay insn is.  */
  2999.  
  3000.   if (num_delays > 1)
  3001.     {
  3002.       sprintf (str, "*delay_type", kind);
  3003.       attr = find_attr (str, 0);
  3004.       if (! attr) abort ();
  3005.       common_av = find_most_used (attr);
  3006.  
  3007.       printf ("  insn = delay_insn;\n");
  3008.       printf ("  switch (recog_memoized (insn))\n");
  3009.       printf ("    {\n");
  3010.  
  3011.       sprintf (str, " * %d;\n      break;", max_slots);
  3012.       for (av = attr->first_value; av; av = av->next)
  3013.     if (av != common_av)
  3014.       write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
  3015.  
  3016.       write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
  3017.       printf ("    }\n\n");
  3018.  
  3019.       /* Ensure matched.  Otherwise, shouldn't have been called.  */
  3020.       printf ("  if (slot < %d)\n", max_slots);
  3021.       printf ("    abort ();\n\n");
  3022.     }
  3023.  
  3024.   /* If just one type of delay slot, write simple switch.  */
  3025.   if (num_delays == 1 && max_slots == 1)
  3026.     {
  3027.       printf ("  insn = candidate_insn;\n");
  3028.       printf ("  switch (recog_memoized (insn))\n");
  3029.       printf ("    {\n");
  3030.  
  3031.       attr = find_attr ("*delay_1_0", 0);
  3032.       if (! attr) abort ();
  3033.       common_av = find_most_used (attr);
  3034.  
  3035.       for (av = attr->first_value; av; av = av->next)
  3036.     if (av != common_av)
  3037.       write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
  3038.  
  3039.       write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
  3040.       printf ("    }\n");
  3041.     }
  3042.  
  3043.   else
  3044.     {
  3045.       /* Write a nested CASE.  The first indicates which condition we need to
  3046.      test, and the inner CASE tests the condition.  */
  3047.       printf ("  insn = candidate_insn;\n");
  3048.       printf ("  switch (slot)\n");
  3049.       printf ("    {\n");
  3050.  
  3051.       for (delay = delays; delay; delay = delay->next)
  3052.     for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
  3053.       {
  3054.         printf ("    case %d:\n",
  3055.             (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
  3056.         printf ("      switch (recog_memoized (insn))\n");
  3057.         printf ("\t{\n");
  3058.  
  3059.         sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
  3060.         attr = find_attr (str, 0);
  3061.         if (! attr) abort ();
  3062.         common_av = find_most_used (attr);
  3063.  
  3064.         for (av = attr->first_value; av; av = av->next)
  3065.           if (av != common_av)
  3066.         write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
  3067.  
  3068.         write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
  3069.         printf ("      }\n");
  3070.       }
  3071.  
  3072.       printf ("    default:\n");
  3073.       printf ("      abort ();\n");     
  3074.       printf ("    }\n");
  3075.     }
  3076.  
  3077.   printf ("}\n\n");
  3078. }
  3079.  
  3080. /* Write routines to compute conflict cost for function units.  Then write a
  3081.    table describing the available function units.  */
  3082.  
  3083. void
  3084. write_function_unit_info ()
  3085. {
  3086.   struct function_unit *unit;
  3087.   struct attr_desc *case_attr, *attr;
  3088.   struct attr_value *av, *common_av;
  3089.   rtx value;
  3090.   char *str;
  3091.   int using_case;
  3092.   int i;
  3093.  
  3094.   /* Write out conflict routines for function units.  Don't bother writing
  3095.      one if there is only one busy value.  */
  3096.  
  3097.   for (unit = units; unit; unit = unit->next)
  3098.     {
  3099.       /* See if only one case exists and if there is a constant value for
  3100.      that case.  If so, we don't need a function.  */
  3101.       str = (char *) xmalloc (strlen (unit->name) + 10);
  3102.       sprintf (str, "*%s_cases", unit->name);
  3103.       attr = find_attr (str, 0);
  3104.       if (! attr) abort ();
  3105.       value = find_single_value (attr);
  3106.       if (value && GET_CODE (value) == CONST_STRING)
  3107.     {
  3108.       sprintf (str, "*%s_case_%s", unit->name, XSTR (value, 0));
  3109.       attr = find_attr (str, 0);
  3110.       if (! attr) abort ();
  3111.       value = find_single_value (attr);
  3112.       if (value && GET_CODE (value) == CONST_STRING)
  3113.         {
  3114.           unit->needs_conflict_function = 0;
  3115.           unit->default_cost = value;
  3116.           continue;
  3117.         }
  3118.     }
  3119.  
  3120.       /* The function first computes the case from the candidate insn.  */
  3121.       unit->needs_conflict_function = 1;
  3122.       unit->default_cost = make_numeric_value (0);
  3123.  
  3124.       printf ("static int\n");
  3125.       printf ("%s_unit_conflict_cost (executing_insn, candidate_insn)\n",
  3126.           unit->name);
  3127.       printf ("     rtx executing_insn;\n");
  3128.       printf ("     rtx candidate_insn;\n");
  3129.       printf ("{\n");
  3130.       printf ("  rtx insn;\n");
  3131.       printf ("  int casenum;\n\n");
  3132.       printf ("  insn = candidate_insn;\n");
  3133.       printf ("  switch (recog_memoized (insn))\n");
  3134.       printf ("    {\n");
  3135.  
  3136.       /* Write the `switch' statement to get the case value.  */
  3137.       sprintf (str, "*%s_cases", unit->name);
  3138.       case_attr = find_attr (str, 0);
  3139.       if (! case_attr) abort ();
  3140.       common_av = find_most_used (case_attr);
  3141.  
  3142.       for (av = case_attr->first_value; av; av = av->next)
  3143.     if (av != common_av)
  3144.       write_attr_case (case_attr, av, 1,
  3145.                "casenum =", ";", 4, unit->condexp);
  3146.  
  3147.       write_attr_case (case_attr, common_av, 0,
  3148.                "casenum =", ";", 4, unit->condexp);
  3149.       printf ("    }\n\n");
  3150.  
  3151.       /* Now write an outer switch statement on each case.  Then write
  3152.      the tests on the executing function within each.  */
  3153.       printf ("  insn = executing_insn;\n");
  3154.       printf ("  switch (casenum)\n");
  3155.       printf ("    {\n");
  3156.  
  3157.       for (i = 0; i < unit->num_opclasses; i++)
  3158.     {
  3159.       /* Ensure using this case.  */
  3160.       using_case = 0;
  3161.       for (av = case_attr->first_value; av; av = av->next)
  3162.         if (av->num_insns
  3163.         && contained_in_p (make_numeric_value (i), av->value))
  3164.           using_case = 1;
  3165.  
  3166.       if (! using_case)
  3167.         continue;
  3168.  
  3169.       printf ("    case %d:\n", i);
  3170.       sprintf (str, "*%s_case_%d", unit->name, i);
  3171.       attr = find_attr (str, 0);
  3172.       if (! attr) abort ();
  3173.  
  3174.       /* If single value, just write it.  */
  3175.       value = find_single_value (attr);
  3176.       if (value)
  3177.         write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2);
  3178.       else
  3179.         {
  3180.           common_av = find_most_used (attr);
  3181.           printf ("      switch (recog_memoized (insn))\n");
  3182.           printf ("\t{\n");
  3183.  
  3184.           for (av = attr->first_value; av; av = av->next)
  3185.         if (av != common_av)
  3186.           write_attr_case (attr, av, 1,
  3187.                    "return", ";", 8, unit->condexp);
  3188.  
  3189.           write_attr_case (attr, common_av, 0,
  3190.                    "return", ";", 8, unit->condexp);
  3191.           printf ("      }\n\n");
  3192.         }
  3193.     }
  3194.  
  3195.       printf ("    }\n}\n\n");
  3196.     }
  3197.  
  3198.   /* Now that all functions have been written, write the table describing
  3199.      the function units.   The name is included for documenation purposes
  3200.      only.  */
  3201.  
  3202.   printf ("struct function_unit_desc function_units[] = {\n");
  3203.  
  3204.   for (unit = units; unit; unit = unit->next)
  3205.     {
  3206.       printf ("  {\"%s\", %d, %d, %d, %s, %s_unit_ready_cost, ",
  3207.           unit->name, 1 << unit->num, unit->multiplicity,
  3208.           unit->simultaneity, XSTR (unit->default_cost, 0), unit->name);
  3209.  
  3210.       if (unit->needs_conflict_function)
  3211.     printf ("%s_unit_conflict_cost", unit->name);
  3212.       else
  3213.     printf ("0");
  3214.  
  3215.       printf ("}, \n");
  3216.     }
  3217.  
  3218.   printf ("};\n\n");
  3219. }
  3220.  
  3221. /* This page contains miscellaneous utility routines.  */
  3222.  
  3223. /* Given a string, return the number of comma-separated elements in it.
  3224.    Return 0 for the null string.  */
  3225.  
  3226. int
  3227. n_comma_elts (s)
  3228.      char *s;
  3229. {
  3230.   int n;
  3231.  
  3232.   if (*s == '\0')
  3233.     return 0;
  3234.  
  3235.   for (n = 1; *s; s++)
  3236.     if (*s == ',')
  3237.       n++;
  3238.  
  3239.   return n;
  3240. }
  3241.  
  3242. /* Given a pointer to a (char *), return a malloc'ed string containing the
  3243.    next comma-separated element.  Advance the pointer to after the string
  3244.    scanned, or the end-of-string.  Return NULL if at end of string.  */
  3245.  
  3246. char *
  3247. next_comma_elt (pstr)
  3248.      char **pstr;
  3249. {
  3250.   char *out_str;
  3251.   char *p;
  3252.  
  3253.   if (**pstr == '\0')
  3254.     return NULL;
  3255.  
  3256.   /* Find end of string to compute length.  */
  3257.   for (p = *pstr; *p != ',' && *p != '\0'; p++)
  3258.     ;
  3259.  
  3260.   out_str = (char *) xmalloc (p - *pstr + 1);
  3261.   for (p = out_str; **pstr != ',' && **pstr != '\0'; (*pstr)++)
  3262.     *p++ = **pstr;
  3263.  
  3264.   *p++ = '\0';
  3265.   if (**pstr == ',')
  3266.     (*pstr)++;
  3267.  
  3268.   return out_str;
  3269. }
  3270.  
  3271. /* Return a `struct attr_desc' pointer for a given named attribute.  If CREATE
  3272.    is non-zero, build a new attribute, if one does not exist.  */
  3273.  
  3274. struct attr_desc *
  3275. find_attr (name, create)
  3276.      char *name;
  3277.      int create;
  3278. {
  3279.   struct attr_desc *attr;
  3280.   char *new_name;
  3281.  
  3282.   /* Before we resort to using `strcmp', see if the string address matches
  3283.      anywhere.  In most cases, it should have been canonicalized to do so.  */
  3284.   if (name == alternative_name)
  3285.     return NULL;
  3286.  
  3287.   for (attr = attrs; attr; attr = attr->next)
  3288.     if (name == attr->name)
  3289.       return attr;
  3290.  
  3291.   /* Otherwise, do it the slow way.  */
  3292.   for (attr = attrs; attr; attr = attr->next)
  3293.     if (! strcmp (name, attr->name))
  3294.       return attr;
  3295.  
  3296.   if (! create)
  3297.     return NULL;
  3298.  
  3299.   new_name = (char *) xmalloc (strlen (name) + 1);
  3300.   strcpy (new_name, name);
  3301.  
  3302.   attr = (struct attr_desc *) xmalloc (sizeof (struct attr_desc));
  3303.   attr->name = new_name;
  3304.   attr->first_value = attr->default_val = NULL;
  3305.   attr->is_numeric = attr->is_special = 0;
  3306.   attr->next = attrs;
  3307.   attrs = attr;
  3308.  
  3309.   return attr;
  3310. }
  3311.  
  3312. /* Create internal attribute with the given default value.  */
  3313.  
  3314. void
  3315. make_internal_attr (name, value, special)
  3316.      char *name;
  3317.      rtx value;
  3318.      int special;
  3319. {
  3320.   struct attr_desc *attr;
  3321.  
  3322.   attr = find_attr (name, 1);
  3323.   if (attr->default_val)
  3324.     abort ();
  3325.  
  3326.   attr->is_numeric = 1;
  3327.   attr->is_special = special;
  3328.   attr->default_val = get_attr_value (value, attr, -2);
  3329. }
  3330.  
  3331. /* Find the most used value of an attribute.  */
  3332.  
  3333. struct attr_value *
  3334. find_most_used (attr)
  3335.      struct attr_desc *attr;
  3336. {
  3337.   struct attr_value *av;
  3338.   struct attr_value *most_used;
  3339.   int nuses;
  3340.  
  3341.   most_used = NULL;
  3342.   nuses = -1;
  3343.  
  3344.   for (av = attr->first_value; av; av = av->next)
  3345.     if (av->num_insns > nuses)
  3346.       nuses = av->num_insns, most_used = av;
  3347.  
  3348.   return most_used;
  3349. }
  3350.  
  3351. /* If an attribute only has a single value used, return it.  Otherwise
  3352.    return NULL.  */
  3353.  
  3354. rtx
  3355. find_single_value (attr)
  3356.      struct attr_desc *attr;
  3357. {
  3358.   struct attr_value *av;
  3359.   rtx unique_value;
  3360.  
  3361.   unique_value = NULL;
  3362.   for (av = attr->first_value; av; av = av->next)
  3363.     if (av->num_insns)
  3364.       {
  3365.     if (unique_value)
  3366.       return NULL;
  3367.     else
  3368.       unique_value = av->value;
  3369.       }
  3370.  
  3371.   return unique_value;
  3372. }
  3373.  
  3374. /* Return (attr_value "n") */
  3375.  
  3376. rtx
  3377. make_numeric_value (n)
  3378.      int n;
  3379. {
  3380.   static rtx int_values[20];
  3381.   rtx exp;
  3382.  
  3383.   if (n < 0)
  3384.     abort ();
  3385.  
  3386.   if (n < 20 && int_values[n])
  3387.     return int_values[n];
  3388.  
  3389.   exp = rtx_alloc (CONST_STRING);
  3390.   XSTR (exp, 0) = (char *) xmalloc ((n < 1000 ? 4
  3391.                      : HOST_BITS_PER_INT * 3 / 10 + 3));
  3392.   sprintf (XSTR (exp, 0), "%d", n);
  3393.  
  3394.   if (n < 20)
  3395.     int_values[n] = exp;
  3396.  
  3397.   return exp;
  3398. }
  3399.  
  3400. int
  3401. xrealloc (ptr, size)
  3402.      char *ptr;
  3403.      int size;
  3404. {
  3405.   int result = realloc (ptr, size);
  3406.   if (!result)
  3407.     fatal ("virtual memory exhausted");
  3408.   return result;
  3409. }
  3410.  
  3411. int
  3412. xmalloc (size)
  3413. {
  3414.   register int val = malloc (size);
  3415.  
  3416.   if (val == 0)
  3417.     fatal ("virtual memory exhausted");
  3418.   return val;
  3419. }
  3420.  
  3421. void
  3422. fatal (s, a1, a2)
  3423.      char *s;
  3424. {
  3425.   fprintf (stderr, "genattrtab: ");
  3426.   fprintf (stderr, s, a1, a2);
  3427.   fprintf (stderr, "\n");
  3428.   exit (FATAL_EXIT_CODE);
  3429. }
  3430.  
  3431. /* More 'friendly' abort that prints the line and file.
  3432.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  3433.  
  3434. void
  3435. fancy_abort ()
  3436. {
  3437.   fatal ("Internal gcc abort.");
  3438. }
  3439.  
  3440. int
  3441. main (argc, argv)
  3442.      int argc;
  3443.      char **argv;
  3444. {
  3445.   rtx desc;
  3446.   FILE *infile;
  3447.   extern rtx read_rtx ();
  3448.   register int c;
  3449.   struct attr_desc *attr;
  3450.   struct attr_value *av;
  3451.   struct insn_def *id;
  3452.   rtx tem;
  3453.  
  3454.   obstack_init (rtl_obstack);
  3455.  
  3456.   if (argc <= 1)
  3457.     fatal ("No input file name.");
  3458.  
  3459.   infile = fopen (argv[1], "r");
  3460.   if (infile == 0)
  3461.     {
  3462.       perror (argv[1]);
  3463.       exit (FATAL_EXIT_CODE);
  3464.     }
  3465.  
  3466.   init_rtl ();
  3467.  
  3468.   /* Set up true and false rtx's */
  3469.   true_rtx = rtx_alloc (CONST_INT);
  3470.   false_rtx = rtx_alloc (CONST_INT);
  3471.   XINT (true_rtx, 0) = 1;
  3472.   XINT (false_rtx, 0) = 0;
  3473.   RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
  3474.  
  3475.   printf ("/* Generated automatically by the program `genattrtab'\n\
  3476. from the machine description file `md'.  */\n\n");
  3477.  
  3478.   /* Read the machine description.  */
  3479.  
  3480.   while (1)
  3481.     {
  3482.       c = read_skip_spaces (infile);
  3483.       if (c == EOF)
  3484.     break;
  3485.       ungetc (c, infile);
  3486.  
  3487.       desc = read_rtx (infile);
  3488.       if (GET_CODE (desc) == DEFINE_INSN
  3489.       || GET_CODE (desc) == DEFINE_PEEPHOLE
  3490.       || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
  3491.     gen_insn (desc);
  3492.  
  3493.       else if (GET_CODE (desc) == DEFINE_EXPAND)
  3494.     insn_code_number++, insn_index_number++;
  3495.  
  3496.       else if (GET_CODE (desc) == DEFINE_SPLIT)
  3497.     insn_code_number++, insn_index_number++;
  3498.  
  3499.       else if (GET_CODE (desc) == DEFINE_ATTR)
  3500.     {
  3501.       gen_attr (desc);
  3502.       insn_index_number++;
  3503.     }
  3504.  
  3505.       else if (GET_CODE (desc) == DEFINE_DELAY)
  3506.     {
  3507.       gen_delay (desc);
  3508.       insn_index_number++;
  3509.     }
  3510.  
  3511.       else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
  3512.     {
  3513.       gen_unit (desc);
  3514.       insn_index_number++;
  3515.     }
  3516.     }
  3517.  
  3518.   /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one.  */
  3519.   if (! got_define_asm_attributes)
  3520.     {
  3521.       tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
  3522.       XVEC (tem, 0) = rtvec_alloc (0);
  3523.       gen_insn (tem);
  3524.     }
  3525.  
  3526.   /* Expand DEFINE_DELAY information into new attribute.  */
  3527.   if (num_delays)
  3528.     expand_delays ();
  3529.  
  3530.   /* Expand DEFINE_FUNCTION_UNIT information into new attributes.  */
  3531.   if (num_units)
  3532.     expand_units ();
  3533.  
  3534.   printf ("#include \"config.h\"\n");
  3535.   printf ("#include \"rtl.h\"\n");
  3536.   printf ("#include \"insn-config.h\"\n");
  3537.   printf ("#include \"recog.h\"\n");
  3538.   printf ("#include \"regs.h\"\n");
  3539.   printf ("#include \"real.h\"\n");
  3540.   printf ("#include \"output.h\"\n");
  3541.   printf ("#include \"insn-attr.h\"\n");
  3542.   printf ("\n");  
  3543.   printf ("#define operands recog_operand\n\n");
  3544.  
  3545.   /* Make `insn_alternatives'.  */
  3546.   insn_alternatives = (int *) xmalloc (insn_code_number * sizeof (int));
  3547.   for (id = defs; id; id = id->next)
  3548.     insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
  3549.  
  3550.   /* Prepare to write out attribute subroutines by checking everything stored
  3551.      away and building the attribute cases.  */
  3552.  
  3553.   check_defs ();
  3554.   for (attr = attrs; attr; attr = attr->next)
  3555.     {
  3556.       check_attr_value (attr->default_val->value, attr);
  3557.       fill_attr (attr);
  3558.     }
  3559.  
  3560.   /* Construct extra attributes for `length'.  */
  3561.   make_length_attrs ();
  3562.  
  3563.   /* Perform any possible optimizations to speed up compilation. */
  3564.   optimize_attrs ();
  3565.  
  3566.   /* Now write out all the `gen_attr_...' routines.  Do these before the
  3567.      special routines (specifically before write_function_unit_info), so
  3568.      that they get defined before they are used.  */
  3569.  
  3570.   for (attr = attrs; attr; attr = attr->next)
  3571.     {
  3572.       if (! attr->is_special)
  3573.     write_attr_get (attr);
  3574.     }
  3575.  
  3576.   /* Write out delay eligibility information, if DEFINE_DELAY present.
  3577.      (The function to compute the number of delay slots will be written
  3578.      below.)  */
  3579.   if (num_delays)
  3580.     {
  3581.       write_eligible_delay ("delay");
  3582.       if (have_annul_true)
  3583.     write_eligible_delay ("annul_true");
  3584.       if (have_annul_false)
  3585.     write_eligible_delay ("annul_false");
  3586.     }
  3587.  
  3588.   /* Write out information about function units.  */
  3589.   if (num_units)
  3590.     write_function_unit_info ();
  3591.  
  3592.   fflush (stdout);
  3593.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  3594. }
  3595.