OSDN Git Service

PR middle-end/27332
[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 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 2, 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 COPYING.  If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.  */
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 "gensupport.h"
32
33 /* Given a predicate expression EXP, from form NAME at line LINENO,
34    verify that it does not contain any RTL constructs which are not
35    valid in predicate definitions.  Returns true if EXP is
36    INvalid; issues error messages, caller need not.  */
37 static bool
38 validate_exp (rtx exp, const char *name, int lineno)
39 {
40   if (exp == 0)
41     {
42       message_with_line (lineno, "%s: must give a predicate expression", name);
43       return true;
44     }
45
46   switch (GET_CODE (exp))
47     {
48       /* Ternary, binary, unary expressions: recurse into subexpressions.  */
49     case IF_THEN_ELSE:
50       if (validate_exp (XEXP (exp, 2), name, lineno))
51         return true;
52       /* else fall through */
53     case AND:
54     case IOR:
55       if (validate_exp (XEXP (exp, 1), name, lineno))
56         return true;
57       /* else fall through */
58     case NOT:
59       return validate_exp (XEXP (exp, 0), name, lineno);
60
61       /* MATCH_CODE might have a syntax error in its path expression.  */
62     case MATCH_CODE:
63       {
64         const char *p;
65         for (p = XSTR (exp, 1); *p; p++)
66           {
67             if (!ISDIGIT (*p) && !ISLOWER (*p))
68               {
69                 message_with_line (lineno, "%s: invalid character in path "
70                                    "string '%s'", name, XSTR (exp, 1));
71                 have_error = 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       message_with_line (lineno,
85                          "%s: cannot use '%s' in a predicate expression",
86                          name, GET_RTX_NAME (GET_CODE (exp)));
87       have_error = 1;
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   struct pred_data *pred;
98   const char *p;
99
100   if (!ISALPHA (XSTR (defn, 0)[0]) && XSTR (defn, 0)[0] != '_')
101     goto bad_name;
102   for (p = XSTR (defn, 0) + 1; *p; p++)
103     if (!ISALNUM (*p) && *p != '_')
104       goto bad_name;
105   
106   if (validate_exp (XEXP (defn, 1), XSTR (defn, 0), lineno))
107     return;
108
109   pred = XCNEW (struct pred_data);
110   pred->name = XSTR (defn, 0);
111   pred->exp = XEXP (defn, 1);
112   pred->c_block = XSTR (defn, 2);
113
114   if (GET_CODE (defn) == DEFINE_SPECIAL_PREDICATE)
115     pred->special = true;
116
117   add_predicate (pred);
118   return;
119
120  bad_name:
121   message_with_line (lineno,
122                      "%s: predicate name must be a valid C function name",
123                      XSTR (defn, 0));
124   have_error = 1;
125   return;
126 }
127
128 /* Given a predicate, if it has an embedded C block, write the block
129    out as a static inline subroutine, and augment the RTL test with a
130    match_test that calls that subroutine.  For instance,
131
132        (define_predicate "basereg_operand"
133          (match_operand 0 "register_operand")
134        {
135          if (GET_CODE (op) == SUBREG)
136            op = SUBREG_REG (op);
137          return REG_POINTER (op);
138        })
139
140    becomes
141
142        static inline int basereg_operand_1(rtx op, enum machine_mode mode)
143        {
144          if (GET_CODE (op) == SUBREG)
145            op = SUBREG_REG (op);
146          return REG_POINTER (op);
147        }
148
149        (define_predicate "basereg_operand"
150          (and (match_operand 0 "register_operand")
151               (match_test "basereg_operand_1 (op, mode)")))
152
153    The only wart is that there's no way to insist on a { } string in
154    an RTL template, so we have to handle "" strings.  */
155
156    
157 static void
158 write_predicate_subfunction (struct pred_data *p)
159 {
160   const char *match_test_str;
161   rtx match_test_exp, and_exp;
162
163   if (p->c_block[0] == '\0')
164     return;
165
166   /* Construct the function-call expression.  */
167   obstack_grow (rtl_obstack, p->name, strlen (p->name));
168   obstack_grow (rtl_obstack, "_1 (op, mode)",
169                 sizeof "_1 (op, mode)");
170   match_test_str = XOBFINISH (rtl_obstack, const char *);
171
172   /* Add the function-call expression to the complete expression to be
173      evaluated.  */
174   match_test_exp = rtx_alloc (MATCH_TEST);
175   XSTR (match_test_exp, 0) = match_test_str;
176
177   and_exp = rtx_alloc (AND);
178   XEXP (and_exp, 0) = p->exp;
179   XEXP (and_exp, 1) = match_test_exp;
180
181   p->exp = and_exp;
182
183   printf ("static inline int\n"
184           "%s_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n",
185           p->name);
186   print_rtx_ptr_loc (p->c_block);
187   if (p->c_block[0] == '{')
188     fputs (p->c_block, stdout);
189   else
190     printf ("{\n  %s\n}", p->c_block);
191   fputs ("\n\n", stdout);
192 }
193
194 /* Given a predicate expression EXP, from form NAME, determine whether
195    it refers to the variable given as VAR.  */
196 static bool
197 needs_variable (rtx exp, const char *var)
198 {
199   switch (GET_CODE (exp))
200     {
201       /* Ternary, binary, unary expressions need a variable if
202          any of their subexpressions do.  */
203     case IF_THEN_ELSE:
204       if (needs_variable (XEXP (exp, 2), var))
205         return true;
206       /* else fall through */
207     case AND:
208     case IOR:
209       if (needs_variable (XEXP (exp, 1), var))
210         return true;
211       /* else fall through */
212     case NOT:
213       return needs_variable (XEXP (exp, 0), var);
214
215       /* MATCH_CODE uses "op", but nothing else.  */
216     case MATCH_CODE:
217       return !strcmp (var, "op");
218
219       /* MATCH_OPERAND uses "op" and may use "mode".  */
220     case MATCH_OPERAND:
221       if (!strcmp (var, "op"))
222         return true;
223       if (!strcmp (var, "mode") && GET_MODE (exp) == VOIDmode)
224         return true;
225       return false;
226
227       /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
228     case MATCH_TEST:
229       {
230         const char *p = XSTR (exp, 0);
231         const char *q = strstr (p, var);
232         if (!q)
233           return false;
234         if (q != p && (ISALNUM (q[-1]) || q[-1] == '_'))
235           return false;
236         q += strlen (var);
237         if (ISALNUM (q[0] || q[0] == '_'))
238           return false;
239       }
240       return true;
241
242     default:
243       gcc_unreachable ();
244     }
245 }
246
247 /* Given an RTL expression EXP, find all subexpressions which we may
248    assume to perform mode tests.  Normal MATCH_OPERAND does;
249    MATCH_CODE does if it applies to the whole expression and accepts
250    CONST_INT or CONST_DOUBLE; and we have to assume that MATCH_TEST
251    does not.  These combine in almost-boolean fashion - the only
252    exception is that (not X) must be assumed not to perform a mode
253    test, whether or not X does.
254
255    The mark is the RTL /v flag, which is true for subexpressions which
256    do *not* perform mode tests.
257 */
258 #define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
259 static void
260 mark_mode_tests (rtx exp)
261 {
262   switch (GET_CODE (exp))
263     {
264     case MATCH_OPERAND:
265       {
266         struct pred_data *p = lookup_predicate (XSTR (exp, 1));
267         if (!p)
268           error ("reference to undefined predicate '%s'", XSTR (exp, 1));
269         else if (p->special || GET_MODE (exp) != VOIDmode)
270           NO_MODE_TEST (exp) = 1;
271       }
272       break;
273
274     case MATCH_CODE:
275       if (XSTR (exp, 1)[0] != '\0'
276           || (!strstr (XSTR (exp, 0), "const_int")
277               && !strstr (XSTR (exp, 0), "const_double")))
278         NO_MODE_TEST (exp) = 1;
279       break;
280
281     case MATCH_TEST:
282     case NOT:
283       NO_MODE_TEST (exp) = 1;
284       break;
285
286     case AND:
287       mark_mode_tests (XEXP (exp, 0));
288       mark_mode_tests (XEXP (exp, 1));
289
290       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
291                             && NO_MODE_TEST (XEXP (exp, 1)));
292       break;
293       
294     case IOR:
295       mark_mode_tests (XEXP (exp, 0));
296       mark_mode_tests (XEXP (exp, 1));
297
298       NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
299                             || NO_MODE_TEST (XEXP (exp, 1)));
300       break;
301
302     case IF_THEN_ELSE:
303       /* A ? B : C does a mode test if (one of A and B) does a mode
304          test, and C does too.  */
305       mark_mode_tests (XEXP (exp, 0));
306       mark_mode_tests (XEXP (exp, 1));
307       mark_mode_tests (XEXP (exp, 2));
308
309       NO_MODE_TEST (exp) = ((NO_MODE_TEST (XEXP (exp, 0))
310                              && NO_MODE_TEST (XEXP (exp, 1)))
311                             || NO_MODE_TEST (XEXP (exp, 2)));
312       break;
313
314     default:
315       gcc_unreachable ();
316     }
317 }
318
319 /* Given a predicate, work out where in its RTL expression to add
320    tests for proper modes.  Special predicates do not get any such
321    tests.  We try to avoid adding tests when we don't have to; in
322    particular, other normal predicates can be counted on to do it for
323    us.  */
324
325 static void
326 add_mode_tests (struct pred_data *p)
327 {
328   rtx match_test_exp, and_exp;
329   rtx *pos;
330
331   /* Don't touch special predicates.  */
332   if (p->special)
333     return;
334
335   mark_mode_tests (p->exp);
336
337   /* If the whole expression already tests the mode, we're done.  */
338   if (!NO_MODE_TEST (p->exp))
339     return;
340
341   match_test_exp = rtx_alloc (MATCH_TEST);
342   XSTR (match_test_exp, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
343   and_exp = rtx_alloc (AND);
344   XEXP (and_exp, 1) = match_test_exp;
345
346   /* It is always correct to rewrite p->exp as
347
348         (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
349
350      but there are a couple forms where we can do better.  If the
351      top-level pattern is an IOR, and one of the two branches does test
352      the mode, we can wrap just the branch that doesn't.  Likewise, if
353      we have an IF_THEN_ELSE, and one side of it tests the mode, we can
354      wrap just the side that doesn't.  And, of course, we can repeat this
355      descent as many times as it works.  */
356
357   pos = &p->exp;
358   for (;;)
359     {
360       rtx subexp = *pos;
361
362       switch (GET_CODE (subexp))
363         {
364         case IOR:
365           {
366             int test0 = NO_MODE_TEST (XEXP (subexp, 0));
367             int test1 = NO_MODE_TEST (XEXP (subexp, 1));
368             
369             gcc_assert (test0 || test1);
370             
371             if (test0 && test1)
372               goto break_loop;
373             pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
374           }
375           break;
376           
377         case IF_THEN_ELSE:
378           {
379             int test0 = NO_MODE_TEST (XEXP (subexp, 0));
380             int test1 = NO_MODE_TEST (XEXP (subexp, 1));
381             int test2 = NO_MODE_TEST (XEXP (subexp, 2));
382             
383             gcc_assert ((test0 && test1) || test2);
384             
385             if (test0 && test1 && test2)
386               goto break_loop;
387             if (test0 && test1)
388               /* Must put it on the dependent clause, not the
389                  controlling expression, or we change the meaning of
390                  the test.  */
391               pos = &XEXP (subexp, 1);
392             else
393               pos = &XEXP (subexp, 2);
394           }
395           break;
396           
397         default:
398           goto break_loop;
399         }
400     }
401  break_loop:
402   XEXP (and_exp, 0) = *pos;
403   *pos = and_exp;
404 }
405
406 /* PATH is a string describing a path from the root of an RTL
407    expression to an inner subexpression to be tested.  Output
408    code which computes the subexpression from the variable
409    holding the root of the expression.  */
410 static void
411 write_extract_subexp (const char *path)
412 {
413   int len = strlen (path);
414   int i;
415
416   /* We first write out the operations (XEXP or XVECEXP) in reverse
417      order, then write "op", then the indices in forward order.  */
418   for (i = len - 1; i >= 0; i--)
419     {
420       if (ISLOWER (path[i]))
421         fputs ("XVECEXP (", stdout);
422       else if (ISDIGIT (path[i]))
423         fputs ("XEXP (", stdout);
424       else
425         gcc_unreachable ();
426     }
427
428   fputs ("op", stdout);
429
430   for (i = 0; i < len; i++)
431     {
432       if (ISLOWER (path[i]))
433         printf (", 0, %d)", path[i] - 'a');
434       else if (ISDIGIT (path[i]))
435         printf (", %d)", path[i] - '0');
436       else
437         gcc_unreachable ();
438     }
439 }
440
441 /* CODES is a list of RTX codes.  Write out an expression which
442    determines whether the operand has one of those codes.  */
443 static void
444 write_match_code (const char *path, const char *codes)
445 {
446   const char *code;
447
448   while ((code = scan_comma_elt (&codes)) != 0)
449     {
450       fputs ("GET_CODE (", stdout);
451       write_extract_subexp (path);
452       fputs (") == ", stdout);
453       while (code < codes)
454         {
455           putchar (TOUPPER (*code));
456           code++;
457         }
458       
459       if (*codes == ',')
460         fputs (" || ", stdout);
461     }
462 }
463
464 /* EXP is an RTL (sub)expression for a predicate.  Recursively
465    descend the expression and write out an equivalent C expression.  */
466 static void
467 write_predicate_expr (rtx exp)
468 {
469   switch (GET_CODE (exp))
470     {
471     case AND:
472       putchar ('(');
473       write_predicate_expr (XEXP (exp, 0));
474       fputs (") && (", stdout);
475       write_predicate_expr (XEXP (exp, 1));
476       putchar (')');
477       break;
478   
479     case IOR:
480       putchar ('(');
481       write_predicate_expr (XEXP (exp, 0));
482       fputs (") || (", stdout);
483       write_predicate_expr (XEXP (exp, 1));
484       putchar (')');
485       break;
486
487     case NOT:
488       fputs ("!(", stdout);
489       write_predicate_expr (XEXP (exp, 0));
490       putchar (')');
491       break;
492
493     case IF_THEN_ELSE:
494       putchar ('(');
495       write_predicate_expr (XEXP (exp, 0));
496       fputs (") ? (", stdout);
497       write_predicate_expr (XEXP (exp, 1));
498       fputs (") : (", stdout);
499       write_predicate_expr (XEXP (exp, 2));
500       putchar (')');
501       break;
502
503     case MATCH_OPERAND:
504       if (GET_MODE (exp) == VOIDmode)
505         printf ("%s (op, mode)", XSTR (exp, 1));
506       else
507         printf ("%s (op, %smode)", XSTR (exp, 1), mode_name[GET_MODE (exp)]);
508       break;
509
510     case MATCH_CODE:
511       write_match_code (XSTR (exp, 1), XSTR (exp, 0));
512       break;
513
514     case MATCH_TEST:
515       print_c_condition (XSTR (exp, 0));
516       break;
517
518     default:
519       gcc_unreachable ();
520     }
521 }
522
523 /* Given a predicate, write out a complete C function to compute it.  */
524 static void
525 write_one_predicate_function (struct pred_data *p)
526 {
527   if (!p->exp)
528     return;
529
530   write_predicate_subfunction (p);
531   add_mode_tests (p);
532
533   /* A normal predicate can legitimately not look at enum machine_mode
534      if it accepts only CONST_INTs and/or CONST_DOUBLEs.  */
535   printf ("int\n%s (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)\n"
536           "{\n  return ",
537           p->name);
538   write_predicate_expr (p->exp);
539   fputs (";\n}\n\n", stdout);
540 }
541 \f
542 /* Constraints fall into two categories: register constraints
543    (define_register_constraint), and others (define_constraint,
544    define_memory_constraint, define_address_constraint).  We
545    work out automatically which of the various old-style macros
546    they correspond to, and produce appropriate code.  They all
547    go in the same hash table so we can verify that there are no
548    duplicate names.  */
549
550 /* All data from one constraint definition.  */
551 struct constraint_data
552 {
553   struct constraint_data *next_this_letter;
554   struct constraint_data *next_textual;
555   const char *name;
556   const char *c_name;    /* same as .name unless mangling is necessary */
557   size_t namelen;
558   const char *regclass;  /* for register constraints */
559   rtx exp;               /* for other constraints */
560   unsigned int lineno;   /* line of definition */
561   unsigned int is_register  : 1;
562   unsigned int is_const_int : 1;
563   unsigned int is_const_dbl : 1;
564   unsigned int is_extra     : 1;
565   unsigned int is_memory    : 1;
566   unsigned int is_address   : 1;
567 };
568
569 /* Overview of all constraints beginning with a given letter.  */
570
571 static struct constraint_data *
572 constraints_by_letter_table[1<<CHAR_BIT];
573
574 /* For looking up all the constraints in the order that they appeared
575    in the machine description.  */
576 static struct constraint_data *first_constraint;
577 static struct constraint_data **last_constraint_ptr = &first_constraint;
578
579 #define FOR_ALL_CONSTRAINTS(iter_) \
580   for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
581
582 /* These letters, and all names beginning with them, are reserved for
583    generic constraints.  */
584 static const char generic_constraint_letters[] = "EFVXgimnoprs";
585
586 /* Machine-independent code expects that constraints with these
587    (initial) letters will allow only (a subset of all) CONST_INTs.  */
588
589 static const char const_int_constraints[] = "IJKLMNOP";
590
591 /* Machine-independent code expects that constraints with these
592    (initial) letters will allow only (a subset of all) CONST_DOUBLEs.  */
593
594 static const char const_dbl_constraints[] = "GH";
595
596 /* Summary data used to decide whether to output various functions and
597    macro definitions.  */
598 static unsigned int constraint_max_namelen;
599 static bool have_register_constraints;
600 static bool have_memory_constraints;
601 static bool have_address_constraints;
602 static bool have_extra_constraints;
603 static bool have_const_int_constraints;
604 static bool have_const_dbl_constraints;
605
606 /* Convert NAME, which contains angle brackets and/or underscores, to
607    a string that can be used as part of a C identifier.  The string
608    comes from the rtl_obstack.  */
609 static const char *
610 mangle (const char *name)
611 {
612   for (; *name; name++)
613     switch (*name)
614       {
615       case '_': obstack_grow (rtl_obstack, "__", 2); break;
616       case '<': obstack_grow (rtl_obstack, "_l", 2); break;
617       case '>': obstack_grow (rtl_obstack, "_g", 2); break;
618       default: obstack_1grow (rtl_obstack, *name); break;
619       }
620
621   obstack_1grow (rtl_obstack, '\0');
622   return obstack_finish (rtl_obstack);
623 }
624
625 /* Add one constraint, of any sort, to the tables.  NAME is its name;
626    REGCLASS is the register class, if any; EXP is the expression to
627    test, if any;  IS_MEMORY and IS_ADDRESS indicate memory and address
628    constraints, respectively; LINENO is the line number from the MD reader.
629    Not all combinations of arguments are valid; most importantly, REGCLASS
630    is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
631    meaningful for constraints with EXP.
632
633    This function enforces all syntactic and semantic rules about what
634    constraints can be defined.  */
635
636 static void
637 add_constraint (const char *name, const char *regclass,
638                 rtx exp, bool is_memory, bool is_address,
639                 int lineno)
640 {
641   struct constraint_data *c, **iter, **slot;
642   const char *p;
643   bool need_mangled_name = false;
644   bool is_const_int;
645   bool is_const_dbl;
646   size_t namelen;
647
648   if (exp && validate_exp (exp, name, lineno))
649     return;
650
651   if (!ISALPHA (name[0]) && name[0] != '_')
652     {
653       if (name[1] == '\0')
654         message_with_line (lineno, "constraint name '%s' is not "
655                            "a letter or underscore", name);
656       else
657         message_with_line (lineno, "constraint name '%s' does not begin "
658                            "with a letter or underscore", name);
659       have_error = 1;
660       return;
661     }
662   for (p = name; *p; p++)
663     if (!ISALNUM (*p))
664       {
665         if (*p == '<' || *p == '>' || *p == '_')
666           need_mangled_name = true;
667         else
668           {
669             message_with_line (lineno,
670                                "constraint name '%s' must be composed of "
671                                "letters, digits, underscores, and "
672                                "angle brackets", name);
673             have_error = 1;
674             return;
675           }
676       }
677
678   if (strchr (generic_constraint_letters, name[0]))
679     {
680       if (name[1] == '\0')
681         message_with_line (lineno, "constraint letter '%s' cannot be "
682                            "redefined by the machine description", name);
683       else
684         message_with_line (lineno, "constraint name '%s' cannot be defined by "
685                            "the machine description, as it begins with '%c'",
686                            name, name[0]);
687       have_error = 1;
688       return;
689     }
690
691   
692   namelen = strlen (name);
693   slot = &constraints_by_letter_table[(unsigned int)name[0]];
694   for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
695     {
696       /* This causes slot to end up pointing to the
697          next_this_letter field of the last constraint with a name
698          of equal or greater length than the new constraint; hence
699          the new constraint will be inserted after all previous
700          constraints with names of the same length.  */
701       if ((*iter)->namelen >= namelen)
702         slot = iter;
703
704       if (!strcmp ((*iter)->name, name))
705         {
706           message_with_line (lineno, "redefinition of constraint '%s'", name);
707           message_with_line ((*iter)->lineno, "previous definition is here");
708           have_error = 1;
709           return;
710         }
711       else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
712         {
713           message_with_line (lineno, "defining constraint '%s' here", name);
714           message_with_line ((*iter)->lineno, "renders constraint '%s' "
715                              "(defined here) a prefix", (*iter)->name);
716           have_error = 1;
717           return;
718         }
719       else if (!strncmp ((*iter)->name, name, namelen))
720         {
721           message_with_line (lineno, "constraint '%s' is a prefix", name);
722           message_with_line ((*iter)->lineno, "of constraint '%s' "
723                              "(defined here)", (*iter)->name);
724           have_error = 1;
725           return;
726         }
727     }
728
729   is_const_int = strchr (const_int_constraints, name[0]) != 0;
730   is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
731
732   if (is_const_int || is_const_dbl)
733     {
734       enum rtx_code appropriate_code
735         = is_const_int ? CONST_INT : CONST_DOUBLE;
736
737       /* Consider relaxing this requirement in the future.  */
738       if (regclass
739           || GET_CODE (exp) != AND
740           || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
741           || strcmp (XSTR (XEXP (exp, 0), 0),
742                      GET_RTX_NAME (appropriate_code)))
743         {
744           if (name[1] == '\0')
745             message_with_line (lineno, "constraint letter '%c' is reserved "
746                                "for %s constraints",
747                                name[0], GET_RTX_NAME (appropriate_code));
748           else
749             message_with_line (lineno, "constraint names beginning with '%c' "
750                                "(%s) are reserved for %s constraints",
751                                name[0], name, 
752                                GET_RTX_NAME (appropriate_code));
753
754           have_error = 1;
755           return;
756         }
757
758       if (is_memory)
759         {
760           if (name[1] == '\0')
761             message_with_line (lineno, "constraint letter '%c' cannot be a "
762                                "memory constraint", name[0]);
763           else
764             message_with_line (lineno, "constraint name '%s' begins with '%c', "
765                                "and therefore cannot be a memory constraint",
766                                name, name[0]);
767
768           have_error = 1;
769           return;
770         }
771       else if (is_address)
772         {
773           if (name[1] == '\0')
774             message_with_line (lineno, "constraint letter '%c' cannot be a "
775                                "memory constraint", name[0]);
776           else
777             message_with_line (lineno, "constraint name '%s' begins with '%c', "
778                                "and therefore cannot be a memory constraint",
779                                name, name[0]);
780
781           have_error = 1;
782           return;
783         }
784
785       /* Remove the redundant (and (match_code "const_(int|double)")
786          from the expression.  */
787       exp = XEXP (exp, 1);
788     }
789
790   
791   c = obstack_alloc (rtl_obstack, sizeof (struct constraint_data));
792   c->name = name;
793   c->c_name = need_mangled_name ? mangle (name) : name;
794   c->lineno = lineno;
795   c->namelen = namelen;
796   c->regclass = regclass;
797   c->exp = exp;
798   c->is_register = regclass != 0;
799   c->is_const_int = is_const_int;
800   c->is_const_dbl = is_const_dbl;
801   c->is_extra = !(regclass || is_const_int || is_const_dbl);
802   c->is_memory = is_memory;
803   c->is_address = is_address;
804
805   c->next_this_letter = *slot;
806   *slot = c;
807
808   /* Insert this constraint in the list of all constraints in textual
809      order.  */
810   c->next_textual = 0;
811   *last_constraint_ptr = c;
812   last_constraint_ptr = &c->next_textual;
813
814   constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
815   have_register_constraints |= c->is_register;
816   have_const_int_constraints |= c->is_const_int;
817   have_const_dbl_constraints |= c->is_const_dbl;
818   have_extra_constraints |= c->is_extra;
819   have_memory_constraints |= c->is_memory;
820   have_address_constraints |= c->is_address;
821 }
822
823 /* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
824    DEFINE_ADDRESS_CONSTRAINT expression, C.  */
825 static void
826 process_define_constraint (rtx c, int lineno)
827 {
828   add_constraint (XSTR (c, 0), 0, XEXP (c, 2),
829                   GET_CODE (c) == DEFINE_MEMORY_CONSTRAINT,
830                   GET_CODE (c) == DEFINE_ADDRESS_CONSTRAINT,
831                   lineno);
832 }
833
834 /* Process a DEFINE_REGISTER_CONSTRAINT expression, C.  */
835 static void
836 process_define_register_constraint (rtx c, int lineno)
837 {
838   add_constraint (XSTR (c, 0), XSTR (c, 1), 0, false, false, lineno);
839 }
840
841 /* Write out an enumeration with one entry per machine-specific
842    constraint.  */
843 static void
844 write_enum_constraint_num (void)
845 {
846   struct constraint_data *c;
847
848   fputs ("enum constraint_num\n"
849          "{\n"
850          "  CONSTRAINT__UNKNOWN = 0", stdout);
851   FOR_ALL_CONSTRAINTS (c)
852     printf (",\n  CONSTRAINT_%s", c->c_name);
853   puts ("\n};\n");
854 }
855
856 /* Write out a function which looks at a string and determines what
857    constraint name, if any, it begins with.  */
858 static void
859 write_lookup_constraint (void)
860 {
861   unsigned int i;
862   puts ("enum constraint_num\n"
863         "lookup_constraint (const char *str)\n"
864         "{\n"
865         "  switch (str[0])\n"
866         "    {");
867
868   for (i = 0; i < ARRAY_SIZE(constraints_by_letter_table); i++)
869     {
870       struct constraint_data *c = constraints_by_letter_table[i];
871       if (!c)
872         continue;
873
874       printf ("    case '%c':\n", i);
875       if (c->namelen == 1)
876         printf ("      return CONSTRAINT_%s;\n", c->c_name);
877       else
878         {
879           do
880             {
881               printf ("      if (!strncmp (str, \"%s\", %lu))\n"
882                       "        return CONSTRAINT_%s;\n",
883                       c->name, (unsigned long int) c->namelen, c->c_name);
884               c = c->next_this_letter;
885             }
886           while (c);
887           puts ("      break;");
888         }
889     }
890
891   puts ("    default: break;\n"
892         "    }\n"
893         "  return CONSTRAINT__UNKNOWN;\n"
894         "}\n");
895 }
896
897 /* Write out the function which computes constraint name lengths from
898    their enumerators. */
899 static void
900 write_insn_constraint_len (void)
901 {
902   struct constraint_data *c;
903
904   if (constraint_max_namelen == 1)
905     return;
906
907   puts ("size_t\n"
908         "insn_constraint_len (enum constraint_num c)\n"
909         "{\n"
910         "  switch (c)\n"
911         "    {");
912
913   FOR_ALL_CONSTRAINTS (c)
914     if (c->namelen > 1)
915       printf ("    case CONSTRAINT_%s: return %lu;\n", c->c_name,
916               (unsigned long int) c->namelen);
917
918   puts ("    default: break;\n"
919         "    }\n"
920         "  return 1;\n"
921         "}\n");
922 }
923   
924 /* Write out the function which computes the register class corresponding
925    to a register constraint.  */
926 static void
927 write_regclass_for_constraint (void)
928 {
929   struct constraint_data *c;
930
931   puts ("enum reg_class\n"
932         "regclass_for_constraint (enum constraint_num c)\n"
933         "{\n"
934         "  switch (c)\n"
935         "    {");
936
937   FOR_ALL_CONSTRAINTS (c)
938     if (c->is_register)
939       printf ("    case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
940
941   puts ("    default: break;\n"
942         "    }\n"
943         "  return NO_REGS;\n"
944         "}\n");
945 }
946
947 /* Write out the functions which compute whether a given value matches
948    a given non-register constraint.  */
949 static void
950 write_tm_constrs_h (void)
951 {
952   struct constraint_data *c;
953
954   printf ("\
955 /* Generated automatically by the program '%s'\n\
956    from the machine description file '%s'.  */\n\n", progname, in_fname);
957
958   puts ("\
959 #ifndef GCC_TM_CONSTRS_H\n\
960 #define GCC_TM_CONSTRS_H\n");
961
962   FOR_ALL_CONSTRAINTS (c)
963     if (!c->is_register)
964       {
965         bool needs_ival = needs_variable (c->exp, "ival");
966         bool needs_hval = needs_variable (c->exp, "hval");
967         bool needs_lval = needs_variable (c->exp, "lval");
968         bool needs_rval = needs_variable (c->exp, "rval");
969         bool needs_mode = (needs_variable (c->exp, "mode")
970                            || needs_hval || needs_lval || needs_rval);
971
972         printf ("static inline bool\n"
973                 "satisfies_constraint_%s (rtx op)\n"
974                 "{\n", c->c_name);
975         if (needs_mode)
976           puts ("enum machine_mode mode = GET_MODE (op);");
977         if (needs_ival)
978           puts ("  HOST_WIDE_INT ival = 0;");
979         if (needs_hval)
980           puts ("  HOST_WIDE_INT hval = 0;");
981         if (needs_lval)
982           puts ("  unsigned HOST_WIDE_INT lval = 0;");
983         if (needs_rval)
984           puts ("  const REAL_VALUE_TYPE *rval = 0;");
985
986         if (needs_ival)
987           puts ("  if (GET_CODE (op) == CONST_INT)\n"
988                 "    ival = INTVAL (op);");
989         if (needs_hval)
990           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
991                 "    hval = CONST_DOUBLE_HIGH (op);");
992         if (needs_lval)
993           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
994                 "    lval = CONST_DOUBLE_LOW (op);");
995         if (needs_rval)
996           puts ("  if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
997                 "    rval = CONST_DOUBLE_REAL_VALUE (op);");
998           
999         fputs ("  return ", stdout);
1000         write_predicate_expr (c->exp);
1001         fputs (";\n}\n", stdout);
1002       }
1003   puts ("#endif /* tm-constrs.h */");
1004 }
1005
1006 /* Write out the wrapper function, constraint_satisfied_p, that maps
1007    a CONSTRAINT_xxx constant to one of the predicate functions generated
1008    above.  */
1009 static void
1010 write_constraint_satisfied_p (void)
1011 {
1012   struct constraint_data *c;
1013
1014   puts ("bool\n"
1015         "constraint_satisfied_p (rtx op, enum constraint_num c)\n"
1016         "{\n"
1017         "  switch (c)\n"
1018         "    {");
1019
1020   FOR_ALL_CONSTRAINTS (c)
1021     if (!c->is_register)
1022       printf ("    case CONSTRAINT_%s: "
1023               "return satisfies_constraint_%s (op);\n",
1024               c->c_name, c->c_name);
1025
1026   puts ("    default: break;\n"
1027         "    }\n"
1028         "  return false;\n"
1029         "}\n");
1030 }
1031
1032 /* Write out the function which computes whether a given value matches
1033    a given CONST_INT constraint.  This doesn't just forward to
1034    constraint_satisfied_p because caller passes the INTVAL, not the RTX.  */
1035 static void
1036 write_insn_const_int_ok_for_constraint (void)
1037 {
1038   struct constraint_data *c;
1039
1040   puts ("bool\n"
1041         "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1042                                           "enum constraint_num c)\n"
1043         "{\n"
1044         "  switch (c)\n"
1045         "    {");
1046
1047   FOR_ALL_CONSTRAINTS (c)
1048     if (c->is_const_int)
1049       {
1050         printf ("    case CONSTRAINT_%s:\n      return ", c->c_name);
1051         write_predicate_expr (c->exp);
1052         fputs (";\n\n", stdout);
1053       }
1054
1055   puts ("    default: break;\n"
1056         "    }\n"
1057         "  return false;\n"
1058         "}\n");
1059 }
1060
1061
1062 /* Write out the function which computes whether a given constraint is
1063    a memory constraint.  */
1064 static void
1065 write_insn_extra_memory_constraint (void)
1066 {
1067   struct constraint_data *c;
1068
1069   puts ("bool\n"
1070         "insn_extra_memory_constraint (enum constraint_num c)\n"
1071         "{\n"
1072         "  switch (c)\n"
1073         "    {");
1074
1075   FOR_ALL_CONSTRAINTS (c)
1076     if (c->is_memory)
1077       printf ("    case CONSTRAINT_%s:\n      return true;\n\n", c->c_name);
1078
1079   puts ("    default: break;\n"
1080         "    }\n"
1081         "  return false;\n"
1082         "}\n");
1083 }
1084
1085 /* Write out the function which computes whether a given constraint is
1086    an address constraint.  */
1087 static void
1088 write_insn_extra_address_constraint (void)
1089 {
1090   struct constraint_data *c;
1091
1092   puts ("bool\n"
1093         "insn_extra_address_constraint (enum constraint_num c)\n"
1094         "{\n"
1095         "  switch (c)\n"
1096         "    {");
1097
1098   FOR_ALL_CONSTRAINTS (c)
1099     if (c->is_address)
1100       printf ("    case CONSTRAINT_%s:\n      return true;\n\n", c->c_name);
1101
1102   puts ("    default: break;\n"
1103         "    }\n"
1104         "  return false;\n"
1105         "}\n");
1106 }
1107
1108 \f
1109 /* Write tm-preds.h.  Unfortunately, it is impossible to forward-declare
1110    an enumeration in portable C, so we have to condition all these
1111    prototypes on HAVE_MACHINE_MODES.  */
1112 static void
1113 write_tm_preds_h (void)
1114 {
1115   struct pred_data *p;
1116
1117   printf ("\
1118 /* Generated automatically by the program '%s'\n\
1119    from the machine description file '%s'.  */\n\n", progname, in_fname);
1120
1121   puts ("\
1122 #ifndef GCC_TM_PREDS_H\n\
1123 #define GCC_TM_PREDS_H\n\
1124 \n\
1125 #ifdef HAVE_MACHINE_MODES");
1126
1127   FOR_ALL_PREDICATES (p)
1128     printf ("extern int %s (rtx, enum machine_mode);\n", p->name);
1129
1130   puts ("#endif /* HAVE_MACHINE_MODES */\n");
1131
1132   if (constraint_max_namelen > 0)
1133     {
1134       write_enum_constraint_num ();
1135       puts ("extern enum constraint_num lookup_constraint (const char *);\n"
1136             "extern bool constraint_satisfied_p (rtx, enum constraint_num);\n");
1137
1138       if (constraint_max_namelen > 1)
1139         puts ("extern size_t insn_constraint_len (enum constraint_num);\n"
1140               "#define CONSTRAINT_LEN(c_,s_) "
1141               "insn_constraint_len (lookup_constraint (s_))\n");
1142       else
1143         puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1144       if (have_register_constraints)
1145         puts ("extern enum reg_class regclass_for_constraint "
1146               "(enum constraint_num);\n"
1147               "#define REG_CLASS_FROM_CONSTRAINT(c_,s_) \\\n"
1148               "    regclass_for_constraint (lookup_constraint (s_))\n");
1149       else
1150         puts ("#define REG_CLASS_FROM_CONSTRAINT(c_,s_) NO_REGS");
1151       if (have_const_int_constraints)
1152         puts ("extern bool insn_const_int_ok_for_constraint "
1153               "(HOST_WIDE_INT, enum constraint_num);\n"
1154               "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1155               "    insn_const_int_ok_for_constraint (v_, "
1156               "lookup_constraint (s_))\n");
1157       if (have_const_dbl_constraints)
1158         puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1159               "    constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1160       else
1161         puts ("#define CONST_DOUBLE_OK_FOR_CONSTRAINT_P(v_,c_,s_) 0\n");
1162       if (have_extra_constraints)
1163         puts ("#define EXTRA_CONSTRAINT_STR(v_,c_,s_) \\\n"
1164               "    constraint_satisfied_p (v_, lookup_constraint (s_))\n");
1165       if (have_memory_constraints)
1166         puts ("extern bool "
1167               "insn_extra_memory_constraint (enum constraint_num);\n"
1168               "#define EXTRA_MEMORY_CONSTRAINT(c_,s_) "
1169               "insn_extra_memory_constraint (lookup_constraint (s_))\n");
1170       else
1171         puts ("#define EXTRA_MEMORY_CONSTRAINT(c_,s_) false\n");
1172       if (have_address_constraints)
1173         puts ("extern bool "
1174               "insn_extra_address_constraint (enum constraint_num);\n"
1175               "#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) "
1176               "insn_extra_address_constraint (lookup_constraint (s_))\n");
1177       else
1178         puts ("#define EXTRA_ADDRESS_CONSTRAINT(c_,s_) false\n");
1179     }
1180
1181   puts ("#endif /* tm-preds.h */");
1182 }
1183
1184 /* Write insn-preds.c.  
1185    N.B. the list of headers to include was copied from genrecog; it
1186    may not be ideal.
1187
1188    FUTURE: Write #line markers referring back to the machine
1189    description.  (Can't practically do this now since we don't know
1190    the line number of the C block - just the line number of the enclosing
1191    expression.)  */
1192 static void
1193 write_insn_preds_c (void)
1194 {
1195   struct pred_data *p;
1196
1197   printf ("\
1198 /* Generated automatically by the program '%s'\n\
1199    from the machine description file '%s'.  */\n\n", progname, in_fname);
1200
1201   puts ("\
1202 #include \"config.h\"\n\
1203 #include \"system.h\"\n\
1204 #include \"coretypes.h\"\n\
1205 #include \"tm.h\"\n\
1206 #include \"rtl.h\"\n\
1207 #include \"tree.h\"\n\
1208 #include \"tm_p.h\"\n\
1209 #include \"function.h\"\n\
1210 #include \"insn-config.h\"\n\
1211 #include \"recog.h\"\n\
1212 #include \"real.h\"\n\
1213 #include \"output.h\"\n\
1214 #include \"flags.h\"\n\
1215 #include \"hard-reg-set.h\"\n\
1216 #include \"resource.h\"\n\
1217 #include \"toplev.h\"\n\
1218 #include \"reload.h\"\n\
1219 #include \"regs.h\"\n\
1220 #include \"tm-constrs.h\"\n");
1221
1222   FOR_ALL_PREDICATES (p)
1223     write_one_predicate_function (p);
1224
1225   if (constraint_max_namelen > 0)
1226     {
1227       write_lookup_constraint ();
1228       write_regclass_for_constraint ();
1229       write_constraint_satisfied_p ();
1230       
1231       if (constraint_max_namelen > 1)
1232         write_insn_constraint_len ();
1233
1234       if (have_const_int_constraints)
1235         write_insn_const_int_ok_for_constraint ();
1236
1237       if (have_memory_constraints)
1238         write_insn_extra_memory_constraint ();
1239       if (have_address_constraints)
1240         write_insn_extra_address_constraint ();
1241     }
1242 }
1243
1244 /* Argument parsing.  */
1245 static bool gen_header;
1246 static bool gen_constrs;
1247
1248 static bool
1249 parse_option (const char *opt)
1250 {
1251   if (!strcmp (opt, "-h"))
1252     {
1253       gen_header = true;
1254       return 1;
1255     }
1256   else if (!strcmp (opt, "-c"))
1257     {
1258       gen_constrs = true;
1259       return 1;
1260     }
1261   else
1262     return 0;
1263 }
1264
1265 /* Master control.  */
1266 int
1267 main (int argc, char **argv)
1268 {
1269   rtx defn;
1270   int pattern_lineno, next_insn_code = 0;
1271
1272   progname = argv[0];
1273   if (argc <= 1)
1274     fatal ("no input file name");
1275   if (init_md_reader_args_cb (argc, argv, parse_option) != SUCCESS_EXIT_CODE)
1276     return FATAL_EXIT_CODE;
1277
1278   while ((defn = read_md_rtx (&pattern_lineno, &next_insn_code)) != 0)
1279     switch (GET_CODE (defn))
1280       {
1281       case DEFINE_PREDICATE:
1282       case DEFINE_SPECIAL_PREDICATE:
1283         process_define_predicate (defn, pattern_lineno);
1284         break;
1285
1286       case DEFINE_CONSTRAINT:
1287       case DEFINE_MEMORY_CONSTRAINT:
1288       case DEFINE_ADDRESS_CONSTRAINT:
1289         process_define_constraint (defn, pattern_lineno);
1290         break;
1291
1292       case DEFINE_REGISTER_CONSTRAINT:
1293         process_define_register_constraint (defn, pattern_lineno);
1294         break;
1295
1296       default:
1297         break;
1298       }
1299
1300   if (gen_header)
1301     write_tm_preds_h ();
1302   else if (gen_constrs)
1303     write_tm_constrs_h ();
1304   else
1305     write_insn_preds_c ();
1306
1307   if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1308     return FATAL_EXIT_CODE;
1309
1310   return SUCCESS_EXIT_CODE;
1311 }