1*38fd1498Szrj /* Generate code from to output assembler insns as recognized from rtl.
2*38fd1498Szrj Copyright (C) 1987-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj
21*38fd1498Szrj /* This program reads the machine description for the compiler target machine
22*38fd1498Szrj and produces a file containing these things:
23*38fd1498Szrj
24*38fd1498Szrj 1. An array of `struct insn_data_d', which is indexed by insn code number,
25*38fd1498Szrj which contains:
26*38fd1498Szrj
27*38fd1498Szrj a. `name' is the name for that pattern. Nameless patterns are
28*38fd1498Szrj given a name.
29*38fd1498Szrj
30*38fd1498Szrj b. `output' hold either the output template, an array of output
31*38fd1498Szrj templates, or an output function.
32*38fd1498Szrj
33*38fd1498Szrj c. `genfun' is the function to generate a body for that pattern,
34*38fd1498Szrj given operands as arguments.
35*38fd1498Szrj
36*38fd1498Szrj d. `n_operands' is the number of distinct operands in the pattern
37*38fd1498Szrj for that insn,
38*38fd1498Szrj
39*38fd1498Szrj e. `n_dups' is the number of match_dup's that appear in the insn's
40*38fd1498Szrj pattern. This says how many elements of `recog_data.dup_loc' are
41*38fd1498Szrj significant after an insn has been recognized.
42*38fd1498Szrj
43*38fd1498Szrj f. `n_alternatives' is the number of alternatives in the constraints
44*38fd1498Szrj of each pattern.
45*38fd1498Szrj
46*38fd1498Szrj g. `output_format' tells what type of thing `output' is.
47*38fd1498Szrj
48*38fd1498Szrj h. `operand' is the base of an array of operand data for the insn.
49*38fd1498Szrj
50*38fd1498Szrj 2. An array of `struct insn_operand data', used by `operand' above.
51*38fd1498Szrj
52*38fd1498Szrj a. `predicate', an int-valued function, is the match_operand predicate
53*38fd1498Szrj for this operand.
54*38fd1498Szrj
55*38fd1498Szrj b. `constraint' is the constraint for this operand.
56*38fd1498Szrj
57*38fd1498Szrj c. `address_p' indicates that the operand appears within ADDRESS
58*38fd1498Szrj rtx's.
59*38fd1498Szrj
60*38fd1498Szrj d. `mode' is the machine mode that that operand is supposed to have.
61*38fd1498Szrj
62*38fd1498Szrj e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
63*38fd1498Szrj
64*38fd1498Szrj f. `eliminable', is nonzero for operands that are matched normally by
65*38fd1498Szrj MATCH_OPERAND; it is zero for operands that should not be changed during
66*38fd1498Szrj register elimination such as MATCH_OPERATORs.
67*38fd1498Szrj
68*38fd1498Szrj g. `allows_mem', is true for operands that accept MEM rtxes.
69*38fd1498Szrj
70*38fd1498Szrj The code number of an insn is simply its position in the machine
71*38fd1498Szrj description; code numbers are assigned sequentially to entries in
72*38fd1498Szrj the description, starting with code number 0.
73*38fd1498Szrj
74*38fd1498Szrj Thus, the following entry in the machine description
75*38fd1498Szrj
76*38fd1498Szrj (define_insn "clrdf"
77*38fd1498Szrj [(set (match_operand:DF 0 "general_operand" "")
78*38fd1498Szrj (const_int 0))]
79*38fd1498Szrj ""
80*38fd1498Szrj "clrd %0")
81*38fd1498Szrj
82*38fd1498Szrj assuming it is the 25th entry present, would cause
83*38fd1498Szrj insn_data[24].template to be "clrd %0", and
84*38fd1498Szrj insn_data[24].n_operands to be 1. */
85*38fd1498Szrj
86*38fd1498Szrj #include "bconfig.h"
87*38fd1498Szrj #include "system.h"
88*38fd1498Szrj #include "coretypes.h"
89*38fd1498Szrj #include "tm.h"
90*38fd1498Szrj #include "rtl.h"
91*38fd1498Szrj #include "errors.h"
92*38fd1498Szrj #include "read-md.h"
93*38fd1498Szrj #include "gensupport.h"
94*38fd1498Szrj
95*38fd1498Szrj /* No instruction can have more operands than this. Sorry for this
96*38fd1498Szrj arbitrary limit, but what machine will have an instruction with
97*38fd1498Szrj this many operands? */
98*38fd1498Szrj
99*38fd1498Szrj #define MAX_MAX_OPERANDS 40
100*38fd1498Szrj
101*38fd1498Szrj static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
102*38fd1498Szrj
103*38fd1498Szrj static int n_occurrences (int, const char *);
104*38fd1498Szrj static const char *strip_whitespace (const char *);
105*38fd1498Szrj
106*38fd1498Szrj /* This counts all operands used in the md file. The first is null. */
107*38fd1498Szrj
108*38fd1498Szrj static int next_operand_number = 1;
109*38fd1498Szrj
110*38fd1498Szrj /* Record in this chain all information about the operands we will output. */
111*38fd1498Szrj
112*38fd1498Szrj struct operand_data
113*38fd1498Szrj {
114*38fd1498Szrj struct operand_data *next;
115*38fd1498Szrj int index;
116*38fd1498Szrj const char *predicate;
117*38fd1498Szrj const char *constraint;
118*38fd1498Szrj machine_mode mode;
119*38fd1498Szrj unsigned char n_alternatives;
120*38fd1498Szrj char address_p;
121*38fd1498Szrj char strict_low;
122*38fd1498Szrj char eliminable;
123*38fd1498Szrj char seen;
124*38fd1498Szrj };
125*38fd1498Szrj
126*38fd1498Szrj /* Begin with a null operand at index 0. */
127*38fd1498Szrj
128*38fd1498Szrj static struct operand_data null_operand =
129*38fd1498Szrj {
130*38fd1498Szrj 0, 0, "", "", E_VOIDmode, 0, 0, 0, 0, 0
131*38fd1498Szrj };
132*38fd1498Szrj
133*38fd1498Szrj static struct operand_data *odata = &null_operand;
134*38fd1498Szrj static struct operand_data **odata_end = &null_operand.next;
135*38fd1498Szrj
136*38fd1498Szrj /* Must match the constants in recog.h. */
137*38fd1498Szrj
138*38fd1498Szrj #define INSN_OUTPUT_FORMAT_NONE 0 /* abort */
139*38fd1498Szrj #define INSN_OUTPUT_FORMAT_SINGLE 1 /* const char * */
140*38fd1498Szrj #define INSN_OUTPUT_FORMAT_MULTI 2 /* const char * const * */
141*38fd1498Szrj #define INSN_OUTPUT_FORMAT_FUNCTION 3 /* const char * (*)(...) */
142*38fd1498Szrj
143*38fd1498Szrj /* Record in this chain all information that we will output,
144*38fd1498Szrj associated with the code number of the insn. */
145*38fd1498Szrj
146*38fd1498Szrj struct data
147*38fd1498Szrj {
148*38fd1498Szrj struct data *next;
149*38fd1498Szrj const char *name;
150*38fd1498Szrj const char *template_code;
151*38fd1498Szrj file_location loc;
152*38fd1498Szrj int code_number;
153*38fd1498Szrj int n_generator_args; /* Number of arguments passed to generator */
154*38fd1498Szrj int n_operands; /* Number of operands this insn recognizes */
155*38fd1498Szrj int n_dups; /* Number times match_dup appears in pattern */
156*38fd1498Szrj int n_alternatives; /* Number of alternatives in each constraint */
157*38fd1498Szrj int operand_number; /* Operand index in the big array. */
158*38fd1498Szrj int output_format; /* INSN_OUTPUT_FORMAT_*. */
159*38fd1498Szrj struct operand_data operand[MAX_MAX_OPERANDS];
160*38fd1498Szrj };
161*38fd1498Szrj
162*38fd1498Szrj /* This variable points to the first link in the insn chain. */
163*38fd1498Szrj static struct data *idata;
164*38fd1498Szrj
165*38fd1498Szrj /* This variable points to the end of the insn chain. This is where
166*38fd1498Szrj everything relevant from the machien description is appended to. */
167*38fd1498Szrj static struct data **idata_end;
168*38fd1498Szrj
169*38fd1498Szrj
170*38fd1498Szrj static void output_prologue (void);
171*38fd1498Szrj static void output_operand_data (void);
172*38fd1498Szrj static void output_insn_data (void);
173*38fd1498Szrj static void output_get_insn_name (void);
174*38fd1498Szrj static void scan_operands (struct data *, rtx, int, int);
175*38fd1498Szrj static int compare_operands (struct operand_data *,
176*38fd1498Szrj struct operand_data *);
177*38fd1498Szrj static void place_operands (struct data *);
178*38fd1498Szrj static void process_template (struct data *, const char *);
179*38fd1498Szrj static void validate_insn_alternatives (struct data *);
180*38fd1498Szrj static void validate_insn_operands (struct data *);
181*38fd1498Szrj
182*38fd1498Szrj struct constraint_data
183*38fd1498Szrj {
184*38fd1498Szrj struct constraint_data *next_this_letter;
185*38fd1498Szrj file_location loc;
186*38fd1498Szrj unsigned int namelen;
187*38fd1498Szrj char name[1];
188*38fd1498Szrj };
189*38fd1498Szrj
190*38fd1498Szrj /* All machine-independent constraint characters (except digits) that
191*38fd1498Szrj are handled outside the define*_constraint mechanism. */
192*38fd1498Szrj static const char indep_constraints[] = ",=+%*?!^$#&g";
193*38fd1498Szrj
194*38fd1498Szrj static struct constraint_data *
195*38fd1498Szrj constraints_by_letter_table[1 << CHAR_BIT];
196*38fd1498Szrj
197*38fd1498Szrj static int mdep_constraint_len (const char *, file_location, int);
198*38fd1498Szrj static void note_constraint (md_rtx_info *);
199*38fd1498Szrj
200*38fd1498Szrj static void
output_prologue(void)201*38fd1498Szrj output_prologue (void)
202*38fd1498Szrj {
203*38fd1498Szrj printf ("/* Generated automatically by the program `genoutput'\n\
204*38fd1498Szrj from the machine description file `md'. */\n\n");
205*38fd1498Szrj
206*38fd1498Szrj printf ("#define IN_TARGET_CODE 1\n");
207*38fd1498Szrj printf ("#include \"config.h\"\n");
208*38fd1498Szrj printf ("#include \"system.h\"\n");
209*38fd1498Szrj printf ("#include \"coretypes.h\"\n");
210*38fd1498Szrj printf ("#include \"backend.h\"\n");
211*38fd1498Szrj printf ("#include \"predict.h\"\n");
212*38fd1498Szrj printf ("#include \"tree.h\"\n");
213*38fd1498Szrj printf ("#include \"rtl.h\"\n");
214*38fd1498Szrj printf ("#include \"flags.h\"\n");
215*38fd1498Szrj printf ("#include \"alias.h\"\n");
216*38fd1498Szrj printf ("#include \"varasm.h\"\n");
217*38fd1498Szrj printf ("#include \"stor-layout.h\"\n");
218*38fd1498Szrj printf ("#include \"calls.h\"\n");
219*38fd1498Szrj printf ("#include \"insn-config.h\"\n");
220*38fd1498Szrj printf ("#include \"expmed.h\"\n");
221*38fd1498Szrj printf ("#include \"dojump.h\"\n");
222*38fd1498Szrj printf ("#include \"explow.h\"\n");
223*38fd1498Szrj printf ("#include \"memmodel.h\"\n");
224*38fd1498Szrj printf ("#include \"emit-rtl.h\"\n");
225*38fd1498Szrj printf ("#include \"stmt.h\"\n");
226*38fd1498Szrj printf ("#include \"expr.h\"\n");
227*38fd1498Szrj printf ("#include \"insn-codes.h\"\n");
228*38fd1498Szrj printf ("#include \"tm_p.h\"\n");
229*38fd1498Szrj printf ("#include \"regs.h\"\n");
230*38fd1498Szrj printf ("#include \"conditions.h\"\n");
231*38fd1498Szrj printf ("#include \"insn-attr.h\"\n\n");
232*38fd1498Szrj printf ("#include \"recog.h\"\n\n");
233*38fd1498Szrj printf ("#include \"diagnostic-core.h\"\n");
234*38fd1498Szrj printf ("#include \"output.h\"\n");
235*38fd1498Szrj printf ("#include \"target.h\"\n");
236*38fd1498Szrj printf ("#include \"tm-constrs.h\"\n");
237*38fd1498Szrj }
238*38fd1498Szrj
239*38fd1498Szrj static void
output_operand_data(void)240*38fd1498Szrj output_operand_data (void)
241*38fd1498Szrj {
242*38fd1498Szrj struct operand_data *d;
243*38fd1498Szrj
244*38fd1498Szrj printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
245*38fd1498Szrj
246*38fd1498Szrj for (d = odata; d; d = d->next)
247*38fd1498Szrj {
248*38fd1498Szrj struct pred_data *pred;
249*38fd1498Szrj
250*38fd1498Szrj printf (" {\n");
251*38fd1498Szrj
252*38fd1498Szrj printf (" %s,\n",
253*38fd1498Szrj d->predicate && d->predicate[0] ? d->predicate : "0");
254*38fd1498Szrj
255*38fd1498Szrj printf (" \"%s\",\n", d->constraint ? d->constraint : "");
256*38fd1498Szrj
257*38fd1498Szrj printf (" E_%smode,\n", GET_MODE_NAME (d->mode));
258*38fd1498Szrj
259*38fd1498Szrj printf (" %d,\n", d->strict_low);
260*38fd1498Szrj
261*38fd1498Szrj printf (" %d,\n", d->constraint == NULL ? 1 : 0);
262*38fd1498Szrj
263*38fd1498Szrj printf (" %d,\n", d->eliminable);
264*38fd1498Szrj
265*38fd1498Szrj pred = NULL;
266*38fd1498Szrj if (d->predicate)
267*38fd1498Szrj pred = lookup_predicate (d->predicate);
268*38fd1498Szrj printf (" %d\n", pred && pred->codes[MEM]);
269*38fd1498Szrj
270*38fd1498Szrj printf (" },\n");
271*38fd1498Szrj }
272*38fd1498Szrj printf ("};\n\n\n");
273*38fd1498Szrj }
274*38fd1498Szrj
275*38fd1498Szrj static void
output_insn_data(void)276*38fd1498Szrj output_insn_data (void)
277*38fd1498Szrj {
278*38fd1498Szrj struct data *d;
279*38fd1498Szrj int name_offset = 0;
280*38fd1498Szrj int next_name_offset;
281*38fd1498Szrj const char * last_name = 0;
282*38fd1498Szrj const char * next_name = 0;
283*38fd1498Szrj struct data *n;
284*38fd1498Szrj
285*38fd1498Szrj for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
286*38fd1498Szrj if (n->name)
287*38fd1498Szrj {
288*38fd1498Szrj next_name = n->name;
289*38fd1498Szrj break;
290*38fd1498Szrj }
291*38fd1498Szrj
292*38fd1498Szrj printf ("#if GCC_VERSION >= 2007\n__extension__\n#endif\n");
293*38fd1498Szrj printf ("\nconst struct insn_data_d insn_data[] = \n{\n");
294*38fd1498Szrj
295*38fd1498Szrj for (d = idata; d; d = d->next)
296*38fd1498Szrj {
297*38fd1498Szrj printf (" /* %s:%d */\n", d->loc.filename, d->loc.lineno);
298*38fd1498Szrj printf (" {\n");
299*38fd1498Szrj
300*38fd1498Szrj if (d->name)
301*38fd1498Szrj {
302*38fd1498Szrj printf (" \"%s\",\n", d->name);
303*38fd1498Szrj name_offset = 0;
304*38fd1498Szrj last_name = d->name;
305*38fd1498Szrj next_name = 0;
306*38fd1498Szrj for (n = d->next, next_name_offset = 1; n;
307*38fd1498Szrj n = n->next, next_name_offset++)
308*38fd1498Szrj {
309*38fd1498Szrj if (n->name)
310*38fd1498Szrj {
311*38fd1498Szrj next_name = n->name;
312*38fd1498Szrj break;
313*38fd1498Szrj }
314*38fd1498Szrj }
315*38fd1498Szrj }
316*38fd1498Szrj else
317*38fd1498Szrj {
318*38fd1498Szrj name_offset++;
319*38fd1498Szrj if (next_name && (last_name == 0
320*38fd1498Szrj || name_offset > next_name_offset / 2))
321*38fd1498Szrj printf (" \"%s-%d\",\n", next_name,
322*38fd1498Szrj next_name_offset - name_offset);
323*38fd1498Szrj else
324*38fd1498Szrj printf (" \"%s+%d\",\n", last_name, name_offset);
325*38fd1498Szrj }
326*38fd1498Szrj
327*38fd1498Szrj switch (d->output_format)
328*38fd1498Szrj {
329*38fd1498Szrj case INSN_OUTPUT_FORMAT_NONE:
330*38fd1498Szrj printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
331*38fd1498Szrj printf (" { 0 },\n");
332*38fd1498Szrj printf ("#else\n");
333*38fd1498Szrj printf (" { 0, 0, 0 },\n");
334*38fd1498Szrj printf ("#endif\n");
335*38fd1498Szrj break;
336*38fd1498Szrj case INSN_OUTPUT_FORMAT_SINGLE:
337*38fd1498Szrj {
338*38fd1498Szrj const char *p = d->template_code;
339*38fd1498Szrj char prev = 0;
340*38fd1498Szrj
341*38fd1498Szrj printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
342*38fd1498Szrj printf (" { .single =\n");
343*38fd1498Szrj printf ("#else\n");
344*38fd1498Szrj printf (" {\n");
345*38fd1498Szrj printf ("#endif\n");
346*38fd1498Szrj printf (" \"");
347*38fd1498Szrj while (*p)
348*38fd1498Szrj {
349*38fd1498Szrj if (IS_VSPACE (*p) && prev != '\\')
350*38fd1498Szrj {
351*38fd1498Szrj /* Preserve two consecutive \n's or \r's, but treat \r\n
352*38fd1498Szrj as a single newline. */
353*38fd1498Szrj if (*p == '\n' && prev != '\r')
354*38fd1498Szrj printf ("\\n\\\n");
355*38fd1498Szrj }
356*38fd1498Szrj else
357*38fd1498Szrj putchar (*p);
358*38fd1498Szrj prev = *p;
359*38fd1498Szrj ++p;
360*38fd1498Szrj }
361*38fd1498Szrj printf ("\",\n");
362*38fd1498Szrj printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
363*38fd1498Szrj printf (" },\n");
364*38fd1498Szrj printf ("#else\n");
365*38fd1498Szrj printf (" 0, 0 },\n");
366*38fd1498Szrj printf ("#endif\n");
367*38fd1498Szrj }
368*38fd1498Szrj break;
369*38fd1498Szrj case INSN_OUTPUT_FORMAT_MULTI:
370*38fd1498Szrj printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
371*38fd1498Szrj printf (" { .multi = output_%d },\n", d->code_number);
372*38fd1498Szrj printf ("#else\n");
373*38fd1498Szrj printf (" { 0, output_%d, 0 },\n", d->code_number);
374*38fd1498Szrj printf ("#endif\n");
375*38fd1498Szrj break;
376*38fd1498Szrj case INSN_OUTPUT_FORMAT_FUNCTION:
377*38fd1498Szrj printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
378*38fd1498Szrj printf (" { .function = output_%d },\n", d->code_number);
379*38fd1498Szrj printf ("#else\n");
380*38fd1498Szrj printf (" { 0, 0, output_%d },\n", d->code_number);
381*38fd1498Szrj printf ("#endif\n");
382*38fd1498Szrj break;
383*38fd1498Szrj default:
384*38fd1498Szrj gcc_unreachable ();
385*38fd1498Szrj }
386*38fd1498Szrj
387*38fd1498Szrj if (d->name && d->name[0] != '*')
388*38fd1498Szrj printf (" { (insn_gen_fn::stored_funcptr) gen_%s },\n", d->name);
389*38fd1498Szrj else
390*38fd1498Szrj printf (" { 0 },\n");
391*38fd1498Szrj
392*38fd1498Szrj printf (" &operand_data[%d],\n", d->operand_number);
393*38fd1498Szrj printf (" %d,\n", d->n_generator_args);
394*38fd1498Szrj printf (" %d,\n", d->n_operands);
395*38fd1498Szrj printf (" %d,\n", d->n_dups);
396*38fd1498Szrj printf (" %d,\n", d->n_alternatives);
397*38fd1498Szrj printf (" %d\n", d->output_format);
398*38fd1498Szrj
399*38fd1498Szrj printf (" },\n");
400*38fd1498Szrj }
401*38fd1498Szrj printf ("};\n\n\n");
402*38fd1498Szrj }
403*38fd1498Szrj
404*38fd1498Szrj static void
output_get_insn_name(void)405*38fd1498Szrj output_get_insn_name (void)
406*38fd1498Szrj {
407*38fd1498Szrj printf ("const char *\n");
408*38fd1498Szrj printf ("get_insn_name (int code)\n");
409*38fd1498Szrj printf ("{\n");
410*38fd1498Szrj printf (" if (code == NOOP_MOVE_INSN_CODE)\n");
411*38fd1498Szrj printf (" return \"NOOP_MOVE\";\n");
412*38fd1498Szrj printf (" else\n");
413*38fd1498Szrj printf (" return insn_data[code].name;\n");
414*38fd1498Szrj printf ("}\n");
415*38fd1498Szrj }
416*38fd1498Szrj
417*38fd1498Szrj
418*38fd1498Szrj /* Stores the operand data into `d->operand[i]'.
419*38fd1498Szrj
420*38fd1498Szrj THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
421*38fd1498Szrj THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
422*38fd1498Szrj
423*38fd1498Szrj static void
scan_operands(struct data * d,rtx part,int this_address_p,int this_strict_low)424*38fd1498Szrj scan_operands (struct data *d, rtx part, int this_address_p,
425*38fd1498Szrj int this_strict_low)
426*38fd1498Szrj {
427*38fd1498Szrj int i, j;
428*38fd1498Szrj const char *format_ptr;
429*38fd1498Szrj int opno;
430*38fd1498Szrj
431*38fd1498Szrj if (part == 0)
432*38fd1498Szrj return;
433*38fd1498Szrj
434*38fd1498Szrj switch (GET_CODE (part))
435*38fd1498Szrj {
436*38fd1498Szrj case MATCH_OPERAND:
437*38fd1498Szrj opno = XINT (part, 0);
438*38fd1498Szrj if (opno >= MAX_MAX_OPERANDS)
439*38fd1498Szrj {
440*38fd1498Szrj error_at (d->loc, "maximum number of operands exceeded");
441*38fd1498Szrj return;
442*38fd1498Szrj }
443*38fd1498Szrj if (d->operand[opno].seen)
444*38fd1498Szrj error_at (d->loc, "repeated operand number %d\n", opno);
445*38fd1498Szrj
446*38fd1498Szrj d->operand[opno].seen = 1;
447*38fd1498Szrj d->operand[opno].mode = GET_MODE (part);
448*38fd1498Szrj d->operand[opno].strict_low = this_strict_low;
449*38fd1498Szrj d->operand[opno].predicate = XSTR (part, 1);
450*38fd1498Szrj d->operand[opno].constraint = strip_whitespace (XSTR (part, 2));
451*38fd1498Szrj d->operand[opno].n_alternatives
452*38fd1498Szrj = n_occurrences (',', d->operand[opno].constraint) + 1;
453*38fd1498Szrj d->operand[opno].address_p = this_address_p;
454*38fd1498Szrj d->operand[opno].eliminable = 1;
455*38fd1498Szrj return;
456*38fd1498Szrj
457*38fd1498Szrj case MATCH_SCRATCH:
458*38fd1498Szrj opno = XINT (part, 0);
459*38fd1498Szrj if (opno >= MAX_MAX_OPERANDS)
460*38fd1498Szrj {
461*38fd1498Szrj error_at (d->loc, "maximum number of operands exceeded");
462*38fd1498Szrj return;
463*38fd1498Szrj }
464*38fd1498Szrj if (d->operand[opno].seen)
465*38fd1498Szrj error_at (d->loc, "repeated operand number %d\n", opno);
466*38fd1498Szrj
467*38fd1498Szrj d->operand[opno].seen = 1;
468*38fd1498Szrj d->operand[opno].mode = GET_MODE (part);
469*38fd1498Szrj d->operand[opno].strict_low = 0;
470*38fd1498Szrj d->operand[opno].predicate = "scratch_operand";
471*38fd1498Szrj d->operand[opno].constraint = strip_whitespace (XSTR (part, 1));
472*38fd1498Szrj d->operand[opno].n_alternatives
473*38fd1498Szrj = n_occurrences (',', d->operand[opno].constraint) + 1;
474*38fd1498Szrj d->operand[opno].address_p = 0;
475*38fd1498Szrj d->operand[opno].eliminable = 0;
476*38fd1498Szrj return;
477*38fd1498Szrj
478*38fd1498Szrj case MATCH_OPERATOR:
479*38fd1498Szrj case MATCH_PARALLEL:
480*38fd1498Szrj opno = XINT (part, 0);
481*38fd1498Szrj if (opno >= MAX_MAX_OPERANDS)
482*38fd1498Szrj {
483*38fd1498Szrj error_at (d->loc, "maximum number of operands exceeded");
484*38fd1498Szrj return;
485*38fd1498Szrj }
486*38fd1498Szrj if (d->operand[opno].seen)
487*38fd1498Szrj error_at (d->loc, "repeated operand number %d\n", opno);
488*38fd1498Szrj
489*38fd1498Szrj d->operand[opno].seen = 1;
490*38fd1498Szrj d->operand[opno].mode = GET_MODE (part);
491*38fd1498Szrj d->operand[opno].strict_low = 0;
492*38fd1498Szrj d->operand[opno].predicate = XSTR (part, 1);
493*38fd1498Szrj d->operand[opno].constraint = 0;
494*38fd1498Szrj d->operand[opno].address_p = 0;
495*38fd1498Szrj d->operand[opno].eliminable = 0;
496*38fd1498Szrj for (i = 0; i < XVECLEN (part, 2); i++)
497*38fd1498Szrj scan_operands (d, XVECEXP (part, 2, i), 0, 0);
498*38fd1498Szrj return;
499*38fd1498Szrj
500*38fd1498Szrj case STRICT_LOW_PART:
501*38fd1498Szrj scan_operands (d, XEXP (part, 0), 0, 1);
502*38fd1498Szrj return;
503*38fd1498Szrj
504*38fd1498Szrj default:
505*38fd1498Szrj break;
506*38fd1498Szrj }
507*38fd1498Szrj
508*38fd1498Szrj format_ptr = GET_RTX_FORMAT (GET_CODE (part));
509*38fd1498Szrj
510*38fd1498Szrj for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
511*38fd1498Szrj switch (*format_ptr++)
512*38fd1498Szrj {
513*38fd1498Szrj case 'e':
514*38fd1498Szrj case 'u':
515*38fd1498Szrj scan_operands (d, XEXP (part, i), 0, 0);
516*38fd1498Szrj break;
517*38fd1498Szrj case 'E':
518*38fd1498Szrj if (XVEC (part, i) != NULL)
519*38fd1498Szrj for (j = 0; j < XVECLEN (part, i); j++)
520*38fd1498Szrj scan_operands (d, XVECEXP (part, i, j), 0, 0);
521*38fd1498Szrj break;
522*38fd1498Szrj }
523*38fd1498Szrj }
524*38fd1498Szrj
525*38fd1498Szrj /* Compare two operands for content equality. */
526*38fd1498Szrj
527*38fd1498Szrj static int
compare_operands(struct operand_data * d0,struct operand_data * d1)528*38fd1498Szrj compare_operands (struct operand_data *d0, struct operand_data *d1)
529*38fd1498Szrj {
530*38fd1498Szrj const char *p0, *p1;
531*38fd1498Szrj
532*38fd1498Szrj p0 = d0->predicate;
533*38fd1498Szrj if (!p0)
534*38fd1498Szrj p0 = "";
535*38fd1498Szrj p1 = d1->predicate;
536*38fd1498Szrj if (!p1)
537*38fd1498Szrj p1 = "";
538*38fd1498Szrj if (strcmp (p0, p1) != 0)
539*38fd1498Szrj return 0;
540*38fd1498Szrj
541*38fd1498Szrj p0 = d0->constraint;
542*38fd1498Szrj if (!p0)
543*38fd1498Szrj p0 = "";
544*38fd1498Szrj p1 = d1->constraint;
545*38fd1498Szrj if (!p1)
546*38fd1498Szrj p1 = "";
547*38fd1498Szrj if (strcmp (p0, p1) != 0)
548*38fd1498Szrj return 0;
549*38fd1498Szrj
550*38fd1498Szrj if (d0->mode != d1->mode)
551*38fd1498Szrj return 0;
552*38fd1498Szrj
553*38fd1498Szrj if (d0->strict_low != d1->strict_low)
554*38fd1498Szrj return 0;
555*38fd1498Szrj
556*38fd1498Szrj if (d0->eliminable != d1->eliminable)
557*38fd1498Szrj return 0;
558*38fd1498Szrj
559*38fd1498Szrj return 1;
560*38fd1498Szrj }
561*38fd1498Szrj
562*38fd1498Szrj /* Scan the list of operands we've already committed to output and either
563*38fd1498Szrj find a subsequence that is the same, or allocate a new one at the end. */
564*38fd1498Szrj
565*38fd1498Szrj static void
place_operands(struct data * d)566*38fd1498Szrj place_operands (struct data *d)
567*38fd1498Szrj {
568*38fd1498Szrj struct operand_data *od, *od2;
569*38fd1498Szrj int i;
570*38fd1498Szrj
571*38fd1498Szrj if (d->n_operands == 0)
572*38fd1498Szrj {
573*38fd1498Szrj d->operand_number = 0;
574*38fd1498Szrj return;
575*38fd1498Szrj }
576*38fd1498Szrj
577*38fd1498Szrj /* Brute force substring search. */
578*38fd1498Szrj for (od = odata, i = 0; od; od = od->next, i = 0)
579*38fd1498Szrj if (compare_operands (od, &d->operand[0]))
580*38fd1498Szrj {
581*38fd1498Szrj od2 = od->next;
582*38fd1498Szrj i = 1;
583*38fd1498Szrj while (1)
584*38fd1498Szrj {
585*38fd1498Szrj if (i == d->n_operands)
586*38fd1498Szrj goto full_match;
587*38fd1498Szrj if (od2 == NULL)
588*38fd1498Szrj goto partial_match;
589*38fd1498Szrj if (! compare_operands (od2, &d->operand[i]))
590*38fd1498Szrj break;
591*38fd1498Szrj ++i, od2 = od2->next;
592*38fd1498Szrj }
593*38fd1498Szrj }
594*38fd1498Szrj
595*38fd1498Szrj /* Either partial match at the end of the list, or no match. In either
596*38fd1498Szrj case, we tack on what operands are remaining to the end of the list. */
597*38fd1498Szrj partial_match:
598*38fd1498Szrj d->operand_number = next_operand_number - i;
599*38fd1498Szrj for (; i < d->n_operands; ++i)
600*38fd1498Szrj {
601*38fd1498Szrj od2 = &d->operand[i];
602*38fd1498Szrj *odata_end = od2;
603*38fd1498Szrj odata_end = &od2->next;
604*38fd1498Szrj od2->index = next_operand_number++;
605*38fd1498Szrj }
606*38fd1498Szrj *odata_end = NULL;
607*38fd1498Szrj return;
608*38fd1498Szrj
609*38fd1498Szrj full_match:
610*38fd1498Szrj d->operand_number = od->index;
611*38fd1498Szrj return;
612*38fd1498Szrj }
613*38fd1498Szrj
614*38fd1498Szrj
615*38fd1498Szrj /* Process an assembler template from a define_insn or a define_peephole.
616*38fd1498Szrj It is either the assembler code template, a list of assembler code
617*38fd1498Szrj templates, or C code to generate the assembler code template. */
618*38fd1498Szrj
619*38fd1498Szrj static void
process_template(struct data * d,const char * template_code)620*38fd1498Szrj process_template (struct data *d, const char *template_code)
621*38fd1498Szrj {
622*38fd1498Szrj const char *cp;
623*38fd1498Szrj int i;
624*38fd1498Szrj
625*38fd1498Szrj /* Templates starting with * contain straight code to be run. */
626*38fd1498Szrj if (template_code[0] == '*')
627*38fd1498Szrj {
628*38fd1498Szrj d->template_code = 0;
629*38fd1498Szrj d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
630*38fd1498Szrj
631*38fd1498Szrj puts ("\nstatic const char *");
632*38fd1498Szrj printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n",
633*38fd1498Szrj d->code_number);
634*38fd1498Szrj puts ("{");
635*38fd1498Szrj rtx_reader_ptr->print_md_ptr_loc (template_code);
636*38fd1498Szrj puts (template_code + 1);
637*38fd1498Szrj puts ("}");
638*38fd1498Szrj }
639*38fd1498Szrj
640*38fd1498Szrj /* If the assembler code template starts with a @ it is a newline-separated
641*38fd1498Szrj list of assembler code templates, one for each alternative. */
642*38fd1498Szrj else if (template_code[0] == '@')
643*38fd1498Szrj {
644*38fd1498Szrj int found_star = 0;
645*38fd1498Szrj
646*38fd1498Szrj for (cp = &template_code[1]; *cp; )
647*38fd1498Szrj {
648*38fd1498Szrj while (ISSPACE (*cp))
649*38fd1498Szrj cp++;
650*38fd1498Szrj if (*cp == '*')
651*38fd1498Szrj found_star = 1;
652*38fd1498Szrj while (!IS_VSPACE (*cp) && *cp != '\0')
653*38fd1498Szrj ++cp;
654*38fd1498Szrj }
655*38fd1498Szrj d->template_code = 0;
656*38fd1498Szrj if (found_star)
657*38fd1498Szrj {
658*38fd1498Szrj d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
659*38fd1498Szrj puts ("\nstatic const char *");
660*38fd1498Szrj printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
661*38fd1498Szrj "rtx_insn *insn ATTRIBUTE_UNUSED)\n", d->code_number);
662*38fd1498Szrj puts ("{");
663*38fd1498Szrj puts (" switch (which_alternative)\n {");
664*38fd1498Szrj }
665*38fd1498Szrj else
666*38fd1498Szrj {
667*38fd1498Szrj d->output_format = INSN_OUTPUT_FORMAT_MULTI;
668*38fd1498Szrj printf ("\nstatic const char * const output_%d[] = {\n",
669*38fd1498Szrj d->code_number);
670*38fd1498Szrj }
671*38fd1498Szrj
672*38fd1498Szrj for (i = 0, cp = &template_code[1]; *cp; )
673*38fd1498Szrj {
674*38fd1498Szrj const char *ep, *sp, *bp;
675*38fd1498Szrj
676*38fd1498Szrj while (ISSPACE (*cp))
677*38fd1498Szrj cp++;
678*38fd1498Szrj
679*38fd1498Szrj bp = cp;
680*38fd1498Szrj if (found_star)
681*38fd1498Szrj {
682*38fd1498Szrj printf (" case %d:", i);
683*38fd1498Szrj if (*cp == '*')
684*38fd1498Szrj {
685*38fd1498Szrj printf ("\n ");
686*38fd1498Szrj cp++;
687*38fd1498Szrj }
688*38fd1498Szrj else
689*38fd1498Szrj printf (" return \"");
690*38fd1498Szrj }
691*38fd1498Szrj else
692*38fd1498Szrj printf (" \"");
693*38fd1498Szrj
694*38fd1498Szrj for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
695*38fd1498Szrj if (!ISSPACE (*ep))
696*38fd1498Szrj sp = ep + 1;
697*38fd1498Szrj
698*38fd1498Szrj if (sp != ep)
699*38fd1498Szrj message_at (d->loc, "trailing whitespace in output template");
700*38fd1498Szrj
701*38fd1498Szrj while (cp < sp)
702*38fd1498Szrj {
703*38fd1498Szrj putchar (*cp);
704*38fd1498Szrj cp++;
705*38fd1498Szrj }
706*38fd1498Szrj
707*38fd1498Szrj if (!found_star)
708*38fd1498Szrj puts ("\",");
709*38fd1498Szrj else if (*bp != '*')
710*38fd1498Szrj puts ("\";");
711*38fd1498Szrj else
712*38fd1498Szrj {
713*38fd1498Szrj /* The usual action will end with a return.
714*38fd1498Szrj If there is neither break or return at the end, this is
715*38fd1498Szrj assumed to be intentional; this allows to have multiple
716*38fd1498Szrj consecutive alternatives share some code. */
717*38fd1498Szrj puts ("");
718*38fd1498Szrj }
719*38fd1498Szrj i++;
720*38fd1498Szrj }
721*38fd1498Szrj if (i == 1)
722*38fd1498Szrj message_at (d->loc, "'@' is redundant for output template with"
723*38fd1498Szrj " single alternative");
724*38fd1498Szrj if (i != d->n_alternatives)
725*38fd1498Szrj error_at (d->loc, "wrong number of alternatives in the output"
726*38fd1498Szrj " template");
727*38fd1498Szrj
728*38fd1498Szrj if (found_star)
729*38fd1498Szrj puts (" default: gcc_unreachable ();\n }\n}");
730*38fd1498Szrj else
731*38fd1498Szrj printf ("};\n");
732*38fd1498Szrj }
733*38fd1498Szrj else
734*38fd1498Szrj {
735*38fd1498Szrj d->template_code = template_code;
736*38fd1498Szrj d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
737*38fd1498Szrj }
738*38fd1498Szrj }
739*38fd1498Szrj
740*38fd1498Szrj /* Check insn D for consistency in number of constraint alternatives. */
741*38fd1498Szrj
742*38fd1498Szrj static void
validate_insn_alternatives(struct data * d)743*38fd1498Szrj validate_insn_alternatives (struct data *d)
744*38fd1498Szrj {
745*38fd1498Szrj int n = 0, start;
746*38fd1498Szrj
747*38fd1498Szrj /* Make sure all the operands have the same number of alternatives
748*38fd1498Szrj in their constraints. Let N be that number. */
749*38fd1498Szrj for (start = 0; start < d->n_operands; start++)
750*38fd1498Szrj if (d->operand[start].n_alternatives > 0)
751*38fd1498Szrj {
752*38fd1498Szrj int len, i;
753*38fd1498Szrj const char *p;
754*38fd1498Szrj char c;
755*38fd1498Szrj int which_alternative = 0;
756*38fd1498Szrj int alternative_count_unsure = 0;
757*38fd1498Szrj bool seen_write = false;
758*38fd1498Szrj
759*38fd1498Szrj for (p = d->operand[start].constraint; (c = *p); p += len)
760*38fd1498Szrj {
761*38fd1498Szrj if ((c == '%' || c == '=' || c == '+')
762*38fd1498Szrj && p != d->operand[start].constraint)
763*38fd1498Szrj error_at (d->loc, "character '%c' can only be used at the"
764*38fd1498Szrj " beginning of a constraint string", c);
765*38fd1498Szrj
766*38fd1498Szrj if (c == '=' || c == '+')
767*38fd1498Szrj seen_write = true;
768*38fd1498Szrj
769*38fd1498Szrj /* Earlyclobber operands must always be marked write-only
770*38fd1498Szrj or read/write. */
771*38fd1498Szrj if (!seen_write && c == '&')
772*38fd1498Szrj error_at (d->loc, "earlyclobber operands may not be"
773*38fd1498Szrj " read-only in alternative %d", which_alternative);
774*38fd1498Szrj
775*38fd1498Szrj if (ISSPACE (c) || strchr (indep_constraints, c))
776*38fd1498Szrj len = 1;
777*38fd1498Szrj else if (ISDIGIT (c))
778*38fd1498Szrj {
779*38fd1498Szrj const char *q = p;
780*38fd1498Szrj do
781*38fd1498Szrj q++;
782*38fd1498Szrj while (ISDIGIT (*q));
783*38fd1498Szrj len = q - p;
784*38fd1498Szrj }
785*38fd1498Szrj else
786*38fd1498Szrj len = mdep_constraint_len (p, d->loc, start);
787*38fd1498Szrj
788*38fd1498Szrj if (c == ',')
789*38fd1498Szrj {
790*38fd1498Szrj which_alternative++;
791*38fd1498Szrj continue;
792*38fd1498Szrj }
793*38fd1498Szrj
794*38fd1498Szrj for (i = 1; i < len; i++)
795*38fd1498Szrj if (p[i] == '\0')
796*38fd1498Szrj {
797*38fd1498Szrj error_at (d->loc, "NUL in alternative %d of operand %d",
798*38fd1498Szrj which_alternative, start);
799*38fd1498Szrj alternative_count_unsure = 1;
800*38fd1498Szrj break;
801*38fd1498Szrj }
802*38fd1498Szrj else if (strchr (",#*", p[i]))
803*38fd1498Szrj {
804*38fd1498Szrj error_at (d->loc, "'%c' in alternative %d of operand %d",
805*38fd1498Szrj p[i], which_alternative, start);
806*38fd1498Szrj alternative_count_unsure = 1;
807*38fd1498Szrj }
808*38fd1498Szrj }
809*38fd1498Szrj if (!alternative_count_unsure)
810*38fd1498Szrj {
811*38fd1498Szrj if (n == 0)
812*38fd1498Szrj n = d->operand[start].n_alternatives;
813*38fd1498Szrj else if (n != d->operand[start].n_alternatives)
814*38fd1498Szrj error_at (d->loc, "wrong number of alternatives in operand %d",
815*38fd1498Szrj start);
816*38fd1498Szrj }
817*38fd1498Szrj }
818*38fd1498Szrj
819*38fd1498Szrj /* Record the insn's overall number of alternatives. */
820*38fd1498Szrj d->n_alternatives = n;
821*38fd1498Szrj }
822*38fd1498Szrj
823*38fd1498Szrj /* Verify that there are no gaps in operand numbers for INSNs. */
824*38fd1498Szrj
825*38fd1498Szrj static void
validate_insn_operands(struct data * d)826*38fd1498Szrj validate_insn_operands (struct data *d)
827*38fd1498Szrj {
828*38fd1498Szrj int i;
829*38fd1498Szrj
830*38fd1498Szrj for (i = 0; i < d->n_operands; ++i)
831*38fd1498Szrj if (d->operand[i].seen == 0)
832*38fd1498Szrj error_at (d->loc, "missing operand %d", i);
833*38fd1498Szrj }
834*38fd1498Szrj
835*38fd1498Szrj static void
validate_optab_operands(struct data * d)836*38fd1498Szrj validate_optab_operands (struct data *d)
837*38fd1498Szrj {
838*38fd1498Szrj if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
839*38fd1498Szrj return;
840*38fd1498Szrj
841*38fd1498Szrj /* Miscellaneous tests. */
842*38fd1498Szrj if (strncmp (d->name, "cstore", 6) == 0
843*38fd1498Szrj && d->name[strlen (d->name) - 1] == '4'
844*38fd1498Szrj && d->operand[0].mode == VOIDmode)
845*38fd1498Szrj {
846*38fd1498Szrj message_at (d->loc, "missing mode for operand 0 of cstore");
847*38fd1498Szrj have_error = 1;
848*38fd1498Szrj }
849*38fd1498Szrj }
850*38fd1498Szrj
851*38fd1498Szrj /* Look at a define_insn just read. Assign its code number. Record
852*38fd1498Szrj on idata the template and the number of arguments. If the insn has
853*38fd1498Szrj a hairy output action, output a function for now. */
854*38fd1498Szrj
855*38fd1498Szrj static void
gen_insn(md_rtx_info * info)856*38fd1498Szrj gen_insn (md_rtx_info *info)
857*38fd1498Szrj {
858*38fd1498Szrj struct pattern_stats stats;
859*38fd1498Szrj rtx insn = info->def;
860*38fd1498Szrj data *d = new data;
861*38fd1498Szrj int i;
862*38fd1498Szrj
863*38fd1498Szrj d->code_number = info->index;
864*38fd1498Szrj d->loc = info->loc;
865*38fd1498Szrj if (XSTR (insn, 0)[0])
866*38fd1498Szrj d->name = XSTR (insn, 0);
867*38fd1498Szrj else
868*38fd1498Szrj d->name = 0;
869*38fd1498Szrj
870*38fd1498Szrj /* Build up the list in the same order as the insns are seen
871*38fd1498Szrj in the machine description. */
872*38fd1498Szrj d->next = 0;
873*38fd1498Szrj *idata_end = d;
874*38fd1498Szrj idata_end = &d->next;
875*38fd1498Szrj
876*38fd1498Szrj memset (d->operand, 0, sizeof (d->operand));
877*38fd1498Szrj
878*38fd1498Szrj for (i = 0; i < XVECLEN (insn, 1); i++)
879*38fd1498Szrj scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
880*38fd1498Szrj
881*38fd1498Szrj get_pattern_stats (&stats, XVEC (insn, 1));
882*38fd1498Szrj d->n_generator_args = stats.num_generator_args;
883*38fd1498Szrj d->n_operands = stats.num_insn_operands;
884*38fd1498Szrj d->n_dups = stats.num_dups;
885*38fd1498Szrj
886*38fd1498Szrj validate_insn_operands (d);
887*38fd1498Szrj validate_insn_alternatives (d);
888*38fd1498Szrj validate_optab_operands (d);
889*38fd1498Szrj place_operands (d);
890*38fd1498Szrj process_template (d, XTMPL (insn, 3));
891*38fd1498Szrj }
892*38fd1498Szrj
893*38fd1498Szrj /* Look at a define_peephole just read. Assign its code number.
894*38fd1498Szrj Record on idata the template and the number of arguments.
895*38fd1498Szrj If the insn has a hairy output action, output it now. */
896*38fd1498Szrj
897*38fd1498Szrj static void
gen_peephole(md_rtx_info * info)898*38fd1498Szrj gen_peephole (md_rtx_info *info)
899*38fd1498Szrj {
900*38fd1498Szrj struct pattern_stats stats;
901*38fd1498Szrj data *d = new data;
902*38fd1498Szrj int i;
903*38fd1498Szrj
904*38fd1498Szrj d->code_number = info->index;
905*38fd1498Szrj d->loc = info->loc;
906*38fd1498Szrj d->name = 0;
907*38fd1498Szrj
908*38fd1498Szrj /* Build up the list in the same order as the insns are seen
909*38fd1498Szrj in the machine description. */
910*38fd1498Szrj d->next = 0;
911*38fd1498Szrj *idata_end = d;
912*38fd1498Szrj idata_end = &d->next;
913*38fd1498Szrj
914*38fd1498Szrj memset (d->operand, 0, sizeof (d->operand));
915*38fd1498Szrj
916*38fd1498Szrj /* Get the number of operands by scanning all the patterns of the
917*38fd1498Szrj peephole optimizer. But ignore all the rest of the information
918*38fd1498Szrj thus obtained. */
919*38fd1498Szrj rtx peep = info->def;
920*38fd1498Szrj for (i = 0; i < XVECLEN (peep, 0); i++)
921*38fd1498Szrj scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
922*38fd1498Szrj
923*38fd1498Szrj get_pattern_stats (&stats, XVEC (peep, 0));
924*38fd1498Szrj d->n_generator_args = 0;
925*38fd1498Szrj d->n_operands = stats.num_insn_operands;
926*38fd1498Szrj d->n_dups = 0;
927*38fd1498Szrj
928*38fd1498Szrj validate_insn_alternatives (d);
929*38fd1498Szrj place_operands (d);
930*38fd1498Szrj process_template (d, XTMPL (peep, 2));
931*38fd1498Szrj }
932*38fd1498Szrj
933*38fd1498Szrj /* Process a define_expand just read. Assign its code number,
934*38fd1498Szrj only for the purposes of `insn_gen_function'. */
935*38fd1498Szrj
936*38fd1498Szrj static void
gen_expand(md_rtx_info * info)937*38fd1498Szrj gen_expand (md_rtx_info *info)
938*38fd1498Szrj {
939*38fd1498Szrj struct pattern_stats stats;
940*38fd1498Szrj rtx insn = info->def;
941*38fd1498Szrj data *d = new data;
942*38fd1498Szrj int i;
943*38fd1498Szrj
944*38fd1498Szrj d->code_number = info->index;
945*38fd1498Szrj d->loc = info->loc;
946*38fd1498Szrj if (XSTR (insn, 0)[0])
947*38fd1498Szrj d->name = XSTR (insn, 0);
948*38fd1498Szrj else
949*38fd1498Szrj d->name = 0;
950*38fd1498Szrj
951*38fd1498Szrj /* Build up the list in the same order as the insns are seen
952*38fd1498Szrj in the machine description. */
953*38fd1498Szrj d->next = 0;
954*38fd1498Szrj *idata_end = d;
955*38fd1498Szrj idata_end = &d->next;
956*38fd1498Szrj
957*38fd1498Szrj memset (d->operand, 0, sizeof (d->operand));
958*38fd1498Szrj
959*38fd1498Szrj /* Scan the operands to get the specified predicates and modes,
960*38fd1498Szrj since expand_binop needs to know them. */
961*38fd1498Szrj
962*38fd1498Szrj if (XVEC (insn, 1))
963*38fd1498Szrj for (i = 0; i < XVECLEN (insn, 1); i++)
964*38fd1498Szrj scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
965*38fd1498Szrj
966*38fd1498Szrj get_pattern_stats (&stats, XVEC (insn, 1));
967*38fd1498Szrj d->n_generator_args = stats.num_generator_args;
968*38fd1498Szrj d->n_operands = stats.num_insn_operands;
969*38fd1498Szrj d->n_dups = stats.num_dups;
970*38fd1498Szrj d->template_code = 0;
971*38fd1498Szrj d->output_format = INSN_OUTPUT_FORMAT_NONE;
972*38fd1498Szrj
973*38fd1498Szrj validate_insn_alternatives (d);
974*38fd1498Szrj validate_optab_operands (d);
975*38fd1498Szrj place_operands (d);
976*38fd1498Szrj }
977*38fd1498Szrj
978*38fd1498Szrj static void
init_insn_for_nothing(void)979*38fd1498Szrj init_insn_for_nothing (void)
980*38fd1498Szrj {
981*38fd1498Szrj idata = XCNEW (struct data);
982*38fd1498Szrj new (idata) data ();
983*38fd1498Szrj idata->name = "*placeholder_for_nothing";
984*38fd1498Szrj idata->loc = file_location ("<internal>", 0, 0);
985*38fd1498Szrj idata_end = &idata->next;
986*38fd1498Szrj }
987*38fd1498Szrj
988*38fd1498Szrj extern int main (int, const char **);
989*38fd1498Szrj
990*38fd1498Szrj int
main(int argc,const char ** argv)991*38fd1498Szrj main (int argc, const char **argv)
992*38fd1498Szrj {
993*38fd1498Szrj progname = "genoutput";
994*38fd1498Szrj
995*38fd1498Szrj init_insn_for_nothing ();
996*38fd1498Szrj
997*38fd1498Szrj if (!init_rtx_reader_args (argc, argv))
998*38fd1498Szrj return (FATAL_EXIT_CODE);
999*38fd1498Szrj
1000*38fd1498Szrj output_prologue ();
1001*38fd1498Szrj
1002*38fd1498Szrj /* Read the machine description. */
1003*38fd1498Szrj
1004*38fd1498Szrj md_rtx_info info;
1005*38fd1498Szrj while (read_md_rtx (&info))
1006*38fd1498Szrj switch (GET_CODE (info.def))
1007*38fd1498Szrj {
1008*38fd1498Szrj case DEFINE_INSN:
1009*38fd1498Szrj gen_insn (&info);
1010*38fd1498Szrj break;
1011*38fd1498Szrj
1012*38fd1498Szrj case DEFINE_PEEPHOLE:
1013*38fd1498Szrj gen_peephole (&info);
1014*38fd1498Szrj break;
1015*38fd1498Szrj
1016*38fd1498Szrj case DEFINE_EXPAND:
1017*38fd1498Szrj gen_expand (&info);
1018*38fd1498Szrj break;
1019*38fd1498Szrj
1020*38fd1498Szrj case DEFINE_CONSTRAINT:
1021*38fd1498Szrj case DEFINE_REGISTER_CONSTRAINT:
1022*38fd1498Szrj case DEFINE_ADDRESS_CONSTRAINT:
1023*38fd1498Szrj case DEFINE_MEMORY_CONSTRAINT:
1024*38fd1498Szrj case DEFINE_SPECIAL_MEMORY_CONSTRAINT:
1025*38fd1498Szrj note_constraint (&info);
1026*38fd1498Szrj break;
1027*38fd1498Szrj
1028*38fd1498Szrj default:
1029*38fd1498Szrj break;
1030*38fd1498Szrj }
1031*38fd1498Szrj
1032*38fd1498Szrj printf ("\n\n");
1033*38fd1498Szrj output_operand_data ();
1034*38fd1498Szrj output_insn_data ();
1035*38fd1498Szrj output_get_insn_name ();
1036*38fd1498Szrj
1037*38fd1498Szrj fflush (stdout);
1038*38fd1498Szrj return (ferror (stdout) != 0 || have_error
1039*38fd1498Szrj ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1040*38fd1498Szrj }
1041*38fd1498Szrj
1042*38fd1498Szrj /* Return the number of occurrences of character C in string S or
1043*38fd1498Szrj -1 if S is the null string. */
1044*38fd1498Szrj
1045*38fd1498Szrj static int
n_occurrences(int c,const char * s)1046*38fd1498Szrj n_occurrences (int c, const char *s)
1047*38fd1498Szrj {
1048*38fd1498Szrj int n = 0;
1049*38fd1498Szrj
1050*38fd1498Szrj if (s == 0 || *s == '\0')
1051*38fd1498Szrj return -1;
1052*38fd1498Szrj
1053*38fd1498Szrj while (*s)
1054*38fd1498Szrj n += (*s++ == c);
1055*38fd1498Szrj
1056*38fd1498Szrj return n;
1057*38fd1498Szrj }
1058*38fd1498Szrj
1059*38fd1498Szrj /* Remove whitespace in `s' by moving up characters until the end.
1060*38fd1498Szrj Return a new string. */
1061*38fd1498Szrj
1062*38fd1498Szrj static const char *
strip_whitespace(const char * s)1063*38fd1498Szrj strip_whitespace (const char *s)
1064*38fd1498Szrj {
1065*38fd1498Szrj char *p, *q;
1066*38fd1498Szrj char ch;
1067*38fd1498Szrj
1068*38fd1498Szrj if (s == 0)
1069*38fd1498Szrj return 0;
1070*38fd1498Szrj
1071*38fd1498Szrj p = q = XNEWVEC (char, strlen (s) + 1);
1072*38fd1498Szrj while ((ch = *s++) != '\0')
1073*38fd1498Szrj if (! ISSPACE (ch))
1074*38fd1498Szrj *p++ = ch;
1075*38fd1498Szrj
1076*38fd1498Szrj *p = '\0';
1077*38fd1498Szrj return q;
1078*38fd1498Szrj }
1079*38fd1498Szrj
1080*38fd1498Szrj /* Record just enough information about the constraint in *INFO to allow
1081*38fd1498Szrj checking of operand constraint strings above, in validate_insn_alternatives.
1082*38fd1498Szrj Does not validate most properties of the constraint itself; does enforce
1083*38fd1498Szrj no duplicate names, no overlap with MI constraints, and no prefixes. */
1084*38fd1498Szrj static void
note_constraint(md_rtx_info * info)1085*38fd1498Szrj note_constraint (md_rtx_info *info)
1086*38fd1498Szrj {
1087*38fd1498Szrj rtx exp = info->def;
1088*38fd1498Szrj const char *name = XSTR (exp, 0);
1089*38fd1498Szrj struct constraint_data **iter, **slot, *new_cdata;
1090*38fd1498Szrj
1091*38fd1498Szrj if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
1092*38fd1498Szrj name = general_mem;
1093*38fd1498Szrj unsigned int namelen = strlen (name);
1094*38fd1498Szrj
1095*38fd1498Szrj if (strchr (indep_constraints, name[0]))
1096*38fd1498Szrj {
1097*38fd1498Szrj if (name[1] == '\0')
1098*38fd1498Szrj error_at (info->loc, "constraint letter '%s' cannot be "
1099*38fd1498Szrj "redefined by the machine description", name);
1100*38fd1498Szrj else
1101*38fd1498Szrj error_at (info->loc, "constraint name '%s' cannot be defined by "
1102*38fd1498Szrj "the machine description, as it begins with '%c'",
1103*38fd1498Szrj name, name[0]);
1104*38fd1498Szrj return;
1105*38fd1498Szrj }
1106*38fd1498Szrj
1107*38fd1498Szrj slot = &constraints_by_letter_table[(unsigned int)name[0]];
1108*38fd1498Szrj for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
1109*38fd1498Szrj {
1110*38fd1498Szrj /* This causes slot to end up pointing to the
1111*38fd1498Szrj next_this_letter field of the last constraint with a name
1112*38fd1498Szrj of equal or greater length than the new constraint; hence
1113*38fd1498Szrj the new constraint will be inserted after all previous
1114*38fd1498Szrj constraints with names of the same length. */
1115*38fd1498Szrj if ((*iter)->namelen >= namelen)
1116*38fd1498Szrj slot = iter;
1117*38fd1498Szrj
1118*38fd1498Szrj if (!strcmp ((*iter)->name, name))
1119*38fd1498Szrj {
1120*38fd1498Szrj error_at (info->loc, "redefinition of constraint '%s'", name);
1121*38fd1498Szrj message_at ((*iter)->loc, "previous definition is here");
1122*38fd1498Szrj return;
1123*38fd1498Szrj }
1124*38fd1498Szrj else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
1125*38fd1498Szrj {
1126*38fd1498Szrj error_at (info->loc, "defining constraint '%s' here", name);
1127*38fd1498Szrj message_at ((*iter)->loc, "renders constraint '%s' "
1128*38fd1498Szrj "(defined here) a prefix", (*iter)->name);
1129*38fd1498Szrj return;
1130*38fd1498Szrj }
1131*38fd1498Szrj else if (!strncmp ((*iter)->name, name, namelen))
1132*38fd1498Szrj {
1133*38fd1498Szrj error_at (info->loc, "constraint '%s' is a prefix", name);
1134*38fd1498Szrj message_at ((*iter)->loc, "of constraint '%s' "
1135*38fd1498Szrj "(defined here)", (*iter)->name);
1136*38fd1498Szrj return;
1137*38fd1498Szrj }
1138*38fd1498Szrj }
1139*38fd1498Szrj new_cdata = XNEWVAR (struct constraint_data,
1140*38fd1498Szrj sizeof (struct constraint_data) + namelen);
1141*38fd1498Szrj new (new_cdata) constraint_data ();
1142*38fd1498Szrj strcpy (CONST_CAST (char *, new_cdata->name), name);
1143*38fd1498Szrj new_cdata->namelen = namelen;
1144*38fd1498Szrj new_cdata->loc = info->loc;
1145*38fd1498Szrj new_cdata->next_this_letter = *slot;
1146*38fd1498Szrj *slot = new_cdata;
1147*38fd1498Szrj }
1148*38fd1498Szrj
1149*38fd1498Szrj /* Return the length of the constraint name beginning at position S
1150*38fd1498Szrj of an operand constraint string, or issue an error message if there
1151*38fd1498Szrj is no such constraint. Does not expect to be called for generic
1152*38fd1498Szrj constraints. */
1153*38fd1498Szrj static int
mdep_constraint_len(const char * s,file_location loc,int opno)1154*38fd1498Szrj mdep_constraint_len (const char *s, file_location loc, int opno)
1155*38fd1498Szrj {
1156*38fd1498Szrj struct constraint_data *p;
1157*38fd1498Szrj
1158*38fd1498Szrj p = constraints_by_letter_table[(unsigned int)s[0]];
1159*38fd1498Szrj
1160*38fd1498Szrj if (p)
1161*38fd1498Szrj for (; p; p = p->next_this_letter)
1162*38fd1498Szrj if (!strncmp (s, p->name, p->namelen))
1163*38fd1498Szrj return p->namelen;
1164*38fd1498Szrj
1165*38fd1498Szrj error_at (loc, "error: undefined machine-specific constraint "
1166*38fd1498Szrj "at this point: \"%s\"", s);
1167*38fd1498Szrj message_at (loc, "note: in operand %d", opno);
1168*38fd1498Szrj return 1; /* safe */
1169*38fd1498Szrj }
1170