remove extraneous code checked in with previous delta
[official-gcc.git] / gcc / genattr.c
blob30160a443795a66ff07ca8cccebc3636e1229c7a
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000 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"
27 #include "errors.h"
29 static struct obstack obstack;
30 struct obstack *rtl_obstack = &obstack;
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
35 /* A range of values. */
37 struct range
39 int min;
40 int max;
43 /* Record information about each function unit mentioned in a
44 DEFINE_FUNCTION_UNIT. */
46 struct function_unit
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. */
57 static void extend_range PARAMS ((struct range *, int, int));
58 static void init_range PARAMS ((struct range *));
59 static void write_upcase PARAMS ((const char *));
60 static void gen_attr PARAMS ((rtx));
61 static void write_units PARAMS ((int, struct range *, struct range *,
62 struct range *, struct range *,
63 struct range *));
64 static void
65 extend_range (range, min, max)
66 struct range *range;
67 int min;
68 int max;
70 if (range->min > min) range->min = min;
71 if (range->max < max) range->max = max;
74 static void
75 init_range (range)
76 struct range *range;
78 range->min = 100000;
79 range->max = -1;
82 static void
83 write_upcase (str)
84 const char *str;
86 for (; *str; str++)
87 putchar (TOUPPER(*str));
90 static void
91 gen_attr (attr)
92 rtx attr;
94 const char *p;
95 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
97 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
99 /* If numeric attribute, don't need to write an enum. */
100 if (*XSTR (attr, 1) == '\0')
101 printf ("extern int get_attr_%s PARAMS ((%s));\n", XSTR (attr, 0),
102 (is_const ? "void" : "rtx"));
103 else
105 printf ("enum attr_%s {", XSTR (attr, 0));
106 write_upcase (XSTR (attr, 0));
107 printf ("_");
109 for (p = XSTR (attr, 1); *p != '\0'; p++)
111 if (*p == ',')
113 printf (", ");
114 write_upcase (XSTR (attr, 0));
115 printf ("_");
117 else
118 putchar (TOUPPER(*p));
121 printf ("};\n");
122 printf ("extern enum attr_%s get_attr_%s PARAMS ((%s));\n\n",
123 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
126 /* If `length' attribute, write additional function definitions and define
127 variables used by `insn_current_length'. */
128 if (! strcmp (XSTR (attr, 0), "length"))
130 printf ("extern void shorten_branches PARAMS ((rtx));\n");
131 printf ("extern int insn_default_length PARAMS ((rtx));\n");
132 printf ("extern int insn_variable_length_p PARAMS ((rtx));\n");
133 printf ("extern int insn_current_length PARAMS ((rtx));\n\n");
134 printf ("extern int *insn_addresses;\n");
135 printf ("extern int insn_current_address;\n\n");
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;
149 int i, q_size;
151 printf ("#define INSN_SCHEDULING\n\n");
152 printf ("extern int result_ready_cost PARAMS ((rtx));\n");
153 printf ("extern int function_units_used PARAMS ((rtx));\n\n");
154 printf ("extern struct function_unit_desc\n");
155 printf ("{\n");
156 printf (" const 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) PARAMS ((rtx));\n");
163 printf (" int (*conflict_cost_function) PARAMS ((rtx, rtx));\n");
164 printf (" int max_blockage;\n");
165 printf (" unsigned int (*blockage_range_function) PARAMS ((rtx));\n");
166 printf (" int (*blockage_function) PARAMS ((rtx, rtx));\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++)
181 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
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)
188 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
192 xmalloc (size)
193 size_t size;
195 register PTR val = (PTR) malloc (size);
197 if (val == 0)
198 fatal ("virtual memory exhausted");
199 return val;
203 xrealloc (old, size)
204 PTR old;
205 size_t size;
207 register PTR ptr;
208 if (old)
209 ptr = (PTR) realloc (old, size);
210 else
211 ptr = (PTR) malloc (size);
212 if (!ptr)
213 fatal ("virtual memory exhausted");
214 return ptr;
217 extern int main PARAMS ((int, char **));
220 main (argc, argv)
221 int argc;
222 char **argv;
224 rtx desc;
225 FILE *infile;
226 register int c;
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;
234 int i;
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);
242 progname = "genattr";
243 obstack_init (rtl_obstack);
245 if (argc <= 1)
246 fatal ("No input file name.");
248 infile = fopen (argv[1], "r");
249 if (infile == 0)
251 perror (argv[1]);
252 return (FATAL_EXIT_CODE);
254 read_rtx_filename = argv[1];
256 printf ("/* Generated automatically by the program `genattr'\n\
257 from the machine description file `md'. */\n\n");
259 /* For compatibility, define the attribute `alternative', which is just
260 a reference to the variable `which_alternative'. */
262 printf ("#define HAVE_ATTR_alternative\n");
263 printf ("#define get_attr_alternative(insn) which_alternative\n");
265 /* Read the machine description. */
267 while (1)
269 c = read_skip_spaces (infile);
270 if (c == EOF)
271 break;
272 ungetc (c, infile);
274 desc = read_rtx (infile);
275 if (GET_CODE (desc) == DEFINE_ATTR)
276 gen_attr (desc);
278 else if (GET_CODE (desc) == DEFINE_DELAY)
280 if (! have_delay)
282 printf ("#define DELAY_SLOTS\n");
283 printf ("extern int num_delay_slots PARAMS ((rtx));\n");
284 printf ("extern int eligible_for_delay PARAMS ((rtx, int, rtx, int));\n\n");
285 printf ("extern int const_num_delay_slots PARAMS ((rtx));\n\n");
286 have_delay = 1;
289 for (i = 0; i < XVECLEN (desc, 1); i += 3)
291 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
293 printf ("#define ANNUL_IFTRUE_SLOTS\n");
294 printf ("extern int eligible_for_annul_true PARAMS ((rtx, int, rtx, int));\n");
295 have_annul_true = 1;
298 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
300 printf ("#define ANNUL_IFFALSE_SLOTS\n");
301 printf ("extern int eligible_for_annul_false PARAMS ((rtx, int, rtx, int));\n");
302 have_annul_false = 1;
307 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
309 const char *name = XSTR (desc, 0);
310 int multiplicity = XINT (desc, 1);
311 int simultaneity = XINT (desc, 2);
312 int ready_cost = MAX (XINT (desc, 4), 1);
313 int issue_delay = MAX (XINT (desc, 5), 1);
314 int issueexp_p = (XVEC (desc, 6) != 0);
316 for (unit = units; unit; unit = unit->next)
317 if (strcmp (unit->name, name) == 0)
318 break;
320 if (unit == 0)
322 int len = strlen (name) + 1;
323 unit = (struct function_unit *)
324 alloca (sizeof (struct function_unit));
325 unit->name = (char *) alloca (len);
326 bcopy (name, unit->name, len);
327 unit->multiplicity = multiplicity;
328 unit->simultaneity = simultaneity;
329 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
330 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
331 unit->next = units;
332 units = unit;
333 num_units++;
335 extend_range (&all_multiplicity, multiplicity, multiplicity);
336 extend_range (&all_simultaneity, simultaneity, simultaneity);
338 else if (unit->multiplicity != multiplicity
339 || unit->simultaneity != simultaneity)
340 fatal ("Differing specifications given for `%s' function unit.",
341 unit->name);
343 extend_range (&unit->ready_cost, ready_cost, ready_cost);
344 extend_range (&unit->issue_delay,
345 issueexp_p ? 1 : issue_delay, issue_delay);
346 extend_range (&all_ready_cost,
347 unit->ready_cost.min, unit->ready_cost.max);
348 extend_range (&all_issue_delay,
349 unit->issue_delay.min, unit->issue_delay.max);
353 if (num_units > 0)
355 /* Compute the range of blockage cost values. See genattrtab.c
356 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
358 MAX (ISSUE-DELAY (E,C),
359 READY-COST (E) - (READY-COST (C) - 1))
361 and otherwise
363 MAX (ISSUE-DELAY (E,C),
364 READY-COST (E) - (READY-COST (C) - 1),
365 READY-COST (E) - FILL-TIME) */
367 for (unit = units; unit; unit = unit->next)
369 struct range blockage;
371 blockage = unit->issue_delay;
372 blockage.max = MAX (unit->ready_cost.max
373 - (unit->ready_cost.min - 1),
374 blockage.max);
375 blockage.min = MAX (1, blockage.min);
377 if (unit->simultaneity != 0)
379 int fill_time = ((unit->simultaneity - 1)
380 * unit->issue_delay.min);
381 blockage.min = MAX (unit->ready_cost.min - fill_time,
382 blockage.min);
383 blockage.max = MAX (unit->ready_cost.max - fill_time,
384 blockage.max);
386 extend_range (&all_blockage, blockage.min, blockage.max);
389 write_units (num_units, &all_multiplicity, &all_simultaneity,
390 &all_ready_cost, &all_issue_delay, &all_blockage);
393 /* Output flag masks for use by reorg.
395 Flags are used to hold branch direction and prediction information
396 for use by eligible_for_... */
397 printf("\n#define ATTR_FLAG_forward\t0x1\n");
398 printf("#define ATTR_FLAG_backward\t0x2\n");
399 printf("#define ATTR_FLAG_likely\t0x4\n");
400 printf("#define ATTR_FLAG_very_likely\t0x8\n");
401 printf("#define ATTR_FLAG_unlikely\t0x10\n");
402 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
404 fflush (stdout);
405 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
408 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
409 const char *
410 get_insn_name (code)
411 int code ATTRIBUTE_UNUSED;
413 return NULL;