Debian Codegraph @

gcc/genpreds.cc

raw
1 /* Generate from machine description:
2 - prototype declarations for operand predicates (tm-preds.h)
3 - function definitions of operand predicates, if defined new-style
4 (insn-preds.cc)
5 Copyright (C) 2001-2025 Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "obstack.h"
30 #include "read-md.h"
31 #include "gensupport.h"
32
33 static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
34
35 /* Given a predicate expression EXP, from form NAME at location LOC,
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. */
39 static bool
40 validate_exp (rtx exp, const char *name, file_location loc)
41 {
42 if (exp == 0)
43 {
44 message_at (loc, "%s: must give a predicate expression", name);
45 return true;
46 }
47
48 switch (GET_CODE (exp))
49 {
50 /* Ternary, binary, unary expressions: recurse into subexpressions. */
51 case IF_THEN_ELSE:
52 if (validate_exp (XEXP (exp, 2), name, loc))
53 return true;
54 /* fall through */
55 case AND:
56 case IOR:
57 if (validate_exp (XEXP (exp, 1), name, loc))
58 return true;
59 /* fall through */
60 case NOT:
61 return validate_exp (XEXP (exp, 0), name, loc);
62
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 {
71 error_at (loc, "%s: invalid character in path "
72 "string '%s'", name, XSTR (exp, 1));
73 return true;
74 }
75 }
76 }
77 gcc_fallthrough ();
78
79 /* These need no special checking. */
80 case MATCH_OPERAND:
81 case MATCH_TEST:
82 return false;
83
84 default:
85 error_at (loc, "%s: cannot use '%s' in a predicate expression",
86 name, GET_RTX_NAME (GET_CODE (exp)));
87 return true;
88 }
89 }
90
91 /* Predicates are defined with (define_predicate) or
92 (define_special_predicate) expressions in the machine description. */
93 static void
94 process_define_predicate (md_rtx_info *info)
95 {
96 validate_exp (XEXP (info->def, 1), XSTR (info->def, 0), info->loc);
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
113 static inline bool basereg_operand_1(rtx op, machine_mode mode)
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
125 an RTL template, so we have to handle "" strings. */
126
127
128 static void
129 write_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)");
141 match_test_str = XOBFINISH (rtl_obstack, const char *);
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 bool\n"
155 "%s_1 (rtx op ATTRIBUTE_UNUSED, machine_mode mode ATTRIBUTE_UNUSED)\n",
156 p->name);
157 rtx_reader_ptr->print_md_ptr_loc (p->c_block);
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
165 /* Given a predicate expression EXP, from form NAME, determine whether
166 it refers to the variable given as VAR. */
167 static bool
168 needs_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;
177 /* fall through */
178 case AND:
179 case IOR:
180 if (needs_variable (XEXP (exp, 1), var))
181 return true;
182 /* fall through */
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);
208 if (ISALNUM (q[0]) || q[0] == '_')
209 return false;
210 }
211 return true;
212
213 default:
214 gcc_unreachable ();
215 }
216 }
217
218 /* Given an RTL expression EXP, find all subexpressions which we may
219 assume to perform mode tests. Normal MATCH_OPERAND does;
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.
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)
230 static void
231 mark_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));
240 else if (p->special || GET_MODE (exp) != VOIDmode)
241 NO_MODE_TEST (exp) = 1;
242 }
243 break;
244
245 case MATCH_CODE:
246 NO_MODE_TEST (exp) = 1;
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;
261
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:
283 gcc_unreachable ();
284 }
285 }
286
287 /* Determine whether the expression EXP is a MATCH_CODE that should
288 be written as a switch statement. */
289 static bool
290 generate_switch_p (rtx exp)
291 {
292 return GET_CODE (exp) == MATCH_CODE
293 && strchr (XSTR (exp, 0), ',');
294 }
295
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
302 static void
303 add_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
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:
323 /* Special handling for (VOIDmode) LABEL_REFs. */
324 case LABEL_REF:
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
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);
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";
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;
378
379 switch (GET_CODE (subexp))
380 {
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
384 push this AND down into the second operand of an existing
385 AND expression. */
386 if (generate_switch_p (XEXP (subexp, 0)))
387 pos = &XEXP (subexp, 1);
388 goto break_loop;
389
390 case IOR:
391 {
392 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
393 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
394
395 gcc_assert (test0 || test1);
396
397 if (test0 && test1)
398 goto break_loop;
399 pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
400 }
401 break;
402
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));
408
409 gcc_assert ((test0 && test1) || test2);
410
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
416 the test. */
417 pos = &XEXP (subexp, 1);
418 else
419 pos = &XEXP (subexp, 2);
420 }
421 break;
422
423 default:
424 goto break_loop;
425 }
426 }
427 break_loop:
428 XEXP (and_exp, 0) = *pos;
429 *pos = and_exp;
430 }
431
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. */
436 static void
437 write_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
451 gcc_unreachable ();
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 }
466
467 /* CODES is a list of RTX codes. Write out an expression which
468 determines whether the operand has one of those codes. */
469 static void
470 write_match_code (const char *path, const char *codes)
471 {
472 const char *code;
473
474 while ((code = scan_comma_elt (&codes)) != 0)
475 {
476 fputs ("GET_CODE (", stdout);
477 write_extract_subexp (path);
478 fputs (") == ", stdout);
479 while (code < codes)
480 {
481 putchar (TOUPPER (*code));
482 code++;
483 }
484
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. */
492 static void
493 write_predicate_expr (rtx exp)
494 {
495 switch (GET_CODE (exp))
496 {
497 case AND:
498 putchar ('(');
499 write_predicate_expr (XEXP (exp, 0));
500 fputs (") && (", stdout);
501 write_predicate_expr (XEXP (exp, 1));
502 putchar (')');
503 break;
504
505 case IOR:
506 putchar ('(');
507 write_predicate_expr (XEXP (exp, 0));
508 fputs (") || (", stdout);
509 write_predicate_expr (XEXP (exp, 1));
510 putchar (')');
511 break;
512
513 case NOT:
514 fputs ("!(", stdout);
515 write_predicate_expr (XEXP (exp, 0));
516 putchar (')');
517 break;
518
519 case IF_THEN_ELSE:
520 putchar ('(');
521 write_predicate_expr (XEXP (exp, 0));
522 fputs (") ? (", stdout);
523 write_predicate_expr (XEXP (exp, 1));
524 fputs (") : (", stdout);
525 write_predicate_expr (XEXP (exp, 2));
526 putchar (')');
527 break;
528
529 case MATCH_OPERAND:
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)]);
534 break;
535
536 case MATCH_CODE:
537 write_match_code (XSTR (exp, 1), XSTR (exp, 0));
538 break;
539
540 case MATCH_TEST:
541 rtx_reader_ptr->print_c_condition (stdout, XSTR (exp, 0));
542 break;
543
544 default:
545 gcc_unreachable ();
546 }
547 }
548
549 /* Write the MATCH_CODE expression EXP as a switch statement. */
550
551 static void
552 write_match_code_switch (rtx exp)
553 {
554 const char *codes = XSTR (exp, 0);
555 const char *path = XSTR (exp, 1);
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 }
570 fputs (":\n", stdout);
571 }
572 }
573
574 /* Given a predicate expression EXP, write out a sequence of stmts
575 to evaluate it. This is similar to write_predicate_expr but can
576 generate efficient switch statements. */
577
578 static void
579 write_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 }
618 break;
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
637 fputs (" return ",stdout);
638 write_predicate_expr (exp);
639 fputs (";\n", stdout);
640 }
641
642 /* Given a predicate, write out a complete C function to compute it. */
643 static void
644 write_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
652 /* A normal predicate can legitimately not look at machine_mode
653 if it accepts only CONST_INTs and/or CONST_WIDE_INT and/or CONST_DOUBLEs. */
654 printf ("bool\n%s (rtx op, machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
655 p->name);
656 write_predicate_stmts (p->exp);
657 fputs ("}\n\n", stdout);
658 }
659
660 /* Constraints fall into two categories: register constraints
661 (define_register_constraint), and others (define_constraint,
662 define_memory_constraint, define_special_memory_constraint,
663 define_relaxed_memory_constraint, define_address_constraint). We work out
664 automatically which of the various old-style macros they correspond to, and
665 produce appropriate code. They all go in the same hash table so we can
666 verify that there are no duplicate names. */
667
668 /* All data from one constraint definition. */
669 class constraint_data
670 {
671 public:
672 class constraint_data *next_this_letter;
673 class constraint_data *next_textual;
674 const char *name;
675 const char *c_name; /* same as .name unless mangling is necessary */
676 file_location loc; /* location of definition */
677 size_t namelen;
678 const char *regclass; /* for register constraints */
679 rtx exp; /* for other constraints */
680 const char *filter; /* the register filter condition, or null if none */
681 unsigned int is_register : 1;
682 unsigned int is_const_int : 1;
683 unsigned int is_const_dbl : 1;
684 unsigned int is_extra : 1;
685 unsigned int is_memory : 1;
686 unsigned int is_special_memory: 1;
687 unsigned int is_relaxed_memory: 1;
688 unsigned int is_address : 1;
689 unsigned int maybe_allows_reg : 1;
690 unsigned int maybe_allows_mem : 1;
691 };
692
693 /* Overview of all constraints beginning with a given letter. */
694
695 static class constraint_data *
696 constraints_by_letter_table[1<<CHAR_BIT];
697
698 /* For looking up all the constraints in the order that they appeared
699 in the machine description. */
700 static class constraint_data *first_constraint;
701 static class constraint_data **last_constraint_ptr = &first_constraint;
702
703 #define FOR_ALL_CONSTRAINTS(iter_) \
704 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
705
706 /* Contraint letters that have a special meaning and that cannot be used
707 in define*_constraints. */
708 static const char generic_constraint_letters[] = "g";
709
710 /* Machine-independent code expects that constraints with these
711 (initial) letters will allow only (a subset of all) CONST_INTs. */
712
713 static const char const_int_constraints[] = "IJKLMNOP";
714
715 /* Machine-independent code expects that constraints with these
716 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
717
718 static const char const_dbl_constraints[] = "GH";
719
720 /* Summary data used to decide whether to output various functions and
721 macro definitions. */
722 static unsigned int constraint_max_namelen;
723 static bool have_register_constraints;
724 static bool have_memory_constraints;
725 static bool have_special_memory_constraints;
726 static bool have_relaxed_memory_constraints;
727 static bool have_address_constraints;
728 static bool have_extra_constraints;
729 static bool have_const_int_constraints;
730 static unsigned int num_constraints;
731
732 static const constraint_data **enum_order;
733 static unsigned int register_start, register_end;
734 static unsigned int satisfied_start;
735 static unsigned int const_int_start, const_int_end;
736 static unsigned int memory_start, memory_end;
737 static unsigned int special_memory_start, special_memory_end;
738 static unsigned int relaxed_memory_start, relaxed_memory_end;
739 static unsigned int address_start, address_end;
740 static unsigned int maybe_allows_none_start, maybe_allows_none_end;
741 static unsigned int maybe_allows_reg_start, maybe_allows_reg_end;
742 static unsigned int maybe_allows_mem_start, maybe_allows_mem_end;
743
744 /* Convert NAME, which contains angle brackets and/or underscores, to
745 a string that can be used as part of a C identifier. The string
746 comes from the rtl_obstack. */
747 static const char *
748 mangle (const char *name)
749 {
750 for (; *name; name++)
751 switch (*name)
752 {
753 case '_': obstack_grow (rtl_obstack, "__", 2); break;
754 case '<': obstack_grow (rtl_obstack, "_l", 2); break;
755 case '>': obstack_grow (rtl_obstack, "_g", 2); break;
756 case ':': obstack_grow (rtl_obstack, "_c", 2); break;
757 default: obstack_1grow (rtl_obstack, *name); break;
758 }
759
760 obstack_1grow (rtl_obstack, '\0');
761 return XOBFINISH (rtl_obstack, const char *);
762 }
763
764 /* Add one constraint, of any sort, to the tables. NAME is its name; REGCLASS
765 is the register class, if any; EXP is the expression to test, if any;
766 IS_MEMORY, IS_SPECIAL_MEMORY, IS_RELAXED_MEMORY and IS_ADDRESS indicate
767 memory, special memory, and address constraints, respectively; LOC is the .md
768 file location; FILTER is the filter condition for a register constraint,
769 or null if none.
770
771 Not all combinations of arguments are valid; most importantly, REGCLASS is
772 mutually exclusive with EXP, and
773 IS_MEMORY/IS_SPECIAL_MEMORY/IS_RELAXED_MEMORY/IS_ADDRESS are only meaningful
774 for constraints with EXP.
775
776 This function enforces all syntactic and semantic rules about what
777 constraints can be defined. */
778
779 static void
780 add_constraint (const char *name, const char *regclass,
781 rtx exp, bool is_memory, bool is_special_memory,
782 bool is_relaxed_memory, bool is_address, file_location loc,
783 const char *filter = nullptr)
784 {
785 class constraint_data *c, **iter, **slot;
786 const char *p;
787 bool need_mangled_name = false;
788 bool is_const_int;
789 bool is_const_dbl;
790 size_t namelen;
791
792 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
793 name = general_mem;
794
795 if (exp && validate_exp (exp, name, loc))
796 return;
797
798 for (p = name; *p; p++)
799 if (!ISALNUM (*p))
800 {
801 if (*p == '<' || *p == '>' || *p == '_' || *p == ':')
802 need_mangled_name = true;
803 else
804 {
805 error_at (loc, "constraint name '%s' must be composed of letters,"
806 " digits, underscores, colon and angle brackets",
807 name);
808 return;
809 }
810 }
811
812 if (strchr (generic_constraint_letters, name[0]))
813 {
814 if (name[1] == '\0')
815 error_at (loc, "constraint letter '%s' cannot be "
816 "redefined by the machine description", name);
817 else
818 error_at (loc, "constraint name '%s' cannot be defined by the machine"
819 " description, as it begins with '%c'", name, name[0]);
820 return;
821 }
822
823
824 namelen = strlen (name);
825 slot = &constraints_by_letter_table[(unsigned int)name[0]];
826 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
827 {
828 /* This causes slot to end up pointing to the
829 next_this_letter field of the last constraint with a name
830 of equal or greater length than the new constraint; hence
831 the new constraint will be inserted after all previous
832 constraints with names of the same length. */
833 if ((*iter)->namelen >= namelen)
834 slot = iter;
835
836 if (!strcmp ((*iter)->name, name))
837 {
838 error_at (loc, "redefinition of constraint '%s'", name);
839 message_at ((*iter)->loc, "previous definition is here");
840 return;
841 }
842 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
843 {
844 error_at (loc, "defining constraint '%s' here", name);
845 message_at ((*iter)->loc, "renders constraint '%s' "
846 "(defined here) a prefix", (*iter)->name);
847 return;
848 }
849 else if (!strncmp ((*iter)->name, name, namelen))
850 {
851 error_at (loc, "constraint '%s' is a prefix", name);
852 message_at ((*iter)->loc, "of constraint '%s' (defined here)",
853 (*iter)->name);
854 return;
855 }
856 }
857
858 is_const_int = strchr (const_int_constraints, name[0]) != 0;
859 is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
860
861 if (is_const_int || is_const_dbl)
862 {
863 enum rtx_code appropriate_code
864 = is_const_int ? CONST_INT : CONST_DOUBLE;
865
866 /* Consider relaxing this requirement in the future. */
867 if (regclass
868 || GET_CODE (exp) != AND
869 || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
870 || strcmp (XSTR (XEXP (exp, 0), 0),
871 GET_RTX_NAME (appropriate_code)))
872 {
873 if (name[1] == '\0')
874 error_at (loc, "constraint letter '%c' is reserved "
875 "for %s constraints", name[0],
876 GET_RTX_NAME (appropriate_code));
877 else
878 error_at (loc, "constraint names beginning with '%c' "
879 "(%s) are reserved for %s constraints",
880 name[0], name, GET_RTX_NAME (appropriate_code));
881 return;
882 }
883
884 if (is_memory || is_special_memory || is_relaxed_memory)
885 {
886 if (name[1] == '\0')
887 error_at (loc, "constraint letter '%c' cannot be a "
888 "memory constraint", name[0]);
889 else
890 error_at (loc, "constraint name '%s' begins with '%c', "
891 "and therefore cannot be a memory constraint",
892 name, name[0]);
893 return;
894 }
895 else if (is_address)
896 {
897 if (name[1] == '\0')
898 error_at (loc, "constraint letter '%c' cannot be an "
899 "address constraint", name[0]);
900 else
901 error_at (loc, "constraint name '%s' begins with '%c', "
902 "and therefore cannot be an address constraint",
903 name, name[0]);
904 return;
905 }
906 }
907
908
909 c = XOBNEW (rtl_obstack, class constraint_data);
910 c->name = name;
911 c->c_name = need_mangled_name ? mangle (name) : name;
912 c->loc = loc;
913 c->namelen = namelen;
914 c->regclass = regclass;
915 c->exp = exp;
916 c->filter = filter;
917 c->is_register = regclass != 0;
918 c->is_const_int = is_const_int;
919 c->is_const_dbl = is_const_dbl;
920 c->is_extra = !(regclass || is_const_int || is_const_dbl);
921 c->is_memory = is_memory;
922 c->is_special_memory = is_special_memory;
923 c->is_relaxed_memory = is_relaxed_memory;
924 c->is_address = is_address;
925 c->maybe_allows_reg = true;
926 c->maybe_allows_mem = true;
927 if (exp)
928 {
929 char codes[NUM_RTX_CODE];
930 compute_test_codes (exp, loc, codes);
931 if (!codes[REG] && !codes[SUBREG])
932 c->maybe_allows_reg = false;
933 if (!codes[MEM])
934 c->maybe_allows_mem = false;
935 }
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;
948 have_extra_constraints |= c->is_extra;
949 have_memory_constraints |= c->is_memory;
950 have_special_memory_constraints |= c->is_special_memory;
951 have_relaxed_memory_constraints |= c->is_relaxed_memory;
952 have_address_constraints |= c->is_address;
953 num_constraints += 1;
954 }
955
956 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT,
957 DEFINE_SPECIAL_MEMORY_CONSTRAINT, DEFINE_RELAXED_MEMORY_CONSTRAINT, or
958 DEFINE_ADDRESS_CONSTRAINT expression, C. */
959 static void
960 process_define_constraint (md_rtx_info *info)
961 {
962 add_constraint (XSTR (info->def, 0), 0, XEXP (info->def, 2),
963 GET_CODE (info->def) == DEFINE_MEMORY_CONSTRAINT,
964 GET_CODE (info->def) == DEFINE_SPECIAL_MEMORY_CONSTRAINT,
965 GET_CODE (info->def) == DEFINE_RELAXED_MEMORY_CONSTRAINT,
966 GET_CODE (info->def) == DEFINE_ADDRESS_CONSTRAINT,
967 info->loc);
968 }
969
970 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
971 static void
972 process_define_register_constraint (md_rtx_info *info)
973 {
974 add_constraint (XSTR (info->def, 0), XSTR (info->def, 1),
975 0, false, false, false, false, info->loc,
976 XSTR (info->def, 3));
977 }
978
979 /* Put the constraints into enum order. We want to keep constraints
980 of the same type together so that query functions can be simple
981 range checks. */
982 static void
983 choose_enum_order (void)
984 {
985 class constraint_data *c;
986
987 enum_order = XNEWVEC (const constraint_data *, num_constraints);
988 unsigned int next = 0;
989
990 register_start = next;
991 FOR_ALL_CONSTRAINTS (c)
992 if (c->is_register)
993 enum_order[next++] = c;
994 register_end = next;
995
996 satisfied_start = next;
997
998 const_int_start = next;
999 FOR_ALL_CONSTRAINTS (c)
1000 if (c->is_const_int)
1001 enum_order[next++] = c;
1002 const_int_end = next;
1003
1004 memory_start = next;
1005 FOR_ALL_CONSTRAINTS (c)
1006 if (c->is_memory)
1007 enum_order[next++] = c;
1008 memory_end = next;
1009
1010 special_memory_start = next;
1011 FOR_ALL_CONSTRAINTS (c)
1012 if (c->is_special_memory)
1013 enum_order[next++] = c;
1014 special_memory_end = next;
1015
1016 relaxed_memory_start = next;
1017 FOR_ALL_CONSTRAINTS (c)
1018 if (c->is_relaxed_memory)
1019 enum_order[next++] = c;
1020 relaxed_memory_end = next;
1021
1022 address_start = next;
1023 FOR_ALL_CONSTRAINTS (c)
1024 if (c->is_address)
1025 enum_order[next++] = c;
1026 address_end = next;
1027
1028 maybe_allows_none_start = next;
1029 FOR_ALL_CONSTRAINTS (c)
1030 if (!c->is_register && !c->is_const_int && !c->is_memory
1031 && !c->is_special_memory && !c->is_relaxed_memory && !c->is_address
1032 && !c->maybe_allows_reg && !c->maybe_allows_mem)
1033 enum_order[next++] = c;
1034 maybe_allows_none_end = next;
1035
1036 maybe_allows_reg_start = next;
1037 FOR_ALL_CONSTRAINTS (c)
1038 if (!c->is_register && !c->is_const_int && !c->is_memory
1039 && !c->is_special_memory && !c->is_relaxed_memory && !c->is_address
1040 && c->maybe_allows_reg && !c->maybe_allows_mem)
1041 enum_order[next++] = c;
1042 maybe_allows_reg_end = next;
1043
1044 maybe_allows_mem_start = next;
1045 FOR_ALL_CONSTRAINTS (c)
1046 if (!c->is_register && !c->is_const_int && !c->is_memory
1047 && !c->is_special_memory && !c->is_relaxed_memory && !c->is_address
1048 && !c->maybe_allows_reg && c->maybe_allows_mem)
1049 enum_order[next++] = c;
1050 maybe_allows_mem_end = next;
1051
1052 FOR_ALL_CONSTRAINTS (c)
1053 if (!c->is_register && !c->is_const_int && !c->is_memory
1054 && !c->is_special_memory && !c->is_relaxed_memory && !c->is_address
1055 && c->maybe_allows_reg && c->maybe_allows_mem)
1056 enum_order[next++] = c;
1057 gcc_assert (next == num_constraints);
1058 }
1059
1060 /* Write out an enumeration with one entry per machine-specific
1061 constraint. */
1062 static void
1063 write_enum_constraint_num (void)
1064 {
1065 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
1066 fputs ("enum constraint_num\n"
1067 "{\n"
1068 " CONSTRAINT__UNKNOWN = 0", stdout);
1069 for (unsigned int i = 0; i < num_constraints; ++i)
1070 printf (",\n CONSTRAINT_%s", enum_order[i]->c_name);
1071 puts (",\n CONSTRAINT__LIMIT\n};\n");
1072 }
1073
1074 /* Write out a function which looks at a string and determines what
1075 constraint name, if any, it begins with. */
1076 static void
1077 write_lookup_constraint_1 (void)
1078 {
1079 unsigned int i;
1080 puts ("enum constraint_num\n"
1081 "lookup_constraint_1 (const char *str)\n"
1082 "{\n"
1083 " switch (str[0])\n"
1084 " {");
1085
1086 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1087 {
1088 class constraint_data *c = constraints_by_letter_table[i];
1089 if (!c)
1090 continue;
1091
1092 printf (" case '%c':\n", i);
1093 if (c->namelen == 1)
1094 printf (" return CONSTRAINT_%s;\n", c->c_name);
1095 else
1096 {
1097 do
1098 {
1099 if (c->namelen > 2)
1100 printf (" if (!strncmp (str + 1, \"%s\", "
1101 HOST_SIZE_T_PRINT_UNSIGNED "))\n"
1102 " return CONSTRAINT_%s;\n",
1103 c->name + 1, (fmt_size_t) (c->namelen - 1),
1104 c->c_name);
1105 else
1106 printf (" if (str[1] == '%c')\n"
1107 " return CONSTRAINT_%s;\n",
1108 c->name[1], c->c_name);
1109 c = c->next_this_letter;
1110 }
1111 while (c);
1112 puts (" break;");
1113 }
1114 }
1115
1116 puts (" default: break;\n"
1117 " }\n"
1118 " return CONSTRAINT__UNKNOWN;\n"
1119 "}\n");
1120 }
1121
1122 /* Write out an array that maps single-letter characters to their
1123 constraints (if that fits in a character) or 255 if lookup_constraint_1
1124 must be called. */
1125 static void
1126 write_lookup_constraint_array (void)
1127 {
1128 unsigned int i;
1129 printf ("const unsigned char lookup_constraint_array[] = {\n ");
1130 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1131 {
1132 if (i != 0)
1133 printf (",\n ");
1134 class constraint_data *c = constraints_by_letter_table[i];
1135 if (!c)
1136 printf ("CONSTRAINT__UNKNOWN");
1137 else if (c->namelen == 1)
1138 printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1139 else
1140 printf ("UCHAR_MAX");
1141 }
1142 printf ("\n};\n\n");
1143 }
1144
1145 /* Write out a function which looks at a string and determines what
1146 the constraint name length is. */
1147 static void
1148 write_insn_constraint_len (void)
1149 {
1150 unsigned int i;
1151
1152 puts ("static inline size_t\n"
1153 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
1154 "{\n"
1155 " switch (fc)\n"
1156 " {");
1157
1158 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1159 {
1160 class constraint_data *c = constraints_by_letter_table[i];
1161
1162 if (!c
1163 || c->namelen == 1)
1164 continue;
1165
1166 /* Constraints with multiple characters should have the same
1167 length. */
1168 {
1169 class constraint_data *c2 = c->next_this_letter;
1170 size_t len = c->namelen;
1171 while (c2)
1172 {
1173 if (c2->namelen != len)
1174 error ("Multi-letter constraints with first letter '%c' "
1175 "should have same length", i);
1176 c2 = c2->next_this_letter;
1177 }
1178 }
1179
1180 printf (" case '%c': return " HOST_SIZE_T_PRINT_UNSIGNED ";\n",
1181 i, (fmt_size_t) c->namelen);
1182 }
1183
1184 puts (" default: break;\n"
1185 " }\n"
1186 " return 1;\n"
1187 "}\n");
1188 }
1189
1190 /* Write out the function which computes the register class corresponding
1191 to a register constraint. */
1192 static void
1193 write_reg_class_for_constraint_1 (void)
1194 {
1195 class constraint_data *c;
1196
1197 puts ("enum reg_class\n"
1198 "reg_class_for_constraint_1 (enum constraint_num c)\n"
1199 "{\n"
1200 " switch (c)\n"
1201 " {");
1202
1203 FOR_ALL_CONSTRAINTS (c)
1204 if (c->is_register)
1205 printf (" case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1206
1207 puts (" default: break;\n"
1208 " }\n"
1209 " return NO_REGS;\n"
1210 "}\n");
1211 }
1212
1213 /* Write out the functions which compute whether a given value matches
1214 a given non-register constraint. */
1215 static void
1216 write_tm_constrs_h (void)
1217 {
1218 class constraint_data *c;
1219
1220 printf ("\
1221 /* Generated automatically by the program '%s'\n\
1222 from the machine description file '%s'. */\n\n", progname,
1223 md_reader_ptr->get_top_level_filename ());
1224
1225 puts ("\
1226 #ifndef GCC_TM_CONSTRS_H\n\
1227 #define GCC_TM_CONSTRS_H\n");
1228
1229 FOR_ALL_CONSTRAINTS (c)
1230 if (!c->is_register)
1231 {
1232 bool needs_ival = needs_variable (c->exp, "ival");
1233 bool needs_hval = needs_variable (c->exp, "hval");
1234 bool needs_lval = needs_variable (c->exp, "lval");
1235 bool needs_rval = needs_variable (c->exp, "rval");
1236 bool needs_mode = (needs_variable (c->exp, "mode")
1237 || needs_hval || needs_lval || needs_rval);
1238 bool needs_op = (needs_variable (c->exp, "op")
1239 || needs_ival || needs_mode);
1240
1241 printf ("static inline bool\n"
1242 "satisfies_constraint_%s (rtx %s)\n"
1243 "{\n", c->c_name,
1244 needs_op ? "op" : "ARG_UNUSED (op)");
1245 if (needs_mode)
1246 puts (" machine_mode mode = GET_MODE (op);");
1247 if (needs_ival)
1248 puts (" HOST_WIDE_INT ival = 0;");
1249 if (needs_hval)
1250 puts (" HOST_WIDE_INT hval = 0;");
1251 if (needs_lval)
1252 puts (" unsigned HOST_WIDE_INT lval = 0;");
1253 if (needs_rval)
1254 puts (" const REAL_VALUE_TYPE *rval = 0;");
1255
1256 if (needs_ival)
1257 puts (" if (CONST_INT_P (op))\n"
1258 " ival = INTVAL (op);");
1259 #if TARGET_SUPPORTS_WIDE_INT
1260 if (needs_lval || needs_hval)
1261 error ("you can't use lval or hval");
1262 #else
1263 if (needs_hval)
1264 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1265 " hval = CONST_DOUBLE_HIGH (op);");
1266 if (needs_lval)
1267 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1268 " lval = CONST_DOUBLE_LOW (op);");
1269 #endif
1270 if (needs_rval)
1271 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1272 " rval = CONST_DOUBLE_REAL_VALUE (op);");
1273
1274 write_predicate_stmts (c->exp);
1275 fputs ("}\n", stdout);
1276 }
1277 puts ("#endif /* tm-constrs.h */");
1278 }
1279
1280 /* Write out the wrapper function, constraint_satisfied_p, that maps
1281 a CONSTRAINT_xxx constant to one of the predicate functions generated
1282 above. */
1283 static void
1284 write_constraint_satisfied_p_array (void)
1285 {
1286 if (satisfied_start == num_constraints)
1287 return;
1288
1289 printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n ");
1290 for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1291 {
1292 if (i != satisfied_start)
1293 printf (",\n ");
1294 printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1295 }
1296 printf ("\n};\n\n");
1297 }
1298
1299 /* Write out the function which computes whether a given value matches
1300 a given CONST_INT constraint. This doesn't just forward to
1301 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1302 static void
1303 write_insn_const_int_ok_for_constraint (void)
1304 {
1305 class constraint_data *c;
1306
1307 puts ("bool\n"
1308 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1309 "enum constraint_num c)\n"
1310 "{\n"
1311 " switch (c)\n"
1312 " {");
1313
1314 FOR_ALL_CONSTRAINTS (c)
1315 if (c->is_const_int)
1316 {
1317 printf (" case CONSTRAINT_%s:\n return ", c->c_name);
1318 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1319 we know at this point that we have a const_int, so we need not
1320 bother with that part of the test. */
1321 write_predicate_expr (XEXP (c->exp, 1));
1322 fputs (";\n\n", stdout);
1323 }
1324
1325 puts (" default: break;\n"
1326 " }\n"
1327 " return false;\n"
1328 "}\n");
1329 }
1330
1331 /* Print the init_reg_class_start_regs function, which initializes
1332 this_target_constraints->register_filters from the C conditions
1333 in the define_register_constraints. */
1334 static void
1335 write_init_reg_class_start_regs ()
1336 {
1337 printf ("\n"
1338 "void\n"
1339 "init_reg_class_start_regs ()\n"
1340 "{\n");
1341 if (!register_filters.is_empty ())
1342 {
1343 printf (" for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER;"
1344 " ++regno)\n"
1345 " {\n");
1346 for (unsigned int i = 0; i < register_filters.length (); ++i)
1347 {
1348 printf (" if (");
1349 rtx_reader_ptr->print_c_condition (stdout, register_filters[i]);
1350 printf (")\n"
1351 " SET_HARD_REG_BIT (%s[%d], regno);\n",
1352 "this_target_constraints->register_filters", i);
1353 }
1354 printf (" }\n");
1355 }
1356 printf ("}\n");
1357 }
1358
1359 /* Write a definition for a function NAME that returns true if a given
1360 constraint_num is in the range [START, END). */
1361 static void
1362 write_range_function (const char *name, unsigned int start, unsigned int end)
1363 {
1364 printf ("static inline bool\n");
1365 if (start != end)
1366 printf ("%s (enum constraint_num c)\n"
1367 "{\n"
1368 " return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1369 "}\n\n",
1370 name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1371 else
1372 printf ("%s (enum constraint_num)\n"
1373 "{\n"
1374 " return false;\n"
1375 "}\n\n", name);
1376 }
1377
1378 /* Write a definition for insn_extra_constraint_allows_reg_mem function. */
1379 static void
1380 write_allows_reg_mem_function (void)
1381 {
1382 printf ("static inline void\n"
1383 "insn_extra_constraint_allows_reg_mem (enum constraint_num c,\n"
1384 "\t\t\t\t bool *allows_reg, bool *allows_mem)\n"
1385 "{\n");
1386 if (maybe_allows_none_start != maybe_allows_none_end)
1387 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1388 " return;\n",
1389 enum_order[maybe_allows_none_start]->c_name,
1390 enum_order[maybe_allows_none_end - 1]->c_name);
1391 if (maybe_allows_reg_start != maybe_allows_reg_end)
1392 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1393 " {\n"
1394 " *allows_reg = true;\n"
1395 " return;\n"
1396 " }\n",
1397 enum_order[maybe_allows_reg_start]->c_name,
1398 enum_order[maybe_allows_reg_end - 1]->c_name);
1399 if (maybe_allows_mem_start != maybe_allows_mem_end)
1400 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1401 " {\n"
1402 " *allows_mem = true;\n"
1403 " return;\n"
1404 " }\n",
1405 enum_order[maybe_allows_mem_start]->c_name,
1406 enum_order[maybe_allows_mem_end - 1]->c_name);
1407 printf (" (void) c;\n"
1408 " *allows_reg = true;\n"
1409 " *allows_mem = true;\n"
1410 "}\n\n");
1411 }
1412
1413 /* VEC is a list of key/value pairs, with the keys being lower bounds
1414 of a range. Output a decision tree that handles the keys covered by
1415 [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1416 INDENT is the number of spaces to indent the code. */
1417 static void
1418 print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1419 unsigned int start, unsigned int end, const char *fallback,
1420 unsigned int indent)
1421 {
1422 while (start < end)
1423 {
1424 unsigned int mid = (start + end) / 2;
1425 printf ("%*sif (c >= CONSTRAINT_%s)\n",
1426 indent, "", enum_order[vec[mid].first]->c_name);
1427 if (mid + 1 == end)
1428 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1429 else
1430 {
1431 printf ("%*s{\n", indent + 2, "");
1432 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1433 printf ("%*s}\n", indent + 2, "");
1434 }
1435 end = mid;
1436 }
1437 printf ("%*sreturn %s;\n", indent, "", fallback);
1438 }
1439
1440 /* Print the get_register_filter function, which returns a pointer
1441 to the start register filter for a given constraint, or null if none. */
1442 static void
1443 write_get_register_filter ()
1444 {
1445 constraint_data *c;
1446
1447 printf ("\n"
1448 "#ifdef GCC_HARD_REG_SET_H\n"
1449 "static inline const HARD_REG_SET *\n"
1450 "get_register_filter (constraint_num%s)\n",
1451 register_filters.is_empty () ? "" : " c");
1452 printf ("{\n");
1453 FOR_ALL_CONSTRAINTS (c)
1454 if (c->is_register && c->filter)
1455 {
1456 printf (" if (c == CONSTRAINT_%s)\n", c->c_name);
1457 printf (" return &this_target_constraints->register_filters[%d];\n",
1458 get_register_filter_id (c->filter));
1459 }
1460 printf (" return nullptr;\n"
1461 "}\n"
1462 "#endif\n");
1463 }
1464
1465 /* Print the get_register_filter_id function, which returns the index
1466 of the given constraint's register filter in
1467 this_target_constraints->register_filters, or -1 if none. */
1468 static void
1469 write_get_register_filter_id ()
1470 {
1471 constraint_data *c;
1472
1473 printf ("\n"
1474 "static inline int\n"
1475 "get_register_filter_id (constraint_num%s)\n",
1476 register_filters.is_empty () ? "" : " c");
1477 printf ("{\n");
1478 FOR_ALL_CONSTRAINTS (c)
1479 if (c->is_register && c->filter)
1480 {
1481 printf (" if (c == CONSTRAINT_%s)\n", c->c_name);
1482 printf (" return %d;\n", get_register_filter_id (c->filter));
1483 }
1484 printf (" return -1;\n"
1485 "}\n");
1486 }
1487
1488 /* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1489 an enumeration in portable C, so we have to condition all these
1490 prototypes on HAVE_MACHINE_MODES. */
1491 static void
1492 write_tm_preds_h (void)
1493 {
1494 struct pred_data *p;
1495
1496 printf ("\
1497 /* Generated automatically by the program '%s'\n\
1498 from the machine description file '%s'. */\n\n", progname,
1499 md_reader_ptr->get_top_level_filename ());
1500
1501 puts ("\
1502 #ifndef GCC_TM_PREDS_H\n\
1503 #define GCC_TM_PREDS_H\n\
1504 \n\
1505 #ifdef HAVE_MACHINE_MODES");
1506
1507 FOR_ALL_PREDICATES (p)
1508 printf ("extern bool %s (rtx, machine_mode);\n", p->name);
1509
1510 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1511
1512 /* Print the definition of the target_constraints structure. */
1513 printf ("#ifdef GCC_HARD_REG_SET_H\n"
1514 "struct target_constraints {\n"
1515 " HARD_REG_SET register_filters[%d];\n",
1516 MAX (register_filters.length (), 1));
1517 printf ("};\n"
1518 "\n"
1519 "extern struct target_constraints default_target_constraints;\n"
1520 "#if SWITCHABLE_TARGET\n"
1521 "extern struct target_constraints *this_target_constraints;\n"
1522 "#else\n"
1523 "#define this_target_constraints (&default_target_constraints)\n"
1524 "#endif\n");
1525
1526 /* Print TEST_REGISTER_FILTER_BIT, which tests whether register REGNO
1527 is a valid start register for register filter ID. */
1528 printf ("\n"
1529 "#define TEST_REGISTER_FILTER_BIT(ID, REGNO) \\\n");
1530 if (register_filters.is_empty ())
1531 printf (" ((void) (ID), (void) (REGNO), false)\n");
1532 else
1533 printf (" TEST_HARD_REG_BIT ("
1534 "this_target_constraints->register_filters[ID], REGNO)\n");
1535
1536 /* Print test_register_filters, which tests whether register REGNO
1537 is a valid start register for the mask of register filters in MASK. */
1538 printf ("\n"
1539 "inline bool\n"
1540 "test_register_filters (unsigned int%s, unsigned int%s)\n",
1541 register_filters.is_empty () ? "" : " mask",
1542 register_filters.is_empty () ? "" : " regno");
1543 printf ("{\n");
1544 if (register_filters.is_empty ())
1545 printf (" return true;\n");
1546 else
1547 {
1548 printf (" for (unsigned int id = 0; id < %d; ++id)\n",
1549 register_filters.length ());
1550 printf (" if ((mask & (1U << id))\n"
1551 "\t&& !TEST_REGISTER_FILTER_BIT (id, regno))\n"
1552 " return false;\n"
1553 " return true;\n");
1554 }
1555 printf ("}\n"
1556 "#endif\n"
1557 "\n");
1558
1559 if (constraint_max_namelen > 0)
1560 {
1561 write_enum_constraint_num ();
1562 puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1563 "extern const unsigned char lookup_constraint_array[];\n"
1564 "\n"
1565 "/* Return the constraint at the beginning of P, or"
1566 " CONSTRAINT__UNKNOWN if it\n"
1567 " isn't recognized. */\n"
1568 "\n"
1569 "static inline enum constraint_num\n"
1570 "lookup_constraint (const char *p)\n"
1571 "{\n"
1572 " unsigned int index = lookup_constraint_array"
1573 "[(unsigned char) *p];\n"
1574 " return (index == UCHAR_MAX\n"
1575 " ? lookup_constraint_1 (p)\n"
1576 " : (enum constraint_num) index);\n"
1577 "}\n");
1578 if (satisfied_start == num_constraints)
1579 puts ("/* Return true if X satisfies constraint C. */\n"
1580 "\n"
1581 "static inline bool\n"
1582 "constraint_satisfied_p (rtx, enum constraint_num)\n"
1583 "{\n"
1584 " return false;\n"
1585 "}\n");
1586 else
1587 printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
1588 "\n"
1589 "/* Return true if X satisfies constraint C. */\n"
1590 "\n"
1591 "static inline bool\n"
1592 "constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1593 "{\n"
1594 " int i = (int) c - (int) CONSTRAINT_%s;\n"
1595 " return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
1596 "}\n"
1597 "\n",
1598 enum_order[satisfied_start]->name);
1599
1600 write_range_function ("insn_extra_register_constraint",
1601 register_start, register_end);
1602 write_range_function ("insn_extra_memory_constraint",
1603 memory_start, memory_end);
1604 write_range_function ("insn_extra_special_memory_constraint",
1605 special_memory_start, special_memory_end);
1606 write_range_function ("insn_extra_relaxed_memory_constraint",
1607 relaxed_memory_start, relaxed_memory_end);
1608 write_range_function ("insn_extra_address_constraint",
1609 address_start, address_end);
1610 write_allows_reg_mem_function ();
1611
1612 if (constraint_max_namelen > 1)
1613 {
1614 write_insn_constraint_len ();
1615 puts ("#define CONSTRAINT_LEN(c_,s_) "
1616 "insn_constraint_len (c_,s_)\n");
1617 }
1618 else
1619 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1620 if (have_register_constraints)
1621 puts ("extern enum reg_class reg_class_for_constraint_1 "
1622 "(enum constraint_num);\n"
1623 "\n"
1624 "static inline enum reg_class\n"
1625 "reg_class_for_constraint (enum constraint_num c)\n"
1626 "{\n"
1627 " if (insn_extra_register_constraint (c))\n"
1628 " return reg_class_for_constraint_1 (c);\n"
1629 " return NO_REGS;\n"
1630 "}\n");
1631 else
1632 puts ("static inline enum reg_class\n"
1633 "reg_class_for_constraint (enum constraint_num)\n"
1634 "{\n"
1635 " return NO_REGS;\n"
1636 "}\n");
1637 if (have_const_int_constraints)
1638 puts ("extern bool insn_const_int_ok_for_constraint "
1639 "(HOST_WIDE_INT, enum constraint_num);\n"
1640 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1641 " insn_const_int_ok_for_constraint (v_, "
1642 "lookup_constraint (s_))\n");
1643 else
1644 puts ("static inline bool\n"
1645 "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1646 " enum constraint_num)\n"
1647 "{\n"
1648 " return false;\n"
1649 "}\n");
1650
1651 puts ("enum constraint_type\n"
1652 "{\n"
1653 " CT_REGISTER,\n"
1654 " CT_CONST_INT,\n"
1655 " CT_MEMORY,\n"
1656 " CT_SPECIAL_MEMORY,\n"
1657 " CT_RELAXED_MEMORY,\n"
1658 " CT_ADDRESS,\n"
1659 " CT_FIXED_FORM\n"
1660 "};\n"
1661 "\n"
1662 "static inline enum constraint_type\n"
1663 "get_constraint_type (enum constraint_num c)\n"
1664 "{");
1665 auto_vec <std::pair <unsigned int, const char *>, 4> values;
1666 if (const_int_start != const_int_end)
1667 values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
1668 if (memory_start != memory_end)
1669 values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
1670 if (special_memory_start != special_memory_end)
1671 values.safe_push (std::make_pair (special_memory_start,
1672 "CT_SPECIAL_MEMORY"));
1673 if (relaxed_memory_start != relaxed_memory_end)
1674 values.safe_push (std::make_pair (relaxed_memory_start,
1675 "CT_RELAXED_MEMORY"));
1676 if (address_start != address_end)
1677 values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1678 if (address_end != num_constraints)
1679 values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1680 print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1681 puts ("}");
1682
1683 write_get_register_filter ();
1684 write_get_register_filter_id ();
1685 }
1686
1687 puts ("#endif /* tm-preds.h */");
1688 }
1689
1690 /* Write insn-preds.cc.
1691 N.B. the list of headers to include was copied from genrecog; it
1692 may not be ideal.
1693
1694 FUTURE: Write #line markers referring back to the machine
1695 description. (Can't practically do this now since we don't know
1696 the line number of the C block - just the line number of the enclosing
1697 expression.) */
1698 static void
1699 write_insn_preds_c (void)
1700 {
1701 struct pred_data *p;
1702
1703 printf ("\
1704 /* Generated automatically by the program '%s'\n\
1705 from the machine description file '%s'. */\n\n", progname,
1706 md_reader_ptr->get_top_level_filename ());
1707
1708 puts ("\
1709 #define IN_TARGET_CODE 1\n\
1710 #include \"config.h\"\n\
1711 #include \"system.h\"\n\
1712 #include \"coretypes.h\"\n\
1713 #include \"backend.h\"\n\
1714 #include \"predict.h\"\n\
1715 #include \"tree.h\"\n\
1716 #include \"rtl.h\"\n\
1717 #include \"alias.h\"\n\
1718 #include \"varasm.h\"\n\
1719 #include \"stor-layout.h\"\n\
1720 #include \"calls.h\"\n\
1721 #include \"memmodel.h\"\n\
1722 #include \"tm_p.h\"\n\
1723 #include \"insn-config.h\"\n\
1724 #include \"recog.h\"\n\
1725 #include \"output.h\"\n\
1726 #include \"flags.h\"\n\
1727 #include \"df.h\"\n\
1728 #include \"resource.h\"\n\
1729 #include \"diagnostic-core.h\"\n\
1730 #include \"reload.h\"\n\
1731 #include \"regs.h\"\n\
1732 #include \"emit-rtl.h\"\n\
1733 #include \"tm-constrs.h\"\n\
1734 #include \"target.h\"\n");
1735
1736 printf ("\n"
1737 "struct target_constraints default_target_constraints;\n"
1738 "#if SWITCHABLE_TARGET\n"
1739 "struct target_constraints *this_target_constraints"
1740 " = &default_target_constraints;\n"
1741 "#endif\n");
1742
1743 FOR_ALL_PREDICATES (p)
1744 write_one_predicate_function (p);
1745
1746 if (constraint_max_namelen > 0)
1747 {
1748 write_lookup_constraint_1 ();
1749 write_lookup_constraint_array ();
1750 if (have_register_constraints)
1751 write_reg_class_for_constraint_1 ();
1752 write_constraint_satisfied_p_array ();
1753
1754 if (have_const_int_constraints)
1755 write_insn_const_int_ok_for_constraint ();
1756 }
1757
1758 write_init_reg_class_start_regs ();
1759 }
1760
1761 /* Argument parsing. */
1762 static bool gen_header;
1763 static bool gen_constrs;
1764
1765 static bool
1766 parse_option (const char *opt)
1767 {
1768 if (!strcmp (opt, "-h"))
1769 {
1770 gen_header = true;
1771 return 1;
1772 }
1773 else if (!strcmp (opt, "-c"))
1774 {
1775 gen_constrs = true;
1776 return 1;
1777 }
1778 else
1779 return 0;
1780 }
1781
1782 /* Master control. */
1783 int
1784 main (int argc, const char **argv)
1785 {
1786 progname = argv[0];
1787 if (argc <= 1)
1788 fatal ("no input file name");
1789 if (!init_rtx_reader_args_cb (argc, argv, parse_option))
1790 return FATAL_EXIT_CODE;
1791
1792 md_rtx_info info;
1793 while (read_md_rtx (&info))
1794 switch (GET_CODE (info.def))
1795 {
1796 case DEFINE_PREDICATE:
1797 case DEFINE_SPECIAL_PREDICATE:
1798 process_define_predicate (&info);
1799 break;
1800
1801 case DEFINE_CONSTRAINT:
1802 case DEFINE_MEMORY_CONSTRAINT:
1803 case DEFINE_SPECIAL_MEMORY_CONSTRAINT:
1804 case DEFINE_RELAXED_MEMORY_CONSTRAINT:
1805 case DEFINE_ADDRESS_CONSTRAINT:
1806 process_define_constraint (&info);
1807 break;
1808
1809 case DEFINE_REGISTER_CONSTRAINT:
1810 process_define_register_constraint (&info);
1811 break;
1812
1813 default:
1814 break;
1815 }
1816
1817 choose_enum_order ();
1818
1819 if (gen_header)
1820 write_tm_preds_h ();
1821 else if (gen_constrs)
1822 write_tm_constrs_h ();
1823 else
1824 write_insn_preds_c ();
1825
1826 if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1827 return FATAL_EXIT_CODE;
1828
1829 return SUCCESS_EXIT_CODE;
1830 }