OSDN Git Service

2012-04-13 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / genpreds.c
1 /* Generate from machine description:
2    - prototype declarations for operand predicates (tm-preds.h)
3    - function definitions of operand predicates, if defined new-style
4      (insn-preds.c)
5    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
6    Free Software Foundation, Inc.
7
8 This file is part of GCC.
9
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)
13 any later version.
14
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.
19
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/>.  */
23
24 #include "bconfig.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "errors.h"
30 #include "obstack.h"
31 #include "read-md.h"
32 #include "gensupport.h"
33
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.  */
38 static bool
39 validate_exp (rtx exp, const char *name, int lineno)
40 {
41   if (exp == 0)
42     {
43       message_with_line (lineno, "%s: must give a predicate expression", name);
44       return true;
45     }
46
47   switch (GET_CODE (exp))
48     {
49       /* Ternary, binary, unary expressions: recurse into subexpressions.  */
50     case IF_THEN_ELSE:
51       if (validate_exp (XEXP (exp, 2), name, lineno))
52         return true;
53       /* else fall through */
54     case AND:
55     case IOR:
56       if (validate_exp (XEXP (exp, 1), name, lineno))
57         return true;
58       /* else fall through */
59     case NOT:
60       return validate_exp (XEXP (exp, 0), name, lineno);
61
62       /* MATCH_CODE might have a syntax error in its path expression.  */
63     case MATCH_CODE:
64       {
65         const char *p;
66         for (p = XSTR (exp, 1); *p; p++)
67           {
68             if (!ISDIGIT (*p) && !ISLOWER (*p))
69               {
70                 error_with_line (lineno, "%s: invalid character in path "
71                                  "string '%s'", name, XSTR (exp, 1));
72                 return true;
73               }
74           }
75       }
76       /* fall through */
77
78       /* These need no special checking.  */
79     case MATCH_OPERAND:
80     case MATCH_TEST:
81       return false;
82
83     default:
84       error_with_line (lineno,
85                        "%s: cannot use '%s' in a predicate expression",
86                        name, GET_RTX_NAME (GET_CODE (exp)));
87       return true;
88     }
89 }
90
91 /* Predicates are defined with (define_predicate) or
92    (define_special_predicate) expressions in the machine description.  */
93 static void
94 process_define_predicate (rtx defn, int lineno)
95 {
96   validate_exp (XEXP (defn, 1), XSTR (defn, 0), lineno);
97 }
98
99 /* Given a predicate, if it has an embedded C block, write the block
100    out as a static inline subroutine, and augment the RTL test with a
101    match_test that calls that subroutine.  For instance,
102
103        (define_predicate "basereg_operand"
104          (match_operand 0 "register_operand")
105        {
106          if (GET_CODE (op) == SUBREG)
107            op = SUBREG_REG (op);
108          return REG_POINTER (op);
109        })
110
111    becomes
112
113        static inline int basereg_operand_1(rtx op, enum machine_mode mode)
114        {
115          if (GET_CODE (op) == SUBREG)
116            op = SUBREG_REG (op);
117          return REG_POINTER (op);
118        }
119
120        (define_predicate "basereg_operand"
121          (and (match_operand 0 "register_operand")
122               (match_test "basereg_operand_1 (op, mode)")))
123
124    The only wart is that there's no way to insist on a { } string in
125    an RTL template, so we have to handle "" strings.  */
126
127
128 static void
129 write_predicate_subfunction (struct pred_data *p)
130 {
131   const char *match_test_str;
132   rtx match_test_exp, and_exp;
133
134   if (p->c_block[0] == '\0')
135     return;
136
137   /* Construct the function-call expression.  */
138   obstack_grow (rtl_obstack, p->name, strlen (p->name));
139   obstack_grow (rtl_obstack, "_1 (op, mode)",
140                 sizeof "_1 (op, mode)");
141   match_test_str = XOBFINISH (rtl_obstack, const char *);
142
143   /* Add the function-call expression to the complete expression to be
144      evaluated.  */
145   match_test_exp = rtx_alloc (MATCH_TEST);
146   XSTR (match_test_exp, 0) = match_test_str;
147
148   and_exp = rtx_alloc (AND);
149   XEXP (and_exp, 0) = p->exp;
150   XEXP (and_exp, 1) = match_test_exp;
151
152   p->exp = and_exp;
153
154   printf ("static inline int\n"
155           "%s_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n",
156           p->name);
157   print_md_ptr_loc (p->c_block);
158   if (p->c_block[0] == '{')
159     fputs (p->c_block, stdout);
160   else
161     printf ("{\n  %s\n}", p->c_block);
162   fputs ("\n\n", stdout);
163 }
164
165 /* Given a predicate expression EXP, from form NAME, determine whether
166    it refers to the variable given as VAR.  */
167 static bool
168 needs_variable (rtx exp, const char *var)
169 {
170   switch (GET_CODE (exp))
171     {
172       /* Ternary, binary, unary expressions need a variable if
173          any of their subexpressions do.  */
174     case IF_THEN_ELSE:
175       if (needs_variable (XEXP (exp, 2), var))
176         return true;
177       /* else fall through */
178     case AND:
179     case IOR:
180       if (needs_variable (XEXP (exp, 1), var))
181         return true;
182       /* else fall through */
183     case NOT:
184       return needs_variable (XEXP (exp, 0), var);
185
186       /* MATCH_CODE uses "op", but nothing else.  */
187     case MATCH_CODE:
188       return !strcmp (var, "op");
189
190       /* MATCH_OPERAND uses "op" and may use "mode".  */
191     case MATCH_OPERAND:
192       if (!strcmp (var, "op"))
193         return true;
194       if (!strcmp (var, "mode") && GET_MODE (exp) == VOIDmode)
195         return true;
196       return false;
197
198       /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
199     case MATCH_TEST:
200       {
201         const char *p = XSTR (exp, 0);
202         const char *q = strstr (p, var);
203         if (!q)
204           return false;
205         if (q != p && (ISALNUM (q[-1]) || q[-1] == '_'))
206           return false;
207         q += strlen (var);
208         if (ISALNUM (q[0]) || q[0] == '_')
209           return false;
210       }
211       return true;
212
213     default:
214       gcc_unreachable ();
215     }
216 }
217
218 /* Given an RTL expression EXP, find all subexpressions which we may
219    assume to perform mode tests.  Normal MATCH_OPERAND does;
220    MATCH_CODE 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.
225
226    The mark is the RTL /v flag, which is true for subexpressions which
227    do *not* perform mode tests.
228 */
229 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
230 static void
231 mark_mode_tests (rtx exp)
232 {
233   switch (GET_CODE (exp))
234     {
235     case MATCH_OPERAND:
236       {
237         struct pred_data *p = lookup_predicate (XSTR (exp, 1));
238         if (!p)
239           error ("reference to undefined predicate '%s'", XSTR (exp, 1));
240         else if (p->special || GET_MODE (exp) != VOIDmode)
241           NO_MODE_TEST (exp) = 1;
242       }
243       break;
244
245     case MATCH_CODE:
246       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;
250       break;
251
252     case MATCH_TEST:
253     case NOT:
254       NO_MODE_TEST (exp) = 1;
255       break;
256
257     case AND:
258       mark_mode_tests (XEXP (exp, 0));
259       mark_mode_tests (XEXP (exp, 1));
260
261       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
262                             && NO_MODE_TEST (XEXP (exp, 1)));
263       break;
264
265     case IOR:
266       mark_mode_tests (XEXP (exp, 0));
267       mark_mode_tests (XEXP (exp, 1));
268
269       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
270                             || NO_MODE_TEST (XEXP (exp, 1)));
271       break;
272
273     case IF_THEN_ELSE:
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));
279
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)));
283       break;
284
285     default:
286       gcc_unreachable ();
287     }
288 }
289
290 /* Determine whether the expression EXP is a MATCH_CODE that should
291    be written as a switch statement.  */
292 static bool
293 generate_switch_p (rtx exp)
294 {
295   return GET_CODE (exp) == MATCH_CODE
296          && strchr (XSTR (exp, 0), ',');
297 }
298
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
303    us.  */
304
305 static void
306 add_mode_tests (struct pred_data *p)
307 {
308   rtx match_test_exp, and_exp;
309   rtx *pos;
310
311   /* Don't touch special predicates.  */
312   if (p->special)
313     return;
314
315   mark_mode_tests (p->exp);
316
317   /* If the whole expression already tests the mode, we're done.  */
318   if (!NO_MODE_TEST (p->exp))
319     return;
320
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;
325
326   /* It is always correct to rewrite p->exp as
327
328         (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
329
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.  */
336
337   pos = &p->exp;
338   for (;;)
339     {
340       rtx subexp = *pos;
341
342       switch (GET_CODE (subexp))
343         {
344         case AND:
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
348              AND expression.  */
349           if (generate_switch_p (XEXP (subexp, 0)))
350             pos = &XEXP (subexp, 1);
351           goto break_loop;
352
353         case IOR:
354           {
355             int test0 = NO_MODE_TEST (XEXP (subexp, 0));
356             int test1 = NO_MODE_TEST (XEXP (subexp, 1));
357
358             gcc_assert (test0 || test1);
359
360             if (test0 && test1)
361               goto break_loop;
362             pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
363           }
364           break;
365
366         case IF_THEN_ELSE:
367           {
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));
371
372             gcc_assert ((test0 && test1) || test2);
373
374             if (test0 && test1 && test2)
375               goto break_loop;
376             if (test0 && test1)
377               /* Must put it on the dependent clause, not the
378                  controlling expression, or we change the meaning of
379                  the test.  */
380               pos = &XEXP (subexp, 1);
381             else
382               pos = &XEXP (subexp, 2);
383           }
384           break;
385
386         default:
387           goto break_loop;
388         }
389     }
390  break_loop:
391   XEXP (and_exp, 0) = *pos;
392   *pos = and_exp;
393 }
394
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.  */
399 static void
400 write_extract_subexp (const char *path)
401 {
402   int len = strlen (path);
403   int i;
404
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--)
408     {
409       if (ISLOWER (path[i]))
410         fputs ("XVECEXP (", stdout);
411       else if (ISDIGIT (path[i]))
412         fputs ("XEXP (", stdout);
413       else
414         gcc_unreachable ();
415     }
416
417   fputs ("op", stdout);
418
419   for (i = 0; i < len; i++)
420     {
421       if (ISLOWER (path[i]))
422         printf (", 0, %d)", path[i] - 'a');
423       else if (ISDIGIT (path[i]))
424         printf (", %d)", path[i] - '0');
425       else
426         gcc_unreachable ();
427     }
428 }
429
430 /* CODES is a list of RTX codes.  Write out an expression which
431    determines whether the operand has one of those codes.  */
432 static void
433 write_match_code (const char *path, const char *codes)
434 {
435   const char *code;
436
437   while ((code = scan_comma_elt (&codes)) != 0)
438     {
439       fputs ("GET_CODE (", stdout);
440       write_extract_subexp (path);
441       fputs (") == ", stdout);
442       while (code < codes)
443         {
444           putchar (TOUPPER (*code));
445           code++;
446         }
447
448       if (*codes == ',')
449         fputs (" || ", stdout);
450     }
451 }
452
453 /* EXP is an RTL (sub)expression for a predicate.  Recursively
454    descend the expression and write out an equivalent C expression.  */
455 static void
456 write_predicate_expr (rtx exp)
457 {
458   switch (GET_CODE (exp))
459     {
460     case AND:
461       putchar ('(');
462       write_predicate_expr (XEXP (exp, 0));
463       fputs (") && (", stdout);
464       write_predicate_expr (XEXP (exp, 1));
465       putchar (')');
466       break;
467
468     case IOR:
469       putchar ('(');
470       write_predicate_expr (XEXP (exp, 0));
471       fputs (") || (", stdout);
472       write_predicate_expr (XEXP (exp, 1));
473       putchar (')');
474       break;
475
476     case NOT:
477       fputs ("!(", stdout);
478       write_predicate_expr (XEXP (exp, 0));
479       putchar (')');
480       break;
481
482     case IF_THEN_ELSE:
483       putchar ('(');
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));
489       putchar (')');
490       break;
491
492     case MATCH_OPERAND:
493       if (GET_MODE (exp) == VOIDmode)
494         printf ("%s (op, mode)", XSTR (exp, 1));
495       else
496         printf ("%s (op, %smode)", XSTR (exp, 1), mode_name[GET_MODE (exp)]);
497       break;
498
499     case MATCH_CODE:
500       write_match_code (XSTR (exp, 1), XSTR (exp, 0));
501       break;
502
503     case MATCH_TEST:
504       print_c_condition (XSTR (exp, 0));
505       break;
506
507     default:
508       gcc_unreachable ();
509     }
510 }
511
512 /* Write the MATCH_CODE expression EXP as a switch statement.  */
513
514 static void
515 write_match_code_switch (rtx exp)
516 {
517   const char *codes = XSTR (exp, 0);
518   const char *path = XSTR (exp, 1);
519   const char *code;
520
521   fputs ("  switch (GET_CODE (", stdout);
522   write_extract_subexp (path);
523   fputs ("))\n    {\n", stdout);
524
525   while ((code = scan_comma_elt (&codes)) != 0)
526     {
527       fputs ("    case ", stdout);
528       while (code < codes)
529         {
530           putchar (TOUPPER (*code));
531           code++;
532         }
533       fputs(":\n", stdout);
534     }
535 }
536
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.  */
540
541 static void
542 write_predicate_stmts (rtx exp)
543 {
544   switch (GET_CODE (exp))
545     {
546     case MATCH_CODE:
547       if (generate_switch_p (exp))
548         {
549           write_match_code_switch (exp);
550           puts ("      return true;\n"
551                 "    default:\n"
552                 "      break;\n"
553                 "    }\n"
554                 "  return false;");
555           return;
556         }
557       break;
558
559     case AND:
560       if (generate_switch_p (XEXP (exp, 0)))
561         {
562           write_match_code_switch (XEXP (exp, 0));
563           puts ("      break;\n"
564                 "    default:\n"
565                 "      return false;\n"
566                 "    }");
567           exp = XEXP (exp, 1);
568         }
569       break;
570
571     case IOR:
572       if (generate_switch_p (XEXP (exp, 0)))
573         {
574           write_match_code_switch (XEXP (exp, 0));
575           puts ("      return true;\n"
576                 "    default:\n"
577                 "      break;\n"
578                 "    }");
579           exp = XEXP (exp, 1);
580         }
581       break;
582
583     case NOT:
584       if (generate_switch_p (XEXP (exp, 0)))
585         {
586           write_match_code_switch (XEXP (exp, 0));
587           puts ("      return false;\n"
588                 "    default:\n"
589                 "      break;\n"
590                 "    }\n"
591                 "  return true;");
592           return;
593         }
594       break;
595
596     default:
597       break;
598     }
599
600   fputs("  return ",stdout);
601   write_predicate_expr (exp);
602   fputs(";\n", stdout);
603 }
604
605 /* Given a predicate, write out a complete C function to compute it.  */
606 static void
607 write_one_predicate_function (struct pred_data *p)
608 {
609   if (!p->exp)
610     return;
611
612   write_predicate_subfunction (p);
613   add_mode_tests (p);
614
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",
618           p->name);
619   write_predicate_stmts (p->exp);
620   fputs ("}\n\n", stdout);
621 }
622 \f
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
629    duplicate names.  */
630
631 /* All data from one constraint definition.  */
632 struct constraint_data
633 {
634   struct constraint_data *next_this_letter;
635   struct constraint_data *next_textual;
636   const char *name;
637   const char *c_name;    /* same as .name unless mangling is necessary */
638   size_t namelen;
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;
648 };
649
650 /* Overview of all constraints beginning with a given letter.  */
651
652 static struct constraint_data *
653 constraints_by_letter_table[1<<CHAR_BIT];
654
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;
659
660 #define FOR_ALL_CONSTRAINTS(iter_) \
661   for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
662
663 /* These letters, and all names beginning with them, are reserved for
664    generic constraints.
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";
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 bool have_const_dbl_constraints;
689
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.  */
693 static const char *
694 mangle (const char *name)
695 {
696   for (; *name; name++)
697     switch (*name)
698       {
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;
703       }
704
705   obstack_1grow (rtl_obstack, '\0');
706   return XOBFINISH (rtl_obstack, const char *);
707 }
708
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.
716
717    This function enforces all syntactic and semantic rules about what
718    constraints can be defined.  */
719
720 static void
721 add_constraint (const char *name, const char *regclass,
722                 rtx exp, bool is_memory, bool is_address,
723                 int lineno)
724 {
725   struct constraint_data *c, **iter, **slot;
726   const char *p;
727   bool need_mangled_name = false;
728   bool is_const_int;
729   bool is_const_dbl;
730   size_t namelen;
731
732   if (exp && validate_exp (exp, name, lineno))
733     return;
734
735   if (!ISALPHA (name[0]) && name[0] != '_')
736     {
737       if (name[1] == '\0')
738         error_with_line (lineno, "constraint name '%s' is not "
739                          "a letter or underscore", name);
740       else
741         error_with_line (lineno, "constraint name '%s' does not begin "
742                          "with a letter or underscore", name);
743       return;
744     }
745   for (p = name; *p; p++)
746     if (!ISALNUM (*p))
747       {
748         if (*p == '<' || *p == '>' || *p == '_')
749           need_mangled_name = true;
750         else
751           {
752             error_with_line (lineno,
753                              "constraint name '%s' must be composed of "
754                              "letters, digits, underscores, and "
755                              "angle brackets", name);
756             return;
757           }
758       }
759
760   if (strchr (generic_constraint_letters, name[0]))
761     {
762       if (name[1] == '\0')
763         error_with_line (lineno, "constraint letter '%s' cannot be "
764                          "redefined by the machine description", name);
765       else
766         error_with_line (lineno, "constraint name '%s' cannot be defined by "
767                          "the machine description, as it begins with '%c'",
768                          name, name[0]);
769       return;
770     }
771
772
773   namelen = strlen (name);
774   slot = &constraints_by_letter_table[(unsigned int)name[0]];
775   for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
776     {
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)
783         slot = iter;
784
785       if (!strcmp ((*iter)->name, name))
786         {
787           error_with_line (lineno, "redefinition of constraint '%s'", name);
788           message_with_line ((*iter)->lineno, "previous definition is here");
789           return;
790         }
791       else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
792         {
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);
796           return;
797         }
798       else if (!strncmp ((*iter)->name, name, namelen))
799         {
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);
803           return;
804         }
805     }
806
807   is_const_int = strchr (const_int_constraints, name[0]) != 0;
808   is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
809
810   if (is_const_int || is_const_dbl)
811     {
812       enum rtx_code appropriate_code
813         = is_const_int ? CONST_INT : CONST_DOUBLE;
814
815       /* Consider relaxing this requirement in the future.  */
816       if (regclass
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)))
821         {
822           if (name[1] == '\0')
823             error_with_line (lineno, "constraint letter '%c' is reserved "
824                              "for %s constraints",
825                              name[0], GET_RTX_NAME (appropriate_code));
826           else
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));
830           return;
831         }
832
833       if (is_memory)
834         {
835           if (name[1] == '\0')
836             error_with_line (lineno, "constraint letter '%c' cannot be a "
837                              "memory constraint", name[0]);
838           else
839             error_with_line (lineno, "constraint name '%s' begins with '%c', "
840                              "and therefore cannot be a memory constraint",
841                              name, name[0]);
842           return;
843         }
844       else if (is_address)
845         {
846           if (name[1] == '\0')
847             error_with_line (lineno, "constraint letter '%c' cannot be a "
848                              "memory constraint", name[0]);
849           else
850             error_with_line (lineno, "constraint name '%s' begins with '%c', "
851                              "and therefore cannot be a memory constraint",
852                              name, name[0]);
853           return;
854         }
855     }
856
857
858   c = XOBNEW (rtl_obstack, struct constraint_data);
859   c->name = name;
860   c->c_name = need_mangled_name ? mangle (name) : name;
861   c->lineno = lineno;
862   c->namelen = namelen;
863   c->regclass = regclass;
864   c->exp = exp;
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;
871
872   c->next_this_letter = *slot;
873   *slot = c;
874
875   /* Insert this constraint in the list of all constraints in textual
876      order.  */
877   c->next_textual = 0;
878   *last_constraint_ptr = c;
879   last_constraint_ptr = &c->next_textual;
880
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;
888 }
889
890 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
891    DEFINE_ADDRESS_CONSTRAINT expression, C.  */
892 static void
893 process_define_constraint (rtx c, int lineno)
894 {
895   add_constraint (XSTR (c, 0), 0, XEXP (c, 2),
896                   GET_CODE (c) == DEFINE_MEMORY_CONSTRAINT,
897                   GET_CODE (c) == DEFINE_ADDRESS_CONSTRAINT,
898                   lineno);
899 }
900
901 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C.  */
902 static void
903 process_define_register_constraint (rtx c, int lineno)
904 {
905   add_constraint (XSTR (c, 0), XSTR (c, 1), 0, false, false, lineno);
906 }
907
908 /* Write out an enumeration with one entry per machine-specific
909    constraint.  */
910 static void
911 write_enum_constraint_num (void)
912 {
913   struct constraint_data *c;
914
915   fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
916   fputs ("enum constraint_num\n"
917          "{\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");
922 }
923
924 /* Write out a function which looks at a string and determines what
925    constraint name, if any, it begins with.  */
926 static void
927 write_lookup_constraint (void)
928 {
929   unsigned int i;
930   puts ("enum constraint_num\n"
931         "lookup_constraint (const char *str)\n"
932         "{\n"
933         "  switch (str[0])\n"
934         "    {");
935
936   for (i = 0; i < ARRAY_SIZE(constraints_by_letter_table); i++)
937     {
938       struct constraint_data *c = constraints_by_letter_table[i];
939       if (!c)
940         continue;
941
942       printf ("    case '%c':\n", i);
943       if (c->namelen == 1)
944         printf ("      return CONSTRAINT_%s;\n", c->c_name);
945       else
946         {
947           do
948             {
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;
953             }
954           while (c);
955           puts ("      break;");
956         }
957     }
958
959   puts ("    default: break;\n"
960         "    }\n"
961         "  return CONSTRAINT__UNKNOWN;\n"
962         "}\n");
963 }
964
965 /* Write out a function which looks at a string and determines what
966    the constraint name length is.  */
967 static void
968 write_insn_constraint_len (void)
969 {
970   unsigned int i;
971
972   puts ("static inline size_t\n"
973         "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
974         "{\n"
975         "  switch (fc)\n"
976         "    {");
977
978   for (i = 0; i < ARRAY_SIZE(constraints_by_letter_table); i++)
979     {
980       struct constraint_data *c = constraints_by_letter_table[i];
981
982       if (!c
983           || c->namelen == 1)
984         continue;
985
986       /* Constraints with multiple characters should have the same
987          length.  */
988       {
989         struct constraint_data *c2 = c->next_this_letter;
990         size_t len = c->namelen;
991         while (c2)
992           {
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;
997           }
998       }
999
1000       printf ("    case '%c': return %lu;\n",
1001               i, (unsigned long int) c->namelen);
1002     }
1003
1004   puts ("    default: break;\n"
1005         "    }\n"
1006         "  return 1;\n"
1007         "}\n");
1008 }
1009
1010 /* Write out the function which computes the register class corresponding
1011    to a register constraint.  */
1012 static void
1013 write_regclass_for_constraint (void)
1014 {
1015   struct constraint_data *c;
1016
1017   puts ("enum reg_class\n"
1018         "regclass_for_constraint (enum constraint_num c)\n"
1019         "{\n"
1020         "  switch (c)\n"
1021         "    {");
1022
1023   FOR_ALL_CONSTRAINTS (c)
1024     if (c->is_register)
1025       printf ("    case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1026
1027   puts ("    default: break;\n"
1028         "    }\n"
1029         "  return NO_REGS;\n"
1030         "}\n");
1031 }
1032
1033 /* Write out the functions which compute whether a given value matches
1034    a given non-register constraint.  */
1035 static void
1036 write_tm_constrs_h (void)
1037 {
1038   struct constraint_data *c;
1039
1040   printf ("\
1041 /* Generated automatically by the program '%s'\n\
1042    from the machine description file '%s'.  */\n\n", progname, in_fname);
1043
1044   puts ("\
1045 #ifndef GCC_TM_CONSTRS_H\n\
1046 #define GCC_TM_CONSTRS_H\n");
1047
1048   FOR_ALL_CONSTRAINTS (c)
1049     if (!c->is_register)
1050       {
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);
1059
1060         printf ("static inline bool\n"
1061                 "satisfies_constraint_%s (rtx %s)\n"
1062                 "{\n", c->c_name,
1063                 needs_op ? "op" : "ARG_UNUSED (op)");
1064         if (needs_mode)
1065           puts ("  enum machine_mode mode = GET_MODE (op);");
1066         if (needs_ival)
1067           puts ("  HOST_WIDE_INT ival = 0;");
1068         if (needs_hval)
1069           puts ("  HOST_WIDE_INT hval = 0;");
1070         if (needs_lval)
1071           puts ("  unsigned HOST_WIDE_INT lval = 0;");
1072         if (needs_rval)
1073           puts ("  const REAL_VALUE_TYPE *rval = 0;");
1074
1075         if (needs_ival)
1076           puts ("  if (CONST_INT_P (op))\n"
1077                 "    ival = INTVAL (op);");
1078         if (needs_hval)
1079           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1080                 "    hval = CONST_DOUBLE_HIGH (op);");
1081         if (needs_lval)
1082           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1083                 "    lval = CONST_DOUBLE_LOW (op);");
1084         if (needs_rval)
1085           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1086                 "    rval = CONST_DOUBLE_REAL_VALUE (op);");
1087
1088         write_predicate_stmts (c->exp);
1089         fputs ("}\n", stdout);
1090       }
1091   puts ("#endif /* tm-constrs.h */");
1092 }
1093
1094 /* Write out the wrapper function, constraint_satisfied_p, that maps
1095    a CONSTRAINT_xxx constant to one of the predicate functions generated
1096    above.  */
1097 static void
1098 write_constraint_satisfied_p (void)
1099 {
1100   struct constraint_data *c;
1101
1102   puts ("bool\n"
1103         "constraint_satisfied_p (rtx op, enum constraint_num c)\n"
1104         "{\n"
1105         "  switch (c)\n"
1106         "    {");
1107
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);
1113
1114   puts ("    default: break;\n"
1115         "    }\n"
1116         "  return false;\n"
1117         "}\n");
1118 }
1119
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.  */
1123 static void
1124 write_insn_const_int_ok_for_constraint (void)
1125 {
1126   struct constraint_data *c;
1127
1128   puts ("bool\n"
1129         "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1130                                           "enum constraint_num c)\n"
1131         "{\n"
1132         "  switch (c)\n"
1133         "    {");
1134
1135   FOR_ALL_CONSTRAINTS (c)
1136     if (c->is_const_int)
1137       {
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);
1144       }
1145
1146   puts ("    default: break;\n"
1147         "    }\n"
1148         "  return false;\n"
1149         "}\n");
1150 }
1151
1152
1153 /* Write out the function which computes whether a given constraint is
1154    a memory constraint.  */
1155 static void
1156 write_insn_extra_memory_constraint (void)
1157 {
1158   struct constraint_data *c;
1159
1160   puts ("bool\n"
1161         "insn_extra_memory_constraint (enum constraint_num c)\n"
1162         "{\n"
1163         "  switch (c)\n"
1164         "    {");
1165
1166   FOR_ALL_CONSTRAINTS (c)
1167     if (c->is_memory)
1168       printf ("    case CONSTRAINT_%s:\n      return true;\n\n", c->c_name);
1169
1170   puts ("    default: break;\n"
1171         "    }\n"
1172         "  return false;\n"
1173         "}\n");
1174 }
1175
1176 /* Write out the function which computes whether a given constraint is
1177    an address constraint.  */
1178 static void
1179 write_insn_extra_address_constraint (void)
1180 {
1181   struct constraint_data *c;
1182
1183   puts ("bool\n"
1184         "insn_extra_address_constraint (enum constraint_num c)\n"
1185         "{\n"
1186         "  switch (c)\n"
1187         "    {");
1188
1189   FOR_ALL_CONSTRAINTS (c)
1190     if (c->is_address)
1191       printf ("    case CONSTRAINT_%s:\n      return true;\n\n", c->c_name);
1192
1193   puts ("    default: break;\n"
1194         "    }\n"
1195         "  return false;\n"
1196         "}\n");
1197 }
1198
1199 \f
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.  */
1203 static void
1204 write_tm_preds_h (void)
1205 {
1206   struct pred_data *p;
1207
1208   printf ("\
1209 /* Generated automatically by the program '%s'\n\
1210    from the machine description file '%s'.  */\n\n", progname, in_fname);
1211
1212   puts ("\
1213 #ifndef GCC_TM_PREDS_H\n\
1214 #define GCC_TM_PREDS_H\n\
1215 \n\
1216 #ifdef HAVE_MACHINE_MODES");
1217
1218   FOR_ALL_PREDICATES (p)
1219     printf ("extern int %s (rtx, enum machine_mode);\n", p->name);
1220
1221   puts ("#endif /* HAVE_MACHINE_MODES */\n");
1222
1223   if (constraint_max_namelen > 0)
1224     {
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");
1228
1229       if (constraint_max_namelen > 1)
1230         {
1231           write_insn_constraint_len ();
1232           puts ("#define CONSTRAINT_LEN(c_,s_) "
1233                 "insn_constraint_len (c_,s_)\n");
1234         }
1235       else
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");
1244       else
1245         puts ("#define REG_CLASS_FROM_CONSTRAINT(c_,s_) NO_REGS\n"
1246               "#define REG_CLASS_FOR_CONSTRAINT(x_) \\\n"
1247               "    NO_REGS\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");
1257       else
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");
1267       else
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");
1274       else
1275         puts ("#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) false\n");
1276     }
1277
1278   puts ("#endif /* tm-preds.h */");
1279 }
1280
1281 /* Write insn-preds.c.
1282    N.B. the list of headers to include was copied from genrecog; it
1283    may not be ideal.
1284
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
1288    expression.)  */
1289 static void
1290 write_insn_preds_c (void)
1291 {
1292   struct pred_data *p;
1293
1294   printf ("\
1295 /* Generated automatically by the program '%s'\n\
1296    from the machine description file '%s'.  */\n\n", progname, in_fname);
1297
1298   puts ("\
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");
1317
1318   FOR_ALL_PREDICATES (p)
1319     write_one_predicate_function (p);
1320
1321   if (constraint_max_namelen > 0)
1322     {
1323       write_lookup_constraint ();
1324       if (have_register_constraints)
1325         write_regclass_for_constraint ();
1326       write_constraint_satisfied_p ();
1327
1328       if (have_const_int_constraints)
1329         write_insn_const_int_ok_for_constraint ();
1330
1331       if (have_memory_constraints)
1332         write_insn_extra_memory_constraint ();
1333       if (have_address_constraints)
1334         write_insn_extra_address_constraint ();
1335     }
1336 }
1337
1338 /* Argument parsing.  */
1339 static bool gen_header;
1340 static bool gen_constrs;
1341
1342 static bool
1343 parse_option (const char *opt)
1344 {
1345   if (!strcmp (opt, "-h"))
1346     {
1347       gen_header = true;
1348       return 1;
1349     }
1350   else if (!strcmp (opt, "-c"))
1351     {
1352       gen_constrs = true;
1353       return 1;
1354     }
1355   else
1356     return 0;
1357 }
1358
1359 /* Master control.  */
1360 int
1361 main (int argc, char **argv)
1362 {
1363   rtx defn;
1364   int pattern_lineno, next_insn_code = 0;
1365
1366   progname = argv[0];
1367   if (argc <= 1)
1368     fatal ("no input file name");
1369   if (!init_rtx_reader_args_cb (argc, argv, parse_option))
1370     return FATAL_EXIT_CODE;
1371
1372   while ((defn = read_md_rtx (&pattern_lineno, &next_insn_code)) != 0)
1373     switch (GET_CODE (defn))
1374       {
1375       case DEFINE_PREDICATE:
1376       case DEFINE_SPECIAL_PREDICATE:
1377         process_define_predicate (defn, pattern_lineno);
1378         break;
1379
1380       case DEFINE_CONSTRAINT:
1381       case DEFINE_MEMORY_CONSTRAINT:
1382       case DEFINE_ADDRESS_CONSTRAINT:
1383         process_define_constraint (defn, pattern_lineno);
1384         break;
1385
1386       case DEFINE_REGISTER_CONSTRAINT:
1387         process_define_register_constraint (defn, pattern_lineno);
1388         break;
1389
1390       default:
1391         break;
1392       }
1393
1394   if (gen_header)
1395     write_tm_preds_h ();
1396   else if (gen_constrs)
1397     write_tm_constrs_h ();
1398   else
1399     write_insn_preds_c ();
1400
1401   if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1402     return FATAL_EXIT_CODE;
1403
1404   return SUCCESS_EXIT_CODE;
1405 }