OSDN Git Service

(write_units): Write #defines for the number of
[pf3gnuchains/gcc-fork.git] / gcc / genattr.c
1 /* Generate attribute information (insn-attr.h) from machine description.
2    Copyright (C) 1991 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@nyu.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21
22 #include <stdio.h>
23 #include "config.h"
24 #include "rtl.h"
25 #include "obstack.h"
26
27 static struct obstack obstack;
28 struct obstack *rtl_obstack = &obstack;
29
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
32
33 extern void free ();
34 extern int atoi ();
35 extern rtx read_rtx ();
36
37 char *xmalloc ();
38 static void fatal ();
39 void fancy_abort ();
40
41 struct namelist
42 {
43   struct namelist *next;
44   char *name;
45 };
46
47 static void
48 write_upcase (str)
49     char *str;
50 {
51   for (; *str; str++)
52     if (*str >= 'a' && *str <= 'z')
53       printf ("%c", *str - 'a' + 'A');
54     else
55       printf ("%c", *str);
56 }
57
58 static void
59 gen_attr (attr)
60      rtx attr;
61 {
62   char *p;
63
64   printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
65
66   /* If numeric attribute, don't need to write an enum.  */
67   if (*XSTR (attr, 1) == '\0')
68     printf ("extern int get_attr_%s ();\n", XSTR (attr, 0));
69   else
70     {
71       printf ("enum attr_%s {", XSTR (attr, 0));
72       write_upcase (XSTR (attr, 0));
73       printf ("_");
74
75       for (p = XSTR (attr, 1); *p != '\0'; p++)
76         {
77           if (*p == ',')
78             {
79               printf (", ");
80               write_upcase (XSTR (attr, 0));
81               printf ("_");
82             }
83           else if (*p >= 'a' && *p <= 'z')
84             printf ("%c", *p - 'a' + 'A');
85           else
86             printf ("%c", *p);
87         }
88
89       printf ("};\n");
90       printf ("extern enum attr_%s get_attr_%s ();\n\n",
91               XSTR (attr, 0), XSTR (attr, 0));
92     }
93
94   /* If `length' attribute, write additional function definitions and define
95      variables used by `insn_current_length'.  */
96   if (! strcmp (XSTR (attr, 0), "length"))
97     {
98       printf ("extern void init_lengths ();\n");
99       printf ("extern void shorten_branches ();\n");
100       printf ("extern int insn_default_length ();\n");
101       printf ("extern int insn_variable_length_p ();\n");
102       printf ("extern int insn_current_length ();\n\n");
103       printf ("extern int *insn_addresses;\n");
104       printf ("extern int insn_current_address;\n\n");
105     }
106 }
107
108 static void
109 write_units (num_units, min_ready_cost, max_ready_cost,
110              min_busy_delay, max_busy_delay)
111 {
112   printf ("#define INSN_SCHEDULING\n\n");
113   printf ("extern int result_ready_cost ();\n");
114   printf ("extern int function_units_used ();\n\n");
115   printf ("extern struct function_unit_desc\n");
116   printf ("{\n");
117   printf ("  char *name;\n");
118   printf ("  int bitmask;\n");
119   printf ("  int multiplicity;\n");
120   printf ("  int simultaneity;\n");
121   printf ("  int default_cost;\n");
122   printf ("  int max_busy_cost;\n");
123   printf ("  int (*ready_cost_function) ();\n");
124   printf ("  int (*conflict_cost_function) ();\n");
125   printf ("} function_units[];\n\n");
126   printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
127   printf ("#define MIN_READY_COST %d\n", min_ready_cost);
128   printf ("#define MAX_READY_COST %d\n", max_ready_cost);
129   printf ("#define MIN_BUSY_DELAY %d\n", min_busy_delay);
130   printf ("#define MAX_BUSY_DELAY %d\n\n", max_busy_delay);
131 }
132
133 char *
134 xmalloc (size)
135      unsigned size;
136 {
137   register char *val = (char *) malloc (size);
138
139   if (val == 0)
140     fatal ("virtual memory exhausted");
141   return val;
142 }
143
144 char *
145 xrealloc (ptr, size)
146      char *ptr;
147      unsigned size;
148 {
149   char * result = (char *) realloc (ptr, size);
150   if (!result)
151     fatal ("virtual memory exhausted");
152   return result;
153 }
154
155 static void
156 fatal (s, a1, a2)
157      char *s;
158 {
159   fprintf (stderr, "genattr: ");
160   fprintf (stderr, s, a1, a2);
161   fprintf (stderr, "\n");
162   exit (FATAL_EXIT_CODE);
163 }
164
165 /* More 'friendly' abort that prints the line and file.
166    config.h can #define abort fancy_abort if you like that sort of thing.  */
167
168 void
169 fancy_abort ()
170 {
171   fatal ("Internal gcc abort.");
172 }
173 \f
174 int
175 main (argc, argv)
176      int argc;
177      char **argv;
178 {
179   rtx desc;
180   FILE *infile;
181   register int c;
182   int have_delay = 0;
183   int have_annul_true = 0;
184   int have_annul_false = 0;
185   int have_units = 0;
186   int num_units = 0;
187   int min_ready_cost = 100000, max_ready_cost = -1;
188   int min_busy_delay = 100000, max_busy_delay = -1;
189   struct namelist *units = 0;
190   int i;
191
192   obstack_init (rtl_obstack);
193
194   if (argc <= 1)
195     fatal ("No input file name.");
196
197   infile = fopen (argv[1], "r");
198   if (infile == 0)
199     {
200       perror (argv[1]);
201       exit (FATAL_EXIT_CODE);
202     }
203
204   init_rtl ();
205
206   printf ("/* Generated automatically by the program `genattr'\n\
207 from the machine description file `md'.  */\n\n");
208
209   /* For compatibility, define the attribute `alternative', which is just
210      a reference to the variable `which_alternative'.  */
211
212   printf ("#define HAVE_ATTR_alternative\n");
213   printf ("#define get_attr_alternative(insn) which_alternative\n");
214      
215   /* Read the machine description.  */
216
217   while (1)
218     {
219       c = read_skip_spaces (infile);
220       if (c == EOF)
221         break;
222       ungetc (c, infile);
223
224       desc = read_rtx (infile);
225       if (GET_CODE (desc) == DEFINE_ATTR)
226         gen_attr (desc);
227
228       else if (GET_CODE (desc) == DEFINE_DELAY)
229         {
230           if (! have_delay)
231             {
232               printf ("#define DELAY_SLOTS\n");
233               printf ("extern int num_delay_slots ();\n");
234               printf ("extern int eligible_for_delay ();\n\n");
235               have_delay = 1;
236             }
237
238           for (i = 0; i < XVECLEN (desc, 1); i += 3)
239             {
240               if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
241                 {
242                   printf ("#define ANNUL_IFTRUE_SLOTS\n");
243                   printf ("extern int eligible_for_annul_true ();\n");
244                   have_annul_true = 1;
245                 }
246
247               if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
248                 {
249                   printf ("#define ANNUL_IFFALSE_SLOTS\n");
250                   printf ("extern int eligible_for_annul_false ();\n");
251                   have_annul_false = 1;
252                 }
253             }
254         }
255
256       else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
257         {
258           struct namelist *unit;
259           char *name = XSTR (desc, 0);
260           int ready_cost = XINT (desc, 4);
261           int busy_delay = XINT (desc, 5);
262
263           have_units = 1;
264           if (min_ready_cost > ready_cost) min_ready_cost = ready_cost;
265           if (max_ready_cost < ready_cost) max_ready_cost = ready_cost;
266           if (min_busy_delay > busy_delay) min_busy_delay = busy_delay;
267           if (max_busy_delay < busy_delay) max_busy_delay = busy_delay;
268
269           /* If the optional conflict vector was specified, the busy delay
270              may be zero.  */
271           if (XVEC (desc, 6) != 0) min_busy_delay = 0;
272
273           for (unit = units; unit; unit = unit->next)
274             if (strcmp (unit->name, name) == 0)
275               break;
276           if (unit == 0)
277             {
278               int len = strlen (name) + 1;
279               unit = (struct namelist *) alloca (sizeof (struct namelist));
280               unit->name = (char *) alloca (len);
281               bcopy (name, unit->name, len);
282               unit->next = units;
283               units = unit;
284               num_units++;
285             }
286         }
287     }
288
289   if (have_units)
290     write_units (num_units, min_ready_cost, max_ready_cost,
291                  min_busy_delay, max_busy_delay);
292
293   fflush (stdout);
294   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
295   /* NOTREACHED */
296   return 0;
297 }