]> gcc.gnu.org Git - gcc.git/blame - gcc/genattr.c
Daily bump.
[gcc.git] / gcc / genattr.c
CommitLineData
f12b8918 1/* Generate attribute information (insn-attr.h) from machine description.
3b708058 2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
c9309570 3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
41299f41 4
1322177d 5This file is part of GCC.
41299f41 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
41299f41 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
41299f41
TW
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
41299f41
TW
21
22
0d64891c 23#include "hconfig.h"
0b93b64e 24#include "system.h"
41299f41 25#include "rtl.h"
f8b6598e 26#include "errors.h"
c88c0d42 27#include "gensupport.h"
41299f41 28
41299f41 29
ef3fad04
TW
30/* A range of values. */
31
f12b8918
TM
32struct range
33{
34 int min;
35 int max;
36};
ef3fad04 37
f12b8918
TM
38/* Record information about each function unit mentioned in a
39 DEFINE_FUNCTION_UNIT. */
ef3fad04 40
f12b8918
TM
41struct function_unit
42{
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. */
50};
b29d5f75 51
a94ae8f5
KG
52static void extend_range PARAMS ((struct range *, int, int));
53static void init_range PARAMS ((struct range *));
54static void write_upcase PARAMS ((const char *));
55static void gen_attr PARAMS ((rtx));
56static void write_units PARAMS ((int, struct range *, struct range *,
56c0e996
BS
57 struct range *, struct range *,
58 struct range *));
ef3fad04 59static void
f12b8918
TM
60extend_range (range, min, max)
61 struct range *range;
62 int min;
63 int max;
ef3fad04 64{
f12b8918
TM
65 if (range->min > min) range->min = min;
66 if (range->max < max) range->max = max;
ef3fad04
TW
67}
68
69static void
f12b8918
TM
70init_range (range)
71 struct range *range;
ef3fad04 72{
f12b8918
TM
73 range->min = 100000;
74 range->max = -1;
ef3fad04
TW
75}
76
41299f41
TW
77static void
78write_upcase (str)
92a438d1 79 const char *str;
41299f41 80{
f12b8918 81 for (; *str; str++)
92a438d1 82 putchar (TOUPPER(*str));
4beba5da 83}
4beba5da
TM
84
85static void
f12b8918
TM
86gen_attr (attr)
87 rtx attr;
4beba5da 88{
9a5834ae 89 const char *p, *tag;
ffee6d93 90 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
41299f41 91
f12b8918 92 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
41299f41 93
f12b8918 94 /* If numeric attribute, don't need to write an enum. */
9a5834ae
ZW
95 p = XSTR (attr, 1);
96 if (*p == '\0')
a94ae8f5 97 printf ("extern int get_attr_%s PARAMS ((%s));\n", XSTR (attr, 0),
ffee6d93 98 (is_const ? "void" : "rtx"));
41299f41
TW
99 else
100 {
f12b8918 101 printf ("enum attr_%s {", XSTR (attr, 0));
4beba5da 102
9a5834ae 103 while ((tag = scan_comma_elt (&p)) != 0)
41299f41 104 {
9a5834ae
ZW
105 write_upcase (XSTR (attr, 0));
106 putchar ('_');
107 while (tag != p)
108 putchar (TOUPPER (*tag++));
0b42c8f8
ZW
109 if (*p == ',')
110 fputs (", ", stdout);
41299f41
TW
111 }
112
9a5834ae 113 fputs ("};\n", stdout);
a94ae8f5 114 printf ("extern enum attr_%s get_attr_%s PARAMS ((%s));\n\n",
ffee6d93 115 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
41299f41
TW
116 }
117
f12b8918
TM
118 /* If `length' attribute, write additional function definitions and define
119 variables used by `insn_current_length'. */
120 if (! strcmp (XSTR (attr, 0), "length"))
41299f41 121 {
9a5834ae
ZW
122 puts ("\
123extern void shorten_branches PARAMS ((rtx));\n\
124extern int insn_default_length PARAMS ((rtx));\n\
125extern int insn_variable_length_p PARAMS ((rtx));\n\
126extern int insn_current_length PARAMS ((rtx));\n\n\
127#include \"insn-addr.h\"\n");
41299f41
TW
128 }
129}
130
131static void
f12b8918
TM
132write_units (num_units, multiplicity, simultaneity,
133 ready_cost, issue_delay, blockage)
134 int num_units;
135 struct range *multiplicity;
136 struct range *simultaneity;
137 struct range *ready_cost;
138 struct range *issue_delay;
139 struct range *blockage;
140{
141 int i, q_size;
142
143 printf ("#define INSN_SCHEDULING\n\n");
a94ae8f5
KG
144 printf ("extern int result_ready_cost PARAMS ((rtx));\n");
145 printf ("extern int function_units_used PARAMS ((rtx));\n\n");
8b60264b 146 printf ("extern const struct function_unit_desc\n");
41299f41 147 printf ("{\n");
8b60264b
KG
148 printf (" const char *const name;\n");
149 printf (" const int bitmask;\n");
150 printf (" const int multiplicity;\n");
151 printf (" const int simultaneity;\n");
152 printf (" const int default_cost;\n");
153 printf (" const int max_issue_delay;\n");
154 printf (" int (*const ready_cost_function) PARAMS ((rtx));\n");
155 printf (" int (*const conflict_cost_function) PARAMS ((rtx, rtx));\n");
156 printf (" const int max_blockage;\n");
157 printf (" unsigned int (*const blockage_range_function) PARAMS ((rtx));\n");
158 printf (" int (*const blockage_function) PARAMS ((rtx, rtx));\n");
f12b8918
TM
159 printf ("} function_units[];\n\n");
160 printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
161 printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
162 printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
163 printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
164 printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
165 printf ("#define MIN_READY_COST %d\n", ready_cost->min);
166 printf ("#define MAX_READY_COST %d\n", ready_cost->max);
167 printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
168 printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
169 printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
170 printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
171 for (i = 0; (1 << i) < blockage->max; i++)
ef3fad04 172 ;
f12b8918 173 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
4beba5da 174
f12b8918
TM
175 /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
176 MAX_READY_COST. This is the longest time an isnsn may be queued. */
177 i = MAX (blockage->max, ready_cost->max);
178 for (q_size = 1; q_size <= i; q_size <<= 1)
179 ;
180 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
41299f41
TW
181}
182
a94ae8f5 183extern int main PARAMS ((int, char **));
c1b59dce 184
41299f41
TW
185int
186main (argc, argv)
187 int argc;
188 char **argv;
189{
190 rtx desc;
f12b8918
TM
191 int have_delay = 0;
192 int have_annul_true = 0;
193 int have_annul_false = 0;
fae15c93 194 int num_insn_reservations = 0;
f12b8918
TM
195 int num_units = 0;
196 struct range all_simultaneity, all_multiplicity;
197 struct range all_ready_cost, all_issue_delay, all_blockage;
198 struct function_unit *units = 0, *unit;
41299f41
TW
199 int i;
200
f12b8918
TM
201 init_range (&all_multiplicity);
202 init_range (&all_simultaneity);
203 init_range (&all_ready_cost);
204 init_range (&all_issue_delay);
205 init_range (&all_blockage);
206
f8b6598e 207 progname = "genattr";
41299f41
TW
208
209 if (argc <= 1)
1f978f5f 210 fatal ("no input file name");
41299f41 211
04d8aa70 212 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
c88c0d42 213 return (FATAL_EXIT_CODE);
41299f41 214
0313e85b
ZW
215 puts ("/* Generated automatically by the program `genattr'");
216 puts (" from the machine description file `md'. */\n");
217 puts ("#ifndef GCC_INSN_ATTR_H");
218 puts ("#define GCC_INSN_ATTR_H\n");
41299f41 219
f12b8918
TM
220 /* For compatibility, define the attribute `alternative', which is just
221 a reference to the variable `which_alternative'. */
222
0313e85b
ZW
223 puts ("#define HAVE_ATTR_alternative");
224 puts ("#define get_attr_alternative(insn) which_alternative");
f12b8918 225
41299f41
TW
226 /* Read the machine description. */
227
228 while (1)
229 {
c88c0d42
CP
230 int line_no, insn_code_number;
231
232 desc = read_md_rtx (&line_no, &insn_code_number);
233 if (desc == NULL)
41299f41 234 break;
41299f41 235
f12b8918
TM
236 if (GET_CODE (desc) == DEFINE_ATTR)
237 gen_attr (desc);
41299f41 238
f12b8918
TM
239 else if (GET_CODE (desc) == DEFINE_DELAY)
240 {
241 if (! have_delay)
242 {
243 printf ("#define DELAY_SLOTS\n");
a94ae8f5
KG
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");
f12b8918
TM
247 have_delay = 1;
248 }
41299f41 249
f12b8918
TM
250 for (i = 0; i < XVECLEN (desc, 1); i += 3)
251 {
252 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
253 {
254 printf ("#define ANNUL_IFTRUE_SLOTS\n");
a94ae8f5 255 printf ("extern int eligible_for_annul_true PARAMS ((rtx, int, rtx, int));\n");
f12b8918
TM
256 have_annul_true = 1;
257 }
ef3fad04 258
f12b8918
TM
259 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
260 {
261 printf ("#define ANNUL_IFFALSE_SLOTS\n");
a94ae8f5 262 printf ("extern int eligible_for_annul_false PARAMS ((rtx, int, rtx, int));\n");
f12b8918
TM
263 have_annul_false = 1;
264 }
265 }
266 }
ef3fad04 267
4beba5da
TM
268 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
269 {
3cce094d 270 const char *name = XSTR (desc, 0);
f12b8918
TM
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);
ef3fad04 276
f12b8918
TM
277 for (unit = units; unit; unit = unit->next)
278 if (strcmp (unit->name, name) == 0)
279 break;
ef3fad04 280
f12b8918
TM
281 if (unit == 0)
282 {
f12b8918 283 unit = (struct function_unit *)
b548dffb
ZW
284 xmalloc (sizeof (struct function_unit));
285 unit->name = xstrdup (name);
f12b8918
TM
286 unit->multiplicity = multiplicity;
287 unit->simultaneity = simultaneity;
288 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
289 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
290 unit->next = units;
291 units = unit;
292 num_units++;
293
294 extend_range (&all_multiplicity, multiplicity, multiplicity);
295 extend_range (&all_simultaneity, simultaneity, simultaneity);
296 }
297 else if (unit->multiplicity != multiplicity
298 || unit->simultaneity != simultaneity)
357351e5 299 fatal ("Differing specifications given for `%s' function unit",
f12b8918 300 unit->name);
ef3fad04 301
f12b8918
TM
302 extend_range (&unit->ready_cost, ready_cost, ready_cost);
303 extend_range (&unit->issue_delay,
304 issueexp_p ? 1 : issue_delay, issue_delay);
305 extend_range (&all_ready_cost,
306 unit->ready_cost.min, unit->ready_cost.max);
307 extend_range (&all_issue_delay,
308 unit->issue_delay.min, unit->issue_delay.max);
309 }
fae15c93
VM
310 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
311 num_insn_reservations++;
f12b8918 312 }
ef3fad04 313
fae15c93 314 if (num_units > 0 || num_insn_reservations > 0)
f12b8918 315 {
fae15c93
VM
316 if (num_units > 0)
317 printf ("#define TRADITIONAL_PIPELINE_INTERFACE 1\n");
318
319 if (num_insn_reservations > 0)
320 printf ("#define DFA_PIPELINE_INTERFACE 1\n");
321
f12b8918
TM
322 /* Compute the range of blockage cost values. See genattrtab.c
323 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
ef3fad04 324
f12b8918
TM
325 MAX (ISSUE-DELAY (E,C),
326 READY-COST (E) - (READY-COST (C) - 1))
ef3fad04 327
f12b8918 328 and otherwise
4beba5da 329
f12b8918
TM
330 MAX (ISSUE-DELAY (E,C),
331 READY-COST (E) - (READY-COST (C) - 1),
332 READY-COST (E) - FILL-TIME) */
4beba5da 333
f12b8918
TM
334 for (unit = units; unit; unit = unit->next)
335 {
336 struct range blockage;
4beba5da 337
f12b8918
TM
338 blockage = unit->issue_delay;
339 blockage.max = MAX (unit->ready_cost.max
340 - (unit->ready_cost.min - 1),
341 blockage.max);
342 blockage.min = MAX (1, blockage.min);
4beba5da 343
f12b8918
TM
344 if (unit->simultaneity != 0)
345 {
346 int fill_time = ((unit->simultaneity - 1)
347 * unit->issue_delay.min);
348 blockage.min = MAX (unit->ready_cost.min - fill_time,
349 blockage.min);
350 blockage.max = MAX (unit->ready_cost.max - fill_time,
351 blockage.max);
352 }
353 extend_range (&all_blockage, blockage.min, blockage.max);
354 }
4beba5da 355
f12b8918
TM
356 write_units (num_units, &all_multiplicity, &all_simultaneity,
357 &all_ready_cost, &all_issue_delay, &all_blockage);
fae15c93
VM
358
359 /* Output interface for pipeline hazards recognition based on
360 DFA (deterministic finite state automata. */
361 printf ("\n/* DFA based pipeline interface. */");
362 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
363 printf ("#define AUTOMATON_STATE_ALTS 0\n");
364 printf ("#endif\n\n");
365 printf ("#ifndef CPU_UNITS_QUERY\n");
366 printf ("#define CPU_UNITS_QUERY 0\n");
367 printf ("#endif\n\n");
368 /* Interface itself: */
369 printf ("extern int max_dfa_issue_rate;\n\n");
370 printf ("/* The following macro value is calculated from the\n");
371 printf (" automaton based pipeline description and is equal to\n");
372 printf (" maximal number of all insns described in constructions\n");
373 printf (" `define_insn_reservation' which can be issued on the\n");
374 printf (" same processor cycle. */\n");
375 printf ("#define MAX_DFA_ISSUE_RATE max_dfa_issue_rate\n\n");
376 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
377 printf ("extern int insn_default_latency PARAMS ((rtx));\n\n");
378 printf ("/* Return nonzero if there is a bypass for given insn\n");
379 printf (" which is a data producer. */\n");
380 printf ("extern int bypass_p PARAMS ((rtx));\n\n");
381 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
382 printf (" Use the function if bypass_p returns nonzero for\n");
383 printf (" the 1st insn. */\n");
384 printf ("extern int insn_latency PARAMS ((rtx, rtx));\n\n");
385 printf ("/* The following function returns number of alternative\n");
386 printf (" reservations of given insn. It may be used for better\n");
387 printf (" insns scheduling heuristics. */\n");
388 printf ("extern int insn_alts PARAMS ((rtx));\n\n");
389 printf ("/* Maximal possible number of insns waiting results being\n");
390 printf (" produced by insns whose execution is not finished. */\n");
391 printf ("extern int max_insn_queue_index;\n\n");
392 printf ("/* Pointer to data describing current state of DFA. */\n");
393 printf ("typedef void *state_t;\n\n");
394 printf ("/* Size of the data in bytes. */\n");
395 printf ("extern int state_size PARAMS ((void));\n\n");
396 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
397 printf (" as all functional units were not reserved. */\n");
398 printf ("extern void state_reset PARAMS ((state_t));\n");
399 printf ("/* The following function returns negative value if given\n");
400 printf (" insn can be issued in processor state described by given\n");
401 printf (" DFA state. In this case, the DFA state is changed to\n");
402 printf (" reflect the current and future reservations by given\n");
403 printf (" insn. Otherwise the function returns minimal time\n");
404 printf (" delay to issue the insn. This delay may be zero\n");
405 printf (" for superscalar or VLIW processors. If the second\n");
406 printf (" parameter is NULL the function changes given DFA state\n");
407 printf (" as new processor cycle started. */\n");
408 printf ("extern int state_transition PARAMS ((state_t, rtx));\n");
409 printf ("\n#if AUTOMATON_STATE_ALTS\n");
410 printf ("/* The following function returns number of possible\n");
411 printf (" alternative reservations of given insn in given\n");
412 printf (" DFA state. It may be used for better insns scheduling\n");
413 printf (" heuristics. By default the function is defined if\n");
414 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
415 printf (" implementation may require much memory. */\n");
416 printf ("extern int state_alts PARAMS ((state_t, rtx));\n");
417 printf ("#endif\n\n");
418 printf ("extern int min_issue_delay PARAMS ((state_t, rtx));\n");
419 printf ("/* The following function returns nonzero if no one insn\n");
420 printf (" can be issued in current DFA state. */\n");
421 printf ("extern int state_dead_lock_p PARAMS ((state_t));\n");
422 printf ("/* The function returns minimal delay of issue of the 2nd\n");
423 printf (" insn after issuing the 1st insn in given DFA state.\n");
424 printf (" The 1st insn should be issued in given state (i.e.\n");
425 printf (" state_transition should return negative value for\n");
426 printf (" the insn and the state). Data dependencies between\n");
427 printf (" the insns are ignored by the function. */\n");
428 printf
429 ("extern int min_insn_conflict_delay PARAMS ((state_t, rtx, rtx));\n");
430 printf ("/* The following function outputs reservations for given\n");
431 printf (" insn as they are described in the corresponding\n");
432 printf (" define_insn_reservation. */\n");
433 printf ("extern void print_reservation PARAMS ((FILE *, rtx));\n");
434 printf ("\n#if CPU_UNITS_QUERY\n");
435 printf ("/* The following function returns code of functional unit\n");
436 printf (" with given name (see define_cpu_unit). */\n");
437 printf ("extern int get_cpu_unit_code PARAMS ((const char *));\n");
438 printf ("/* The following function returns nonzero if functional\n");
439 printf (" unit with given code is currently reserved in given\n");
440 printf (" DFA state. */\n");
441 printf ("extern int cpu_unit_reservation_p PARAMS ((state_t, int));\n");
442 printf ("#endif\n\n");
443 printf ("/* Initiate and finish work with DFA. They should be\n");
444 printf (" called as the first and the last interface\n");
445 printf (" functions. */\n");
446 printf ("extern void dfa_start PARAMS ((void));\n");
447 printf ("extern void dfa_finish PARAMS ((void));\n");
448 }
449 else
450 {
451 /* Otherwise we do no scheduling, but we need these typedefs
452 in order to avoid uglifying other code with more ifdefs. */
453 printf ("typedef void *state_t;\n\n");
a224278b 454 }
b29d5f75 455
eaa48dab
JL
456 /* Output flag masks for use by reorg.
457
458 Flags are used to hold branch direction and prediction information
459 for use by eligible_for_... */
460 printf("\n#define ATTR_FLAG_forward\t0x1\n");
461 printf("#define ATTR_FLAG_backward\t0x2\n");
462 printf("#define ATTR_FLAG_likely\t0x4\n");
463 printf("#define ATTR_FLAG_very_likely\t0x8\n");
464 printf("#define ATTR_FLAG_unlikely\t0x10\n");
465 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
466
0313e85b
ZW
467 puts("\n#endif /* GCC_INSN_ATTR_H */");
468
469 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
470 return FATAL_EXIT_CODE;
471
472 return SUCCESS_EXIT_CODE;
41299f41 473}
a995e389
RH
474
475/* Define this so we can link with print-rtl.o to get debug_rtx function. */
476const char *
477get_insn_name (code)
c1b59dce 478 int code ATTRIBUTE_UNUSED;
a995e389
RH
479{
480 return NULL;
481}
This page took 1.400132 seconds and 5 git commands to generate.