1 /* Generate insn-target-def.h, an automatically-generated part of targetm.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "gensupport.h"
28 #include "hash-table.h"
30 /* This class hashes define_insns and define_expands by name. */
31 struct insn_hasher
: nofree_ptr_hash
<rtx_def
>
33 typedef rtx value_type
;
34 typedef const char *compare_type
;
36 static inline hashval_t
hash (rtx
);
37 static inline bool equal (rtx
, const char *);
41 insn_hasher::hash (rtx x
)
43 return htab_hash_string (XSTR (x
, 0));
47 insn_hasher::equal (rtx x
, const char *y
)
49 return strcmp (XSTR (x
, 0), y
) == 0;
52 /* All define_insns and define_expands, hashed by name. */
53 static hash_table
<insn_hasher
> *insns
;
55 /* Records the prototype suffix X for each invalid_X stub that has been
57 static hash_table
<nofree_string_hash
> *stubs
;
59 /* Records which C conditions have been wrapped in functions, as a mapping
60 from the C condition to the function name. */
61 static hash_map
<nofree_string_hash
, const char *> *have_funcs
;
63 /* Return true if the part of the prototype at P is for an argument
64 name. If so, point *END_OUT to the first character after the name.
65 If OPNO_OUT is nonnull, set *OPNO_OUT to the number of the associated
66 operand. If REQUIRED_OUT is nonnull, set *REQUIRED_OUT to whether the
67 .md pattern is required to match the operand. */
70 parse_argument (const char *p
, const char **end_out
,
71 unsigned int *opno_out
= 0,
72 bool *required_out
= 0)
76 if (p
[0] == 'x' && ISDIGIT (p
[1]))
82 else if (p
[0] == 'o' && p
[1] == 'p' && p
[2] == 't' && ISDIGIT (p
[3]))
86 *required_out
= false;
92 unsigned int opno
= strtol (p
, &endptr
, 10);
100 /* Output hook definitions for pattern NAME, which has target-insns.def
101 prototype PROTOTYPE. */
104 def_target_insn (const char *name
, const char *prototype
)
106 /* Get an upper-case form of NAME. */
108 char *upper_name
= XALLOCAVEC (char, strlen (name
) + 1);
109 for (i
= 0; name
[i
]; ++i
)
110 upper_name
[i
] = TOUPPER (name
[i
]);
113 /* Check that the prototype is valid and concatenate the types
114 together to get a suffix. */
115 char *suffix
= XALLOCAVEC (char, strlen (prototype
) + 1);
117 unsigned int opno
= 0;
118 unsigned int required_ops
= 0;
119 unsigned int this_opno
;
121 for (const char *p
= prototype
; *p
; ++p
)
122 if (parse_argument (p
, &p
, &this_opno
, &required_p
))
124 if (this_opno
!= opno
|| (*p
!= ',' && *p
!= ')'))
126 error ("invalid prototype for '%s'", name
);
127 exit (FATAL_EXIT_CODE
);
129 if (required_p
&& required_ops
< opno
)
131 error ("prototype for '%s' has required operands after"
132 " optional operands", name
);
133 exit (FATAL_EXIT_CODE
);
138 /* Skip over ')'s. */
142 else if (*p
== ')' || *p
== ',')
144 /* We found the end of a parameter without finding a
146 if (strcmp (prototype
, "(void)") != 0)
148 error ("argument %d of '%s' did not have the expected name",
150 exit (FATAL_EXIT_CODE
);
153 else if (*p
!= '(' && !ISSPACE (*p
))
157 /* See whether we have an implementation of this pattern. */
158 hashval_t hash
= htab_hash_string (name
);
160 const char *have_name
= name
;
161 if (rtx insn
= insns
->find_with_hash (name
, hash
))
164 get_pattern_stats (&stats
, XVEC (insn
, 1));
165 unsigned int actual_ops
= stats
.num_generator_args
;
166 if (opno
== required_ops
&& opno
!= actual_ops
)
167 error_at (get_file_location (insn
),
168 "'%s' must have %d operands (excluding match_dups)",
170 else if (actual_ops
< required_ops
)
171 error_at (get_file_location (insn
),
172 "'%s' must have at least %d operands (excluding match_dups)",
174 else if (actual_ops
> opno
)
175 error_at (get_file_location (insn
),
176 "'%s' must have no more than %d operands"
177 " (excluding match_dups)", name
, opno
);
179 const char *test
= XSTR (insn
, 2);
180 truth
= maybe_eval_c_test (test
);
181 gcc_assert (truth
!= 0);
184 /* Try to reuse an existing function that performs the same test. */
186 const char *&entry
= have_funcs
->get_or_insert (test
, &existed
);
190 printf ("\nstatic bool\n");
191 printf ("target_have_%s (void)\n", name
);
194 rtx_reader_ptr
->print_c_condition (test
);
200 printf ("\nstatic rtx_insn *\n");
201 printf ("target_gen_%s ", name
);
202 /* Print the prototype with the argument names after ACTUAL_OPS
204 const char *p
= prototype
, *end
;
206 if (parse_argument (p
, &end
, &this_opno
) && this_opno
>= actual_ops
)
209 fputc (*p
++, stdout
);
213 printf (" gcc_checking_assert (targetm.have_%s ());\n", name
);
214 printf (" return insnify (gen_%s (", name
);
215 for (i
= 0; i
< actual_ops
; ++i
)
216 printf ("%s%s%d", i
== 0 ? "" : ", ",
217 i
< required_ops
? "x" : "opt", i
);
223 const char **slot
= stubs
->find_slot (suffix
, INSERT
);
226 *slot
= xstrdup (suffix
);
227 printf ("\nstatic rtx_insn *\n");
228 printf ("invalid_%s ", suffix
);
229 /* Print the prototype with the argument names removed. */
230 const char *p
= prototype
;
232 if (!parse_argument (p
, &p
))
233 fputc (*p
++, stdout
);
235 printf (" gcc_unreachable ();\n");
239 printf ("\n#undef TARGET_HAVE_%s\n", upper_name
);
240 printf ("#define TARGET_HAVE_%s ", upper_name
);
242 printf ("hook_bool_void_false\n");
244 printf ("hook_bool_void_true\n");
246 printf ("target_have_%s\n", have_name
);
248 printf ("#undef TARGET_GEN_%s\n", upper_name
);
249 printf ("#define TARGET_GEN_%s ", upper_name
);
251 printf ("invalid_%s\n", suffix
);
253 printf ("target_gen_%s\n", name
);
255 printf ("#undef TARGET_CODE_FOR_%s\n", upper_name
);
256 printf ("#define TARGET_CODE_FOR_%s ", upper_name
);
258 printf ("CODE_FOR_nothing\n");
260 printf ("CODE_FOR_%s\n", name
);
263 /* Record the DEFINE_INSN or DEFINE_EXPAND described by INFO. */
266 add_insn (md_rtx_info
*info
)
269 const char *name
= XSTR (def
, 0);
270 if (name
[0] == 0 || name
[0] == '*')
273 hashval_t hash
= htab_hash_string (name
);
274 rtx
*slot
= insns
->find_slot_with_hash (name
, hash
, INSERT
);
276 error_at (info
->loc
, "duplicate definition of '%s'", name
);
282 main (int argc
, const char **argv
)
284 progname
= "gentarget-def";
286 if (!init_rtx_reader_args (argc
, argv
))
287 return (FATAL_EXIT_CODE
);
289 insns
= new hash_table
<insn_hasher
> (31);
290 stubs
= new hash_table
<nofree_string_hash
> (31);
291 have_funcs
= new hash_map
<nofree_string_hash
, const char *>;
294 while (read_md_rtx (&info
))
295 switch (GET_CODE (info
.def
))
306 printf ("/* Generated automatically by the program `gentarget-def'. */\n");
307 printf ("#ifndef GCC_INSN_TARGET_DEF_H\n");
308 printf ("#define GCC_INSN_TARGET_DEF_H\n");
310 /* Output a routine to convert an rtx to an rtx_insn sequence.
311 ??? At some point the gen_* functions themselves should return
313 printf ("\nstatic inline rtx_insn *\n");
314 printf ("insnify (rtx x)\n");
316 printf (" if (!x)\n");
317 printf (" return NULL;\n");
318 printf (" if (rtx_insn *insn = dyn_cast <rtx_insn *> (x))\n");
319 printf (" return insn;\n");
320 printf (" start_sequence ();\n");
321 printf (" emit (x, false);\n");
322 printf (" rtx_insn *res = get_insns ();\n");
323 printf (" end_sequence ();\n");
324 printf (" return res;\n");
327 #define DEF_TARGET_INSN(INSN, ARGS) \
328 def_target_insn (#INSN, #ARGS);
329 #include "target-insns.def"
330 #undef DEF_TARGET_INSN
332 printf ("\n#endif /* GCC_INSN_TARGET_DEF_H */\n");
334 if (have_error
|| ferror (stdout
) || fflush (stdout
) || fclose (stdout
))
335 return FATAL_EXIT_CODE
;
337 return SUCCESS_EXIT_CODE
;