OSDN Git Service

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