]>
Commit | Line | Data |
---|---|---|
3916d6d8 | 1 | /* Support routines for the various generation passes. |
c88c0d42 CP |
2 | Copyright (C) 2000 Free Software Foundation, Inc. |
3 | ||
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "hconfig.h" | |
22 | #include "system.h" | |
23 | #include "rtl.h" | |
3916d6d8 | 24 | #include "obstack.h" |
c88c0d42 CP |
25 | #include "errors.h" |
26 | #include "gensupport.h" | |
27 | ||
3916d6d8 RH |
28 | |
29 | static struct obstack obstack; | |
30 | struct obstack *rtl_obstack = &obstack; | |
31 | ||
32 | #define obstack_chunk_alloc xmalloc | |
33 | #define obstack_chunk_free free | |
34 | ||
c88c0d42 | 35 | static int sequence_num; |
3262c1f5 RH |
36 | static int errors; |
37 | ||
38 | static int predicable_default; | |
39 | static const char *predicable_true; | |
40 | static const char *predicable_false; | |
41 | ||
42 | /* We initially queue all patterns, process the define_insn and | |
43 | define_cond_exec patterns, then return them one at a time. */ | |
c88c0d42 | 44 | |
3262c1f5 RH |
45 | struct queue_elem |
46 | { | |
47 | rtx data; | |
48 | int lineno; | |
49 | struct queue_elem *next; | |
c88c0d42 CP |
50 | }; |
51 | ||
3262c1f5 RH |
52 | static struct queue_elem *define_attr_queue; |
53 | static struct queue_elem **define_attr_tail = &define_attr_queue; | |
54 | static struct queue_elem *define_insn_queue; | |
55 | static struct queue_elem **define_insn_tail = &define_insn_queue; | |
56 | static struct queue_elem *define_cond_exec_queue; | |
57 | static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue; | |
58 | static struct queue_elem *other_queue; | |
59 | static struct queue_elem **other_tail = &other_queue; | |
c88c0d42 | 60 | |
3262c1f5 | 61 | static void queue_pattern PARAMS ((rtx, struct queue_elem ***, int)); |
36244024 | 62 | static void remove_constraints PARAMS ((rtx)); |
3262c1f5 RH |
63 | static void process_rtx PARAMS ((rtx, int)); |
64 | ||
65 | static int is_predicable PARAMS ((struct queue_elem *)); | |
66 | static void identify_predicable_attribute PARAMS ((void)); | |
67 | static int n_alternatives PARAMS ((const char *)); | |
68 | static void collect_insn_data PARAMS ((rtx, int *, int *)); | |
69 | static rtx alter_predicate_for_insn PARAMS ((rtx, int, int, int)); | |
70 | static const char *alter_test_for_insn PARAMS ((struct queue_elem *, | |
71 | struct queue_elem *)); | |
72 | static char *shift_output_template PARAMS ((char *, const char *, int)); | |
73 | static const char *alter_output_for_insn PARAMS ((struct queue_elem *, | |
74 | struct queue_elem *, | |
75 | int, int)); | |
76 | static void process_one_cond_exec PARAMS ((struct queue_elem *)); | |
77 | static void process_define_cond_exec PARAMS ((void)); | |
3916d6d8 RH |
78 | \f |
79 | void | |
80 | message_with_line VPARAMS ((int lineno, const char *msg, ...)) | |
81 | { | |
82 | #ifndef ANSI_PROTOTYPES | |
83 | int lineno; | |
84 | const char *msg; | |
85 | #endif | |
86 | va_list ap; | |
87 | ||
88 | VA_START (ap, msg); | |
89 | ||
90 | #ifndef ANSI_PROTOTYPES | |
91 | lineno = va_arg (ap, int); | |
92 | msg = va_arg (ap, const char *); | |
93 | #endif | |
36244024 | 94 | |
3916d6d8 RH |
95 | fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno); |
96 | vfprintf (stderr, msg, ap); | |
97 | fputc ('\n', stderr); | |
98 | ||
99 | va_end (ap); | |
100 | } | |
101 | \f | |
3262c1f5 RH |
102 | /* Queue PATTERN on LIST_TAIL. */ |
103 | ||
104 | static void | |
105 | queue_pattern (pattern, list_tail, lineno) | |
106 | rtx pattern; | |
107 | struct queue_elem ***list_tail; | |
108 | int lineno; | |
109 | { | |
110 | struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e)); | |
111 | e->data = pattern; | |
112 | e->lineno = lineno; | |
113 | e->next = NULL; | |
114 | **list_tail = e; | |
115 | *list_tail = &e->next; | |
116 | } | |
117 | ||
c88c0d42 CP |
118 | /* Recursively remove constraints from an rtx. */ |
119 | ||
120 | static void | |
121 | remove_constraints (part) | |
122 | rtx part; | |
123 | { | |
124 | register int i, j; | |
125 | register const char *format_ptr; | |
126 | ||
127 | if (part == 0) | |
128 | return; | |
129 | ||
130 | if (GET_CODE (part) == MATCH_OPERAND) | |
131 | XSTR (part, 2) = ""; | |
132 | else if (GET_CODE (part) == MATCH_SCRATCH) | |
133 | XSTR (part, 1) = ""; | |
134 | ||
135 | format_ptr = GET_RTX_FORMAT (GET_CODE (part)); | |
136 | ||
137 | for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++) | |
138 | switch (*format_ptr++) | |
139 | { | |
140 | case 'e': | |
141 | case 'u': | |
142 | remove_constraints (XEXP (part, i)); | |
143 | break; | |
144 | case 'E': | |
145 | if (XVEC (part, i) != NULL) | |
146 | for (j = 0; j < XVECLEN (part, i); j++) | |
147 | remove_constraints (XVECEXP (part, i, j)); | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
3262c1f5 | 152 | /* Process a top level rtx in some way, queueing as appropriate. */ |
c88c0d42 CP |
153 | |
154 | static void | |
3262c1f5 RH |
155 | process_rtx (desc, lineno) |
156 | rtx desc; | |
157 | int lineno; | |
158 | { | |
159 | switch (GET_CODE (desc)) | |
160 | { | |
161 | case DEFINE_INSN: | |
162 | queue_pattern (desc, &define_insn_tail, lineno); | |
163 | break; | |
164 | ||
165 | case DEFINE_COND_EXEC: | |
166 | queue_pattern (desc, &define_cond_exec_tail, lineno); | |
167 | break; | |
168 | ||
169 | case DEFINE_ATTR: | |
170 | queue_pattern (desc, &define_attr_tail, lineno); | |
171 | break; | |
172 | ||
173 | case DEFINE_INSN_AND_SPLIT: | |
174 | { | |
175 | const char *split_cond; | |
176 | rtx split; | |
de4bfbcb | 177 | int i; |
3262c1f5 RH |
178 | |
179 | /* Create a split with values from the insn_and_split. */ | |
180 | split = rtx_alloc (DEFINE_SPLIT); | |
de4bfbcb RH |
181 | |
182 | i = XVECLEN (desc, 1); | |
fbd40359 | 183 | XVEC (split, 0) = rtvec_alloc (i); |
de4bfbcb RH |
184 | while (--i >= 0) |
185 | { | |
186 | XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i)); | |
187 | remove_constraints (XVECEXP (split, 0, i)); | |
188 | } | |
3262c1f5 RH |
189 | |
190 | /* If the split condition starts with "&&", append it to the | |
191 | insn condition to create the new split condition. */ | |
192 | split_cond = XSTR (desc, 4); | |
193 | if (split_cond[0] == '&' && split_cond[1] == '&') | |
194 | { | |
195 | const char *insn_cond = XSTR (desc, 2); | |
196 | size_t insn_cond_len = strlen (insn_cond); | |
197 | size_t split_cond_len = strlen (split_cond); | |
198 | char *combined; | |
199 | ||
200 | combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1); | |
201 | memcpy (combined, insn_cond, insn_cond_len); | |
202 | memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1); | |
203 | ||
204 | split_cond = combined; | |
205 | } | |
206 | XSTR (split, 1) = split_cond; | |
207 | XVEC (split, 2) = XVEC (desc, 5); | |
208 | XSTR (split, 3) = XSTR (desc, 6); | |
209 | ||
210 | /* Fix up the DEFINE_INSN. */ | |
211 | PUT_CODE (desc, DEFINE_INSN); | |
212 | XVEC (desc, 4) = XVEC (desc, 7); | |
213 | ||
214 | /* Queue them. */ | |
215 | queue_pattern (desc, &define_insn_tail, lineno); | |
216 | queue_pattern (split, &other_tail, lineno); | |
217 | break; | |
218 | } | |
219 | ||
220 | default: | |
221 | queue_pattern (desc, &other_tail, lineno); | |
222 | break; | |
c88c0d42 CP |
223 | } |
224 | } | |
3916d6d8 | 225 | \f |
3262c1f5 RH |
226 | /* Return true if attribute PREDICABLE is true for ELEM, which holds |
227 | a DEFINE_INSN. */ | |
228 | ||
229 | static int | |
230 | is_predicable (elem) | |
231 | struct queue_elem *elem; | |
232 | { | |
233 | rtvec vec = XVEC (elem->data, 4); | |
234 | const char *value; | |
235 | int i; | |
236 | ||
237 | if (! vec) | |
238 | return predicable_default; | |
239 | ||
240 | for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i) | |
241 | { | |
242 | rtx sub = RTVEC_ELT (vec, i); | |
243 | switch (GET_CODE (sub)) | |
244 | { | |
245 | case SET_ATTR: | |
246 | if (strcmp (XSTR (sub, 0), "predicable") == 0) | |
247 | { | |
248 | value = XSTR (sub, 1); | |
249 | goto found; | |
250 | } | |
251 | break; | |
252 | ||
253 | case SET_ATTR_ALTERNATIVE: | |
254 | if (strcmp (XSTR (sub, 0), "predicable") == 0) | |
255 | { | |
256 | message_with_line (elem->lineno, | |
257 | "multiple alternatives for `predicable'"); | |
258 | errors = 1; | |
259 | return 0; | |
260 | } | |
261 | break; | |
262 | ||
263 | case SET: | |
264 | if (GET_CODE (SET_DEST (sub)) != ATTR | |
265 | || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0) | |
266 | break; | |
267 | sub = SET_SRC (sub); | |
268 | if (GET_CODE (sub) == CONST_STRING) | |
269 | { | |
270 | value = XSTR (sub, 0); | |
271 | goto found; | |
272 | } | |
273 | ||
274 | /* ??? It would be possible to handle this if we really tried. | |
275 | It's not easy though, and I'm not going to bother until it | |
276 | really proves necessary. */ | |
277 | message_with_line (elem->lineno, | |
278 | "non-constant value for `predicable'"); | |
279 | errors = 1; | |
280 | return 0; | |
281 | ||
282 | default: | |
283 | abort (); | |
284 | } | |
285 | } | |
286 | ||
287 | return predicable_default; | |
288 | ||
289 | found: | |
290 | /* Verify that predicability does not vary on the alternative. */ | |
291 | /* ??? It should be possible to handle this by simply eliminating | |
292 | the non-predicable alternatives from the insn. FRV would like | |
293 | to do this. Delay this until we've got the basics solid. */ | |
294 | if (strchr (value, ',') != NULL) | |
295 | { | |
296 | message_with_line (elem->lineno, | |
297 | "multiple alternatives for `predicable'"); | |
298 | errors = 1; | |
299 | return 0; | |
300 | } | |
301 | ||
302 | /* Find out which value we're looking at. */ | |
303 | if (strcmp (value, predicable_true) == 0) | |
304 | return 1; | |
305 | if (strcmp (value, predicable_false) == 0) | |
306 | return 0; | |
307 | ||
308 | message_with_line (elem->lineno, | |
309 | "Unknown value `%s' for `predicable' attribute", | |
310 | value); | |
311 | errors = 1; | |
312 | return 0; | |
313 | } | |
314 | ||
315 | /* Examine the attribute "predicable"; discover its boolean values | |
316 | and its default. */ | |
317 | ||
318 | static void | |
319 | identify_predicable_attribute () | |
320 | { | |
321 | struct queue_elem *elem; | |
322 | char *true, *false; | |
323 | const char *value; | |
324 | size_t len; | |
325 | ||
326 | /* Look for the DEFINE_ATTR for `predicable', which must exist. */ | |
327 | for (elem = define_attr_queue; elem ; elem = elem->next) | |
328 | if (strcmp (XSTR (elem->data, 0), "predicable") == 0) | |
329 | goto found; | |
330 | ||
331 | message_with_line (define_cond_exec_queue->lineno, | |
332 | "Attribute `predicable' not defined"); | |
333 | errors = 1; | |
334 | return; | |
335 | ||
336 | found: | |
337 | value = XSTR (elem->data, 1); | |
338 | len = strlen (value); | |
339 | false = (char *) xmalloc (len + 1); | |
340 | memcpy (false, value, len + 1); | |
341 | ||
342 | true = strchr (false, ','); | |
343 | if (true == NULL || strchr (++true, ',') != NULL) | |
344 | { | |
345 | message_with_line (elem->lineno, | |
346 | "Attribute `predicable' is not a boolean"); | |
347 | errors = 1; | |
348 | return; | |
349 | } | |
350 | true[-1] = '\0'; | |
351 | ||
352 | predicable_true = true; | |
353 | predicable_false = false; | |
354 | ||
355 | switch (GET_CODE (XEXP (elem->data, 2))) | |
356 | { | |
357 | case CONST_STRING: | |
358 | value = XSTR (XEXP (elem->data, 2), 0); | |
359 | break; | |
360 | ||
361 | case CONST: | |
362 | message_with_line (elem->lineno, | |
363 | "Attribute `predicable' cannot be const"); | |
364 | errors = 1; | |
365 | return; | |
366 | ||
367 | default: | |
368 | message_with_line (elem->lineno, | |
369 | "Attribute `predicable' must have a constant default"); | |
370 | errors = 1; | |
371 | return; | |
372 | } | |
373 | ||
374 | if (strcmp (value, true) == 0) | |
375 | predicable_default = 1; | |
376 | else if (strcmp (value, false) == 0) | |
377 | predicable_default = 0; | |
378 | else | |
379 | { | |
380 | message_with_line (elem->lineno, | |
381 | "Unknown value `%s' for `predicable' attribute", | |
382 | value); | |
383 | errors = 1; | |
384 | } | |
385 | } | |
386 | ||
387 | /* Return the number of alternatives in constraint S. */ | |
388 | ||
389 | static int | |
390 | n_alternatives (s) | |
391 | const char *s; | |
392 | { | |
393 | int n = 1; | |
394 | ||
395 | if (s) | |
396 | while (*s) | |
397 | n += (*s++ == ','); | |
398 | ||
399 | return n; | |
400 | } | |
401 | ||
402 | /* Determine how many alternatives there are in INSN, and how many | |
403 | operands. */ | |
404 | ||
405 | static void | |
406 | collect_insn_data (pattern, palt, pmax) | |
407 | rtx pattern; | |
408 | int *palt, *pmax; | |
409 | { | |
410 | const char *fmt; | |
411 | enum rtx_code code; | |
412 | int i, j, len; | |
413 | ||
414 | code = GET_CODE (pattern); | |
415 | switch (code) | |
416 | { | |
417 | case MATCH_OPERAND: | |
892ecf92 RH |
418 | i = n_alternatives (XSTR (pattern, 2)); |
419 | *palt = (i > *palt ? i : *palt); | |
3262c1f5 RH |
420 | /* FALLTHRU */ |
421 | ||
422 | case MATCH_OPERATOR: | |
423 | case MATCH_SCRATCH: | |
424 | case MATCH_PARALLEL: | |
425 | case MATCH_INSN: | |
426 | i = XINT (pattern, 0); | |
427 | if (i > *pmax) | |
428 | *pmax = i; | |
429 | break; | |
430 | ||
431 | default: | |
432 | break; | |
433 | } | |
434 | ||
435 | fmt = GET_RTX_FORMAT (code); | |
436 | len = GET_RTX_LENGTH (code); | |
437 | for (i = 0; i < len; i++) | |
438 | { | |
439 | switch (fmt[i]) | |
440 | { | |
441 | case 'e': case 'u': | |
442 | collect_insn_data (XEXP (pattern, i), palt, pmax); | |
443 | break; | |
444 | ||
445 | case 'V': | |
446 | if (XVEC (pattern, i) == NULL) | |
447 | break; | |
448 | /* FALLTHRU */ | |
449 | case 'E': | |
450 | for (j = XVECLEN (pattern, i) - 1; j >= 0; --j) | |
451 | collect_insn_data (XVECEXP (pattern, i, j), palt, pmax); | |
452 | break; | |
453 | ||
454 | case 'i': case 'w': case '0': case 's': case 'S': | |
455 | break; | |
456 | ||
457 | default: | |
458 | abort (); | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | static rtx | |
464 | alter_predicate_for_insn (pattern, alt, max_op, lineno) | |
465 | rtx pattern; | |
466 | int alt, max_op, lineno; | |
467 | { | |
468 | const char *fmt; | |
469 | enum rtx_code code; | |
470 | int i, j, len; | |
471 | ||
472 | code = GET_CODE (pattern); | |
473 | switch (code) | |
474 | { | |
475 | case MATCH_OPERAND: | |
476 | { | |
477 | const char *c = XSTR (pattern, 2); | |
478 | ||
479 | if (n_alternatives (c) != 1) | |
480 | { | |
481 | message_with_line (lineno, | |
482 | "Too many alternatives for operand %d", | |
483 | XINT (pattern, 0)); | |
484 | errors = 1; | |
485 | return NULL; | |
486 | } | |
487 | ||
488 | /* Replicate C as needed to fill out ALT alternatives. */ | |
489 | if (c && *c && alt > 1) | |
490 | { | |
491 | size_t c_len = strlen (c); | |
492 | size_t len = alt * (c_len + 1); | |
493 | char *new_c = (char *) xmalloc (len); | |
494 | ||
495 | memcpy (new_c, c, c_len); | |
496 | for (i = 1; i < alt; ++i) | |
497 | { | |
498 | new_c[i * (c_len + 1) - 1] = ','; | |
499 | memcpy (&new_c[i * (c_len + 1)], c, c_len); | |
500 | } | |
501 | new_c[len - 1] = '\0'; | |
502 | XSTR (pattern, 2) = new_c; | |
503 | } | |
504 | } | |
505 | /* FALLTHRU */ | |
506 | ||
507 | case MATCH_OPERATOR: | |
508 | case MATCH_SCRATCH: | |
509 | case MATCH_PARALLEL: | |
510 | case MATCH_INSN: | |
511 | XINT (pattern, 0) += max_op; | |
512 | break; | |
513 | ||
514 | default: | |
515 | break; | |
516 | } | |
517 | ||
518 | fmt = GET_RTX_FORMAT (code); | |
519 | len = GET_RTX_LENGTH (code); | |
520 | for (i = 0; i < len; i++) | |
521 | { | |
522 | rtx r; | |
523 | ||
524 | switch (fmt[i]) | |
525 | { | |
526 | case 'e': case 'u': | |
527 | r = alter_predicate_for_insn (XEXP (pattern, i), alt, | |
528 | max_op, lineno); | |
529 | if (r == NULL) | |
530 | return r; | |
531 | break; | |
532 | ||
533 | case 'E': | |
534 | for (j = XVECLEN (pattern, i) - 1; j >= 0; --j) | |
535 | { | |
536 | r = alter_predicate_for_insn (XVECEXP (pattern, i, j), | |
537 | alt, max_op, lineno); | |
538 | if (r == NULL) | |
539 | return r; | |
540 | } | |
541 | break; | |
542 | ||
543 | case 'i': case 'w': case '0': case 's': | |
544 | break; | |
545 | ||
546 | default: | |
547 | abort (); | |
548 | } | |
549 | } | |
550 | ||
551 | return pattern; | |
552 | } | |
553 | ||
554 | static const char * | |
555 | alter_test_for_insn (ce_elem, insn_elem) | |
556 | struct queue_elem *ce_elem, *insn_elem; | |
557 | { | |
558 | const char *ce_test, *insn_test; | |
559 | char *new_test; | |
560 | size_t len, ce_len, insn_len; | |
561 | ||
562 | ce_test = XSTR (ce_elem->data, 1); | |
563 | insn_test = XSTR (insn_elem->data, 2); | |
564 | if (!ce_test || *ce_test == '\0') | |
565 | return insn_test; | |
566 | if (!insn_test || *insn_test == '\0') | |
567 | return ce_test; | |
568 | ||
569 | ce_len = strlen (ce_test); | |
570 | insn_len = strlen (insn_test); | |
571 | len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1; | |
572 | new_test = (char *) xmalloc (len); | |
573 | ||
574 | sprintf (new_test, "(%s) && (%s)", ce_test, insn_test); | |
575 | ||
576 | return new_test; | |
577 | } | |
578 | ||
579 | /* Adjust all of the operand numbers in OLD to match the shift they'll | |
580 | get from an operand displacement of DISP. Return a pointer after the | |
581 | adjusted string. */ | |
582 | ||
583 | static char * | |
584 | shift_output_template (new, old, disp) | |
585 | char *new; | |
586 | const char *old; | |
587 | int disp; | |
588 | { | |
589 | while (*old) | |
590 | { | |
591 | char c = *old++; | |
592 | *new++ = c; | |
593 | if (c == '%') | |
594 | { | |
595 | c = *old++; | |
596 | if (ISDIGIT ((unsigned char) c)) | |
597 | c += disp; | |
598 | else if (ISUPPER ((unsigned char) c) | |
599 | || ISLOWER ((unsigned char) c)) | |
600 | { | |
601 | *new++ = c; | |
602 | c = *old++ + disp; | |
603 | } | |
604 | *new++ = c; | |
605 | } | |
606 | } | |
607 | ||
608 | return new; | |
609 | } | |
610 | ||
611 | static const char * | |
612 | alter_output_for_insn (ce_elem, insn_elem, alt, max_op) | |
613 | struct queue_elem *ce_elem, *insn_elem; | |
614 | int alt, max_op; | |
615 | { | |
616 | const char *ce_out, *insn_out; | |
617 | char *new, *p; | |
618 | size_t len, ce_len, insn_len; | |
619 | ||
620 | /* ??? Could coordinate with genoutput to not duplicate code here. */ | |
621 | ||
622 | ce_out = XSTR (ce_elem->data, 2); | |
623 | insn_out = XSTR (insn_elem->data, 3); | |
624 | if (!ce_out || *ce_out == '\0') | |
625 | return insn_out; | |
626 | ||
627 | ce_len = strlen (ce_out); | |
628 | insn_len = strlen (insn_out); | |
629 | ||
630 | if (*insn_out == '*') | |
631 | /* You must take care of the predicate yourself. */ | |
632 | return insn_out; | |
633 | ||
634 | if (*insn_out == '@') | |
635 | { | |
636 | len = (ce_len + 1) * alt + insn_len + 1; | |
637 | p = new = xmalloc (len); | |
638 | ||
639 | do | |
640 | { | |
641 | do | |
642 | *p++ = *insn_out++; | |
643 | while (ISSPACE ((unsigned char) *insn_out)); | |
644 | ||
645 | if (*insn_out != '#') | |
646 | { | |
647 | p = shift_output_template (p, ce_out, max_op); | |
648 | *p++ = ' '; | |
649 | } | |
650 | ||
651 | do | |
652 | *p++ = *insn_out++; | |
653 | while (*insn_out && *insn_out != '\n'); | |
654 | } | |
655 | while (*insn_out); | |
656 | *p = '\0'; | |
657 | } | |
658 | else | |
659 | { | |
660 | len = ce_len + 1 + insn_len + 1; | |
661 | new = xmalloc (len); | |
662 | ||
663 | p = shift_output_template (new, ce_out, max_op); | |
664 | *p++ = ' '; | |
665 | memcpy (p, insn_out, insn_len + 1); | |
666 | } | |
667 | ||
668 | return new; | |
669 | } | |
670 | ||
671 | /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */ | |
672 | ||
673 | static void | |
674 | process_one_cond_exec (ce_elem) | |
675 | struct queue_elem *ce_elem; | |
676 | { | |
677 | struct queue_elem *insn_elem; | |
678 | for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next) | |
679 | { | |
680 | int alternatives, max_operand; | |
681 | rtx pred, insn, pattern; | |
682 | ||
683 | if (! is_predicable (insn_elem)) | |
684 | continue; | |
685 | ||
686 | alternatives = 1; | |
687 | max_operand = -1; | |
688 | collect_insn_data (insn_elem->data, &alternatives, &max_operand); | |
689 | max_operand += 1; | |
690 | ||
691 | if (XVECLEN (ce_elem->data, 0) != 1) | |
692 | { | |
693 | message_with_line (ce_elem->lineno, | |
694 | "too many patterns in predicate"); | |
695 | errors = 1; | |
696 | return; | |
697 | } | |
698 | ||
699 | pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0)); | |
700 | pred = alter_predicate_for_insn (pred, alternatives, max_operand, | |
701 | ce_elem->lineno); | |
702 | if (pred == NULL) | |
703 | return; | |
704 | ||
705 | /* Construct a new pattern for the new insn. */ | |
706 | insn = copy_rtx (insn_elem->data); | |
707 | XSTR (insn, 0) = ""; | |
708 | pattern = rtx_alloc (COND_EXEC); | |
709 | XEXP (pattern, 0) = pred; | |
710 | if (XVECLEN (insn, 1) == 1) | |
711 | { | |
712 | XEXP (pattern, 1) = XVECEXP (insn, 1, 0); | |
713 | XVECEXP (insn, 1, 0) = pattern; | |
714 | PUT_NUM_ELEM (XVEC (insn, 1), 1); | |
715 | } | |
716 | else | |
717 | { | |
718 | XEXP (pattern, 1) = rtx_alloc (PARALLEL); | |
719 | XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1); | |
720 | XVEC (insn, 1) = rtvec_alloc (1); | |
721 | XVECEXP (insn, 1, 0) = pattern; | |
722 | } | |
723 | ||
724 | XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem); | |
725 | XSTR (insn, 3) = alter_output_for_insn (ce_elem, insn_elem, | |
726 | alternatives, max_operand); | |
727 | ||
728 | /* ??? Set `predicable' to false. Not crucial since it's really | |
729 | only used here, and we won't reprocess this new pattern. */ | |
730 | ||
731 | /* Put the new pattern on the `other' list so that it | |
732 | (a) is not reprocessed by other define_cond_exec patterns | |
733 | (b) appears after all normal define_insn patterns. | |
734 | ||
735 | ??? B is debatable. If one has normal insns that match | |
736 | cond_exec patterns, they will be preferred over these | |
737 | generated patterns. Whether this matters in practice, or if | |
738 | it's a good thing, or whether we should thread these new | |
739 | patterns into the define_insn chain just after their generator | |
740 | is something we'll have to experiment with. */ | |
741 | ||
742 | queue_pattern (insn, &other_tail, insn_elem->lineno); | |
743 | } | |
744 | } | |
745 | ||
746 | /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN | |
747 | patterns appropriately. */ | |
748 | ||
749 | static void | |
750 | process_define_cond_exec () | |
751 | { | |
752 | struct queue_elem *elem; | |
753 | ||
754 | identify_predicable_attribute (); | |
755 | if (errors) | |
756 | return; | |
757 | ||
758 | for (elem = define_cond_exec_queue; elem ; elem = elem->next) | |
759 | process_one_cond_exec (elem); | |
760 | } | |
761 | \f | |
c88c0d42 CP |
762 | /* The entry point for initializing the reader. */ |
763 | ||
3262c1f5 | 764 | int |
c88c0d42 | 765 | init_md_reader (filename) |
3262c1f5 | 766 | const char *filename; |
c88c0d42 | 767 | { |
3262c1f5 RH |
768 | FILE *input_file; |
769 | int c; | |
770 | ||
3916d6d8 | 771 | read_rtx_filename = filename; |
c88c0d42 | 772 | input_file = fopen (filename, "r"); |
c88c0d42 CP |
773 | if (input_file == 0) |
774 | { | |
775 | perror (filename); | |
776 | return FATAL_EXIT_CODE; | |
777 | } | |
778 | ||
3916d6d8 | 779 | obstack_init (rtl_obstack); |
3262c1f5 | 780 | errors = 0; |
c88c0d42 | 781 | sequence_num = 0; |
c88c0d42 | 782 | |
3262c1f5 RH |
783 | /* Read the entire file. */ |
784 | while (1) | |
c88c0d42 | 785 | { |
3262c1f5 RH |
786 | rtx desc; |
787 | int lineno; | |
788 | ||
c88c0d42 CP |
789 | c = read_skip_spaces (input_file); |
790 | if (c == EOF) | |
3262c1f5 | 791 | break; |
c88c0d42 CP |
792 | |
793 | ungetc (c, input_file); | |
3262c1f5 | 794 | lineno = read_rtx_lineno; |
c88c0d42 | 795 | desc = read_rtx (input_file); |
3262c1f5 | 796 | process_rtx (desc, lineno); |
c88c0d42 | 797 | } |
3262c1f5 RH |
798 | fclose (input_file); |
799 | ||
800 | /* Process define_cond_exec patterns. */ | |
801 | if (define_cond_exec_queue != NULL) | |
802 | process_define_cond_exec (); | |
803 | ||
804 | return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE; | |
805 | } | |
806 | ||
807 | /* The entry point for reading a single rtx from an md file. */ | |
808 | ||
809 | rtx | |
810 | read_md_rtx (lineno, seqnr) | |
811 | int *lineno; | |
812 | int *seqnr; | |
813 | { | |
814 | struct queue_elem **queue, *elem; | |
815 | rtx desc; | |
816 | ||
817 | /* Read all patterns from a given queue before moving on to the next. */ | |
818 | if (define_attr_queue != NULL) | |
819 | queue = &define_attr_queue; | |
820 | else if (define_insn_queue != NULL) | |
821 | queue = &define_insn_queue; | |
822 | else if (other_queue != NULL) | |
823 | queue = &other_queue; | |
824 | else | |
825 | return NULL_RTX; | |
826 | ||
827 | elem = *queue; | |
828 | *queue = elem->next; | |
829 | desc = elem->data; | |
830 | *lineno = elem->lineno; | |
c88c0d42 | 831 | *seqnr = sequence_num; |
3262c1f5 RH |
832 | |
833 | free (elem); | |
834 | ||
c88c0d42 CP |
835 | switch (GET_CODE (desc)) |
836 | { | |
3262c1f5 RH |
837 | case DEFINE_INSN: |
838 | case DEFINE_EXPAND: | |
839 | case DEFINE_SPLIT: | |
840 | case DEFINE_PEEPHOLE: | |
841 | case DEFINE_PEEPHOLE2: | |
842 | sequence_num++; | |
843 | break; | |
844 | ||
845 | default: | |
846 | break; | |
c88c0d42 CP |
847 | } |
848 | ||
849 | return desc; | |
850 | } | |
1b3459d2 ZW |
851 | |
852 | /* Until we can use the versions in libiberty. */ | |
853 | char * | |
854 | xstrdup (input) | |
855 | const char *input; | |
856 | { | |
857 | register size_t len = strlen (input) + 1; | |
858 | register char *output = xmalloc (len); | |
859 | memcpy (output, input, len); | |
860 | return output; | |
861 | } | |
862 | ||
863 | PTR | |
864 | xrealloc (old, size) | |
865 | PTR old; | |
866 | size_t size; | |
867 | { | |
868 | register PTR ptr; | |
869 | if (old) | |
870 | ptr = (PTR) realloc (old, size); | |
871 | else | |
872 | ptr = (PTR) malloc (size); | |
873 | if (!ptr) | |
874 | fatal ("virtual memory exhausted"); | |
875 | return ptr; | |
876 | } | |
877 | ||
878 | PTR | |
879 | xmalloc (size) | |
880 | size_t size; | |
881 | { | |
882 | register PTR val = (PTR) malloc (size); | |
883 | ||
884 | if (val == 0) | |
885 | fatal ("virtual memory exhausted"); | |
886 | return val; | |
887 | } |