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