Debian Codegraph @

gcc/config/aarch64/aarch64-c.cc

raw
1 /* Target-specific code for C family languages.
2 Copyright (C) 2015-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 "input.h"
27 #include "memmodel.h"
28 #include "tm_p.h"
29 #include "flags.h"
30 #include "c-family/c-common.h"
31 #include "cpplib.h"
32 #include "c-family/c-pragma.h"
33 #include "langhooks.h"
34 #include "target.h"
35
36
37 #define builtin_define(TXT) cpp_define (pfile, TXT)
38 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
39
40
41 static void
42 aarch64_def_or_undef (bool def_p, const char *macro, cpp_reader *pfile)
43 {
44 if (def_p)
45 cpp_define (pfile, macro);
46 else
47 cpp_undef (pfile, macro);
48 }
49
50 /* Define the macros that we always expect to have on AArch64. */
51
52 static void
53 aarch64_define_unconditional_macros (cpp_reader *pfile)
54 {
55 builtin_define ("__aarch64__");
56 builtin_define ("__ARM_64BIT_STATE");
57
58 builtin_define ("__ARM_ARCH_ISA_A64");
59 builtin_define_with_int_value ("__ARM_ALIGN_MAX_PWR", 28);
60 builtin_define_with_int_value ("__ARM_ALIGN_MAX_STACK_PWR", 16);
61
62 /* __ARM_ARCH_8A is not mandated by ACLE but we define it unconditionally
63 as interoperability with the same arm macro. */
64 builtin_define ("__ARM_ARCH_8A");
65
66 builtin_define_with_int_value ("__ARM_ARCH_PROFILE",
67 TARGET_V8R ? 'R' : 'A');
68 builtin_define ("__ARM_FEATURE_CLZ");
69 builtin_define ("__ARM_FEATURE_IDIV");
70 builtin_define ("__ARM_FEATURE_UNALIGNED");
71 builtin_define ("__ARM_PCS_AAPCS64");
72 builtin_define_with_int_value ("__ARM_SIZEOF_WCHAR_T", WCHAR_TYPE_SIZE / 8);
73
74 builtin_define ("__GCC_ASM_FLAG_OUTPUTS__");
75
76 builtin_define ("__ARM_STATE_ZA");
77 builtin_define ("__ARM_STATE_ZT0");
78 builtin_define ("__ARM_NEON_SVE_BRIDGE");
79
80 /* Define keyword attributes like __arm_streaming as macros that expand
81 to the associated [[...]] attribute. Use __extension__ in the attribute
82 for C, since the [[...]] syntax was only added in C23. */
83 #define DEFINE_ARM_KEYWORD_MACRO(NAME) \
84 builtin_define_with_value ("__arm_" NAME, \
85 lang_GNU_CXX () \
86 ? "[[arm::" NAME "]]" \
87 : "[[__extension__ arm::" NAME "]]", 0);
88
89 DEFINE_ARM_KEYWORD_MACRO ("streaming");
90 DEFINE_ARM_KEYWORD_MACRO ("streaming_compatible");
91 DEFINE_ARM_KEYWORD_MACRO ("locally_streaming");
92
93 #undef DEFINE_ARM_KEYWORD_MACRO
94
95 /* Same for the keyword attributes that take arguments. The snag here
96 is that some old modes warn about or reject variadic arguments. */
97 auto *cpp_opts = cpp_get_options (parse_in);
98 if (!cpp_opts->traditional)
99 {
100 auto old_warn_variadic_macros = cpp_opts->warn_variadic_macros;
101 auto old_cpp_warn_c90_c99_compat = cpp_opts->cpp_warn_c90_c99_compat;
102
103 cpp_opts->warn_variadic_macros = false;
104 cpp_opts->cpp_warn_c90_c99_compat = 0;
105
106 #define DEFINE_ARM_KEYWORD_MACRO_ARGS(NAME) \
107 builtin_define_with_value ("__arm_" NAME "(...)", \
108 lang_GNU_CXX () \
109 ? "[[arm::" NAME "(__VA_ARGS__)]]" \
110 : "[[__extension__ arm::" NAME \
111 "(__VA_ARGS__)]]", 0);
112
113 DEFINE_ARM_KEYWORD_MACRO_ARGS ("new");
114 DEFINE_ARM_KEYWORD_MACRO_ARGS ("preserves");
115 DEFINE_ARM_KEYWORD_MACRO_ARGS ("in");
116 DEFINE_ARM_KEYWORD_MACRO_ARGS ("out");
117 DEFINE_ARM_KEYWORD_MACRO_ARGS ("inout");
118
119 #undef DEFINE_ARM_KEYWORD_MACRO_ARGS
120
121 cpp_opts->warn_variadic_macros = old_warn_variadic_macros;
122 cpp_opts->cpp_warn_c90_c99_compat = old_cpp_warn_c90_c99_compat;
123 }
124 }
125
126 /* Undefine/redefine macros that depend on the current backend state and may
127 need to change when a target pragma modifies the backend state. */
128
129 static void
130 aarch64_update_cpp_builtins (cpp_reader *pfile)
131 {
132 aarch64_def_or_undef (flag_unsafe_math_optimizations, "__ARM_FP_FAST", pfile);
133
134 cpp_undef (pfile, "__ARM_ARCH");
135 builtin_define_with_int_value ("__ARM_ARCH", TARGET_V9A ? 9 : 8);
136
137 builtin_define_with_int_value ("__ARM_SIZEOF_MINIMAL_ENUM",
138 flag_short_enums ? 1 : 4);
139 aarch64_def_or_undef (TARGET_BIG_END, "__AARCH64EB__", pfile);
140 aarch64_def_or_undef (TARGET_BIG_END, "__ARM_BIG_ENDIAN", pfile);
141 aarch64_def_or_undef (!TARGET_BIG_END, "__AARCH64EL__", pfile);
142
143 aarch64_def_or_undef (TARGET_FLOAT, "__ARM_FEATURE_FMA", pfile);
144
145 if (TARGET_FLOAT)
146 {
147 builtin_define_with_int_value ("__ARM_FP", 0x0E);
148 builtin_define ("__ARM_FP16_FORMAT_IEEE");
149 builtin_define ("__ARM_FP16_ARGS");
150 }
151 else
152 cpp_undef (pfile, "__ARM_FP");
153
154 aarch64_def_or_undef (TARGET_FP_F16INST,
155 "__ARM_FEATURE_FP16_SCALAR_ARITHMETIC", pfile);
156 aarch64_def_or_undef (TARGET_SIMD_F16INST,
157 "__ARM_FEATURE_FP16_VECTOR_ARITHMETIC", pfile);
158
159 aarch64_def_or_undef (TARGET_SIMD, "__ARM_FEATURE_NUMERIC_MAXMIN", pfile);
160 aarch64_def_or_undef (TARGET_SIMD, "__ARM_NEON", pfile);
161
162
163 aarch64_def_or_undef (TARGET_CRC32, "__ARM_FEATURE_CRC32", pfile);
164 aarch64_def_or_undef (TARGET_DOTPROD, "__ARM_FEATURE_DOTPROD", pfile);
165 aarch64_def_or_undef (TARGET_COMPLEX, "__ARM_FEATURE_COMPLEX", pfile);
166 aarch64_def_or_undef (TARGET_JSCVT, "__ARM_FEATURE_JCVT", pfile);
167
168 cpp_undef (pfile, "__AARCH64_CMODEL_TINY__");
169 cpp_undef (pfile, "__AARCH64_CMODEL_SMALL__");
170 cpp_undef (pfile, "__AARCH64_CMODEL_LARGE__");
171
172 switch (aarch64_cmodel)
173 {
174 case AARCH64_CMODEL_TINY:
175 case AARCH64_CMODEL_TINY_PIC:
176 builtin_define ("__AARCH64_CMODEL_TINY__");
177 break;
178 case AARCH64_CMODEL_SMALL:
179 case AARCH64_CMODEL_SMALL_PIC:
180 builtin_define ("__AARCH64_CMODEL_SMALL__");
181 break;
182 case AARCH64_CMODEL_LARGE:
183 builtin_define ("__AARCH64_CMODEL_LARGE__");
184 break;
185 default:
186 break;
187 }
188
189 aarch64_def_or_undef (TARGET_ILP32, "_ILP32", pfile);
190 aarch64_def_or_undef (TARGET_ILP32, "__ILP32__", pfile);
191
192 aarch64_def_or_undef (TARGET_AES && TARGET_SHA2, "__ARM_FEATURE_CRYPTO", pfile);
193 aarch64_def_or_undef (TARGET_SIMD_RDMA, "__ARM_FEATURE_QRDMX", pfile);
194 aarch64_def_or_undef (TARGET_SVE, "__ARM_FEATURE_SVE", pfile);
195 cpp_undef (pfile, "__ARM_FEATURE_SVE_BITS");
196 cpp_undef (pfile, "__ARM_FEATURE_SVE_VECTOR_OPERATORS");
197 if (TARGET_SVE)
198 {
199 int bits;
200 int ops = 1;
201 if (!BITS_PER_SVE_VECTOR.is_constant (&bits))
202 {
203 bits = 0;
204 ops = 2;
205 }
206 builtin_define_with_int_value ("__ARM_FEATURE_SVE_BITS", bits);
207 builtin_define_with_int_value ("__ARM_FEATURE_SVE_VECTOR_OPERATORS", ops);
208 }
209 aarch64_def_or_undef (TARGET_SVE_I8MM,
210 "__ARM_FEATURE_SVE_MATMUL_INT8", pfile);
211 aarch64_def_or_undef (TARGET_SVE_F32MM,
212 "__ARM_FEATURE_SVE_MATMUL_FP32", pfile);
213 aarch64_def_or_undef (TARGET_SVE_F64MM,
214 "__ARM_FEATURE_SVE_MATMUL_FP64", pfile);
215 aarch64_def_or_undef (AARCH64_HAVE_ISA (SVE_B16B16)
216 && (TARGET_SVE2 || TARGET_SME2),
217 "__ARM_FEATURE_SVE_B16B16", pfile);
218 aarch64_def_or_undef (TARGET_SVE2, "__ARM_FEATURE_SVE2", pfile);
219 aarch64_def_or_undef (TARGET_SVE2_AES, "__ARM_FEATURE_SVE2_AES", pfile);
220 aarch64_def_or_undef (TARGET_SVE2_BITPERM,
221 "__ARM_FEATURE_SVE2_BITPERM", pfile);
222 aarch64_def_or_undef (TARGET_SVE2_SHA3, "__ARM_FEATURE_SVE2_SHA3", pfile);
223 aarch64_def_or_undef (TARGET_SVE2_SM4, "__ARM_FEATURE_SVE2_SM4", pfile);
224 aarch64_def_or_undef (TARGET_SVE2p1, "__ARM_FEATURE_SVE2p1", pfile);
225
226 aarch64_def_or_undef (TARGET_LSE, "__ARM_FEATURE_ATOMICS", pfile);
227 aarch64_def_or_undef (TARGET_AES, "__ARM_FEATURE_AES", pfile);
228 aarch64_def_or_undef (TARGET_SHA2, "__ARM_FEATURE_SHA2", pfile);
229 aarch64_def_or_undef (TARGET_SHA3, "__ARM_FEATURE_SHA3", pfile);
230 aarch64_def_or_undef (TARGET_SHA3, "__ARM_FEATURE_SHA512", pfile);
231 aarch64_def_or_undef (TARGET_SM4, "__ARM_FEATURE_SM3", pfile);
232 aarch64_def_or_undef (TARGET_SM4, "__ARM_FEATURE_SM4", pfile);
233 aarch64_def_or_undef (TARGET_F16FML, "__ARM_FEATURE_FP16_FML", pfile);
234
235 aarch64_def_or_undef (TARGET_FRINT, "__ARM_FEATURE_FRINT", pfile);
236 aarch64_def_or_undef (TARGET_TME, "__ARM_FEATURE_TME", pfile);
237 aarch64_def_or_undef (TARGET_RNG, "__ARM_FEATURE_RNG", pfile);
238 aarch64_def_or_undef (TARGET_MEMTAG, "__ARM_FEATURE_MEMORY_TAGGING", pfile);
239
240 aarch64_def_or_undef (aarch_bti_enabled (),
241 "__ARM_FEATURE_BTI_DEFAULT", pfile);
242
243 cpp_undef (pfile, "__ARM_FEATURE_PAC_DEFAULT");
244 if (aarch_ra_sign_scope != AARCH_FUNCTION_NONE)
245 {
246 int v = 0;
247 if (aarch64_ra_sign_key == AARCH64_KEY_A)
248 v |= 1;
249 if (aarch64_ra_sign_key == AARCH64_KEY_B)
250 v |= 2;
251 if (aarch_ra_sign_scope == AARCH_FUNCTION_ALL)
252 v |= 4;
253 builtin_define_with_int_value ("__ARM_FEATURE_PAC_DEFAULT", v);
254 }
255
256 aarch64_def_or_undef (TARGET_PAUTH, "__ARM_FEATURE_PAUTH", pfile);
257 aarch64_def_or_undef (TARGET_BTI, "__ARM_FEATURE_BTI", pfile);
258 aarch64_def_or_undef (aarch64_gcs_enabled (),
259 "__ARM_FEATURE_GCS_DEFAULT", pfile);
260 aarch64_def_or_undef (TARGET_GCS, "__ARM_FEATURE_GCS", pfile);
261 aarch64_def_or_undef (TARGET_I8MM, "__ARM_FEATURE_MATMUL_INT8", pfile);
262 aarch64_def_or_undef (TARGET_BF16_SIMD,
263 "__ARM_FEATURE_BF16_VECTOR_ARITHMETIC", pfile);
264 aarch64_def_or_undef (TARGET_BF16_FP,
265 "__ARM_FEATURE_BF16_SCALAR_ARITHMETIC", pfile);
266 aarch64_def_or_undef (TARGET_BF16_FP,
267 "__ARM_FEATURE_BF16", pfile);
268 aarch64_def_or_undef (TARGET_SVE_BF16,
269 "__ARM_FEATURE_SVE_BF16", pfile);
270
271 aarch64_def_or_undef (TARGET_LUT, "__ARM_FEATURE_LUT", pfile);
272
273 aarch64_def_or_undef (TARGET_FP8, "__ARM_FEATURE_FP8", pfile);
274
275 aarch64_def_or_undef (TARGET_FP8DOT2, "__ARM_FEATURE_FP8DOT2", pfile);
276
277 aarch64_def_or_undef (TARGET_FP8DOT4, "__ARM_FEATURE_FP8DOT4", pfile);
278
279 aarch64_def_or_undef (TARGET_FP8FMA, "__ARM_FEATURE_FP8FMA", pfile);
280
281 aarch64_def_or_undef (TARGET_LS64,
282 "__ARM_FEATURE_LS64", pfile);
283 aarch64_def_or_undef (TARGET_RCPC, "__ARM_FEATURE_RCPC", pfile);
284 aarch64_def_or_undef (TARGET_D128, "__ARM_FEATURE_SYSREG128", pfile);
285
286 aarch64_def_or_undef (TARGET_SME, "__ARM_FEATURE_SME", pfile);
287 aarch64_def_or_undef (TARGET_SME_I16I64, "__ARM_FEATURE_SME_I16I64", pfile);
288 aarch64_def_or_undef (AARCH64_HAVE_ISA (SME_B16B16),
289 "__ARM_FEATURE_SME_B16B16", pfile);
290 aarch64_def_or_undef (AARCH64_HAVE_ISA (SME_F16F16),
291 "__ARM_FEATURE_SME_F16F16", pfile);
292 aarch64_def_or_undef (TARGET_SME_F64F64, "__ARM_FEATURE_SME_F64F64", pfile);
293 aarch64_def_or_undef (TARGET_SME2, "__ARM_FEATURE_SME2", pfile);
294 aarch64_def_or_undef (AARCH64_HAVE_ISA (SME2p1),
295 "__ARM_FEATURE_SME2p1", pfile);
296 aarch64_def_or_undef (TARGET_FAMINMAX, "__ARM_FEATURE_FAMINMAX", pfile);
297
298 /* Not for ACLE, but required to keep "float.h" correct if we switch
299 target between implementations that do or do not support ARMv8.2-A
300 16-bit floating-point extensions. */
301 cpp_undef (pfile, "__FLT_EVAL_METHOD__");
302 builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
303 c_flt_eval_method (true));
304 cpp_undef (pfile, "__FLT_EVAL_METHOD_C99__");
305 builtin_define_with_int_value ("__FLT_EVAL_METHOD_C99__",
306 c_flt_eval_method (false));
307 }
308
309 /* Implement TARGET_CPU_CPP_BUILTINS. */
310
311 void
312 aarch64_cpu_cpp_builtins (cpp_reader *pfile)
313 {
314 aarch64_define_unconditional_macros (pfile);
315 aarch64_update_cpp_builtins (pfile);
316 }
317
318 /* Hook to validate the current #pragma GCC target and set the state, and
319 update the macros based on what was changed. If ARGS is NULL, then
320 POP_TARGET is used to reset the options. */
321
322 static bool
323 aarch64_pragma_target_parse (tree args, tree pop_target)
324 {
325 /* If args is not NULL then process it and setup the target-specific
326 information that it specifies. */
327 if (args)
328 {
329 if (!aarch64_process_target_attr (args))
330 return false;
331
332 aarch64_override_options_internal (&global_options);
333 }
334
335 /* args is NULL, restore to the state described in pop_target. */
336 else
337 {
338 pop_target = pop_target ? pop_target : target_option_default_node;
339 cl_target_option_restore (&global_options, &global_options_set,
340 TREE_TARGET_OPTION (pop_target));
341 }
342
343 target_option_current_node
344 = build_target_option_node (&global_options, &global_options_set);
345
346 aarch64_reset_previous_fndecl ();
347 /* For the definitions, ensure all newly defined macros are considered
348 as used for -Wunused-macros. There is no point warning about the
349 compiler predefined macros. */
350 cpp_options *cpp_opts = cpp_get_options (parse_in);
351 unsigned char saved_warn_unused_macros = cpp_opts->warn_unused_macros;
352 cpp_opts->warn_unused_macros = 0;
353
354 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
355 aarch64_update_cpp_builtins (parse_in);
356 cpp_stop_forcing_token_locations (parse_in);
357
358 cpp_opts->warn_unused_macros = saved_warn_unused_macros;
359
360 /* If we're popping or reseting make sure to update the globals so that
361 the optab availability predicates get recomputed. */
362 if (pop_target)
363 aarch64_save_restore_target_globals (pop_target);
364
365 return true;
366 }
367
368 /* Implement "#pragma GCC aarch64". */
369 static void
370 aarch64_pragma_aarch64 (cpp_reader *)
371 {
372 tree x;
373 if (pragma_lex (&x) != CPP_STRING)
374 {
375 error ("%<#pragma GCC aarch64%> requires a string parameter");
376 return;
377 }
378
379 const char *name = TREE_STRING_POINTER (x);
380 if (strcmp (name, "arm_sve.h") == 0)
381 aarch64_sve::handle_arm_sve_h (false);
382 else if (strcmp (name, "arm_sme.h") == 0)
383 aarch64_sve::handle_arm_sme_h (false);
384 else if (strcmp (name, "arm_neon.h") == 0)
385 handle_arm_neon_h ();
386 else if (strcmp (name, "arm_acle.h") == 0)
387 handle_arm_acle_h ();
388 else if (strcmp (name, "arm_neon_sve_bridge.h") == 0)
389 aarch64_sve::handle_arm_neon_sve_bridge_h (false);
390 else
391 error ("unknown %<#pragma GCC aarch64%> option %qs", name);
392 }
393
394 /* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN. */
395 static tree
396 aarch64_resolve_overloaded_builtin (location_t location,
397 tree fndecl, void *uncast_arglist, bool)
398 {
399 vec<tree, va_gc> empty = {};
400 vec<tree, va_gc> *arglist = (uncast_arglist
401 ? (vec<tree, va_gc> *) uncast_arglist
402 : &empty);
403 unsigned int code = DECL_MD_FUNCTION_CODE (fndecl);
404 unsigned int subcode = code >> AARCH64_BUILTIN_SHIFT;
405 tree new_fndecl;
406 switch (code & AARCH64_BUILTIN_CLASS)
407 {
408 case AARCH64_BUILTIN_GENERAL:
409 return aarch64_resolve_overloaded_builtin_general (location, fndecl,
410 uncast_arglist);
411 case AARCH64_BUILTIN_SVE:
412 new_fndecl = aarch64_sve::resolve_overloaded_builtin (location, subcode,
413 arglist);
414 break;
415 }
416 if (new_fndecl == NULL_TREE || new_fndecl == error_mark_node)
417 return new_fndecl;
418 return build_function_call_vec (location, vNULL, new_fndecl, arglist,
419 NULL, fndecl);
420 }
421
422 /* Implement TARGET_CHECK_BUILTIN_CALL. */
423 static bool
424 aarch64_check_builtin_call (location_t loc, vec<location_t> arg_loc,
425 tree fndecl, tree orig_fndecl, unsigned int nargs,
426 tree *args, bool)
427 {
428 unsigned int code = DECL_MD_FUNCTION_CODE (fndecl);
429 unsigned int subcode = code >> AARCH64_BUILTIN_SHIFT;
430 switch (code & AARCH64_BUILTIN_CLASS)
431 {
432 case AARCH64_BUILTIN_GENERAL:
433 return aarch64_general_check_builtin_call (loc, arg_loc, subcode,
434 orig_fndecl, nargs, args);
435 case AARCH64_BUILTIN_SVE:
436 return aarch64_sve::check_builtin_call (loc, arg_loc, subcode,
437 orig_fndecl, nargs, args);
438 }
439 gcc_unreachable ();
440 }
441
442 /* Implement REGISTER_TARGET_PRAGMAS. */
443
444 void
445 aarch64_register_pragmas (void)
446 {
447 /* Update pragma hook to allow parsing #pragma GCC target. */
448 targetm.target_option.pragma_parse = aarch64_pragma_target_parse;
449
450 targetm.resolve_overloaded_builtin = aarch64_resolve_overloaded_builtin;
451 targetm.check_builtin_call = aarch64_check_builtin_call;
452
453 c_register_pragma ("GCC", "aarch64", aarch64_pragma_aarch64);
454 }