1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987, 1991, 1992, 1993, 1997, 1998, 1999, 2000, 2003,
3 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
30 #include "gensupport.h"
33 /* This structure contains all the information needed to describe one
34 set of extractions methods. Each method may be used by more than
35 one pattern if the operands are in the same place.
37 The string for each operand describes that path to the operand and
38 contains `0' through `9' when going into an expression and `a' through
39 `z' when going into a vector. We assume here that only the first operand
40 of an rtl expression is a vector. genrecog.c makes the same assumption
41 (and uses the same representation) and it is currently true. */
47 unsigned int op_count
;
48 unsigned int dup_count
;
52 struct code_ptr
*insns
;
53 struct extraction
*next
;
56 /* Holds a single insn code that uses an extraction method. */
60 struct code_ptr
*next
;
63 /* All extractions needed for this machine description. */
64 static struct extraction
*extractions
;
66 /* All insn codes for old-style peepholes. */
67 static struct code_ptr
*peepholes
;
69 /* This structure is used by gen_insn and walk_rtx to accumulate the
70 data that will be used to produce an extractions structure. */
83 /* Forward declarations. */
84 static void walk_rtx (rtx
, struct accum_extract
*);
87 gen_insn (rtx insn
, int insn_code_number
)
90 unsigned int op_count
, dup_count
, j
;
92 struct code_ptr
*link
;
93 struct accum_extract acc
;
95 acc
.oplocs
.create (10);
96 acc
.duplocs
.create (10);
97 acc
.dupnums
.create (10);
98 acc
.pathstr
.create (20);
100 /* Walk the insn's pattern, remembering at all times the path
101 down to the walking point. */
103 if (XVECLEN (insn
, 1) == 1)
104 walk_rtx (XVECEXP (insn
, 1, 0), &acc
);
106 for (i
= XVECLEN (insn
, 1) - 1; i
>= 0; i
--)
108 acc
.pathstr
.safe_push ('a' + i
);
109 walk_rtx (XVECEXP (insn
, 1, i
), &acc
);
113 link
= XNEW (struct code_ptr
);
114 link
->insn_code
= insn_code_number
;
116 /* See if we find something that already had this extraction method. */
118 op_count
= acc
.oplocs
.length ();
119 dup_count
= acc
.duplocs
.length ();
120 gcc_assert (dup_count
== acc
.dupnums
.length ());
122 for (p
= extractions
; p
; p
= p
->next
)
124 if (p
->op_count
!= op_count
|| p
->dup_count
!= dup_count
)
127 for (j
= 0; j
< op_count
; j
++)
129 char *a
= p
->oplocs
[j
];
130 char *b
= acc
.oplocs
[j
];
131 if (a
!= b
&& (!a
|| !b
|| strcmp (a
, b
)))
138 for (j
= 0; j
< dup_count
; j
++)
139 if (p
->dupnums
[j
] != acc
.dupnums
[j
]
140 || strcmp (p
->duplocs
[j
], acc
.duplocs
[j
]))
146 /* This extraction is the same as ours. Just link us in. */
147 link
->next
= p
->insns
;
152 /* Otherwise, make a new extraction method. We stash the arrays
153 after the extraction structure in memory. */
155 p
= XNEWVAR (struct extraction
, sizeof (struct extraction
)
156 + op_count
*sizeof (char *)
157 + dup_count
*sizeof (char *)
158 + dup_count
*sizeof (int));
159 p
->op_count
= op_count
;
160 p
->dup_count
= dup_count
;
161 p
->next
= extractions
;
166 p
->oplocs
= (char **)((char *)p
+ sizeof (struct extraction
));
167 p
->duplocs
= p
->oplocs
+ op_count
;
168 p
->dupnums
= (int *)(p
->duplocs
+ dup_count
);
170 memcpy(p
->oplocs
, acc
.oplocs
.address(), op_count
*sizeof(locstr
));
171 memcpy(p
->duplocs
, acc
.duplocs
.address(), dup_count
*sizeof(locstr
));
172 memcpy(p
->dupnums
, acc
.dupnums
.address(), dup_count
*sizeof(int));
175 acc
.oplocs
.release ();
176 acc
.duplocs
.release ();
177 acc
.dupnums
.release ();
178 acc
.pathstr
.release ();
181 /* Helper subroutine of walk_rtx: given a vec<locstr>, an index, and a
182 string, insert the string at the index, which should either already
183 exist and be NULL, or not yet exist within the vector. In the latter
184 case the vector is enlarged as appropriate. */
186 VEC_safe_set_locstr (vec
<locstr
> *vp
, unsigned int ix
, char *str
)
188 if (ix
< (*vp
).length ())
192 message_with_line (line_no
, "repeated operand number %d", ix
);
200 while (ix
> (*vp
).length ())
201 vp
->safe_push (NULL
);
206 /* Another helper subroutine of walk_rtx: given a vec<char>, convert it
207 to a NUL-terminated string in malloc memory. */
209 VEC_char_to_string (vec
<char> v
)
211 size_t n
= v
.length ();
212 char *s
= XNEWVEC (char, n
+ 1);
213 memcpy (s
, v
.address (), n
);
219 walk_rtx (rtx x
, struct accum_extract
*acc
)
239 VEC_safe_set_locstr (&acc
->oplocs
, XINT (x
, 0),
240 VEC_char_to_string (acc
->pathstr
));
245 VEC_safe_set_locstr (&acc
->oplocs
, XINT (x
, 0),
246 VEC_char_to_string (acc
->pathstr
));
248 base
= (code
== MATCH_OPERATOR
? '0' : 'a');
249 for (i
= XVECLEN (x
, 2) - 1; i
>= 0; i
--)
251 acc
->pathstr
.safe_push (base
+ i
);
252 walk_rtx (XVECEXP (x
, 2, i
), acc
);
260 acc
->duplocs
.safe_push (VEC_char_to_string (acc
->pathstr
));
261 acc
->dupnums
.safe_push (XINT (x
, 0));
263 if (code
== MATCH_DUP
)
266 base
= (code
== MATCH_OP_DUP
? '0' : 'a');
267 for (i
= XVECLEN (x
, 1) - 1; i
>= 0; i
--)
269 acc
->pathstr
.safe_push (base
+ i
);
270 walk_rtx (XVECEXP (x
, 1, i
), acc
);
279 fmt
= GET_RTX_FORMAT (code
);
280 len
= GET_RTX_LENGTH (code
);
281 for (i
= 0; i
< len
; i
++)
283 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
285 acc
->pathstr
.safe_push ('0' + i
);
286 walk_rtx (XEXP (x
, i
), acc
);
289 else if (fmt
[i
] == 'E')
292 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
294 acc
->pathstr
.safe_push ('a' + j
);
295 walk_rtx (XVECEXP (x
, i
, j
), acc
);
302 /* Given a PATH, representing a path down the instruction's
303 pattern from the root to a certain point, output code to
304 evaluate to the rtx at that point. */
307 print_path (const char *path
)
309 int len
= strlen (path
);
314 /* Don't emit "pat", since we may try to take the address of it,
315 which isn't what is intended. */
316 fputs ("PATTERN (insn)", stdout
);
320 /* We first write out the operations (XEXP or XVECEXP) in reverse
321 order, then write "pat", then the indices in forward order. */
323 for (i
= len
- 1; i
>= 0 ; i
--)
325 if (ISLOWER (path
[i
]))
326 fputs ("XVECEXP (", stdout
);
327 else if (ISDIGIT (path
[i
]))
328 fputs ("XEXP (", stdout
);
333 fputs ("pat", stdout
);
335 for (i
= 0; i
< len
; i
++)
337 if (ISLOWER (path
[i
]))
338 printf (", 0, %d)", path
[i
] - 'a');
339 else if (ISDIGIT(path
[i
]))
340 printf (", %d)", path
[i
] - '0');
349 /* N.B. Code below avoids putting squiggle braces in column 1 inside
350 a string, because this confuses some editors' syntax highlighting
354 /* Generated automatically by the program `genextract'\n\
355 from the machine description file `md'. */\n\
357 #include \"config.h\"\n\
358 #include \"system.h\"\n\
359 #include \"coretypes.h\"\n\
361 #include \"rtl.h\"\n\
362 #include \"insn-config.h\"\n\
363 #include \"recog.h\"\n\
364 #include \"diagnostic-core.h\"\n\
366 /* This variable is used as the \"location\" of any missing operand\n\
367 whose numbers are skipped by a given pattern. */\n\
368 static rtx junk ATTRIBUTE_UNUSED;\n");
372 insn_extract (rtx insn)\n{\n\
373 rtx *ro = recog_data.operand;\n\
374 rtx **ro_loc = recog_data.operand_loc;\n\
375 rtx pat = PATTERN (insn);\n\
376 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
378 #ifdef ENABLE_CHECKING\n\
379 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
380 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
384 switch (INSN_CODE (insn))\n\
387 /* Control reaches here if insn_extract has been called with an\n\
388 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
389 corresponds to a DEFINE_EXPAND in the machine description;\n\
390 either way, a bug. */\n\
391 if (INSN_CODE (insn) < 0)\n\
392 fatal_insn (\"unrecognizable insn:\", insn);\n\
394 fatal_insn (\"insn with invalid code number:\", insn);\n");
398 main (int argc
, char **argv
)
402 struct extraction
*p
;
403 struct code_ptr
*link
;
405 int insn_code_number
;
407 progname
= "genextract";
409 if (!init_rtx_reader_args (argc
, argv
))
410 return (FATAL_EXIT_CODE
);
412 /* Read the machine description. */
414 while ((desc
= read_md_rtx (&line_no
, &insn_code_number
)) != NULL
)
416 if (GET_CODE (desc
) == DEFINE_INSN
)
417 gen_insn (desc
, insn_code_number
);
419 else if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
421 struct code_ptr
*link
= XNEW (struct code_ptr
);
423 link
->insn_code
= insn_code_number
;
424 link
->next
= peepholes
;
430 return FATAL_EXIT_CODE
;
434 /* Write out code to handle peepholes and the insn_codes that it should
438 for (link
= peepholes
; link
; link
= link
->next
)
439 printf (" case %d:\n", link
->insn_code
);
441 /* The vector in the insn says how many operands it has.
442 And all it contains are operands. In fact, the vector was
443 created just for the sake of this function. We need to set the
444 location of the operands for sake of simplifications after
445 extraction, like eliminating subregs. */
446 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
447 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
451 /* Write out all the ways to extract insn operands. */
452 for (p
= extractions
; p
; p
= p
->next
)
454 for (link
= p
->insns
; link
; link
= link
->next
)
457 name
= get_insn_name (i
);
459 printf (" case %d: /* %s */\n", i
, name
);
461 printf (" case %d:\n", i
);
464 for (i
= 0; i
< p
->op_count
; i
++)
466 if (p
->oplocs
[i
] == 0)
468 printf (" ro[%d] = const0_rtx;\n", i
);
469 printf (" ro_loc[%d] = &junk;\n", i
);
473 printf (" ro[%d] = *(ro_loc[%d] = &", i
, i
);
474 print_path (p
->oplocs
[i
]);
479 for (i
= 0; i
< p
->dup_count
; i
++)
481 printf (" recog_data.dup_loc[%d] = &", i
);
482 print_path (p
->duplocs
[i
]);
484 printf (" recog_data.dup_num[%d] = %d;\n", i
, p
->dupnums
[i
]);
492 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);