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