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

  1. /* Generate attribute information (insn-attr.h) from machine description.
  2.    Copyright (C) 1989 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.  
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "rtl.h"
  25. #include "obstack.h"
  26.  
  27. struct obstack obstack;
  28. struct obstack *rtl_obstack = &obstack;
  29.  
  30. #define obstack_chunk_alloc xmalloc
  31. #define obstack_chunk_free free
  32. extern int xmalloc ();
  33. extern void free ();
  34.  
  35. extern int atoi ();
  36.  
  37. void fatal ();
  38. void fancy_abort ();
  39.  
  40. void
  41. write_upcase (str)
  42.     char *str;
  43. {
  44.   for (; *str; str++)
  45.     if (*str >= 'a' && *str <= 'z')
  46.       printf ("%c", *str - 'a' + 'A');
  47.     else
  48.       printf ("%c", *str);
  49. }
  50.  
  51. void
  52. gen_attr (attr)
  53.      rtx attr;
  54. {
  55.   char *p;
  56.  
  57.   printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
  58.  
  59.   /* If numeric attribute, don't need to write an enum.  */
  60.   if (*XSTR (attr, 1) == '\0')
  61.     printf ("extern int get_attr_%s ();\n", XSTR (attr, 0));
  62.   else
  63.     {
  64.       printf ("enum attr_%s {", XSTR (attr, 0));
  65.       write_upcase (XSTR (attr, 0));
  66.       printf ("_");
  67.  
  68.       for (p = XSTR (attr, 1); *p != '\0'; p++)
  69.     {
  70.       if (*p == ',')
  71.         {
  72.           printf (", ");
  73.           write_upcase (XSTR (attr, 0));
  74.           printf ("_");
  75.         }
  76.       else if (*p >= 'a' && *p <= 'z')
  77.         printf ("%c", *p - 'a' + 'A');
  78.       else
  79.         printf ("%c", *p);
  80.     }
  81.  
  82.       printf ("};\n");
  83.       printf ("extern enum attr_%s get_attr_%s ();\n\n",
  84.           XSTR (attr, 0), XSTR (attr, 0));
  85.     }
  86.  
  87.   /* If `length' attribute, write additional function definitions and define
  88.      variables used by `insn_current_length'.  */
  89.   if (! strcmp (XSTR (attr, 0), "length"))
  90.     {
  91.       printf ("extern void init_lengths ();\n");
  92.       printf ("extern void shorten_branches ();\n");
  93.       printf ("extern int insn_default_length ();\n");
  94.       printf ("extern int insn_variable_length_p ();\n");
  95.       printf ("extern int insn_current_length ();\n\n");
  96.       printf ("extern int *insn_addresses;\n");
  97.       printf ("extern int insn_current_address;\n\n");
  98.     }
  99. }
  100.  
  101. void
  102. write_units ()
  103. {
  104.   printf ("#define INSN_SCHEDULING\n\n");
  105.   printf ("extern int result_ready_cost ();\n");
  106.   printf ("extern int function_units_used ();\n\n");
  107.   printf ("extern struct function_unit_desc\n");
  108.   printf ("{\n");
  109.   printf ("  char *name;\n");
  110.   printf ("  int bitmask;\n");
  111.   printf ("  int multiplicity;\n");
  112.   printf ("  int simultaneity;\n");
  113.   printf ("  int default_cost;\n");
  114.   printf ("  int (*ready_cost_function) ();\n");
  115.   printf ("  int (*conflict_cost_function) ();\n");
  116.   printf ("} function_units[];\n\n");
  117. }
  118.  
  119. int
  120. xmalloc (size)
  121. {
  122.   register int val = malloc (size);
  123.  
  124.   if (val == 0)
  125.     fatal ("virtual memory exhausted");
  126.   return val;
  127. }
  128.  
  129. int
  130. xrealloc (ptr, size)
  131.      char *ptr;
  132.      int size;
  133. {
  134.   int result = realloc (ptr, size);
  135.   if (!result)
  136.     fatal ("virtual memory exhausted");
  137.   return result;
  138. }
  139.  
  140. void
  141. fatal (s, a1, a2)
  142.      char *s;
  143. {
  144.   fprintf (stderr, "genattr: ");
  145.   fprintf (stderr, s, a1, a2);
  146.   fprintf (stderr, "\n");
  147.   exit (FATAL_EXIT_CODE);
  148. }
  149.  
  150. /* More 'friendly' abort that prints the line and file.
  151.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  152.  
  153. void
  154. fancy_abort ()
  155. {
  156.   fatal ("Internal gcc abort.");
  157. }
  158.  
  159. int
  160. main (argc, argv)
  161.      int argc;
  162.      char **argv;
  163. {
  164.   rtx desc;
  165.   FILE *infile;
  166.   extern rtx read_rtx ();
  167.   register int c;
  168.   int have_delay = 0;
  169.   int have_annul_true = 0;
  170.   int have_annul_false = 0;
  171.   int have_units = 0;
  172.   int i;
  173.  
  174.   obstack_init (rtl_obstack);
  175.  
  176.   if (argc <= 1)
  177.     fatal ("No input file name.");
  178.  
  179.   infile = fopen (argv[1], "r");
  180.   if (infile == 0)
  181.     {
  182.       perror (argv[1]);
  183.       exit (FATAL_EXIT_CODE);
  184.     }
  185.  
  186.   init_rtl ();
  187.  
  188.   printf ("/* Generated automatically by the program `genattr'\n\
  189. from the machine description file `md'.  */\n\n");
  190.  
  191.   /* For compatibility, define the attribute `alternative', which is just
  192.      a reference to the variable `which_alternative'.  *.
  193.  
  194.   printf ("#define HAVE_ATTR_alternative\n");
  195.   printf ("#define get_attr_alternative(insn) which_alternative\n");
  196.      
  197.   /* Read the machine description.  */
  198.  
  199.   while (1)
  200.     {
  201.       c = read_skip_spaces (infile);
  202.       if (c == EOF)
  203.     break;
  204.       ungetc (c, infile);
  205.  
  206.       desc = read_rtx (infile);
  207.       if (GET_CODE (desc) == DEFINE_ATTR)
  208.     gen_attr (desc);
  209.  
  210.       else if (GET_CODE (desc) == DEFINE_DELAY)
  211.         {
  212.       if (! have_delay)
  213.         {
  214.           printf ("#define DELAY_SLOTS\n");
  215.           printf ("extern int num_delay_slots ();\n");
  216.           printf ("extern int eligible_for_delay ();\n\n");
  217.           have_delay = 1;
  218.         }
  219.  
  220.       for (i = 0; i < XVECLEN (desc, 1); i += 3)
  221.         {
  222.           if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
  223.         {
  224.           printf ("#define ANNUL_IFTRUE_SLOTS\n");
  225.           printf ("extern int eligible_for_annul_true ();\n");
  226.           have_annul_true = 1;
  227.         }
  228.  
  229.           if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
  230.         {
  231.           printf ("#define ANNUL_IFFALSE_SLOTS\n");
  232.           printf ("extern int eligible_for_annul_false ();\n");
  233.           have_annul_false = 1;
  234.         }
  235.         }
  236.         }
  237.  
  238.       else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT && ! have_units)
  239.     {
  240.       have_units = 1;
  241.       write_units ();
  242.     }
  243.     }
  244.  
  245.   fflush (stdout);
  246.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  247. }
  248.  
  249.