OSDN Git Service

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