OSDN Git Service

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