home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / genemit.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  21KB  |  799 lines

  1. /* Generate code from machine description to emit insns as rtl.
  2.    Copyright (C) 1987, 1988, 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. #include <stdio.h>
  22. #include "hconfig.h"
  23. #include "rtl.h"
  24. #include "obstack.h"
  25.  
  26. static struct obstack obstack;
  27. struct obstack *rtl_obstack = &obstack;
  28.  
  29. #define obstack_chunk_alloc xmalloc
  30. #define obstack_chunk_free free
  31.  
  32. extern void free ();
  33. extern rtx read_rtx ();
  34.  
  35. char *xmalloc ();
  36. static void fatal ();
  37. void fancy_abort ();
  38.  
  39. static int max_opno;
  40. static int max_dup_opno;
  41. static int register_constraints;
  42. static int insn_code_number;
  43. static int insn_index_number;
  44.  
  45. /* Data structure for recording the patterns of insns that have CLOBBERs.
  46.    We use this to output a function that adds these CLOBBERs to a 
  47.    previously-allocated PARALLEL expression.  */
  48.  
  49. struct clobber_pat
  50. {
  51.   struct clobber_ent *insns;
  52.   rtx pattern;
  53.   int first_clobber;
  54.   struct clobber_pat *next;
  55. } *clobber_list;
  56.  
  57. /* Records one insn that uses the clobber list.  */
  58.  
  59. struct clobber_ent
  60. {
  61.   int code_number;        /* Counts only insns.  */
  62.   struct clobber_ent *next;
  63. };
  64.  
  65. static void
  66. max_operand_1 (x)
  67.      rtx x;
  68. {
  69.   register RTX_CODE code;
  70.   register int i;
  71.   register int len;
  72.   register char *fmt;
  73.  
  74.   if (x == 0)
  75.     return;
  76.  
  77.   code = GET_CODE (x);
  78.  
  79.   if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
  80.     register_constraints = 1;
  81.   if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
  82.     register_constraints = 1;
  83.   if (code == MATCH_OPERAND || code == MATCH_OPERATOR
  84.       || code == MATCH_PARALLEL)
  85.     max_opno = MAX (max_opno, XINT (x, 0));
  86.   if (code == MATCH_DUP || code == MATCH_OP_DUP || code == MATCH_PAR_DUP)
  87.     max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
  88.  
  89.   fmt = GET_RTX_FORMAT (code);
  90.   len = GET_RTX_LENGTH (code);
  91.   for (i = 0; i < len; i++)
  92.     {
  93.       if (fmt[i] == 'e' || fmt[i] == 'u')
  94.     max_operand_1 (XEXP (x, i));
  95.       else if (fmt[i] == 'E')
  96.     {
  97.       int j;
  98.       for (j = 0; j < XVECLEN (x, i); j++)
  99.         max_operand_1 (XVECEXP (x, i, j));
  100.     }
  101.     }
  102. }
  103.  
  104. static int
  105. max_operand_vec (insn, arg)
  106.      rtx insn;
  107.      int arg;
  108. {
  109.   register int len = XVECLEN (insn, arg);
  110.   register int i;
  111.  
  112.   max_opno = -1;
  113.   max_dup_opno = -1;
  114.  
  115.   for (i = 0; i < len; i++)
  116.     max_operand_1 (XVECEXP (insn, arg, i));
  117.  
  118.   return max_opno + 1;
  119. }
  120.  
  121. static void
  122. print_code (code)
  123.      RTX_CODE code;
  124. {
  125.   register char *p1;
  126.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  127.     {
  128.       if (*p1 >= 'a' && *p1 <= 'z')
  129.     putchar (*p1 + 'A' - 'a');
  130.       else
  131.     putchar (*p1);
  132.     }
  133. }
  134.  
  135. /* Print a C expression to construct an RTX just like X,
  136.    substituting any operand references appearing within.  */
  137.  
  138. static void
  139. gen_exp (x)
  140.      rtx x;
  141. {
  142.   register RTX_CODE code;
  143.   register int i;
  144.   register int len;
  145.   register char *fmt;
  146.  
  147.   if (x == 0)
  148.     {
  149.       printf ("NULL_RTX");
  150.       return;
  151.     }
  152.  
  153.   code = GET_CODE (x);
  154.  
  155.   switch (code)
  156.     {
  157.     case MATCH_OPERAND:
  158.     case MATCH_DUP:
  159.       printf ("operand%d", XINT (x, 0));
  160.       return;
  161.  
  162.     case MATCH_OP_DUP:
  163.       printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
  164.           XINT (x, 0), XINT (x, 0));
  165.       for (i = 0; i < XVECLEN (x, 1); i++)
  166.     {
  167.       printf (",\n\t\t");
  168.       gen_exp (XVECEXP (x, 1, i));
  169.     }
  170.       printf (")");
  171.       return;
  172.  
  173.     case MATCH_OPERATOR:
  174.       printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
  175.       printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  176.       for (i = 0; i < XVECLEN (x, 2); i++)
  177.     {
  178.       printf (",\n\t\t");
  179.       gen_exp (XVECEXP (x, 2, i));
  180.     }
  181.       printf (")");
  182.       return;
  183.  
  184.     case MATCH_PARALLEL:
  185.     case MATCH_PAR_DUP:
  186.       printf ("operand%d", XINT (x, 0));
  187.       return;
  188.  
  189.     case MATCH_SCRATCH:
  190.       printf ("gen_rtx (SCRATCH, %smode, 0)", GET_MODE_NAME (GET_MODE (x)));
  191.       return;
  192.  
  193.     case ADDRESS:
  194.       fatal ("ADDRESS expression code used in named instruction pattern");
  195.  
  196.     case PC:
  197.       printf ("pc_rtx");
  198.       return;
  199.  
  200.     case CC0:
  201.       printf ("cc0_rtx");
  202.       return;
  203.  
  204.     case CONST_INT:
  205.       if (INTVAL (x) == 0)
  206.     printf ("const0_rtx");
  207.       else if (INTVAL (x) == 1)
  208.     printf ("const1_rtx");
  209.       else if (INTVAL (x) == -1)
  210.     printf ("constm1_rtx");
  211.       else if (INTVAL (x) == STORE_FLAG_VALUE)
  212.     printf ("const_true_rtx");
  213.       else
  214.     printf (
  215. #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT         
  216.         "GEN_INT (%d)",
  217. #else
  218.         "GEN_INT (%ld)",
  219. #endif
  220.         INTVAL (x));
  221.       return;
  222.  
  223.     case CONST_DOUBLE:
  224.       /* These shouldn't be written in MD files.  Instead, the appropriate
  225.      routines in varasm.c should be called.  */
  226.       abort ();
  227.     }
  228.  
  229.   printf ("gen_rtx (");
  230.   print_code (code);
  231.   printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  232.  
  233.   fmt = GET_RTX_FORMAT (code);
  234.   len = GET_RTX_LENGTH (code);
  235.   for (i = 0; i < len; i++)
  236.     {
  237.       if (fmt[i] == '0')
  238.     break;
  239.       printf (", ");
  240.       if (fmt[i] == 'e' || fmt[i] == 'u')
  241.     gen_exp (XEXP (x, i));
  242.       else if (fmt[i] == 'i')
  243.     printf ("%u", XINT (x, i));
  244.       else if (fmt[i] == 's')
  245.     printf ("\"%s\"", XSTR (x, i));
  246.       else if (fmt[i] == 'E')
  247.     {
  248.       int j;
  249.       printf ("gen_rtvec (%d", XVECLEN (x, i));
  250.       for (j = 0; j < XVECLEN (x, i); j++)
  251.         {
  252.           printf (",\n\t\t");
  253.           gen_exp (XVECEXP (x, i, j));
  254.         }
  255.       printf (")");
  256.     }
  257.       else
  258.     abort ();
  259.     }
  260.   printf (")");
  261. }  
  262.  
  263. /* Generate the `gen_...' function for a DEFINE_INSN.  */
  264.  
  265. static void
  266. gen_insn (insn)
  267.      rtx insn;
  268. {
  269.   int operands;
  270.   register int i;
  271.  
  272.   /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
  273.      registers or MATCH_SCRATCHes.  If so, store away the information for
  274.      later. */
  275.  
  276.   if (XVEC (insn, 1))
  277.     {
  278.       for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
  279.     if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
  280.         || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
  281.         && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
  282.       break;
  283.  
  284.       if (i != XVECLEN (insn, 1) - 1)
  285.     {
  286.       register struct clobber_pat *p;
  287.       register struct clobber_ent *link
  288.         = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
  289.       register int j;
  290.  
  291.       link->code_number = insn_code_number;
  292.  
  293.       /* See if any previous CLOBBER_LIST entry is the same as this
  294.          one.  */
  295.  
  296.       for (p = clobber_list; p; p = p->next)
  297.         {
  298.           if (p->first_clobber != i + 1
  299.           || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
  300.         continue;
  301.  
  302.           for (j = i + 1; j < XVECLEN (insn, 1); j++)
  303.         {
  304.           rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
  305.           rtx new = XEXP (XVECEXP (insn, 1, j), 0);
  306.  
  307.           /* OLD and NEW are the same if both are to be a SCRATCH
  308.              of the same mode, 
  309.              or if both are registers of the same mode and number.  */
  310.           if (! (GET_MODE (old) == GET_MODE (new)
  311.              && ((GET_CODE (old) == MATCH_SCRATCH
  312.                   && GET_CODE (new) == MATCH_SCRATCH)
  313.                  || (GET_CODE (old) == REG && GET_CODE (new) == REG
  314.                  && REGNO (old) == REGNO (new)))))
  315.             break;
  316.         }
  317.       
  318.           if (j == XVECLEN (insn, 1))
  319.         break;
  320.         }
  321.  
  322.       if (p == 0)
  323.         {
  324.           p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
  325.       
  326.           p->insns = 0;
  327.           p->pattern = insn;
  328.           p->first_clobber = i + 1;
  329.           p->next = clobber_list;
  330.           clobber_list = p;
  331.         }
  332.  
  333.       link->next = p->insns;
  334.       p->insns = link;
  335.     }
  336.     }
  337.  
  338.   /* Don't mention instructions whose names are the null string.
  339.      They are in the machine description just to be recognized.  */
  340.   if (strlen (XSTR (insn, 0)) == 0)
  341.     return;
  342.  
  343.   /* Find out how many operands this function has,
  344.      and also whether any of them have register constraints.  */
  345.   register_constraints = 0