OSDN Git Service

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