]>
Commit | Line | Data |
---|---|---|
65963943 | 1 | /* Generate code from machine description to emit insns as rtl. |
7adcbafe | 2 | Copyright (C) 1987-2022 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 | 31 | /* Data structure for recording the patterns of insns that have CLOBBERs. |
3d7aafde | 32 | We use this to output a function that adds these CLOBBERs to a |
65963943 RK |
33 | previously-allocated PARALLEL expression. */ |
34 | ||
35 | struct clobber_pat | |
36 | { | |
e5f6a288 | 37 | struct clobber_ent *insns; |
65963943 RK |
38 | rtx pattern; |
39 | int first_clobber; | |
40 | struct clobber_pat *next; | |
751aa7cc | 41 | int has_hard_reg; |
65963943 RK |
42 | } *clobber_list; |
43 | ||
e5f6a288 RK |
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 | ||
3d7aafde | 52 | static void output_peephole2_scratches (rtx); |
e009aaf3 | 53 | |
886456e2 RS |
54 | /* True for <X>_optab if that optab isn't allowed to fail. */ |
55 | static bool nofail_optabs[NUM_OPTABS]; | |
65963943 RK |
56 | \f |
57 | static void | |
3d7aafde | 58 | print_code (RTX_CODE code) |
65963943 | 59 | { |
b3694847 | 60 | const char *p1; |
65963943 | 61 | for (p1 = GET_RTX_NAME (code); *p1; p1++) |
c3284718 | 62 | putchar (TOUPPER (*p1)); |
65963943 RK |
63 | } |
64 | ||
ede7cd44 | 65 | static void |
3d7aafde | 66 | gen_rtx_scratch (rtx x, enum rtx_code subroutine_type) |
ede7cd44 RH |
67 | { |
68 | if (subroutine_type == DEFINE_PEEPHOLE2) | |
69 | { | |
70 | printf ("operand%d", XINT (x, 0)); | |
71 | } | |
72 | else | |
73 | { | |
74 | printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x))); | |
75 | } | |
76 | } | |
77 | ||
65963943 RK |
78 | /* Print a C expression to construct an RTX just like X, |
79 | substituting any operand references appearing within. */ | |
80 | ||
81 | static void | |
14196e02 | 82 | gen_exp (rtx x, enum rtx_code subroutine_type, char *used, md_rtx_info *info) |
65963943 | 83 | { |
b3694847 SS |
84 | RTX_CODE code; |
85 | int i; | |
86 | int len; | |
87 | const char *fmt; | |
f7df4a84 | 88 | const char *sep = ""; |
65963943 RK |
89 | |
90 | if (x == 0) | |
91 | { | |
3d678dca | 92 | printf ("NULL_RTX"); |
65963943 RK |
93 | return; |
94 | } | |
95 | ||
96 | code = GET_CODE (x); | |
97 | ||
98 | switch (code) | |
99 | { | |
100 | case MATCH_OPERAND: | |
101 | case MATCH_DUP: | |
ab55f58c RH |
102 | if (used) |
103 | { | |
104 | if (used[XINT (x, 0)]) | |
105 | { | |
106 | printf ("copy_rtx (operand%d)", XINT (x, 0)); | |
107 | return; | |
108 | } | |
109 | used[XINT (x, 0)] = 1; | |
110 | } | |
65963943 RK |
111 | printf ("operand%d", XINT (x, 0)); |
112 | return; | |
113 | ||
114 | case MATCH_OP_DUP: | |
600555f5 KH |
115 | printf ("gen_rtx_fmt_"); |
116 | for (i = 0; i < XVECLEN (x, 1); i++) | |
117 | printf ("e"); | |
118 | printf (" (GET_CODE (operand%d), ", XINT (x, 0)); | |
4ceb7595 JC |
119 | if (GET_MODE (x) == VOIDmode) |
120 | printf ("GET_MODE (operand%d)", XINT (x, 0)); | |
121 | else | |
122 | printf ("%smode", GET_MODE_NAME (GET_MODE (x))); | |
65963943 RK |
123 | for (i = 0; i < XVECLEN (x, 1); i++) |
124 | { | |
125 | printf (",\n\t\t"); | |
14196e02 | 126 | gen_exp (XVECEXP (x, 1, i), subroutine_type, used, info); |
65963943 RK |
127 | } |
128 | printf (")"); | |
129 | return; | |
130 | ||
131 | case MATCH_OPERATOR: | |
600555f5 KH |
132 | printf ("gen_rtx_fmt_"); |
133 | for (i = 0; i < XVECLEN (x, 2); i++) | |
134 | printf ("e"); | |
135 | printf (" (GET_CODE (operand%d)", XINT (x, 0)); | |
65963943 RK |
136 | printf (", %smode", GET_MODE_NAME (GET_MODE (x))); |
137 | for (i = 0; i < XVECLEN (x, 2); i++) | |
138 | { | |
139 | printf (",\n\t\t"); | |
14196e02 | 140 | gen_exp (XVECEXP (x, 2, i), subroutine_type, used, info); |
65963943 RK |
141 | } |
142 | printf (")"); | |
143 | return; | |
144 | ||
145 | case MATCH_PARALLEL: | |
8fabbfe6 | 146 | case MATCH_PAR_DUP: |
65963943 RK |
147 | printf ("operand%d", XINT (x, 0)); |
148 | return; | |
149 | ||
150 | case MATCH_SCRATCH: | |
ede7cd44 | 151 | gen_rtx_scratch (x, subroutine_type); |
65963943 RK |
152 | return; |
153 | ||
65963943 RK |
154 | case PC: |
155 | printf ("pc_rtx"); | |
156 | return; | |
3810076b BS |
157 | case RETURN: |
158 | printf ("ret_rtx"); | |
159 | return; | |
26898771 BS |
160 | case SIMPLE_RETURN: |
161 | printf ("simple_return_rtx"); | |
162 | return; | |
3e89ed8d JH |
163 | case CLOBBER: |
164 | if (REG_P (XEXP (x, 0))) | |
165 | { | |
14196e02 AH |
166 | printf ("gen_hard_reg_clobber (%smode, %i)", |
167 | GET_MODE_NAME (GET_MODE (XEXP (x, 0))), | |
168 | REGNO (XEXP (x, 0))); | |
3e89ed8d JH |
169 | return; |
170 | } | |
171 | break; | |
65963943 RK |
172 | |
173 | case CONST_INT: | |
174 | if (INTVAL (x) == 0) | |
3d678dca RS |
175 | printf ("const0_rtx"); |
176 | else if (INTVAL (x) == 1) | |
177 | printf ("const1_rtx"); | |
178 | else if (INTVAL (x) == -1) | |
179 | printf ("constm1_rtx"); | |
11b635fe | 180 | else if (-MAX_SAVED_CONST_INT <= INTVAL (x) |
3e3b8d63 | 181 | && INTVAL (x) <= MAX_SAVED_CONST_INT) |
11b635fe KH |
182 | printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]", |
183 | (int) INTVAL (x)); | |
12651878 | 184 | else if (INTVAL (x) == STORE_FLAG_VALUE) |
3d678dca RS |
185 | printf ("const_true_rtx"); |
186 | else | |
76d31c63 JL |
187 | { |
188 | printf ("GEN_INT ("); | |
bb9b3805 | 189 | printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x)); |
76d31c63 JL |
190 | printf (")"); |
191 | } | |
3d678dca RS |
192 | return; |
193 | ||
194 | case CONST_DOUBLE: | |
859be2e4 MR |
195 | /* Handle `const_double_zero' rtx. */ |
196 | if (CONST_DOUBLE_REAL_VALUE (x)->cl == rvc_zero) | |
197 | { | |
198 | printf ("CONST_DOUBLE_ATOF (\"0\", %smode)", | |
199 | GET_MODE_NAME (GET_MODE (x))); | |
200 | return; | |
201 | } | |
202 | /* Fall through. */ | |
091a3ac7 | 203 | case CONST_FIXED: |
807e902e | 204 | case CONST_WIDE_INT: |
3d678dca | 205 | /* These shouldn't be written in MD files. Instead, the appropriate |
e53b6e56 | 206 | routines in varasm.cc should be called. */ |
b2d59f6f | 207 | gcc_unreachable (); |
1d300e19 KG |
208 | |
209 | default: | |
210 | break; | |
65963943 RK |
211 | } |
212 | ||
3b80f6ca | 213 | printf ("gen_rtx_"); |
65963943 | 214 | print_code (code); |
f7df4a84 RS |
215 | printf (" ("); |
216 | if (!always_void_p (code)) | |
217 | { | |
218 | printf ("%smode", GET_MODE_NAME (GET_MODE (x))); | |
219 | sep = ",\n\t"; | |
220 | } | |
65963943 RK |
221 | |
222 | fmt = GET_RTX_FORMAT (code); | |
223 | len = GET_RTX_LENGTH (code); | |
224 | for (i = 0; i < len; i++) | |
225 | { | |
226 | if (fmt[i] == '0') | |
227 | break; | |
f7df4a84 | 228 | fputs (sep, stdout); |
b2d59f6f | 229 | switch (fmt[i]) |
65963943 | 230 | { |
b2d59f6f | 231 | case 'e': case 'u': |
14196e02 | 232 | gen_exp (XEXP (x, i), subroutine_type, used, info); |
b2d59f6f NS |
233 | break; |
234 | ||
235 | case 'i': | |
236 | printf ("%u", XINT (x, i)); | |
237 | break; | |
238 | ||
9fccb335 RS |
239 | case 'r': |
240 | printf ("%u", REGNO (x)); | |
241 | break; | |
242 | ||
91914e56 RS |
243 | case 'p': |
244 | /* We don't have a way of parsing polynomial offsets yet, | |
245 | and hopefully never will. */ | |
246 | printf ("%d", SUBREG_BYTE (x).to_constant ()); | |
247 | break; | |
248 | ||
b2d59f6f NS |
249 | case 's': |
250 | printf ("\"%s\"", XSTR (x, i)); | |
251 | break; | |
252 | ||
253 | case 'E': | |
254 | { | |
255 | int j; | |
256 | printf ("gen_rtvec (%d", XVECLEN (x, i)); | |
257 | for (j = 0; j < XVECLEN (x, i); j++) | |
258 | { | |
259 | printf (",\n\t\t"); | |
14196e02 | 260 | gen_exp (XVECEXP (x, i, j), subroutine_type, used, info); |
b2d59f6f NS |
261 | } |
262 | printf (")"); | |
263 | break; | |
264 | } | |
265 | ||
266 | default: | |
267 | gcc_unreachable (); | |
65963943 | 268 | } |
f7df4a84 | 269 | sep = ",\n\t"; |
65963943 RK |
270 | } |
271 | printf (")"); | |
3d7aafde | 272 | } |
3beaff21 RS |
273 | |
274 | /* Output code to emit the instruction patterns in VEC, with each element | |
275 | becoming a separate instruction. USED is as for gen_exp. */ | |
276 | ||
277 | static void | |
14196e02 | 278 | gen_emit_seq (rtvec vec, char *used, md_rtx_info *info) |
3beaff21 RS |
279 | { |
280 | for (int i = 0, len = GET_NUM_ELEM (vec); i < len; ++i) | |
281 | { | |
9d8895c9 | 282 | bool last_p = (i == len - 1); |
3beaff21 | 283 | rtx next = RTVEC_ELT (vec, i); |
9d8895c9 RS |
284 | if (const char *name = get_emit_function (next)) |
285 | { | |
286 | printf (" %s (", name); | |
14196e02 | 287 | gen_exp (next, DEFINE_EXPAND, used, info); |
9d8895c9 RS |
288 | printf (");\n"); |
289 | if (!last_p && needs_barrier_p (next)) | |
290 | printf (" emit_barrier ();"); | |
291 | } | |
292 | else | |
293 | { | |
294 | printf (" emit ("); | |
14196e02 | 295 | gen_exp (next, DEFINE_EXPAND, used, info); |
9d8895c9 RS |
296 | printf (", %s);\n", last_p ? "false" : "true"); |
297 | } | |
3beaff21 RS |
298 | } |
299 | } | |
65963943 | 300 | \f |
886456e2 RS |
301 | /* Emit the given C code to the output file. The code is allowed to |
302 | fail if CAN_FAIL_P. NAME describes what we're generating, | |
303 | for use in error messages. */ | |
304 | ||
305 | static void | |
306 | emit_c_code (const char *code, bool can_fail_p, const char *name) | |
307 | { | |
308 | if (can_fail_p) | |
309 | printf ("#define FAIL return (end_sequence (), _val)\n"); | |
310 | else | |
311 | printf ("#define FAIL _Pragma (\"GCC error \\\"%s cannot FAIL\\\"\")" | |
312 | " (void)0\n", name); | |
f8cb8bcd | 313 | printf ("#define DONE return (_val = get_insns (), " |
886456e2 RS |
314 | "end_sequence (), _val)\n"); |
315 | ||
b78027d1 | 316 | rtx_reader_ptr->print_md_ptr_loc (code); |
886456e2 RS |
317 | printf ("%s\n", code); |
318 | ||
319 | printf ("#undef DONE\n"); | |
320 | printf ("#undef FAIL\n"); | |
321 | } | |
322 | \f | |
65963943 RK |
323 | /* Generate the `gen_...' function for a DEFINE_INSN. */ |
324 | ||
325 | static void | |
5d2d3e43 | 326 | gen_insn (md_rtx_info *info) |
65963943 | 327 | { |
e792559a | 328 | struct pattern_stats stats; |
b3694847 | 329 | int i; |
65963943 RK |
330 | |
331 | /* See if the pattern for this insn ends with a group of CLOBBERs of (hard) | |
332 | registers or MATCH_SCRATCHes. If so, store away the information for | |
0f41302f | 333 | later. */ |
65963943 | 334 | |
5d2d3e43 | 335 | rtx insn = info->def; |
65963943 RK |
336 | if (XVEC (insn, 1)) |
337 | { | |
751aa7cc RK |
338 | int has_hard_reg = 0; |
339 | ||
65963943 | 340 | for (i = XVECLEN (insn, 1) - 1; i > 0; i--) |
751aa7cc | 341 | { |
17d184e5 | 342 | if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER) |
751aa7cc RK |
343 | break; |
344 | ||
f8cfc6aa | 345 | if (REG_P (XEXP (XVECEXP (insn, 1, i), 0))) |
751aa7cc RK |
346 | has_hard_reg = 1; |
347 | else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH) | |
348 | break; | |
349 | } | |
65963943 RK |
350 | |
351 | if (i != XVECLEN (insn, 1) - 1) | |
352 | { | |
b3694847 | 353 | struct clobber_pat *p; |
5ed6ace5 | 354 | struct clobber_ent *link = XNEW (struct clobber_ent); |
b3694847 | 355 | int j; |
e5f6a288 | 356 | |
5d2d3e43 | 357 | link->code_number = info->index; |
e5f6a288 RK |
358 | |
359 | /* See if any previous CLOBBER_LIST entry is the same as this | |
360 | one. */ | |
361 | ||
362 | for (p = clobber_list; p; p = p->next) | |
363 | { | |
364 | if (p->first_clobber != i + 1 | |
365 | || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1)) | |
366 | continue; | |
367 | ||
368 | for (j = i + 1; j < XVECLEN (insn, 1); j++) | |
369 | { | |
8ad97cfc KG |
370 | rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0); |
371 | rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0); | |
e5f6a288 | 372 | |
8ad97cfc | 373 | /* OLD and NEW_INSN are the same if both are to be a SCRATCH |
3d7aafde | 374 | of the same mode, |
e5f6a288 | 375 | or if both are registers of the same mode and number. */ |
14196e02 AH |
376 | if (! (GET_CODE (old_rtx) == GET_CODE (new_rtx) |
377 | && GET_MODE (old_rtx) == GET_MODE (new_rtx) | |
8ad97cfc KG |
378 | && ((GET_CODE (old_rtx) == MATCH_SCRATCH |
379 | && GET_CODE (new_rtx) == MATCH_SCRATCH) | |
380 | || (REG_P (old_rtx) && REG_P (new_rtx) | |
381 | && REGNO (old_rtx) == REGNO (new_rtx))))) | |
e5f6a288 RK |
382 | break; |
383 | } | |
3d7aafde | 384 | |
e5f6a288 RK |
385 | if (j == XVECLEN (insn, 1)) |
386 | break; | |
387 | } | |
388 | ||
389 | if (p == 0) | |
390 | { | |
5ed6ace5 | 391 | p = XNEW (struct clobber_pat); |
3d7aafde | 392 | |
e5f6a288 RK |
393 | p->insns = 0; |
394 | p->pattern = insn; | |
395 | p->first_clobber = i + 1; | |
396 | p->next = clobber_list; | |
751aa7cc | 397 | p->has_hard_reg = has_hard_reg; |
e5f6a288 RK |
398 | clobber_list = p; |
399 | } | |
400 | ||
401 | link->next = p->insns; | |
402 | p->insns = link; | |
65963943 RK |
403 | } |
404 | } | |
405 | ||
6b6ca844 RK |
406 | /* Don't mention instructions whose names are the null string |
407 | or begin with '*'. They are in the machine description just | |
408 | to be recognized. */ | |
409 | if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*') | |
65963943 RK |
410 | return; |
411 | ||
5d2d3e43 | 412 | printf ("/* %s:%d */\n", info->loc.filename, info->loc.lineno); |
821e35ba | 413 | |
85a45cbb | 414 | /* Find out how many operands this function has. */ |
e792559a RS |
415 | get_pattern_stats (&stats, XVEC (insn, 1)); |
416 | if (stats.max_dup_opno > stats.max_opno) | |
5d2d3e43 | 417 | fatal_at (info->loc, "match_dup operand number has no match_operand"); |
65963943 RK |
418 | |
419 | /* Output the function name and argument declarations. */ | |
420 | printf ("rtx\ngen_%s (", XSTR (insn, 0)); | |
e792559a RS |
421 | if (stats.num_generator_args) |
422 | for (i = 0; i < stats.num_generator_args; i++) | |
6906ba40 KG |
423 | if (i) |
424 | printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i); | |
425 | else | |
426 | printf ("rtx operand%d ATTRIBUTE_UNUSED", i); | |
427 | else | |
428 | printf ("void"); | |
65963943 | 429 | printf (")\n"); |
65963943 RK |
430 | printf ("{\n"); |
431 | ||
6614fd40 | 432 | /* Output code to construct and return the rtl for the instruction body. */ |
65963943 | 433 | |
313d38e3 RS |
434 | rtx pattern = add_implicit_parallel (XVEC (insn, 1)); |
435 | /* ??? This is the traditional behavior, but seems suspect. */ | |
436 | char *used = (XVECLEN (insn, 1) == 1 | |
437 | ? NULL | |
438 | : XCNEWVEC (char, stats.num_generator_args)); | |
439 | printf (" return "); | |
14196e02 | 440 | gen_exp (pattern, DEFINE_INSN, used, info); |
313d38e3 RS |
441 | printf (";\n}\n\n"); |
442 | XDELETEVEC (used); | |
65963943 RK |
443 | } |
444 | \f | |
445 | /* Generate the `gen_...' function for a DEFINE_EXPAND. */ | |
446 | ||
447 | static void | |
5d2d3e43 | 448 | gen_expand (md_rtx_info *info) |
65963943 | 449 | { |
e792559a | 450 | struct pattern_stats stats; |
b3694847 | 451 | int i; |
329b3cc3 | 452 | char *used; |
65963943 | 453 | |
5d2d3e43 | 454 | rtx expand = info->def; |
65963943 | 455 | if (strlen (XSTR (expand, 0)) == 0) |
5d2d3e43 | 456 | fatal_at (info->loc, "define_expand lacks a name"); |
65963943 | 457 | if (XVEC (expand, 1) == 0) |
5d2d3e43 RS |
458 | fatal_at (info->loc, "define_expand for %s lacks a pattern", |
459 | XSTR (expand, 0)); | |
65963943 | 460 | |
85a45cbb | 461 | /* Find out how many operands this function has. */ |
e792559a | 462 | get_pattern_stats (&stats, XVEC (expand, 1)); |
d5a216fa JJ |
463 | if (stats.min_scratch_opno != -1 |
464 | && stats.min_scratch_opno <= MAX (stats.max_opno, stats.max_dup_opno)) | |
465 | fatal_at (info->loc, "define_expand for %s needs to have match_scratch " | |
466 | "numbers above all other operands", XSTR (expand, 0)); | |
65963943 RK |
467 | |
468 | /* Output the function name and argument declarations. */ | |
469 | printf ("rtx\ngen_%s (", XSTR (expand, 0)); | |
e792559a RS |
470 | if (stats.num_generator_args) |
471 | for (i = 0; i < stats.num_generator_args; i++) | |
6906ba40 KG |
472 | if (i) |
473 | printf (",\n\trtx operand%d", i); | |
474 | else | |
475 | printf ("rtx operand%d", i); | |
476 | else | |
477 | printf ("void"); | |
65963943 | 478 | printf (")\n"); |
65963943 RK |
479 | printf ("{\n"); |
480 | ||
481 | /* If we don't have any C code to write, only one insn is being written, | |
482 | and no MATCH_DUPs are present, we can just return the desired insn | |
483 | like we do for a DEFINE_INSN. This saves memory. */ | |
484 | if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0') | |
e792559a | 485 | && stats.max_opno >= stats.max_dup_opno |
65963943 RK |
486 | && XVECLEN (expand, 1) == 1) |
487 | { | |
488 | printf (" return "); | |
14196e02 | 489 | gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL, info); |
65963943 RK |
490 | printf (";\n}\n\n"); |
491 | return; | |
492 | } | |
493 | ||
494 | /* For each operand referred to only with MATCH_DUPs, | |
495 | make a local variable. */ | |
e792559a | 496 | for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++) |
65963943 | 497 | printf (" rtx operand%d;\n", i); |
e67d1102 | 498 | printf (" rtx_insn *_val = 0;\n"); |
65963943 RK |
499 | printf (" start_sequence ();\n"); |
500 | ||
501 | /* The fourth operand of DEFINE_EXPAND is some code to be executed | |
502 | before the actual construction. | |
503 | This code expects to refer to `operands' | |
504 | just as the output-code in a DEFINE_INSN does, | |
505 | but here `operands' is an automatic array. | |
506 | So copy the operand values there before executing it. */ | |
507 | if (XSTR (expand, 3) && *XSTR (expand, 3)) | |
508 | { | |
e2bef702 | 509 | printf (" {\n"); |
e792559a RS |
510 | if (stats.num_operand_vars > 0) |
511 | printf (" rtx operands[%d];\n", stats.num_operand_vars); | |
512 | ||
65963943 | 513 | /* Output code to copy the arguments into `operands'. */ |
e792559a | 514 | for (i = 0; i < stats.num_generator_args; i++) |
e2bef702 | 515 | printf (" operands[%d] = operand%d;\n", i, i); |
65963943 RK |
516 | |
517 | /* Output the special code to be executed before the sequence | |
518 | is generated. */ | |
886456e2 RS |
519 | optab_pattern p; |
520 | bool can_fail_p = true; | |
521 | if (find_optab (&p, XSTR (expand, 0))) | |
522 | { | |
523 | gcc_assert (p.op < NUM_OPTABS); | |
524 | if (nofail_optabs[p.op]) | |
525 | can_fail_p = false; | |
526 | } | |
527 | emit_c_code (XSTR (expand, 3), can_fail_p, XSTR (expand, 0)); | |
65963943 RK |
528 | |
529 | /* Output code to copy the arguments back out of `operands' | |
530 | (unless we aren't going to use them at all). */ | |
531 | if (XVEC (expand, 1) != 0) | |
532 | { | |
d5a216fa | 533 | for (i = 0; i <= MAX (stats.max_opno, stats.max_dup_opno); i++) |
60d3aec4 JJ |
534 | { |
535 | printf (" operand%d = operands[%d];\n", i, i); | |
536 | printf (" (void) operand%d;\n", i); | |
537 | } | |
65963943 | 538 | } |
e2bef702 | 539 | printf (" }\n"); |
65963943 RK |
540 | } |
541 | ||
e792559a | 542 | used = XCNEWVEC (char, stats.num_operand_vars); |
14196e02 | 543 | gen_emit_seq (XVEC (expand, 1), used, info); |
329b3cc3 KK |
544 | XDELETEVEC (used); |
545 | ||
2f937369 | 546 | /* Call `get_insns' to extract the list of all the |
65963943 RK |
547 | insns emitted within this gen_... function. */ |
548 | ||
2f937369 | 549 | printf (" _val = get_insns ();\n"); |
65963943 RK |
550 | printf (" end_sequence ();\n"); |
551 | printf (" return _val;\n}\n\n"); | |
552 | } | |
553 | ||
2f937369 | 554 | /* Like gen_expand, but generates insns resulting from splitting SPLIT. */ |
0f41302f | 555 | |
65963943 | 556 | static void |
5d2d3e43 | 557 | gen_split (md_rtx_info *info) |
65963943 | 558 | { |
e792559a | 559 | struct pattern_stats stats; |
b3694847 | 560 | int i; |
5d2d3e43 | 561 | rtx split = info->def; |
27c38fbe KG |
562 | const char *const name = |
563 | ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split"); | |
751aa7cc | 564 | const char *unused; |
ab55f58c | 565 | char *used; |
ede7cd44 | 566 | |
65963943 | 567 | if (XVEC (split, 0) == 0) |
5d2d3e43 RS |
568 | fatal_at (info->loc, "%s lacks a pattern", |
569 | GET_RTX_NAME (GET_CODE (split))); | |
65963943 | 570 | else if (XVEC (split, 2) == 0) |
5d2d3e43 RS |
571 | fatal_at (info->loc, "%s lacks a replacement pattern", |
572 | GET_RTX_NAME (GET_CODE (split))); | |
65963943 RK |
573 | |
574 | /* Find out how many operands this function has. */ | |
575 | ||
e792559a RS |
576 | get_pattern_stats (&stats, XVEC (split, 2)); |
577 | unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : ""); | |
578 | used = XCNEWVEC (char, stats.num_operand_vars); | |
65963943 | 579 | |
ede7cd44 RH |
580 | /* Output the prototype, function name and argument declarations. */ |
581 | if (GET_CODE (split) == DEFINE_PEEPHOLE2) | |
582 | { | |
bb5c4956 | 583 | printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n", |
5d2d3e43 RS |
584 | name, info->index); |
585 | printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED," | |
586 | " rtx *operands%s)\n", | |
587 | name, info->index, unused); | |
ede7cd44 RH |
588 | } |
589 | else | |
590 | { | |
bb5c4956 | 591 | printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n", |
5d2d3e43 | 592 | info->index); |
bb5c4956 MM |
593 | printf ("rtx_insn *\ngen_split_%d " |
594 | "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n", | |
5d2d3e43 | 595 | info->index, unused); |
ede7cd44 | 596 | } |
65963943 RK |
597 | printf ("{\n"); |
598 | ||
599 | /* Declare all local variables. */ | |
e792559a | 600 | for (i = 0; i < stats.num_operand_vars; i++) |
65963943 | 601 | printf (" rtx operand%d;\n", i); |
bb5c4956 | 602 | printf (" rtx_insn *_val = NULL;\n"); |
ede7cd44 RH |
603 | |
604 | if (GET_CODE (split) == DEFINE_PEEPHOLE2) | |
605 | output_peephole2_scratches (split); | |
606 | ||
9976b31c SB |
607 | const char *fn = info->loc.filename; |
608 | for (const char *p = fn; *p; p++) | |
609 | if (*p == '/') | |
610 | fn = p + 1; | |
611 | ||
eddcad13 | 612 | printf (" if (dump_file)\n"); |
9976b31c SB |
613 | printf (" fprintf (dump_file, \"Splitting with gen_%s_%d (%s:%d)\\n\");\n", |
614 | name, info->index, fn, info->loc.lineno); | |
eddcad13 | 615 | |
65963943 RK |
616 | printf (" start_sequence ();\n"); |
617 | ||
618 | /* The fourth operand of DEFINE_SPLIT is some code to be executed | |
619 | before the actual construction. */ | |
620 | ||
621 | if (XSTR (split, 3)) | |
886456e2 | 622 | emit_c_code (XSTR (split, 3), true, name); |
65963943 RK |
623 | |
624 | /* Output code to copy the arguments back out of `operands' */ | |
e792559a | 625 | for (i = 0; i < stats.num_operand_vars; i++) |
60d3aec4 JJ |
626 | { |
627 | printf (" operand%d = operands[%d];\n", i, i); | |
628 | printf (" (void) operand%d;\n", i); | |
629 | } | |
65963943 | 630 | |
14196e02 | 631 | gen_emit_seq (XVEC (split, 2), used, info); |
65963943 | 632 | |
2f937369 | 633 | /* Call `get_insns' to make a list of all the |
65963943 RK |
634 | insns emitted within this gen_... function. */ |
635 | ||
2f937369 | 636 | printf (" _val = get_insns ();\n"); |
65963943 RK |
637 | printf (" end_sequence ();\n"); |
638 | printf (" return _val;\n}\n\n"); | |
ab55f58c RH |
639 | |
640 | free (used); | |
65963943 RK |
641 | } |
642 | \f | |
643 | /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient | |
644 | size for the insn and an INSN_CODE, and inserts the required CLOBBERs at | |
645 | the end of the vector. */ | |
646 | ||
647 | static void | |
14196e02 | 648 | output_add_clobbers (md_rtx_info *info) |
65963943 RK |
649 | { |
650 | struct clobber_pat *clobber; | |
e5f6a288 | 651 | struct clobber_ent *ent; |
65963943 RK |
652 | int i; |
653 | ||
6906ba40 | 654 | printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n"); |
65963943 | 655 | printf ("{\n"); |
65963943 RK |
656 | printf (" switch (insn_code_number)\n"); |
657 | printf (" {\n"); | |
658 | ||
659 | for (clobber = clobber_list; clobber; clobber = clobber->next) | |
660 | { | |
e5f6a288 RK |
661 | for (ent = clobber->insns; ent; ent = ent->next) |
662 | printf (" case %d:\n", ent->code_number); | |
65963943 RK |
663 | |
664 | for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++) | |
665 | { | |
666 | printf (" XVECEXP (pattern, 0, %d) = ", i); | |
ede7cd44 | 667 | gen_exp (XVECEXP (clobber->pattern, 1, i), |
14196e02 | 668 | GET_CODE (clobber->pattern), NULL, info); |
65963943 RK |
669 | printf (";\n"); |
670 | } | |
671 | ||
e5f6a288 | 672 | printf (" break;\n\n"); |
65963943 RK |
673 | } |
674 | ||
675 | printf (" default:\n"); | |
b2d59f6f | 676 | printf (" gcc_unreachable ();\n"); |
65963943 RK |
677 | printf (" }\n"); |
678 | printf ("}\n"); | |
679 | } | |
680 | \f | |
6327d36a RK |
681 | /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code |
682 | number that will have clobbers added (as indicated by `recog') and returns | |
683 | 1 if those include a clobber of a hard reg or 0 if all of them just clobber | |
684 | SCRATCH. */ | |
751aa7cc RK |
685 | |
686 | static void | |
3d7aafde | 687 | output_added_clobbers_hard_reg_p (void) |
751aa7cc RK |
688 | { |
689 | struct clobber_pat *clobber; | |
690 | struct clobber_ent *ent; | |
a29b099d | 691 | int clobber_p, used; |
751aa7cc | 692 | |
6906ba40 | 693 | printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n"); |
751aa7cc RK |
694 | printf ("{\n"); |
695 | printf (" switch (insn_code_number)\n"); | |
696 | printf (" {\n"); | |
697 | ||
698 | for (clobber_p = 0; clobber_p <= 1; clobber_p++) | |
699 | { | |
a29b099d | 700 | used = 0; |
751aa7cc RK |
701 | for (clobber = clobber_list; clobber; clobber = clobber->next) |
702 | if (clobber->has_hard_reg == clobber_p) | |
703 | for (ent = clobber->insns; ent; ent = ent->next) | |
a29b099d JJ |
704 | { |
705 | printf (" case %d:\n", ent->code_number); | |
706 | used++; | |
707 | } | |
751aa7cc | 708 | |
a29b099d JJ |
709 | if (used) |
710 | printf (" return %d;\n\n", clobber_p); | |
751aa7cc RK |
711 | } |
712 | ||
713 | printf (" default:\n"); | |
b2d59f6f | 714 | printf (" gcc_unreachable ();\n"); |
751aa7cc RK |
715 | printf (" }\n"); |
716 | printf ("}\n"); | |
717 | } | |
718 | \f | |
ede7cd44 | 719 | /* Generate code to invoke find_free_register () as needed for the |
dc297297 | 720 | scratch registers used by the peephole2 pattern in SPLIT. */ |
ede7cd44 RH |
721 | |
722 | static void | |
3d7aafde | 723 | output_peephole2_scratches (rtx split) |
ede7cd44 RH |
724 | { |
725 | int i; | |
726 | int insn_nr = 0; | |
f1ed99cd | 727 | bool first = true; |
ede7cd44 RH |
728 | |
729 | for (i = 0; i < XVECLEN (split, 0); i++) | |
730 | { | |
731 | rtx elt = XVECEXP (split, 0, i); | |
732 | if (GET_CODE (elt) == MATCH_SCRATCH) | |
733 | { | |
734 | int last_insn_nr = insn_nr; | |
735 | int cur_insn_nr = insn_nr; | |
736 | int j; | |
737 | for (j = i + 1; j < XVECLEN (split, 0); j++) | |
738 | if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP) | |
739 | { | |
740 | if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0)) | |
741 | last_insn_nr = cur_insn_nr; | |
742 | } | |
743 | else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH) | |
744 | cur_insn_nr++; | |
23280139 | 745 | |
f1ed99cd JJ |
746 | if (first) |
747 | { | |
748 | printf (" HARD_REG_SET _regs_allocated;\n"); | |
749 | printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n"); | |
750 | first = false; | |
751 | } | |
752 | ||
23280139 | 753 | printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\ |
3d7aafde | 754 | return NULL;\n", |
ede7cd44 | 755 | XINT (elt, 0), |
23280139 | 756 | insn_nr, last_insn_nr, |
ede7cd44 RH |
757 | XSTR (elt, 1), |
758 | GET_MODE_NAME (GET_MODE (elt))); | |
759 | ||
760 | } | |
761 | else if (GET_CODE (elt) != MATCH_DUP) | |
762 | insn_nr++; | |
763 | } | |
764 | } | |
65963943 | 765 | |
0016d8d9 RS |
766 | /* Print "arg<N>" parameter declarations for each argument N of ONAME. */ |
767 | ||
768 | static void | |
769 | print_overload_arguments (overloaded_name *oname) | |
770 | { | |
771 | for (unsigned int i = 0; i < oname->arg_types.length (); ++i) | |
772 | printf ("%s%s arg%d", i == 0 ? "" : ", ", oname->arg_types[i], i); | |
773 | } | |
774 | ||
ae5bdb7f | 775 | /* Print code to test whether INSTANCE should be chosen, given that |
0016d8d9 RS |
776 | argument N of the overload is available as "arg<N>". */ |
777 | ||
778 | static void | |
779 | print_overload_test (overloaded_instance *instance) | |
780 | { | |
781 | for (unsigned int i = 0; i < instance->arg_values.length (); ++i) | |
782 | printf ("%sarg%d == %s", i == 0 ? " if (" : "\n && ", | |
783 | i, instance->arg_values[i]); | |
784 | printf (")\n"); | |
785 | } | |
786 | ||
787 | /* Emit a maybe_code_for_* function for ONAME. */ | |
788 | ||
789 | static void | |
790 | handle_overloaded_code_for (overloaded_name *oname) | |
791 | { | |
792 | /* Print the function prototype. */ | |
793 | printf ("\ninsn_code\nmaybe_code_for_%s (", oname->name); | |
794 | print_overload_arguments (oname); | |
795 | printf (")\n{\n"); | |
796 | ||
797 | /* Use a sequence of "if" statements for each instance. */ | |
798 | for (overloaded_instance *instance = oname->first_instance; | |
799 | instance; instance = instance->next) | |
800 | { | |
801 | print_overload_test (instance); | |
802 | printf (" return CODE_FOR_%s;\n", instance->name); | |
803 | } | |
804 | ||
805 | /* Return null if no match was found. */ | |
806 | printf (" return CODE_FOR_nothing;\n}\n"); | |
807 | } | |
808 | ||
809 | /* Emit a maybe_gen_* function for ONAME. */ | |
810 | ||
811 | static void | |
812 | handle_overloaded_gen (overloaded_name *oname) | |
813 | { | |
d281492d | 814 | unsigned HOST_WIDE_INT seen = 0; |
0016d8d9 | 815 | /* All patterns must have the same number of operands. */ |
0016d8d9 RS |
816 | for (overloaded_instance *instance = oname->first_instance->next; |
817 | instance; instance = instance->next) | |
818 | { | |
d281492d RS |
819 | pattern_stats stats; |
820 | get_pattern_stats (&stats, XVEC (instance->insn, 1)); | |
821 | unsigned HOST_WIDE_INT mask | |
822 | = HOST_WIDE_INT_1U << stats.num_generator_args; | |
823 | if (seen & mask) | |
824 | continue; | |
825 | ||
826 | seen |= mask; | |
827 | ||
828 | /* Print the function prototype. */ | |
829 | printf ("\nrtx\nmaybe_gen_%s (", oname->name); | |
830 | print_overload_arguments (oname); | |
831 | for (int i = 0; i < stats.num_generator_args; ++i) | |
832 | printf (", rtx x%d", i); | |
833 | printf (")\n{\n"); | |
834 | ||
835 | /* Use maybe_code_for_*, instead of duplicating the selection | |
836 | logic here. */ | |
837 | printf (" insn_code code = maybe_code_for_%s (", oname->name); | |
838 | for (unsigned int i = 0; i < oname->arg_types.length (); ++i) | |
839 | printf ("%sarg%d", i == 0 ? "" : ", ", i); | |
840 | printf (");\n" | |
841 | " if (code != CODE_FOR_nothing)\n" | |
842 | " {\n" | |
843 | " gcc_assert (insn_data[code].n_generator_args == %d);\n" | |
844 | " return GEN_FCN (code) (", stats.num_generator_args); | |
845 | for (int i = 0; i < stats.num_generator_args; ++i) | |
846 | printf ("%sx%d", i == 0 ? "" : ", ", i); | |
847 | printf (");\n" | |
848 | " }\n" | |
849 | " else\n" | |
850 | " return NULL_RTX;\n" | |
851 | "}\n"); | |
0016d8d9 | 852 | } |
0016d8d9 RS |
853 | } |
854 | ||
65963943 | 855 | int |
66b0fe8f | 856 | main (int argc, const char **argv) |
65963943 | 857 | { |
f8b6598e | 858 | progname = "genemit"; |
65963943 | 859 | |
600ab3fc | 860 | if (!init_rtx_reader_args (argc, argv)) |
c88c0d42 | 861 | return (FATAL_EXIT_CODE); |
65963943 | 862 | |
886456e2 RS |
863 | #define DEF_INTERNAL_OPTAB_FN(NAME, FLAGS, OPTAB, TYPE) \ |
864 | nofail_optabs[OPTAB##_optab] = true; | |
865 | #include "internal-fn.def" | |
866 | ||
65963943 | 867 | /* Assign sequential codes to all entries in the machine description |
e53b6e56 | 868 | in parallel with the tables in insn-output.cc. */ |
65963943 | 869 | |
65963943 RK |
870 | printf ("/* Generated automatically by the program `genemit'\n\ |
871 | from the machine description file `md'. */\n\n"); | |
872 | ||
8fcc61f8 | 873 | printf ("#define IN_TARGET_CODE 1\n"); |
65963943 | 874 | printf ("#include \"config.h\"\n"); |
729da3f5 | 875 | printf ("#include \"system.h\"\n"); |
4977bab6 | 876 | printf ("#include \"coretypes.h\"\n"); |
c7131fb2 | 877 | printf ("#include \"backend.h\"\n"); |
9fdcd34e | 878 | printf ("#include \"predict.h\"\n"); |
4d648807 | 879 | printf ("#include \"tree.h\"\n"); |
c7131fb2 AM |
880 | printf ("#include \"rtl.h\"\n"); |
881 | printf ("#include \"alias.h\"\n"); | |
d8a2d370 DN |
882 | printf ("#include \"varasm.h\"\n"); |
883 | printf ("#include \"stor-layout.h\"\n"); | |
884 | printf ("#include \"calls.h\"\n"); | |
4d0cdd0c | 885 | printf ("#include \"memmodel.h\"\n"); |
6baf1cc8 | 886 | printf ("#include \"tm_p.h\"\n"); |
36566b39 | 887 | printf ("#include \"flags.h\"\n"); |
36566b39 PK |
888 | printf ("#include \"insn-config.h\"\n"); |
889 | printf ("#include \"expmed.h\"\n"); | |
890 | printf ("#include \"dojump.h\"\n"); | |
891 | printf ("#include \"explow.h\"\n"); | |
892 | printf ("#include \"emit-rtl.h\"\n"); | |
893 | printf ("#include \"stmt.h\"\n"); | |
65963943 | 894 | printf ("#include \"expr.h\"\n"); |
b0710fe1 | 895 | printf ("#include \"insn-codes.h\"\n"); |
e78d8e51 | 896 | printf ("#include \"optabs.h\"\n"); |
c413e1b5 | 897 | printf ("#include \"dfp.h\"\n"); |
65963943 | 898 | printf ("#include \"output.h\"\n"); |
23280139 | 899 | printf ("#include \"recog.h\"\n"); |
c7131fb2 | 900 | printf ("#include \"df.h\"\n"); |
ede7cd44 | 901 | printf ("#include \"resource.h\"\n"); |
d80eb1e1 | 902 | printf ("#include \"reload.h\"\n"); |
79a3f089 | 903 | printf ("#include \"diagnostic-core.h\"\n"); |
5abc5de9 | 904 | printf ("#include \"regs.h\"\n"); |
279bb624 | 905 | printf ("#include \"tm-constrs.h\"\n"); |
5abc5de9 | 906 | printf ("#include \"ggc.h\"\n"); |
3b1552c1 | 907 | printf ("#include \"target.h\"\n\n"); |
65963943 RK |
908 | |
909 | /* Read the machine description. */ | |
910 | ||
5d2d3e43 RS |
911 | md_rtx_info info; |
912 | while (read_md_rtx (&info)) | |
913 | switch (GET_CODE (info.def)) | |
914 | { | |
915 | case DEFINE_INSN: | |
916 | gen_insn (&info); | |
c88c0d42 | 917 | break; |
ede7cd44 | 918 | |
5d2d3e43 RS |
919 | case DEFINE_EXPAND: |
920 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
921 | gen_expand (&info); | |
922 | break; | |
821e35ba | 923 | |
5d2d3e43 RS |
924 | case DEFINE_SPLIT: |
925 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
926 | gen_split (&info); | |
927 | break; | |
821e35ba | 928 | |
5d2d3e43 RS |
929 | case DEFINE_PEEPHOLE2: |
930 | printf ("/* %s:%d */\n", info.loc.filename, info.loc.lineno); | |
931 | gen_split (&info); | |
932 | break; | |
821e35ba | 933 | |
5d2d3e43 RS |
934 | default: |
935 | break; | |
936 | } | |
65963943 | 937 | |
751aa7cc RK |
938 | /* Write out the routines to add CLOBBERs to a pattern and say whether they |
939 | clobber a hard reg. */ | |
14196e02 | 940 | output_add_clobbers (&info); |
751aa7cc | 941 | output_added_clobbers_hard_reg_p (); |
65963943 | 942 | |
0016d8d9 RS |
943 | for (overloaded_name *oname = rtx_reader_ptr->get_overloads (); |
944 | oname; oname = oname->next) | |
945 | { | |
946 | handle_overloaded_code_for (oname); | |
947 | handle_overloaded_gen (oname); | |
948 | } | |
949 | ||
65963943 | 950 | fflush (stdout); |
c1b59dce | 951 | return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); |
65963943 | 952 | } |