1 /* Generate from machine description:
2 - some flags HAVE_... saying which simple standard instructions are
3 available for this machine.
4 Copyright (C) 1987-2015 Free Software Foundation, Inc.
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
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
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/>. */
25 #include "coretypes.h"
31 #include "gensupport.h"
33 /* Obstack to remember insns with. */
34 static struct obstack obstack
;
36 /* Max size of names encountered. */
37 static int max_id_len
;
39 /* Max operand encountered in a scan over some insn. */
42 static void max_operand_1 (rtx
);
43 static int num_operands (rtx
);
44 static void gen_proto (rtx
);
45 static void gen_macro (const char *, int, int);
47 /* Count the number of match_operand's found. */
62 if (code
== MATCH_OPERAND
|| code
== MATCH_OPERATOR
63 || code
== MATCH_PARALLEL
)
64 max_opno
= MAX (max_opno
, XINT (x
, 0));
66 fmt
= GET_RTX_FORMAT (code
);
67 len
= GET_RTX_LENGTH (code
);
68 for (i
= 0; i
< len
; i
++)
70 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
71 max_operand_1 (XEXP (x
, i
));
72 else if (fmt
[i
] == 'E')
75 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
76 max_operand_1 (XVECEXP (x
, i
, j
));
82 num_operands (rtx insn
)
84 int len
= XVECLEN (insn
, 1);
89 for (i
= 0; i
< len
; i
++)
90 max_operand_1 (XVECEXP (insn
, 1, i
));
95 /* Print out a wrapper macro for a function which corrects the number
96 of arguments it takes. Any missing arguments are assumed to be at
99 gen_macro (const char *name
, int real
, int expect
)
103 gcc_assert (real
<= expect
);
106 /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
107 fputs ("#define GEN_", stdout
);
108 for (i
= 0; name
[i
]; i
++)
109 putchar (TOUPPER (name
[i
]));
112 for (i
= 0; i
< expect
- 1; i
++)
113 printf ("%c, ", i
+ 'A');
114 printf ("%c) gen_%s (", i
+ 'A', name
);
116 for (i
= 0; i
< real
- 1; i
++)
117 printf ("(%c), ", i
+ 'A');
118 printf ("(%c))\n", i
+ 'A');
121 /* Print out prototype information for a generator function. If the
122 insn pattern has been elided, print out a dummy generator that
128 int num
= num_operands (insn
);
130 const char *name
= XSTR (insn
, 0);
131 int truth
= maybe_eval_c_test (XSTR (insn
, 2));
133 /* Many md files don't refer to the last two operands passed to the
134 call patterns. This means their generator functions will be two
135 arguments too short. Instead of changing every md file to touch
136 those operands, we wrap the prototypes in macros that take the
137 correct number of arguments. */
138 if (name
[0] == 'c' || name
[0] == 's')
140 if (!strcmp (name
, "call")
141 || !strcmp (name
, "call_pop")
142 || !strcmp (name
, "sibcall")
143 || !strcmp (name
, "sibcall_pop"))
144 gen_macro (name
, num
, 4);
145 else if (!strcmp (name
, "call_value")
146 || !strcmp (name
, "call_value_pop")
147 || !strcmp (name
, "sibcall_value")
148 || !strcmp (name
, "sibcall_value_pop"))
149 gen_macro (name
, num
, 5);
153 printf ("extern rtx gen_%-*s (", max_id_len
, name
);
155 printf ("static inline rtx gen_%-*s (", max_id_len
, name
);
158 fputs ("void", stdout
);
161 for (i
= 1; i
< num
; i
++)
162 fputs ("rtx, ", stdout
);
164 fputs ("rtx", stdout
);
169 /* Some back ends want to take the address of generator functions,
170 so we cannot simply use #define for these dummy definitions. */
173 printf ("static inline rtx\ngen_%s", name
);
177 for (i
= 0; i
< num
-1; i
++)
178 printf ("rtx ARG_UNUSED (%c), ", 'a' + i
);
179 printf ("rtx ARG_UNUSED (%c))\n", 'a' + i
);
183 puts ("{\n return 0;\n}");
189 gen_insn (md_rtx_info
*info
)
191 rtx insn
= info
->def
;
192 const char *name
= XSTR (insn
, 0);
196 int truth
= maybe_eval_c_test (XSTR (insn
, 2));
198 lt
= strchr (name
, '<');
199 if (lt
&& strchr (lt
+ 1, '>'))
201 error_at (info
->loc
, "unresolved iterator");
205 gt
= strchr (name
, '>');
208 error_at (info
->loc
, "unmatched angle brackets, likely "
209 "an error in iterator syntax");
213 /* Don't mention instructions whose names are the null string
214 or begin with '*'. They are in the machine description just
216 if (name
[0] == 0 || name
[0] == '*')
221 if (len
> max_id_len
)
227 printf ("#define HAVE_%s 1\n", name
);
230 /* Write the macro definition, putting \'s at the end of each line,
232 printf ("#define HAVE_%s (", name
);
233 for (p
= XSTR (insn
, 2); *p
; p
++)
236 fputs (" \\\n", stdout
);
240 fputs (")\n", stdout
);
243 obstack_grow (&obstack
, &insn
, sizeof (rtx
));
247 main (int argc
, char **argv
)
253 progname
= "genflags";
254 obstack_init (&obstack
);
256 /* We need to see all the possibilities. Elided insns may have
257 direct calls to their generators in C code. */
260 if (!init_rtx_reader_args (argc
, argv
))
261 return (FATAL_EXIT_CODE
);
263 puts ("/* Generated automatically by the program `genflags'");
264 puts (" from the machine description file `md'. */\n");
265 puts ("#ifndef GCC_INSN_FLAGS_H");
266 puts ("#define GCC_INSN_FLAGS_H\n");
268 /* Read the machine description. */
271 while (read_md_rtx (&info
))
272 switch (GET_CODE (info
.def
))
283 /* Print out the prototypes now. */
285 obstack_grow (&obstack
, &dummy
, sizeof (rtx
));
286 insns
= XOBFINISH (&obstack
, rtx
*);
288 for (insn_ptr
= insns
; *insn_ptr
; insn_ptr
++)
289 gen_proto (*insn_ptr
);
291 puts ("\n#endif /* GCC_INSN_FLAGS_H */");
293 if (have_error
|| ferror (stdout
) || fflush (stdout
) || fclose (stdout
))
294 return FATAL_EXIT_CODE
;
296 return SUCCESS_EXIT_CODE
;