]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/bpf/bpf.h
Update copyright years.
[thirdparty/gcc.git] / gcc / config / bpf / bpf.h
1 /* Definition of the eBPF target for GCC.
2 Copyright (C) 2019-2022 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
7 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, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 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 #ifndef GCC_BPF_H
21 #define GCC_BPF_H
22
23 /**** Controlling the Compilation Driver. */
24
25 #define ASM_SPEC "%{mbig-endian:-EB} %{!mbig-endian:-EL} %{mxbpf:-mxbpf}"
26 #define LINK_SPEC "%{mbig-endian:-EB} %{!mbig-endian:-EL}"
27 #define LIB_SPEC ""
28 #define STARTFILE_SPEC ""
29
30 /**** Run-time Target Specification. */
31
32 #define TARGET_CPU_CPP_BUILTINS() bpf_target_macros (pfile)
33
34 /**** Storage Layout. */
35
36 /* Endianness and word size. */
37 #define BITS_BIG_ENDIAN 0
38 #define BYTES_BIG_ENDIAN (TARGET_BIG_ENDIAN)
39 #define WORDS_BIG_ENDIAN (TARGET_BIG_ENDIAN)
40 #define BITS_PER_WORD 64
41 #define UNITS_PER_WORD 8
42
43 /* When storing an integer whose size is less than 64-bit in a
44 register, promote it to a DImode. */
45 #define PROMOTE_MODE(M, UNSIGNEDP, TYPE) \
46 do \
47 { \
48 if (GET_MODE_CLASS (M) == MODE_INT \
49 && GET_MODE_SIZE (M) < 8) \
50 M = DImode; \
51 } while (0)
52
53 /* Align argument parameters on the stack to 64-bit, at a minimum. */
54 #define PARM_BOUNDARY 64
55
56 /* The hardware enforces that the stack pointer should be aligned to
57 64-bit at any time. */
58 #define STACK_BOUNDARY 64
59
60 /* Function entry points are aligned to 64 bits. */
61 #define FUNCTION_BOUNDARY 64
62
63 /* Maximum alignment required by data of any type. */
64 #define BIGGEST_ALIGNMENT 64
65
66 /* The best alignment to use in cases where we have a choice. */
67 #define FASTEST_ALIGNMENT 64
68
69 /* Use a fast alignment when storing arrays of chars in a local. */
70 #define LOCAL_ALIGNMENT(TYPE, ALIGN) \
71 (TREE_CODE (TYPE) == ARRAY_TYPE \
72 && TYPE_MODE (TREE_TYPE (TYPE)) == QImode \
73 && (ALIGN) < FASTEST_ALIGNMENT ? FASTEST_ALIGNMENT : (ALIGN))
74
75 /* The load and store instructions won't work if the data is not in
76 it's expected alignment. */
77 #define STRICT_ALIGNMENT 1
78
79 /* We use Pmode as the mode of the size increment operand in an
80 `allocate_stack' pattern. */
81 #define STACK_SIZE_MODE Pmode
82
83 /**** Layout of Source Language Data Types. */
84
85 #define INT_TYPE_SIZE 32
86 #define SHORT_TYPE_SIZE 16
87 #define LONG_TYPE_SIZE 64
88 #define LONG_LONG_TYPE_SIZE 64
89 #define CHAR_TYPE_SIZE 8
90 #define FLOAT_TYPE_SIZE 32
91 #define DOUBLE_TYPE_SIZE 64
92 #define LONG_DOUBLE_TYPE_SIZE 64
93
94 #define INTPTR_TYPE "long int"
95 #define UINTPTR_TYPE "long unsigned int"
96 #define SIZE_TYPE "long unsigned int"
97 #define PTRDIFF_TYPE "long int"
98
99 #define SIG_ATOMIC_TYPE "char"
100
101 #define INT8_TYPE "char"
102 #define INT16_TYPE "short int"
103 #define INT32_TYPE "int"
104 #define INT64_TYPE "long int"
105 #define UINT8_TYPE "unsigned char"
106 #define UINT16_TYPE "short unsigned int"
107 #define UINT32_TYPE "unsigned int"
108 #define UINT64_TYPE "long unsigned int"
109
110 #define INT_LEAST8_TYPE INT8_TYPE
111 #define INT_LEAST16_TYPE INT16_TYPE
112 #define INT_LEAST32_TYPE INT32_TYPE
113 #define INT_LEAST64_TYPE INT64_TYPE
114 #define UINT_LEAST8_TYPE UINT8_TYPE
115 #define UINT_LEAST16_TYPE UINT16_TYPE
116 #define UINT_LEAST32_TYPE UINT32_TYPE
117 #define UINT_LEAST64_TYPE UINT64_TYPE
118
119 #define INT_FAST8_TYPE INT8_TYPE
120 #define INT_FAST16_TYPE INT16_TYPE
121 #define INT_FAST32_TYPE INT32_TYPE
122 #define INT_FAST64_TYPE INT64_TYPE
123 #define UINT_FAST8_TYPE UINT8_TYPE
124 #define UINT_FAST16_TYPE UINT16_TYPE
125 #define UINT_FAST32_TYPE UINT32_TYPE
126 #define UINT_FAST64_TYPE UINT64_TYPE
127
128 /* `char' is signed by default, like in x86. */
129 #define DEFAULT_SIGNED_CHAR 1
130
131 /* `wchar_t' is a signed 32-bit type. The second constant is used by
132 cpp, which can't use WCHAR_TYPE. */
133 #define WCHAR_TYPE "int"
134 #define WCHAR_TYPE_SIZE 32
135
136 /* `wint_t' is a signed 32-bit type. */
137 #define WINT_TYPE "int"
138 #define WINT_TYPE_SIZE 32
139
140 /**** Register Usage. */
141
142 /*** Basic Characteristics of Registers. */
143
144 #define BPF_R0 0
145 #define BPF_R1 1
146 #define BPF_R2 2
147 #define BPF_R3 3
148 #define BPF_R4 4
149 #define BPF_R5 5
150 #define BPF_R6 6
151 #define BPF_CTX BPF_R6
152 #define BPF_R7 7
153 #define BPF_R8 8
154 #define BPF_R9 9
155 #define BPF_SP BPF_R9
156 #define BPF_R10 10
157 #define BPF_FP BPF_R10
158 /* 11 is not a real eBPF hard register and is eliminated or not used
159 in the final assembler. See below. */
160
161 #define FIRST_PSEUDO_REGISTER 12
162
163 /* The registers %r0..%r8 are available for general allocation.
164 %r9 is the pseudo-stack pointer.
165 %r10 is the stack frame, which is read-only.
166 %r11 (__arg__) is a fake register that always gets eliminated. */
167 #define FIXED_REGISTERS \
168 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}
169
170 /* %r0..%r5 are clobbered by function calls. */
171 #define CALL_USED_REGISTERS \
172 {1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}
173
174 /**** Register Classes. */
175
176 enum reg_class
177 {
178 NO_REGS, /* no registers in set. */
179 ALL_REGS, /* all registers. */
180 LIM_REG_CLASSES /* max value + 1. */
181 };
182
183 #define N_REG_CLASSES (int) LIM_REG_CLASSES
184 #define GENERAL_REGS ALL_REGS
185
186 /* An initializer containing the names of the register classes as C
187 string constants. These names are used in writing some of the
188 debugging dumps. */
189 #define REG_CLASS_NAMES \
190 { \
191 "NO_REGS", \
192 "ALL_REGS" \
193 }
194
195 /* An initializer containing the contents of the register classes, as
196 integers which are bit masks. The Nth integer specifies the
197 contents of class N. The way the integer MASK is interpreted is
198 that register R is in the class if `MASK & (1 << R)' is 1.
199
200 In eBPF all the hard registers are considered general-purpose
201 integer registers. */
202 #define REG_CLASS_CONTENTS \
203 { \
204 0x00000000, /* NO_REGS */ \
205 0x00000fff, /* ALL_REGS */ \
206 }
207
208 /* A C expression whose value is a register class containing hard
209 register REGNO. In general there is more that one such class;
210 choose a class which is "minimal", meaning that no smaller class
211 also contains the register. */
212 #define REGNO_REG_CLASS(REGNO) GENERAL_REGS
213
214 /* A macro whose definition is the name of the class to which a
215 valid base register must belong. A base register is one used in
216 an address which is the register value plus a displacement. */
217 #define BASE_REG_CLASS GENERAL_REGS
218
219 /* A macro whose definition is the name of the class to which a
220 valid index register must belong. An index register is one used
221 in an address where its value is either multiplied by a scale
222 factor or added to another register (as well as added to a
223 displacement). */
224 #define INDEX_REG_CLASS NO_REGS
225
226 /* C expression which is nonzero if register number REGNO is suitable
227 for use as a base register in operand addresses. In eBPF every
228 hard register can be used for this purpose. */
229 #define REGNO_OK_FOR_BASE_P(REGNO) \
230 ((REGNO) < FIRST_PSEUDO_REGISTER)
231
232 /* C expression which is nonzero if register number REGNO is suitable
233 for use as an index register in operand addresses. */
234 #define REGNO_OK_FOR_INDEX_P(REGNO) false
235
236 /**** Debugging Info ****/
237
238 /* In eBPF it is not possible to unwind frames. Disable CFA. */
239
240 #define DWARF2_FRAME_INFO 0
241
242 /**** Stack Layout and Calling Conventions. */
243
244 /*** Basic Stack Layout. */
245
246 #define STACK_GROWS_DOWNWARD 1
247 #define FRAME_GROWS_DOWNWARD 1
248
249 /* The argument pointer always points to the first argument. */
250 #define FIRST_PARM_OFFSET(FNDECL) 0
251
252 /* Unsupported. */
253 #define RETURN_ADDR_RTX(count, frame) const0_rtx
254
255 /*** Registers That Address the Stack Frame. */
256
257 #define FRAME_POINTER_REGNUM 10
258 #define STACK_POINTER_REGNUM 9
259 #define ARG_POINTER_REGNUM 11
260 #define STATIC_CHAIN_REGNUM 8
261
262 /*** Registers elimination. */
263
264 #define ELIMINABLE_REGS \
265 {{ ARG_POINTER_REGNUM, FRAME_POINTER_REGNUM }, \
266 { ARG_POINTER_REGNUM, STACK_POINTER_REGNUM }}
267
268 /* Define the offset between two registers, one to be eliminated, and
269 the other its replacement, at the start of a routine. */
270 #define INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET) \
271 do \
272 { \
273 (OFFSET) = bpf_initial_elimination_offset ((FROM), (TO)); \
274 } while (0)
275
276 /*** Passing Function Arguments on the Stack. */
277
278 /* The eBPF ABI doesn't support passing arguments on the stack. Only
279 in the first five registers. Code in bpf.c assures the stack is
280 never used when passing arguments. However, we still have to
281 define the constants below. */
282
283 /* If nonzero, function arguments will be evaluated from last to
284 first, rather than from first to last. */
285 #define PUSH_ARGS_REVERSED 1
286
287 /* Allocate stack space for arguments at the beginning of each
288 function. */
289 #define ACCUMULATE_OUTGOING_ARGS 1
290
291 /*** Passing Arguments in Registers. */
292
293 /* Use an integer in order to keep track of the number of arguments
294 passed to a function in integer registers, up to
295 MAX_ARGS_IN_REGISTERS. */
296 #define CUMULATIVE_ARGS int
297
298 /* INIT_CUMULATIVE_ARGS initializes a variable CUM of type
299 CUMULATIVE_ARGS for a call to a function whose data type is FNTYPE.
300 For a library call, FNTYPE is 0. */
301 #define INIT_CUMULATIVE_ARGS(CUM,FNTYPE,LIBNAME,FNDECL,N_NAMED_ARGS) \
302 memset (&(CUM), 0, sizeof (CUM))
303
304 /* Nonzero if N is the number of a hard register in which function
305 arguments are sometimes passed. */
306 #define FUNCTION_ARG_REGNO_P(N) ((N) >= 1 && (N) <= 5)
307
308 /*** How Scalar Function Values are Returned. */
309
310 /* Define how to find the value returned by a library function
311 assuming the value has mode MODE. This is always %r0 for eBPF. */
312 #define LIBCALL_VALUE(MODE) \
313 gen_rtx_REG ((MODE), 0)
314
315 /*** Generating Code for Profiling. */
316
317 /* We do not support profiling yet, so do not call `mcount'. */
318 #define FUNCTION_PROFILER(FILE, LABELNO) do { } while (0)
319
320 /*** Function Entry and Exit. */
321
322 /* We do not require an accurate stack pointer at function return.
323 This is because the stack pointer's original value is initialized
324 from the frame pointer, rather than decreased, to satisfy the
325 kernel's verifier. Thus, we have to save the stack pointer in
326 function prologue and restore it in function epilogue. If
327 EXIT_IGNORE_STACK is not set, then superfluous instructions are
328 generated to save and restore the stack pointer after and before
329 the function epilogue, respectively. */
330 #define EXIT_IGNORE_STACK 1
331
332 /**** Support for Nested Functions. */
333
334 /* We have to define TRAMPOLINE_SIZE even if we don't ever generate
335 them. Set to 64 arbitrarily. */
336 #define TRAMPOLINE_SIZE 64
337
338 /**** Addressing Modes. */
339
340 /* Maximum number of registers that can appear in a valid memory
341 address. */
342 #define MAX_REGS_PER_ADDRESS 1
343
344 /* 1 if X is an rtx for a constant that is a valid address. */
345
346 #define CONSTANT_ADDRESS_P(X) 0
347
348 /**** Describing Relative Costs of Operations. */
349
350 /* Cost of a branch instruction. A value of 1 is the default. */
351 #define BRANCH_COST(SPEED_P,PREDICTABLE_P) 1
352
353 /* The SPARC port says: Nonzero if access to memory by bytes is slow
354 and undesirable. For RISC chips, it means that access to memory by
355 bytes is no better than access by words when possible, so grab a
356 whole word and maybe make use of that. */
357 #define SLOW_BYTE_ACCESS 1
358
359 /* Threshold of number of scalar memory-to-memory move instructions,
360 _below_ which a sequence of insns should be generated instead of a
361 string move insn or a library call. */
362 #define MOVE_RATIO(speed) 128
363
364 /* Threshold of number of scalar move instructions, _below_ which a
365 sequence of insns should be generated to clear memory instead of a
366 string clear insn or a library call. */
367 #define CLEAR_RATIO(speed) 128
368
369 /* Threshold of number of scalar move instructions, _below_ which a
370 sequence of insns should be generated to set memory to a constant
371 value, instead of a block set insn or a library call. */
372 #define SET_RATIO(speed) 128
373
374 /* True if it is as good or better to call a constant function address
375 than to call an address kept in a register. */
376 #define NO_FUNCTION_CSE 1
377
378 /**** Dividing the Output into Sections. */
379
380 #define TEXT_SECTION_ASM_OP "\t.text"
381 #define DATA_SECTION_ASM_OP "\t.data"
382 #define BSS_SECTION_ASM_OP "\t.bss"
383
384 /**** Defining the Output Assembler Language. */
385
386 /*** The Overall Framework of an Assembler File. */
387
388 #define ASM_COMMENT_START ";"
389
390 /* Output to assembler file text saying following lines
391 may contain character constants, extra white space, comments, etc. */
392
393 #ifndef ASM_APP_ON
394 #define ASM_APP_ON " #APP\n"
395 #endif
396
397 /* Output to assembler file text saying following lines
398 no longer contain unusual constructs. */
399
400 #ifndef ASM_APP_OFF
401 #define ASM_APP_OFF " #NO_APP\n"
402 #endif
403
404 /*** Output of Data. */
405
406 /*** Output of Uninitialized Variables. */
407
408 /* A C statement (sans semicolon) to output to the stdio stream
409 FILE the assembler definition of uninitialized global DECL named
410 NAME whose size is SIZE bytes and alignment is ALIGN bytes.
411 Try to use asm_output_aligned_bss to implement this macro. */
412
413 #define ASM_OUTPUT_ALIGNED_BSS(FILE, DECL, NAME, SIZE, ALIGN) \
414 do \
415 { \
416 ASM_OUTPUT_TYPE_DIRECTIVE (FILE, NAME, "object"); \
417 fprintf ((FILE), "%s", "\t.lcomm\t"); \
418 assemble_name ((FILE), (NAME)); \
419 fprintf ((FILE), "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n", \
420 (SIZE), (ALIGN) / BITS_PER_UNIT); \
421 } \
422 while (0)
423
424 /*** Output and Generation of Labels. */
425
426 /* Globalizing directive for a label. */
427 #define GLOBAL_ASM_OP "\t.global\t"
428
429 /* This is how to store into the string LABEL
430 the symbol_ref name of an internal numbered label where
431 PREFIX is the class of label and NUM is the number within the class.
432 This is suitable for output with `assemble_name'. */
433
434 #undef ASM_GENERATE_INTERNAL_LABEL
435 #define ASM_GENERATE_INTERNAL_LABEL(LABEL,PREFIX,NUM) \
436 sprintf ((LABEL), "*%s%s%ld", (LOCAL_LABEL_PREFIX), (PREFIX), (long)(NUM))
437
438 /*** Output of Assembler Instructions. */
439
440 #define REGISTER_NAMES \
441 { "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7", \
442 "%r8", "%r9", "%fp", "__arg__" }
443
444 #define ADDITIONAL_REGISTER_NAMES \
445 { { "%a", 0 }, { "%ctx", 6 }, { "%r10" , 10 } }
446
447 #define LOCAL_LABEL_PREFIX "."
448 #define USER_LABEL_PREFIX ""
449
450 #define PRINT_OPERAND(STREAM,X,CODE) \
451 bpf_print_operand ((STREAM),(X),(CODE))
452
453 #define PRINT_OPERAND_ADDRESS(STREAM,X) \
454 bpf_print_operand_address ((STREAM), (X))
455
456 /*** Assembler Commands for Alignment. */
457
458 /* This is how to output an assembler line that says to advance the
459 location counter to a multiple of 2**LOG bytes. */
460 #define ASM_OUTPUT_ALIGN(STREAM,LOG) \
461 fprintf (STREAM, "\t.align\t%d\n", (LOG))
462
463 /**** Miscellaneous Parameters. */
464
465 /* Specify the machine mode that this machine uses for the index in
466 the tablejump instruction. */
467 #define CASE_VECTOR_MODE DImode
468
469 /* Define if operations between registers with integral mode smaller
470 than a word are always performed on the entire register. */
471 #define WORD_REGISTER_OPERATIONS 1
472
473 /* C expression indicating when insns that read memory in MEM_MODE, an
474 integral mode narrower than a word, set the bits outsize of
475 MEM_MODE to be either the sign-extension or the zero-extension of
476 the data read. */
477 #define LOAD_EXTEND_OP(MODE) ZERO_EXTEND
478
479 /* The maximum number of bytes that a single instruction can move
480 quickly between memory and registers or between two memory
481 locations. */
482 #define MOVE_MAX 8
483
484 /* An alias for the machine mode for pointers. */
485 #define Pmode DImode
486
487 /* An alias for the machine mode used for memory references to
488 functions being called, in 'call' RTL expressions. */
489 #define FUNCTION_MODE Pmode
490
491 /* No libm on eBPF (for now.) */
492 #define MATH_LIBRARY ""
493
494 /**** libgcc settings. */
495
496 /* Iterating over the global constructors and destructors and
497 executing them requires the ability of doing indirect calls.
498
499 eBPF doesn't support indirect calls, so no chance of supporting
500 constructors and destructors. */
501 #define DO_GLOBAL_CTORS_BODY \
502 do { } while (0)
503 #define DO_GLOBAL_DTORS_BODY \
504 do { } while (0)
505
506 #endif /* ! GCC_BPF_H */