OSDN Git Service

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