2000-07-03 Donn Terry (donnte@microsoft.com)
[official-gcc.git] / gcc / genattr.c
blob6a593edeb21ad5b5849ea53d4d83a42e5e94b3f9
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 "errors.h"
27 #include "gensupport.h"
30 /* A range of values. */
32 struct range
34 int min;
35 int max;
38 /* Record information about each function unit mentioned in a
39 DEFINE_FUNCTION_UNIT. */
41 struct function_unit
43 char *name; /* Function unit name. */
44 struct function_unit *next; /* Next function unit. */
45 int multiplicity; /* Number of units of this type. */
46 int simultaneity; /* Maximum number of simultaneous insns
47 on this function unit or 0 if unlimited. */
48 struct range ready_cost; /* Range of ready cost values. */
49 struct range issue_delay; /* Range of issue delay values. */
52 static void extend_range PARAMS ((struct range *, int, int));
53 static void init_range PARAMS ((struct range *));
54 static void write_upcase PARAMS ((const char *));
55 static void gen_attr PARAMS ((rtx));
56 static void write_units PARAMS ((int, struct range *, struct range *,
57 struct range *, struct range *,
58 struct range *));
59 static void
60 extend_range (range, min, max)
61 struct range *range;
62 int min;
63 int max;
65 if (range->min > min) range->min = min;
66 if (range->max < max) range->max = max;
69 static void
70 init_range (range)
71 struct range *range;
73 range->min = 100000;
74 range->max = -1;
77 static void
78 write_upcase (str)
79 const char *str;
81 for (; *str; str++)
82 putchar (TOUPPER(*str));
85 static void
86 gen_attr (attr)
87 rtx attr;
89 const char *p;
90 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
92 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
94 /* If numeric attribute, don't need to write an enum. */
95 if (*XSTR (attr, 1) == '\0')
96 printf ("extern int get_attr_%s PARAMS ((%s));\n", XSTR (attr, 0),
97 (is_const ? "void" : "rtx"));
98 else
100 printf ("enum attr_%s {", XSTR (attr, 0));
101 write_upcase (XSTR (attr, 0));
102 printf ("_");
104 for (p = XSTR (attr, 1); *p != '\0'; p++)
106 if (*p == ',')
108 printf (", ");
109 write_upcase (XSTR (attr, 0));
110 printf ("_");
112 else
113 putchar (TOUPPER(*p));
116 printf ("};\n");
117 printf ("extern enum attr_%s get_attr_%s PARAMS ((%s));\n\n",
118 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
121 /* If `length' attribute, write additional function definitions and define
122 variables used by `insn_current_length'. */
123 if (! strcmp (XSTR (attr, 0), "length"))
125 printf ("extern void shorten_branches PARAMS ((rtx));\n");
126 printf ("extern int insn_default_length PARAMS ((rtx));\n");
127 printf ("extern int insn_variable_length_p PARAMS ((rtx));\n");
128 printf ("extern int insn_current_length PARAMS ((rtx));\n\n");
129 printf ("extern int *insn_addresses;\n");
130 printf ("extern int insn_current_address;\n\n");
134 static void
135 write_units (num_units, multiplicity, simultaneity,
136 ready_cost, issue_delay, blockage)
137 int num_units;
138 struct range *multiplicity;
139 struct range *simultaneity;
140 struct range *ready_cost;
141 struct range *issue_delay;
142 struct range *blockage;
144 int i, q_size;
146 printf ("#define INSN_SCHEDULING\n\n");
147 printf ("extern int result_ready_cost PARAMS ((rtx));\n");
148 printf ("extern int function_units_used PARAMS ((rtx));\n\n");
149 printf ("extern struct function_unit_desc\n");
150 printf ("{\n");
151 printf (" const char *name;\n");
152 printf (" int bitmask;\n");
153 printf (" int multiplicity;\n");
154 printf (" int simultaneity;\n");
155 printf (" int default_cost;\n");
156 printf (" int max_issue_delay;\n");
157 printf (" int (*ready_cost_function) PARAMS ((rtx));\n");
158 printf (" int (*conflict_cost_function) PARAMS ((rtx, rtx));\n");
159 printf (" int max_blockage;\n");
160 printf (" unsigned int (*blockage_range_function) PARAMS ((rtx));\n");
161 printf (" int (*blockage_function) PARAMS ((rtx, rtx));\n");
162 printf ("} function_units[];\n\n");
163 printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
164 printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
165 printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
166 printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
167 printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
168 printf ("#define MIN_READY_COST %d\n", ready_cost->min);
169 printf ("#define MAX_READY_COST %d\n", ready_cost->max);
170 printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
171 printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
172 printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
173 printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
174 for (i = 0; (1 << i) < blockage->max; i++)
176 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
178 /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
179 MAX_READY_COST. This is the longest time an isnsn may be queued. */
180 i = MAX (blockage->max, ready_cost->max);
181 for (q_size = 1; q_size <= i; q_size <<= 1)
183 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
186 extern int main PARAMS ((int, char **));
189 main (argc, argv)
190 int argc;
191 char **argv;
193 rtx desc;
194 int have_delay = 0;
195 int have_annul_true = 0;
196 int have_annul_false = 0;
197 int num_units = 0;
198 struct range all_simultaneity, all_multiplicity;
199 struct range all_ready_cost, all_issue_delay, all_blockage;
200 struct function_unit *units = 0, *unit;
201 int i;
203 init_range (&all_multiplicity);
204 init_range (&all_simultaneity);
205 init_range (&all_ready_cost);
206 init_range (&all_issue_delay);
207 init_range (&all_blockage);
209 progname = "genattr";
211 if (argc <= 1)
212 fatal ("No input file name.");
214 if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
215 return (FATAL_EXIT_CODE);
217 printf ("/* Generated automatically by the program `genattr'\n\
218 from the machine description file `md'. */\n\n");
220 /* For compatibility, define the attribute `alternative', which is just
221 a reference to the variable `which_alternative'. */
223 printf ("#define HAVE_ATTR_alternative\n");
224 printf ("#define get_attr_alternative(insn) which_alternative\n");
226 /* Read the machine description. */
228 while (1)
230 int line_no, insn_code_number;
232 desc = read_md_rtx (&line_no, &insn_code_number);
233 if (desc == NULL)
234 break;
236 if (GET_CODE (desc) == DEFINE_ATTR)
237 gen_attr (desc);
239 else if (GET_CODE (desc) == DEFINE_DELAY)
241 if (! have_delay)
243 printf ("#define DELAY_SLOTS\n");
244 printf ("extern int num_delay_slots PARAMS ((rtx));\n");
245 printf ("extern int eligible_for_delay PARAMS ((rtx, int, rtx, int));\n\n");
246 printf ("extern int const_num_delay_slots PARAMS ((rtx));\n\n");
247 have_delay = 1;
250 for (i = 0; i < XVECLEN (desc, 1); i += 3)
252 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
254 printf ("#define ANNUL_IFTRUE_SLOTS\n");
255 printf ("extern int eligible_for_annul_true PARAMS ((rtx, int, rtx, int));\n");
256 have_annul_true = 1;
259 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
261 printf ("#define ANNUL_IFFALSE_SLOTS\n");
262 printf ("extern int eligible_for_annul_false PARAMS ((rtx, int, rtx, int));\n");
263 have_annul_false = 1;
268 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
270 const char *name = XSTR (desc, 0);
271 int multiplicity = XINT (desc, 1);
272 int simultaneity = XINT (desc, 2);
273 int ready_cost = MAX (XINT (desc, 4), 1);
274 int issue_delay = MAX (XINT (desc, 5), 1);
275 int issueexp_p = (XVEC (desc, 6) != 0);
277 for (unit = units; unit; unit = unit->next)
278 if (strcmp (unit->name, name) == 0)
279 break;
281 if (unit == 0)
283 int len = strlen (name) + 1;
284 unit = (struct function_unit *)
285 alloca (sizeof (struct function_unit));
286 unit->name = (char *) alloca (len);
287 bcopy (name, unit->name, len);
288 unit->multiplicity = multiplicity;
289 unit->simultaneity = simultaneity;
290 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
291 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
292 unit->next = units;
293 units = unit;
294 num_units++;
296 extend_range (&all_multiplicity, multiplicity, multiplicity);
297 extend_range (&all_simultaneity, simultaneity, simultaneity);
299 else if (unit->multiplicity != multiplicity
300 || unit->simultaneity != simultaneity)
301 fatal ("Differing specifications given for `%s' function unit.",
302 unit->name);
304 extend_range (&unit->ready_cost, ready_cost, ready_cost);
305 extend_range (&unit->issue_delay,
306 issueexp_p ? 1 : issue_delay, issue_delay);
307 extend_range (&all_ready_cost,
308 unit->ready_cost.min, unit->ready_cost.max);
309 extend_range (&all_issue_delay,
310 unit->issue_delay.min, unit->issue_delay.max);
314 if (num_units > 0)
316 /* Compute the range of blockage cost values. See genattrtab.c
317 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
319 MAX (ISSUE-DELAY (E,C),
320 READY-COST (E) - (READY-COST (C) - 1))
322 and otherwise
324 MAX (ISSUE-DELAY (E,C),
325 READY-COST (E) - (READY-COST (C) - 1),
326 READY-COST (E) - FILL-TIME) */
328 for (unit = units; unit; unit = unit->next)
330 struct range blockage;
332 blockage = unit->issue_delay;
333 blockage.max = MAX (unit->ready_cost.max
334 - (unit->ready_cost.min - 1),
335 blockage.max);
336 blockage.min = MAX (1, blockage.min);
338 if (unit->simultaneity != 0)
340 int fill_time = ((unit->simultaneity - 1)
341 * unit->issue_delay.min);
342 blockage.min = MAX (unit->ready_cost.min - fill_time,
343 blockage.min);
344 blockage.max = MAX (unit->ready_cost.max - fill_time,
345 blockage.max);
347 extend_range (&all_blockage, blockage.min, blockage.max);
350 write_units (num_units, &all_multiplicity, &all_simultaneity,
351 &all_ready_cost, &all_issue_delay, &all_blockage);
354 /* Output flag masks for use by reorg.
356 Flags are used to hold branch direction and prediction information
357 for use by eligible_for_... */
358 printf("\n#define ATTR_FLAG_forward\t0x1\n");
359 printf("#define ATTR_FLAG_backward\t0x2\n");
360 printf("#define ATTR_FLAG_likely\t0x4\n");
361 printf("#define ATTR_FLAG_very_likely\t0x8\n");
362 printf("#define ATTR_FLAG_unlikely\t0x10\n");
363 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
365 fflush (stdout);
366 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
369 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
370 const char *
371 get_insn_name (code)
372 int code ATTRIBUTE_UNUSED;
374 return NULL;