1 /* Generate from machine description:
2 - prototype declarations for operand predicates (tm-preds.h)
3 - function definitions of operand predicates, if defined new-style
5 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
6 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
32 #include "gensupport.h"
34 /* Given a predicate expression EXP, from form NAME at line LINENO,
35 verify that it does not contain any RTL constructs which are not
36 valid in predicate definitions. Returns true if EXP is
37 INvalid; issues error messages, caller need not. */
39 validate_exp (rtx exp
, const char *name
, int lineno
)
43 message_with_line (lineno
, "%s: must give a predicate expression", name
);
47 switch (GET_CODE (exp
))
49 /* Ternary, binary, unary expressions: recurse into subexpressions. */
51 if (validate_exp (XEXP (exp
, 2), name
, lineno
))
53 /* else fall through */
56 if (validate_exp (XEXP (exp
, 1), name
, lineno
))
58 /* else fall through */
60 return validate_exp (XEXP (exp
, 0), name
, lineno
);
62 /* MATCH_CODE might have a syntax error in its path expression. */
66 for (p
= XSTR (exp
, 1); *p
; p
++)
68 if (!ISDIGIT (*p
) && !ISLOWER (*p
))
70 error_with_line (lineno
, "%s: invalid character in path "
71 "string '%s'", name
, XSTR (exp
, 1));
78 /* These need no special checking. */
84 error_with_line (lineno
,
85 "%s: cannot use '%s' in a predicate expression",
86 name
, GET_RTX_NAME (GET_CODE (exp
)));
91 /* Predicates are defined with (define_predicate) or
92 (define_special_predicate) expressions in the machine description. */
94 process_define_predicate (rtx defn
, int lineno
)
96 validate_exp (XEXP (defn
, 1), XSTR (defn
, 0), lineno
);
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,
103 (define_predicate "basereg_operand"
104 (match_operand 0 "register_operand")
106 if (GET_CODE (op) == SUBREG)
107 op = SUBREG_REG (op);
108 return REG_POINTER (op);
113 static inline int basereg_operand_1(rtx op, enum machine_mode mode)
115 if (GET_CODE (op) == SUBREG)
116 op = SUBREG_REG (op);
117 return REG_POINTER (op);
120 (define_predicate "basereg_operand"
121 (and (match_operand 0 "register_operand")
122 (match_test "basereg_operand_1 (op, mode)")))
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. */
129 write_predicate_subfunction (struct pred_data
*p
)
131 const char *match_test_str
;
132 rtx match_test_exp
, and_exp
;
134 if (p
->c_block
[0] == '\0')
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 *);
143 /* Add the function-call expression to the complete expression to be
145 match_test_exp
= rtx_alloc (MATCH_TEST
);
146 XSTR (match_test_exp
, 0) = match_test_str
;
148 and_exp
= rtx_alloc (AND
);
149 XEXP (and_exp
, 0) = p
->exp
;
150 XEXP (and_exp
, 1) = match_test_exp
;
154 printf ("static inline int\n"
155 "%s_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n",
157 print_md_ptr_loc (p
->c_block
);
158 if (p
->c_block
[0] == '{')
159 fputs (p
->c_block
, stdout
);
161 printf ("{\n %s\n}", p
->c_block
);
162 fputs ("\n\n", stdout
);
165 /* Given a predicate expression EXP, from form NAME, determine whether
166 it refers to the variable given as VAR. */
168 needs_variable (rtx exp
, const char *var
)
170 switch (GET_CODE (exp
))
172 /* Ternary, binary, unary expressions need a variable if
173 any of their subexpressions do. */
175 if (needs_variable (XEXP (exp
, 2), var
))
177 /* else fall through */
180 if (needs_variable (XEXP (exp
, 1), var
))
182 /* else fall through */
184 return needs_variable (XEXP (exp
, 0), var
);
186 /* MATCH_CODE uses "op", but nothing else. */
188 return !strcmp (var
, "op");
190 /* MATCH_OPERAND uses "op" and may use "mode". */
192 if (!strcmp (var
, "op"))
194 if (!strcmp (var
, "mode") && GET_MODE (exp
) == VOIDmode
)
198 /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
201 const char *p
= XSTR (exp
, 0);
202 const char *q
= strstr (p
, var
);
205 if (q
!= p
&& (ISALNUM (q
[-1]) || q
[-1] == '_'))
208 if (ISALNUM (q
[0]) || q
[0] == '_')
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 does if it applies to the whole expression and accepts
221 CONST_INT or CONST_DOUBLE; and we have to assume that MATCH_TEST
222 does not. These combine in almost-boolean fashion - the only
223 exception is that (not X) must be assumed not to perform a mode
224 test, whether or not X does.
226 The mark is the RTL /v flag, which is true for subexpressions which
227 do *not* perform mode tests.
229 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
231 mark_mode_tests (rtx exp
)
233 switch (GET_CODE (exp
))
237 struct pred_data
*p
= lookup_predicate (XSTR (exp
, 1));
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;
246 if (XSTR (exp
, 1)[0] != '\0'
247 || (!strstr (XSTR (exp
, 0), "const_int")
248 && !strstr (XSTR (exp
, 0), "const_double")))
249 NO_MODE_TEST (exp
) = 1;
254 NO_MODE_TEST (exp
) = 1;
258 mark_mode_tests (XEXP (exp
, 0));
259 mark_mode_tests (XEXP (exp
, 1));
261 NO_MODE_TEST (exp
) = (NO_MODE_TEST (XEXP (exp
, 0))
262 && NO_MODE_TEST (XEXP (exp
, 1)));
266 mark_mode_tests (XEXP (exp
, 0));
267 mark_mode_tests (XEXP (exp
, 1));
269 NO_MODE_TEST (exp
) = (NO_MODE_TEST (XEXP (exp
, 0))
270 || NO_MODE_TEST (XEXP (exp
, 1)));
274 /* A ? B : C does a mode test if (one of A and B) does a mode
275 test, and C does too. */
276 mark_mode_tests (XEXP (exp
, 0));
277 mark_mode_tests (XEXP (exp
, 1));
278 mark_mode_tests (XEXP (exp
, 2));
280 NO_MODE_TEST (exp
) = ((NO_MODE_TEST (XEXP (exp
, 0))
281 && NO_MODE_TEST (XEXP (exp
, 1)))
282 || NO_MODE_TEST (XEXP (exp
, 2)));
290 /* Determine whether the expression EXP is a MATCH_CODE that should
291 be written as a switch statement. */
293 generate_switch_p (rtx exp
)
295 return GET_CODE (exp
) == MATCH_CODE
296 && strchr (XSTR (exp
, 0), ',');
299 /* Given a predicate, work out where in its RTL expression to add
300 tests for proper modes. Special predicates do not get any such
301 tests. We try to avoid adding tests when we don't have to; in
302 particular, other normal predicates can be counted on to do it for
306 add_mode_tests (struct pred_data
*p
)
308 rtx match_test_exp
, and_exp
;
311 /* Don't touch special predicates. */
315 mark_mode_tests (p
->exp
);
317 /* If the whole expression already tests the mode, we're done. */
318 if (!NO_MODE_TEST (p
->exp
))
321 match_test_exp
= rtx_alloc (MATCH_TEST
);
322 XSTR (match_test_exp
, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
323 and_exp
= rtx_alloc (AND
);
324 XEXP (and_exp
, 1) = match_test_exp
;
326 /* It is always correct to rewrite p->exp as
328 (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
330 but there are a couple forms where we can do better. If the
331 top-level pattern is an IOR, and one of the two branches does test
332 the mode, we can wrap just the branch that doesn't. Likewise, if
333 we have an IF_THEN_ELSE, and one side of it tests the mode, we can
334 wrap just the side that doesn't. And, of course, we can repeat this
335 descent as many times as it works. */
342 switch (GET_CODE (subexp
))
345 /* The switch code generation in write_predicate_stmts prefers
346 rtx code tests to be at the top of the expression tree. So
347 push this AND down into the second operand of an existing
349 if (generate_switch_p (XEXP (subexp
, 0)))
350 pos
= &XEXP (subexp
, 1);
355 int test0
= NO_MODE_TEST (XEXP (subexp
, 0));
356 int test1
= NO_MODE_TEST (XEXP (subexp
, 1));
358 gcc_assert (test0
|| test1
);
362 pos
= test0
? &XEXP (subexp
, 0) : &XEXP (subexp
, 1);
368 int test0
= NO_MODE_TEST (XEXP (subexp
, 0));
369 int test1
= NO_MODE_TEST (XEXP (subexp
, 1));
370 int test2
= NO_MODE_TEST (XEXP (subexp
, 2));
372 gcc_assert ((test0
&& test1
) || test2
);
374 if (test0
&& test1
&& test2
)
377 /* Must put it on the dependent clause, not the
378 controlling expression, or we change the meaning of
380 pos
= &XEXP (subexp
, 1);
382 pos
= &XEXP (subexp
, 2);
391 XEXP (and_exp
, 0) = *pos
;
395 /* PATH is a string describing a path from the root of an RTL
396 expression to an inner subexpression to be tested. Output
397 code which computes the subexpression from the variable
398 holding the root of the expression. */
400 write_extract_subexp (const char *path
)
402 int len
= strlen (path
);
405 /* We first write out the operations (XEXP or XVECEXP) in reverse
406 order, then write "op", then the indices in forward order. */
407 for (i
= len
- 1; i
>= 0; i
--)
409 if (ISLOWER (path
[i
]))
410 fputs ("XVECEXP (", stdout
);
411 else if (ISDIGIT (path
[i
]))
412 fputs ("XEXP (", stdout
);
417 fputs ("op", stdout
);
419 for (i
= 0; i
< len
; i
++)
421 if (ISLOWER (path
[i
]))
422 printf (", 0, %d)", path
[i
] - 'a');
423 else if (ISDIGIT (path
[i
]))
424 printf (", %d)", path
[i
] - '0');
430 /* CODES is a list of RTX codes. Write out an expression which
431 determines whether the operand has one of those codes. */
433 write_match_code (const char *path
, const char *codes
)
437 while ((code
= scan_comma_elt (&codes
)) != 0)
439 fputs ("GET_CODE (", stdout
);
440 write_extract_subexp (path
);
441 fputs (") == ", stdout
);
444 putchar (TOUPPER (*code
));
449 fputs (" || ", stdout
);
453 /* EXP is an RTL (sub)expression for a predicate. Recursively
454 descend the expression and write out an equivalent C expression. */
456 write_predicate_expr (rtx exp
)
458 switch (GET_CODE (exp
))
462 write_predicate_expr (XEXP (exp
, 0));
463 fputs (") && (", stdout
);
464 write_predicate_expr (XEXP (exp
, 1));
470 write_predicate_expr (XEXP (exp
, 0));
471 fputs (") || (", stdout
);
472 write_predicate_expr (XEXP (exp
, 1));
477 fputs ("!(", stdout
);
478 write_predicate_expr (XEXP (exp
, 0));
484 write_predicate_expr (XEXP (exp
, 0));
485 fputs (") ? (", stdout
);
486 write_predicate_expr (XEXP (exp
, 1));
487 fputs (") : (", stdout
);
488 write_predicate_expr (XEXP (exp
, 2));
493 if (GET_MODE (exp
) == VOIDmode
)
494 printf ("%s (op, mode)", XSTR (exp
, 1));
496 printf ("%s (op, %smode)", XSTR (exp
, 1), mode_name
[GET_MODE (exp
)]);
500 write_match_code (XSTR (exp
, 1), XSTR (exp
, 0));
504 print_c_condition (XSTR (exp
, 0));
512 /* Write the MATCH_CODE expression EXP as a switch statement. */
515 write_match_code_switch (rtx exp
)
517 const char *codes
= XSTR (exp
, 0);
518 const char *path
= XSTR (exp
, 1);
521 fputs (" switch (GET_CODE (", stdout
);
522 write_extract_subexp (path
);
523 fputs ("))\n {\n", stdout
);
525 while ((code
= scan_comma_elt (&codes
)) != 0)
527 fputs (" case ", stdout
);
530 putchar (TOUPPER (*code
));
533 fputs(":\n", stdout
);
537 /* Given a predicate expression EXP, write out a sequence of stmts
538 to evaluate it. This is similar to write_predicate_expr but can
539 generate efficient switch statements. */
542 write_predicate_stmts (rtx exp
)
544 switch (GET_CODE (exp
))
547 if (generate_switch_p (exp
))
549 write_match_code_switch (exp
);
550 puts (" return true;\n"
560 if (generate_switch_p (XEXP (exp
, 0)))
562 write_match_code_switch (XEXP (exp
, 0));
572 if (generate_switch_p (XEXP (exp
, 0)))
574 write_match_code_switch (XEXP (exp
, 0));
575 puts (" return true;\n"
584 if (generate_switch_p (XEXP (exp
, 0)))
586 write_match_code_switch (XEXP (exp
, 0));
587 puts (" return false;\n"
600 fputs(" return ",stdout
);
601 write_predicate_expr (exp
);
602 fputs(";\n", stdout
);
605 /* Given a predicate, write out a complete C function to compute it. */
607 write_one_predicate_function (struct pred_data
*p
)
612 write_predicate_subfunction (p
);
615 /* A normal predicate can legitimately not look at enum machine_mode
616 if it accepts only CONST_INTs and/or CONST_DOUBLEs. */
617 printf ("int\n%s (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
619 write_predicate_stmts (p
->exp
);
620 fputs ("}\n\n", stdout
);
623 /* Constraints fall into two categories: register constraints
624 (define_register_constraint), and others (define_constraint,
625 define_memory_constraint, define_address_constraint). We
626 work out automatically which of the various old-style macros
627 they correspond to, and produce appropriate code. They all
628 go in the same hash table so we can verify that there are no
631 /* All data from one constraint definition. */
632 struct constraint_data
634 struct constraint_data
*next_this_letter
;
635 struct constraint_data
*next_textual
;
637 const char *c_name
; /* same as .name unless mangling is necessary */
639 const char *regclass
; /* for register constraints */
640 rtx exp
; /* for other constraints */
641 unsigned int lineno
; /* line of definition */
642 unsigned int is_register
: 1;
643 unsigned int is_const_int
: 1;
644 unsigned int is_const_dbl
: 1;
645 unsigned int is_extra
: 1;
646 unsigned int is_memory
: 1;
647 unsigned int is_address
: 1;
650 /* Overview of all constraints beginning with a given letter. */
652 static struct constraint_data
*
653 constraints_by_letter_table
[1<<CHAR_BIT
];
655 /* For looking up all the constraints in the order that they appeared
656 in the machine description. */
657 static struct constraint_data
*first_constraint
;
658 static struct constraint_data
**last_constraint_ptr
= &first_constraint
;
660 #define FOR_ALL_CONSTRAINTS(iter_) \
661 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
663 /* These letters, and all names beginning with them, are reserved for
665 The 'm' constraint is not mentioned here since that constraint
666 letter can be overridden by the back end by defining the
667 TARGET_MEM_CONSTRAINT macro. */
668 static const char generic_constraint_letters
[] = "EFVXginoprs";
670 /* Machine-independent code expects that constraints with these
671 (initial) letters will allow only (a subset of all) CONST_INTs. */
673 static const char const_int_constraints
[] = "IJKLMNOP";
675 /* Machine-independent code expects that constraints with these
676 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
678 static const char const_dbl_constraints
[] = "GH";
680 /* Summary data used to decide whether to output various functions and
681 macro definitions. */
682 static unsigned int constraint_max_namelen
;
683 static bool have_register_constraints
;
684 static bool have_memory_constraints
;
685 static bool have_address_constraints
;
686 static bool have_extra_constraints
;
687 static bool have_const_int_constraints
;
688 static bool have_const_dbl_constraints
;
690 /* Convert NAME, which contains angle brackets and/or underscores, to
691 a string that can be used as part of a C identifier. The string
692 comes from the rtl_obstack. */
694 mangle (const char *name
)
696 for (; *name
; name
++)
699 case '_': obstack_grow (rtl_obstack
, "__", 2); break;
700 case '<': obstack_grow (rtl_obstack
, "_l", 2); break;
701 case '>': obstack_grow (rtl_obstack
, "_g", 2); break;
702 default: obstack_1grow (rtl_obstack
, *name
); break;
705 obstack_1grow (rtl_obstack
, '\0');
706 return XOBFINISH (rtl_obstack
, const char *);
709 /* Add one constraint, of any sort, to the tables. NAME is its name;
710 REGCLASS is the register class, if any; EXP is the expression to
711 test, if any; IS_MEMORY and IS_ADDRESS indicate memory and address
712 constraints, respectively; LINENO is the line number from the MD reader.
713 Not all combinations of arguments are valid; most importantly, REGCLASS
714 is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
715 meaningful for constraints with EXP.
717 This function enforces all syntactic and semantic rules about what
718 constraints can be defined. */
721 add_constraint (const char *name
, const char *regclass
,
722 rtx exp
, bool is_memory
, bool is_address
,
725 struct constraint_data
*c
, **iter
, **slot
;
727 bool need_mangled_name
= false;
732 if (exp
&& validate_exp (exp
, name
, lineno
))
735 if (!ISALPHA (name
[0]) && name
[0] != '_')
738 error_with_line (lineno
, "constraint name '%s' is not "
739 "a letter or underscore", name
);
741 error_with_line (lineno
, "constraint name '%s' does not begin "
742 "with a letter or underscore", name
);
745 for (p
= name
; *p
; p
++)
748 if (*p
== '<' || *p
== '>' || *p
== '_')
749 need_mangled_name
= true;
752 error_with_line (lineno
,
753 "constraint name '%s' must be composed of "
754 "letters, digits, underscores, and "
755 "angle brackets", name
);
760 if (strchr (generic_constraint_letters
, name
[0]))
763 error_with_line (lineno
, "constraint letter '%s' cannot be "
764 "redefined by the machine description", name
);
766 error_with_line (lineno
, "constraint name '%s' cannot be defined by "
767 "the machine description, as it begins with '%c'",
773 namelen
= strlen (name
);
774 slot
= &constraints_by_letter_table
[(unsigned int)name
[0]];
775 for (iter
= slot
; *iter
; iter
= &(*iter
)->next_this_letter
)
777 /* This causes slot to end up pointing to the
778 next_this_letter field of the last constraint with a name
779 of equal or greater length than the new constraint; hence
780 the new constraint will be inserted after all previous
781 constraints with names of the same length. */
782 if ((*iter
)->namelen
>= namelen
)
785 if (!strcmp ((*iter
)->name
, name
))
787 error_with_line (lineno
, "redefinition of constraint '%s'", name
);
788 message_with_line ((*iter
)->lineno
, "previous definition is here");
791 else if (!strncmp ((*iter
)->name
, name
, (*iter
)->namelen
))
793 error_with_line (lineno
, "defining constraint '%s' here", name
);
794 message_with_line ((*iter
)->lineno
, "renders constraint '%s' "
795 "(defined here) a prefix", (*iter
)->name
);
798 else if (!strncmp ((*iter
)->name
, name
, namelen
))
800 error_with_line (lineno
, "constraint '%s' is a prefix", name
);
801 message_with_line ((*iter
)->lineno
, "of constraint '%s' "
802 "(defined here)", (*iter
)->name
);
807 is_const_int
= strchr (const_int_constraints
, name
[0]) != 0;
808 is_const_dbl
= strchr (const_dbl_constraints
, name
[0]) != 0;
810 if (is_const_int
|| is_const_dbl
)
812 enum rtx_code appropriate_code
813 = is_const_int
? CONST_INT
: CONST_DOUBLE
;
815 /* Consider relaxing this requirement in the future. */
817 || GET_CODE (exp
) != AND
818 || GET_CODE (XEXP (exp
, 0)) != MATCH_CODE
819 || strcmp (XSTR (XEXP (exp
, 0), 0),
820 GET_RTX_NAME (appropriate_code
)))
823 error_with_line (lineno
, "constraint letter '%c' is reserved "
824 "for %s constraints",
825 name
[0], GET_RTX_NAME (appropriate_code
));
827 error_with_line (lineno
, "constraint names beginning with '%c' "
828 "(%s) are reserved for %s constraints",
829 name
[0], name
, GET_RTX_NAME (appropriate_code
));
836 error_with_line (lineno
, "constraint letter '%c' cannot be a "
837 "memory constraint", name
[0]);
839 error_with_line (lineno
, "constraint name '%s' begins with '%c', "
840 "and therefore cannot be a memory constraint",
847 error_with_line (lineno
, "constraint letter '%c' cannot be a "
848 "memory constraint", name
[0]);
850 error_with_line (lineno
, "constraint name '%s' begins with '%c', "
851 "and therefore cannot be a memory constraint",
858 c
= XOBNEW (rtl_obstack
, struct constraint_data
);
860 c
->c_name
= need_mangled_name
? mangle (name
) : name
;
862 c
->namelen
= namelen
;
863 c
->regclass
= regclass
;
865 c
->is_register
= regclass
!= 0;
866 c
->is_const_int
= is_const_int
;
867 c
->is_const_dbl
= is_const_dbl
;
868 c
->is_extra
= !(regclass
|| is_const_int
|| is_const_dbl
);
869 c
->is_memory
= is_memory
;
870 c
->is_address
= is_address
;
872 c
->next_this_letter
= *slot
;
875 /* Insert this constraint in the list of all constraints in textual
878 *last_constraint_ptr
= c
;
879 last_constraint_ptr
= &c
->next_textual
;
881 constraint_max_namelen
= MAX (constraint_max_namelen
, strlen (name
));
882 have_register_constraints
|= c
->is_register
;
883 have_const_int_constraints
|= c
->is_const_int
;
884 have_const_dbl_constraints
|= c
->is_const_dbl
;
885 have_extra_constraints
|= c
->is_extra
;
886 have_memory_constraints
|= c
->is_memory
;
887 have_address_constraints
|= c
->is_address
;
890 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
891 DEFINE_ADDRESS_CONSTRAINT expression, C. */
893 process_define_constraint (rtx c
, int lineno
)
895 add_constraint (XSTR (c
, 0), 0, XEXP (c
, 2),
896 GET_CODE (c
) == DEFINE_MEMORY_CONSTRAINT
,
897 GET_CODE (c
) == DEFINE_ADDRESS_CONSTRAINT
,
901 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
903 process_define_register_constraint (rtx c
, int lineno
)
905 add_constraint (XSTR (c
, 0), XSTR (c
, 1), 0, false, false, lineno
);
908 /* Write out an enumeration with one entry per machine-specific
911 write_enum_constraint_num (void)
913 struct constraint_data
*c
;
915 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout
);
916 fputs ("enum constraint_num\n"
918 " CONSTRAINT__UNKNOWN = 0", stdout
);
919 FOR_ALL_CONSTRAINTS (c
)
920 printf (",\n CONSTRAINT_%s", c
->c_name
);
921 puts (",\n CONSTRAINT__LIMIT\n};\n");
924 /* Write out a function which looks at a string and determines what
925 constraint name, if any, it begins with. */
927 write_lookup_constraint (void)
930 puts ("enum constraint_num\n"
931 "lookup_constraint (const char *str)\n"
936 for (i
= 0; i
< ARRAY_SIZE(constraints_by_letter_table
); i
++)
938 struct constraint_data
*c
= constraints_by_letter_table
[i
];
942 printf (" case '%c':\n", i
);
944 printf (" return CONSTRAINT_%s;\n", c
->c_name
);
949 printf (" if (!strncmp (str, \"%s\", %lu))\n"
950 " return CONSTRAINT_%s;\n",
951 c
->name
, (unsigned long int) c
->namelen
, c
->c_name
);
952 c
= c
->next_this_letter
;
959 puts (" default: break;\n"
961 " return CONSTRAINT__UNKNOWN;\n"
965 /* Write out a function which looks at a string and determines what
966 the constraint name length is. */
968 write_insn_constraint_len (void)
972 puts ("static inline size_t\n"
973 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
978 for (i
= 0; i
< ARRAY_SIZE(constraints_by_letter_table
); i
++)
980 struct constraint_data
*c
= constraints_by_letter_table
[i
];
986 /* Constraints with multiple characters should have the same
989 struct constraint_data
*c2
= c
->next_this_letter
;
990 size_t len
= c
->namelen
;
993 if (c2
->namelen
!= len
)
994 error ("Multi-letter constraints with first letter '%c' "
995 "should have same length", i
);
996 c2
= c2
->next_this_letter
;
1000 printf (" case '%c': return %lu;\n",
1001 i
, (unsigned long int) c
->namelen
);
1004 puts (" default: break;\n"
1010 /* Write out the function which computes the register class corresponding
1011 to a register constraint. */
1013 write_regclass_for_constraint (void)
1015 struct constraint_data
*c
;
1017 puts ("enum reg_class\n"
1018 "regclass_for_constraint (enum constraint_num c)\n"
1023 FOR_ALL_CONSTRAINTS (c
)
1025 printf (" case CONSTRAINT_%s: return %s;\n", c
->c_name
, c
->regclass
);
1027 puts (" default: break;\n"
1029 " return NO_REGS;\n"
1033 /* Write out the functions which compute whether a given value matches
1034 a given non-register constraint. */
1036 write_tm_constrs_h (void)
1038 struct constraint_data
*c
;
1041 /* Generated automatically by the program '%s'\n\
1042 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1045 #ifndef GCC_TM_CONSTRS_H\n\
1046 #define GCC_TM_CONSTRS_H\n");
1048 FOR_ALL_CONSTRAINTS (c
)
1049 if (!c
->is_register
)
1051 bool needs_ival
= needs_variable (c
->exp
, "ival");
1052 bool needs_hval
= needs_variable (c
->exp
, "hval");
1053 bool needs_lval
= needs_variable (c
->exp
, "lval");
1054 bool needs_rval
= needs_variable (c
->exp
, "rval");
1055 bool needs_mode
= (needs_variable (c
->exp
, "mode")
1056 || needs_hval
|| needs_lval
|| needs_rval
);
1057 bool needs_op
= (needs_variable (c
->exp
, "op")
1058 || needs_ival
|| needs_mode
);
1060 printf ("static inline bool\n"
1061 "satisfies_constraint_%s (rtx %s)\n"
1063 needs_op
? "op" : "ARG_UNUSED (op)");
1065 puts (" enum machine_mode mode = GET_MODE (op);");
1067 puts (" HOST_WIDE_INT ival = 0;");
1069 puts (" HOST_WIDE_INT hval = 0;");
1071 puts (" unsigned HOST_WIDE_INT lval = 0;");
1073 puts (" const REAL_VALUE_TYPE *rval = 0;");
1076 puts (" if (CONST_INT_P (op))\n"
1077 " ival = INTVAL (op);");
1079 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1080 " hval = CONST_DOUBLE_HIGH (op);");
1082 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1083 " lval = CONST_DOUBLE_LOW (op);");
1085 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1086 " rval = CONST_DOUBLE_REAL_VALUE (op);");
1088 write_predicate_stmts (c
->exp
);
1089 fputs ("}\n", stdout
);
1091 puts ("#endif /* tm-constrs.h */");
1094 /* Write out the wrapper function, constraint_satisfied_p, that maps
1095 a CONSTRAINT_xxx constant to one of the predicate functions generated
1098 write_constraint_satisfied_p (void)
1100 struct constraint_data
*c
;
1103 "constraint_satisfied_p (rtx op, enum constraint_num c)\n"
1108 FOR_ALL_CONSTRAINTS (c
)
1109 if (!c
->is_register
)
1110 printf (" case CONSTRAINT_%s: "
1111 "return satisfies_constraint_%s (op);\n",
1112 c
->c_name
, c
->c_name
);
1114 puts (" default: break;\n"
1120 /* Write out the function which computes whether a given value matches
1121 a given CONST_INT constraint. This doesn't just forward to
1122 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1124 write_insn_const_int_ok_for_constraint (void)
1126 struct constraint_data
*c
;
1129 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1130 "enum constraint_num c)\n"
1135 FOR_ALL_CONSTRAINTS (c
)
1136 if (c
->is_const_int
)
1138 printf (" case CONSTRAINT_%s:\n return ", c
->c_name
);
1139 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1140 we know at this point that we have a const_int, so we need not
1141 bother with that part of the test. */
1142 write_predicate_expr (XEXP (c
->exp
, 1));
1143 fputs (";\n\n", stdout
);
1146 puts (" default: break;\n"
1153 /* Write out the function which computes whether a given constraint is
1154 a memory constraint. */
1156 write_insn_extra_memory_constraint (void)
1158 struct constraint_data
*c
;
1161 "insn_extra_memory_constraint (enum constraint_num c)\n"
1166 FOR_ALL_CONSTRAINTS (c
)
1168 printf (" case CONSTRAINT_%s:\n return true;\n\n", c
->c_name
);
1170 puts (" default: break;\n"
1176 /* Write out the function which computes whether a given constraint is
1177 an address constraint. */
1179 write_insn_extra_address_constraint (void)
1181 struct constraint_data
*c
;
1184 "insn_extra_address_constraint (enum constraint_num c)\n"
1189 FOR_ALL_CONSTRAINTS (c
)
1191 printf (" case CONSTRAINT_%s:\n return true;\n\n", c
->c_name
);
1193 puts (" default: break;\n"
1200 /* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1201 an enumeration in portable C, so we have to condition all these
1202 prototypes on HAVE_MACHINE_MODES. */
1204 write_tm_preds_h (void)
1206 struct pred_data
*p
;
1209 /* Generated automatically by the program '%s'\n\
1210 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1213 #ifndef GCC_TM_PREDS_H\n\
1214 #define GCC_TM_PREDS_H\n\
1216 #ifdef HAVE_MACHINE_MODES");
1218 FOR_ALL_PREDICATES (p
)
1219 printf ("extern int %s (rtx, enum machine_mode);\n", p
->name
);
1221 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1223 if (constraint_max_namelen
> 0)
1225 write_enum_constraint_num ();
1226 puts ("extern enum constraint_num lookup_constraint (const char *);\n"
1227 "extern bool constraint_satisfied_p (rtx, enum constraint_num);\n");
1229 if (constraint_max_namelen
> 1)
1231 write_insn_constraint_len ();
1232 puts ("#define CONSTRAINT_LEN(c_,s_) "
1233 "insn_constraint_len (c_,s_)\n");
1236 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1237 if (have_register_constraints
)
1238 puts ("extern enum reg_class regclass_for_constraint "
1239 "(enum constraint_num);\n"
1240 "#define REG_CLASS_FROM_CONSTRAINT(c_,s_) \\\n"
1241 " regclass_for_constraint (lookup_constraint (s_))\n"
1242 "#define REG_CLASS_FOR_CONSTRAINT(x_) \\\n"
1243 " regclass_for_constraint (x_)\n");
1245 puts ("#define REG_CLASS_FROM_CONSTRAINT(c_,s_) NO_REGS\n"
1246 "#define REG_CLASS_FOR_CONSTRAINT(x_) \\\n"
1248 if (have_const_int_constraints
)
1249 puts ("extern bool insn_const_int_ok_for_constraint "
1250 "(HOST_WIDE_INT, enum constraint_num);\n"
1251 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1252 " insn_const_int_ok_for_constraint (v_, "
1253 "lookup_constraint (s_))\n");
1254 if (have_const_dbl_constraints
)
1255 puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1256 " constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1258 puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) 0\n");
1259 if (have_extra_constraints
)
1260 puts ("#define EXTRA_CONSTRAINT_STR(v_,c_,s_) \\\n"
1261 " constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1262 if (have_memory_constraints
)
1263 puts ("extern bool "
1264 "insn_extra_memory_constraint (enum constraint_num);\n"
1265 "#define EXTRA_MEMORY_CONSTRAINT(c_,s_) "
1266 "insn_extra_memory_constraint (lookup_constraint (s_))\n");
1268 puts ("#define EXTRA_MEMORY_CONSTRAINT(c_,s_) false\n");
1269 if (have_address_constraints
)
1270 puts ("extern bool "
1271 "insn_extra_address_constraint (enum constraint_num);\n"
1272 "#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) "
1273 "insn_extra_address_constraint (lookup_constraint (s_))\n");
1275 puts ("#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) false\n");
1278 puts ("#endif /* tm-preds.h */");
1281 /* Write insn-preds.c.
1282 N.B. the list of headers to include was copied from genrecog; it
1285 FUTURE: Write #line markers referring back to the machine
1286 description. (Can't practically do this now since we don't know
1287 the line number of the C block - just the line number of the enclosing
1290 write_insn_preds_c (void)
1292 struct pred_data
*p
;
1295 /* Generated automatically by the program '%s'\n\
1296 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1299 #include \"config.h\"\n\
1300 #include \"system.h\"\n\
1301 #include \"coretypes.h\"\n\
1302 #include \"tm.h\"\n\
1303 #include \"rtl.h\"\n\
1304 #include \"tree.h\"\n\
1305 #include \"tm_p.h\"\n\
1306 #include \"function.h\"\n\
1307 #include \"insn-config.h\"\n\
1308 #include \"recog.h\"\n\
1309 #include \"output.h\"\n\
1310 #include \"flags.h\"\n\
1311 #include \"hard-reg-set.h\"\n\
1312 #include \"resource.h\"\n\
1313 #include \"diagnostic-core.h\"\n\
1314 #include \"reload.h\"\n\
1315 #include \"regs.h\"\n\
1316 #include \"tm-constrs.h\"\n");
1318 FOR_ALL_PREDICATES (p
)
1319 write_one_predicate_function (p
);
1321 if (constraint_max_namelen
> 0)
1323 write_lookup_constraint ();
1324 if (have_register_constraints
)
1325 write_regclass_for_constraint ();
1326 write_constraint_satisfied_p ();
1328 if (have_const_int_constraints
)
1329 write_insn_const_int_ok_for_constraint ();
1331 if (have_memory_constraints
)
1332 write_insn_extra_memory_constraint ();
1333 if (have_address_constraints
)
1334 write_insn_extra_address_constraint ();
1338 /* Argument parsing. */
1339 static bool gen_header
;
1340 static bool gen_constrs
;
1343 parse_option (const char *opt
)
1345 if (!strcmp (opt
, "-h"))
1350 else if (!strcmp (opt
, "-c"))
1359 /* Master control. */
1361 main (int argc
, char **argv
)
1364 int pattern_lineno
, next_insn_code
= 0;
1368 fatal ("no input file name");
1369 if (!init_rtx_reader_args_cb (argc
, argv
, parse_option
))
1370 return FATAL_EXIT_CODE
;
1372 while ((defn
= read_md_rtx (&pattern_lineno
, &next_insn_code
)) != 0)
1373 switch (GET_CODE (defn
))
1375 case DEFINE_PREDICATE
:
1376 case DEFINE_SPECIAL_PREDICATE
:
1377 process_define_predicate (defn
, pattern_lineno
);
1380 case DEFINE_CONSTRAINT
:
1381 case DEFINE_MEMORY_CONSTRAINT
:
1382 case DEFINE_ADDRESS_CONSTRAINT
:
1383 process_define_constraint (defn
, pattern_lineno
);
1386 case DEFINE_REGISTER_CONSTRAINT
:
1387 process_define_register_constraint (defn
, pattern_lineno
);
1395 write_tm_preds_h ();
1396 else if (gen_constrs
)
1397 write_tm_constrs_h ();
1399 write_insn_preds_c ();
1401 if (have_error
|| ferror (stdout
) || fflush (stdout
) || fclose (stdout
))
1402 return FATAL_EXIT_CODE
;
1404 return SUCCESS_EXIT_CODE
;