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