Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / config / arm / arm-c.cc
blob59c0d8ce747a72050a5ae0431564735ff59fad52
1 /* Copyright (C) 2007-2023 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 for more details.
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
19 #define IN_TARGET_CODE 1
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "c-family/c-common.h"
26 #include "memmodel.h"
27 #include "tm_p.h"
28 #include "c-family/c-pragma.h"
29 #include "stringpool.h"
30 #include "arm-builtins.h"
31 #include "arm-protos.h"
33 tree
34 arm_resolve_cde_builtin (location_t loc, tree fndecl, void *arglist)
36 vec<tree, va_gc> *params = static_cast<vec<tree, va_gc> *> (arglist);
37 unsigned param_num = params ? params->length() : 0;
38 unsigned num_args = list_length (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) - 1;
39 /* Ensure this function has the correct number of arguments.
40 This won't happen when using the intrinsics defined by the ACLE, since
41 they're exposed to the user via a wrapper in the arm_cde.h header that has
42 the correct number of arguments ... hence the compiler would already catch
43 an incorrect number of arguments there.
45 It is still possible to get here if the user tries to call the __bulitin_*
46 functions directly. We could print some error message in this function,
47 but instead we leave it to the rest of the code to catch this problem in
48 the same way that other __builtin_* functions catch it.
50 This does mean an odd error message, but it's consistent with the rest of
51 the builtins. */
52 if (param_num != num_args)
53 return NULL_TREE;
55 tree to_return = NULL_TREE;
56 /* Take the functions return type since that's the same type as the arguments
57 this function needs (the types of the builtin function all come from the
58 machine mode of the RTL pattern, and they're all the same and calculated
59 in the same way). */
60 tree pattern_type = TREE_TYPE (TREE_TYPE (fndecl));
62 unsigned i;
63 /* Hard coding the number of parameters we don't want to cast at the end of
64 the builtin. This is the easiest approach for the CDE intrinsics, and
65 introducing a parameter to store in the builtins.def macros seems overkill
66 when they're only relevant here. */
67 unsigned end_args = arm_cde_end_args (fndecl);
68 unsigned cast_param_end = param_num - end_args;
69 /* For the vcx1q patterns that don't need any casts. */
70 if (cast_param_end == 1)
71 return NULL_TREE;
73 /* In order to check all arguments rather than complaining on the first
74 invalid one we record whether *any* arguments are invalid using this
75 boolean variable. */
76 bool invalid = false;
77 for (i = 1; i < cast_param_end; i++)
79 tree this_param = (*params)[i];
80 if (TREE_CODE (this_param) == ERROR_MARK)
82 invalid = true;
83 continue;
85 tree param_type = TREE_TYPE (this_param);
87 /* Return value is cast to type that second argument originally was.
88 All non-constant arguments are cast to the return type calculated from
89 the RTL pattern.
91 Set the return type to an unqualified version of the type of the first
92 parameter. The first parameter since that is how the intrinsics are
93 defined -- to always return the same type as the first polymorphic
94 argument. Unqualified version of the type since we don't want passing
95 a constant parameter to mean that the return value of the builtin is
96 also constant. */
97 if (i == 1)
98 to_return = build_qualified_type (param_type, 0 MEM_STAT_INFO);
100 /* The only requirement of these intrinsics on the type of the variable
101 is that it's 128 bits wide. All other types are valid and we simply
102 VIEW_CONVERT_EXPR them to the type of the underlying builtin. */
103 tree type_size = TYPE_SIZE (param_type);
104 if (! tree_fits_shwi_p (type_size)
105 || tree_to_shwi (type_size) != 128)
107 error_at (loc,
108 "argument %u to function %qE is of type %qT which is not "
109 "known to be 128 bits wide",
110 i + 1, fndecl, param_type);
111 invalid = true;
112 continue;
115 /* Only convert the argument if we actually need to. */
116 if (! check_base_type (pattern_type, param_type))
117 (*params)[i] = build1 (VIEW_CONVERT_EXPR, pattern_type, this_param);
119 if (invalid)
120 return NULL_TREE;
122 /* We know it's safe to call this since this builtin is here to implement an
123 ACLE function, and those functions are only for C/C++. */
124 tree call_expr = build_function_call_vec (loc, vNULL, fndecl, params,
125 NULL, fndecl);
127 gcc_assert (to_return != NULL_TREE);
128 if (! check_base_type (to_return, pattern_type))
129 return build1 (VIEW_CONVERT_EXPR, to_return, call_expr);
130 return call_expr;
133 /* Implement "#pragma GCC arm". */
134 static void
135 arm_pragma_arm (cpp_reader *)
137 tree x;
138 if (pragma_lex (&x) != CPP_STRING)
140 error ("%<#pragma GCC arm%> requires a string parameter");
141 return;
144 const char *name = TREE_STRING_POINTER (x);
145 if (strcmp (name, "arm_mve_types.h") == 0)
146 arm_mve::handle_arm_mve_types_h ();
147 else
148 error ("unknown %<#pragma GCC arm%> option %qs", name);
151 /* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN. This is currently only
152 used for the MVE related builtins for the CDE extension.
153 Here we ensure the type of arguments is such that the size is correct, and
154 then return a tree that describes the same function call but with the
155 relevant types cast as necessary. */
156 tree
157 arm_resolve_overloaded_builtin (location_t loc, tree fndecl, void *arglist)
159 if (arm_describe_resolver (fndecl) == arm_cde_resolver)
160 return arm_resolve_cde_builtin (loc, fndecl, arglist);
161 return NULL_TREE;
164 /* Output C specific EABI object attributes. These cannot be done in
165 arm.cc because they require information from the C frontend. */
167 static void
168 arm_output_c_attributes (void)
170 int wchar_size = (int)(TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
171 arm_emit_eabi_attribute ("Tag_ABI_PCS_wchar_t", 18, wchar_size);
175 /* Setup so that common code calls arm_output_c_attributes. */
177 void
178 arm_lang_object_attributes_init (void)
180 arm_lang_output_object_attributes_hook = arm_output_c_attributes;
183 #define builtin_define(TXT) cpp_define (pfile, TXT)
184 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
186 /* Define or undefine macros based on the current target. If the user does
187 #pragma GCC target, we need to adjust the macros dynamically. */
189 static void
190 def_or_undef_macro(struct cpp_reader* pfile, const char *name, bool def_p)
192 if (def_p)
193 cpp_define (pfile, name);
194 else
195 cpp_undef (pfile, name);
198 static void
199 arm_cpu_builtins (struct cpp_reader* pfile)
201 def_or_undef_macro (pfile, "__ARM_FEATURE_DSP", TARGET_DSP_MULTIPLY);
202 def_or_undef_macro (pfile, "__ARM_FEATURE_QBIT", TARGET_ARM_QBIT);
203 def_or_undef_macro (pfile, "__ARM_FEATURE_SAT", TARGET_ARM_SAT);
204 def_or_undef_macro (pfile, "__ARM_FEATURE_CRYPTO", TARGET_CRYPTO);
205 def_or_undef_macro (pfile, "__ARM_FEATURE_AES", TARGET_CRYPTO);
206 def_or_undef_macro (pfile, "__ARM_FEATURE_SHA2", TARGET_CRYPTO);
208 def_or_undef_macro (pfile, "__ARM_FEATURE_UNALIGNED", unaligned_access);
210 def_or_undef_macro (pfile, "__ARM_FEATURE_QRDMX", TARGET_NEON_RDMA);
212 def_or_undef_macro (pfile, "__ARM_FEATURE_CRC32", TARGET_CRC32);
213 def_or_undef_macro (pfile, "__ARM_FEATURE_DOTPROD", TARGET_DOTPROD);
214 def_or_undef_macro (pfile, "__ARM_FEATURE_COMPLEX", TARGET_COMPLEX);
215 def_or_undef_macro (pfile, "__ARM_32BIT_STATE", TARGET_32BIT);
217 def_or_undef_macro (pfile, "__ARM_FEATURE_PAUTH", TARGET_HAVE_PACBTI);
218 def_or_undef_macro (pfile, "__ARM_FEATURE_BTI", TARGET_HAVE_PACBTI);
219 def_or_undef_macro (pfile, "__ARM_FEATURE_BTI_DEFAULT",
220 aarch_enable_bti == 1);
222 cpp_undef (pfile, "__ARM_FEATURE_PAC_DEFAULT");
223 if (aarch_ra_sign_scope != AARCH_FUNCTION_NONE)
225 unsigned int pac = 1;
227 gcc_assert (aarch_ra_sign_key == AARCH_KEY_A);
229 if (aarch_ra_sign_scope == AARCH_FUNCTION_ALL)
230 pac |= 0x4;
232 builtin_define_with_int_value ("__ARM_FEATURE_PAC_DEFAULT", pac);
235 cpp_undef (pfile, "__ARM_FEATURE_MVE");
236 if (TARGET_HAVE_MVE && TARGET_HAVE_MVE_FLOAT)
238 builtin_define_with_int_value ("__ARM_FEATURE_MVE", 3);
240 else if (TARGET_HAVE_MVE)
242 builtin_define_with_int_value ("__ARM_FEATURE_MVE", 1);
245 cpp_undef (pfile, "__ARM_FEATURE_CMSE");
246 if (arm_arch8 && !arm_arch_notm)
248 if (arm_arch_cmse && use_cmse)
249 builtin_define_with_int_value ("__ARM_FEATURE_CMSE", 3);
250 else
251 builtin_define ("__ARM_FEATURE_CMSE");
254 cpp_undef (pfile, "__ARM_FEATURE_LDREX");
255 if (TARGET_ARM_FEATURE_LDREX)
256 builtin_define_with_int_value ("__ARM_FEATURE_LDREX",
257 TARGET_ARM_FEATURE_LDREX);
259 /* ACLE says that __ARM_FEATURE_CLZ is defined if the hardware
260 supports it; it's also clear that this doesn't mean the current
261 ISA, so we define this even when compiling for Thumb1 if the
262 target supports CLZ in A32. */
263 def_or_undef_macro (pfile, "__ARM_FEATURE_CLZ",
264 ((TARGET_ARM_ARCH >= 5 && arm_arch_notm)
265 || TARGET_ARM_ARCH_ISA_THUMB >=2));
267 def_or_undef_macro (pfile, "__ARM_FEATURE_NUMERIC_MAXMIN",
268 TARGET_ARM_ARCH >= 8 && TARGET_NEON && TARGET_VFP5);
270 def_or_undef_macro (pfile, "__ARM_FEATURE_SIMD32", TARGET_INT_SIMD);
272 builtin_define_with_int_value ("__ARM_SIZEOF_MINIMAL_ENUM",
273 flag_short_enums ? 1 : 4);
274 builtin_define_type_sizeof ("__ARM_SIZEOF_WCHAR_T", wchar_type_node);
276 cpp_undef (pfile, "__ARM_ARCH_PROFILE");
277 if (TARGET_ARM_ARCH_PROFILE)
278 builtin_define_with_int_value ("__ARM_ARCH_PROFILE",
279 TARGET_ARM_ARCH_PROFILE);
281 /* Define __arm__ even when in thumb mode, for
282 consistency with armcc. */
283 builtin_define ("__arm__");
284 if (TARGET_ARM_ARCH)
286 cpp_undef (pfile, "__ARM_ARCH");
287 builtin_define_with_int_value ("__ARM_ARCH", TARGET_ARM_ARCH);
289 if (arm_arch_notm)
290 builtin_define ("__ARM_ARCH_ISA_ARM");
291 builtin_define ("__APCS_32__");
293 def_or_undef_macro (pfile, "__GCC_ASM_FLAG_OUTPUTS__", !TARGET_THUMB1);
295 def_or_undef_macro (pfile, "__thumb__", TARGET_THUMB);
296 def_or_undef_macro (pfile, "__thumb2__", TARGET_THUMB2);
297 if (TARGET_BIG_END)
298 def_or_undef_macro (pfile, "__THUMBEB__", TARGET_THUMB);
299 else
300 def_or_undef_macro (pfile, "__THUMBEL__", TARGET_THUMB);
302 cpp_undef (pfile, "__ARM_ARCH_ISA_THUMB");
303 if (TARGET_ARM_ARCH_ISA_THUMB)
304 builtin_define_with_int_value ("__ARM_ARCH_ISA_THUMB",
305 TARGET_ARM_ARCH_ISA_THUMB);
307 if (TARGET_BIG_END)
309 builtin_define ("__ARMEB__");
310 builtin_define ("__ARM_BIG_ENDIAN");
312 else
314 builtin_define ("__ARMEL__");
317 if (TARGET_SOFT_FLOAT)
318 builtin_define ("__SOFTFP__");
320 builtin_define ("__VFP_FP__");
322 cpp_undef (pfile, "__ARM_FP");
323 if (TARGET_ARM_FP)
324 builtin_define_with_int_value ("__ARM_FP", TARGET_ARM_FP);
326 def_or_undef_macro (pfile, "__ARM_FP16_FORMAT_IEEE",
327 arm_fp16_format == ARM_FP16_FORMAT_IEEE);
328 def_or_undef_macro (pfile, "__ARM_FP16_FORMAT_ALTERNATIVE",
329 arm_fp16_format == ARM_FP16_FORMAT_ALTERNATIVE);
330 def_or_undef_macro (pfile, "__ARM_FP16_ARGS",
331 arm_fp16_format != ARM_FP16_FORMAT_NONE);
333 def_or_undef_macro (pfile, "__ARM_FEATURE_FP16_SCALAR_ARITHMETIC",
334 TARGET_VFP_FP16INST);
335 def_or_undef_macro (pfile, "__ARM_FEATURE_FP16_VECTOR_ARITHMETIC",
336 TARGET_NEON_FP16INST);
337 def_or_undef_macro (pfile, "__ARM_FEATURE_FP16_FML", TARGET_FP16FML);
339 def_or_undef_macro (pfile, "__ARM_FEATURE_FMA", TARGET_FMA);
340 def_or_undef_macro (pfile, "__ARM_NEON__", TARGET_NEON);
341 def_or_undef_macro (pfile, "__ARM_NEON", TARGET_NEON);
343 cpp_undef (pfile, "__ARM_NEON_FP");
344 if (TARGET_NEON_FP)
345 builtin_define_with_int_value ("__ARM_NEON_FP", TARGET_NEON_FP);
347 /* Add a define for interworking. Needed when building libgcc.a. */
348 if (arm_cpp_interwork)
349 builtin_define ("__THUMB_INTERWORK__");
351 builtin_define (arm_arch_name);
352 if (arm_arch_xscale)
353 builtin_define ("__XSCALE__");
354 if (arm_arch_iwmmxt)
356 builtin_define ("__IWMMXT__");
357 builtin_define ("__ARM_WMMX");
359 if (arm_arch_iwmmxt2)
360 builtin_define ("__IWMMXT2__");
361 /* ARMv6KZ was originally identified as the misspelled __ARM_ARCH_6ZK__. To
362 preserve the existing behavior, the misspelled feature macro must still be
363 defined. */
364 if (arm_arch6kz)
365 builtin_define ("__ARM_ARCH_6ZK__");
366 if (TARGET_AAPCS_BASED)
368 if (arm_pcs_default == ARM_PCS_AAPCS_VFP)
369 builtin_define ("__ARM_PCS_VFP");
370 else if (arm_pcs_default == ARM_PCS_AAPCS)
371 builtin_define ("__ARM_PCS");
372 builtin_define ("__ARM_EABI__");
375 def_or_undef_macro (pfile, "__FDPIC__", TARGET_FDPIC);
377 def_or_undef_macro (pfile, "__ARM_ARCH_EXT_IDIV__", TARGET_IDIV);
378 def_or_undef_macro (pfile, "__ARM_FEATURE_IDIV", TARGET_IDIV);
380 def_or_undef_macro (pfile, "__ARM_ASM_SYNTAX_UNIFIED__", inline_asm_unified);
382 cpp_undef (pfile, "__ARM_FEATURE_COPROC");
383 if (TARGET_32BIT && arm_arch4 && !(arm_arch8 && arm_arch_notm))
385 int coproc_level = 0x1;
387 if (arm_arch5t)
388 coproc_level |= 0x2;
389 if (arm_arch5te)
390 coproc_level |= 0x4;
391 if (arm_arch6)
392 coproc_level |= 0x8;
394 builtin_define_with_int_value ("__ARM_FEATURE_COPROC", coproc_level);
397 def_or_undef_macro (pfile, "__ARM_FEATURE_CDE", TARGET_CDE);
398 cpp_undef (pfile, "__ARM_FEATURE_CDE_COPROC");
399 if (TARGET_CDE)
400 builtin_define_with_int_value ("__ARM_FEATURE_CDE_COPROC",
401 arm_arch_cde_coproc);
403 def_or_undef_macro (pfile, "__ARM_FEATURE_MATMUL_INT8", TARGET_I8MM);
404 def_or_undef_macro (pfile, "__ARM_FEATURE_BF16_SCALAR_ARITHMETIC",
405 TARGET_BF16_FP);
406 def_or_undef_macro (pfile, "__ARM_FEATURE_BF16_VECTOR_ARITHMETIC",
407 TARGET_BF16_SIMD);
408 def_or_undef_macro (pfile, "__ARM_BF16_FORMAT_ALTERNATIVE",
409 TARGET_BF16_FP || TARGET_BF16_SIMD);
412 void
413 arm_cpu_cpp_builtins (struct cpp_reader * pfile)
415 builtin_assert ("cpu=arm");
416 builtin_assert ("machine=arm");
418 arm_cpu_builtins (pfile);
421 /* Hook to validate the current #pragma GCC target and set the arch custom
422 mode state. If ARGS is NULL, then POP_TARGET is used to reset
423 the options. */
425 static bool
426 arm_pragma_target_parse (tree args, tree pop_target)
428 tree prev_tree = target_option_current_node;
429 tree cur_tree;
430 struct cl_target_option *prev_opt;
431 struct cl_target_option *cur_opt;
433 if (! args)
435 cur_tree = ((pop_target) ? pop_target : target_option_default_node);
436 cl_target_option_restore (&global_options, &global_options_set,
437 TREE_TARGET_OPTION (cur_tree));
439 else
441 cur_tree = arm_valid_target_attribute_tree (args, &global_options,
442 &global_options_set);
443 if (cur_tree == NULL_TREE)
445 cl_target_option_restore (&global_options, &global_options_set,
446 TREE_TARGET_OPTION (prev_tree));
447 return false;
450 /* handle_pragma_pop_options and handle_pragma_reset_options will set
451 target_option_current_node, but not handle_pragma_target. */
452 target_option_current_node = cur_tree;
453 arm_configure_build_target (&arm_active_target,
454 TREE_TARGET_OPTION (cur_tree), false);
455 arm_option_reconfigure_globals ();
458 /* Update macros if target_node changes. The global state will be restored
459 by arm_set_current_function. */
460 prev_opt = TREE_TARGET_OPTION (prev_tree);
461 cur_opt = TREE_TARGET_OPTION (cur_tree);
463 gcc_assert (prev_opt);
464 gcc_assert (cur_opt);
466 if (cur_opt != prev_opt)
468 /* For the definitions, ensure all newly defined macros are considered
469 as used for -Wunused-macros. There is no point warning about the
470 compiler predefined macros. */
471 cpp_options *cpp_opts = cpp_get_options (parse_in);
472 unsigned char saved_warn_unused_macros = cpp_opts->warn_unused_macros;
474 cpp_opts->warn_unused_macros = 0;
476 /* Update macros. */
477 gcc_assert (cur_opt->x_target_flags == target_flags);
479 /* Don't warn for macros that have context sensitive values depending on
480 other attributes.
481 See warn_of_redefinition, reset after cpp_create_definition. */
482 tree acond_macro = get_identifier ("__ARM_NEON_FP");
483 C_CPP_HASHNODE (acond_macro)->flags |= NODE_CONDITIONAL ;
485 acond_macro = get_identifier ("__ARM_FP");
486 C_CPP_HASHNODE (acond_macro)->flags |= NODE_CONDITIONAL;
488 acond_macro = get_identifier ("__ARM_FEATURE_LDREX");
489 C_CPP_HASHNODE (acond_macro)->flags |= NODE_CONDITIONAL;
491 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
492 arm_cpu_builtins (parse_in);
493 cpp_stop_forcing_token_locations (parse_in);
495 cpp_opts->warn_unused_macros = saved_warn_unused_macros;
497 /* Make sure that target_reinit is called for next function, since
498 TREE_TARGET_OPTION might change with the #pragma even if there is
499 no target attribute attached to the function. */
500 arm_reset_previous_fndecl ();
502 /* If going to the default mode, we restore the initial states.
503 if cur_tree is a new target, states will be saved/restored on a per
504 function basis in arm_set_current_function. */
505 if (cur_tree == target_option_default_node)
506 save_restore_target_globals (cur_tree);
509 return true;
512 /* Register target pragmas. We need to add the hook for parsing #pragma GCC
513 option here rather than in arm.cc since it will pull in various preprocessor
514 functions, and those are not present in languages like fortran without a
515 preprocessor. */
517 void
518 arm_register_target_pragmas (void)
520 /* Update pragma hook to allow parsing #pragma GCC target. */
521 targetm.target_option.pragma_parse = arm_pragma_target_parse;
522 targetm.resolve_overloaded_builtin = arm_resolve_overloaded_builtin;
524 c_register_pragma ("GCC", "arm", arm_pragma_arm);
526 #ifdef REGISTER_SUBTARGET_PRAGMAS
527 REGISTER_SUBTARGET_PRAGMAS ();
528 #endif