xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/genpreds.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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-2015 Free Software Foundation, Inc.
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13 
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "obstack.h"
30 #include "read-md.h"
31 #include "gensupport.h"
32 
33 static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
34 
35 /* Given a predicate expression EXP, from form NAME at 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)
41 {
42   if (exp == 0)
43     {
44       message_with_line (lineno, "%s: must give a predicate expression", name);
45       return true;
46     }
47 
48   switch (GET_CODE (exp))
49     {
50       /* Ternary, binary, unary expressions: recurse into subexpressions.  */
51     case IF_THEN_ELSE:
52       if (validate_exp (XEXP (exp, 2), name, 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);
62 
63       /* MATCH_CODE might have a syntax error in its path expression.  */
64     case MATCH_CODE:
65       {
66 	const char *p;
67 	for (p = XSTR (exp, 1); *p; p++)
68 	  {
69 	    if (!ISDIGIT (*p) && !ISLOWER (*p))
70 	      {
71 		error_with_line (lineno, "%s: invalid character in path "
72 				 "string '%s'", name, XSTR (exp, 1));
73 		return true;
74 	      }
75 	  }
76       }
77       /* fall through */
78 
79       /* These need no special checking.  */
80     case MATCH_OPERAND:
81     case MATCH_TEST:
82       return false;
83 
84     default:
85       error_with_line (lineno,
86 		       "%s: cannot use '%s' in a predicate expression",
87 		       name, GET_RTX_NAME (GET_CODE (exp)));
88       return true;
89     }
90 }
91 
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)
96 {
97   validate_exp (XEXP (defn, 1), XSTR (defn, 0), lineno);
98 }
99 
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,
103 
104        (define_predicate "basereg_operand"
105          (match_operand 0 "register_operand")
106        {
107          if (GET_CODE (op) == SUBREG)
108            op = SUBREG_REG (op);
109          return REG_POINTER (op);
110        })
111 
112    becomes
113 
114        static inline int basereg_operand_1(rtx op, machine_mode mode)
115        {
116          if (GET_CODE (op) == SUBREG)
117            op = SUBREG_REG (op);
118          return REG_POINTER (op);
119        }
120 
121        (define_predicate "basereg_operand"
122          (and (match_operand 0 "register_operand")
123 	      (match_test "basereg_operand_1 (op, mode)")))
124 
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.  */
127 
128 
129 static void
130 write_predicate_subfunction (struct pred_data *p)
131 {
132   const char *match_test_str;
133   rtx match_test_exp, and_exp;
134 
135   if (p->c_block[0] == '\0')
136     return;
137 
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 *);
143 
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;
148 
149   and_exp = rtx_alloc (AND);
150   XEXP (and_exp, 0) = p->exp;
151   XEXP (and_exp, 1) = match_test_exp;
152 
153   p->exp = and_exp;
154 
155   printf ("static inline int\n"
156 	  "%s_1 (rtx op, 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);
164 }
165 
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)
170 {
171   switch (GET_CODE (exp))
172     {
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);
186 
187       /* MATCH_CODE uses "op", but nothing else.  */
188     case MATCH_CODE:
189       return !strcmp (var, "op");
190 
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;
198 
199       /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
200     case MATCH_TEST:
201       {
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;
211       }
212       return true;
213 
214     default:
215       gcc_unreachable ();
216     }
217 }
218 
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.
226 
227    The mark is the RTL /v flag, which is true for subexpressions which
228    do *not* perform mode tests.
229 */
230 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
231 static void
232 mark_mode_tests (rtx exp)
233 {
234   switch (GET_CODE (exp))
235     {
236     case MATCH_OPERAND:
237       {
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;
243       }
244       break;
245 
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;
252 
253     case MATCH_TEST:
254     case NOT:
255       NO_MODE_TEST (exp) = 1;
256       break;
257 
258     case AND:
259       mark_mode_tests (XEXP (exp, 0));
260       mark_mode_tests (XEXP (exp, 1));
261 
262       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
263 			    && NO_MODE_TEST (XEXP (exp, 1)));
264       break;
265 
266     case IOR:
267       mark_mode_tests (XEXP (exp, 0));
268       mark_mode_tests (XEXP (exp, 1));
269 
270       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
271 			    || NO_MODE_TEST (XEXP (exp, 1)));
272       break;
273 
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));
280 
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;
285 
286     default:
287       gcc_unreachable ();
288     }
289 }
290 
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)
295 {
296   return GET_CODE (exp) == MATCH_CODE
297 	 && strchr (XSTR (exp, 0), ',');
298 }
299 
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.  */
305 
306 static void
307 add_mode_tests (struct pred_data *p)
308 {
309   rtx match_test_exp, and_exp;
310   rtx *pos;
311 
312   /* Don't touch special predicates.  */
313   if (p->special)
314     return;
315 
316   mark_mode_tests (p->exp);
317 
318   /* If the whole expression already tests the mode, we're done.  */
319   if (!NO_MODE_TEST (p->exp))
320     return;
321 
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;
326 
327   /* It is always correct to rewrite p->exp as
328 
329         (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
330 
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.  */
337 
338   pos = &p->exp;
339   for (;;)
340     {
341       rtx subexp = *pos;
342 
343       switch (GET_CODE (subexp))
344 	{
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;
353 
354 	case IOR:
355 	  {
356 	    int test0 = NO_MODE_TEST (XEXP (subexp, 0));
357 	    int test1 = NO_MODE_TEST (XEXP (subexp, 1));
358 
359 	    gcc_assert (test0 || test1);
360 
361 	    if (test0 && test1)
362 	      goto break_loop;
363 	    pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
364 	  }
365 	  break;
366 
367 	case IF_THEN_ELSE:
368 	  {
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));
372 
373 	    gcc_assert ((test0 && test1) || test2);
374 
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);
384 	  }
385 	  break;
386 
387 	default:
388 	  goto break_loop;
389 	}
390     }
391  break_loop:
392   XEXP (and_exp, 0) = *pos;
393   *pos = and_exp;
394 }
395 
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)
402 {
403   int len = strlen (path);
404   int i;
405 
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--)
409     {
410       if (ISLOWER (path[i]))
411 	fputs ("XVECEXP (", stdout);
412       else if (ISDIGIT (path[i]))
413 	fputs ("XEXP (", stdout);
414       else
415 	gcc_unreachable ();
416     }
417 
418   fputs ("op", stdout);
419 
420   for (i = 0; i < len; i++)
421     {
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 ();
428     }
429 }
430 
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)
435 {
436   const char *code;
437 
438   while ((code = scan_comma_elt (&codes)) != 0)
439     {
440       fputs ("GET_CODE (", stdout);
441       write_extract_subexp (path);
442       fputs (") == ", stdout);
443       while (code < codes)
444 	{
445 	  putchar (TOUPPER (*code));
446 	  code++;
447 	}
448 
449       if (*codes == ',')
450 	fputs (" || ", stdout);
451     }
452 }
453 
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)
458 {
459   switch (GET_CODE (exp))
460     {
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;
468 
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;
476 
477     case NOT:
478       fputs ("!(", stdout);
479       write_predicate_expr (XEXP (exp, 0));
480       putchar (')');
481       break;
482 
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;
492 
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;
499 
500     case MATCH_CODE:
501       write_match_code (XSTR (exp, 1), XSTR (exp, 0));
502       break;
503 
504     case MATCH_TEST:
505       print_c_condition (XSTR (exp, 0));
506       break;
507 
508     default:
509       gcc_unreachable ();
510     }
511 }
512 
513 /* Write the MATCH_CODE expression EXP as a switch statement.  */
514 
515 static void
516 write_match_code_switch (rtx exp)
517 {
518   const char *codes = XSTR (exp, 0);
519   const char *path = XSTR (exp, 1);
520   const char *code;
521 
522   fputs ("  switch (GET_CODE (", stdout);
523   write_extract_subexp (path);
524   fputs ("))\n    {\n", stdout);
525 
526   while ((code = scan_comma_elt (&codes)) != 0)
527     {
528       fputs ("    case ", stdout);
529       while (code < codes)
530 	{
531 	  putchar (TOUPPER (*code));
532 	  code++;
533 	}
534       fputs (":\n", stdout);
535     }
536 }
537 
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.  */
541 
542 static void
543 write_predicate_stmts (rtx exp)
544 {
545   switch (GET_CODE (exp))
546     {
547     case MATCH_CODE:
548       if (generate_switch_p (exp))
549 	{
550 	  write_match_code_switch (exp);
551 	  puts ("      return true;\n"
552 		"    default:\n"
553 		"      break;\n"
554 		"    }\n"
555 		"  return false;");
556 	  return;
557 	}
558       break;
559 
560     case AND:
561       if (generate_switch_p (XEXP (exp, 0)))
562 	{
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);
569 	}
570       break;
571 
572     case IOR:
573       if (generate_switch_p (XEXP (exp, 0)))
574 	{
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);
581 	}
582       break;
583 
584     case NOT:
585       if (generate_switch_p (XEXP (exp, 0)))
586 	{
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;
594 	}
595       break;
596 
597     default:
598       break;
599     }
600 
601   fputs ("  return ",stdout);
602   write_predicate_expr (exp);
603   fputs (";\n", stdout);
604 }
605 
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)
609 {
610   if (!p->exp)
611     return;
612 
613   write_predicate_subfunction (p);
614   add_mode_tests (p);
615 
616   /* A normal predicate can legitimately not look at 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, machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
619 	  p->name);
620   write_predicate_stmts (p->exp);
621   fputs ("}\n\n", stdout);
622 }
623 
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.  */
631 
632 /* All data from one constraint definition.  */
633 struct constraint_data
634 {
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;
649   unsigned int maybe_allows_reg : 1;
650   unsigned int maybe_allows_mem : 1;
651 };
652 
653 /* Overview of all constraints beginning with a given letter.  */
654 
655 static struct constraint_data *
656 constraints_by_letter_table[1<<CHAR_BIT];
657 
658 /* For looking up all the constraints in the order that they appeared
659    in the machine description.  */
660 static struct constraint_data *first_constraint;
661 static struct constraint_data **last_constraint_ptr = &first_constraint;
662 
663 #define FOR_ALL_CONSTRAINTS(iter_) \
664   for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
665 
666 /* Contraint letters that have a special meaning and that cannot be used
667    in define*_constraints.  */
668 static const char generic_constraint_letters[] = "g";
669 
670 /* Machine-independent code expects that constraints with these
671    (initial) letters will allow only (a subset of all) CONST_INTs.  */
672 
673 static const char const_int_constraints[] = "IJKLMNOP";
674 
675 /* Machine-independent code expects that constraints with these
676    (initial) letters will allow only (a subset of all) CONST_DOUBLEs.  */
677 
678 static const char const_dbl_constraints[] = "GH";
679 
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 unsigned int num_constraints;
689 
690 static const constraint_data **enum_order;
691 static unsigned int register_start, register_end;
692 static unsigned int satisfied_start;
693 static unsigned int const_int_start, const_int_end;
694 static unsigned int memory_start, memory_end;
695 static unsigned int address_start, address_end;
696 static unsigned int maybe_allows_none_start, maybe_allows_none_end;
697 static unsigned int maybe_allows_reg_start, maybe_allows_reg_end;
698 static unsigned int maybe_allows_mem_start, maybe_allows_mem_end;
699 
700 /* Convert NAME, which contains angle brackets and/or underscores, to
701    a string that can be used as part of a C identifier.  The string
702    comes from the rtl_obstack.  */
703 static const char *
704 mangle (const char *name)
705 {
706   for (; *name; name++)
707     switch (*name)
708       {
709       case '_': obstack_grow (rtl_obstack, "__", 2); break;
710       case '<':	obstack_grow (rtl_obstack, "_l", 2); break;
711       case '>':	obstack_grow (rtl_obstack, "_g", 2); break;
712       default: obstack_1grow (rtl_obstack, *name); break;
713       }
714 
715   obstack_1grow (rtl_obstack, '\0');
716   return XOBFINISH (rtl_obstack, const char *);
717 }
718 
719 /* Return a bitmask, bit 1 if EXP maybe allows a REG/SUBREG, 2 if EXP
720    maybe allows a MEM.  Bits should be clear only when we are sure it
721    will not allow a REG/SUBREG or a MEM.  */
722 static int
723 compute_maybe_allows (rtx exp)
724 {
725   switch (GET_CODE (exp))
726     {
727     case IF_THEN_ELSE:
728       /* Conservative answer is like IOR, of the THEN and ELSE branches.  */
729       return compute_maybe_allows (XEXP (exp, 1))
730 	     | compute_maybe_allows (XEXP (exp, 2));
731     case AND:
732       return compute_maybe_allows (XEXP (exp, 0))
733 	     & compute_maybe_allows (XEXP (exp, 1));
734     case IOR:
735       return compute_maybe_allows (XEXP (exp, 0))
736 	     | compute_maybe_allows (XEXP (exp, 1));
737     case MATCH_CODE:
738       if (*XSTR (exp, 1) == '\0')
739 	return (strstr (XSTR (exp, 0), "reg") != NULL ? 1 : 0)
740 	       | (strstr (XSTR (exp, 0), "mem") != NULL ? 2 : 0);
741       /* FALLTHRU */
742     default:
743       return 3;
744     }
745 }
746 
747 /* Add one constraint, of any sort, to the tables.  NAME is its name;
748    REGCLASS is the register class, if any; EXP is the expression to
749    test, if any;  IS_MEMORY and IS_ADDRESS indicate memory and address
750    constraints, respectively; LINENO is the line number from the MD reader.
751    Not all combinations of arguments are valid; most importantly, REGCLASS
752    is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
753    meaningful for constraints with EXP.
754 
755    This function enforces all syntactic and semantic rules about what
756    constraints can be defined.  */
757 
758 static void
759 add_constraint (const char *name, const char *regclass,
760 		rtx exp, bool is_memory, bool is_address,
761 		int lineno)
762 {
763   struct constraint_data *c, **iter, **slot;
764   const char *p;
765   bool need_mangled_name = false;
766   bool is_const_int;
767   bool is_const_dbl;
768   size_t namelen;
769 
770   if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
771     name = general_mem;
772 
773   if (exp && validate_exp (exp, name, lineno))
774     return;
775 
776   for (p = name; *p; p++)
777     if (!ISALNUM (*p))
778       {
779 	if (*p == '<' || *p == '>' || *p == '_')
780 	  need_mangled_name = true;
781 	else
782 	  {
783 	    error_with_line (lineno,
784 			     "constraint name '%s' must be composed of "
785 			     "letters, digits, underscores, and "
786 			     "angle brackets", name);
787 	    return;
788 	  }
789       }
790 
791   if (strchr (generic_constraint_letters, name[0]))
792     {
793       if (name[1] == '\0')
794 	error_with_line (lineno, "constraint letter '%s' cannot be "
795 			 "redefined by the machine description", name);
796       else
797 	error_with_line (lineno, "constraint name '%s' cannot be defined by "
798 			 "the machine description, as it begins with '%c'",
799 			 name, name[0]);
800       return;
801     }
802 
803 
804   namelen = strlen (name);
805   slot = &constraints_by_letter_table[(unsigned int)name[0]];
806   for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
807     {
808       /* This causes slot to end up pointing to the
809 	 next_this_letter field of the last constraint with a name
810 	 of equal or greater length than the new constraint; hence
811 	 the new constraint will be inserted after all previous
812 	 constraints with names of the same length.  */
813       if ((*iter)->namelen >= namelen)
814 	slot = iter;
815 
816       if (!strcmp ((*iter)->name, name))
817 	{
818 	  error_with_line (lineno, "redefinition of constraint '%s'", name);
819 	  message_with_line ((*iter)->lineno, "previous definition is here");
820 	  return;
821 	}
822       else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
823 	{
824 	  error_with_line (lineno, "defining constraint '%s' here", name);
825 	  message_with_line ((*iter)->lineno, "renders constraint '%s' "
826 			     "(defined here) a prefix", (*iter)->name);
827 	  return;
828 	}
829       else if (!strncmp ((*iter)->name, name, namelen))
830 	{
831 	  error_with_line (lineno, "constraint '%s' is a prefix", name);
832 	  message_with_line ((*iter)->lineno, "of constraint '%s' "
833 			     "(defined here)", (*iter)->name);
834 	  return;
835 	}
836     }
837 
838   is_const_int = strchr (const_int_constraints, name[0]) != 0;
839   is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
840 
841   if (is_const_int || is_const_dbl)
842     {
843       enum rtx_code appropriate_code
844 	= is_const_int ? CONST_INT : CONST_DOUBLE;
845 
846       /* Consider relaxing this requirement in the future.  */
847       if (regclass
848 	  || GET_CODE (exp) != AND
849 	  || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
850 	  || strcmp (XSTR (XEXP (exp, 0), 0),
851 		     GET_RTX_NAME (appropriate_code)))
852 	{
853 	  if (name[1] == '\0')
854 	    error_with_line (lineno, "constraint letter '%c' is reserved "
855 			     "for %s constraints",
856 			     name[0], GET_RTX_NAME (appropriate_code));
857 	  else
858 	    error_with_line (lineno, "constraint names beginning with '%c' "
859 			     "(%s) are reserved for %s constraints",
860 			     name[0], name, GET_RTX_NAME (appropriate_code));
861 	  return;
862 	}
863 
864       if (is_memory)
865 	{
866 	  if (name[1] == '\0')
867 	    error_with_line (lineno, "constraint letter '%c' cannot be a "
868 			     "memory constraint", name[0]);
869 	  else
870 	    error_with_line (lineno, "constraint name '%s' begins with '%c', "
871 			     "and therefore cannot be a memory constraint",
872 			     name, name[0]);
873 	  return;
874 	}
875       else if (is_address)
876 	{
877 	  if (name[1] == '\0')
878 	    error_with_line (lineno, "constraint letter '%c' cannot be a "
879 			     "memory constraint", name[0]);
880 	  else
881 	    error_with_line (lineno, "constraint name '%s' begins with '%c', "
882 			     "and therefore cannot be a memory constraint",
883 			     name, name[0]);
884 	  return;
885 	}
886     }
887 
888 
889   c = XOBNEW (rtl_obstack, struct constraint_data);
890   c->name = name;
891   c->c_name = need_mangled_name ? mangle (name) : name;
892   c->lineno = lineno;
893   c->namelen = namelen;
894   c->regclass = regclass;
895   c->exp = exp;
896   c->is_register = regclass != 0;
897   c->is_const_int = is_const_int;
898   c->is_const_dbl = is_const_dbl;
899   c->is_extra = !(regclass || is_const_int || is_const_dbl);
900   c->is_memory = is_memory;
901   c->is_address = is_address;
902   int maybe_allows = 3;
903   if (exp)
904     maybe_allows = compute_maybe_allows (exp);
905   c->maybe_allows_reg = (maybe_allows & 1) != 0;
906   c->maybe_allows_mem = (maybe_allows & 2) != 0;
907 
908   c->next_this_letter = *slot;
909   *slot = c;
910 
911   /* Insert this constraint in the list of all constraints in textual
912      order.  */
913   c->next_textual = 0;
914   *last_constraint_ptr = c;
915   last_constraint_ptr = &c->next_textual;
916 
917   constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
918   have_register_constraints |= c->is_register;
919   have_const_int_constraints |= c->is_const_int;
920   have_extra_constraints |= c->is_extra;
921   have_memory_constraints |= c->is_memory;
922   have_address_constraints |= c->is_address;
923   num_constraints += 1;
924 }
925 
926 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
927    DEFINE_ADDRESS_CONSTRAINT expression, C.  */
928 static void
929 process_define_constraint (rtx c, int lineno)
930 {
931   add_constraint (XSTR (c, 0), 0, XEXP (c, 2),
932 		  GET_CODE (c) == DEFINE_MEMORY_CONSTRAINT,
933 		  GET_CODE (c) == DEFINE_ADDRESS_CONSTRAINT,
934 		  lineno);
935 }
936 
937 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C.  */
938 static void
939 process_define_register_constraint (rtx c, int lineno)
940 {
941   add_constraint (XSTR (c, 0), XSTR (c, 1), 0, false, false, lineno);
942 }
943 
944 /* Put the constraints into enum order.  We want to keep constraints
945    of the same type together so that query functions can be simple
946    range checks.  */
947 static void
948 choose_enum_order (void)
949 {
950   struct constraint_data *c;
951 
952   enum_order = XNEWVEC (const constraint_data *, num_constraints);
953   unsigned int next = 0;
954 
955   register_start = next;
956   FOR_ALL_CONSTRAINTS (c)
957     if (c->is_register)
958       enum_order[next++] = c;
959   register_end = next;
960 
961   satisfied_start = next;
962 
963   const_int_start = next;
964   FOR_ALL_CONSTRAINTS (c)
965     if (c->is_const_int)
966       enum_order[next++] = c;
967   const_int_end = next;
968 
969   memory_start = next;
970   FOR_ALL_CONSTRAINTS (c)
971     if (c->is_memory)
972       enum_order[next++] = c;
973   memory_end = next;
974 
975   address_start = next;
976   FOR_ALL_CONSTRAINTS (c)
977     if (c->is_address)
978       enum_order[next++] = c;
979   address_end = next;
980 
981   maybe_allows_none_start = next;
982   FOR_ALL_CONSTRAINTS (c)
983     if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
984 	&& !c->maybe_allows_reg && !c->maybe_allows_mem)
985       enum_order[next++] = c;
986   maybe_allows_none_end = next;
987 
988   maybe_allows_reg_start = next;
989   FOR_ALL_CONSTRAINTS (c)
990     if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
991 	&& c->maybe_allows_reg && !c->maybe_allows_mem)
992       enum_order[next++] = c;
993   maybe_allows_reg_end = next;
994 
995   maybe_allows_mem_start = next;
996   FOR_ALL_CONSTRAINTS (c)
997     if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
998 	&& !c->maybe_allows_reg && c->maybe_allows_mem)
999       enum_order[next++] = c;
1000   maybe_allows_mem_end = next;
1001 
1002   FOR_ALL_CONSTRAINTS (c)
1003     if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1004 	&& c->maybe_allows_reg && c->maybe_allows_mem)
1005       enum_order[next++] = c;
1006   gcc_assert (next == num_constraints);
1007 }
1008 
1009 /* Write out an enumeration with one entry per machine-specific
1010    constraint.  */
1011 static void
1012 write_enum_constraint_num (void)
1013 {
1014   fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
1015   fputs ("enum constraint_num\n"
1016 	 "{\n"
1017 	 "  CONSTRAINT__UNKNOWN = 0", stdout);
1018   for (unsigned int i = 0; i < num_constraints; ++i)
1019     printf (",\n  CONSTRAINT_%s", enum_order[i]->c_name);
1020   puts (",\n  CONSTRAINT__LIMIT\n};\n");
1021 }
1022 
1023 /* Write out a function which looks at a string and determines what
1024    constraint name, if any, it begins with.  */
1025 static void
1026 write_lookup_constraint_1 (void)
1027 {
1028   unsigned int i;
1029   puts ("enum constraint_num\n"
1030 	"lookup_constraint_1 (const char *str)\n"
1031 	"{\n"
1032 	"  switch (str[0])\n"
1033 	"    {");
1034 
1035   for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1036     {
1037       struct constraint_data *c = constraints_by_letter_table[i];
1038       if (!c)
1039 	continue;
1040 
1041       printf ("    case '%c':\n", i);
1042       if (c->namelen == 1)
1043 	printf ("      return CONSTRAINT_%s;\n", c->c_name);
1044       else
1045 	{
1046 	  do
1047 	    {
1048 	      printf ("      if (!strncmp (str + 1, \"%s\", %lu))\n"
1049 		      "        return CONSTRAINT_%s;\n",
1050 		      c->name + 1, (unsigned long int) c->namelen - 1,
1051 		      c->c_name);
1052 	      c = c->next_this_letter;
1053 	    }
1054 	  while (c);
1055 	  puts ("      break;");
1056 	}
1057     }
1058 
1059   puts ("    default: break;\n"
1060 	"    }\n"
1061 	"  return CONSTRAINT__UNKNOWN;\n"
1062 	"}\n");
1063 }
1064 
1065 /* Write out an array that maps single-letter characters to their
1066    constraints (if that fits in a character) or 255 if lookup_constraint_1
1067    must be called.  */
1068 static void
1069 write_lookup_constraint_array (void)
1070 {
1071   unsigned int i;
1072   printf ("const unsigned char lookup_constraint_array[] = {\n  ");
1073   for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1074     {
1075       if (i != 0)
1076 	printf (",\n  ");
1077       struct constraint_data *c = constraints_by_letter_table[i];
1078       if (!c)
1079 	printf ("CONSTRAINT__UNKNOWN");
1080       else if (c->namelen == 1)
1081 	printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1082       else
1083 	printf ("UCHAR_MAX");
1084     }
1085   printf ("\n};\n\n");
1086 }
1087 
1088 /* Write out a function which looks at a string and determines what
1089    the constraint name length is.  */
1090 static void
1091 write_insn_constraint_len (void)
1092 {
1093   unsigned int i;
1094 
1095   puts ("static inline size_t\n"
1096 	"insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
1097 	"{\n"
1098 	"  switch (fc)\n"
1099 	"    {");
1100 
1101   for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1102     {
1103       struct constraint_data *c = constraints_by_letter_table[i];
1104 
1105       if (!c
1106       	  || c->namelen == 1)
1107 	continue;
1108 
1109       /* Constraints with multiple characters should have the same
1110 	 length.  */
1111       {
1112 	struct constraint_data *c2 = c->next_this_letter;
1113 	size_t len = c->namelen;
1114 	while (c2)
1115 	  {
1116 	    if (c2->namelen != len)
1117 	      error ("Multi-letter constraints with first letter '%c' "
1118 		     "should have same length", i);
1119 	    c2 = c2->next_this_letter;
1120 	  }
1121       }
1122 
1123       printf ("    case '%c': return %lu;\n",
1124 	      i, (unsigned long int) c->namelen);
1125     }
1126 
1127   puts ("    default: break;\n"
1128 	"    }\n"
1129 	"  return 1;\n"
1130 	"}\n");
1131 }
1132 
1133 /* Write out the function which computes the register class corresponding
1134    to a register constraint.  */
1135 static void
1136 write_reg_class_for_constraint_1 (void)
1137 {
1138   struct constraint_data *c;
1139 
1140   puts ("enum reg_class\n"
1141 	"reg_class_for_constraint_1 (enum constraint_num c)\n"
1142 	"{\n"
1143 	"  switch (c)\n"
1144 	"    {");
1145 
1146   FOR_ALL_CONSTRAINTS (c)
1147     if (c->is_register)
1148       printf ("    case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1149 
1150   puts ("    default: break;\n"
1151 	"    }\n"
1152 	"  return NO_REGS;\n"
1153 	"}\n");
1154 }
1155 
1156 /* Write out the functions which compute whether a given value matches
1157    a given non-register constraint.  */
1158 static void
1159 write_tm_constrs_h (void)
1160 {
1161   struct constraint_data *c;
1162 
1163   printf ("\
1164 /* Generated automatically by the program '%s'\n\
1165    from the machine description file '%s'.  */\n\n", progname, in_fname);
1166 
1167   puts ("\
1168 #ifndef GCC_TM_CONSTRS_H\n\
1169 #define GCC_TM_CONSTRS_H\n");
1170 
1171   FOR_ALL_CONSTRAINTS (c)
1172     if (!c->is_register)
1173       {
1174 	bool needs_ival = needs_variable (c->exp, "ival");
1175 	bool needs_hval = needs_variable (c->exp, "hval");
1176 	bool needs_lval = needs_variable (c->exp, "lval");
1177 	bool needs_rval = needs_variable (c->exp, "rval");
1178 	bool needs_mode = (needs_variable (c->exp, "mode")
1179 			   || needs_hval || needs_lval || needs_rval);
1180 	bool needs_op = (needs_variable (c->exp, "op")
1181 			 || needs_ival || needs_mode);
1182 
1183 	printf ("static inline bool\n"
1184 		"satisfies_constraint_%s (rtx %s)\n"
1185 		"{\n", c->c_name,
1186 		needs_op ? "op" : "ARG_UNUSED (op)");
1187 	if (needs_mode)
1188 	  puts ("  machine_mode mode = GET_MODE (op);");
1189 	if (needs_ival)
1190 	  puts ("  HOST_WIDE_INT ival = 0;");
1191 	if (needs_hval)
1192 	  puts ("  HOST_WIDE_INT hval = 0;");
1193 	if (needs_lval)
1194 	  puts ("  unsigned HOST_WIDE_INT lval = 0;");
1195 	if (needs_rval)
1196 	  puts ("  const REAL_VALUE_TYPE *rval = 0;");
1197 
1198 	if (needs_ival)
1199 	  puts ("  if (CONST_INT_P (op))\n"
1200 		"    ival = INTVAL (op);");
1201 #if TARGET_SUPPORTS_WIDE_INT
1202 	if (needs_lval || needs_hval)
1203 	  error ("you can't use lval or hval");
1204 #else
1205 	if (needs_hval)
1206 	  puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1207 		"    hval = CONST_DOUBLE_HIGH (op);");
1208 	if (needs_lval)
1209 	  puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1210 		"    lval = CONST_DOUBLE_LOW (op);");
1211 #endif
1212 	if (needs_rval)
1213 	  puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1214 		"    rval = CONST_DOUBLE_REAL_VALUE (op);");
1215 
1216 	write_predicate_stmts (c->exp);
1217 	fputs ("}\n", stdout);
1218       }
1219   puts ("#endif /* tm-constrs.h */");
1220 }
1221 
1222 /* Write out the wrapper function, constraint_satisfied_p, that maps
1223    a CONSTRAINT_xxx constant to one of the predicate functions generated
1224    above.  */
1225 static void
1226 write_constraint_satisfied_p_array (void)
1227 {
1228   if (satisfied_start == num_constraints)
1229     return;
1230 
1231   printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n  ");
1232   for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1233     {
1234       if (i != satisfied_start)
1235 	printf (",\n  ");
1236       printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1237     }
1238   printf ("\n};\n\n");
1239 }
1240 
1241 /* Write out the function which computes whether a given value matches
1242    a given CONST_INT constraint.  This doesn't just forward to
1243    constraint_satisfied_p because caller passes the INTVAL, not the RTX.  */
1244 static void
1245 write_insn_const_int_ok_for_constraint (void)
1246 {
1247   struct constraint_data *c;
1248 
1249   puts ("bool\n"
1250 	"insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1251 	                                  "enum constraint_num c)\n"
1252 	"{\n"
1253 	"  switch (c)\n"
1254 	"    {");
1255 
1256   FOR_ALL_CONSTRAINTS (c)
1257     if (c->is_const_int)
1258       {
1259 	printf ("    case CONSTRAINT_%s:\n      return ", c->c_name);
1260 	/* c->exp is guaranteed to be (and (match_code "const_int") (...));
1261 	   we know at this point that we have a const_int, so we need not
1262 	   bother with that part of the test.  */
1263 	write_predicate_expr (XEXP (c->exp, 1));
1264 	fputs (";\n\n", stdout);
1265       }
1266 
1267   puts ("    default: break;\n"
1268 	"    }\n"
1269 	"  return false;\n"
1270 	"}\n");
1271 }
1272 
1273 /* Write a definition for a function NAME that returns true if a given
1274    constraint_num is in the range [START, END).  */
1275 static void
1276 write_range_function (const char *name, unsigned int start, unsigned int end)
1277 {
1278   printf ("static inline bool\n");
1279   if (start != end)
1280     printf ("%s (enum constraint_num c)\n"
1281 	    "{\n"
1282 	    "  return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1283 	    "}\n\n",
1284 	    name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1285   else
1286     printf ("%s (enum constraint_num)\n"
1287 	    "{\n"
1288 	    "  return false;\n"
1289 	    "}\n\n", name);
1290 }
1291 
1292 /* Write a definition for insn_extra_constraint_allows_reg_mem function.  */
1293 static void
1294 write_allows_reg_mem_function (void)
1295 {
1296   printf ("static inline void\n"
1297 	  "insn_extra_constraint_allows_reg_mem (enum constraint_num c,\n"
1298 	  "\t\t\t\t      bool *allows_reg, bool *allows_mem)\n"
1299 	  "{\n");
1300   if (maybe_allows_none_start != maybe_allows_none_end)
1301     printf ("  if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1302 	    "    return;\n",
1303 	    enum_order[maybe_allows_none_start]->c_name,
1304 	    enum_order[maybe_allows_none_end - 1]->c_name);
1305   if (maybe_allows_reg_start != maybe_allows_reg_end)
1306     printf ("  if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1307 	    "    {\n"
1308 	    "      *allows_reg = true;\n"
1309 	    "      return;\n"
1310 	    "    }\n",
1311 	    enum_order[maybe_allows_reg_start]->c_name,
1312 	    enum_order[maybe_allows_reg_end - 1]->c_name);
1313   if (maybe_allows_mem_start != maybe_allows_mem_end)
1314     printf ("  if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1315 	    "    {\n"
1316 	    "      *allows_mem = true;\n"
1317 	    "      return;\n"
1318 	    "    }\n",
1319 	    enum_order[maybe_allows_mem_start]->c_name,
1320 	    enum_order[maybe_allows_mem_end - 1]->c_name);
1321   printf ("  (void) c;\n"
1322 	  "  *allows_reg = true;\n"
1323 	  "  *allows_mem = true;\n"
1324 	  "}\n\n");
1325 }
1326 
1327 /* VEC is a list of key/value pairs, with the keys being lower bounds
1328    of a range.  Output a decision tree that handles the keys covered by
1329    [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1330    INDENT is the number of spaces to indent the code.  */
1331 static void
1332 print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1333 		 unsigned int start, unsigned int end, const char *fallback,
1334 		 unsigned int indent)
1335 {
1336   while (start < end)
1337     {
1338       unsigned int mid = (start + end) / 2;
1339       printf ("%*sif (c >= CONSTRAINT_%s)\n",
1340 	      indent, "", enum_order[vec[mid].first]->c_name);
1341       if (mid + 1 == end)
1342 	print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1343       else
1344 	{
1345 	  printf ("%*s{\n", indent + 2, "");
1346 	  print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1347 	  printf ("%*s}\n", indent + 2, "");
1348 	}
1349       end = mid;
1350     }
1351   printf ("%*sreturn %s;\n", indent, "", fallback);
1352 }
1353 
1354 /* Write tm-preds.h.  Unfortunately, it is impossible to forward-declare
1355    an enumeration in portable C, so we have to condition all these
1356    prototypes on HAVE_MACHINE_MODES.  */
1357 static void
1358 write_tm_preds_h (void)
1359 {
1360   struct pred_data *p;
1361 
1362   printf ("\
1363 /* Generated automatically by the program '%s'\n\
1364    from the machine description file '%s'.  */\n\n", progname, in_fname);
1365 
1366   puts ("\
1367 #ifndef GCC_TM_PREDS_H\n\
1368 #define GCC_TM_PREDS_H\n\
1369 \n\
1370 #ifdef HAVE_MACHINE_MODES");
1371 
1372   FOR_ALL_PREDICATES (p)
1373     printf ("extern int %s (rtx, machine_mode);\n", p->name);
1374 
1375   puts ("#endif /* HAVE_MACHINE_MODES */\n");
1376 
1377   if (constraint_max_namelen > 0)
1378     {
1379       write_enum_constraint_num ();
1380       puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1381 	    "extern const unsigned char lookup_constraint_array[];\n"
1382 	    "\n"
1383 	    "/* Return the constraint at the beginning of P, or"
1384 	    " CONSTRAINT__UNKNOWN if it\n"
1385 	    "   isn't recognized.  */\n"
1386 	    "\n"
1387 	    "static inline enum constraint_num\n"
1388 	    "lookup_constraint (const char *p)\n"
1389 	    "{\n"
1390 	    "  unsigned int index = lookup_constraint_array"
1391 	    "[(unsigned char) *p];\n"
1392 	    "  return (index == UCHAR_MAX\n"
1393 	    "          ? lookup_constraint_1 (p)\n"
1394 	    "          : (enum constraint_num) index);\n"
1395 	    "}\n");
1396       if (satisfied_start == num_constraints)
1397 	puts ("/* Return true if X satisfies constraint C.  */\n"
1398 	      "\n"
1399 	      "static inline bool\n"
1400 	      "constraint_satisfied_p (rtx, enum constraint_num)\n"
1401 	      "{\n"
1402 	      "  return false;\n"
1403 	      "}\n");
1404       else
1405 	printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
1406 		"\n"
1407 		"/* Return true if X satisfies constraint C.  */\n"
1408 		"\n"
1409 		"static inline bool\n"
1410 		"constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1411 		"{\n"
1412 		"  int i = (int) c - (int) CONSTRAINT_%s;\n"
1413 		"  return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
1414 		"}\n"
1415 		"\n",
1416 		enum_order[satisfied_start]->name);
1417 
1418       write_range_function ("insn_extra_register_constraint",
1419 			    register_start, register_end);
1420       write_range_function ("insn_extra_memory_constraint",
1421 			    memory_start, memory_end);
1422       write_range_function ("insn_extra_address_constraint",
1423 			    address_start, address_end);
1424       write_allows_reg_mem_function ();
1425 
1426       if (constraint_max_namelen > 1)
1427         {
1428 	  write_insn_constraint_len ();
1429 	  puts ("#define CONSTRAINT_LEN(c_,s_) "
1430 		"insn_constraint_len (c_,s_)\n");
1431 	}
1432       else
1433 	puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1434       if (have_register_constraints)
1435 	puts ("extern enum reg_class reg_class_for_constraint_1 "
1436 	      "(enum constraint_num);\n"
1437 	      "\n"
1438 	      "static inline enum reg_class\n"
1439 	      "reg_class_for_constraint (enum constraint_num c)\n"
1440 	      "{\n"
1441 	      "  if (insn_extra_register_constraint (c))\n"
1442 	      "    return reg_class_for_constraint_1 (c);\n"
1443 	      "  return NO_REGS;\n"
1444 	      "}\n");
1445       else
1446 	puts ("static inline enum reg_class\n"
1447 	      "reg_class_for_constraint (enum constraint_num)\n"
1448 	      "{\n"
1449 	      "  return NO_REGS;\n"
1450 	      "}\n");
1451       if (have_const_int_constraints)
1452 	puts ("extern bool insn_const_int_ok_for_constraint "
1453 	      "(HOST_WIDE_INT, enum constraint_num);\n"
1454 	      "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1455 	      "    insn_const_int_ok_for_constraint (v_, "
1456 	      "lookup_constraint (s_))\n");
1457       else
1458 	puts ("static inline bool\n"
1459 	      "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1460 	      " enum constraint_num)\n"
1461 	      "{\n"
1462 	      "  return false;\n"
1463 	      "}\n");
1464 
1465       puts ("enum constraint_type\n"
1466 	    "{\n"
1467 	    "  CT_REGISTER,\n"
1468 	    "  CT_CONST_INT,\n"
1469 	    "  CT_MEMORY,\n"
1470 	    "  CT_ADDRESS,\n"
1471 	    "  CT_FIXED_FORM\n"
1472 	    "};\n"
1473 	    "\n"
1474 	    "static inline enum constraint_type\n"
1475 	    "get_constraint_type (enum constraint_num c)\n"
1476 	    "{");
1477       auto_vec <std::pair <unsigned int, const char *>, 4> values;
1478       if (const_int_start != const_int_end)
1479 	values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
1480       if (memory_start != memory_end)
1481 	values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
1482       if (address_start != address_end)
1483 	values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1484       if (address_end != num_constraints)
1485 	values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1486       print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1487       puts ("}");
1488     }
1489 
1490   puts ("#endif /* tm-preds.h */");
1491 }
1492 
1493 /* Write insn-preds.c.
1494    N.B. the list of headers to include was copied from genrecog; it
1495    may not be ideal.
1496 
1497    FUTURE: Write #line markers referring back to the machine
1498    description.  (Can't practically do this now since we don't know
1499    the line number of the C block - just the line number of the enclosing
1500    expression.)  */
1501 static void
1502 write_insn_preds_c (void)
1503 {
1504   struct pred_data *p;
1505 
1506   printf ("\
1507 /* Generated automatically by the program '%s'\n\
1508    from the machine description file '%s'.  */\n\n", progname, in_fname);
1509 
1510   puts ("\
1511 #include \"config.h\"\n\
1512 #include \"system.h\"\n\
1513 #include \"coretypes.h\"\n\
1514 #include \"tm.h\"\n\
1515 #include \"rtl.h\"\n\
1516 #include \"hash-set.h\"\n\
1517 #include \"machmode.h\"\n\
1518 #include \"vec.h\"\n\
1519 #include \"double-int.h\"\n\
1520 #include \"input.h\"\n\
1521 #include \"alias.h\"\n\
1522 #include \"symtab.h\"\n\
1523 #include \"wide-int.h\"\n\
1524 #include \"inchash.h\"\n\
1525 #include \"tree.h\"\n\
1526 #include \"varasm.h\"\n\
1527 #include \"stor-layout.h\"\n\
1528 #include \"calls.h\"\n\
1529 #include \"tm_p.h\"\n\
1530 #include \"hashtab.h\"\n\
1531 #include \"hash-set.h\"\n\
1532 #include \"vec.h\"\n\
1533 #include \"machmode.h\"\n\
1534 #include \"hard-reg-set.h\"\n\
1535 #include \"input.h\"\n\
1536 #include \"function.h\"\n\
1537 #include \"insn-config.h\"\n\
1538 #include \"recog.h\"\n\
1539 #include \"output.h\"\n\
1540 #include \"flags.h\"\n\
1541 #include \"hard-reg-set.h\"\n\
1542 #include \"predict.h\"\n\
1543 #include \"basic-block.h\"\n\
1544 #include \"resource.h\"\n\
1545 #include \"diagnostic-core.h\"\n\
1546 #include \"reload.h\"\n\
1547 #include \"regs.h\"\n\
1548 #include \"tm-constrs.h\"\n");
1549 
1550   FOR_ALL_PREDICATES (p)
1551     write_one_predicate_function (p);
1552 
1553   if (constraint_max_namelen > 0)
1554     {
1555       write_lookup_constraint_1 ();
1556       write_lookup_constraint_array ();
1557       if (have_register_constraints)
1558 	write_reg_class_for_constraint_1 ();
1559       write_constraint_satisfied_p_array ();
1560 
1561       if (have_const_int_constraints)
1562 	write_insn_const_int_ok_for_constraint ();
1563     }
1564 }
1565 
1566 /* Argument parsing.  */
1567 static bool gen_header;
1568 static bool gen_constrs;
1569 
1570 static bool
1571 parse_option (const char *opt)
1572 {
1573   if (!strcmp (opt, "-h"))
1574     {
1575       gen_header = true;
1576       return 1;
1577     }
1578   else if (!strcmp (opt, "-c"))
1579     {
1580       gen_constrs = true;
1581       return 1;
1582     }
1583   else
1584     return 0;
1585 }
1586 
1587 /* Master control.  */
1588 int
1589 main (int argc, char **argv)
1590 {
1591   rtx defn;
1592   int pattern_lineno, next_insn_code = 0;
1593 
1594   progname = argv[0];
1595   if (argc <= 1)
1596     fatal ("no input file name");
1597   if (!init_rtx_reader_args_cb (argc, argv, parse_option))
1598     return FATAL_EXIT_CODE;
1599 
1600   while ((defn = read_md_rtx (&pattern_lineno, &next_insn_code)) != 0)
1601     switch (GET_CODE (defn))
1602       {
1603       case DEFINE_PREDICATE:
1604       case DEFINE_SPECIAL_PREDICATE:
1605 	process_define_predicate (defn, pattern_lineno);
1606 	break;
1607 
1608       case DEFINE_CONSTRAINT:
1609       case DEFINE_MEMORY_CONSTRAINT:
1610       case DEFINE_ADDRESS_CONSTRAINT:
1611 	process_define_constraint (defn, pattern_lineno);
1612 	break;
1613 
1614       case DEFINE_REGISTER_CONSTRAINT:
1615 	process_define_register_constraint (defn, pattern_lineno);
1616 	break;
1617 
1618       default:
1619 	break;
1620       }
1621 
1622   choose_enum_order ();
1623 
1624   if (gen_header)
1625     write_tm_preds_h ();
1626   else if (gen_constrs)
1627     write_tm_constrs_h ();
1628   else
1629     write_insn_preds_c ();
1630 
1631   if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1632     return FATAL_EXIT_CODE;
1633 
1634   return SUCCESS_EXIT_CODE;
1635 }
1636