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