comparison gcc/read-rtl.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* RTL reader for GCC. 1 /* RTL reader for GCC.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc. 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free 7 the terms of the GNU General Public License as published by the Free
70 struct iterator_group { 70 struct iterator_group {
71 /* Tables of "mapping" structures, one for attributes and one for 71 /* Tables of "mapping" structures, one for attributes and one for
72 iterators. */ 72 iterators. */
73 htab_t attrs, iterators; 73 htab_t attrs, iterators;
74 74
75 /* The C++ type of the iterator, such as "machine_mode" for modes. */
76 const char *type;
77
75 /* Treat the given string as the name of a standard mode, etc., and 78 /* Treat the given string as the name of a standard mode, etc., and
76 return its integer value. */ 79 return its integer value. */
77 int (*find_builtin) (const char *); 80 int (*find_builtin) (const char *);
78 81
79 /* Make the given rtx use the iterator value given by the third argument. 82 /* Make the given rtx use the iterator value given by the third argument.
80 If the iterator applies to operands, the second argument gives the 83 If the iterator applies to operands, the second argument gives the
81 operand index, otherwise it is ignored. */ 84 operand index, otherwise it is ignored. */
82 void (*apply_iterator) (rtx, unsigned int, int); 85 void (*apply_iterator) (rtx, unsigned int, int);
86
87 /* Return the C token for the given standard mode, code, etc. */
88 const char *(*get_c_token) (int);
83 }; 89 };
84 90
85 /* Records one use of an iterator. */ 91 /* Records one use of an iterator. */
86 struct iterator_use { 92 struct iterator_use {
87 /* The iterator itself. */ 93 /* The iterator itself. */
159 165
160 static void 166 static void
161 apply_mode_iterator (rtx x, unsigned int, int mode) 167 apply_mode_iterator (rtx x, unsigned int, int mode)
162 { 168 {
163 PUT_MODE (x, (machine_mode) mode); 169 PUT_MODE (x, (machine_mode) mode);
170 }
171
172 static const char *
173 get_mode_token (int mode)
174 {
175 return concat ("E_", GET_MODE_NAME (mode), "mode", NULL);
164 } 176 }
165 177
166 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn", 178 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
167 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */ 179 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
168 180
204 apply_code_iterator (rtx x, unsigned int, int code) 216 apply_code_iterator (rtx x, unsigned int, int code)
205 { 217 {
206 PUT_CODE (x, (enum rtx_code) code); 218 PUT_CODE (x, (enum rtx_code) code);
207 } 219 }
208 220
221 static const char *
222 get_code_token (int code)
223 {
224 char *name = xstrdup (GET_RTX_NAME (code));
225 for (int i = 0; name[i]; ++i)
226 name[i] = TOUPPER (name[i]);
227 return name;
228 }
229
209 /* Implementations of the iterator_group callbacks for ints. */ 230 /* Implementations of the iterator_group callbacks for ints. */
210 231
211 /* Since GCC does not construct a table of valid constants, 232 /* Since GCC does not construct a table of valid constants,
212 we have to accept any int as valid. No cross-checking can 233 we have to accept any int as valid. No cross-checking can
213 be done. */ 234 be done. */
220 } 241 }
221 242
222 static void 243 static void
223 apply_int_iterator (rtx x, unsigned int index, int value) 244 apply_int_iterator (rtx x, unsigned int index, int value)
224 { 245 {
225 XINT (x, index) = value; 246 if (GET_CODE (x) == SUBREG)
247 SUBREG_BYTE (x) = value;
248 else
249 XINT (x, index) = value;
250 }
251
252 static const char *
253 get_int_token (int value)
254 {
255 char buffer[HOST_BITS_PER_INT + 1];
256 sprintf (buffer, "%d", value);
257 return xstrdup (buffer);
226 } 258 }
227 259
228 #ifdef GENERATOR_FILE 260 #ifdef GENERATOR_FILE
229 261
230 /* This routine adds attribute or does nothing depending on VALUE. When 262 /* This routine adds attribute or does nothing depending on VALUE. When
312 iter_name = value->iter_name; 344 iter_name = value->iter_name;
313 return iter_name; 345 return iter_name;
314 } 346 }
315 347
316 /* Map attribute string P to its current value. Return null if the attribute 348 /* Map attribute string P to its current value. Return null if the attribute
317 isn't known. */ 349 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
350 there. */
318 351
319 static struct map_value * 352 static struct map_value *
320 map_attr_string (const char *p) 353 map_attr_string (const char *p, mapping **iterator_out = 0)
321 { 354 {
322 const char *attr; 355 const char *attr;
323 struct mapping *iterator; 356 struct mapping *iterator;
324 unsigned int i; 357 unsigned int i;
325 struct mapping *m; 358 struct mapping *m;
364 } 397 }
365 /* Find the attribute value associated with the current 398 /* Find the attribute value associated with the current
366 iterator value. */ 399 iterator value. */
367 for (v = m->values; v; v = v->next) 400 for (v = m->values; v; v = v->next)
368 if (v->number == iterator->current_value->number) 401 if (v->number == iterator->current_value->number)
369 return v; 402 {
403 if (iterator_out)
404 *iterator_out = iterator;
405 return v;
406 }
370 } 407 }
371 } 408 }
372 return NULL; 409 return NULL;
373 } 410 }
374 411
540 if (iterator->current_value) 577 if (iterator->current_value)
541 current_iterators.safe_push (iterator); 578 current_iterators.safe_push (iterator);
542 return 1; 579 return 1;
543 } 580 }
544 581
582 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
583 be many instances of two overloaded_names having the same name but
584 different arguments, so hashing on the name should be good enough in
585 practice. */
586
587 static hashval_t
588 overloaded_name_hash (const void *uncast_oname)
589 {
590 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
591 return htab_hash_string (oname->name);
592 }
593
594 /* Return true if two overloaded_names are similar enough to share
595 the same generated functions. */
596
597 static int
598 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
599 {
600 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
601 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
602 if (strcmp (oname1->name, oname2->name) != 0
603 || oname1->arg_types.length () != oname2->arg_types.length ())
604 return 0;
605
606 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
607 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
608 return 0;
609
610 return 1;
611 }
612
613 /* Return true if X has an instruction name in XSTR (X, 0). */
614
615 static bool
616 named_rtx_p (rtx x)
617 {
618 switch (GET_CODE (x))
619 {
620 case DEFINE_EXPAND:
621 case DEFINE_INSN:
622 case DEFINE_INSN_AND_SPLIT:
623 return true;
624
625 default:
626 return false;
627 }
628 }
629
630 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
631 If so, return the associated overloaded_name and add the iterator for
632 each argument to ITERATORS. Return null otherwise. */
633
634 overloaded_name *
635 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
636 {
637 /* Check for the leading '@'. */
638 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
639 return NULL;
640
641 /* Remove the '@', so that no other code needs to worry about it. */
642 const char *name = XSTR (original, 0);
643 copy_md_ptr_loc (name + 1, name);
644 name += 1;
645 XSTR (original, 0) = name;
646
647 /* Build a copy of the name without the '<...>' attribute strings.
648 Add the iterator associated with each such attribute string to ITERATORS
649 and add an associated argument to TMP_ONAME. */
650 char *copy = ASTRDUP (name);
651 char *base = copy, *start, *end;
652 overloaded_name tmp_oname;
653 tmp_oname.arg_types.create (current_iterators.length ());
654 bool pending_underscore_p = false;
655 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
656 {
657 *end = 0;
658 mapping *iterator;
659 if (!map_attr_string (start + 1, &iterator))
660 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
661 *end = '>';
662
663 /* Remove a trailing underscore, so that we don't end a name
664 with "_" or turn "_<...>_" into "__". */
665 if (start != base && start[-1] == '_')
666 {
667 start -= 1;
668 pending_underscore_p = true;
669 }
670
671 /* Add the text between either the last '>' or the start of
672 the string and this '<'. */
673 obstack_grow (&m_string_obstack, base, start - base);
674 base = end + 1;
675
676 /* If there's a character we need to keep after the '>', check
677 whether we should prefix it with a previously-dropped '_'. */
678 if (base[0] != 0 && base[0] != '<')
679 {
680 if (pending_underscore_p && base[0] != '_')
681 obstack_1grow (&m_string_obstack, '_');
682 pending_underscore_p = false;
683 }
684
685 /* Record an argument for ITERATOR. */
686 iterators->safe_push (iterator);
687 tmp_oname.arg_types.safe_push (iterator->group->type);
688 }
689 if (base == copy)
690 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
691
692 size_t length = obstack_object_size (&m_string_obstack);
693 if (length == 0)
694 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
695
696 /* Get the completed name. */
697 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
698 char *new_name = XOBFINISH (&m_string_obstack, char *);
699 tmp_oname.name = new_name;
700
701 if (!m_overloads_htab)
702 m_overloads_htab = htab_create (31, overloaded_name_hash,
703 overloaded_name_eq_p, NULL);
704
705 /* See whether another pattern had the same overload name and list
706 of argument types. Create a new permanent one if not. */
707 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
708 overloaded_name *oname = (overloaded_name *) *slot;
709 if (!oname)
710 {
711 *slot = oname = new overloaded_name;
712 oname->name = tmp_oname.name;
713 oname->arg_types = tmp_oname.arg_types;
714 oname->next = NULL;
715 oname->first_instance = NULL;
716 oname->next_instance_ptr = &oname->first_instance;
717
718 *m_next_overload_ptr = oname;
719 m_next_overload_ptr = &oname->next;
720 }
721 else
722 {
723 obstack_free (&m_string_obstack, new_name);
724 tmp_oname.arg_types.release ();
725 }
726
727 return oname;
728 }
729
730 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
731 gives the iterator associated with argument I of ONAME. */
732
733 static void
734 add_overload_instance (overloaded_name *oname, vec<mapping *> iterators, rtx x)
735 {
736 /* Create the instance. */
737 overloaded_instance *instance = new overloaded_instance;
738 instance->next = NULL;
739 instance->arg_values.create (oname->arg_types.length ());
740 for (unsigned int i = 0; i < iterators.length (); ++i)
741 {
742 int value = iterators[i]->current_value->number;
743 const char *name = iterators[i]->group->get_c_token (value);
744 instance->arg_values.quick_push (name);
745 }
746 instance->name = XSTR (x, 0);
747 instance->insn = x;
748
749 /* Chain it onto the end of ONAME's list. */
750 *oname->next_instance_ptr = instance;
751 oname->next_instance_ptr = &instance->next;
752 }
753
545 /* Expand all iterators in the current rtx, which is given as ORIGINAL. 754 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
546 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */ 755 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
547 756
548 static void 757 static void
549 apply_iterators (rtx original, vec<rtx> *queue) 758 apply_iterators (rtx original, vec<rtx> *queue)
557 766
558 if (iterator_uses.is_empty ()) 767 if (iterator_uses.is_empty ())
559 { 768 {
560 /* Raise an error if any attributes were used. */ 769 /* Raise an error if any attributes were used. */
561 apply_attribute_uses (); 770 apply_attribute_uses ();
771
772 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
773 fatal_with_file_and_line ("'@' used without iterators");
774
562 queue->safe_push (original); 775 queue->safe_push (original);
563 return; 776 return;
564 } 777 }
565 778
566 /* Clear out the iterators from the previous run. */ 779 /* Clear out the iterators from the previous run. */
577 htab_traverse (modes.iterators, add_current_iterators, NULL); 790 htab_traverse (modes.iterators, add_current_iterators, NULL);
578 htab_traverse (codes.iterators, add_current_iterators, NULL); 791 htab_traverse (codes.iterators, add_current_iterators, NULL);
579 htab_traverse (ints.iterators, add_current_iterators, NULL); 792 htab_traverse (ints.iterators, add_current_iterators, NULL);
580 htab_traverse (substs.iterators, add_current_iterators, NULL); 793 htab_traverse (substs.iterators, add_current_iterators, NULL);
581 gcc_assert (!current_iterators.is_empty ()); 794 gcc_assert (!current_iterators.is_empty ());
795
796 /* Check whether this is a '@' overloaded pattern. */
797 auto_vec<mapping *, 16> iterators;
798 overloaded_name *oname
799 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
582 800
583 for (;;) 801 for (;;)
584 { 802 {
585 /* Apply the current iterator values. Accumulate a condition to 803 /* Apply the current iterator values. Accumulate a condition to
586 say when the resulting rtx can be used. */ 804 say when the resulting rtx can be used. */
611 current_iterator_name = iuse->iterator->name; 829 current_iterator_name = iuse->iterator->name;
612 iuse->iterator->group->apply_iterator (iuse->x, iuse->index, 830 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
613 v->number); 831 v->number);
614 } 832 }
615 } 833 }
834
835 if (oname)
836 add_overload_instance (oname, iterators, x);
837
616 /* Add the new rtx to the end of the queue. */ 838 /* Add the new rtx to the end of the queue. */
617 queue->safe_push (x); 839 queue->safe_push (x);
618 840
619 /* Lexicographically increment the iterator value sequence. 841 /* Lexicographically increment the iterator value sequence.
620 That is, cycle through iterator values, starting from the right, 842 That is, cycle through iterator values, starting from the right,
687 int i; 909 int i;
688 910
689 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); 911 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
690 modes.iterators = htab_create (13, leading_string_hash, 912 modes.iterators = htab_create (13, leading_string_hash,
691 leading_string_eq_p, 0); 913 leading_string_eq_p, 0);
914 modes.type = "machine_mode";
692 modes.find_builtin = find_mode; 915 modes.find_builtin = find_mode;
693 modes.apply_iterator = apply_mode_iterator; 916 modes.apply_iterator = apply_mode_iterator;
917 modes.get_c_token = get_mode_token;
694 918
695 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); 919 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
696 codes.iterators = htab_create (13, leading_string_hash, 920 codes.iterators = htab_create (13, leading_string_hash,
697 leading_string_eq_p, 0); 921 leading_string_eq_p, 0);
922 codes.type = "rtx_code";
698 codes.find_builtin = find_code; 923 codes.find_builtin = find_code;
699 codes.apply_iterator = apply_code_iterator; 924 codes.apply_iterator = apply_code_iterator;
925 codes.get_c_token = get_code_token;
700 926
701 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); 927 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
702 ints.iterators = htab_create (13, leading_string_hash, 928 ints.iterators = htab_create (13, leading_string_hash,
703 leading_string_eq_p, 0); 929 leading_string_eq_p, 0);
930 ints.type = "int";
704 ints.find_builtin = find_int; 931 ints.find_builtin = find_int;
705 ints.apply_iterator = apply_int_iterator; 932 ints.apply_iterator = apply_int_iterator;
933 ints.get_c_token = get_int_token;
706 934
707 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); 935 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
708 substs.iterators = htab_create (13, leading_string_hash, 936 substs.iterators = htab_create (13, leading_string_hash,
709 leading_string_eq_p, 0); 937 leading_string_eq_p, 0);
938 substs.type = "int";
710 substs.find_builtin = find_int; /* We don't use it, anyway. */ 939 substs.find_builtin = find_int; /* We don't use it, anyway. */
711 #ifdef GENERATOR_FILE 940 #ifdef GENERATOR_FILE
712 substs.apply_iterator = apply_subst_iterator; 941 substs.apply_iterator = apply_subst_iterator;
713 #endif 942 #endif
943 substs.get_c_token = get_int_token;
714 944
715 lower = add_mapping (&modes, modes.attrs, "mode"); 945 lower = add_mapping (&modes, modes.attrs, "mode");
716 upper = add_mapping (&modes, modes.attrs, "MODE"); 946 upper = add_mapping (&modes, modes.attrs, "MODE");
717 lower_ptr = &lower->values; 947 lower_ptr = &lower->values;
718 upper_ptr = &upper->values; 948 upper_ptr = &upper->values;
1227 1457
1228 static int 1458 static int
1229 parse_reg_note_name (const char *string) 1459 parse_reg_note_name (const char *string)
1230 { 1460 {
1231 for (int i = 0; i < REG_NOTE_MAX; i++) 1461 for (int i = 0; i < REG_NOTE_MAX; i++)
1232 if (0 == strcmp (string, GET_REG_NOTE_NAME (i))) 1462 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1233 return i; 1463 return i;
1234 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string); 1464 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1235 } 1465 }
1236 1466
1237 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of 1467 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1458 { 1688 {
1459 /* Obstack to store scratch vector in. */ 1689 /* Obstack to store scratch vector in. */
1460 struct obstack vector_stack; 1690 struct obstack vector_stack;
1461 int list_counter = 0; 1691 int list_counter = 0;
1462 rtvec return_vec = NULL_RTVEC; 1692 rtvec return_vec = NULL_RTVEC;
1693 rtx saved_rtx = NULL_RTX;
1463 1694
1464 require_char_ws ('['); 1695 require_char_ws ('[');
1465 1696
1466 /* Add expressions to a list, while keeping a count. */ 1697 /* Add expressions to a list, while keeping a count. */
1467 obstack_init (&vector_stack); 1698 obstack_init (&vector_stack);
1468 while ((c = read_skip_spaces ()) && c != ']') 1699 while ((c = read_skip_spaces ()) && c != ']')
1469 { 1700 {
1470 if (c == EOF) 1701 if (c == EOF)
1471 fatal_expected_char (']', c); 1702 fatal_expected_char (']', c);
1472 unread_char (c); 1703 unread_char (c);
1473 list_counter++; 1704
1474 obstack_ptr_grow (&vector_stack, read_nested_rtx ()); 1705 rtx value;
1706 int repeat_count = 1;
1707 if (c == 'r')
1708 {
1709 /* Process "repeated xN" directive. */
1710 read_name (&name);
1711 if (strcmp (name.string, "repeated"))
1712 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1713 name.string);
1714 read_name (&name);
1715 if (!sscanf (name.string, "x%d", &repeat_count))
1716 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1717 name.string);
1718
1719 /* We already saw one of the instances. */
1720 repeat_count--;
1721 value = saved_rtx;
1722 }
1723 else
1724 value = read_nested_rtx ();
1725
1726 for (; repeat_count > 0; repeat_count--)
1727 {
1728 list_counter++;
1729 obstack_ptr_grow (&vector_stack, value);
1730 }
1731 saved_rtx = value;
1475 } 1732 }
1476 if (list_counter > 0) 1733 if (list_counter > 0)
1477 { 1734 {
1478 return_vec = rtvec_alloc (list_counter); 1735 return_vec = rtvec_alloc (list_counter);
1479 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack), 1736 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1606 } 1863 }
1607 break; 1864 break;
1608 1865
1609 case 'i': 1866 case 'i':
1610 case 'n': 1867 case 'n':
1868 case 'p':
1611 /* Can be an iterator or an integer constant. */ 1869 /* Can be an iterator or an integer constant. */
1612 read_name (&name); 1870 read_name (&name);
1613 record_potential_iterator_use (&ints, return_rtx, idx, name.string); 1871 record_potential_iterator_use (&ints, return_rtx, idx, name.string);
1614 break; 1872 break;
1615 1873