]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genpreds.c
Update copyright years.
[thirdparty/gcc.git] / gcc / genpreds.c
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.c)
5 Copyright (C) 2001-2016 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 /* else fall through */
55 case AND:
56 case IOR:
57 if (validate_exp (XEXP (exp, 1), name, loc))
58 return true;
59 /* else 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 /* fall through */
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 int 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 int\n"
155 "%s_1 (rtx op, machine_mode mode ATTRIBUTE_UNUSED)\n",
156 p->name);
157 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 /* else fall through */
178 case AND:
179 case IOR:
180 if (needs_variable (XEXP (exp, 1), var))
181 return true;
182 /* else 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 print_c_condition (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 ("int\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 \f
660 /* Constraints fall into two categories: register constraints
661 (define_register_constraint), and others (define_constraint,
662 define_memory_constraint, define_address_constraint). We
663 work out automatically which of the various old-style macros
664 they correspond to, and produce appropriate code. They all
665 go in the same hash table so we can verify that there are no
666 duplicate names. */
667
668 /* All data from one constraint definition. */
669 struct 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 */
675 file_location loc; /* location of definition */
676 size_t namelen;
677 const char *regclass; /* for register constraints */
678 rtx exp; /* for other constraints */
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;
684 unsigned int is_address : 1;
685 unsigned int maybe_allows_reg : 1;
686 unsigned int maybe_allows_mem : 1;
687 };
688
689 /* Overview of all constraints beginning with a given letter. */
690
691 static struct constraint_data *
692 constraints_by_letter_table[1<<CHAR_BIT];
693
694 /* For looking up all the constraints in the order that they appeared
695 in the machine description. */
696 static struct constraint_data *first_constraint;
697 static struct constraint_data **last_constraint_ptr = &first_constraint;
698
699 #define FOR_ALL_CONSTRAINTS(iter_) \
700 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
701
702 /* Contraint letters that have a special meaning and that cannot be used
703 in define*_constraints. */
704 static const char generic_constraint_letters[] = "g";
705
706 /* Machine-independent code expects that constraints with these
707 (initial) letters will allow only (a subset of all) CONST_INTs. */
708
709 static const char const_int_constraints[] = "IJKLMNOP";
710
711 /* Machine-independent code expects that constraints with these
712 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
713
714 static const char const_dbl_constraints[] = "GH";
715
716 /* Summary data used to decide whether to output various functions and
717 macro definitions. */
718 static unsigned int constraint_max_namelen;
719 static bool have_register_constraints;
720 static bool have_memory_constraints;
721 static bool have_address_constraints;
722 static bool have_extra_constraints;
723 static bool have_const_int_constraints;
724 static unsigned int num_constraints;
725
726 static const constraint_data **enum_order;
727 static unsigned int register_start, register_end;
728 static unsigned int satisfied_start;
729 static unsigned int const_int_start, const_int_end;
730 static unsigned int memory_start, memory_end;
731 static unsigned int address_start, address_end;
732 static unsigned int maybe_allows_none_start, maybe_allows_none_end;
733 static unsigned int maybe_allows_reg_start, maybe_allows_reg_end;
734 static unsigned int maybe_allows_mem_start, maybe_allows_mem_end;
735
736 /* Convert NAME, which contains angle brackets and/or underscores, to
737 a string that can be used as part of a C identifier. The string
738 comes from the rtl_obstack. */
739 static const char *
740 mangle (const char *name)
741 {
742 for (; *name; name++)
743 switch (*name)
744 {
745 case '_': obstack_grow (rtl_obstack, "__", 2); break;
746 case '<': obstack_grow (rtl_obstack, "_l", 2); break;
747 case '>': obstack_grow (rtl_obstack, "_g", 2); break;
748 default: obstack_1grow (rtl_obstack, *name); break;
749 }
750
751 obstack_1grow (rtl_obstack, '\0');
752 return XOBFINISH (rtl_obstack, const char *);
753 }
754
755 /* Add one constraint, of any sort, to the tables. NAME is its name;
756 REGCLASS is the register class, if any; EXP is the expression to
757 test, if any; IS_MEMORY and IS_ADDRESS indicate memory and address
758 constraints, respectively; LOC is the .md file location.
759
760 Not all combinations of arguments are valid; most importantly, REGCLASS
761 is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
762 meaningful for constraints with EXP.
763
764 This function enforces all syntactic and semantic rules about what
765 constraints can be defined. */
766
767 static void
768 add_constraint (const char *name, const char *regclass,
769 rtx exp, bool is_memory, bool is_address,
770 file_location loc)
771 {
772 struct constraint_data *c, **iter, **slot;
773 const char *p;
774 bool need_mangled_name = false;
775 bool is_const_int;
776 bool is_const_dbl;
777 size_t namelen;
778
779 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
780 name = general_mem;
781
782 if (exp && validate_exp (exp, name, loc))
783 return;
784
785 for (p = name; *p; p++)
786 if (!ISALNUM (*p))
787 {
788 if (*p == '<' || *p == '>' || *p == '_')
789 need_mangled_name = true;
790 else
791 {
792 error_at (loc, "constraint name '%s' must be composed of letters,"
793 " digits, underscores, and angle brackets", name);
794 return;
795 }
796 }
797
798 if (strchr (generic_constraint_letters, name[0]))
799 {
800 if (name[1] == '\0')
801 error_at (loc, "constraint letter '%s' cannot be "
802 "redefined by the machine description", name);
803 else
804 error_at (loc, "constraint name '%s' cannot be defined by the machine"
805 " description, as it begins with '%c'", name, name[0]);
806 return;
807 }
808
809
810 namelen = strlen (name);
811 slot = &constraints_by_letter_table[(unsigned int)name[0]];
812 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
813 {
814 /* This causes slot to end up pointing to the
815 next_this_letter field of the last constraint with a name
816 of equal or greater length than the new constraint; hence
817 the new constraint will be inserted after all previous
818 constraints with names of the same length. */
819 if ((*iter)->namelen >= namelen)
820 slot = iter;
821
822 if (!strcmp ((*iter)->name, name))
823 {
824 error_at (loc, "redefinition of constraint '%s'", name);
825 message_at ((*iter)->loc, "previous definition is here");
826 return;
827 }
828 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
829 {
830 error_at (loc, "defining constraint '%s' here", name);
831 message_at ((*iter)->loc, "renders constraint '%s' "
832 "(defined here) a prefix", (*iter)->name);
833 return;
834 }
835 else if (!strncmp ((*iter)->name, name, namelen))
836 {
837 error_at (loc, "constraint '%s' is a prefix", name);
838 message_at ((*iter)->loc, "of constraint '%s' (defined here)",
839 (*iter)->name);
840 return;
841 }
842 }
843
844 is_const_int = strchr (const_int_constraints, name[0]) != 0;
845 is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
846
847 if (is_const_int || is_const_dbl)
848 {
849 enum rtx_code appropriate_code
850 = is_const_int ? CONST_INT : CONST_DOUBLE;
851
852 /* Consider relaxing this requirement in the future. */
853 if (regclass
854 || GET_CODE (exp) != AND
855 || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
856 || strcmp (XSTR (XEXP (exp, 0), 0),
857 GET_RTX_NAME (appropriate_code)))
858 {
859 if (name[1] == '\0')
860 error_at (loc, "constraint letter '%c' is reserved "
861 "for %s constraints", name[0],
862 GET_RTX_NAME (appropriate_code));
863 else
864 error_at (loc, "constraint names beginning with '%c' "
865 "(%s) are reserved for %s constraints",
866 name[0], name, GET_RTX_NAME (appropriate_code));
867 return;
868 }
869
870 if (is_memory)
871 {
872 if (name[1] == '\0')
873 error_at (loc, "constraint letter '%c' cannot be a "
874 "memory constraint", name[0]);
875 else
876 error_at (loc, "constraint name '%s' begins with '%c', "
877 "and therefore cannot be a memory constraint",
878 name, name[0]);
879 return;
880 }
881 else if (is_address)
882 {
883 if (name[1] == '\0')
884 error_at (loc, "constraint letter '%c' cannot be an "
885 "address constraint", name[0]);
886 else
887 error_at (loc, "constraint name '%s' begins with '%c', "
888 "and therefore cannot be an address constraint",
889 name, name[0]);
890 return;
891 }
892 }
893
894
895 c = XOBNEW (rtl_obstack, struct constraint_data);
896 c->name = name;
897 c->c_name = need_mangled_name ? mangle (name) : name;
898 c->loc = loc;
899 c->namelen = namelen;
900 c->regclass = regclass;
901 c->exp = exp;
902 c->is_register = regclass != 0;
903 c->is_const_int = is_const_int;
904 c->is_const_dbl = is_const_dbl;
905 c->is_extra = !(regclass || is_const_int || is_const_dbl);
906 c->is_memory = is_memory;
907 c->is_address = is_address;
908 c->maybe_allows_reg = true;
909 c->maybe_allows_mem = true;
910 if (exp)
911 {
912 char codes[NUM_RTX_CODE];
913 compute_test_codes (exp, loc, codes);
914 if (!codes[REG] && !codes[SUBREG])
915 c->maybe_allows_reg = false;
916 if (!codes[MEM])
917 c->maybe_allows_mem = false;
918 }
919 c->next_this_letter = *slot;
920 *slot = c;
921
922 /* Insert this constraint in the list of all constraints in textual
923 order. */
924 c->next_textual = 0;
925 *last_constraint_ptr = c;
926 last_constraint_ptr = &c->next_textual;
927
928 constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
929 have_register_constraints |= c->is_register;
930 have_const_int_constraints |= c->is_const_int;
931 have_extra_constraints |= c->is_extra;
932 have_memory_constraints |= c->is_memory;
933 have_address_constraints |= c->is_address;
934 num_constraints += 1;
935 }
936
937 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
938 DEFINE_ADDRESS_CONSTRAINT expression, C. */
939 static void
940 process_define_constraint (md_rtx_info *info)
941 {
942 add_constraint (XSTR (info->def, 0), 0, XEXP (info->def, 2),
943 GET_CODE (info->def) == DEFINE_MEMORY_CONSTRAINT,
944 GET_CODE (info->def) == DEFINE_ADDRESS_CONSTRAINT,
945 info->loc);
946 }
947
948 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
949 static void
950 process_define_register_constraint (md_rtx_info *info)
951 {
952 add_constraint (XSTR (info->def, 0), XSTR (info->def, 1),
953 0, false, false, info->loc);
954 }
955
956 /* Put the constraints into enum order. We want to keep constraints
957 of the same type together so that query functions can be simple
958 range checks. */
959 static void
960 choose_enum_order (void)
961 {
962 struct constraint_data *c;
963
964 enum_order = XNEWVEC (const constraint_data *, num_constraints);
965 unsigned int next = 0;
966
967 register_start = next;
968 FOR_ALL_CONSTRAINTS (c)
969 if (c->is_register)
970 enum_order[next++] = c;
971 register_end = next;
972
973 satisfied_start = next;
974
975 const_int_start = next;
976 FOR_ALL_CONSTRAINTS (c)
977 if (c->is_const_int)
978 enum_order[next++] = c;
979 const_int_end = next;
980
981 memory_start = next;
982 FOR_ALL_CONSTRAINTS (c)
983 if (c->is_memory)
984 enum_order[next++] = c;
985 memory_end = next;
986
987 address_start = next;
988 FOR_ALL_CONSTRAINTS (c)
989 if (c->is_address)
990 enum_order[next++] = c;
991 address_end = next;
992
993 maybe_allows_none_start = next;
994 FOR_ALL_CONSTRAINTS (c)
995 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
996 && !c->maybe_allows_reg && !c->maybe_allows_mem)
997 enum_order[next++] = c;
998 maybe_allows_none_end = next;
999
1000 maybe_allows_reg_start = next;
1001 FOR_ALL_CONSTRAINTS (c)
1002 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1003 && c->maybe_allows_reg && !c->maybe_allows_mem)
1004 enum_order[next++] = c;
1005 maybe_allows_reg_end = next;
1006
1007 maybe_allows_mem_start = next;
1008 FOR_ALL_CONSTRAINTS (c)
1009 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1010 && !c->maybe_allows_reg && c->maybe_allows_mem)
1011 enum_order[next++] = c;
1012 maybe_allows_mem_end = next;
1013
1014 FOR_ALL_CONSTRAINTS (c)
1015 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1016 && c->maybe_allows_reg && c->maybe_allows_mem)
1017 enum_order[next++] = c;
1018 gcc_assert (next == num_constraints);
1019 }
1020
1021 /* Write out an enumeration with one entry per machine-specific
1022 constraint. */
1023 static void
1024 write_enum_constraint_num (void)
1025 {
1026 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
1027 fputs ("enum constraint_num\n"
1028 "{\n"
1029 " CONSTRAINT__UNKNOWN = 0", stdout);
1030 for (unsigned int i = 0; i < num_constraints; ++i)
1031 printf (",\n CONSTRAINT_%s", enum_order[i]->c_name);
1032 puts (",\n CONSTRAINT__LIMIT\n};\n");
1033 }
1034
1035 /* Write out a function which looks at a string and determines what
1036 constraint name, if any, it begins with. */
1037 static void
1038 write_lookup_constraint_1 (void)
1039 {
1040 unsigned int i;
1041 puts ("enum constraint_num\n"
1042 "lookup_constraint_1 (const char *str)\n"
1043 "{\n"
1044 " switch (str[0])\n"
1045 " {");
1046
1047 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1048 {
1049 struct constraint_data *c = constraints_by_letter_table[i];
1050 if (!c)
1051 continue;
1052
1053 printf (" case '%c':\n", i);
1054 if (c->namelen == 1)
1055 printf (" return CONSTRAINT_%s;\n", c->c_name);
1056 else
1057 {
1058 do
1059 {
1060 printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
1061 " return CONSTRAINT_%s;\n",
1062 c->name + 1, (unsigned long int) c->namelen - 1,
1063 c->c_name);
1064 c = c->next_this_letter;
1065 }
1066 while (c);
1067 puts (" break;");
1068 }
1069 }
1070
1071 puts (" default: break;\n"
1072 " }\n"
1073 " return CONSTRAINT__UNKNOWN;\n"
1074 "}\n");
1075 }
1076
1077 /* Write out an array that maps single-letter characters to their
1078 constraints (if that fits in a character) or 255 if lookup_constraint_1
1079 must be called. */
1080 static void
1081 write_lookup_constraint_array (void)
1082 {
1083 unsigned int i;
1084 printf ("const unsigned char lookup_constraint_array[] = {\n ");
1085 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1086 {
1087 if (i != 0)
1088 printf (",\n ");
1089 struct constraint_data *c = constraints_by_letter_table[i];
1090 if (!c)
1091 printf ("CONSTRAINT__UNKNOWN");
1092 else if (c->namelen == 1)
1093 printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1094 else
1095 printf ("UCHAR_MAX");
1096 }
1097 printf ("\n};\n\n");
1098 }
1099
1100 /* Write out a function which looks at a string and determines what
1101 the constraint name length is. */
1102 static void
1103 write_insn_constraint_len (void)
1104 {
1105 unsigned int i;
1106
1107 puts ("static inline size_t\n"
1108 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
1109 "{\n"
1110 " switch (fc)\n"
1111 " {");
1112
1113 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1114 {
1115 struct constraint_data *c = constraints_by_letter_table[i];
1116
1117 if (!c
1118 || c->namelen == 1)
1119 continue;
1120
1121 /* Constraints with multiple characters should have the same
1122 length. */
1123 {
1124 struct constraint_data *c2 = c->next_this_letter;
1125 size_t len = c->namelen;
1126 while (c2)
1127 {
1128 if (c2->namelen != len)
1129 error ("Multi-letter constraints with first letter '%c' "
1130 "should have same length", i);
1131 c2 = c2->next_this_letter;
1132 }
1133 }
1134
1135 printf (" case '%c': return %lu;\n",
1136 i, (unsigned long int) c->namelen);
1137 }
1138
1139 puts (" default: break;\n"
1140 " }\n"
1141 " return 1;\n"
1142 "}\n");
1143 }
1144
1145 /* Write out the function which computes the register class corresponding
1146 to a register constraint. */
1147 static void
1148 write_reg_class_for_constraint_1 (void)
1149 {
1150 struct constraint_data *c;
1151
1152 puts ("enum reg_class\n"
1153 "reg_class_for_constraint_1 (enum constraint_num c)\n"
1154 "{\n"
1155 " switch (c)\n"
1156 " {");
1157
1158 FOR_ALL_CONSTRAINTS (c)
1159 if (c->is_register)
1160 printf (" case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1161
1162 puts (" default: break;\n"
1163 " }\n"
1164 " return NO_REGS;\n"
1165 "}\n");
1166 }
1167
1168 /* Write out the functions which compute whether a given value matches
1169 a given non-register constraint. */
1170 static void
1171 write_tm_constrs_h (void)
1172 {
1173 struct constraint_data *c;
1174
1175 printf ("\
1176 /* Generated automatically by the program '%s'\n\
1177 from the machine description file '%s'. */\n\n", progname, in_fname);
1178
1179 puts ("\
1180 #ifndef GCC_TM_CONSTRS_H\n\
1181 #define GCC_TM_CONSTRS_H\n");
1182
1183 FOR_ALL_CONSTRAINTS (c)
1184 if (!c->is_register)
1185 {
1186 bool needs_ival = needs_variable (c->exp, "ival");
1187 bool needs_hval = needs_variable (c->exp, "hval");
1188 bool needs_lval = needs_variable (c->exp, "lval");
1189 bool needs_rval = needs_variable (c->exp, "rval");
1190 bool needs_mode = (needs_variable (c->exp, "mode")
1191 || needs_hval || needs_lval || needs_rval);
1192 bool needs_op = (needs_variable (c->exp, "op")
1193 || needs_ival || needs_mode);
1194
1195 printf ("static inline bool\n"
1196 "satisfies_constraint_%s (rtx %s)\n"
1197 "{\n", c->c_name,
1198 needs_op ? "op" : "ARG_UNUSED (op)");
1199 if (needs_mode)
1200 puts (" machine_mode mode = GET_MODE (op);");
1201 if (needs_ival)
1202 puts (" HOST_WIDE_INT ival = 0;");
1203 if (needs_hval)
1204 puts (" HOST_WIDE_INT hval = 0;");
1205 if (needs_lval)
1206 puts (" unsigned HOST_WIDE_INT lval = 0;");
1207 if (needs_rval)
1208 puts (" const REAL_VALUE_TYPE *rval = 0;");
1209
1210 if (needs_ival)
1211 puts (" if (CONST_INT_P (op))\n"
1212 " ival = INTVAL (op);");
1213 #if TARGET_SUPPORTS_WIDE_INT
1214 if (needs_lval || needs_hval)
1215 error ("you can't use lval or hval");
1216 #else
1217 if (needs_hval)
1218 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1219 " hval = CONST_DOUBLE_HIGH (op);");
1220 if (needs_lval)
1221 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1222 " lval = CONST_DOUBLE_LOW (op);");
1223 #endif
1224 if (needs_rval)
1225 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1226 " rval = CONST_DOUBLE_REAL_VALUE (op);");
1227
1228 write_predicate_stmts (c->exp);
1229 fputs ("}\n", stdout);
1230 }
1231 puts ("#endif /* tm-constrs.h */");
1232 }
1233
1234 /* Write out the wrapper function, constraint_satisfied_p, that maps
1235 a CONSTRAINT_xxx constant to one of the predicate functions generated
1236 above. */
1237 static void
1238 write_constraint_satisfied_p_array (void)
1239 {
1240 if (satisfied_start == num_constraints)
1241 return;
1242
1243 printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n ");
1244 for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1245 {
1246 if (i != satisfied_start)
1247 printf (",\n ");
1248 printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1249 }
1250 printf ("\n};\n\n");
1251 }
1252
1253 /* Write out the function which computes whether a given value matches
1254 a given CONST_INT constraint. This doesn't just forward to
1255 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1256 static void
1257 write_insn_const_int_ok_for_constraint (void)
1258 {
1259 struct constraint_data *c;
1260
1261 puts ("bool\n"
1262 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1263 "enum constraint_num c)\n"
1264 "{\n"
1265 " switch (c)\n"
1266 " {");
1267
1268 FOR_ALL_CONSTRAINTS (c)
1269 if (c->is_const_int)
1270 {
1271 printf (" case CONSTRAINT_%s:\n return ", c->c_name);
1272 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1273 we know at this point that we have a const_int, so we need not
1274 bother with that part of the test. */
1275 write_predicate_expr (XEXP (c->exp, 1));
1276 fputs (";\n\n", stdout);
1277 }
1278
1279 puts (" default: break;\n"
1280 " }\n"
1281 " return false;\n"
1282 "}\n");
1283 }
1284 \f
1285 /* Write a definition for a function NAME that returns true if a given
1286 constraint_num is in the range [START, END). */
1287 static void
1288 write_range_function (const char *name, unsigned int start, unsigned int end)
1289 {
1290 printf ("static inline bool\n");
1291 if (start != end)
1292 printf ("%s (enum constraint_num c)\n"
1293 "{\n"
1294 " return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1295 "}\n\n",
1296 name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1297 else
1298 printf ("%s (enum constraint_num)\n"
1299 "{\n"
1300 " return false;\n"
1301 "}\n\n", name);
1302 }
1303
1304 /* Write a definition for insn_extra_constraint_allows_reg_mem function. */
1305 static void
1306 write_allows_reg_mem_function (void)
1307 {
1308 printf ("static inline void\n"
1309 "insn_extra_constraint_allows_reg_mem (enum constraint_num c,\n"
1310 "\t\t\t\t bool *allows_reg, bool *allows_mem)\n"
1311 "{\n");
1312 if (maybe_allows_none_start != maybe_allows_none_end)
1313 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1314 " return;\n",
1315 enum_order[maybe_allows_none_start]->c_name,
1316 enum_order[maybe_allows_none_end - 1]->c_name);
1317 if (maybe_allows_reg_start != maybe_allows_reg_end)
1318 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1319 " {\n"
1320 " *allows_reg = true;\n"
1321 " return;\n"
1322 " }\n",
1323 enum_order[maybe_allows_reg_start]->c_name,
1324 enum_order[maybe_allows_reg_end - 1]->c_name);
1325 if (maybe_allows_mem_start != maybe_allows_mem_end)
1326 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1327 " {\n"
1328 " *allows_mem = true;\n"
1329 " return;\n"
1330 " }\n",
1331 enum_order[maybe_allows_mem_start]->c_name,
1332 enum_order[maybe_allows_mem_end - 1]->c_name);
1333 printf (" (void) c;\n"
1334 " *allows_reg = true;\n"
1335 " *allows_mem = true;\n"
1336 "}\n\n");
1337 }
1338
1339 /* VEC is a list of key/value pairs, with the keys being lower bounds
1340 of a range. Output a decision tree that handles the keys covered by
1341 [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1342 INDENT is the number of spaces to indent the code. */
1343 static void
1344 print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1345 unsigned int start, unsigned int end, const char *fallback,
1346 unsigned int indent)
1347 {
1348 while (start < end)
1349 {
1350 unsigned int mid = (start + end) / 2;
1351 printf ("%*sif (c >= CONSTRAINT_%s)\n",
1352 indent, "", enum_order[vec[mid].first]->c_name);
1353 if (mid + 1 == end)
1354 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1355 else
1356 {
1357 printf ("%*s{\n", indent + 2, "");
1358 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1359 printf ("%*s}\n", indent + 2, "");
1360 }
1361 end = mid;
1362 }
1363 printf ("%*sreturn %s;\n", indent, "", fallback);
1364 }
1365
1366 /* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1367 an enumeration in portable C, so we have to condition all these
1368 prototypes on HAVE_MACHINE_MODES. */
1369 static void
1370 write_tm_preds_h (void)
1371 {
1372 struct pred_data *p;
1373
1374 printf ("\
1375 /* Generated automatically by the program '%s'\n\
1376 from the machine description file '%s'. */\n\n", progname, in_fname);
1377
1378 puts ("\
1379 #ifndef GCC_TM_PREDS_H\n\
1380 #define GCC_TM_PREDS_H\n\
1381 \n\
1382 #ifdef HAVE_MACHINE_MODES");
1383
1384 FOR_ALL_PREDICATES (p)
1385 printf ("extern int %s (rtx, machine_mode);\n", p->name);
1386
1387 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1388
1389 if (constraint_max_namelen > 0)
1390 {
1391 write_enum_constraint_num ();
1392 puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1393 "extern const unsigned char lookup_constraint_array[];\n"
1394 "\n"
1395 "/* Return the constraint at the beginning of P, or"
1396 " CONSTRAINT__UNKNOWN if it\n"
1397 " isn't recognized. */\n"
1398 "\n"
1399 "static inline enum constraint_num\n"
1400 "lookup_constraint (const char *p)\n"
1401 "{\n"
1402 " unsigned int index = lookup_constraint_array"
1403 "[(unsigned char) *p];\n"
1404 " return (index == UCHAR_MAX\n"
1405 " ? lookup_constraint_1 (p)\n"
1406 " : (enum constraint_num) index);\n"
1407 "}\n");
1408 if (satisfied_start == num_constraints)
1409 puts ("/* Return true if X satisfies constraint C. */\n"
1410 "\n"
1411 "static inline bool\n"
1412 "constraint_satisfied_p (rtx, enum constraint_num)\n"
1413 "{\n"
1414 " return false;\n"
1415 "}\n");
1416 else
1417 printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
1418 "\n"
1419 "/* Return true if X satisfies constraint C. */\n"
1420 "\n"
1421 "static inline bool\n"
1422 "constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1423 "{\n"
1424 " int i = (int) c - (int) CONSTRAINT_%s;\n"
1425 " return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
1426 "}\n"
1427 "\n",
1428 enum_order[satisfied_start]->name);
1429
1430 write_range_function ("insn_extra_register_constraint",
1431 register_start, register_end);
1432 write_range_function ("insn_extra_memory_constraint",
1433 memory_start, memory_end);
1434 write_range_function ("insn_extra_address_constraint",
1435 address_start, address_end);
1436 write_allows_reg_mem_function ();
1437
1438 if (constraint_max_namelen > 1)
1439 {
1440 write_insn_constraint_len ();
1441 puts ("#define CONSTRAINT_LEN(c_,s_) "
1442 "insn_constraint_len (c_,s_)\n");
1443 }
1444 else
1445 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1446 if (have_register_constraints)
1447 puts ("extern enum reg_class reg_class_for_constraint_1 "
1448 "(enum constraint_num);\n"
1449 "\n"
1450 "static inline enum reg_class\n"
1451 "reg_class_for_constraint (enum constraint_num c)\n"
1452 "{\n"
1453 " if (insn_extra_register_constraint (c))\n"
1454 " return reg_class_for_constraint_1 (c);\n"
1455 " return NO_REGS;\n"
1456 "}\n");
1457 else
1458 puts ("static inline enum reg_class\n"
1459 "reg_class_for_constraint (enum constraint_num)\n"
1460 "{\n"
1461 " return NO_REGS;\n"
1462 "}\n");
1463 if (have_const_int_constraints)
1464 puts ("extern bool insn_const_int_ok_for_constraint "
1465 "(HOST_WIDE_INT, enum constraint_num);\n"
1466 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1467 " insn_const_int_ok_for_constraint (v_, "
1468 "lookup_constraint (s_))\n");
1469 else
1470 puts ("static inline bool\n"
1471 "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1472 " enum constraint_num)\n"
1473 "{\n"
1474 " return false;\n"
1475 "}\n");
1476
1477 puts ("enum constraint_type\n"
1478 "{\n"
1479 " CT_REGISTER,\n"
1480 " CT_CONST_INT,\n"
1481 " CT_MEMORY,\n"
1482 " CT_ADDRESS,\n"
1483 " CT_FIXED_FORM\n"
1484 "};\n"
1485 "\n"
1486 "static inline enum constraint_type\n"
1487 "get_constraint_type (enum constraint_num c)\n"
1488 "{");
1489 auto_vec <std::pair <unsigned int, const char *>, 4> values;
1490 if (const_int_start != const_int_end)
1491 values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
1492 if (memory_start != memory_end)
1493 values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
1494 if (address_start != address_end)
1495 values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1496 if (address_end != num_constraints)
1497 values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1498 print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1499 puts ("}");
1500 }
1501
1502 puts ("#endif /* tm-preds.h */");
1503 }
1504
1505 /* Write insn-preds.c.
1506 N.B. the list of headers to include was copied from genrecog; it
1507 may not be ideal.
1508
1509 FUTURE: Write #line markers referring back to the machine
1510 description. (Can't practically do this now since we don't know
1511 the line number of the C block - just the line number of the enclosing
1512 expression.) */
1513 static void
1514 write_insn_preds_c (void)
1515 {
1516 struct pred_data *p;
1517
1518 printf ("\
1519 /* Generated automatically by the program '%s'\n\
1520 from the machine description file '%s'. */\n\n", progname, in_fname);
1521
1522 puts ("\
1523 #include \"config.h\"\n\
1524 #include \"system.h\"\n\
1525 #include \"coretypes.h\"\n\
1526 #include \"backend.h\"\n\
1527 #include \"predict.h\"\n\
1528 #include \"tree.h\"\n\
1529 #include \"rtl.h\"\n\
1530 #include \"alias.h\"\n\
1531 #include \"varasm.h\"\n\
1532 #include \"stor-layout.h\"\n\
1533 #include \"calls.h\"\n\
1534 #include \"tm_p.h\"\n\
1535 #include \"insn-config.h\"\n\
1536 #include \"recog.h\"\n\
1537 #include \"output.h\"\n\
1538 #include \"flags.h\"\n\
1539 #include \"df.h\"\n\
1540 #include \"resource.h\"\n\
1541 #include \"diagnostic-core.h\"\n\
1542 #include \"reload.h\"\n\
1543 #include \"regs.h\"\n\
1544 #include \"emit-rtl.h\"\n\
1545 #include \"tm-constrs.h\"\n");
1546
1547 FOR_ALL_PREDICATES (p)
1548 write_one_predicate_function (p);
1549
1550 if (constraint_max_namelen > 0)
1551 {
1552 write_lookup_constraint_1 ();
1553 write_lookup_constraint_array ();
1554 if (have_register_constraints)
1555 write_reg_class_for_constraint_1 ();
1556 write_constraint_satisfied_p_array ();
1557
1558 if (have_const_int_constraints)
1559 write_insn_const_int_ok_for_constraint ();
1560 }
1561 }
1562
1563 /* Argument parsing. */
1564 static bool gen_header;
1565 static bool gen_constrs;
1566
1567 static bool
1568 parse_option (const char *opt)
1569 {
1570 if (!strcmp (opt, "-h"))
1571 {
1572 gen_header = true;
1573 return 1;
1574 }
1575 else if (!strcmp (opt, "-c"))
1576 {
1577 gen_constrs = true;
1578 return 1;
1579 }
1580 else
1581 return 0;
1582 }
1583
1584 /* Master control. */
1585 int
1586 main (int argc, char **argv)
1587 {
1588 progname = argv[0];
1589 if (argc <= 1)
1590 fatal ("no input file name");
1591 if (!init_rtx_reader_args_cb (argc, argv, parse_option))
1592 return FATAL_EXIT_CODE;
1593
1594 md_rtx_info info;
1595 while (read_md_rtx (&info))
1596 switch (GET_CODE (info.def))
1597 {
1598 case DEFINE_PREDICATE:
1599 case DEFINE_SPECIAL_PREDICATE:
1600 process_define_predicate (&info);
1601 break;
1602
1603 case DEFINE_CONSTRAINT:
1604 case DEFINE_MEMORY_CONSTRAINT:
1605 case DEFINE_ADDRESS_CONSTRAINT:
1606 process_define_constraint (&info);
1607 break;
1608
1609 case DEFINE_REGISTER_CONSTRAINT:
1610 process_define_register_constraint (&info);
1611 break;
1612
1613 default:
1614 break;
1615 }
1616
1617 choose_enum_order ();
1618
1619 if (gen_header)
1620 write_tm_preds_h ();
1621 else if (gen_constrs)
1622 write_tm_constrs_h ();
1623 else
1624 write_insn_preds_c ();
1625
1626 if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1627 return FATAL_EXIT_CODE;
1628
1629 return SUCCESS_EXIT_CODE;
1630 }