OSDN Git Service

Fix mips64vr4100-elf build failure.
[pf3gnuchains/gcc-fork.git] / gcc / genattr.c
1 /* Generate attribute information (insn-attr.h) from machine description.
2    Copyright (C) 1991, 1994, 1996, 1998 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@vlsi1.ultra.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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "hconfig.h"
24 #ifdef __STDC__
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include "system.h"
30 #include "rtl.h"
31 #include "obstack.h"
32
33 static struct obstack obstack;
34 struct obstack *rtl_obstack = &obstack;
35
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
38
39 char *xmalloc PROTO((unsigned));
40 static void fatal PVPROTO ((char *, ...)) ATTRIBUTE_PRINTF_1;
41 void fancy_abort PROTO((void));
42
43 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
44 char **insn_name_ptr = 0;
45
46 /* A range of values.  */
47
48 struct range
49 {
50   int min;
51   int max;
52 };
53
54 /* Record information about each function unit mentioned in a
55    DEFINE_FUNCTION_UNIT.  */
56
57 struct function_unit
58 {
59   char *name;                   /* Function unit name.  */
60   struct function_unit *next;   /* Next function unit.  */
61   int multiplicity;             /* Number of units of this type.  */
62   int simultaneity;             /* Maximum number of simultaneous insns
63                                    on this function unit or 0 if unlimited.  */
64   struct range ready_cost;      /* Range of ready cost values.  */
65   struct range issue_delay;     /* Range of issue delay values.  */
66 };
67
68 static void extend_range PROTO((struct range *, int, int));
69 static void init_range PROTO((struct range *));
70 static void write_upcase PROTO((char *));
71 static void gen_attr PROTO((rtx));
72 static void write_units PROTO((int, struct range *, struct range *,
73                                struct range *, struct range *,
74                                struct range *));
75 static void
76 extend_range (range, min, max)
77      struct range *range;
78      int min;
79      int max;
80 {
81   if (range->min > min) range->min = min;
82   if (range->max < max) range->max = max;
83 }
84
85 static void
86 init_range (range)
87      struct range *range;
88 {
89   range->min = 100000;
90   range->max = -1;
91 }
92
93 static void
94 write_upcase (str)
95     char *str;
96 {
97   for (; *str; str++)
98     if (*str >= 'a' && *str <= 'z')
99       printf ("%c", *str - 'a' + 'A');
100     else
101       printf ("%c", *str);
102 }
103
104 static void
105 gen_attr (attr)
106      rtx attr;
107 {
108   char *p;
109
110   printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
111
112   /* If numeric attribute, don't need to write an enum.  */
113   if (*XSTR (attr, 1) == '\0')
114     printf ("extern int get_attr_%s ();\n", XSTR (attr, 0));
115   else
116     {
117       printf ("enum attr_%s {", XSTR (attr, 0));
118       write_upcase (XSTR (attr, 0));
119       printf ("_");
120
121       for (p = XSTR (attr, 1); *p != '\0'; p++)
122         {
123           if (*p == ',')
124             {
125               printf (", ");
126               write_upcase (XSTR (attr, 0));
127               printf ("_");
128             }
129           else if (*p >= 'a' && *p <= 'z')
130             printf ("%c", *p - 'a' + 'A');
131           else
132             printf ("%c", *p);
133         }
134
135       printf ("};\n");
136       printf ("extern enum attr_%s get_attr_%s ();\n\n",
137               XSTR (attr, 0), XSTR (attr, 0));
138     }
139
140   /* If `length' attribute, write additional function definitions and define
141      variables used by `insn_current_length'.  */
142   if (! strcmp (XSTR (attr, 0), "length"))
143     {
144       printf ("extern void init_lengths ();\n");
145       printf ("extern void shorten_branches PROTO((rtx));\n");
146       printf ("extern int insn_default_length PROTO((rtx));\n");
147       printf ("extern int insn_variable_length_p PROTO((rtx));\n");
148       printf ("extern int insn_current_length PROTO((rtx));\n\n");
149       printf ("extern int *insn_addresses;\n");
150       printf ("extern int insn_current_address;\n\n");
151     }
152 }
153
154 static void
155 write_units (num_units, multiplicity, simultaneity,
156              ready_cost, issue_delay, blockage)
157      int num_units;
158      struct range *multiplicity;
159      struct range *simultaneity;
160      struct range *ready_cost;
161      struct range *issue_delay;
162      struct range *blockage;
163 {
164   int i, q_size;
165
166   printf ("#define INSN_SCHEDULING\n\n");
167   printf ("extern int result_ready_cost PROTO((rtx));\n");
168   printf ("extern int function_units_used PROTO((rtx));\n\n");
169   printf ("extern struct function_unit_desc\n");
170   printf ("{\n");
171   printf ("  char *name;\n");
172   printf ("  int bitmask;\n");
173   printf ("  int multiplicity;\n");
174   printf ("  int simultaneity;\n");
175   printf ("  int default_cost;\n");
176   printf ("  int max_issue_delay;\n");
177   printf ("  int (*ready_cost_function) ();\n");
178   printf ("  int (*conflict_cost_function) ();\n");
179   printf ("  int max_blockage;\n");
180   printf ("  unsigned int (*blockage_range_function) ();\n");
181   printf ("  int (*blockage_function) ();\n");
182   printf ("} function_units[];\n\n");
183   printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
184   printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
185   printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
186   printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
187   printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
188   printf ("#define MIN_READY_COST %d\n", ready_cost->min);
189   printf ("#define MAX_READY_COST %d\n", ready_cost->max);
190   printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
191   printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
192   printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
193   printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
194   for (i = 0; (1 << i) < blockage->max; i++)
195     ;
196   printf ("#define BLOCKAGE_BITS %d\n", i + 1);
197
198   /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
199      MAX_READY_COST.  This is the longest time an isnsn may be queued.  */
200   i = MAX (blockage->max, ready_cost->max);
201   for (q_size = 1; q_size <= i; q_size <<= 1)
202     ;
203   printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
204 }
205
206 char *
207 xmalloc (size)
208      unsigned size;
209 {
210   register char *val = (char *) malloc (size);
211
212   if (val == 0)
213     fatal ("virtual memory exhausted");
214   return val;
215 }
216
217 char *
218 xrealloc (ptr, size)
219      char *ptr;
220      unsigned size;
221 {
222   char * result = (char *) realloc (ptr, size);
223   if (!result)
224     fatal ("virtual memory exhausted");
225   return result;
226 }
227
228 static void
229 fatal VPROTO ((char *format, ...))
230 {
231 #ifndef __STDC__
232   char *format;
233 #endif
234   va_list ap;
235
236   VA_START (ap, format);
237
238 #ifndef __STDC__
239   format = va_arg (ap, char *);
240 #endif
241
242   fprintf (stderr, "genattr: ");
243   vfprintf (stderr, format, ap);
244   va_end (ap);
245   fprintf (stderr, "\n");
246   exit (FATAL_EXIT_CODE);
247 }
248
249 /* More 'friendly' abort that prints the line and file.
250    config.h can #define abort fancy_abort if you like that sort of thing.  */
251
252 void
253 fancy_abort ()
254 {
255   fatal ("Internal gcc abort.");
256 }
257 \f
258 int
259 main (argc, argv)
260      int argc;
261      char **argv;
262 {
263   rtx desc;
264   FILE *infile;
265   register int c;
266   int have_delay = 0;
267   int have_annul_true = 0;
268   int have_annul_false = 0;
269   int num_units = 0;
270   struct range all_simultaneity, all_multiplicity;
271   struct range all_ready_cost, all_issue_delay, all_blockage;
272   struct function_unit *units = 0, *unit;
273   int i;
274
275   init_range (&all_multiplicity);
276   init_range (&all_simultaneity);
277   init_range (&all_ready_cost);
278   init_range (&all_issue_delay);
279   init_range (&all_blockage);
280
281   obstack_init (rtl_obstack);
282
283   if (argc <= 1)
284     fatal ("No input file name.");
285
286   infile = fopen (argv[1], "r");
287   if (infile == 0)
288     {
289       perror (argv[1]);
290       exit (FATAL_EXIT_CODE);
291     }
292
293   init_rtl ();
294
295   printf ("/* Generated automatically by the program `genattr'\n\
296 from the machine description file `md'.  */\n\n");
297
298   /* For compatibility, define the attribute `alternative', which is just
299      a reference to the variable `which_alternative'.  */
300
301   printf ("#define HAVE_ATTR_alternative\n");
302   printf ("#define get_attr_alternative(insn) which_alternative\n");
303      
304   /* Read the machine description.  */
305
306   while (1)
307     {
308       c = read_skip_spaces (infile);
309       if (c == EOF)
310         break;
311       ungetc (c, infile);
312
313       desc = read_rtx (infile);
314       if (GET_CODE (desc) == DEFINE_ATTR)
315         gen_attr (desc);
316
317       else if (GET_CODE (desc) == DEFINE_DELAY)
318         {
319           if (! have_delay)
320             {
321               printf ("#define DELAY_SLOTS\n");
322               printf ("extern int num_delay_slots PROTO((rtx));\n");
323               printf ("extern int eligible_for_delay PROTO((rtx, int, rtx, int));\n\n");
324               printf ("extern int const_num_delay_slots PROTO((rtx));\n\n");
325               have_delay = 1;
326             }
327
328           for (i = 0; i < XVECLEN (desc, 1); i += 3)
329             {
330               if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
331                 {
332                   printf ("#define ANNUL_IFTRUE_SLOTS\n");
333                   printf ("extern int eligible_for_annul_true ();\n");
334                   have_annul_true = 1;
335                 }
336
337               if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
338                 {
339                   printf ("#define ANNUL_IFFALSE_SLOTS\n");
340                   printf ("extern int eligible_for_annul_false ();\n");
341                   have_annul_false = 1;
342                 }
343             }
344         }
345
346       else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
347         {
348           char *name = XSTR (desc, 0);
349           int multiplicity = XINT (desc, 1);
350           int simultaneity = XINT (desc, 2);
351           int ready_cost = MAX (XINT (desc, 4), 1);
352           int issue_delay = MAX (XINT (desc, 5), 1);
353           int issueexp_p = (XVEC (desc, 6) != 0);
354
355           for (unit = units; unit; unit = unit->next)
356             if (strcmp (unit->name, name) == 0)
357               break;
358
359           if (unit == 0)
360             {
361               int len = strlen (name) + 1;
362               unit = (struct function_unit *)
363                 alloca (sizeof (struct function_unit));
364               unit->name = (char *) alloca (len);
365               bcopy (name, unit->name, len);
366               unit->multiplicity = multiplicity;
367               unit->simultaneity = simultaneity;
368               unit->ready_cost.min = unit->ready_cost.max = ready_cost;
369               unit->issue_delay.min = unit->issue_delay.max = issue_delay;
370               unit->next = units;
371               units = unit;
372               num_units++;
373
374               extend_range (&all_multiplicity, multiplicity, multiplicity);
375               extend_range (&all_simultaneity, simultaneity, simultaneity);
376             }
377           else if (unit->multiplicity != multiplicity
378                    || unit->simultaneity != simultaneity)
379             fatal ("Differing specifications given for `%s' function unit.",
380                    unit->name);
381
382           extend_range (&unit->ready_cost, ready_cost, ready_cost);
383           extend_range (&unit->issue_delay,
384                         issueexp_p ? 1 : issue_delay, issue_delay);
385           extend_range (&all_ready_cost,
386                         unit->ready_cost.min, unit->ready_cost.max);
387           extend_range (&all_issue_delay,
388                         unit->issue_delay.min, unit->issue_delay.max);
389         }
390     }
391
392   if (num_units > 0)
393     {
394       /* Compute the range of blockage cost values.  See genattrtab.c
395          for the derivation.  BLOCKAGE (E,C) when SIMULTANEITY is zero is
396
397              MAX (ISSUE-DELAY (E,C),
398                   READY-COST (E) - (READY-COST (C) - 1))
399
400          and otherwise
401
402              MAX (ISSUE-DELAY (E,C),
403                   READY-COST (E) - (READY-COST (C) - 1),
404                   READY-COST (E) - FILL-TIME)  */
405
406       for (unit = units; unit; unit = unit->next)
407         {
408           struct range blockage;
409
410           blockage = unit->issue_delay;
411           blockage.max = MAX (unit->ready_cost.max
412                               - (unit->ready_cost.min - 1),
413                               blockage.max);
414           blockage.min = MAX (1, blockage.min);
415
416           if (unit->simultaneity != 0)
417             {
418               int fill_time = ((unit->simultaneity - 1)
419                                * unit->issue_delay.min);
420               blockage.min = MAX (unit->ready_cost.min - fill_time,
421                                   blockage.min);
422               blockage.max = MAX (unit->ready_cost.max - fill_time,
423                                   blockage.max);
424             }
425           extend_range (&all_blockage, blockage.min, blockage.max);
426         }
427
428       write_units (num_units, &all_multiplicity, &all_simultaneity,
429                    &all_ready_cost, &all_issue_delay, &all_blockage);
430     }
431
432   /* Output flag masks for use by reorg.  
433
434      Flags are used to hold branch direction and prediction information
435      for use by eligible_for_...  */
436   printf("\n#define ATTR_FLAG_forward\t0x1\n");
437   printf("#define ATTR_FLAG_backward\t0x2\n");
438   printf("#define ATTR_FLAG_likely\t0x4\n");
439   printf("#define ATTR_FLAG_very_likely\t0x8\n");
440   printf("#define ATTR_FLAG_unlikely\t0x10\n");
441   printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
442
443   fflush (stdout);
444   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
445   /* NOTREACHED */
446   return 0;
447 }