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

  1. /* Generate code from to output assembler insns as recognized from rtl.
  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 reads the machine description for the compiler target machine
  22.    and produces a file containing these things:
  23.  
  24.    1. An array of strings `insn_template' which is indexed by insn code number
  25.    and contains the template for output of that insn,
  26.  
  27.    2. An array of functions `insn_outfun' which, indexed by the insn code
  28.    number, gives the function that returns a template to use for output of
  29.    that insn.  This is used only in the cases where the template is not
  30.    constant.  These cases are specified by a * or @ at the beginning of the
  31.    template string in the machine description.  They are identified for the
  32.    sake of other parts of the compiler by a zero element in `insn_template'.
  33.   
  34.    3. An array of functions `insn_gen_function' which, indexed
  35.    by insn code number, gives the function to generate a body
  36.    for that pattern, given operands as arguments.
  37.  
  38.    4. An array of strings `insn_name' which, indexed by insn code number,
  39.    gives the name for that pattern.  Nameless patterns are given a name.
  40.  
  41.    5. An array of ints `insn_n_operands' which is indexed by insn code number
  42.    and contains the number of distinct operands in the pattern for that insn,
  43.  
  44.    6. An array of ints `insn_n_dups' which is indexed by insn code number
  45.    and contains the number of match_dup's that appear in the insn's pattern.
  46.    This says how many elements of `recog_dup_loc' are significant
  47.    after an insn has been recognized.
  48.  
  49.    7. An array of arrays of operand constraint strings,
  50.    `insn_operand_constraint',
  51.    indexed first by insn code number and second by operand number,
  52.    containing the constraint for that operand.
  53.  
  54.    This array is generated only if register constraints appear in 
  55.    match_operand rtx's.
  56.  
  57.    8. An array of arrays of chars which indicate which operands of
  58.    which insn patterns appear within ADDRESS rtx's.  This array is
  59.    called `insn_operand_address_p' and is generated only if there
  60.    are *no* register constraints in the match_operand rtx's.
  61.  
  62.    9. An array of arrays of machine modes, `insn_operand_mode',
  63.    indexed first by insn code number and second by operand number,
  64.    containing the machine mode that that operand is supposed to have.
  65.    Also `insn_operand_strict_low', which is nonzero for operands
  66.    contained in a STRICT_LOW_PART.
  67.  
  68.    10. An array of arrays of int-valued functions, `insn_operand_predicate',
  69.    indexed first by insn code number and second by operand number,
  70.    containing the match_operand predicate for this operand.
  71.  
  72.    11. An array of ints, `insn_n_alternatives', that gives the number
  73.    of alternatives in the constraints of each pattern.
  74.  
  75. The code number of an insn is simply its position in the machine description;
  76. code numbers are assigned sequentially to entries in the description,
  77. starting with code number 0.
  78.  
  79. Thus, the following entry in the machine description
  80.  
  81.     (define_insn "clrdf"
  82.       [(set (match_operand:DF 0 "general_operand" "")
  83.         (const_int 0))]
  84.       ""
  85.       "clrd %0")
  86.  
  87. assuming it is the 25th entry present, would cause
  88. insn_template[24] to be "clrd %0", and insn_n_operands[24] to be 1.
  89. It would not make an case in output_insn_hairy because the template
  90. given in the entry is a constant (it does not start with `*').  */
  91.  
  92. #include <stdio.h>
  93. #include "config.h"
  94. #include "rtl.h"
  95. #include "obstack.h"
  96.  
  97. /* No instruction can have more operands than this.
  98.    Sorry for this arbitrary limit, but what machine will
  99.    have an instruction with this many operands?  */
  100.  
  101. #define MAX_MAX_OPERANDS 40
  102.  
  103. struct obstack obstack;
  104. struct obstack *rtl_obstack = &obstack;
  105.  
  106. #define obstack_chunk_alloc xmalloc
  107. #define obstack_chunk_free free
  108. extern int xmalloc ();
  109. extern void free ();
  110.  
  111. void fatal ();
  112. void fancy_abort ();
  113. void error ();
  114. void mybcopy ();
  115. void mybzero ();
  116.  
  117. /* insns in the machine description are assigned sequential code numbers
  118.    that are used by insn-recog.c (produced by genrecog) to communicate
  119.    to insn-output.c (produced by this program).  */
  120.  
  121. int next_code_number;
  122.  
  123. /* This counts all definitions in the md file,
  124.    for the sake of error messages.  */
  125.  
  126. int next_index_number;
  127.  
  128. /* Record in this chain all information that we will output,
  129.    associated with the code number of the insn.  */
  130.  
  131. struct data
  132. {
  133.   int code_number;
  134.   int index_number;
  135.   char *name;
  136.   char *template;        /* string such as "movl %1,%0" */
  137.   int n_operands;        /* Number of operands this insn recognizes */
  138.   int n_dups;            /* Number times match_dup appears in pattern */
  139.   int n_alternatives;        /* Number of alternatives in each constraint */
  140.   struct data *next;
  141.   char *constraints[MAX_MAX_OPERANDS];
  142.   /* Number of alternatives in constraints of operand N.  */
  143.   int op_n_alternatives[MAX_MAX_OPERANDS];
  144.   char *predicates[MAX_MAX_OPERANDS];
  145.   char address_p[MAX_MAX_OPERANDS];
  146.   enum machine_mode modes[MAX_MAX_OPERANDS];
  147.   char strict_low[MAX_MAX_OPERANDS];
  148.   char outfun;            /* Nonzero means this has an output function */
  149. };
  150.  
  151. /* This variable points to the first link in the chain.  */
  152.  
  153. struct data *insn_data;
  154.  
  155. /* Pointer to the last link in the chain, so new elements
  156.    can be added at the end.  */
  157.  
  158. struct data *end_of_insn_data;
  159.  
  160. /* Nonzero if any match_operand has a constraint string;
  161.    implies that REGISTER_CONSTRAINTS will be defined
  162.    for this machine description.  */
  163.  
  164. int have_constraints;
  165.  
  166. void
  167. output_prologue ()
  168. {
  169.  
  170.   printf ("/* Generated automatically by the program `genoutput'\n\
  171. from the machine description file `md'.  */\n\n");
  172.  
  173.   printf ("#include \"config.h\"\n");
  174.   printf ("#include \"rtl.h\"\n");
  175.   printf ("#include \"regs.h\"\n");
  176.   printf ("#include \"hard-reg-set.h\"\n");
  177.   printf ("#include \"real.h\"\n");
  178.   printf ("#include \"insn-config.h\"\n\n");
  179.   printf ("#include \"conditions.h\"\n");
  180.   printf ("#include \"insn-flags.h\"\n");
  181.   printf ("#include \"insn-attr.h\"\n\n");
  182.   printf ("#include \"insn-codes.h\"\n\n");
  183.   printf ("#include \"recog.h\"\n\n");
  184.  
  185.   printf ("#include <stdio.h>\n");
  186.   printf ("#include \"output.h\"\n");
  187. }
  188.  
  189. void
  190. output_epilogue ()
  191. {
  192.   register struct data *d;
  193.  
  194.   printf ("\nchar * const insn_template[] =\n  {\n");
  195.   for (d = insn_data; d; d = d->next)
  196.     {
  197.       if (d->template)
  198.     printf ("    \"%s\",\n", d->template);
  199.       else
  200.     printf ("    0,\n");
  201.     }
  202.   printf ("  };\n");
  203.  
  204.   printf ("\nchar *(*const insn_outfun[])() =\n  {\n");
  205.   for (d = insn_data; d; d = d->next)
  206.     {
  207.       if (d->outfun)
  208.     printf ("    output_%d,\n", d->code_number);
  209.       else
  210.     printf ("    0,\n");
  211.     }
  212.   printf ("  };\n");
  213.  
  214.   printf ("\nrtx (*const insn_gen_function[]) () =\n  {\n");
  215.   for (d = insn_data; d; d = d->next)
  216.     {
  217.       if (d->name)
  218.     printf ("    gen_%s,\n", d->name);
  219.       else
  220.     printf ("    0,\n");
  221.     }
  222.   printf ("  };\n");
  223.  
  224.   printf ("\nchar *insn_name[] =\n  {\n");
  225.   {
  226.     int offset = 0;
  227.     int next;
  228.     char * last_name = 0;
  229.     char * next_name;
  230.     register struct data *n;
  231.  
  232.     for (n = insn_data, next = 0; n; n = n->next, next++)
  233.       if (n->name)
  234.     {
  235.       next_name = n->name;
  236.       break;
  237.     }
  238.  
  239.     for (d = insn_data; d; d = d->next)
  240.       {
  241.     if (d->name)
  242.       {
  243.         printf ("    \"%s\",\n", d->name);
  244.         offset = 0;
  245.         last_name = d->name;
  246.         next_name = 0;
  247.         for (n = d->next, next = 1; n; n = n->next, next++)
  248.           if (n->name)
  249.         {
  250.           next_name = n->name;
  251.           break;
  252.         }
  253.       }
  254.     else
  255.       {
  256.         offset++;
  257.         if (next_name && (last_name == 0 || offset > next / 2))
  258.           printf ("    \"%s-%d\",\n", next_name, next - offset);
  259.         else
  260.           printf ("    \"%s+%d\",\n", last_name, offset);
  261.       }
  262.       }
  263.   }
  264.   printf ("  };\n");
  265.   printf ("char **insn_name_ptr = insn_name;\n");
  266.  
  267.   printf ("\nconst int insn_n_operands[] =\n  {\n");
  268.   for (d = insn_data; d; d = d->next)
  269.     printf ("    %d,\n", d->n_operands);
  270.   printf ("  };\n");
  271.  
  272.   printf ("\nconst int insn_n_dups[] =\n  {\n");
  273.   for (d = insn_data; d; d = d->next)
  274.     printf ("    %d,\n", d->n_dups);
  275.   printf ("  };\n");
  276.  
  277.   if (have_constraints)
  278.     {
  279.       printf ("\nchar *const insn_operand_constraint[][MAX_RECOG_OPERANDS] =\n  {\n");
  280.       for (d = insn_data; d; d = d->next)
  281.     {
  282.       register int i;
  283.       printf ("    {");
  284.       for (i = 0; i < d->n_operands; i++)
  285.         {
  286.           if (d->constraints[i] == 0)
  287.         printf (" \"\",");
  288.           else
  289.         printf (" \"%s\",", d->constraints[i]);
  290.         }
  291.       if (d->n_operands == 0)
  292.         printf (" 0");
  293.       printf (" },\n");
  294.     }
  295.       printf ("  };\n");
  296.     }
  297.   else
  298.     {
  299.       printf ("\nconst char insn_operand_address_p[][MAX_RECOG_OPERANDS] =\n  {\n");
  300.       for (d = insn_data; d; d = d->next)
  301.     {
  302.       register int i;
  303.       printf ("    {");
  304.       for (i = 0; i < d->n_operands; i++)
  305.         printf (" %d,", d->address_p[i]);
  306.       if (d->n_operands == 0)
  307.         printf (" 0");
  308.       printf (" },\n");
  309.     }
  310.       printf ("  };\n");
  311.     }
  312.  
  313.   printf ("\nconst enum machine_mode insn_operand_mode[][MAX_RECOG_OPERANDS] =\n  {\n");
  314.   for (d = insn_data; d; d = d->next)
  315.     {
  316.       register int i;
  317.       printf ("    {");
  318.       for (i = 0; i < d->n_operands; i++)
  319.     printf (" %smode,", GET_MODE_NAME (d->modes[i]));
  320.       if (d->n_operands == 0)
  321.     printf (" VOIDmode");
  322.       printf (" },\n");
  323.     }
  324.   printf ("  };\n");
  325.  
  326.   printf ("\nconst char insn_operand_strict_low[][MAX_RECOG_OPERANDS] =\n  {\n");
  327.   for (d = insn_data; d; d = d->next)
  328.     {
  329.       register int i;
  330.       printf ("    {");
  331.       for (i = 0; i < d->n_operands; i++)
  332.     printf (" %d,", d->strict_low[i]);
  333.       if (d->n_operands == 0)
  334.     printf (" 0");
  335.       printf (" },\n");
  336.     }
  337.   printf ("  };\n");
  338.  
  339.   {
  340.     /* We need to define all predicates used.  Keep a list of those we
  341.        have defined so far.  There normally aren't very many predicates used,
  342.        so a linked list should be fast enough.  */
  343.     struct predicate { char *name; struct predicate *next; } *predicates = 0;
  344.     struct predicate *p;
  345.     int i;
  346.  
  347.     printf ("\n");
  348.     for (d = insn_data; d; d = d->next)
  349.       for (i = 0; i < d->n_operands; i++)
  350.     if (d->predicates[i] && d->predicates[i][0])
  351.       {
  352.         for (p = predicates; p; p = p->next)
  353.           if (! strcmp (p->name, d->predicates[i]))
  354.         break;
  355.  
  356.         if (p == 0)
  357.           {
  358.         printf ("extern int %s ();\n", d->predicates[i]);
  359.         p = (struct predicate *) alloca (sizeof (struct predicate));
  360.         p->name = d->predicates[i];
  361.         p->next = predicates;
  362.         predicates = p;
  363.           }
  364.       }
  365.     
  366.     printf ("\nint (*const insn_operand_predicate[][MAX_RECOG_OPERANDS])() =\n  {\n");
  367.     for (d = insn_data; d; d = d->next)
  368.       {
  369.     printf ("    {");
  370.     for (i = 0; i < d->n_operands; i++)
  371.       printf (" %s,", ((d->predicates[i] && d->predicates[i][0])
  372.                ? d->predicates[i] : "0"));
  373.     if (d->n_operands == 0)
  374.       printf (" 0");
  375.     printf (" },\n");
  376.       }
  377.     printf ("  };\n");
  378.   }
  379.  
  380.   printf ("\nconst int insn_n_alternatives[] =\n  {\n");
  381.   for (d = insn_data; d; d = d->next)
  382.     printf ("    %d,\n", d->n_alternatives);
  383.   printf("  };\n");
  384. }
  385.  
  386. /* scan_operands (X) stores in max_opno the largest operand
  387.    number present in X, if that is larger than the previous
  388.    value of max_opno.  It stores all the constraints in `constraints'
  389.    and all the machine modes in `modes'.
  390.  
  391.    THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
  392.    THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART.  */
  393.  
  394. int max_opno;
  395. int num_dups;
  396. char *constraints[MAX_MAX_OPERANDS];
  397. int op_n_alternatives[MAX_MAX_OPERANDS];
  398. char *predicates[MAX_MAX_OPERANDS];
  399. char address_p[MAX_MAX_OPERANDS];
  400. enum machine_mode modes[MAX_MAX_OPERANDS];
  401. char strict_low[MAX_MAX_OPERANDS];
  402.  
  403. void
  404. scan_operands (part, this_address_p, this_strict_low)
  405.      rtx part;
  406.      int this_address_p;
  407.      int this_strict_low;
  408. {
  409.   register int i, j;
  410.   register char *format_ptr;
  411.   int opno;
  412.  
  413.   if (part == 0)
  414.     return;
  415.  
  416.   switch (GET_CODE (part))
  417.     {
  418.     case MATCH_OPERAND:
  419.       opno = XINT (part, 0);
  420.       if (opno > max_opno)
  421.     max_opno = opno;
  422.       if (max_opno >= MAX_MAX_OPERANDS)
  423.     error ("Too many operands (%d) in one instruction pattern.\n",
  424.            max_opno + 1);
  425.       modes[opno] = GET_MODE (part);
  426.       strict_low[opno] = this_strict_low;
  427.       predicates[opno] = XSTR (part, 1);
  428.       constraints[opno] = XSTR (part, 2);
  429.       if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
  430.     {
  431.       op_n_alternatives[opno] = n_occurrences (',', XSTR (part, 2)) + 1;
  432.       have_constraints = 1;
  433.     }
  434.       address_p[opno] = this_address_p;
  435.       return;
  436.  
  437.     case MATCH_SCRATCH:
  438.       opno = XINT (part, 0);
  439.       if (opno > max_opno)
  440.     max_opno = opno;
  441.       if (max_opno >= MAX_MAX_OPERANDS)
  442.     error ("Too many operands (%d) in one instruction pattern.\n",
  443.            max_opno + 1);
  444.       modes[opno] = GET_MODE (part);
  445.       strict_low[opno] = 0;
  446.       predicates[opno] = "scratch_operand";
  447.       constraints[opno] = XSTR (part, 1);
  448.       if (XSTR (part, 1) != 0 && *XSTR (part, 1) != 0)
  449.     {
  450.       op_n_alternatives[opno] = n_occurrences (',', XSTR (part, 1)) + 1;
  451.       have_constraints = 1;
  452.     }
  453.       address_p[opno] = 0;
  454.       return;
  455.  
  456.     case MATCH_OPERATOR:
  457.     case MATCH_PARALLEL:
  458.       opno = XINT (part, 0);
  459.       if (opno > max_opno)
  460.     max_opno = opno;
  461.       if (max_opno >= MAX_MAX_OPERANDS)
  462.     error ("Too many operands (%d) in one instruction pattern.\n",
  463.            max_opno + 1);
  464.       modes[opno] = GET_MODE (part);
  465.       strict_low[opno] = 0;
  466.       predicates[opno] = XSTR (part, 1);
  467.       constraints[opno] = 0;
  468.       address_p[opno] = 0;
  469.       for (i = 0; i < XVECLEN (part, 2); i++)
  470.     scan_operands (XVECEXP (part, 2, i), 0, 0);
  471.       return;
  472.  
  473.     case MATCH_DUP:
  474.     case MATCH_OP_DUP:
  475.       ++num_dups;
  476.       return;
  477.  
  478.     case ADDRESS:
  479.       scan_operands (XEXP (part, 0), 1, 0);
  480.       return;
  481.  
  482.     case STRICT_LOW_PART:
  483.       scan_operands (XEXP (part, 0), 0, 1);
  484.       return;
  485.     }
  486.  
  487.   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  488.  
  489.   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  490.     switch (*format_ptr++)
  491.       {
  492.       case 'e':
  493.     scan_operands (XEXP (part, i), 0, 0);
  494.     break;
  495.       case 'E':
  496.     if (XVEC (part, i) != NULL)
  497.       for (j = 0; j < XVECLEN (part, i); j++)
  498.         scan_operands (XVECEXP (part, i, j), 0, 0);
  499.     break;
  500.       }
  501. }
  502.  
  503. /* Process an assembler template from a define_insn or a define_peephole.
  504.    It is either the assembler code template, a list of assembler code
  505.    templates, or C code to generate the assembler code template.  */
  506.  
  507. void
  508. process_template (d, template)
  509.     struct data *d;
  510.     char *template;
  511. {
  512.   register char *cp;
  513.   register int i;
  514.  
  515.   /* We need to consider only the instructions whose assembler code template
  516.      starts with a * or @.  These are the ones where C code is run to decide
  517.      on a template to use.  So for all others just return now.  */
  518.  
  519.   if (template[0] != '*' && template[0] != '@')
  520.     {
  521.       d->template = template;
  522.       d->outfun = 0;
  523.       return;
  524.     }
  525.  
  526.   d->template = 0;
  527.   d->outfun = 1;
  528.  
  529.   printf ("\nstatic char *\n");
  530.   printf ("output_%d (operands, insn)\n", d->code_number);
  531.   printf ("     rtx *operands;\n");
  532.   printf ("     rtx insn;\n");
  533.   printf ("{\n");
  534.  
  535.   /* If the assembler code template starts with a @ it is a newline-separated
  536.      list of assembler code templates, one for each alternative.  So produce
  537.      a routine to select the correct one.  */
  538.  
  539.   if (template[0] == '@')
  540.     {
  541.  
  542.       printf ("  switch (which_alternative)\n");
  543.       printf ("    {\n");
  544.  
  545.       for (i = 0, cp = &template[1]; *cp; )
  546.     {
  547.       printf ("    case %d:\n", i);
  548.       while (*cp == '\n' || *cp == ' ' || *cp== '\t')
  549.         cp++;
  550.  
  551.       printf ("      return \"");
  552.       while (*cp != '\n' && *cp != '\0')
  553.         putchar (*cp++);
  554.  
  555.       printf ("\";\n");
  556.       i++;
  557.     }
  558.  
  559.       if (i != d->n_alternatives)
  560.     fatal ("Insn pattern %d has %d alternatives but %d assembler choices",
  561.            d->index_number, d->n_alternatives, i);
  562.  
  563.       printf ("    }\n");
  564.     }
  565.   else
  566.     {
  567.        /* The following is done in a funny way to get around problems in
  568.       VAX-11 "C" on VMS.  It is the equivalent of:
  569.         printf ("%s\n", &template[1])); */
  570.       cp = &template[1];
  571.       while (*cp) putchar (*cp++);
  572.       putchar ('\n');
  573.     }
  574.  
  575.   printf ("}\n");
  576. }
  577.  
  578. /* Check insn D for consistency in number of constraint alternatives.  */
  579.  
  580. void
  581. validate_insn_alternatives (d)
  582.      struct data *d;
  583. {
  584.   register int n = 0, start;
  585.   /* Make sure all the operands have the same number of
  586.      alternatives in their constraints.
  587.      Let N be that number.  */
  588.   for (start = 0; start < d->n_operands; start++)
  589.     if (d->op_n_alternatives[start] > 0)
  590.       {
  591.     if (n == 0)
  592.       n = d->op_n_alternatives[start];
  593.     else if (n != d->op_n_alternatives[start])
  594.       error ("wrong number of alternatives in operand %d of insn number %d",
  595.          start, d->index_number);
  596.       }
  597.   /* Record the insn's overall number of alternatives.  */
  598.   d->n_alternatives = n;
  599. }
  600.  
  601. /* Look at a define_insn just read.  Assign its code number.
  602.    Record on insn_data the template and the number of arguments.
  603.    If the insn has a hairy output action, output a function for now.  */
  604.  
  605. void
  606. gen_insn (insn)
  607.      rtx insn;
  608. {
  609.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  610.   register int i;
  611.  
  612.   d->code_number = next_code_number++;
  613.   d->index_number = next_index_number;
  614.   if (XSTR (insn, 0)[0])
  615.     d->name = XSTR (insn, 0);
  616.   else
  617.     d->name = 0;
  618.  
  619.   /* Build up the list in the same order as the insns are seen
  620.      in the machine description.  */
  621.   d->next = 0;
  622.   if (end_of_insn_data)
  623.     end_of_insn_data->next = d;
  624.   else
  625.     insn_data = d;
  626.  
  627.   end_of_insn_data = d;
  628.  
  629.   max_opno = -1;
  630.   num_dups = 0;
  631.  
  632.   mybzero (constraints, sizeof constraints);
  633.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  634.   mybzero (predicates, sizeof predicates);
  635.   mybzero (address_p, sizeof address_p);
  636.   mybzero (modes, sizeof modes);
  637.   mybzero (strict_low, sizeof strict_low);
  638.  
  639.   for (i = 0; i < XVECLEN (insn, 1); i++)
  640.     scan_operands (XVECEXP (insn, 1, i), 0, 0);
  641.  
  642.   d->n_operands = max_opno + 1;
  643.   d->n_dups = num_dups;
  644.  
  645.   mybcopy (constraints, d->constraints, sizeof constraints);
  646.   mybcopy (op_n_alternatives, d->op_n_alternatives, sizeof op_n_alternatives);
  647.   mybcopy (predicates, d->predicates, sizeof predicates);
  648.   mybcopy (address_p, d->address_p, sizeof address_p);
  649.   mybcopy (modes, d->modes, sizeof modes);
  650.   mybcopy (strict_low, d->strict_low, sizeof strict_low);
  651.  
  652.   validate_insn_alternatives (d);
  653.   process_template (d, XSTR (insn, 3));
  654. }
  655.  
  656. /* Look at a define_peephole just read.  Assign its code number.
  657.    Record on insn_data the template and the number of arguments.
  658.    If the insn has a hairy output action, output it now.  */
  659.  
  660. void
  661. gen_peephole (peep)
  662.      rtx peep;
  663. {
  664.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  665.   register int i;
  666.  
  667.   d->code_number = next_code_number++;
  668.   d->index_number = next_index_number;
  669.   d->name = 0;
  670.  
  671.   /* Build up the list in the same order as the insns are seen
  672.      in the machine description.  */
  673.   d->next = 0;
  674.   if (end_of_insn_data)
  675.     end_of_insn_data->next = d;
  676.   else
  677.     insn_data = d;
  678.  
  679.   end_of_insn_data = d;
  680.  
  681.   max_opno = -1;
  682.   mybzero (constraints, sizeof constraints);
  683.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  684.  
  685.   /* Get the number of operands by scanning all the
  686.      patterns of the peephole optimizer.
  687.      But ignore all the rest of the information thus obtained.  */
  688.   for (i = 0; i < XVECLEN (peep, 0); i++)
  689.     scan_operands (XVECEXP (peep, 0, i), 0, 0);
  690.  
  691.   d->n_operands = max_opno + 1;
  692.   d->n_dups = 0;
  693.  
  694.   mybcopy (constraints, d->constraints, sizeof constraints);
  695.   mybcopy (op_n_alternatives, d->op_n_alternatives, sizeof op_n_alternatives);
  696.   mybzero (d->predicates, sizeof predicates);
  697.   mybzero (d->address_p, sizeof address_p);
  698.   mybzero (d->modes, sizeof modes);
  699.   mybzero (d->strict_low, sizeof strict_low);
  700.  
  701.   validate_insn_alternatives (d);
  702.   process_template (d, XSTR (peep, 2));
  703. }
  704.  
  705. /* Process a define_expand just read.  Assign its code number,
  706.    only for the purposes of `insn_gen_function'.  */
  707.  
  708. void
  709. gen_expand (insn)
  710.      rtx insn;
  711. {
  712.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  713.   register int i;
  714.  
  715.   d->code_number = next_code_number++;
  716.   d->index_number = next_index_number;
  717.   if (XSTR (insn, 0)[0])
  718.     d->name = XSTR (insn, 0);
  719.   else
  720.     d->name = 0;
  721.  
  722.   /* Build up the list in the same order as the insns are seen
  723.      in the machine description.  */
  724.   d->next = 0;
  725.   if (end_of_insn_data)
  726.     end_of_insn_data->next = d;
  727.   else
  728.     insn_data = d;
  729.  
  730.   end_of_insn_data = d;
  731.  
  732.   max_opno = -1;
  733.   num_dups = 0;
  734.  
  735.   /* Scan the operands to get the specified predicates and modes,
  736.      since expand_binop needs to know them.  */
  737.  
  738.   mybzero (predicates, sizeof predicates);
  739.   mybzero (modes, sizeof modes);
  740.  
  741.   if (XVEC (insn, 1))
  742.     for (i = 0; i < XVECLEN (insn, 1); i++)
  743.       scan_operands (XVECEXP (insn, 1, i), 0, 0);
  744.  
  745.   d->n_operands = max_opno + 1;
  746.  
  747.   mybcopy (predicates, d->predicates, sizeof predicates);
  748.   mybcopy (modes, d->modes, sizeof modes);
  749.  
  750.   mybzero (d->constraints, sizeof constraints);
  751.   mybzero (d->op_n_alternatives, sizeof op_n_alternatives);
  752.   mybzero (d->address_p, sizeof address_p);
  753.   mybzero (d->strict_low, sizeof strict_low);
  754.  
  755.   d->n_dups = 0;
  756.   d->template = 0;
  757.   d->outfun = 0;
  758.   validate_insn_alternatives (d);
  759. }
  760.  
  761. /* Process a define_split just read.  Assign its code number,
  762.    only for reasons of consistency and to simplify genrecog.  */
  763.  
  764.  
  765. void
  766. gen_split (split)
  767.      rtx split;
  768. {
  769.   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  770.   register int i;
  771.  
  772.   d->code_number = next_code_number++;
  773.   d->index_number = next_index_number;
  774.   d->name = 0;
  775.  
  776.   /* Build up the list in the same order as the insns are seen
  777.      in the machine description.  */
  778.   d->next = 0;
  779.   if (end_of_insn_data)
  780.     end_of_insn_data->next = d;
  781.   else
  782.     insn_data = d;
  783.  
  784.   end_of_insn_data = d;
  785.  
  786.   max_opno = -1;
  787.   num_dups = 0;
  788.  
  789.   mybzero (constraints, sizeof constraints);
  790.   mybzero (op_n_alternatives, sizeof op_n_alternatives);
  791.  
  792.   /* Get the number of operands by scanning all the
  793.      patterns of the split patterns.
  794.      But ignore all the rest of the information thus obtained.  */
  795.   for (i = 0; i < XVECLEN (split, 0); i++)
  796.     scan_operands (XVECEXP (split, 0, i), 0, 0);
  797.  
  798.   d->n_operands = max_opno + 1;
  799.  
  800.   mybzero (d->constraints, sizeof constraints);
  801.   mybzero (d->op_n_alternatives, sizeof op_n_alternatives);
  802.   mybzero (d->predicates, sizeof predicates);
  803.   mybzero (d->address_p, sizeof address_p);
  804.   mybzero (d->modes, sizeof modes);
  805.   mybzero (d->strict_low, sizeof strict_low);
  806.  
  807.   d->n_dups = 0;
  808.   d->template = 0;
  809.   d->outfun = 0;
  810. }
  811.  
  812. int
  813. xmalloc (size)
  814. {
  815.   register int val = malloc (size);
  816.  
  817.   if (val == 0)
  818.     fatal ("virtual memory exhausted");
  819.   return val;
  820. }
  821.  
  822. int
  823. xrealloc (ptr, size)
  824.      char *ptr;
  825.      int size;
  826. {
  827.   int result = realloc (ptr, size);
  828.   if (!result)
  829.     fatal ("virtual memory exhausted");
  830.   return result;
  831. }
  832.  
  833. void
  834. mybzero (b, length)
  835.      register char *b;
  836.      register int length;
  837. {
  838.   while (length-- > 0)
  839.     *b++ = 0;
  840. }
  841.  
  842. void
  843. mybcopy (b1, b2, length)
  844.      register char *b1;
  845.      register char *b2;
  846.      register int length;
  847. {
  848.   while (length-- > 0)
  849.     *b2++ = *b1++;
  850. }
  851.  
  852. void
  853. fatal (s, a1, a2, a3, a4)
  854.      char *s;
  855. {
  856.   fprintf (stderr, "genoutput: ");
  857.   fprintf (stderr, s, a1, a2, a3, a4);
  858.   fprintf (stderr, "\n");
  859.   exit (FATAL_EXIT_CODE);
  860. }
  861.  
  862. /* More 'friendly' abort that prints the line and file.
  863.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  864.  
  865. void
  866. fancy_abort ()
  867. {
  868.   fatal ("Internal gcc abort.");
  869. }
  870.  
  871. void
  872. error (s, a1, a2)
  873.      char *s;
  874. {
  875.   fprintf (stderr, "genoutput: ");
  876.   fprintf (stderr, s, a1, a2);
  877.   fprintf (stderr, "\n");
  878. }
  879.  
  880. int
  881. main (argc, argv)
  882.      int argc;
  883.      char **argv;
  884. {
  885.   rtx desc;
  886.   FILE *infile;
  887.   extern rtx read_rtx ();
  888.   register int c;
  889.  
  890.   obstack_init (rtl_obstack);
  891.  
  892.   if (argc <= 1)
  893.     fatal ("No input file name.");
  894.  
  895.   infile = fopen (argv[1], "r");
  896.   if (infile == 0)
  897.     {
  898.       perror (argv[1]);
  899.       exit (FATAL_EXIT_CODE);
  900.     }
  901.  
  902.   init_rtl ();
  903.  
  904.   output_prologue ();
  905.   next_code_number = 0;
  906.   next_index_number = 0;
  907.   have_constraints = 0;
  908.  
  909.   /* Read the machine description.  */
  910.  
  911.   while (1)
  912.     {
  913.       c = read_skip_spaces (infile);
  914.       if (c == EOF)
  915.     break;
  916.       ungetc (c, infile);
  917.  
  918.       desc = read_rtx (infile);
  919.       if (GET_CODE (desc) == DEFINE_INSN)
  920.     gen_insn (desc);
  921.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  922.     gen_peephole (desc);
  923.       if (GET_CODE (desc) == DEFINE_EXPAND)
  924.     gen_expand (desc);
  925.       if (GET_CODE (desc) == DEFINE_SPLIT)
  926.     gen_split (desc);
  927.       next_index_number++;
  928.     }
  929.  
  930.   output_epilogue ();
  931.  
  932.   fflush (stdout);
  933.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  934. }
  935.  
  936. int
  937. n_occurrences (c, s)
  938.      char c;
  939.      char *s;
  940. {
  941.   int n = 0;
  942.   while (*s)
  943.     n += (*s++ == c);
  944.   return n;
  945. }
  946.