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
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"
31 #include "gensupport.h"
33 /* Given a predicate expression EXP, from form NAME at line LINENO,
34 verify that it does not contain any RTL constructs which are not
35 valid in predicate definitions. Returns true if EXP is
36 INvalid; issues error messages, caller need not. */
38 validate_exp (rtx exp
, const char *name
, int lineno
)
42 message_with_line (lineno
, "%s: must give a predicate expression", name
);
46 switch (GET_CODE (exp
))
48 /* Ternary, binary, unary expressions: recurse into subexpressions. */
50 if (validate_exp (XEXP (exp
, 2), name
, lineno
))
52 /* else fall through */
55 if (validate_exp (XEXP (exp
, 1), name
, lineno
))
57 /* else fall through */
59 return validate_exp (XEXP (exp
, 0), name
, lineno
);
61 /* MATCH_CODE might have a syntax error in its path expression. */
65 for (p
= XSTR (exp
, 1); *p
; p
++)
67 if (!ISDIGIT (*p
) && !ISLOWER (*p
))
69 message_with_line (lineno
, "%s: invalid character in path "
70 "string '%s'", name
, XSTR (exp
, 1));
78 /* These need no special checking. */
84 message_with_line (lineno
,
85 "%s: cannot use '%s' in a predicate expression",
86 name
, GET_RTX_NAME (GET_CODE (exp
)));
92 /* Predicates are defined with (define_predicate) or
93 (define_special_predicate) expressions in the machine description. */
95 process_define_predicate (rtx defn
, int lineno
)
97 struct pred_data
*pred
;
100 if (!ISALPHA (XSTR (defn
, 0)[0]) && XSTR (defn
, 0)[0] != '_')
102 for (p
= XSTR (defn
, 0) + 1; *p
; p
++)
103 if (!ISALNUM (*p
) && *p
!= '_')
106 if (validate_exp (XEXP (defn
, 1), XSTR (defn
, 0), lineno
))
109 pred
= XCNEW (struct pred_data
);
110 pred
->name
= XSTR (defn
, 0);
111 pred
->exp
= XEXP (defn
, 1);
112 pred
->c_block
= XSTR (defn
, 2);
114 if (GET_CODE (defn
) == DEFINE_SPECIAL_PREDICATE
)
115 pred
->special
= true;
117 add_predicate (pred
);
121 message_with_line (lineno
,
122 "%s: predicate name must be a valid C function name",
128 /* Given a predicate, if it has an embedded C block, write the block
129 out as a static inline subroutine, and augment the RTL test with a
130 match_test that calls that subroutine. For instance,
132 (define_predicate "basereg_operand"
133 (match_operand 0 "register_operand")
135 if (GET_CODE (op) == SUBREG)
136 op = SUBREG_REG (op);
137 return REG_POINTER (op);
142 static inline int basereg_operand_1(rtx op, enum machine_mode mode)
144 if (GET_CODE (op) == SUBREG)
145 op = SUBREG_REG (op);
146 return REG_POINTER (op);
149 (define_predicate "basereg_operand"
150 (and (match_operand 0 "register_operand")
151 (match_test "basereg_operand_1 (op, mode)")))
153 The only wart is that there's no way to insist on a { } string in
154 an RTL template, so we have to handle "" strings. */
158 write_predicate_subfunction (struct pred_data
*p
)
160 const char *match_test_str
;
161 rtx match_test_exp
, and_exp
;
163 if (p
->c_block
[0] == '\0')
166 /* Construct the function-call expression. */
167 obstack_grow (rtl_obstack
, p
->name
, strlen (p
->name
));
168 obstack_grow (rtl_obstack
, "_1 (op, mode)",
169 sizeof "_1 (op, mode)");
170 match_test_str
= XOBFINISH (rtl_obstack
, const char *);
172 /* Add the function-call expression to the complete expression to be
174 match_test_exp
= rtx_alloc (MATCH_TEST
);
175 XSTR (match_test_exp
, 0) = match_test_str
;
177 and_exp
= rtx_alloc (AND
);
178 XEXP (and_exp
, 0) = p
->exp
;
179 XEXP (and_exp
, 1) = match_test_exp
;
183 printf ("static inline int\n"
184 "%s_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n",
186 print_rtx_ptr_loc (p
->c_block
);
187 if (p
->c_block
[0] == '{')
188 fputs (p
->c_block
, stdout
);
190 printf ("{\n %s\n}", p
->c_block
);
191 fputs ("\n\n", stdout
);
194 /* Given a predicate expression EXP, from form NAME, determine whether
195 it refers to the variable given as VAR. */
197 needs_variable (rtx exp
, const char *var
)
199 switch (GET_CODE (exp
))
201 /* Ternary, binary, unary expressions need a variable if
202 any of their subexpressions do. */
204 if (needs_variable (XEXP (exp
, 2), var
))
206 /* else fall through */
209 if (needs_variable (XEXP (exp
, 1), var
))
211 /* else fall through */
213 return needs_variable (XEXP (exp
, 0), var
);
215 /* MATCH_CODE uses "op", but nothing else. */
217 return !strcmp (var
, "op");
219 /* MATCH_OPERAND uses "op" and may use "mode". */
221 if (!strcmp (var
, "op"))
223 if (!strcmp (var
, "mode") && GET_MODE (exp
) == VOIDmode
)
227 /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
230 const char *p
= XSTR (exp
, 0);
231 const char *q
= strstr (p
, var
);
234 if (q
!= p
&& (ISALNUM (q
[-1]) || q
[-1] == '_'))
237 if (ISALNUM (q
[0]) || q
[0] == '_')
247 /* Given an RTL expression EXP, find all subexpressions which we may
248 assume to perform mode tests. Normal MATCH_OPERAND does;
249 MATCH_CODE does if it applies to the whole expression and accepts
250 CONST_INT or CONST_DOUBLE; and we have to assume that MATCH_TEST
251 does not. These combine in almost-boolean fashion - the only
252 exception is that (not X) must be assumed not to perform a mode
253 test, whether or not X does.
255 The mark is the RTL /v flag, which is true for subexpressions which
256 do *not* perform mode tests.
258 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
260 mark_mode_tests (rtx exp
)
262 switch (GET_CODE (exp
))
266 struct pred_data
*p
= lookup_predicate (XSTR (exp
, 1));
268 error ("reference to undefined predicate '%s'", XSTR (exp
, 1));
269 else if (p
->special
|| GET_MODE (exp
) != VOIDmode
)
270 NO_MODE_TEST (exp
) = 1;
275 if (XSTR (exp
, 1)[0] != '\0'
276 || (!strstr (XSTR (exp
, 0), "const_int")
277 && !strstr (XSTR (exp
, 0), "const_double")))
278 NO_MODE_TEST (exp
) = 1;
283 NO_MODE_TEST (exp
) = 1;
287 mark_mode_tests (XEXP (exp
, 0));
288 mark_mode_tests (XEXP (exp
, 1));
290 NO_MODE_TEST (exp
) = (NO_MODE_TEST (XEXP (exp
, 0))
291 && NO_MODE_TEST (XEXP (exp
, 1)));
295 mark_mode_tests (XEXP (exp
, 0));
296 mark_mode_tests (XEXP (exp
, 1));
298 NO_MODE_TEST (exp
) = (NO_MODE_TEST (XEXP (exp
, 0))
299 || NO_MODE_TEST (XEXP (exp
, 1)));
303 /* A ? B : C does a mode test if (one of A and B) does a mode
304 test, and C does too. */
305 mark_mode_tests (XEXP (exp
, 0));
306 mark_mode_tests (XEXP (exp
, 1));
307 mark_mode_tests (XEXP (exp
, 2));
309 NO_MODE_TEST (exp
) = ((NO_MODE_TEST (XEXP (exp
, 0))
310 && NO_MODE_TEST (XEXP (exp
, 1)))
311 || NO_MODE_TEST (XEXP (exp
, 2)));
319 /* Determine whether the expression EXP is a MATCH_CODE that should
320 be written as a switch statement. */
322 generate_switch_p (rtx exp
)
324 return GET_CODE (exp
) == MATCH_CODE
325 && strchr (XSTR (exp
, 0), ',');
328 /* Given a predicate, work out where in its RTL expression to add
329 tests for proper modes. Special predicates do not get any such
330 tests. We try to avoid adding tests when we don't have to; in
331 particular, other normal predicates can be counted on to do it for
335 add_mode_tests (struct pred_data
*p
)
337 rtx match_test_exp
, and_exp
;
340 /* Don't touch special predicates. */
344 mark_mode_tests (p
->exp
);
346 /* If the whole expression already tests the mode, we're done. */
347 if (!NO_MODE_TEST (p
->exp
))
350 match_test_exp
= rtx_alloc (MATCH_TEST
);
351 XSTR (match_test_exp
, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
352 and_exp
= rtx_alloc (AND
);
353 XEXP (and_exp
, 1) = match_test_exp
;
355 /* It is always correct to rewrite p->exp as
357 (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
359 but there are a couple forms where we can do better. If the
360 top-level pattern is an IOR, and one of the two branches does test
361 the mode, we can wrap just the branch that doesn't. Likewise, if
362 we have an IF_THEN_ELSE, and one side of it tests the mode, we can
363 wrap just the side that doesn't. And, of course, we can repeat this
364 descent as many times as it works. */
371 switch (GET_CODE (subexp
))
374 /* The switch code generation in write_predicate_stmts prefers
375 rtx code tests to be at the top of the expression tree. So
376 push this AND down into the second operand of an existing
378 if (generate_switch_p (XEXP (subexp
, 0)))
379 pos
= &XEXP (subexp
, 1);
384 int test0
= NO_MODE_TEST (XEXP (subexp
, 0));
385 int test1
= NO_MODE_TEST (XEXP (subexp
, 1));
387 gcc_assert (test0
|| test1
);
391 pos
= test0
? &XEXP (subexp
, 0) : &XEXP (subexp
, 1);
397 int test0
= NO_MODE_TEST (XEXP (subexp
, 0));
398 int test1
= NO_MODE_TEST (XEXP (subexp
, 1));
399 int test2
= NO_MODE_TEST (XEXP (subexp
, 2));
401 gcc_assert ((test0
&& test1
) || test2
);
403 if (test0
&& test1
&& test2
)
406 /* Must put it on the dependent clause, not the
407 controlling expression, or we change the meaning of
409 pos
= &XEXP (subexp
, 1);
411 pos
= &XEXP (subexp
, 2);
420 XEXP (and_exp
, 0) = *pos
;
424 /* PATH is a string describing a path from the root of an RTL
425 expression to an inner subexpression to be tested. Output
426 code which computes the subexpression from the variable
427 holding the root of the expression. */
429 write_extract_subexp (const char *path
)
431 int len
= strlen (path
);
434 /* We first write out the operations (XEXP or XVECEXP) in reverse
435 order, then write "op", then the indices in forward order. */
436 for (i
= len
- 1; i
>= 0; i
--)
438 if (ISLOWER (path
[i
]))
439 fputs ("XVECEXP (", stdout
);
440 else if (ISDIGIT (path
[i
]))
441 fputs ("XEXP (", stdout
);
446 fputs ("op", stdout
);
448 for (i
= 0; i
< len
; i
++)
450 if (ISLOWER (path
[i
]))
451 printf (", 0, %d)", path
[i
] - 'a');
452 else if (ISDIGIT (path
[i
]))
453 printf (", %d)", path
[i
] - '0');
459 /* CODES is a list of RTX codes. Write out an expression which
460 determines whether the operand has one of those codes. */
462 write_match_code (const char *path
, const char *codes
)
466 while ((code
= scan_comma_elt (&codes
)) != 0)
468 fputs ("GET_CODE (", stdout
);
469 write_extract_subexp (path
);
470 fputs (") == ", stdout
);
473 putchar (TOUPPER (*code
));
478 fputs (" || ", stdout
);
482 /* EXP is an RTL (sub)expression for a predicate. Recursively
483 descend the expression and write out an equivalent C expression. */
485 write_predicate_expr (rtx exp
)
487 switch (GET_CODE (exp
))
491 write_predicate_expr (XEXP (exp
, 0));
492 fputs (") && (", stdout
);
493 write_predicate_expr (XEXP (exp
, 1));
499 write_predicate_expr (XEXP (exp
, 0));
500 fputs (") || (", stdout
);
501 write_predicate_expr (XEXP (exp
, 1));
506 fputs ("!(", stdout
);
507 write_predicate_expr (XEXP (exp
, 0));
513 write_predicate_expr (XEXP (exp
, 0));
514 fputs (") ? (", stdout
);
515 write_predicate_expr (XEXP (exp
, 1));
516 fputs (") : (", stdout
);
517 write_predicate_expr (XEXP (exp
, 2));
522 if (GET_MODE (exp
) == VOIDmode
)
523 printf ("%s (op, mode)", XSTR (exp
, 1));
525 printf ("%s (op, %smode)", XSTR (exp
, 1), mode_name
[GET_MODE (exp
)]);
529 write_match_code (XSTR (exp
, 1), XSTR (exp
, 0));
533 print_c_condition (XSTR (exp
, 0));
541 /* Write the MATCH_CODE expression EXP as a switch statement. */
544 write_match_code_switch (rtx exp
)
546 const char *codes
= XSTR (exp
, 0);
547 const char *path
= XSTR (exp
, 1);
550 fputs (" switch (GET_CODE (", stdout
);
551 write_extract_subexp (path
);
552 fputs ("))\n {\n", stdout
);
554 while ((code
= scan_comma_elt (&codes
)) != 0)
556 fputs (" case ", stdout
);
559 putchar (TOUPPER (*code
));
562 fputs(":\n", stdout
);
566 /* Given a predicate expression EXP, write out a sequence of stmts
567 to evaluate it. This is similar to write_predicate_expr but can
568 generate efficient switch statements. */
571 write_predicate_stmts (rtx exp
)
573 switch (GET_CODE (exp
))
576 if (generate_switch_p (exp
))
578 write_match_code_switch (exp
);
579 puts (" return true;\n"
589 if (generate_switch_p (XEXP (exp
, 0)))
591 write_match_code_switch (XEXP (exp
, 0));
601 if (generate_switch_p (XEXP (exp
, 0)))
603 write_match_code_switch (XEXP (exp
, 0));
604 puts (" return true;\n"
613 if (generate_switch_p (XEXP (exp
, 0)))
615 write_match_code_switch (XEXP (exp
, 0));
616 puts (" return false;\n"
629 fputs(" return ",stdout
);
630 write_predicate_expr (exp
);
631 fputs(";\n", stdout
);
634 /* Given a predicate, write out a complete C function to compute it. */
636 write_one_predicate_function (struct pred_data
*p
)
641 write_predicate_subfunction (p
);
644 /* A normal predicate can legitimately not look at enum machine_mode
645 if it accepts only CONST_INTs and/or CONST_DOUBLEs. */
646 printf ("int\n%s (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
648 write_predicate_stmts (p
->exp
);
649 fputs ("}\n\n", stdout
);
652 /* Constraints fall into two categories: register constraints
653 (define_register_constraint), and others (define_constraint,
654 define_memory_constraint, define_address_constraint). We
655 work out automatically which of the various old-style macros
656 they correspond to, and produce appropriate code. They all
657 go in the same hash table so we can verify that there are no
660 /* All data from one constraint definition. */
661 struct constraint_data
663 struct constraint_data
*next_this_letter
;
664 struct constraint_data
*next_textual
;
666 const char *c_name
; /* same as .name unless mangling is necessary */
668 const char *regclass
; /* for register constraints */
669 rtx exp
; /* for other constraints */
670 unsigned int lineno
; /* line of definition */
671 unsigned int is_register
: 1;
672 unsigned int is_const_int
: 1;
673 unsigned int is_const_dbl
: 1;
674 unsigned int is_extra
: 1;
675 unsigned int is_memory
: 1;
676 unsigned int is_address
: 1;
679 /* Overview of all constraints beginning with a given letter. */
681 static struct constraint_data
*
682 constraints_by_letter_table
[1<<CHAR_BIT
];
684 /* For looking up all the constraints in the order that they appeared
685 in the machine description. */
686 static struct constraint_data
*first_constraint
;
687 static struct constraint_data
**last_constraint_ptr
= &first_constraint
;
689 #define FOR_ALL_CONSTRAINTS(iter_) \
690 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
692 /* These letters, and all names beginning with them, are reserved for
694 The 'm' constraint is not mentioned here since that constraint
695 letter can be overridden by the back end by defining the
696 TARGET_MEM_CONSTRAINT macro. */
697 static const char generic_constraint_letters
[] = "EFVXginoprs";
699 /* Machine-independent code expects that constraints with these
700 (initial) letters will allow only (a subset of all) CONST_INTs. */
702 static const char const_int_constraints
[] = "IJKLMNOP";
704 /* Machine-independent code expects that constraints with these
705 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
707 static const char const_dbl_constraints
[] = "GH";
709 /* Summary data used to decide whether to output various functions and
710 macro definitions. */
711 static unsigned int constraint_max_namelen
;
712 static bool have_register_constraints
;
713 static bool have_memory_constraints
;
714 static bool have_address_constraints
;
715 static bool have_extra_constraints
;
716 static bool have_const_int_constraints
;
717 static bool have_const_dbl_constraints
;
719 /* Convert NAME, which contains angle brackets and/or underscores, to
720 a string that can be used as part of a C identifier. The string
721 comes from the rtl_obstack. */
723 mangle (const char *name
)
725 for (; *name
; name
++)
728 case '_': obstack_grow (rtl_obstack
, "__", 2); break;
729 case '<': obstack_grow (rtl_obstack
, "_l", 2); break;
730 case '>': obstack_grow (rtl_obstack
, "_g", 2); break;
731 default: obstack_1grow (rtl_obstack
, *name
); break;
734 obstack_1grow (rtl_obstack
, '\0');
735 return XOBFINISH (rtl_obstack
, const char *);
738 /* Add one constraint, of any sort, to the tables. NAME is its name;
739 REGCLASS is the register class, if any; EXP is the expression to
740 test, if any; IS_MEMORY and IS_ADDRESS indicate memory and address
741 constraints, respectively; LINENO is the line number from the MD reader.
742 Not all combinations of arguments are valid; most importantly, REGCLASS
743 is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
744 meaningful for constraints with EXP.
746 This function enforces all syntactic and semantic rules about what
747 constraints can be defined. */
750 add_constraint (const char *name
, const char *regclass
,
751 rtx exp
, bool is_memory
, bool is_address
,
754 struct constraint_data
*c
, **iter
, **slot
;
756 bool need_mangled_name
= false;
761 if (exp
&& validate_exp (exp
, name
, lineno
))
764 if (!ISALPHA (name
[0]) && name
[0] != '_')
767 message_with_line (lineno
, "constraint name '%s' is not "
768 "a letter or underscore", name
);
770 message_with_line (lineno
, "constraint name '%s' does not begin "
771 "with a letter or underscore", name
);
775 for (p
= name
; *p
; p
++)
778 if (*p
== '<' || *p
== '>' || *p
== '_')
779 need_mangled_name
= true;
782 message_with_line (lineno
,
783 "constraint name '%s' must be composed of "
784 "letters, digits, underscores, and "
785 "angle brackets", name
);
791 if (strchr (generic_constraint_letters
, name
[0]))
794 message_with_line (lineno
, "constraint letter '%s' cannot be "
795 "redefined by the machine description", name
);
797 message_with_line (lineno
, "constraint name '%s' cannot be defined by "
798 "the machine description, as it begins with '%c'",
805 namelen
= strlen (name
);
806 slot
= &constraints_by_letter_table
[(unsigned int)name
[0]];
807 for (iter
= slot
; *iter
; iter
= &(*iter
)->next_this_letter
)
809 /* This causes slot to end up pointing to the
810 next_this_letter field of the last constraint with a name
811 of equal or greater length than the new constraint; hence
812 the new constraint will be inserted after all previous
813 constraints with names of the same length. */
814 if ((*iter
)->namelen
>= namelen
)
817 if (!strcmp ((*iter
)->name
, name
))
819 message_with_line (lineno
, "redefinition of constraint '%s'", name
);
820 message_with_line ((*iter
)->lineno
, "previous definition is here");
824 else if (!strncmp ((*iter
)->name
, name
, (*iter
)->namelen
))
826 message_with_line (lineno
, "defining constraint '%s' here", name
);
827 message_with_line ((*iter
)->lineno
, "renders constraint '%s' "
828 "(defined here) a prefix", (*iter
)->name
);
832 else if (!strncmp ((*iter
)->name
, name
, namelen
))
834 message_with_line (lineno
, "constraint '%s' is a prefix", name
);
835 message_with_line ((*iter
)->lineno
, "of constraint '%s' "
836 "(defined here)", (*iter
)->name
);
842 is_const_int
= strchr (const_int_constraints
, name
[0]) != 0;
843 is_const_dbl
= strchr (const_dbl_constraints
, name
[0]) != 0;
845 if (is_const_int
|| is_const_dbl
)
847 enum rtx_code appropriate_code
848 = is_const_int
? CONST_INT
: CONST_DOUBLE
;
850 /* Consider relaxing this requirement in the future. */
852 || GET_CODE (exp
) != AND
853 || GET_CODE (XEXP (exp
, 0)) != MATCH_CODE
854 || strcmp (XSTR (XEXP (exp
, 0), 0),
855 GET_RTX_NAME (appropriate_code
)))
858 message_with_line (lineno
, "constraint letter '%c' is reserved "
859 "for %s constraints",
860 name
[0], GET_RTX_NAME (appropriate_code
));
862 message_with_line (lineno
, "constraint names beginning with '%c' "
863 "(%s) are reserved for %s constraints",
865 GET_RTX_NAME (appropriate_code
));
874 message_with_line (lineno
, "constraint letter '%c' cannot be a "
875 "memory constraint", name
[0]);
877 message_with_line (lineno
, "constraint name '%s' begins with '%c', "
878 "and therefore cannot be a memory constraint",
887 message_with_line (lineno
, "constraint letter '%c' cannot be a "
888 "memory constraint", name
[0]);
890 message_with_line (lineno
, "constraint name '%s' begins with '%c', "
891 "and therefore cannot be a memory constraint",
900 c
= XOBNEW (rtl_obstack
, struct constraint_data
);
902 c
->c_name
= need_mangled_name
? mangle (name
) : name
;
904 c
->namelen
= namelen
;
905 c
->regclass
= regclass
;
907 c
->is_register
= regclass
!= 0;
908 c
->is_const_int
= is_const_int
;
909 c
->is_const_dbl
= is_const_dbl
;
910 c
->is_extra
= !(regclass
|| is_const_int
|| is_const_dbl
);
911 c
->is_memory
= is_memory
;
912 c
->is_address
= is_address
;
914 c
->next_this_letter
= *slot
;
917 /* Insert this constraint in the list of all constraints in textual
920 *last_constraint_ptr
= c
;
921 last_constraint_ptr
= &c
->next_textual
;
923 constraint_max_namelen
= MAX (constraint_max_namelen
, strlen (name
));
924 have_register_constraints
|= c
->is_register
;
925 have_const_int_constraints
|= c
->is_const_int
;
926 have_const_dbl_constraints
|= c
->is_const_dbl
;
927 have_extra_constraints
|= c
->is_extra
;
928 have_memory_constraints
|= c
->is_memory
;
929 have_address_constraints
|= c
->is_address
;
932 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
933 DEFINE_ADDRESS_CONSTRAINT expression, C. */
935 process_define_constraint (rtx c
, int lineno
)
937 add_constraint (XSTR (c
, 0), 0, XEXP (c
, 2),
938 GET_CODE (c
) == DEFINE_MEMORY_CONSTRAINT
,
939 GET_CODE (c
) == DEFINE_ADDRESS_CONSTRAINT
,
943 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
945 process_define_register_constraint (rtx c
, int lineno
)
947 add_constraint (XSTR (c
, 0), XSTR (c
, 1), 0, false, false, lineno
);
950 /* Write out an enumeration with one entry per machine-specific
953 write_enum_constraint_num (void)
955 struct constraint_data
*c
;
957 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout
);
958 fputs ("enum constraint_num\n"
960 " CONSTRAINT__UNKNOWN = 0", stdout
);
961 FOR_ALL_CONSTRAINTS (c
)
962 printf (",\n CONSTRAINT_%s", c
->c_name
);
963 puts (",\n CONSTRAINT__LIMIT\n};\n");
966 /* Write out a function which looks at a string and determines what
967 constraint name, if any, it begins with. */
969 write_lookup_constraint (void)
972 puts ("enum constraint_num\n"
973 "lookup_constraint (const char *str)\n"
978 for (i
= 0; i
< ARRAY_SIZE(constraints_by_letter_table
); i
++)
980 struct constraint_data
*c
= constraints_by_letter_table
[i
];
984 printf (" case '%c':\n", i
);
986 printf (" return CONSTRAINT_%s;\n", c
->c_name
);
991 printf (" if (!strncmp (str, \"%s\", %lu))\n"
992 " return CONSTRAINT_%s;\n",
993 c
->name
, (unsigned long int) c
->namelen
, c
->c_name
);
994 c
= c
->next_this_letter
;
1001 puts (" default: break;\n"
1003 " return CONSTRAINT__UNKNOWN;\n"
1007 /* Write out a function which looks at a string and determines what
1008 the constraint name length is. */
1010 write_insn_constraint_len (void)
1014 puts ("static inline size_t\n"
1015 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
1020 for (i
= 0; i
< ARRAY_SIZE(constraints_by_letter_table
); i
++)
1022 struct constraint_data
*c
= constraints_by_letter_table
[i
];
1028 /* Constraints with multiple characters should have the same
1031 struct constraint_data
*c2
= c
->next_this_letter
;
1032 size_t len
= c
->namelen
;
1035 if (c2
->namelen
!= len
)
1036 error ("Multi-letter constraints with first letter '%c' "
1037 "should have same length", i
);
1038 c2
= c2
->next_this_letter
;
1042 printf (" case '%c': return %lu;\n",
1043 i
, (unsigned long int) c
->namelen
);
1046 puts (" default: break;\n"
1052 /* Write out the function which computes the register class corresponding
1053 to a register constraint. */
1055 write_regclass_for_constraint (void)
1057 struct constraint_data
*c
;
1059 puts ("enum reg_class\n"
1060 "regclass_for_constraint (enum constraint_num c)\n"
1065 FOR_ALL_CONSTRAINTS (c
)
1067 printf (" case CONSTRAINT_%s: return %s;\n", c
->c_name
, c
->regclass
);
1069 puts (" default: break;\n"
1071 " return NO_REGS;\n"
1075 /* Write out the functions which compute whether a given value matches
1076 a given non-register constraint. */
1078 write_tm_constrs_h (void)
1080 struct constraint_data
*c
;
1083 /* Generated automatically by the program '%s'\n\
1084 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1087 #ifndef GCC_TM_CONSTRS_H\n\
1088 #define GCC_TM_CONSTRS_H\n");
1090 FOR_ALL_CONSTRAINTS (c
)
1091 if (!c
->is_register
)
1093 bool needs_ival
= needs_variable (c
->exp
, "ival");
1094 bool needs_hval
= needs_variable (c
->exp
, "hval");
1095 bool needs_lval
= needs_variable (c
->exp
, "lval");
1096 bool needs_rval
= needs_variable (c
->exp
, "rval");
1097 bool needs_mode
= (needs_variable (c
->exp
, "mode")
1098 || needs_hval
|| needs_lval
|| needs_rval
);
1099 bool needs_op
= (needs_variable (c
->exp
, "op")
1100 || needs_ival
|| needs_mode
);
1102 printf ("static inline bool\n"
1103 "satisfies_constraint_%s (rtx %s)\n"
1105 needs_op
? "op" : "ARG_UNUSED (op)");
1107 puts (" enum machine_mode mode = GET_MODE (op);");
1109 puts (" HOST_WIDE_INT ival = 0;");
1111 puts (" HOST_WIDE_INT hval = 0;");
1113 puts (" unsigned HOST_WIDE_INT lval = 0;");
1115 puts (" const REAL_VALUE_TYPE *rval = 0;");
1118 puts (" if (CONST_INT_P (op))\n"
1119 " ival = INTVAL (op);");
1121 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1122 " hval = CONST_DOUBLE_HIGH (op);");
1124 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1125 " lval = CONST_DOUBLE_LOW (op);");
1127 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1128 " rval = CONST_DOUBLE_REAL_VALUE (op);");
1130 write_predicate_stmts (c
->exp
);
1131 fputs ("}\n", stdout
);
1133 puts ("#endif /* tm-constrs.h */");
1136 /* Write out the wrapper function, constraint_satisfied_p, that maps
1137 a CONSTRAINT_xxx constant to one of the predicate functions generated
1140 write_constraint_satisfied_p (void)
1142 struct constraint_data
*c
;
1145 "constraint_satisfied_p (rtx op, enum constraint_num c)\n"
1150 FOR_ALL_CONSTRAINTS (c
)
1151 if (!c
->is_register
)
1152 printf (" case CONSTRAINT_%s: "
1153 "return satisfies_constraint_%s (op);\n",
1154 c
->c_name
, c
->c_name
);
1156 puts (" default: break;\n"
1162 /* Write out the function which computes whether a given value matches
1163 a given CONST_INT constraint. This doesn't just forward to
1164 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1166 write_insn_const_int_ok_for_constraint (void)
1168 struct constraint_data
*c
;
1171 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1172 "enum constraint_num c)\n"
1177 FOR_ALL_CONSTRAINTS (c
)
1178 if (c
->is_const_int
)
1180 printf (" case CONSTRAINT_%s:\n return ", c
->c_name
);
1181 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1182 we know at this point that we have a const_int, so we need not
1183 bother with that part of the test. */
1184 write_predicate_expr (XEXP (c
->exp
, 1));
1185 fputs (";\n\n", stdout
);
1188 puts (" default: break;\n"
1195 /* Write out the function which computes whether a given constraint is
1196 a memory constraint. */
1198 write_insn_extra_memory_constraint (void)
1200 struct constraint_data
*c
;
1203 "insn_extra_memory_constraint (enum constraint_num c)\n"
1208 FOR_ALL_CONSTRAINTS (c
)
1210 printf (" case CONSTRAINT_%s:\n return true;\n\n", c
->c_name
);
1212 puts (" default: break;\n"
1218 /* Write out the function which computes whether a given constraint is
1219 an address constraint. */
1221 write_insn_extra_address_constraint (void)
1223 struct constraint_data
*c
;
1226 "insn_extra_address_constraint (enum constraint_num c)\n"
1231 FOR_ALL_CONSTRAINTS (c
)
1233 printf (" case CONSTRAINT_%s:\n return true;\n\n", c
->c_name
);
1235 puts (" default: break;\n"
1242 /* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1243 an enumeration in portable C, so we have to condition all these
1244 prototypes on HAVE_MACHINE_MODES. */
1246 write_tm_preds_h (void)
1248 struct pred_data
*p
;
1251 /* Generated automatically by the program '%s'\n\
1252 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1255 #ifndef GCC_TM_PREDS_H\n\
1256 #define GCC_TM_PREDS_H\n\
1258 #ifdef HAVE_MACHINE_MODES");
1260 FOR_ALL_PREDICATES (p
)
1261 printf ("extern int %s (rtx, enum machine_mode);\n", p
->name
);
1263 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1265 if (constraint_max_namelen
> 0)
1267 write_enum_constraint_num ();
1268 puts ("extern enum constraint_num lookup_constraint (const char *);\n"
1269 "extern bool constraint_satisfied_p (rtx, enum constraint_num);\n");
1271 if (constraint_max_namelen
> 1)
1273 write_insn_constraint_len ();
1274 puts ("#define CONSTRAINT_LEN(c_,s_) "
1275 "insn_constraint_len (c_,s_)\n");
1278 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1279 if (have_register_constraints
)
1280 puts ("extern enum reg_class regclass_for_constraint "
1281 "(enum constraint_num);\n"
1282 "#define REG_CLASS_FROM_CONSTRAINT(c_,s_) \\\n"
1283 " regclass_for_constraint (lookup_constraint (s_))\n"
1284 "#define REG_CLASS_FOR_CONSTRAINT(x_) \\\n"
1285 " regclass_for_constraint (x_)\n");
1287 puts ("#define REG_CLASS_FROM_CONSTRAINT(c_,s_) NO_REGS\n"
1288 "#define REG_CLASS_FOR_CONSTRAINT(x_) \\\n"
1290 if (have_const_int_constraints
)
1291 puts ("extern bool insn_const_int_ok_for_constraint "
1292 "(HOST_WIDE_INT, enum constraint_num);\n"
1293 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1294 " insn_const_int_ok_for_constraint (v_, "
1295 "lookup_constraint (s_))\n");
1296 if (have_const_dbl_constraints
)
1297 puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1298 " constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1300 puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) 0\n");
1301 if (have_extra_constraints
)
1302 puts ("#define EXTRA_CONSTRAINT_STR(v_,c_,s_) \\\n"
1303 " constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1304 if (have_memory_constraints
)
1305 puts ("extern bool "
1306 "insn_extra_memory_constraint (enum constraint_num);\n"
1307 "#define EXTRA_MEMORY_CONSTRAINT(c_,s_) "
1308 "insn_extra_memory_constraint (lookup_constraint (s_))\n");
1310 puts ("#define EXTRA_MEMORY_CONSTRAINT(c_,s_) false\n");
1311 if (have_address_constraints
)
1312 puts ("extern bool "
1313 "insn_extra_address_constraint (enum constraint_num);\n"
1314 "#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) "
1315 "insn_extra_address_constraint (lookup_constraint (s_))\n");
1317 puts ("#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) false\n");
1320 puts ("#endif /* tm-preds.h */");
1323 /* Write insn-preds.c.
1324 N.B. the list of headers to include was copied from genrecog; it
1327 FUTURE: Write #line markers referring back to the machine
1328 description. (Can't practically do this now since we don't know
1329 the line number of the C block - just the line number of the enclosing
1332 write_insn_preds_c (void)
1334 struct pred_data
*p
;
1337 /* Generated automatically by the program '%s'\n\
1338 from the machine description file '%s'. */\n\n", progname
, in_fname
);
1341 #include \"config.h\"\n\
1342 #include \"system.h\"\n\
1343 #include \"coretypes.h\"\n\
1344 #include \"tm.h\"\n\
1345 #include \"rtl.h\"\n\
1346 #include \"tree.h\"\n\
1347 #include \"tm_p.h\"\n\
1348 #include \"function.h\"\n\
1349 #include \"insn-config.h\"\n\
1350 #include \"recog.h\"\n\
1351 #include \"output.h\"\n\
1352 #include \"flags.h\"\n\
1353 #include \"hard-reg-set.h\"\n\
1354 #include \"resource.h\"\n\
1355 #include \"toplev.h\"\n\
1356 #include \"reload.h\"\n\
1357 #include \"regs.h\"\n\
1358 #include \"tm-constrs.h\"\n");
1360 FOR_ALL_PREDICATES (p
)
1361 write_one_predicate_function (p
);
1363 if (constraint_max_namelen
> 0)
1365 write_lookup_constraint ();
1366 if (have_register_constraints
)
1367 write_regclass_for_constraint ();
1368 write_constraint_satisfied_p ();
1370 if (have_const_int_constraints
)
1371 write_insn_const_int_ok_for_constraint ();
1373 if (have_memory_constraints
)
1374 write_insn_extra_memory_constraint ();
1375 if (have_address_constraints
)
1376 write_insn_extra_address_constraint ();
1380 /* Argument parsing. */
1381 static bool gen_header
;
1382 static bool gen_constrs
;
1385 parse_option (const char *opt
)
1387 if (!strcmp (opt
, "-h"))
1392 else if (!strcmp (opt
, "-c"))
1401 /* Master control. */
1403 main (int argc
, char **argv
)
1406 int pattern_lineno
, next_insn_code
= 0;
1410 fatal ("no input file name");
1411 if (init_md_reader_args_cb (argc
, argv
, parse_option
) != SUCCESS_EXIT_CODE
)
1412 return FATAL_EXIT_CODE
;
1414 while ((defn
= read_md_rtx (&pattern_lineno
, &next_insn_code
)) != 0)
1415 switch (GET_CODE (defn
))
1417 case DEFINE_PREDICATE
:
1418 case DEFINE_SPECIAL_PREDICATE
:
1419 process_define_predicate (defn
, pattern_lineno
);
1422 case DEFINE_CONSTRAINT
:
1423 case DEFINE_MEMORY_CONSTRAINT
:
1424 case DEFINE_ADDRESS_CONSTRAINT
:
1425 process_define_constraint (defn
, pattern_lineno
);
1428 case DEFINE_REGISTER_CONSTRAINT
:
1429 process_define_register_constraint (defn
, pattern_lineno
);
1437 write_tm_preds_h ();
1438 else if (gen_constrs
)
1439 write_tm_constrs_h ();
1441 write_insn_preds_c ();
1443 if (have_error
|| ferror (stdout
) || fflush (stdout
) || fclose (stdout
))
1444 return FATAL_EXIT_CODE
;
1446 return SUCCESS_EXIT_CODE
;