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