| 1 |
/* Subroutines used for code generation for eBPF. |
| 2 |
Copyright (C) 2019-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 |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License 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 |
#define IN_TARGET_CODE 1 |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "tm.h" |
| 26 |
#include "rtl.h" |
| 27 |
#include "regs.h" |
| 28 |
#include "insn-config.h" |
| 29 |
#include "insn-attr.h" |
| 30 |
#include "recog.h" |
| 31 |
#include "output.h" |
| 32 |
#include "alias.h" |
| 33 |
#include "tree.h" |
| 34 |
#include "stringpool.h" |
| 35 |
#include "attribs.h" |
| 36 |
#include "varasm.h" |
| 37 |
#include "stor-layout.h" |
| 38 |
#include "calls.h" |
| 39 |
#include "function.h" |
| 40 |
#include "explow.h" |
| 41 |
#include "memmodel.h" |
| 42 |
#include "emit-rtl.h" |
| 43 |
#include "reload.h" |
| 44 |
#include "tm_p.h" |
| 45 |
#include "target.h" |
| 46 |
#include "target-def.h" |
| 47 |
#include "basic-block.h" |
| 48 |
#include "expr.h" |
| 49 |
#include "optabs.h" |
| 50 |
#include "bitmap.h" |
| 51 |
#include "df.h" |
| 52 |
#include "c-family/c-common.h" |
| 53 |
#include "diagnostic.h" |
| 54 |
#include "builtins.h" |
| 55 |
#include "predict.h" |
| 56 |
#include "langhooks.h" |
| 57 |
#include "flags.h" |
| 58 |
|
| 59 |
#include "cfg.h" /* needed for struct control_flow_graph used in BB macros */ |
| 60 |
#include "gimple.h" |
| 61 |
#include "gimple-iterator.h" |
| 62 |
#include "gimple-walk.h" |
| 63 |
#include "tree-pass.h" |
| 64 |
#include "tree-iterator.h" |
| 65 |
|
| 66 |
#include "context.h" |
| 67 |
#include "pass_manager.h" |
| 68 |
|
| 69 |
#include "gimplify.h" |
| 70 |
#include "gimplify-me.h" |
| 71 |
|
| 72 |
#include "core-builtins.h" |
| 73 |
#include "opts.h" |
| 74 |
|
| 75 |
/* Per-function machine data. */ |
| 76 |
struct GTY(()) machine_function |
| 77 |
{ |
| 78 |
/* Number of bytes saved on the stack for local variables. */ |
| 79 |
int local_vars_size; |
| 80 |
}; |
| 81 |
|
| 82 |
/* Handle an attribute requiring a FUNCTION_DECL; |
| 83 |
arguments as in struct attribute_spec.handler. */ |
| 84 |
|
| 85 |
static tree |
| 86 |
bpf_handle_fndecl_attribute (tree *node, tree name, |
| 87 |
tree args, |
| 88 |
int flags ATTRIBUTE_UNUSED, |
| 89 |
bool *no_add_attrs) |
| 90 |
{ |
| 91 |
if (TREE_CODE (*node) != FUNCTION_DECL) |
| 92 |
{ |
| 93 |
warning (OPT_Wattributes, "%qE attribute only applies to functions", |
| 94 |
name); |
| 95 |
*no_add_attrs = true; |
| 96 |
} |
| 97 |
|
| 98 |
if (is_attribute_p ("kernel_helper", name)) |
| 99 |
{ |
| 100 |
if (args) |
| 101 |
{ |
| 102 |
tree cst = TREE_VALUE (args); |
| 103 |
if (TREE_CODE (cst) != INTEGER_CST) |
| 104 |
{ |
| 105 |
warning (OPT_Wattributes, "%qE attribute requires an integer argument", |
| 106 |
name); |
| 107 |
*no_add_attrs = true; |
| 108 |
} |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
warning (OPT_Wattributes, "%qE requires an argument", name); |
| 113 |
*no_add_attrs = true; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return NULL_TREE; |
| 118 |
} |
| 119 |
|
| 120 |
/* Handle preserve_access_index attribute, which can be applied to structs, |
| 121 |
unions and classes. Actually adding the attribute to the TYPE_DECL is |
| 122 |
taken care of for us, so just warn for types that aren't supported. */ |
| 123 |
|
| 124 |
static tree |
| 125 |
bpf_handle_preserve_access_index_attribute (tree *node, tree name, |
| 126 |
tree args ATTRIBUTE_UNUSED, |
| 127 |
int flags ATTRIBUTE_UNUSED, |
| 128 |
bool *no_add_attrs) |
| 129 |
{ |
| 130 |
if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) |
| 131 |
{ |
| 132 |
warning (OPT_Wattributes, |
| 133 |
"%qE attribute only applies to structure, union and class types", |
| 134 |
name); |
| 135 |
*no_add_attrs = true; |
| 136 |
} |
| 137 |
|
| 138 |
return NULL_TREE; |
| 139 |
} |
| 140 |
|
| 141 |
/* Target-specific attributes. */ |
| 142 |
|
| 143 |
TARGET_GNU_ATTRIBUTES (bpf_attribute_table, |
| 144 |
{ |
| 145 |
/* Syntax: { name, min_len, max_len, decl_required, type_required, |
| 146 |
function_type_required, affects_type_identity, handler, |
| 147 |
exclude } */ |
| 148 |
|
| 149 |
/* Attribute to mark function prototypes as kernel helpers. */ |
| 150 |
{ "kernel_helper", 1, 1, true, false, false, false, |
| 151 |
bpf_handle_fndecl_attribute, NULL }, |
| 152 |
|
| 153 |
/* CO-RE support: attribute to mark that all accesses to the declared |
| 154 |
struct/union/array should be recorded. */ |
| 155 |
{ "preserve_access_index", 0, -1, false, true, false, true, |
| 156 |
bpf_handle_preserve_access_index_attribute, NULL }, |
| 157 |
|
| 158 |
/* Support for `naked' function attribute. */ |
| 159 |
{ "naked", 0, 1, false, false, false, false, |
| 160 |
bpf_handle_fndecl_attribute, NULL } |
| 161 |
}); |
| 162 |
|
| 163 |
#undef TARGET_ATTRIBUTE_TABLE |
| 164 |
#define TARGET_ATTRIBUTE_TABLE bpf_attribute_table |
| 165 |
|
| 166 |
/* Data structures for the eBPF specific built-ins. */ |
| 167 |
|
| 168 |
/* Maximum number of arguments taken by a builtin function, plus |
| 169 |
one. */ |
| 170 |
#define BPF_BUILTIN_MAX_ARGS 5 |
| 171 |
|
| 172 |
GTY (()) tree bpf_builtins[(int) BPF_BUILTIN_MAX]; |
| 173 |
|
| 174 |
void bpf_register_coreattr_pass (void); |
| 175 |
|
| 176 |
/* Initialize the per-function machine status. */ |
| 177 |
|
| 178 |
static struct machine_function * |
| 179 |
bpf_init_machine_status (void) |
| 180 |
{ |
| 181 |
/* Note this initializes all fields to 0, which is just OK for |
| 182 |
us. */ |
| 183 |
return ggc_cleared_alloc<machine_function> (); |
| 184 |
} |
| 185 |
|
| 186 |
/* Override options and do some other initialization. */ |
| 187 |
|
| 188 |
static void |
| 189 |
bpf_option_override (void) |
| 190 |
{ |
| 191 |
/* Set the initializer for the per-function status structure. */ |
| 192 |
init_machine_status = bpf_init_machine_status; |
| 193 |
|
| 194 |
/* BPF CO-RE support requires BTF debug info generation. */ |
| 195 |
if (TARGET_BPF_CORE |
| 196 |
&& (!btf_debuginfo_p () || (debug_info_level < DINFO_LEVEL_NORMAL))) |
| 197 |
error ("BPF CO-RE requires BTF debugging information, use %<-gbtf%>"); |
| 198 |
|
| 199 |
/* BPF applications always generate .BTF.ext. */ |
| 200 |
write_symbols |= BTF_WITH_CORE_DEBUG; |
| 201 |
|
| 202 |
/* Unlike much of the other BTF debug information, the information necessary |
| 203 |
for CO-RE relocations is added to the CTF container by the BPF backend. |
| 204 |
Enabling LTO adds some complications in the generation of the BPF CO-RE |
| 205 |
relocations because if LTO is in effect, the relocations need to be |
| 206 |
generated late in the LTO link phase. This poses a new challenge for the |
| 207 |
compiler to now provide means to combine the early BTF and late BTF CO-RE |
| 208 |
debug info, similar to DWARF debug info. BTF/CO-RE debug info is not |
| 209 |
amenable to such a split generation and a later merging. |
| 210 |
|
| 211 |
In any case, in absence of linker support for BTF sections at this time, |
| 212 |
it is acceptable to simply disallow LTO for BPF CO-RE compilations. */ |
| 213 |
|
| 214 |
if (flag_lto && TARGET_BPF_CORE) |
| 215 |
sorry ("BPF CO-RE does not support LTO"); |
| 216 |
|
| 217 |
/* -gbtf implies -mcore when using the BPF backend, unless -mno-co-re |
| 218 |
is specified. */ |
| 219 |
if (btf_debuginfo_p () |
| 220 |
&& (debug_info_level >= DINFO_LEVEL_NORMAL) |
| 221 |
&& !(target_flags_explicit & MASK_BPF_CORE)) |
| 222 |
target_flags |= MASK_BPF_CORE; |
| 223 |
|
| 224 |
/* -gbtf implies -gprune-btf for BPF target. */ |
| 225 |
if (btf_debuginfo_p ()) |
| 226 |
SET_OPTION_IF_UNSET (&global_options, &global_options_set, |
| 227 |
debug_prune_btf, true); |
| 228 |
|
| 229 |
/* Determine available features from ISA setting (-mcpu=). */ |
| 230 |
if (bpf_has_jmpext == -1) |
| 231 |
bpf_has_jmpext = (bpf_isa >= ISA_V2); |
| 232 |
|
| 233 |
if (bpf_has_alu32 == -1) |
| 234 |
bpf_has_alu32 = (bpf_isa >= ISA_V3); |
| 235 |
|
| 236 |
if (bpf_has_jmp32 == -1) |
| 237 |
bpf_has_jmp32 = (bpf_isa >= ISA_V3); |
| 238 |
|
| 239 |
if (bpf_has_v3_atomics == -1) |
| 240 |
bpf_has_v3_atomics = (bpf_isa >= ISA_V3); |
| 241 |
|
| 242 |
if (bpf_has_bswap == -1) |
| 243 |
bpf_has_bswap = (bpf_isa >= ISA_V4); |
| 244 |
|
| 245 |
if (bpf_has_sdiv == -1) |
| 246 |
bpf_has_sdiv = (bpf_isa >= ISA_V4); |
| 247 |
|
| 248 |
if (bpf_has_smov == -1) |
| 249 |
bpf_has_smov = (bpf_isa >= ISA_V4); |
| 250 |
|
| 251 |
/* Disable -fstack-protector as it is not supported in BPF. */ |
| 252 |
if (flag_stack_protect) |
| 253 |
{ |
| 254 |
if (!flag_stack_protector_set_by_fhardened_p) |
| 255 |
inform (input_location, |
| 256 |
"%<-fstack-protector%> does not work " |
| 257 |
"on this architecture"); |
| 258 |
flag_stack_protect = 0; |
| 259 |
} |
| 260 |
|
| 261 |
/* The BPF target does not support tail call optimization. */ |
| 262 |
flag_optimize_sibling_calls = 0; |
| 263 |
} |
| 264 |
|
| 265 |
#undef TARGET_OPTION_OVERRIDE |
| 266 |
#define TARGET_OPTION_OVERRIDE bpf_option_override |
| 267 |
|
| 268 |
/* Implement TARGET_ASM_INIT_SECTIONS. */ |
| 269 |
|
| 270 |
static void |
| 271 |
bpf_asm_init_sections (void) |
| 272 |
{ |
| 273 |
if (btf_debuginfo_p () && btf_with_core_debuginfo_p ()) |
| 274 |
btf_ext_init (); |
| 275 |
} |
| 276 |
|
| 277 |
#undef TARGET_ASM_INIT_SECTIONS |
| 278 |
#define TARGET_ASM_INIT_SECTIONS bpf_asm_init_sections |
| 279 |
|
| 280 |
/* Implement TARGET_ASM_FILE_END. */ |
| 281 |
|
| 282 |
static void |
| 283 |
bpf_file_end (void) |
| 284 |
{ |
| 285 |
if (btf_debuginfo_p () && btf_with_core_debuginfo_p ()) |
| 286 |
{ |
| 287 |
btf_ext_output (); |
| 288 |
btf_finalize (); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
#undef TARGET_ASM_FILE_END |
| 293 |
#define TARGET_ASM_FILE_END bpf_file_end |
| 294 |
|
| 295 |
/* Return an RTX representing the place where a function returns or |
| 296 |
receives a value of data type RET_TYPE, a tree node representing a |
| 297 |
data type. */ |
| 298 |
|
| 299 |
static rtx |
| 300 |
bpf_function_value (const_tree ret_type, |
| 301 |
const_tree fntype_or_decl, |
| 302 |
bool outgoing ATTRIBUTE_UNUSED) |
| 303 |
{ |
| 304 |
enum machine_mode mode; |
| 305 |
int unsignedp; |
| 306 |
|
| 307 |
mode = TYPE_MODE (ret_type); |
| 308 |
if (INTEGRAL_TYPE_P (ret_type)) |
| 309 |
mode = promote_function_mode (ret_type, mode, &unsignedp, |
| 310 |
fntype_or_decl, 1); |
| 311 |
|
| 312 |
return gen_rtx_REG (mode, BPF_R0); |
| 313 |
} |
| 314 |
|
| 315 |
#undef TARGET_FUNCTION_VALUE |
| 316 |
#define TARGET_FUNCTION_VALUE bpf_function_value |
| 317 |
|
| 318 |
/* Return true if REGNO is the number of a hard register in which the |
| 319 |
values of called function may come back. */ |
| 320 |
|
| 321 |
static bool |
| 322 |
bpf_function_value_regno_p (const unsigned int regno) |
| 323 |
{ |
| 324 |
return (regno == BPF_R0); |
| 325 |
} |
| 326 |
|
| 327 |
#undef TARGET_FUNCTION_VALUE_REGNO_P |
| 328 |
#define TARGET_FUNCTION_VALUE_REGNO_P bpf_function_value_regno_p |
| 329 |
|
| 330 |
|
| 331 |
/* Determine whether to warn about lack of return statement in a |
| 332 |
function. */ |
| 333 |
|
| 334 |
static bool |
| 335 |
bpf_warn_func_return (tree decl) |
| 336 |
{ |
| 337 |
/* Naked functions are implemented entirely in assembly, including |
| 338 |
the return instructions. */ |
| 339 |
return lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) == NULL_TREE; |
| 340 |
} |
| 341 |
|
| 342 |
#undef TARGET_WARN_FUNC_RETURN |
| 343 |
#define TARGET_WARN_FUNC_RETURN bpf_warn_func_return |
| 344 |
|
| 345 |
/* Compute the size of the function's stack frame, including the local |
| 346 |
area and the register-save area. */ |
| 347 |
|
| 348 |
static void |
| 349 |
bpf_compute_frame_layout (void) |
| 350 |
{ |
| 351 |
int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT; |
| 352 |
int padding_locals; |
| 353 |
|
| 354 |
/* Set the space used in the stack by local variables. This is |
| 355 |
rounded up to respect the minimum stack alignment. */ |
| 356 |
cfun->machine->local_vars_size = get_frame_size (); |
| 357 |
|
| 358 |
padding_locals = cfun->machine->local_vars_size % stack_alignment; |
| 359 |
if (padding_locals) |
| 360 |
padding_locals = stack_alignment - padding_locals; |
| 361 |
|
| 362 |
cfun->machine->local_vars_size += padding_locals; |
| 363 |
|
| 364 |
/* Check that the total size of the frame doesn't exceed the limit |
| 365 |
imposed by eBPF. */ |
| 366 |
if (cfun->machine->local_vars_size > bpf_frame_limit) |
| 367 |
{ |
| 368 |
static int stack_limit_exceeded = 0; |
| 369 |
|
| 370 |
if (!stack_limit_exceeded) |
| 371 |
error ("eBPF stack limit exceeded"); |
| 372 |
stack_limit_exceeded = 1; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
#undef TARGET_COMPUTE_FRAME_LAYOUT |
| 377 |
#define TARGET_COMPUTE_FRAME_LAYOUT bpf_compute_frame_layout |
| 378 |
|
| 379 |
/* Defined to initialize data for func_info region in .BTF.ext section. */ |
| 380 |
|
| 381 |
static void |
| 382 |
bpf_function_prologue (FILE *f ATTRIBUTE_UNUSED) |
| 383 |
{ |
| 384 |
if (btf_debuginfo_p ()) |
| 385 |
btf_add_func_info_for (cfun->decl, current_function_func_begin_label); |
| 386 |
} |
| 387 |
|
| 388 |
#undef TARGET_ASM_FUNCTION_PROLOGUE |
| 389 |
#define TARGET_ASM_FUNCTION_PROLOGUE bpf_function_prologue |
| 390 |
|
| 391 |
/* Expand to the instructions in a function prologue. This function |
| 392 |
is called when expanding the 'prologue' pattern in bpf.md. */ |
| 393 |
|
| 394 |
void |
| 395 |
bpf_expand_prologue (void) |
| 396 |
{ |
| 397 |
/* The BPF "hardware" provides a fresh new set of registers for each |
| 398 |
called function, some of which are initialized to the values of |
| 399 |
the arguments passed in the first five registers. In doing so, |
| 400 |
it saves the values of the registers of the caller, and restores |
| 401 |
them upon returning. Therefore, there is no need to save the |
| 402 |
callee-saved registers here. In fact, the kernel implementation |
| 403 |
refuses to run programs in which registers are referred before |
| 404 |
being initialized. */ |
| 405 |
|
| 406 |
/* BPF does not support functions that allocate stack space |
| 407 |
dynamically. This should have been checked already and an error |
| 408 |
emitted. */ |
| 409 |
gcc_assert (!cfun->calls_alloca); |
| 410 |
|
| 411 |
/* If we ever need to have a proper prologue here, please mind the |
| 412 |
`naked' function attribute. */ |
| 413 |
} |
| 414 |
|
| 415 |
/* Expand to the instructions in a function epilogue. This function |
| 416 |
is called when expanding the 'epilogue' pattern in bpf.md. */ |
| 417 |
|
| 418 |
void |
| 419 |
bpf_expand_epilogue (void) |
| 420 |
{ |
| 421 |
/* See note in bpf_expand_prologue for an explanation on why we are |
| 422 |
not restoring callee-saved registers in BPF. */ |
| 423 |
|
| 424 |
if (lookup_attribute ("naked", DECL_ATTRIBUTES (cfun->decl)) != NULL_TREE) |
| 425 |
return; |
| 426 |
emit_jump_insn (gen_exit ()); |
| 427 |
} |
| 428 |
|
| 429 |
/* Expand to the instructions for a conditional branch. This function |
| 430 |
is called when expanding the 'cbranch<mode>4' pattern in bpf.md. */ |
| 431 |
|
| 432 |
void |
| 433 |
bpf_expand_cbranch (machine_mode mode, rtx *operands) |
| 434 |
{ |
| 435 |
/* If all jump instructions are available, nothing special to do here. */ |
| 436 |
if (bpf_has_jmpext) |
| 437 |
return; |
| 438 |
|
| 439 |
enum rtx_code code = GET_CODE (operands[0]); |
| 440 |
|
| 441 |
/* Without the conditional branch instructions jslt, jsle, jlt, jle, we need |
| 442 |
to convert conditional branches that would use them to an available |
| 443 |
operation instead by reversing the comparison. */ |
| 444 |
if ((code == LT || code == LE || code == LTU || code == LEU)) |
| 445 |
{ |
| 446 |
/* Reverse the condition. */ |
| 447 |
PUT_CODE (operands[0], reverse_condition (code)); |
| 448 |
|
| 449 |
/* Swap the operands, and ensure that the first is a register. */ |
| 450 |
if (!register_operand (operands[2], mode)) |
| 451 |
operands[2] = force_reg (mode, operands[2]); |
| 452 |
|
| 453 |
rtx tmp = operands[1]; |
| 454 |
operands[1] = operands[2]; |
| 455 |
operands[2] = tmp; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
/* Return the initial difference between the specified pair of |
| 460 |
registers. The registers that can figure in FROM, and TO, are |
| 461 |
specified by ELIMINABLE_REGS in bpf.h. |
| 462 |
|
| 463 |
This function is used in the definition of |
| 464 |
INITIAL_ELIMINATION_OFFSET in bpf.h */ |
| 465 |
|
| 466 |
HOST_WIDE_INT |
| 467 |
bpf_initial_elimination_offset (int from, int to) |
| 468 |
{ |
| 469 |
HOST_WIDE_INT ret; |
| 470 |
|
| 471 |
if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM) |
| 472 |
ret = 0; |
| 473 |
else if (from == STACK_POINTER_REGNUM && to == FRAME_POINTER_REGNUM) |
| 474 |
ret = -(cfun->machine->local_vars_size); |
| 475 |
else |
| 476 |
gcc_unreachable (); |
| 477 |
|
| 478 |
return ret; |
| 479 |
} |
| 480 |
|
| 481 |
/* Return the number of consecutive hard registers, starting at |
| 482 |
register number REGNO, required to hold a value of mode MODE. */ |
| 483 |
|
| 484 |
static unsigned int |
| 485 |
bpf_hard_regno_nregs (unsigned int regno ATTRIBUTE_UNUSED, |
| 486 |
enum machine_mode mode) |
| 487 |
{ |
| 488 |
return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD); |
| 489 |
} |
| 490 |
|
| 491 |
#undef TARGET_HARD_REGNO_NREGS |
| 492 |
#define TARGET_HARD_REGNO_NREGS bpf_hard_regno_nregs |
| 493 |
|
| 494 |
/* Return true if it is permissible to store a value of mode MODE in |
| 495 |
hard register number REGNO, or in several registers starting with |
| 496 |
that one. */ |
| 497 |
|
| 498 |
static bool |
| 499 |
bpf_hard_regno_mode_ok (unsigned int regno ATTRIBUTE_UNUSED, |
| 500 |
enum machine_mode mode) |
| 501 |
{ |
| 502 |
switch (mode) |
| 503 |
{ |
| 504 |
case E_SImode: |
| 505 |
case E_DImode: |
| 506 |
case E_HImode: |
| 507 |
case E_QImode: |
| 508 |
case E_TImode: |
| 509 |
case E_SFmode: |
| 510 |
case E_DFmode: |
| 511 |
return true; |
| 512 |
default: |
| 513 |
return false; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
#undef TARGET_HARD_REGNO_MODE_OK |
| 518 |
#define TARGET_HARD_REGNO_MODE_OK bpf_hard_regno_mode_ok |
| 519 |
|
| 520 |
/* Return true if a function must have and use a frame pointer. */ |
| 521 |
|
| 522 |
static bool |
| 523 |
bpf_frame_pointer_required (void) |
| 524 |
{ |
| 525 |
/* We do not have a stack pointer, so we absolutely depend on the |
| 526 |
frame-pointer in order to access the stack... and fishes walk and |
| 527 |
pigs fly glglgl */ |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
#undef TARGET_FRAME_POINTER_REQUIRED |
| 532 |
#define TARGET_FRAME_POINTER_REQUIRED bpf_frame_pointer_required |
| 533 |
|
| 534 |
/* Return `true' if the given RTX X is a valid base for an indirect |
| 535 |
memory access. STRICT has the same meaning than in |
| 536 |
bpf_legitimate_address_p. */ |
| 537 |
|
| 538 |
static inline bool |
| 539 |
bpf_address_base_p (rtx x, bool strict) |
| 540 |
{ |
| 541 |
return (GET_CODE (x) == REG |
| 542 |
&& (REGNO (x) < 11 |
| 543 |
|| (!strict && REGNO (x) >= FIRST_PSEUDO_REGISTER))); |
| 544 |
} |
| 545 |
|
| 546 |
/* Return true if X (a RTX) is a legitimate memory address on the |
| 547 |
target machine for a memory operand of mode MODE. */ |
| 548 |
|
| 549 |
static bool |
| 550 |
bpf_legitimate_address_p (machine_mode mode, |
| 551 |
rtx x, |
| 552 |
bool strict, |
| 553 |
code_helper = ERROR_MARK) |
| 554 |
{ |
| 555 |
switch (GET_CODE (x)) |
| 556 |
{ |
| 557 |
case CONST_INT: |
| 558 |
return (mode == FUNCTION_MODE); |
| 559 |
|
| 560 |
case REG: |
| 561 |
return bpf_address_base_p (x, strict); |
| 562 |
|
| 563 |
case PLUS: |
| 564 |
{ |
| 565 |
/* Accept (PLUS ADDR_BASE CONST_INT), provided CONST_INT fits |
| 566 |
in a signed 16-bit. |
| 567 |
|
| 568 |
Note that LABEL_REF and SYMBOL_REF are not allowed in |
| 569 |
REG+IMM addresses, because it is almost certain they will |
| 570 |
overload the offset field. */ |
| 571 |
|
| 572 |
rtx x0 = XEXP (x, 0); |
| 573 |
rtx x1 = XEXP (x, 1); |
| 574 |
|
| 575 |
if (bpf_address_base_p (x0, strict) && GET_CODE (x1) == CONST_INT) |
| 576 |
return IN_RANGE (INTVAL (x1), -1 - 0x7fff, 0x7fff); |
| 577 |
|
| 578 |
/* Check if any of the PLUS operation operands is a CORE unspec, and at |
| 579 |
least the local value for the offset fits in the 16 bits available |
| 580 |
in the encoding. */ |
| 581 |
if (bpf_address_base_p (x1, strict) |
| 582 |
&& GET_CODE (x0) == UNSPEC && XINT (x0, 1) == UNSPEC_CORE_RELOC) |
| 583 |
return IN_RANGE (INTVAL (XVECEXP (x0, 0, 0)), -1 - 0x7fff, 0x7fff); |
| 584 |
if (bpf_address_base_p (x0, strict) |
| 585 |
&& GET_CODE (x1) == UNSPEC && XINT (x1, 1) == UNSPEC_CORE_RELOC) |
| 586 |
return IN_RANGE (INTVAL (XVECEXP (x1, 0, 0)), -1 - 0x7fff, 0x7fff); |
| 587 |
|
| 588 |
break; |
| 589 |
} |
| 590 |
default: |
| 591 |
break; |
| 592 |
} |
| 593 |
|
| 594 |
return false; |
| 595 |
} |
| 596 |
|
| 597 |
#undef TARGET_LEGITIMATE_ADDRESS_P |
| 598 |
#define TARGET_LEGITIMATE_ADDRESS_P bpf_legitimate_address_p |
| 599 |
|
| 600 |
/* Describe the relative costs of RTL expressions. Return true when |
| 601 |
all subexpressions of X have been processed, and false when |
| 602 |
`rtx_cost' should recurse. */ |
| 603 |
|
| 604 |
static bool |
| 605 |
bpf_rtx_costs (rtx x ATTRIBUTE_UNUSED, |
| 606 |
enum machine_mode mode ATTRIBUTE_UNUSED, |
| 607 |
int outer_code ATTRIBUTE_UNUSED, |
| 608 |
int opno ATTRIBUTE_UNUSED, |
| 609 |
int *total ATTRIBUTE_UNUSED, |
| 610 |
bool speed ATTRIBUTE_UNUSED) |
| 611 |
{ |
| 612 |
/* To be written. */ |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
#undef TARGET_RTX_COSTS |
| 617 |
#define TARGET_RTX_COSTS bpf_rtx_costs |
| 618 |
|
| 619 |
static int |
| 620 |
bpf_insn_cost (rtx_insn *insn, bool speed ATTRIBUTE_UNUSED) |
| 621 |
{ |
| 622 |
rtx pat = PATTERN (insn); |
| 623 |
if(GET_CODE (pat) == SET |
| 624 |
&& GET_CODE (XEXP (pat, 1)) == UNSPEC |
| 625 |
&& XINT (XEXP (pat, 1), 1) == UNSPEC_CORE_RELOC) |
| 626 |
return COSTS_N_INSNS (100); |
| 627 |
|
| 628 |
return COSTS_N_INSNS (1); |
| 629 |
} |
| 630 |
|
| 631 |
#undef TARGET_INSN_COST |
| 632 |
#define TARGET_INSN_COST bpf_insn_cost |
| 633 |
|
| 634 |
/* Return true if an argument at the position indicated by CUM should |
| 635 |
be passed by reference. If the hook returns true, a copy of that |
| 636 |
argument is made in memory and a pointer to the argument is passed |
| 637 |
instead of the argument itself. */ |
| 638 |
|
| 639 |
static bool |
| 640 |
bpf_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED, |
| 641 |
const function_arg_info &arg) |
| 642 |
{ |
| 643 |
unsigned num_bytes = arg.type_size_in_bytes (); |
| 644 |
|
| 645 |
/* Pass aggregates and values bigger than 5 words by reference. |
| 646 |
Everything else is passed by copy. */ |
| 647 |
return (arg.aggregate_type_p () || (num_bytes > 8*5)); |
| 648 |
} |
| 649 |
|
| 650 |
#undef TARGET_PASS_BY_REFERENCE |
| 651 |
#define TARGET_PASS_BY_REFERENCE bpf_pass_by_reference |
| 652 |
|
| 653 |
/* Return a RTX indicating whether a function argument is passed in a |
| 654 |
register and if so, which register. */ |
| 655 |
|
| 656 |
static rtx |
| 657 |
bpf_function_arg (cumulative_args_t ca, const function_arg_info &arg) |
| 658 |
{ |
| 659 |
CUMULATIVE_ARGS *cum = get_cumulative_args (ca); |
| 660 |
|
| 661 |
if (*cum < 5) |
| 662 |
return gen_rtx_REG (arg.mode, *cum + 1); |
| 663 |
else |
| 664 |
/* An error will be emitted for this in |
| 665 |
bpf_function_arg_advance. */ |
| 666 |
return NULL_RTX; |
| 667 |
} |
| 668 |
|
| 669 |
#undef TARGET_FUNCTION_ARG |
| 670 |
#define TARGET_FUNCTION_ARG bpf_function_arg |
| 671 |
|
| 672 |
/* Update the summarizer variable pointed by CA to advance past an |
| 673 |
argument in the argument list. */ |
| 674 |
|
| 675 |
static void |
| 676 |
bpf_function_arg_advance (cumulative_args_t ca, |
| 677 |
const function_arg_info &arg) |
| 678 |
{ |
| 679 |
CUMULATIVE_ARGS *cum = get_cumulative_args (ca); |
| 680 |
unsigned num_bytes = arg.type_size_in_bytes (); |
| 681 |
unsigned num_words = CEIL (num_bytes, UNITS_PER_WORD); |
| 682 |
|
| 683 |
if (*cum <= 5 && *cum + num_words > 5) |
| 684 |
{ |
| 685 |
/* Too many arguments for BPF. However, if the function is |
| 686 |
gonna be inline for sure, we let it pass. Otherwise, issue |
| 687 |
an error. */ |
| 688 |
if (!lookup_attribute ("always_inline", |
| 689 |
DECL_ATTRIBUTES (cfun->decl))) |
| 690 |
error ("too many function arguments for eBPF"); |
| 691 |
} |
| 692 |
|
| 693 |
*cum += num_words; |
| 694 |
} |
| 695 |
|
| 696 |
#undef TARGET_FUNCTION_ARG_ADVANCE |
| 697 |
#define TARGET_FUNCTION_ARG_ADVANCE bpf_function_arg_advance |
| 698 |
|
| 699 |
/* Output the assembly code for a constructor. Since eBPF doesn't |
| 700 |
support indirect calls, constructors are not supported. */ |
| 701 |
|
| 702 |
static void |
| 703 |
bpf_output_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
| 704 |
{ |
| 705 |
tree decl = SYMBOL_REF_DECL (symbol); |
| 706 |
|
| 707 |
if (decl) |
| 708 |
sorry_at (DECL_SOURCE_LOCATION (decl), |
| 709 |
"no constructors"); |
| 710 |
else |
| 711 |
sorry ("no constructors"); |
| 712 |
} |
| 713 |
|
| 714 |
#undef TARGET_ASM_CONSTRUCTOR |
| 715 |
#define TARGET_ASM_CONSTRUCTOR bpf_output_constructor |
| 716 |
|
| 717 |
/* Output the assembly code for a destructor. Since eBPF doesn't |
| 718 |
support indirect calls, destructors are not supported. */ |
| 719 |
|
| 720 |
static void |
| 721 |
bpf_output_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
| 722 |
{ |
| 723 |
tree decl = SYMBOL_REF_DECL (symbol); |
| 724 |
|
| 725 |
if (decl) |
| 726 |
sorry_at (DECL_SOURCE_LOCATION (decl), |
| 727 |
"no destructors"); |
| 728 |
else |
| 729 |
sorry ("no destructors"); |
| 730 |
} |
| 731 |
|
| 732 |
#undef TARGET_ASM_DESTRUCTOR |
| 733 |
#define TARGET_ASM_DESTRUCTOR bpf_output_destructor |
| 734 |
|
| 735 |
/* Return the appropriate instruction to CALL to a function. TARGET |
| 736 |
is an RTX denoting the address of the called function. |
| 737 |
|
| 738 |
The main purposes of this function are: |
| 739 |
- To reject indirect CALL instructions, which are not supported by |
| 740 |
eBPF. |
| 741 |
- To recognize calls to kernel helper functions and emit the |
| 742 |
corresponding CALL N instruction. |
| 743 |
|
| 744 |
This function is called from the expansion of the 'call' pattern in |
| 745 |
bpf.md. */ |
| 746 |
|
| 747 |
const char * |
| 748 |
bpf_output_call (rtx target) |
| 749 |
{ |
| 750 |
rtx xops[1]; |
| 751 |
|
| 752 |
switch (GET_CODE (target)) |
| 753 |
{ |
| 754 |
case CONST_INT: |
| 755 |
output_asm_insn ("call\t%0", &target); |
| 756 |
break; |
| 757 |
case SYMBOL_REF: |
| 758 |
{ |
| 759 |
tree decl = SYMBOL_REF_DECL (target); |
| 760 |
tree attr; |
| 761 |
|
| 762 |
if (decl |
| 763 |
&& (attr = lookup_attribute ("kernel_helper", |
| 764 |
DECL_ATTRIBUTES (decl)))) |
| 765 |
{ |
| 766 |
tree attr_args = TREE_VALUE (attr); |
| 767 |
|
| 768 |
xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args))); |
| 769 |
output_asm_insn ("call\t%0", xops); |
| 770 |
} |
| 771 |
else |
| 772 |
output_asm_insn ("call\t%0", &target); |
| 773 |
|
| 774 |
break; |
| 775 |
} |
| 776 |
default: |
| 777 |
if (TARGET_XBPF) |
| 778 |
output_asm_insn ("call\t%0", &target); |
| 779 |
else |
| 780 |
{ |
| 781 |
error ("indirect call in function, which are not supported by eBPF"); |
| 782 |
output_asm_insn ("call 0", NULL); |
| 783 |
} |
| 784 |
break; |
| 785 |
} |
| 786 |
|
| 787 |
return ""; |
| 788 |
} |
| 789 |
|
| 790 |
const char * |
| 791 |
bpf_output_move (rtx *operands, const char *templ) |
| 792 |
{ |
| 793 |
bpf_output_core_reloc (operands, 2); |
| 794 |
return templ; |
| 795 |
} |
| 796 |
|
| 797 |
/* Print register name according to assembly dialect. In normal |
| 798 |
syntax registers are printed like %rN where N is the register |
| 799 |
number. |
| 800 |
|
| 801 |
In pseudoc syntax, the register names do not feature a '%' prefix. |
| 802 |
Additionally, the code 'w' denotes that the register should be |
| 803 |
printed as wN instead of rN, where N is the register number, but |
| 804 |
only when the value stored in the operand OP is 32-bit wide. |
| 805 |
Finally, the code 'W' denotes that the register should be printed |
| 806 |
as wN instead of rN, in all cases, regardless of the mode of the |
| 807 |
value stored in the operand. */ |
| 808 |
|
| 809 |
static void |
| 810 |
bpf_print_register (FILE *file, rtx op, int code) |
| 811 |
{ |
| 812 |
if(asm_dialect == ASM_NORMAL) |
| 813 |
fprintf (file, "%s", reg_names[REGNO (op)]); |
| 814 |
else |
| 815 |
{ |
| 816 |
if (code == 'W' || (code == 'w' && GET_MODE_SIZE (GET_MODE (op)) <= 4)) |
| 817 |
{ |
| 818 |
if (REGNO (op) == BPF_FP) |
| 819 |
fprintf (file, "w10"); |
| 820 |
else |
| 821 |
fprintf (file, "w%s", reg_names[REGNO (op)]+2); |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
if (REGNO (op) == BPF_FP) |
| 826 |
fprintf (file, "r10"); |
| 827 |
else |
| 828 |
fprintf (file, "%s", reg_names[REGNO (op)]+1); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
/* Variable defined to implement 'M' operand modifier for the special cases |
| 834 |
where the parentheses should not be printed surrounding a memory address |
| 835 |
operand. */ |
| 836 |
static bool no_parentheses_mem_operand; |
| 837 |
|
| 838 |
/* Print an instruction operand. This function is called in the macro |
| 839 |
PRINT_OPERAND defined in bpf.h */ |
| 840 |
|
| 841 |
void |
| 842 |
bpf_print_operand (FILE *file, rtx op, int code) |
| 843 |
{ |
| 844 |
switch (GET_CODE (op)) |
| 845 |
{ |
| 846 |
case REG: |
| 847 |
bpf_print_register (file, op, code); |
| 848 |
break; |
| 849 |
case MEM: |
| 850 |
no_parentheses_mem_operand = (code == 'M'); |
| 851 |
output_address (GET_MODE (op), XEXP (op, 0)); |
| 852 |
break; |
| 853 |
case CONST_DOUBLE: |
| 854 |
if (GET_MODE (op) == VOIDmode) |
| 855 |
{ |
| 856 |
if (CONST_DOUBLE_HIGH (op)) |
| 857 |
fprintf (file, HOST_WIDE_INT_PRINT_DOUBLE_HEX, |
| 858 |
CONST_DOUBLE_HIGH (op), CONST_DOUBLE_LOW (op)); |
| 859 |
else if (CONST_DOUBLE_LOW (op) < 0) |
| 860 |
fprintf (file, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_LOW (op)); |
| 861 |
else |
| 862 |
fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (op)); |
| 863 |
} |
| 864 |
else |
| 865 |
{ |
| 866 |
long vals[2]; |
| 867 |
real_to_target (vals, CONST_DOUBLE_REAL_VALUE (op), GET_MODE (op)); |
| 868 |
vals[0] &= 0xffffffff; |
| 869 |
vals[1] &= 0xffffffff; |
| 870 |
if (GET_MODE (op) == SFmode) |
| 871 |
fprintf (file, "0x%08lx", vals[0]); |
| 872 |
else if (GET_MODE (op) == DFmode) |
| 873 |
{ |
| 874 |
/* Note: real_to_target puts vals in target word order. */ |
| 875 |
if (WORDS_BIG_ENDIAN) |
| 876 |
fprintf (file, "0x%08lx%08lx", vals[0], vals[1]); |
| 877 |
else |
| 878 |
fprintf (file, "0x%08lx%08lx", vals[1], vals[0]); |
| 879 |
} |
| 880 |
else |
| 881 |
gcc_unreachable (); |
| 882 |
} |
| 883 |
break; |
| 884 |
case UNSPEC: |
| 885 |
if (XINT (op, 1) == UNSPEC_CORE_RELOC) |
| 886 |
bpf_print_operand (file, XVECEXP (op, 0, 0), code); |
| 887 |
else |
| 888 |
gcc_unreachable (); |
| 889 |
break; |
| 890 |
default: |
| 891 |
output_addr_const (file, op); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
#define PAREN_OPEN (asm_dialect == ASM_NORMAL ? "[" : no_parentheses_mem_operand ? "" : "(") |
| 896 |
#define PAREN_CLOSE (asm_dialect == ASM_NORMAL ? "]" : no_parentheses_mem_operand ? "" : ")") |
| 897 |
|
| 898 |
/* Print an operand which is an address. This function should handle |
| 899 |
any legit address, as accepted by bpf_legitimate_address_p, and |
| 900 |
also addresses that are valid in CALL instructions. |
| 901 |
|
| 902 |
This function is called in the PRINT_OPERAND_ADDRESS macro defined |
| 903 |
in bpf.h */ |
| 904 |
|
| 905 |
void |
| 906 |
bpf_print_operand_address (FILE *file, rtx addr) |
| 907 |
{ |
| 908 |
switch (GET_CODE (addr)) |
| 909 |
{ |
| 910 |
case REG: |
| 911 |
fprintf (file, "%s", PAREN_OPEN); |
| 912 |
bpf_print_register (file, addr, 0); |
| 913 |
fprintf (file, "+0%s", PAREN_CLOSE); |
| 914 |
break; |
| 915 |
case PLUS: |
| 916 |
{ |
| 917 |
rtx op0 = XEXP (addr, 0); |
| 918 |
rtx op1 = XEXP (addr, 1); |
| 919 |
|
| 920 |
if (GET_CODE (op1) == REG) { |
| 921 |
op0 = op1; |
| 922 |
op1 = XEXP (addr, 0); |
| 923 |
} |
| 924 |
|
| 925 |
if (GET_CODE (op0) == REG |
| 926 |
&& (GET_CODE (op1) == CONST_INT |
| 927 |
|| (GET_CODE (op1) == UNSPEC |
| 928 |
&& XINT (op1, 1) == UNSPEC_CORE_RELOC))) |
| 929 |
{ |
| 930 |
fprintf (file, "%s", PAREN_OPEN); |
| 931 |
bpf_print_register (file, op0, 0); |
| 932 |
fprintf (file, "+"); |
| 933 |
if (GET_CODE (op1) == UNSPEC) |
| 934 |
output_addr_const (file, XVECEXP (op1, 0, 0)); |
| 935 |
else |
| 936 |
output_addr_const (file, op1); |
| 937 |
fprintf (file, "%s", PAREN_CLOSE); |
| 938 |
} |
| 939 |
else |
| 940 |
fatal_insn ("invalid address in operand", addr); |
| 941 |
break; |
| 942 |
} |
| 943 |
case MEM: |
| 944 |
/* Fallthrough. */ |
| 945 |
case LABEL_REF: |
| 946 |
/* Fallthrough. */ |
| 947 |
fatal_insn ("unsupported operand", addr); |
| 948 |
break; |
| 949 |
default: |
| 950 |
output_addr_const (file, addr); |
| 951 |
break; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
#undef PAREN_OPEN |
| 956 |
#undef PAREN_CLOSE |
| 957 |
|
| 958 |
/* Add a BPF builtin function with NAME, CODE and TYPE. Return |
| 959 |
the function decl or NULL_TREE if the builtin was not added. */ |
| 960 |
|
| 961 |
static inline tree |
| 962 |
def_builtin (const char *name, enum bpf_builtins code, tree type) |
| 963 |
{ |
| 964 |
tree t |
| 965 |
= add_builtin_function (name, type, code, BUILT_IN_MD, NULL, NULL); |
| 966 |
|
| 967 |
bpf_builtins[code] = t; |
| 968 |
|
| 969 |
return t; |
| 970 |
} |
| 971 |
|
| 972 |
/* Define machine-specific built-in functions. */ |
| 973 |
|
| 974 |
static void |
| 975 |
bpf_init_builtins (void) |
| 976 |
{ |
| 977 |
tree ullt = long_long_unsigned_type_node; |
| 978 |
|
| 979 |
/* Built-ins for BPF_LD_ABS and BPF_LD_IND instructions. */ |
| 980 |
|
| 981 |
def_builtin ("__builtin_bpf_load_byte", BPF_BUILTIN_LOAD_BYTE, |
| 982 |
build_function_type_list (ullt, ullt, 0)); |
| 983 |
def_builtin ("__builtin_bpf_load_half", BPF_BUILTIN_LOAD_HALF, |
| 984 |
build_function_type_list (ullt, ullt, 0)); |
| 985 |
def_builtin ("__builtin_bpf_load_word", BPF_BUILTIN_LOAD_WORD, |
| 986 |
build_function_type_list (ullt, ullt, 0)); |
| 987 |
|
| 988 |
def_builtin ("__builtin_preserve_access_index", |
| 989 |
BPF_BUILTIN_PRESERVE_ACCESS_INDEX, |
| 990 |
build_function_type_list (ptr_type_node, ptr_type_node, 0)); |
| 991 |
def_builtin ("__builtin_preserve_field_info", |
| 992 |
BPF_BUILTIN_PRESERVE_FIELD_INFO, |
| 993 |
build_function_type_list (unsigned_type_node, ptr_type_node, |
| 994 |
unsigned_type_node, 0)); |
| 995 |
def_builtin ("__builtin_btf_type_id", |
| 996 |
BPF_BUILTIN_BTF_TYPE_ID, |
| 997 |
build_function_type_list (integer_type_node, ptr_type_node, |
| 998 |
integer_type_node, 0)); |
| 999 |
def_builtin ("__builtin_preserve_type_info", |
| 1000 |
BPF_BUILTIN_PRESERVE_TYPE_INFO, |
| 1001 |
build_function_type_list (integer_type_node, ptr_type_node, |
| 1002 |
integer_type_node, 0)); |
| 1003 |
def_builtin ("__builtin_preserve_enum_value", |
| 1004 |
BPF_BUILTIN_PRESERVE_ENUM_VALUE, |
| 1005 |
build_function_type_list (integer_type_node, ptr_type_node, |
| 1006 |
integer_type_node, integer_type_node, |
| 1007 |
0)); |
| 1008 |
|
| 1009 |
def_builtin ("__builtin_core_reloc", |
| 1010 |
BPF_BUILTIN_CORE_RELOC, |
| 1011 |
build_function_type_list (integer_type_node,integer_type_node, |
| 1012 |
0)); |
| 1013 |
DECL_PURE_P (bpf_builtins[BPF_BUILTIN_CORE_RELOC]) = 1; |
| 1014 |
TREE_NOTHROW (bpf_builtins[BPF_BUILTIN_CORE_RELOC]) = 1; |
| 1015 |
|
| 1016 |
bpf_init_core_builtins (); |
| 1017 |
} |
| 1018 |
|
| 1019 |
#undef TARGET_INIT_BUILTINS |
| 1020 |
#define TARGET_INIT_BUILTINS bpf_init_builtins |
| 1021 |
|
| 1022 |
/* Expand a call to a BPF-specific built-in function that was set up |
| 1023 |
with bpf_init_builtins. */ |
| 1024 |
|
| 1025 |
static rtx |
| 1026 |
bpf_expand_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, |
| 1027 |
rtx subtarget ATTRIBUTE_UNUSED, |
| 1028 |
machine_mode mode ATTRIBUTE_UNUSED, |
| 1029 |
int ignore ATTRIBUTE_UNUSED) |
| 1030 |
{ |
| 1031 |
tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0); |
| 1032 |
int code = DECL_MD_FUNCTION_CODE (fndecl); |
| 1033 |
|
| 1034 |
if (code == BPF_BUILTIN_LOAD_BYTE |
| 1035 |
|| code == BPF_BUILTIN_LOAD_HALF |
| 1036 |
|| code == BPF_BUILTIN_LOAD_WORD) |
| 1037 |
{ |
| 1038 |
/* Expand an indirect load from the sk_buff in the context. |
| 1039 |
There is just one argument to the builtin, which is the |
| 1040 |
offset. |
| 1041 |
|
| 1042 |
We try first to expand a ldabs* instruction. In case this |
| 1043 |
fails, we try a ldind* instruction. */ |
| 1044 |
|
| 1045 |
enum insn_code abs_icode |
| 1046 |
= (code == BPF_BUILTIN_LOAD_BYTE ? CODE_FOR_ldabsb |
| 1047 |
: code == BPF_BUILTIN_LOAD_HALF ? CODE_FOR_ldabsh |
| 1048 |
: CODE_FOR_ldabsw); |
| 1049 |
|
| 1050 |
enum insn_code ind_icode |
| 1051 |
= (code == BPF_BUILTIN_LOAD_BYTE ? CODE_FOR_ldindb |
| 1052 |
: code == BPF_BUILTIN_LOAD_HALF ? CODE_FOR_ldindh |
| 1053 |
: CODE_FOR_ldindw); |
| 1054 |
|
| 1055 |
tree offset_arg = CALL_EXPR_ARG (exp, 0); |
| 1056 |
struct expand_operand ops[2]; |
| 1057 |
|
| 1058 |
create_input_operand (&ops[0], expand_normal (offset_arg), |
| 1059 |
TYPE_MODE (TREE_TYPE (offset_arg))); |
| 1060 |
create_input_operand (&ops[1], const0_rtx, SImode); |
| 1061 |
|
| 1062 |
if (!maybe_expand_insn (abs_icode, 2, ops) |
| 1063 |
&& !maybe_expand_insn (ind_icode, 2, ops)) |
| 1064 |
{ |
| 1065 |
error ("invalid argument to built-in function"); |
| 1066 |
return gen_rtx_REG (ops[0].mode, BPF_R0); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* The result of the load is in R0. */ |
| 1070 |
return gen_rtx_REG (ops[0].mode, BPF_R0); |
| 1071 |
} |
| 1072 |
else |
| 1073 |
{ |
| 1074 |
rtx ret = bpf_expand_core_builtin (exp, (enum bpf_builtins) code); |
| 1075 |
if (ret != NULL_RTX) |
| 1076 |
return ret; |
| 1077 |
} |
| 1078 |
|
| 1079 |
error ("invalid built-in function at expansion"); |
| 1080 |
gcc_unreachable (); |
| 1081 |
} |
| 1082 |
|
| 1083 |
#undef TARGET_EXPAND_BUILTIN |
| 1084 |
#define TARGET_EXPAND_BUILTIN bpf_expand_builtin |
| 1085 |
|
| 1086 |
static tree |
| 1087 |
bpf_resolve_overloaded_builtin (location_t loc, tree fndecl, void *arglist, |
| 1088 |
bool complain ATTRIBUTE_UNUSED) |
| 1089 |
{ |
| 1090 |
int code = DECL_MD_FUNCTION_CODE (fndecl); |
| 1091 |
if (code > BPF_CORE_BUILTINS_MARKER) |
| 1092 |
return bpf_resolve_overloaded_core_builtin (loc, fndecl, arglist); |
| 1093 |
else |
| 1094 |
return NULL_TREE; |
| 1095 |
} |
| 1096 |
|
| 1097 |
#undef TARGET_RESOLVE_OVERLOADED_BUILTIN |
| 1098 |
#define TARGET_RESOLVE_OVERLOADED_BUILTIN bpf_resolve_overloaded_builtin |
| 1099 |
|
| 1100 |
static rtx |
| 1101 |
bpf_delegitimize_address (rtx rtl) |
| 1102 |
{ |
| 1103 |
if (GET_CODE (rtl) == UNSPEC |
| 1104 |
&& XINT (rtl, 1) == UNSPEC_CORE_RELOC) |
| 1105 |
return XVECEXP (rtl, 0, 0); |
| 1106 |
|
| 1107 |
return rtl; |
| 1108 |
} |
| 1109 |
|
| 1110 |
#undef TARGET_DELEGITIMIZE_ADDRESS |
| 1111 |
#define TARGET_DELEGITIMIZE_ADDRESS bpf_delegitimize_address |
| 1112 |
|
| 1113 |
/* Initialize target-specific function library calls. This is mainly |
| 1114 |
used to call library-provided soft-fp operations, since eBPF |
| 1115 |
doesn't support floating-point in "hardware". */ |
| 1116 |
|
| 1117 |
static void |
| 1118 |
bpf_init_libfuncs (void) |
| 1119 |
{ |
| 1120 |
set_conv_libfunc (sext_optab, DFmode, SFmode, |
| 1121 |
"__bpf_extendsfdf2"); |
| 1122 |
set_conv_libfunc (trunc_optab, SFmode, DFmode, |
| 1123 |
"__bpf_truncdfsf2"); |
| 1124 |
set_conv_libfunc (sfix_optab, SImode, DFmode, |
| 1125 |
"__bpf_fix_truncdfsi"); |
| 1126 |
set_conv_libfunc (sfloat_optab, DFmode, SImode, |
| 1127 |
"__bpf_floatsidf"); |
| 1128 |
set_conv_libfunc (ufloat_optab, DFmode, SImode, |
| 1129 |
"__bpf_floatunsidf"); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#undef TARGET_INIT_LIBFUNCS |
| 1133 |
#define TARGET_INIT_LIBFUNCS bpf_init_libfuncs |
| 1134 |
|
| 1135 |
/* Define the mechanism that will be used for describing frame unwind |
| 1136 |
information to the debugger. In eBPF it is not possible to unwind |
| 1137 |
frames. */ |
| 1138 |
|
| 1139 |
static enum unwind_info_type |
| 1140 |
bpf_debug_unwind_info () |
| 1141 |
{ |
| 1142 |
return UI_NONE; |
| 1143 |
} |
| 1144 |
|
| 1145 |
#undef TARGET_DEBUG_UNWIND_INFO |
| 1146 |
#define TARGET_DEBUG_UNWIND_INFO bpf_debug_unwind_info |
| 1147 |
|
| 1148 |
/* Output assembly directives to assemble data of various sized and |
| 1149 |
alignments. */ |
| 1150 |
|
| 1151 |
#undef TARGET_ASM_BYTE_OP |
| 1152 |
#define TARGET_ASM_BYTE_OP "\t.byte\t" |
| 1153 |
#undef TARGET_ASM_ALIGNED_HI_OP |
| 1154 |
#define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" |
| 1155 |
#undef TARGET_ASM_ALIGNED_SI_OP |
| 1156 |
#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t" |
| 1157 |
#undef TARGET_ASM_ALIGNED_DI_OP |
| 1158 |
#define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t" |
| 1159 |
|
| 1160 |
/* Implement target hook TARGET_ASM_NAMED_SECTION. */ |
| 1161 |
|
| 1162 |
static void |
| 1163 |
bpf_asm_named_section (const char *name, unsigned int flags, |
| 1164 |
tree decl) |
| 1165 |
{ |
| 1166 |
/* In BPF section names are used to encode the kind of BPF program |
| 1167 |
and other metadata, involving all sort of non alphanumeric |
| 1168 |
characters. This includes for example names like /foo//bar/baz. |
| 1169 |
This makes it necessary to quote section names to make sure the |
| 1170 |
assembler doesn't get confused. For example, the example above |
| 1171 |
would be interpreted unqouted as a section name "/foo" followed |
| 1172 |
by a line comment "//bar/baz". |
| 1173 |
|
| 1174 |
Note that we only quote the section name if it contains any |
| 1175 |
character not in the set [0-9a-zA-Z_]. This is because |
| 1176 |
default_elf_asm_named_section generally expects unquoted names |
| 1177 |
and checks for particular names like |
| 1178 |
__patchable_function_entries. */ |
| 1179 |
|
| 1180 |
bool needs_quoting = false; |
| 1181 |
|
| 1182 |
for (const char *p = name; *p != '\0'; ++p) |
| 1183 |
if (!(*p == '_' |
| 1184 |
|| (*p >= '0' && *p <= '9') |
| 1185 |
|| (*p >= 'a' && *p <= 'z') |
| 1186 |
|| (*p >= 'A' && *p <= 'Z'))) |
| 1187 |
needs_quoting = true; |
| 1188 |
|
| 1189 |
if (needs_quoting) |
| 1190 |
{ |
| 1191 |
char *quoted_name |
| 1192 |
= (char *) xcalloc (1, strlen (name) * 2 + 2); |
| 1193 |
char *q = quoted_name; |
| 1194 |
|
| 1195 |
*(q++) = '"'; |
| 1196 |
for (const char *p = name; *p != '\0'; ++p) |
| 1197 |
{ |
| 1198 |
if (*p == '"' || *p == '\\') |
| 1199 |
*(q++) = '\\'; |
| 1200 |
*(q++) = *p; |
| 1201 |
} |
| 1202 |
*(q++) = '"'; |
| 1203 |
*(q++) = '\0'; |
| 1204 |
|
| 1205 |
default_elf_asm_named_section (quoted_name, flags, decl); |
| 1206 |
free (quoted_name); |
| 1207 |
} |
| 1208 |
else |
| 1209 |
default_elf_asm_named_section (name, flags, decl); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#undef TARGET_ASM_NAMED_SECTION |
| 1213 |
#define TARGET_ASM_NAMED_SECTION bpf_asm_named_section |
| 1214 |
|
| 1215 |
/* Implement target hook small_register_classes_for_mode_p. */ |
| 1216 |
|
| 1217 |
static bool |
| 1218 |
bpf_small_register_classes_for_mode_p (machine_mode mode) |
| 1219 |
{ |
| 1220 |
if (TARGET_XBPF) |
| 1221 |
return 1; |
| 1222 |
else |
| 1223 |
/* Avoid putting function addresses in registers, as calling these |
| 1224 |
is not supported in eBPF. */ |
| 1225 |
return (mode != FUNCTION_MODE); |
| 1226 |
} |
| 1227 |
|
| 1228 |
#undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P |
| 1229 |
#define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \ |
| 1230 |
bpf_small_register_classes_for_mode_p |
| 1231 |
|
| 1232 |
static bool |
| 1233 |
bpf_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size, |
| 1234 |
unsigned int align ATTRIBUTE_UNUSED, |
| 1235 |
enum by_pieces_operation op, |
| 1236 |
bool speed_p) |
| 1237 |
{ |
| 1238 |
if (op != COMPARE_BY_PIECES) |
| 1239 |
return default_use_by_pieces_infrastructure_p (size, align, op, speed_p); |
| 1240 |
|
| 1241 |
return size <= COMPARE_MAX_PIECES; |
| 1242 |
} |
| 1243 |
|
| 1244 |
#undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P |
| 1245 |
#define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \ |
| 1246 |
bpf_use_by_pieces_infrastructure_p |
| 1247 |
|
| 1248 |
/* Helper for bpf_expand_cpymem. Emit an unrolled loop moving the bytes |
| 1249 |
from SRC to DST. */ |
| 1250 |
|
| 1251 |
static void |
| 1252 |
emit_move_loop (rtx src, rtx dst, machine_mode mode, int offset, int inc, |
| 1253 |
unsigned iters, unsigned remainder) |
| 1254 |
{ |
| 1255 |
rtx reg = gen_reg_rtx (mode); |
| 1256 |
|
| 1257 |
/* First copy in chunks as large as alignment permits. */ |
| 1258 |
for (unsigned int i = 0; i < iters; i++) |
| 1259 |
{ |
| 1260 |
emit_move_insn (reg, adjust_address (src, mode, offset)); |
| 1261 |
emit_move_insn (adjust_address (dst, mode, offset), reg); |
| 1262 |
offset += inc; |
| 1263 |
} |
| 1264 |
|
| 1265 |
/* Handle remaining bytes which might be smaller than the chunks |
| 1266 |
used above. */ |
| 1267 |
if (remainder & 4) |
| 1268 |
{ |
| 1269 |
emit_move_insn (reg, adjust_address (src, SImode, offset)); |
| 1270 |
emit_move_insn (adjust_address (dst, SImode, offset), reg); |
| 1271 |
offset += (inc < 0 ? -4 : 4); |
| 1272 |
remainder -= 4; |
| 1273 |
} |
| 1274 |
if (remainder & 2) |
| 1275 |
{ |
| 1276 |
emit_move_insn (reg, adjust_address (src, HImode, offset)); |
| 1277 |
emit_move_insn (adjust_address (dst, HImode, offset), reg); |
| 1278 |
offset += (inc < 0 ? -2 : 2); |
| 1279 |
remainder -= 2; |
| 1280 |
} |
| 1281 |
if (remainder & 1) |
| 1282 |
{ |
| 1283 |
emit_move_insn (reg, adjust_address (src, QImode, offset)); |
| 1284 |
emit_move_insn (adjust_address (dst, QImode, offset), reg); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
/* Expand cpymem/movmem, as from __builtin_memcpy/memmove. |
| 1289 |
OPERANDS are the same as the cpymem/movmem patterns. |
| 1290 |
IS_MOVE is true if this is a memmove, false for memcpy. |
| 1291 |
Return true if we successfully expanded, or false if we cannot |
| 1292 |
and must punt to a libcall. */ |
| 1293 |
|
| 1294 |
bool |
| 1295 |
bpf_expand_cpymem (rtx *operands, bool is_move) |
| 1296 |
{ |
| 1297 |
/* Size must be constant for this expansion to work. */ |
| 1298 |
const char *name = is_move ? "memmove" : "memcpy"; |
| 1299 |
if (!CONST_INT_P (operands[2])) |
| 1300 |
{ |
| 1301 |
if (flag_building_libgcc) |
| 1302 |
warning (0, "could not inline call to %<__builtin_%s%>: " |
| 1303 |
"size must be constant", name); |
| 1304 |
else |
| 1305 |
error ("could not inline call to %<__builtin_%s%>: " |
| 1306 |
"size must be constant", name); |
| 1307 |
return false; |
| 1308 |
} |
| 1309 |
|
| 1310 |
/* Alignment is a CONST_INT. */ |
| 1311 |
gcc_assert (CONST_INT_P (operands[3])); |
| 1312 |
|
| 1313 |
rtx dst = operands[0]; |
| 1314 |
rtx src = operands[1]; |
| 1315 |
rtx size = operands[2]; |
| 1316 |
unsigned HOST_WIDE_INT size_bytes = UINTVAL (size); |
| 1317 |
unsigned align = UINTVAL (operands[3]); |
| 1318 |
enum machine_mode mode; |
| 1319 |
switch (align) |
| 1320 |
{ |
| 1321 |
case 1: mode = QImode; break; |
| 1322 |
case 2: mode = HImode; break; |
| 1323 |
case 4: mode = SImode; break; |
| 1324 |
case 8: mode = DImode; break; |
| 1325 |
default: |
| 1326 |
gcc_unreachable (); |
| 1327 |
} |
| 1328 |
|
| 1329 |
/* For sizes above threshold, always use a libcall. */ |
| 1330 |
if (size_bytes > (unsigned HOST_WIDE_INT) bpf_inline_memops_threshold) |
| 1331 |
{ |
| 1332 |
if (flag_building_libgcc) |
| 1333 |
warning (0, "could not inline call to %<__builtin_%s%>: " |
| 1334 |
"too many bytes, use %<-minline-memops-threshold%>", name); |
| 1335 |
else |
| 1336 |
error ("could not inline call to %<__builtin_%s%>: " |
| 1337 |
"too many bytes, use %<-minline-memops-threshold%>", name); |
| 1338 |
return false; |
| 1339 |
} |
| 1340 |
|
| 1341 |
unsigned iters = size_bytes >> ceil_log2 (align); |
| 1342 |
unsigned remainder = size_bytes & (align - 1); |
| 1343 |
|
| 1344 |
int inc = GET_MODE_SIZE (mode); |
| 1345 |
rtx_code_label *fwd_label, *done_label; |
| 1346 |
if (is_move) |
| 1347 |
{ |
| 1348 |
/* For memmove, be careful of overlap. It is not a concern for memcpy. |
| 1349 |
To handle overlap, we check (at runtime) if SRC < DST, and if so do |
| 1350 |
the move "backwards" starting from SRC + SIZE. */ |
| 1351 |
fwd_label = gen_label_rtx (); |
| 1352 |
done_label = gen_label_rtx (); |
| 1353 |
|
| 1354 |
rtx dst_addr = copy_to_mode_reg (Pmode, XEXP (dst, 0)); |
| 1355 |
rtx src_addr = copy_to_mode_reg (Pmode, XEXP (src, 0)); |
| 1356 |
emit_cmp_and_jump_insns (src_addr, dst_addr, GEU, NULL_RTX, Pmode, |
| 1357 |
true, fwd_label, profile_probability::even ()); |
| 1358 |
|
| 1359 |
/* Emit the "backwards" unrolled loop. */ |
| 1360 |
emit_move_loop (src, dst, mode, size_bytes, -inc, iters, remainder); |
| 1361 |
emit_jump_insn (gen_jump (done_label)); |
| 1362 |
emit_barrier (); |
| 1363 |
|
| 1364 |
emit_label (fwd_label); |
| 1365 |
} |
| 1366 |
|
| 1367 |
emit_move_loop (src, dst, mode, 0, inc, iters, remainder); |
| 1368 |
|
| 1369 |
if (is_move) |
| 1370 |
emit_label (done_label); |
| 1371 |
|
| 1372 |
return true; |
| 1373 |
} |
| 1374 |
|
| 1375 |
/* Expand setmem, as from __builtin_memset. |
| 1376 |
OPERANDS are the same as the setmem pattern. |
| 1377 |
Return true if the expansion was successful, false otherwise. */ |
| 1378 |
|
| 1379 |
bool |
| 1380 |
bpf_expand_setmem (rtx *operands) |
| 1381 |
{ |
| 1382 |
/* Size must be constant for this expansion to work. */ |
| 1383 |
if (!CONST_INT_P (operands[1])) |
| 1384 |
{ |
| 1385 |
if (flag_building_libgcc) |
| 1386 |
warning (0, "could not inline call to %<__builtin_memset%>: " |
| 1387 |
"size must be constant"); |
| 1388 |
else |
| 1389 |
error ("could not inline call to %<__builtin_memset%>: " |
| 1390 |
"size must be constant"); |
| 1391 |
return false; |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* Alignment is a CONST_INT. */ |
| 1395 |
gcc_assert (CONST_INT_P (operands[3])); |
| 1396 |
|
| 1397 |
rtx dst = operands[0]; |
| 1398 |
rtx size = operands[1]; |
| 1399 |
rtx val = operands[2]; |
| 1400 |
unsigned HOST_WIDE_INT size_bytes = UINTVAL (size); |
| 1401 |
unsigned align = UINTVAL (operands[3]); |
| 1402 |
enum machine_mode mode; |
| 1403 |
switch (align) |
| 1404 |
{ |
| 1405 |
case 1: mode = QImode; break; |
| 1406 |
case 2: mode = HImode; break; |
| 1407 |
case 4: mode = SImode; break; |
| 1408 |
case 8: mode = DImode; break; |
| 1409 |
default: |
| 1410 |
gcc_unreachable (); |
| 1411 |
} |
| 1412 |
|
| 1413 |
/* For sizes above threshold, always use a libcall. */ |
| 1414 |
if (size_bytes > (unsigned HOST_WIDE_INT) bpf_inline_memops_threshold) |
| 1415 |
{ |
| 1416 |
if (flag_building_libgcc) |
| 1417 |
warning (0, "could not inline call to %<__builtin_memset%>: " |
| 1418 |
"too many bytes, use %<-minline-memops-threshold%>"); |
| 1419 |
else |
| 1420 |
error ("could not inline call to %<__builtin_memset%>: " |
| 1421 |
"too many bytes, use %<-minline-memops-threshold%>"); |
| 1422 |
return false; |
| 1423 |
} |
| 1424 |
|
| 1425 |
unsigned iters = size_bytes >> ceil_log2 (align); |
| 1426 |
unsigned remainder = size_bytes & (align - 1); |
| 1427 |
unsigned inc = GET_MODE_SIZE (mode); |
| 1428 |
unsigned offset = 0; |
| 1429 |
|
| 1430 |
for (unsigned int i = 0; i < iters; i++) |
| 1431 |
{ |
| 1432 |
emit_move_insn (adjust_address (dst, mode, offset), val); |
| 1433 |
offset += inc; |
| 1434 |
} |
| 1435 |
if (remainder & 4) |
| 1436 |
{ |
| 1437 |
emit_move_insn (adjust_address (dst, SImode, offset), val); |
| 1438 |
offset += 4; |
| 1439 |
remainder -= 4; |
| 1440 |
} |
| 1441 |
if (remainder & 2) |
| 1442 |
{ |
| 1443 |
emit_move_insn (adjust_address (dst, HImode, offset), val); |
| 1444 |
offset += 2; |
| 1445 |
remainder -= 2; |
| 1446 |
} |
| 1447 |
if (remainder & 1) |
| 1448 |
emit_move_insn (adjust_address (dst, QImode, offset), val); |
| 1449 |
|
| 1450 |
return true; |
| 1451 |
} |
| 1452 |
|
| 1453 |
#undef TARGET_DOCUMENTATION_NAME |
| 1454 |
#define TARGET_DOCUMENTATION_NAME "BPF" |
| 1455 |
|
| 1456 |
/* Finally, build the GCC target. */ |
| 1457 |
|
| 1458 |
struct gcc_target targetm = TARGET_INITIALIZER; |
| 1459 |
|
| 1460 |
#include "gt-bpf.h" |