]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genpreds.c
libphobos: core.atomic should have fallback when there's no libatomic.
[thirdparty/gcc.git] / gcc / genpreds.c
CommitLineData
e23a6527 1/* Generate from machine description:
cbf464bd 2 - prototype declarations for operand predicates (tm-preds.h)
3 - function definitions of operand predicates, if defined new-style
4 (insn-preds.c)
fbd26352 5 Copyright (C) 2001-2019 Free Software Foundation, Inc.
e23a6527 6
049df704 7This file is part of GCC.
e23a6527 8
049df704 9GCC is free software; you can redistribute it and/or modify
e23a6527 10it under the terms of the GNU General Public License as published by
8c4c00c1 11the Free Software Foundation; either version 3, or (at your option)
e23a6527 12any later version.
13
049df704 14GCC is distributed in the hope that it will be useful,
e23a6527 15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
8c4c00c1 20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
e23a6527 22
805e22b2 23#include "bconfig.h"
e23a6527 24#include "system.h"
805e22b2 25#include "coretypes.h"
26#include "tm.h"
e23a6527 27#include "rtl.h"
cbf464bd 28#include "errors.h"
cbf464bd 29#include "obstack.h"
960ebfe7 30#include "read-md.h"
026d3868 31#include "gensupport.h"
e23a6527 32
69449463 33static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
34
5d1f1932 35/* Given a predicate expression EXP, from form NAME at location LOC,
026d3868 36 verify that it does not contain any RTL constructs which are not
37 valid in predicate definitions. Returns true if EXP is
38 INvalid; issues error messages, caller need not. */
39static bool
5d1f1932 40validate_exp (rtx exp, const char *name, file_location loc)
e23a6527 41{
026d3868 42 if (exp == 0)
cbf464bd 43 {
5d1f1932 44 message_at (loc, "%s: must give a predicate expression", name);
026d3868 45 return true;
cbf464bd 46 }
47
026d3868 48 switch (GET_CODE (exp))
49 {
50 /* Ternary, binary, unary expressions: recurse into subexpressions. */
51 case IF_THEN_ELSE:
5d1f1932 52 if (validate_exp (XEXP (exp, 2), name, loc))
026d3868 53 return true;
e3533433 54 /* fall through */
026d3868 55 case AND:
56 case IOR:
5d1f1932 57 if (validate_exp (XEXP (exp, 1), name, loc))
026d3868 58 return true;
e3533433 59 /* fall through */
026d3868 60 case NOT:
5d1f1932 61 return validate_exp (XEXP (exp, 0), name, loc);
cbf464bd 62
026d3868 63 /* MATCH_CODE might have a syntax error in its path expression. */
64 case MATCH_CODE:
65 {
66 const char *p;
67 for (p = XSTR (exp, 1); *p; p++)
68 {
69 if (!ISDIGIT (*p) && !ISLOWER (*p))
70 {
5d1f1932 71 error_at (loc, "%s: invalid character in path "
72 "string '%s'", name, XSTR (exp, 1));
026d3868 73 return true;
74 }
75 }
76 }
3c77f69c 77 gcc_fallthrough ();
cbf464bd 78
026d3868 79 /* These need no special checking. */
80 case MATCH_OPERAND:
81 case MATCH_TEST:
82 return false;
83
84 default:
5d1f1932 85 error_at (loc, "%s: cannot use '%s' in a predicate expression",
86 name, GET_RTX_NAME (GET_CODE (exp)));
026d3868 87 return true;
88 }
cbf464bd 89}
90
026d3868 91/* Predicates are defined with (define_predicate) or
92 (define_special_predicate) expressions in the machine description. */
cbf464bd 93static void
c04601c1 94process_define_predicate (md_rtx_info *info)
cbf464bd 95{
c04601c1 96 validate_exp (XEXP (info->def, 1), XSTR (info->def, 0), info->loc);
cbf464bd 97}
98
99/* Given a predicate, if it has an embedded C block, write the block
100 out as a static inline subroutine, and augment the RTL test with a
101 match_test that calls that subroutine. For instance,
102
103 (define_predicate "basereg_operand"
104 (match_operand 0 "register_operand")
105 {
106 if (GET_CODE (op) == SUBREG)
107 op = SUBREG_REG (op);
108 return REG_POINTER (op);
109 })
110
111 becomes
112
3754d046 113 static inline int basereg_operand_1(rtx op, machine_mode mode)
cbf464bd 114 {
115 if (GET_CODE (op) == SUBREG)
116 op = SUBREG_REG (op);
117 return REG_POINTER (op);
118 }
119
120 (define_predicate "basereg_operand"
121 (and (match_operand 0 "register_operand")
122 (match_test "basereg_operand_1 (op, mode)")))
123
124 The only wart is that there's no way to insist on a { } string in
dac49aa5 125 an RTL template, so we have to handle "" strings. */
cbf464bd 126
48e1416a 127
cbf464bd 128static void
129write_predicate_subfunction (struct pred_data *p)
130{
131 const char *match_test_str;
132 rtx match_test_exp, and_exp;
133
134 if (p->c_block[0] == '\0')
135 return;
136
137 /* Construct the function-call expression. */
138 obstack_grow (rtl_obstack, p->name, strlen (p->name));
139 obstack_grow (rtl_obstack, "_1 (op, mode)",
140 sizeof "_1 (op, mode)");
4fac984f 141 match_test_str = XOBFINISH (rtl_obstack, const char *);
cbf464bd 142
143 /* Add the function-call expression to the complete expression to be
144 evaluated. */
145 match_test_exp = rtx_alloc (MATCH_TEST);
146 XSTR (match_test_exp, 0) = match_test_str;
147
148 and_exp = rtx_alloc (AND);
149 XEXP (and_exp, 0) = p->exp;
150 XEXP (and_exp, 1) = match_test_exp;
151
152 p->exp = and_exp;
153
154 printf ("static inline int\n"
692dc725 155 "%s_1 (rtx op ATTRIBUTE_UNUSED, machine_mode mode ATTRIBUTE_UNUSED)\n",
cbf464bd 156 p->name);
e1e9159b 157 rtx_reader_ptr->print_md_ptr_loc (p->c_block);
cbf464bd 158 if (p->c_block[0] == '{')
159 fputs (p->c_block, stdout);
160 else
161 printf ("{\n %s\n}", p->c_block);
162 fputs ("\n\n", stdout);
163}
164
026d3868 165/* Given a predicate expression EXP, from form NAME, determine whether
166 it refers to the variable given as VAR. */
167static bool
168needs_variable (rtx exp, const char *var)
169{
170 switch (GET_CODE (exp))
171 {
172 /* Ternary, binary, unary expressions need a variable if
173 any of their subexpressions do. */
174 case IF_THEN_ELSE:
175 if (needs_variable (XEXP (exp, 2), var))
176 return true;
e3533433 177 /* fall through */
026d3868 178 case AND:
179 case IOR:
180 if (needs_variable (XEXP (exp, 1), var))
181 return true;
e3533433 182 /* fall through */
026d3868 183 case NOT:
184 return needs_variable (XEXP (exp, 0), var);
185
186 /* MATCH_CODE uses "op", but nothing else. */
187 case MATCH_CODE:
188 return !strcmp (var, "op");
189
190 /* MATCH_OPERAND uses "op" and may use "mode". */
191 case MATCH_OPERAND:
192 if (!strcmp (var, "op"))
193 return true;
194 if (!strcmp (var, "mode") && GET_MODE (exp) == VOIDmode)
195 return true;
196 return false;
197
198 /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
199 case MATCH_TEST:
200 {
201 const char *p = XSTR (exp, 0);
202 const char *q = strstr (p, var);
203 if (!q)
204 return false;
205 if (q != p && (ISALNUM (q[-1]) || q[-1] == '_'))
206 return false;
207 q += strlen (var);
6d92fc03 208 if (ISALNUM (q[0]) || q[0] == '_')
026d3868 209 return false;
210 }
211 return true;
212
213 default:
214 gcc_unreachable ();
215 }
216}
217
cbf464bd 218/* Given an RTL expression EXP, find all subexpressions which we may
219 assume to perform mode tests. Normal MATCH_OPERAND does;
358c7825 220 MATCH_CODE doesn't as such (although certain codes always have
221 VOIDmode); and we have to assume that MATCH_TEST does not.
222 These combine in almost-boolean fashion - the only exception is
223 that (not X) must be assumed not to perform a mode test, whether
224 or not X does.
cbf464bd 225
226 The mark is the RTL /v flag, which is true for subexpressions which
227 do *not* perform mode tests.
228*/
229#define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
230static void
231mark_mode_tests (rtx exp)
232{
233 switch (GET_CODE (exp))
234 {
235 case MATCH_OPERAND:
236 {
237 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
238 if (!p)
239 error ("reference to undefined predicate '%s'", XSTR (exp, 1));
3f980c56 240 else if (p->special || GET_MODE (exp) != VOIDmode)
cbf464bd 241 NO_MODE_TEST (exp) = 1;
242 }
243 break;
244
245 case MATCH_CODE:
358c7825 246 NO_MODE_TEST (exp) = 1;
cbf464bd 247 break;
248
249 case MATCH_TEST:
250 case NOT:
251 NO_MODE_TEST (exp) = 1;
252 break;
253
254 case AND:
255 mark_mode_tests (XEXP (exp, 0));
256 mark_mode_tests (XEXP (exp, 1));
257
258 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
259 && NO_MODE_TEST (XEXP (exp, 1)));
260 break;
48e1416a 261
cbf464bd 262 case IOR:
263 mark_mode_tests (XEXP (exp, 0));
264 mark_mode_tests (XEXP (exp, 1));
265
266 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
267 || NO_MODE_TEST (XEXP (exp, 1)));
268 break;
269
270 case IF_THEN_ELSE:
271 /* A ? B : C does a mode test if (one of A and B) does a mode
272 test, and C does too. */
273 mark_mode_tests (XEXP (exp, 0));
274 mark_mode_tests (XEXP (exp, 1));
275 mark_mode_tests (XEXP (exp, 2));
276
277 NO_MODE_TEST (exp) = ((NO_MODE_TEST (XEXP (exp, 0))
278 && NO_MODE_TEST (XEXP (exp, 1)))
279 || NO_MODE_TEST (XEXP (exp, 2)));
280 break;
281
282 default:
026d3868 283 gcc_unreachable ();
cbf464bd 284 }
285}
286
868178ed 287/* Determine whether the expression EXP is a MATCH_CODE that should
288 be written as a switch statement. */
289static bool
290generate_switch_p (rtx exp)
291{
292 return GET_CODE (exp) == MATCH_CODE
293 && strchr (XSTR (exp, 0), ',');
294}
295
cbf464bd 296/* Given a predicate, work out where in its RTL expression to add
297 tests for proper modes. Special predicates do not get any such
298 tests. We try to avoid adding tests when we don't have to; in
299 particular, other normal predicates can be counted on to do it for
300 us. */
301
302static void
303add_mode_tests (struct pred_data *p)
304{
305 rtx match_test_exp, and_exp;
306 rtx *pos;
307
308 /* Don't touch special predicates. */
309 if (p->special)
310 return;
311
358c7825 312 /* Check whether the predicate accepts const scalar ints (which always
313 have a stored mode of VOIDmode, but logically have a real mode)
314 and whether it matches anything besides const scalar ints. */
315 bool matches_const_scalar_int_p = false;
316 bool matches_other_p = false;
317 for (int i = 0; i < NUM_RTX_CODE; ++i)
318 if (p->codes[i])
319 switch (i)
320 {
321 case CONST_INT:
322 case CONST_WIDE_INT:
8770d0ad 323 /* Special handling for (VOIDmode) LABEL_REFs. */
324 case LABEL_REF:
358c7825 325 matches_const_scalar_int_p = true;
326 break;
327
328 case CONST_DOUBLE:
329 if (!TARGET_SUPPORTS_WIDE_INT)
330 matches_const_scalar_int_p = true;
331 matches_other_p = true;
332 break;
333
334 default:
335 matches_other_p = true;
336 break;
337 }
338
339 /* There's no need for a mode check if the predicate only accepts
340 constant integers. The code checks in the predicate are enough
341 to establish that the mode is VOIDmode.
342
343 Note that the predicate itself should check whether a scalar
344 integer is in range of the given mode. */
345 if (!matches_other_p)
346 return;
347
cbf464bd 348 mark_mode_tests (p->exp);
349
350 /* If the whole expression already tests the mode, we're done. */
351 if (!NO_MODE_TEST (p->exp))
352 return;
353
354 match_test_exp = rtx_alloc (MATCH_TEST);
358c7825 355 if (matches_const_scalar_int_p)
356 XSTR (match_test_exp, 0) = ("mode == VOIDmode || GET_MODE (op) == mode"
357 " || GET_MODE (op) == VOIDmode");
358 else
359 XSTR (match_test_exp, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
cbf464bd 360 and_exp = rtx_alloc (AND);
361 XEXP (and_exp, 1) = match_test_exp;
362
363 /* It is always correct to rewrite p->exp as
364
365 (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
366
367 but there are a couple forms where we can do better. If the
368 top-level pattern is an IOR, and one of the two branches does test
369 the mode, we can wrap just the branch that doesn't. Likewise, if
370 we have an IF_THEN_ELSE, and one side of it tests the mode, we can
371 wrap just the side that doesn't. And, of course, we can repeat this
372 descent as many times as it works. */
373
374 pos = &p->exp;
375 for (;;)
376 {
377 rtx subexp = *pos;
e0a4c0c2 378
379 switch (GET_CODE (subexp))
cbf464bd 380 {
868178ed 381 case AND:
382 /* The switch code generation in write_predicate_stmts prefers
383 rtx code tests to be at the top of the expression tree. So
588ce679 384 push this AND down into the second operand of an existing
868178ed 385 AND expression. */
386 if (generate_switch_p (XEXP (subexp, 0)))
387 pos = &XEXP (subexp, 1);
388 goto break_loop;
389
e0a4c0c2 390 case IOR:
391 {
392 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
393 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
48e1416a 394
e0a4c0c2 395 gcc_assert (test0 || test1);
48e1416a 396
e0a4c0c2 397 if (test0 && test1)
398 goto break_loop;
399 pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
400 }
401 break;
48e1416a 402
e0a4c0c2 403 case IF_THEN_ELSE:
404 {
405 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
406 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
407 int test2 = NO_MODE_TEST (XEXP (subexp, 2));
48e1416a 408
e0a4c0c2 409 gcc_assert ((test0 && test1) || test2);
48e1416a 410
e0a4c0c2 411 if (test0 && test1 && test2)
412 goto break_loop;
413 if (test0 && test1)
414 /* Must put it on the dependent clause, not the
415 controlling expression, or we change the meaning of
dac49aa5 416 the test. */
e0a4c0c2 417 pos = &XEXP (subexp, 1);
418 else
419 pos = &XEXP (subexp, 2);
420 }
421 break;
48e1416a 422
e0a4c0c2 423 default:
424 goto break_loop;
cbf464bd 425 }
cbf464bd 426 }
e0a4c0c2 427 break_loop:
cbf464bd 428 XEXP (and_exp, 0) = *pos;
429 *pos = and_exp;
430}
431
6c9ff279 432/* PATH is a string describing a path from the root of an RTL
433 expression to an inner subexpression to be tested. Output
434 code which computes the subexpression from the variable
435 holding the root of the expression. */
436static void
437write_extract_subexp (const char *path)
438{
439 int len = strlen (path);
440 int i;
441
442 /* We first write out the operations (XEXP or XVECEXP) in reverse
443 order, then write "op", then the indices in forward order. */
444 for (i = len - 1; i >= 0; i--)
445 {
446 if (ISLOWER (path[i]))
447 fputs ("XVECEXP (", stdout);
448 else if (ISDIGIT (path[i]))
449 fputs ("XEXP (", stdout);
450 else
026d3868 451 gcc_unreachable ();
6c9ff279 452 }
453
454 fputs ("op", stdout);
455
456 for (i = 0; i < len; i++)
457 {
458 if (ISLOWER (path[i]))
459 printf (", 0, %d)", path[i] - 'a');
460 else if (ISDIGIT (path[i]))
461 printf (", %d)", path[i] - '0');
462 else
463 gcc_unreachable ();
464 }
465}
cbf464bd 466
467/* CODES is a list of RTX codes. Write out an expression which
468 determines whether the operand has one of those codes. */
469static void
6c9ff279 470write_match_code (const char *path, const char *codes)
cbf464bd 471{
472 const char *code;
473
474 while ((code = scan_comma_elt (&codes)) != 0)
475 {
6c9ff279 476 fputs ("GET_CODE (", stdout);
477 write_extract_subexp (path);
478 fputs (") == ", stdout);
cbf464bd 479 while (code < codes)
480 {
481 putchar (TOUPPER (*code));
482 code++;
483 }
48e1416a 484
cbf464bd 485 if (*codes == ',')
486 fputs (" || ", stdout);
487 }
488}
489
490/* EXP is an RTL (sub)expression for a predicate. Recursively
491 descend the expression and write out an equivalent C expression. */
492static void
026d3868 493write_predicate_expr (rtx exp)
cbf464bd 494{
495 switch (GET_CODE (exp))
496 {
497 case AND:
498 putchar ('(');
026d3868 499 write_predicate_expr (XEXP (exp, 0));
cbf464bd 500 fputs (") && (", stdout);
026d3868 501 write_predicate_expr (XEXP (exp, 1));
cbf464bd 502 putchar (')');
503 break;
48e1416a 504
cbf464bd 505 case IOR:
506 putchar ('(');
026d3868 507 write_predicate_expr (XEXP (exp, 0));
cbf464bd 508 fputs (") || (", stdout);
026d3868 509 write_predicate_expr (XEXP (exp, 1));
cbf464bd 510 putchar (')');
511 break;
512
513 case NOT:
514 fputs ("!(", stdout);
026d3868 515 write_predicate_expr (XEXP (exp, 0));
cbf464bd 516 putchar (')');
517 break;
518
519 case IF_THEN_ELSE:
520 putchar ('(');
026d3868 521 write_predicate_expr (XEXP (exp, 0));
cbf464bd 522 fputs (") ? (", stdout);
026d3868 523 write_predicate_expr (XEXP (exp, 1));
cbf464bd 524 fputs (") : (", stdout);
026d3868 525 write_predicate_expr (XEXP (exp, 2));
cbf464bd 526 putchar (')');
527 break;
528
529 case MATCH_OPERAND:
3f980c56 530 if (GET_MODE (exp) == VOIDmode)
531 printf ("%s (op, mode)", XSTR (exp, 1));
532 else
533 printf ("%s (op, %smode)", XSTR (exp, 1), mode_name[GET_MODE (exp)]);
cbf464bd 534 break;
535
536 case MATCH_CODE:
6c9ff279 537 write_match_code (XSTR (exp, 1), XSTR (exp, 0));
cbf464bd 538 break;
539
540 case MATCH_TEST:
e1e9159b 541 rtx_reader_ptr->print_c_condition (XSTR (exp, 0));
cbf464bd 542 break;
543
544 default:
026d3868 545 gcc_unreachable ();
cbf464bd 546 }
547}
548
868178ed 549/* Write the MATCH_CODE expression EXP as a switch statement. */
550
551static void
552write_match_code_switch (rtx exp)
553{
c062b0d1 554 const char *codes = XSTR (exp, 0);
555 const char *path = XSTR (exp, 1);
868178ed 556 const char *code;
557
558 fputs (" switch (GET_CODE (", stdout);
559 write_extract_subexp (path);
560 fputs ("))\n {\n", stdout);
561
562 while ((code = scan_comma_elt (&codes)) != 0)
563 {
564 fputs (" case ", stdout);
565 while (code < codes)
566 {
567 putchar (TOUPPER (*code));
568 code++;
569 }
9af5ce0c 570 fputs (":\n", stdout);
868178ed 571 }
572}
573
588ce679 574/* Given a predicate expression EXP, write out a sequence of stmts
868178ed 575 to evaluate it. This is similar to write_predicate_expr but can
576 generate efficient switch statements. */
577
578static void
579write_predicate_stmts (rtx exp)
580{
581 switch (GET_CODE (exp))
582 {
583 case MATCH_CODE:
584 if (generate_switch_p (exp))
585 {
586 write_match_code_switch (exp);
587 puts (" return true;\n"
588 " default:\n"
589 " break;\n"
590 " }\n"
591 " return false;");
592 return;
593 }
594 break;
595
596 case AND:
597 if (generate_switch_p (XEXP (exp, 0)))
598 {
599 write_match_code_switch (XEXP (exp, 0));
600 puts (" break;\n"
601 " default:\n"
602 " return false;\n"
603 " }");
604 exp = XEXP (exp, 1);
605 }
606 break;
607
608 case IOR:
609 if (generate_switch_p (XEXP (exp, 0)))
610 {
611 write_match_code_switch (XEXP (exp, 0));
612 puts (" return true;\n"
613 " default:\n"
614 " break;\n"
615 " }");
616 exp = XEXP (exp, 1);
617 }
21455936 618 break;
868178ed 619
620 case NOT:
621 if (generate_switch_p (XEXP (exp, 0)))
622 {
623 write_match_code_switch (XEXP (exp, 0));
624 puts (" return false;\n"
625 " default:\n"
626 " break;\n"
627 " }\n"
628 " return true;");
629 return;
630 }
631 break;
632
633 default:
634 break;
635 }
636
9af5ce0c 637 fputs (" return ",stdout);
868178ed 638 write_predicate_expr (exp);
9af5ce0c 639 fputs (";\n", stdout);
868178ed 640}
641
cbf464bd 642/* Given a predicate, write out a complete C function to compute it. */
643static void
644write_one_predicate_function (struct pred_data *p)
645{
646 if (!p->exp)
647 return;
648
649 write_predicate_subfunction (p);
650 add_mode_tests (p);
651
3754d046 652 /* A normal predicate can legitimately not look at machine_mode
e913b5cd 653 if it accepts only CONST_INTs and/or CONST_WIDE_INT and/or CONST_DOUBLEs. */
3754d046 654 printf ("int\n%s (rtx op, machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
cbf464bd 655 p->name);
868178ed 656 write_predicate_stmts (p->exp);
657 fputs ("}\n\n", stdout);
e23a6527 658}
026d3868 659\f
660/* Constraints fall into two categories: register constraints
661 (define_register_constraint), and others (define_constraint,
6b3b345a 662 define_memory_constraint, define_special_memory_constraint,
663 define_address_constraint). We work out automatically which of the
664 various old-style macros they correspond to, and produce
665 appropriate code. They all go in the same hash table so we can
666 verify that there are no duplicate names. */
026d3868 667
668/* All data from one constraint definition. */
669struct constraint_data
670{
671 struct constraint_data *next_this_letter;
672 struct constraint_data *next_textual;
673 const char *name;
674 const char *c_name; /* same as .name unless mangling is necessary */
5d1f1932 675 file_location loc; /* location of definition */
026d3868 676 size_t namelen;
677 const char *regclass; /* for register constraints */
678 rtx exp; /* for other constraints */
dce70584 679 unsigned int is_register : 1;
680 unsigned int is_const_int : 1;
681 unsigned int is_const_dbl : 1;
682 unsigned int is_extra : 1;
683 unsigned int is_memory : 1;
6b3b345a 684 unsigned int is_special_memory: 1;
dce70584 685 unsigned int is_address : 1;
686 unsigned int maybe_allows_reg : 1;
687 unsigned int maybe_allows_mem : 1;
026d3868 688};
689
690/* Overview of all constraints beginning with a given letter. */
691
692static struct constraint_data *
693constraints_by_letter_table[1<<CHAR_BIT];
694
695/* For looking up all the constraints in the order that they appeared
696 in the machine description. */
697static struct constraint_data *first_constraint;
698static struct constraint_data **last_constraint_ptr = &first_constraint;
699
700#define FOR_ALL_CONSTRAINTS(iter_) \
701 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
702
69449463 703/* Contraint letters that have a special meaning and that cannot be used
704 in define*_constraints. */
705static const char generic_constraint_letters[] = "g";
026d3868 706
707/* Machine-independent code expects that constraints with these
708 (initial) letters will allow only (a subset of all) CONST_INTs. */
709
710static const char const_int_constraints[] = "IJKLMNOP";
711
712/* Machine-independent code expects that constraints with these
713 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
714
715static const char const_dbl_constraints[] = "GH";
716
717/* Summary data used to decide whether to output various functions and
718 macro definitions. */
719static unsigned int constraint_max_namelen;
720static bool have_register_constraints;
721static bool have_memory_constraints;
6b3b345a 722static bool have_special_memory_constraints;
026d3868 723static bool have_address_constraints;
724static bool have_extra_constraints;
725static bool have_const_int_constraints;
f0737592 726static unsigned int num_constraints;
727
728static const constraint_data **enum_order;
729static unsigned int register_start, register_end;
730static unsigned int satisfied_start;
4e67d0bf 731static unsigned int const_int_start, const_int_end;
f0737592 732static unsigned int memory_start, memory_end;
6b3b345a 733static unsigned int special_memory_start, special_memory_end;
f0737592 734static unsigned int address_start, address_end;
dce70584 735static unsigned int maybe_allows_none_start, maybe_allows_none_end;
736static unsigned int maybe_allows_reg_start, maybe_allows_reg_end;
737static unsigned int maybe_allows_mem_start, maybe_allows_mem_end;
026d3868 738
739/* Convert NAME, which contains angle brackets and/or underscores, to
740 a string that can be used as part of a C identifier. The string
741 comes from the rtl_obstack. */
742static const char *
743mangle (const char *name)
744{
745 for (; *name; name++)
746 switch (*name)
747 {
748 case '_': obstack_grow (rtl_obstack, "__", 2); break;
749 case '<': obstack_grow (rtl_obstack, "_l", 2); break;
750 case '>': obstack_grow (rtl_obstack, "_g", 2); break;
751 default: obstack_1grow (rtl_obstack, *name); break;
752 }
753
754 obstack_1grow (rtl_obstack, '\0');
65b198c2 755 return XOBFINISH (rtl_obstack, const char *);
026d3868 756}
757
758/* Add one constraint, of any sort, to the tables. NAME is its name;
759 REGCLASS is the register class, if any; EXP is the expression to
6b3b345a 760 test, if any; IS_MEMORY, IS_SPECIAL_MEMORY and IS_ADDRESS indicate
761 memory, special memory, and address constraints, respectively; LOC
762 is the .md file location.
5d1f1932 763
6b3b345a 764 Not all combinations of arguments are valid; most importantly,
765 REGCLASS is mutually exclusive with EXP, and
766 IS_MEMORY/IS_SPECIAL_MEMORY/IS_ADDRESS are only meaningful for
767 constraints with EXP.
026d3868 768
769 This function enforces all syntactic and semantic rules about what
770 constraints can be defined. */
771
772static void
773add_constraint (const char *name, const char *regclass,
6b3b345a 774 rtx exp, bool is_memory, bool is_special_memory,
775 bool is_address, file_location loc)
026d3868 776{
777 struct constraint_data *c, **iter, **slot;
778 const char *p;
779 bool need_mangled_name = false;
780 bool is_const_int;
781 bool is_const_dbl;
782 size_t namelen;
783
69449463 784 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
785 name = general_mem;
786
5d1f1932 787 if (exp && validate_exp (exp, name, loc))
026d3868 788 return;
789
026d3868 790 for (p = name; *p; p++)
791 if (!ISALNUM (*p))
792 {
793 if (*p == '<' || *p == '>' || *p == '_')
794 need_mangled_name = true;
795 else
796 {
5d1f1932 797 error_at (loc, "constraint name '%s' must be composed of letters,"
798 " digits, underscores, and angle brackets", name);
026d3868 799 return;
800 }
801 }
802
803 if (strchr (generic_constraint_letters, name[0]))
804 {
805 if (name[1] == '\0')
5d1f1932 806 error_at (loc, "constraint letter '%s' cannot be "
807 "redefined by the machine description", name);
026d3868 808 else
5d1f1932 809 error_at (loc, "constraint name '%s' cannot be defined by the machine"
810 " description, as it begins with '%c'", name, name[0]);
026d3868 811 return;
812 }
813
48e1416a 814
026d3868 815 namelen = strlen (name);
816 slot = &constraints_by_letter_table[(unsigned int)name[0]];
817 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
818 {
819 /* This causes slot to end up pointing to the
820 next_this_letter field of the last constraint with a name
821 of equal or greater length than the new constraint; hence
822 the new constraint will be inserted after all previous
823 constraints with names of the same length. */
824 if ((*iter)->namelen >= namelen)
825 slot = iter;
826
827 if (!strcmp ((*iter)->name, name))
828 {
5d1f1932 829 error_at (loc, "redefinition of constraint '%s'", name);
830 message_at ((*iter)->loc, "previous definition is here");
026d3868 831 return;
832 }
833 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
834 {
5d1f1932 835 error_at (loc, "defining constraint '%s' here", name);
836 message_at ((*iter)->loc, "renders constraint '%s' "
837 "(defined here) a prefix", (*iter)->name);
026d3868 838 return;
839 }
840 else if (!strncmp ((*iter)->name, name, namelen))
841 {
5d1f1932 842 error_at (loc, "constraint '%s' is a prefix", name);
843 message_at ((*iter)->loc, "of constraint '%s' (defined here)",
844 (*iter)->name);
026d3868 845 return;
846 }
847 }
848
849 is_const_int = strchr (const_int_constraints, name[0]) != 0;
850 is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
851
852 if (is_const_int || is_const_dbl)
853 {
854 enum rtx_code appropriate_code
855 = is_const_int ? CONST_INT : CONST_DOUBLE;
08f4222b 856
026d3868 857 /* Consider relaxing this requirement in the future. */
858 if (regclass
859 || GET_CODE (exp) != AND
860 || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
861 || strcmp (XSTR (XEXP (exp, 0), 0),
862 GET_RTX_NAME (appropriate_code)))
863 {
864 if (name[1] == '\0')
5d1f1932 865 error_at (loc, "constraint letter '%c' is reserved "
866 "for %s constraints", name[0],
867 GET_RTX_NAME (appropriate_code));
026d3868 868 else
5d1f1932 869 error_at (loc, "constraint names beginning with '%c' "
870 "(%s) are reserved for %s constraints",
871 name[0], name, GET_RTX_NAME (appropriate_code));
026d3868 872 return;
873 }
874
875 if (is_memory)
876 {
877 if (name[1] == '\0')
5d1f1932 878 error_at (loc, "constraint letter '%c' cannot be a "
879 "memory constraint", name[0]);
026d3868 880 else
5d1f1932 881 error_at (loc, "constraint name '%s' begins with '%c', "
882 "and therefore cannot be a memory constraint",
883 name, name[0]);
026d3868 884 return;
885 }
6b3b345a 886 else if (is_special_memory)
887 {
888 if (name[1] == '\0')
889 error_at (loc, "constraint letter '%c' cannot be a "
890 "special memory constraint", name[0]);
891 else
892 error_at (loc, "constraint name '%s' begins with '%c', "
893 "and therefore cannot be a special memory constraint",
894 name, name[0]);
895 return;
896 }
026d3868 897 else if (is_address)
898 {
899 if (name[1] == '\0')
5d1f1932 900 error_at (loc, "constraint letter '%c' cannot be an "
901 "address constraint", name[0]);
026d3868 902 else
5d1f1932 903 error_at (loc, "constraint name '%s' begins with '%c', "
904 "and therefore cannot be an address constraint",
905 name, name[0]);
026d3868 906 return;
907 }
026d3868 908 }
909
48e1416a 910
65b198c2 911 c = XOBNEW (rtl_obstack, struct constraint_data);
026d3868 912 c->name = name;
913 c->c_name = need_mangled_name ? mangle (name) : name;
5d1f1932 914 c->loc = loc;
026d3868 915 c->namelen = namelen;
916 c->regclass = regclass;
917 c->exp = exp;
918 c->is_register = regclass != 0;
919 c->is_const_int = is_const_int;
920 c->is_const_dbl = is_const_dbl;
921 c->is_extra = !(regclass || is_const_int || is_const_dbl);
922 c->is_memory = is_memory;
6b3b345a 923 c->is_special_memory = is_special_memory;
026d3868 924 c->is_address = is_address;
d5e47a46 925 c->maybe_allows_reg = true;
926 c->maybe_allows_mem = true;
dce70584 927 if (exp)
d5e47a46 928 {
929 char codes[NUM_RTX_CODE];
5d1f1932 930 compute_test_codes (exp, loc, codes);
d5e47a46 931 if (!codes[REG] && !codes[SUBREG])
932 c->maybe_allows_reg = false;
933 if (!codes[MEM])
934 c->maybe_allows_mem = false;
935 }
026d3868 936 c->next_this_letter = *slot;
937 *slot = c;
938
939 /* Insert this constraint in the list of all constraints in textual
940 order. */
941 c->next_textual = 0;
942 *last_constraint_ptr = c;
943 last_constraint_ptr = &c->next_textual;
944
945 constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
946 have_register_constraints |= c->is_register;
947 have_const_int_constraints |= c->is_const_int;
026d3868 948 have_extra_constraints |= c->is_extra;
949 have_memory_constraints |= c->is_memory;
6b3b345a 950 have_special_memory_constraints |= c->is_special_memory;
026d3868 951 have_address_constraints |= c->is_address;
f0737592 952 num_constraints += 1;
026d3868 953}
954
6b3b345a 955/* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT,
956 DEFINE_SPECIAL_MEMORY_CONSTRAINT, or DEFINE_ADDRESS_CONSTRAINT
957 expression, C. */
026d3868 958static void
c04601c1 959process_define_constraint (md_rtx_info *info)
026d3868 960{
c04601c1 961 add_constraint (XSTR (info->def, 0), 0, XEXP (info->def, 2),
962 GET_CODE (info->def) == DEFINE_MEMORY_CONSTRAINT,
6b3b345a 963 GET_CODE (info->def) == DEFINE_SPECIAL_MEMORY_CONSTRAINT,
c04601c1 964 GET_CODE (info->def) == DEFINE_ADDRESS_CONSTRAINT,
965 info->loc);
026d3868 966}
967
968/* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
969static void
c04601c1 970process_define_register_constraint (md_rtx_info *info)
026d3868 971{
c04601c1 972 add_constraint (XSTR (info->def, 0), XSTR (info->def, 1),
6b3b345a 973 0, false, false, false, info->loc);
026d3868 974}
975
f0737592 976/* Put the constraints into enum order. We want to keep constraints
977 of the same type together so that query functions can be simple
978 range checks. */
979static void
980choose_enum_order (void)
981{
982 struct constraint_data *c;
983
984 enum_order = XNEWVEC (const constraint_data *, num_constraints);
985 unsigned int next = 0;
986
987 register_start = next;
988 FOR_ALL_CONSTRAINTS (c)
989 if (c->is_register)
990 enum_order[next++] = c;
991 register_end = next;
992
993 satisfied_start = next;
994
4e67d0bf 995 const_int_start = next;
996 FOR_ALL_CONSTRAINTS (c)
997 if (c->is_const_int)
998 enum_order[next++] = c;
999 const_int_end = next;
1000
f0737592 1001 memory_start = next;
1002 FOR_ALL_CONSTRAINTS (c)
1003 if (c->is_memory)
1004 enum_order[next++] = c;
1005 memory_end = next;
1006
6b3b345a 1007 special_memory_start = next;
1008 FOR_ALL_CONSTRAINTS (c)
1009 if (c->is_special_memory)
1010 enum_order[next++] = c;
1011 special_memory_end = next;
1012
f0737592 1013 address_start = next;
1014 FOR_ALL_CONSTRAINTS (c)
1015 if (c->is_address)
1016 enum_order[next++] = c;
1017 address_end = next;
1018
dce70584 1019 maybe_allows_none_start = next;
1020 FOR_ALL_CONSTRAINTS (c)
6b3b345a 1021 if (!c->is_register && !c->is_const_int && !c->is_memory
1022 && !c->is_special_memory && !c->is_address
dce70584 1023 && !c->maybe_allows_reg && !c->maybe_allows_mem)
1024 enum_order[next++] = c;
1025 maybe_allows_none_end = next;
1026
1027 maybe_allows_reg_start = next;
1028 FOR_ALL_CONSTRAINTS (c)
6b3b345a 1029 if (!c->is_register && !c->is_const_int && !c->is_memory
1030 && !c->is_special_memory && !c->is_address
dce70584 1031 && c->maybe_allows_reg && !c->maybe_allows_mem)
1032 enum_order[next++] = c;
1033 maybe_allows_reg_end = next;
1034
1035 maybe_allows_mem_start = next;
1036 FOR_ALL_CONSTRAINTS (c)
6b3b345a 1037 if (!c->is_register && !c->is_const_int && !c->is_memory
1038 && !c->is_special_memory && !c->is_address
dce70584 1039 && !c->maybe_allows_reg && c->maybe_allows_mem)
1040 enum_order[next++] = c;
1041 maybe_allows_mem_end = next;
1042
f0737592 1043 FOR_ALL_CONSTRAINTS (c)
6b3b345a 1044 if (!c->is_register && !c->is_const_int && !c->is_memory
1045 && !c->is_special_memory && !c->is_address
dce70584 1046 && c->maybe_allows_reg && c->maybe_allows_mem)
f0737592 1047 enum_order[next++] = c;
1048 gcc_assert (next == num_constraints);
1049}
1050
026d3868 1051/* Write out an enumeration with one entry per machine-specific
1052 constraint. */
1053static void
1054write_enum_constraint_num (void)
1055{
abcb15b5 1056 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
026d3868 1057 fputs ("enum constraint_num\n"
1058 "{\n"
1059 " CONSTRAINT__UNKNOWN = 0", stdout);
f0737592 1060 for (unsigned int i = 0; i < num_constraints; ++i)
1061 printf (",\n CONSTRAINT_%s", enum_order[i]->c_name);
14792f4e 1062 puts (",\n CONSTRAINT__LIMIT\n};\n");
026d3868 1063}
1064
1065/* Write out a function which looks at a string and determines what
1066 constraint name, if any, it begins with. */
1067static void
76d4a13c 1068write_lookup_constraint_1 (void)
026d3868 1069{
1070 unsigned int i;
1071 puts ("enum constraint_num\n"
76d4a13c 1072 "lookup_constraint_1 (const char *str)\n"
026d3868 1073 "{\n"
1074 " switch (str[0])\n"
1075 " {");
1076
9af5ce0c 1077 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
026d3868 1078 {
1079 struct constraint_data *c = constraints_by_letter_table[i];
1080 if (!c)
1081 continue;
1082
1083 printf (" case '%c':\n", i);
1084 if (c->namelen == 1)
1085 printf (" return CONSTRAINT_%s;\n", c->c_name);
1086 else
1087 {
1088 do
1089 {
0b588b3d 1090 printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
026d3868 1091 " return CONSTRAINT_%s;\n",
0b588b3d 1092 c->name + 1, (unsigned long int) c->namelen - 1,
1093 c->c_name);
026d3868 1094 c = c->next_this_letter;
1095 }
1096 while (c);
1097 puts (" break;");
1098 }
1099 }
1100
1101 puts (" default: break;\n"
1102 " }\n"
1103 " return CONSTRAINT__UNKNOWN;\n"
1104 "}\n");
1105}
1106
76d4a13c 1107/* Write out an array that maps single-letter characters to their
1108 constraints (if that fits in a character) or 255 if lookup_constraint_1
1109 must be called. */
1110static void
1111write_lookup_constraint_array (void)
1112{
1113 unsigned int i;
1114 printf ("const unsigned char lookup_constraint_array[] = {\n ");
1115 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1116 {
1117 if (i != 0)
1118 printf (",\n ");
1119 struct constraint_data *c = constraints_by_letter_table[i];
1120 if (!c)
1121 printf ("CONSTRAINT__UNKNOWN");
1122 else if (c->namelen == 1)
1123 printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1124 else
1125 printf ("UCHAR_MAX");
1126 }
1127 printf ("\n};\n\n");
1128}
1129
b0ecf094 1130/* Write out a function which looks at a string and determines what
1131 the constraint name length is. */
026d3868 1132static void
1133write_insn_constraint_len (void)
1134{
b0ecf094 1135 unsigned int i;
026d3868 1136
b0ecf094 1137 puts ("static inline size_t\n"
1138 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
026d3868 1139 "{\n"
b0ecf094 1140 " switch (fc)\n"
026d3868 1141 " {");
1142
9af5ce0c 1143 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
b0ecf094 1144 {
1145 struct constraint_data *c = constraints_by_letter_table[i];
1146
1147 if (!c
1148 || c->namelen == 1)
1149 continue;
1150
1151 /* Constraints with multiple characters should have the same
1152 length. */
1153 {
1154 struct constraint_data *c2 = c->next_this_letter;
1155 size_t len = c->namelen;
1156 while (c2)
1157 {
1158 if (c2->namelen != len)
1159 error ("Multi-letter constraints with first letter '%c' "
1160 "should have same length", i);
1161 c2 = c2->next_this_letter;
1162 }
1163 }
1164
1165 printf (" case '%c': return %lu;\n",
1166 i, (unsigned long int) c->namelen);
1167 }
026d3868 1168
1169 puts (" default: break;\n"
1170 " }\n"
1171 " return 1;\n"
1172 "}\n");
1173}
48e1416a 1174
026d3868 1175/* Write out the function which computes the register class corresponding
1176 to a register constraint. */
1177static void
f0737592 1178write_reg_class_for_constraint_1 (void)
026d3868 1179{
1180 struct constraint_data *c;
1181
1182 puts ("enum reg_class\n"
f0737592 1183 "reg_class_for_constraint_1 (enum constraint_num c)\n"
026d3868 1184 "{\n"
1185 " switch (c)\n"
1186 " {");
1187
1188 FOR_ALL_CONSTRAINTS (c)
1189 if (c->is_register)
1190 printf (" case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1191
1192 puts (" default: break;\n"
1193 " }\n"
1194 " return NO_REGS;\n"
1195 "}\n");
1196}
1197
1198/* Write out the functions which compute whether a given value matches
1199 a given non-register constraint. */
1200static void
a014ec0f 1201write_tm_constrs_h (void)
026d3868 1202{
1203 struct constraint_data *c;
a014ec0f 1204
1205 printf ("\
1206/* Generated automatically by the program '%s'\n\
ec468049 1207 from the machine description file '%s'. */\n\n", progname,
d3ab2953 1208 md_reader_ptr->get_top_level_filename ());
a014ec0f 1209
1210 puts ("\
1211#ifndef GCC_TM_CONSTRS_H\n\
1212#define GCC_TM_CONSTRS_H\n");
026d3868 1213
026d3868 1214 FOR_ALL_CONSTRAINTS (c)
1215 if (!c->is_register)
1216 {
1217 bool needs_ival = needs_variable (c->exp, "ival");
1218 bool needs_hval = needs_variable (c->exp, "hval");
1219 bool needs_lval = needs_variable (c->exp, "lval");
1220 bool needs_rval = needs_variable (c->exp, "rval");
1221 bool needs_mode = (needs_variable (c->exp, "mode")
1222 || needs_hval || needs_lval || needs_rval);
5e156a9d 1223 bool needs_op = (needs_variable (c->exp, "op")
1224 || needs_ival || needs_mode);
026d3868 1225
1226 printf ("static inline bool\n"
5e156a9d 1227 "satisfies_constraint_%s (rtx %s)\n"
1228 "{\n", c->c_name,
1229 needs_op ? "op" : "ARG_UNUSED (op)");
026d3868 1230 if (needs_mode)
3754d046 1231 puts (" machine_mode mode = GET_MODE (op);");
026d3868 1232 if (needs_ival)
1233 puts (" HOST_WIDE_INT ival = 0;");
1234 if (needs_hval)
1235 puts (" HOST_WIDE_INT hval = 0;");
1236 if (needs_lval)
1237 puts (" unsigned HOST_WIDE_INT lval = 0;");
1238 if (needs_rval)
1239 puts (" const REAL_VALUE_TYPE *rval = 0;");
1240
1241 if (needs_ival)
971ba038 1242 puts (" if (CONST_INT_P (op))\n"
026d3868 1243 " ival = INTVAL (op);");
e913b5cd 1244#if TARGET_SUPPORTS_WIDE_INT
1245 if (needs_lval || needs_hval)
1246 error ("you can't use lval or hval");
1247#else
026d3868 1248 if (needs_hval)
1249 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1250 " hval = CONST_DOUBLE_HIGH (op);");
1251 if (needs_lval)
1252 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1253 " lval = CONST_DOUBLE_LOW (op);");
e913b5cd 1254#endif
026d3868 1255 if (needs_rval)
1256 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1257 " rval = CONST_DOUBLE_REAL_VALUE (op);");
868178ed 1258
1259 write_predicate_stmts (c->exp);
1260 fputs ("}\n", stdout);
026d3868 1261 }
a014ec0f 1262 puts ("#endif /* tm-constrs.h */");
026d3868 1263}
1264
1265/* Write out the wrapper function, constraint_satisfied_p, that maps
1266 a CONSTRAINT_xxx constant to one of the predicate functions generated
1267 above. */
1268static void
ad5d2f05 1269write_constraint_satisfied_p_array (void)
026d3868 1270{
f0737592 1271 if (satisfied_start == num_constraints)
1272 return;
1273
ad5d2f05 1274 printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n ");
1275 for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1276 {
1277 if (i != satisfied_start)
1278 printf (",\n ");
1279 printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1280 }
1281 printf ("\n};\n\n");
026d3868 1282}
1283
1284/* Write out the function which computes whether a given value matches
1285 a given CONST_INT constraint. This doesn't just forward to
1286 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1287static void
1288write_insn_const_int_ok_for_constraint (void)
1289{
1290 struct constraint_data *c;
1291
1292 puts ("bool\n"
1293 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1294 "enum constraint_num c)\n"
1295 "{\n"
1296 " switch (c)\n"
1297 " {");
1298
1299 FOR_ALL_CONSTRAINTS (c)
1300 if (c->is_const_int)
1301 {
1302 printf (" case CONSTRAINT_%s:\n return ", c->c_name);
5e156a9d 1303 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1304 we know at this point that we have a const_int, so we need not
1305 bother with that part of the test. */
1306 write_predicate_expr (XEXP (c->exp, 1));
026d3868 1307 fputs (";\n\n", stdout);
1308 }
1309
1310 puts (" default: break;\n"
1311 " }\n"
1312 " return false;\n"
1313 "}\n");
1314}
f0737592 1315\f
1316/* Write a definition for a function NAME that returns true if a given
1317 constraint_num is in the range [START, END). */
026d3868 1318static void
f0737592 1319write_range_function (const char *name, unsigned int start, unsigned int end)
026d3868 1320{
f0737592 1321 printf ("static inline bool\n");
1322 if (start != end)
1323 printf ("%s (enum constraint_num c)\n"
1324 "{\n"
1325 " return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1326 "}\n\n",
1327 name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1328 else
1329 printf ("%s (enum constraint_num)\n"
1330 "{\n"
1331 " return false;\n"
1332 "}\n\n", name);
026d3868 1333}
1334
dce70584 1335/* Write a definition for insn_extra_constraint_allows_reg_mem function. */
1336static void
1337write_allows_reg_mem_function (void)
1338{
1339 printf ("static inline void\n"
1340 "insn_extra_constraint_allows_reg_mem (enum constraint_num c,\n"
1341 "\t\t\t\t bool *allows_reg, bool *allows_mem)\n"
1342 "{\n");
1343 if (maybe_allows_none_start != maybe_allows_none_end)
1344 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1345 " return;\n",
1346 enum_order[maybe_allows_none_start]->c_name,
1347 enum_order[maybe_allows_none_end - 1]->c_name);
1348 if (maybe_allows_reg_start != maybe_allows_reg_end)
1349 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1350 " {\n"
1351 " *allows_reg = true;\n"
1352 " return;\n"
1353 " }\n",
1354 enum_order[maybe_allows_reg_start]->c_name,
1355 enum_order[maybe_allows_reg_end - 1]->c_name);
1356 if (maybe_allows_mem_start != maybe_allows_mem_end)
1357 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1358 " {\n"
1359 " *allows_mem = true;\n"
1360 " return;\n"
1361 " }\n",
1362 enum_order[maybe_allows_mem_start]->c_name,
1363 enum_order[maybe_allows_mem_end - 1]->c_name);
1364 printf (" (void) c;\n"
1365 " *allows_reg = true;\n"
1366 " *allows_mem = true;\n"
1367 "}\n\n");
1368}
1369
79bc09fb 1370/* VEC is a list of key/value pairs, with the keys being lower bounds
1371 of a range. Output a decision tree that handles the keys covered by
1372 [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1373 INDENT is the number of spaces to indent the code. */
1374static void
1375print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1376 unsigned int start, unsigned int end, const char *fallback,
1377 unsigned int indent)
1378{
1379 while (start < end)
1380 {
1381 unsigned int mid = (start + end) / 2;
1382 printf ("%*sif (c >= CONSTRAINT_%s)\n",
1383 indent, "", enum_order[vec[mid].first]->c_name);
1384 if (mid + 1 == end)
1385 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1386 else
1387 {
1388 printf ("%*s{\n", indent + 2, "");
1389 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1390 printf ("%*s}\n", indent + 2, "");
1391 }
1392 end = mid;
1393 }
1394 printf ("%*sreturn %s;\n", indent, "", fallback);
1395}
1396
026d3868 1397/* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1398 an enumeration in portable C, so we have to condition all these
1399 prototypes on HAVE_MACHINE_MODES. */
1400static void
1401write_tm_preds_h (void)
1402{
1403 struct pred_data *p;
1404
1405 printf ("\
1406/* Generated automatically by the program '%s'\n\
ec468049 1407 from the machine description file '%s'. */\n\n", progname,
d3ab2953 1408 md_reader_ptr->get_top_level_filename ());
026d3868 1409
1410 puts ("\
1411#ifndef GCC_TM_PREDS_H\n\
1412#define GCC_TM_PREDS_H\n\
1413\n\
1414#ifdef HAVE_MACHINE_MODES");
1415
1416 FOR_ALL_PREDICATES (p)
3754d046 1417 printf ("extern int %s (rtx, machine_mode);\n", p->name);
026d3868 1418
1419 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1420
1421 if (constraint_max_namelen > 0)
1422 {
1423 write_enum_constraint_num ();
76d4a13c 1424 puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1425 "extern const unsigned char lookup_constraint_array[];\n"
1426 "\n"
1427 "/* Return the constraint at the beginning of P, or"
1428 " CONSTRAINT__UNKNOWN if it\n"
1429 " isn't recognized. */\n"
1430 "\n"
1431 "static inline enum constraint_num\n"
1432 "lookup_constraint (const char *p)\n"
1433 "{\n"
1434 " unsigned int index = lookup_constraint_array"
1435 "[(unsigned char) *p];\n"
1436 " return (index == UCHAR_MAX\n"
1437 " ? lookup_constraint_1 (p)\n"
1438 " : (enum constraint_num) index);\n"
1439 "}\n");
f0737592 1440 if (satisfied_start == num_constraints)
1441 puts ("/* Return true if X satisfies constraint C. */\n"
1442 "\n"
1443 "static inline bool\n"
1444 "constraint_satisfied_p (rtx, enum constraint_num)\n"
1445 "{\n"
1446 " return false;\n"
1447 "}\n");
1448 else
ad5d2f05 1449 printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
f0737592 1450 "\n"
1451 "/* Return true if X satisfies constraint C. */\n"
1452 "\n"
1453 "static inline bool\n"
1454 "constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1455 "{\n"
ad5d2f05 1456 " int i = (int) c - (int) CONSTRAINT_%s;\n"
1457 " return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
f0737592 1458 "}\n"
1459 "\n",
1460 enum_order[satisfied_start]->name);
1461
1462 write_range_function ("insn_extra_register_constraint",
1463 register_start, register_end);
1464 write_range_function ("insn_extra_memory_constraint",
1465 memory_start, memory_end);
6b3b345a 1466 write_range_function ("insn_extra_special_memory_constraint",
1467 special_memory_start, special_memory_end);
f0737592 1468 write_range_function ("insn_extra_address_constraint",
1469 address_start, address_end);
dce70584 1470 write_allows_reg_mem_function ();
026d3868 1471
1472 if (constraint_max_namelen > 1)
b0ecf094 1473 {
1474 write_insn_constraint_len ();
1475 puts ("#define CONSTRAINT_LEN(c_,s_) "
1476 "insn_constraint_len (c_,s_)\n");
1477 }
026d3868 1478 else
1479 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1480 if (have_register_constraints)
f0737592 1481 puts ("extern enum reg_class reg_class_for_constraint_1 "
026d3868 1482 "(enum constraint_num);\n"
f0737592 1483 "\n"
1484 "static inline enum reg_class\n"
1485 "reg_class_for_constraint (enum constraint_num c)\n"
1486 "{\n"
1487 " if (insn_extra_register_constraint (c))\n"
1488 " return reg_class_for_constraint_1 (c);\n"
1489 " return NO_REGS;\n"
79bc09fb 1490 "}\n");
026d3868 1491 else
f0737592 1492 puts ("static inline enum reg_class\n"
1493 "reg_class_for_constraint (enum constraint_num)\n"
1494 "{\n"
1495 " return NO_REGS;\n"
79bc09fb 1496 "}\n");
026d3868 1497 if (have_const_int_constraints)
1498 puts ("extern bool insn_const_int_ok_for_constraint "
1499 "(HOST_WIDE_INT, enum constraint_num);\n"
1500 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1501 " insn_const_int_ok_for_constraint (v_, "
1502 "lookup_constraint (s_))\n");
4e67d0bf 1503 else
1504 puts ("static inline bool\n"
1505 "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1506 " enum constraint_num)\n"
1507 "{\n"
1508 " return false;\n"
1509 "}\n");
79bc09fb 1510
1511 puts ("enum constraint_type\n"
1512 "{\n"
1513 " CT_REGISTER,\n"
4e67d0bf 1514 " CT_CONST_INT,\n"
79bc09fb 1515 " CT_MEMORY,\n"
6b3b345a 1516 " CT_SPECIAL_MEMORY,\n"
79bc09fb 1517 " CT_ADDRESS,\n"
1518 " CT_FIXED_FORM\n"
1519 "};\n"
1520 "\n"
1521 "static inline enum constraint_type\n"
1522 "get_constraint_type (enum constraint_num c)\n"
1523 "{");
4e67d0bf 1524 auto_vec <std::pair <unsigned int, const char *>, 4> values;
1525 if (const_int_start != const_int_end)
1526 values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
79bc09fb 1527 if (memory_start != memory_end)
1528 values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
6b3b345a 1529 if (special_memory_start != special_memory_end)
1530 values.safe_push (std::make_pair (special_memory_start, "CT_SPECIAL_MEMORY"));
79bc09fb 1531 if (address_start != address_end)
1532 values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1533 if (address_end != num_constraints)
1534 values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1535 print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1536 puts ("}");
026d3868 1537 }
1538
1539 puts ("#endif /* tm-preds.h */");
1540}
e23a6527 1541
48e1416a 1542/* Write insn-preds.c.
cbf464bd 1543 N.B. the list of headers to include was copied from genrecog; it
1544 may not be ideal.
1545
1546 FUTURE: Write #line markers referring back to the machine
1547 description. (Can't practically do this now since we don't know
1548 the line number of the C block - just the line number of the enclosing
1549 expression.) */
1550static void
1551write_insn_preds_c (void)
1552{
1553 struct pred_data *p;
1554
1555 printf ("\
1556/* Generated automatically by the program '%s'\n\
ec468049 1557 from the machine description file '%s'. */\n\n", progname,
d3ab2953 1558 md_reader_ptr->get_top_level_filename ());
cbf464bd 1559
1560 puts ("\
785790dc 1561#define IN_TARGET_CODE 1\n\
cbf464bd 1562#include \"config.h\"\n\
1563#include \"system.h\"\n\
1564#include \"coretypes.h\"\n\
9ef16211 1565#include \"backend.h\"\n\
d040a5b0 1566#include \"predict.h\"\n\
9ef16211 1567#include \"tree.h\"\n\
cbf464bd 1568#include \"rtl.h\"\n\
b20a8bb4 1569#include \"alias.h\"\n\
9ed99284 1570#include \"varasm.h\"\n\
1571#include \"stor-layout.h\"\n\
1572#include \"calls.h\"\n\
ad7b10a2 1573#include \"memmodel.h\"\n\
cbf464bd 1574#include \"tm_p.h\"\n\
cbf464bd 1575#include \"insn-config.h\"\n\
1576#include \"recog.h\"\n\
cbf464bd 1577#include \"output.h\"\n\
1578#include \"flags.h\"\n\
9ef16211 1579#include \"df.h\"\n\
cbf464bd 1580#include \"resource.h\"\n\
c18cb818 1581#include \"diagnostic-core.h\"\n\
78c3d863 1582#include \"reload.h\"\n\
a014ec0f 1583#include \"regs.h\"\n\
73bde8df 1584#include \"emit-rtl.h\"\n\
b395382f 1585#include \"tm-constrs.h\"\n\
1586#include \"target.h\"\n");
cbf464bd 1587
1588 FOR_ALL_PREDICATES (p)
1589 write_one_predicate_function (p);
026d3868 1590
1591 if (constraint_max_namelen > 0)
1592 {
76d4a13c 1593 write_lookup_constraint_1 ();
1594 write_lookup_constraint_array ();
ed5217e6 1595 if (have_register_constraints)
f0737592 1596 write_reg_class_for_constraint_1 ();
ad5d2f05 1597 write_constraint_satisfied_p_array ();
48e1416a 1598
026d3868 1599 if (have_const_int_constraints)
1600 write_insn_const_int_ok_for_constraint ();
026d3868 1601 }
cbf464bd 1602}
1603
1604/* Argument parsing. */
1605static bool gen_header;
a014ec0f 1606static bool gen_constrs;
1607
cbf464bd 1608static bool
1609parse_option (const char *opt)
1610{
1611 if (!strcmp (opt, "-h"))
1612 {
1613 gen_header = true;
1614 return 1;
1615 }
a014ec0f 1616 else if (!strcmp (opt, "-c"))
1617 {
1618 gen_constrs = true;
1619 return 1;
1620 }
cbf464bd 1621 else
1622 return 0;
1623}
1624
1625/* Master control. */
e23a6527 1626int
16570c04 1627main (int argc, const char **argv)
e23a6527 1628{
cbf464bd 1629 progname = argv[0];
1630 if (argc <= 1)
1631 fatal ("no input file name");
77ba95d0 1632 if (!init_rtx_reader_args_cb (argc, argv, parse_option))
cbf464bd 1633 return FATAL_EXIT_CODE;
1634
c04601c1 1635 md_rtx_info info;
1636 while (read_md_rtx (&info))
1637 switch (GET_CODE (info.def))
026d3868 1638 {
1639 case DEFINE_PREDICATE:
1640 case DEFINE_SPECIAL_PREDICATE:
c04601c1 1641 process_define_predicate (&info);
026d3868 1642 break;
1643
1644 case DEFINE_CONSTRAINT:
1645 case DEFINE_MEMORY_CONSTRAINT:
6b3b345a 1646 case DEFINE_SPECIAL_MEMORY_CONSTRAINT:
026d3868 1647 case DEFINE_ADDRESS_CONSTRAINT:
c04601c1 1648 process_define_constraint (&info);
026d3868 1649 break;
1650
1651 case DEFINE_REGISTER_CONSTRAINT:
c04601c1 1652 process_define_register_constraint (&info);
026d3868 1653 break;
1654
1655 default:
1656 break;
1657 }
e23a6527 1658
f0737592 1659 choose_enum_order ();
1660
cbf464bd 1661 if (gen_header)
1662 write_tm_preds_h ();
a014ec0f 1663 else if (gen_constrs)
1664 write_tm_constrs_h ();
cbf464bd 1665 else
1666 write_insn_preds_c ();
e23a6527 1667
cbf464bd 1668 if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
e23a6527 1669 return FATAL_EXIT_CODE;
1670
1671 return SUCCESS_EXIT_CODE;
1672}