]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genattr.c
system.h (CTYPE_CONV, [...]): New macros.
[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"
f8b6598e 27#include "errors.h"
41299f41 28
f12b8918 29static struct obstack obstack;
41299f41
TW
30struct obstack *rtl_obstack = &obstack;
31
32#define obstack_chunk_alloc xmalloc
33#define obstack_chunk_free free
34
ef3fad04
TW
35/* A range of values. */
36
f12b8918
TM
37struct range
38{
39 int min;
40 int max;
41};
ef3fad04 42
f12b8918
TM
43/* Record information about each function unit mentioned in a
44 DEFINE_FUNCTION_UNIT. */
ef3fad04 45
f12b8918
TM
46struct 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};
b29d5f75 56
56c0e996 57static void extend_range PROTO((struct range *, int, int));
e009aaf3 58static void init_range PROTO((struct range *));
92a438d1 59static void write_upcase PROTO((const char *));
56c0e996
BS
60static void gen_attr PROTO((rtx));
61static void write_units PROTO((int, struct range *, struct range *,
62 struct range *, struct range *,
63 struct range *));
ef3fad04 64static void
f12b8918
TM
65extend_range (range, min, max)
66 struct range *range;
67 int min;
68 int max;
ef3fad04 69{
f12b8918
TM
70 if (range->min > min) range->min = min;
71 if (range->max < max) range->max = max;
ef3fad04
TW
72}
73
74static void
f12b8918
TM
75init_range (range)
76 struct range *range;
ef3fad04 77{
f12b8918
TM
78 range->min = 100000;
79 range->max = -1;
ef3fad04
TW
80}
81
41299f41
TW
82static void
83write_upcase (str)
92a438d1 84 const char *str;
41299f41 85{
f12b8918 86 for (; *str; str++)
92a438d1 87 putchar (TOUPPER(*str));
4beba5da 88}
4beba5da
TM
89
90static void
f12b8918
TM
91gen_attr (attr)
92 rtx attr;
4beba5da 93{
f12b8918 94 char *p;
ffee6d93 95 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
41299f41 96
f12b8918 97 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
41299f41 98
f12b8918
TM
99 /* If numeric attribute, don't need to write an enum. */
100 if (*XSTR (attr, 1) == '\0')
ffee6d93
KG
101 printf ("extern int get_attr_%s PROTO((%s));\n", XSTR (attr, 0),
102 (is_const ? "void" : "rtx"));
41299f41
TW
103 else
104 {
f12b8918
TM
105 printf ("enum attr_%s {", XSTR (attr, 0));
106 write_upcase (XSTR (attr, 0));
107 printf ("_");
4beba5da 108
f12b8918 109 for (p = XSTR (attr, 1); *p != '\0'; p++)
41299f41 110 {
f12b8918
TM
111 if (*p == ',')
112 {
113 printf (", ");
114 write_upcase (XSTR (attr, 0));
115 printf ("_");
116 }
f12b8918 117 else
92a438d1 118 putchar (TOUPPER(*p));
41299f41
TW
119 }
120
f12b8918 121 printf ("};\n");
ffee6d93
KG
122 printf ("extern enum attr_%s get_attr_%s PROTO((%s));\n\n",
123 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
41299f41
TW
124 }
125
f12b8918
TM
126 /* If `length' attribute, write additional function definitions and define
127 variables used by `insn_current_length'. */
128 if (! strcmp (XSTR (attr, 0), "length"))
41299f41 129 {
3eb76828
JW
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");
f12b8918
TM
134 printf ("extern int *insn_addresses;\n");
135 printf ("extern int insn_current_address;\n\n");
41299f41
TW
136 }
137}
138
139static void
f12b8918
TM
140write_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");
3eb76828
JW
152 printf ("extern int result_ready_cost PROTO((rtx));\n");
153 printf ("extern int function_units_used PROTO((rtx));\n\n");
f12b8918 154 printf ("extern struct function_unit_desc\n");
41299f41 155 printf ("{\n");
69277eec 156 printf (" const char *name;\n");
f12b8918
TM
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");
69277eec
KG
162 printf (" int (*ready_cost_function) PROTO ((rtx));\n");
163 printf (" int (*conflict_cost_function) PROTO ((rtx, rtx));\n");
f12b8918 164 printf (" int max_blockage;\n");
69277eec
KG
165 printf (" unsigned int (*blockage_range_function) PROTO ((rtx));\n");
166 printf (" int (*blockage_function) PROTO ((rtx, rtx));\n");
f12b8918
TM
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++)
ef3fad04 180 ;
f12b8918 181 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
4beba5da 182
f12b8918
TM
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);
41299f41
TW
189}
190
2778b98d 191PTR
4beba5da 192xmalloc (size)
2778b98d 193 size_t size;
4beba5da 194{
2778b98d 195 register PTR val = (PTR) malloc (size);
4beba5da
TM
196
197 if (val == 0)
198 fatal ("virtual memory exhausted");
199 return val;
200}
201
2778b98d 202PTR
470b68c0
RH
203xrealloc (old, size)
204 PTR old;
2778b98d 205 size_t size;
4beba5da 206{
470b68c0 207 register PTR ptr;
09d83d25 208 if (old)
470b68c0
RH
209 ptr = (PTR) realloc (old, size);
210 else
211 ptr = (PTR) malloc (size);
212 if (!ptr)
f12b8918 213 fatal ("virtual memory exhausted");
470b68c0 214 return ptr;
4beba5da
TM
215}
216
c1b59dce
KG
217extern int main PROTO ((int, char **));
218
41299f41
TW
219int
220main (argc, argv)
221 int argc;
222 char **argv;
223{
224 rtx desc;
225 FILE *infile;
41299f41 226 register int c;
f12b8918
TM
227 int have_delay = 0;
228 int have_annul_true = 0;
229 int have_annul_false = 0;
230 int num_units = 0;
231 struct range all_simultaneity, all_multiplicity;
232 struct range all_ready_cost, all_issue_delay, all_blockage;
233 struct function_unit *units = 0, *unit;
41299f41
TW
234 int i;
235
f12b8918
TM
236 init_range (&all_multiplicity);
237 init_range (&all_simultaneity);
238 init_range (&all_ready_cost);
239 init_range (&all_issue_delay);
240 init_range (&all_blockage);
241
f8b6598e 242 progname = "genattr";
41299f41
TW
243 obstack_init (rtl_obstack);
244
245 if (argc <= 1)
246 fatal ("No input file name.");
247
248 infile = fopen (argv[1], "r");
249 if (infile == 0)
250 {
251 perror (argv[1]);
c1b59dce 252 return (FATAL_EXIT_CODE);
41299f41
TW
253 }
254
f12b8918 255 printf ("/* Generated automatically by the program `genattr'\n\
4beba5da 256from the machine description file `md'. */\n\n");
41299f41 257
f12b8918
TM
258 /* For compatibility, define the attribute `alternative', which is just
259 a reference to the variable `which_alternative'. */
260
261 printf ("#define HAVE_ATTR_alternative\n");
262 printf ("#define get_attr_alternative(insn) which_alternative\n");
263
41299f41
TW
264 /* Read the machine description. */
265
266 while (1)
267 {
268 c = read_skip_spaces (infile);
269 if (c == EOF)
270 break;
271 ungetc (c, infile);
272
273 desc = read_rtx (infile);
f12b8918
TM
274 if (GET_CODE (desc) == DEFINE_ATTR)
275 gen_attr (desc);
41299f41 276
f12b8918
TM
277 else if (GET_CODE (desc) == DEFINE_DELAY)
278 {
279 if (! have_delay)
280 {
281 printf ("#define DELAY_SLOTS\n");
3eb76828 282 printf ("extern int num_delay_slots PROTO((rtx));\n");
eaa48dab 283 printf ("extern int eligible_for_delay PROTO((rtx, int, rtx, int));\n\n");
3eb76828 284 printf ("extern int const_num_delay_slots PROTO((rtx));\n\n");
f12b8918
TM
285 have_delay = 1;
286 }
41299f41 287
f12b8918
TM
288 for (i = 0; i < XVECLEN (desc, 1); i += 3)
289 {
290 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
291 {
292 printf ("#define ANNUL_IFTRUE_SLOTS\n");
69277eec 293 printf ("extern int eligible_for_annul_true PROTO ((rtx, int, rtx, int));\n");
f12b8918
TM
294 have_annul_true = 1;
295 }
ef3fad04 296
f12b8918
TM
297 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
298 {
299 printf ("#define ANNUL_IFFALSE_SLOTS\n");
69277eec 300 printf ("extern int eligible_for_annul_false PROTO ((rtx, int, rtx, int));\n");
f12b8918
TM
301 have_annul_false = 1;
302 }
303 }
304 }
ef3fad04 305
4beba5da
TM
306 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
307 {
f12b8918
TM
308 char *name = XSTR (desc, 0);
309 int multiplicity = XINT (desc, 1);
310 int simultaneity = XINT (desc, 2);
311 int ready_cost = MAX (XINT (desc, 4), 1);
312 int issue_delay = MAX (XINT (desc, 5), 1);
313 int issueexp_p = (XVEC (desc, 6) != 0);
ef3fad04 314
f12b8918
TM
315 for (unit = units; unit; unit = unit->next)
316 if (strcmp (unit->name, name) == 0)
317 break;
ef3fad04 318
f12b8918
TM
319 if (unit == 0)
320 {
321 int len = strlen (name) + 1;
322 unit = (struct function_unit *)
323 alloca (sizeof (struct function_unit));
324 unit->name = (char *) alloca (len);
325 bcopy (name, unit->name, len);
326 unit->multiplicity = multiplicity;
327 unit->simultaneity = simultaneity;
328 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
329 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
330 unit->next = units;
331 units = unit;
332 num_units++;
333
334 extend_range (&all_multiplicity, multiplicity, multiplicity);
335 extend_range (&all_simultaneity, simultaneity, simultaneity);
336 }
337 else if (unit->multiplicity != multiplicity
338 || unit->simultaneity != simultaneity)
339 fatal ("Differing specifications given for `%s' function unit.",
340 unit->name);
ef3fad04 341
f12b8918
TM
342 extend_range (&unit->ready_cost, ready_cost, ready_cost);
343 extend_range (&unit->issue_delay,
344 issueexp_p ? 1 : issue_delay, issue_delay);
345 extend_range (&all_ready_cost,
346 unit->ready_cost.min, unit->ready_cost.max);
347 extend_range (&all_issue_delay,
348 unit->issue_delay.min, unit->issue_delay.max);
349 }
350 }
ef3fad04 351
f12b8918
TM
352 if (num_units > 0)
353 {
354 /* Compute the range of blockage cost values. See genattrtab.c
355 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
ef3fad04 356
f12b8918
TM
357 MAX (ISSUE-DELAY (E,C),
358 READY-COST (E) - (READY-COST (C) - 1))
ef3fad04 359
f12b8918 360 and otherwise
4beba5da 361
f12b8918
TM
362 MAX (ISSUE-DELAY (E,C),
363 READY-COST (E) - (READY-COST (C) - 1),
364 READY-COST (E) - FILL-TIME) */
4beba5da 365
f12b8918
TM
366 for (unit = units; unit; unit = unit->next)
367 {
368 struct range blockage;
4beba5da 369
f12b8918
TM
370 blockage = unit->issue_delay;
371 blockage.max = MAX (unit->ready_cost.max
372 - (unit->ready_cost.min - 1),
373 blockage.max);
374 blockage.min = MAX (1, blockage.min);
4beba5da 375
f12b8918
TM
376 if (unit->simultaneity != 0)
377 {
378 int fill_time = ((unit->simultaneity - 1)
379 * unit->issue_delay.min);
380 blockage.min = MAX (unit->ready_cost.min - fill_time,
381 blockage.min);
382 blockage.max = MAX (unit->ready_cost.max - fill_time,
383 blockage.max);
384 }
385 extend_range (&all_blockage, blockage.min, blockage.max);
386 }
4beba5da 387
f12b8918
TM
388 write_units (num_units, &all_multiplicity, &all_simultaneity,
389 &all_ready_cost, &all_issue_delay, &all_blockage);
ef3fad04 390 }
b29d5f75 391
eaa48dab
JL
392 /* Output flag masks for use by reorg.
393
394 Flags are used to hold branch direction and prediction information
395 for use by eligible_for_... */
396 printf("\n#define ATTR_FLAG_forward\t0x1\n");
397 printf("#define ATTR_FLAG_backward\t0x2\n");
398 printf("#define ATTR_FLAG_likely\t0x4\n");
399 printf("#define ATTR_FLAG_very_likely\t0x8\n");
400 printf("#define ATTR_FLAG_unlikely\t0x10\n");
401 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
402
41299f41 403 fflush (stdout);
c1b59dce 404 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
41299f41 405}
a995e389
RH
406
407/* Define this so we can link with print-rtl.o to get debug_rtx function. */
408const char *
409get_insn_name (code)
c1b59dce 410 int code ATTRIBUTE_UNUSED;
a995e389
RH
411{
412 return NULL;
413}