2010-06-20 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / genattr.c
blobe4609ca41743a47935f9697773acaa3e62897ec0
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000, 2003, 2004, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "read-md.h"
30 #include "gensupport.h"
33 static void write_upcase (const char *);
34 static void gen_attr (rtx);
36 static void
37 write_upcase (const char *str)
39 for (; *str; str++)
40 putchar (TOUPPER(*str));
43 static void
44 gen_attr (rtx attr)
46 const char *p, *tag;
47 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
49 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
51 /* If numeric attribute, don't need to write an enum. */
52 if (GET_CODE (attr) == DEFINE_ENUM_ATTR)
53 printf ("extern enum %s get_attr_%s (%s);\n\n",
54 XSTR (attr, 1), XSTR (attr, 0), (is_const ? "void" : "rtx"));
55 else
57 p = XSTR (attr, 1);
58 if (*p == '\0')
59 printf ("extern int get_attr_%s (%s);\n", XSTR (attr, 0),
60 (is_const ? "void" : "rtx"));
61 else
63 printf ("enum attr_%s {", XSTR (attr, 0));
65 while ((tag = scan_comma_elt (&p)) != 0)
67 write_upcase (XSTR (attr, 0));
68 putchar ('_');
69 while (tag != p)
70 putchar (TOUPPER (*tag++));
71 if (*p == ',')
72 fputs (", ", stdout);
74 fputs ("};\n", stdout);
76 printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
77 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
81 /* If `length' attribute, write additional function definitions and define
82 variables used by `insn_current_length'. */
83 if (! strcmp (XSTR (attr, 0), "length"))
85 puts ("\
86 extern void shorten_branches (rtx);\n\
87 extern int insn_default_length (rtx);\n\
88 extern int insn_min_length (rtx);\n\
89 extern int insn_variable_length_p (rtx);\n\
90 extern int insn_current_length (rtx);\n\n\
91 #include \"insn-addr.h\"\n");
95 int
96 main (int argc, char **argv)
98 rtx desc;
99 int have_delay = 0;
100 int have_annul_true = 0;
101 int have_annul_false = 0;
102 int num_insn_reservations = 0;
103 int i;
105 progname = "genattr";
107 if (!init_rtx_reader_args (argc, argv))
108 return (FATAL_EXIT_CODE);
110 puts ("/* Generated automatically by the program `genattr'");
111 puts (" from the machine description file `md'. */\n");
112 puts ("#ifndef GCC_INSN_ATTR_H");
113 puts ("#define GCC_INSN_ATTR_H\n");
115 /* For compatibility, define the attribute `alternative', which is just
116 a reference to the variable `which_alternative'. */
118 puts ("#define HAVE_ATTR_alternative");
119 puts ("#define get_attr_alternative(insn) which_alternative");
121 /* Read the machine description. */
123 while (1)
125 int line_no, insn_code_number;
127 desc = read_md_rtx (&line_no, &insn_code_number);
128 if (desc == NULL)
129 break;
131 if (GET_CODE (desc) == DEFINE_ATTR
132 || GET_CODE (desc) == DEFINE_ENUM_ATTR)
133 gen_attr (desc);
135 else if (GET_CODE (desc) == DEFINE_DELAY)
137 if (! have_delay)
139 printf ("#define DELAY_SLOTS\n");
140 printf ("extern int num_delay_slots (rtx);\n");
141 printf ("extern int eligible_for_delay (rtx, int, rtx, int);\n\n");
142 printf ("extern int const_num_delay_slots (rtx);\n\n");
143 have_delay = 1;
146 for (i = 0; i < XVECLEN (desc, 1); i += 3)
148 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
150 printf ("#define ANNUL_IFTRUE_SLOTS\n");
151 printf ("extern int eligible_for_annul_true (rtx, int, rtx, int);\n");
152 have_annul_true = 1;
155 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
157 printf ("#define ANNUL_IFFALSE_SLOTS\n");
158 printf ("extern int eligible_for_annul_false (rtx, int, rtx, int);\n");
159 have_annul_false = 1;
164 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
165 num_insn_reservations++;
168 if (num_insn_reservations > 0)
170 /* Output interface for pipeline hazards recognition based on
171 DFA (deterministic finite state automata. */
172 printf ("\n#define INSN_SCHEDULING\n");
173 printf ("\n/* DFA based pipeline interface. */");
174 printf ("\n#ifndef AUTOMATON_ALTS\n");
175 printf ("#define AUTOMATON_ALTS 0\n");
176 printf ("#endif\n\n");
177 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
178 printf ("#define AUTOMATON_STATE_ALTS 0\n");
179 printf ("#endif\n\n");
180 printf ("#ifndef CPU_UNITS_QUERY\n");
181 printf ("#define CPU_UNITS_QUERY 0\n");
182 printf ("#endif\n\n");
183 /* Interface itself: */
184 printf ("/* Internal insn code number used by automata. */\n");
185 printf ("extern int internal_dfa_insn_code (rtx);\n\n");
186 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
187 printf ("extern int insn_default_latency (rtx);\n\n");
188 printf ("/* Return nonzero if there is a bypass for given insn\n");
189 printf (" which is a data producer. */\n");
190 printf ("extern int bypass_p (rtx);\n\n");
191 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
192 printf (" Use the function if bypass_p returns nonzero for\n");
193 printf (" the 1st insn. */\n");
194 printf ("extern int insn_latency (rtx, rtx);\n\n");
195 printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
196 printf (" Use the function if bypass_p returns nonzero for\n");
197 printf (" the 1st insn. */\n");
198 printf ("extern int maximal_insn_latency (rtx);\n\n");
199 printf ("\n#if AUTOMATON_ALTS\n");
200 printf ("/* The following function returns number of alternative\n");
201 printf (" reservations of given insn. It may be used for better\n");
202 printf (" insns scheduling heuristics. */\n");
203 printf ("extern int insn_alts (rtx);\n\n");
204 printf ("#endif\n\n");
205 printf ("/* Maximal possible number of insns waiting results being\n");
206 printf (" produced by insns whose execution is not finished. */\n");
207 printf ("extern const int max_insn_queue_index;\n\n");
208 printf ("/* Pointer to data describing current state of DFA. */\n");
209 printf ("typedef void *state_t;\n\n");
210 printf ("/* Size of the data in bytes. */\n");
211 printf ("extern int state_size (void);\n\n");
212 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
213 printf (" as all functional units were not reserved. */\n");
214 printf ("extern void state_reset (state_t);\n");
215 printf ("/* The following function returns negative value if given\n");
216 printf (" insn can be issued in processor state described by given\n");
217 printf (" DFA state. In this case, the DFA state is changed to\n");
218 printf (" reflect the current and future reservations by given\n");
219 printf (" insn. Otherwise the function returns minimal time\n");
220 printf (" delay to issue the insn. This delay may be zero\n");
221 printf (" for superscalar or VLIW processors. If the second\n");
222 printf (" parameter is NULL the function changes given DFA state\n");
223 printf (" as new processor cycle started. */\n");
224 printf ("extern int state_transition (state_t, rtx);\n");
225 printf ("\n#if AUTOMATON_STATE_ALTS\n");
226 printf ("/* The following function returns number of possible\n");
227 printf (" alternative reservations of given insn in given\n");
228 printf (" DFA state. It may be used for better insns scheduling\n");
229 printf (" heuristics. By default the function is defined if\n");
230 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
231 printf (" implementation may require much memory. */\n");
232 printf ("extern int state_alts (state_t, rtx);\n");
233 printf ("#endif\n\n");
234 printf ("extern int min_issue_delay (state_t, rtx);\n");
235 printf ("/* The following function returns nonzero if no one insn\n");
236 printf (" can be issued in current DFA state. */\n");
237 printf ("extern int state_dead_lock_p (state_t);\n");
238 printf ("/* The function returns minimal delay of issue of the 2nd\n");
239 printf (" insn after issuing the 1st insn in given DFA state.\n");
240 printf (" The 1st insn should be issued in given state (i.e.\n");
241 printf (" state_transition should return negative value for\n");
242 printf (" the insn and the state). Data dependencies between\n");
243 printf (" the insns are ignored by the function. */\n");
244 printf
245 ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
246 printf ("/* The following function outputs reservations for given\n");
247 printf (" insn as they are described in the corresponding\n");
248 printf (" define_insn_reservation. */\n");
249 printf ("extern void print_reservation (FILE *, rtx);\n");
250 printf ("\n#if CPU_UNITS_QUERY\n");
251 printf ("/* The following function returns code of functional unit\n");
252 printf (" with given name (see define_cpu_unit). */\n");
253 printf ("extern int get_cpu_unit_code (const char *);\n");
254 printf ("/* The following function returns nonzero if functional\n");
255 printf (" unit with given code is currently reserved in given\n");
256 printf (" DFA state. */\n");
257 printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
258 printf ("#endif\n\n");
259 printf ("/* The following function returns true if insn\n");
260 printf (" has a dfa reservation. */\n");
261 printf ("extern bool insn_has_dfa_reservation_p (rtx);\n\n");
262 printf ("/* Clean insn code cache. It should be called if there\n");
263 printf (" is a chance that condition value in a\n");
264 printf (" define_insn_reservation will be changed after\n");
265 printf (" last call of dfa_start. */\n");
266 printf ("extern void dfa_clean_insn_cache (void);\n\n");
267 printf ("extern void dfa_clear_single_insn_cache (rtx);\n\n");
268 printf ("/* Initiate and finish work with DFA. They should be\n");
269 printf (" called as the first and the last interface\n");
270 printf (" functions. */\n");
271 printf ("extern void dfa_start (void);\n");
272 printf ("extern void dfa_finish (void);\n");
274 else
276 /* Otherwise we do no scheduling, but we need these typedefs
277 in order to avoid uglifying other code with more ifdefs. */
278 printf ("typedef void *state_t;\n\n");
281 /* Output flag masks for use by reorg.
283 Flags are used to hold branch direction and prediction information
284 for use by eligible_for_... */
285 printf("\n#define ATTR_FLAG_forward\t0x1\n");
286 printf("#define ATTR_FLAG_backward\t0x2\n");
287 printf("#define ATTR_FLAG_likely\t0x4\n");
288 printf("#define ATTR_FLAG_very_likely\t0x8\n");
289 printf("#define ATTR_FLAG_unlikely\t0x10\n");
290 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
292 puts("\n#endif /* GCC_INSN_ATTR_H */");
294 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
295 return FATAL_EXIT_CODE;
297 return SUCCESS_EXIT_CODE;