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