Concretize gimple_cond_set_{lhs|rhs}
[official-gcc.git] / gcc / genpreds.c
blobb14a4ac104b331fb4688716b45cda63f2b04c045
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-2014 Free Software Foundation, Inc.
7 This file is part of GCC.
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.
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.
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/>. */
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"
33 static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
35 /* Given a predicate expression EXP, from form NAME at line LINENO,
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, int lineno)
42 if (exp == 0)
44 message_with_line (lineno, "%s: must give a predicate expression", name);
45 return true;
48 switch (GET_CODE (exp))
50 /* Ternary, binary, unary expressions: recurse into subexpressions. */
51 case IF_THEN_ELSE:
52 if (validate_exp (XEXP (exp, 2), name, lineno))
53 return true;
54 /* else fall through */
55 case AND:
56 case IOR:
57 if (validate_exp (XEXP (exp, 1), name, lineno))
58 return true;
59 /* else fall through */
60 case NOT:
61 return validate_exp (XEXP (exp, 0), name, lineno);
63 /* MATCH_CODE might have a syntax error in its path expression. */
64 case MATCH_CODE:
66 const char *p;
67 for (p = XSTR (exp, 1); *p; p++)
69 if (!ISDIGIT (*p) && !ISLOWER (*p))
71 error_with_line (lineno, "%s: invalid character in path "
72 "string '%s'", name, XSTR (exp, 1));
73 return true;
77 /* fall through */
79 /* These need no special checking. */
80 case MATCH_OPERAND:
81 case MATCH_TEST:
82 return false;
84 default:
85 error_with_line (lineno,
86 "%s: cannot use '%s' in a predicate expression",
87 name, GET_RTX_NAME (GET_CODE (exp)));
88 return true;
92 /* Predicates are defined with (define_predicate) or
93 (define_special_predicate) expressions in the machine description. */
94 static void
95 process_define_predicate (rtx defn, int lineno)
97 validate_exp (XEXP (defn, 1), XSTR (defn, 0), lineno);
100 /* Given a predicate, if it has an embedded C block, write the block
101 out as a static inline subroutine, and augment the RTL test with a
102 match_test that calls that subroutine. For instance,
104 (define_predicate "basereg_operand"
105 (match_operand 0 "register_operand")
107 if (GET_CODE (op) == SUBREG)
108 op = SUBREG_REG (op);
109 return REG_POINTER (op);
112 becomes
114 static inline int basereg_operand_1(rtx op, enum machine_mode mode)
116 if (GET_CODE (op) == SUBREG)
117 op = SUBREG_REG (op);
118 return REG_POINTER (op);
121 (define_predicate "basereg_operand"
122 (and (match_operand 0 "register_operand")
123 (match_test "basereg_operand_1 (op, mode)")))
125 The only wart is that there's no way to insist on a { } string in
126 an RTL template, so we have to handle "" strings. */
129 static void
130 write_predicate_subfunction (struct pred_data *p)
132 const char *match_test_str;
133 rtx match_test_exp, and_exp;
135 if (p->c_block[0] == '\0')
136 return;
138 /* Construct the function-call expression. */
139 obstack_grow (rtl_obstack, p->name, strlen (p->name));
140 obstack_grow (rtl_obstack, "_1 (op, mode)",
141 sizeof "_1 (op, mode)");
142 match_test_str = XOBFINISH (rtl_obstack, const char *);
144 /* Add the function-call expression to the complete expression to be
145 evaluated. */
146 match_test_exp = rtx_alloc (MATCH_TEST);
147 XSTR (match_test_exp, 0) = match_test_str;
149 and_exp = rtx_alloc (AND);
150 XEXP (and_exp, 0) = p->exp;
151 XEXP (and_exp, 1) = match_test_exp;
153 p->exp = and_exp;
155 printf ("static inline int\n"
156 "%s_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n",
157 p->name);
158 print_md_ptr_loc (p->c_block);
159 if (p->c_block[0] == '{')
160 fputs (p->c_block, stdout);
161 else
162 printf ("{\n %s\n}", p->c_block);
163 fputs ("\n\n", stdout);
166 /* Given a predicate expression EXP, from form NAME, determine whether
167 it refers to the variable given as VAR. */
168 static bool
169 needs_variable (rtx exp, const char *var)
171 switch (GET_CODE (exp))
173 /* Ternary, binary, unary expressions need a variable if
174 any of their subexpressions do. */
175 case IF_THEN_ELSE:
176 if (needs_variable (XEXP (exp, 2), var))
177 return true;
178 /* else fall through */
179 case AND:
180 case IOR:
181 if (needs_variable (XEXP (exp, 1), var))
182 return true;
183 /* else fall through */
184 case NOT:
185 return needs_variable (XEXP (exp, 0), var);
187 /* MATCH_CODE uses "op", but nothing else. */
188 case MATCH_CODE:
189 return !strcmp (var, "op");
191 /* MATCH_OPERAND uses "op" and may use "mode". */
192 case MATCH_OPERAND:
193 if (!strcmp (var, "op"))
194 return true;
195 if (!strcmp (var, "mode") && GET_MODE (exp) == VOIDmode)
196 return true;
197 return false;
199 /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
200 case MATCH_TEST:
202 const char *p = XSTR (exp, 0);
203 const char *q = strstr (p, var);
204 if (!q)
205 return false;
206 if (q != p && (ISALNUM (q[-1]) || q[-1] == '_'))
207 return false;
208 q += strlen (var);
209 if (ISALNUM (q[0]) || q[0] == '_')
210 return false;
212 return true;
214 default:
215 gcc_unreachable ();
219 /* Given an RTL expression EXP, find all subexpressions which we may
220 assume to perform mode tests. Normal MATCH_OPERAND does;
221 MATCH_CODE does if it applies to the whole expression and accepts
222 CONST_INT or CONST_DOUBLE; and we have to assume that MATCH_TEST
223 does not. These combine in almost-boolean fashion - the only
224 exception is that (not X) must be assumed not to perform a mode
225 test, whether or not X does.
227 The mark is the RTL /v flag, which is true for subexpressions which
228 do *not* perform mode tests.
230 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
231 static void
232 mark_mode_tests (rtx exp)
234 switch (GET_CODE (exp))
236 case MATCH_OPERAND:
238 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
239 if (!p)
240 error ("reference to undefined predicate '%s'", XSTR (exp, 1));
241 else if (p->special || GET_MODE (exp) != VOIDmode)
242 NO_MODE_TEST (exp) = 1;
244 break;
246 case MATCH_CODE:
247 if (XSTR (exp, 1)[0] != '\0'
248 || (!strstr (XSTR (exp, 0), "const_int")
249 && !strstr (XSTR (exp, 0), "const_double")))
250 NO_MODE_TEST (exp) = 1;
251 break;
253 case MATCH_TEST:
254 case NOT:
255 NO_MODE_TEST (exp) = 1;
256 break;
258 case AND:
259 mark_mode_tests (XEXP (exp, 0));
260 mark_mode_tests (XEXP (exp, 1));
262 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
263 && NO_MODE_TEST (XEXP (exp, 1)));
264 break;
266 case IOR:
267 mark_mode_tests (XEXP (exp, 0));
268 mark_mode_tests (XEXP (exp, 1));
270 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
271 || NO_MODE_TEST (XEXP (exp, 1)));
272 break;
274 case IF_THEN_ELSE:
275 /* A ? B : C does a mode test if (one of A and B) does a mode
276 test, and C does too. */
277 mark_mode_tests (XEXP (exp, 0));
278 mark_mode_tests (XEXP (exp, 1));
279 mark_mode_tests (XEXP (exp, 2));
281 NO_MODE_TEST (exp) = ((NO_MODE_TEST (XEXP (exp, 0))
282 && NO_MODE_TEST (XEXP (exp, 1)))
283 || NO_MODE_TEST (XEXP (exp, 2)));
284 break;
286 default:
287 gcc_unreachable ();
291 /* Determine whether the expression EXP is a MATCH_CODE that should
292 be written as a switch statement. */
293 static bool
294 generate_switch_p (rtx exp)
296 return GET_CODE (exp) == MATCH_CODE
297 && strchr (XSTR (exp, 0), ',');
300 /* Given a predicate, work out where in its RTL expression to add
301 tests for proper modes. Special predicates do not get any such
302 tests. We try to avoid adding tests when we don't have to; in
303 particular, other normal predicates can be counted on to do it for
304 us. */
306 static void
307 add_mode_tests (struct pred_data *p)
309 rtx match_test_exp, and_exp;
310 rtx *pos;
312 /* Don't touch special predicates. */
313 if (p->special)
314 return;
316 mark_mode_tests (p->exp);
318 /* If the whole expression already tests the mode, we're done. */
319 if (!NO_MODE_TEST (p->exp))
320 return;
322 match_test_exp = rtx_alloc (MATCH_TEST);
323 XSTR (match_test_exp, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
324 and_exp = rtx_alloc (AND);
325 XEXP (and_exp, 1) = match_test_exp;
327 /* It is always correct to rewrite p->exp as
329 (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
331 but there are a couple forms where we can do better. If the
332 top-level pattern is an IOR, and one of the two branches does test
333 the mode, we can wrap just the branch that doesn't. Likewise, if
334 we have an IF_THEN_ELSE, and one side of it tests the mode, we can
335 wrap just the side that doesn't. And, of course, we can repeat this
336 descent as many times as it works. */
338 pos = &p->exp;
339 for (;;)
341 rtx subexp = *pos;
343 switch (GET_CODE (subexp))
345 case AND:
346 /* The switch code generation in write_predicate_stmts prefers
347 rtx code tests to be at the top of the expression tree. So
348 push this AND down into the second operand of an existing
349 AND expression. */
350 if (generate_switch_p (XEXP (subexp, 0)))
351 pos = &XEXP (subexp, 1);
352 goto break_loop;
354 case IOR:
356 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
357 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
359 gcc_assert (test0 || test1);
361 if (test0 && test1)
362 goto break_loop;
363 pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
365 break;
367 case IF_THEN_ELSE:
369 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
370 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
371 int test2 = NO_MODE_TEST (XEXP (subexp, 2));
373 gcc_assert ((test0 && test1) || test2);
375 if (test0 && test1 && test2)
376 goto break_loop;
377 if (test0 && test1)
378 /* Must put it on the dependent clause, not the
379 controlling expression, or we change the meaning of
380 the test. */
381 pos = &XEXP (subexp, 1);
382 else
383 pos = &XEXP (subexp, 2);
385 break;
387 default:
388 goto break_loop;
391 break_loop:
392 XEXP (and_exp, 0) = *pos;
393 *pos = and_exp;
396 /* PATH is a string describing a path from the root of an RTL
397 expression to an inner subexpression to be tested. Output
398 code which computes the subexpression from the variable
399 holding the root of the expression. */
400 static void
401 write_extract_subexp (const char *path)
403 int len = strlen (path);
404 int i;
406 /* We first write out the operations (XEXP or XVECEXP) in reverse
407 order, then write "op", then the indices in forward order. */
408 for (i = len - 1; i >= 0; i--)
410 if (ISLOWER (path[i]))
411 fputs ("XVECEXP (", stdout);
412 else if (ISDIGIT (path[i]))
413 fputs ("XEXP (", stdout);
414 else
415 gcc_unreachable ();
418 fputs ("op", stdout);
420 for (i = 0; i < len; i++)
422 if (ISLOWER (path[i]))
423 printf (", 0, %d)", path[i] - 'a');
424 else if (ISDIGIT (path[i]))
425 printf (", %d)", path[i] - '0');
426 else
427 gcc_unreachable ();
431 /* CODES is a list of RTX codes. Write out an expression which
432 determines whether the operand has one of those codes. */
433 static void
434 write_match_code (const char *path, const char *codes)
436 const char *code;
438 while ((code = scan_comma_elt (&codes)) != 0)
440 fputs ("GET_CODE (", stdout);
441 write_extract_subexp (path);
442 fputs (") == ", stdout);
443 while (code < codes)
445 putchar (TOUPPER (*code));
446 code++;
449 if (*codes == ',')
450 fputs (" || ", stdout);
454 /* EXP is an RTL (sub)expression for a predicate. Recursively
455 descend the expression and write out an equivalent C expression. */
456 static void
457 write_predicate_expr (rtx exp)
459 switch (GET_CODE (exp))
461 case AND:
462 putchar ('(');
463 write_predicate_expr (XEXP (exp, 0));
464 fputs (") && (", stdout);
465 write_predicate_expr (XEXP (exp, 1));
466 putchar (')');
467 break;
469 case IOR:
470 putchar ('(');
471 write_predicate_expr (XEXP (exp, 0));
472 fputs (") || (", stdout);
473 write_predicate_expr (XEXP (exp, 1));
474 putchar (')');
475 break;
477 case NOT:
478 fputs ("!(", stdout);
479 write_predicate_expr (XEXP (exp, 0));
480 putchar (')');
481 break;
483 case IF_THEN_ELSE:
484 putchar ('(');
485 write_predicate_expr (XEXP (exp, 0));
486 fputs (") ? (", stdout);
487 write_predicate_expr (XEXP (exp, 1));
488 fputs (") : (", stdout);
489 write_predicate_expr (XEXP (exp, 2));
490 putchar (')');
491 break;
493 case MATCH_OPERAND:
494 if (GET_MODE (exp) == VOIDmode)
495 printf ("%s (op, mode)", XSTR (exp, 1));
496 else
497 printf ("%s (op, %smode)", XSTR (exp, 1), mode_name[GET_MODE (exp)]);
498 break;
500 case MATCH_CODE:
501 write_match_code (XSTR (exp, 1), XSTR (exp, 0));
502 break;
504 case MATCH_TEST:
505 print_c_condition (XSTR (exp, 0));
506 break;
508 default:
509 gcc_unreachable ();
513 /* Write the MATCH_CODE expression EXP as a switch statement. */
515 static void
516 write_match_code_switch (rtx exp)
518 const char *codes = XSTR (exp, 0);
519 const char *path = XSTR (exp, 1);
520 const char *code;
522 fputs (" switch (GET_CODE (", stdout);
523 write_extract_subexp (path);
524 fputs ("))\n {\n", stdout);
526 while ((code = scan_comma_elt (&codes)) != 0)
528 fputs (" case ", stdout);
529 while (code < codes)
531 putchar (TOUPPER (*code));
532 code++;
534 fputs (":\n", stdout);
538 /* Given a predicate expression EXP, write out a sequence of stmts
539 to evaluate it. This is similar to write_predicate_expr but can
540 generate efficient switch statements. */
542 static void
543 write_predicate_stmts (rtx exp)
545 switch (GET_CODE (exp))
547 case MATCH_CODE:
548 if (generate_switch_p (exp))
550 write_match_code_switch (exp);
551 puts (" return true;\n"
552 " default:\n"
553 " break;\n"
554 " }\n"
555 " return false;");
556 return;
558 break;
560 case AND:
561 if (generate_switch_p (XEXP (exp, 0)))
563 write_match_code_switch (XEXP (exp, 0));
564 puts (" break;\n"
565 " default:\n"
566 " return false;\n"
567 " }");
568 exp = XEXP (exp, 1);
570 break;
572 case IOR:
573 if (generate_switch_p (XEXP (exp, 0)))
575 write_match_code_switch (XEXP (exp, 0));
576 puts (" return true;\n"
577 " default:\n"
578 " break;\n"
579 " }");
580 exp = XEXP (exp, 1);
582 break;
584 case NOT:
585 if (generate_switch_p (XEXP (exp, 0)))
587 write_match_code_switch (XEXP (exp, 0));
588 puts (" return false;\n"
589 " default:\n"
590 " break;\n"
591 " }\n"
592 " return true;");
593 return;
595 break;
597 default:
598 break;
601 fputs (" return ",stdout);
602 write_predicate_expr (exp);
603 fputs (";\n", stdout);
606 /* Given a predicate, write out a complete C function to compute it. */
607 static void
608 write_one_predicate_function (struct pred_data *p)
610 if (!p->exp)
611 return;
613 write_predicate_subfunction (p);
614 add_mode_tests (p);
616 /* A normal predicate can legitimately not look at enum machine_mode
617 if it accepts only CONST_INTs and/or CONST_WIDE_INT and/or CONST_DOUBLEs. */
618 printf ("int\n%s (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
619 p->name);
620 write_predicate_stmts (p->exp);
621 fputs ("}\n\n", stdout);
624 /* Constraints fall into two categories: register constraints
625 (define_register_constraint), and others (define_constraint,
626 define_memory_constraint, define_address_constraint). We
627 work out automatically which of the various old-style macros
628 they correspond to, and produce appropriate code. They all
629 go in the same hash table so we can verify that there are no
630 duplicate names. */
632 /* All data from one constraint definition. */
633 struct constraint_data
635 struct constraint_data *next_this_letter;
636 struct constraint_data *next_textual;
637 const char *name;
638 const char *c_name; /* same as .name unless mangling is necessary */
639 size_t namelen;
640 const char *regclass; /* for register constraints */
641 rtx exp; /* for other constraints */
642 unsigned int lineno; /* line of definition */
643 unsigned int is_register : 1;
644 unsigned int is_const_int : 1;
645 unsigned int is_const_dbl : 1;
646 unsigned int is_extra : 1;
647 unsigned int is_memory : 1;
648 unsigned int is_address : 1;
651 /* Overview of all constraints beginning with a given letter. */
653 static struct constraint_data *
654 constraints_by_letter_table[1<<CHAR_BIT];
656 /* For looking up all the constraints in the order that they appeared
657 in the machine description. */
658 static struct constraint_data *first_constraint;
659 static struct constraint_data **last_constraint_ptr = &first_constraint;
661 #define FOR_ALL_CONSTRAINTS(iter_) \
662 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
664 /* Contraint letters that have a special meaning and that cannot be used
665 in define*_constraints. */
666 static const char generic_constraint_letters[] = "g";
668 /* Machine-independent code expects that constraints with these
669 (initial) letters will allow only (a subset of all) CONST_INTs. */
671 static const char const_int_constraints[] = "IJKLMNOP";
673 /* Machine-independent code expects that constraints with these
674 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
676 static const char const_dbl_constraints[] = "GH";
678 /* Summary data used to decide whether to output various functions and
679 macro definitions. */
680 static unsigned int constraint_max_namelen;
681 static bool have_register_constraints;
682 static bool have_memory_constraints;
683 static bool have_address_constraints;
684 static bool have_extra_constraints;
685 static bool have_const_int_constraints;
686 static unsigned int num_constraints;
688 static const constraint_data **enum_order;
689 static unsigned int register_start, register_end;
690 static unsigned int satisfied_start;
691 static unsigned int const_int_start, const_int_end;
692 static unsigned int memory_start, memory_end;
693 static unsigned int address_start, address_end;
695 /* Convert NAME, which contains angle brackets and/or underscores, to
696 a string that can be used as part of a C identifier. The string
697 comes from the rtl_obstack. */
698 static const char *
699 mangle (const char *name)
701 for (; *name; name++)
702 switch (*name)
704 case '_': obstack_grow (rtl_obstack, "__", 2); break;
705 case '<': obstack_grow (rtl_obstack, "_l", 2); break;
706 case '>': obstack_grow (rtl_obstack, "_g", 2); break;
707 default: obstack_1grow (rtl_obstack, *name); break;
710 obstack_1grow (rtl_obstack, '\0');
711 return XOBFINISH (rtl_obstack, const char *);
714 /* Add one constraint, of any sort, to the tables. NAME is its name;
715 REGCLASS is the register class, if any; EXP is the expression to
716 test, if any; IS_MEMORY and IS_ADDRESS indicate memory and address
717 constraints, respectively; LINENO is the line number from the MD reader.
718 Not all combinations of arguments are valid; most importantly, REGCLASS
719 is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
720 meaningful for constraints with EXP.
722 This function enforces all syntactic and semantic rules about what
723 constraints can be defined. */
725 static void
726 add_constraint (const char *name, const char *regclass,
727 rtx exp, bool is_memory, bool is_address,
728 int lineno)
730 struct constraint_data *c, **iter, **slot;
731 const char *p;
732 bool need_mangled_name = false;
733 bool is_const_int;
734 bool is_const_dbl;
735 size_t namelen;
737 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
738 name = general_mem;
740 if (exp && validate_exp (exp, name, lineno))
741 return;
743 for (p = name; *p; p++)
744 if (!ISALNUM (*p))
746 if (*p == '<' || *p == '>' || *p == '_')
747 need_mangled_name = true;
748 else
750 error_with_line (lineno,
751 "constraint name '%s' must be composed of "
752 "letters, digits, underscores, and "
753 "angle brackets", name);
754 return;
758 if (strchr (generic_constraint_letters, name[0]))
760 if (name[1] == '\0')
761 error_with_line (lineno, "constraint letter '%s' cannot be "
762 "redefined by the machine description", name);
763 else
764 error_with_line (lineno, "constraint name '%s' cannot be defined by "
765 "the machine description, as it begins with '%c'",
766 name, name[0]);
767 return;
771 namelen = strlen (name);
772 slot = &constraints_by_letter_table[(unsigned int)name[0]];
773 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
775 /* This causes slot to end up pointing to the
776 next_this_letter field of the last constraint with a name
777 of equal or greater length than the new constraint; hence
778 the new constraint will be inserted after all previous
779 constraints with names of the same length. */
780 if ((*iter)->namelen >= namelen)
781 slot = iter;
783 if (!strcmp ((*iter)->name, name))
785 error_with_line (lineno, "redefinition of constraint '%s'", name);
786 message_with_line ((*iter)->lineno, "previous definition is here");
787 return;
789 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
791 error_with_line (lineno, "defining constraint '%s' here", name);
792 message_with_line ((*iter)->lineno, "renders constraint '%s' "
793 "(defined here) a prefix", (*iter)->name);
794 return;
796 else if (!strncmp ((*iter)->name, name, namelen))
798 error_with_line (lineno, "constraint '%s' is a prefix", name);
799 message_with_line ((*iter)->lineno, "of constraint '%s' "
800 "(defined here)", (*iter)->name);
801 return;
805 is_const_int = strchr (const_int_constraints, name[0]) != 0;
806 is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
808 if (is_const_int || is_const_dbl)
810 enum rtx_code appropriate_code
811 = is_const_int ? CONST_INT : CONST_DOUBLE;
813 /* Consider relaxing this requirement in the future. */
814 if (regclass
815 || GET_CODE (exp) != AND
816 || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
817 || strcmp (XSTR (XEXP (exp, 0), 0),
818 GET_RTX_NAME (appropriate_code)))
820 if (name[1] == '\0')
821 error_with_line (lineno, "constraint letter '%c' is reserved "
822 "for %s constraints",
823 name[0], GET_RTX_NAME (appropriate_code));
824 else
825 error_with_line (lineno, "constraint names beginning with '%c' "
826 "(%s) are reserved for %s constraints",
827 name[0], name, GET_RTX_NAME (appropriate_code));
828 return;
831 if (is_memory)
833 if (name[1] == '\0')
834 error_with_line (lineno, "constraint letter '%c' cannot be a "
835 "memory constraint", name[0]);
836 else
837 error_with_line (lineno, "constraint name '%s' begins with '%c', "
838 "and therefore cannot be a memory constraint",
839 name, name[0]);
840 return;
842 else if (is_address)
844 if (name[1] == '\0')
845 error_with_line (lineno, "constraint letter '%c' cannot be a "
846 "memory constraint", name[0]);
847 else
848 error_with_line (lineno, "constraint name '%s' begins with '%c', "
849 "and therefore cannot be a memory constraint",
850 name, name[0]);
851 return;
856 c = XOBNEW (rtl_obstack, struct constraint_data);
857 c->name = name;
858 c->c_name = need_mangled_name ? mangle (name) : name;
859 c->lineno = lineno;
860 c->namelen = namelen;
861 c->regclass = regclass;
862 c->exp = exp;
863 c->is_register = regclass != 0;
864 c->is_const_int = is_const_int;
865 c->is_const_dbl = is_const_dbl;
866 c->is_extra = !(regclass || is_const_int || is_const_dbl);
867 c->is_memory = is_memory;
868 c->is_address = is_address;
870 c->next_this_letter = *slot;
871 *slot = c;
873 /* Insert this constraint in the list of all constraints in textual
874 order. */
875 c->next_textual = 0;
876 *last_constraint_ptr = c;
877 last_constraint_ptr = &c->next_textual;
879 constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
880 have_register_constraints |= c->is_register;
881 have_const_int_constraints |= c->is_const_int;
882 have_extra_constraints |= c->is_extra;
883 have_memory_constraints |= c->is_memory;
884 have_address_constraints |= c->is_address;
885 num_constraints += 1;
888 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
889 DEFINE_ADDRESS_CONSTRAINT expression, C. */
890 static void
891 process_define_constraint (rtx c, int lineno)
893 add_constraint (XSTR (c, 0), 0, XEXP (c, 2),
894 GET_CODE (c) == DEFINE_MEMORY_CONSTRAINT,
895 GET_CODE (c) == DEFINE_ADDRESS_CONSTRAINT,
896 lineno);
899 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
900 static void
901 process_define_register_constraint (rtx c, int lineno)
903 add_constraint (XSTR (c, 0), XSTR (c, 1), 0, false, false, lineno);
906 /* Put the constraints into enum order. We want to keep constraints
907 of the same type together so that query functions can be simple
908 range checks. */
909 static void
910 choose_enum_order (void)
912 struct constraint_data *c;
914 enum_order = XNEWVEC (const constraint_data *, num_constraints);
915 unsigned int next = 0;
917 register_start = next;
918 FOR_ALL_CONSTRAINTS (c)
919 if (c->is_register)
920 enum_order[next++] = c;
921 register_end = next;
923 satisfied_start = next;
925 const_int_start = next;
926 FOR_ALL_CONSTRAINTS (c)
927 if (c->is_const_int)
928 enum_order[next++] = c;
929 const_int_end = next;
931 memory_start = next;
932 FOR_ALL_CONSTRAINTS (c)
933 if (c->is_memory)
934 enum_order[next++] = c;
935 memory_end = next;
937 address_start = next;
938 FOR_ALL_CONSTRAINTS (c)
939 if (c->is_address)
940 enum_order[next++] = c;
941 address_end = next;
943 FOR_ALL_CONSTRAINTS (c)
944 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address)
945 enum_order[next++] = c;
946 gcc_assert (next == num_constraints);
949 /* Write out an enumeration with one entry per machine-specific
950 constraint. */
951 static void
952 write_enum_constraint_num (void)
954 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
955 fputs ("enum constraint_num\n"
956 "{\n"
957 " CONSTRAINT__UNKNOWN = 0", stdout);
958 for (unsigned int i = 0; i < num_constraints; ++i)
959 printf (",\n CONSTRAINT_%s", enum_order[i]->c_name);
960 puts (",\n CONSTRAINT__LIMIT\n};\n");
963 /* Write out a function which looks at a string and determines what
964 constraint name, if any, it begins with. */
965 static void
966 write_lookup_constraint_1 (void)
968 unsigned int i;
969 puts ("enum constraint_num\n"
970 "lookup_constraint_1 (const char *str)\n"
971 "{\n"
972 " switch (str[0])\n"
973 " {");
975 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
977 struct constraint_data *c = constraints_by_letter_table[i];
978 if (!c)
979 continue;
981 printf (" case '%c':\n", i);
982 if (c->namelen == 1)
983 printf (" return CONSTRAINT_%s;\n", c->c_name);
984 else
988 printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
989 " return CONSTRAINT_%s;\n",
990 c->name + 1, (unsigned long int) c->namelen - 1,
991 c->c_name);
992 c = c->next_this_letter;
994 while (c);
995 puts (" break;");
999 puts (" default: break;\n"
1000 " }\n"
1001 " return CONSTRAINT__UNKNOWN;\n"
1002 "}\n");
1005 /* Write out an array that maps single-letter characters to their
1006 constraints (if that fits in a character) or 255 if lookup_constraint_1
1007 must be called. */
1008 static void
1009 write_lookup_constraint_array (void)
1011 unsigned int i;
1012 printf ("const unsigned char lookup_constraint_array[] = {\n ");
1013 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1015 if (i != 0)
1016 printf (",\n ");
1017 struct constraint_data *c = constraints_by_letter_table[i];
1018 if (!c)
1019 printf ("CONSTRAINT__UNKNOWN");
1020 else if (c->namelen == 1)
1021 printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1022 else
1023 printf ("UCHAR_MAX");
1025 printf ("\n};\n\n");
1028 /* Write out a function which looks at a string and determines what
1029 the constraint name length is. */
1030 static void
1031 write_insn_constraint_len (void)
1033 unsigned int i;
1035 puts ("static inline size_t\n"
1036 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
1037 "{\n"
1038 " switch (fc)\n"
1039 " {");
1041 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1043 struct constraint_data *c = constraints_by_letter_table[i];
1045 if (!c
1046 || c->namelen == 1)
1047 continue;
1049 /* Constraints with multiple characters should have the same
1050 length. */
1052 struct constraint_data *c2 = c->next_this_letter;
1053 size_t len = c->namelen;
1054 while (c2)
1056 if (c2->namelen != len)
1057 error ("Multi-letter constraints with first letter '%c' "
1058 "should have same length", i);
1059 c2 = c2->next_this_letter;
1063 printf (" case '%c': return %lu;\n",
1064 i, (unsigned long int) c->namelen);
1067 puts (" default: break;\n"
1068 " }\n"
1069 " return 1;\n"
1070 "}\n");
1073 /* Write out the function which computes the register class corresponding
1074 to a register constraint. */
1075 static void
1076 write_reg_class_for_constraint_1 (void)
1078 struct constraint_data *c;
1080 puts ("enum reg_class\n"
1081 "reg_class_for_constraint_1 (enum constraint_num c)\n"
1082 "{\n"
1083 " switch (c)\n"
1084 " {");
1086 FOR_ALL_CONSTRAINTS (c)
1087 if (c->is_register)
1088 printf (" case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1090 puts (" default: break;\n"
1091 " }\n"
1092 " return NO_REGS;\n"
1093 "}\n");
1096 /* Write out the functions which compute whether a given value matches
1097 a given non-register constraint. */
1098 static void
1099 write_tm_constrs_h (void)
1101 struct constraint_data *c;
1103 printf ("\
1104 /* Generated automatically by the program '%s'\n\
1105 from the machine description file '%s'. */\n\n", progname, in_fname);
1107 puts ("\
1108 #ifndef GCC_TM_CONSTRS_H\n\
1109 #define GCC_TM_CONSTRS_H\n");
1111 FOR_ALL_CONSTRAINTS (c)
1112 if (!c->is_register)
1114 bool needs_ival = needs_variable (c->exp, "ival");
1115 bool needs_hval = needs_variable (c->exp, "hval");
1116 bool needs_lval = needs_variable (c->exp, "lval");
1117 bool needs_rval = needs_variable (c->exp, "rval");
1118 bool needs_mode = (needs_variable (c->exp, "mode")
1119 || needs_hval || needs_lval || needs_rval);
1120 bool needs_op = (needs_variable (c->exp, "op")
1121 || needs_ival || needs_mode);
1123 printf ("static inline bool\n"
1124 "satisfies_constraint_%s (rtx %s)\n"
1125 "{\n", c->c_name,
1126 needs_op ? "op" : "ARG_UNUSED (op)");
1127 if (needs_mode)
1128 puts (" enum machine_mode mode = GET_MODE (op);");
1129 if (needs_ival)
1130 puts (" HOST_WIDE_INT ival = 0;");
1131 if (needs_hval)
1132 puts (" HOST_WIDE_INT hval = 0;");
1133 if (needs_lval)
1134 puts (" unsigned HOST_WIDE_INT lval = 0;");
1135 if (needs_rval)
1136 puts (" const REAL_VALUE_TYPE *rval = 0;");
1138 if (needs_ival)
1139 puts (" if (CONST_INT_P (op))\n"
1140 " ival = INTVAL (op);");
1141 #if TARGET_SUPPORTS_WIDE_INT
1142 if (needs_lval || needs_hval)
1143 error ("you can't use lval or hval");
1144 #else
1145 if (needs_hval)
1146 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1147 " hval = CONST_DOUBLE_HIGH (op);");
1148 if (needs_lval)
1149 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1150 " lval = CONST_DOUBLE_LOW (op);");
1151 #endif
1152 if (needs_rval)
1153 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1154 " rval = CONST_DOUBLE_REAL_VALUE (op);");
1156 write_predicate_stmts (c->exp);
1157 fputs ("}\n", stdout);
1159 puts ("#endif /* tm-constrs.h */");
1162 /* Write out the wrapper function, constraint_satisfied_p, that maps
1163 a CONSTRAINT_xxx constant to one of the predicate functions generated
1164 above. */
1165 static void
1166 write_constraint_satisfied_p_array (void)
1168 if (satisfied_start == num_constraints)
1169 return;
1171 printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n ");
1172 for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1174 if (i != satisfied_start)
1175 printf (",\n ");
1176 printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1178 printf ("\n};\n\n");
1181 /* Write out the function which computes whether a given value matches
1182 a given CONST_INT constraint. This doesn't just forward to
1183 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1184 static void
1185 write_insn_const_int_ok_for_constraint (void)
1187 struct constraint_data *c;
1189 puts ("bool\n"
1190 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1191 "enum constraint_num c)\n"
1192 "{\n"
1193 " switch (c)\n"
1194 " {");
1196 FOR_ALL_CONSTRAINTS (c)
1197 if (c->is_const_int)
1199 printf (" case CONSTRAINT_%s:\n return ", c->c_name);
1200 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1201 we know at this point that we have a const_int, so we need not
1202 bother with that part of the test. */
1203 write_predicate_expr (XEXP (c->exp, 1));
1204 fputs (";\n\n", stdout);
1207 puts (" default: break;\n"
1208 " }\n"
1209 " return false;\n"
1210 "}\n");
1213 /* Write a definition for a function NAME that returns true if a given
1214 constraint_num is in the range [START, END). */
1215 static void
1216 write_range_function (const char *name, unsigned int start, unsigned int end)
1218 printf ("static inline bool\n");
1219 if (start != end)
1220 printf ("%s (enum constraint_num c)\n"
1221 "{\n"
1222 " return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1223 "}\n\n",
1224 name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1225 else
1226 printf ("%s (enum constraint_num)\n"
1227 "{\n"
1228 " return false;\n"
1229 "}\n\n", name);
1232 /* VEC is a list of key/value pairs, with the keys being lower bounds
1233 of a range. Output a decision tree that handles the keys covered by
1234 [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1235 INDENT is the number of spaces to indent the code. */
1236 static void
1237 print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1238 unsigned int start, unsigned int end, const char *fallback,
1239 unsigned int indent)
1241 while (start < end)
1243 unsigned int mid = (start + end) / 2;
1244 printf ("%*sif (c >= CONSTRAINT_%s)\n",
1245 indent, "", enum_order[vec[mid].first]->c_name);
1246 if (mid + 1 == end)
1247 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1248 else
1250 printf ("%*s{\n", indent + 2, "");
1251 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1252 printf ("%*s}\n", indent + 2, "");
1254 end = mid;
1256 printf ("%*sreturn %s;\n", indent, "", fallback);
1259 /* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1260 an enumeration in portable C, so we have to condition all these
1261 prototypes on HAVE_MACHINE_MODES. */
1262 static void
1263 write_tm_preds_h (void)
1265 struct pred_data *p;
1267 printf ("\
1268 /* Generated automatically by the program '%s'\n\
1269 from the machine description file '%s'. */\n\n", progname, in_fname);
1271 puts ("\
1272 #ifndef GCC_TM_PREDS_H\n\
1273 #define GCC_TM_PREDS_H\n\
1275 #ifdef HAVE_MACHINE_MODES");
1277 FOR_ALL_PREDICATES (p)
1278 printf ("extern int %s (rtx, enum machine_mode);\n", p->name);
1280 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1282 if (constraint_max_namelen > 0)
1284 write_enum_constraint_num ();
1285 puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1286 "extern const unsigned char lookup_constraint_array[];\n"
1287 "\n"
1288 "/* Return the constraint at the beginning of P, or"
1289 " CONSTRAINT__UNKNOWN if it\n"
1290 " isn't recognized. */\n"
1291 "\n"
1292 "static inline enum constraint_num\n"
1293 "lookup_constraint (const char *p)\n"
1294 "{\n"
1295 " unsigned int index = lookup_constraint_array"
1296 "[(unsigned char) *p];\n"
1297 " return (index == UCHAR_MAX\n"
1298 " ? lookup_constraint_1 (p)\n"
1299 " : (enum constraint_num) index);\n"
1300 "}\n");
1301 if (satisfied_start == num_constraints)
1302 puts ("/* Return true if X satisfies constraint C. */\n"
1303 "\n"
1304 "static inline bool\n"
1305 "constraint_satisfied_p (rtx, enum constraint_num)\n"
1306 "{\n"
1307 " return false;\n"
1308 "}\n");
1309 else
1310 printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
1311 "\n"
1312 "/* Return true if X satisfies constraint C. */\n"
1313 "\n"
1314 "static inline bool\n"
1315 "constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1316 "{\n"
1317 " int i = (int) c - (int) CONSTRAINT_%s;\n"
1318 " return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
1319 "}\n"
1320 "\n",
1321 enum_order[satisfied_start]->name);
1323 write_range_function ("insn_extra_register_constraint",
1324 register_start, register_end);
1325 write_range_function ("insn_extra_memory_constraint",
1326 memory_start, memory_end);
1327 write_range_function ("insn_extra_address_constraint",
1328 address_start, address_end);
1330 if (constraint_max_namelen > 1)
1332 write_insn_constraint_len ();
1333 puts ("#define CONSTRAINT_LEN(c_,s_) "
1334 "insn_constraint_len (c_,s_)\n");
1336 else
1337 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1338 if (have_register_constraints)
1339 puts ("extern enum reg_class reg_class_for_constraint_1 "
1340 "(enum constraint_num);\n"
1341 "\n"
1342 "static inline enum reg_class\n"
1343 "reg_class_for_constraint (enum constraint_num c)\n"
1344 "{\n"
1345 " if (insn_extra_register_constraint (c))\n"
1346 " return reg_class_for_constraint_1 (c);\n"
1347 " return NO_REGS;\n"
1348 "}\n");
1349 else
1350 puts ("static inline enum reg_class\n"
1351 "reg_class_for_constraint (enum constraint_num)\n"
1352 "{\n"
1353 " return NO_REGS;\n"
1354 "}\n");
1355 if (have_const_int_constraints)
1356 puts ("extern bool insn_const_int_ok_for_constraint "
1357 "(HOST_WIDE_INT, enum constraint_num);\n"
1358 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1359 " insn_const_int_ok_for_constraint (v_, "
1360 "lookup_constraint (s_))\n");
1361 else
1362 puts ("static inline bool\n"
1363 "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1364 " enum constraint_num)\n"
1365 "{\n"
1366 " return false;\n"
1367 "}\n");
1369 puts ("enum constraint_type\n"
1370 "{\n"
1371 " CT_REGISTER,\n"
1372 " CT_CONST_INT,\n"
1373 " CT_MEMORY,\n"
1374 " CT_ADDRESS,\n"
1375 " CT_FIXED_FORM\n"
1376 "};\n"
1377 "\n"
1378 "static inline enum constraint_type\n"
1379 "get_constraint_type (enum constraint_num c)\n"
1380 "{");
1381 auto_vec <std::pair <unsigned int, const char *>, 4> values;
1382 if (const_int_start != const_int_end)
1383 values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
1384 if (memory_start != memory_end)
1385 values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
1386 if (address_start != address_end)
1387 values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1388 if (address_end != num_constraints)
1389 values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1390 print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1391 puts ("}");
1394 puts ("#endif /* tm-preds.h */");
1397 /* Write insn-preds.c.
1398 N.B. the list of headers to include was copied from genrecog; it
1399 may not be ideal.
1401 FUTURE: Write #line markers referring back to the machine
1402 description. (Can't practically do this now since we don't know
1403 the line number of the C block - just the line number of the enclosing
1404 expression.) */
1405 static void
1406 write_insn_preds_c (void)
1408 struct pred_data *p;
1410 printf ("\
1411 /* Generated automatically by the program '%s'\n\
1412 from the machine description file '%s'. */\n\n", progname, in_fname);
1414 puts ("\
1415 #include \"config.h\"\n\
1416 #include \"system.h\"\n\
1417 #include \"coretypes.h\"\n\
1418 #include \"tm.h\"\n\
1419 #include \"rtl.h\"\n\
1420 #include \"tree.h\"\n\
1421 #include \"varasm.h\"\n\
1422 #include \"stor-layout.h\"\n\
1423 #include \"calls.h\"\n\
1424 #include \"tm_p.h\"\n\
1425 #include \"function.h\"\n\
1426 #include \"insn-config.h\"\n\
1427 #include \"recog.h\"\n\
1428 #include \"output.h\"\n\
1429 #include \"flags.h\"\n\
1430 #include \"hard-reg-set.h\"\n\
1431 #include \"resource.h\"\n\
1432 #include \"diagnostic-core.h\"\n\
1433 #include \"reload.h\"\n\
1434 #include \"regs.h\"\n\
1435 #include \"tm-constrs.h\"\n");
1437 FOR_ALL_PREDICATES (p)
1438 write_one_predicate_function (p);
1440 if (constraint_max_namelen > 0)
1442 write_lookup_constraint_1 ();
1443 write_lookup_constraint_array ();
1444 if (have_register_constraints)
1445 write_reg_class_for_constraint_1 ();
1446 write_constraint_satisfied_p_array ();
1448 if (have_const_int_constraints)
1449 write_insn_const_int_ok_for_constraint ();
1453 /* Argument parsing. */
1454 static bool gen_header;
1455 static bool gen_constrs;
1457 static bool
1458 parse_option (const char *opt)
1460 if (!strcmp (opt, "-h"))
1462 gen_header = true;
1463 return 1;
1465 else if (!strcmp (opt, "-c"))
1467 gen_constrs = true;
1468 return 1;
1470 else
1471 return 0;
1474 /* Master control. */
1476 main (int argc, char **argv)
1478 rtx defn;
1479 int pattern_lineno, next_insn_code = 0;
1481 progname = argv[0];
1482 if (argc <= 1)
1483 fatal ("no input file name");
1484 if (!init_rtx_reader_args_cb (argc, argv, parse_option))
1485 return FATAL_EXIT_CODE;
1487 while ((defn = read_md_rtx (&pattern_lineno, &next_insn_code)) != 0)
1488 switch (GET_CODE (defn))
1490 case DEFINE_PREDICATE:
1491 case DEFINE_SPECIAL_PREDICATE:
1492 process_define_predicate (defn, pattern_lineno);
1493 break;
1495 case DEFINE_CONSTRAINT:
1496 case DEFINE_MEMORY_CONSTRAINT:
1497 case DEFINE_ADDRESS_CONSTRAINT:
1498 process_define_constraint (defn, pattern_lineno);
1499 break;
1501 case DEFINE_REGISTER_CONSTRAINT:
1502 process_define_register_constraint (defn, pattern_lineno);
1503 break;
1505 default:
1506 break;
1509 choose_enum_order ();
1511 if (gen_header)
1512 write_tm_preds_h ();
1513 else if (gen_constrs)
1514 write_tm_constrs_h ();
1515 else
1516 write_insn_preds_c ();
1518 if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1519 return FATAL_EXIT_CODE;
1521 return SUCCESS_EXIT_CODE;