1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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
8 Software Foundation; either version 3, or (at your option) any later
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "gensupport.h"
30 /* This structure contains all the information needed to describe one
31 set of extractions methods. Each method may be used by more than
32 one pattern if the operands are in the same place.
34 The string for each operand describes that path to the operand and
35 contains `0' through `9' when going into an expression and `a' through
36 `z' when going into a vector. We assume here that only the first operand
37 of an rtl expression is a vector. genrecog.c makes the same assumption
38 (and uses the same representation) and it is currently true. */
44 unsigned int op_count
;
45 unsigned int dup_count
;
49 struct code_ptr
*insns
;
50 struct extraction
*next
;
53 /* Holds a single insn code that uses an extraction method. */
57 struct code_ptr
*next
;
60 /* All extractions needed for this machine description. */
61 static struct extraction
*extractions
;
63 /* All insn codes for old-style peepholes. */
64 static struct code_ptr
*peepholes
;
66 /* This structure is used by gen_insn and walk_rtx to accumulate the
67 data that will be used to produce an extractions structure. */
80 /* Forward declarations. */
81 static void walk_rtx (rtx
, struct accum_extract
*);
84 gen_insn (rtx insn
, int insn_code_number
)
87 unsigned int op_count
, dup_count
, j
;
89 struct code_ptr
*link
;
90 struct accum_extract acc
;
92 acc
.oplocs
.create (10);
93 acc
.duplocs
.create (10);
94 acc
.dupnums
.create (10);
95 acc
.pathstr
.create (20);
97 /* Walk the insn's pattern, remembering at all times the path
98 down to the walking point. */
100 if (XVECLEN (insn
, 1) == 1)
101 walk_rtx (XVECEXP (insn
, 1, 0), &acc
);
103 for (i
= XVECLEN (insn
, 1) - 1; i
>= 0; i
--)
105 acc
.pathstr
.safe_push ('a' + i
);
106 walk_rtx (XVECEXP (insn
, 1, i
), &acc
);
110 link
= XNEW (struct code_ptr
);
111 link
->insn_code
= insn_code_number
;
113 /* See if we find something that already had this extraction method. */
115 op_count
= acc
.oplocs
.length ();
116 dup_count
= acc
.duplocs
.length ();
117 gcc_assert (dup_count
== acc
.dupnums
.length ());
119 for (p
= extractions
; p
; p
= p
->next
)
121 if (p
->op_count
!= op_count
|| p
->dup_count
!= dup_count
)
124 for (j
= 0; j
< op_count
; j
++)
126 char *a
= p
->oplocs
[j
];
127 char *b
= acc
.oplocs
[j
];
128 if (a
!= b
&& (!a
|| !b
|| strcmp (a
, b
)))
135 for (j
= 0; j
< dup_count
; j
++)
136 if (p
->dupnums
[j
] != acc
.dupnums
[j
]
137 || strcmp (p
->duplocs
[j
], acc
.duplocs
[j
]))
143 /* This extraction is the same as ours. Just link us in. */
144 link
->next
= p
->insns
;
149 /* Otherwise, make a new extraction method. We stash the arrays
150 after the extraction structure in memory. */
152 p
= XNEWVAR (struct extraction
, sizeof (struct extraction
)
153 + op_count
*sizeof (char *)
154 + dup_count
*sizeof (char *)
155 + dup_count
*sizeof (int));
156 p
->op_count
= op_count
;
157 p
->dup_count
= dup_count
;
158 p
->next
= extractions
;
163 p
->oplocs
= (char **)((char *)p
+ sizeof (struct extraction
));
164 p
->duplocs
= p
->oplocs
+ op_count
;
165 p
->dupnums
= (int *)(p
->duplocs
+ dup_count
);
167 memcpy (p
->oplocs
, acc
.oplocs
.address (), op_count
* sizeof (locstr
));
168 memcpy (p
->duplocs
, acc
.duplocs
.address (), dup_count
* sizeof (locstr
));
169 memcpy (p
->dupnums
, acc
.dupnums
.address (), dup_count
* sizeof (int));
172 acc
.oplocs
.release ();
173 acc
.duplocs
.release ();
174 acc
.dupnums
.release ();
175 acc
.pathstr
.release ();
178 /* Helper subroutine of walk_rtx: given a vec<locstr>, an index, and a
179 string, insert the string at the index, which should either already
180 exist and be NULL, or not yet exist within the vector. In the latter
181 case the vector is enlarged as appropriate. */
183 VEC_safe_set_locstr (vec
<locstr
> *vp
, unsigned int ix
, char *str
)
185 if (ix
< (*vp
).length ())
189 message_with_line (line_no
, "repeated operand number %d", ix
);
197 while (ix
> (*vp
).length ())
198 vp
->safe_push (NULL
);
203 /* Another helper subroutine of walk_rtx: given a vec<char>, convert it
204 to a NUL-terminated string in malloc memory. */
206 VEC_char_to_string (vec
<char> v
)
208 size_t n
= v
.length ();
209 char *s
= XNEWVEC (char, n
+ 1);
210 memcpy (s
, v
.address (), n
);
216 walk_rtx (rtx x
, struct accum_extract
*acc
)
236 VEC_safe_set_locstr (&acc
->oplocs
, XINT (x
, 0),
237 VEC_char_to_string (acc
->pathstr
));
242 VEC_safe_set_locstr (&acc
->oplocs
, XINT (x
, 0),
243 VEC_char_to_string (acc
->pathstr
));
245 base
= (code
== MATCH_OPERATOR
? '0' : 'a');
246 for (i
= XVECLEN (x
, 2) - 1; i
>= 0; i
--)
248 acc
->pathstr
.safe_push (base
+ i
);
249 walk_rtx (XVECEXP (x
, 2, i
), acc
);
257 acc
->duplocs
.safe_push (VEC_char_to_string (acc
->pathstr
));
258 acc
->dupnums
.safe_push (XINT (x
, 0));
260 if (code
== MATCH_DUP
)
263 base
= (code
== MATCH_OP_DUP
? '0' : 'a');
264 for (i
= XVECLEN (x
, 1) - 1; i
>= 0; i
--)
266 acc
->pathstr
.safe_push (base
+ i
);
267 walk_rtx (XVECEXP (x
, 1, i
), acc
);
276 fmt
= GET_RTX_FORMAT (code
);
277 len
= GET_RTX_LENGTH (code
);
278 for (i
= 0; i
< len
; i
++)
280 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
282 acc
->pathstr
.safe_push ('0' + i
);
283 walk_rtx (XEXP (x
, i
), acc
);
286 else if (fmt
[i
] == 'E')
289 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
291 acc
->pathstr
.safe_push ('a' + j
);
292 walk_rtx (XVECEXP (x
, i
, j
), acc
);
299 /* Given a PATH, representing a path down the instruction's
300 pattern from the root to a certain point, output code to
301 evaluate to the rtx at that point. */
304 print_path (const char *path
)
306 int len
= strlen (path
);
311 /* Don't emit "pat", since we may try to take the address of it,
312 which isn't what is intended. */
313 fputs ("PATTERN (insn)", stdout
);
317 /* We first write out the operations (XEXP or XVECEXP) in reverse
318 order, then write "pat", then the indices in forward order. */
320 for (i
= len
- 1; i
>= 0 ; i
--)
322 if (ISLOWER (path
[i
]))
323 fputs ("XVECEXP (", stdout
);
324 else if (ISDIGIT (path
[i
]))
325 fputs ("XEXP (", stdout
);
330 fputs ("pat", stdout
);
332 for (i
= 0; i
< len
; i
++)
334 if (ISLOWER (path
[i
]))
335 printf (", 0, %d)", path
[i
] - 'a');
336 else if (ISDIGIT (path
[i
]))
337 printf (", %d)", path
[i
] - '0');
346 /* N.B. Code below avoids putting squiggle braces in column 1 inside
347 a string, because this confuses some editors' syntax highlighting
351 /* Generated automatically by the program `genextract'\n\
352 from the machine description file `md'. */\n\
354 #include \"config.h\"\n\
355 #include \"system.h\"\n\
356 #include \"coretypes.h\"\n\
358 #include \"rtl.h\"\n\
359 #include \"insn-config.h\"\n\
360 #include \"recog.h\"\n\
361 #include \"diagnostic-core.h\"\n\
363 /* This variable is used as the \"location\" of any missing operand\n\
364 whose numbers are skipped by a given pattern. */\n\
365 static rtx junk ATTRIBUTE_UNUSED;\n");
369 insn_extract (rtx_insn *insn)\n{\n\
370 rtx *ro = recog_data.operand;\n\
371 rtx **ro_loc = recog_data.operand_loc;\n\
372 rtx pat = PATTERN (insn);\n\
373 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
375 #ifdef ENABLE_CHECKING\n\
376 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
377 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
381 switch (INSN_CODE (insn))\n\
384 /* Control reaches here if insn_extract has been called with an\n\
385 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
386 corresponds to a DEFINE_EXPAND in the machine description;\n\
387 either way, a bug. */\n\
388 if (INSN_CODE (insn) < 0)\n\
389 fatal_insn (\"unrecognizable insn:\", insn);\n\
391 fatal_insn (\"insn with invalid code number:\", insn);\n");
395 main (int argc
, char **argv
)
399 struct extraction
*p
;
400 struct code_ptr
*link
;
402 int insn_code_number
;
404 progname
= "genextract";
406 if (!init_rtx_reader_args (argc
, argv
))
407 return (FATAL_EXIT_CODE
);
409 /* Read the machine description. */
411 while ((desc
= read_md_rtx (&line_no
, &insn_code_number
)) != NULL
)
413 if (GET_CODE (desc
) == DEFINE_INSN
)
414 gen_insn (desc
, insn_code_number
);
416 else if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
418 struct code_ptr
*link
= XNEW (struct code_ptr
);
420 link
->insn_code
= insn_code_number
;
421 link
->next
= peepholes
;
427 return FATAL_EXIT_CODE
;
431 /* Write out code to handle peepholes and the insn_codes that it should
435 for (link
= peepholes
; link
; link
= link
->next
)
436 printf (" case %d:\n", link
->insn_code
);
438 /* The vector in the insn says how many operands it has.
439 And all it contains are operands. In fact, the vector was
440 created just for the sake of this function. We need to set the
441 location of the operands for sake of simplifications after
442 extraction, like eliminating subregs. */
443 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
444 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
448 /* Write out all the ways to extract insn operands. */
449 for (p
= extractions
; p
; p
= p
->next
)
451 for (link
= p
->insns
; link
; link
= link
->next
)
454 name
= get_insn_name (i
);
456 printf (" case %d: /* %s */\n", i
, name
);
458 printf (" case %d:\n", i
);
461 for (i
= 0; i
< p
->op_count
; i
++)
463 if (p
->oplocs
[i
] == 0)
465 printf (" ro[%d] = const0_rtx;\n", i
);
466 printf (" ro_loc[%d] = &junk;\n", i
);
470 printf (" ro[%d] = *(ro_loc[%d] = &", i
, i
);
471 print_path (p
->oplocs
[i
]);
476 for (i
= 0; i
< p
->dup_count
; i
++)
478 printf (" recog_data.dup_loc[%d] = &", i
);
479 print_path (p
->duplocs
[i
]);
481 printf (" recog_data.dup_num[%d] = %d;\n", i
, p
->dupnums
[i
]);
489 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);