1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987-2020 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' then 'A' through to 'Z' when going into a vector. We assume here that
37 only the first operand of an rtl expression is a vector. genrecog.c makes
38 the same assumption (and uses the same representation) and it is currently
45 unsigned int op_count
;
46 unsigned int dup_count
;
50 struct code_ptr
*insns
;
51 struct extraction
*next
;
54 /* Holds a single insn code that uses an extraction method. */
58 struct code_ptr
*next
;
61 /* All extractions needed for this machine description. */
62 static struct extraction
*extractions
;
64 /* All insn codes for old-style peepholes. */
65 static struct code_ptr
*peepholes
;
67 /* This structure is used by gen_insn and walk_rtx to accumulate the
68 data that will be used to produce an extractions structure. */
74 accum_extract () : oplocs (10), duplocs (10), dupnums (10), pathstr (20) {}
76 auto_vec
<locstr
> oplocs
;
77 auto_vec
<locstr
> duplocs
;
78 auto_vec
<int> dupnums
;
79 auto_vec
<char> pathstr
;
82 /* Forward declarations. */
83 static void walk_rtx (md_rtx_info
*, rtx
, class accum_extract
*);
85 #define UPPER_OFFSET ('A' - ('z' - 'a' + 1))
87 /* Convert integer OPERAND into a character - either into [a-zA-Z] for vector
88 operands or [0-9] for integer operands - and push onto the end of the path
91 push_pathstr_operand (int operand
, bool is_vector
,
92 class accum_extract
*acc
)
94 if (is_vector
&& 'a' + operand
> 'z')
95 acc
->pathstr
.safe_push (operand
+ UPPER_OFFSET
);
97 acc
->pathstr
.safe_push (operand
+ 'a');
99 acc
->pathstr
.safe_push (operand
+ '0');
103 gen_insn (md_rtx_info
*info
)
106 unsigned int op_count
, dup_count
, j
;
107 struct extraction
*p
;
108 struct code_ptr
*link
;
109 class accum_extract acc
;
111 /* Walk the insn's pattern, remembering at all times the path
112 down to the walking point. */
114 rtx insn
= info
->def
;
115 if (XVECLEN (insn
, 1) == 1)
116 walk_rtx (info
, XVECEXP (insn
, 1, 0), &acc
);
118 for (i
= XVECLEN (insn
, 1) - 1; i
>= 0; i
--)
120 push_pathstr_operand (i
, true, &acc
);
121 walk_rtx (info
, XVECEXP (insn
, 1, i
), &acc
);
125 link
= XNEW (struct code_ptr
);
126 link
->insn_code
= info
->index
;
128 /* See if we find something that already had this extraction method. */
130 op_count
= acc
.oplocs
.length ();
131 dup_count
= acc
.duplocs
.length ();
132 gcc_assert (dup_count
== acc
.dupnums
.length ());
134 for (p
= extractions
; p
; p
= p
->next
)
136 if (p
->op_count
!= op_count
|| p
->dup_count
!= dup_count
)
139 for (j
= 0; j
< op_count
; j
++)
141 char *a
= p
->oplocs
[j
];
142 char *b
= acc
.oplocs
[j
];
143 if (a
!= b
&& (!a
|| !b
|| strcmp (a
, b
)))
150 for (j
= 0; j
< dup_count
; j
++)
151 if (p
->dupnums
[j
] != acc
.dupnums
[j
]
152 || strcmp (p
->duplocs
[j
], acc
.duplocs
[j
]))
158 /* This extraction is the same as ours. Just link us in. */
159 link
->next
= p
->insns
;
164 /* Otherwise, make a new extraction method. We stash the arrays
165 after the extraction structure in memory. */
167 p
= XNEWVAR (struct extraction
, sizeof (struct extraction
)
168 + op_count
*sizeof (char *)
169 + dup_count
*sizeof (char *)
170 + dup_count
*sizeof (int));
171 p
->op_count
= op_count
;
172 p
->dup_count
= dup_count
;
173 p
->next
= extractions
;
178 p
->oplocs
= (char **)((char *)p
+ sizeof (struct extraction
));
179 p
->duplocs
= p
->oplocs
+ op_count
;
180 p
->dupnums
= (int *)(p
->duplocs
+ dup_count
);
182 memcpy (p
->oplocs
, acc
.oplocs
.address (), op_count
* sizeof (locstr
));
183 memcpy (p
->duplocs
, acc
.duplocs
.address (), dup_count
* sizeof (locstr
));
184 memcpy (p
->dupnums
, acc
.dupnums
.address (), dup_count
* sizeof (int));
187 /* Helper subroutine of walk_rtx: given a vec<locstr>, an index, and a
188 string, insert the string at the index, which should either already
189 exist and be NULL, or not yet exist within the vector. In the latter
190 case the vector is enlarged as appropriate. INFO describes the
191 containing define_* expression. */
193 VEC_safe_set_locstr (md_rtx_info
*info
, vec
<locstr
> *vp
,
194 unsigned int ix
, char *str
)
196 if (ix
< (*vp
).length ())
200 message_at (info
->loc
, "repeated operand number %d", ix
);
208 while (ix
> (*vp
).length ())
209 vp
->safe_push (NULL
);
214 /* Another helper subroutine of walk_rtx: given a vec<char>, convert it
215 to a NUL-terminated string in malloc memory. */
217 VEC_char_to_string (vec
<char> v
)
219 size_t n
= v
.length ();
220 char *s
= XNEWVEC (char, n
+ 1);
221 memcpy (s
, v
.address (), n
);
227 walk_rtx (md_rtx_info
*info
, rtx x
, class accum_extract
*acc
)
247 VEC_safe_set_locstr (info
, &acc
->oplocs
, XINT (x
, 0),
248 VEC_char_to_string (acc
->pathstr
));
253 VEC_safe_set_locstr (info
, &acc
->oplocs
, XINT (x
, 0),
254 VEC_char_to_string (acc
->pathstr
));
256 for (i
= XVECLEN (x
, 2) - 1; i
>= 0; i
--)
258 push_pathstr_operand (i
, code
!= MATCH_OPERATOR
, acc
);
259 walk_rtx (info
, XVECEXP (x
, 2, i
), acc
);
267 acc
->duplocs
.safe_push (VEC_char_to_string (acc
->pathstr
));
268 acc
->dupnums
.safe_push (XINT (x
, 0));
270 if (code
== MATCH_DUP
)
273 for (i
= XVECLEN (x
, 1) - 1; i
>= 0; i
--)
275 push_pathstr_operand (i
, code
!= MATCH_OP_DUP
, acc
);
276 walk_rtx (info
, XVECEXP (x
, 1, i
), acc
);
285 fmt
= GET_RTX_FORMAT (code
);
286 len
= GET_RTX_LENGTH (code
);
287 for (i
= 0; i
< len
; i
++)
289 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
291 push_pathstr_operand (i
, false, acc
);
292 walk_rtx (info
, XEXP (x
, i
), acc
);
295 else if (fmt
[i
] == 'E')
298 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
300 push_pathstr_operand (j
, true, acc
);
301 walk_rtx (info
, XVECEXP (x
, i
, j
), acc
);
308 /* Given a PATH, representing a path down the instruction's
309 pattern from the root to a certain point, output code to
310 evaluate to the rtx at that point. */
313 print_path (const char *path
)
315 int len
= strlen (path
);
320 /* Don't emit "pat", since we may try to take the address of it,
321 which isn't what is intended. */
322 fputs ("PATTERN (insn)", stdout
);
326 /* We first write out the operations (XEXP or XVECEXP) in reverse
327 order, then write "pat", then the indices in forward order. */
329 for (i
= len
- 1; i
>= 0 ; i
--)
331 if (ISLOWER (path
[i
]) || ISUPPER (path
[i
]))
332 fputs ("XVECEXP (", stdout
);
333 else if (ISDIGIT (path
[i
]))
334 fputs ("XEXP (", stdout
);
339 fputs ("pat", stdout
);
341 for (i
= 0; i
< len
; i
++)
343 if (ISUPPER (path
[i
]))
344 printf (", 0, %d)", path
[i
] - UPPER_OFFSET
);
345 else if (ISLOWER (path
[i
]))
346 printf (", 0, %d)", path
[i
] - 'a');
347 else if (ISDIGIT (path
[i
]))
348 printf (", %d)", path
[i
] - '0');
357 /* N.B. Code below avoids putting squiggle braces in column 1 inside
358 a string, because this confuses some editors' syntax highlighting
362 /* Generated automatically by the program `genextract'\n\
363 from the machine description file `md'. */\n\
365 #define IN_TARGET_CODE 1\n\
366 #include \"config.h\"\n\
367 #include \"system.h\"\n\
368 #include \"coretypes.h\"\n\
370 #include \"rtl.h\"\n\
371 #include \"insn-config.h\"\n\
372 #include \"recog.h\"\n\
373 #include \"diagnostic-core.h\"\n\
375 /* This variable is used as the \"location\" of any missing operand\n\
376 whose numbers are skipped by a given pattern. */\n\
377 static rtx junk ATTRIBUTE_UNUSED;\n");
381 insn_extract (rtx_insn *insn)\n{\n\
382 rtx *ro = recog_data.operand;\n\
383 rtx **ro_loc = recog_data.operand_loc;\n\
384 rtx pat = PATTERN (insn);\n\
385 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
387 if (flag_checking)\n\
389 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
390 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
394 switch (INSN_CODE (insn))\n\
397 /* Control reaches here if insn_extract has been called with an\n\
398 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
399 corresponds to a DEFINE_EXPAND in the machine description;\n\
400 either way, a bug. */\n\
401 if (INSN_CODE (insn) < 0)\n\
402 fatal_insn (\"unrecognizable insn:\", insn);\n\
404 fatal_insn (\"insn with invalid code number:\", insn);\n");
408 main (int argc
, const char **argv
)
411 struct extraction
*p
;
412 struct code_ptr
*link
;
415 progname
= "genextract";
417 if (!init_rtx_reader_args (argc
, argv
))
418 return (FATAL_EXIT_CODE
);
420 /* Read the machine description. */
423 while (read_md_rtx (&info
))
424 switch (GET_CODE (info
.def
))
430 case DEFINE_PEEPHOLE
:
432 struct code_ptr
*link
= XNEW (struct code_ptr
);
434 link
->insn_code
= info
.index
;
435 link
->next
= peepholes
;
445 return FATAL_EXIT_CODE
;
449 /* Write out code to handle peepholes and the insn_codes that it should
453 for (link
= peepholes
; link
; link
= link
->next
)
454 printf (" case %d:\n", link
->insn_code
);
456 /* The vector in the insn says how many operands it has.
457 And all it contains are operands. In fact, the vector was
458 created just for the sake of this function. We need to set the
459 location of the operands for sake of simplifications after
460 extraction, like eliminating subregs. */
461 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
462 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
466 /* Write out all the ways to extract insn operands. */
467 for (p
= extractions
; p
; p
= p
->next
)
469 for (link
= p
->insns
; link
; link
= link
->next
)
472 name
= get_insn_name (i
);
474 printf (" case %d: /* %s */\n", i
, name
);
476 printf (" case %d:\n", i
);
479 for (i
= 0; i
< p
->op_count
; i
++)
481 if (p
->oplocs
[i
] == 0)
483 printf (" ro[%d] = const0_rtx;\n", i
);
484 printf (" ro_loc[%d] = &junk;\n", i
);
488 printf (" ro[%d] = *(ro_loc[%d] = &", i
, i
);
489 print_path (p
->oplocs
[i
]);
494 for (i
= 0; i
< p
->dup_count
; i
++)
496 printf (" recog_data.dup_loc[%d] = &", i
);
497 print_path (p
->duplocs
[i
]);
499 printf (" recog_data.dup_num[%d] = %d;\n", i
, p
->dupnums
[i
]);
507 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);