]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/read-rtl.c
This patch rewrites the old VEC macro-based interface into a new one
[thirdparty/gcc.git] / gcc / read-rtl.c
1 /* RTL reader for GCC.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "bconfig.h"
23
24 /* Disable rtl checking; it conflicts with the iterator handling. */
25 #undef ENABLE_RTL_CHECKING
26
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "obstack.h"
32 #include "hashtab.h"
33 #include "read-md.h"
34 #include "gensupport.h"
35
36 /* One element in a singly-linked list of (integer, string) pairs. */
37 struct map_value {
38 struct map_value *next;
39 int number;
40 const char *string;
41 };
42
43 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
44 The integers are iterator values; the strings are either C conditions
45 or attribute values. */
46 struct mapping {
47 /* The name of the iterator or attribute. */
48 const char *name;
49
50 /* The group (modes or codes) to which the iterator or attribute belongs. */
51 struct iterator_group *group;
52
53 /* The list of (integer, string) pairs. */
54 struct map_value *values;
55
56 /* For iterators, records the current value of the iterator. */
57 struct map_value *current_value;
58 };
59
60 /* Vector definitions for the above. */
61 typedef struct mapping *mapping_ptr;
62
63 /* A structure for abstracting the common parts of iterators. */
64 struct iterator_group {
65 /* Tables of "mapping" structures, one for attributes and one for
66 iterators. */
67 htab_t attrs, iterators;
68
69 /* Treat the given string as the name of a standard mode, etc., and
70 return its integer value. */
71 int (*find_builtin) (const char *);
72
73 /* Make the given pointer use the given iterator value. */
74 void (*apply_iterator) (void *, int);
75 };
76
77 /* Records one use of an iterator. */
78 struct iterator_use {
79 /* The iterator itself. */
80 struct mapping *iterator;
81
82 /* The location of the use, as passed to the apply_iterator callback. */
83 void *ptr;
84 };
85
86 /* Vector definitions for the above. */
87 typedef struct iterator_use iterator_use;
88
89 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
90 in a non-string rtx field. */
91 struct attribute_use {
92 /* The group that describes the use site. */
93 struct iterator_group *group;
94
95 /* The name of the attribute, possibly with an "iterator:" prefix. */
96 const char *value;
97
98 /* The location of the use, as passed to GROUP's apply_iterator callback. */
99 void *ptr;
100 };
101
102 /* Vector definitions for the above. */
103 typedef struct attribute_use attribute_use;
104
105 static void validate_const_int (const char *);
106 static rtx read_rtx_code (const char *);
107 static rtx read_nested_rtx (void);
108 static rtx read_rtx_variadic (rtx);
109
110 /* The mode and code iterator structures. */
111 static struct iterator_group modes, codes, ints;
112
113 /* All iterators used in the current rtx. */
114 static vec<mapping_ptr> current_iterators;
115
116 /* The list of all iterator uses in the current rtx. */
117 static vec<iterator_use> iterator_uses;
118
119 /* The list of all attribute uses in the current rtx. */
120 static vec<attribute_use> attribute_uses;
121
122 /* Implementations of the iterator_group callbacks for modes. */
123
124 static int
125 find_mode (const char *name)
126 {
127 int i;
128
129 for (i = 0; i < NUM_MACHINE_MODES; i++)
130 if (strcmp (GET_MODE_NAME (i), name) == 0)
131 return i;
132
133 fatal_with_file_and_line ("unknown mode `%s'", name);
134 }
135
136 static void
137 apply_mode_iterator (void *loc, int mode)
138 {
139 PUT_MODE ((rtx) loc, (enum machine_mode) mode);
140 }
141
142 /* Implementations of the iterator_group callbacks for codes. */
143
144 static int
145 find_code (const char *name)
146 {
147 int i;
148
149 for (i = 0; i < NUM_RTX_CODE; i++)
150 if (strcmp (GET_RTX_NAME (i), name) == 0)
151 return i;
152
153 fatal_with_file_and_line ("unknown rtx code `%s'", name);
154 }
155
156 static void
157 apply_code_iterator (void *loc, int code)
158 {
159 PUT_CODE ((rtx) loc, (enum rtx_code) code);
160 }
161
162 /* Implementations of the iterator_group callbacks for ints. */
163
164 /* Since GCC does not construct a table of valid constants,
165 we have to accept any int as valid. No cross-checking can
166 be done. */
167
168 static int
169 find_int (const char *name)
170 {
171 validate_const_int (name);
172 return atoi (name);
173 }
174
175 static void
176 apply_int_iterator (void *loc, int value)
177 {
178 *(int *)loc = value;
179 }
180
181 /* Map attribute string P to its current value. Return null if the attribute
182 isn't known. */
183
184 static struct map_value *
185 map_attr_string (const char *p)
186 {
187 const char *attr;
188 struct mapping *iterator;
189 unsigned int i;
190 struct mapping *m;
191 struct map_value *v;
192 int iterator_name_len;
193
194 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
195 attribute name. */
196 attr = strchr (p, ':');
197 if (attr == 0)
198 {
199 iterator_name_len = -1;
200 attr = p;
201 }
202 else
203 {
204 iterator_name_len = attr - p;
205 attr++;
206 }
207
208 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
209 {
210 /* If an iterator name was specified, check that it matches. */
211 if (iterator_name_len >= 0
212 && (strncmp (p, iterator->name, iterator_name_len) != 0
213 || iterator->name[iterator_name_len] != 0))
214 continue;
215
216 /* Find the attribute specification. */
217 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
218 if (m)
219 /* Find the attribute value associated with the current
220 iterator value. */
221 for (v = m->values; v; v = v->next)
222 if (v->number == iterator->current_value->number)
223 return v;
224 }
225 return NULL;
226 }
227
228 /* Apply the current iterator values to STRING. Return the new string
229 if any changes were needed, otherwise return STRING itself. */
230
231 static const char *
232 apply_iterator_to_string (const char *string)
233 {
234 char *base, *copy, *p, *start, *end;
235 struct map_value *v;
236
237 if (string == 0)
238 return string;
239
240 base = p = copy = ASTRDUP (string);
241 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
242 {
243 p = start + 1;
244
245 *end = 0;
246 v = map_attr_string (p);
247 *end = '>';
248 if (v == 0)
249 continue;
250
251 /* Add everything between the last copied byte and the '<',
252 then add in the attribute value. */
253 obstack_grow (&string_obstack, base, start - base);
254 obstack_grow (&string_obstack, v->string, strlen (v->string));
255 base = end + 1;
256 }
257 if (base != copy)
258 {
259 obstack_grow (&string_obstack, base, strlen (base) + 1);
260 copy = XOBFINISH (&string_obstack, char *);
261 copy_md_ptr_loc (copy, string);
262 return copy;
263 }
264 return string;
265 }
266
267 /* Return a deep copy of X, substituting the current iterator
268 values into any strings. */
269
270 static rtx
271 copy_rtx_for_iterators (rtx original)
272 {
273 const char *format_ptr;
274 int i, j;
275 rtx x;
276
277 if (original == 0)
278 return original;
279
280 /* Create a shallow copy of ORIGINAL. */
281 x = rtx_alloc (GET_CODE (original));
282 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
283
284 /* Change each string and recursively change each rtx. */
285 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
286 for (i = 0; format_ptr[i] != 0; i++)
287 switch (format_ptr[i])
288 {
289 case 'T':
290 XTMPL (x, i) = apply_iterator_to_string (XTMPL (x, i));
291 break;
292
293 case 'S':
294 case 's':
295 XSTR (x, i) = apply_iterator_to_string (XSTR (x, i));
296 break;
297
298 case 'e':
299 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
300 break;
301
302 case 'V':
303 case 'E':
304 if (XVEC (original, i))
305 {
306 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
307 for (j = 0; j < XVECLEN (x, i); j++)
308 XVECEXP (x, i, j)
309 = copy_rtx_for_iterators (XVECEXP (original, i, j));
310 }
311 break;
312
313 default:
314 break;
315 }
316 return x;
317 }
318
319 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
320 has the form "&& ..." (as used in define_insn_and_splits), assume that
321 EXTRA is already satisfied. Empty strings are treated like "true". */
322
323 static const char *
324 add_condition_to_string (const char *original, const char *extra)
325 {
326 if (original != 0 && original[0] == '&' && original[1] == '&')
327 return original;
328 return join_c_conditions (original, extra);
329 }
330
331 /* Like add_condition, but applied to all conditions in rtx X. */
332
333 static void
334 add_condition_to_rtx (rtx x, const char *extra)
335 {
336 switch (GET_CODE (x))
337 {
338 case DEFINE_INSN:
339 case DEFINE_EXPAND:
340 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
341 break;
342
343 case DEFINE_SPLIT:
344 case DEFINE_PEEPHOLE:
345 case DEFINE_PEEPHOLE2:
346 case DEFINE_COND_EXEC:
347 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
348 break;
349
350 case DEFINE_INSN_AND_SPLIT:
351 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
352 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
353 break;
354
355 default:
356 break;
357 }
358 }
359
360 /* Apply the current iterator values to all attribute_uses. */
361
362 static void
363 apply_attribute_uses (void)
364 {
365 struct map_value *v;
366 attribute_use *ause;
367 unsigned int i;
368
369 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
370 {
371 v = map_attr_string (ause->value);
372 if (!v)
373 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
374 ause->group->apply_iterator (ause->ptr,
375 ause->group->find_builtin (v->string));
376 }
377 }
378
379 /* A htab_traverse callback for iterators. Add all used iterators
380 to current_iterators. */
381
382 static int
383 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
384 {
385 struct mapping *iterator;
386
387 iterator = (struct mapping *) *slot;
388 if (iterator->current_value)
389 current_iterators.safe_push (iterator);
390 return 1;
391 }
392
393 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
394 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
395
396 static void
397 apply_iterators (rtx original, rtx *queue)
398 {
399 unsigned int i;
400 const char *condition;
401 iterator_use *iuse;
402 struct mapping *iterator;
403 struct map_value *v;
404 rtx x;
405
406 if (iterator_uses.is_empty ())
407 {
408 /* Raise an error if any attributes were used. */
409 apply_attribute_uses ();
410 XEXP (*queue, 0) = original;
411 XEXP (*queue, 1) = NULL_RTX;
412 return;
413 }
414
415 /* Clear out the iterators from the previous run. */
416 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
417 iterator->current_value = NULL;
418 current_iterators.truncate (0);
419
420 /* Mark the iterators that we need this time. */
421 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
422 iuse->iterator->current_value = iuse->iterator->values;
423
424 /* Get the list of iterators that are in use, preserving the
425 definition order within each group. */
426 htab_traverse (modes.iterators, add_current_iterators, NULL);
427 htab_traverse (codes.iterators, add_current_iterators, NULL);
428 htab_traverse (ints.iterators, add_current_iterators, NULL);
429 gcc_assert (!current_iterators.is_empty ());
430
431 for (;;)
432 {
433 /* Apply the current iterator values. Accumulate a condition to
434 say when the resulting rtx can be used. */
435 condition = NULL;
436 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
437 {
438 v = iuse->iterator->current_value;
439 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
440 condition = join_c_conditions (condition, v->string);
441 }
442 apply_attribute_uses ();
443 x = copy_rtx_for_iterators (original);
444 add_condition_to_rtx (x, condition);
445
446 /* Add the new rtx to the end of the queue. */
447 XEXP (*queue, 0) = x;
448 XEXP (*queue, 1) = NULL_RTX;
449
450 /* Lexicographically increment the iterator value sequence.
451 That is, cycle through iterator values, starting from the right,
452 and stopping when one of them doesn't wrap around. */
453 i = current_iterators.length ();
454 for (;;)
455 {
456 if (i == 0)
457 return;
458 i--;
459 iterator = current_iterators[i];
460 iterator->current_value = iterator->current_value->next;
461 if (iterator->current_value)
462 break;
463 iterator->current_value = iterator->values;
464 }
465
466 /* At least one more rtx to go. Allocate room for it. */
467 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
468 queue = &XEXP (*queue, 1);
469 }
470 }
471
472 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
473 of the mapping and GROUP is the group to which it belongs. */
474
475 static struct mapping *
476 add_mapping (struct iterator_group *group, htab_t table, const char *name)
477 {
478 struct mapping *m;
479 void **slot;
480
481 m = XNEW (struct mapping);
482 m->name = xstrdup (name);
483 m->group = group;
484 m->values = 0;
485 m->current_value = NULL;
486
487 slot = htab_find_slot (table, m, INSERT);
488 if (*slot != 0)
489 fatal_with_file_and_line ("`%s' already defined", name);
490
491 *slot = m;
492 return m;
493 }
494
495 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
496 END_PTR points to the current null terminator for the list; return
497 a pointer the new null terminator. */
498
499 static struct map_value **
500 add_map_value (struct map_value **end_ptr, int number, const char *string)
501 {
502 struct map_value *value;
503
504 value = XNEW (struct map_value);
505 value->next = 0;
506 value->number = number;
507 value->string = string;
508
509 *end_ptr = value;
510 return &value->next;
511 }
512
513 /* Do one-time initialization of the mode and code attributes. */
514
515 static void
516 initialize_iterators (void)
517 {
518 struct mapping *lower, *upper;
519 struct map_value **lower_ptr, **upper_ptr;
520 char *copy, *p;
521 int i;
522
523 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
524 modes.iterators = htab_create (13, leading_string_hash,
525 leading_string_eq_p, 0);
526 modes.find_builtin = find_mode;
527 modes.apply_iterator = apply_mode_iterator;
528
529 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
530 codes.iterators = htab_create (13, leading_string_hash,
531 leading_string_eq_p, 0);
532 codes.find_builtin = find_code;
533 codes.apply_iterator = apply_code_iterator;
534
535 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
536 ints.iterators = htab_create (13, leading_string_hash,
537 leading_string_eq_p, 0);
538 ints.find_builtin = find_int;
539 ints.apply_iterator = apply_int_iterator;
540
541 lower = add_mapping (&modes, modes.attrs, "mode");
542 upper = add_mapping (&modes, modes.attrs, "MODE");
543 lower_ptr = &lower->values;
544 upper_ptr = &upper->values;
545 for (i = 0; i < MAX_MACHINE_MODE; i++)
546 {
547 copy = xstrdup (GET_MODE_NAME (i));
548 for (p = copy; *p != 0; p++)
549 *p = TOLOWER (*p);
550
551 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
552 lower_ptr = add_map_value (lower_ptr, i, copy);
553 }
554
555 lower = add_mapping (&codes, codes.attrs, "code");
556 upper = add_mapping (&codes, codes.attrs, "CODE");
557 lower_ptr = &lower->values;
558 upper_ptr = &upper->values;
559 for (i = 0; i < NUM_RTX_CODE; i++)
560 {
561 copy = xstrdup (GET_RTX_NAME (i));
562 for (p = copy; *p != 0; p++)
563 *p = TOUPPER (*p);
564
565 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
566 upper_ptr = add_map_value (upper_ptr, i, copy);
567 }
568 }
569 \f
570 /* Provide a version of a function to read a long long if the system does
571 not provide one. */
572 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
573 HOST_WIDE_INT atoll (const char *);
574
575 HOST_WIDE_INT
576 atoll (const char *p)
577 {
578 int neg = 0;
579 HOST_WIDE_INT tmp_wide;
580
581 while (ISSPACE (*p))
582 p++;
583 if (*p == '-')
584 neg = 1, p++;
585 else if (*p == '+')
586 p++;
587
588 tmp_wide = 0;
589 while (ISDIGIT (*p))
590 {
591 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
592 if (new_wide < tmp_wide)
593 {
594 /* Return INT_MAX equiv on overflow. */
595 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
596 break;
597 }
598 tmp_wide = new_wide;
599 p++;
600 }
601
602 if (neg)
603 tmp_wide = -tmp_wide;
604 return tmp_wide;
605 }
606 #endif
607 \f
608 /* Process a define_conditions directive, starting with the optional
609 space after the "define_conditions". The directive looks like this:
610
611 (define_conditions [
612 (number "string")
613 (number "string")
614 ...
615 ])
616
617 It's not intended to appear in machine descriptions. It is
618 generated by (the program generated by) genconditions.c, and
619 slipped in at the beginning of the sequence of MD files read by
620 most of the other generators. */
621 static void
622 read_conditions (void)
623 {
624 int c;
625
626 c = read_skip_spaces ();
627 if (c != '[')
628 fatal_expected_char ('[', c);
629
630 while ( (c = read_skip_spaces ()) != ']')
631 {
632 struct md_name name;
633 char *expr;
634 int value;
635
636 if (c != '(')
637 fatal_expected_char ('(', c);
638
639 read_name (&name);
640 validate_const_int (name.string);
641 value = atoi (name.string);
642
643 c = read_skip_spaces ();
644 if (c != '"')
645 fatal_expected_char ('"', c);
646 expr = read_quoted_string ();
647
648 c = read_skip_spaces ();
649 if (c != ')')
650 fatal_expected_char (')', c);
651
652 add_c_test (expr, value);
653 }
654 }
655
656 static void
657 validate_const_int (const char *string)
658 {
659 const char *cp;
660 int valid = 1;
661
662 cp = string;
663 while (*cp && ISSPACE (*cp))
664 cp++;
665 if (*cp == '-' || *cp == '+')
666 cp++;
667 if (*cp == 0)
668 valid = 0;
669 for (; *cp; cp++)
670 if (! ISDIGIT (*cp))
671 valid = 0;
672 if (!valid)
673 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
674 }
675
676 /* Record that PTR uses iterator ITERATOR. */
677
678 static void
679 record_iterator_use (struct mapping *iterator, void *ptr)
680 {
681 struct iterator_use iuse = {iterator, ptr};
682 iterator_uses.safe_push (iuse);
683 }
684
685 /* Record that PTR uses attribute VALUE, which must match a built-in
686 value from group GROUP. */
687
688 static void
689 record_attribute_use (struct iterator_group *group, void *ptr,
690 const char *value)
691 {
692 struct attribute_use ause = {group, value, ptr};
693 attribute_uses.safe_push (ause);
694 }
695
696 /* Interpret NAME as either a built-in value, iterator or attribute
697 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
698 callback. */
699
700 static void
701 record_potential_iterator_use (struct iterator_group *group, void *ptr,
702 const char *name)
703 {
704 struct mapping *m;
705 size_t len;
706
707 len = strlen (name);
708 if (name[0] == '<' && name[len - 1] == '>')
709 {
710 /* Copy the attribute string into permanent storage, without the
711 angle brackets around it. */
712 obstack_grow0 (&string_obstack, name + 1, len - 2);
713 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
714 }
715 else
716 {
717 m = (struct mapping *) htab_find (group->iterators, &name);
718 if (m != 0)
719 record_iterator_use (m, ptr);
720 else
721 group->apply_iterator (ptr, group->find_builtin (name));
722 }
723 }
724
725 /* Finish reading a declaration of the form:
726
727 (define... <name> [<value1> ... <valuen>])
728
729 from the MD file, where each <valuei> is either a bare symbol name or a
730 "(<name> <string>)" pair. The "(define..." part has already been read.
731
732 Represent the declaration as a "mapping" structure; add it to TABLE
733 (which belongs to GROUP) and return it. */
734
735 static struct mapping *
736 read_mapping (struct iterator_group *group, htab_t table)
737 {
738 struct md_name name;
739 struct mapping *m;
740 struct map_value **end_ptr;
741 const char *string;
742 int number, c;
743
744 /* Read the mapping name and create a structure for it. */
745 read_name (&name);
746 m = add_mapping (group, table, name.string);
747
748 c = read_skip_spaces ();
749 if (c != '[')
750 fatal_expected_char ('[', c);
751
752 /* Read each value. */
753 end_ptr = &m->values;
754 c = read_skip_spaces ();
755 do
756 {
757 if (c != '(')
758 {
759 /* A bare symbol name that is implicitly paired to an
760 empty string. */
761 unread_char (c);
762 read_name (&name);
763 string = "";
764 }
765 else
766 {
767 /* A "(name string)" pair. */
768 read_name (&name);
769 string = read_string (false);
770 c = read_skip_spaces ();
771 if (c != ')')
772 fatal_expected_char (')', c);
773 }
774 number = group->find_builtin (name.string);
775 end_ptr = add_map_value (end_ptr, number, string);
776 c = read_skip_spaces ();
777 }
778 while (c != ']');
779
780 return m;
781 }
782
783 /* Check newly-created code iterator ITERATOR to see whether every code has the
784 same format. */
785
786 static void
787 check_code_iterator (struct mapping *iterator)
788 {
789 struct map_value *v;
790 enum rtx_code bellwether;
791
792 bellwether = (enum rtx_code) iterator->values->number;
793 for (v = iterator->values->next; v != 0; v = v->next)
794 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
795 fatal_with_file_and_line ("code iterator `%s' combines "
796 "different rtx formats", iterator->name);
797 }
798
799 /* Read an rtx-related declaration from the MD file, given that it
800 starts with directive name RTX_NAME. Return true if it expands to
801 one or more rtxes (as defined by rtx.def). When returning true,
802 store the list of rtxes as an EXPR_LIST in *X. */
803
804 bool
805 read_rtx (const char *rtx_name, rtx *x)
806 {
807 static rtx queue_head;
808
809 /* Do one-time initialization. */
810 if (queue_head == 0)
811 {
812 initialize_iterators ();
813 queue_head = rtx_alloc (EXPR_LIST);
814 }
815
816 /* Handle various rtx-related declarations that aren't themselves
817 encoded as rtxes. */
818 if (strcmp (rtx_name, "define_conditions") == 0)
819 {
820 read_conditions ();
821 return false;
822 }
823 if (strcmp (rtx_name, "define_mode_attr") == 0)
824 {
825 read_mapping (&modes, modes.attrs);
826 return false;
827 }
828 if (strcmp (rtx_name, "define_mode_iterator") == 0)
829 {
830 read_mapping (&modes, modes.iterators);
831 return false;
832 }
833 if (strcmp (rtx_name, "define_code_attr") == 0)
834 {
835 read_mapping (&codes, codes.attrs);
836 return false;
837 }
838 if (strcmp (rtx_name, "define_code_iterator") == 0)
839 {
840 check_code_iterator (read_mapping (&codes, codes.iterators));
841 return false;
842 }
843 if (strcmp (rtx_name, "define_int_attr") == 0)
844 {
845 read_mapping (&ints, ints.attrs);
846 return false;
847 }
848 if (strcmp (rtx_name, "define_int_iterator") == 0)
849 {
850 read_mapping (&ints, ints.iterators);
851 return false;
852 }
853
854 apply_iterators (read_rtx_code (rtx_name), &queue_head);
855 iterator_uses.truncate (0);
856 attribute_uses.truncate (0);
857
858 *x = queue_head;
859 return true;
860 }
861
862 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
863 either an rtx code or a code iterator. Parse the rest of the rtx and
864 return it. */
865
866 static rtx
867 read_rtx_code (const char *code_name)
868 {
869 int i;
870 RTX_CODE code;
871 struct mapping *iterator;
872 const char *format_ptr;
873 struct md_name name;
874 rtx return_rtx;
875 int c;
876 HOST_WIDE_INT tmp_wide;
877
878 /* Linked list structure for making RTXs: */
879 struct rtx_list
880 {
881 struct rtx_list *next;
882 rtx value; /* Value of this node. */
883 };
884
885 /* If this code is an iterator, build the rtx using the iterator's
886 first value. */
887 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
888 if (iterator != 0)
889 code = (enum rtx_code) iterator->values->number;
890 else
891 code = (enum rtx_code) codes.find_builtin (code_name);
892
893 /* If we end up with an insn expression then we free this space below. */
894 return_rtx = rtx_alloc (code);
895 format_ptr = GET_RTX_FORMAT (code);
896 PUT_CODE (return_rtx, code);
897
898 if (iterator)
899 record_iterator_use (iterator, return_rtx);
900
901 /* If what follows is `: mode ', read it and
902 store the mode in the rtx. */
903
904 i = read_skip_spaces ();
905 if (i == ':')
906 {
907 read_name (&name);
908 record_potential_iterator_use (&modes, return_rtx, name.string);
909 }
910 else
911 unread_char (i);
912
913 for (i = 0; format_ptr[i] != 0; i++)
914 switch (format_ptr[i])
915 {
916 /* 0 means a field for internal use only.
917 Don't expect it to be present in the input. */
918 case '0':
919 break;
920
921 case 'e':
922 case 'u':
923 XEXP (return_rtx, i) = read_nested_rtx ();
924 break;
925
926 case 'V':
927 /* 'V' is an optional vector: if a closeparen follows,
928 just store NULL for this element. */
929 c = read_skip_spaces ();
930 unread_char (c);
931 if (c == ')')
932 {
933 XVEC (return_rtx, i) = 0;
934 break;
935 }
936 /* Now process the vector. */
937
938 case 'E':
939 {
940 /* Obstack to store scratch vector in. */
941 struct obstack vector_stack;
942 int list_counter = 0;
943 rtvec return_vec = NULL_RTVEC;
944
945 c = read_skip_spaces ();
946 if (c != '[')
947 fatal_expected_char ('[', c);
948
949 /* Add expressions to a list, while keeping a count. */
950 obstack_init (&vector_stack);
951 while ((c = read_skip_spaces ()) && c != ']')
952 {
953 if (c == EOF)
954 fatal_expected_char (']', c);
955 unread_char (c);
956 list_counter++;
957 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
958 }
959 if (list_counter > 0)
960 {
961 return_vec = rtvec_alloc (list_counter);
962 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
963 list_counter * sizeof (rtx));
964 }
965 else if (format_ptr[i] == 'E')
966 fatal_with_file_and_line ("vector must have at least one element");
967 XVEC (return_rtx, i) = return_vec;
968 obstack_free (&vector_stack, NULL);
969 /* close bracket gotten */
970 }
971 break;
972
973 case 'S':
974 case 'T':
975 case 's':
976 {
977 char *stringbuf;
978 int star_if_braced;
979
980 c = read_skip_spaces ();
981 unread_char (c);
982 if (c == ')')
983 {
984 /* 'S' fields are optional and should be NULL if no string
985 was given. Also allow normal 's' and 'T' strings to be
986 omitted, treating them in the same way as empty strings. */
987 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
988 break;
989 }
990
991 /* The output template slot of a DEFINE_INSN,
992 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
993 gets a star inserted as its first character, if it is
994 written with a brace block instead of a string constant. */
995 star_if_braced = (format_ptr[i] == 'T');
996
997 stringbuf = read_string (star_if_braced);
998
999 /* For insn patterns, we want to provide a default name
1000 based on the file and line, like "*foo.md:12", if the
1001 given name is blank. These are only for define_insn and
1002 define_insn_and_split, to aid debugging. */
1003 if (*stringbuf == '\0'
1004 && i == 0
1005 && (GET_CODE (return_rtx) == DEFINE_INSN
1006 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1007 {
1008 char line_name[20];
1009 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1010 const char *slash;
1011 for (slash = fn; *slash; slash ++)
1012 if (*slash == '/' || *slash == '\\' || *slash == ':')
1013 fn = slash + 1;
1014 obstack_1grow (&string_obstack, '*');
1015 obstack_grow (&string_obstack, fn, strlen (fn));
1016 sprintf (line_name, ":%d", read_md_lineno);
1017 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1018 stringbuf = XOBFINISH (&string_obstack, char *);
1019 }
1020
1021 if (star_if_braced)
1022 XTMPL (return_rtx, i) = stringbuf;
1023 else
1024 XSTR (return_rtx, i) = stringbuf;
1025 }
1026 break;
1027
1028 case 'w':
1029 read_name (&name);
1030 validate_const_int (name.string);
1031 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1032 tmp_wide = atoi (name.string);
1033 #else
1034 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1035 tmp_wide = atol (name.string);
1036 #else
1037 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1038 But prefer not to use our hand-rolled function above either. */
1039 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1040 tmp_wide = atoll (name.string);
1041 #else
1042 tmp_wide = atoq (name.string);
1043 #endif
1044 #endif
1045 #endif
1046 XWINT (return_rtx, i) = tmp_wide;
1047 break;
1048
1049 case 'i':
1050 case 'n':
1051 /* Can be an iterator or an integer constant. */
1052 read_name (&name);
1053 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1054 name.string);
1055 break;
1056
1057 default:
1058 gcc_unreachable ();
1059 }
1060
1061 c = read_skip_spaces ();
1062 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1063 arbitrary number of arguments for them. */
1064 if (c == '('
1065 && (GET_CODE (return_rtx) == AND
1066 || GET_CODE (return_rtx) == IOR))
1067 return read_rtx_variadic (return_rtx);
1068
1069 unread_char (c);
1070 return return_rtx;
1071 }
1072
1073 /* Read a nested rtx construct from the MD file and return it. */
1074
1075 static rtx
1076 read_nested_rtx (void)
1077 {
1078 struct md_name name;
1079 int c;
1080 rtx return_rtx;
1081
1082 c = read_skip_spaces ();
1083 if (c != '(')
1084 fatal_expected_char ('(', c);
1085
1086 read_name (&name);
1087 if (strcmp (name.string, "nil") == 0)
1088 return_rtx = NULL;
1089 else
1090 return_rtx = read_rtx_code (name.string);
1091
1092 c = read_skip_spaces ();
1093 if (c != ')')
1094 fatal_expected_char (')', c);
1095
1096 return return_rtx;
1097 }
1098
1099 /* Mutually recursive subroutine of read_rtx which reads
1100 (thing x1 x2 x3 ...) and produces RTL as if
1101 (thing x1 (thing x2 (thing x3 ...))) had been written.
1102 When called, FORM is (thing x1 x2), and the file position
1103 is just past the leading parenthesis of x3. Only works
1104 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1105 static rtx
1106 read_rtx_variadic (rtx form)
1107 {
1108 char c = '(';
1109 rtx p = form, q;
1110
1111 do
1112 {
1113 unread_char (c);
1114
1115 q = rtx_alloc (GET_CODE (p));
1116 PUT_MODE (q, GET_MODE (p));
1117
1118 XEXP (q, 0) = XEXP (p, 1);
1119 XEXP (q, 1) = read_nested_rtx ();
1120
1121 XEXP (p, 1) = q;
1122 p = q;
1123 c = read_skip_spaces ();
1124 }
1125 while (c == '(');
1126 unread_char (c);
1127 return form;
1128 }