]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genattr.c
Daily bump.
[thirdparty/gcc.git] / gcc / genattr.c
CommitLineData
f12b8918 1/* Generate attribute information (insn-attr.h) from machine description.
34627ce6 2 Copyright (C) 1991, 1994, 1996, 1998, 1999 Free Software Foundation, Inc.
c9309570 3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
41299f41
TW
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
a35311b0
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
41299f41
TW
21
22
0d64891c 23#include "hconfig.h"
0b93b64e 24#include "system.h"
41299f41
TW
25#include "rtl.h"
26#include "obstack.h"
27
f12b8918 28static struct obstack obstack;
41299f41
TW
29struct obstack *rtl_obstack = &obstack;
30
31#define obstack_chunk_alloc xmalloc
32#define obstack_chunk_free free
33
2a611d21 34void fatal PVPROTO ((const char *, ...))
bf94d1ec 35 ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
41299f41 36
4db83042
MM
37/* Define this so we can link with print-rtl.o to get debug_rtx function. */
38char **insn_name_ptr = 0;
39
ef3fad04
TW
40/* A range of values. */
41
f12b8918
TM
42struct range
43{
44 int min;
45 int max;
46};
ef3fad04 47
f12b8918
TM
48/* Record information about each function unit mentioned in a
49 DEFINE_FUNCTION_UNIT. */
ef3fad04 50
f12b8918
TM
51struct function_unit
52{
53 char *name; /* Function unit name. */
54 struct function_unit *next; /* Next function unit. */
55 int multiplicity; /* Number of units of this type. */
56 int simultaneity; /* Maximum number of simultaneous insns
57 on this function unit or 0 if unlimited. */
58 struct range ready_cost; /* Range of ready cost values. */
59 struct range issue_delay; /* Range of issue delay values. */
60};
b29d5f75 61
56c0e996 62static void extend_range PROTO((struct range *, int, int));
e009aaf3 63static void init_range PROTO((struct range *));
56c0e996
BS
64static void write_upcase PROTO((char *));
65static void gen_attr PROTO((rtx));
66static void write_units PROTO((int, struct range *, struct range *,
67 struct range *, struct range *,
68 struct range *));
ef3fad04 69static void
f12b8918
TM
70extend_range (range, min, max)
71 struct range *range;
72 int min;
73 int max;
ef3fad04 74{
f12b8918
TM
75 if (range->min > min) range->min = min;
76 if (range->max < max) range->max = max;
ef3fad04
TW
77}
78
79static void
f12b8918
TM
80init_range (range)
81 struct range *range;
ef3fad04 82{
f12b8918
TM
83 range->min = 100000;
84 range->max = -1;
ef3fad04
TW
85}
86
41299f41
TW
87static void
88write_upcase (str)
f12b8918 89 char *str;
41299f41 90{
f12b8918
TM
91 for (; *str; str++)
92 if (*str >= 'a' && *str <= 'z')
93 printf ("%c", *str - 'a' + 'A');
41299f41 94 else
f12b8918 95 printf ("%c", *str);
4beba5da 96}
4beba5da
TM
97
98static void
f12b8918
TM
99gen_attr (attr)
100 rtx attr;
4beba5da 101{
f12b8918 102 char *p;
ffee6d93 103 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
41299f41 104
f12b8918 105 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
41299f41 106
f12b8918
TM
107 /* If numeric attribute, don't need to write an enum. */
108 if (*XSTR (attr, 1) == '\0')
ffee6d93
KG
109 printf ("extern int get_attr_%s PROTO((%s));\n", XSTR (attr, 0),
110 (is_const ? "void" : "rtx"));
41299f41
TW
111 else
112 {
f12b8918
TM
113 printf ("enum attr_%s {", XSTR (attr, 0));
114 write_upcase (XSTR (attr, 0));
115 printf ("_");
4beba5da 116
f12b8918 117 for (p = XSTR (attr, 1); *p != '\0'; p++)
41299f41 118 {
f12b8918
TM
119 if (*p == ',')
120 {
121 printf (", ");
122 write_upcase (XSTR (attr, 0));
123 printf ("_");
124 }
125 else if (*p >= 'a' && *p <= 'z')
126 printf ("%c", *p - 'a' + 'A');
127 else
128 printf ("%c", *p);
41299f41
TW
129 }
130
f12b8918 131 printf ("};\n");
ffee6d93
KG
132 printf ("extern enum attr_%s get_attr_%s PROTO((%s));\n\n",
133 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
41299f41
TW
134 }
135
f12b8918
TM
136 /* If `length' attribute, write additional function definitions and define
137 variables used by `insn_current_length'. */
138 if (! strcmp (XSTR (attr, 0), "length"))
41299f41 139 {
3eb76828
JW
140 printf ("extern void shorten_branches PROTO((rtx));\n");
141 printf ("extern int insn_default_length PROTO((rtx));\n");
142 printf ("extern int insn_variable_length_p PROTO((rtx));\n");
143 printf ("extern int insn_current_length PROTO((rtx));\n\n");
f12b8918
TM
144 printf ("extern int *insn_addresses;\n");
145 printf ("extern int insn_current_address;\n\n");
41299f41
TW
146 }
147}
148
149static void
f12b8918
TM
150write_units (num_units, multiplicity, simultaneity,
151 ready_cost, issue_delay, blockage)
152 int num_units;
153 struct range *multiplicity;
154 struct range *simultaneity;
155 struct range *ready_cost;
156 struct range *issue_delay;
157 struct range *blockage;
158{
159 int i, q_size;
160
161 printf ("#define INSN_SCHEDULING\n\n");
3eb76828
JW
162 printf ("extern int result_ready_cost PROTO((rtx));\n");
163 printf ("extern int function_units_used PROTO((rtx));\n\n");
f12b8918 164 printf ("extern struct function_unit_desc\n");
41299f41 165 printf ("{\n");
69277eec 166 printf (" const char *name;\n");
f12b8918
TM
167 printf (" int bitmask;\n");
168 printf (" int multiplicity;\n");
169 printf (" int simultaneity;\n");
170 printf (" int default_cost;\n");
171 printf (" int max_issue_delay;\n");
69277eec
KG
172 printf (" int (*ready_cost_function) PROTO ((rtx));\n");
173 printf (" int (*conflict_cost_function) PROTO ((rtx, rtx));\n");
f12b8918 174 printf (" int max_blockage;\n");
69277eec
KG
175 printf (" unsigned int (*blockage_range_function) PROTO ((rtx));\n");
176 printf (" int (*blockage_function) PROTO ((rtx, rtx));\n");
f12b8918
TM
177 printf ("} function_units[];\n\n");
178 printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
179 printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
180 printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
181 printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
182 printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
183 printf ("#define MIN_READY_COST %d\n", ready_cost->min);
184 printf ("#define MAX_READY_COST %d\n", ready_cost->max);
185 printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
186 printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
187 printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
188 printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
189 for (i = 0; (1 << i) < blockage->max; i++)
ef3fad04 190 ;
f12b8918 191 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
4beba5da 192
f12b8918
TM
193 /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
194 MAX_READY_COST. This is the longest time an isnsn may be queued. */
195 i = MAX (blockage->max, ready_cost->max);
196 for (q_size = 1; q_size <= i; q_size <<= 1)
197 ;
198 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
41299f41
TW
199}
200
2778b98d 201PTR
4beba5da 202xmalloc (size)
2778b98d 203 size_t size;
4beba5da 204{
2778b98d 205 register PTR val = (PTR) malloc (size);
4beba5da
TM
206
207 if (val == 0)
208 fatal ("virtual memory exhausted");
209 return val;
210}
211
2778b98d 212PTR
470b68c0
RH
213xrealloc (old, size)
214 PTR old;
2778b98d 215 size_t size;
4beba5da 216{
470b68c0 217 register PTR ptr;
09d83d25 218 if (old)
470b68c0
RH
219 ptr = (PTR) realloc (old, size);
220 else
221 ptr = (PTR) malloc (size);
222 if (!ptr)
f12b8918 223 fatal ("virtual memory exhausted");
470b68c0 224 return ptr;
4beba5da
TM
225}
226
2a611d21 227void
85fda1eb 228fatal VPROTO ((const char *format, ...))
41299f41 229{
5148a72b 230#ifndef ANSI_PROTOTYPES
85fda1eb 231 const char *format;
320e7c40
KG
232#endif
233 va_list ap;
234
235 VA_START (ap, format);
236
5148a72b 237#ifndef ANSI_PROTOTYPES
85fda1eb 238 format = va_arg (ap, const char *);
320e7c40
KG
239#endif
240
f12b8918 241 fprintf (stderr, "genattr: ");
320e7c40
KG
242 vfprintf (stderr, format, ap);
243 va_end (ap);
41299f41
TW
244 fprintf (stderr, "\n");
245 exit (FATAL_EXIT_CODE);
246}
247
41299f41
TW
248int
249main (argc, argv)
250 int argc;
251 char **argv;
252{
253 rtx desc;
254 FILE *infile;
41299f41 255 register int c;
f12b8918
TM
256 int have_delay = 0;
257 int have_annul_true = 0;
258 int have_annul_false = 0;
259 int num_units = 0;
260 struct range all_simultaneity, all_multiplicity;
261 struct range all_ready_cost, all_issue_delay, all_blockage;
262 struct function_unit *units = 0, *unit;
41299f41
TW
263 int i;
264
f12b8918
TM
265 init_range (&all_multiplicity);
266 init_range (&all_simultaneity);
267 init_range (&all_ready_cost);
268 init_range (&all_issue_delay);
269 init_range (&all_blockage);
270
41299f41
TW
271 obstack_init (rtl_obstack);
272
273 if (argc <= 1)
274 fatal ("No input file name.");
275
276 infile = fopen (argv[1], "r");
277 if (infile == 0)
278 {
279 perror (argv[1]);
280 exit (FATAL_EXIT_CODE);
281 }
282
283 init_rtl ();
284
f12b8918 285 printf ("/* Generated automatically by the program `genattr'\n\
4beba5da 286from the machine description file `md'. */\n\n");
41299f41 287
f12b8918
TM
288 /* For compatibility, define the attribute `alternative', which is just
289 a reference to the variable `which_alternative'. */
290
291 printf ("#define HAVE_ATTR_alternative\n");
292 printf ("#define get_attr_alternative(insn) which_alternative\n");
293
41299f41
TW
294 /* Read the machine description. */
295
296 while (1)
297 {
298 c = read_skip_spaces (infile);
299 if (c == EOF)
300 break;
301 ungetc (c, infile);
302
303 desc = read_rtx (infile);
f12b8918
TM
304 if (GET_CODE (desc) == DEFINE_ATTR)
305 gen_attr (desc);
41299f41 306
f12b8918
TM
307 else if (GET_CODE (desc) == DEFINE_DELAY)
308 {
309 if (! have_delay)
310 {
311 printf ("#define DELAY_SLOTS\n");
3eb76828 312 printf ("extern int num_delay_slots PROTO((rtx));\n");
eaa48dab 313 printf ("extern int eligible_for_delay PROTO((rtx, int, rtx, int));\n\n");
3eb76828 314 printf ("extern int const_num_delay_slots PROTO((rtx));\n\n");
f12b8918
TM
315 have_delay = 1;
316 }
41299f41 317
f12b8918
TM
318 for (i = 0; i < XVECLEN (desc, 1); i += 3)
319 {
320 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
321 {
322 printf ("#define ANNUL_IFTRUE_SLOTS\n");
69277eec 323 printf ("extern int eligible_for_annul_true PROTO ((rtx, int, rtx, int));\n");
f12b8918
TM
324 have_annul_true = 1;
325 }
ef3fad04 326
f12b8918
TM
327 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
328 {
329 printf ("#define ANNUL_IFFALSE_SLOTS\n");
69277eec 330 printf ("extern int eligible_for_annul_false PROTO ((rtx, int, rtx, int));\n");
f12b8918
TM
331 have_annul_false = 1;
332 }
333 }
334 }
ef3fad04 335
4beba5da
TM
336 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
337 {
f12b8918
TM
338 char *name = XSTR (desc, 0);
339 int multiplicity = XINT (desc, 1);
340 int simultaneity = XINT (desc, 2);
341 int ready_cost = MAX (XINT (desc, 4), 1);
342 int issue_delay = MAX (XINT (desc, 5), 1);
343 int issueexp_p = (XVEC (desc, 6) != 0);
ef3fad04 344
f12b8918
TM
345 for (unit = units; unit; unit = unit->next)
346 if (strcmp (unit->name, name) == 0)
347 break;
ef3fad04 348
f12b8918
TM
349 if (unit == 0)
350 {
351 int len = strlen (name) + 1;
352 unit = (struct function_unit *)
353 alloca (sizeof (struct function_unit));
354 unit->name = (char *) alloca (len);
355 bcopy (name, unit->name, len);
356 unit->multiplicity = multiplicity;
357 unit->simultaneity = simultaneity;
358 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
359 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
360 unit->next = units;
361 units = unit;
362 num_units++;
363
364 extend_range (&all_multiplicity, multiplicity, multiplicity);
365 extend_range (&all_simultaneity, simultaneity, simultaneity);
366 }
367 else if (unit->multiplicity != multiplicity
368 || unit->simultaneity != simultaneity)
369 fatal ("Differing specifications given for `%s' function unit.",
370 unit->name);
ef3fad04 371
f12b8918
TM
372 extend_range (&unit->ready_cost, ready_cost, ready_cost);
373 extend_range (&unit->issue_delay,
374 issueexp_p ? 1 : issue_delay, issue_delay);
375 extend_range (&all_ready_cost,
376 unit->ready_cost.min, unit->ready_cost.max);
377 extend_range (&all_issue_delay,
378 unit->issue_delay.min, unit->issue_delay.max);
379 }
380 }
ef3fad04 381
f12b8918
TM
382 if (num_units > 0)
383 {
384 /* Compute the range of blockage cost values. See genattrtab.c
385 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
ef3fad04 386
f12b8918
TM
387 MAX (ISSUE-DELAY (E,C),
388 READY-COST (E) - (READY-COST (C) - 1))
ef3fad04 389
f12b8918 390 and otherwise
4beba5da 391
f12b8918
TM
392 MAX (ISSUE-DELAY (E,C),
393 READY-COST (E) - (READY-COST (C) - 1),
394 READY-COST (E) - FILL-TIME) */
4beba5da 395
f12b8918
TM
396 for (unit = units; unit; unit = unit->next)
397 {
398 struct range blockage;
4beba5da 399
f12b8918
TM
400 blockage = unit->issue_delay;
401 blockage.max = MAX (unit->ready_cost.max
402 - (unit->ready_cost.min - 1),
403 blockage.max);
404 blockage.min = MAX (1, blockage.min);
4beba5da 405
f12b8918
TM
406 if (unit->simultaneity != 0)
407 {
408 int fill_time = ((unit->simultaneity - 1)
409 * unit->issue_delay.min);
410 blockage.min = MAX (unit->ready_cost.min - fill_time,
411 blockage.min);
412 blockage.max = MAX (unit->ready_cost.max - fill_time,
413 blockage.max);
414 }
415 extend_range (&all_blockage, blockage.min, blockage.max);
416 }
4beba5da 417
f12b8918
TM
418 write_units (num_units, &all_multiplicity, &all_simultaneity,
419 &all_ready_cost, &all_issue_delay, &all_blockage);
ef3fad04 420 }
b29d5f75 421
eaa48dab
JL
422 /* Output flag masks for use by reorg.
423
424 Flags are used to hold branch direction and prediction information
425 for use by eligible_for_... */
426 printf("\n#define ATTR_FLAG_forward\t0x1\n");
427 printf("#define ATTR_FLAG_backward\t0x2\n");
428 printf("#define ATTR_FLAG_likely\t0x4\n");
429 printf("#define ATTR_FLAG_very_likely\t0x8\n");
430 printf("#define ATTR_FLAG_unlikely\t0x10\n");
431 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
432
41299f41
TW
433 fflush (stdout);
434 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
435 /* NOTREACHED */
436 return 0;
437}