]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gengenrtl.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / gengenrtl.cc
CommitLineData
3b80f6ca 1/* Generate code to allocate RTL structures.
a945c346 2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3b80f6ca 3
1322177d 4This file is part of GCC.
3b80f6ca 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
3b80f6ca 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
3b80f6ca
RH
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
3b80f6ca
RH
19
20
4977bab6 21#include "bconfig.h"
b04cd507 22#include "system.h"
aa0b4465 23
8f9eb495 24struct rtx_definition
3b80f6ca 25{
8b60264b 26 const char *const enumname, *const name, *const format;
3b80f6ca
RH
27};
28
0974c7d7
ZW
29/* rtl.def needs CONST_DOUBLE_FORMAT, but we don't care what
30 CONST_DOUBLE_FORMAT is because we're not going to be generating
31 anything for CONST_DOUBLE anyway. */
32#define CONST_DOUBLE_FORMAT ""
33
9a238586 34#define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { #ENUM, NAME, FORMAT },
3b80f6ca 35
8f9eb495
AJ
36static const struct rtx_definition defs[] =
37{
3b80f6ca
RH
38#include "rtl.def" /* rtl expressions are documented here */
39};
c3284718 40#define NUM_RTX_CODE ARRAY_SIZE (defs)
3b80f6ca 41
0b5826ac 42static const char *formats[NUM_RTX_CODE];
c5c76735 43\f
0133b7d9
RH
44/* Decode a format letter into a C type string. */
45
3b80f6ca 46static const char *
3d7aafde 47type_from_format (int c)
3b80f6ca
RH
48{
49 switch (c)
50 {
51 case 'i':
c5c76735
JL
52 return "int ";
53
3b80f6ca 54 case 'w':
c5c76735
JL
55 return "HOST_WIDE_INT ";
56
91914e56
RS
57 case 'p':
58 return "poly_uint16 ";
59
3b80f6ca 60 case 's':
3cce094d 61 return "const char *";
c5c76735
JL
62
63 case 'e': case 'u':
64 return "rtx ";
65
3b80f6ca 66 case 'E':
c5c76735 67 return "rtvec ";
0dfa1860 68 case 't':
b8244d74 69 return "tree ";
c8ea9a0f 70 case 'B':
b8244d74 71 return "basic_block ";
3b80f6ca 72 default:
b2d59f6f 73 gcc_unreachable ();
3b80f6ca
RH
74 }
75}
76
0133b7d9
RH
77/* Decode a format letter into the proper accessor function. */
78
3b80f6ca 79static const char *
3d7aafde 80accessor_from_format (int c)
3b80f6ca
RH
81{
82 switch (c)
83 {
84 case 'i':
85 return "XINT";
c5c76735 86
3b80f6ca
RH
87 case 'w':
88 return "XWINT";
c5c76735 89
3b80f6ca
RH
90 case 's':
91 return "XSTR";
c5c76735
JL
92
93 case 'e': case 'u':
3b80f6ca 94 return "XEXP";
c5c76735 95
3b80f6ca
RH
96 case 'E':
97 return "XVEC";
c5c76735 98
0dfa1860
MM
99 case 't':
100 return "XTREE";
c5c76735 101
c8ea9a0f
JH
102 case 'B':
103 return "XBBDEF";
10d1bb36
JH
104
105 default:
b2d59f6f 106 gcc_unreachable ();
3b80f6ca
RH
107 }
108}
109
c5c76735
JL
110/* Return nonzero if we should ignore FMT, an RTL format, when making
111 the list of formats we write routines to create. */
0133b7d9 112
3b80f6ca 113static int
3d7aafde 114special_format (const char *fmt)
3b80f6ca
RH
115{
116 return (strchr (fmt, '*') != 0
117 || strchr (fmt, 'V') != 0
118 || strchr (fmt, 'S') != 0
9fccb335
RS
119 || strchr (fmt, 'n') != 0
120 || strchr (fmt, 'r') != 0);
3b80f6ca
RH
121}
122
f7df4a84
RS
123/* Return true if CODE always has VOIDmode. */
124
125static inline bool
126always_void_p (int idx)
127{
128 return strcmp (defs[idx].enumname, "SET") == 0;
129}
130
5692c7bc
ZW
131/* Return nonzero if the RTL code given by index IDX is one that we should
132 generate a gen_rtx_raw_FOO macro for, not gen_rtx_FOO (because gen_rtx_FOO
e53b6e56 133 is a wrapper in emit-rtl.cc). */
0133b7d9 134
3b80f6ca 135static int
3d7aafde 136special_rtx (int idx)
3b80f6ca 137{
38e60c55
DM
138 return (strcmp (defs[idx].enumname, "EXPR_LIST") == 0
139 || strcmp (defs[idx].enumname, "INSN_LIST") == 0
d6e1e8b8 140 || strcmp (defs[idx].enumname, "INSN") == 0
a756c6be 141 || strcmp (defs[idx].enumname, "CONST_INT") == 0
41472af8 142 || strcmp (defs[idx].enumname, "REG") == 0
ddef6bc7 143 || strcmp (defs[idx].enumname, "SUBREG") == 0
a06e3c40 144 || strcmp (defs[idx].enumname, "MEM") == 0
3810076b 145 || strcmp (defs[idx].enumname, "PC") == 0
3810076b 146 || strcmp (defs[idx].enumname, "RETURN") == 0
26898771 147 || strcmp (defs[idx].enumname, "SIMPLE_RETURN") == 0
a06e3c40 148 || strcmp (defs[idx].enumname, "CONST_VECTOR") == 0);
3b80f6ca
RH
149}
150
5692c7bc
ZW
151/* Return nonzero if the RTL code given by index IDX is one that we should
152 generate no macro for at all (because gen_rtx_FOO is never used or
153 cannot have the obvious interface). */
154
155static int
3d7aafde 156excluded_rtx (int idx)
5692c7bc 157{
fcc74520
RS
158 return (strcmp (defs[idx].enumname, "VAR_LOCATION") == 0
159 || strcmp (defs[idx].enumname, "CONST_DOUBLE") == 0
160 || strcmp (defs[idx].enumname, "CONST_WIDE_INT") == 0
0c12fc9b 161 || strcmp (defs[idx].enumname, "CONST_POLY_INT") == 0
fcc74520 162 || strcmp (defs[idx].enumname, "CONST_FIXED") == 0);
5692c7bc
ZW
163}
164
dc297297 165/* Place a list of all format specifiers we use into the array FORMAT. */
0133b7d9 166
3b80f6ca 167static void
3d7aafde 168find_formats (void)
3b80f6ca 169{
3ef996b0 170 unsigned int i;
3b80f6ca 171
3ef996b0 172 for (i = 0; i < NUM_RTX_CODE; i++)
3b80f6ca
RH
173 {
174 const char **f;
175
176 if (special_format (defs[i].format))
177 continue;
178
c5c76735 179 for (f = formats; *f; f++)
0133b7d9 180 if (! strcmp (*f, defs[i].format))
3b80f6ca
RH
181 break;
182
c5c76735 183 if (*f == 0)
3b80f6ca
RH
184 *f = defs[i].format;
185 }
186}
187
3b80f6ca 188
c5c76735
JL
189/* Generate macros to generate RTL of code IDX using the functions we
190 write. */
0133b7d9 191
8f9eb495 192static void
3d7aafde 193genmacro (int idx)
3b80f6ca
RH
194{
195 const char *p;
f7df4a84 196 const char *sep = "";
3b80f6ca
RH
197 int i;
198
c5c76735
JL
199 /* We write a macro that defines gen_rtx_RTLCODE to be an equivalent to
200 gen_rtx_fmt_FORMAT where FORMAT is the RTX_FORMAT of RTLCODE. */
3b80f6ca 201
5692c7bc
ZW
202 if (excluded_rtx (idx))
203 /* Don't define a macro for this code. */
204 return;
205
f7df4a84
RS
206 bool has_mode_p = !always_void_p (idx);
207 printf ("#define gen_rtx_%s%s(",
c5c76735 208 special_rtx (idx) ? "raw_" : "", defs[idx].enumname);
f7df4a84
RS
209 if (has_mode_p)
210 {
211 printf ("MODE");
212 sep = ", ";
213 }
c5c76735
JL
214
215 for (p = defs[idx].format, i = 0; *p != 0; p++)
3b80f6ca 216 if (*p != '0')
f7df4a84
RS
217 {
218 printf ("%sARG%d", sep, i++);
219 sep = ", ";
220 }
221
222 printf (") \\\n gen_rtx_fmt_%s (%s, %s",
223 defs[idx].format, defs[idx].enumname,
224 has_mode_p ? "(MODE)" : "VOIDmode");
3b80f6ca 225
c5c76735 226 for (p = defs[idx].format, i = 0; *p != 0; p++)
3b80f6ca 227 if (*p != '0')
c5c76735
JL
228 printf (", (ARG%d)", i++);
229
f8a83ee3 230 puts (")");
3b80f6ca
RH
231}
232
20fa157e 233/* Generate the code for functions to generate RTL whose format is FORMAT. */
0133b7d9 234
3b80f6ca 235static void
3d7aafde 236gendef (const char *format)
3b80f6ca
RH
237{
238 const char *p;
239 int i, j;
8f9eb495 240
20fa157e 241 /* Write the definition of the init function name and the types
c5c76735 242 of the arguments. */
3b80f6ca 243
20fa157e
IL
244 puts ("static inline rtx");
245 printf ("init_rtx_fmt_%s (rtx rt, machine_mode mode", format);
c5c76735 246 for (p = format, i = 0; *p != 0; p++)
3b80f6ca 247 if (*p != '0')
6906ba40 248 printf (",\n\t%sarg%d", type_from_format (*p), i++);
20fa157e 249 puts (")");
3b80f6ca 250
20fa157e 251 /* Now write out the body of the init function itself. */
f8a83ee3 252 puts ("{");
8deccbb7 253 puts (" PUT_MODE_RAW (rt, mode);");
3b80f6ca
RH
254
255 for (p = format, i = j = 0; *p ; ++p, ++i)
91914e56 256 if (*p == '0')
f8a83ee3 257 printf (" X0EXP (rt, %d) = NULL_RTX;\n", i);
91914e56
RS
258 else if (*p == 'p')
259 printf (" SUBREG_BYTE (rt) = arg%d;\n", j++);
260 else
261 printf (" %s (rt, %d) = arg%d;\n", accessor_from_format (*p), i, j++);
3b80f6ca 262
20fa157e
IL
263 puts (" return rt;\n}\n");
264
265 /* Write the definition of the gen function name and the types
266 of the arguments. */
267
268 puts ("static inline rtx");
269 printf ("gen_rtx_fmt_%s_stat (RTX_CODE code, machine_mode mode", format);
270 for (p = format, i = 0; *p != 0; p++)
271 if (*p != '0')
272 printf (",\n\t%sarg%d", type_from_format (*p), i++);
273 puts (" MEM_STAT_DECL)");
274
275 /* Now write out the body of the function itself, which allocates
276 the memory and initializes it. */
277 puts ("{");
278 puts (" rtx rt;\n");
279
280 puts (" rt = rtx_alloc (code PASS_MEM_STAT);");
281 printf (" return init_rtx_fmt_%s (rt, mode", format);
282 for (p = format, i = 0; *p != 0; p++)
283 if (*p != '0')
284 printf (", arg%d", i++);
285 puts (");\n}\n");
286
287 /* Write the definition of gen macro. */
288
6ac9d3a3
JH
289 printf ("#define gen_rtx_fmt_%s(c, m", format);
290 for (p = format, i = 0; *p != 0; p++)
291 if (*p != '0')
20fa157e
IL
292 printf (", arg%d", i++);
293 printf (") \\\n gen_rtx_fmt_%s_stat ((c), (m)", format);
6ac9d3a3
JH
294 for (p = format, i = 0; *p != 0; p++)
295 if (*p != '0')
20fa157e 296 printf (", (arg%d)", i++);
6ac9d3a3 297 printf (" MEM_STAT_INFO)\n\n");
20fa157e
IL
298
299 /* Write the definition of alloca macro. */
300
301 printf ("#define alloca_rtx_fmt_%s(c, m", format);
302 for (p = format, i = 0; *p != 0; p++)
303 if (*p != '0')
304 printf (", arg%d", i++);
305 printf (") \\\n init_rtx_fmt_%s (rtx_alloca ((c)), (m)", format);
306 for (p = format, i = 0; *p != 0; p++)
307 if (*p != '0')
308 printf (", (arg%d)", i++);
309 printf (")\n\n");
3b80f6ca
RH
310}
311
c5c76735 312/* Generate the documentation header for files we write. */
0133b7d9 313
3b80f6ca 314static void
3d7aafde 315genlegend (void)
3b80f6ca 316{
f8a83ee3 317 puts ("/* Generated automatically by gengenrtl from rtl.def. */\n");
3b80f6ca
RH
318}
319
c5c76735 320/* Generate the text of the header file we make, genrtl.h. */
0133b7d9 321
3b80f6ca 322static void
3d7aafde 323genheader (void)
3b80f6ca 324{
3ef996b0 325 unsigned int i;
3b80f6ca 326 const char **fmt;
0313e85b
ZW
327
328 puts ("#ifndef GCC_GENRTL_H");
329 puts ("#define GCC_GENRTL_H\n");
9dbe7947 330 puts ("#include \"statistics.h\"\n");
0313e85b 331
3b80f6ca 332 for (fmt = formats; *fmt; ++fmt)
6ac9d3a3 333 gendef (*fmt);
3b80f6ca 334
f8a83ee3 335 putchar ('\n');
3b80f6ca 336
3ef996b0 337 for (i = 0; i < NUM_RTX_CODE; i++)
c5c76735
JL
338 if (! special_format (defs[i].format))
339 genmacro (i);
0313e85b
ZW
340
341 puts ("\n#endif /* GCC_GENRTL_H */");
3b80f6ca
RH
342}
343
6ac9d3a3 344/* This is the main program. */
c5c76735 345
3b80f6ca 346int
6ac9d3a3 347main (void)
3b80f6ca 348{
3b80f6ca 349 find_formats ();
c5c76735 350 genlegend ();
3b80f6ca 351
6ac9d3a3 352 genheader ();
3b80f6ca 353
0313e85b
ZW
354 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
355 return FATAL_EXIT_CODE;
356
357 return SUCCESS_EXIT_CODE;
3b80f6ca 358}