altivec.md (altivec_mov<mode>, [...]): Change the RTL attribute "length" from "4...
[official-gcc.git] / gcc / gensupport.c
blob0150346d40915ae54b7707556a638f54e725a7e5
1 /* Support routines for the various generation passes.
2 Copyright (C) 2000-2019 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 named patterns and INSN_CODEs. */
47 static int insn_sequence_num;
49 /* Counter for define_splits. */
50 static int split_sequence_num;
52 /* Counter for define_peephole2s. */
53 static int peephole2_sequence_num;
55 static int predicable_default;
56 static const char *predicable_true;
57 static const char *predicable_false;
59 static const char *subst_true = "yes";
60 static const char *subst_false = "no";
62 static htab_t condition_table;
64 /* We initially queue all patterns, process the define_insn,
65 define_cond_exec and define_subst patterns, then return
66 them one at a time. */
68 struct queue_elem
70 rtx data;
71 file_location loc;
72 struct queue_elem *next;
73 /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT or
74 DEFINE_INSN_AND_REWRITE, SPLIT points to the generated DEFINE_SPLIT. */
75 struct queue_elem *split;
78 #define MNEMONIC_ATTR_NAME "mnemonic"
79 #define MNEMONIC_HTAB_SIZE 1024
81 static struct queue_elem *define_attr_queue;
82 static struct queue_elem **define_attr_tail = &define_attr_queue;
83 static struct queue_elem *define_pred_queue;
84 static struct queue_elem **define_pred_tail = &define_pred_queue;
85 static struct queue_elem *define_insn_queue;
86 static struct queue_elem **define_insn_tail = &define_insn_queue;
87 static struct queue_elem *define_cond_exec_queue;
88 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
89 static struct queue_elem *define_subst_queue;
90 static struct queue_elem **define_subst_tail = &define_subst_queue;
91 static struct queue_elem *other_queue;
92 static struct queue_elem **other_tail = &other_queue;
93 static struct queue_elem *define_subst_attr_queue;
94 static struct queue_elem **define_subst_attr_tail = &define_subst_attr_queue;
96 /* Mapping from DEFINE_* rtxes to their location in the source file. */
97 static hash_map <rtx, file_location> *rtx_locs;
99 static void remove_constraints (rtx);
101 static int is_predicable (struct queue_elem *);
102 static void identify_predicable_attribute (void);
103 static int n_alternatives (const char *);
104 static void collect_insn_data (rtx, int *, int *);
105 static const char *alter_test_for_insn (struct queue_elem *,
106 struct queue_elem *);
107 static char *shift_output_template (char *, const char *, int);
108 static const char *alter_output_for_insn (struct queue_elem *,
109 struct queue_elem *,
110 int, int);
111 static void process_one_cond_exec (struct queue_elem *);
112 static void process_define_cond_exec (void);
113 static void init_predicate_table (void);
114 static void record_insn_name (int, const char *);
116 static bool has_subst_attribute (struct queue_elem *, struct queue_elem *);
117 static const char * alter_output_for_subst_insn (rtx, int);
118 static void alter_attrs_for_subst_insn (struct queue_elem *, int);
119 static void process_substs_on_one_elem (struct queue_elem *,
120 struct queue_elem *);
121 static rtx subst_dup (rtx, int, int);
122 static void process_define_subst (void);
124 static const char * duplicate_alternatives (const char *, int);
125 static const char * duplicate_each_alternative (const char * str, int n_dup);
127 typedef const char * (*constraints_handler_t) (const char *, int);
128 static rtx alter_constraints (rtx, int, constraints_handler_t);
129 static rtx adjust_operands_numbers (rtx);
130 static rtx replace_duplicating_operands_in_pattern (rtx);
132 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
133 the gensupport programs. */
136 gen_rtx_CONST_INT (machine_mode ARG_UNUSED (mode),
137 HOST_WIDE_INT arg)
139 rtx rt = rtx_alloc (CONST_INT);
141 XWINT (rt, 0) = arg;
142 return rt;
145 /* Return the rtx pattern specified by the list of rtxes in a
146 define_insn or define_split. */
149 add_implicit_parallel (rtvec vec)
151 if (GET_NUM_ELEM (vec) == 1)
152 return RTVEC_ELT (vec, 0);
153 else
155 rtx pattern = rtx_alloc (PARALLEL);
156 XVEC (pattern, 0) = vec;
157 return pattern;
161 /* Predicate handling.
163 We construct from the machine description a table mapping each
164 predicate to a list of the rtl codes it can possibly match. The
165 function 'maybe_both_true' uses it to deduce that there are no
166 expressions that can be matches by certain pairs of tree nodes.
167 Also, if a predicate can match only one code, we can hardwire that
168 code into the node testing the predicate.
170 Some predicates are flagged as special. validate_pattern will not
171 warn about modeless match_operand expressions if they have a
172 special predicate. Predicates that allow only constants are also
173 treated as special, for this purpose.
175 validate_pattern will warn about predicates that allow non-lvalues
176 when they appear in destination operands.
178 Calculating the set of rtx codes that can possibly be accepted by a
179 predicate expression EXP requires a three-state logic: any given
180 subexpression may definitively accept a code C (Y), definitively
181 reject a code C (N), or may have an indeterminate effect (I). N
182 and I is N; Y or I is Y; Y and I, N or I are both I. Here are full
183 truth tables.
185 a b a&b a|b
186 Y Y Y Y
187 N Y N Y
188 N N N N
189 I Y I Y
190 I N N I
191 I I I I
193 We represent Y with 1, N with 0, I with 2. If any code is left in
194 an I state by the complete expression, we must assume that that
195 code can be accepted. */
197 #define N 0
198 #define Y 1
199 #define I 2
201 #define TRISTATE_AND(a,b) \
202 ((a) == I ? ((b) == N ? N : I) : \
203 (b) == I ? ((a) == N ? N : I) : \
204 (a) && (b))
206 #define TRISTATE_OR(a,b) \
207 ((a) == I ? ((b) == Y ? Y : I) : \
208 (b) == I ? ((a) == Y ? Y : I) : \
209 (a) || (b))
211 #define TRISTATE_NOT(a) \
212 ((a) == I ? I : !(a))
214 /* 0 means no warning about that code yet, 1 means warned. */
215 static char did_you_mean_codes[NUM_RTX_CODE];
217 /* Recursively calculate the set of rtx codes accepted by the
218 predicate expression EXP, writing the result to CODES. LOC is
219 the .md file location of the directive containing EXP. */
221 void
222 compute_test_codes (rtx exp, file_location loc, char *codes)
224 char op0_codes[NUM_RTX_CODE];
225 char op1_codes[NUM_RTX_CODE];
226 char op2_codes[NUM_RTX_CODE];
227 int i;
229 switch (GET_CODE (exp))
231 case AND:
232 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
233 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
234 for (i = 0; i < NUM_RTX_CODE; i++)
235 codes[i] = TRISTATE_AND (op0_codes[i], op1_codes[i]);
236 break;
238 case IOR:
239 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
240 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
241 for (i = 0; i < NUM_RTX_CODE; i++)
242 codes[i] = TRISTATE_OR (op0_codes[i], op1_codes[i]);
243 break;
244 case NOT:
245 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
246 for (i = 0; i < NUM_RTX_CODE; i++)
247 codes[i] = TRISTATE_NOT (op0_codes[i]);
248 break;
250 case IF_THEN_ELSE:
251 /* a ? b : c accepts the same codes as (a & b) | (!a & c). */
252 compute_test_codes (XEXP (exp, 0), loc, op0_codes);
253 compute_test_codes (XEXP (exp, 1), loc, op1_codes);
254 compute_test_codes (XEXP (exp, 2), loc, op2_codes);
255 for (i = 0; i < NUM_RTX_CODE; i++)
256 codes[i] = TRISTATE_OR (TRISTATE_AND (op0_codes[i], op1_codes[i]),
257 TRISTATE_AND (TRISTATE_NOT (op0_codes[i]),
258 op2_codes[i]));
259 break;
261 case MATCH_CODE:
262 /* MATCH_CODE allows a specified list of codes. However, if it
263 does not apply to the top level of the expression, it does not
264 constrain the set of codes for the top level. */
265 if (XSTR (exp, 1)[0] != '\0')
267 memset (codes, Y, NUM_RTX_CODE);
268 break;
271 memset (codes, N, NUM_RTX_CODE);
273 const char *next_code = XSTR (exp, 0);
274 const char *code;
276 if (*next_code == '\0')
278 error_at (loc, "empty match_code expression");
279 break;
282 while ((code = scan_comma_elt (&next_code)) != 0)
284 size_t n = next_code - code;
285 int found_it = 0;
287 for (i = 0; i < NUM_RTX_CODE; i++)
288 if (!strncmp (code, GET_RTX_NAME (i), n)
289 && GET_RTX_NAME (i)[n] == '\0')
291 codes[i] = Y;
292 found_it = 1;
293 break;
295 if (!found_it)
297 error_at (loc, "match_code \"%.*s\" matches nothing",
298 (int) n, code);
299 for (i = 0; i < NUM_RTX_CODE; i++)
300 if (!strncasecmp (code, GET_RTX_NAME (i), n)
301 && GET_RTX_NAME (i)[n] == '\0'
302 && !did_you_mean_codes[i])
304 did_you_mean_codes[i] = 1;
305 message_at (loc, "(did you mean \"%s\"?)",
306 GET_RTX_NAME (i));
311 break;
313 case MATCH_OPERAND:
314 /* MATCH_OPERAND disallows the set of codes that the named predicate
315 disallows, and is indeterminate for the codes that it does allow. */
317 struct pred_data *p = lookup_predicate (XSTR (exp, 1));
318 if (!p)
320 error_at (loc, "reference to unknown predicate '%s'",
321 XSTR (exp, 1));
322 break;
324 for (i = 0; i < NUM_RTX_CODE; i++)
325 codes[i] = p->codes[i] ? I : N;
327 break;
330 case MATCH_TEST:
331 /* (match_test WHATEVER) is completely indeterminate. */
332 memset (codes, I, NUM_RTX_CODE);
333 break;
335 default:
336 error_at (loc, "'%s' cannot be used in predicates or constraints",
337 GET_RTX_NAME (GET_CODE (exp)));
338 memset (codes, I, NUM_RTX_CODE);
339 break;
343 #undef TRISTATE_OR
344 #undef TRISTATE_AND
345 #undef TRISTATE_NOT
347 /* Return true if NAME is a valid predicate name. */
349 static bool
350 valid_predicate_name_p (const char *name)
352 const char *p;
354 if (!ISALPHA (name[0]) && name[0] != '_')
355 return false;
356 for (p = name + 1; *p; p++)
357 if (!ISALNUM (*p) && *p != '_')
358 return false;
359 return true;
362 /* Process define_predicate directive DESC, which appears at location LOC.
363 Compute the set of codes that can be matched, and record this as a known
364 predicate. */
366 static void
367 process_define_predicate (rtx desc, file_location loc)
369 struct pred_data *pred;
370 char codes[NUM_RTX_CODE];
371 int i;
373 if (!valid_predicate_name_p (XSTR (desc, 0)))
375 error_at (loc, "%s: predicate name must be a valid C function name",
376 XSTR (desc, 0));
377 return;
380 pred = XCNEW (struct pred_data);
381 pred->name = XSTR (desc, 0);
382 pred->exp = XEXP (desc, 1);
383 pred->c_block = XSTR (desc, 2);
384 if (GET_CODE (desc) == DEFINE_SPECIAL_PREDICATE)
385 pred->special = true;
387 compute_test_codes (XEXP (desc, 1), loc, codes);
389 for (i = 0; i < NUM_RTX_CODE; i++)
390 if (codes[i] != N)
391 add_predicate_code (pred, (enum rtx_code) i);
393 add_predicate (pred);
395 #undef I
396 #undef N
397 #undef Y
399 /* Queue PATTERN on LIST_TAIL. Return the address of the new queue
400 element. */
402 static struct queue_elem *
403 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
404 file_location loc)
406 struct queue_elem *e = XNEW (struct queue_elem);
407 e->data = pattern;
408 e->loc = loc;
409 e->next = NULL;
410 e->split = NULL;
411 **list_tail = e;
412 *list_tail = &e->next;
413 return e;
416 /* Remove element ELEM from QUEUE. */
417 static void
418 remove_from_queue (struct queue_elem *elem, struct queue_elem **queue)
420 struct queue_elem *prev, *e;
421 prev = NULL;
422 for (e = *queue; e ; e = e->next)
424 if (e == elem)
425 break;
426 prev = e;
428 if (e == NULL)
429 return;
431 if (prev)
432 prev->next = elem->next;
433 else
434 *queue = elem->next;
437 /* Build a define_attr for an binary attribute with name NAME and
438 possible values "yes" and "no", and queue it. */
439 static void
440 add_define_attr (const char *name)
442 struct queue_elem *e = XNEW (struct queue_elem);
443 rtx t1 = rtx_alloc (DEFINE_ATTR);
444 XSTR (t1, 0) = name;
445 XSTR (t1, 1) = "no,yes";
446 XEXP (t1, 2) = rtx_alloc (CONST_STRING);
447 XSTR (XEXP (t1, 2), 0) = "yes";
448 e->data = t1;
449 e->loc = file_location ("built-in", -1, -1);
450 e->next = define_attr_queue;
451 define_attr_queue = e;
455 /* Recursively remove constraints from an rtx. */
457 static void
458 remove_constraints (rtx part)
460 int i, j;
461 const char *format_ptr;
463 if (part == 0)
464 return;
466 if (GET_CODE (part) == MATCH_OPERAND)
467 XSTR (part, 2) = "";
468 else if (GET_CODE (part) == MATCH_SCRATCH)
469 XSTR (part, 1) = "";
471 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
473 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
474 switch (*format_ptr++)
476 case 'e':
477 case 'u':
478 remove_constraints (XEXP (part, i));
479 break;
480 case 'E':
481 if (XVEC (part, i) != NULL)
482 for (j = 0; j < XVECLEN (part, i); j++)
483 remove_constraints (XVECEXP (part, i, j));
484 break;
488 /* Recursively replace MATCH_OPERANDs with MATCH_DUPs and MATCH_OPERATORs
489 with MATCH_OP_DUPs in X. */
491 static rtx
492 replace_operands_with_dups (rtx x)
494 if (x == 0)
495 return x;
497 rtx newx;
498 if (GET_CODE (x) == MATCH_OPERAND)
500 newx = rtx_alloc (MATCH_DUP);
501 XINT (newx, 0) = XINT (x, 0);
503 else if (GET_CODE (x) == MATCH_OPERATOR)
505 newx = rtx_alloc (MATCH_OP_DUP);
506 XINT (newx, 0) = XINT (x, 0);
507 XVEC (newx, 1) = XVEC (x, 2);
509 else
510 newx = shallow_copy_rtx (x);
512 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (x));
513 for (int i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
514 switch (*format_ptr++)
516 case 'e':
517 case 'u':
518 XEXP (newx, i) = replace_operands_with_dups (XEXP (x, i));
519 break;
520 case 'E':
521 if (XVEC (x, i) != NULL)
523 XVEC (newx, i) = rtvec_alloc (XVECLEN (x, i));
524 for (int j = 0; j < XVECLEN (x, i); j++)
525 XVECEXP (newx, i, j)
526 = replace_operands_with_dups (XVECEXP (x, i, j));
528 break;
530 return newx;
533 /* Convert matching pattern VEC from a DEFINE_INSN_AND_REWRITE into
534 a sequence that should be generated by the splitter. */
536 static rtvec
537 gen_rewrite_sequence (rtvec vec)
539 rtvec new_vec = rtvec_alloc (1);
540 rtx x = add_implicit_parallel (vec);
541 RTVEC_ELT (new_vec, 0) = replace_operands_with_dups (x);
542 return new_vec;
545 /* Process a top level rtx in some way, queuing as appropriate. */
547 static void
548 process_rtx (rtx desc, file_location loc)
550 switch (GET_CODE (desc))
552 case DEFINE_INSN:
553 queue_pattern (desc, &define_insn_tail, loc);
554 break;
556 case DEFINE_COND_EXEC:
557 queue_pattern (desc, &define_cond_exec_tail, loc);
558 break;
560 case DEFINE_SUBST:
561 queue_pattern (desc, &define_subst_tail, loc);
562 break;
564 case DEFINE_SUBST_ATTR:
565 queue_pattern (desc, &define_subst_attr_tail, loc);
566 break;
568 case DEFINE_ATTR:
569 case DEFINE_ENUM_ATTR:
570 queue_pattern (desc, &define_attr_tail, loc);
571 break;
573 case DEFINE_PREDICATE:
574 case DEFINE_SPECIAL_PREDICATE:
575 process_define_predicate (desc, loc);
576 /* Fall through. */
578 case DEFINE_CONSTRAINT:
579 case DEFINE_REGISTER_CONSTRAINT:
580 case DEFINE_MEMORY_CONSTRAINT:
581 case DEFINE_SPECIAL_MEMORY_CONSTRAINT:
582 case DEFINE_ADDRESS_CONSTRAINT:
583 queue_pattern (desc, &define_pred_tail, loc);
584 break;
586 case DEFINE_INSN_AND_SPLIT:
587 case DEFINE_INSN_AND_REWRITE:
589 const char *split_cond;
590 rtx split;
591 rtvec attr;
592 int i;
593 struct queue_elem *insn_elem;
594 struct queue_elem *split_elem;
595 int split_code = (GET_CODE (desc) == DEFINE_INSN_AND_REWRITE ? 5 : 6);
597 /* Create a split with values from the insn_and_split. */
598 split = rtx_alloc (DEFINE_SPLIT);
600 i = XVECLEN (desc, 1);
601 XVEC (split, 0) = rtvec_alloc (i);
602 while (--i >= 0)
604 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
605 remove_constraints (XVECEXP (split, 0, i));
608 /* If the split condition starts with "&&", append it to the
609 insn condition to create the new split condition. */
610 split_cond = XSTR (desc, 4);
611 if (split_cond[0] == '&' && split_cond[1] == '&')
613 rtx_reader_ptr->copy_md_ptr_loc (split_cond + 2, split_cond);
614 split_cond = rtx_reader_ptr->join_c_conditions (XSTR (desc, 2),
615 split_cond + 2);
617 else if (GET_CODE (desc) == DEFINE_INSN_AND_REWRITE)
618 error_at (loc, "the rewrite condition must start with `&&'");
619 XSTR (split, 1) = split_cond;
620 if (GET_CODE (desc) == DEFINE_INSN_AND_REWRITE)
621 XVEC (split, 2) = gen_rewrite_sequence (XVEC (desc, 1));
622 else
623 XVEC (split, 2) = XVEC (desc, 5);
624 XSTR (split, 3) = XSTR (desc, split_code);
626 /* Fix up the DEFINE_INSN. */
627 attr = XVEC (desc, split_code + 1);
628 PUT_CODE (desc, DEFINE_INSN);
629 XVEC (desc, 4) = attr;
631 /* Queue them. */
632 insn_elem = queue_pattern (desc, &define_insn_tail, loc);
633 split_elem = queue_pattern (split, &other_tail, loc);
634 insn_elem->split = split_elem;
635 break;
638 default:
639 queue_pattern (desc, &other_tail, loc);
640 break;
644 /* Return true if attribute PREDICABLE is true for ELEM, which holds
645 a DEFINE_INSN. */
647 static int
648 is_predicable (struct queue_elem *elem)
650 rtvec vec = XVEC (elem->data, 4);
651 const char *value;
652 int i;
654 if (! vec)
655 return predicable_default;
657 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
659 rtx sub = RTVEC_ELT (vec, i);
660 switch (GET_CODE (sub))
662 case SET_ATTR:
663 if (strcmp (XSTR (sub, 0), "predicable") == 0)
665 value = XSTR (sub, 1);
666 goto found;
668 break;
670 case SET_ATTR_ALTERNATIVE:
671 if (strcmp (XSTR (sub, 0), "predicable") == 0)
673 error_at (elem->loc, "multiple alternatives for `predicable'");
674 return 0;
676 break;
678 case SET:
679 if (GET_CODE (SET_DEST (sub)) != ATTR
680 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
681 break;
682 sub = SET_SRC (sub);
683 if (GET_CODE (sub) == CONST_STRING)
685 value = XSTR (sub, 0);
686 goto found;
689 /* ??? It would be possible to handle this if we really tried.
690 It's not easy though, and I'm not going to bother until it
691 really proves necessary. */
692 error_at (elem->loc, "non-constant value for `predicable'");
693 return 0;
695 default:
696 gcc_unreachable ();
700 return predicable_default;
702 found:
703 /* Find out which value we're looking at. Multiple alternatives means at
704 least one is predicable. */
705 if (strchr (value, ',') != NULL)
706 return 1;
707 if (strcmp (value, predicable_true) == 0)
708 return 1;
709 if (strcmp (value, predicable_false) == 0)
710 return 0;
712 error_at (elem->loc, "unknown value `%s' for `predicable' attribute", value);
713 return 0;
716 /* Find attribute SUBST in ELEM and assign NEW_VALUE to it. */
717 static void
718 change_subst_attribute (struct queue_elem *elem,
719 struct queue_elem *subst_elem,
720 const char *new_value)
722 rtvec attrs_vec = XVEC (elem->data, 4);
723 const char *subst_name = XSTR (subst_elem->data, 0);
724 int i;
726 if (! attrs_vec)
727 return;
729 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
731 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
732 if (GET_CODE (cur_attr) != SET_ATTR)
733 continue;
734 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
736 XSTR (cur_attr, 1) = new_value;
737 return;
742 /* Return true if ELEM has the attribute with the name of DEFINE_SUBST
743 represented by SUBST_ELEM and this attribute has value SUBST_TRUE.
744 DEFINE_SUBST isn't applied to patterns without such attribute. In other
745 words, we suppose the default value of the attribute to be 'no' since it is
746 always generated automatically in read-rtl.c. */
747 static bool
748 has_subst_attribute (struct queue_elem *elem, struct queue_elem *subst_elem)
750 rtvec attrs_vec = XVEC (elem->data, 4);
751 const char *value, *subst_name = XSTR (subst_elem->data, 0);
752 int i;
754 if (! attrs_vec)
755 return false;
757 for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
759 rtx cur_attr = RTVEC_ELT (attrs_vec, i);
760 switch (GET_CODE (cur_attr))
762 case SET_ATTR:
763 if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
765 value = XSTR (cur_attr, 1);
766 goto found;
768 break;
770 case SET:
771 if (GET_CODE (SET_DEST (cur_attr)) != ATTR
772 || strcmp (XSTR (SET_DEST (cur_attr), 0), subst_name) != 0)
773 break;
774 cur_attr = SET_SRC (cur_attr);
775 if (GET_CODE (cur_attr) == CONST_STRING)
777 value = XSTR (cur_attr, 0);
778 goto found;
781 /* Only (set_attr "subst" "yes/no") and
782 (set (attr "subst" (const_string "yes/no")))
783 are currently allowed. */
784 error_at (elem->loc, "unsupported value for `%s'", subst_name);
785 return false;
787 case SET_ATTR_ALTERNATIVE:
788 error_at (elem->loc,
789 "%s: `set_attr_alternative' is unsupported by "
790 "`define_subst'", XSTR (elem->data, 0));
791 return false;
794 default:
795 gcc_unreachable ();
799 return false;
801 found:
802 if (strcmp (value, subst_true) == 0)
803 return true;
804 if (strcmp (value, subst_false) == 0)
805 return false;
807 error_at (elem->loc, "unknown value `%s' for `%s' attribute",
808 value, subst_name);
809 return false;
812 /* Compare RTL-template of original define_insn X to input RTL-template of
813 define_subst PT. Return 1 if the templates match, 0 otherwise.
814 During the comparison, the routine also fills global_array OPERAND_DATA. */
815 static bool
816 subst_pattern_match (rtx x, rtx pt, file_location loc)
818 RTX_CODE code, code_pt;
819 int i, j, len;
820 const char *fmt, *pred_name;
822 code = GET_CODE (x);
823 code_pt = GET_CODE (pt);
825 if (code_pt == MATCH_OPERAND)
827 /* MATCH_DUP, and MATCH_OP_DUP don't have a specified mode, so we
828 always accept them. */
829 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt)
830 && (code != MATCH_DUP && code != MATCH_OP_DUP))
831 return false; /* Modes don't match. */
833 if (code == MATCH_OPERAND)
835 pred_name = XSTR (pt, 1);
836 if (pred_name[0] != 0)
838 const struct pred_data *pred_pt = lookup_predicate (pred_name);
839 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
840 return false; /* Predicates don't match. */
844 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
845 operand_data[XINT (pt, 0)] = x;
846 return true;
849 if (code_pt == MATCH_OPERATOR)
851 int x_vecexp_pos = -1;
853 /* Compare modes. */
854 if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt))
855 return false;
857 /* In case X is also match_operator, compare predicates. */
858 if (code == MATCH_OPERATOR)
860 pred_name = XSTR (pt, 1);
861 if (pred_name[0] != 0)
863 const struct pred_data *pred_pt = lookup_predicate (pred_name);
864 if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
865 return false;
869 /* Compare operands.
870 MATCH_OPERATOR in input template could match in original template
871 either 1) MATCH_OPERAND, 2) UNSPEC, 3) ordinary operation (like PLUS).
872 In the first case operands are at (XVECEXP (x, 2, j)), in the second
873 - at (XVECEXP (x, 0, j)), in the last one - (XEXP (x, j)).
874 X_VECEXP_POS variable shows, where to look for these operands. */
875 if (code == UNSPEC
876 || code == UNSPEC_VOLATILE)
877 x_vecexp_pos = 0;
878 else if (code == MATCH_OPERATOR)
879 x_vecexp_pos = 2;
880 else
881 x_vecexp_pos = -1;
883 /* MATCH_OPERATOR or UNSPEC case. */
884 if (x_vecexp_pos >= 0)
886 /* Compare operands number in X and PT. */
887 if (XVECLEN (x, x_vecexp_pos) != XVECLEN (pt, 2))
888 return false;
889 for (j = 0; j < XVECLEN (pt, 2); j++)
890 if (!subst_pattern_match (XVECEXP (x, x_vecexp_pos, j),
891 XVECEXP (pt, 2, j), loc))
892 return false;
895 /* Ordinary operator. */
896 else
898 /* Compare operands number in X and PT.
899 We count operands differently for X and PT since we compare
900 an operator (with operands directly in RTX) and MATCH_OPERATOR
901 (that has a vector with operands). */
902 if (GET_RTX_LENGTH (code) != XVECLEN (pt, 2))
903 return false;
904 for (j = 0; j < XVECLEN (pt, 2); j++)
905 if (!subst_pattern_match (XEXP (x, j), XVECEXP (pt, 2, j), loc))
906 return false;
909 /* Store the operand to OPERAND_DATA array. */
910 gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
911 operand_data[XINT (pt, 0)] = x;
912 return true;
915 if (code_pt == MATCH_PAR_DUP
916 || code_pt == MATCH_DUP
917 || code_pt == MATCH_OP_DUP
918 || code_pt == MATCH_SCRATCH
919 || code_pt == MATCH_PARALLEL)
921 /* Currently interface for these constructions isn't defined -
922 probably they aren't needed in input template of define_subst at all.
923 So, for now their usage in define_subst is forbidden. */
924 error_at (loc, "%s cannot be used in define_subst",
925 GET_RTX_NAME (code_pt));
928 gcc_assert (code != MATCH_PAR_DUP
929 && code_pt != MATCH_DUP
930 && code_pt != MATCH_OP_DUP
931 && code_pt != MATCH_SCRATCH
932 && code_pt != MATCH_PARALLEL
933 && code_pt != MATCH_OPERAND
934 && code_pt != MATCH_OPERATOR);
935 /* If PT is none of the handled above, then we match only expressions with
936 the same code in X. */
937 if (code != code_pt)
938 return false;
940 fmt = GET_RTX_FORMAT (code_pt);
941 len = GET_RTX_LENGTH (code_pt);
943 for (i = 0; i < len; i++)
945 if (fmt[i] == '0')
946 break;
948 switch (fmt[i])
950 case 'r': case 'p': case 'i': case 'w': case 's':
951 continue;
953 case 'e': case 'u':
954 if (!subst_pattern_match (XEXP (x, i), XEXP (pt, i), loc))
955 return false;
956 break;
957 case 'E':
959 if (XVECLEN (x, i) != XVECLEN (pt, i))
960 return false;
961 for (j = 0; j < XVECLEN (pt, i); j++)
962 if (!subst_pattern_match (XVECEXP (x, i, j),
963 XVECEXP (pt, i, j), loc))
964 return false;
965 break;
967 default:
968 gcc_unreachable ();
972 return true;
975 /* Examine the attribute "predicable"; discover its boolean values
976 and its default. */
978 static void
979 identify_predicable_attribute (void)
981 struct queue_elem *elem;
982 char *p_true, *p_false;
983 const char *value;
985 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
986 for (elem = define_attr_queue; elem ; elem = elem->next)
987 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
988 goto found;
990 error_at (define_cond_exec_queue->loc,
991 "attribute `predicable' not defined");
992 return;
994 found:
995 value = XSTR (elem->data, 1);
996 p_false = xstrdup (value);
997 p_true = strchr (p_false, ',');
998 if (p_true == NULL || strchr (++p_true, ',') != NULL)
1000 error_at (elem->loc, "attribute `predicable' is not a boolean");
1001 free (p_false);
1002 return;
1004 p_true[-1] = '\0';
1006 predicable_true = p_true;
1007 predicable_false = p_false;
1009 switch (GET_CODE (XEXP (elem->data, 2)))
1011 case CONST_STRING:
1012 value = XSTR (XEXP (elem->data, 2), 0);
1013 break;
1015 case CONST:
1016 error_at (elem->loc, "attribute `predicable' cannot be const");
1017 free (p_false);
1018 return;
1020 default:
1021 error_at (elem->loc,
1022 "attribute `predicable' must have a constant default");
1023 free (p_false);
1024 return;
1027 if (strcmp (value, p_true) == 0)
1028 predicable_default = 1;
1029 else if (strcmp (value, p_false) == 0)
1030 predicable_default = 0;
1031 else
1033 error_at (elem->loc, "unknown value `%s' for `predicable' attribute",
1034 value);
1035 free (p_false);
1039 /* Return the number of alternatives in constraint S. */
1041 static int
1042 n_alternatives (const char *s)
1044 int n = 1;
1046 if (s)
1047 while (*s)
1048 n += (*s++ == ',');
1050 return n;
1053 /* The routine scans rtl PATTERN, find match_operand in it and counts
1054 number of alternatives. If PATTERN contains several match_operands
1055 with different number of alternatives, error is emitted, and the
1056 routine returns 0. If all match_operands in PATTERN have the same
1057 number of alternatives, it's stored in N_ALT, and the routine returns 1.
1058 LOC is the location of PATTERN, for error reporting. */
1059 static int
1060 get_alternatives_number (rtx pattern, int *n_alt, file_location loc)
1062 const char *fmt;
1063 enum rtx_code code;
1064 int i, j, len;
1066 if (!n_alt)
1067 return 0;
1069 code = GET_CODE (pattern);
1070 switch (code)
1072 case MATCH_OPERAND:
1073 i = n_alternatives (XSTR (pattern, 2));
1074 /* n_alternatives returns 1 if constraint string is empty -
1075 here we fix it up. */
1076 if (!*(XSTR (pattern, 2)))
1077 i = 0;
1078 if (*n_alt <= 0)
1079 *n_alt = i;
1081 else if (i && i != *n_alt)
1083 error_at (loc, "wrong number of alternatives in operand %d",
1084 XINT (pattern, 0));
1085 return 0;
1088 default:
1089 break;
1092 fmt = GET_RTX_FORMAT (code);
1093 len = GET_RTX_LENGTH (code);
1094 for (i = 0; i < len; i++)
1096 switch (fmt[i])
1098 case 'e': case 'u':
1099 if (!get_alternatives_number (XEXP (pattern, i), n_alt, loc))
1100 return 0;
1101 break;
1103 case 'V':
1104 if (XVEC (pattern, i) == NULL)
1105 break;
1106 /* FALLTHRU */
1108 case 'E':
1109 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1110 if (!get_alternatives_number (XVECEXP (pattern, i, j), n_alt, loc))
1111 return 0;
1112 break;
1114 case 'r': case 'p': case 'i': case 'w':
1115 case '0': case 's': case 'S': case 'T':
1116 break;
1118 default:
1119 gcc_unreachable ();
1122 return 1;
1125 /* Determine how many alternatives there are in INSN, and how many
1126 operands. */
1128 static void
1129 collect_insn_data (rtx pattern, int *palt, int *pmax)
1131 const char *fmt;
1132 enum rtx_code code;
1133 int i, j, len;
1135 code = GET_CODE (pattern);
1136 switch (code)
1138 case MATCH_OPERAND:
1139 case MATCH_SCRATCH:
1140 i = n_alternatives (XSTR (pattern, code == MATCH_SCRATCH ? 1 : 2));
1141 *palt = (i > *palt ? i : *palt);
1142 /* Fall through. */
1144 case MATCH_OPERATOR:
1145 case MATCH_PARALLEL:
1146 i = XINT (pattern, 0);
1147 if (i > *pmax)
1148 *pmax = i;
1149 break;
1151 default:
1152 break;
1155 fmt = GET_RTX_FORMAT (code);
1156 len = GET_RTX_LENGTH (code);
1157 for (i = 0; i < len; i++)
1159 switch (fmt[i])
1161 case 'e': case 'u':
1162 collect_insn_data (XEXP (pattern, i), palt, pmax);
1163 break;
1165 case 'V':
1166 if (XVEC (pattern, i) == NULL)
1167 break;
1168 /* Fall through. */
1169 case 'E':
1170 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1171 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
1172 break;
1174 case 'r': case 'p': case 'i': case 'w':
1175 case '0': case 's': case 'S': case 'T':
1176 break;
1178 default:
1179 gcc_unreachable ();
1184 static rtx
1185 alter_predicate_for_insn (rtx pattern, int alt, int max_op,
1186 file_location loc)
1188 const char *fmt;
1189 enum rtx_code code;
1190 int i, j, len;
1192 code = GET_CODE (pattern);
1193 switch (code)
1195 case MATCH_OPERAND:
1197 const char *c = XSTR (pattern, 2);
1199 if (n_alternatives (c) != 1)
1201 error_at (loc, "too many alternatives for operand %d",
1202 XINT (pattern, 0));
1203 return NULL;
1206 /* Replicate C as needed to fill out ALT alternatives. */
1207 if (c && *c && alt > 1)
1209 size_t c_len = strlen (c);
1210 size_t len = alt * (c_len + 1);
1211 char *new_c = XNEWVEC (char, len);
1213 memcpy (new_c, c, c_len);
1214 for (i = 1; i < alt; ++i)
1216 new_c[i * (c_len + 1) - 1] = ',';
1217 memcpy (&new_c[i * (c_len + 1)], c, c_len);
1219 new_c[len - 1] = '\0';
1220 XSTR (pattern, 2) = new_c;
1223 /* Fall through. */
1225 case MATCH_OPERATOR:
1226 case MATCH_SCRATCH:
1227 case MATCH_PARALLEL:
1228 XINT (pattern, 0) += max_op;
1229 break;
1231 default:
1232 break;
1235 fmt = GET_RTX_FORMAT (code);
1236 len = GET_RTX_LENGTH (code);
1237 for (i = 0; i < len; i++)
1239 rtx r;
1241 switch (fmt[i])
1243 case 'e': case 'u':
1244 r = alter_predicate_for_insn (XEXP (pattern, i), alt, max_op, loc);
1245 if (r == NULL)
1246 return r;
1247 break;
1249 case 'E':
1250 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1252 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
1253 alt, max_op, loc);
1254 if (r == NULL)
1255 return r;
1257 break;
1259 case 'r': case 'p': case 'i': case 'w': case '0': case 's':
1260 break;
1262 default:
1263 gcc_unreachable ();
1267 return pattern;
1270 /* Duplicate constraints in PATTERN. If pattern is from original
1271 rtl-template, we need to duplicate each alternative - for that we
1272 need to use duplicate_each_alternative () as a functor ALTER.
1273 If pattern is from output-pattern of define_subst, we need to
1274 duplicate constraints in another way - with duplicate_alternatives ().
1275 N_DUP is multiplication factor. */
1276 static rtx
1277 alter_constraints (rtx pattern, int n_dup, constraints_handler_t alter)
1279 const char *fmt;
1280 enum rtx_code code;
1281 int i, j, len;
1283 code = GET_CODE (pattern);
1284 switch (code)
1286 case MATCH_OPERAND:
1287 XSTR (pattern, 2) = alter (XSTR (pattern, 2), n_dup);
1288 break;
1290 default:
1291 break;
1294 fmt = GET_RTX_FORMAT (code);
1295 len = GET_RTX_LENGTH (code);
1296 for (i = 0; i < len; i++)
1298 rtx r;
1300 switch (fmt[i])
1302 case 'e': case 'u':
1303 r = alter_constraints (XEXP (pattern, i), n_dup, alter);
1304 if (r == NULL)
1305 return r;
1306 break;
1308 case 'E':
1309 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1311 r = alter_constraints (XVECEXP (pattern, i, j), n_dup, alter);
1312 if (r == NULL)
1313 return r;
1315 break;
1317 case 'r': case 'p': case 'i': case 'w': case '0': case 's':
1318 break;
1320 default:
1321 break;
1325 return pattern;
1328 static const char *
1329 alter_test_for_insn (struct queue_elem *ce_elem,
1330 struct queue_elem *insn_elem)
1332 return rtx_reader_ptr->join_c_conditions (XSTR (ce_elem->data, 1),
1333 XSTR (insn_elem->data, 2));
1336 /* Modify VAL, which is an attribute expression for the "enabled" attribute,
1337 to take "ce_enabled" into account. Return the new expression. */
1338 static rtx
1339 modify_attr_enabled_ce (rtx val)
1341 rtx eq_attr, str;
1342 rtx ite;
1343 eq_attr = rtx_alloc (EQ_ATTR);
1344 ite = rtx_alloc (IF_THEN_ELSE);
1345 str = rtx_alloc (CONST_STRING);
1347 XSTR (eq_attr, 0) = "ce_enabled";
1348 XSTR (eq_attr, 1) = "yes";
1349 XSTR (str, 0) = "no";
1350 XEXP (ite, 0) = eq_attr;
1351 XEXP (ite, 1) = val;
1352 XEXP (ite, 2) = str;
1354 return ite;
1357 /* Alter the attribute vector of INSN, which is a COND_EXEC variant created
1358 from a define_insn pattern. We must modify the "predicable" attribute
1359 to be named "ce_enabled", and also change any "enabled" attribute that's
1360 present so that it takes ce_enabled into account.
1361 We rely on the fact that INSN was created with copy_rtx, and modify data
1362 in-place. */
1364 static void
1365 alter_attrs_for_insn (rtx insn)
1367 static bool global_changes_made = false;
1368 rtvec vec = XVEC (insn, 4);
1369 rtvec new_vec;
1370 rtx val, set;
1371 int num_elem;
1372 int predicable_idx = -1;
1373 int enabled_idx = -1;
1374 int i;
1376 if (! vec)
1377 return;
1379 num_elem = GET_NUM_ELEM (vec);
1380 for (i = num_elem - 1; i >= 0; --i)
1382 rtx sub = RTVEC_ELT (vec, i);
1383 switch (GET_CODE (sub))
1385 case SET_ATTR:
1386 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1388 predicable_idx = i;
1389 XSTR (sub, 0) = "ce_enabled";
1391 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1393 enabled_idx = i;
1394 XSTR (sub, 0) = "nonce_enabled";
1396 break;
1398 case SET_ATTR_ALTERNATIVE:
1399 if (strcmp (XSTR (sub, 0), "predicable") == 0)
1400 /* We already give an error elsewhere. */
1401 return;
1402 else if (strcmp (XSTR (sub, 0), "enabled") == 0)
1404 enabled_idx = i;
1405 XSTR (sub, 0) = "nonce_enabled";
1407 break;
1409 case SET:
1410 if (GET_CODE (SET_DEST (sub)) != ATTR)
1411 break;
1412 if (strcmp (XSTR (SET_DEST (sub), 0), "predicable") == 0)
1414 sub = SET_SRC (sub);
1415 if (GET_CODE (sub) == CONST_STRING)
1417 predicable_idx = i;
1418 XSTR (sub, 0) = "ce_enabled";
1420 else
1421 /* We already give an error elsewhere. */
1422 return;
1423 break;
1425 if (strcmp (XSTR (SET_DEST (sub), 0), "enabled") == 0)
1427 enabled_idx = i;
1428 XSTR (SET_DEST (sub), 0) = "nonce_enabled";
1430 break;
1432 default:
1433 gcc_unreachable ();
1436 if (predicable_idx == -1)
1437 return;
1439 if (!global_changes_made)
1441 struct queue_elem *elem;
1443 global_changes_made = true;
1444 add_define_attr ("ce_enabled");
1445 add_define_attr ("nonce_enabled");
1447 for (elem = define_attr_queue; elem ; elem = elem->next)
1448 if (strcmp (XSTR (elem->data, 0), "enabled") == 0)
1450 XEXP (elem->data, 2)
1451 = modify_attr_enabled_ce (XEXP (elem->data, 2));
1454 if (enabled_idx == -1)
1455 return;
1457 new_vec = rtvec_alloc (num_elem + 1);
1458 for (i = 0; i < num_elem; i++)
1459 RTVEC_ELT (new_vec, i) = RTVEC_ELT (vec, i);
1460 val = rtx_alloc (IF_THEN_ELSE);
1461 XEXP (val, 0) = rtx_alloc (EQ_ATTR);
1462 XEXP (val, 1) = rtx_alloc (CONST_STRING);
1463 XEXP (val, 2) = rtx_alloc (CONST_STRING);
1464 XSTR (XEXP (val, 0), 0) = "nonce_enabled";
1465 XSTR (XEXP (val, 0), 1) = "yes";
1466 XSTR (XEXP (val, 1), 0) = "yes";
1467 XSTR (XEXP (val, 2), 0) = "no";
1468 set = rtx_alloc (SET);
1469 SET_DEST (set) = rtx_alloc (ATTR);
1470 XSTR (SET_DEST (set), 0) = "enabled";
1471 SET_SRC (set) = modify_attr_enabled_ce (val);
1472 RTVEC_ELT (new_vec, i) = set;
1473 XVEC (insn, 4) = new_vec;
1476 /* As number of constraints is changed after define_subst, we need to
1477 process attributes as well - we need to duplicate them the same way
1478 that we duplicated constraints in original pattern
1479 ELEM is a queue element, containing our rtl-template,
1480 N_DUP - multiplication factor. */
1481 static void
1482 alter_attrs_for_subst_insn (struct queue_elem * elem, int n_dup)
1484 rtvec vec = XVEC (elem->data, 4);
1485 int num_elem;
1486 int i;
1488 if (n_dup < 2 || ! vec)
1489 return;
1491 num_elem = GET_NUM_ELEM (vec);
1492 for (i = num_elem - 1; i >= 0; --i)
1494 rtx sub = RTVEC_ELT (vec, i);
1495 switch (GET_CODE (sub))
1497 case SET_ATTR:
1498 if (strchr (XSTR (sub, 1), ',') != NULL)
1499 XSTR (sub, 1) = duplicate_alternatives (XSTR (sub, 1), n_dup);
1500 break;
1502 case SET_ATTR_ALTERNATIVE:
1503 case SET:
1504 error_at (elem->loc,
1505 "%s: `define_subst' does not support attributes "
1506 "assigned by `set' and `set_attr_alternative'",
1507 XSTR (elem->data, 0));
1508 return;
1510 default:
1511 gcc_unreachable ();
1516 /* Adjust all of the operand numbers in SRC to match the shift they'll
1517 get from an operand displacement of DISP. Return a pointer after the
1518 adjusted string. */
1520 static char *
1521 shift_output_template (char *dest, const char *src, int disp)
1523 while (*src)
1525 char c = *src++;
1526 *dest++ = c;
1527 if (c == '%')
1529 c = *src++;
1530 if (ISDIGIT ((unsigned char) c))
1531 c += disp;
1532 else if (ISALPHA (c))
1534 *dest++ = c;
1535 c = *src++ + disp;
1537 *dest++ = c;
1541 return dest;
1544 static const char *
1545 alter_output_for_insn (struct queue_elem *ce_elem,
1546 struct queue_elem *insn_elem,
1547 int alt, int max_op)
1549 const char *ce_out, *insn_out;
1550 char *result, *p;
1551 size_t len, ce_len, insn_len;
1553 /* ??? Could coordinate with genoutput to not duplicate code here. */
1555 ce_out = XSTR (ce_elem->data, 2);
1556 insn_out = XTMPL (insn_elem->data, 3);
1557 if (!ce_out || *ce_out == '\0')
1558 return insn_out;
1560 ce_len = strlen (ce_out);
1561 insn_len = strlen (insn_out);
1563 if (*insn_out == '*')
1564 /* You must take care of the predicate yourself. */
1565 return insn_out;
1567 if (*insn_out == '@')
1569 len = (ce_len + 1) * alt + insn_len + 1;
1570 p = result = XNEWVEC (char, len);
1575 *p++ = *insn_out++;
1576 while (ISSPACE ((unsigned char) *insn_out));
1578 if (*insn_out != '#')
1580 p = shift_output_template (p, ce_out, max_op);
1581 *p++ = ' ';
1585 *p++ = *insn_out++;
1586 while (*insn_out && *insn_out != '\n');
1588 while (*insn_out);
1589 *p = '\0';
1591 else
1593 len = ce_len + 1 + insn_len + 1;
1594 result = XNEWVEC (char, len);
1596 p = shift_output_template (result, ce_out, max_op);
1597 *p++ = ' ';
1598 memcpy (p, insn_out, insn_len + 1);
1601 return result;
1604 /* From string STR "a,b,c" produce "a,b,c,a,b,c,a,b,c", i.e. original
1605 string, duplicated N_DUP times. */
1607 static const char *
1608 duplicate_alternatives (const char * str, int n_dup)
1610 int i, len, new_len;
1611 char *result, *sp;
1612 const char *cp;
1614 if (n_dup < 2)
1615 return str;
1617 while (ISSPACE (*str))
1618 str++;
1620 if (*str == '\0')
1621 return str;
1623 cp = str;
1624 len = strlen (str);
1625 new_len = (len + 1) * n_dup;
1627 sp = result = XNEWVEC (char, new_len);
1629 /* Global modifier characters mustn't be duplicated: skip if found. */
1630 if (*cp == '=' || *cp == '+' || *cp == '%')
1632 *sp++ = *cp++;
1633 len--;
1636 /* Copy original constraints N_DUP times. */
1637 for (i = 0; i < n_dup; i++, sp += len+1)
1639 memcpy (sp, cp, len);
1640 *(sp+len) = (i == n_dup - 1) ? '\0' : ',';
1643 return result;
1646 /* From string STR "a,b,c" produce "a,a,a,b,b,b,c,c,c", i.e. string where
1647 each alternative from the original string is duplicated N_DUP times. */
1648 static const char *
1649 duplicate_each_alternative (const char * str, int n_dup)
1651 int i, len, new_len;
1652 char *result, *sp, *ep, *cp;
1654 if (n_dup < 2)
1655 return str;
1657 while (ISSPACE (*str))
1658 str++;
1660 if (*str == '\0')
1661 return str;
1663 cp = xstrdup (str);
1665 new_len = (strlen (cp) + 1) * n_dup;
1667 sp = result = XNEWVEC (char, new_len);
1669 /* Global modifier characters mustn't be duplicated: skip if found. */
1670 if (*cp == '=' || *cp == '+' || *cp == '%')
1671 *sp++ = *cp++;
1675 if ((ep = strchr (cp, ',')) != NULL)
1676 *ep++ = '\0';
1677 len = strlen (cp);
1679 /* Copy a constraint N_DUP times. */
1680 for (i = 0; i < n_dup; i++, sp += len + 1)
1682 memcpy (sp, cp, len);
1683 *(sp+len) = (ep == NULL && i == n_dup - 1) ? '\0' : ',';
1686 cp = ep;
1688 while (cp != NULL);
1690 return result;
1693 /* Alter the output of INSN whose pattern was modified by
1694 DEFINE_SUBST. We must replicate output strings according
1695 to the new number of alternatives ALT in substituted pattern.
1696 If ALT equals 1, output has one alternative or defined by C
1697 code, then output is returned without any changes. */
1699 static const char *
1700 alter_output_for_subst_insn (rtx insn, int alt)
1702 const char *insn_out, *old_out;
1703 char *new_out, *cp;
1704 size_t old_len, new_len;
1705 int j;
1707 insn_out = XTMPL (insn, 3);
1709 if (alt < 2 || *insn_out != '@')
1710 return insn_out;
1712 old_out = insn_out + 1;
1713 while (ISSPACE (*old_out))
1714 old_out++;
1715 old_len = strlen (old_out);
1717 new_len = alt * (old_len + 1) + 1;
1719 new_out = XNEWVEC (char, new_len);
1720 new_out[0] = '@';
1722 for (j = 0, cp = new_out + 1; j < alt; j++, cp += old_len + 1)
1724 memcpy (cp, old_out, old_len);
1725 cp[old_len] = (j == alt - 1) ? '\0' : '\n';
1728 return new_out;
1731 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
1733 static void
1734 process_one_cond_exec (struct queue_elem *ce_elem)
1736 struct queue_elem *insn_elem;
1737 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
1739 int alternatives, max_operand;
1740 rtx pred, insn, pattern, split;
1741 char *new_name;
1742 int i;
1744 if (! is_predicable (insn_elem))
1745 continue;
1747 alternatives = 1;
1748 max_operand = -1;
1749 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
1750 max_operand += 1;
1752 if (XVECLEN (ce_elem->data, 0) != 1)
1754 error_at (ce_elem->loc, "too many patterns in predicate");
1755 return;
1758 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
1759 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
1760 ce_elem->loc);
1761 if (pred == NULL)
1762 return;
1764 /* Construct a new pattern for the new insn. */
1765 insn = copy_rtx (insn_elem->data);
1766 new_name = XNEWVAR (char, strlen XSTR (insn_elem->data, 0) + 4);
1767 sprintf (new_name, "*p %s", XSTR (insn_elem->data, 0));
1768 XSTR (insn, 0) = new_name;
1769 pattern = rtx_alloc (COND_EXEC);
1770 XEXP (pattern, 0) = pred;
1771 XEXP (pattern, 1) = add_implicit_parallel (XVEC (insn, 1));
1772 XVEC (insn, 1) = rtvec_alloc (1);
1773 XVECEXP (insn, 1, 0) = pattern;
1775 if (XVEC (ce_elem->data, 3) != NULL)
1777 rtvec attributes = rtvec_alloc (XVECLEN (insn, 4)
1778 + XVECLEN (ce_elem->data, 3));
1779 int i = 0;
1780 int j = 0;
1781 for (i = 0; i < XVECLEN (insn, 4); i++)
1782 RTVEC_ELT (attributes, i) = XVECEXP (insn, 4, i);
1784 for (j = 0; j < XVECLEN (ce_elem->data, 3); j++, i++)
1785 RTVEC_ELT (attributes, i) = XVECEXP (ce_elem->data, 3, j);
1787 XVEC (insn, 4) = attributes;
1790 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
1791 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
1792 alternatives, max_operand);
1793 alter_attrs_for_insn (insn);
1795 /* Put the new pattern on the `other' list so that it
1796 (a) is not reprocessed by other define_cond_exec patterns
1797 (b) appears after all normal define_insn patterns.
1799 ??? B is debatable. If one has normal insns that match
1800 cond_exec patterns, they will be preferred over these
1801 generated patterns. Whether this matters in practice, or if
1802 it's a good thing, or whether we should thread these new
1803 patterns into the define_insn chain just after their generator
1804 is something we'll have to experiment with. */
1806 queue_pattern (insn, &other_tail, insn_elem->loc);
1808 if (!insn_elem->split)
1809 continue;
1811 /* If the original insn came from a define_insn_and_split,
1812 generate a new split to handle the predicated insn. */
1813 split = copy_rtx (insn_elem->split->data);
1814 /* Predicate the pattern matched by the split. */
1815 pattern = rtx_alloc (COND_EXEC);
1816 XEXP (pattern, 0) = pred;
1817 XEXP (pattern, 1) = add_implicit_parallel (XVEC (split, 0));
1818 XVEC (split, 0) = rtvec_alloc (1);
1819 XVECEXP (split, 0, 0) = pattern;
1821 /* Predicate all of the insns generated by the split. */
1822 for (i = 0; i < XVECLEN (split, 2); i++)
1824 pattern = rtx_alloc (COND_EXEC);
1825 XEXP (pattern, 0) = pred;
1826 XEXP (pattern, 1) = XVECEXP (split, 2, i);
1827 XVECEXP (split, 2, i) = pattern;
1829 /* Add the new split to the queue. */
1830 queue_pattern (split, &other_tail, insn_elem->split->loc);
1834 /* Try to apply define_substs to the given ELEM.
1835 Only define_substs, specified via attributes would be applied.
1836 If attribute, requiring define_subst, is set, but no define_subst
1837 was applied, ELEM would be deleted. */
1839 static void
1840 process_substs_on_one_elem (struct queue_elem *elem,
1841 struct queue_elem *queue)
1843 struct queue_elem *subst_elem;
1844 int i, j, patterns_match;
1846 for (subst_elem = define_subst_queue;
1847 subst_elem; subst_elem = subst_elem->next)
1849 int alternatives, alternatives_subst;
1850 rtx subst_pattern;
1851 rtvec subst_pattern_vec;
1853 if (!has_subst_attribute (elem, subst_elem))
1854 continue;
1856 /* Compare original rtl-pattern from define_insn with input
1857 pattern from define_subst.
1858 Also, check if numbers of alternatives are the same in all
1859 match_operands. */
1860 if (XVECLEN (elem->data, 1) != XVECLEN (subst_elem->data, 1))
1861 continue;
1862 patterns_match = 1;
1863 alternatives = -1;
1864 alternatives_subst = -1;
1865 for (j = 0; j < XVECLEN (elem->data, 1); j++)
1867 if (!subst_pattern_match (XVECEXP (elem->data, 1, j),
1868 XVECEXP (subst_elem->data, 1, j),
1869 subst_elem->loc))
1871 patterns_match = 0;
1872 break;
1875 if (!get_alternatives_number (XVECEXP (elem->data, 1, j),
1876 &alternatives, subst_elem->loc))
1878 patterns_match = 0;
1879 break;
1883 /* Check if numbers of alternatives are the same in all
1884 match_operands in output template of define_subst. */
1885 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1887 if (!get_alternatives_number (XVECEXP (subst_elem->data, 3, j),
1888 &alternatives_subst,
1889 subst_elem->loc))
1891 patterns_match = 0;
1892 break;
1896 if (!patterns_match)
1897 continue;
1899 /* Clear array in which we save occupied indexes of operands. */
1900 memset (used_operands_numbers, 0, sizeof (used_operands_numbers));
1902 /* Create a pattern, based on the output one from define_subst. */
1903 subst_pattern_vec = rtvec_alloc (XVECLEN (subst_elem->data, 3));
1904 for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
1906 subst_pattern = copy_rtx (XVECEXP (subst_elem->data, 3, j));
1908 /* Duplicate constraints in substitute-pattern. */
1909 subst_pattern = alter_constraints (subst_pattern, alternatives,
1910 duplicate_each_alternative);
1912 subst_pattern = adjust_operands_numbers (subst_pattern);
1914 /* Substitute match_dup and match_op_dup in the new pattern and
1915 duplicate constraints. */
1916 subst_pattern = subst_dup (subst_pattern, alternatives,
1917 alternatives_subst);
1919 replace_duplicating_operands_in_pattern (subst_pattern);
1921 /* We don't need any constraints in DEFINE_EXPAND. */
1922 if (GET_CODE (elem->data) == DEFINE_EXPAND)
1923 remove_constraints (subst_pattern);
1925 RTVEC_ELT (subst_pattern_vec, j) = subst_pattern;
1927 XVEC (elem->data, 1) = subst_pattern_vec;
1929 for (i = 0; i < MAX_OPERANDS; i++)
1930 match_operand_entries_in_pattern[i] = NULL;
1932 if (GET_CODE (elem->data) == DEFINE_INSN)
1934 XTMPL (elem->data, 3) =
1935 alter_output_for_subst_insn (elem->data, alternatives_subst);
1936 alter_attrs_for_subst_insn (elem, alternatives_subst);
1939 /* Recalculate condition, joining conditions from original and
1940 DEFINE_SUBST input patterns. */
1941 XSTR (elem->data, 2)
1942 = rtx_reader_ptr->join_c_conditions (XSTR (subst_elem->data, 2),
1943 XSTR (elem->data, 2));
1944 /* Mark that subst was applied by changing attribute from "yes"
1945 to "no". */
1946 change_subst_attribute (elem, subst_elem, subst_false);
1949 /* If ELEM contains a subst attribute with value "yes", then we
1950 expected that a subst would be applied, but it wasn't - so,
1951 we need to remove that elementto avoid duplicating. */
1952 for (subst_elem = define_subst_queue;
1953 subst_elem; subst_elem = subst_elem->next)
1955 if (has_subst_attribute (elem, subst_elem))
1957 remove_from_queue (elem, &queue);
1958 return;
1963 /* This is a subroutine of mark_operands_used_in_match_dup.
1964 This routine is marks all MATCH_OPERANDs inside PATTERN as occupied. */
1965 static void
1966 mark_operands_from_match_dup (rtx pattern)
1968 const char *fmt;
1969 int i, j, len, opno;
1971 if (GET_CODE (pattern) == MATCH_OPERAND
1972 || GET_CODE (pattern) == MATCH_OPERATOR
1973 || GET_CODE (pattern) == MATCH_PARALLEL)
1975 opno = XINT (pattern, 0);
1976 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
1977 used_operands_numbers [opno] = 1;
1979 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
1980 len = GET_RTX_LENGTH (GET_CODE (pattern));
1981 for (i = 0; i < len; i++)
1983 switch (fmt[i])
1985 case 'e': case 'u':
1986 mark_operands_from_match_dup (XEXP (pattern, i));
1987 break;
1988 case 'E':
1989 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
1990 mark_operands_from_match_dup (XVECEXP (pattern, i, j));
1991 break;
1996 /* This is a subroutine of adjust_operands_numbers.
1997 It goes through all expressions in PATTERN and when MATCH_DUP is
1998 met, all MATCH_OPERANDs inside it is marked as occupied. The
1999 process of marking is done by routin mark_operands_from_match_dup. */
2000 static void
2001 mark_operands_used_in_match_dup (rtx pattern)
2003 const char *fmt;
2004 int i, j, len, opno;
2006 if (GET_CODE (pattern) == MATCH_DUP)
2008 opno = XINT (pattern, 0);
2009 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2010 mark_operands_from_match_dup (operand_data[opno]);
2011 return;
2013 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2014 len = GET_RTX_LENGTH (GET_CODE (pattern));
2015 for (i = 0; i < len; i++)
2017 switch (fmt[i])
2019 case 'e': case 'u':
2020 mark_operands_used_in_match_dup (XEXP (pattern, i));
2021 break;
2022 case 'E':
2023 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2024 mark_operands_used_in_match_dup (XVECEXP (pattern, i, j));
2025 break;
2030 /* This is subroutine of renumerate_operands_in_pattern.
2031 It finds first not-occupied operand-index. */
2032 static int
2033 find_first_unused_number_of_operand ()
2035 int i;
2036 for (i = 0; i < MAX_OPERANDS; i++)
2037 if (!used_operands_numbers[i])
2038 return i;
2039 return MAX_OPERANDS;
2042 /* This is subroutine of adjust_operands_numbers.
2043 It visits all expressions in PATTERN and assigns not-occupied
2044 operand indexes to MATCH_OPERANDs and MATCH_OPERATORs of this
2045 PATTERN. */
2046 static void
2047 renumerate_operands_in_pattern (rtx pattern)
2049 const char *fmt;
2050 enum rtx_code code;
2051 int i, j, len, new_opno;
2052 code = GET_CODE (pattern);
2054 if (code == MATCH_OPERAND
2055 || code == MATCH_OPERATOR)
2057 new_opno = find_first_unused_number_of_operand ();
2058 gcc_assert (new_opno >= 0 && new_opno < MAX_OPERANDS);
2059 XINT (pattern, 0) = new_opno;
2060 used_operands_numbers [new_opno] = 1;
2063 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2064 len = GET_RTX_LENGTH (GET_CODE (pattern));
2065 for (i = 0; i < len; i++)
2067 switch (fmt[i])
2069 case 'e': case 'u':
2070 renumerate_operands_in_pattern (XEXP (pattern, i));
2071 break;
2072 case 'E':
2073 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2074 renumerate_operands_in_pattern (XVECEXP (pattern, i, j));
2075 break;
2080 /* If output pattern of define_subst contains MATCH_DUP, then this
2081 expression would be replaced with the pattern, matched with
2082 MATCH_OPERAND from input pattern. This pattern could contain any
2083 number of MATCH_OPERANDs, MATCH_OPERATORs etc., so it's possible
2084 that a MATCH_OPERAND from output_pattern (if any) would have the
2085 same number, as MATCH_OPERAND from copied pattern. To avoid such
2086 indexes overlapping, we assign new indexes to MATCH_OPERANDs,
2087 laying in the output pattern outside of MATCH_DUPs. */
2088 static rtx
2089 adjust_operands_numbers (rtx pattern)
2091 mark_operands_used_in_match_dup (pattern);
2093 renumerate_operands_in_pattern (pattern);
2095 return pattern;
2098 /* Generate RTL expression
2099 (match_dup OPNO)
2101 static rtx
2102 generate_match_dup (int opno)
2104 rtx return_rtx = rtx_alloc (MATCH_DUP);
2105 PUT_CODE (return_rtx, MATCH_DUP);
2106 XINT (return_rtx, 0) = opno;
2107 return return_rtx;
2110 /* This routine checks all match_operands in PATTERN and if some of
2111 have the same index, it replaces all of them except the first one to
2112 match_dup.
2113 Usually, match_operands with the same indexes are forbidden, but
2114 after define_subst copy an RTL-expression from original template,
2115 indexes of existed and just-copied match_operands could coincide.
2116 To fix it, we replace one of them with match_dup. */
2117 static rtx
2118 replace_duplicating_operands_in_pattern (rtx pattern)
2120 const char *fmt;
2121 int i, j, len, opno;
2122 rtx mdup;
2124 if (GET_CODE (pattern) == MATCH_OPERAND)
2126 opno = XINT (pattern, 0);
2127 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2128 if (match_operand_entries_in_pattern[opno] == NULL)
2130 match_operand_entries_in_pattern[opno] = pattern;
2131 return NULL;
2133 else
2135 /* Compare predicates before replacing with match_dup. */
2136 if (strcmp (XSTR (pattern, 1),
2137 XSTR (match_operand_entries_in_pattern[opno], 1)))
2139 error ("duplicated match_operands with different predicates were"
2140 " found.");
2141 return NULL;
2143 return generate_match_dup (opno);
2146 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2147 len = GET_RTX_LENGTH (GET_CODE (pattern));
2148 for (i = 0; i < len; i++)
2150 switch (fmt[i])
2152 case 'e': case 'u':
2153 mdup = replace_duplicating_operands_in_pattern (XEXP (pattern, i));
2154 if (mdup)
2155 XEXP (pattern, i) = mdup;
2156 break;
2157 case 'E':
2158 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2160 mdup =
2161 replace_duplicating_operands_in_pattern (XVECEXP
2162 (pattern, i, j));
2163 if (mdup)
2164 XVECEXP (pattern, i, j) = mdup;
2166 break;
2169 return NULL;
2172 /* The routine modifies given input PATTERN of define_subst, replacing
2173 MATCH_DUP and MATCH_OP_DUP with operands from define_insn original
2174 pattern, whose operands are stored in OPERAND_DATA array.
2175 It also duplicates constraints in operands - constraints from
2176 define_insn operands are duplicated N_SUBST_ALT times, constraints
2177 from define_subst operands are duplicated N_ALT times.
2178 After the duplication, returned output rtl-pattern contains every
2179 combination of input constraints Vs constraints from define_subst
2180 output. */
2181 static rtx
2182 subst_dup (rtx pattern, int n_alt, int n_subst_alt)
2184 const char *fmt;
2185 enum rtx_code code;
2186 int i, j, len, opno;
2188 code = GET_CODE (pattern);
2189 switch (code)
2191 case MATCH_DUP:
2192 case MATCH_OP_DUP:
2193 opno = XINT (pattern, 0);
2195 gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
2197 if (operand_data[opno])
2199 pattern = copy_rtx (operand_data[opno]);
2201 /* Duplicate constraints. */
2202 pattern = alter_constraints (pattern, n_subst_alt,
2203 duplicate_alternatives);
2205 break;
2207 default:
2208 break;
2211 fmt = GET_RTX_FORMAT (GET_CODE (pattern));
2212 len = GET_RTX_LENGTH (GET_CODE (pattern));
2213 for (i = 0; i < len; i++)
2215 switch (fmt[i])
2217 case 'e': case 'u':
2218 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2219 XEXP (pattern, i) = subst_dup (XEXP (pattern, i),
2220 n_alt, n_subst_alt);
2221 break;
2222 case 'V':
2223 if (XVEC (pattern, i) == NULL)
2224 break;
2225 /* FALLTHRU */
2226 case 'E':
2227 if (code != MATCH_DUP && code != MATCH_OP_DUP)
2228 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
2229 XVECEXP (pattern, i, j) = subst_dup (XVECEXP (pattern, i, j),
2230 n_alt, n_subst_alt);
2231 break;
2233 case 'r': case 'p': case 'i': case 'w':
2234 case '0': case 's': case 'S': case 'T':
2235 break;
2237 default:
2238 gcc_unreachable ();
2241 return pattern;
2244 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
2245 patterns appropriately. */
2247 static void
2248 process_define_cond_exec (void)
2250 struct queue_elem *elem;
2252 identify_predicable_attribute ();
2253 if (have_error)
2254 return;
2256 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
2257 process_one_cond_exec (elem);
2260 /* If we have any DEFINE_SUBST patterns, expand DEFINE_INSN and
2261 DEFINE_EXPAND patterns appropriately. */
2263 static void
2264 process_define_subst (void)
2266 struct queue_elem *elem, *elem_attr;
2268 /* Check if each define_subst has corresponding define_subst_attr. */
2269 for (elem = define_subst_queue; elem ; elem = elem->next)
2271 for (elem_attr = define_subst_attr_queue;
2272 elem_attr;
2273 elem_attr = elem_attr->next)
2274 if (strcmp (XSTR (elem->data, 0), XSTR (elem_attr->data, 1)) == 0)
2275 goto found;
2277 error_at (elem->loc,
2278 "%s: `define_subst' must have at least one "
2279 "corresponding `define_subst_attr'",
2280 XSTR (elem->data, 0));
2281 return;
2283 found:
2284 continue;
2287 for (elem = define_insn_queue; elem ; elem = elem->next)
2288 process_substs_on_one_elem (elem, define_insn_queue);
2289 for (elem = other_queue; elem ; elem = elem->next)
2291 if (GET_CODE (elem->data) != DEFINE_EXPAND)
2292 continue;
2293 process_substs_on_one_elem (elem, other_queue);
2297 /* A subclass of rtx_reader which reads .md files and calls process_rtx on
2298 the top-level elements. */
2300 class gen_reader : public rtx_reader
2302 public:
2303 gen_reader () : rtx_reader (false) {}
2304 void handle_unknown_directive (file_location, const char *);
2307 void
2308 gen_reader::handle_unknown_directive (file_location loc, const char *rtx_name)
2310 auto_vec<rtx, 32> subrtxs;
2311 if (!read_rtx (rtx_name, &subrtxs))
2312 return;
2314 rtx x;
2315 unsigned int i;
2316 FOR_EACH_VEC_ELT (subrtxs, i, x)
2317 process_rtx (x, loc);
2320 /* Comparison function for the mnemonic hash table. */
2322 static int
2323 htab_eq_string (const void *s1, const void *s2)
2325 return strcmp ((const char*)s1, (const char*)s2) == 0;
2328 /* Add mnemonic STR with length LEN to the mnemonic hash table
2329 MNEMONIC_HTAB. A trailing zero end character is appended to STR
2330 and a permanent heap copy of STR is created. */
2332 static void
2333 add_mnemonic_string (htab_t mnemonic_htab, const char *str, size_t len)
2335 char *new_str;
2336 void **slot;
2337 char *str_zero = (char*)alloca (len + 1);
2339 memcpy (str_zero, str, len);
2340 str_zero[len] = '\0';
2342 slot = htab_find_slot (mnemonic_htab, str_zero, INSERT);
2344 if (*slot)
2345 return;
2347 /* Not found; create a permanent copy and add it to the hash table. */
2348 new_str = XNEWVAR (char, len + 1);
2349 memcpy (new_str, str_zero, len + 1);
2350 *slot = new_str;
2353 /* Scan INSN for mnemonic strings and add them to the mnemonic hash
2354 table in MNEMONIC_HTAB.
2356 The mnemonics cannot be found if they are emitted using C code.
2358 If a mnemonic string contains ';' or a newline the string assumed
2359 to consist of more than a single instruction. The attribute value
2360 will then be set to the user defined default value. */
2362 static void
2363 gen_mnemonic_setattr (htab_t mnemonic_htab, rtx insn)
2365 const char *template_code, *cp;
2366 int i;
2367 int vec_len;
2368 rtx set_attr;
2369 char *attr_name;
2370 rtvec new_vec;
2371 struct obstack *string_obstack = rtx_reader_ptr->get_string_obstack ();
2373 template_code = XTMPL (insn, 3);
2375 /* Skip patterns which use C code to emit the template. */
2376 if (template_code[0] == '*')
2377 return;
2379 if (template_code[0] == '@')
2380 cp = &template_code[1];
2381 else
2382 cp = &template_code[0];
2384 for (i = 0; *cp; )
2386 const char *ep, *sp;
2387 size_t size = 0;
2389 while (ISSPACE (*cp))
2390 cp++;
2392 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
2393 if (!ISSPACE (*ep))
2394 sp = ep + 1;
2396 if (i > 0)
2397 obstack_1grow (string_obstack, ',');
2399 while (cp < sp && ((*cp >= '0' && *cp <= '9')
2400 || (*cp >= 'a' && *cp <= 'z')))
2403 obstack_1grow (string_obstack, *cp);
2404 cp++;
2405 size++;
2408 while (cp < sp)
2410 if (*cp == ';' || (*cp == '\\' && cp[1] == 'n'))
2412 /* Don't set a value if there are more than one
2413 instruction in the string. */
2414 obstack_blank_fast (string_obstack, -size);
2415 size = 0;
2417 cp = sp;
2418 break;
2420 cp++;
2422 if (size == 0)
2423 obstack_1grow (string_obstack, '*');
2424 else
2425 add_mnemonic_string (mnemonic_htab,
2426 (char *) obstack_next_free (string_obstack) - size,
2427 size);
2428 i++;
2431 /* An insn definition might emit an empty string. */
2432 if (obstack_object_size (string_obstack) == 0)
2433 return;
2435 obstack_1grow (string_obstack, '\0');
2437 set_attr = rtx_alloc (SET_ATTR);
2438 XSTR (set_attr, 1) = XOBFINISH (string_obstack, char *);
2439 attr_name = XNEWVAR (char, strlen (MNEMONIC_ATTR_NAME) + 1);
2440 strcpy (attr_name, MNEMONIC_ATTR_NAME);
2441 XSTR (set_attr, 0) = attr_name;
2443 if (!XVEC (insn, 4))
2444 vec_len = 0;
2445 else
2446 vec_len = XVECLEN (insn, 4);
2448 new_vec = rtvec_alloc (vec_len + 1);
2449 for (i = 0; i < vec_len; i++)
2450 RTVEC_ELT (new_vec, i) = XVECEXP (insn, 4, i);
2451 RTVEC_ELT (new_vec, vec_len) = set_attr;
2452 XVEC (insn, 4) = new_vec;
2455 /* This function is called for the elements in the mnemonic hashtable
2456 and generates a comma separated list of the mnemonics. */
2458 static int
2459 mnemonic_htab_callback (void **slot, void *info ATTRIBUTE_UNUSED)
2461 struct obstack *string_obstack = rtx_reader_ptr->get_string_obstack ();
2463 obstack_grow (string_obstack, (char*) *slot, strlen ((char*) *slot));
2464 obstack_1grow (string_obstack, ',');
2465 return 1;
2468 /* Generate (set_attr "mnemonic" "..") RTXs and append them to every
2469 insn definition in case the back end requests it by defining the
2470 mnemonic attribute. The values for the attribute will be extracted
2471 from the output patterns of the insn definitions as far as
2472 possible. */
2474 static void
2475 gen_mnemonic_attr (void)
2477 struct queue_elem *elem;
2478 rtx mnemonic_attr = NULL;
2479 htab_t mnemonic_htab;
2480 const char *str, *p;
2481 int i;
2482 struct obstack *string_obstack = rtx_reader_ptr->get_string_obstack ();
2484 if (have_error)
2485 return;
2487 /* Look for the DEFINE_ATTR for `mnemonic'. */
2488 for (elem = define_attr_queue; elem != *define_attr_tail; elem = elem->next)
2489 if (GET_CODE (elem->data) == DEFINE_ATTR
2490 && strcmp (XSTR (elem->data, 0), MNEMONIC_ATTR_NAME) == 0)
2492 mnemonic_attr = elem->data;
2493 break;
2496 /* A (define_attr "mnemonic" "...") indicates that the back-end
2497 wants a mnemonic attribute to be generated. */
2498 if (!mnemonic_attr)
2499 return;
2501 mnemonic_htab = htab_create_alloc (MNEMONIC_HTAB_SIZE, htab_hash_string,
2502 htab_eq_string, 0, xcalloc, free);
2504 for (elem = define_insn_queue; elem; elem = elem->next)
2506 rtx insn = elem->data;
2507 bool found = false;
2509 /* Check if the insn definition already has
2510 (set_attr "mnemonic" ...) or (set (attr "mnemonic") ...). */
2511 if (XVEC (insn, 4))
2512 for (i = 0; i < XVECLEN (insn, 4); i++)
2514 rtx set_attr = XVECEXP (insn, 4, i);
2516 switch (GET_CODE (set_attr))
2518 case SET_ATTR:
2519 case SET_ATTR_ALTERNATIVE:
2520 if (strcmp (XSTR (set_attr, 0), MNEMONIC_ATTR_NAME) == 0)
2521 found = true;
2522 break;
2523 case SET:
2524 if (GET_CODE (SET_DEST (set_attr)) == ATTR
2525 && strcmp (XSTR (SET_DEST (set_attr), 0),
2526 MNEMONIC_ATTR_NAME) == 0)
2527 found = true;
2528 break;
2529 default:
2530 break;
2534 if (!found)
2535 gen_mnemonic_setattr (mnemonic_htab, insn);
2538 /* Add the user defined values to the hash table. */
2539 str = XSTR (mnemonic_attr, 1);
2540 while ((p = scan_comma_elt (&str)) != NULL)
2541 add_mnemonic_string (mnemonic_htab, p, str - p);
2543 htab_traverse (mnemonic_htab, mnemonic_htab_callback, NULL);
2545 /* Replace the last ',' with the zero end character. */
2546 *((char *) obstack_next_free (string_obstack) - 1) = '\0';
2547 XSTR (mnemonic_attr, 1) = XOBFINISH (string_obstack, char *);
2550 /* Check if there are DEFINE_ATTRs with the same name. */
2551 static void
2552 check_define_attr_duplicates ()
2554 struct queue_elem *elem;
2555 htab_t attr_htab;
2556 char * attr_name;
2557 void **slot;
2559 attr_htab = htab_create (500, htab_hash_string, htab_eq_string, NULL);
2561 for (elem = define_attr_queue; elem; elem = elem->next)
2563 attr_name = xstrdup (XSTR (elem->data, 0));
2565 slot = htab_find_slot (attr_htab, attr_name, INSERT);
2567 /* Duplicate. */
2568 if (*slot)
2570 error_at (elem->loc, "redefinition of attribute '%s'", attr_name);
2571 htab_delete (attr_htab);
2572 return;
2575 *slot = attr_name;
2578 htab_delete (attr_htab);
2581 /* The entry point for initializing the reader. */
2583 rtx_reader *
2584 init_rtx_reader_args_cb (int argc, const char **argv,
2585 bool (*parse_opt) (const char *))
2587 /* Prepare to read input. */
2588 condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
2589 init_predicate_table ();
2590 obstack_init (rtl_obstack);
2592 /* Start at 1, to make 0 available for CODE_FOR_nothing. */
2593 insn_sequence_num = 1;
2595 /* These sequences are not used as indices, so can start at 1 also. */
2596 split_sequence_num = 1;
2597 peephole2_sequence_num = 1;
2599 gen_reader *reader = new gen_reader ();
2600 reader->read_md_files (argc, argv, parse_opt);
2602 if (define_attr_queue != NULL)
2603 check_define_attr_duplicates ();
2605 /* Process define_cond_exec patterns. */
2606 if (define_cond_exec_queue != NULL)
2607 process_define_cond_exec ();
2609 /* Process define_subst patterns. */
2610 if (define_subst_queue != NULL)
2611 process_define_subst ();
2613 if (define_attr_queue != NULL)
2614 gen_mnemonic_attr ();
2616 if (have_error)
2618 delete reader;
2619 return NULL;
2622 return reader;
2625 /* Programs that don't have their own options can use this entry point
2626 instead. */
2627 rtx_reader *
2628 init_rtx_reader_args (int argc, const char **argv)
2630 return init_rtx_reader_args_cb (argc, argv, 0);
2633 /* Try to read a single rtx from the file. Return true on success,
2634 describing it in *INFO. */
2636 bool
2637 read_md_rtx (md_rtx_info *info)
2639 int truth, *counter;
2640 rtx def;
2642 /* Discard insn patterns which we know can never match (because
2643 their C test is provably always false). If insn_elision is
2644 false, our caller needs to see all the patterns. Note that the
2645 elided patterns are never counted by the sequence numbering; it
2646 is the caller's responsibility, when insn_elision is false, not
2647 to use elided pattern numbers for anything. */
2650 struct queue_elem **queue, *elem;
2652 /* Read all patterns from a given queue before moving on to the next. */
2653 if (define_attr_queue != NULL)
2654 queue = &define_attr_queue;
2655 else if (define_pred_queue != NULL)
2656 queue = &define_pred_queue;
2657 else if (define_insn_queue != NULL)
2658 queue = &define_insn_queue;
2659 else if (other_queue != NULL)
2660 queue = &other_queue;
2661 else
2662 return false;
2664 elem = *queue;
2665 *queue = elem->next;
2666 def = elem->data;
2667 info->def = def;
2668 info->loc = elem->loc;
2669 free (elem);
2671 truth = maybe_eval_c_test (get_c_test (def));
2673 while (truth == 0 && insn_elision);
2675 /* Perform code-specific processing and pick the appropriate sequence
2676 number counter. */
2677 switch (GET_CODE (def))
2679 case DEFINE_INSN:
2680 case DEFINE_EXPAND:
2681 /* insn_sequence_num is used here so the name table will match caller's
2682 idea of insn numbering, whether or not elision is active. */
2683 record_insn_name (insn_sequence_num, XSTR (def, 0));
2685 /* Fall through. */
2686 case DEFINE_PEEPHOLE:
2687 counter = &insn_sequence_num;
2688 break;
2690 case DEFINE_SPLIT:
2691 counter = &split_sequence_num;
2692 break;
2694 case DEFINE_PEEPHOLE2:
2695 counter = &peephole2_sequence_num;
2696 break;
2698 default:
2699 counter = NULL;
2700 break;
2703 if (counter)
2705 info->index = *counter;
2706 if (truth != 0)
2707 *counter += 1;
2709 else
2710 info->index = -1;
2712 if (!rtx_locs)
2713 rtx_locs = new hash_map <rtx, file_location>;
2714 rtx_locs->put (info->def, info->loc);
2716 return true;
2719 /* Return the file location of DEFINE_* rtx X, which was previously
2720 returned by read_md_rtx. */
2721 file_location
2722 get_file_location (rtx x)
2724 gcc_assert (rtx_locs);
2725 file_location *entry = rtx_locs->get (x);
2726 gcc_assert (entry);
2727 return *entry;
2730 /* Return the number of possible INSN_CODEs. Only meaningful once the
2731 whole file has been processed. */
2732 unsigned int
2733 get_num_insn_codes ()
2735 return insn_sequence_num;
2738 /* Return the C test that says whether definition rtx DEF can be used,
2739 or "" if it can be used unconditionally. */
2741 const char *
2742 get_c_test (rtx x)
2744 switch (GET_CODE (x))
2746 case DEFINE_INSN:
2747 case DEFINE_EXPAND:
2748 case DEFINE_SUBST:
2749 return XSTR (x, 2);
2751 case DEFINE_SPLIT:
2752 case DEFINE_PEEPHOLE:
2753 case DEFINE_PEEPHOLE2:
2754 return XSTR (x, 1);
2756 default:
2757 return "";
2761 /* Helper functions for insn elision. */
2763 /* Compute a hash function of a c_test structure, which is keyed
2764 by its ->expr field. */
2765 hashval_t
2766 hash_c_test (const void *x)
2768 const struct c_test *a = (const struct c_test *) x;
2769 const unsigned char *base, *s = (const unsigned char *) a->expr;
2770 hashval_t hash;
2771 unsigned char c;
2772 unsigned int len;
2774 base = s;
2775 hash = 0;
2777 while ((c = *s++) != '\0')
2779 hash += c + (c << 17);
2780 hash ^= hash >> 2;
2783 len = s - base;
2784 hash += len + (len << 17);
2785 hash ^= hash >> 2;
2787 return hash;
2790 /* Compare two c_test expression structures. */
2792 cmp_c_test (const void *x, const void *y)
2794 const struct c_test *a = (const struct c_test *) x;
2795 const struct c_test *b = (const struct c_test *) y;
2797 return !strcmp (a->expr, b->expr);
2800 /* Given a string representing a C test expression, look it up in the
2801 condition_table and report whether or not its value is known
2802 at compile time. Returns a tristate: 1 for known true, 0 for
2803 known false, -1 for unknown. */
2805 maybe_eval_c_test (const char *expr)
2807 const struct c_test *test;
2808 struct c_test dummy;
2810 if (expr[0] == 0)
2811 return 1;
2813 dummy.expr = expr;
2814 test = (const struct c_test *)htab_find (condition_table, &dummy);
2815 if (!test)
2816 return -1;
2817 return test->value;
2820 /* Record the C test expression EXPR in the condition_table, with
2821 value VAL. Duplicates clobber previous entries. */
2823 void
2824 add_c_test (const char *expr, int value)
2826 struct c_test *test;
2828 if (expr[0] == 0)
2829 return;
2831 test = XNEW (struct c_test);
2832 test->expr = expr;
2833 test->value = value;
2835 *(htab_find_slot (condition_table, test, INSERT)) = test;
2838 /* For every C test, call CALLBACK with two arguments: a pointer to
2839 the condition structure and INFO. Stops when CALLBACK returns zero. */
2840 void
2841 traverse_c_tests (htab_trav callback, void *info)
2843 if (condition_table)
2844 htab_traverse (condition_table, callback, info);
2847 /* Helper functions for define_predicate and define_special_predicate
2848 processing. Shared between genrecog.c and genpreds.c. */
2850 static htab_t predicate_table;
2851 struct pred_data *first_predicate;
2852 static struct pred_data **last_predicate = &first_predicate;
2854 static hashval_t
2855 hash_struct_pred_data (const void *ptr)
2857 return htab_hash_string (((const struct pred_data *)ptr)->name);
2860 static int
2861 eq_struct_pred_data (const void *a, const void *b)
2863 return !strcmp (((const struct pred_data *)a)->name,
2864 ((const struct pred_data *)b)->name);
2867 struct pred_data *
2868 lookup_predicate (const char *name)
2870 struct pred_data key;
2871 key.name = name;
2872 return (struct pred_data *) htab_find (predicate_table, &key);
2875 /* Record that predicate PRED can accept CODE. */
2877 void
2878 add_predicate_code (struct pred_data *pred, enum rtx_code code)
2880 if (!pred->codes[code])
2882 pred->num_codes++;
2883 pred->codes[code] = true;
2885 if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
2886 pred->allows_non_const = true;
2888 if (code != REG
2889 && code != SUBREG
2890 && code != MEM
2891 && code != CONCAT
2892 && code != PARALLEL
2893 && code != STRICT_LOW_PART
2894 && code != ZERO_EXTRACT
2895 && code != SCRATCH)
2896 pred->allows_non_lvalue = true;
2898 if (pred->num_codes == 1)
2899 pred->singleton = code;
2900 else if (pred->num_codes == 2)
2901 pred->singleton = UNKNOWN;
2905 void
2906 add_predicate (struct pred_data *pred)
2908 void **slot = htab_find_slot (predicate_table, pred, INSERT);
2909 if (*slot)
2911 error ("duplicate predicate definition for '%s'", pred->name);
2912 return;
2914 *slot = pred;
2915 *last_predicate = pred;
2916 last_predicate = &pred->next;
2919 /* This array gives the initial content of the predicate table. It
2920 has entries for all predicates defined in recog.c. */
2922 struct std_pred_table
2924 const char *name;
2925 bool special;
2926 bool allows_const_p;
2927 RTX_CODE codes[NUM_RTX_CODE];
2930 static const struct std_pred_table std_preds[] = {
2931 {"general_operand", false, true, {SUBREG, REG, MEM}},
2932 {"address_operand", true, true, {SUBREG, REG, MEM, PLUS, MINUS, MULT,
2933 ZERO_EXTEND, SIGN_EXTEND, AND}},
2934 {"register_operand", false, false, {SUBREG, REG}},
2935 {"pmode_register_operand", true, false, {SUBREG, REG}},
2936 {"scratch_operand", false, false, {SCRATCH, REG}},
2937 {"immediate_operand", false, true, {UNKNOWN}},
2938 {"const_int_operand", false, false, {CONST_INT}},
2939 #if TARGET_SUPPORTS_WIDE_INT
2940 {"const_scalar_int_operand", false, false, {CONST_INT, CONST_WIDE_INT}},
2941 {"const_double_operand", false, false, {CONST_DOUBLE}},
2942 #else
2943 {"const_double_operand", false, false, {CONST_INT, CONST_DOUBLE}},
2944 #endif
2945 {"nonimmediate_operand", false, false, {SUBREG, REG, MEM}},
2946 {"nonmemory_operand", false, true, {SUBREG, REG}},
2947 {"push_operand", false, false, {MEM}},
2948 {"pop_operand", false, false, {MEM}},
2949 {"memory_operand", false, false, {SUBREG, MEM}},
2950 {"indirect_operand", false, false, {SUBREG, MEM}},
2951 {"ordered_comparison_operator", false, false, {EQ, NE,
2952 LE, LT, GE, GT,
2953 LEU, LTU, GEU, GTU}},
2954 {"comparison_operator", false, false, {EQ, NE,
2955 LE, LT, GE, GT,
2956 LEU, LTU, GEU, GTU,
2957 UNORDERED, ORDERED,
2958 UNEQ, UNGE, UNGT,
2959 UNLE, UNLT, LTGT}}
2961 #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
2963 /* Initialize the table of predicate definitions, starting with
2964 the information we have on generic predicates. */
2966 static void
2967 init_predicate_table (void)
2969 size_t i, j;
2970 struct pred_data *pred;
2972 predicate_table = htab_create_alloc (37, hash_struct_pred_data,
2973 eq_struct_pred_data, 0,
2974 xcalloc, free);
2976 for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
2978 pred = XCNEW (struct pred_data);
2979 pred->name = std_preds[i].name;
2980 pred->special = std_preds[i].special;
2982 for (j = 0; std_preds[i].codes[j] != 0; j++)
2983 add_predicate_code (pred, std_preds[i].codes[j]);
2985 if (std_preds[i].allows_const_p)
2986 for (j = 0; j < NUM_RTX_CODE; j++)
2987 if (GET_RTX_CLASS (j) == RTX_CONST_OBJ)
2988 add_predicate_code (pred, (enum rtx_code) j);
2990 add_predicate (pred);
2994 /* These functions allow linkage with print-rtl.c. Also, some generators
2995 like to annotate their output with insn names. */
2997 /* Holds an array of names indexed by insn_code_number. */
2998 static char **insn_name_ptr = 0;
2999 static int insn_name_ptr_size = 0;
3001 const char *
3002 get_insn_name (int code)
3004 if (code < insn_name_ptr_size)
3005 return insn_name_ptr[code];
3006 else
3007 return NULL;
3010 static void
3011 record_insn_name (int code, const char *name)
3013 static const char *last_real_name = "insn";
3014 static int last_real_code = 0;
3015 char *new_name;
3017 if (insn_name_ptr_size <= code)
3019 int new_size;
3020 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
3021 insn_name_ptr = XRESIZEVEC (char *, insn_name_ptr, new_size);
3022 memset (insn_name_ptr + insn_name_ptr_size, 0,
3023 sizeof (char *) * (new_size - insn_name_ptr_size));
3024 insn_name_ptr_size = new_size;
3027 if (!name || name[0] == '\0')
3029 new_name = XNEWVAR (char, strlen (last_real_name) + 10);
3030 sprintf (new_name, "%s+%d", last_real_name, code - last_real_code);
3032 else
3034 last_real_name = new_name = xstrdup (name);
3035 last_real_code = code;
3038 insn_name_ptr[code] = new_name;
3041 /* Make STATS describe the operands that appear in rtx X. */
3043 static void
3044 get_pattern_stats_1 (struct pattern_stats *stats, rtx x)
3046 RTX_CODE code;
3047 int i;
3048 int len;
3049 const char *fmt;
3051 if (x == NULL_RTX)
3052 return;
3054 code = GET_CODE (x);
3055 switch (code)
3057 case MATCH_OPERAND:
3058 case MATCH_OPERATOR:
3059 case MATCH_PARALLEL:
3060 stats->max_opno = MAX (stats->max_opno, XINT (x, 0));
3061 break;
3063 case MATCH_DUP:
3064 case MATCH_OP_DUP:
3065 case MATCH_PAR_DUP:
3066 stats->num_dups++;
3067 stats->max_dup_opno = MAX (stats->max_dup_opno, XINT (x, 0));
3068 break;
3070 case MATCH_SCRATCH:
3071 if (stats->min_scratch_opno == -1)
3072 stats->min_scratch_opno = XINT (x, 0);
3073 else
3074 stats->min_scratch_opno = MIN (stats->min_scratch_opno, XINT (x, 0));
3075 stats->max_scratch_opno = MAX (stats->max_scratch_opno, XINT (x, 0));
3076 break;
3078 default:
3079 break;
3082 fmt = GET_RTX_FORMAT (code);
3083 len = GET_RTX_LENGTH (code);
3084 for (i = 0; i < len; i++)
3086 if (fmt[i] == 'e' || fmt[i] == 'u')
3087 get_pattern_stats_1 (stats, XEXP (x, i));
3088 else if (fmt[i] == 'E')
3090 int j;
3091 for (j = 0; j < XVECLEN (x, i); j++)
3092 get_pattern_stats_1 (stats, XVECEXP (x, i, j));
3097 /* Make STATS describe the operands that appear in instruction pattern
3098 PATTERN. */
3100 void
3101 get_pattern_stats (struct pattern_stats *stats, rtvec pattern)
3103 int i, len;
3105 stats->max_opno = -1;
3106 stats->max_dup_opno = -1;
3107 stats->min_scratch_opno = -1;
3108 stats->max_scratch_opno = -1;
3109 stats->num_dups = 0;
3111 len = GET_NUM_ELEM (pattern);
3112 for (i = 0; i < len; i++)
3113 get_pattern_stats_1 (stats, RTVEC_ELT (pattern, i));
3115 stats->num_generator_args = stats->max_opno + 1;
3116 stats->num_insn_operands = MAX (stats->max_opno,
3117 stats->max_scratch_opno) + 1;
3118 stats->num_operand_vars = MAX (stats->max_opno,
3119 MAX (stats->max_dup_opno,
3120 stats->max_scratch_opno)) + 1;
3123 /* Return the emit_* function that should be used for pattern X, or NULL
3124 if we can't pick a particular type at compile time and should instead
3125 fall back to "emit". */
3127 const char *
3128 get_emit_function (rtx x)
3130 switch (classify_insn (x))
3132 case INSN:
3133 return "emit_insn";
3135 case CALL_INSN:
3136 return "emit_call_insn";
3138 case JUMP_INSN:
3139 return "emit_jump_insn";
3141 case UNKNOWN:
3142 return NULL;
3144 default:
3145 gcc_unreachable ();
3149 /* Return true if we must emit a barrier after pattern X. */
3151 bool
3152 needs_barrier_p (rtx x)
3154 return (GET_CODE (x) == SET
3155 && GET_CODE (SET_DEST (x)) == PC
3156 && GET_CODE (SET_SRC (x)) == LABEL_REF);
3159 #define NS "NULL"
3160 #define ZS "'\\0'"
3161 #define OPTAB_CL(o, p, c, b, l) { #o, p, #b, ZS, #l, o, c, UNKNOWN, 1 },
3162 #define OPTAB_CX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 1 },
3163 #define OPTAB_CD(o, p) { #o, p, NS, ZS, NS, o, UNKNOWN, UNKNOWN, 2 },
3164 #define OPTAB_NL(o, p, c, b, s, l) { #o, p, #b, #s, #l, o, c, c, 3 },
3165 #define OPTAB_NC(o, p, c) { #o, p, NS, ZS, NS, o, c, c, 3 },
3166 #define OPTAB_NX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 3 },
3167 #define OPTAB_VL(o, p, c, b, s, l) { #o, p, #b, #s, #l, o, c, UNKNOWN, 3 },
3168 #define OPTAB_VC(o, p, c) { #o, p, NS, ZS, NS, o, c, UNKNOWN, 3 },
3169 #define OPTAB_VX(o, p) { #o, p, NULL, NULL, NULL, o, UNKNOWN, UNKNOWN, 3 },
3170 #define OPTAB_DC(o, p, c) { #o, p, NS, ZS, NS, o, c, c, 4 },
3171 #define OPTAB_D(o, p) { #o, p, NS, ZS, NS, o, UNKNOWN, UNKNOWN, 4 },
3173 /* An array of all optabs. Note that the same optab can appear more
3174 than once, with a different pattern. */
3175 optab_def optabs[] = {
3176 { "unknown_optab", NULL, NS, ZS, NS, unknown_optab, UNKNOWN, UNKNOWN, 0 },
3177 #include "optabs.def"
3180 /* The number of entries in optabs[]. */
3181 unsigned int num_optabs = ARRAY_SIZE (optabs);
3183 #undef OPTAB_CL
3184 #undef OPTAB_CX
3185 #undef OPTAB_CD
3186 #undef OPTAB_NL
3187 #undef OPTAB_NC
3188 #undef OPTAB_NX
3189 #undef OPTAB_VL
3190 #undef OPTAB_VC
3191 #undef OPTAB_VX
3192 #undef OPTAB_DC
3193 #undef OPTAB_D
3195 /* Return true if instruction NAME matches pattern PAT, storing information
3196 about the match in P if so. */
3198 static bool
3199 match_pattern (optab_pattern *p, const char *name, const char *pat)
3201 bool force_float = false;
3202 bool force_int = false;
3203 bool force_partial_int = false;
3204 bool force_fixed = false;
3206 if (pat == NULL)
3207 return false;
3208 for (; ; ++pat)
3210 if (*pat != '$')
3212 if (*pat != *name++)
3213 return false;
3214 if (*pat == '\0')
3215 return true;
3216 continue;
3218 switch (*++pat)
3220 case 'I':
3221 force_int = 1;
3222 break;
3223 case 'P':
3224 force_partial_int = 1;
3225 break;
3226 case 'F':
3227 force_float = 1;
3228 break;
3229 case 'Q':
3230 force_fixed = 1;
3231 break;
3233 case 'a':
3234 case 'b':
3236 int i;
3238 /* This loop will stop at the first prefix match, so
3239 look through the modes in reverse order, in case
3240 there are extra CC modes and CC is a prefix of the
3241 CC modes (as it should be). */
3242 for (i = (MAX_MACHINE_MODE) - 1; i >= 0; i--)
3244 const char *p, *q;
3245 for (p = GET_MODE_NAME (i), q = name; *p; p++, q++)
3246 if (TOLOWER (*p) != *q)
3247 break;
3248 if (*p == 0
3249 && (! force_int || mode_class[i] == MODE_INT
3250 || mode_class[i] == MODE_VECTOR_INT)
3251 && (! force_partial_int
3252 || mode_class[i] == MODE_INT
3253 || mode_class[i] == MODE_PARTIAL_INT
3254 || mode_class[i] == MODE_VECTOR_INT)
3255 && (! force_float
3256 || mode_class[i] == MODE_FLOAT
3257 || mode_class[i] == MODE_DECIMAL_FLOAT
3258 || mode_class[i] == MODE_COMPLEX_FLOAT
3259 || mode_class[i] == MODE_VECTOR_FLOAT)
3260 && (! force_fixed
3261 || mode_class[i] == MODE_FRACT
3262 || mode_class[i] == MODE_UFRACT
3263 || mode_class[i] == MODE_ACCUM
3264 || mode_class[i] == MODE_UACCUM
3265 || mode_class[i] == MODE_VECTOR_FRACT
3266 || mode_class[i] == MODE_VECTOR_UFRACT
3267 || mode_class[i] == MODE_VECTOR_ACCUM
3268 || mode_class[i] == MODE_VECTOR_UACCUM))
3269 break;
3272 if (i < 0)
3273 return false;
3274 name += strlen (GET_MODE_NAME (i));
3275 if (*pat == 'a')
3276 p->m1 = i;
3277 else
3278 p->m2 = i;
3280 force_int = false;
3281 force_partial_int = false;
3282 force_float = false;
3283 force_fixed = false;
3285 break;
3287 default:
3288 gcc_unreachable ();
3293 /* Return true if NAME is the name of an optab, describing it in P if so. */
3295 bool
3296 find_optab (optab_pattern *p, const char *name)
3298 if (*name == 0 || *name == '*')
3299 return false;
3301 /* See if NAME matches one of the patterns we have for the optabs
3302 we know about. */
3303 for (unsigned int pindex = 0; pindex < ARRAY_SIZE (optabs); pindex++)
3305 p->m1 = p->m2 = 0;
3306 if (match_pattern (p, name, optabs[pindex].pattern))
3308 p->name = name;
3309 p->op = optabs[pindex].op;
3310 p->sort_num = (p->op << 16) | (p->m2 << 8) | p->m1;
3311 return true;
3314 return false;