]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genoutput.c
gimple-predict.h: New file.
[thirdparty/gcc.git] / gcc / genoutput.c
CommitLineData
9db4e0ec 1/* Generate code from to output assembler insns as recognized from rtl.
5624e564 2 Copyright (C) 1987-2015 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. */
d1457701 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 228 printf ("#include \"coretypes.h\"\n");
c7131fb2 229 printf ("#include \"backend.h\"\n");
9fdcd34e 230 printf ("#include \"predict.h\"\n");
c7131fb2
AM
231 printf ("#include \"tree.h\"\n");
232 printf ("#include \"rtl.h\"\n");
ccd043a9 233 printf ("#include \"flags.h\"\n");
40e23961 234 printf ("#include \"alias.h\"\n");
d8a2d370
DN
235 printf ("#include \"varasm.h\"\n");
236 printf ("#include \"stor-layout.h\"\n");
237 printf ("#include \"calls.h\"\n");
36566b39
PK
238 printf ("#include \"insn-config.h\"\n");
239 printf ("#include \"expmed.h\"\n");
240 printf ("#include \"dojump.h\"\n");
241 printf ("#include \"explow.h\"\n");
242 printf ("#include \"emit-rtl.h\"\n");
243 printf ("#include \"stmt.h\"\n");
f3a8030a 244 printf ("#include \"expr.h\"\n");
e78d8e51 245 printf ("#include \"insn-codes.h\"\n");
6baf1cc8 246 printf ("#include \"tm_p.h\"\n");
9db4e0ec 247 printf ("#include \"regs.h\"\n");
9db4e0ec 248 printf ("#include \"conditions.h\"\n");
9db4e0ec 249 printf ("#include \"insn-attr.h\"\n\n");
9db4e0ec 250 printf ("#include \"recog.h\"\n\n");
79a3f089 251 printf ("#include \"diagnostic-core.h\"\n");
9db4e0ec 252 printf ("#include \"output.h\"\n");
4977bab6 253 printf ("#include \"target.h\"\n");
279bb624 254 printf ("#include \"tm-constrs.h\"\n");
9db4e0ec
RK
255}
256
a995e389 257static void
3d7aafde 258output_operand_data (void)
a995e389 259{
b3694847 260 struct operand_data *d;
a995e389
RH
261
262 printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
263
264 for (d = odata; d; d = d->next)
9db4e0ec 265 {
4fd3a105
RS
266 struct pred_data *pred;
267
a995e389
RH
268 printf (" {\n");
269
270 printf (" %s,\n",
271 d->predicate && d->predicate[0] ? d->predicate : "0");
272
19af6455 273 printf (" \"%s\",\n", d->constraint ? d->constraint : "");
9db4e0ec 274
a995e389
RH
275 printf (" %smode,\n", GET_MODE_NAME (d->mode));
276
dfac187e
BS
277 printf (" %d,\n", d->strict_low);
278
6ddfdb0f
BS
279 printf (" %d,\n", d->constraint == NULL ? 1 : 0);
280
4fd3a105
RS
281 printf (" %d,\n", d->eliminable);
282
283 pred = NULL;
284 if (d->predicate)
285 pred = lookup_predicate (d->predicate);
286 printf (" %d\n", pred && pred->codes[MEM]);
a995e389 287
c3284718 288 printf (" },\n");
a995e389 289 }
c3284718 290 printf ("};\n\n\n");
a995e389
RH
291}
292
293static void
3d7aafde 294output_insn_data (void)
a995e389 295{
b3694847 296 struct data *d;
a995e389
RH
297 int name_offset = 0;
298 int next_name_offset;
299 const char * last_name = 0;
300 const char * next_name = 0;
b3694847 301 struct data *n;
a995e389
RH
302
303 for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
304 if (n->name)
9db4e0ec 305 {
a995e389
RH
306 next_name = n->name;
307 break;
9db4e0ec 308 }
9db4e0ec 309
3897f229 310 printf ("#if GCC_VERSION >= 2007\n__extension__\n#endif\n");
f12c802a 311 printf ("\nconst struct insn_data_d insn_data[] = \n{\n");
9db4e0ec 312
a995e389 313 for (d = idata; d; d = d->next)
9db4e0ec 314 {
1e9c8405 315 printf (" /* %s:%d */\n", d->filename, d->lineno);
a995e389
RH
316 printf (" {\n");
317
318 if (d->name)
9db4e0ec 319 {
a995e389
RH
320 printf (" \"%s\",\n", d->name);
321 name_offset = 0;
322 last_name = d->name;
323 next_name = 0;
324 for (n = d->next, next_name_offset = 1; n;
325 n = n->next, next_name_offset++)
9db4e0ec 326 {
a995e389
RH
327 if (n->name)
328 {
329 next_name = n->name;
330 break;
331 }
9db4e0ec 332 }
9db4e0ec 333 }
a995e389 334 else
9db4e0ec 335 {
a995e389
RH
336 name_offset++;
337 if (next_name && (last_name == 0
338 || name_offset > next_name_offset / 2))
339 printf (" \"%s-%d\",\n", next_name,
340 next_name_offset - name_offset);
341 else
342 printf (" \"%s+%d\",\n", last_name, name_offset);
9db4e0ec 343 }
9db4e0ec 344
4bbf910e
RH
345 switch (d->output_format)
346 {
347 case INSN_OUTPUT_FORMAT_NONE:
9ca1483b 348 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
3897f229
JM
349 printf (" { 0 },\n");
350 printf ("#else\n");
351 printf (" { 0, 0, 0 },\n");
352 printf ("#endif\n");
4bbf910e
RH
353 break;
354 case INSN_OUTPUT_FORMAT_SINGLE:
212d447c 355 {
8ad97cfc 356 const char *p = d->template_code;
212d447c 357 char prev = 0;
3d7aafde 358
9ca1483b 359 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
3897f229
JM
360 printf (" { .single =\n");
361 printf ("#else\n");
362 printf (" {\n");
363 printf ("#endif\n");
212d447c
DC
364 printf (" \"");
365 while (*p)
366 {
6b8b9d7b
CM
367 if (IS_VSPACE (*p) && prev != '\\')
368 {
369 /* Preserve two consecutive \n's or \r's, but treat \r\n
370 as a single newline. */
371 if (*p == '\n' && prev != '\r')
372 printf ("\\n\\\n");
373 }
212d447c
DC
374 else
375 putchar (*p);
376 prev = *p;
377 ++p;
378 }
379 printf ("\",\n");
9ca1483b 380 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
3897f229
JM
381 printf (" },\n");
382 printf ("#else\n");
383 printf (" 0, 0 },\n");
384 printf ("#endif\n");
212d447c 385 }
4bbf910e
RH
386 break;
387 case INSN_OUTPUT_FORMAT_MULTI:
9ca1483b 388 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
3897f229
JM
389 printf (" { .multi = output_%d },\n", d->code_number);
390 printf ("#else\n");
391 printf (" { 0, output_%d, 0 },\n", d->code_number);
392 printf ("#endif\n");
393 break;
4bbf910e 394 case INSN_OUTPUT_FORMAT_FUNCTION:
9ca1483b 395 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
3897f229
JM
396 printf (" { .function = output_%d },\n", d->code_number);
397 printf ("#else\n");
398 printf (" { 0, 0, output_%d },\n", d->code_number);
399 printf ("#endif\n");
4bbf910e
RH
400 break;
401 default:
b2d59f6f 402 gcc_unreachable ();
4bbf910e 403 }
a995e389
RH
404
405 if (d->name && d->name[0] != '*')
eb923e54 406 printf (" { (insn_gen_fn::stored_funcptr) gen_%s },\n", d->name);
a995e389 407 else
eb923e54 408 printf (" { 0 },\n");
a995e389
RH
409
410 printf (" &operand_data[%d],\n", d->operand_number);
b29387ee 411 printf (" %d,\n", d->n_generator_args);
a995e389
RH
412 printf (" %d,\n", d->n_operands);
413 printf (" %d,\n", d->n_dups);
4bbf910e
RH
414 printf (" %d,\n", d->n_alternatives);
415 printf (" %d\n", d->output_format);
a995e389 416
c3284718 417 printf (" },\n");
9db4e0ec 418 }
a995e389
RH
419 printf ("};\n\n\n");
420}
9db4e0ec 421
a995e389 422static void
3d7aafde 423output_get_insn_name (void)
a995e389
RH
424{
425 printf ("const char *\n");
6906ba40 426 printf ("get_insn_name (int code)\n");
a995e389 427 printf ("{\n");
a0f0e963
SB
428 printf (" if (code == NOOP_MOVE_INSN_CODE)\n");
429 printf (" return \"NOOP_MOVE\";\n");
430 printf (" else\n");
431 printf (" return insn_data[code].name;\n");
a995e389 432 printf ("}\n");
9db4e0ec 433}
a995e389 434
9db4e0ec 435\f
b29387ee 436/* Stores the operand data into `d->operand[i]'.
9db4e0ec
RK
437
438 THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
439 THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
440
9db4e0ec 441static void
3d7aafde
AJ
442scan_operands (struct data *d, rtx part, int this_address_p,
443 int this_strict_low)
9db4e0ec 444{
b3694847
SS
445 int i, j;
446 const char *format_ptr;
9db4e0ec
RK
447 int opno;
448
449 if (part == 0)
450 return;
451
452 switch (GET_CODE (part))
453 {
454 case MATCH_OPERAND:
455 opno = XINT (part, 0);
b29387ee 456 if (opno >= MAX_MAX_OPERANDS)
5a806d65 457 {
bb933490 458 error_with_line (d->lineno, "maximum number of operands exceeded");
5a806d65
RK
459 return;
460 }
a995e389 461 if (d->operand[opno].seen)
bb933490 462 error_with_line (d->lineno, "repeated operand number %d\n", opno);
d96a2fcd 463
a995e389
RH
464 d->operand[opno].seen = 1;
465 d->operand[opno].mode = GET_MODE (part);
466 d->operand[opno].strict_low = this_strict_low;
467 d->operand[opno].predicate = XSTR (part, 1);
665f2503
RK
468 d->operand[opno].constraint = strip_whitespace (XSTR (part, 2));
469 d->operand[opno].n_alternatives
470 = n_occurrences (',', d->operand[opno].constraint) + 1;
a995e389 471 d->operand[opno].address_p = this_address_p;
dfac187e 472 d->operand[opno].eliminable = 1;
9db4e0ec
RK
473 return;
474
475 case MATCH_SCRATCH:
476 opno = XINT (part, 0);
b29387ee 477 if (opno >= MAX_MAX_OPERANDS)
5a806d65 478 {
bb933490 479 error_with_line (d->lineno, "maximum number of operands exceeded");
5a806d65
RK
480 return;
481 }
a995e389 482 if (d->operand[opno].seen)
bb933490 483 error_with_line (d->lineno, "repeated operand number %d\n", opno);
d96a2fcd 484
a995e389
RH
485 d->operand[opno].seen = 1;
486 d->operand[opno].mode = GET_MODE (part);
487 d->operand[opno].strict_low = 0;
488 d->operand[opno].predicate = "scratch_operand";
665f2503
RK
489 d->operand[opno].constraint = strip_whitespace (XSTR (part, 1));
490 d->operand[opno].n_alternatives
491 = n_occurrences (',', d->operand[opno].constraint) + 1;
a995e389 492 d->operand[opno].address_p = 0;
dfac187e 493 d->operand[opno].eliminable = 0;
9db4e0ec
RK
494 return;
495
496 case MATCH_OPERATOR:
497 case MATCH_PARALLEL:
498 opno = XINT (part, 0);
b29387ee 499 if (opno >= MAX_MAX_OPERANDS)
5a806d65 500 {
bb933490 501 error_with_line (d->lineno, "maximum number of operands exceeded");
5a806d65
RK
502 return;
503 }
a995e389 504 if (d->operand[opno].seen)
bb933490 505 error_with_line (d->lineno, "repeated operand number %d\n", opno);
d96a2fcd 506
a995e389
RH
507 d->operand[opno].seen = 1;
508 d->operand[opno].mode = GET_MODE (part);
509 d->operand[opno].strict_low = 0;
510 d->operand[opno].predicate = XSTR (part, 1);
511 d->operand[opno].constraint = 0;
512 d->operand[opno].address_p = 0;
dfac187e 513 d->operand[opno].eliminable = 0;
9db4e0ec 514 for (i = 0; i < XVECLEN (part, 2); i++)
a995e389 515 scan_operands (d, XVECEXP (part, 2, i), 0, 0);
9db4e0ec
RK
516 return;
517
9db4e0ec 518 case STRICT_LOW_PART:
a995e389 519 scan_operands (d, XEXP (part, 0), 0, 1);
9db4e0ec 520 return;
3d7aafde 521
ccd043a9
RL
522 default:
523 break;
9db4e0ec
RK
524 }
525
526 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
527
528 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
529 switch (*format_ptr++)
530 {
531 case 'e':
ccd043a9 532 case 'u':
a995e389 533 scan_operands (d, XEXP (part, i), 0, 0);
9db4e0ec
RK
534 break;
535 case 'E':
536 if (XVEC (part, i) != NULL)
537 for (j = 0; j < XVECLEN (part, i); j++)
a995e389 538 scan_operands (d, XVECEXP (part, i, j), 0, 0);
9db4e0ec
RK
539 break;
540 }
541}
a995e389
RH
542
543/* Compare two operands for content equality. */
544
545static int
3d7aafde 546compare_operands (struct operand_data *d0, struct operand_data *d1)
a995e389 547{
c1b59dce 548 const char *p0, *p1;
a995e389
RH
549
550 p0 = d0->predicate;
551 if (!p0)
552 p0 = "";
553 p1 = d1->predicate;
554 if (!p1)
555 p1 = "";
556 if (strcmp (p0, p1) != 0)
557 return 0;
558
19af6455
BS
559 p0 = d0->constraint;
560 if (!p0)
561 p0 = "";
562 p1 = d1->constraint;
563 if (!p1)
564 p1 = "";
565 if (strcmp (p0, p1) != 0)
566 return 0;
a995e389
RH
567
568 if (d0->mode != d1->mode)
569 return 0;
570
a995e389
RH
571 if (d0->strict_low != d1->strict_low)
572 return 0;
573
dfac187e
BS
574 if (d0->eliminable != d1->eliminable)
575 return 0;
576
a995e389
RH
577 return 1;
578}
579
580/* Scan the list of operands we've already committed to output and either
581 find a subsequence that is the same, or allocate a new one at the end. */
582
583static void
3d7aafde 584place_operands (struct data *d)
a995e389
RH
585{
586 struct operand_data *od, *od2;
587 int i;
588
589 if (d->n_operands == 0)
590 {
591 d->operand_number = 0;
592 return;
593 }
594
595 /* Brute force substring search. */
596 for (od = odata, i = 0; od; od = od->next, i = 0)
597 if (compare_operands (od, &d->operand[0]))
598 {
599 od2 = od->next;
600 i = 1;
601 while (1)
602 {
603 if (i == d->n_operands)
604 goto full_match;
605 if (od2 == NULL)
606 goto partial_match;
607 if (! compare_operands (od2, &d->operand[i]))
608 break;
609 ++i, od2 = od2->next;
610 }
611 }
612
613 /* Either partial match at the end of the list, or no match. In either
614 case, we tack on what operands are remaining to the end of the list. */
615 partial_match:
616 d->operand_number = next_operand_number - i;
617 for (; i < d->n_operands; ++i)
618 {
619 od2 = &d->operand[i];
620 *odata_end = od2;
621 odata_end = &od2->next;
622 od2->index = next_operand_number++;
623 }
624 *odata_end = NULL;
625 return;
626
627 full_match:
628 d->operand_number = od->index;
629 return;
630}
631
9db4e0ec
RK
632\f
633/* Process an assembler template from a define_insn or a define_peephole.
634 It is either the assembler code template, a list of assembler code
635 templates, or C code to generate the assembler code template. */
636
637static void
8ad97cfc 638process_template (struct data *d, const char *template_code)
9db4e0ec 639{
b3694847
SS
640 const char *cp;
641 int i;
9db4e0ec 642
4bbf910e 643 /* Templates starting with * contain straight code to be run. */
8ad97cfc 644 if (template_code[0] == '*')
9db4e0ec 645 {
8ad97cfc 646 d->template_code = 0;
4bbf910e 647 d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
9db4e0ec 648
4bbf910e 649 puts ("\nstatic const char *");
95770ca3 650 printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n",
6906ba40 651 d->code_number);
4bbf910e 652 puts ("{");
d2a3ce4e 653 print_md_ptr_loc (template_code);
8ad97cfc 654 puts (template_code + 1);
4bbf910e
RH
655 puts ("}");
656 }
9db4e0ec
RK
657
658 /* If the assembler code template starts with a @ it is a newline-separated
4bbf910e 659 list of assembler code templates, one for each alternative. */
8ad97cfc 660 else if (template_code[0] == '@')
9db4e0ec 661 {
94c765ab 662 int found_star = 0;
9db4e0ec 663
94c765ab
R
664 for (cp = &template_code[1]; *cp; )
665 {
666 while (ISSPACE (*cp))
667 cp++;
668 if (*cp == '*')
669 found_star = 1;
670 while (!IS_VSPACE (*cp) && *cp != '\0')
671 ++cp;
672 }
673 d->template_code = 0;
674 if (found_star)
675 {
676 d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
677 puts ("\nstatic const char *");
678 printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
95770ca3 679 "rtx_insn *insn ATTRIBUTE_UNUSED)\n", d->code_number);
94c765ab
R
680 puts ("{");
681 puts (" switch (which_alternative)\n {");
682 }
683 else
684 {
685 d->output_format = INSN_OUTPUT_FORMAT_MULTI;
686 printf ("\nstatic const char * const output_%d[] = {\n",
687 d->code_number);
688 }
9db4e0ec 689
8ad97cfc 690 for (i = 0, cp = &template_code[1]; *cp; )
9db4e0ec 691 {
94c765ab 692 const char *ep, *sp, *bp;
4112be4a 693
6b8b9d7b 694 while (ISSPACE (*cp))
9db4e0ec
RK
695 cp++;
696
94c765ab
R
697 bp = cp;
698 if (found_star)
699 {
700 printf (" case %d:", i);
701 if (*cp == '*')
702 {
703 printf ("\n ");
704 cp++;
705 }
706 else
707 printf (" return \"");
708 }
709 else
710 printf (" \"");
4112be4a
JJ
711
712 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
713 if (!ISSPACE (*ep))
714 sp = ep + 1;
715
716 if (sp != ep)
717 message_with_line (d->lineno,
718 "trailing whitespace in output template");
719
720 while (cp < sp)
2f013c71
RK
721 {
722 putchar (*cp);
723 cp++;
724 }
9db4e0ec 725
94c765ab
R
726 if (!found_star)
727 puts ("\",");
728 else if (*bp != '*')
729 puts ("\";");
730 else
731 {
732 /* The usual action will end with a return.
733 If there is neither break or return at the end, this is
734 assumed to be intentional; this allows to have multiple
735 consecutive alternatives share some code. */
736 puts ("");
737 }
9db4e0ec
RK
738 i++;
739 }
c6d79bee
JH
740 if (i == 1)
741 message_with_line (d->lineno,
742 "'@' is redundant for output template with single alternative");
743 if (i != d->n_alternatives)
bb933490
RS
744 error_with_line (d->lineno,
745 "wrong number of alternatives in the output template");
9db4e0ec 746
94c765ab
R
747 if (found_star)
748 puts (" default: gcc_unreachable ();\n }\n}");
749 else
750 printf ("};\n");
9db4e0ec
RK
751 }
752 else
753 {
8ad97cfc 754 d->template_code = template_code;
4bbf910e 755 d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
9db4e0ec 756 }
9db4e0ec
RK
757}
758\f
759/* Check insn D for consistency in number of constraint alternatives. */
760
761static void
3d7aafde 762validate_insn_alternatives (struct data *d)
9db4e0ec 763{
b3694847 764 int n = 0, start;
a995e389
RH
765
766 /* Make sure all the operands have the same number of alternatives
767 in their constraints. Let N be that number. */
9db4e0ec 768 for (start = 0; start < d->n_operands; start++)
a995e389 769 if (d->operand[start].n_alternatives > 0)
9db4e0ec 770 {
97488870
R
771 int len, i;
772 const char *p;
773 char c;
774 int which_alternative = 0;
775 int alternative_count_unsure = 0;
5fd4bc96 776 bool seen_write = false;
97488870
R
777
778 for (p = d->operand[start].constraint; (c = *p); p += len)
779 {
73f793e3
RS
780 if ((c == '%' || c == '=' || c == '+')
781 && p != d->operand[start].constraint)
782 error_with_line (d->lineno,
783 "character '%c' can only be used at the"
784 " beginning of a constraint string", c);
5fd4bc96
JG
785
786 if (c == '=' || c == '+')
787 seen_write = true;
788
789 /* Earlyclobber operands must always be marked write-only
790 or read/write. */
791 if (!seen_write && c == '&')
792 error_with_line (d->lineno,
793 "earlyclobber operands may not be"
794 " read-only in alternative %d",
795 which_alternative);
796
f38840db
ZW
797 if (ISSPACE (c) || strchr (indep_constraints, c))
798 len = 1;
799 else if (ISDIGIT (c))
800 {
801 const char *q = p;
802 do
803 q++;
804 while (ISDIGIT (*q));
805 len = q - p;
806 }
807 else
808 len = mdep_constraint_len (p, d->lineno, start);
97488870
R
809
810 if (c == ',')
811 {
812 which_alternative++;
813 continue;
814 }
815
816 for (i = 1; i < len; i++)
817 if (p[i] == '\0')
818 {
bb933490
RS
819 error_with_line (d->lineno,
820 "NUL in alternative %d of operand %d",
821 which_alternative, start);
97488870
R
822 alternative_count_unsure = 1;
823 break;
824 }
825 else if (strchr (",#*", p[i]))
826 {
bb933490
RS
827 error_with_line (d->lineno,
828 "'%c' in alternative %d of operand %d",
829 p[i], which_alternative, start);
97488870
R
830 alternative_count_unsure = 1;
831 }
832 }
bb933490 833 if (!alternative_count_unsure)
d96a2fcd 834 {
bb933490
RS
835 if (n == 0)
836 n = d->operand[start].n_alternatives;
837 else if (n != d->operand[start].n_alternatives)
838 error_with_line (d->lineno,
d96a2fcd
RH
839 "wrong number of alternatives in operand %d",
840 start);
d96a2fcd 841 }
9db4e0ec 842 }
a995e389 843
9db4e0ec
RK
844 /* Record the insn's overall number of alternatives. */
845 d->n_alternatives = n;
846}
c77e04ae
RH
847
848/* Verify that there are no gaps in operand numbers for INSNs. */
849
850static void
3d7aafde 851validate_insn_operands (struct data *d)
c77e04ae
RH
852{
853 int i;
854
855 for (i = 0; i < d->n_operands; ++i)
856 if (d->operand[i].seen == 0)
bb933490 857 error_with_line (d->lineno, "missing operand %d", i);
c77e04ae 858}
7ddf71e3
PB
859
860static void
861validate_optab_operands (struct data *d)
862{
863 if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
864 return;
865
866 /* Miscellaneous tests. */
867 if (strncmp (d->name, "cstore", 6) == 0
868 && d->name[strlen (d->name) - 1] == '4'
869 && d->operand[0].mode == VOIDmode)
870 {
871 message_with_line (d->lineno, "missing mode for operand 0 of cstore");
872 have_error = 1;
873 }
874}
9db4e0ec 875\f
a995e389
RH
876/* Look at a define_insn just read. Assign its code number. Record
877 on idata the template and the number of arguments. If the insn has
878 a hairy output action, output a function for now. */
9db4e0ec
RK
879
880static void
3d7aafde 881gen_insn (rtx insn, int lineno)
9db4e0ec 882{
b29387ee 883 struct pattern_stats stats;
5ed6ace5 884 struct data *d = XNEW (struct data);
b3694847 885 int i;
9db4e0ec 886
c88c0d42 887 d->code_number = next_code_number;
9db4e0ec 888 d->index_number = next_index_number;
d2a3ce4e 889 d->filename = read_md_filename;
d96a2fcd 890 d->lineno = lineno;
9db4e0ec
RK
891 if (XSTR (insn, 0)[0])
892 d->name = XSTR (insn, 0);
893 else
894 d->name = 0;
895
896 /* Build up the list in the same order as the insns are seen
897 in the machine description. */
898 d->next = 0;
a995e389
RH
899 *idata_end = d;
900 idata_end = &d->next;
9db4e0ec 901
a995e389 902 memset (d->operand, 0, sizeof (d->operand));
9db4e0ec
RK
903
904 for (i = 0; i < XVECLEN (insn, 1); i++)
a995e389 905 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
9db4e0ec 906
b29387ee
RS
907 get_pattern_stats (&stats, XVEC (insn, 1));
908 d->n_generator_args = stats.num_generator_args;
909 d->n_operands = stats.num_insn_operands;
910 d->n_dups = stats.num_dups;
9db4e0ec 911
c77e04ae 912 validate_insn_operands (d);
9db4e0ec 913 validate_insn_alternatives (d);
7ddf71e3 914 validate_optab_operands (d);
a995e389 915 place_operands (d);
1f3b37a3 916 process_template (d, XTMPL (insn, 3));
9db4e0ec
RK
917}
918\f
919/* Look at a define_peephole just read. Assign its code number.
a995e389 920 Record on idata the template and the number of arguments.
9db4e0ec
RK
921 If the insn has a hairy output action, output it now. */
922
923static void
3d7aafde 924gen_peephole (rtx peep, int lineno)
9db4e0ec 925{
b29387ee 926 struct pattern_stats stats;
5ed6ace5 927 struct data *d = XNEW (struct data);
b3694847 928 int i;
9db4e0ec 929
c88c0d42 930 d->code_number = next_code_number;
9db4e0ec 931 d->index_number = next_index_number;
d2a3ce4e 932 d->filename = read_md_filename;
d96a2fcd 933 d->lineno = lineno;
9db4e0ec
RK
934 d->name = 0;
935
936 /* Build up the list in the same order as the insns are seen
937 in the machine description. */
938 d->next = 0;
a995e389
RH
939 *idata_end = d;
940 idata_end = &d->next;
9db4e0ec 941
a995e389
RH
942 memset (d->operand, 0, sizeof (d->operand));
943
944 /* Get the number of operands by scanning all the patterns of the
945 peephole optimizer. But ignore all the rest of the information
946 thus obtained. */
9db4e0ec 947 for (i = 0; i < XVECLEN (peep, 0); i++)
a995e389 948 scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
9db4e0ec 949
b29387ee
RS
950 get_pattern_stats (&stats, XVEC (peep, 0));
951 d->n_generator_args = 0;
952 d->n_operands = stats.num_insn_operands;
9db4e0ec
RK
953 d->n_dups = 0;
954
9db4e0ec 955 validate_insn_alternatives (d);
a995e389 956 place_operands (d);
1f3b37a3 957 process_template (d, XTMPL (peep, 2));
9db4e0ec
RK
958}
959\f
960/* Process a define_expand just read. Assign its code number,
961 only for the purposes of `insn_gen_function'. */
962
963static void
3d7aafde 964gen_expand (rtx insn, int lineno)
9db4e0ec 965{
b29387ee 966 struct pattern_stats stats;
5ed6ace5 967 struct data *d = XNEW (struct data);
b3694847 968 int i;
9db4e0ec 969
c88c0d42 970 d->code_number = next_code_number;
9db4e0ec 971 d->index_number = next_index_number;
d2a3ce4e 972 d->filename = read_md_filename;
d96a2fcd 973 d->lineno = lineno;
9db4e0ec
RK
974 if (XSTR (insn, 0)[0])
975 d->name = XSTR (insn, 0);
976 else
977 d->name = 0;
978
979 /* Build up the list in the same order as the insns are seen
980 in the machine description. */
981 d->next = 0;
a995e389
RH
982 *idata_end = d;
983 idata_end = &d->next;
9db4e0ec 984
a995e389 985 memset (d->operand, 0, sizeof (d->operand));
9db4e0ec
RK
986
987 /* Scan the operands to get the specified predicates and modes,
988 since expand_binop needs to know them. */
989
9db4e0ec
RK
990 if (XVEC (insn, 1))
991 for (i = 0; i < XVECLEN (insn, 1); i++)
a995e389 992 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
9db4e0ec 993
b29387ee
RS
994 get_pattern_stats (&stats, XVEC (insn, 1));
995 d->n_generator_args = stats.num_generator_args;
996 d->n_operands = stats.num_insn_operands;
997 d->n_dups = stats.num_dups;
8ad97cfc 998 d->template_code = 0;
4bbf910e 999 d->output_format = INSN_OUTPUT_FORMAT_NONE;
a995e389 1000
9db4e0ec 1001 validate_insn_alternatives (d);
7ddf71e3 1002 validate_optab_operands (d);
a995e389 1003 place_operands (d);
9db4e0ec
RK
1004}
1005\f
1006/* Process a define_split just read. Assign its code number,
1007 only for reasons of consistency and to simplify genrecog. */
1008
9db4e0ec 1009static void
3d7aafde 1010gen_split (rtx split, int lineno)
9db4e0ec 1011{
b29387ee 1012 struct pattern_stats stats;
5ed6ace5 1013 struct data *d = XNEW (struct data);
b3694847 1014 int i;
9db4e0ec 1015
c88c0d42 1016 d->code_number = next_code_number;
9db4e0ec 1017 d->index_number = next_index_number;
d2a3ce4e 1018 d->filename = read_md_filename;
d96a2fcd 1019 d->lineno = lineno;
9db4e0ec
RK
1020 d->name = 0;
1021
1022 /* Build up the list in the same order as the insns are seen
1023 in the machine description. */
1024 d->next = 0;
a995e389
RH
1025 *idata_end = d;
1026 idata_end = &d->next;
9db4e0ec 1027
a995e389 1028 memset (d->operand, 0, sizeof (d->operand));
9db4e0ec 1029
a995e389
RH
1030 /* Get the number of operands by scanning all the patterns of the
1031 split patterns. But ignore all the rest of the information thus
1032 obtained. */
9db4e0ec 1033 for (i = 0; i < XVECLEN (split, 0); i++)
a995e389 1034 scan_operands (d, XVECEXP (split, 0, i), 0, 0);
9db4e0ec 1035
b29387ee
RS
1036 get_pattern_stats (&stats, XVEC (split, 0));
1037 d->n_generator_args = 0;
1038 d->n_operands = stats.num_insn_operands;
9db4e0ec 1039 d->n_dups = 0;
42495ca0 1040 d->n_alternatives = 0;
8ad97cfc 1041 d->template_code = 0;
4bbf910e 1042 d->output_format = INSN_OUTPUT_FORMAT_NONE;
a995e389
RH
1043
1044 place_operands (d);
9db4e0ec 1045}
9db4e0ec 1046
e714561a
SB
1047static void
1048init_insn_for_nothing (void)
1049{
1050 memset (&nothing, 0, sizeof (nothing));
1051 nothing.name = "*placeholder_for_nothing";
1052 nothing.filename = "<internal>";
1053}
1054
3d7aafde 1055extern int main (int, char **);
c1b59dce 1056
9db4e0ec 1057int
3d7aafde 1058main (int argc, char **argv)
9db4e0ec
RK
1059{
1060 rtx desc;
9db4e0ec 1061
d80eb1e1
RH
1062 progname = "genoutput";
1063
e714561a
SB
1064 init_insn_for_nothing ();
1065
600ab3fc 1066 if (!init_rtx_reader_args (argc, argv))
c88c0d42 1067 return (FATAL_EXIT_CODE);
9db4e0ec 1068
9db4e0ec 1069 output_prologue ();
9db4e0ec 1070 next_index_number = 0;
9db4e0ec
RK
1071
1072 /* Read the machine description. */
1073
1074 while (1)
1075 {
c88c0d42
CP
1076 int line_no;
1077
1078 desc = read_md_rtx (&line_no, &next_code_number);
1079 if (desc == NULL)
9db4e0ec 1080 break;
9db4e0ec 1081
f38840db
ZW
1082 switch (GET_CODE (desc))
1083 {
1084 case DEFINE_INSN:
1085 gen_insn (desc, line_no);
1086 break;
1087
1088 case DEFINE_PEEPHOLE:
1089 gen_peephole (desc, line_no);
1090 break;
1091
1092 case DEFINE_EXPAND:
1093 gen_expand (desc, line_no);
1094 break;
1095
1096 case DEFINE_SPLIT:
1097 case DEFINE_PEEPHOLE2:
1098 gen_split (desc, line_no);
1099 break;
1100
f38840db
ZW
1101 case DEFINE_CONSTRAINT:
1102 case DEFINE_REGISTER_CONSTRAINT:
1103 case DEFINE_ADDRESS_CONSTRAINT:
1104 case DEFINE_MEMORY_CONSTRAINT:
1105 note_constraint (desc, line_no);
1106 break;
f38840db
ZW
1107
1108 default:
1109 break;
1110 }
9db4e0ec
RK
1111 next_index_number++;
1112 }
1113
c3284718 1114 printf ("\n\n");
a995e389
RH
1115 output_operand_data ();
1116 output_insn_data ();
1117 output_get_insn_name ();
9db4e0ec
RK
1118
1119 fflush (stdout);
c1b59dce 1120 return (ferror (stdout) != 0 || have_error
6a270722 1121 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
9db4e0ec
RK
1122}
1123
665f2503
RK
1124/* Return the number of occurrences of character C in string S or
1125 -1 if S is the null string. */
1126
9db4e0ec 1127static int
3d7aafde 1128n_occurrences (int c, const char *s)
9db4e0ec
RK
1129{
1130 int n = 0;
665f2503
RK
1131
1132 if (s == 0 || *s == '\0')
1133 return -1;
1134
9db4e0ec
RK
1135 while (*s)
1136 n += (*s++ == c);
665f2503 1137
9db4e0ec
RK
1138 return n;
1139}
88a56c2e 1140
665f2503
RK
1141/* Remove whitespace in `s' by moving up characters until the end.
1142 Return a new string. */
1143
1144static const char *
3d7aafde 1145strip_whitespace (const char *s)
88a56c2e 1146{
665f2503
RK
1147 char *p, *q;
1148 char ch;
1149
1150 if (s == 0)
1151 return 0;
88a56c2e 1152
5ed6ace5 1153 p = q = XNEWVEC (char, strlen (s) + 1);
88a56c2e
HPN
1154 while ((ch = *s++) != '\0')
1155 if (! ISSPACE (ch))
1156 *p++ = ch;
1157
1158 *p = '\0';
665f2503 1159 return q;
88a56c2e 1160}
97488870 1161
f38840db
ZW
1162/* Record just enough information about a constraint to allow checking
1163 of operand constraint strings above, in validate_insn_alternatives.
1164 Does not validate most properties of the constraint itself; does
1165 enforce no duplicate names, no overlap with MI constraints, and no
1166 prefixes. EXP is the define_*constraint form, LINENO the line number
1167 reported by the reader. */
1168static void
1169note_constraint (rtx exp, int lineno)
1170{
1171 const char *name = XSTR (exp, 0);
8ad97cfc 1172 struct constraint_data **iter, **slot, *new_cdata;
f38840db 1173
8677664e
RS
1174 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
1175 name = general_mem;
1176 unsigned int namelen = strlen (name);
1177
1178 if (strchr (indep_constraints, name[0]))
f38840db
ZW
1179 {
1180 if (name[1] == '\0')
bb933490
RS
1181 error_with_line (lineno, "constraint letter '%s' cannot be "
1182 "redefined by the machine description", name);
f38840db 1183 else
bb933490
RS
1184 error_with_line (lineno, "constraint name '%s' cannot be defined by "
1185 "the machine description, as it begins with '%c'",
1186 name, name[0]);
f38840db
ZW
1187 return;
1188 }
1189
1190 slot = &constraints_by_letter_table[(unsigned int)name[0]];
1191 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
1192 {
1193 /* This causes slot to end up pointing to the
1194 next_this_letter field of the last constraint with a name
1195 of equal or greater length than the new constraint; hence
1196 the new constraint will be inserted after all previous
1197 constraints with names of the same length. */
1198 if ((*iter)->namelen >= namelen)
1199 slot = iter;
1200
1201 if (!strcmp ((*iter)->name, name))
1202 {
bb933490 1203 error_with_line (lineno, "redefinition of constraint '%s'", name);
f38840db 1204 message_with_line ((*iter)->lineno, "previous definition is here");
f38840db
ZW
1205 return;
1206 }
1207 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
1208 {
bb933490 1209 error_with_line (lineno, "defining constraint '%s' here", name);
f38840db
ZW
1210 message_with_line ((*iter)->lineno, "renders constraint '%s' "
1211 "(defined here) a prefix", (*iter)->name);
f38840db
ZW
1212 return;
1213 }
1214 else if (!strncmp ((*iter)->name, name, namelen))
1215 {
bb933490 1216 error_with_line (lineno, "constraint '%s' is a prefix", name);
f38840db
ZW
1217 message_with_line ((*iter)->lineno, "of constraint '%s' "
1218 "(defined here)", (*iter)->name);
f38840db
ZW
1219 return;
1220 }
1221 }
8ad97cfc 1222 new_cdata = XNEWVAR (struct constraint_data, sizeof (struct constraint_data) + namelen);
c3284718 1223 strcpy (CONST_CAST (char *, new_cdata->name), name);
8ad97cfc
KG
1224 new_cdata->namelen = namelen;
1225 new_cdata->lineno = lineno;
1226 new_cdata->next_this_letter = *slot;
1227 *slot = new_cdata;
f38840db
ZW
1228}
1229
1230/* Return the length of the constraint name beginning at position S
1231 of an operand constraint string, or issue an error message if there
1232 is no such constraint. Does not expect to be called for generic
1233 constraints. */
1234static int
1235mdep_constraint_len (const char *s, int lineno, int opno)
1236{
1237 struct constraint_data *p;
1238
1239 p = constraints_by_letter_table[(unsigned int)s[0]];
1240
1241 if (p)
1242 for (; p; p = p->next_this_letter)
1243 if (!strncmp (s, p->name, p->namelen))
1244 return p->namelen;
1245
bb933490
RS
1246 error_with_line (lineno,
1247 "error: undefined machine-specific constraint "
1248 "at this point: \"%s\"", s);
f38840db 1249 message_with_line (lineno, "note: in operand %d", opno);
f38840db
ZW
1250 return 1; /* safe */
1251}