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

  1. /* Generate code from machine description to recognize rtl as insns.
  2.    Copyright (C) 1987-1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This program is used to produce insn-recog.c, which contains
  22.    a function called `recog' plus its subroutines.
  23.    These functions contain a decision tree
  24.    that recognizes whether an rtx, the argument given to recog,
  25.    is a valid instruction.
  26.  
  27.    recog returns -1 if the rtx is not valid.
  28.    If the rtx is valid, recog returns a nonnegative number
  29.    which is the insn code number for the pattern that matched.
  30.    This is the same as the order in the machine description of the
  31.    entry that matched.  This number can be used as an index into various
  32.    insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands
  33.    (found in insn-output.c).
  34.  
  35.    The third argument to recog is an optional pointer to an int.
  36.    If present, recog will accept a pattern if it matches except for
  37.    missing CLOBBER expressions at the end.  In that case, the value
  38.    pointed to by the optional pointer will be set to the number of
  39.    CLOBBERs that need to be added (it should be initialized to zero by
  40.    the caller).  If it is set nonzero, the caller should allocate a
  41.    PARALLEL of the appropriate size, copy the initial entries, and call
  42.    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
  43.  
  44.    This program also generates the function `split_insns',
  45.    which returns 0 if the rtl could not be split, or
  46.    it returns the split rtl in a SEQUENCE.  */
  47.  
  48. #include <stdio.h>
  49. #include "config.h"
  50. #include "rtl.h"
  51. #include "obstack.h"
  52.  
  53. struct obstack obstack;
  54. struct obstack *rtl_obstack = &obstack;
  55.  
  56. #define obstack_chunk_alloc xmalloc
  57. #define obstack_chunk_free free
  58. extern int xmalloc ();
  59. extern void free ();
  60.  
  61. /* Data structure for decision tree for recognizing
  62.    legitimate instructions.  */
  63.  
  64. struct decision
  65. {
  66.   int number;
  67.   char *position;
  68.   RTX_CODE code;
  69.   char *exact;
  70.   enum machine_mode mode;
  71.   char *tests;
  72.   int insn_code_number;
  73.   int num_clobbers_to_add;
  74.   struct decision *next;
  75.   struct decision *success;
  76.   int opno;
  77.   int dupno;
  78.   int dupcount;
  79.   int test_elt_zero_int;
  80.   int elt_zero_int;
  81.   int test_elt_one_int;
  82.   int elt_one_int;
  83.   int ignmode;
  84.   struct decision *afterward;
  85.   int label_needed;
  86.   char *c_test;
  87.   char *reg_class;
  88.   char enforce_mode;
  89.   int veclen;
  90.   int subroutine_number;
  91.   /* Used for DEFINE_SPLITs.  */
  92.   char *c_hook;
  93.   rtx split_sequence;
  94. };
  95.  
  96. #define SUBROUTINE_THRESHOLD 50
  97.  
  98. int next_subroutine_number;
  99.  
  100. /* We can write two types of subroutines: One for insn recognition and
  101.    one to split insns.  This defines which type is being written.  */
  102.  
  103. enum routine_type {RECOG, SPLIT};
  104.  
  105. /*
  106. recognize (top)
  107. {
  108.  staten:
  109.   x = XVECEXP (top, 0, 3);
  110.   if (test_code (GET_CODE (x))
  111.       && test_mode (MODE (x))
  112.       && whatever_else)
  113.     goto statep;
  114.   else if (next one...)
  115.     goto statem:
  116.   goto stater;
  117.  
  118.  statep:
  119.   actions...;
  120.   return 1;
  121.  
  122.  statem:
  123.   x = stack[depth--];
  124.   more tests...;
  125.  
  126.  stateq:
  127.   stack[++depth] = x;
  128.   x = XEXP (stack[depth], 0);
  129.   more tests...;
  130.  
  131.  stater:
  132.   x = XEXP (stack[depth], 1);
  133. }
  134.  
  135. */
  136.  
  137. int next_number;
  138.  
  139. int next_insn_code;
  140.  
  141. int next_index;
  142.  
  143. /* Number of MATCH_DUP's seen so far in this instruction.  */
  144. int dupcount;
  145.  
  146. struct decision *add_to_sequence ();
  147. struct decision *merge_trees ();
  148. struct decision *try_merge_2 ();
  149. void write_subroutine ();
  150. void print_code ();
  151. void clear_codes ();
  152. void clear_modes ();
  153. void change_state ();
  154. void write_tree ();
  155. char *copystr ();
  156. char *concat ();
  157. void fatal ();
  158. void fancy_abort ();
  159. void mybzero ();
  160.  
  161. struct decision *first;
  162.  
  163. /* Construct and return a sequence of decisions
  164.    that will recognize INSN.  */
  165.  
  166. struct decision *
  167. make_insn_sequence (insn)
  168.      rtx insn;
  169. {
  170.   rtx x;
  171.   char *c_test = XSTR (insn, 2);
  172.   struct decision *last;
  173.  
  174.   dupcount = 0;
  175.  
  176.   if (XVECLEN (insn, 1) == 1)
  177.     x = XVECEXP (insn, 1, 0);
  178.   else
  179.     {
  180.       x = rtx_alloc (PARALLEL);
  181.       XVEC (x, 0) = XVEC (insn, 1);
  182.       PUT_MODE (x, VOIDmode);
  183.     }
  184.  
  185.   last = add_to_sequence (x, 0, "");
  186.  
  187.   if (c_test[0])
  188.     last->c_test = c_test;
  189.   last->insn_code_number = next_insn_code;
  190.   last->num_clobbers_to_add = 0;
  191.  
  192.   /* If X is a PARALLEL, see if it ends with a group of CLOBBERs of (hard)
  193.      registers or MATCH_SCRATCHes.  If so, set up to recognize the pattern
  194.      without these CLOBBERs.  */
  195.  
  196.   if (GET_CODE (x) == PARALLEL)
  197.     {
  198.       int i;
  199.  
  200.       for (i = XVECLEN (x, 0); i > 0; i--)
  201.     if (GET_CODE (XVECEXP (x, 0, i - 1)) != CLOBBER
  202.         || (GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != REG
  203.         && GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != MATCH_SCRATCH))
  204.       break;
  205.  
  206.       if (i != XVECLEN (x, 0))
  207.     {
  208.       rtx new;
  209.       struct decision *previous_first = first;
  210.  
  211.       if (i == 1)
  212.         new = XVECEXP (x, 0, 0);
  213.       else
  214.         {
  215.           int j;
  216.  
  217.           new = rtx_alloc (PARALLEL);
  218.           XVEC (new, 0) = rtvec_alloc (i);
  219.           for (j = i - 1; j >= 0; j--)
  220.         XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
  221.         }
  222.  
  223.       last = add_to_sequence (new, 0, "");
  224.  
  225.       if (c_test[0])
  226.         last->c_test = c_test;
  227.       last->insn_code_number = next_insn_code;
  228.       last->num_clobbers_to_add = XVECLEN (x, 0) - i;
  229.       first = merge_trees (previous_first, first);
  230.     }
  231.     }
  232.  
  233.   next_insn_code++;
  234.   return first;
  235. }
  236.  
  237. struct decision *
  238. make_split_sequence (insn)
  239.      rtx insn;
  240. {
  241.   rtx x;
  242.   char *c_test = XSTR (insn, 1);
  243.   char *c_hook = XSTR (insn, 3);
  244.   struct decision *last;
  245.  
  246.   dupcount = 0;
  247.  
  248.   if (XVECLEN (insn, 0) == 1)
  249.     x = XVECEXP (insn, 0, 0);
  250.   else
  251.     {
  252.       x = rtx_alloc (PARALLEL);
  253.       XVEC (x, 0) = XVEC (insn, 0);
  254.       PUT_MODE (x, VOIDmode);
  255.     }
  256.  
  257.   last = add_to_sequence (x, 0, "");
  258.  
  259.   if (c_test[0])
  260.     last->c_test = c_test;
  261.   if (c_hook != 0 && c_hook[0] != 0)
  262.     last->c_hook = c_hook;
  263.   last->split_sequence = XEXP (insn, 2);
  264.   last->insn_code_number = next_insn_code++;
  265.  
  266.   /* Define the subroutine we will call below and emit in genemit.  */
  267.   printf ("extern rtx gen_split_%d ();\n", last->insn_code_number);
  268.  
  269.   return first;
  270. }
  271.  
  272. struct decision *
  273. add_to_sequence (pattern, last, position)
  274.      rtx pattern;
  275.      struct decision *last;
  276.      char *position;
  277. {
  278.   register RTX_CODE code;
  279.   register struct decision *new
  280.     = (struct decision *) xmalloc (sizeof (struct decision));
  281.   struct decision *this;
  282.   char *newpos;
  283.   register char *fmt;
  284.   register int i;
  285.   int depth;
  286.   int len;
  287.  
  288.   new->number = next_number++;
  289.   new->position = copystr (position);
  290.   new->exact = 0;
  291.   new->next = 0;
  292.   new->success = 0;
  293.   new->insn_code_number = -1;
  294.   new->num_clobbers_to_add = 0;
  295.   new->tests = 0;
  296.   new->opno = -1;
  297.   new->dupno = -1;
  298.   new->dupcount = -1;
  299.   new->test_elt_zero_int = 0;
  300.   new->test_elt_one_int = 0;
  301.   new->elt_zero_int = 0;
  302.   new->elt_one_int = 0;
  303.   new->enforce_mode = 0;
  304.   new->ignmode = 0;
  305.   new->afterward = 0;
  306.   new->label_needed = 0;
  307.   new->c_test = 0;
  308.   new->c_hook = 0;
  309.   new->split_sequence = 0;
  310.   new->reg_class = 0;
  311.   new->veclen = 0;
  312.   new->subroutine_number = 0;
  313.  
  314.   this = new;
  315.  
  316.   if (last == 0)
  317.     first = new;
  318.   else
  319.     last->success = new;
  320.  
  321.   depth = strlen (position);
  322.   newpos = (char *) alloca (depth + 2);
  323.   strcpy (newpos, position);
  324.   newpos[depth + 1] = 0;
  325.  
  326.  restart:
  327.  
  328.   if (pattern == 0)
  329.     {
  330.       new->exact = "0";
  331.       new->code = UNKNOWN;
  332.       new->mode = VOIDmode;
  333.       return new;
  334.     }
  335.  
  336.   new->mode = GET_MODE (pattern);
  337.   new->code = code = GET_CODE (pattern);
  338.  
  339.   switch (code)
  340.     {
  341.     case MATCH_OPERAND:
  342.       new->opno = XINT (pattern, 0);
  343.       new->code = UNKNOWN;
  344.       new->tests = XSTR (pattern, 1);
  345.       if (*new->tests == 0)
  346.     new->tests = 0;
  347.       new->reg_class = XSTR (pattern, 2);
  348.       if (*new->reg_class == 0)
  349.     new->reg_class = 0;
  350.       return new;
  351.  
  352.     case MATCH_SCRATCH:
  353.       new->opno = XINT (pattern, 0);
  354.       new->code = UNKNOWN;
  355.       new->tests = "scratch_operand";
  356.       if (*new->tests == 0)
  357.     new->tests = 0;
  358.       new->reg_class = XSTR (pattern, 1);
  359.       if (*new->reg_class == 0)
  360.     new->reg_class = 0;
  361.       return new;
  362.  
  363.     case MATCH_OPERATOR:
  364.       new->opno = XINT (pattern, 0);
  365.       new->code = UNKNOWN;
  366.       new->tests = XSTR (pattern, 1);
  367.       if (*new->tests == 0)
  368.     new->tests = 0;
  369.       for (i = 0; i < XVECLEN (pattern, 2); i++)
  370.     {
  371.       newpos[depth] = i + '0';
  372.       new = add_to_sequence (XVECEXP (pattern, 2, i), new, newpos);
  373.     }
  374.       this->success->enforce_mode = 0;
  375.       return new;
  376.  
  377.     case MATCH_PARALLEL:
  378.       new->opno = XINT (pattern, 0);
  379.       new->code = PARALLEL;
  380.       new->tests = XSTR (pattern, 1);
  381.       if (*new->tests == 0)
  382.     new->tests = 0;
  383.       for (i = 0; i < XVECLEN (pattern, 2); i++)
  384.     {
  385.       newpos[depth] = i + 'a';
  386.       new = add_to_sequence (XVECEXP (pattern, 2, i), new, newpos);
  387.     }
  388.       this->success->enforce_mode = 0;
  389.       return new;
  390.  
  391.     case MATCH_OP_DUP:
  392.       new->opno = XINT (pattern, 0);
  393.       new->dupno = XINT (pattern, 0);
  394.       new->dupcount = dupcount++;
  395.       new->code = UNKNOWN;
  396.       new->tests = 0;
  397.       for (i = 0; i < XVECLEN (pattern, 1); i++)
  398.     {
  399.       newpos[depth] = i + '0';
  400.       new = add_to_sequence (XVECEXP (pattern, 1, i), new, newpos);
  401.     }
  402.       this->success->enforce_mode = 0;
  403.       return new;
  404.  
  405.     case MATCH_DUP:
  406.       new->dupno = XINT (pattern, 0);
  407.       new->dupcount = dupcount++;
  408.       new->code = UNKNOWN;
  409.       return new;
  410.  
  411.     case ADDRESS:
  412.       pattern = XEXP (pattern, 0);
  413.       goto restart;
  414.  
  415.     case PC:
  416.       new->exact = "pc_rtx";
  417.       return new;
  418.  
  419.     case CC0:
  420.       new->exact = "cc0_rtx";
  421.       return new;
  422.  
  423.     case CONST_INT:
  424.       if (INTVAL (pattern) == 0)
  425.     {
  426.       new->exact = "const0_rtx";
  427.       return new;
  428.     }
  429.       if (INTVAL (pattern) == 1)
  430.     {
  431.       new->exact = "const1_rtx";
  432.       return new;
  433.     }
  434.       if (INTVAL (pattern) == -1)
  435.     {
  436.       new->exact = "constm1_rtx";
  437.       return new;
  438.     }
  439.       if (INTVAL (pattern) == STORE_FLAG_VALUE)
  440.     {
  441.       new->exact = "const_true_rtx";
  442.       return new;
  443.     }
  444.       break;
  445.  
  446.     case SET:
  447.       newpos[depth] = '0';
  448.       new = add_to_sequence (SET_DEST (pattern), new, newpos);
  449.       this->success->enforce_mode = 1;
  450.       newpos[depth] = '1';
  451.       new = add_to_sequence (SET_SRC (pattern), new, newpos);
  452.       return new;
  453.  
  454.     case STRICT_LOW_PART:
  455.       newpos[depth] = '0';
  456.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  457.       this->success->enforce_mode = 1;
  458.       return new;
  459.  
  460.     case SUBREG:
  461.       this->test_elt_one_int = 1;
  462.       this->elt_one_int = XINT (pattern, 1);
  463.       newpos[depth] = '0';
  464.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  465.       this->success->enforce_mode = 1;
  466.       return new;
  467.  
  468.     case ZERO_EXTRACT:
  469.     case SIGN_EXTRACT:
  470.       newpos[depth] = '0';
  471.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  472.       this->success->enforce_mode = 1;
  473.       newpos[depth] = '1';
  474.       new = add_to_sequence (XEXP (pattern, 1), new, newpos);
  475.       newpos[depth] = '2';
  476.       new = add_to_sequence (XEXP (pattern, 2), new, newpos);
  477.       return new;
  478.     }
  479.  
  480.   fmt = GET_RTX_FORMAT (code);
  481.   len = GET_RTX_LENGTH (code);
  482.   for (i = 0; i < len; i++)
  483.     {
  484.       newpos[depth] = '0' + i;
  485.       if (fmt[i] == 'e' || fmt[i] == 'u')
  486.     new = add_to_sequence (XEXP (pattern, i), new, newpos);
  487.       else if (fmt[i] == 'i' && i == 0)
  488.     {
  489.       this->test_elt_zero_int = 1;
  490.       this->elt_zero_int = XINT (pattern, i);
  491.     }
  492.       else if (fmt[i] == 'i' && i == 1)
  493.     {
  494.       this->test_elt_one_int = 1;
  495.       this->elt_one_int = XINT (pattern, i);
  496.     }
  497.       else if (fmt[i] == 'E')
  498.     {
  499.       register int j;
  500.       /* We do not handle a vector appearing as other than
  501.          the first item, just because nothing uses them
  502.          and by handling only the special case
  503.          we can use one element in newpos for either
  504.          the item number of a subexpression
  505.          or the element number in a vector.  */
  506.       if (i != 0)
  507.         abort ();
  508.       this->veclen = XVECLEN (pattern, i);
  509.       for (j = 0; j < XVECLEN (pattern, i); j++)
  510.         {
  511.           newpos[depth] = 'a' + j;
  512.           new = add_to_sequence (XVECEXP (pattern, i, j),
  513.                      new, newpos);
  514.         }
  515.     }
  516.       else if (fmt[i] != '0')
  517.     abort ();
  518.     }
  519.   return new;
  520. }
  521.  
  522. /* Merge two decision trees OLD and ADD,
  523.    modifying OLD destructively,
  524.    and return the merged tree.  */
  525.  
  526. struct decision *
  527. merge_trees (old, add)
  528.      register struct decision *old, *add;
  529. {
  530.   while (add)
  531.     {
  532.       register struct decision *next = add->next;
  533.       add->next = 0;
  534.       if (!try_merge_1 (old, add))
  535.     old = try_merge_2 (old, add);
  536.       add = next;
  537.     }
  538.   return old;
  539. }
  540.  
  541. /* Merge ADD into the next-chain starting with OLD
  542.    only if it overlaps a condition already tested in OLD.
  543.    Returns 1 if successful (OLD is modified),
  544.    0 if nothing has been done.  */
  545.  
  546. int
  547. try_merge_1 (old, add)
  548.      register struct decision *old, *add;
  549. {
  550.   while (old)
  551.     {
  552.       if ((old->position == add->position
  553.        || (old->position && add->position
  554.            && !strcmp (old->position, add->position)))
  555.       && (old->tests == add->tests
  556.           || (old->tests && add->tests && !strcmp (old->tests, add->tests)))
  557.       && (old->c_test == add->c_test
  558.           || (old->c_test && add->c_test && !strcmp (old->c_test, add->c_test)))
  559.       && (old->c_hook == add->c_hook
  560.           || (old->c_hook && add->c_hook && !strcmp (old->c_hook, add->c_hook)))
  561.       && old->test_elt_zero_int == add->test_elt_zero_int
  562.       && old->elt_zero_int == add->elt_zero_int
  563.       && old->test_elt_one_int == add->test_elt_one_int
  564.       && old->elt_one_int == add->elt_one_int
  565.       && old->veclen == add->veclen
  566.       && old->dupno == add->dupno
  567.       && old->opno == add->opno
  568.       && (old->tests == 0
  569.           || (add->enforce_mode ? no_same_mode (old) : old->next == 0))
  570.       && old->code == add->code
  571.       && old->mode == add->mode
  572.       && (old->exact == add->exact
  573.           || (old->exact && add->exact && ! strcmp (old->exact, add->exact))))
  574.     {
  575.       old->success = merge_trees (old->success, add->success);
  576.       if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
  577.         fatal ("Two actions at one point in tree");
  578.       if (old->insn_code_number == -1)
  579.         old->insn_code_number = add->insn_code_number;
  580.       return 1;
  581.     }
  582.       old = old->next;
  583.     }
  584.   return 0;
  585. }
  586.  
  587. /* Merge ADD into the next-chain that starts with OLD,
  588.    preferably after something that tests the same place
  589.    that ADD does.
  590.    The next-chain of ADD itself is ignored, and it is set
  591.    up for entering ADD into the new chain.
  592.    Returns the new chain.  */
  593.  
  594. struct decision *
  595. try_merge_2 (old, add)
  596.      struct decision *old, *add;
  597. {
  598.   register struct decision *p;
  599.   struct decision *last = 0;
  600.   struct decision *last_same_place = 0;
  601.  
  602.   /* Put this in after the others that test the same place,
  603.      if there are any.  If not, find the last chain element
  604.      and insert there.
  605.  
  606.      One modification: if this one is NOT a MATCH_OPERAND,
  607.      put it before any MATCH_OPERANDS that test the same place.
  608.  
  609.      Another: if enforce_mode (i.e. this is first operand of a SET),
  610.      put this after the last thing that tests the same place for
  611.      the same mode.  */
  612.  
  613. #if 0
  614.   int operand = 0 != add->tests;
  615. #endif
  616.  
  617.   for (p = old; p; p = p->next)
  618.     {
  619.       if (p->position == add->position
  620.       || (p->position && add->position
  621.           && !strcmp (p->position, add->position)))
  622.     {
  623.       last_same_place = p;
  624.       /* If enforce_mode, segregate the modes in numerical order.  */
  625.       if (p->enforce_mode && (int) add->mode < (int) p->mode)
  626.         break;
  627. #if 0
  628.       /* Keep explicit decompositions before those that test predicates.
  629.          If enforce_mode, do this separately within each mode.  */
  630.       if (! p->enforce_mode || p->mode == add->mode)
  631.         if (!operand && p->tests)
  632.           break;
  633. #endif
  634.     }
  635.       /* If this is past the end of the decisions at the same place as ADD,
  636.      stop looking now; add ADD before here.  */
  637.       else if (last_same_place)
  638.     break;
  639.       last = p;
  640.     }
  641.  
  642.   /* Insert before P, which means after LAST.  */
  643.  
  644.   if (last)
  645.     {
  646.       add->next = last->next;
  647.       last->next = add;
  648.       return old;
  649.     }
  650.  
  651.   add->next = old;
  652.   return add;
  653. }
  654.  
  655. int
  656. no_same_mode (node)
  657.      struct decision *node;
  658. {
  659.   register struct decision *p;
  660.   register enum machine_mode mode = node->mode;
  661.  
  662.   for (p = node->next; p; p = p->next)
  663.     if (p->mode == mode)
  664.       return 0;
  665.  
  666.   return 1;
  667. }
  668.  
  669. /* Count the number of subnodes of node NODE, assumed to be the start
  670.    of a next-chain.  If the number is high enough, make NODE start
  671.    a separate subroutine in the C code that is generated.
  672.  
  673.    TYPE gives the type of routine we are writing.  */
  674.  
  675. int
  676. break_out_subroutines (node, type)
  677.      struct decision *node;
  678.      enum routine_type type;
  679. {
  680.   int size = 0;
  681.   struct decision *sub;
  682.   for (sub = node; sub; sub = sub->next)
  683.     size += 1 + break_out_subroutines (sub->success, type);
  684.   if (size > SUBROUTINE_THRESHOLD)
  685.     {
  686.       node->subroutine_number = ++next_subroutine_number;
  687.       write_subroutine (node, type);
  688.       size = 1;
  689.     }
  690.   return size;
  691. }
  692.  
  693. void
  694. write_subroutine (tree, type)
  695.      struct decision *tree;
  696.      enum routine_type type;
  697. {
  698.   char *return_type = (type == SPLIT ? "rtx" : "int");
  699.  
  700.   if (type == SPLIT)
  701.     {
  702.       printf ("rtx\nsplit_%d (x0, insn)\n", tree->subroutine_number);
  703.       printf ("     register rtx x0;\n     rtx insn;\n");
  704.     }
  705.   else
  706.     {
  707.       printf ("int\nrecog_%d (x0, insn, pnum_clobbers)\n",
  708.           tree->subroutine_number);
  709.       printf ("     register rtx x0;\n     rtx insn;\n");
  710.       printf ("     int *pnum_clobbers;\n");
  711.     }
  712.  
  713.   printf ("{\n");
  714.   printf ("  register rtx *ro = &recog_operand[0];\n");
  715.   printf ("  register rtx x1, x2, x3, x4, x5;\n  rtx x6, x7, x8, x9, x10, x11;\n");
  716.   printf ("  %s tem;\n", return_type);
  717.   write_tree (tree, "", 0, "", 1, type);
  718.   printf (" ret0: return %d;\n}\n\n", type == SPLIT ? 0 : -1);
  719. }
  720.  
  721. /* Write out C code to perform the decisions in the tree.  */
  722.  
  723. char *
  724. write_tree_1 (tree, prevpos, afterward, afterpos, initial, type)
  725.      struct decision *tree;
  726.      char *prevpos;
  727.      int afterward;
  728.      char *afterpos;
  729.      int initial;
  730.      enum routine_type type;
  731. {
  732.   register struct decision *p, *p1;
  733.   char *pos;
  734.   register int depth;
  735.   int ignmode;
  736.   enum anon1 { NO_SWITCH, CODE_SWITCH, MODE_SWITCH } in_switch = NO_SWITCH;
  737.   char modemap[NUM_MACHINE_MODES];
  738.   char codemap[NUM_RTX_CODE];
  739.  
  740.   pos = prevpos;
  741.  
  742.   tree->label_needed = 1;
  743.   for (p = tree; p; p = p->next)
  744.     {
  745.       /* Find the next alternative to p
  746.      that might be true when p is true.
  747.      Test that one next if p's successors fail.
  748.      Note that when the `tests' field is nonzero
  749.      it is up to the specified test-function to compare machine modes
  750.      and some (such as general_operand) don't always do so.
  751.      But when inside a switch-on-modes we ignore this and
  752.      consider all modes mutually exclusive.  */
  753.       for (p1 = p->next; p1; p1 = p1->next)
  754.     if (((p->code == UNKNOWN || p1->code == UNKNOWN || p->code == p1->code)
  755.          && (p->mode == VOIDmode || p1->mode == VOIDmode
  756.          || p->mode == p1->mode
  757.          || (in_switch != MODE_SWITCH && (p->tests || p1->tests))))
  758.         || strcmp (p1->position, p->position))
  759.       break;
  760.       p->afterward = p1;
  761.       if (p1) p1->label_needed = 1;
  762.  
  763.       if (in_switch == MODE_SWITCH
  764.       && (p->mode == VOIDmode || (! p->enforce_mode && p->tests != 0)))
  765.     {
  766.       in_switch = NO_SWITCH;
  767.       printf ("  }\n");
  768.     }
  769.       if (in_switch == CODE_SWITCH && p->code == UNKNOWN)
  770.     {
  771.       in_switch = NO_SWITCH;
  772.       printf ("  }\n");
  773.     }
  774.  
  775.       if (p->label_needed)
  776.     printf (" L%d:\n", p->number);
  777.  
  778.       if (p->success == 0 && p->insn_code_number < 0)
  779.     abort ();
  780.  
  781.       change_state (pos, p->position);
  782.       pos = p->position;
  783.       depth = strlen (pos);
  784.  
  785.       ignmode = p->ignmode || pos[depth - 1] == '*' || p->tests;
  786.  
  787.       if (in_switch == NO_SWITCH)
  788.     {
  789.       /* If p and its alternatives all want the same mode,
  790.          reject all others at once, first, then ignore the mode.  */
  791.       if (!ignmode && p->mode != VOIDmode && p->next && same_modes (p, p->mode))
  792.         {
  793.           printf ("  if (GET_MODE (x%d) != %smode)\n",
  794.               depth, GET_MODE_NAME (p->mode));
  795.           if (afterward)
  796.         {
  797.           printf ("    {\n    ");
  798.           change_state (pos, afterpos);
  799.           printf ("      goto L%d;\n    }\n", afterward);
  800.         }
  801.           else
  802.         printf ("    goto ret0;\n");
  803.           clear_modes (p);
  804.           ignmode = 1;
  805.         }
  806.  
  807.       /* If p and its alternatives all want the same code,
  808.          reject all others at once, first, then ignore the code.  */
  809.       if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
  810.         {
  811.           printf ("  if (GET_CODE (x%d) != ", depth);
  812.           print_code (p->code);
  813.           printf (")\n");
  814.           if (afterward)
  815.         {
  816.           printf ("    {");
  817.           change_state (pos, afterpos);
  818.           printf ("    goto L%d; }\n", afterward);
  819.         }
  820.           else
  821.         printf ("    goto ret0;\n");
  822.           clear_codes (p);
  823.         }
  824.     }
  825.  
  826.       /* If p and its alternatives all have different modes
  827.      and there are at least 4 of them, make a switch.  */
  828.       if (in_switch == NO_SWITCH && pos[depth-1] != '*')
  829.     {
  830.       register int i;
  831.       int lose = 0;
  832.  
  833.       mybzero (modemap, sizeof modemap);
  834.       for (p1 = p, i = 0;
  835.            (p1 && p1->mode != VOIDmode
  836.         && (p1->tests == 0 || p1->enforce_mode));
  837.            p1 = p1->next, i++)
  838.         {
  839.           if (! p->enforce_mode && modemap[(int) p1->mode])
  840.         {
  841.           lose = 1;
  842.           break;
  843.         }
  844.           modemap[(int) p1->mode] = 1;
  845.         }
  846.       if (!lose && i >= 4)
  847.         {
  848.           in_switch = MODE_SWITCH;
  849.           printf (" switch (GET_MODE (x%d))\n  {\n", depth);
  850.         }
  851.     }
  852.  
  853.       if (in_switch == NO_SWITCH)
  854.     {
  855.       register int i;
  856.       mybzero (codemap, sizeof codemap);
  857.       for (p1 = p, i = 0; p1 && p1->code != UNKNOWN; p1 = p1->next, i++)
  858.         {
  859.           if (codemap[(int) p1->code])
  860.         break;
  861.           codemap[(int) p1->code] = 1;
  862.         }
  863.       if ((p1 == 0 || p1->code == UNKNOWN) && i >= 4)
  864.         {
  865.           in_switch = CODE_SWITCH;
  866.           printf (" switch (GET_CODE (x%d))\n  {\n", depth);
  867.         }
  868.     }
  869.  
  870.       if (in_switch == MODE_SWITCH)
  871.     {
  872.       if (modemap[(int) p->mode])
  873.         {
  874.           printf ("  case %smode:\n", GET_MODE_NAME (p->mode));
  875.           modemap[(int) p->mode] = 0;
  876.         }
  877.     }
  878.       if (in_switch == CODE_SWITCH)
  879.     {
  880.       if (codemap[(int) p->code])
  881.         {
  882.           printf ("  case ");
  883.           print_code (p->code);
  884.           printf (":\n");
  885.           codemap[(int) p->code] = 0;
  886.         }
  887.     }
  888.  
  889.       printf ("  if (");
  890.       if (p->exact || (p->code != UNKNOWN && in_switch != CODE_SWITCH))
  891.     {
  892.       if (p->exact)
  893.         printf ("x%d == %s", depth, p->exact);
  894.       else
  895.         {
  896.           printf ("GET_CODE (x%d) == ", depth);
  897.           print_code (p->code);
  898.         }
  899.       printf (" && ");
  900.     }
  901.       if (p->mode != VOIDmode && !ignmode && in_switch != MODE_SWITCH)
  902.     printf ("GET_MODE (x%d) == %smode && ",
  903.         depth, GET_MODE_NAME (p->mode));
  904.       if (p->test_elt_zero_int)
  905.     printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
  906.       if (p->veclen)
  907.     printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
  908.       if (p->test_elt_one_int)
  909.     printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
  910.       if (p->dupno >= 0)
  911.     printf ("rtx_equal_p (x%d, ro[%d]) && ", depth, p->dupno);
  912.       if (p->tests)
  913.     printf ("%s (x%d, %smode)", p->tests, depth,
  914.         GET_MODE_NAME (p->mode));
  915.       else
  916.     printf ("1");
  917.  
  918.       if (p->opno >= 0)
  919.     printf (")\n    { ro[%d] = x%d; ",
  920.         p->opno, depth);
  921.       else
  922.     printf (")\n    ");
  923.  
  924.       if (p->c_test)
  925.     printf ("if (%s) ", p->c_test);
  926.  
  927.       if (p->insn_code_number >= 0)
  928.     {
  929.       if (type == SPLIT)
  930.         printf ("return gen_split_%d (operands);", p->insn_code_number);
  931.       else
  932.         {
  933.           if (p->num_clobbers_to_add)
  934.         {
  935.           printf ("\n      {\n");
  936.           printf ("\tif (pnum_clobbers == 0) goto ret0; ");
  937.           printf ("*pnum_clobbers = %d; ", p->num_clobbers_to_add);
  938.           printf ("return %d;\n      }", p->insn_code_number);
  939.         }
  940.           else
  941.         printf ("return %d;", p->insn_code_number);
  942.         }
  943.     }
  944.       else
  945.     printf ("goto L%d;", p->success->number);
  946.  
  947.       if (p->opno >= 0)
  948.     printf (" }\n");
  949.       else
  950.     printf ("\n");
  951.  
  952.       /* Now, if inside a switch, branch to next switch member
  953.      that might also need to be tested if this one fails.  */
  954.  
  955.       if (in_switch == CODE_SWITCH)
  956.     {
  957.       /* Find the next alternative to p
  958.          that might be applicable if p was applicable.  */
  959.       for (p1 = p->next; p1; p1 = p1->next)
  960.         if (p1->code == UNKNOWN || p->code == p1->code)
  961.           break;
  962.       if (p1 == 0 || p1->code == UNKNOWN)
  963.         printf ("  break;\n");
  964.       else if (p1 != p->next)
  965.         {
  966.           printf (" goto L%d;\n", p1->number);
  967.           p1->label_needed = 1;
  968.         }
  969.     }
  970.  
  971.       if (in_switch == MODE_SWITCH)
  972.     {
  973.       /* Find the next alternative to p
  974.          that might be applicable if p was applicable.  */
  975.       for (p1 = p->next; p1; p1 = p1->next)
  976.         if (p1->mode == VOIDmode || p->mode == p1->mode)
  977.           break;
  978.       if (p1 == 0 || p1->mode == VOIDmode)
  979.         printf ("  break;\n");
  980.       else if (p1 != p->next)
  981.         {
  982.           printf (" goto L%d;\n", p1->number);
  983.           p1->label_needed = 1;
  984.         }
  985.     }
  986.     }
  987.  
  988.   if (in_switch != NO_SWITCH)
  989.     printf ("  }\n");
  990.  
  991.   if (afterward)
  992.     {
  993.       change_state (pos, afterpos);
  994.       printf ("  goto L%d;\n", afterward);
  995.     }
  996.   else
  997.     printf ("  goto ret0;\n");
  998.   return pos;
  999. }
  1000.  
  1001. void
  1002. write_tree (tree, prevpos, afterward, afterpos, initial, type)
  1003.      struct decision *tree;
  1004.      char *prevpos;
  1005.      int afterward;
  1006.      char *afterpos;
  1007.      int initial;
  1008.      enum routine_type type;
  1009. {
  1010.   register struct decision *p;
  1011.   char *pos = prevpos;
  1012.   char *name_prefix = (type == SPLIT ? "split" : "recog");
  1013.   char *call_suffix = (type == SPLIT ? "" : ", pnum_clobbers");
  1014.  
  1015.   if (tree->subroutine_number > 0 && ! initial)
  1016.     {
  1017.       printf (" L%d:\n", tree->number);
  1018.  
  1019.       if (afterward)
  1020.     {
  1021.       printf ("  tem = %s_%d (x0, insn%s);\n",
  1022.           name_prefix, tree->subroutine_number, call_suffix);
  1023.       printf ("  if (tem >= 0) return tem;\n");
  1024.       change_state (pos, afterpos);
  1025.       printf ("  goto L%d;\n", afterward);
  1026.     }
  1027.       else
  1028.     printf ("  return %s_%d (x0, insn%s);\n",
  1029.         name_prefix, tree->subroutine_number, call_suffix);
  1030.       return;
  1031.     }
  1032.  
  1033.   pos = write_tree_1 (tree, prevpos, afterward, afterpos, initial, type);
  1034.  
  1035.   for (p = tree; p; p = p->next)
  1036.     if (p->success)
  1037.       {
  1038.     pos = p->position;
  1039.     write_tree (p->success, pos,
  1040.             p->afterward ? p->afterward->number : afterward,
  1041.             p->afterward ? pos : afterpos, 0, type);
  1042.       }
  1043. }
  1044.  
  1045. void
  1046. print_code (code)
  1047.      RTX_CODE code;
  1048. {
  1049.   register char *p1;
  1050.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  1051.     {
  1052.       if (*p1 >= 'a' && *p1 <= 'z')
  1053.     putchar (*p1 + 'A' - 'a');
  1054.       else
  1055.     putchar (*p1);
  1056.     }
  1057. }
  1058.  
  1059. int
  1060. same_codes (p, code)
  1061.      register struct decision *p;
  1062.      register RTX_CODE code;
  1063. {
  1064.   for (; p; p = p->next)
  1065.     if (p->code != code)
  1066.       return 0;
  1067.  
  1068.   return 1;
  1069. }
  1070.  
  1071. void
  1072. clear_codes (p)
  1073.      register struct decision *p;
  1074. {
  1075.   for (; p; p = p->next)
  1076.     p->code = UNKNOWN;
  1077. }
  1078.  
  1079. int
  1080. same_modes (p, mode)
  1081.      register struct decision *p;
  1082.      register enum machine_mode mode;
  1083. {
  1084.   for (; p; p = p->next)
  1085.     if (p->mode != mode || p->tests)
  1086.       return 0;
  1087.  
  1088.   return 1;
  1089. }
  1090.  
  1091. void
  1092. clear_modes (p)
  1093.      register struct decision *p;
  1094. {
  1095.   for (; p; p = p->next)
  1096.     p->ignmode = 1;
  1097. }
  1098.  
  1099. void
  1100. change_state (oldpos, newpos)
  1101.      char *oldpos;
  1102.      char *newpos;
  1103. {
  1104.   int odepth = strlen (oldpos);
  1105.   int depth = odepth;
  1106.   int ndepth = strlen (newpos);
  1107.  
  1108.   /* Pop up as many levels as necessary.  */
  1109.  
  1110.   while (strncmp (oldpos, newpos, depth))
  1111.     --depth;
  1112.  
  1113.   /* Go down to desired level.  */
  1114.  
  1115.   while (depth < ndepth)
  1116.     {
  1117.       if (newpos[depth] == '*')
  1118.     printf ("  x%d = recog_addr_dummy;\n  XEXP (x%d, 0) = x%d;\n",
  1119.         depth + 1, depth + 1, depth);
  1120.       else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
  1121.     printf ("  x%d = XVECEXP (x%d, 0, %d);\n",
  1122.         depth + 1, depth, newpos[depth] - 'a');
  1123.       else
  1124.     printf ("  x%d = XEXP (x%d, %c);\n",
  1125.         depth + 1, depth, newpos[depth]);
  1126.       ++depth;
  1127.     }
  1128. }
  1129.  
  1130. char *
  1131. copystr (s1)
  1132.      char *s1;
  1133. {
  1134.   register char *tem;
  1135.  
  1136.   if (s1 == 0)
  1137.     return 0;
  1138.  
  1139.   tem = (char *) xmalloc (strlen (s1) + 1);
  1140.   strcpy (tem, s1);
  1141.  
  1142.   return tem;
  1143. }
  1144.  
  1145. void
  1146. mybzero (b, length)
  1147.      register char *b;
  1148.      register int length;
  1149. {
  1150.   while (length-- > 0)
  1151.     *b++ = 0;
  1152. }
  1153.  
  1154. char *
  1155. concat (s1, s2)
  1156.      char *s1, *s2;
  1157. {
  1158.   register char *tem;
  1159.  
  1160.   if (s1 == 0)
  1161.     return s2;
  1162.   if (s2 == 0)
  1163.     return s1;
  1164.  
  1165.   tem = (char *) xmalloc (strlen (s1) + strlen (s2) + 2);
  1166.   strcpy (tem, s1);
  1167.   strcat (tem, " ");
  1168.   strcat (tem, s2);
  1169.  
  1170.   return tem;
  1171. }
  1172.  
  1173. int
  1174. xrealloc (ptr, size)
  1175.      char *ptr;
  1176.      int size;
  1177. {
  1178.   int result = realloc (ptr, size);
  1179.   if (!result)
  1180.     fatal ("virtual memory exhausted");
  1181.   return result;
  1182. }
  1183.  
  1184. int
  1185. xmalloc (size)
  1186. {
  1187.   register int val = malloc (size);
  1188.  
  1189.   if (val == 0)
  1190.     fatal ("virtual memory exhausted");
  1191.   return val;
  1192. }
  1193.  
  1194. void
  1195. fatal (s, a1, a2)
  1196.      char *s;
  1197. {
  1198.   fprintf (stderr, "genrecog: ");
  1199.   fprintf (stderr, s, a1, a2);
  1200.   fprintf (stderr, "\n");
  1201.   fprintf (stderr, "after %d instruction definitions\n", next_index);
  1202.   exit (FATAL_EXIT_CODE);
  1203. }
  1204.  
  1205. /* More 'friendly' abort that prints the line and file.
  1206.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1207.  
  1208. void
  1209. fancy_abort ()
  1210. {
  1211.   fatal ("Internal gcc abort.");
  1212. }
  1213.  
  1214. int
  1215. main (argc, argv)
  1216.      int argc;
  1217.      char **argv;
  1218. {
  1219.   rtx desc;
  1220.   struct decision *tree = 0;
  1221.   struct decision *split_tree = 0;
  1222.   FILE *infile;
  1223.   extern rtx read_rtx ();
  1224.   register int c;
  1225.  
  1226.   obstack_init (rtl_obstack);
  1227.  
  1228.   if (argc <= 1)
  1229.     fatal ("No input file name.");
  1230.  
  1231.   infile = fopen (argv[1], "r");
  1232.   if (infile == 0)
  1233.     {
  1234.       perror (argv[1]);
  1235.       exit (FATAL_EXIT_CODE);
  1236.     }
  1237.  
  1238.   init_rtl ();
  1239.   next_insn_code = 0;
  1240.   next_index = 0;
  1241.  
  1242.   printf ("/* Generated automatically by the program `genrecog'\n\
  1243. from the machine description file `md'.  */\n\n");
  1244.  
  1245.   printf ("#include \"config.h\"\n");
  1246.   printf ("#include \"rtl.h\"\n");
  1247.   printf ("#include \"insn-config.h\"\n");
  1248.   printf ("#include \"recog.h\"\n");
  1249.   printf ("#include \"real.h\"\n");
  1250.   printf ("#include \"output.h\"\n");
  1251.   printf ("#include \"flags.h\"\n");
  1252.   printf ("\n");
  1253.  
  1254.   /* Read the machine description.  */
  1255.  
  1256.   while (1)
  1257.     {
  1258.       c = read_skip_spaces (infile);
  1259.       if (c == EOF)
  1260.     break;
  1261.       ungetc (c, infile);
  1262.  
  1263.       desc = read_rtx (infile);
  1264.       if (GET_CODE (desc) == DEFINE_INSN)
  1265.     tree = merge_trees (tree, make_insn_sequence (desc));
  1266.       else if (GET_CODE (desc) == DEFINE_SPLIT)
  1267.     split_tree = merge_trees (split_tree, make_split_sequence (desc));
  1268.       if (GET_CODE (desc) == DEFINE_PEEPHOLE
  1269.       || GET_CODE (desc) == DEFINE_EXPAND)
  1270.     next_insn_code++;
  1271.       next_index++;
  1272.     }
  1273.  
  1274.   printf ("\n\
  1275. /* `recog' contains a decision tree\n\
  1276.    that recognizes whether the rtx X0 is a valid instruction.\n\
  1277. \n\
  1278.    recog returns -1 if the rtx is not valid.\n\
  1279.    If the rtx is valid, recog returns a nonnegative number\n\
  1280.    which is the insn code number for the pattern that matched.\n");
  1281.   printf ("   This is the same as the order in the machine description of\n\
  1282.    the entry that matched.  This number can be used as an index into\n\
  1283.    entry that matched.  This number can be used as an index into various\n\
  1284.    insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands\n\
  1285.    (found in insn-output.c).\n\n");
  1286.   printf ("   The third argument to recog is an optional pointer to an int.\n\
  1287.    If present, recog will accept a pattern if it matches except for\n\
  1288.    missing CLOBBER expressions at the end.  In that case, the value\n\
  1289.    pointed to by the optional pointer will be set to the number of\n\
  1290.    CLOBBERs that need to be added (it should be initialized to zero by\n\
  1291.    the caller).  If it is set nonzero, the caller should allocate a\n\
  1292.    PARALLEL of the appropriate size, copy the initial entries, and call\n\
  1293.    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
  1294.  
  1295.   if (split_tree)
  1296.     printf ("\n\n   The function split_insns returns 0 if the rtl could not\n\
  1297.    be split or the split rtl in a SEQUENCE if it can be.");
  1298.  
  1299.   printf ("*/\n\n");
  1300.  
  1301.   printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
  1302.   printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
  1303.   printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
  1304.   printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
  1305.   printf ("extern rtx recog_addr_dummy;\n\n");
  1306.   printf ("#define operands recog_operand\n\n");
  1307.  
  1308.   next_subroutine_number = 0;
  1309.   break_out_subroutines (tree, RECOG);
  1310.  
  1311.   printf ("int\nrecog (x0, insn, pnum_clobbers)\n");
  1312.   printf ("     register rtx x0;\n     rtx insn;\n");
  1313.   printf ("     int *pnum_clobbers;\n{\n");
  1314.   printf ("  register rtx *ro = &recog_operand[0];\n");
  1315.   printf ("  register rtx x1, x2, x3, x4, x5;\n  rtx x6, x7, x8, x9, x10, x11;\n");
  1316.   printf ("  int tem;\n");
  1317.  
  1318.   if (tree)
  1319.     write_tree (tree, "", 0, "", 1, RECOG);
  1320.   printf (" ret0: return -1;\n}\n");
  1321.  
  1322.   next_subroutine_number = 0;
  1323.   break_out_subroutines (split_tree, SPLIT);
  1324.  
  1325.   printf ("rtx\nsplit_insns (x0, insn)\n     register rtx x0;\n     rtx insn;\n{\n");
  1326.   printf ("  register rtx *ro = &recog_operand[0];\n");
  1327.   printf ("  register rtx x1, x2, x3, x4, x5;\n  rtx x6, x7, x8, x9, x10, x11;\n");
  1328.   printf ("  rtx tem;\n");
  1329.  
  1330.   if (split_tree)
  1331.     write_tree (split_tree, "", 0, "", 1, SPLIT);
  1332.   printf (" ret0: return 0;\n}\n");
  1333.  
  1334.   fflush (stdout);
  1335.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  1336. }
  1337.