Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / genattr.c
blob92bb469ab91ce37d5f48a3b18c5d37698f3fb804
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)
5 This file is part of GNU CC.
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.
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.
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. */
23 #include "hconfig.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "obstack.h"
28 static struct obstack obstack;
29 struct obstack *rtl_obstack = &obstack;
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free free
34 extern rtx read_rtx PROTO((FILE *));
36 char *xmalloc PROTO((unsigned));
38 #ifdef HAVE_VPRINTF
39 void fatal PVPROTO((char *, ...));
40 #else
41 /* We must not provide any prototype here, even if ANSI C. */
42 void fatal PROTO(());
43 #endif
45 void fancy_abort ();
47 /* A range of values. */
49 struct range
51 int min;
52 int max;
55 /* Record information about each function unit mentioned in a
56 DEFINE_FUNCTION_UNIT. */
58 struct function_unit
60 char *name; /* Function unit name. */
61 struct function_unit *next; /* Next function unit. */
62 int multiplicity; /* Number of units of this type. */
63 int simultaneity; /* Maximum number of simultaneous insns
64 on this function unit or 0 if unlimited. */
65 struct range ready_cost; /* Range of ready cost values. */
66 struct range issue_delay; /* Range of issue delay values. */
69 static void
70 extend_range (range, min, max)
71 struct range *range;
72 int min;
73 int max;
75 if (range->min > min) range->min = min;
76 if (range->max < max) range->max = max;
79 static void
80 init_range (range)
81 struct range *range;
83 range->min = 100000;
84 range->max = -1;
87 static void
88 write_upcase (str)
89 char *str;
91 for (; *str; str++)
92 if (*str >= 'a' && *str <= 'z')
93 printf ("%c", *str - 'a' + 'A');
94 else
95 printf ("%c", *str);
98 static void
99 gen_attr (attr)
100 rtx attr;
102 char *p;
104 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
106 /* If numeric attribute, don't need to write an enum. */
107 if (*XSTR (attr, 1) == '\0')
108 printf ("extern int get_attr_%s ();\n", XSTR (attr, 0));
109 else
111 printf ("enum attr_%s {", XSTR (attr, 0));
112 write_upcase (XSTR (attr, 0));
113 printf ("_");
115 for (p = XSTR (attr, 1); *p != '\0'; p++)
117 if (*p == ',')
119 printf (", ");
120 write_upcase (XSTR (attr, 0));
121 printf ("_");
123 else if (*p >= 'a' && *p <= 'z')
124 printf ("%c", *p - 'a' + 'A');
125 else
126 printf ("%c", *p);
129 printf ("};\n");
130 printf ("extern enum attr_%s get_attr_%s ();\n\n",
131 XSTR (attr, 0), XSTR (attr, 0));
134 /* If `length' attribute, write additional function definitions and define
135 variables used by `insn_current_length'. */
136 if (! strcmp (XSTR (attr, 0), "length"))
138 printf ("extern void init_lengths ();\n");
139 printf ("extern void shorten_branches PROTO((rtx));\n");
140 printf ("extern int insn_default_length PROTO((rtx));\n");
141 printf ("extern int insn_variable_length_p PROTO((rtx));\n");
142 printf ("extern int insn_current_length PROTO((rtx));\n\n");
143 printf ("extern int *insn_addresses;\n");
144 printf ("extern int insn_current_address;\n\n");
148 static void
149 write_units (num_units, multiplicity, simultaneity,
150 ready_cost, issue_delay, blockage)
151 int num_units;
152 struct range *multiplicity;
153 struct range *simultaneity;
154 struct range *ready_cost;
155 struct range *issue_delay;
156 struct range *blockage;
158 int i, q_size;
160 printf ("#define INSN_SCHEDULING\n\n");
161 printf ("extern int result_ready_cost PROTO((rtx));\n");
162 printf ("extern int function_units_used PROTO((rtx));\n\n");
163 printf ("extern struct function_unit_desc\n");
164 printf ("{\n");
165 printf (" char *name;\n");
166 printf (" int bitmask;\n");
167 printf (" int multiplicity;\n");
168 printf (" int simultaneity;\n");
169 printf (" int default_cost;\n");
170 printf (" int max_issue_delay;\n");
171 printf (" int (*ready_cost_function) ();\n");
172 printf (" int (*conflict_cost_function) ();\n");
173 printf (" int max_blockage;\n");
174 printf (" unsigned int (*blockage_range_function) ();\n");
175 printf (" int (*blockage_function) ();\n");
176 printf ("} function_units[];\n\n");
177 printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
178 printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
179 printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
180 printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
181 printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
182 printf ("#define MIN_READY_COST %d\n", ready_cost->min);
183 printf ("#define MAX_READY_COST %d\n", ready_cost->max);
184 printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
185 printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
186 printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
187 printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
188 for (i = 0; (1 << i) < blockage->max; i++)
190 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
192 /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
193 MAX_READY_COST. This is the longest time an isnsn may be queued. */
194 i = MAX (blockage->max, ready_cost->max);
195 for (q_size = 1; q_size <= i; q_size <<= 1)
197 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
200 char *
201 xmalloc (size)
202 unsigned size;
204 register char *val = (char *) malloc (size);
206 if (val == 0)
207 fatal ("virtual memory exhausted");
208 return val;
211 char *
212 xrealloc (ptr, size)
213 char *ptr;
214 unsigned size;
216 char * result = (char *) realloc (ptr, size);
217 if (!result)
218 fatal ("virtual memory exhausted");
219 return result;
222 #ifdef HAVE_VPRINTF
224 void
225 fatal VPROTO((char *s, ...))
227 #ifndef ANSI_PROTOTYPES
228 char *s;
229 #endif
230 va_list ap;
232 VA_START (ap, s);
234 #ifndef ANSI_PROTOTYPES
235 s = va_arg (ap, char *);
236 #endif
238 fprintf (stderr, "genattr: ");
239 vfprintf (stderr, s, ap);
240 va_end (ap);
241 fprintf (stderr, "\n");
242 exit (FATAL_EXIT_CODE);
244 #else /* not HAVE_VPRINTF */
246 static void
247 fatal (s, a1, a2)
248 char *s;
250 fprintf (stderr, "genattr: ");
251 fprintf (stderr, s, a1, a2);
252 fprintf (stderr, "\n");
253 exit (FATAL_EXIT_CODE);
255 #endif /* not HAVE_VPRINTF */
257 /* More 'friendly' abort that prints the line and file.
258 config.h can #define abort fancy_abort if you like that sort of thing. */
260 void
261 fancy_abort ()
263 fatal ("Internal gcc abort.");
267 main (argc, argv)
268 int argc;
269 char **argv;
271 rtx desc;
272 FILE *infile;
273 register int c;
274 int have_delay = 0;
275 int have_annul_true = 0;
276 int have_annul_false = 0;
277 int num_units = 0;
278 struct range all_simultaneity, all_multiplicity;
279 struct range all_ready_cost, all_issue_delay, all_blockage;
280 struct function_unit *units = 0, *unit;
281 int i;
283 init_range (&all_multiplicity);
284 init_range (&all_simultaneity);
285 init_range (&all_ready_cost);
286 init_range (&all_issue_delay);
287 init_range (&all_blockage);
289 obstack_init (rtl_obstack);
291 if (argc <= 1)
292 fatal ("No input file name.");
294 infile = fopen (argv[1], "r");
295 if (infile == 0)
297 perror (argv[1]);
298 exit (FATAL_EXIT_CODE);
301 init_rtl ();
303 printf ("/* Generated automatically by the program `genattr'\n\
304 from the machine description file `md'. */\n\n");
306 /* For compatibility, define the attribute `alternative', which is just
307 a reference to the variable `which_alternative'. */
309 printf ("#define HAVE_ATTR_alternative\n");
310 printf ("#define get_attr_alternative(insn) which_alternative\n");
312 /* Read the machine description. */
314 while (1)
316 c = read_skip_spaces (infile);
317 if (c == EOF)
318 break;
319 ungetc (c, infile);
321 desc = read_rtx (infile);
322 if (GET_CODE (desc) == DEFINE_ATTR)
323 gen_attr (desc);
325 else if (GET_CODE (desc) == DEFINE_DELAY)
327 if (! have_delay)
329 printf ("#define DELAY_SLOTS\n");
330 printf ("extern int num_delay_slots PROTO((rtx));\n");
331 printf ("extern int eligible_for_delay PROTO((rtx, int, rtx, int));\n\n");
332 printf ("extern int const_num_delay_slots PROTO((rtx));\n\n");
333 have_delay = 1;
336 for (i = 0; i < XVECLEN (desc, 1); i += 3)
338 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
340 printf ("#define ANNUL_IFTRUE_SLOTS\n");
341 printf ("extern int eligible_for_annul_true ();\n");
342 have_annul_true = 1;
345 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
347 printf ("#define ANNUL_IFFALSE_SLOTS\n");
348 printf ("extern int eligible_for_annul_false ();\n");
349 have_annul_false = 1;
354 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
356 char *name = XSTR (desc, 0);
357 int multiplicity = XINT (desc, 1);
358 int simultaneity = XINT (desc, 2);
359 int ready_cost = MAX (XINT (desc, 4), 1);
360 int issue_delay = MAX (XINT (desc, 5), 1);
361 int issueexp_p = (XVEC (desc, 6) != 0);
363 for (unit = units; unit; unit = unit->next)
364 if (strcmp (unit->name, name) == 0)
365 break;
367 if (unit == 0)
369 int len = strlen (name) + 1;
370 unit = (struct function_unit *)
371 alloca (sizeof (struct function_unit));
372 unit->name = (char *) alloca (len);
373 bcopy (name, unit->name, len);
374 unit->multiplicity = multiplicity;
375 unit->simultaneity = simultaneity;
376 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
377 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
378 unit->next = units;
379 units = unit;
380 num_units++;
382 extend_range (&all_multiplicity, multiplicity, multiplicity);
383 extend_range (&all_simultaneity, simultaneity, simultaneity);
385 else if (unit->multiplicity != multiplicity
386 || unit->simultaneity != simultaneity)
387 fatal ("Differing specifications given for `%s' function unit.",
388 unit->name);
390 extend_range (&unit->ready_cost, ready_cost, ready_cost);
391 extend_range (&unit->issue_delay,
392 issueexp_p ? 1 : issue_delay, issue_delay);
393 extend_range (&all_ready_cost,
394 unit->ready_cost.min, unit->ready_cost.max);
395 extend_range (&all_issue_delay,
396 unit->issue_delay.min, unit->issue_delay.max);
400 if (num_units > 0)
402 /* Compute the range of blockage cost values. See genattrtab.c
403 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
405 MAX (ISSUE-DELAY (E,C),
406 READY-COST (E) - (READY-COST (C) - 1))
408 and otherwise
410 MAX (ISSUE-DELAY (E,C),
411 READY-COST (E) - (READY-COST (C) - 1),
412 READY-COST (E) - FILL-TIME) */
414 for (unit = units; unit; unit = unit->next)
416 struct range blockage;
418 blockage = unit->issue_delay;
419 blockage.max = MAX (unit->ready_cost.max
420 - (unit->ready_cost.min - 1),
421 blockage.max);
422 blockage.min = MAX (1, blockage.min);
424 if (unit->simultaneity != 0)
426 int fill_time = ((unit->simultaneity - 1)
427 * unit->issue_delay.min);
428 blockage.min = MAX (unit->ready_cost.min - fill_time,
429 blockage.min);
430 blockage.max = MAX (unit->ready_cost.max - fill_time,
431 blockage.max);
433 extend_range (&all_blockage, blockage.min, blockage.max);
436 write_units (num_units, &all_multiplicity, &all_simultaneity,
437 &all_ready_cost, &all_issue_delay, &all_blockage);
440 /* Output flag masks for use by reorg.
442 Flags are used to hold branch direction and prediction information
443 for use by eligible_for_... */
444 printf("\n#define ATTR_FLAG_forward\t0x1\n");
445 printf("#define ATTR_FLAG_backward\t0x2\n");
446 printf("#define ATTR_FLAG_likely\t0x4\n");
447 printf("#define ATTR_FLAG_very_likely\t0x8\n");
448 printf("#define ATTR_FLAG_unlikely\t0x10\n");
449 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
451 fflush (stdout);
452 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
453 /* NOTREACHED */
454 return 0;