]>
Commit | Line | Data |
---|---|---|
bccafa26 | 1 | /* RTL reader for GCC. |
fbd26352 | 2 | Copyright (C) 1987-2019 Free Software Foundation, Inc. |
875d8740 | 3 | |
f12b58b3 | 4 | This file is part of GCC. |
875d8740 | 5 | |
f12b58b3 | 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 | |
8c4c00c1 | 8 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 9 | version. |
875d8740 | 10 | |
f12b58b3 | 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
875d8740 | 15 | |
16 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
875d8740 | 19 | |
836c1c68 | 20 | /* This file is compiled twice: once for the generator programs |
21 | once for the compiler. */ | |
22 | #ifdef GENERATOR_FILE | |
805e22b2 | 23 | #include "bconfig.h" |
836c1c68 | 24 | #else |
25 | #include "config.h" | |
26 | #endif | |
4f1159f8 | 27 | |
fd781bb2 | 28 | /* Disable rtl checking; it conflicts with the iterator handling. */ |
4f1159f8 | 29 | #undef ENABLE_RTL_CHECKING |
30 | ||
875d8740 | 31 | #include "system.h" |
805e22b2 | 32 | #include "coretypes.h" |
33 | #include "tm.h" | |
875d8740 | 34 | #include "rtl.h" |
35 | #include "obstack.h" | |
960ebfe7 | 36 | #include "read-md.h" |
af908c02 | 37 | #include "gensupport.h" |
875d8740 | 38 | |
836c1c68 | 39 | #ifndef GENERATOR_FILE |
40 | #include "function.h" | |
41 | #include "memmodel.h" | |
42 | #include "emit-rtl.h" | |
43 | #endif | |
44 | ||
4a293229 | 45 | /* One element in a singly-linked list of (integer, string) pairs. */ |
46 | struct map_value { | |
47 | struct map_value *next; | |
48 | int number; | |
49 | const char *string; | |
50 | }; | |
51 | ||
fd781bb2 | 52 | /* Maps an iterator or attribute name to a list of (integer, string) pairs. |
f5d566ff | 53 | The integers are iterator values; the strings are either C conditions |
4a293229 | 54 | or attribute values. */ |
55 | struct mapping { | |
fd781bb2 | 56 | /* The name of the iterator or attribute. */ |
4a293229 | 57 | const char *name; |
58 | ||
fd781bb2 | 59 | /* The group (modes or codes) to which the iterator or attribute belongs. */ |
60 | struct iterator_group *group; | |
4a293229 | 61 | |
4a293229 | 62 | /* The list of (integer, string) pairs. */ |
63 | struct map_value *values; | |
f5d566ff | 64 | |
65 | /* For iterators, records the current value of the iterator. */ | |
66 | struct map_value *current_value; | |
4a293229 | 67 | }; |
68 | ||
f5d566ff | 69 | /* A structure for abstracting the common parts of iterators. */ |
fd781bb2 | 70 | struct iterator_group { |
f5d566ff | 71 | /* Tables of "mapping" structures, one for attributes and one for |
72 | iterators. */ | |
fd781bb2 | 73 | htab_t attrs, iterators; |
4a293229 | 74 | |
60eea7fb | 75 | /* The C++ type of the iterator, such as "machine_mode" for modes. */ |
76 | const char *type; | |
77 | ||
f5d566ff | 78 | /* Treat the given string as the name of a standard mode, etc., and |
b3453c30 | 79 | return its integer value. */ |
80 | int (*find_builtin) (const char *); | |
4a293229 | 81 | |
0fdb6b5d | 82 | /* Make the given rtx use the iterator value given by the third argument. |
83 | If the iterator applies to operands, the second argument gives the | |
84 | operand index, otherwise it is ignored. */ | |
85 | void (*apply_iterator) (rtx, unsigned int, int); | |
60eea7fb | 86 | |
87 | /* Return the C token for the given standard mode, code, etc. */ | |
88 | const char *(*get_c_token) (int); | |
f5d566ff | 89 | }; |
4a293229 | 90 | |
f5d566ff | 91 | /* Records one use of an iterator. */ |
92 | struct iterator_use { | |
93 | /* The iterator itself. */ | |
94 | struct mapping *iterator; | |
95 | ||
0fdb6b5d | 96 | /* The location of the use, as passed to the apply_iterator callback. |
97 | The index is the number of the operand that used the iterator | |
98 | if applicable, otherwise it is ignored. */ | |
99 | rtx x; | |
100 | unsigned int index; | |
4a293229 | 101 | }; |
102 | ||
f5d566ff | 103 | /* Records one use of an attribute (the "<[iterator:]attribute>" syntax) |
104 | in a non-string rtx field. */ | |
105 | struct attribute_use { | |
106 | /* The group that describes the use site. */ | |
107 | struct iterator_group *group; | |
108 | ||
109 | /* The name of the attribute, possibly with an "iterator:" prefix. */ | |
110 | const char *value; | |
111 | ||
0fdb6b5d | 112 | /* The location of the use, as passed to GROUP's apply_iterator callback. |
113 | The index is the number of the operand that used the iterator | |
114 | if applicable, otherwise it is ignored. */ | |
115 | rtx x; | |
116 | unsigned int index; | |
e64e4ed9 | 117 | }; |
118 | ||
cbedf5a3 | 119 | /* This struct is used to link subst_attr named ATTR_NAME with |
120 | corresponding define_subst named ITER_NAME. */ | |
121 | struct subst_attr_to_iter_mapping | |
122 | { | |
123 | char *attr_name; | |
124 | char *iter_name; | |
125 | }; | |
126 | ||
127 | /* Hash-table to store links between subst-attributes and | |
128 | define_substs. */ | |
129 | htab_t subst_attr_to_iter_map = NULL; | |
130 | /* This global stores name of subst-iterator which is currently being | |
131 | processed. */ | |
132 | const char *current_iterator_name; | |
133 | ||
b3453c30 | 134 | static void validate_const_int (const char *); |
836c1c68 | 135 | static void one_time_initialization (void); |
4a293229 | 136 | |
d3ab2953 | 137 | /* Global singleton. */ |
138 | rtx_reader *rtx_reader_ptr = NULL; | |
139 | ||
fd781bb2 | 140 | /* The mode and code iterator structures. */ |
cbedf5a3 | 141 | static struct iterator_group modes, codes, ints, substs; |
4a293229 | 142 | |
f5d566ff | 143 | /* All iterators used in the current rtx. */ |
04009ada | 144 | static vec<mapping *> current_iterators; |
f5d566ff | 145 | |
146 | /* The list of all iterator uses in the current rtx. */ | |
f1f41a6c | 147 | static vec<iterator_use> iterator_uses; |
f5d566ff | 148 | |
149 | /* The list of all attribute uses in the current rtx. */ | |
f1f41a6c | 150 | static vec<attribute_use> attribute_uses; |
875d8740 | 151 | |
fd781bb2 | 152 | /* Implementations of the iterator_group callbacks for modes. */ |
4a293229 | 153 | |
154 | static int | |
b3453c30 | 155 | find_mode (const char *name) |
4a293229 | 156 | { |
157 | int i; | |
158 | ||
159 | for (i = 0; i < NUM_MACHINE_MODES; i++) | |
160 | if (strcmp (GET_MODE_NAME (i), name) == 0) | |
161 | return i; | |
162 | ||
b3453c30 | 163 | fatal_with_file_and_line ("unknown mode `%s'", name); |
4a293229 | 164 | } |
165 | ||
4a293229 | 166 | static void |
0fdb6b5d | 167 | apply_mode_iterator (rtx x, unsigned int, int mode) |
4a293229 | 168 | { |
0fdb6b5d | 169 | PUT_MODE (x, (machine_mode) mode); |
4a293229 | 170 | } |
171 | ||
60eea7fb | 172 | static const char * |
173 | get_mode_token (int mode) | |
174 | { | |
175 | return concat ("E_", GET_MODE_NAME (mode), "mode", NULL); | |
176 | } | |
177 | ||
836c1c68 | 178 | /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn", |
179 | "cnote" etc, and CODE_LABEL is special-cased as "clabel". */ | |
180 | ||
181 | struct compact_insn_name { | |
182 | RTX_CODE code; | |
183 | const char *name; | |
184 | }; | |
185 | ||
186 | static const compact_insn_name compact_insn_names[] = { | |
187 | { DEBUG_INSN, "cdebug_insn" }, | |
188 | { INSN, "cinsn" }, | |
189 | { JUMP_INSN, "cjump_insn" }, | |
190 | { CALL_INSN, "ccall_insn" }, | |
191 | { JUMP_TABLE_DATA, "cjump_table_data" }, | |
192 | { BARRIER, "cbarrier" }, | |
193 | { CODE_LABEL, "clabel" }, | |
194 | { NOTE, "cnote" } | |
195 | }; | |
196 | ||
c08a1447 | 197 | /* Return the rtx code for NAME, or UNKNOWN if NAME isn't a valid rtx code. */ |
4a293229 | 198 | |
c08a1447 | 199 | static rtx_code |
200 | maybe_find_code (const char *name) | |
4a293229 | 201 | { |
c08a1447 | 202 | for (int i = 0; i < NUM_RTX_CODE; i++) |
4a293229 | 203 | if (strcmp (GET_RTX_NAME (i), name) == 0) |
c08a1447 | 204 | return (rtx_code) i; |
4a293229 | 205 | |
c08a1447 | 206 | for (int i = 0; i < (signed)ARRAY_SIZE (compact_insn_names); i++) |
836c1c68 | 207 | if (strcmp (compact_insn_names[i].name, name) == 0) |
208 | return compact_insn_names[i].code; | |
209 | ||
c08a1447 | 210 | return UNKNOWN; |
211 | } | |
212 | ||
213 | /* Implementations of the iterator_group callbacks for codes. */ | |
214 | ||
215 | static int | |
216 | find_code (const char *name) | |
217 | { | |
218 | rtx_code code = maybe_find_code (name); | |
219 | if (code == UNKNOWN) | |
220 | fatal_with_file_and_line ("unknown rtx code `%s'", name); | |
221 | return code; | |
4a293229 | 222 | } |
223 | ||
4a293229 | 224 | static void |
0fdb6b5d | 225 | apply_code_iterator (rtx x, unsigned int, int code) |
4a293229 | 226 | { |
0fdb6b5d | 227 | PUT_CODE (x, (enum rtx_code) code); |
4a293229 | 228 | } |
229 | ||
60eea7fb | 230 | static const char * |
231 | get_code_token (int code) | |
232 | { | |
233 | char *name = xstrdup (GET_RTX_NAME (code)); | |
234 | for (int i = 0; name[i]; ++i) | |
235 | name[i] = TOUPPER (name[i]); | |
236 | return name; | |
237 | } | |
238 | ||
65729bd0 | 239 | /* Implementations of the iterator_group callbacks for ints. */ |
240 | ||
241 | /* Since GCC does not construct a table of valid constants, | |
242 | we have to accept any int as valid. No cross-checking can | |
243 | be done. */ | |
244 | ||
245 | static int | |
246 | find_int (const char *name) | |
247 | { | |
248 | validate_const_int (name); | |
249 | return atoi (name); | |
250 | } | |
251 | ||
252 | static void | |
0fdb6b5d | 253 | apply_int_iterator (rtx x, unsigned int index, int value) |
65729bd0 | 254 | { |
9edf7ea8 | 255 | if (GET_CODE (x) == SUBREG) |
256 | SUBREG_BYTE (x) = value; | |
257 | else | |
258 | XINT (x, index) = value; | |
65729bd0 | 259 | } |
260 | ||
60eea7fb | 261 | static const char * |
262 | get_int_token (int value) | |
263 | { | |
264 | char buffer[HOST_BITS_PER_INT + 1]; | |
265 | sprintf (buffer, "%d", value); | |
266 | return xstrdup (buffer); | |
267 | } | |
268 | ||
836c1c68 | 269 | #ifdef GENERATOR_FILE |
270 | ||
cbedf5a3 | 271 | /* This routine adds attribute or does nothing depending on VALUE. When |
272 | VALUE is 1, it does nothing - the first duplicate of original | |
273 | template is kept untouched when it's subjected to a define_subst. | |
0fdb6b5d | 274 | When VALUE isn't 1, the routine modifies RTL-template RT, adding |
cbedf5a3 | 275 | attribute, named exactly as define_subst, which later will be |
276 | applied. If such attribute has already been added, then no the | |
277 | routine has no effect. */ | |
278 | static void | |
0fdb6b5d | 279 | apply_subst_iterator (rtx rt, unsigned int, int value) |
cbedf5a3 | 280 | { |
cbedf5a3 | 281 | rtx new_attr; |
282 | rtvec attrs_vec, new_attrs_vec; | |
283 | int i; | |
2f9f8666 | 284 | /* define_split has no attributes. */ |
285 | if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT) | |
cbedf5a3 | 286 | return; |
287 | gcc_assert (GET_CODE (rt) == DEFINE_INSN | |
2f9f8666 | 288 | || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT |
e8c17f8e | 289 | || GET_CODE (rt) == DEFINE_INSN_AND_REWRITE |
cbedf5a3 | 290 | || GET_CODE (rt) == DEFINE_EXPAND); |
291 | ||
e8c17f8e | 292 | int attrs = (GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7 |
293 | : GET_CODE (rt) == DEFINE_INSN_AND_REWRITE ? 6 : 4); | |
2f9f8666 | 294 | attrs_vec = XVEC (rt, attrs); |
cbedf5a3 | 295 | |
296 | /* If we've already added attribute 'current_iterator_name', then we | |
297 | have nothing to do now. */ | |
298 | if (attrs_vec) | |
299 | { | |
300 | for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++) | |
301 | { | |
302 | if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0) | |
303 | return; | |
304 | } | |
305 | } | |
306 | ||
307 | /* Add attribute with subst name - it serves as a mark for | |
308 | define_subst which later would be applied to this pattern. */ | |
309 | new_attr = rtx_alloc (SET_ATTR); | |
310 | PUT_CODE (new_attr, SET_ATTR); | |
311 | XSTR (new_attr, 0) = xstrdup (current_iterator_name); | |
312 | XSTR (new_attr, 1) = xstrdup ("yes"); | |
313 | ||
314 | if (!attrs_vec) | |
315 | { | |
316 | new_attrs_vec = rtvec_alloc (1); | |
317 | new_attrs_vec->elem[0] = new_attr; | |
318 | } | |
319 | else | |
320 | { | |
321 | new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1); | |
322 | memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0], | |
323 | GET_NUM_ELEM (attrs_vec) * sizeof (rtx)); | |
324 | new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr; | |
325 | } | |
2f9f8666 | 326 | XVEC (rt, attrs) = new_attrs_vec; |
cbedf5a3 | 327 | } |
328 | ||
329 | /* Map subst-attribute ATTR to subst iterator ITER. */ | |
330 | ||
331 | static void | |
332 | bind_subst_iter_and_attr (const char *iter, const char *attr) | |
333 | { | |
334 | struct subst_attr_to_iter_mapping *value; | |
335 | void **slot; | |
336 | if (!subst_attr_to_iter_map) | |
337 | subst_attr_to_iter_map = | |
338 | htab_create (1, leading_string_hash, leading_string_eq_p, 0); | |
339 | value = XNEW (struct subst_attr_to_iter_mapping); | |
340 | value->attr_name = xstrdup (attr); | |
341 | value->iter_name = xstrdup (iter); | |
342 | slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT); | |
343 | *slot = value; | |
344 | } | |
345 | ||
836c1c68 | 346 | #endif /* #ifdef GENERATOR_FILE */ |
347 | ||
cbedf5a3 | 348 | /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */ |
349 | ||
350 | static char* | |
351 | find_subst_iter_by_attr (const char *attr) | |
352 | { | |
353 | char *iter_name = NULL; | |
354 | struct subst_attr_to_iter_mapping *value; | |
355 | value = (struct subst_attr_to_iter_mapping*) | |
356 | htab_find (subst_attr_to_iter_map, &attr); | |
357 | if (value) | |
358 | iter_name = value->iter_name; | |
359 | return iter_name; | |
360 | } | |
361 | ||
f5d566ff | 362 | /* Map attribute string P to its current value. Return null if the attribute |
60eea7fb | 363 | isn't known. If ITERATOR_OUT is nonnull, store the associated iterator |
364 | there. */ | |
e64e4ed9 | 365 | |
366 | static struct map_value * | |
60eea7fb | 367 | map_attr_string (const char *p, mapping **iterator_out = 0) |
e64e4ed9 | 368 | { |
369 | const char *attr; | |
f5d566ff | 370 | struct mapping *iterator; |
371 | unsigned int i; | |
e64e4ed9 | 372 | struct mapping *m; |
373 | struct map_value *v; | |
f5d566ff | 374 | int iterator_name_len; |
e64e4ed9 | 375 | |
f5d566ff | 376 | /* Peel off any "iterator:" prefix. Set ATTR to the start of the |
377 | attribute name. */ | |
e64e4ed9 | 378 | attr = strchr (p, ':'); |
379 | if (attr == 0) | |
f5d566ff | 380 | { |
381 | iterator_name_len = -1; | |
382 | attr = p; | |
383 | } | |
e64e4ed9 | 384 | else |
385 | { | |
f5d566ff | 386 | iterator_name_len = attr - p; |
e64e4ed9 | 387 | attr++; |
388 | } | |
389 | ||
f1f41a6c | 390 | FOR_EACH_VEC_ELT (current_iterators, i, iterator) |
e64e4ed9 | 391 | { |
f5d566ff | 392 | /* If an iterator name was specified, check that it matches. */ |
393 | if (iterator_name_len >= 0 | |
394 | && (strncmp (p, iterator->name, iterator_name_len) != 0 | |
395 | || iterator->name[iterator_name_len] != 0)) | |
396 | continue; | |
e64e4ed9 | 397 | |
f5d566ff | 398 | /* Find the attribute specification. */ |
399 | m = (struct mapping *) htab_find (iterator->group->attrs, &attr); | |
400 | if (m) | |
cbedf5a3 | 401 | { |
402 | /* In contrast to code/mode/int iterators, attributes of subst | |
403 | iterators are linked to one specific subst-iterator. So, if | |
404 | we are dealing with subst-iterator, we should check if it's | |
405 | the one which linked with the given attribute. */ | |
406 | if (iterator->group == &substs) | |
407 | { | |
408 | char *iter_name = find_subst_iter_by_attr (attr); | |
409 | if (strcmp (iter_name, iterator->name) != 0) | |
410 | continue; | |
411 | } | |
412 | /* Find the attribute value associated with the current | |
413 | iterator value. */ | |
414 | for (v = m->values; v; v = v->next) | |
415 | if (v->number == iterator->current_value->number) | |
60eea7fb | 416 | { |
417 | if (iterator_out) | |
418 | *iterator_out = iterator; | |
419 | return v; | |
420 | } | |
cbedf5a3 | 421 | } |
e64e4ed9 | 422 | } |
f5d566ff | 423 | return NULL; |
e64e4ed9 | 424 | } |
425 | ||
f5d566ff | 426 | /* Apply the current iterator values to STRING. Return the new string |
427 | if any changes were needed, otherwise return STRING itself. */ | |
4a293229 | 428 | |
e1e9159b | 429 | const char * |
d3ab2953 | 430 | md_reader::apply_iterator_to_string (const char *string) |
4a293229 | 431 | { |
e64e4ed9 | 432 | char *base, *copy, *p, *start, *end; |
4a293229 | 433 | struct map_value *v; |
434 | ||
435 | if (string == 0) | |
436 | return string; | |
437 | ||
438 | base = p = copy = ASTRDUP (string); | |
192342fa | 439 | while ((start = strchr (p, '<')) && (end = strchr (start, '>'))) |
4a293229 | 440 | { |
441 | p = start + 1; | |
442 | ||
4a293229 | 443 | *end = 0; |
f5d566ff | 444 | v = map_attr_string (p); |
4a293229 | 445 | *end = '>'; |
4a293229 | 446 | if (v == 0) |
447 | continue; | |
448 | ||
449 | /* Add everything between the last copied byte and the '<', | |
450 | then add in the attribute value. */ | |
e1e9159b | 451 | obstack_grow (&m_string_obstack, base, start - base); |
452 | obstack_grow (&m_string_obstack, v->string, strlen (v->string)); | |
4a293229 | 453 | base = end + 1; |
454 | } | |
455 | if (base != copy) | |
456 | { | |
e1e9159b | 457 | obstack_grow (&m_string_obstack, base, strlen (base) + 1); |
458 | copy = XOBFINISH (&m_string_obstack, char *); | |
77c2564f | 459 | copy_md_ptr_loc (copy, string); |
9e959634 | 460 | return copy; |
4a293229 | 461 | } |
462 | return string; | |
463 | } | |
464 | ||
f5d566ff | 465 | /* Return a deep copy of X, substituting the current iterator |
466 | values into any strings. */ | |
4a293229 | 467 | |
e1e9159b | 468 | rtx |
d3ab2953 | 469 | md_reader::copy_rtx_for_iterators (rtx original) |
4a293229 | 470 | { |
873bf3e1 | 471 | const char *format_ptr, *p; |
4a293229 | 472 | int i, j; |
473 | rtx x; | |
4a293229 | 474 | |
475 | if (original == 0) | |
476 | return original; | |
477 | ||
478 | /* Create a shallow copy of ORIGINAL. */ | |
f5d566ff | 479 | x = rtx_alloc (GET_CODE (original)); |
480 | memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original))); | |
e64e4ed9 | 481 | |
4a293229 | 482 | /* Change each string and recursively change each rtx. */ |
f5d566ff | 483 | format_ptr = GET_RTX_FORMAT (GET_CODE (original)); |
4a293229 | 484 | for (i = 0; format_ptr[i] != 0; i++) |
485 | switch (format_ptr[i]) | |
486 | { | |
4a293229 | 487 | case 'T': |
873bf3e1 | 488 | while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i)))) |
489 | XTMPL (x, i) = p; | |
4f1159f8 | 490 | break; |
491 | ||
492 | case 'S': | |
4a293229 | 493 | case 's': |
873bf3e1 | 494 | while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i)))) |
495 | XSTR (x, i) = p; | |
4a293229 | 496 | break; |
497 | ||
498 | case 'e': | |
f5d566ff | 499 | XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i)); |
4a293229 | 500 | break; |
501 | ||
502 | case 'V': | |
503 | case 'E': | |
504 | if (XVEC (original, i)) | |
505 | { | |
506 | XVEC (x, i) = rtvec_alloc (XVECLEN (original, i)); | |
507 | for (j = 0; j < XVECLEN (x, i); j++) | |
f5d566ff | 508 | XVECEXP (x, i, j) |
509 | = copy_rtx_for_iterators (XVECEXP (original, i, j)); | |
4a293229 | 510 | } |
511 | break; | |
512 | ||
513 | default: | |
514 | break; | |
515 | } | |
516 | return x; | |
517 | } | |
518 | ||
836c1c68 | 519 | #ifdef GENERATOR_FILE |
520 | ||
4a293229 | 521 | /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL |
522 | has the form "&& ..." (as used in define_insn_and_splits), assume that | |
523 | EXTRA is already satisfied. Empty strings are treated like "true". */ | |
524 | ||
525 | static const char * | |
526 | add_condition_to_string (const char *original, const char *extra) | |
527 | { | |
9e959634 | 528 | if (original != 0 && original[0] == '&' && original[1] == '&') |
4a293229 | 529 | return original; |
e1e9159b | 530 | return rtx_reader_ptr->join_c_conditions (original, extra); |
4a293229 | 531 | } |
532 | ||
533 | /* Like add_condition, but applied to all conditions in rtx X. */ | |
534 | ||
535 | static void | |
536 | add_condition_to_rtx (rtx x, const char *extra) | |
537 | { | |
538 | switch (GET_CODE (x)) | |
539 | { | |
540 | case DEFINE_INSN: | |
541 | case DEFINE_EXPAND: | |
cbedf5a3 | 542 | case DEFINE_SUBST: |
4a293229 | 543 | XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra); |
544 | break; | |
545 | ||
546 | case DEFINE_SPLIT: | |
547 | case DEFINE_PEEPHOLE: | |
548 | case DEFINE_PEEPHOLE2: | |
549 | case DEFINE_COND_EXEC: | |
550 | XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra); | |
551 | break; | |
552 | ||
553 | case DEFINE_INSN_AND_SPLIT: | |
e8c17f8e | 554 | case DEFINE_INSN_AND_REWRITE: |
4a293229 | 555 | XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra); |
556 | XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra); | |
557 | break; | |
558 | ||
559 | default: | |
560 | break; | |
561 | } | |
562 | } | |
563 | ||
f5d566ff | 564 | /* Apply the current iterator values to all attribute_uses. */ |
565 | ||
566 | static void | |
567 | apply_attribute_uses (void) | |
568 | { | |
569 | struct map_value *v; | |
570 | attribute_use *ause; | |
571 | unsigned int i; | |
572 | ||
f1f41a6c | 573 | FOR_EACH_VEC_ELT (attribute_uses, i, ause) |
f5d566ff | 574 | { |
575 | v = map_attr_string (ause->value); | |
576 | if (!v) | |
577 | fatal_with_file_and_line ("unknown iterator value `%s'", ause->value); | |
0fdb6b5d | 578 | ause->group->apply_iterator (ause->x, ause->index, |
f5d566ff | 579 | ause->group->find_builtin (v->string)); |
580 | } | |
581 | } | |
582 | ||
583 | /* A htab_traverse callback for iterators. Add all used iterators | |
584 | to current_iterators. */ | |
4a293229 | 585 | |
586 | static int | |
f5d566ff | 587 | add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED) |
4a293229 | 588 | { |
fd781bb2 | 589 | struct mapping *iterator; |
4a293229 | 590 | |
fd781bb2 | 591 | iterator = (struct mapping *) *slot; |
f5d566ff | 592 | if (iterator->current_value) |
f1f41a6c | 593 | current_iterators.safe_push (iterator); |
4a293229 | 594 | return 1; |
595 | } | |
596 | ||
60eea7fb | 597 | /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't |
598 | be many instances of two overloaded_names having the same name but | |
599 | different arguments, so hashing on the name should be good enough in | |
600 | practice. */ | |
601 | ||
602 | static hashval_t | |
603 | overloaded_name_hash (const void *uncast_oname) | |
604 | { | |
605 | const overloaded_name *oname = (const overloaded_name *) uncast_oname; | |
606 | return htab_hash_string (oname->name); | |
607 | } | |
608 | ||
609 | /* Return true if two overloaded_names are similar enough to share | |
610 | the same generated functions. */ | |
611 | ||
612 | static int | |
613 | overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2) | |
614 | { | |
615 | const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1; | |
616 | const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2; | |
617 | if (strcmp (oname1->name, oname2->name) != 0 | |
618 | || oname1->arg_types.length () != oname2->arg_types.length ()) | |
619 | return 0; | |
620 | ||
621 | for (unsigned int i = 0; i < oname1->arg_types.length (); ++i) | |
622 | if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0) | |
623 | return 0; | |
624 | ||
625 | return 1; | |
626 | } | |
627 | ||
628 | /* Return true if X has an instruction name in XSTR (X, 0). */ | |
629 | ||
630 | static bool | |
631 | named_rtx_p (rtx x) | |
632 | { | |
633 | switch (GET_CODE (x)) | |
634 | { | |
635 | case DEFINE_EXPAND: | |
636 | case DEFINE_INSN: | |
637 | case DEFINE_INSN_AND_SPLIT: | |
e8c17f8e | 638 | case DEFINE_INSN_AND_REWRITE: |
60eea7fb | 639 | return true; |
640 | ||
641 | default: | |
642 | return false; | |
643 | } | |
644 | } | |
645 | ||
646 | /* Check whether ORIGINAL is a named pattern whose name starts with '@'. | |
647 | If so, return the associated overloaded_name and add the iterator for | |
648 | each argument to ITERATORS. Return null otherwise. */ | |
649 | ||
650 | overloaded_name * | |
651 | md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators) | |
652 | { | |
653 | /* Check for the leading '@'. */ | |
654 | if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@') | |
655 | return NULL; | |
656 | ||
657 | /* Remove the '@', so that no other code needs to worry about it. */ | |
658 | const char *name = XSTR (original, 0); | |
659 | copy_md_ptr_loc (name + 1, name); | |
660 | name += 1; | |
661 | XSTR (original, 0) = name; | |
662 | ||
663 | /* Build a copy of the name without the '<...>' attribute strings. | |
664 | Add the iterator associated with each such attribute string to ITERATORS | |
665 | and add an associated argument to TMP_ONAME. */ | |
666 | char *copy = ASTRDUP (name); | |
667 | char *base = copy, *start, *end; | |
668 | overloaded_name tmp_oname; | |
669 | tmp_oname.arg_types.create (current_iterators.length ()); | |
670 | bool pending_underscore_p = false; | |
671 | while ((start = strchr (base, '<')) && (end = strchr (start, '>'))) | |
672 | { | |
673 | *end = 0; | |
674 | mapping *iterator; | |
675 | if (!map_attr_string (start + 1, &iterator)) | |
676 | fatal_with_file_and_line ("unknown iterator `%s'", start + 1); | |
677 | *end = '>'; | |
678 | ||
679 | /* Remove a trailing underscore, so that we don't end a name | |
680 | with "_" or turn "_<...>_" into "__". */ | |
681 | if (start != base && start[-1] == '_') | |
682 | { | |
683 | start -= 1; | |
684 | pending_underscore_p = true; | |
685 | } | |
686 | ||
687 | /* Add the text between either the last '>' or the start of | |
688 | the string and this '<'. */ | |
689 | obstack_grow (&m_string_obstack, base, start - base); | |
690 | base = end + 1; | |
691 | ||
692 | /* If there's a character we need to keep after the '>', check | |
693 | whether we should prefix it with a previously-dropped '_'. */ | |
694 | if (base[0] != 0 && base[0] != '<') | |
695 | { | |
696 | if (pending_underscore_p && base[0] != '_') | |
697 | obstack_1grow (&m_string_obstack, '_'); | |
698 | pending_underscore_p = false; | |
699 | } | |
700 | ||
701 | /* Record an argument for ITERATOR. */ | |
702 | iterators->safe_push (iterator); | |
703 | tmp_oname.arg_types.safe_push (iterator->group->type); | |
704 | } | |
705 | if (base == copy) | |
706 | fatal_with_file_and_line ("no iterator attributes in name `%s'", name); | |
707 | ||
708 | size_t length = obstack_object_size (&m_string_obstack); | |
709 | if (length == 0) | |
710 | fatal_with_file_and_line ("`%s' only contains iterator attributes", name); | |
711 | ||
712 | /* Get the completed name. */ | |
713 | obstack_grow (&m_string_obstack, base, strlen (base) + 1); | |
714 | char *new_name = XOBFINISH (&m_string_obstack, char *); | |
715 | tmp_oname.name = new_name; | |
716 | ||
717 | if (!m_overloads_htab) | |
718 | m_overloads_htab = htab_create (31, overloaded_name_hash, | |
719 | overloaded_name_eq_p, NULL); | |
720 | ||
721 | /* See whether another pattern had the same overload name and list | |
722 | of argument types. Create a new permanent one if not. */ | |
723 | void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT); | |
724 | overloaded_name *oname = (overloaded_name *) *slot; | |
725 | if (!oname) | |
726 | { | |
727 | *slot = oname = new overloaded_name; | |
728 | oname->name = tmp_oname.name; | |
729 | oname->arg_types = tmp_oname.arg_types; | |
730 | oname->next = NULL; | |
731 | oname->first_instance = NULL; | |
732 | oname->next_instance_ptr = &oname->first_instance; | |
733 | ||
734 | *m_next_overload_ptr = oname; | |
735 | m_next_overload_ptr = &oname->next; | |
736 | } | |
737 | else | |
738 | { | |
739 | obstack_free (&m_string_obstack, new_name); | |
740 | tmp_oname.arg_types.release (); | |
741 | } | |
742 | ||
743 | return oname; | |
744 | } | |
745 | ||
746 | /* Add an instance of ONAME for instruction pattern X. ITERATORS[I] | |
747 | gives the iterator associated with argument I of ONAME. */ | |
748 | ||
749 | static void | |
750 | add_overload_instance (overloaded_name *oname, vec<mapping *> iterators, rtx x) | |
751 | { | |
752 | /* Create the instance. */ | |
753 | overloaded_instance *instance = new overloaded_instance; | |
754 | instance->next = NULL; | |
755 | instance->arg_values.create (oname->arg_types.length ()); | |
756 | for (unsigned int i = 0; i < iterators.length (); ++i) | |
757 | { | |
758 | int value = iterators[i]->current_value->number; | |
759 | const char *name = iterators[i]->group->get_c_token (value); | |
760 | instance->arg_values.quick_push (name); | |
761 | } | |
762 | instance->name = XSTR (x, 0); | |
763 | instance->insn = x; | |
764 | ||
765 | /* Chain it onto the end of ONAME's list. */ | |
766 | *oname->next_instance_ptr = instance; | |
767 | oname->next_instance_ptr = &instance->next; | |
768 | } | |
769 | ||
f5d566ff | 770 | /* Expand all iterators in the current rtx, which is given as ORIGINAL. |
771 | Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */ | |
772 | ||
773 | static void | |
b0e2a5e8 | 774 | apply_iterators (rtx original, vec<rtx> *queue) |
f5d566ff | 775 | { |
776 | unsigned int i; | |
777 | const char *condition; | |
778 | iterator_use *iuse; | |
779 | struct mapping *iterator; | |
780 | struct map_value *v; | |
781 | rtx x; | |
782 | ||
f1f41a6c | 783 | if (iterator_uses.is_empty ()) |
f5d566ff | 784 | { |
785 | /* Raise an error if any attributes were used. */ | |
786 | apply_attribute_uses (); | |
60eea7fb | 787 | |
788 | if (named_rtx_p (original) && XSTR (original, 0)[0] == '@') | |
789 | fatal_with_file_and_line ("'@' used without iterators"); | |
790 | ||
b0e2a5e8 | 791 | queue->safe_push (original); |
f5d566ff | 792 | return; |
793 | } | |
794 | ||
795 | /* Clear out the iterators from the previous run. */ | |
f1f41a6c | 796 | FOR_EACH_VEC_ELT (current_iterators, i, iterator) |
f5d566ff | 797 | iterator->current_value = NULL; |
f1f41a6c | 798 | current_iterators.truncate (0); |
f5d566ff | 799 | |
800 | /* Mark the iterators that we need this time. */ | |
f1f41a6c | 801 | FOR_EACH_VEC_ELT (iterator_uses, i, iuse) |
f5d566ff | 802 | iuse->iterator->current_value = iuse->iterator->values; |
803 | ||
804 | /* Get the list of iterators that are in use, preserving the | |
805 | definition order within each group. */ | |
806 | htab_traverse (modes.iterators, add_current_iterators, NULL); | |
807 | htab_traverse (codes.iterators, add_current_iterators, NULL); | |
65729bd0 | 808 | htab_traverse (ints.iterators, add_current_iterators, NULL); |
cbedf5a3 | 809 | htab_traverse (substs.iterators, add_current_iterators, NULL); |
f1f41a6c | 810 | gcc_assert (!current_iterators.is_empty ()); |
f5d566ff | 811 | |
60eea7fb | 812 | /* Check whether this is a '@' overloaded pattern. */ |
813 | auto_vec<mapping *, 16> iterators; | |
814 | overloaded_name *oname | |
815 | = rtx_reader_ptr->handle_overloaded_name (original, &iterators); | |
816 | ||
f5d566ff | 817 | for (;;) |
818 | { | |
819 | /* Apply the current iterator values. Accumulate a condition to | |
820 | say when the resulting rtx can be used. */ | |
4441642c | 821 | condition = ""; |
f1f41a6c | 822 | FOR_EACH_VEC_ELT (iterator_uses, i, iuse) |
f5d566ff | 823 | { |
cbedf5a3 | 824 | if (iuse->iterator->group == &substs) |
825 | continue; | |
f5d566ff | 826 | v = iuse->iterator->current_value; |
0fdb6b5d | 827 | iuse->iterator->group->apply_iterator (iuse->x, iuse->index, |
828 | v->number); | |
e1e9159b | 829 | condition = rtx_reader_ptr->join_c_conditions (condition, v->string); |
f5d566ff | 830 | } |
831 | apply_attribute_uses (); | |
e1e9159b | 832 | x = rtx_reader_ptr->copy_rtx_for_iterators (original); |
f5d566ff | 833 | add_condition_to_rtx (x, condition); |
834 | ||
cbedf5a3 | 835 | /* We apply subst iterator after RTL-template is copied, as during |
836 | subst-iterator processing, we could add an attribute to the | |
837 | RTL-template, and we don't want to do it in the original one. */ | |
838 | FOR_EACH_VEC_ELT (iterator_uses, i, iuse) | |
839 | { | |
840 | v = iuse->iterator->current_value; | |
841 | if (iuse->iterator->group == &substs) | |
842 | { | |
0fdb6b5d | 843 | iuse->x = x; |
844 | iuse->index = 0; | |
cbedf5a3 | 845 | current_iterator_name = iuse->iterator->name; |
0fdb6b5d | 846 | iuse->iterator->group->apply_iterator (iuse->x, iuse->index, |
847 | v->number); | |
cbedf5a3 | 848 | } |
849 | } | |
60eea7fb | 850 | |
851 | if (oname) | |
852 | add_overload_instance (oname, iterators, x); | |
853 | ||
f5d566ff | 854 | /* Add the new rtx to the end of the queue. */ |
b0e2a5e8 | 855 | queue->safe_push (x); |
f5d566ff | 856 | |
857 | /* Lexicographically increment the iterator value sequence. | |
858 | That is, cycle through iterator values, starting from the right, | |
859 | and stopping when one of them doesn't wrap around. */ | |
f1f41a6c | 860 | i = current_iterators.length (); |
f5d566ff | 861 | for (;;) |
862 | { | |
863 | if (i == 0) | |
864 | return; | |
865 | i--; | |
f1f41a6c | 866 | iterator = current_iterators[i]; |
f5d566ff | 867 | iterator->current_value = iterator->current_value->next; |
868 | if (iterator->current_value) | |
869 | break; | |
870 | iterator->current_value = iterator->values; | |
871 | } | |
f5d566ff | 872 | } |
873 | } | |
836c1c68 | 874 | #endif /* #ifdef GENERATOR_FILE */ |
f5d566ff | 875 | |
4a293229 | 876 | /* Add a new "mapping" structure to hashtable TABLE. NAME is the name |
b3453c30 | 877 | of the mapping and GROUP is the group to which it belongs. */ |
4a293229 | 878 | |
879 | static struct mapping * | |
b3453c30 | 880 | add_mapping (struct iterator_group *group, htab_t table, const char *name) |
4a293229 | 881 | { |
882 | struct mapping *m; | |
883 | void **slot; | |
884 | ||
885 | m = XNEW (struct mapping); | |
886 | m->name = xstrdup (name); | |
887 | m->group = group; | |
4a293229 | 888 | m->values = 0; |
f5d566ff | 889 | m->current_value = NULL; |
4a293229 | 890 | |
891 | slot = htab_find_slot (table, m, INSERT); | |
892 | if (*slot != 0) | |
b3453c30 | 893 | fatal_with_file_and_line ("`%s' already defined", name); |
4a293229 | 894 | |
895 | *slot = m; | |
896 | return m; | |
897 | } | |
898 | ||
899 | /* Add the pair (NUMBER, STRING) to a list of map_value structures. | |
900 | END_PTR points to the current null terminator for the list; return | |
901 | a pointer the new null terminator. */ | |
902 | ||
903 | static struct map_value ** | |
904 | add_map_value (struct map_value **end_ptr, int number, const char *string) | |
905 | { | |
906 | struct map_value *value; | |
907 | ||
908 | value = XNEW (struct map_value); | |
909 | value->next = 0; | |
910 | value->number = number; | |
911 | value->string = string; | |
912 | ||
913 | *end_ptr = value; | |
914 | return &value->next; | |
915 | } | |
916 | ||
917 | /* Do one-time initialization of the mode and code attributes. */ | |
918 | ||
919 | static void | |
fd781bb2 | 920 | initialize_iterators (void) |
4a293229 | 921 | { |
922 | struct mapping *lower, *upper; | |
923 | struct map_value **lower_ptr, **upper_ptr; | |
924 | char *copy, *p; | |
925 | int i; | |
926 | ||
ac0640e5 | 927 | modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); |
928 | modes.iterators = htab_create (13, leading_string_hash, | |
929 | leading_string_eq_p, 0); | |
60eea7fb | 930 | modes.type = "machine_mode"; |
4a293229 | 931 | modes.find_builtin = find_mode; |
fd781bb2 | 932 | modes.apply_iterator = apply_mode_iterator; |
60eea7fb | 933 | modes.get_c_token = get_mode_token; |
4a293229 | 934 | |
ac0640e5 | 935 | codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); |
936 | codes.iterators = htab_create (13, leading_string_hash, | |
937 | leading_string_eq_p, 0); | |
60eea7fb | 938 | codes.type = "rtx_code"; |
4a293229 | 939 | codes.find_builtin = find_code; |
fd781bb2 | 940 | codes.apply_iterator = apply_code_iterator; |
60eea7fb | 941 | codes.get_c_token = get_code_token; |
4a293229 | 942 | |
65729bd0 | 943 | ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); |
944 | ints.iterators = htab_create (13, leading_string_hash, | |
945 | leading_string_eq_p, 0); | |
60eea7fb | 946 | ints.type = "int"; |
65729bd0 | 947 | ints.find_builtin = find_int; |
948 | ints.apply_iterator = apply_int_iterator; | |
60eea7fb | 949 | ints.get_c_token = get_int_token; |
65729bd0 | 950 | |
cbedf5a3 | 951 | substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0); |
952 | substs.iterators = htab_create (13, leading_string_hash, | |
953 | leading_string_eq_p, 0); | |
60eea7fb | 954 | substs.type = "int"; |
cbedf5a3 | 955 | substs.find_builtin = find_int; /* We don't use it, anyway. */ |
836c1c68 | 956 | #ifdef GENERATOR_FILE |
cbedf5a3 | 957 | substs.apply_iterator = apply_subst_iterator; |
836c1c68 | 958 | #endif |
60eea7fb | 959 | substs.get_c_token = get_int_token; |
cbedf5a3 | 960 | |
b3453c30 | 961 | lower = add_mapping (&modes, modes.attrs, "mode"); |
962 | upper = add_mapping (&modes, modes.attrs, "MODE"); | |
4a293229 | 963 | lower_ptr = &lower->values; |
964 | upper_ptr = &upper->values; | |
965 | for (i = 0; i < MAX_MACHINE_MODE; i++) | |
966 | { | |
967 | copy = xstrdup (GET_MODE_NAME (i)); | |
968 | for (p = copy; *p != 0; p++) | |
969 | *p = TOLOWER (*p); | |
970 | ||
971 | upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i)); | |
972 | lower_ptr = add_map_value (lower_ptr, i, copy); | |
973 | } | |
974 | ||
b3453c30 | 975 | lower = add_mapping (&codes, codes.attrs, "code"); |
976 | upper = add_mapping (&codes, codes.attrs, "CODE"); | |
4a293229 | 977 | lower_ptr = &lower->values; |
978 | upper_ptr = &upper->values; | |
979 | for (i = 0; i < NUM_RTX_CODE; i++) | |
980 | { | |
981 | copy = xstrdup (GET_RTX_NAME (i)); | |
982 | for (p = copy; *p != 0; p++) | |
983 | *p = TOUPPER (*p); | |
984 | ||
985 | lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i)); | |
986 | upper_ptr = add_map_value (upper_ptr, i, copy); | |
987 | } | |
988 | } | |
875d8740 | 989 | \f |
990 | /* Provide a version of a function to read a long long if the system does | |
991 | not provide one. */ | |
b5e563e4 | 992 | #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ) |
3ad4992f | 993 | HOST_WIDE_INT atoll (const char *); |
a92a1bb2 | 994 | |
875d8740 | 995 | HOST_WIDE_INT |
3ad4992f | 996 | atoll (const char *p) |
875d8740 | 997 | { |
998 | int neg = 0; | |
999 | HOST_WIDE_INT tmp_wide; | |
1000 | ||
337d789b | 1001 | while (ISSPACE (*p)) |
875d8740 | 1002 | p++; |
1003 | if (*p == '-') | |
1004 | neg = 1, p++; | |
1005 | else if (*p == '+') | |
1006 | p++; | |
1007 | ||
1008 | tmp_wide = 0; | |
337d789b | 1009 | while (ISDIGIT (*p)) |
875d8740 | 1010 | { |
1011 | HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0'); | |
1012 | if (new_wide < tmp_wide) | |
1013 | { | |
1014 | /* Return INT_MAX equiv on overflow. */ | |
7097b942 | 1015 | tmp_wide = HOST_WIDE_INT_M1U >> 1; |
875d8740 | 1016 | break; |
1017 | } | |
1018 | tmp_wide = new_wide; | |
1019 | p++; | |
1020 | } | |
1021 | ||
1022 | if (neg) | |
1023 | tmp_wide = -tmp_wide; | |
1024 | return tmp_wide; | |
1025 | } | |
1026 | #endif | |
af908c02 | 1027 | \f |
836c1c68 | 1028 | |
1029 | #ifdef GENERATOR_FILE | |
220dcf2f | 1030 | /* Process a define_conditions directive, starting with the optional |
1031 | space after the "define_conditions". The directive looks like this: | |
af908c02 | 1032 | |
1033 | (define_conditions [ | |
1034 | (number "string") | |
1035 | (number "string") | |
1036 | ... | |
1037 | ]) | |
1038 | ||
1039 | It's not intended to appear in machine descriptions. It is | |
1040 | generated by (the program generated by) genconditions.c, and | |
1041 | slipped in at the beginning of the sequence of MD files read by | |
1042 | most of the other generators. */ | |
e1e9159b | 1043 | void |
d3ab2953 | 1044 | md_reader::read_conditions () |
af908c02 | 1045 | { |
1046 | int c; | |
1047 | ||
3604118d | 1048 | require_char_ws ('['); |
af908c02 | 1049 | |
b3453c30 | 1050 | while ( (c = read_skip_spaces ()) != ']') |
af908c02 | 1051 | { |
220dcf2f | 1052 | struct md_name name; |
af908c02 | 1053 | char *expr; |
1054 | int value; | |
1055 | ||
1056 | if (c != '(') | |
b3453c30 | 1057 | fatal_expected_char ('(', c); |
af908c02 | 1058 | |
220dcf2f | 1059 | read_name (&name); |
1060 | validate_const_int (name.string); | |
1061 | value = atoi (name.string); | |
af908c02 | 1062 | |
3604118d | 1063 | require_char_ws ('"'); |
b3453c30 | 1064 | expr = read_quoted_string (); |
af908c02 | 1065 | |
3604118d | 1066 | require_char_ws (')'); |
af908c02 | 1067 | |
1068 | add_c_test (expr, value); | |
1069 | } | |
af908c02 | 1070 | } |
836c1c68 | 1071 | #endif /* #ifdef GENERATOR_FILE */ |
875d8740 | 1072 | |
bd7c2be3 | 1073 | static void |
b3453c30 | 1074 | validate_const_int (const char *string) |
bd7c2be3 | 1075 | { |
1076 | const char *cp; | |
1077 | int valid = 1; | |
1078 | ||
1079 | cp = string; | |
337d789b | 1080 | while (*cp && ISSPACE (*cp)) |
bd7c2be3 | 1081 | cp++; |
1082 | if (*cp == '-' || *cp == '+') | |
1083 | cp++; | |
1084 | if (*cp == 0) | |
1085 | valid = 0; | |
1086 | for (; *cp; cp++) | |
1087 | if (! ISDIGIT (*cp)) | |
d13600bf | 1088 | { |
1089 | valid = 0; | |
1090 | break; | |
1091 | } | |
bd7c2be3 | 1092 | if (!valid) |
b3453c30 | 1093 | fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string); |
bd7c2be3 | 1094 | } |
1095 | ||
e913b5cd | 1096 | static void |
1097 | validate_const_wide_int (const char *string) | |
1098 | { | |
1099 | const char *cp; | |
1100 | int valid = 1; | |
1101 | ||
1102 | cp = string; | |
1103 | while (*cp && ISSPACE (*cp)) | |
1104 | cp++; | |
1105 | /* Skip the leading 0x. */ | |
1106 | if (cp[0] == '0' || cp[1] == 'x') | |
1107 | cp += 2; | |
1108 | else | |
1109 | valid = 0; | |
1110 | if (*cp == 0) | |
1111 | valid = 0; | |
1112 | for (; *cp; cp++) | |
1113 | if (! ISXDIGIT (*cp)) | |
1114 | valid = 0; | |
1115 | if (!valid) | |
1116 | fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string); | |
1117 | } | |
1118 | ||
0fdb6b5d | 1119 | /* Record that X uses iterator ITERATOR. If the use is in an operand |
1120 | of X, INDEX is the index of that operand, otherwise it is ignored. */ | |
4a293229 | 1121 | |
f5d566ff | 1122 | static void |
0fdb6b5d | 1123 | record_iterator_use (struct mapping *iterator, rtx x, unsigned int index) |
f5d566ff | 1124 | { |
0fdb6b5d | 1125 | struct iterator_use iuse = {iterator, x, index}; |
f1f41a6c | 1126 | iterator_uses.safe_push (iuse); |
f5d566ff | 1127 | } |
1128 | ||
0fdb6b5d | 1129 | /* Record that X uses attribute VALUE, which must match a built-in |
1130 | value from group GROUP. If the use is in an operand of X, INDEX | |
1131 | is the index of that operand, otherwise it is ignored. */ | |
f5d566ff | 1132 | |
1133 | static void | |
0fdb6b5d | 1134 | record_attribute_use (struct iterator_group *group, rtx x, |
1135 | unsigned int index, const char *value) | |
f5d566ff | 1136 | { |
0fdb6b5d | 1137 | struct attribute_use ause = {group, value, x, index}; |
f1f41a6c | 1138 | attribute_uses.safe_push (ause); |
f5d566ff | 1139 | } |
1140 | ||
1141 | /* Interpret NAME as either a built-in value, iterator or attribute | |
0fdb6b5d | 1142 | for group GROUP. X and INDEX are the values to pass to GROUP's |
1143 | apply_iterator callback. */ | |
f5d566ff | 1144 | |
e1e9159b | 1145 | void |
d3ab2953 | 1146 | md_reader::record_potential_iterator_use (struct iterator_group *group, |
0fdb6b5d | 1147 | rtx x, unsigned int index, |
1148 | const char *name) | |
4a293229 | 1149 | { |
1150 | struct mapping *m; | |
f5d566ff | 1151 | size_t len; |
4a293229 | 1152 | |
f5d566ff | 1153 | len = strlen (name); |
1154 | if (name[0] == '<' && name[len - 1] == '>') | |
1155 | { | |
1156 | /* Copy the attribute string into permanent storage, without the | |
1157 | angle brackets around it. */ | |
e1e9159b | 1158 | obstack_grow0 (&m_string_obstack, name + 1, len - 2); |
0fdb6b5d | 1159 | record_attribute_use (group, x, index, |
1160 | XOBFINISH (&m_string_obstack, char *)); | |
f5d566ff | 1161 | } |
1162 | else | |
1163 | { | |
1164 | m = (struct mapping *) htab_find (group->iterators, &name); | |
1165 | if (m != 0) | |
0fdb6b5d | 1166 | record_iterator_use (m, x, index); |
f5d566ff | 1167 | else |
0fdb6b5d | 1168 | group->apply_iterator (x, index, group->find_builtin (name)); |
f5d566ff | 1169 | } |
4a293229 | 1170 | } |
1171 | ||
836c1c68 | 1172 | #ifdef GENERATOR_FILE |
1173 | ||
4a293229 | 1174 | /* Finish reading a declaration of the form: |
1175 | ||
1176 | (define... <name> [<value1> ... <valuen>]) | |
1177 | ||
b3453c30 | 1178 | from the MD file, where each <valuei> is either a bare symbol name or a |
4a293229 | 1179 | "(<name> <string>)" pair. The "(define..." part has already been read. |
1180 | ||
1181 | Represent the declaration as a "mapping" structure; add it to TABLE | |
1182 | (which belongs to GROUP) and return it. */ | |
1183 | ||
e1e9159b | 1184 | struct mapping * |
d3ab2953 | 1185 | md_reader::read_mapping (struct iterator_group *group, htab_t table) |
4a293229 | 1186 | { |
220dcf2f | 1187 | struct md_name name; |
4a293229 | 1188 | struct mapping *m; |
1189 | struct map_value **end_ptr; | |
1190 | const char *string; | |
1191 | int number, c; | |
1192 | ||
1193 | /* Read the mapping name and create a structure for it. */ | |
220dcf2f | 1194 | read_name (&name); |
1195 | m = add_mapping (group, table, name.string); | |
4a293229 | 1196 | |
3604118d | 1197 | require_char_ws ('['); |
4a293229 | 1198 | |
1199 | /* Read each value. */ | |
1200 | end_ptr = &m->values; | |
b3453c30 | 1201 | c = read_skip_spaces (); |
4a293229 | 1202 | do |
1203 | { | |
1204 | if (c != '(') | |
1205 | { | |
1206 | /* A bare symbol name that is implicitly paired to an | |
1207 | empty string. */ | |
b3453c30 | 1208 | unread_char (c); |
220dcf2f | 1209 | read_name (&name); |
4a293229 | 1210 | string = ""; |
1211 | } | |
1212 | else | |
1213 | { | |
1214 | /* A "(name string)" pair. */ | |
220dcf2f | 1215 | read_name (&name); |
b3453c30 | 1216 | string = read_string (false); |
3604118d | 1217 | require_char_ws (')'); |
4a293229 | 1218 | } |
220dcf2f | 1219 | number = group->find_builtin (name.string); |
4a293229 | 1220 | end_ptr = add_map_value (end_ptr, number, string); |
b3453c30 | 1221 | c = read_skip_spaces (); |
4a293229 | 1222 | } |
1223 | while (c != ']'); | |
1224 | ||
4a293229 | 1225 | return m; |
1226 | } | |
1227 | ||
cbedf5a3 | 1228 | /* For iterator with name ATTR_NAME generate define_attr with values |
1229 | 'yes' and 'no'. This attribute is used to mark templates to which | |
1230 | define_subst ATTR_NAME should be applied. This attribute is set and | |
1231 | defined implicitly and automatically. */ | |
1232 | static void | |
b0e2a5e8 | 1233 | add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue) |
cbedf5a3 | 1234 | { |
1235 | rtx const_str, return_rtx; | |
1236 | ||
1237 | return_rtx = rtx_alloc (DEFINE_ATTR); | |
1238 | PUT_CODE (return_rtx, DEFINE_ATTR); | |
1239 | ||
1240 | const_str = rtx_alloc (CONST_STRING); | |
1241 | PUT_CODE (const_str, CONST_STRING); | |
1242 | XSTR (const_str, 0) = xstrdup ("no"); | |
1243 | ||
1244 | XSTR (return_rtx, 0) = xstrdup (attr_name); | |
1245 | XSTR (return_rtx, 1) = xstrdup ("no,yes"); | |
1246 | XEXP (return_rtx, 2) = const_str; | |
1247 | ||
b0e2a5e8 | 1248 | queue->safe_push (return_rtx); |
cbedf5a3 | 1249 | } |
1250 | ||
1251 | /* This routine generates DEFINE_SUBST_ATTR expression with operands | |
1252 | ATTR_OPERANDS and places it to QUEUE. */ | |
1253 | static void | |
b0e2a5e8 | 1254 | add_define_subst_attr (const char **attr_operands, vec<rtx> *queue) |
cbedf5a3 | 1255 | { |
1256 | rtx return_rtx; | |
1257 | int i; | |
1258 | ||
1259 | return_rtx = rtx_alloc (DEFINE_SUBST_ATTR); | |
1260 | PUT_CODE (return_rtx, DEFINE_SUBST_ATTR); | |
1261 | ||
1262 | for (i = 0; i < 4; i++) | |
1263 | XSTR (return_rtx, i) = xstrdup (attr_operands[i]); | |
1264 | ||
b0e2a5e8 | 1265 | queue->safe_push (return_rtx); |
cbedf5a3 | 1266 | } |
1267 | ||
1268 | /* Read define_subst_attribute construction. It has next form: | |
1269 | (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>) | |
1270 | Attribute is substituted with value1 when no subst is applied and with | |
1271 | value2 in the opposite case. | |
1272 | Attributes are added to SUBST_ATTRS_TABLE. | |
1273 | In case the iterator is encountered for the first time, it's added to | |
1274 | SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */ | |
1275 | ||
1276 | static void | |
1277 | read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table, | |
b0e2a5e8 | 1278 | vec<rtx> *queue) |
cbedf5a3 | 1279 | { |
1280 | struct mapping *m; | |
1281 | struct map_value **end_ptr; | |
1282 | const char *attr_operands[4]; | |
cbedf5a3 | 1283 | int i; |
1284 | ||
1285 | for (i = 0; i < 4; i++) | |
e1e9159b | 1286 | attr_operands[i] = rtx_reader_ptr->read_string (false); |
cbedf5a3 | 1287 | |
b0e2a5e8 | 1288 | add_define_subst_attr (attr_operands, queue); |
cbedf5a3 | 1289 | |
1290 | bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]); | |
1291 | ||
1292 | m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]); | |
1293 | if (!m) | |
1294 | { | |
1295 | m = add_mapping (&substs, subst_iters_table, attr_operands[1]); | |
1296 | end_ptr = &m->values; | |
1297 | end_ptr = add_map_value (end_ptr, 1, ""); | |
1298 | end_ptr = add_map_value (end_ptr, 2, ""); | |
1299 | ||
b0e2a5e8 | 1300 | add_define_attr_for_define_subst (attr_operands[1], queue); |
cbedf5a3 | 1301 | } |
1302 | ||
1303 | m = add_mapping (&substs, subst_attrs_table, attr_operands[0]); | |
1304 | end_ptr = &m->values; | |
1305 | end_ptr = add_map_value (end_ptr, 1, attr_operands[2]); | |
1306 | end_ptr = add_map_value (end_ptr, 2, attr_operands[3]); | |
1307 | } | |
1308 | ||
fd781bb2 | 1309 | /* Check newly-created code iterator ITERATOR to see whether every code has the |
f5d566ff | 1310 | same format. */ |
4a293229 | 1311 | |
1312 | static void | |
b3453c30 | 1313 | check_code_iterator (struct mapping *iterator) |
4a293229 | 1314 | { |
1315 | struct map_value *v; | |
1316 | enum rtx_code bellwether; | |
1317 | ||
fd781bb2 | 1318 | bellwether = (enum rtx_code) iterator->values->number; |
1319 | for (v = iterator->values->next; v != 0; v = v->next) | |
4a293229 | 1320 | if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0) |
b3453c30 | 1321 | fatal_with_file_and_line ("code iterator `%s' combines " |
c08a1447 | 1322 | "`%s' and `%s', which have different " |
1323 | "rtx formats", iterator->name, | |
1324 | GET_RTX_NAME (bellwether), | |
1325 | GET_RTX_NAME (v->number)); | |
1326 | } | |
1327 | ||
1328 | /* Check that all values of attribute ATTR are rtx codes that have a | |
1329 | consistent format. Return a representative code. */ | |
1330 | ||
1331 | static rtx_code | |
1332 | check_code_attribute (mapping *attr) | |
1333 | { | |
1334 | rtx_code bellwether = UNKNOWN; | |
1335 | for (map_value *v = attr->values; v != 0; v = v->next) | |
1336 | { | |
1337 | rtx_code code = maybe_find_code (v->string); | |
1338 | if (code == UNKNOWN) | |
1339 | fatal_with_file_and_line ("code attribute `%s' contains " | |
1340 | "unrecognized rtx code `%s'", | |
1341 | attr->name, v->string); | |
1342 | if (bellwether == UNKNOWN) | |
1343 | bellwether = code; | |
1344 | else if (strcmp (GET_RTX_FORMAT (bellwether), | |
1345 | GET_RTX_FORMAT (code)) != 0) | |
1346 | fatal_with_file_and_line ("code attribute `%s' combines " | |
1347 | "`%s' and `%s', which have different " | |
1348 | "rtx formats", attr->name, | |
1349 | GET_RTX_NAME (bellwether), | |
1350 | GET_RTX_NAME (code)); | |
1351 | } | |
1352 | return bellwether; | |
4a293229 | 1353 | } |
1354 | ||
77ba95d0 | 1355 | /* Read an rtx-related declaration from the MD file, given that it |
1356 | starts with directive name RTX_NAME. Return true if it expands to | |
1357 | one or more rtxes (as defined by rtx.def). When returning true, | |
1358 | store the list of rtxes as an EXPR_LIST in *X. */ | |
875d8740 | 1359 | |
fea520eb | 1360 | bool |
e1e9159b | 1361 | rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen) |
875d8740 | 1362 | { |
77ba95d0 | 1363 | /* Handle various rtx-related declarations that aren't themselves |
1364 | encoded as rtxes. */ | |
1365 | if (strcmp (rtx_name, "define_conditions") == 0) | |
4a293229 | 1366 | { |
77ba95d0 | 1367 | read_conditions (); |
1368 | return false; | |
4a293229 | 1369 | } |
77ba95d0 | 1370 | if (strcmp (rtx_name, "define_mode_attr") == 0) |
1371 | { | |
1372 | read_mapping (&modes, modes.attrs); | |
1373 | return false; | |
1374 | } | |
1375 | if (strcmp (rtx_name, "define_mode_iterator") == 0) | |
1376 | { | |
1377 | read_mapping (&modes, modes.iterators); | |
1378 | return false; | |
1379 | } | |
1380 | if (strcmp (rtx_name, "define_code_attr") == 0) | |
1381 | { | |
1382 | read_mapping (&codes, codes.attrs); | |
1383 | return false; | |
1384 | } | |
1385 | if (strcmp (rtx_name, "define_code_iterator") == 0) | |
1386 | { | |
1387 | check_code_iterator (read_mapping (&codes, codes.iterators)); | |
1388 | return false; | |
1389 | } | |
65729bd0 | 1390 | if (strcmp (rtx_name, "define_int_attr") == 0) |
1391 | { | |
1392 | read_mapping (&ints, ints.attrs); | |
1393 | return false; | |
1394 | } | |
1395 | if (strcmp (rtx_name, "define_int_iterator") == 0) | |
1396 | { | |
1397 | read_mapping (&ints, ints.iterators); | |
1398 | return false; | |
1399 | } | |
cbedf5a3 | 1400 | if (strcmp (rtx_name, "define_subst_attr") == 0) |
1401 | { | |
b0e2a5e8 | 1402 | read_subst_mapping (substs.iterators, substs.attrs, rtxen); |
cbedf5a3 | 1403 | |
1404 | /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return | |
1405 | TRUE to process it. */ | |
1406 | return true; | |
1407 | } | |
77ba95d0 | 1408 | |
d3ab2953 | 1409 | apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen); |
f1f41a6c | 1410 | iterator_uses.truncate (0); |
1411 | attribute_uses.truncate (0); | |
4a293229 | 1412 | |
fea520eb | 1413 | return true; |
4a293229 | 1414 | } |
1415 | ||
836c1c68 | 1416 | #endif /* #ifdef GENERATOR_FILE */ |
1417 | ||
1418 | /* Do one-time initialization. */ | |
1419 | ||
1420 | static void | |
1421 | one_time_initialization (void) | |
1422 | { | |
1423 | static bool initialized = false; | |
1424 | ||
1425 | if (!initialized) | |
1426 | { | |
1427 | initialize_iterators (); | |
1428 | initialized = true; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | /* Consume characters until encountering a character in TERMINATOR_CHARS, | |
1433 | consuming the terminator character if CONSUME_TERMINATOR is true. | |
1434 | Return all characters before the terminator as an allocated buffer. */ | |
1435 | ||
1436 | char * | |
1437 | rtx_reader::read_until (const char *terminator_chars, bool consume_terminator) | |
1438 | { | |
1439 | int ch = read_skip_spaces (); | |
1440 | unread_char (ch); | |
1441 | auto_vec<char> buf; | |
1442 | while (1) | |
1443 | { | |
1444 | ch = read_char (); | |
1445 | if (strchr (terminator_chars, ch)) | |
1446 | { | |
1447 | if (!consume_terminator) | |
1448 | unread_char (ch); | |
1449 | break; | |
1450 | } | |
1451 | buf.safe_push (ch); | |
1452 | } | |
1453 | buf.safe_push ('\0'); | |
1454 | return xstrdup (buf.address ()); | |
1455 | } | |
1456 | ||
1457 | /* Subroutine of read_rtx_code, for parsing zero or more flags. */ | |
1458 | ||
1459 | static void | |
1460 | read_flags (rtx return_rtx) | |
1461 | { | |
1462 | while (1) | |
1463 | { | |
1464 | int ch = read_char (); | |
1465 | if (ch != '/') | |
1466 | { | |
1467 | unread_char (ch); | |
1468 | break; | |
1469 | } | |
1470 | ||
1471 | int flag_char = read_char (); | |
1472 | switch (flag_char) | |
1473 | { | |
1474 | case 's': | |
1475 | RTX_FLAG (return_rtx, in_struct) = 1; | |
1476 | break; | |
1477 | case 'v': | |
1478 | RTX_FLAG (return_rtx, volatil) = 1; | |
1479 | break; | |
1480 | case 'u': | |
1481 | RTX_FLAG (return_rtx, unchanging) = 1; | |
1482 | break; | |
1483 | case 'f': | |
1484 | RTX_FLAG (return_rtx, frame_related) = 1; | |
1485 | break; | |
1486 | case 'j': | |
1487 | RTX_FLAG (return_rtx, jump) = 1; | |
1488 | break; | |
1489 | case 'c': | |
1490 | RTX_FLAG (return_rtx, call) = 1; | |
1491 | break; | |
1492 | case 'i': | |
1493 | RTX_FLAG (return_rtx, return_val) = 1; | |
1494 | break; | |
1495 | default: | |
1496 | fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char); | |
1497 | } | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING, | |
1502 | or fail if STRING isn't recognized. */ | |
1503 | ||
1504 | static int | |
1505 | parse_reg_note_name (const char *string) | |
1506 | { | |
1507 | for (int i = 0; i < REG_NOTE_MAX; i++) | |
c9281ef8 | 1508 | if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0) |
836c1c68 | 1509 | return i; |
1510 | fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string); | |
1511 | } | |
1512 | ||
c08a1447 | 1513 | /* Allocate an rtx for code NAME. If NAME is a code iterator or code |
1514 | attribute, record its use for later and use one of its possible | |
1515 | values as an interim rtx code. */ | |
1516 | ||
1517 | rtx | |
1518 | rtx_reader::rtx_alloc_for_name (const char *name) | |
1519 | { | |
1520 | #ifdef GENERATOR_FILE | |
1521 | size_t len = strlen (name); | |
1522 | if (name[0] == '<' && name[len - 1] == '>') | |
1523 | { | |
1524 | /* Copy the attribute string into permanent storage, without the | |
1525 | angle brackets around it. */ | |
1526 | obstack *strings = get_string_obstack (); | |
1527 | obstack_grow0 (strings, name + 1, len - 2); | |
1528 | char *deferred_name = XOBFINISH (strings, char *); | |
1529 | ||
1530 | /* Find the name of the attribute. */ | |
1531 | const char *attr = strchr (deferred_name, ':'); | |
1532 | if (!attr) | |
1533 | attr = deferred_name; | |
1534 | ||
1535 | /* Find the attribute itself. */ | |
1536 | mapping *m = (mapping *) htab_find (codes.attrs, &attr); | |
1537 | if (!m) | |
1538 | fatal_with_file_and_line ("unknown code attribute `%s'", attr); | |
1539 | ||
1540 | /* Pick the first possible code for now, and record the attribute | |
1541 | use for later. */ | |
1542 | rtx x = rtx_alloc (check_code_attribute (m)); | |
1543 | record_attribute_use (&codes, x, 0, deferred_name); | |
1544 | return x; | |
1545 | } | |
1546 | ||
1547 | mapping *iterator = (mapping *) htab_find (codes.iterators, &name); | |
1548 | if (iterator != 0) | |
1549 | { | |
1550 | /* Pick the first possible code for now, and record the iterator | |
1551 | use for later. */ | |
1552 | rtx x = rtx_alloc (rtx_code (iterator->values->number)); | |
1553 | record_iterator_use (iterator, x, 0); | |
1554 | return x; | |
1555 | } | |
1556 | #endif | |
1557 | ||
1558 | return rtx_alloc (rtx_code (codes.find_builtin (name))); | |
1559 | } | |
1560 | ||
77ba95d0 | 1561 | /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of |
1562 | either an rtx code or a code iterator. Parse the rest of the rtx and | |
f5d566ff | 1563 | return it. */ |
4a293229 | 1564 | |
e1e9159b | 1565 | rtx |
1566 | rtx_reader::read_rtx_code (const char *code_name) | |
4a293229 | 1567 | { |
f5d566ff | 1568 | RTX_CODE code; |
19cb6b50 | 1569 | const char *format_ptr; |
220dcf2f | 1570 | struct md_name name; |
875d8740 | 1571 | rtx return_rtx; |
19cb6b50 | 1572 | int c; |
836c1c68 | 1573 | long reuse_id = -1; |
875d8740 | 1574 | |
875d8740 | 1575 | /* Linked list structure for making RTXs: */ |
1576 | struct rtx_list | |
1577 | { | |
1578 | struct rtx_list *next; | |
1579 | rtx value; /* Value of this node. */ | |
1580 | }; | |
1581 | ||
836c1c68 | 1582 | /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */ |
1583 | if (ISDIGIT (code_name[0])) | |
1584 | { | |
1585 | reuse_id = atoi (code_name); | |
1586 | while (char ch = *code_name++) | |
1587 | if (ch == '|') | |
1588 | break; | |
1589 | } | |
1590 | ||
1591 | /* Handle "reuse_rtx". */ | |
1592 | if (strcmp (code_name, "reuse_rtx") == 0) | |
1593 | { | |
1594 | read_name (&name); | |
50079afd | 1595 | unsigned idx = atoi (name.string); |
836c1c68 | 1596 | /* Look it up by ID. */ |
1597 | gcc_assert (idx < m_reuse_rtx_by_id.length ()); | |
1598 | return_rtx = m_reuse_rtx_by_id[idx]; | |
1599 | return return_rtx; | |
1600 | } | |
1601 | ||
875d8740 | 1602 | /* If we end up with an insn expression then we free this space below. */ |
c08a1447 | 1603 | return_rtx = rtx_alloc_for_name (code_name); |
1604 | code = GET_CODE (return_rtx); | |
f5d566ff | 1605 | format_ptr = GET_RTX_FORMAT (code); |
2975871c | 1606 | memset (return_rtx, 0, RTX_CODE_SIZE (code)); |
f5d566ff | 1607 | PUT_CODE (return_rtx, code); |
1608 | ||
836c1c68 | 1609 | if (reuse_id != -1) |
1610 | { | |
1611 | /* Store away for later reuse. */ | |
1612 | m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1); | |
1613 | m_reuse_rtx_by_id[reuse_id] = return_rtx; | |
1614 | } | |
1615 | ||
836c1c68 | 1616 | /* Check for flags. */ |
1617 | read_flags (return_rtx); | |
1618 | ||
1619 | /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */ | |
1620 | if ((GET_CODE (return_rtx) == EXPR_LIST | |
1621 | || GET_CODE (return_rtx) == INSN_LIST | |
1622 | || GET_CODE (return_rtx) == INT_LIST) | |
1623 | && !m_in_call_function_usage) | |
1624 | { | |
1625 | char ch = read_char (); | |
1626 | if (ch == ':') | |
1627 | { | |
1628 | read_name (&name); | |
1629 | PUT_MODE_RAW (return_rtx, | |
1630 | (machine_mode)parse_reg_note_name (name.string)); | |
1631 | } | |
1632 | else | |
1633 | unread_char (ch); | |
1634 | } | |
1635 | ||
875d8740 | 1636 | /* If what follows is `: mode ', read it and |
1637 | store the mode in the rtx. */ | |
1638 | ||
9b12e2dc | 1639 | c = read_skip_spaces (); |
1640 | if (c == ':') | |
875d8740 | 1641 | { |
220dcf2f | 1642 | read_name (&name); |
0fdb6b5d | 1643 | record_potential_iterator_use (&modes, return_rtx, 0, name.string); |
875d8740 | 1644 | } |
1645 | else | |
9b12e2dc | 1646 | unread_char (c); |
875d8740 | 1647 | |
836c1c68 | 1648 | if (INSN_CHAIN_CODE_P (code)) |
1649 | { | |
1650 | read_name (&name); | |
1651 | INSN_UID (return_rtx) = atoi (name.string); | |
1652 | } | |
1653 | ||
1654 | /* Use the format_ptr to parse the various operands of this rtx. */ | |
9b12e2dc | 1655 | for (int idx = 0; format_ptr[idx] != 0; idx++) |
836c1c68 | 1656 | return_rtx = read_rtx_operand (return_rtx, idx); |
1657 | ||
1658 | /* Handle any additional information that after the regular fields | |
1659 | (e.g. when parsing function dumps). */ | |
1660 | handle_any_trailing_information (return_rtx); | |
875d8740 | 1661 | |
e913b5cd | 1662 | if (CONST_WIDE_INT_P (return_rtx)) |
1663 | { | |
1664 | read_name (&name); | |
1665 | validate_const_wide_int (name.string); | |
1666 | { | |
e913b5cd | 1667 | const char *s = name.string; |
1668 | int len; | |
1669 | int index = 0; | |
1670 | int gs = HOST_BITS_PER_WIDE_INT/4; | |
1671 | int pos; | |
1672 | char * buf = XALLOCAVEC (char, gs + 1); | |
1673 | unsigned HOST_WIDE_INT wi; | |
1674 | int wlen; | |
1675 | ||
1676 | /* Skip the leading spaces. */ | |
1677 | while (*s && ISSPACE (*s)) | |
1678 | s++; | |
1679 | ||
1680 | /* Skip the leading 0x. */ | |
1681 | gcc_assert (s[0] == '0'); | |
1682 | gcc_assert (s[1] == 'x'); | |
1683 | s += 2; | |
1684 | ||
1685 | len = strlen (s); | |
1686 | pos = len - gs; | |
1687 | wlen = (len + gs - 1) / gs; /* Number of words needed */ | |
1688 | ||
1689 | return_rtx = const_wide_int_alloc (wlen); | |
1690 | ||
e913b5cd | 1691 | while (pos > 0) |
1692 | { | |
1693 | #if HOST_BITS_PER_WIDE_INT == 64 | |
1694 | sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi); | |
1695 | #else | |
1696 | sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi); | |
1697 | #endif | |
05c25ee6 | 1698 | CWI_ELT (return_rtx, index++) = wi; |
e913b5cd | 1699 | pos -= gs; |
1700 | } | |
1701 | strncpy (buf, s, gs - pos); | |
1702 | buf [gs - pos] = 0; | |
1703 | sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi); | |
05c25ee6 | 1704 | CWI_ELT (return_rtx, index++) = wi; |
e913b5cd | 1705 | /* TODO: After reading, do we want to canonicalize with: |
1706 | value = lookup_const_wide_int (value); ? */ | |
1707 | } | |
1708 | } | |
1709 | ||
77ba95d0 | 1710 | c = read_skip_spaces (); |
1711 | /* Syntactic sugar for AND and IOR, allowing Lisp-like | |
1712 | arbitrary number of arguments for them. */ | |
1713 | if (c == '(' | |
1714 | && (GET_CODE (return_rtx) == AND | |
1715 | || GET_CODE (return_rtx) == IOR)) | |
f5d566ff | 1716 | return read_rtx_variadic (return_rtx); |
77ba95d0 | 1717 | |
1718 | unread_char (c); | |
1719 | return return_rtx; | |
1720 | } | |
1721 | ||
9b12e2dc | 1722 | /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX, |
1723 | based on the corresponding format character within GET_RTX_FORMAT | |
836c1c68 | 1724 | for the GET_CODE (RETURN_RTX), and return RETURN_RTX. |
1725 | This is a virtual function, so that function_reader can override | |
1726 | some parsing, and potentially return a different rtx. */ | |
9b12e2dc | 1727 | |
836c1c68 | 1728 | rtx |
e1e9159b | 1729 | rtx_reader::read_rtx_operand (rtx return_rtx, int idx) |
9b12e2dc | 1730 | { |
1731 | RTX_CODE code = GET_CODE (return_rtx); | |
1732 | const char *format_ptr = GET_RTX_FORMAT (code); | |
1733 | int c; | |
1734 | struct md_name name; | |
1735 | ||
1736 | switch (format_ptr[idx]) | |
1737 | { | |
1738 | /* 0 means a field for internal use only. | |
1739 | Don't expect it to be present in the input. */ | |
1740 | case '0': | |
1741 | if (code == REG) | |
1742 | ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx); | |
1743 | break; | |
1744 | ||
1745 | case 'e': | |
836c1c68 | 1746 | XEXP (return_rtx, idx) = read_nested_rtx (); |
1747 | break; | |
1748 | ||
9b12e2dc | 1749 | case 'u': |
1750 | XEXP (return_rtx, idx) = read_nested_rtx (); | |
1751 | break; | |
1752 | ||
1753 | case 'V': | |
1754 | /* 'V' is an optional vector: if a closeparen follows, | |
1755 | just store NULL for this element. */ | |
1756 | c = read_skip_spaces (); | |
1757 | unread_char (c); | |
1758 | if (c == ')') | |
1759 | { | |
1760 | XVEC (return_rtx, idx) = 0; | |
1761 | break; | |
1762 | } | |
1763 | /* Now process the vector. */ | |
1764 | /* FALLTHRU */ | |
1765 | ||
1766 | case 'E': | |
1767 | { | |
1768 | /* Obstack to store scratch vector in. */ | |
1769 | struct obstack vector_stack; | |
1770 | int list_counter = 0; | |
1771 | rtvec return_vec = NULL_RTVEC; | |
c488a0b5 | 1772 | rtx saved_rtx = NULL_RTX; |
9b12e2dc | 1773 | |
1774 | require_char_ws ('['); | |
1775 | ||
1776 | /* Add expressions to a list, while keeping a count. */ | |
1777 | obstack_init (&vector_stack); | |
1778 | while ((c = read_skip_spaces ()) && c != ']') | |
1779 | { | |
1780 | if (c == EOF) | |
1781 | fatal_expected_char (']', c); | |
1782 | unread_char (c); | |
c488a0b5 | 1783 | |
1784 | rtx value; | |
1785 | int repeat_count = 1; | |
1786 | if (c == 'r') | |
1787 | { | |
1788 | /* Process "repeated xN" directive. */ | |
1789 | read_name (&name); | |
1790 | if (strcmp (name.string, "repeated")) | |
1791 | fatal_with_file_and_line ("invalid directive \"%s\"\n", | |
1792 | name.string); | |
1793 | read_name (&name); | |
1794 | if (!sscanf (name.string, "x%d", &repeat_count)) | |
1795 | fatal_with_file_and_line ("invalid repeat count \"%s\"\n", | |
1796 | name.string); | |
1797 | ||
1798 | /* We already saw one of the instances. */ | |
1799 | repeat_count--; | |
1800 | value = saved_rtx; | |
1801 | } | |
1802 | else | |
1803 | value = read_nested_rtx (); | |
1804 | ||
1805 | for (; repeat_count > 0; repeat_count--) | |
1806 | { | |
1807 | list_counter++; | |
1808 | obstack_ptr_grow (&vector_stack, value); | |
1809 | } | |
1810 | saved_rtx = value; | |
9b12e2dc | 1811 | } |
1812 | if (list_counter > 0) | |
1813 | { | |
1814 | return_vec = rtvec_alloc (list_counter); | |
1815 | memcpy (&return_vec->elem[0], obstack_finish (&vector_stack), | |
1816 | list_counter * sizeof (rtx)); | |
1817 | } | |
1818 | else if (format_ptr[idx] == 'E') | |
1819 | fatal_with_file_and_line ("vector must have at least one element"); | |
1820 | XVEC (return_rtx, idx) = return_vec; | |
1821 | obstack_free (&vector_stack, NULL); | |
1822 | /* close bracket gotten */ | |
1823 | } | |
1824 | break; | |
1825 | ||
1826 | case 'S': | |
1827 | case 'T': | |
1828 | case 's': | |
1829 | { | |
1830 | char *stringbuf; | |
1831 | int star_if_braced; | |
1832 | ||
1833 | c = read_skip_spaces (); | |
1834 | unread_char (c); | |
1835 | if (c == ')') | |
1836 | { | |
1837 | /* 'S' fields are optional and should be NULL if no string | |
1838 | was given. Also allow normal 's' and 'T' strings to be | |
1839 | omitted, treating them in the same way as empty strings. */ | |
1840 | XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : ""); | |
1841 | break; | |
1842 | } | |
1843 | ||
e8c17f8e | 1844 | /* The output template slot of a DEFINE_INSN, DEFINE_INSN_AND_SPLIT, |
1845 | DEFINE_INSN_AND_REWRITE or DEFINE_PEEPHOLE automatically | |
9b12e2dc | 1846 | gets a star inserted as its first character, if it is |
1847 | written with a brace block instead of a string constant. */ | |
1848 | star_if_braced = (format_ptr[idx] == 'T'); | |
1849 | ||
1850 | stringbuf = read_string (star_if_braced); | |
836c1c68 | 1851 | if (!stringbuf) |
1852 | break; | |
9b12e2dc | 1853 | |
836c1c68 | 1854 | #ifdef GENERATOR_FILE |
9b12e2dc | 1855 | /* For insn patterns, we want to provide a default name |
1856 | based on the file and line, like "*foo.md:12", if the | |
1857 | given name is blank. These are only for define_insn and | |
1858 | define_insn_and_split, to aid debugging. */ | |
1859 | if (*stringbuf == '\0' | |
1860 | && idx == 0 | |
1861 | && (GET_CODE (return_rtx) == DEFINE_INSN | |
e8c17f8e | 1862 | || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT |
1863 | || GET_CODE (return_rtx) == DEFINE_INSN_AND_REWRITE)) | |
9b12e2dc | 1864 | { |
836c1c68 | 1865 | struct obstack *string_obstack = get_string_obstack (); |
9b12e2dc | 1866 | char line_name[20]; |
e1e9159b | 1867 | const char *read_md_filename = get_filename (); |
9b12e2dc | 1868 | const char *fn = (read_md_filename ? read_md_filename : "rtx"); |
1869 | const char *slash; | |
1870 | for (slash = fn; *slash; slash ++) | |
1871 | if (*slash == '/' || *slash == '\\' || *slash == ':') | |
1872 | fn = slash + 1; | |
d3ab2953 | 1873 | obstack_1grow (string_obstack, '*'); |
1874 | obstack_grow (string_obstack, fn, strlen (fn)); | |
e1e9159b | 1875 | sprintf (line_name, ":%d", get_lineno ()); |
d3ab2953 | 1876 | obstack_grow (string_obstack, line_name, strlen (line_name)+1); |
1877 | stringbuf = XOBFINISH (string_obstack, char *); | |
9b12e2dc | 1878 | } |
1879 | ||
1880 | /* Find attr-names in the string. */ | |
1881 | char *str; | |
1882 | char *start, *end, *ptr; | |
1883 | char tmpstr[256]; | |
1884 | ptr = &tmpstr[0]; | |
1885 | end = stringbuf; | |
1886 | while ((start = strchr (end, '<')) && (end = strchr (start, '>'))) | |
1887 | { | |
1888 | if ((end - start - 1 > 0) | |
1889 | && (end - start - 1 < (int)sizeof (tmpstr))) | |
1890 | { | |
1891 | strncpy (tmpstr, start+1, end-start-1); | |
1892 | tmpstr[end-start-1] = 0; | |
1893 | end++; | |
1894 | } | |
1895 | else | |
1896 | break; | |
1897 | struct mapping *m | |
1898 | = (struct mapping *) htab_find (substs.attrs, &ptr); | |
1899 | if (m != 0) | |
1900 | { | |
1901 | /* Here we should find linked subst-iter. */ | |
1902 | str = find_subst_iter_by_attr (ptr); | |
1903 | if (str) | |
1904 | m = (struct mapping *) htab_find (substs.iterators, &str); | |
1905 | else | |
1906 | m = 0; | |
1907 | } | |
1908 | if (m != 0) | |
0fdb6b5d | 1909 | record_iterator_use (m, return_rtx, 0); |
9b12e2dc | 1910 | } |
836c1c68 | 1911 | #endif /* #ifdef GENERATOR_FILE */ |
1912 | ||
1913 | const char *string_ptr = finalize_string (stringbuf); | |
9b12e2dc | 1914 | |
1915 | if (star_if_braced) | |
836c1c68 | 1916 | XTMPL (return_rtx, idx) = string_ptr; |
9b12e2dc | 1917 | else |
836c1c68 | 1918 | XSTR (return_rtx, idx) = string_ptr; |
9b12e2dc | 1919 | } |
1920 | break; | |
1921 | ||
1922 | case 'w': | |
1923 | { | |
1924 | HOST_WIDE_INT tmp_wide; | |
1925 | read_name (&name); | |
1926 | validate_const_int (name.string); | |
1927 | #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT | |
1928 | tmp_wide = atoi (name.string); | |
1929 | #else | |
1930 | #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG | |
1931 | tmp_wide = atol (name.string); | |
1932 | #else | |
1933 | /* Prefer atoll over atoq, since the former is in the ISO C99 standard. | |
1934 | But prefer not to use our hand-rolled function above either. */ | |
1935 | #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ) | |
1936 | tmp_wide = atoll (name.string); | |
1937 | #else | |
1938 | tmp_wide = atoq (name.string); | |
1939 | #endif | |
1940 | #endif | |
1941 | #endif | |
1942 | XWINT (return_rtx, idx) = tmp_wide; | |
1943 | } | |
1944 | break; | |
1945 | ||
1946 | case 'i': | |
1947 | case 'n': | |
9edf7ea8 | 1948 | case 'p': |
9b12e2dc | 1949 | /* Can be an iterator or an integer constant. */ |
1950 | read_name (&name); | |
0fdb6b5d | 1951 | record_potential_iterator_use (&ints, return_rtx, idx, name.string); |
9b12e2dc | 1952 | break; |
1953 | ||
1954 | case 'r': | |
1955 | read_name (&name); | |
1956 | validate_const_int (name.string); | |
1957 | set_regno_raw (return_rtx, atoi (name.string), 1); | |
1958 | REG_ATTRS (return_rtx) = NULL; | |
1959 | break; | |
1960 | ||
1961 | default: | |
1962 | gcc_unreachable (); | |
1963 | } | |
836c1c68 | 1964 | |
1965 | return return_rtx; | |
9b12e2dc | 1966 | } |
1967 | ||
f5d566ff | 1968 | /* Read a nested rtx construct from the MD file and return it. */ |
77ba95d0 | 1969 | |
e1e9159b | 1970 | rtx |
1971 | rtx_reader::read_nested_rtx () | |
77ba95d0 | 1972 | { |
1973 | struct md_name name; | |
77ba95d0 | 1974 | rtx return_rtx; |
1975 | ||
836c1c68 | 1976 | /* In compact dumps, trailing "(nil)" values can be omitted. |
1977 | Handle such dumps. */ | |
1978 | if (peek_char () == ')') | |
1979 | return NULL_RTX; | |
1980 | ||
3604118d | 1981 | require_char_ws ('('); |
77ba95d0 | 1982 | |
1983 | read_name (&name); | |
1984 | if (strcmp (name.string, "nil") == 0) | |
1985 | return_rtx = NULL; | |
1986 | else | |
f5d566ff | 1987 | return_rtx = read_rtx_code (name.string); |
77ba95d0 | 1988 | |
3604118d | 1989 | require_char_ws (')'); |
875d8740 | 1990 | |
836c1c68 | 1991 | return_rtx = postprocess (return_rtx); |
1992 | ||
875d8740 | 1993 | return return_rtx; |
1994 | } | |
6c9ff279 | 1995 | |
1996 | /* Mutually recursive subroutine of read_rtx which reads | |
1997 | (thing x1 x2 x3 ...) and produces RTL as if | |
1998 | (thing x1 (thing x2 (thing x3 ...))) had been written. | |
1999 | When called, FORM is (thing x1 x2), and the file position | |
2000 | is just past the leading parenthesis of x3. Only works | |
2001 | for THINGs which are dyadic expressions, e.g. AND, IOR. */ | |
e1e9159b | 2002 | rtx |
2003 | rtx_reader::read_rtx_variadic (rtx form) | |
6c9ff279 | 2004 | { |
2005 | char c = '('; | |
2006 | rtx p = form, q; | |
2007 | ||
2008 | do | |
2009 | { | |
b3453c30 | 2010 | unread_char (c); |
6c9ff279 | 2011 | |
2012 | q = rtx_alloc (GET_CODE (p)); | |
2013 | PUT_MODE (q, GET_MODE (p)); | |
2014 | ||
2015 | XEXP (q, 0) = XEXP (p, 1); | |
f5d566ff | 2016 | XEXP (q, 1) = read_nested_rtx (); |
48e1416a | 2017 | |
6c9ff279 | 2018 | XEXP (p, 1) = q; |
2019 | p = q; | |
b3453c30 | 2020 | c = read_skip_spaces (); |
6c9ff279 | 2021 | } |
2022 | while (c == '('); | |
77ba95d0 | 2023 | unread_char (c); |
6c9ff279 | 2024 | return form; |
2025 | } | |
d3ab2953 | 2026 | |
2027 | /* Constructor for class rtx_reader. */ | |
2028 | ||
836c1c68 | 2029 | rtx_reader::rtx_reader (bool compact) |
2030 | : md_reader (compact), | |
2031 | m_in_call_function_usage (false) | |
d3ab2953 | 2032 | { |
2033 | /* Set the global singleton pointer. */ | |
2034 | rtx_reader_ptr = this; | |
836c1c68 | 2035 | |
2036 | one_time_initialization (); | |
d3ab2953 | 2037 | } |
2038 | ||
2039 | /* Destructor for class rtx_reader. */ | |
2040 | ||
2041 | rtx_reader::~rtx_reader () | |
2042 | { | |
2043 | /* Clear the global singleton pointer. */ | |
2044 | rtx_reader_ptr = NULL; | |
2045 | } |