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

source code of gcc/gengenrtl.cc