1*38fd1498Szrj /* Generate from machine description:
2*38fd1498Szrj - some flags HAVE_... saying which simple standard instructions are
3*38fd1498Szrj available for this machine.
4*38fd1498Szrj Copyright (C) 1987-2018 Free Software Foundation, Inc.
5*38fd1498Szrj
6*38fd1498Szrj This file is part of GCC.
7*38fd1498Szrj
8*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
9*38fd1498Szrj the terms of the GNU General Public License as published by the Free
10*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
11*38fd1498Szrj version.
12*38fd1498Szrj
13*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*38fd1498Szrj for more details.
17*38fd1498Szrj
18*38fd1498Szrj You should have received a copy of the GNU General Public License
19*38fd1498Szrj along with GCC; see the file COPYING3. If not see
20*38fd1498Szrj <http://www.gnu.org/licenses/>. */
21*38fd1498Szrj
22*38fd1498Szrj
23*38fd1498Szrj #include "bconfig.h"
24*38fd1498Szrj #include "system.h"
25*38fd1498Szrj #include "coretypes.h"
26*38fd1498Szrj #include "tm.h"
27*38fd1498Szrj #include "rtl.h"
28*38fd1498Szrj #include "obstack.h"
29*38fd1498Szrj #include "errors.h"
30*38fd1498Szrj #include "read-md.h"
31*38fd1498Szrj #include "gensupport.h"
32*38fd1498Szrj
33*38fd1498Szrj /* Obstack to remember insns with. */
34*38fd1498Szrj static struct obstack obstack;
35*38fd1498Szrj
36*38fd1498Szrj /* Max size of names encountered. */
37*38fd1498Szrj static int max_id_len;
38*38fd1498Szrj
39*38fd1498Szrj /* Max operand encountered in a scan over some insn. */
40*38fd1498Szrj static int max_opno;
41*38fd1498Szrj
42*38fd1498Szrj static void max_operand_1 (rtx);
43*38fd1498Szrj static int num_operands (rtx);
44*38fd1498Szrj static void gen_proto (rtx);
45*38fd1498Szrj
46*38fd1498Szrj /* Count the number of match_operand's found. */
47*38fd1498Szrj
48*38fd1498Szrj static void
max_operand_1(rtx x)49*38fd1498Szrj max_operand_1 (rtx x)
50*38fd1498Szrj {
51*38fd1498Szrj RTX_CODE code;
52*38fd1498Szrj int i;
53*38fd1498Szrj int len;
54*38fd1498Szrj const char *fmt;
55*38fd1498Szrj
56*38fd1498Szrj if (x == 0)
57*38fd1498Szrj return;
58*38fd1498Szrj
59*38fd1498Szrj code = GET_CODE (x);
60*38fd1498Szrj
61*38fd1498Szrj if (code == MATCH_OPERAND || code == MATCH_OPERATOR
62*38fd1498Szrj || code == MATCH_PARALLEL)
63*38fd1498Szrj max_opno = MAX (max_opno, XINT (x, 0));
64*38fd1498Szrj
65*38fd1498Szrj fmt = GET_RTX_FORMAT (code);
66*38fd1498Szrj len = GET_RTX_LENGTH (code);
67*38fd1498Szrj for (i = 0; i < len; i++)
68*38fd1498Szrj {
69*38fd1498Szrj if (fmt[i] == 'e' || fmt[i] == 'u')
70*38fd1498Szrj max_operand_1 (XEXP (x, i));
71*38fd1498Szrj else if (fmt[i] == 'E')
72*38fd1498Szrj {
73*38fd1498Szrj int j;
74*38fd1498Szrj for (j = 0; j < XVECLEN (x, i); j++)
75*38fd1498Szrj max_operand_1 (XVECEXP (x, i, j));
76*38fd1498Szrj }
77*38fd1498Szrj }
78*38fd1498Szrj }
79*38fd1498Szrj
80*38fd1498Szrj static int
num_operands(rtx insn)81*38fd1498Szrj num_operands (rtx insn)
82*38fd1498Szrj {
83*38fd1498Szrj int len = XVECLEN (insn, 1);
84*38fd1498Szrj int i;
85*38fd1498Szrj
86*38fd1498Szrj max_opno = -1;
87*38fd1498Szrj
88*38fd1498Szrj for (i = 0; i < len; i++)
89*38fd1498Szrj max_operand_1 (XVECEXP (insn, 1, i));
90*38fd1498Szrj
91*38fd1498Szrj return max_opno + 1;
92*38fd1498Szrj }
93*38fd1498Szrj
94*38fd1498Szrj /* Print out prototype information for a generator function. If the
95*38fd1498Szrj insn pattern has been elided, print out a dummy generator that
96*38fd1498Szrj does nothing. */
97*38fd1498Szrj
98*38fd1498Szrj static void
gen_proto(rtx insn)99*38fd1498Szrj gen_proto (rtx insn)
100*38fd1498Szrj {
101*38fd1498Szrj int num = num_operands (insn);
102*38fd1498Szrj int i;
103*38fd1498Szrj const char *name = XSTR (insn, 0);
104*38fd1498Szrj int truth = maybe_eval_c_test (XSTR (insn, 2));
105*38fd1498Szrj
106*38fd1498Szrj if (truth != 0)
107*38fd1498Szrj printf ("extern rtx gen_%-*s (", max_id_len, name);
108*38fd1498Szrj else
109*38fd1498Szrj printf ("static inline rtx gen_%-*s (", max_id_len, name);
110*38fd1498Szrj
111*38fd1498Szrj if (num == 0)
112*38fd1498Szrj fputs ("void", stdout);
113*38fd1498Szrj else
114*38fd1498Szrj {
115*38fd1498Szrj for (i = 1; i < num; i++)
116*38fd1498Szrj fputs ("rtx, ", stdout);
117*38fd1498Szrj
118*38fd1498Szrj fputs ("rtx", stdout);
119*38fd1498Szrj }
120*38fd1498Szrj
121*38fd1498Szrj puts (");");
122*38fd1498Szrj
123*38fd1498Szrj /* Some back ends want to take the address of generator functions,
124*38fd1498Szrj so we cannot simply use #define for these dummy definitions. */
125*38fd1498Szrj if (truth == 0)
126*38fd1498Szrj {
127*38fd1498Szrj printf ("static inline rtx\ngen_%s", name);
128*38fd1498Szrj if (num > 0)
129*38fd1498Szrj {
130*38fd1498Szrj putchar ('(');
131*38fd1498Szrj for (i = 0; i < num-1; i++)
132*38fd1498Szrj printf ("rtx ARG_UNUSED (%c), ", 'a' + i);
133*38fd1498Szrj printf ("rtx ARG_UNUSED (%c))\n", 'a' + i);
134*38fd1498Szrj }
135*38fd1498Szrj else
136*38fd1498Szrj puts ("(void)");
137*38fd1498Szrj puts ("{\n return 0;\n}");
138*38fd1498Szrj }
139*38fd1498Szrj
140*38fd1498Szrj }
141*38fd1498Szrj
142*38fd1498Szrj static void
gen_insn(md_rtx_info * info)143*38fd1498Szrj gen_insn (md_rtx_info *info)
144*38fd1498Szrj {
145*38fd1498Szrj rtx insn = info->def;
146*38fd1498Szrj const char *name = XSTR (insn, 0);
147*38fd1498Szrj const char *p;
148*38fd1498Szrj const char *lt, *gt;
149*38fd1498Szrj int len;
150*38fd1498Szrj int truth = maybe_eval_c_test (XSTR (insn, 2));
151*38fd1498Szrj
152*38fd1498Szrj lt = strchr (name, '<');
153*38fd1498Szrj if (lt && strchr (lt + 1, '>'))
154*38fd1498Szrj {
155*38fd1498Szrj error_at (info->loc, "unresolved iterator");
156*38fd1498Szrj return;
157*38fd1498Szrj }
158*38fd1498Szrj
159*38fd1498Szrj gt = strchr (name, '>');
160*38fd1498Szrj if (lt || gt)
161*38fd1498Szrj {
162*38fd1498Szrj error_at (info->loc, "unmatched angle brackets, likely "
163*38fd1498Szrj "an error in iterator syntax");
164*38fd1498Szrj return;
165*38fd1498Szrj }
166*38fd1498Szrj
167*38fd1498Szrj /* Don't mention instructions whose names are the null string
168*38fd1498Szrj or begin with '*'. They are in the machine description just
169*38fd1498Szrj to be recognized. */
170*38fd1498Szrj if (name[0] == 0 || name[0] == '*')
171*38fd1498Szrj return;
172*38fd1498Szrj
173*38fd1498Szrj len = strlen (name);
174*38fd1498Szrj
175*38fd1498Szrj if (len > max_id_len)
176*38fd1498Szrj max_id_len = len;
177*38fd1498Szrj
178*38fd1498Szrj if (truth == 0)
179*38fd1498Szrj /* Emit nothing. */;
180*38fd1498Szrj else if (truth == 1)
181*38fd1498Szrj printf ("#define HAVE_%s 1\n", name);
182*38fd1498Szrj else
183*38fd1498Szrj {
184*38fd1498Szrj /* Write the macro definition, putting \'s at the end of each line,
185*38fd1498Szrj if more than one. */
186*38fd1498Szrj printf ("#define HAVE_%s (", name);
187*38fd1498Szrj for (p = XSTR (insn, 2); *p; p++)
188*38fd1498Szrj {
189*38fd1498Szrj if (IS_VSPACE (*p))
190*38fd1498Szrj fputs (" \\\n", stdout);
191*38fd1498Szrj else
192*38fd1498Szrj putchar (*p);
193*38fd1498Szrj }
194*38fd1498Szrj fputs (")\n", stdout);
195*38fd1498Szrj }
196*38fd1498Szrj
197*38fd1498Szrj obstack_grow (&obstack, &insn, sizeof (rtx));
198*38fd1498Szrj }
199*38fd1498Szrj
200*38fd1498Szrj int
main(int argc,const char ** argv)201*38fd1498Szrj main (int argc, const char **argv)
202*38fd1498Szrj {
203*38fd1498Szrj rtx dummy;
204*38fd1498Szrj rtx *insns;
205*38fd1498Szrj rtx *insn_ptr;
206*38fd1498Szrj
207*38fd1498Szrj progname = "genflags";
208*38fd1498Szrj obstack_init (&obstack);
209*38fd1498Szrj
210*38fd1498Szrj /* We need to see all the possibilities. Elided insns may have
211*38fd1498Szrj direct calls to their generators in C code. */
212*38fd1498Szrj insn_elision = 0;
213*38fd1498Szrj
214*38fd1498Szrj if (!init_rtx_reader_args (argc, argv))
215*38fd1498Szrj return (FATAL_EXIT_CODE);
216*38fd1498Szrj
217*38fd1498Szrj puts ("/* Generated automatically by the program `genflags'");
218*38fd1498Szrj puts (" from the machine description file `md'. */\n");
219*38fd1498Szrj puts ("#ifndef GCC_INSN_FLAGS_H");
220*38fd1498Szrj puts ("#define GCC_INSN_FLAGS_H\n");
221*38fd1498Szrj
222*38fd1498Szrj /* Read the machine description. */
223*38fd1498Szrj
224*38fd1498Szrj md_rtx_info info;
225*38fd1498Szrj while (read_md_rtx (&info))
226*38fd1498Szrj switch (GET_CODE (info.def))
227*38fd1498Szrj {
228*38fd1498Szrj case DEFINE_INSN:
229*38fd1498Szrj case DEFINE_EXPAND:
230*38fd1498Szrj gen_insn (&info);
231*38fd1498Szrj break;
232*38fd1498Szrj
233*38fd1498Szrj default:
234*38fd1498Szrj break;
235*38fd1498Szrj }
236*38fd1498Szrj
237*38fd1498Szrj /* Print out the prototypes now. */
238*38fd1498Szrj dummy = (rtx) 0;
239*38fd1498Szrj obstack_grow (&obstack, &dummy, sizeof (rtx));
240*38fd1498Szrj insns = XOBFINISH (&obstack, rtx *);
241*38fd1498Szrj
242*38fd1498Szrj for (insn_ptr = insns; *insn_ptr; insn_ptr++)
243*38fd1498Szrj gen_proto (*insn_ptr);
244*38fd1498Szrj
245*38fd1498Szrj puts ("\n#endif /* GCC_INSN_FLAGS_H */");
246*38fd1498Szrj
247*38fd1498Szrj if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
248*38fd1498Szrj return FATAL_EXIT_CODE;
249*38fd1498Szrj
250*38fd1498Szrj return SUCCESS_EXIT_CODE;
251*38fd1498Szrj }
252