]> gcc.gnu.org Git - gcc.git/blame - gcc/genpreds.c
function.h (struct rtl_data): Remove struct and accessor macros.
[gcc.git] / gcc / genpreds.c
CommitLineData
1b0c37d7 1/* Generate from machine description:
e543e219
ZW
2 - prototype declarations for operand predicates (tm-preds.h)
3 - function definitions of operand predicates, if defined new-style
4 (insn-preds.c)
5624e564 5 Copyright (C) 2001-2015 Free Software Foundation, Inc.
1b0c37d7 6
40803cd5 7This file is part of GCC.
1b0c37d7 8
40803cd5 9GCC is free software; you can redistribute it and/or modify
1b0c37d7 10it under the terms of the GNU General Public License as published by
9dcd6f09 11the Free Software Foundation; either version 3, or (at your option)
1b0c37d7
ZW
12any later version.
13
40803cd5 14GCC is distributed in the hope that it will be useful,
1b0c37d7
ZW
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
9dcd6f09
NC
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
1b0c37d7 22
4977bab6 23#include "bconfig.h"
1b0c37d7 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
1b0c37d7 27#include "rtl.h"
e543e219 28#include "errors.h"
e543e219 29#include "obstack.h"
10692477 30#include "read-md.h"
f38840db 31#include "gensupport.h"
1b0c37d7 32
8677664e
RS
33static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
34
f38840db
ZW
35/* Given a predicate expression EXP, from form NAME at line LINENO,
36 verify that it does not contain any RTL constructs which are not
37 valid in predicate definitions. Returns true if EXP is
38 INvalid; issues error messages, caller need not. */
39static bool
40validate_exp (rtx exp, const char *name, int lineno)
1b0c37d7 41{
f38840db 42 if (exp == 0)
e543e219 43 {
f38840db
ZW
44 message_with_line (lineno, "%s: must give a predicate expression", name);
45 return true;
e543e219
ZW
46 }
47
f38840db
ZW
48 switch (GET_CODE (exp))
49 {
50 /* Ternary, binary, unary expressions: recurse into subexpressions. */
51 case IF_THEN_ELSE:
52 if (validate_exp (XEXP (exp, 2), name, lineno))
53 return true;
54 /* else fall through */
55 case AND:
56 case IOR:
57 if (validate_exp (XEXP (exp, 1), name, lineno))
58 return true;
59 /* else fall through */
60 case NOT:
61 return validate_exp (XEXP (exp, 0), name, lineno);
e543e219 62
f38840db
ZW
63 /* MATCH_CODE might have a syntax error in its path expression. */
64 case MATCH_CODE:
65 {
66 const char *p;
67 for (p = XSTR (exp, 1); *p; p++)
68 {
69 if (!ISDIGIT (*p) && !ISLOWER (*p))
70 {
bb933490
RS
71 error_with_line (lineno, "%s: invalid character in path "
72 "string '%s'", name, XSTR (exp, 1));
f38840db
ZW
73 return true;
74 }
75 }
76 }
77 /* fall through */
e543e219 78
f38840db
ZW
79 /* These need no special checking. */
80 case MATCH_OPERAND:
81 case MATCH_TEST:
82 return false;
83
84 default:
bb933490
RS
85 error_with_line (lineno,
86 "%s: cannot use '%s' in a predicate expression",
87 name, GET_RTX_NAME (GET_CODE (exp)));
f38840db
ZW
88 return true;
89 }
e543e219
ZW
90}
91
f38840db
ZW
92/* Predicates are defined with (define_predicate) or
93 (define_special_predicate) expressions in the machine description. */
e543e219 94static void
f38840db 95process_define_predicate (rtx defn, int lineno)
e543e219 96{
77059241 97 validate_exp (XEXP (defn, 1), XSTR (defn, 0), lineno);
e543e219
ZW
98}
99
100/* Given a predicate, if it has an embedded C block, write the block
101 out as a static inline subroutine, and augment the RTL test with a
102 match_test that calls that subroutine. For instance,
103
104 (define_predicate "basereg_operand"
105 (match_operand 0 "register_operand")
106 {
107 if (GET_CODE (op) == SUBREG)
108 op = SUBREG_REG (op);
109 return REG_POINTER (op);
110 })
111
112 becomes
113
ef4bddc2 114 static inline int basereg_operand_1(rtx op, machine_mode mode)
e543e219
ZW
115 {
116 if (GET_CODE (op) == SUBREG)
117 op = SUBREG_REG (op);
118 return REG_POINTER (op);
119 }
120
121 (define_predicate "basereg_operand"
122 (and (match_operand 0 "register_operand")
123 (match_test "basereg_operand_1 (op, mode)")))
124
125 The only wart is that there's no way to insist on a { } string in
471854f8 126 an RTL template, so we have to handle "" strings. */
e543e219 127
b8698a0f 128
e543e219
ZW
129static void
130write_predicate_subfunction (struct pred_data *p)
131{
132 const char *match_test_str;
133 rtx match_test_exp, and_exp;
134
135 if (p->c_block[0] == '\0')
136 return;
137
138 /* Construct the function-call expression. */
139 obstack_grow (rtl_obstack, p->name, strlen (p->name));
140 obstack_grow (rtl_obstack, "_1 (op, mode)",
141 sizeof "_1 (op, mode)");
7973fd2a 142 match_test_str = XOBFINISH (rtl_obstack, const char *);
e543e219
ZW
143
144 /* Add the function-call expression to the complete expression to be
145 evaluated. */
146 match_test_exp = rtx_alloc (MATCH_TEST);
147 XSTR (match_test_exp, 0) = match_test_str;
148
149 and_exp = rtx_alloc (AND);
150 XEXP (and_exp, 0) = p->exp;
151 XEXP (and_exp, 1) = match_test_exp;
152
153 p->exp = and_exp;
154
155 printf ("static inline int\n"
ef4bddc2 156 "%s_1 (rtx op, machine_mode mode ATTRIBUTE_UNUSED)\n",
e543e219 157 p->name);
d2a3ce4e 158 print_md_ptr_loc (p->c_block);
e543e219
ZW
159 if (p->c_block[0] == '{')
160 fputs (p->c_block, stdout);
161 else
162 printf ("{\n %s\n}", p->c_block);
163 fputs ("\n\n", stdout);
164}
165
f38840db
ZW
166/* Given a predicate expression EXP, from form NAME, determine whether
167 it refers to the variable given as VAR. */
168static bool
169needs_variable (rtx exp, const char *var)
170{
171 switch (GET_CODE (exp))
172 {
173 /* Ternary, binary, unary expressions need a variable if
174 any of their subexpressions do. */
175 case IF_THEN_ELSE:
176 if (needs_variable (XEXP (exp, 2), var))
177 return true;
178 /* else fall through */
179 case AND:
180 case IOR:
181 if (needs_variable (XEXP (exp, 1), var))
182 return true;
183 /* else fall through */
184 case NOT:
185 return needs_variable (XEXP (exp, 0), var);
186
187 /* MATCH_CODE uses "op", but nothing else. */
188 case MATCH_CODE:
189 return !strcmp (var, "op");
190
191 /* MATCH_OPERAND uses "op" and may use "mode". */
192 case MATCH_OPERAND:
193 if (!strcmp (var, "op"))
194 return true;
195 if (!strcmp (var, "mode") && GET_MODE (exp) == VOIDmode)
196 return true;
197 return false;
198
199 /* MATCH_TEST uses var if XSTR (exp, 0) =~ /\b${var}\b/o; */
200 case MATCH_TEST:
201 {
202 const char *p = XSTR (exp, 0);
203 const char *q = strstr (p, var);
204 if (!q)
205 return false;
206 if (q != p && (ISALNUM (q[-1]) || q[-1] == '_'))
207 return false;
208 q += strlen (var);
3b664bd1 209 if (ISALNUM (q[0]) || q[0] == '_')
f38840db
ZW
210 return false;
211 }
212 return true;
213
214 default:
215 gcc_unreachable ();
216 }
217}
218
e543e219
ZW
219/* Given an RTL expression EXP, find all subexpressions which we may
220 assume to perform mode tests. Normal MATCH_OPERAND does;
40130403
RS
221 MATCH_CODE doesn't as such (although certain codes always have
222 VOIDmode); and we have to assume that MATCH_TEST does not.
223 These combine in almost-boolean fashion - the only exception is
224 that (not X) must be assumed not to perform a mode test, whether
225 or not X does.
e543e219
ZW
226
227 The mark is the RTL /v flag, which is true for subexpressions which
228 do *not* perform mode tests.
229*/
230#define NO_MODE_TEST(EXP) RTX_FLAG (EXP, volatil)
231static void
232mark_mode_tests (rtx exp)
233{
234 switch (GET_CODE (exp))
235 {
236 case MATCH_OPERAND:
237 {
238 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
239 if (!p)
240 error ("reference to undefined predicate '%s'", XSTR (exp, 1));
bb56fc39 241 else if (p->special || GET_MODE (exp) != VOIDmode)
e543e219
ZW
242 NO_MODE_TEST (exp) = 1;
243 }
244 break;
245
246 case MATCH_CODE:
40130403 247 NO_MODE_TEST (exp) = 1;
e543e219
ZW
248 break;
249
250 case MATCH_TEST:
251 case NOT:
252 NO_MODE_TEST (exp) = 1;
253 break;
254
255 case AND:
256 mark_mode_tests (XEXP (exp, 0));
257 mark_mode_tests (XEXP (exp, 1));
258
259 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
260 && NO_MODE_TEST (XEXP (exp, 1)));
261 break;
b8698a0f 262
e543e219
ZW
263 case IOR:
264 mark_mode_tests (XEXP (exp, 0));
265 mark_mode_tests (XEXP (exp, 1));
266
267 NO_MODE_TEST (exp) = (NO_MODE_TEST (XEXP (exp, 0))
268 || NO_MODE_TEST (XEXP (exp, 1)));
269 break;
270
271 case IF_THEN_ELSE:
272 /* A ? B : C does a mode test if (one of A and B) does a mode
273 test, and C does too. */
274 mark_mode_tests (XEXP (exp, 0));
275 mark_mode_tests (XEXP (exp, 1));
276 mark_mode_tests (XEXP (exp, 2));
277
278 NO_MODE_TEST (exp) = ((NO_MODE_TEST (XEXP (exp, 0))
279 && NO_MODE_TEST (XEXP (exp, 1)))
280 || NO_MODE_TEST (XEXP (exp, 2)));
281 break;
282
283 default:
f38840db 284 gcc_unreachable ();
e543e219
ZW
285 }
286}
287
7caf6734
RS
288/* Determine whether the expression EXP is a MATCH_CODE that should
289 be written as a switch statement. */
290static bool
291generate_switch_p (rtx exp)
292{
293 return GET_CODE (exp) == MATCH_CODE
294 && strchr (XSTR (exp, 0), ',');
295}
296
e543e219
ZW
297/* Given a predicate, work out where in its RTL expression to add
298 tests for proper modes. Special predicates do not get any such
299 tests. We try to avoid adding tests when we don't have to; in
300 particular, other normal predicates can be counted on to do it for
301 us. */
302
303static void
304add_mode_tests (struct pred_data *p)
305{
306 rtx match_test_exp, and_exp;
307 rtx *pos;
308
309 /* Don't touch special predicates. */
310 if (p->special)
311 return;
312
40130403
RS
313 /* Check whether the predicate accepts const scalar ints (which always
314 have a stored mode of VOIDmode, but logically have a real mode)
315 and whether it matches anything besides const scalar ints. */
316 bool matches_const_scalar_int_p = false;
317 bool matches_other_p = false;
318 for (int i = 0; i < NUM_RTX_CODE; ++i)
319 if (p->codes[i])
320 switch (i)
321 {
322 case CONST_INT:
323 case CONST_WIDE_INT:
324 matches_const_scalar_int_p = true;
325 break;
326
327 case CONST_DOUBLE:
328 if (!TARGET_SUPPORTS_WIDE_INT)
329 matches_const_scalar_int_p = true;
330 matches_other_p = true;
331 break;
332
333 default:
334 matches_other_p = true;
335 break;
336 }
337
338 /* There's no need for a mode check if the predicate only accepts
339 constant integers. The code checks in the predicate are enough
340 to establish that the mode is VOIDmode.
341
342 Note that the predicate itself should check whether a scalar
343 integer is in range of the given mode. */
344 if (!matches_other_p)
345 return;
346
e543e219
ZW
347 mark_mode_tests (p->exp);
348
349 /* If the whole expression already tests the mode, we're done. */
350 if (!NO_MODE_TEST (p->exp))
351 return;
352
353 match_test_exp = rtx_alloc (MATCH_TEST);
40130403
RS
354 if (matches_const_scalar_int_p)
355 XSTR (match_test_exp, 0) = ("mode == VOIDmode || GET_MODE (op) == mode"
356 " || GET_MODE (op) == VOIDmode");
357 else
358 XSTR (match_test_exp, 0) = "mode == VOIDmode || GET_MODE (op) == mode";
e543e219
ZW
359 and_exp = rtx_alloc (AND);
360 XEXP (and_exp, 1) = match_test_exp;
361
362 /* It is always correct to rewrite p->exp as
363
364 (and (...) (match_test "mode == VOIDmode || GET_MODE (op) == mode"))
365
366 but there are a couple forms where we can do better. If the
367 top-level pattern is an IOR, and one of the two branches does test
368 the mode, we can wrap just the branch that doesn't. Likewise, if
369 we have an IF_THEN_ELSE, and one side of it tests the mode, we can
370 wrap just the side that doesn't. And, of course, we can repeat this
371 descent as many times as it works. */
372
373 pos = &p->exp;
374 for (;;)
375 {
376 rtx subexp = *pos;
b2d59f6f
NS
377
378 switch (GET_CODE (subexp))
e543e219 379 {
7caf6734
RS
380 case AND:
381 /* The switch code generation in write_predicate_stmts prefers
382 rtx code tests to be at the top of the expression tree. So
2a8a8292 383 push this AND down into the second operand of an existing
7caf6734
RS
384 AND expression. */
385 if (generate_switch_p (XEXP (subexp, 0)))
386 pos = &XEXP (subexp, 1);
387 goto break_loop;
388
b2d59f6f
NS
389 case IOR:
390 {
391 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
392 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
b8698a0f 393
b2d59f6f 394 gcc_assert (test0 || test1);
b8698a0f 395
b2d59f6f
NS
396 if (test0 && test1)
397 goto break_loop;
398 pos = test0 ? &XEXP (subexp, 0) : &XEXP (subexp, 1);
399 }
400 break;
b8698a0f 401
b2d59f6f
NS
402 case IF_THEN_ELSE:
403 {
404 int test0 = NO_MODE_TEST (XEXP (subexp, 0));
405 int test1 = NO_MODE_TEST (XEXP (subexp, 1));
406 int test2 = NO_MODE_TEST (XEXP (subexp, 2));
b8698a0f 407
b2d59f6f 408 gcc_assert ((test0 && test1) || test2);
b8698a0f 409
b2d59f6f
NS
410 if (test0 && test1 && test2)
411 goto break_loop;
412 if (test0 && test1)
413 /* Must put it on the dependent clause, not the
414 controlling expression, or we change the meaning of
471854f8 415 the test. */
b2d59f6f
NS
416 pos = &XEXP (subexp, 1);
417 else
418 pos = &XEXP (subexp, 2);
419 }
420 break;
b8698a0f 421
b2d59f6f
NS
422 default:
423 goto break_loop;
e543e219 424 }
e543e219 425 }
b2d59f6f 426 break_loop:
e543e219
ZW
427 XEXP (and_exp, 0) = *pos;
428 *pos = and_exp;
429}
430
6e7a4706
ZW
431/* PATH is a string describing a path from the root of an RTL
432 expression to an inner subexpression to be tested. Output
433 code which computes the subexpression from the variable
434 holding the root of the expression. */
435static void
436write_extract_subexp (const char *path)
437{
438 int len = strlen (path);
439 int i;
440
441 /* We first write out the operations (XEXP or XVECEXP) in reverse
442 order, then write "op", then the indices in forward order. */
443 for (i = len - 1; i >= 0; i--)
444 {
445 if (ISLOWER (path[i]))
446 fputs ("XVECEXP (", stdout);
447 else if (ISDIGIT (path[i]))
448 fputs ("XEXP (", stdout);
449 else
f38840db 450 gcc_unreachable ();
6e7a4706
ZW
451 }
452
453 fputs ("op", stdout);
454
455 for (i = 0; i < len; i++)
456 {
457 if (ISLOWER (path[i]))
458 printf (", 0, %d)", path[i] - 'a');
459 else if (ISDIGIT (path[i]))
460 printf (", %d)", path[i] - '0');
461 else
462 gcc_unreachable ();
463 }
464}
e543e219
ZW
465
466/* CODES is a list of RTX codes. Write out an expression which
467 determines whether the operand has one of those codes. */
468static void
6e7a4706 469write_match_code (const char *path, const char *codes)
e543e219
ZW
470{
471 const char *code;
472
473 while ((code = scan_comma_elt (&codes)) != 0)
474 {
6e7a4706
ZW
475 fputs ("GET_CODE (", stdout);
476 write_extract_subexp (path);
477 fputs (") == ", stdout);
e543e219
ZW
478 while (code < codes)
479 {
480 putchar (TOUPPER (*code));
481 code++;
482 }
b8698a0f 483
e543e219
ZW
484 if (*codes == ',')
485 fputs (" || ", stdout);
486 }
487}
488
489/* EXP is an RTL (sub)expression for a predicate. Recursively
490 descend the expression and write out an equivalent C expression. */
491static void
f38840db 492write_predicate_expr (rtx exp)
e543e219
ZW
493{
494 switch (GET_CODE (exp))
495 {
496 case AND:
497 putchar ('(');
f38840db 498 write_predicate_expr (XEXP (exp, 0));
e543e219 499 fputs (") && (", stdout);
f38840db 500 write_predicate_expr (XEXP (exp, 1));
e543e219
ZW
501 putchar (')');
502 break;
b8698a0f 503
e543e219
ZW
504 case IOR:
505 putchar ('(');
f38840db 506 write_predicate_expr (XEXP (exp, 0));
e543e219 507 fputs (") || (", stdout);
f38840db 508 write_predicate_expr (XEXP (exp, 1));
e543e219
ZW
509 putchar (')');
510 break;
511
512 case NOT:
513 fputs ("!(", stdout);
f38840db 514 write_predicate_expr (XEXP (exp, 0));
e543e219
ZW
515 putchar (')');
516 break;
517
518 case IF_THEN_ELSE:
519 putchar ('(');
f38840db 520 write_predicate_expr (XEXP (exp, 0));
e543e219 521 fputs (") ? (", stdout);
f38840db 522 write_predicate_expr (XEXP (exp, 1));
e543e219 523 fputs (") : (", stdout);
f38840db 524 write_predicate_expr (XEXP (exp, 2));
e543e219
ZW
525 putchar (')');
526 break;
527
528 case MATCH_OPERAND:
bb56fc39
PB
529 if (GET_MODE (exp) == VOIDmode)
530 printf ("%s (op, mode)", XSTR (exp, 1));
531 else
532 printf ("%s (op, %smode)", XSTR (exp, 1), mode_name[GET_MODE (exp)]);
e543e219
ZW
533 break;
534
535 case MATCH_CODE:
6e7a4706 536 write_match_code (XSTR (exp, 1), XSTR (exp, 0));
e543e219
ZW
537 break;
538
539 case MATCH_TEST:
7445392c 540 print_c_condition (XSTR (exp, 0));
e543e219
ZW
541 break;
542
543 default:
f38840db 544 gcc_unreachable ();
e543e219
ZW
545 }
546}
547
7caf6734
RS
548/* Write the MATCH_CODE expression EXP as a switch statement. */
549
550static void
551write_match_code_switch (rtx exp)
552{
c8d560fa
RS
553 const char *codes = XSTR (exp, 0);
554 const char *path = XSTR (exp, 1);
7caf6734
RS
555 const char *code;
556
557 fputs (" switch (GET_CODE (", stdout);
558 write_extract_subexp (path);
559 fputs ("))\n {\n", stdout);
560
561 while ((code = scan_comma_elt (&codes)) != 0)
562 {
563 fputs (" case ", stdout);
564 while (code < codes)
565 {
566 putchar (TOUPPER (*code));
567 code++;
568 }
c3284718 569 fputs (":\n", stdout);
7caf6734
RS
570 }
571}
572
2a8a8292 573/* Given a predicate expression EXP, write out a sequence of stmts
7caf6734
RS
574 to evaluate it. This is similar to write_predicate_expr but can
575 generate efficient switch statements. */
576
577static void
578write_predicate_stmts (rtx exp)
579{
580 switch (GET_CODE (exp))
581 {
582 case MATCH_CODE:
583 if (generate_switch_p (exp))
584 {
585 write_match_code_switch (exp);
586 puts (" return true;\n"
587 " default:\n"
588 " break;\n"
589 " }\n"
590 " return false;");
591 return;
592 }
593 break;
594
595 case AND:
596 if (generate_switch_p (XEXP (exp, 0)))
597 {
598 write_match_code_switch (XEXP (exp, 0));
599 puts (" break;\n"
600 " default:\n"
601 " return false;\n"
602 " }");
603 exp = XEXP (exp, 1);
604 }
605 break;
606
607 case IOR:
608 if (generate_switch_p (XEXP (exp, 0)))
609 {
610 write_match_code_switch (XEXP (exp, 0));
611 puts (" return true;\n"
612 " default:\n"
613 " break;\n"
614 " }");
615 exp = XEXP (exp, 1);
616 }
8547c7f8 617 break;
7caf6734
RS
618
619 case NOT:
620 if (generate_switch_p (XEXP (exp, 0)))
621 {
622 write_match_code_switch (XEXP (exp, 0));
623 puts (" return false;\n"
624 " default:\n"
625 " break;\n"
626 " }\n"
627 " return true;");
628 return;
629 }
630 break;
631
632 default:
633 break;
634 }
635
c3284718 636 fputs (" return ",stdout);
7caf6734 637 write_predicate_expr (exp);
c3284718 638 fputs (";\n", stdout);
7caf6734
RS
639}
640
e543e219
ZW
641/* Given a predicate, write out a complete C function to compute it. */
642static void
643write_one_predicate_function (struct pred_data *p)
644{
645 if (!p->exp)
646 return;
647
648 write_predicate_subfunction (p);
649 add_mode_tests (p);
650
ef4bddc2 651 /* A normal predicate can legitimately not look at machine_mode
807e902e 652 if it accepts only CONST_INTs and/or CONST_WIDE_INT and/or CONST_DOUBLEs. */
ef4bddc2 653 printf ("int\n%s (rtx op, machine_mode mode ATTRIBUTE_UNUSED)\n{\n",
e543e219 654 p->name);
7caf6734
RS
655 write_predicate_stmts (p->exp);
656 fputs ("}\n\n", stdout);
1b0c37d7 657}
f38840db
ZW
658\f
659/* Constraints fall into two categories: register constraints
660 (define_register_constraint), and others (define_constraint,
661 define_memory_constraint, define_address_constraint). We
662 work out automatically which of the various old-style macros
663 they correspond to, and produce appropriate code. They all
664 go in the same hash table so we can verify that there are no
665 duplicate names. */
666
667/* All data from one constraint definition. */
668struct constraint_data
669{
670 struct constraint_data *next_this_letter;
671 struct constraint_data *next_textual;
672 const char *name;
673 const char *c_name; /* same as .name unless mangling is necessary */
674 size_t namelen;
675 const char *regclass; /* for register constraints */
676 rtx exp; /* for other constraints */
677 unsigned int lineno; /* line of definition */
98c1627c
JJ
678 unsigned int is_register : 1;
679 unsigned int is_const_int : 1;
680 unsigned int is_const_dbl : 1;
681 unsigned int is_extra : 1;
682 unsigned int is_memory : 1;
683 unsigned int is_address : 1;
684 unsigned int maybe_allows_reg : 1;
685 unsigned int maybe_allows_mem : 1;
f38840db
ZW
686};
687
688/* Overview of all constraints beginning with a given letter. */
689
690static struct constraint_data *
691constraints_by_letter_table[1<<CHAR_BIT];
692
693/* For looking up all the constraints in the order that they appeared
694 in the machine description. */
695static struct constraint_data *first_constraint;
696static struct constraint_data **last_constraint_ptr = &first_constraint;
697
698#define FOR_ALL_CONSTRAINTS(iter_) \
699 for (iter_ = first_constraint; iter_; iter_ = iter_->next_textual)
700
8677664e
RS
701/* Contraint letters that have a special meaning and that cannot be used
702 in define*_constraints. */
703static const char generic_constraint_letters[] = "g";
f38840db
ZW
704
705/* Machine-independent code expects that constraints with these
706 (initial) letters will allow only (a subset of all) CONST_INTs. */
707
708static const char const_int_constraints[] = "IJKLMNOP";
709
710/* Machine-independent code expects that constraints with these
711 (initial) letters will allow only (a subset of all) CONST_DOUBLEs. */
712
713static const char const_dbl_constraints[] = "GH";
714
715/* Summary data used to decide whether to output various functions and
716 macro definitions. */
717static unsigned int constraint_max_namelen;
718static bool have_register_constraints;
719static bool have_memory_constraints;
720static bool have_address_constraints;
721static bool have_extra_constraints;
722static bool have_const_int_constraints;
2aeedf58
RS
723static unsigned int num_constraints;
724
725static const constraint_data **enum_order;
726static unsigned int register_start, register_end;
727static unsigned int satisfied_start;
d9c35eee 728static unsigned int const_int_start, const_int_end;
2aeedf58
RS
729static unsigned int memory_start, memory_end;
730static unsigned int address_start, address_end;
98c1627c
JJ
731static unsigned int maybe_allows_none_start, maybe_allows_none_end;
732static unsigned int maybe_allows_reg_start, maybe_allows_reg_end;
733static unsigned int maybe_allows_mem_start, maybe_allows_mem_end;
f38840db
ZW
734
735/* Convert NAME, which contains angle brackets and/or underscores, to
736 a string that can be used as part of a C identifier. The string
737 comes from the rtl_obstack. */
738static const char *
739mangle (const char *name)
740{
741 for (; *name; name++)
742 switch (*name)
743 {
744 case '_': obstack_grow (rtl_obstack, "__", 2); break;
745 case '<': obstack_grow (rtl_obstack, "_l", 2); break;
746 case '>': obstack_grow (rtl_obstack, "_g", 2); break;
747 default: obstack_1grow (rtl_obstack, *name); break;
748 }
749
750 obstack_1grow (rtl_obstack, '\0');
7cbb2a85 751 return XOBFINISH (rtl_obstack, const char *);
f38840db
ZW
752}
753
754/* Add one constraint, of any sort, to the tables. NAME is its name;
755 REGCLASS is the register class, if any; EXP is the expression to
756 test, if any; IS_MEMORY and IS_ADDRESS indicate memory and address
757 constraints, respectively; LINENO is the line number from the MD reader.
758 Not all combinations of arguments are valid; most importantly, REGCLASS
759 is mutually exclusive with EXP, and IS_MEMORY/IS_ADDRESS are only
760 meaningful for constraints with EXP.
761
762 This function enforces all syntactic and semantic rules about what
763 constraints can be defined. */
764
765static void
766add_constraint (const char *name, const char *regclass,
767 rtx exp, bool is_memory, bool is_address,
768 int lineno)
769{
770 struct constraint_data *c, **iter, **slot;
771 const char *p;
772 bool need_mangled_name = false;
773 bool is_const_int;
774 bool is_const_dbl;
775 size_t namelen;
776
8677664e
RS
777 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
778 name = general_mem;
779
f38840db
ZW
780 if (exp && validate_exp (exp, name, lineno))
781 return;
782
f38840db
ZW
783 for (p = name; *p; p++)
784 if (!ISALNUM (*p))
785 {
786 if (*p == '<' || *p == '>' || *p == '_')
787 need_mangled_name = true;
788 else
789 {
bb933490
RS
790 error_with_line (lineno,
791 "constraint name '%s' must be composed of "
792 "letters, digits, underscores, and "
793 "angle brackets", name);
f38840db
ZW
794 return;
795 }
796 }
797
798 if (strchr (generic_constraint_letters, name[0]))
799 {
800 if (name[1] == '\0')
bb933490
RS
801 error_with_line (lineno, "constraint letter '%s' cannot be "
802 "redefined by the machine description", name);
f38840db 803 else
bb933490
RS
804 error_with_line (lineno, "constraint name '%s' cannot be defined by "
805 "the machine description, as it begins with '%c'",
806 name, name[0]);
f38840db
ZW
807 return;
808 }
809
b8698a0f 810
f38840db
ZW
811 namelen = strlen (name);
812 slot = &constraints_by_letter_table[(unsigned int)name[0]];
813 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
814 {
815 /* This causes slot to end up pointing to the
816 next_this_letter field of the last constraint with a name
817 of equal or greater length than the new constraint; hence
818 the new constraint will be inserted after all previous
819 constraints with names of the same length. */
820 if ((*iter)->namelen >= namelen)
821 slot = iter;
822
823 if (!strcmp ((*iter)->name, name))
824 {
bb933490 825 error_with_line (lineno, "redefinition of constraint '%s'", name);
f38840db 826 message_with_line ((*iter)->lineno, "previous definition is here");
f38840db
ZW
827 return;
828 }
829 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
830 {
bb933490 831 error_with_line (lineno, "defining constraint '%s' here", name);
f38840db
ZW
832 message_with_line ((*iter)->lineno, "renders constraint '%s' "
833 "(defined here) a prefix", (*iter)->name);
f38840db
ZW
834 return;
835 }
836 else if (!strncmp ((*iter)->name, name, namelen))
837 {
bb933490 838 error_with_line (lineno, "constraint '%s' is a prefix", name);
f38840db
ZW
839 message_with_line ((*iter)->lineno, "of constraint '%s' "
840 "(defined here)", (*iter)->name);
f38840db
ZW
841 return;
842 }
843 }
844
845 is_const_int = strchr (const_int_constraints, name[0]) != 0;
846 is_const_dbl = strchr (const_dbl_constraints, name[0]) != 0;
847
848 if (is_const_int || is_const_dbl)
849 {
850 enum rtx_code appropriate_code
851 = is_const_int ? CONST_INT : CONST_DOUBLE;
852
853 /* Consider relaxing this requirement in the future. */
854 if (regclass
855 || GET_CODE (exp) != AND
856 || GET_CODE (XEXP (exp, 0)) != MATCH_CODE
857 || strcmp (XSTR (XEXP (exp, 0), 0),
858 GET_RTX_NAME (appropriate_code)))
859 {
860 if (name[1] == '\0')
bb933490
RS
861 error_with_line (lineno, "constraint letter '%c' is reserved "
862 "for %s constraints",
863 name[0], GET_RTX_NAME (appropriate_code));
f38840db 864 else
bb933490
RS
865 error_with_line (lineno, "constraint names beginning with '%c' "
866 "(%s) are reserved for %s constraints",
867 name[0], name, GET_RTX_NAME (appropriate_code));
f38840db
ZW
868 return;
869 }
870
871 if (is_memory)
872 {
873 if (name[1] == '\0')
bb933490
RS
874 error_with_line (lineno, "constraint letter '%c' cannot be a "
875 "memory constraint", name[0]);
f38840db 876 else
bb933490
RS
877 error_with_line (lineno, "constraint name '%s' begins with '%c', "
878 "and therefore cannot be a memory constraint",
879 name, name[0]);
f38840db
ZW
880 return;
881 }
882 else if (is_address)
883 {
884 if (name[1] == '\0')
bb933490
RS
885 error_with_line (lineno, "constraint letter '%c' cannot be a "
886 "memory constraint", name[0]);
f38840db 887 else
bb933490
RS
888 error_with_line (lineno, "constraint name '%s' begins with '%c', "
889 "and therefore cannot be a memory constraint",
890 name, name[0]);
f38840db
ZW
891 return;
892 }
f38840db
ZW
893 }
894
b8698a0f 895
7cbb2a85 896 c = XOBNEW (rtl_obstack, struct constraint_data);
f38840db
ZW
897 c->name = name;
898 c->c_name = need_mangled_name ? mangle (name) : name;
899 c->lineno = lineno;
900 c->namelen = namelen;
901 c->regclass = regclass;
902 c->exp = exp;
903 c->is_register = regclass != 0;
904 c->is_const_int = is_const_int;
905 c->is_const_dbl = is_const_dbl;
906 c->is_extra = !(regclass || is_const_int || is_const_dbl);
907 c->is_memory = is_memory;
908 c->is_address = is_address;
851ee5f4
RS
909 c->maybe_allows_reg = true;
910 c->maybe_allows_mem = true;
98c1627c 911 if (exp)
851ee5f4
RS
912 {
913 char codes[NUM_RTX_CODE];
914 compute_test_codes (exp, lineno, codes);
915 if (!codes[REG] && !codes[SUBREG])
916 c->maybe_allows_reg = false;
917 if (!codes[MEM])
918 c->maybe_allows_mem = false;
919 }
f38840db
ZW
920 c->next_this_letter = *slot;
921 *slot = c;
922
923 /* Insert this constraint in the list of all constraints in textual
924 order. */
925 c->next_textual = 0;
926 *last_constraint_ptr = c;
927 last_constraint_ptr = &c->next_textual;
928
929 constraint_max_namelen = MAX (constraint_max_namelen, strlen (name));
930 have_register_constraints |= c->is_register;
931 have_const_int_constraints |= c->is_const_int;
f38840db
ZW
932 have_extra_constraints |= c->is_extra;
933 have_memory_constraints |= c->is_memory;
934 have_address_constraints |= c->is_address;
2aeedf58 935 num_constraints += 1;
f38840db
ZW
936}
937
938/* Process a DEFINE_CONSTRAINT, DEFINE_MEMORY_CONSTRAINT, or
939 DEFINE_ADDRESS_CONSTRAINT expression, C. */
940static void
941process_define_constraint (rtx c, int lineno)
942{
943 add_constraint (XSTR (c, 0), 0, XEXP (c, 2),
944 GET_CODE (c) == DEFINE_MEMORY_CONSTRAINT,
945 GET_CODE (c) == DEFINE_ADDRESS_CONSTRAINT,
946 lineno);
947}
948
949/* Process a DEFINE_REGISTER_CONSTRAINT expression, C. */
950static void
951process_define_register_constraint (rtx c, int lineno)
952{
953 add_constraint (XSTR (c, 0), XSTR (c, 1), 0, false, false, lineno);
954}
955
2aeedf58
RS
956/* Put the constraints into enum order. We want to keep constraints
957 of the same type together so that query functions can be simple
958 range checks. */
959static void
960choose_enum_order (void)
961{
962 struct constraint_data *c;
963
964 enum_order = XNEWVEC (const constraint_data *, num_constraints);
965 unsigned int next = 0;
966
967 register_start = next;
968 FOR_ALL_CONSTRAINTS (c)
969 if (c->is_register)
970 enum_order[next++] = c;
971 register_end = next;
972
973 satisfied_start = next;
974
d9c35eee
RS
975 const_int_start = next;
976 FOR_ALL_CONSTRAINTS (c)
977 if (c->is_const_int)
978 enum_order[next++] = c;
979 const_int_end = next;
980
2aeedf58
RS
981 memory_start = next;
982 FOR_ALL_CONSTRAINTS (c)
983 if (c->is_memory)
984 enum_order[next++] = c;
985 memory_end = next;
986
987 address_start = next;
988 FOR_ALL_CONSTRAINTS (c)
989 if (c->is_address)
990 enum_order[next++] = c;
991 address_end = next;
992
98c1627c
JJ
993 maybe_allows_none_start = next;
994 FOR_ALL_CONSTRAINTS (c)
995 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
996 && !c->maybe_allows_reg && !c->maybe_allows_mem)
997 enum_order[next++] = c;
998 maybe_allows_none_end = next;
999
1000 maybe_allows_reg_start = next;
1001 FOR_ALL_CONSTRAINTS (c)
1002 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1003 && c->maybe_allows_reg && !c->maybe_allows_mem)
1004 enum_order[next++] = c;
1005 maybe_allows_reg_end = next;
1006
1007 maybe_allows_mem_start = next;
1008 FOR_ALL_CONSTRAINTS (c)
1009 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1010 && !c->maybe_allows_reg && c->maybe_allows_mem)
1011 enum_order[next++] = c;
1012 maybe_allows_mem_end = next;
1013
2aeedf58 1014 FOR_ALL_CONSTRAINTS (c)
98c1627c
JJ
1015 if (!c->is_register && !c->is_const_int && !c->is_memory && !c->is_address
1016 && c->maybe_allows_reg && c->maybe_allows_mem)
2aeedf58
RS
1017 enum_order[next++] = c;
1018 gcc_assert (next == num_constraints);
1019}
1020
f38840db
ZW
1021/* Write out an enumeration with one entry per machine-specific
1022 constraint. */
1023static void
1024write_enum_constraint_num (void)
1025{
ff3cb468 1026 fputs ("#define CONSTRAINT_NUM_DEFINED_P 1\n", stdout);
f38840db
ZW
1027 fputs ("enum constraint_num\n"
1028 "{\n"
1029 " CONSTRAINT__UNKNOWN = 0", stdout);
2aeedf58
RS
1030 for (unsigned int i = 0; i < num_constraints; ++i)
1031 printf (",\n CONSTRAINT_%s", enum_order[i]->c_name);
7db7ed3c 1032 puts (",\n CONSTRAINT__LIMIT\n};\n");
f38840db
ZW
1033}
1034
1035/* Write out a function which looks at a string and determines what
1036 constraint name, if any, it begins with. */
1037static void
16a26e42 1038write_lookup_constraint_1 (void)
f38840db
ZW
1039{
1040 unsigned int i;
1041 puts ("enum constraint_num\n"
16a26e42 1042 "lookup_constraint_1 (const char *str)\n"
f38840db
ZW
1043 "{\n"
1044 " switch (str[0])\n"
1045 " {");
1046
c3284718 1047 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
f38840db
ZW
1048 {
1049 struct constraint_data *c = constraints_by_letter_table[i];
1050 if (!c)
1051 continue;
1052
1053 printf (" case '%c':\n", i);
1054 if (c->namelen == 1)
1055 printf (" return CONSTRAINT_%s;\n", c->c_name);
1056 else
1057 {
1058 do
1059 {
71a86758 1060 printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
f38840db 1061 " return CONSTRAINT_%s;\n",
71a86758
RB
1062 c->name + 1, (unsigned long int) c->namelen - 1,
1063 c->c_name);
f38840db
ZW
1064 c = c->next_this_letter;
1065 }
1066 while (c);
1067 puts (" break;");
1068 }
1069 }
1070
1071 puts (" default: break;\n"
1072 " }\n"
1073 " return CONSTRAINT__UNKNOWN;\n"
1074 "}\n");
1075}
1076
16a26e42
RS
1077/* Write out an array that maps single-letter characters to their
1078 constraints (if that fits in a character) or 255 if lookup_constraint_1
1079 must be called. */
1080static void
1081write_lookup_constraint_array (void)
1082{
1083 unsigned int i;
1084 printf ("const unsigned char lookup_constraint_array[] = {\n ");
1085 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
1086 {
1087 if (i != 0)
1088 printf (",\n ");
1089 struct constraint_data *c = constraints_by_letter_table[i];
1090 if (!c)
1091 printf ("CONSTRAINT__UNKNOWN");
1092 else if (c->namelen == 1)
1093 printf ("MIN ((int) CONSTRAINT_%s, (int) UCHAR_MAX)", c->c_name);
1094 else
1095 printf ("UCHAR_MAX");
1096 }
1097 printf ("\n};\n\n");
1098}
1099
5be527d0
RG
1100/* Write out a function which looks at a string and determines what
1101 the constraint name length is. */
f38840db
ZW
1102static void
1103write_insn_constraint_len (void)
1104{
5be527d0 1105 unsigned int i;
f38840db 1106
5be527d0
RG
1107 puts ("static inline size_t\n"
1108 "insn_constraint_len (char fc, const char *str ATTRIBUTE_UNUSED)\n"
f38840db 1109 "{\n"
5be527d0 1110 " switch (fc)\n"
f38840db
ZW
1111 " {");
1112
c3284718 1113 for (i = 0; i < ARRAY_SIZE (constraints_by_letter_table); i++)
5be527d0
RG
1114 {
1115 struct constraint_data *c = constraints_by_letter_table[i];
1116
1117 if (!c
1118 || c->namelen == 1)
1119 continue;
1120
1121 /* Constraints with multiple characters should have the same
1122 length. */
1123 {
1124 struct constraint_data *c2 = c->next_this_letter;
1125 size_t len = c->namelen;
1126 while (c2)
1127 {
1128 if (c2->namelen != len)
1129 error ("Multi-letter constraints with first letter '%c' "
1130 "should have same length", i);
1131 c2 = c2->next_this_letter;
1132 }
1133 }
1134
1135 printf (" case '%c': return %lu;\n",
1136 i, (unsigned long int) c->namelen);
1137 }
f38840db
ZW
1138
1139 puts (" default: break;\n"
1140 " }\n"
1141 " return 1;\n"
1142 "}\n");
1143}
b8698a0f 1144
f38840db
ZW
1145/* Write out the function which computes the register class corresponding
1146 to a register constraint. */
1147static void
2aeedf58 1148write_reg_class_for_constraint_1 (void)
f38840db
ZW
1149{
1150 struct constraint_data *c;
1151
1152 puts ("enum reg_class\n"
2aeedf58 1153 "reg_class_for_constraint_1 (enum constraint_num c)\n"
f38840db
ZW
1154 "{\n"
1155 " switch (c)\n"
1156 " {");
1157
1158 FOR_ALL_CONSTRAINTS (c)
1159 if (c->is_register)
1160 printf (" case CONSTRAINT_%s: return %s;\n", c->c_name, c->regclass);
1161
1162 puts (" default: break;\n"
1163 " }\n"
1164 " return NO_REGS;\n"
1165 "}\n");
1166}
1167
1168/* Write out the functions which compute whether a given value matches
1169 a given non-register constraint. */
1170static void
279bb624 1171write_tm_constrs_h (void)
f38840db
ZW
1172{
1173 struct constraint_data *c;
279bb624
DE
1174
1175 printf ("\
1176/* Generated automatically by the program '%s'\n\
1177 from the machine description file '%s'. */\n\n", progname, in_fname);
1178
1179 puts ("\
1180#ifndef GCC_TM_CONSTRS_H\n\
1181#define GCC_TM_CONSTRS_H\n");
f38840db 1182
f38840db
ZW
1183 FOR_ALL_CONSTRAINTS (c)
1184 if (!c->is_register)
1185 {
1186 bool needs_ival = needs_variable (c->exp, "ival");
1187 bool needs_hval = needs_variable (c->exp, "hval");
1188 bool needs_lval = needs_variable (c->exp, "lval");
1189 bool needs_rval = needs_variable (c->exp, "rval");
1190 bool needs_mode = (needs_variable (c->exp, "mode")
1191 || needs_hval || needs_lval || needs_rval);
9e826585
ZW
1192 bool needs_op = (needs_variable (c->exp, "op")
1193 || needs_ival || needs_mode);
f38840db
ZW
1194
1195 printf ("static inline bool\n"
9e826585
ZW
1196 "satisfies_constraint_%s (rtx %s)\n"
1197 "{\n", c->c_name,
1198 needs_op ? "op" : "ARG_UNUSED (op)");
f38840db 1199 if (needs_mode)
ef4bddc2 1200 puts (" machine_mode mode = GET_MODE (op);");
f38840db
ZW
1201 if (needs_ival)
1202 puts (" HOST_WIDE_INT ival = 0;");
1203 if (needs_hval)
1204 puts (" HOST_WIDE_INT hval = 0;");
1205 if (needs_lval)
1206 puts (" unsigned HOST_WIDE_INT lval = 0;");
1207 if (needs_rval)
1208 puts (" const REAL_VALUE_TYPE *rval = 0;");
1209
1210 if (needs_ival)
481683e1 1211 puts (" if (CONST_INT_P (op))\n"
f38840db 1212 " ival = INTVAL (op);");
807e902e
KZ
1213#if TARGET_SUPPORTS_WIDE_INT
1214 if (needs_lval || needs_hval)
1215 error ("you can't use lval or hval");
1216#else
f38840db
ZW
1217 if (needs_hval)
1218 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1219 " hval = CONST_DOUBLE_HIGH (op);");
1220 if (needs_lval)
1221 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode == VOIDmode)"
1222 " lval = CONST_DOUBLE_LOW (op);");
807e902e 1223#endif
f38840db
ZW
1224 if (needs_rval)
1225 puts (" if (GET_CODE (op) == CONST_DOUBLE && mode != VOIDmode)"
1226 " rval = CONST_DOUBLE_REAL_VALUE (op);");
7caf6734
RS
1227
1228 write_predicate_stmts (c->exp);
1229 fputs ("}\n", stdout);
f38840db 1230 }
279bb624 1231 puts ("#endif /* tm-constrs.h */");
f38840db
ZW
1232}
1233
1234/* Write out the wrapper function, constraint_satisfied_p, that maps
1235 a CONSTRAINT_xxx constant to one of the predicate functions generated
1236 above. */
1237static void
9e6b7874 1238write_constraint_satisfied_p_array (void)
f38840db 1239{
2aeedf58
RS
1240 if (satisfied_start == num_constraints)
1241 return;
1242
9e6b7874
RS
1243 printf ("bool (*constraint_satisfied_p_array[]) (rtx) = {\n ");
1244 for (unsigned int i = satisfied_start; i < num_constraints; ++i)
1245 {
1246 if (i != satisfied_start)
1247 printf (",\n ");
1248 printf ("satisfies_constraint_%s", enum_order[i]->c_name);
1249 }
1250 printf ("\n};\n\n");
f38840db
ZW
1251}
1252
1253/* Write out the function which computes whether a given value matches
1254 a given CONST_INT constraint. This doesn't just forward to
1255 constraint_satisfied_p because caller passes the INTVAL, not the RTX. */
1256static void
1257write_insn_const_int_ok_for_constraint (void)
1258{
1259 struct constraint_data *c;
1260
1261 puts ("bool\n"
1262 "insn_const_int_ok_for_constraint (HOST_WIDE_INT ival, "
1263 "enum constraint_num c)\n"
1264 "{\n"
1265 " switch (c)\n"
1266 " {");
1267
1268 FOR_ALL_CONSTRAINTS (c)
1269 if (c->is_const_int)
1270 {
1271 printf (" case CONSTRAINT_%s:\n return ", c->c_name);
9e826585
ZW
1272 /* c->exp is guaranteed to be (and (match_code "const_int") (...));
1273 we know at this point that we have a const_int, so we need not
1274 bother with that part of the test. */
1275 write_predicate_expr (XEXP (c->exp, 1));
f38840db
ZW
1276 fputs (";\n\n", stdout);
1277 }
1278
1279 puts (" default: break;\n"
1280 " }\n"
1281 " return false;\n"
1282 "}\n");
1283}
2aeedf58
RS
1284\f
1285/* Write a definition for a function NAME that returns true if a given
1286 constraint_num is in the range [START, END). */
f38840db 1287static void
2aeedf58 1288write_range_function (const char *name, unsigned int start, unsigned int end)
f38840db 1289{
2aeedf58
RS
1290 printf ("static inline bool\n");
1291 if (start != end)
1292 printf ("%s (enum constraint_num c)\n"
1293 "{\n"
1294 " return c >= CONSTRAINT_%s && c <= CONSTRAINT_%s;\n"
1295 "}\n\n",
1296 name, enum_order[start]->c_name, enum_order[end - 1]->c_name);
1297 else
1298 printf ("%s (enum constraint_num)\n"
1299 "{\n"
1300 " return false;\n"
1301 "}\n\n", name);
f38840db
ZW
1302}
1303
98c1627c
JJ
1304/* Write a definition for insn_extra_constraint_allows_reg_mem function. */
1305static void
1306write_allows_reg_mem_function (void)
1307{
1308 printf ("static inline void\n"
1309 "insn_extra_constraint_allows_reg_mem (enum constraint_num c,\n"
1310 "\t\t\t\t bool *allows_reg, bool *allows_mem)\n"
1311 "{\n");
1312 if (maybe_allows_none_start != maybe_allows_none_end)
1313 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1314 " return;\n",
1315 enum_order[maybe_allows_none_start]->c_name,
1316 enum_order[maybe_allows_none_end - 1]->c_name);
1317 if (maybe_allows_reg_start != maybe_allows_reg_end)
1318 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1319 " {\n"
1320 " *allows_reg = true;\n"
1321 " return;\n"
1322 " }\n",
1323 enum_order[maybe_allows_reg_start]->c_name,
1324 enum_order[maybe_allows_reg_end - 1]->c_name);
1325 if (maybe_allows_mem_start != maybe_allows_mem_end)
1326 printf (" if (c >= CONSTRAINT_%s && c <= CONSTRAINT_%s)\n"
1327 " {\n"
1328 " *allows_mem = true;\n"
1329 " return;\n"
1330 " }\n",
1331 enum_order[maybe_allows_mem_start]->c_name,
1332 enum_order[maybe_allows_mem_end - 1]->c_name);
1333 printf (" (void) c;\n"
1334 " *allows_reg = true;\n"
1335 " *allows_mem = true;\n"
1336 "}\n\n");
1337}
1338
777e635f
RS
1339/* VEC is a list of key/value pairs, with the keys being lower bounds
1340 of a range. Output a decision tree that handles the keys covered by
1341 [VEC[START], VEC[END]), returning FALLBACK for keys lower then VEC[START]'s.
1342 INDENT is the number of spaces to indent the code. */
1343static void
1344print_type_tree (const vec <std::pair <unsigned int, const char *> > &vec,
1345 unsigned int start, unsigned int end, const char *fallback,
1346 unsigned int indent)
1347{
1348 while (start < end)
1349 {
1350 unsigned int mid = (start + end) / 2;
1351 printf ("%*sif (c >= CONSTRAINT_%s)\n",
1352 indent, "", enum_order[vec[mid].first]->c_name);
1353 if (mid + 1 == end)
1354 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 2);
1355 else
1356 {
1357 printf ("%*s{\n", indent + 2, "");
1358 print_type_tree (vec, mid + 1, end, vec[mid].second, indent + 4);
1359 printf ("%*s}\n", indent + 2, "");
1360 }
1361 end = mid;
1362 }
1363 printf ("%*sreturn %s;\n", indent, "", fallback);
1364}
1365
f38840db
ZW
1366/* Write tm-preds.h. Unfortunately, it is impossible to forward-declare
1367 an enumeration in portable C, so we have to condition all these
1368 prototypes on HAVE_MACHINE_MODES. */
1369static void
1370write_tm_preds_h (void)
1371{
1372 struct pred_data *p;
1373
1374 printf ("\
1375/* Generated automatically by the program '%s'\n\
1376 from the machine description file '%s'. */\n\n", progname, in_fname);
1377
1378 puts ("\
1379#ifndef GCC_TM_PREDS_H\n\
1380#define GCC_TM_PREDS_H\n\
1381\n\
1382#ifdef HAVE_MACHINE_MODES");
1383
1384 FOR_ALL_PREDICATES (p)
ef4bddc2 1385 printf ("extern int %s (rtx, machine_mode);\n", p->name);
f38840db
ZW
1386
1387 puts ("#endif /* HAVE_MACHINE_MODES */\n");
1388
1389 if (constraint_max_namelen > 0)
1390 {
1391 write_enum_constraint_num ();
16a26e42
RS
1392 puts ("extern enum constraint_num lookup_constraint_1 (const char *);\n"
1393 "extern const unsigned char lookup_constraint_array[];\n"
1394 "\n"
1395 "/* Return the constraint at the beginning of P, or"
1396 " CONSTRAINT__UNKNOWN if it\n"
1397 " isn't recognized. */\n"
1398 "\n"
1399 "static inline enum constraint_num\n"
1400 "lookup_constraint (const char *p)\n"
1401 "{\n"
1402 " unsigned int index = lookup_constraint_array"
1403 "[(unsigned char) *p];\n"
1404 " return (index == UCHAR_MAX\n"
1405 " ? lookup_constraint_1 (p)\n"
1406 " : (enum constraint_num) index);\n"
1407 "}\n");
2aeedf58
RS
1408 if (satisfied_start == num_constraints)
1409 puts ("/* Return true if X satisfies constraint C. */\n"
1410 "\n"
1411 "static inline bool\n"
1412 "constraint_satisfied_p (rtx, enum constraint_num)\n"
1413 "{\n"
1414 " return false;\n"
1415 "}\n");
1416 else
9e6b7874 1417 printf ("extern bool (*constraint_satisfied_p_array[]) (rtx);\n"
2aeedf58
RS
1418 "\n"
1419 "/* Return true if X satisfies constraint C. */\n"
1420 "\n"
1421 "static inline bool\n"
1422 "constraint_satisfied_p (rtx x, enum constraint_num c)\n"
1423 "{\n"
9e6b7874
RS
1424 " int i = (int) c - (int) CONSTRAINT_%s;\n"
1425 " return i >= 0 && constraint_satisfied_p_array[i] (x);\n"
2aeedf58
RS
1426 "}\n"
1427 "\n",
1428 enum_order[satisfied_start]->name);
1429
1430 write_range_function ("insn_extra_register_constraint",
1431 register_start, register_end);
1432 write_range_function ("insn_extra_memory_constraint",
1433 memory_start, memory_end);
1434 write_range_function ("insn_extra_address_constraint",
1435 address_start, address_end);
98c1627c 1436 write_allows_reg_mem_function ();
f38840db
ZW
1437
1438 if (constraint_max_namelen > 1)
5be527d0
RG
1439 {
1440 write_insn_constraint_len ();
1441 puts ("#define CONSTRAINT_LEN(c_,s_) "
1442 "insn_constraint_len (c_,s_)\n");
1443 }
f38840db
ZW
1444 else
1445 puts ("#define CONSTRAINT_LEN(c_,s_) 1\n");
1446 if (have_register_constraints)
2aeedf58 1447 puts ("extern enum reg_class reg_class_for_constraint_1 "
f38840db 1448 "(enum constraint_num);\n"
2aeedf58
RS
1449 "\n"
1450 "static inline enum reg_class\n"
1451 "reg_class_for_constraint (enum constraint_num c)\n"
1452 "{\n"
1453 " if (insn_extra_register_constraint (c))\n"
1454 " return reg_class_for_constraint_1 (c);\n"
1455 " return NO_REGS;\n"
777e635f 1456 "}\n");
f38840db 1457 else
2aeedf58
RS
1458 puts ("static inline enum reg_class\n"
1459 "reg_class_for_constraint (enum constraint_num)\n"
1460 "{\n"
1461 " return NO_REGS;\n"
777e635f 1462 "}\n");
f38840db
ZW
1463 if (have_const_int_constraints)
1464 puts ("extern bool insn_const_int_ok_for_constraint "
1465 "(HOST_WIDE_INT, enum constraint_num);\n"
1466 "#define CONST_OK_FOR_CONSTRAINT_P(v_,c_,s_) \\\n"
1467 " insn_const_int_ok_for_constraint (v_, "
1468 "lookup_constraint (s_))\n");
d9c35eee
RS
1469 else
1470 puts ("static inline bool\n"
1471 "insn_const_int_ok_for_constraint (HOST_WIDE_INT,"
1472 " enum constraint_num)\n"
1473 "{\n"
1474 " return false;\n"
1475 "}\n");
777e635f
RS
1476
1477 puts ("enum constraint_type\n"
1478 "{\n"
1479 " CT_REGISTER,\n"
d9c35eee 1480 " CT_CONST_INT,\n"
777e635f
RS
1481 " CT_MEMORY,\n"
1482 " CT_ADDRESS,\n"
1483 " CT_FIXED_FORM\n"
1484 "};\n"
1485 "\n"
1486 "static inline enum constraint_type\n"
1487 "get_constraint_type (enum constraint_num c)\n"
1488 "{");
d9c35eee
RS
1489 auto_vec <std::pair <unsigned int, const char *>, 4> values;
1490 if (const_int_start != const_int_end)
1491 values.safe_push (std::make_pair (const_int_start, "CT_CONST_INT"));
777e635f
RS
1492 if (memory_start != memory_end)
1493 values.safe_push (std::make_pair (memory_start, "CT_MEMORY"));
1494 if (address_start != address_end)
1495 values.safe_push (std::make_pair (address_start, "CT_ADDRESS"));
1496 if (address_end != num_constraints)
1497 values.safe_push (std::make_pair (address_end, "CT_FIXED_FORM"));
1498 print_type_tree (values, 0, values.length (), "CT_REGISTER", 2);
1499 puts ("}");
f38840db
ZW
1500 }
1501
1502 puts ("#endif /* tm-preds.h */");
1503}
1b0c37d7 1504
b8698a0f 1505/* Write insn-preds.c.
e543e219
ZW
1506 N.B. the list of headers to include was copied from genrecog; it
1507 may not be ideal.
1508
1509 FUTURE: Write #line markers referring back to the machine
1510 description. (Can't practically do this now since we don't know
1511 the line number of the C block - just the line number of the enclosing
1512 expression.) */
1513static void
1514write_insn_preds_c (void)
1515{
1516 struct pred_data *p;
1517
1518 printf ("\
1519/* Generated automatically by the program '%s'\n\
1520 from the machine description file '%s'. */\n\n", progname, in_fname);
1521
1522 puts ("\
1523#include \"config.h\"\n\
1524#include \"system.h\"\n\
1525#include \"coretypes.h\"\n\
1526#include \"tm.h\"\n\
1527#include \"rtl.h\"\n\
40e23961
MC
1528#include \"hash-set.h\"\n\
1529#include \"machmode.h\"\n\
2d44c7de 1530#include \"hash-map.h\"\n\
40e23961
MC
1531#include \"vec.h\"\n\
1532#include \"double-int.h\"\n\
1533#include \"input.h\"\n\
1534#include \"alias.h\"\n\
1535#include \"symtab.h\"\n\
1536#include \"wide-int.h\"\n\
1537#include \"inchash.h\"\n\
201312c2 1538#include \"tree.h\"\n\
d8a2d370
DN
1539#include \"varasm.h\"\n\
1540#include \"stor-layout.h\"\n\
1541#include \"calls.h\"\n\
e543e219 1542#include \"tm_p.h\"\n\
83685514
AM
1543#include \"hashtab.h\"\n\
1544#include \"hash-set.h\"\n\
1545#include \"vec.h\"\n\
1546#include \"machmode.h\"\n\
1547#include \"hard-reg-set.h\"\n\
1548#include \"input.h\"\n\
e543e219
ZW
1549#include \"function.h\"\n\
1550#include \"insn-config.h\"\n\
1551#include \"recog.h\"\n\
e543e219
ZW
1552#include \"output.h\"\n\
1553#include \"flags.h\"\n\
1554#include \"hard-reg-set.h\"\n\
60393bbc
AM
1555#include \"predict.h\"\n\
1556#include \"basic-block.h\"\n\
e543e219 1557#include \"resource.h\"\n\
79a3f089 1558#include \"diagnostic-core.h\"\n\
9a9286af 1559#include \"reload.h\"\n\
279bb624 1560#include \"regs.h\"\n\
2bb8cb58 1561#include \"emit-rtl.h\"\n\
279bb624 1562#include \"tm-constrs.h\"\n");
e543e219
ZW
1563
1564 FOR_ALL_PREDICATES (p)
1565 write_one_predicate_function (p);
f38840db
ZW
1566
1567 if (constraint_max_namelen > 0)
1568 {
16a26e42
RS
1569 write_lookup_constraint_1 ();
1570 write_lookup_constraint_array ();
495fb8cd 1571 if (have_register_constraints)
2aeedf58 1572 write_reg_class_for_constraint_1 ();
9e6b7874 1573 write_constraint_satisfied_p_array ();
b8698a0f 1574
f38840db
ZW
1575 if (have_const_int_constraints)
1576 write_insn_const_int_ok_for_constraint ();
f38840db 1577 }
e543e219
ZW
1578}
1579
1580/* Argument parsing. */
1581static bool gen_header;
279bb624
DE
1582static bool gen_constrs;
1583
e543e219
ZW
1584static bool
1585parse_option (const char *opt)
1586{
1587 if (!strcmp (opt, "-h"))
1588 {
1589 gen_header = true;
1590 return 1;
1591 }
279bb624
DE
1592 else if (!strcmp (opt, "-c"))
1593 {
1594 gen_constrs = true;
1595 return 1;
1596 }
e543e219
ZW
1597 else
1598 return 0;
1599}
1600
1601/* Master control. */
1b0c37d7 1602int
e543e219 1603main (int argc, char **argv)
1b0c37d7 1604{
e543e219
ZW
1605 rtx defn;
1606 int pattern_lineno, next_insn_code = 0;
1b0c37d7 1607
e543e219
ZW
1608 progname = argv[0];
1609 if (argc <= 1)
1610 fatal ("no input file name");
600ab3fc 1611 if (!init_rtx_reader_args_cb (argc, argv, parse_option))
e543e219
ZW
1612 return FATAL_EXIT_CODE;
1613
1614 while ((defn = read_md_rtx (&pattern_lineno, &next_insn_code)) != 0)
f38840db
ZW
1615 switch (GET_CODE (defn))
1616 {
1617 case DEFINE_PREDICATE:
1618 case DEFINE_SPECIAL_PREDICATE:
1619 process_define_predicate (defn, pattern_lineno);
1620 break;
1621
1622 case DEFINE_CONSTRAINT:
1623 case DEFINE_MEMORY_CONSTRAINT:
1624 case DEFINE_ADDRESS_CONSTRAINT:
1625 process_define_constraint (defn, pattern_lineno);
1626 break;
1627
1628 case DEFINE_REGISTER_CONSTRAINT:
1629 process_define_register_constraint (defn, pattern_lineno);
1630 break;
1631
1632 default:
1633 break;
1634 }
1b0c37d7 1635
2aeedf58
RS
1636 choose_enum_order ();
1637
e543e219
ZW
1638 if (gen_header)
1639 write_tm_preds_h ();
279bb624
DE
1640 else if (gen_constrs)
1641 write_tm_constrs_h ();
e543e219
ZW
1642 else
1643 write_insn_preds_c ();
1b0c37d7 1644
e543e219 1645 if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
1b0c37d7
ZW
1646 return FATAL_EXIT_CODE;
1647
1648 return SUCCESS_EXIT_CODE;
1649}
This page took 4.394014 seconds and 5 git commands to generate.