| 1 |
/* Generate code to allocate RTL structures. |
| 2 |
Copyright (C) 1997-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 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 |
| 9 |
version. |
| 10 |
|
| 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 |
| 14 |
for more details. |
| 15 |
|
| 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/>. */ |
| 19 |
|
| 20 |
|
| 21 |
#include "bconfig.h" |
| 22 |
#include "system.h" |
| 23 |
|
| 24 |
struct 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 |
|
| 36 |
static 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 |
|
| 42 |
static const char *formats[NUM_RTX_CODE]; |
| 43 |
|
| 44 |
/* Decode a format letter into a C type string. */ |
| 45 |
|
| 46 |
static const char * |
| 47 |
type_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 |
|
| 82 |
static const char * |
| 83 |
accessor_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 |
|
| 119 |
static int |
| 120 |
special_format (const char *fmt) |
| 121 |
{ |
| 122 |
return (strchr (fmt, '*') != 0 |
| 123 |
|| strchr (fmt, 'V') != 0 |
| 124 |
|| strchr (fmt, 'S') != 0 |
| 125 |
|| strchr (fmt, 'n') != 0 |
| 126 |
|| strchr (fmt, 'r') != 0); |
| 127 |
} |
| 128 |
|
| 129 |
/* Return true if CODE always has VOIDmode. */ |
| 130 |
|
| 131 |
static inline bool |
| 132 |
always_void_p (int idx) |
| 133 |
{ |
| 134 |
return strcmp (defs[idx].enumname, "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 |
static int |
| 142 |
special_rtx (int idx) |
| 143 |
{ |
| 144 |
return (strcmp (defs[idx].enumname, "EXPR_LIST") == 0 |
| 145 |
|| strcmp (defs[idx].enumname, "INSN_LIST") == 0 |
| 146 |
|| strcmp (defs[idx].enumname, "INSN") == 0 |
| 147 |
|| strcmp (defs[idx].enumname, "CONST_INT") == 0 |
| 148 |
|| strcmp (defs[idx].enumname, "REG") == 0 |
| 149 |
|| strcmp (defs[idx].enumname, "SUBREG") == 0 |
| 150 |
|| strcmp (defs[idx].enumname, "MEM") == 0 |
| 151 |
|| strcmp (defs[idx].enumname, "PC") == 0 |
| 152 |
|| strcmp (defs[idx].enumname, "RETURN") == 0 |
| 153 |
|| strcmp (defs[idx].enumname, "SIMPLE_RETURN") == 0 |
| 154 |
|| strcmp (defs[idx].enumname, "CONST_VECTOR") == 0); |
| 155 |
} |
| 156 |
|
| 157 |
/* Return nonzero if the RTL code given by index IDX is one that we should |
| 158 |
generate no macro for at all (because gen_rtx_FOO is never used or |
| 159 |
cannot have the obvious interface). */ |
| 160 |
|
| 161 |
static int |
| 162 |
excluded_rtx (int idx) |
| 163 |
{ |
| 164 |
return (strcmp (defs[idx].enumname, "VAR_LOCATION") == 0 |
| 165 |
|| strcmp (defs[idx].enumname, "CONST_DOUBLE") == 0 |
| 166 |
|| strcmp (defs[idx].enumname, "CONST_WIDE_INT") == 0 |
| 167 |
|| strcmp (defs[idx].enumname, "CONST_POLY_INT") == 0 |
| 168 |
|| strcmp (defs[idx].enumname, "CONST_FIXED") == 0); |
| 169 |
} |
| 170 |
|
| 171 |
/* Place a list of all format specifiers we use into the array FORMAT. */ |
| 172 |
|
| 173 |
static void |
| 174 |
find_formats (void) |
| 175 |
{ |
| 176 |
unsigned int i; |
| 177 |
|
| 178 |
for (i = 0; i < NUM_RTX_CODE; i++) |
| 179 |
{ |
| 180 |
const char **f; |
| 181 |
|
| 182 |
if (special_format (defs[i].format)) |
| 183 |
continue; |
| 184 |
|
| 185 |
for (f = formats; *f; f++) |
| 186 |
if (! strcmp (*f, defs[i].format)) |
| 187 |
break; |
| 188 |
|
| 189 |
if (*f == 0) |
| 190 |
*f = defs[i].format; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/* Generate macros to generate RTL of code IDX using the functions we |
| 196 |
write. */ |
| 197 |
|
| 198 |
static void |
| 199 |
genmacro (int idx) |
| 200 |
{ |
| 201 |
const char *p; |
| 202 |
const char *sep = ""; |
| 203 |
int i; |
| 204 |
|
| 205 |
/* We write a macro that defines gen_rtx_RTLCODE to be an equivalent to |
| 206 |
gen_rtx_fmt_FORMAT where FORMAT is the RTX_FORMAT of RTLCODE. */ |
| 207 |
|
| 208 |
if (excluded_rtx (idx)) |
| 209 |
/* Don't define a macro for this code. */ |
| 210 |
return; |
| 211 |
|
| 212 |
bool has_mode_p = !always_void_p (idx); |
| 213 |
printf ("#define gen_rtx_%s%s(", |
| 214 |
special_rtx (idx) ? "raw_" : "", defs[idx].enumname); |
| 215 |
if (has_mode_p) |
| 216 |
{ |
| 217 |
printf ("MODE"); |
| 218 |
sep = ", "; |
| 219 |
} |
| 220 |
|
| 221 |
for (p = defs[idx].format, i = 0; *p != 0; p++) |
| 222 |
if (*p != '0') |
| 223 |
{ |
| 224 |
printf ("%sARG%d", sep, i++); |
| 225 |
sep = ", "; |
| 226 |
} |
| 227 |
|
| 228 |
printf (") \\\n gen_rtx_fmt_%s (%s, %s", |
| 229 |
defs[idx].format, defs[idx].enumname, |
| 230 |
has_mode_p ? "(MODE)" : "VOIDmode"); |
| 231 |
|
| 232 |
for (p = defs[idx].format, i = 0; *p != 0; p++) |
| 233 |
if (*p != '0') |
| 234 |
printf (", (ARG%d)", i++); |
| 235 |
|
| 236 |
puts (")"); |
| 237 |
} |
| 238 |
|
| 239 |
/* Generate the code for functions to generate RTL whose format is FORMAT. */ |
| 240 |
|
| 241 |
static void |
| 242 |
gendef (const char *format) |
| 243 |
{ |
| 244 |
const char *p; |
| 245 |
int i, j; |
| 246 |
|
| 247 |
/* Write the definition of the init function name and the types |
| 248 |
of the arguments. */ |
| 249 |
|
| 250 |
puts ("static inline rtx"); |
| 251 |
printf ("init_rtx_fmt_%s (rtx rt, machine_mode mode", format); |
| 252 |
for (p = format, i = 0; *p != 0; p++) |
| 253 |
if (*p != '0') |
| 254 |
printf (",\n\t%sarg%d", type_from_format (*p), i++); |
| 255 |
puts (")"); |
| 256 |
|
| 257 |
/* Now write out the body of the init function itself. */ |
| 258 |
puts ("{"); |
| 259 |
puts (" PUT_MODE_RAW (rt, mode);"); |
| 260 |
|
| 261 |
for (p = format, i = j = 0; *p ; ++p, ++i) |
| 262 |
if (*p == '0') |
| 263 |
printf (" X0EXP (rt, %d) = NULL_RTX;\n", i); |
| 264 |
else if (*p == 'p') |
| 265 |
printf (" SUBREG_BYTE (rt) = arg%d;\n", j++); |
| 266 |
else |
| 267 |
printf (" %s (rt, %d) = arg%d;\n", accessor_from_format (*p), i, j++); |
| 268 |
|
| 269 |
puts (" return rt;\n}\n"); |
| 270 |
|
| 271 |
/* Write the definition of the gen function name and the types |
| 272 |
of the arguments. */ |
| 273 |
|
| 274 |
puts ("static inline rtx"); |
| 275 |
printf ("gen_rtx_fmt_%s_stat (RTX_CODE code, machine_mode mode", format); |
| 276 |
for (p = format, i = 0; *p != 0; p++) |
| 277 |
if (*p != '0') |
| 278 |
printf (",\n\t%sarg%d", type_from_format (*p), i++); |
| 279 |
puts (" MEM_STAT_DECL)"); |
| 280 |
|
| 281 |
/* Now write out the body of the function itself, which allocates |
| 282 |
the memory and initializes it. */ |
| 283 |
puts ("{"); |
| 284 |
puts (" rtx rt;\n"); |
| 285 |
|
| 286 |
puts (" rt = rtx_alloc (code PASS_MEM_STAT);"); |
| 287 |
printf (" return init_rtx_fmt_%s (rt, mode", format); |
| 288 |
for (p = format, i = 0; *p != 0; p++) |
| 289 |
if (*p != '0') |
| 290 |
printf (", arg%d", i++); |
| 291 |
puts (");\n}\n"); |
| 292 |
|
| 293 |
/* Write the definition of gen macro. */ |
| 294 |
|
| 295 |
printf ("#define gen_rtx_fmt_%s(c, m", format); |
| 296 |
for (p = format, i = 0; *p != 0; p++) |
| 297 |
if (*p != '0') |
| 298 |
printf (", arg%d", i++); |
| 299 |
printf (") \\\n gen_rtx_fmt_%s_stat ((c), (m)", format); |
| 300 |
for (p = format, i = 0; *p != 0; p++) |
| 301 |
if (*p != '0') |
| 302 |
printf (", (arg%d)", i++); |
| 303 |
printf (" MEM_STAT_INFO)\n\n"); |
| 304 |
|
| 305 |
/* Write the definition of alloca macro. */ |
| 306 |
|
| 307 |
printf ("#define alloca_rtx_fmt_%s(c, m", format); |
| 308 |
for (p = format, i = 0; *p != 0; p++) |
| 309 |
if (*p != '0') |
| 310 |
printf (", arg%d", i++); |
| 311 |
printf (") \\\n init_rtx_fmt_%s (rtx_alloca ((c)), (m)", format); |
| 312 |
for (p = format, i = 0; *p != 0; p++) |
| 313 |
if (*p != '0') |
| 314 |
printf (", (arg%d)", i++); |
| 315 |
printf (")\n\n"); |
| 316 |
} |
| 317 |
|
| 318 |
/* Generate the documentation header for files we write. */ |
| 319 |
|
| 320 |
static void |
| 321 |
genlegend (void) |
| 322 |
{ |
| 323 |
puts ("/* Generated automatically by gengenrtl from rtl.def. */\n"); |
| 324 |
} |
| 325 |
|
| 326 |
/* Generate the text of the header file we make, genrtl.h. */ |
| 327 |
|
| 328 |
static void |
| 329 |
genheader (void) |
| 330 |
{ |
| 331 |
unsigned int i; |
| 332 |
const char **fmt; |
| 333 |
|
| 334 |
puts ("#ifndef GCC_GENRTL_H"); |
| 335 |
puts ("#define GCC_GENRTL_H\n"); |
| 336 |
puts ("#include \"statistics.h\"\n"); |
| 337 |
|
| 338 |
for (fmt = formats; *fmt; ++fmt) |
| 339 |
gendef (*fmt); |
| 340 |
|
| 341 |
putchar ('\n'); |
| 342 |
|
| 343 |
for (i = 0; i < NUM_RTX_CODE; i++) |
| 344 |
if (! special_format (defs[i].format)) |
| 345 |
genmacro (i); |
| 346 |
|
| 347 |
puts ("\n#endif /* GCC_GENRTL_H */"); |
| 348 |
} |
| 349 |
|
| 350 |
/* This is the main program. */ |
| 351 |
|
| 352 |
int |
| 353 |
main (void) |
| 354 |
{ |
| 355 |
find_formats (); |
| 356 |
genlegend (); |
| 357 |
|
| 358 |
genheader (); |
| 359 |
|
| 360 |
if (ferror (stdout) || fflush (stdout) || fclose (stdout)) |
| 361 |
return FATAL_EXIT_CODE; |
| 362 |
|
| 363 |
return SUCCESS_EXIT_CODE; |
| 364 |
} |