compiler: Create dummy labels for blank labels.
[official-gcc.git] / gcc / gensupport.c
blobb7681a234c159e61249ada9c50f546a1d63fb3b8
1 /* Support routines for the various generation passes.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "obstack.h"
26 #include "errors.h"
27 #include "read-md.h"
28 #include "gensupport.h"
29 #include "vec.h"
31 #define MAX_OPERANDS 40
33 static rtx operand_data[MAX_OPERANDS];
34 static rtx match_operand_entries_in_pattern[MAX_OPERANDS];
35 static char used_operands_numbers[MAX_OPERANDS];
38 /* In case some macros used by files we include need it, define this here. */
39 int target_flags;
41 int insn_elision = 1;
43 static struct obstack obstack;
44 struct obstack *rtl_obstack = &obstack;
46 /* Counter for patterns that generate code: define_insn, define_expand,
47 define_split, define_peephole, and define_peephole2. See read_md_rtx().
48 Any define_insn_and_splits are already in separate queues so that the
49 insn and the splitter get a unique number also. */
50 static int sequence_num;
52 static int predicable_default;
53 static const char *predicable_true;
54 static const char *predicable_false;
56 static const char *subst_true = "yes";
57 static const char *subst_false = "no";
59 static htab_t condition_table;
61 /* We initially queue all patterns, process the define_insn,
62 define_cond_exec and define_subst patterns, then return
63 them one at a time. */
65 struct queue_elem
67 rtx data;
68 file_location loc;
69 struct queue_elem *next;
70 /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT, SPLIT
71 points to the generated DEFINE_SPLIT. */
72 struct queue_elem *split;
75 #define MNEMONIC_ATTR_NAME "mnemonic"
76 #define MNEMONIC_HTAB_SIZE 1024
78 static struct queue_elem *define_attr_queue;
79 static struct queue_elem **define_attr_tail = &define_attr_queue;
80 static struct queue_elem *define_pred_queue;
81 static struct queue_elem **define_pred_tail = &define_pred_queue;
82 static struct queue_elem *define_insn_queue;
83 static struct queue_elem **define_insn_tail = &define_insn_queue;
84 static struct queue_elem *define_cond_exec_queue;
85 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
86 static struct queue_elem *define_subst_queue;
87 static struct queue_elem **define_subst_tail = &define_subst_queue;
88 static struct queue_elem *other_queue;
89 static struct queue_elem **other_tail = &other_queue;
90 static struct queue_elem *define_subst_attr_queue;
91 static struct queue_elem **define_subst_attr_tail = &define_subst_attr_queue;
93 static void remove_constraints (rtx);
95 static int is_predicable (struct queue_elem *);
96 static void identify_predicable_attribute (void);
97 static int n_alternatives (const char *);
98 static void collect_insn_data (rtx, int *, int *);
99 static const char *alter_test_for_insn (struct queue_elem *,
100 struct queue_elem *);
101 static char *shift_output_template (char *, const char *, int);
102 static const char *alter_output_for_insn (struct queue_elem *,
103 struct queue_elem *,
104 int, int);
105 static void process_one_cond_exec (struct queue_elem *);
106 static void process_define_cond_exec (void);
107 static void init_predicate_table (void);
108 static void record_insn_name (int, const char *);
110 static bool has_subst_attribute (struct queue_elem *, struct queue_elem *);
111 static const char * alter_output_for_subst_insn (rtx, int);
112 static void alter_attrs_for_subst_insn (struct queue_elem *, int);
113 static void process_substs_on_one_elem (struct queue_elem *,
114 struct queue_elem *);
115 static rtx subst_dup (rtx, int, int);
116 static void process_define_subst (void);
118 static const char * duplicate_alternatives (const char *, int);
119 static const char * duplicate_each_alternative (const char * str, int n_dup);
121 typedef const char * (*constraints_handler_t) (const char *, int);
122 static rtx alter_constraints (rtx, int, constraints_handler_t);
123 static rtx adjust_operands_numbers (rtx);
124 static rtx replace_duplicating_operands_in_pattern (rtx);
126 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
127 the gensupport programs. */
130 gen_rtx_CONST_INT (machine_mode ARG_UNUSED (mode),
131 HOST_WIDE_INT arg)
133 rtx rt = rtx_alloc (CONST_INT);
135 XWINT (rt, 0) = arg;
136 return rt;
139 /* Return the rtx pattern specified by the list of rtxes in a
140 define_insn or define_split. */
143 add_implicit_parallel (rtvec vec)
145 if (GET_NUM_ELEM (vec) == 1)
146 return RTVEC_ELT (vec, 0);
147 else
149 rtx pattern = rtx_alloc (PARALLEL);
150 XVEC (pattern, 0) = vec;
151 return pattern;
155 /* Predicate handling.
157 We construct from the machine description a table mapping each
158 predicate to a list of the rtl codes it can possibly match. The
159 function 'maybe_both_true' uses it to deduce that there are no
160 expressions that can be matches by certain pairs of tree nodes.
161 Also, if a predicate can match only one code, we can hardwire that
162 code into the node testing the predicate.
164 Some predicates are flagged as special. validate_pattern will not
165 warn about modeless match_operand expressions if they have a
166 special predicate. Predicates that allow only constants are also
167 treated as special, for this purpose.
169 validate_pattern will warn about predicates that allow non-lvalues
170 when they appear in destination operands.
172 Calculating the set of rtx codes that can possibly be accepted by a
173 predicate expression EXP requires a three-state logic: any given
174 subexpression may definitively accept a code C (Y), definitively
175 reject a code C (N), or may have an indeterminate effect (I). N
176 and I is N; Y or I is Y; Y and I, N or I are both I. Here are full
177 truth tables.
179 a b a&b a|b
180 Y Y Y Y
181 N Y N Y
182 N N N N
183 I Y I Y
184 I N N I
185 I I I I
187 We represent Y with 1, N with 0, I with 2. If any code is left in
188 an I state by the complete expression, we must assume that that
189 code can be accepted. */
191 #define N 0
192 #define Y 1
193 #define I 2
195 #define TRISTATE_AND(a,b) \
196 ((a) == I ? ((b) == N ? N : I) : \
197 (b) == I ? ((a) == N ? N : I) : \
198 (a) && (b))
200 #define TRISTATE_OR(a,b) \
201 ((a) == I ? ((b) == Y ? Y : I) : \
202 (b) == I ? ((a) == Y ? Y : I) : \
203 (a) || (b))
205 #define TRISTATE_NOT(a) \
206 ((a) == I ? I : !(a))
208 /* 0 means no warning about that code yet, 1 means warned. */
209 static char did_you_mean_codes[NUM_RTX_CODE];
211 /* Recursively calculate the set of rtx codes accepted by the
212 predicate expression EXP, writing the result to CODES. LOC is
213 the .md file location of the directive containing EXP. */
215 void
216 compute_test_codes (rtx exp, file_location loc, char *codes)
218 char op0_codes[NUM_RTX_CODE];
219 char op1_codes[NUM_RTX_CODE];
220 char op2_codes[NUM_RTX_CODE];
221 int i;
223 switch (GET_CODE (exp))
225 case AND:
226 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
227 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
228 for (i = 0; i < NUM_RTX_CODE; i++)
229 codes[i] = TRISTATE_AND (op0_codes[i], op1_codes[i]);
230 break;
232 case IOR:
233 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
234 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
235 for (i = 0; i < NUM_RTX_CODE; i++)
236 codes[i] = TRISTATE_OR (op0_codes[i], op1_codes[i]);
237 break;
238 case NOT:
239 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
240 for (i = 0; i < NUM_RTX_CODE; i++)
241 codes[i] = TRISTATE_NOT (op0_codes[i]);
242 break;
244 case IF_THEN_ELSE:
245 /* a ? b : c accepts the same codes as (a & b) | (!a & c). */
246 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
247 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
248 compute_test_codes (XEXP (exp, 2), loc, op2_codes);
249 for (i = 0; i < NUM_RTX_CODE; i++)
250 codes[i] = TRISTATE_OR (TRISTATE_AND (op0_codes[i], op1_codes[i]),
251 TRISTATE_AND (TRISTATE_NOT (op0_codes[i]),
252 op2_codes[i]));
253 break;
255 case MATCH_CODE:
256 /* MATCH_CODE allows a specified list of codes. However, if it
257 does not apply to the top level of the expression, it does not
258 constrain the set of codes for the top level. */
259 if (XSTR (exp, 1)[0] != '\0')
261 memset (codes, Y, NUM_RTX_CODE);
262 break;
265 memset (codes, N, NUM_RTX_CODE);
267 const char *next_code = XSTR (exp, 0);
268 const char *code;
270 if (*next_code == '\0')
272 error_at (loc, "empty match_code expression");
273 break;
276 while ((code = scan_comma_elt (&next_code)) != 0)
278 size_t n = next_code - code;
279 int found_it = 0;
281 for (i = 0; i < NUM_RTX_CODE; i++)
282 if (!strncmp (code, GET_RTX_NAME (i), n)
283 && GET_RTX_NAME (i)[n] == '\0')
285 codes[i] = Y;
286 found_it = 1;
287 break;
289 if (!found_it)
291 error_at (loc, "match_code \"%.*s\" matches nothing",
292 (int) n, code);
293 for (i = 0; i < NUM_RTX_CODE; i++)
294 if (!strncasecmp (code, GET_RTX_NAME (i), n)
295 && GET_RTX_NAME (i)[n] == '\0'
296 && !did_you_mean_codes[i])
298 did_you_mean_codes[i] = 1;
299 message_at (loc, "(did you mean \"%s\"?)",
300 GET_RTX_NAME (i));
305 break;
307 case MATCH_OPERAND:
308 /* MATCH_OPERAND disallows the set of codes that the named predicate
309 disallows, and is indeterminate for the codes that it does allow. */
311 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
312 if (!p)
314 error_at (loc, "reference to unknown predicate '%s'",
315 XSTR (exp, 1));
316 break;
318 for (i = 0; i < NUM_RTX_CODE; i++)
319 codes[i] = p->codes[i] ? I : N;
321 break;
324 case MATCH_TEST:
325 /* (match_test WHATEVER) is completely indeterminate. */
326 memset (codes, I, NUM_RTX_CODE);
327 break;
329 default:
330 error_at (loc, "'%s' cannot be used in predicates or constraints",
331 GET_RTX_NAME (GET_CODE (exp)));
332 memset (codes, I, NUM_RTX_CODE);
333 break;
337 #undef TRISTATE_OR
338 #undef TRISTATE_AND
339 #undef TRISTATE_NOT
341 /* Return true if NAME is a valid predicate name. */
343 static bool
344 valid_predicate_name_p (const char *name)
346 const char *p;
348 if (!ISALPHA (name[0]) && name[0] != '_')
349 return false;
350 for (p = name + 1; *p; p++)
351 if (!ISALNUM (*p) && *p != '_')
352 return false;
353 return true;
356 /* Process define_predicate directive DESC, which appears at location LOC.
357 Compute the set of codes that can be matched, and record this as a known
358 predicate. */
360 static void
361 process_define_predicate (rtx desc, file_location loc)
363 struct pred_data *pred;
364 char codes[NUM_RTX_CODE];
365 int i;
367 if (!valid_predicate_name_p (XSTR (desc, 0)))
369 error_at (loc, "%s: predicate name must be a valid C function name",
370 XSTR (desc, 0));
371 return;
374 pred = XCNEW (struct pred_data);
375 pred->name = XSTR (desc, 0);
376 pred->exp = XEXP (desc, 1);
377 pred->c_block = XSTR (desc, 2);
378 if (GET_CODE (desc) == DEFINE_SPECIAL_PREDICATE)
379 pred->special = true;
381 compute_test_codes (XEXP (desc, 1), loc, codes);
383 for (i = 0; i < NUM_RTX_CODE; i++)
384 if (codes[i] != N)
385 add_predicate_code (pred, (enum rtx_code) i);
387 add_predicate (pred);
389 #undef I
390 #undef N
391 #undef Y
393 /* Queue PATTERN on LIST_TAIL. Return the address of the new queue
394 element. */
396 static struct queue_elem *
397 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
398 file_location loc)
400 struct queue_elem *e = XNEW (struct queue_elem);
401 e->data = pattern;
402 e->loc = loc;
403 e->next = NULL;
404 e->split = NULL;
405 **list_tail = e;
406 *list_tail = &e->next;
407 return e;
410 /* Remove element ELEM from QUEUE. */
411 static void
412 remove_from_queue (struct queue_elem *elem, struct queue_elem **queue)
414 struct queue_elem *prev, *e;
415 prev = NULL;
416 for (e = *queue; e ; e = e->next)
418 if (e == elem)
419 break;
420 prev = e;
422 if (e == NULL)
423 return;
425 if (prev)
426 prev->next = elem->next;
427 else
428 *queue = elem->next;
431 /* Build a define_attr for an binary attribute with name NAME and
432 possible values "yes" and "no", and queue it. */
433 static void
434 add_define_attr (const char *name)
436 struct queue_elem *e = XNEW (struct queue_elem);
437 rtx t1 = rtx_alloc (DEFINE_ATTR);
438 XSTR (t1, 0) = name;
439 XSTR (t1, 1) = "no,yes";
440 XEXP (t1, 2) = rtx_alloc (CONST_STRING);
441 XSTR (XEXP (t1, 2), 0) = "yes";
442 e->data = t1;
443 e->loc = file_location ("built-in", -1);
444 e->next = define_attr_queue;
445 define_attr_queue = e;
449 /* Recursively remove constraints from an rtx. */
451 static void
452 remove_constraints (rtx part)
454 int i, j;
455 const char *format_ptr;
457 if (part == 0)
458 return;
460 if (GET_CODE (part) == MATCH_OPERAND)
461 XSTR (part, 2) = "";
462 else if (GET_CODE (part) == MATCH_SCRATCH)
463 XSTR (part, 1) = "";
465 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
467 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
468 switch (*format_ptr++)
470 case 'e':
471 case 'u':
472 remove_constraints (XEXP (part, i));
473 break;
474 case 'E':
475 if (XVEC (part, i) != NULL)
476 for (j = 0; j < XVECLEN (part, i); j++)
477 remove_constraints (XVECEXP (part, i, j));
478 break;
482 /* Process a top level rtx in some way, queuing as appropriate. */
484 static void
485 process_rtx (rtx desc, file_location loc)
487 switch (GET_CODE (desc))
489 case DEFINE_INSN:
490 queue_pattern (desc, &define_insn_tail, loc);
491 break;
493 case DEFINE_COND_EXEC:
494 queue_pattern (desc, &define_cond_exec_tail, loc);
495 break;
497 case DEFINE_SUBST:
498 queue_pattern (desc, &define_subst_tail, loc);
499 break;
501 case DEFINE_SUBST_ATTR:
502 queue_pattern (desc, &define_subst_attr_tail, loc);
503 break;
505 case DEFINE_ATTR:
506 case DEFINE_ENUM_ATTR:
507 queue_pattern (desc, &define_attr_tail, loc);
508 break;
510 case DEFINE_PREDICATE:
511 case DEFINE_SPECIAL_PREDICATE:
512 process_define_predicate (desc, loc);
513 /* Fall through. */
515 case DEFINE_CONSTRAINT:
516 case DEFINE_REGISTER_CONSTRAINT:
517 case DEFINE_MEMORY_CONSTRAINT:
518 case DEFINE_ADDRESS_CONSTRAINT:
519 queue_pattern (desc, &define_pred_tail, loc);
520 break;
522 case DEFINE_INSN_AND_SPLIT:
524 const char *split_cond;
525 rtx split;
526 rtvec attr;
527 int i;
528 struct queue_elem *insn_elem;
529 struct queue_elem *split_elem;
531 /* Create a split with values from the insn_and_split. */
532 split = rtx_alloc (DEFINE_SPLIT);
534 i = XVECLEN (desc, 1);
535 XVEC (split, 0) = rtvec_alloc (i);
536 while (--i >= 0)
538 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
539 remove_constraints (XVECEXP (split, 0, i));
542 /* If the split condition starts with "&&", append it to the
543 insn condition to create the new split condition. */
544 split_cond = XSTR (desc, 4);
545 if (split_cond[0] == '&' && split_cond[1] == '&')
547 copy_md_ptr_loc (split_cond + 2, split_cond);
548 split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
550 XSTR (split, 1) = split_cond;
551 XVEC (split, 2) = XVEC (desc, 5);
552 XSTR (split, 3) = XSTR (desc, 6);
554 /* Fix up the DEFINE_INSN. */
555 attr = XVEC (desc, 7);
556 PUT_CODE (desc, DEFINE_INSN);
557 XVEC (desc, 4) = attr;
559 /* Queue them. */
560 insn_elem = queue_pattern (desc, &define_insn_tail, loc);
561 split_elem = queue_pattern (split, &other_tail, loc);
562 insn_elem->split = split_elem;
563 break;
566 default:
567 queue_pattern (desc, &other_tail, loc);
568 break;
572 /* Return true if attribute PREDICABLE is true for ELEM, which holds
573 a DEFINE_INSN. */
575 static int
576 is_predicable (struct queue_elem *elem)
578 rtvec vec = XVEC (elem->data, 4);
579 const char *value;
580 int i;
582 if (! vec)
583 return predicable_default;
585 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
587 rtx sub = RTVEC_ELT (vec, i);
588 switch (GET_CODE (sub))
590 case SET_ATTR:
591 if (strcmp (XSTR (sub, 0), "predicable") == 0)
593 value = XSTR (sub, 1);
594 goto found;
596 break;
598 case SET_ATTR_ALTERNATIVE:
599 if (strcmp (XSTR (sub, 0), "predicable") == 0)
601 error_at (elem->loc, "multiple alternatives for `predicable'");
602 return 0;
604 break;
606 case SET:
607 if (GET_CODE (SET_DEST (sub)) != ATTR
608 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
609 break;
610 sub = SET_SRC (sub);
611 if (GET_CODE (sub) == CONST_STRING)
613 value = XSTR (sub, 0);
614 goto found;
617 /* ??? It would be possible to handle this if we really tried.
618 It's not easy though, and I'm not going to bother until it
619 really proves necessary. */
620 error_at (elem->loc, "non-constant value for `predicable'");
621 return 0;
623 default:
624 gcc_unreachable ();
628 return predicable_default;
630 found:
631 /* Find out which value we're looking at. Multiple alternatives means at
632 least one is predicable. */
633 if (strchr (value, ',') != NULL)
634 return 1;
635 if (strcmp (value, predicable_true) == 0)
636 return 1;
637 if (strcmp (value, predicable_false) == 0)
638 return 0;
640 error_at (elem->loc, "unknown value `%s' for `predicable' attribute", value);
641 return 0;
644 /* Find attribute SUBST in ELEM and assign NEW_VALUE to it. */
645 static void
646 change_subst_attribute (struct queue_elem *elem,
647 struct queue_elem *subst_elem,
648 const char *new_value)
650 rtvec attrs_vec = XVEC (elem->data, 4);
651 const char *subst_name = XSTR (subst_elem->data, 0);
652 int i;
654 if (! attrs_vec)
655 return;
657 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
659 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
660 if (GET_CODE (cur_attr) != SET_ATTR)
661 continue;
662 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
664 XSTR (cur_attr, 1) = new_value;
665 return;
670 /* Return true if ELEM has the attribute with the name of DEFINE_SUBST
671 represented by SUBST_ELEM and this attribute has value SUBST_TRUE.
672 DEFINE_SUBST isn't applied to patterns without such attribute. In other
673 words, we suppose the default value of the attribute to be 'no' since it is
674 always generated automaticaly in read-rtl.c. */
675 static bool
676 has_subst_attribute (struct queue_elem *elem, struct queue_elem *subst_elem)
678 rtvec attrs_vec = XVEC (elem->data, 4);
679 const char *value, *subst_name = XSTR (subst_elem->data, 0);
680 int i;
682 if (! attrs_vec)
683 return false;
685 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
687 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
688 switch (GET_CODE (cur_attr))
690 case SET_ATTR:
691 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
693 value = XSTR (cur_attr, 1);
694 goto found;
696 break;
698 case SET:
699 if (GET_CODE (SET_DEST (cur_attr)) != ATTR
700 || strcmp (XSTR (SET_DEST (cur_attr), 0), subst_name) != 0)
701 break;
702 cur_attr = SET_SRC (cur_attr);
703 if (GET_CODE (cur_attr) == CONST_STRING)
705 value = XSTR (cur_attr, 0);
706 goto found;
709 /* Only (set_attr "subst" "yes/no") and
710 (set (attr "subst" (const_string "yes/no")))
711 are currently allowed. */
712 error_at (elem->loc, "unsupported value for `%s'", subst_name);
713 return false;
715 case SET_ATTR_ALTERNATIVE:
716 error_at (elem->loc,
717 "%s: `set_attr_alternative' is unsupported by "
718 "`define_subst'", XSTR (elem->data, 0));
719 return false;
722 default:
723 gcc_unreachable ();
727 return false;
729 found:
730 if (strcmp (value, subst_true) == 0)
731 return true;
732 if (strcmp (value, subst_false) == 0)
733 return false;
735 error_at (elem->loc, "unknown value `%s' for `%s' attribute",
736 value, subst_name);
737 return false;
740 /* Compare RTL-template of original define_insn X to input RTL-template of
741 define_subst PT. Return 1 if the templates match, 0 otherwise.
742 During the comparison, the routine also fills global_array OPERAND_DATA. */
743 static bool
744 subst_pattern_match (rtx x, rtx pt, file_location loc)
746 RTX_CODE code, code_pt;
747 int i, j, len;
748 const char *fmt, *pred_name;
750 code = GET_CODE (x);
751 code_pt = GET_CODE (pt);
753 if (code_pt == MATCH_OPERAND)
755 /* MATCH_DUP, and MATCH_OP_DUP don't have a specified mode, so we
756 always accept them. */
757 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt)
758 && (code != MATCH_DUP && code != MATCH_OP_DUP))
759 return false; /* Modes don't match. */
761 if (code == MATCH_OPERAND)
763 pred_name = XSTR (pt, 1);
764 if (pred_name[0] != 0)
766 const struct pred_data *pred_pt = lookup_predicate (pred_name);
767 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
768 return false; /* Predicates don't match. */
772 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
773 operand_data[XINT (pt, 0)] = x;
774 return true;
777 if (code_pt == MATCH_OPERATOR)
779 int x_vecexp_pos = -1;
781 /* Compare modes. */
782 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt))
783 return false;
785 /* In case X is also match_operator, compare predicates. */
786 if (code == MATCH_OPERATOR)
788 pred_name = XSTR (pt, 1);
789 if (pred_name[0] != 0)
791 const struct pred_data *pred_pt = lookup_predicate (pred_name);
792 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
793 return false;
797 /* Compare operands.
798 MATCH_OPERATOR in input template could match in original template
799 either 1) MATCH_OPERAND, 2) UNSPEC, 3) ordinary operation (like PLUS).
800 In the first case operands are at (XVECEXP (x, 2, j)), in the second
801 - at (XVECEXP (x, 0, j)), in the last one - (XEXP (x, j)).
802 X_VECEXP_POS variable shows, where to look for these operands. */
803 if (code == UNSPEC
804 || code == UNSPEC_VOLATILE)
805 x_vecexp_pos = 0;
806 else if (code == MATCH_OPERATOR)
807 x_vecexp_pos = 2;
808 else
809 x_vecexp_pos = -1;
811 /* MATCH_OPERATOR or UNSPEC case. */
812 if (x_vecexp_pos >= 0)
814 /* Compare operands number in X and PT. */
815 if (XVECLEN (x, x_vecexp_pos) != XVECLEN (pt, 2))
816 return false;
817 for (j = 0; j < XVECLEN (pt, 2); j++)
818 if (!subst_pattern_match (XVECEXP (x, x_vecexp_pos, j),
819 XVECEXP (pt, 2, j), loc))
820 return false;
823 /* Ordinary operator. */
824 else
826 /* Compare operands number in X and PT.
827 We count operands differently for X and PT since we compare
828 an operator (with operands directly in RTX) and MATCH_OPERATOR
829 (that has a vector with operands). */
830 if (GET_RTX_LENGTH (code) != XVECLEN (pt, 2))
831 return false;
832 for (j = 0; j < XVECLEN (pt, 2); j++)
833 if (!subst_pattern_match (XEXP (x, j), XVECEXP (pt, 2, j), loc))
834 return false;
837 /* Store the operand to OPERAND_DATA array. */
838 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
839 operand_data[XINT (pt, 0)] = x;
840 return true;
843 if (code_pt == MATCH_PAR_DUP
844 || code_pt == MATCH_DUP
845 || code_pt == MATCH_OP_DUP
846 || code_pt == MATCH_SCRATCH
847 || code_pt == MATCH_PARALLEL)
849 /* Currently interface for these constructions isn't defined -
850 probably they aren't needed in input template of define_subst at all.
851 So, for now their usage in define_subst is forbidden. */
852 error_at (loc, "%s cannot be used in define_subst",
853 GET_RTX_NAME (code_pt));
856 gcc_assert (code != MATCH_PAR_DUP
857 && code_pt != MATCH_DUP
858 && code_pt != MATCH_OP_DUP
859 && code_pt != MATCH_SCRATCH
860 && code_pt != MATCH_PARALLEL
861 && code_pt != MATCH_OPERAND
862 && code_pt != MATCH_OPERATOR);
863 /* If PT is none of the handled above, then we match only expressions with
864 the same code in X. */
865 if (code != code_pt)
866 return false;
868 fmt = GET_RTX_FORMAT (code_pt);
869 len = GET_RTX_LENGTH (code_pt);
871 for (i = 0; i < len; i++)
873 if (fmt[i] == '0')
874 break;
876 switch (fmt[i])
878 case 'i': case 'r': case 'w': case 's':
879 continue;
881 case 'e': case 'u':
882 if (!subst_pattern_match (XEXP (x, i), XEXP (pt, i), loc))
883 return false;
884 break;
885 case 'E':
887 if (XVECLEN (x, i) != XVECLEN (pt, i))
888 return false;
889 for (j = 0; j < XVECLEN (pt, i); j++)
890 if (!subst_pattern_match (XVECEXP (x, i, j),
891 XVECEXP (pt, i, j), loc))
892 return false;
893 break;
895 default:
896 gcc_unreachable ();
900 return true;
903 /* Examine the attribute "predicable"; discover its boolean values
904 and its default. */
906 static void
907 identify_predicable_attribute (void)
909 struct queue_elem *elem;
910 char *p_true, *p_false;
911 const char *value;
913 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
914 for (elem = define_attr_queue; elem ; elem = elem->next)
915 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
916 goto found;
918 error_at (define_cond_exec_queue->loc,
919 "attribute `predicable' not defined");
920 return;
922 found:
923 value = XSTR (elem->data, 1);
924 p_false = xstrdup (value);
925 p_true = strchr (p_false, ',');
926 if (p_true == NULL || strchr (++p_true, ',') != NULL)
928 error_at (elem->loc, "attribute `predicable' is not a boolean");
929 free (p_false);
930 return;
932 p_true[-1] = '\0';
934 predicable_true = p_true;
935 predicable_false = p_false;
937 switch (GET_CODE (XEXP (elem->data, 2)))
939 case CONST_STRING:
940 value = XSTR (XEXP (elem->data, 2), 0);
941 break;
943 case CONST:
944 error_at (elem->loc, "attribute `predicable' cannot be const");
945 free (p_false);
946 return;
948 default:
949 error_at (elem->loc,
950 "attribute `predicable' must have a constant default");
951 free (p_false);
952 return;
955 if (strcmp (value, p_true) == 0)
956 predicable_default = 1;
957 else if (strcmp (value, p_false) == 0)
958 predicable_default = 0;
959 else
961 error_at (elem->loc, "unknown value `%s' for `predicable' attribute",
962 value);
963 free (p_false);
967 /* Return the number of alternatives in constraint S. */
969 static int
970 n_alternatives (const char *s)
972 int n = 1;
974 if (s)
975 while (*s)
976 n += (*s++ == ',');
978 return n;
981 /* The routine scans rtl PATTERN, find match_operand in it and counts
982 number of alternatives. If PATTERN contains several match_operands
983 with different number of alternatives, error is emitted, and the
984 routine returns 0. If all match_operands in PATTERN have the same
985 number of alternatives, it's stored in N_ALT, and the routine returns 1.
986 LOC is the location of PATTERN, for error reporting. */
987 static int
988 get_alternatives_number (rtx pattern, int *n_alt, file_location loc)
990 const char *fmt;
991 enum rtx_code code;
992 int i, j, len;
994 if (!n_alt)
995 return 0;
997 code = GET_CODE (pattern);
998 switch (code)
1000 case MATCH_OPERAND:
1001 i = n_alternatives (XSTR (pattern, 2));
1002 /* n_alternatives returns 1 if constraint string is empty -
1003 here we fix it up. */
1004 if (!*(XSTR (pattern, 2)))
1005 i = 0;
1006 if (*n_alt <= 0)
1007 *n_alt = i;
1009 else if (i && i != *n_alt)
1011 error_at (loc, "wrong number of alternatives in operand %d",
1012 XINT (pattern, 0));
1013 return 0;
1016 default:
1017 break;
1020 fmt = GET_RTX_FORMAT (code);
1021 len = GET_RTX_LENGTH (code);
1022 for (i = 0; i < len; i++)
1024 switch (fmt[i])
1026 case 'e': case 'u':
1027 if (!get_alternatives_number (XEXP (pattern, i), n_alt, loc))
1028 return 0;
1029 break;
1031 case 'V':
1032 if (XVEC (pattern, i) == NULL)
1033 break;
1035 case 'E':
1036 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1037 if (!get_alternatives_number (XVECEXP (pattern, i, j), n_alt, loc))
1038 return 0;
1039 break;
1041 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
1042 break;
1044 default:
1045 gcc_unreachable ();
1048 return 1;
1051 /* Determine how many alternatives there are in INSN, and how many
1052 operands. */
1054 static void
1055 collect_insn_data (rtx pattern, int *palt, int *pmax)
1057 const char *fmt;
1058 enum rtx_code code;
1059 int i, j, len;
1061 code = GET_CODE (pattern);
1062 switch (code)
1064 case MATCH_OPERAND:
1065 i = n_alternatives (XSTR (pattern, 2));
1066 *palt = (i > *palt ? i : *palt);
1067 /* Fall through. */
1069 case MATCH_OPERATOR:
1070 case MATCH_SCRATCH:
1071 case MATCH_PARALLEL:
1072 i = XINT (pattern, 0);
1073 if (i > *pmax)
1074 *pmax = i;
1075 break;
1077 default:
1078 break;
1081 fmt = GET_RTX_FORMAT (code);
1082 len = GET_RTX_LENGTH (code);
1083 for (i = 0; i < len; i++)
1085 switch (fmt[i])
1087 case 'e': case 'u':
1088 collect_insn_data (XEXP (pattern, i), palt, pmax);
1089 break;
1091 case 'V':
1092 if (XVEC (pattern, i) == NULL)
1093 break;
1094 /* Fall through. */
1095 case 'E':
1096 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1097 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
1098 break;
1100 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
1101 break;
1103 default:
1104 gcc_unreachable ();
1109 static rtx
1110 alter_predicate_for_insn (rtx pattern, int alt, int max_op,
1111 file_location loc)
1113 const char *fmt;
1114 enum rtx_code code;
1115 int i, j, len;
1117 code = GET_CODE (pattern);
1118 switch (code)
1120 case MATCH_OPERAND:
1122 const char *c = XSTR (pattern, 2);
1124 if (n_alternatives (c) != 1)
1126 error_at (loc, "too many alternatives for operand %d",
1127 XINT (pattern, 0));
1128 return NULL;
1131 /* Replicate C as needed to fill out ALT alternatives. */
1132 if (c && *c && alt > 1)
1134 size_t c_len = strlen (c);
1135 size_t len = alt * (c_len + 1);
1136 char *new_c = XNEWVEC (char, len);
1138 memcpy (new_c, c, c_len);
1139 for (i = 1; i < alt; ++i)
1141 new_c[i * (c_len + 1) - 1] = ',';
1142 memcpy (&new_c[i * (c_len + 1)], c, c_len);
1144 new_c[len - 1] = '\0';
1145 XSTR (pattern, 2) = new_c;
1148 /* Fall through. */
1150 case MATCH_OPERATOR:
1151 case MATCH_SCRATCH:
1152 case MATCH_PARALLEL:
1153 XINT (pattern, 0) += max_op;
1154 break;
1156 default:
1157 break;
1160 fmt = GET_RTX_FORMAT (code);
1161 len = GET_RTX_LENGTH (code);
1162 for (i = 0; i < len; i++)
1164 rtx r;
1166 switch (fmt[i])
1168 case 'e': case 'u':
1169 r = alter_predicate_for_insn (XEXP (pattern, i), alt, max_op, loc);
1170 if (r == NULL)
1171 return r;
1172 break;
1174 case 'E':
1175 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1177 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
1178 alt, max_op, loc);
1179 if (r == NULL)
1180 return r;
1182 break;
1184 case 'i': case 'r': case 'w': case '0': case 's':
1185 break;
1187 default:
1188 gcc_unreachable ();
1192 return pattern;
1195 /* Duplicate constraints in PATTERN. If pattern is from original
1196 rtl-template, we need to duplicate each alternative - for that we
1197 need to use duplicate_each_alternative () as a functor ALTER.
1198 If pattern is from output-pattern of define_subst, we need to
1199 duplicate constraints in another way - with duplicate_alternatives ().
1200 N_DUP is multiplication factor. */
1201 static rtx
1202 alter_constraints (rtx pattern, int n_dup, constraints_handler_t alter)
1204 const char *fmt;
1205 enum rtx_code code;
1206 int i, j, len;
1208 code = GET_CODE (pattern);
1209 switch (code)
1211 case MATCH_OPERAND:
1212 XSTR (pattern, 2) = alter (XSTR (pattern, 2), n_dup);
1213 break;
1215 default:
1216 break;
1219 fmt = GET_RTX_FORMAT (code);
1220 len = GET_RTX_LENGTH (code);
1221 for (i = 0; i < len; i++)
1223 rtx r;
1225 switch (fmt[i])
1227 case 'e': case 'u':
1228 r = alter_constraints (XEXP (pattern, i), n_dup, alter);
1229 if (r == NULL)
1230 return r;
1231 break;
1233 case 'E':
1234 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1236 r = alter_constraints (XVECEXP (pattern, i, j), n_dup, alter);
1237 if (r == NULL)
1238 return r;
1240 break;
1242 case 'i': case 'r': case 'w': case '0': case 's':
1243 break;
1245 default:
1246 break;
1250 return pattern;
1253 static const char *
1254 alter_test_for_insn (struct queue_elem *ce_elem,
1255 struct queue_elem *insn_elem)
1257 return join_c_conditions (XSTR (ce_elem->data, 1),
1258 XSTR (insn_elem->data, 2));
1261 /* Modify VAL, which is an attribute expression for the "enabled" attribute,
1262 to take "ce_enabled" into account. Return the new expression. */
1263 static rtx
1264 modify_attr_enabled_ce (rtx val)
1266 rtx eq_attr, str;
1267 rtx ite;
1268 eq_attr = rtx_alloc (EQ_ATTR);
1269 ite = rtx_alloc (IF_THEN_ELSE);
1270 str = rtx_alloc (CONST_STRING);
1272 XSTR (eq_attr, 0) = "ce_enabled";
1273 XSTR (eq_attr, 1) = "yes";
1274 XSTR (str, 0) = "no";
1275 XEXP (ite, 0) = eq_attr;
1276 XEXP (ite, 1) = val;
1277 XEXP (ite, 2) = str;
1279 return ite;
1282 /* Alter the attribute vector of INSN, which is a COND_EXEC variant created
1283 from a define_insn pattern. We must modify the "predicable" attribute
1284 to be named "ce_enabled", and also change any "enabled" attribute that's
1285 present so that it takes ce_enabled into account.
1286 We rely on the fact that INSN was created with copy_rtx, and modify data
1287 in-place. */
1289 static void
1290 alter_attrs_for_insn (rtx insn)
1292 static bool global_changes_made = false;
1293 rtvec vec = XVEC (insn, 4);
1294 rtvec new_vec;
1295 rtx val, set;
1296 int num_elem;
1297 int predicable_idx = -1;
1298 int enabled_idx = -1;
1299 int i;
1301 if (! vec)
1302 return;
1304 num_elem = GET_NUM_ELEM (vec);
1305 for (i = num_elem - 1; i >= 0; --i)
1307 rtx sub = RTVEC_ELT (vec, i);
1308 switch (GET_CODE (sub))
1310 case SET_ATTR:
1311 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1313 predicable_idx = i;
1314 XSTR (sub, 0) = "ce_enabled";
1316 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1318 enabled_idx = i;
1319 XSTR (sub, 0) = "nonce_enabled";
1321 break;
1323 case SET_ATTR_ALTERNATIVE:
1324 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1325 /* We already give an error elsewhere. */
1326 return;
1327 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1329 enabled_idx = i;
1330 XSTR (sub, 0) = "nonce_enabled";
1332 break;
1334 case SET:
1335 if (GET_CODE (SET_DEST (sub)) != ATTR)
1336 break;
1337 if (strcmp (XSTR (SET_DEST (sub), 0), "predicable") == 0)
1339 sub = SET_SRC (sub);
1340 if (GET_CODE (sub) == CONST_STRING)
1342 predicable_idx = i;
1343 XSTR (sub, 0) = "ce_enabled";
1345 else
1346 /* We already give an error elsewhere. */
1347 return;
1348 break;
1350 if (strcmp (XSTR (SET_DEST (sub), 0), "enabled") == 0)
1352 enabled_idx = i;
1353 XSTR (SET_DEST (sub), 0) = "nonce_enabled";
1355 break;
1357 default:
1358 gcc_unreachable ();
1361 if (predicable_idx == -1)
1362 return;
1364 if (!global_changes_made)
1366 struct queue_elem *elem;
1368 global_changes_made = true;
1369 add_define_attr ("ce_enabled");
1370 add_define_attr ("nonce_enabled");
1372 for (elem = define_attr_queue; elem ; elem = elem->next)
1373 if (strcmp (XSTR (elem->data, 0), "enabled") == 0)
1375 XEXP (elem->data, 2)
1376 = modify_attr_enabled_ce (XEXP (elem->data, 2));
1379 if (enabled_idx == -1)
1380 return;
1382 new_vec = rtvec_alloc (num_elem + 1);
1383 for (i = 0; i < num_elem; i++)
1384 RTVEC_ELT (new_vec, i) = RTVEC_ELT (vec, i);
1385 val = rtx_alloc (IF_THEN_ELSE);
1386 XEXP (val, 0) = rtx_alloc (EQ_ATTR);
1387 XEXP (val, 1) = rtx_alloc (CONST_STRING);
1388 XEXP (val, 2) = rtx_alloc (CONST_STRING);
1389 XSTR (XEXP (val, 0), 0) = "nonce_enabled";
1390 XSTR (XEXP (val, 0), 1) = "yes";
1391 XSTR (XEXP (val, 1), 0) = "yes";
1392 XSTR (XEXP (val, 2), 0) = "no";
1393 set = rtx_alloc (SET);
1394 SET_DEST (set) = rtx_alloc (ATTR);
1395 XSTR (SET_DEST (set), 0) = "enabled";
1396 SET_SRC (set) = modify_attr_enabled_ce (val);
1397 RTVEC_ELT (new_vec, i) = set;
1398 XVEC (insn, 4) = new_vec;
1401 /* As number of constraints is changed after define_subst, we need to
1402 process attributes as well - we need to duplicate them the same way
1403 that we duplicated constraints in original pattern
1404 ELEM is a queue element, containing our rtl-template,
1405 N_DUP - multiplication factor. */
1406 static void
1407 alter_attrs_for_subst_insn (struct queue_elem * elem, int n_dup)
1409 rtvec vec = XVEC (elem->data, 4);
1410 int num_elem;
1411 int i;
1413 if (n_dup < 2 || ! vec)
1414 return;
1416 num_elem = GET_NUM_ELEM (vec);
1417 for (i = num_elem - 1; i >= 0; --i)
1419 rtx sub = RTVEC_ELT (vec, i);
1420 switch (GET_CODE (sub))
1422 case SET_ATTR:
1423 if (strchr (XSTR (sub, 1), ',') != NULL)
1424 XSTR (sub, 1) = duplicate_alternatives (XSTR (sub, 1), n_dup);
1425 break;
1427 case SET_ATTR_ALTERNATIVE:
1428 case SET:
1429 error_at (elem->loc,
1430 "%s: `define_subst' does not support attributes "
1431 "assigned by `set' and `set_attr_alternative'",
1432 XSTR (elem->data, 0));
1433 return;
1435 default:
1436 gcc_unreachable ();
1441 /* Adjust all of the operand numbers in SRC to match the shift they'll
1442 get from an operand displacement of DISP. Return a pointer after the
1443 adjusted string. */
1445 static char *
1446 shift_output_template (char *dest, const char *src, int disp)
1448 while (*src)
1450 char c = *src++;
1451 *dest++ = c;
1452 if (c == '%')
1454 c = *src++;
1455 if (ISDIGIT ((unsigned char) c))
1456 c += disp;
1457 else if (ISALPHA (c))
1459 *dest++ = c;
1460 c = *src++ + disp;
1462 *dest++ = c;
1466 return dest;
1469 static const char *
1470 alter_output_for_insn (struct queue_elem *ce_elem,
1471 struct queue_elem *insn_elem,
1472 int alt, int max_op)
1474 const char *ce_out, *insn_out;
1475 char *result, *p;
1476 size_t len, ce_len, insn_len;
1478 /* ??? Could coordinate with genoutput to not duplicate code here. */
1480 ce_out = XSTR (ce_elem->data, 2);
1481 insn_out = XTMPL (insn_elem->data, 3);
1482 if (!ce_out || *ce_out == '\0')
1483 return insn_out;
1485 ce_len = strlen (ce_out);
1486 insn_len = strlen (insn_out);
1488 if (*insn_out == '*')
1489 /* You must take care of the predicate yourself. */
1490 return insn_out;
1492 if (*insn_out == '@')
1494 len = (ce_len + 1) * alt + insn_len + 1;
1495 p = result = XNEWVEC (char, len);
1500 *p++ = *insn_out++;
1501 while (ISSPACE ((unsigned char) *insn_out));
1503 if (*insn_out != '#')
1505 p = shift_output_template (p, ce_out, max_op);
1506 *p++ = ' ';
1510 *p++ = *insn_out++;
1511 while (*insn_out && *insn_out != '\n');
1513 while (*insn_out);
1514 *p = '\0';
1516 else
1518 len = ce_len + 1 + insn_len + 1;
1519 result = XNEWVEC (char, len);
1521 p = shift_output_template (result, ce_out, max_op);
1522 *p++ = ' ';
1523 memcpy (p, insn_out, insn_len + 1);
1526 return result;
1529 /* From string STR "a,b,c" produce "a,b,c,a,b,c,a,b,c", i.e. original
1530 string, duplicated N_DUP times. */
1532 static const char *
1533 duplicate_alternatives (const char * str, int n_dup)
1535 int i, len, new_len;
1536 char *result, *sp;
1537 const char *cp;
1539 if (n_dup < 2)
1540 return str;
1542 while (ISSPACE (*str))
1543 str++;
1545 if (*str == '\0')
1546 return str;
1548 cp = str;
1549 len = strlen (str);
1550 new_len = (len + 1) * n_dup;
1552 sp = result = XNEWVEC (char, new_len);
1554 /* Global modifier characters mustn't be duplicated: skip if found. */
1555 if (*cp == '=' || *cp == '+' || *cp == '%')
1557 *sp++ = *cp++;
1558 len--;
1561 /* Copy original constraints N_DUP times. */
1562 for (i = 0; i < n_dup; i++, sp += len+1)
1564 memcpy (sp, cp, len);
1565 *(sp+len) = (i == n_dup - 1) ? '\0' : ',';
1568 return result;
1571 /* From string STR "a,b,c" produce "a,a,a,b,b,b,c,c,c", i.e. string where
1572 each alternative from the original string is duplicated N_DUP times. */
1573 static const char *
1574 duplicate_each_alternative (const char * str, int n_dup)
1576 int i, len, new_len;
1577 char *result, *sp, *ep, *cp;
1579 if (n_dup < 2)
1580 return str;
1582 while (ISSPACE (*str))
1583 str++;
1585 if (*str == '\0')
1586 return str;
1588 cp = xstrdup (str);
1590 new_len = (strlen (cp) + 1) * n_dup;
1592 sp = result = XNEWVEC (char, new_len);
1594 /* Global modifier characters mustn't be duplicated: skip if found. */
1595 if (*cp == '=' || *cp == '+' || *cp == '%')
1596 *sp++ = *cp++;
1600 if ((ep = strchr (cp, ',')) != NULL)
1601 *ep++ = '\0';
1602 len = strlen (cp);
1604 /* Copy a constraint N_DUP times. */
1605 for (i = 0; i < n_dup; i++, sp += len + 1)
1607 memcpy (sp, cp, len);
1608 *(sp+len) = (ep == NULL && i == n_dup - 1) ? '\0' : ',';
1611 cp = ep;
1613 while (cp != NULL);
1615 return result;
1618 /* Alter the output of INSN whose pattern was modified by
1619 DEFINE_SUBST. We must replicate output strings according
1620 to the new number of alternatives ALT in substituted pattern.
1621 If ALT equals 1, output has one alternative or defined by C
1622 code, then output is returned without any changes. */
1624 static const char *
1625 alter_output_for_subst_insn (rtx insn, int alt)
1627 const char *insn_out, *sp ;
1628 char *old_out, *new_out, *cp;
1629 int i, j, new_len;
1631 insn_out = XTMPL (insn, 3);
1633 if (alt < 2 || *insn_out == '*' || *insn_out != '@')
1634 return insn_out;
1636 old_out = XNEWVEC (char, strlen (insn_out)),
1637 sp = insn_out;
1639 while (ISSPACE (*sp) || *sp == '@')
1640 sp++;
1642 for (i = 0; *sp;)
1643 old_out[i++] = *sp++;
1645 new_len = alt * (i + 1) + 1;
1647 new_out = XNEWVEC (char, new_len);
1648 new_out[0] = '@';
1650 for (j = 0, cp = new_out + 1; j < alt; j++, cp += i + 1)
1652 memcpy (cp, old_out, i);
1653 *(cp+i) = (j == alt - 1) ? '\0' : '\n';
1656 return new_out;
1659 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
1661 static void
1662 process_one_cond_exec (struct queue_elem *ce_elem)
1664 struct queue_elem *insn_elem;
1665 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
1667 int alternatives, max_operand;
1668 rtx pred, insn, pattern, split;
1669 char *new_name;
1670 int i;
1672 if (! is_predicable (insn_elem))
1673 continue;
1675 alternatives = 1;
1676 max_operand = -1;
1677 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
1678 max_operand += 1;
1680 if (XVECLEN (ce_elem->data, 0) != 1)
1682 error_at (ce_elem->loc, "too many patterns in predicate");
1683 return;
1686 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
1687 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
1688 ce_elem->loc);
1689 if (pred == NULL)
1690 return;
1692 /* Construct a new pattern for the new insn. */
1693 insn = copy_rtx (insn_elem->data);
1694 new_name = XNEWVAR (char, strlen XSTR (insn_elem->data, 0) + 4);
1695 sprintf (new_name, "*p %s", XSTR (insn_elem->data, 0));
1696 XSTR (insn, 0) = new_name;
1697 pattern = rtx_alloc (COND_EXEC);
1698 XEXP (pattern, 0) = pred;
1699 XEXP (pattern, 1) = add_implicit_parallel (XVEC (insn, 1));
1700 XVEC (insn, 1) = rtvec_alloc (1);
1701 XVECEXP (insn, 1, 0) = pattern;
1703 if (XVEC (ce_elem->data, 3) != NULL)
1705 rtvec attributes = rtvec_alloc (XVECLEN (insn, 4)
1706 + XVECLEN (ce_elem->data, 3));
1707 int i = 0;
1708 int j = 0;
1709 for (i = 0; i < XVECLEN (insn, 4); i++)
1710 RTVEC_ELT (attributes, i) = XVECEXP (insn, 4, i);
1712 for (j = 0; j < XVECLEN (ce_elem->data, 3); j++, i++)
1713 RTVEC_ELT (attributes, i) = XVECEXP (ce_elem->data, 3, j);
1715 XVEC (insn, 4) = attributes;
1718 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
1719 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
1720 alternatives, max_operand);
1721 alter_attrs_for_insn (insn);
1723 /* Put the new pattern on the `other' list so that it
1724 (a) is not reprocessed by other define_cond_exec patterns
1725 (b) appears after all normal define_insn patterns.
1727 ??? B is debatable. If one has normal insns that match
1728 cond_exec patterns, they will be preferred over these
1729 generated patterns. Whether this matters in practice, or if
1730 it's a good thing, or whether we should thread these new
1731 patterns into the define_insn chain just after their generator
1732 is something we'll have to experiment with. */
1734 queue_pattern (insn, &other_tail, insn_elem->loc);
1736 if (!insn_elem->split)
1737 continue;
1739 /* If the original insn came from a define_insn_and_split,
1740 generate a new split to handle the predicated insn. */
1741 split = copy_rtx (insn_elem->split->data);
1742 /* Predicate the pattern matched by the split. */
1743 pattern = rtx_alloc (COND_EXEC);
1744 XEXP (pattern, 0) = pred;
1745 XEXP (pattern, 1) = add_implicit_parallel (XVEC (split, 0));
1746 XVEC (split, 0) = rtvec_alloc (1);
1747 XVECEXP (split, 0, 0) = pattern;
1749 /* Predicate all of the insns generated by the split. */
1750 for (i = 0; i < XVECLEN (split, 2); i++)
1752 pattern = rtx_alloc (COND_EXEC);
1753 XEXP (pattern, 0) = pred;
1754 XEXP (pattern, 1) = XVECEXP (split, 2, i);
1755 XVECEXP (split, 2, i) = pattern;
1757 /* Add the new split to the queue. */
1758 queue_pattern (split, &other_tail, insn_elem->split->loc);
1762 /* Try to apply define_substs to the given ELEM.
1763 Only define_substs, specified via attributes would be applied.
1764 If attribute, requiring define_subst, is set, but no define_subst
1765 was applied, ELEM would be deleted. */
1767 static void
1768 process_substs_on_one_elem (struct queue_elem *elem,
1769 struct queue_elem *queue)
1771 struct queue_elem *subst_elem;
1772 int i, j, patterns_match;
1774 for (subst_elem = define_subst_queue;
1775 subst_elem; subst_elem = subst_elem->next)
1777 int alternatives, alternatives_subst;
1778 rtx subst_pattern;
1779 rtvec subst_pattern_vec;
1781 if (!has_subst_attribute (elem, subst_elem))
1782 continue;
1784 /* Compare original rtl-pattern from define_insn with input
1785 pattern from define_subst.
1786 Also, check if numbers of alternatives are the same in all
1787 match_operands. */
1788 if (XVECLEN (elem->data, 1) != XVECLEN (subst_elem->data, 1))
1789 continue;
1790 patterns_match = 1;
1791 alternatives = -1;
1792 alternatives_subst = -1;
1793 for (j = 0; j < XVECLEN (elem->data, 1); j++)
1795 if (!subst_pattern_match (XVECEXP (elem->data, 1, j),
1796 XVECEXP (subst_elem->data, 1, j),
1797 subst_elem->loc))
1799 patterns_match = 0;
1800 break;
1803 if (!get_alternatives_number (XVECEXP (elem->data, 1, j),
1804 &alternatives, subst_elem->loc))
1806 patterns_match = 0;
1807 break;
1811 /* Check if numbers of alternatives are the same in all
1812 match_operands in output template of define_subst. */
1813 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1815 if (!get_alternatives_number (XVECEXP (subst_elem->data, 3, j),
1816 &alternatives_subst,
1817 subst_elem->loc))
1819 patterns_match = 0;
1820 break;
1824 if (!patterns_match)
1825 continue;
1827 /* Clear array in which we save occupied indexes of operands. */
1828 memset (used_operands_numbers, 0, sizeof (used_operands_numbers));
1830 /* Create a pattern, based on the output one from define_subst. */
1831 subst_pattern_vec = rtvec_alloc (XVECLEN (subst_elem->data, 3));
1832 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1834 subst_pattern = copy_rtx (XVECEXP (subst_elem->data, 3, j));
1836 /* Duplicate constraints in substitute-pattern. */
1837 subst_pattern = alter_constraints (subst_pattern, alternatives,
1838 duplicate_each_alternative);
1840 subst_pattern = adjust_operands_numbers (subst_pattern);
1842 /* Substitute match_dup and match_op_dup in the new pattern and
1843 duplicate constraints. */
1844 subst_pattern = subst_dup (subst_pattern, alternatives,
1845 alternatives_subst);
1847 replace_duplicating_operands_in_pattern (subst_pattern);
1849 /* We don't need any constraints in DEFINE_EXPAND. */
1850 if (GET_CODE (elem->data) == DEFINE_EXPAND)
1851 remove_constraints (subst_pattern);
1853 RTVEC_ELT (subst_pattern_vec, j) = subst_pattern;
1855 XVEC (elem->data, 1) = subst_pattern_vec;
1857 for (i = 0; i < MAX_OPERANDS; i++)
1858 match_operand_entries_in_pattern[i] = NULL;
1860 if (GET_CODE (elem->data) == DEFINE_INSN)
1862 XTMPL (elem->data, 3) =
1863 alter_output_for_subst_insn (elem->data, alternatives_subst);
1864 alter_attrs_for_subst_insn (elem, alternatives_subst);
1867 /* Recalculate condition, joining conditions from original and
1868 DEFINE_SUBST input patterns. */
1869 XSTR (elem->data, 2) = join_c_conditions (XSTR (subst_elem->data, 2),
1870 XSTR (elem->data, 2));
1871 /* Mark that subst was applied by changing attribute from "yes"
1872 to "no". */
1873 change_subst_attribute (elem, subst_elem, subst_false);
1876 /* If ELEM contains a subst attribute with value "yes", then we
1877 expected that a subst would be applied, but it wasn't - so,
1878 we need to remove that elementto avoid duplicating. */
1879 for (subst_elem = define_subst_queue;
1880 subst_elem; subst_elem = subst_elem->next)
1882 if (has_subst_attribute (elem, subst_elem))
1884 remove_from_queue (elem, &queue);
1885 return;
1890 /* This is a subroutine of mark_operands_used_in_match_dup.
1891 This routine is marks all MATCH_OPERANDs inside PATTERN as occupied. */
1892 static void
1893 mark_operands_from_match_dup (rtx pattern)
1895 const char *fmt;
1896 int i, j, len, opno;
1898 if (GET_CODE (pattern) == MATCH_OPERAND
1899 || GET_CODE (pattern) == MATCH_OPERATOR
1900 || GET_CODE (pattern) == MATCH_PARALLEL)
1902 opno = XINT (pattern, 0);
1903 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
1904 used_operands_numbers [opno] = 1;
1906 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1907 len = GET_RTX_LENGTH (GET_CODE (pattern));
1908 for (i = 0; i < len; i++)
1910 switch (fmt[i])
1912 case 'e': case 'u':
1913 mark_operands_from_match_dup (XEXP (pattern, i));
1914 break;
1915 case 'E':
1916 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1917 mark_operands_from_match_dup (XVECEXP (pattern, i, j));
1918 break;
1923 /* This is a subroutine of adjust_operands_numbers.
1924 It goes through all expressions in PATTERN and when MATCH_DUP is
1925 met, all MATCH_OPERANDs inside it is marked as occupied. The
1926 process of marking is done by routin mark_operands_from_match_dup. */
1927 static void
1928 mark_operands_used_in_match_dup (rtx pattern)
1930 const char *fmt;
1931 int i, j, len, opno;
1933 if (GET_CODE (pattern) == MATCH_DUP)
1935 opno = XINT (pattern, 0);
1936 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
1937 mark_operands_from_match_dup (operand_data[opno]);
1938 return;
1940 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1941 len = GET_RTX_LENGTH (GET_CODE (pattern));
1942 for (i = 0; i < len; i++)
1944 switch (fmt[i])
1946 case 'e': case 'u':
1947 mark_operands_used_in_match_dup (XEXP (pattern, i));
1948 break;
1949 case 'E':
1950 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1951 mark_operands_used_in_match_dup (XVECEXP (pattern, i, j));
1952 break;
1957 /* This is subroutine of renumerate_operands_in_pattern.
1958 It finds first not-occupied operand-index. */
1959 static int
1960 find_first_unused_number_of_operand ()
1962 int i;
1963 for (i = 0; i < MAX_OPERANDS; i++)
1964 if (!used_operands_numbers[i])
1965 return i;
1966 return MAX_OPERANDS;
1969 /* This is subroutine of adjust_operands_numbers.
1970 It visits all expressions in PATTERN and assigns not-occupied
1971 operand indexes to MATCH_OPERANDs and MATCH_OPERATORs of this
1972 PATTERN. */
1973 static void
1974 renumerate_operands_in_pattern (rtx pattern)
1976 const char *fmt;
1977 enum rtx_code code;
1978 int i, j, len, new_opno;
1979 code = GET_CODE (pattern);
1981 if (code == MATCH_OPERAND
1982 || code == MATCH_OPERATOR)
1984 new_opno = find_first_unused_number_of_operand ();
1985 gcc_assert (new_opno >= 0 && new_opno < MAX_OPERANDS);
1986 XINT (pattern, 0) = new_opno;
1987 used_operands_numbers [new_opno] = 1;
1990 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1991 len = GET_RTX_LENGTH (GET_CODE (pattern));
1992 for (i = 0; i < len; i++)
1994 switch (fmt[i])
1996 case 'e': case 'u':
1997 renumerate_operands_in_pattern (XEXP (pattern, i));
1998 break;
1999 case 'E':
2000 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2001 renumerate_operands_in_pattern (XVECEXP (pattern, i, j));
2002 break;
2007 /* If output pattern of define_subst contains MATCH_DUP, then this
2008 expression would be replaced with the pattern, matched with
2009 MATCH_OPERAND from input pattern. This pattern could contain any
2010 number of MATCH_OPERANDs, MATCH_OPERATORs etc., so it's possible
2011 that a MATCH_OPERAND from output_pattern (if any) would have the
2012 same number, as MATCH_OPERAND from copied pattern. To avoid such
2013 indexes overlapping, we assign new indexes to MATCH_OPERANDs,
2014 laying in the output pattern outside of MATCH_DUPs. */
2015 static rtx
2016 adjust_operands_numbers (rtx pattern)
2018 mark_operands_used_in_match_dup (pattern);
2020 renumerate_operands_in_pattern (pattern);
2022 return pattern;
2025 /* Generate RTL expression
2026 (match_dup OPNO)
2028 static rtx
2029 generate_match_dup (int opno)
2031 rtx return_rtx = rtx_alloc (MATCH_DUP);
2032 PUT_CODE (return_rtx, MATCH_DUP);
2033 XINT (return_rtx, 0) = opno;
2034 return return_rtx;
2037 /* This routine checks all match_operands in PATTERN and if some of
2038 have the same index, it replaces all of them except the first one to
2039 match_dup.
2040 Usually, match_operands with the same indexes are forbidden, but
2041 after define_subst copy an RTL-expression from original template,
2042 indexes of existed and just-copied match_operands could coincide.
2043 To fix it, we replace one of them with match_dup. */
2044 static rtx
2045 replace_duplicating_operands_in_pattern (rtx pattern)
2047 const char *fmt;
2048 int i, j, len, opno;
2049 rtx mdup;
2051 if (GET_CODE (pattern) == MATCH_OPERAND)
2053 opno = XINT (pattern, 0);
2054 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2055 if (match_operand_entries_in_pattern[opno] == NULL)
2057 match_operand_entries_in_pattern[opno] = pattern;
2058 return NULL;
2060 else
2062 /* Compare predicates before replacing with match_dup. */
2063 if (strcmp (XSTR (pattern, 1),
2064 XSTR (match_operand_entries_in_pattern[opno], 1)))
2066 error ("duplicated match_operands with different predicates were"
2067 " found.");
2068 return NULL;
2070 return generate_match_dup (opno);
2073 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2074 len = GET_RTX_LENGTH (GET_CODE (pattern));
2075 for (i = 0; i < len; i++)
2077 switch (fmt[i])
2079 case 'e': case 'u':
2080 mdup = replace_duplicating_operands_in_pattern (XEXP (pattern, i));
2081 if (mdup)
2082 XEXP (pattern, i) = mdup;
2083 break;
2084 case 'E':
2085 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2087 mdup =
2088 replace_duplicating_operands_in_pattern (XVECEXP
2089 (pattern, i, j));
2090 if (mdup)
2091 XVECEXP (pattern, i, j) = mdup;
2093 break;
2096 return NULL;
2099 /* The routine modifies given input PATTERN of define_subst, replacing
2100 MATCH_DUP and MATCH_OP_DUP with operands from define_insn original
2101 pattern, whose operands are stored in OPERAND_DATA array.
2102 It also duplicates constraints in operands - constraints from
2103 define_insn operands are duplicated N_SUBST_ALT times, constraints
2104 from define_subst operands are duplicated N_ALT times.
2105 After the duplication, returned output rtl-pattern contains every
2106 combination of input constraints Vs constraints from define_subst
2107 output. */
2108 static rtx
2109 subst_dup (rtx pattern, int n_alt, int n_subst_alt)
2111 const char *fmt;
2112 enum rtx_code code;
2113 int i, j, len, opno;
2115 code = GET_CODE (pattern);
2116 switch (code)
2118 case MATCH_DUP:
2119 case MATCH_OP_DUP:
2120 opno = XINT (pattern, 0);
2122 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2124 if (operand_data[opno])
2126 pattern = copy_rtx (operand_data[opno]);
2128 /* Duplicate constraints. */
2129 pattern = alter_constraints (pattern, n_subst_alt,
2130 duplicate_alternatives);
2132 break;
2134 default:
2135 break;
2138 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2139 len = GET_RTX_LENGTH (GET_CODE (pattern));
2140 for (i = 0; i < len; i++)
2142 switch (fmt[i])
2144 case 'e': case 'u':
2145 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2146 XEXP (pattern, i) = subst_dup (XEXP (pattern, i),
2147 n_alt, n_subst_alt);
2148 break;
2149 case 'V':
2150 if (XVEC (pattern, i) == NULL)
2151 break;
2152 case 'E':
2153 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2154 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2155 XVECEXP (pattern, i, j) = subst_dup (XVECEXP (pattern, i, j),
2156 n_alt, n_subst_alt);
2157 break;
2159 case 'i': case 'r': case 'w': case '0': case 's': case 'S': case 'T':
2160 break;
2162 default:
2163 gcc_unreachable ();
2166 return pattern;
2169 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
2170 patterns appropriately. */
2172 static void
2173 process_define_cond_exec (void)
2175 struct queue_elem *elem;
2177 identify_predicable_attribute ();
2178 if (have_error)
2179 return;
2181 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
2182 process_one_cond_exec (elem);
2185 /* If we have any DEFINE_SUBST patterns, expand DEFINE_INSN and
2186 DEFINE_EXPAND patterns appropriately. */
2188 static void
2189 process_define_subst (void)
2191 struct queue_elem *elem, *elem_attr;
2193 /* Check if each define_subst has corresponding define_subst_attr. */
2194 for (elem = define_subst_queue; elem ; elem = elem->next)
2196 for (elem_attr = define_subst_attr_queue;
2197 elem_attr;
2198 elem_attr = elem_attr->next)
2199 if (strcmp (XSTR (elem->data, 0), XSTR (elem_attr->data, 1)) == 0)
2200 goto found;
2202 error_at (elem->loc,
2203 "%s: `define_subst' must have at least one "
2204 "corresponding `define_subst_attr'",
2205 XSTR (elem->data, 0));
2206 return;
2208 found:
2209 continue;
2212 for (elem = define_insn_queue; elem ; elem = elem->next)
2213 process_substs_on_one_elem (elem, define_insn_queue);
2214 for (elem = other_queue; elem ; elem = elem->next)
2216 if (GET_CODE (elem->data) != DEFINE_EXPAND)
2217 continue;
2218 process_substs_on_one_elem (elem, other_queue);
2222 /* A read_md_files callback for reading an rtx. */
2224 static void
2225 rtx_handle_directive (file_location loc, const char *rtx_name)
2227 auto_vec<rtx, 32> subrtxs;
2228 if (!read_rtx (rtx_name, &subrtxs))
2229 return;
2231 rtx x;
2232 unsigned int i;
2233 FOR_EACH_VEC_ELT (subrtxs, i, x)
2234 process_rtx (x, loc);
2237 /* Comparison function for the mnemonic hash table. */
2239 static int
2240 htab_eq_string (const void *s1, const void *s2)
2242 return strcmp ((const char*)s1, (const char*)s2) == 0;
2245 /* Add mnemonic STR with length LEN to the mnemonic hash table
2246 MNEMONIC_HTAB. A trailing zero end character is appendend to STR
2247 and a permanent heap copy of STR is created. */
2249 static void
2250 add_mnemonic_string (htab_t mnemonic_htab, const char *str, int len)
2252 char *new_str;
2253 void **slot;
2254 char *str_zero = (char*)alloca (len + 1);
2256 memcpy (str_zero, str, len);
2257 str_zero[len] = '\0';
2259 slot = htab_find_slot (mnemonic_htab, str_zero, INSERT);
2261 if (*slot)
2262 return;
2264 /* Not found; create a permanent copy and add it to the hash table. */
2265 new_str = XNEWVAR (char, len + 1);
2266 memcpy (new_str, str_zero, len + 1);
2267 *slot = new_str;
2270 /* Scan INSN for mnemonic strings and add them to the mnemonic hash
2271 table in MNEMONIC_HTAB.
2273 The mnemonics cannot be found if they are emitted using C code.
2275 If a mnemonic string contains ';' or a newline the string assumed
2276 to consist of more than a single instruction. The attribute value
2277 will then be set to the user defined default value. */
2279 static void
2280 gen_mnemonic_setattr (htab_t mnemonic_htab, rtx insn)
2282 const char *template_code, *cp;
2283 int i;
2284 int vec_len;
2285 rtx set_attr;
2286 char *attr_name;
2287 rtvec new_vec;
2289 template_code = XTMPL (insn, 3);
2291 /* Skip patterns which use C code to emit the template. */
2292 if (template_code[0] == '*')
2293 return;
2295 if (template_code[0] == '@')
2296 cp = &template_code[1];
2297 else
2298 cp = &template_code[0];
2300 for (i = 0; *cp; )
2302 const char *ep, *sp;
2303 int size = 0;
2305 while (ISSPACE (*cp))
2306 cp++;
2308 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
2309 if (!ISSPACE (*ep))
2310 sp = ep + 1;
2312 if (i > 0)
2313 obstack_1grow (&string_obstack, ',');
2315 while (cp < sp && ((*cp >= '0' && *cp <= '9')
2316 || (*cp >= 'a' && *cp <= 'z')))
2319 obstack_1grow (&string_obstack, *cp);
2320 cp++;
2321 size++;
2324 while (cp < sp)
2326 if (*cp == ';' || (*cp == '\\' && cp[1] == 'n'))
2328 /* Don't set a value if there are more than one
2329 instruction in the string. */
2330 obstack_next_free (&string_obstack) =
2331 obstack_next_free (&string_obstack) - size;
2332 size = 0;
2334 cp = sp;
2335 break;
2337 cp++;
2339 if (size == 0)
2340 obstack_1grow (&string_obstack, '*');
2341 else
2342 add_mnemonic_string (mnemonic_htab,
2343 obstack_next_free (&string_obstack) - size,
2344 size);
2345 i++;
2348 /* An insn definition might emit an empty string. */
2349 if (obstack_object_size (&string_obstack) == 0)
2350 return;
2352 obstack_1grow (&string_obstack, '\0');
2354 set_attr = rtx_alloc (SET_ATTR);
2355 XSTR (set_attr, 1) = XOBFINISH (&string_obstack, char *);
2356 attr_name = XNEWVAR (char, strlen (MNEMONIC_ATTR_NAME) + 1);
2357 strcpy (attr_name, MNEMONIC_ATTR_NAME);
2358 XSTR (set_attr, 0) = attr_name;
2360 if (!XVEC (insn, 4))
2361 vec_len = 0;
2362 else
2363 vec_len = XVECLEN (insn, 4);
2365 new_vec = rtvec_alloc (vec_len + 1);
2366 for (i = 0; i < vec_len; i++)
2367 RTVEC_ELT (new_vec, i) = XVECEXP (insn, 4, i);
2368 RTVEC_ELT (new_vec, vec_len) = set_attr;
2369 XVEC (insn, 4) = new_vec;
2372 /* This function is called for the elements in the mnemonic hashtable
2373 and generates a comma separated list of the mnemonics. */
2375 static int
2376 mnemonic_htab_callback (void **slot, void *info ATTRIBUTE_UNUSED)
2378 obstack_grow (&string_obstack, (char*)*slot, strlen ((char*)*slot));
2379 obstack_1grow (&string_obstack, ',');
2380 return 1;
2383 /* Generate (set_attr "mnemonic" "..") RTXs and append them to every
2384 insn definition in case the back end requests it by defining the
2385 mnemonic attribute. The values for the attribute will be extracted
2386 from the output patterns of the insn definitions as far as
2387 possible. */
2389 static void
2390 gen_mnemonic_attr (void)
2392 struct queue_elem *elem;
2393 rtx mnemonic_attr = NULL;
2394 htab_t mnemonic_htab;
2395 const char *str, *p;
2396 int i;
2398 if (have_error)
2399 return;
2401 /* Look for the DEFINE_ATTR for `mnemonic'. */
2402 for (elem = define_attr_queue; elem != *define_attr_tail; elem = elem->next)
2403 if (GET_CODE (elem->data) == DEFINE_ATTR
2404 && strcmp (XSTR (elem->data, 0), MNEMONIC_ATTR_NAME) == 0)
2406 mnemonic_attr = elem->data;
2407 break;
2410 /* A (define_attr "mnemonic" "...") indicates that the back-end
2411 wants a mnemonic attribute to be generated. */
2412 if (!mnemonic_attr)
2413 return;
2415 mnemonic_htab = htab_create_alloc (MNEMONIC_HTAB_SIZE, htab_hash_string,
2416 htab_eq_string, 0, xcalloc, free);
2418 for (elem = define_insn_queue; elem; elem = elem->next)
2420 rtx insn = elem->data;
2421 bool found = false;
2423 /* Check if the insn definition already has
2424 (set_attr "mnemonic" ...) or (set (attr "mnemonic") ...). */
2425 if (XVEC (insn, 4))
2426 for (i = 0; i < XVECLEN (insn, 4); i++)
2428 rtx set_attr = XVECEXP (insn, 4, i);
2430 switch (GET_CODE (set_attr))
2432 case SET_ATTR:
2433 case SET_ATTR_ALTERNATIVE:
2434 if (strcmp (XSTR (set_attr, 0), MNEMONIC_ATTR_NAME) == 0)
2435 found = true;
2436 break;
2437 case SET:
2438 if (GET_CODE (SET_DEST (set_attr)) == ATTR
2439 && strcmp (XSTR (SET_DEST (set_attr), 0),
2440 MNEMONIC_ATTR_NAME) == 0)
2441 found = true;
2442 break;
2443 default:
2444 break;
2448 if (!found)
2449 gen_mnemonic_setattr (mnemonic_htab, insn);
2452 /* Add the user defined values to the hash table. */
2453 str = XSTR (mnemonic_attr, 1);
2454 while ((p = scan_comma_elt (&str)) != NULL)
2455 add_mnemonic_string (mnemonic_htab, p, str - p);
2457 htab_traverse (mnemonic_htab, mnemonic_htab_callback, NULL);
2459 /* Replace the last ',' with the zero end character. */
2460 *((char *)obstack_next_free (&string_obstack) - 1) = '\0';
2461 XSTR (mnemonic_attr, 1) = XOBFINISH (&string_obstack, char *);
2464 /* Check if there are DEFINE_ATTRs with the same name. */
2465 static void
2466 check_define_attr_duplicates ()
2468 struct queue_elem *elem;
2469 htab_t attr_htab;
2470 char * attr_name;
2471 void **slot;
2473 attr_htab = htab_create (500, htab_hash_string, htab_eq_string, NULL);
2475 for (elem = define_attr_queue; elem; elem = elem->next)
2477 attr_name = xstrdup (XSTR (elem->data, 0));
2479 slot = htab_find_slot (attr_htab, attr_name, INSERT);
2481 /* Duplicate. */
2482 if (*slot)
2484 error_at (elem->loc, "redefinition of attribute '%s'", attr_name);
2485 htab_delete (attr_htab);
2486 return;
2489 *slot = attr_name;
2492 htab_delete (attr_htab);
2495 /* The entry point for initializing the reader. */
2497 bool
2498 init_rtx_reader_args_cb (int argc, char **argv,
2499 bool (*parse_opt) (const char *))
2501 /* Prepare to read input. */
2502 condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
2503 init_predicate_table ();
2504 obstack_init (rtl_obstack);
2506 /* Start at 1, to make 0 available for CODE_FOR_nothing. */
2507 sequence_num = 1;
2509 read_md_files (argc, argv, parse_opt, rtx_handle_directive);
2511 if (define_attr_queue != NULL)
2512 check_define_attr_duplicates ();
2514 /* Process define_cond_exec patterns. */
2515 if (define_cond_exec_queue != NULL)
2516 process_define_cond_exec ();
2518 /* Process define_subst patterns. */
2519 if (define_subst_queue != NULL)
2520 process_define_subst ();
2522 if (define_attr_queue != NULL)
2523 gen_mnemonic_attr ();
2525 return !have_error;
2528 /* Programs that don't have their own options can use this entry point
2529 instead. */
2530 bool
2531 init_rtx_reader_args (int argc, char **argv)
2533 return init_rtx_reader_args_cb (argc, argv, 0);
2536 /* Try to read a single rtx from the file. Return true on success,
2537 describing it in *INFO. */
2539 bool
2540 read_md_rtx (md_rtx_info *info)
2542 struct queue_elem **queue, *elem;
2543 rtx desc;
2545 discard:
2547 /* Read all patterns from a given queue before moving on to the next. */
2548 if (define_attr_queue != NULL)
2549 queue = &define_attr_queue;
2550 else if (define_pred_queue != NULL)
2551 queue = &define_pred_queue;
2552 else if (define_insn_queue != NULL)
2553 queue = &define_insn_queue;
2554 else if (other_queue != NULL)
2555 queue = &other_queue;
2556 else
2557 return false;
2559 elem = *queue;
2560 *queue = elem->next;
2561 info->def = elem->data;
2562 info->loc = elem->loc;
2563 info->index = sequence_num;
2565 free (elem);
2567 /* Discard insn patterns which we know can never match (because
2568 their C test is provably always false). If insn_elision is
2569 false, our caller needs to see all the patterns. Note that the
2570 elided patterns are never counted by the sequence numbering; it
2571 is the caller's responsibility, when insn_elision is false, not
2572 to use elided pattern numbers for anything. */
2573 desc = info->def;
2574 switch (GET_CODE (desc))
2576 case DEFINE_INSN:
2577 case DEFINE_EXPAND:
2578 case DEFINE_SUBST:
2579 if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
2580 sequence_num++;
2581 else if (insn_elision)
2582 goto discard;
2584 /* info->index is used here so the name table will match caller's
2585 idea of insn numbering, whether or not elision is active. */
2586 record_insn_name (info->index, XSTR (desc, 0));
2587 break;
2589 case DEFINE_SPLIT:
2590 case DEFINE_PEEPHOLE:
2591 case DEFINE_PEEPHOLE2:
2592 if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
2593 sequence_num++;
2594 else if (insn_elision)
2595 goto discard;
2596 break;
2598 default:
2599 break;
2602 return true;
2605 /* Helper functions for insn elision. */
2607 /* Compute a hash function of a c_test structure, which is keyed
2608 by its ->expr field. */
2609 hashval_t
2610 hash_c_test (const void *x)
2612 const struct c_test *a = (const struct c_test *) x;
2613 const unsigned char *base, *s = (const unsigned char *) a->expr;
2614 hashval_t hash;
2615 unsigned char c;
2616 unsigned int len;
2618 base = s;
2619 hash = 0;
2621 while ((c = *s++) != '\0')
2623 hash += c + (c << 17);
2624 hash ^= hash >> 2;
2627 len = s - base;
2628 hash += len + (len << 17);
2629 hash ^= hash >> 2;
2631 return hash;
2634 /* Compare two c_test expression structures. */
2636 cmp_c_test (const void *x, const void *y)
2638 const struct c_test *a = (const struct c_test *) x;
2639 const struct c_test *b = (const struct c_test *) y;
2641 return !strcmp (a->expr, b->expr);
2644 /* Given a string representing a C test expression, look it up in the
2645 condition_table and report whether or not its value is known
2646 at compile time. Returns a tristate: 1 for known true, 0 for
2647 known false, -1 for unknown. */
2649 maybe_eval_c_test (const char *expr)
2651 const struct c_test *test;
2652 struct c_test dummy;
2654 if (expr[0] == 0)
2655 return 1;
2657 dummy.expr = expr;
2658 test = (const struct c_test *)htab_find (condition_table, &dummy);
2659 if (!test)
2660 return -1;
2661 return test->value;
2664 /* Record the C test expression EXPR in the condition_table, with
2665 value VAL. Duplicates clobber previous entries. */
2667 void
2668 add_c_test (const char *expr, int value)
2670 struct c_test *test;
2672 if (expr[0] == 0)
2673 return;
2675 test = XNEW (struct c_test);
2676 test->expr = expr;
2677 test->value = value;
2679 *(htab_find_slot (condition_table, test, INSERT)) = test;
2682 /* For every C test, call CALLBACK with two arguments: a pointer to
2683 the condition structure and INFO. Stops when CALLBACK returns zero. */
2684 void
2685 traverse_c_tests (htab_trav callback, void *info)
2687 if (condition_table)
2688 htab_traverse (condition_table, callback, info);
2691 /* Helper functions for define_predicate and define_special_predicate
2692 processing. Shared between genrecog.c and genpreds.c. */
2694 static htab_t predicate_table;
2695 struct pred_data *first_predicate;
2696 static struct pred_data **last_predicate = &first_predicate;
2698 static hashval_t
2699 hash_struct_pred_data (const void *ptr)
2701 return htab_hash_string (((const struct pred_data *)ptr)->name);
2704 static int
2705 eq_struct_pred_data (const void *a, const void *b)
2707 return !strcmp (((const struct pred_data *)a)->name,
2708 ((const struct pred_data *)b)->name);
2711 struct pred_data *
2712 lookup_predicate (const char *name)
2714 struct pred_data key;
2715 key.name = name;
2716 return (struct pred_data *) htab_find (predicate_table, &key);
2719 /* Record that predicate PRED can accept CODE. */
2721 void
2722 add_predicate_code (struct pred_data *pred, enum rtx_code code)
2724 if (!pred->codes[code])
2726 pred->num_codes++;
2727 pred->codes[code] = true;
2729 if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
2730 pred->allows_non_const = true;
2732 if (code != REG
2733 && code != SUBREG
2734 && code != MEM
2735 && code != CONCAT
2736 && code != PARALLEL
2737 && code != STRICT_LOW_PART
2738 && code != SCRATCH)
2739 pred->allows_non_lvalue = true;
2741 if (pred->num_codes == 1)
2742 pred->singleton = code;
2743 else if (pred->num_codes == 2)
2744 pred->singleton = UNKNOWN;
2748 void
2749 add_predicate (struct pred_data *pred)
2751 void **slot = htab_find_slot (predicate_table, pred, INSERT);
2752 if (*slot)
2754 error ("duplicate predicate definition for '%s'", pred->name);
2755 return;
2757 *slot = pred;
2758 *last_predicate = pred;
2759 last_predicate = &pred->next;
2762 /* This array gives the initial content of the predicate table. It
2763 has entries for all predicates defined in recog.c. */
2765 struct std_pred_table
2767 const char *name;
2768 bool special;
2769 bool allows_const_p;
2770 RTX_CODE codes[NUM_RTX_CODE];
2773 static const struct std_pred_table std_preds[] = {
2774 {"general_operand", false, true, {SUBREG, REG, MEM}},
2775 {"address_operand", true, true, {SUBREG, REG, MEM, PLUS, MINUS, MULT,
2776 ZERO_EXTEND, SIGN_EXTEND, AND}},
2777 {"register_operand", false, false, {SUBREG, REG}},
2778 {"pmode_register_operand", true, false, {SUBREG, REG}},
2779 {"scratch_operand", false, false, {SCRATCH, REG}},
2780 {"immediate_operand", false, true, {UNKNOWN}},
2781 {"const_int_operand", false, false, {CONST_INT}},
2782 #if TARGET_SUPPORTS_WIDE_INT
2783 {"const_scalar_int_operand", false, false, {CONST_INT, CONST_WIDE_INT}},
2784 {"const_double_operand", false, false, {CONST_DOUBLE}},
2785 #else
2786 {"const_double_operand", false, false, {CONST_INT, CONST_DOUBLE}},
2787 #endif
2788 {"nonimmediate_operand", false, false, {SUBREG, REG, MEM}},
2789 {"nonmemory_operand", false, true, {SUBREG, REG}},
2790 {"push_operand", false, false, {MEM}},
2791 {"pop_operand", false, false, {MEM}},
2792 {"memory_operand", false, false, {SUBREG, MEM}},
2793 {"indirect_operand", false, false, {SUBREG, MEM}},
2794 {"ordered_comparison_operator", false, false, {EQ, NE,
2795 LE, LT, GE, GT,
2796 LEU, LTU, GEU, GTU}},
2797 {"comparison_operator", false, false, {EQ, NE,
2798 LE, LT, GE, GT,
2799 LEU, LTU, GEU, GTU,
2800 UNORDERED, ORDERED,
2801 UNEQ, UNGE, UNGT,
2802 UNLE, UNLT, LTGT}}
2804 #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
2806 /* Initialize the table of predicate definitions, starting with
2807 the information we have on generic predicates. */
2809 static void
2810 init_predicate_table (void)
2812 size_t i, j;
2813 struct pred_data *pred;
2815 predicate_table = htab_create_alloc (37, hash_struct_pred_data,
2816 eq_struct_pred_data, 0,
2817 xcalloc, free);
2819 for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
2821 pred = XCNEW (struct pred_data);
2822 pred->name = std_preds[i].name;
2823 pred->special = std_preds[i].special;
2825 for (j = 0; std_preds[i].codes[j] != 0; j++)
2826 add_predicate_code (pred, std_preds[i].codes[j]);
2828 if (std_preds[i].allows_const_p)
2829 for (j = 0; j < NUM_RTX_CODE; j++)
2830 if (GET_RTX_CLASS (j) == RTX_CONST_OBJ)
2831 add_predicate_code (pred, (enum rtx_code) j);
2833 add_predicate (pred);
2837 /* These functions allow linkage with print-rtl.c. Also, some generators
2838 like to annotate their output with insn names. */
2840 /* Holds an array of names indexed by insn_code_number. */
2841 static char **insn_name_ptr = 0;
2842 static int insn_name_ptr_size = 0;
2844 const char *
2845 get_insn_name (int code)
2847 if (code < insn_name_ptr_size)
2848 return insn_name_ptr[code];
2849 else
2850 return NULL;
2853 static void
2854 record_insn_name (int code, const char *name)
2856 static const char *last_real_name = "insn";
2857 static int last_real_code = 0;
2858 char *new_name;
2860 if (insn_name_ptr_size <= code)
2862 int new_size;
2863 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2864 insn_name_ptr = XRESIZEVEC (char *, insn_name_ptr, new_size);
2865 memset (insn_name_ptr + insn_name_ptr_size, 0,
2866 sizeof (char *) * (new_size - insn_name_ptr_size));
2867 insn_name_ptr_size = new_size;
2870 if (!name || name[0] == '\0')
2872 new_name = XNEWVAR (char, strlen (last_real_name) + 10);
2873 sprintf (new_name, "%s+%d", last_real_name, code - last_real_code);
2875 else
2877 last_real_name = new_name = xstrdup (name);
2878 last_real_code = code;
2881 insn_name_ptr[code] = new_name;
2884 /* Make STATS describe the operands that appear in rtx X. */
2886 static void
2887 get_pattern_stats_1 (struct pattern_stats *stats, rtx x)
2889 RTX_CODE code;
2890 int i;
2891 int len;
2892 const char *fmt;
2894 if (x == NULL_RTX)
2895 return;
2897 code = GET_CODE (x);
2898 switch (code)
2900 case MATCH_OPERAND:
2901 case MATCH_OPERATOR:
2902 case MATCH_PARALLEL:
2903 stats->max_opno = MAX (stats->max_opno, XINT (x, 0));
2904 break;
2906 case MATCH_DUP:
2907 case MATCH_OP_DUP:
2908 case MATCH_PAR_DUP:
2909 stats->num_dups++;
2910 stats->max_dup_opno = MAX (stats->max_dup_opno, XINT (x, 0));
2911 break;
2913 case MATCH_SCRATCH:
2914 stats->max_scratch_opno = MAX (stats->max_scratch_opno, XINT (x, 0));
2915 break;
2917 default:
2918 break;
2921 fmt = GET_RTX_FORMAT (code);
2922 len = GET_RTX_LENGTH (code);
2923 for (i = 0; i < len; i++)
2925 if (fmt[i] == 'e' || fmt[i] == 'u')
2926 get_pattern_stats_1 (stats, XEXP (x, i));
2927 else if (fmt[i] == 'E')
2929 int j;
2930 for (j = 0; j < XVECLEN (x, i); j++)
2931 get_pattern_stats_1 (stats, XVECEXP (x, i, j));
2936 /* Make STATS describe the operands that appear in instruction pattern
2937 PATTERN. */
2939 void
2940 get_pattern_stats (struct pattern_stats *stats, rtvec pattern)
2942 int i, len;
2944 stats->max_opno = -1;
2945 stats->max_dup_opno = -1;
2946 stats->max_scratch_opno = -1;
2947 stats->num_dups = 0;
2949 len = GET_NUM_ELEM (pattern);
2950 for (i = 0; i < len; i++)
2951 get_pattern_stats_1 (stats, RTVEC_ELT (pattern, i));
2953 stats->num_generator_args = stats->max_opno + 1;
2954 stats->num_insn_operands = MAX (stats->max_opno,
2955 stats->max_scratch_opno) + 1;
2956 stats->num_operand_vars = MAX (stats->max_opno,
2957 MAX (stats->max_dup_opno,
2958 stats->max_scratch_opno)) + 1;
2961 /* Return the emit_* function that should be used for pattern X, or NULL
2962 if we can't pick a particular type at compile time and should instead
2963 fall back to "emit". */
2965 const char *
2966 get_emit_function (rtx x)
2968 switch (classify_insn (x))
2970 case INSN:
2971 return "emit_insn";
2973 case CALL_INSN:
2974 return "emit_call_insn";
2976 case JUMP_INSN:
2977 return "emit_jump_insn";
2979 case UNKNOWN:
2980 return NULL;
2982 default:
2983 gcc_unreachable ();
2987 /* Return true if we must emit a barrier after pattern X. */
2989 bool
2990 needs_barrier_p (rtx x)
2992 return (GET_CODE (x) == SET
2993 && GET_CODE (SET_DEST (x)) == PC
2994 && GET_CODE (SET_SRC (x)) == LABEL_REF);