]>
Commit | Line | Data |
---|---|---|
8d365e72 | 1 | /* Generate code from machine description to emit insns as rtl. |
d353bf18 | 2 | Copyright (C) 1987-2015 Free Software Foundation, Inc. |
8d365e72 | 3 | |
f12b58b3 | 4 | This file is part of GCC. |
8d365e72 | 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. |
8d365e72 | 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. | |
8d365e72 | 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/>. */ | |
8d365e72 | 19 | |
20 | ||
805e22b2 | 21 | #include "bconfig.h" |
5ce88198 | 22 | #include "system.h" |
805e22b2 | 23 | #include "coretypes.h" |
24 | #include "tm.h" | |
8d365e72 | 25 | #include "rtl.h" |
04b58880 | 26 | #include "errors.h" |
960ebfe7 | 27 | #include "read-md.h" |
c5ddd6b5 | 28 | #include "gensupport.h" |
8d365e72 | 29 | |
8d365e72 | 30 | |
8d365e72 | 31 | /* Data structure for recording the patterns of insns that have CLOBBERs. |
1a97be37 | 32 | We use this to output a function that adds these CLOBBERs to a |
8d365e72 | 33 | previously-allocated PARALLEL expression. */ |
34 | ||
35 | struct clobber_pat | |
36 | { | |
c39100fe | 37 | struct clobber_ent *insns; |
8d365e72 | 38 | rtx pattern; |
39 | int first_clobber; | |
40 | struct clobber_pat *next; | |
388df988 | 41 | int has_hard_reg; |
8d365e72 | 42 | } *clobber_list; |
43 | ||
c39100fe | 44 | /* Records one insn that uses the clobber list. */ |
45 | ||
46 | struct clobber_ent | |
47 | { | |
48 | int code_number; /* Counts only insns. */ | |
49 | struct clobber_ent *next; | |
50 | }; | |
51 | ||
1a97be37 | 52 | static void print_code (RTX_CODE); |
53 | static void gen_exp (rtx, enum rtx_code, char *); | |
54 | static void gen_insn (rtx, int); | |
55 | static void gen_expand (rtx); | |
56 | static void gen_split (rtx); | |
57 | static void output_add_clobbers (void); | |
58 | static void output_added_clobbers_hard_reg_p (void); | |
59 | static void gen_rtx_scratch (rtx, enum rtx_code); | |
60 | static void output_peephole2_scratches (rtx); | |
13de78c5 | 61 | |
8d365e72 | 62 | \f |
63 | static void | |
1a97be37 | 64 | print_code (RTX_CODE code) |
8d365e72 | 65 | { |
19cb6b50 | 66 | const char *p1; |
8d365e72 | 67 | for (p1 = GET_RTX_NAME (code); *p1; p1++) |
9af5ce0c | 68 | putchar (TOUPPER (*p1)); |
8d365e72 | 69 | } |
70 | ||
82575fa7 | 71 | static void |
1a97be37 | 72 | gen_rtx_scratch (rtx x, enum rtx_code subroutine_type) |
82575fa7 | 73 | { |
74 | if (subroutine_type == DEFINE_PEEPHOLE2) | |
75 | { | |
76 | printf ("operand%d", XINT (x, 0)); | |
77 | } | |
78 | else | |
79 | { | |
80 | printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x))); | |
81 | } | |
82 | } | |
83 | ||
8d365e72 | 84 | /* Print a C expression to construct an RTX just like X, |
85 | substituting any operand references appearing within. */ | |
86 | ||
87 | static void | |
1a97be37 | 88 | gen_exp (rtx x, enum rtx_code subroutine_type, char *used) |
8d365e72 | 89 | { |
19cb6b50 | 90 | RTX_CODE code; |
91 | int i; | |
92 | int len; | |
93 | const char *fmt; | |
d1f9b275 | 94 | const char *sep = ""; |
8d365e72 | 95 | |
96 | if (x == 0) | |
97 | { | |
9259c67f | 98 | printf ("NULL_RTX"); |
8d365e72 | 99 | return; |
100 | } | |
101 | ||
102 | code = GET_CODE (x); | |
103 | ||
104 | switch (code) | |
105 | { | |
106 | case MATCH_OPERAND: | |
107 | case MATCH_DUP: | |
eeddd58f | 108 | if (used) |
109 | { | |
110 | if (used[XINT (x, 0)]) | |
111 | { | |
112 | printf ("copy_rtx (operand%d)", XINT (x, 0)); | |
113 | return; | |
114 | } | |
115 | used[XINT (x, 0)] = 1; | |
116 | } | |
8d365e72 | 117 | printf ("operand%d", XINT (x, 0)); |
118 | return; | |
119 | ||
120 | case MATCH_OP_DUP: | |
8d7bd4fd | 121 | printf ("gen_rtx_fmt_"); |
122 | for (i = 0; i < XVECLEN (x, 1); i++) | |
123 | printf ("e"); | |
124 | printf (" (GET_CODE (operand%d), ", XINT (x, 0)); | |
904202c3 | 125 | if (GET_MODE (x) == VOIDmode) |
126 | printf ("GET_MODE (operand%d)", XINT (x, 0)); | |
127 | else | |
128 | printf ("%smode", GET_MODE_NAME (GET_MODE (x))); | |
8d365e72 | 129 | for (i = 0; i < XVECLEN (x, 1); i++) |
130 | { | |
131 | printf (",\n\t\t"); | |
eeddd58f | 132 | gen_exp (XVECEXP (x, 1, i), subroutine_type, used); |
8d365e72 | 133 | } |
134 | printf (")"); | |
135 | return; | |
136 | ||
137 | case MATCH_OPERATOR: | |
8d7bd4fd | 138 | printf ("gen_rtx_fmt_"); |
139 | for (i = 0; i < XVECLEN (x, 2); i++) | |
140 | printf ("e"); | |
141 | printf (" (GET_CODE (operand%d)", XINT (x, 0)); | |
8d365e72 | 142 | printf (", %smode", GET_MODE_NAME (GET_MODE (x))); |
143 | for (i = 0; i < XVECLEN (x, 2); i++) | |
144 | { | |
145 | printf (",\n\t\t"); | |
eeddd58f | 146 | gen_exp (XVECEXP (x, 2, i), subroutine_type, used); |
8d365e72 | 147 | } |
148 | printf (")"); | |
149 | return; | |
150 | ||
151 | case MATCH_PARALLEL: | |
aaaa1a6e | 152 | case MATCH_PAR_DUP: |
8d365e72 | 153 | printf ("operand%d", XINT (x, 0)); |
154 | return; | |
155 | ||
156 | case MATCH_SCRATCH: | |
82575fa7 | 157 | gen_rtx_scratch (x, subroutine_type); |
8d365e72 | 158 | return; |
159 | ||
8d365e72 | 160 | case PC: |
161 | printf ("pc_rtx"); | |
162 | return; | |
1a860023 | 163 | case RETURN: |
164 | printf ("ret_rtx"); | |
165 | return; | |
9cb2517e | 166 | case SIMPLE_RETURN: |
167 | printf ("simple_return_rtx"); | |
168 | return; | |
c09425a0 | 169 | case CLOBBER: |
170 | if (REG_P (XEXP (x, 0))) | |
171 | { | |
172 | printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x, 0))), | |
173 | REGNO (XEXP (x, 0))); | |
174 | return; | |
175 | } | |
176 | break; | |
8d365e72 | 177 | |
178 | case CC0: | |
179 | printf ("cc0_rtx"); | |
180 | return; | |
181 | ||
182 | case CONST_INT: | |
183 | if (INTVAL (x) == 0) | |
9259c67f | 184 | printf ("const0_rtx"); |
185 | else if (INTVAL (x) == 1) | |
186 | printf ("const1_rtx"); | |
187 | else if (INTVAL (x) == -1) | |
188 | printf ("constm1_rtx"); | |
949168fb | 189 | else if (-MAX_SAVED_CONST_INT <= INTVAL (x) |
190 | && INTVAL (x) <= MAX_SAVED_CONST_INT) | |
191 | printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]", | |
192 | (int) INTVAL (x)); | |
9259c67f | 193 | else if (INTVAL (x) == STORE_FLAG_VALUE) |
194 | printf ("const_true_rtx"); | |
195 | else | |
13a0a8d8 | 196 | { |
197 | printf ("GEN_INT ("); | |
fe0e3c2a | 198 | printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x)); |
13a0a8d8 | 199 | printf (")"); |
200 | } | |
9259c67f | 201 | return; |
202 | ||
203 | case CONST_DOUBLE: | |
e397ad8e | 204 | case CONST_FIXED: |
e913b5cd | 205 | case CONST_WIDE_INT: |
9259c67f | 206 | /* These shouldn't be written in MD files. Instead, the appropriate |
207 | routines in varasm.c should be called. */ | |
e0a4c0c2 | 208 | gcc_unreachable (); |
99c14947 | 209 | |
210 | default: | |
211 | break; | |
8d365e72 | 212 | } |
213 | ||
3ad7bb1c | 214 | printf ("gen_rtx_"); |
8d365e72 | 215 | print_code (code); |
d1f9b275 | 216 | printf (" ("); |
217 | if (!always_void_p (code)) | |
218 | { | |
219 | printf ("%smode", GET_MODE_NAME (GET_MODE (x))); | |
220 | sep = ",\n\t"; | |
221 | } | |
8d365e72 | 222 | |
223 | fmt = GET_RTX_FORMAT (code); | |
224 | len = GET_RTX_LENGTH (code); | |
225 | for (i = 0; i < len; i++) | |
226 | { | |
227 | if (fmt[i] == '0') | |
228 | break; | |
d1f9b275 | 229 | fputs (sep, stdout); |
e0a4c0c2 | 230 | switch (fmt[i]) |
8d365e72 | 231 | { |
e0a4c0c2 | 232 | case 'e': case 'u': |
233 | gen_exp (XEXP (x, i), subroutine_type, used); | |
234 | break; | |
235 | ||
236 | case 'i': | |
237 | printf ("%u", XINT (x, i)); | |
238 | break; | |
239 | ||
15183fd2 | 240 | case 'r': |
241 | printf ("%u", REGNO (x)); | |
242 | break; | |
243 | ||
e0a4c0c2 | 244 | case 's': |
245 | printf ("\"%s\"", XSTR (x, i)); | |
246 | break; | |
247 | ||
248 | case 'E': | |
249 | { | |
250 | int j; | |
251 | printf ("gen_rtvec (%d", XVECLEN (x, i)); | |
252 | for (j = 0; j < XVECLEN (x, i); j++) | |
253 | { | |
254 | printf (",\n\t\t"); | |
255 | gen_exp (XVECEXP (x, i, j), subroutine_type, used); | |
256 | } | |
257 | printf (")"); | |
258 | break; | |
259 | } | |
260 | ||
261 | default: | |
262 | gcc_unreachable (); | |
8d365e72 | 263 | } |
d1f9b275 | 264 | sep = ",\n\t"; |
8d365e72 | 265 | } |
266 | printf (")"); | |
1a97be37 | 267 | } |
03acb861 | 268 | |
269 | /* Output code to emit the instruction patterns in VEC, with each element | |
270 | becoming a separate instruction. USED is as for gen_exp. */ | |
271 | ||
272 | static void | |
273 | gen_emit_seq (rtvec vec, char *used) | |
274 | { | |
275 | for (int i = 0, len = GET_NUM_ELEM (vec); i < len; ++i) | |
276 | { | |
16d83c02 | 277 | bool last_p = (i == len - 1); |
03acb861 | 278 | rtx next = RTVEC_ELT (vec, i); |
16d83c02 | 279 | if (const char *name = get_emit_function (next)) |
280 | { | |
281 | printf (" %s (", name); | |
282 | gen_exp (next, DEFINE_EXPAND, used); | |
283 | printf (");\n"); | |
284 | if (!last_p && needs_barrier_p (next)) | |
285 | printf (" emit_barrier ();"); | |
286 | } | |
287 | else | |
288 | { | |
289 | printf (" emit ("); | |
290 | gen_exp (next, DEFINE_EXPAND, used); | |
291 | printf (", %s);\n", last_p ? "false" : "true"); | |
292 | } | |
03acb861 | 293 | } |
294 | } | |
8d365e72 | 295 | \f |
296 | /* Generate the `gen_...' function for a DEFINE_INSN. */ | |
297 | ||
298 | static void | |
c04601c1 | 299 | gen_insn (md_rtx_info *info) |
8d365e72 | 300 | { |
22add764 | 301 | struct pattern_stats stats; |
19cb6b50 | 302 | int i; |
8d365e72 | 303 | |
304 | /* See if the pattern for this insn ends with a group of CLOBBERs of (hard) | |
305 | registers or MATCH_SCRATCHes. If so, store away the information for | |
a92771b8 | 306 | later. */ |
8d365e72 | 307 | |
c04601c1 | 308 | rtx insn = info->def; |
8d365e72 | 309 | if (XVEC (insn, 1)) |
310 | { | |
388df988 | 311 | int has_hard_reg = 0; |
312 | ||
8d365e72 | 313 | for (i = XVECLEN (insn, 1) - 1; i > 0; i--) |
388df988 | 314 | { |
315 | if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER) | |
316 | break; | |
317 | ||
8ad4c111 | 318 | if (REG_P (XEXP (XVECEXP (insn, 1, i), 0))) |
388df988 | 319 | has_hard_reg = 1; |
320 | else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH) | |
321 | break; | |
322 | } | |
8d365e72 | 323 | |
324 | if (i != XVECLEN (insn, 1) - 1) | |
325 | { | |
19cb6b50 | 326 | struct clobber_pat *p; |
4c36ffe6 | 327 | struct clobber_ent *link = XNEW (struct clobber_ent); |
19cb6b50 | 328 | int j; |
c39100fe | 329 | |
c04601c1 | 330 | link->code_number = info->index; |
c39100fe | 331 | |
332 | /* See if any previous CLOBBER_LIST entry is the same as this | |
333 | one. */ | |
334 | ||
335 | for (p = clobber_list; p; p = p->next) | |
336 | { | |
337 | if (p->first_clobber != i + 1 | |
338 | || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1)) | |
339 | continue; | |
340 | ||
341 | for (j = i + 1; j < XVECLEN (insn, 1); j++) | |
342 | { | |
2657a7d5 | 343 | rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0); |
344 | rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0); | |
c39100fe | 345 | |
2657a7d5 | 346 | /* OLD and NEW_INSN are the same if both are to be a SCRATCH |
1a97be37 | 347 | of the same mode, |
c39100fe | 348 | or if both are registers of the same mode and number. */ |
2657a7d5 | 349 | if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx) |
350 | && ((GET_CODE (old_rtx) == MATCH_SCRATCH | |
351 | && GET_CODE (new_rtx) == MATCH_SCRATCH) | |
352 | || (REG_P (old_rtx) && REG_P (new_rtx) | |
353 | && REGNO (old_rtx) == REGNO (new_rtx))))) | |
c39100fe | 354 | break; |
355 | } | |
1a97be37 | 356 | |
c39100fe | 357 | if (j == XVECLEN (insn, 1)) |
358 | break; | |
359 | } | |
360 | ||
361 | if (p == 0) | |
362 | { | |
4c36ffe6 | 363 | p = XNEW (struct clobber_pat); |
1a97be37 | 364 | |
c39100fe | 365 | p->insns = 0; |
366 | p->pattern = insn; | |
367 | p->first_clobber = i + 1; | |
368 | p->next = clobber_list; | |
388df988 | 369 | p->has_hard_reg = has_hard_reg; |
c39100fe | 370 | clobber_list = p; |
371 | } | |
372 | ||
373 | link->next = p->insns; | |
374 | p->insns = link; | |
8d365e72 | 375 | } |
376 | } | |
377 | ||
a0761610 | 378 | /* Don't mention instructions whose names are the null string |
379 | or begin with '*'. They are in the machine description just | |
380 | to be recognized. */ | |
381 | if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*') | |
8d365e72 | 382 | return; |
383 | ||
c04601c1 | 384 | printf ("/* %s:%d */\n", info->loc.filename, info->loc.lineno); |
f1c96ec2 | 385 | |
c18bfb9c | 386 | /* Find out how many operands this function has. */ |
22add764 | 387 | get_pattern_stats (&stats, XVEC (insn, 1)); |
388 | if (stats.max_dup_opno > stats.max_opno) | |
c04601c1 | 389 | fatal_at (info->loc, "match_dup operand number has no match_operand"); |
8d365e72 | 390 | |
391 | /* Output the function name and argument declarations. */ | |
392 | printf ("rtx\ngen_%s (", XSTR (insn, 0)); | |
22add764 | 393 | if (stats.num_generator_args) |
394 | for (i = 0; i < stats.num_generator_args; i++) | |
69dc4d00 | 395 | if (i) |
396 | printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i); | |
397 | else | |
398 | printf ("rtx operand%d ATTRIBUTE_UNUSED", i); | |
399 | else | |
400 | printf ("void"); | |
8d365e72 | 401 | printf (")\n"); |
8d365e72 | 402 | printf ("{\n"); |
403 | ||
8b332087 | 404 | /* Output code to construct and return the rtl for the instruction body. */ |
8d365e72 | 405 | |
2ff8d4ae | 406 | rtx pattern = add_implicit_parallel (XVEC (insn, 1)); |
407 | /* ??? This is the traditional behavior, but seems suspect. */ | |
408 | char *used = (XVECLEN (insn, 1) == 1 | |
409 | ? NULL | |
410 | : XCNEWVEC (char, stats.num_generator_args)); | |
411 | printf (" return "); | |
412 | gen_exp (pattern, DEFINE_INSN, used); | |
413 | printf (";\n}\n\n"); | |
414 | XDELETEVEC (used); | |
8d365e72 | 415 | } |
416 | \f | |
417 | /* Generate the `gen_...' function for a DEFINE_EXPAND. */ | |
418 | ||
419 | static void | |
c04601c1 | 420 | gen_expand (md_rtx_info *info) |
8d365e72 | 421 | { |
22add764 | 422 | struct pattern_stats stats; |
19cb6b50 | 423 | int i; |
778725bb | 424 | char *used; |
8d365e72 | 425 | |
c04601c1 | 426 | rtx expand = info->def; |
8d365e72 | 427 | if (strlen (XSTR (expand, 0)) == 0) |
c04601c1 | 428 | fatal_at (info->loc, "define_expand lacks a name"); |
8d365e72 | 429 | if (XVEC (expand, 1) == 0) |
c04601c1 | 430 | fatal_at (info->loc, "define_expand for %s lacks a pattern", |
431 | XSTR (expand, 0)); | |
8d365e72 | 432 | |
c18bfb9c | 433 | /* Find out how many operands this function has. */ |
22add764 | 434 | get_pattern_stats (&stats, XVEC (expand, 1)); |
8d365e72 | 435 | |
436 | /* Output the function name and argument declarations. */ | |
437 | printf ("rtx\ngen_%s (", XSTR (expand, 0)); | |
22add764 | 438 | if (stats.num_generator_args) |
439 | for (i = 0; i < stats.num_generator_args; i++) | |
69dc4d00 | 440 | if (i) |
441 | printf (",\n\trtx operand%d", i); | |
442 | else | |
443 | printf ("rtx operand%d", i); | |
444 | else | |
445 | printf ("void"); | |
8d365e72 | 446 | printf (")\n"); |
8d365e72 | 447 | printf ("{\n"); |
448 | ||
449 | /* If we don't have any C code to write, only one insn is being written, | |
450 | and no MATCH_DUPs are present, we can just return the desired insn | |
451 | like we do for a DEFINE_INSN. This saves memory. */ | |
452 | if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0') | |
22add764 | 453 | && stats.max_opno >= stats.max_dup_opno |
8d365e72 | 454 | && XVECLEN (expand, 1) == 1) |
455 | { | |
456 | printf (" return "); | |
eeddd58f | 457 | gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL); |
8d365e72 | 458 | printf (";\n}\n\n"); |
459 | return; | |
460 | } | |
461 | ||
462 | /* For each operand referred to only with MATCH_DUPs, | |
463 | make a local variable. */ | |
22add764 | 464 | for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++) |
8d365e72 | 465 | printf (" rtx operand%d;\n", i); |
22add764 | 466 | for (; i <= stats.max_scratch_opno; i++) |
917938fe | 467 | printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i); |
9ed997be | 468 | printf (" rtx_insn *_val = 0;\n"); |
8d365e72 | 469 | printf (" start_sequence ();\n"); |
470 | ||
471 | /* The fourth operand of DEFINE_EXPAND is some code to be executed | |
472 | before the actual construction. | |
473 | This code expects to refer to `operands' | |
474 | just as the output-code in a DEFINE_INSN does, | |
475 | but here `operands' is an automatic array. | |
476 | So copy the operand values there before executing it. */ | |
477 | if (XSTR (expand, 3) && *XSTR (expand, 3)) | |
478 | { | |
d5197c98 | 479 | printf (" {\n"); |
22add764 | 480 | if (stats.num_operand_vars > 0) |
481 | printf (" rtx operands[%d];\n", stats.num_operand_vars); | |
482 | ||
8d365e72 | 483 | /* Output code to copy the arguments into `operands'. */ |
22add764 | 484 | for (i = 0; i < stats.num_generator_args; i++) |
d5197c98 | 485 | printf (" operands[%d] = operand%d;\n", i, i); |
8d365e72 | 486 | |
487 | /* Output the special code to be executed before the sequence | |
488 | is generated. */ | |
77c2564f | 489 | print_md_ptr_loc (XSTR (expand, 3)); |
8d365e72 | 490 | printf ("%s\n", XSTR (expand, 3)); |
491 | ||
492 | /* Output code to copy the arguments back out of `operands' | |
493 | (unless we aren't going to use them at all). */ | |
494 | if (XVEC (expand, 1) != 0) | |
495 | { | |
22add764 | 496 | for (i = 0; i < stats.num_operand_vars; i++) |
4081d362 | 497 | { |
498 | printf (" operand%d = operands[%d];\n", i, i); | |
499 | printf (" (void) operand%d;\n", i); | |
500 | } | |
8d365e72 | 501 | } |
d5197c98 | 502 | printf (" }\n"); |
8d365e72 | 503 | } |
504 | ||
22add764 | 505 | used = XCNEWVEC (char, stats.num_operand_vars); |
03acb861 | 506 | gen_emit_seq (XVEC (expand, 1), used); |
778725bb | 507 | XDELETEVEC (used); |
508 | ||
31d3e01c | 509 | /* Call `get_insns' to extract the list of all the |
8d365e72 | 510 | insns emitted within this gen_... function. */ |
511 | ||
31d3e01c | 512 | printf (" _val = get_insns ();\n"); |
8d365e72 | 513 | printf (" end_sequence ();\n"); |
514 | printf (" return _val;\n}\n\n"); | |
515 | } | |
516 | ||
31d3e01c | 517 | /* Like gen_expand, but generates insns resulting from splitting SPLIT. */ |
a92771b8 | 518 | |
8d365e72 | 519 | static void |
c04601c1 | 520 | gen_split (md_rtx_info *info) |
8d365e72 | 521 | { |
22add764 | 522 | struct pattern_stats stats; |
19cb6b50 | 523 | int i; |
c04601c1 | 524 | rtx split = info->def; |
35823b64 | 525 | const char *const name = |
526 | ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split"); | |
388df988 | 527 | const char *unused; |
eeddd58f | 528 | char *used; |
82575fa7 | 529 | |
8d365e72 | 530 | if (XVEC (split, 0) == 0) |
c04601c1 | 531 | fatal_at (info->loc, "%s lacks a pattern", |
532 | GET_RTX_NAME (GET_CODE (split))); | |
8d365e72 | 533 | else if (XVEC (split, 2) == 0) |
c04601c1 | 534 | fatal_at (info->loc, "%s lacks a replacement pattern", |
535 | GET_RTX_NAME (GET_CODE (split))); | |
8d365e72 | 536 | |
537 | /* Find out how many operands this function has. */ | |
538 | ||
22add764 | 539 | get_pattern_stats (&stats, XVEC (split, 2)); |
540 | unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : ""); | |
541 | used = XCNEWVEC (char, stats.num_operand_vars); | |
8d365e72 | 542 | |
82575fa7 | 543 | /* Output the prototype, function name and argument declarations. */ |
544 | if (GET_CODE (split) == DEFINE_PEEPHOLE2) | |
545 | { | |
58a87a29 | 546 | printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n", |
c04601c1 | 547 | name, info->index); |
548 | printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED," | |
549 | " rtx *operands%s)\n", | |
550 | name, info->index, unused); | |
82575fa7 | 551 | } |
552 | else | |
553 | { | |
58a87a29 | 554 | printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n", |
c04601c1 | 555 | info->index); |
58a87a29 | 556 | printf ("rtx_insn *\ngen_split_%d " |
557 | "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n", | |
c04601c1 | 558 | info->index, unused); |
82575fa7 | 559 | } |
8d365e72 | 560 | printf ("{\n"); |
561 | ||
562 | /* Declare all local variables. */ | |
22add764 | 563 | for (i = 0; i < stats.num_operand_vars; i++) |
8d365e72 | 564 | printf (" rtx operand%d;\n", i); |
58a87a29 | 565 | printf (" rtx_insn *_val = NULL;\n"); |
82575fa7 | 566 | |
567 | if (GET_CODE (split) == DEFINE_PEEPHOLE2) | |
568 | output_peephole2_scratches (split); | |
569 | ||
0eb20de5 | 570 | printf (" if (dump_file)\n"); |
571 | printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n", | |
c04601c1 | 572 | name, info->index); |
0eb20de5 | 573 | |
8d365e72 | 574 | printf (" start_sequence ();\n"); |
575 | ||
576 | /* The fourth operand of DEFINE_SPLIT is some code to be executed | |
577 | before the actual construction. */ | |
578 | ||
579 | if (XSTR (split, 3)) | |
9e959634 | 580 | { |
77c2564f | 581 | print_md_ptr_loc (XSTR (split, 3)); |
9e959634 | 582 | printf ("%s\n", XSTR (split, 3)); |
583 | } | |
8d365e72 | 584 | |
585 | /* Output code to copy the arguments back out of `operands' */ | |
22add764 | 586 | for (i = 0; i < stats.num_operand_vars; i++) |
4081d362 | 587 | { |
588 | printf (" operand%d = operands[%d];\n", i, i); | |
589 | printf (" (void) operand%d;\n", i); | |
590 | } | |
8d365e72 | 591 | |
03acb861 | 592 | gen_emit_seq (XVEC (split, 2), used); |
8d365e72 | 593 | |
31d3e01c | 594 | /* Call `get_insns' to make a list of all the |
8d365e72 | 595 | insns emitted within this gen_... function. */ |
596 | ||
31d3e01c | 597 | printf (" _val = get_insns ();\n"); |
8d365e72 | 598 | printf (" end_sequence ();\n"); |
599 | printf (" return _val;\n}\n\n"); | |
eeddd58f | 600 | |
601 | free (used); | |
8d365e72 | 602 | } |
603 | \f | |
604 | /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient | |
605 | size for the insn and an INSN_CODE, and inserts the required CLOBBERs at | |
606 | the end of the vector. */ | |
607 | ||
608 | static void | |
a046944b | 609 | output_add_clobbers (void) |
8d365e72 | 610 | { |
611 | struct clobber_pat *clobber; | |
c39100fe | 612 | struct clobber_ent *ent; |
8d365e72 | 613 | int i; |
614 | ||
69dc4d00 | 615 | printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n"); |
8d365e72 | 616 | printf ("{\n"); |
8d365e72 | 617 | printf (" switch (insn_code_number)\n"); |
618 | printf (" {\n"); | |
619 | ||
620 | for (clobber = clobber_list; clobber; clobber = clobber->next) | |
621 | { | |
c39100fe | 622 | for (ent = clobber->insns; ent; ent = ent->next) |
623 | printf (" case %d:\n", ent->code_number); | |
8d365e72 | 624 | |
625 | for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++) | |
626 | { | |
627 | printf (" XVECEXP (pattern, 0, %d) = ", i); | |
82575fa7 | 628 | gen_exp (XVECEXP (clobber->pattern, 1, i), |
eeddd58f | 629 | GET_CODE (clobber->pattern), NULL); |
8d365e72 | 630 | printf (";\n"); |
631 | } | |
632 | ||
c39100fe | 633 | printf (" break;\n\n"); |
8d365e72 | 634 | } |
635 | ||
636 | printf (" default:\n"); | |
e0a4c0c2 | 637 | printf (" gcc_unreachable ();\n"); |
8d365e72 | 638 | printf (" }\n"); |
639 | printf ("}\n"); | |
640 | } | |
641 | \f | |
e1b5e428 | 642 | /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code |
643 | number that will have clobbers added (as indicated by `recog') and returns | |
644 | 1 if those include a clobber of a hard reg or 0 if all of them just clobber | |
645 | SCRATCH. */ | |
388df988 | 646 | |
647 | static void | |
1a97be37 | 648 | output_added_clobbers_hard_reg_p (void) |
388df988 | 649 | { |
650 | struct clobber_pat *clobber; | |
651 | struct clobber_ent *ent; | |
c1caaef5 | 652 | int clobber_p, used; |
388df988 | 653 | |
69dc4d00 | 654 | printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n"); |
388df988 | 655 | printf ("{\n"); |
656 | printf (" switch (insn_code_number)\n"); | |
657 | printf (" {\n"); | |
658 | ||
659 | for (clobber_p = 0; clobber_p <= 1; clobber_p++) | |
660 | { | |
c1caaef5 | 661 | used = 0; |
388df988 | 662 | for (clobber = clobber_list; clobber; clobber = clobber->next) |
663 | if (clobber->has_hard_reg == clobber_p) | |
664 | for (ent = clobber->insns; ent; ent = ent->next) | |
c1caaef5 | 665 | { |
666 | printf (" case %d:\n", ent->code_number); | |
667 | used++; | |
668 | } | |
388df988 | 669 | |
c1caaef5 | 670 | if (used) |
671 | printf (" return %d;\n\n", clobber_p); | |
388df988 | 672 | } |
673 | ||
674 | printf (" default:\n"); | |
e0a4c0c2 | 675 | printf (" gcc_unreachable ();\n"); |
388df988 | 676 | printf (" }\n"); |
677 | printf ("}\n"); | |
678 | } | |
679 | \f | |
82575fa7 | 680 | /* Generate code to invoke find_free_register () as needed for the |
aa40f561 | 681 | scratch registers used by the peephole2 pattern in SPLIT. */ |
82575fa7 | 682 | |
683 | static void | |
1a97be37 | 684 | output_peephole2_scratches (rtx split) |
82575fa7 | 685 | { |
686 | int i; | |
687 | int insn_nr = 0; | |
6d86fc31 | 688 | bool first = true; |
82575fa7 | 689 | |
690 | for (i = 0; i < XVECLEN (split, 0); i++) | |
691 | { | |
692 | rtx elt = XVECEXP (split, 0, i); | |
693 | if (GET_CODE (elt) == MATCH_SCRATCH) | |
694 | { | |
695 | int last_insn_nr = insn_nr; | |
696 | int cur_insn_nr = insn_nr; | |
697 | int j; | |
698 | for (j = i + 1; j < XVECLEN (split, 0); j++) | |
699 | if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP) | |
700 | { | |
701 | if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0)) | |
702 | last_insn_nr = cur_insn_nr; | |
703 | } | |
704 | else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH) | |
705 | cur_insn_nr++; | |
3e9f1237 | 706 | |
6d86fc31 | 707 | if (first) |
708 | { | |
709 | printf (" HARD_REG_SET _regs_allocated;\n"); | |
710 | printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n"); | |
711 | first = false; | |
712 | } | |
713 | ||
3e9f1237 | 714 | printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\ |
1a97be37 | 715 | return NULL;\n", |
82575fa7 | 716 | XINT (elt, 0), |
3e9f1237 | 717 | insn_nr, last_insn_nr, |
82575fa7 | 718 | XSTR (elt, 1), |
719 | GET_MODE_NAME (GET_MODE (elt))); | |
720 | ||
721 | } | |
722 | else if (GET_CODE (elt) != MATCH_DUP) | |
723 | insn_nr++; | |
724 | } | |
725 | } | |
8d365e72 | 726 | |
8d365e72 | 727 | int |
1a97be37 | 728 | main (int argc, char **argv) |
8d365e72 | 729 | { |
04b58880 | 730 | progname = "genemit"; |
8d365e72 | 731 | |
77ba95d0 | 732 | if (!init_rtx_reader_args (argc, argv)) |
c5ddd6b5 | 733 | return (FATAL_EXIT_CODE); |
8d365e72 | 734 | |
8d365e72 | 735 | /* Assign sequential codes to all entries in the machine description |
736 | in parallel with the tables in insn-output.c. */ | |
737 | ||
8d365e72 | 738 | printf ("/* Generated automatically by the program `genemit'\n\ |
739 | from the machine description file `md'. */\n\n"); | |
740 | ||
741 | printf ("#include \"config.h\"\n"); | |
9aaa1fcc | 742 | printf ("#include \"system.h\"\n"); |
805e22b2 | 743 | printf ("#include \"coretypes.h\"\n"); |
9ef16211 | 744 | printf ("#include \"backend.h\"\n"); |
d040a5b0 | 745 | printf ("#include \"predict.h\"\n"); |
41a8aa41 | 746 | printf ("#include \"tree.h\"\n"); |
9ef16211 | 747 | printf ("#include \"rtl.h\"\n"); |
748 | printf ("#include \"alias.h\"\n"); | |
9ed99284 | 749 | printf ("#include \"varasm.h\"\n"); |
750 | printf ("#include \"stor-layout.h\"\n"); | |
751 | printf ("#include \"calls.h\"\n"); | |
7953c610 | 752 | printf ("#include \"tm_p.h\"\n"); |
d53441c8 | 753 | printf ("#include \"flags.h\"\n"); |
d53441c8 | 754 | printf ("#include \"insn-config.h\"\n"); |
755 | printf ("#include \"expmed.h\"\n"); | |
756 | printf ("#include \"dojump.h\"\n"); | |
757 | printf ("#include \"explow.h\"\n"); | |
758 | printf ("#include \"emit-rtl.h\"\n"); | |
759 | printf ("#include \"stmt.h\"\n"); | |
8d365e72 | 760 | printf ("#include \"expr.h\"\n"); |
34517c64 | 761 | printf ("#include \"insn-codes.h\"\n"); |
d8fc4d0b | 762 | printf ("#include \"optabs.h\"\n"); |
3b55c036 | 763 | printf ("#include \"dfp.h\"\n"); |
8d365e72 | 764 | printf ("#include \"output.h\"\n"); |
3e9f1237 | 765 | printf ("#include \"recog.h\"\n"); |
9ef16211 | 766 | printf ("#include \"df.h\"\n"); |
82575fa7 | 767 | printf ("#include \"resource.h\"\n"); |
05806416 | 768 | printf ("#include \"reload.h\"\n"); |
c18cb818 | 769 | printf ("#include \"diagnostic-core.h\"\n"); |
fb16c776 | 770 | printf ("#include \"regs.h\"\n"); |
a014ec0f | 771 | printf ("#include \"tm-constrs.h\"\n"); |
fb16c776 | 772 | printf ("#include \"ggc.h\"\n"); |
0eb20de5 | 773 | printf ("#include \"dumpfile.h\"\n"); |
e374c627 | 774 | printf ("#include \"target.h\"\n\n"); |
a70fd435 | 775 | printf ("#define FAIL return (end_sequence (), _val)\n"); |
31d3e01c | 776 | printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n"); |
8d365e72 | 777 | |
778 | /* Read the machine description. */ | |
779 | ||
c04601c1 | 780 | md_rtx_info info; |
781 | while (read_md_rtx (&info)) | |
782 | switch (GET_CODE (info.def)) | |
783 | { | |
784 | case DEFINE_INSN: | |
785 | gen_insn (&info); | |
c5ddd6b5 | 786 | break; |
82575fa7 | 787 | |
c04601c1 | 788 | case DEFINE_EXPAND: |
789 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
790 | gen_expand (&info); | |
791 | break; | |
f1c96ec2 | 792 | |
c04601c1 | 793 | case DEFINE_SPLIT: |
794 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
795 | gen_split (&info); | |
796 | break; | |
f1c96ec2 | 797 | |
c04601c1 | 798 | case DEFINE_PEEPHOLE2: |
799 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
800 | gen_split (&info); | |
801 | break; | |
f1c96ec2 | 802 | |
c04601c1 | 803 | default: |
804 | break; | |
805 | } | |
8d365e72 | 806 | |
388df988 | 807 | /* Write out the routines to add CLOBBERs to a pattern and say whether they |
808 | clobber a hard reg. */ | |
8d365e72 | 809 | output_add_clobbers (); |
388df988 | 810 | output_added_clobbers_hard_reg_p (); |
8d365e72 | 811 | |
8d365e72 | 812 | fflush (stdout); |
947491b7 | 813 | return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); |
8d365e72 | 814 | } |