1 /* RISC-V-specific code for C family languages.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 Contributed by Andrew Waterman (andrew@sifive.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #define IN_TARGET_CODE 1
23 #define INCLUDE_STRING
26 #include "coretypes.h"
28 #include "c-family/c-common.h"
30 #include "c-family/c-pragma.h"
33 #include "riscv-subset.h"
35 #define builtin_define(TXT) cpp_define (pfile, TXT)
38 riscv_ext_version_value (unsigned major
, unsigned minor
)
40 return (major
* RISCV_MAJOR_VERSION_BASE
)
41 + (minor
* RISCV_MINOR_VERSION_BASE
);
44 /* Implement TARGET_CPU_CPP_BUILTINS. */
47 riscv_cpu_cpp_builtins (cpp_reader
*pfile
)
49 builtin_define ("__riscv");
51 if (TARGET_RVC
|| TARGET_ZCA
)
52 builtin_define ("__riscv_compressed");
55 builtin_define (TARGET_64BIT
? "__riscv_64e" : "__riscv_32e");
58 builtin_define ("__riscv_atomic");
61 builtin_define ("__riscv_mul");
63 builtin_define ("__riscv_div");
64 if (TARGET_DIV
&& TARGET_MUL
)
65 builtin_define ("__riscv_muldiv");
67 builtin_define_with_int_value ("__riscv_xlen", UNITS_PER_WORD
* 8);
68 if (TARGET_HARD_FLOAT
)
69 builtin_define_with_int_value ("__riscv_flen", UNITS_PER_FP_REG
* 8);
71 if ((TARGET_HARD_FLOAT
|| TARGET_ZFINX
) && TARGET_FDIV
)
73 builtin_define ("__riscv_fdiv");
74 builtin_define ("__riscv_fsqrt");
81 builtin_define ("__riscv_abi_rve");
86 builtin_define ("__riscv_float_abi_soft");
91 builtin_define ("__riscv_float_abi_single");
96 builtin_define ("__riscv_float_abi_double");
100 switch (riscv_cmodel
)
103 builtin_define ("__riscv_cmodel_medlow");
107 builtin_define ("__riscv_cmodel_large");
112 builtin_define ("__riscv_cmodel_medany");
116 if (riscv_user_wants_strict_align
)
117 builtin_define_with_int_value ("__riscv_misaligned_avoid", 1);
118 else if (riscv_slow_unaligned_access_p
)
119 builtin_define_with_int_value ("__riscv_misaligned_slow", 1);
121 builtin_define_with_int_value ("__riscv_misaligned_fast", 1);
123 if (TARGET_MIN_VLEN
!= 0)
124 builtin_define_with_int_value ("__riscv_v_min_vlen", TARGET_MIN_VLEN
);
126 if (TARGET_VECTOR_ELEN_64
)
127 builtin_define_with_int_value ("__riscv_v_elen", 64);
128 else if (TARGET_VECTOR_ELEN_32
)
129 builtin_define_with_int_value ("__riscv_v_elen", 32);
131 if (TARGET_VECTOR_ELEN_FP_64
)
132 builtin_define_with_int_value ("__riscv_v_elen_fp", 64);
133 else if (TARGET_VECTOR_ELEN_FP_32
)
134 builtin_define_with_int_value ("__riscv_v_elen_fp", 32);
135 else if (TARGET_MIN_VLEN
!= 0)
136 builtin_define_with_int_value ("__riscv_v_elen_fp", 0);
140 builtin_define ("__riscv_vector");
141 builtin_define_with_int_value ("__riscv_v_intrinsic",
142 riscv_ext_version_value (0, 12));
144 if (rvv_vector_bits
== RVV_VECTOR_BITS_ZVL
)
145 builtin_define_with_int_value ("__riscv_v_fixed_vlen", TARGET_MIN_VLEN
);
148 if (TARGET_XTHEADVECTOR
)
149 builtin_define_with_int_value ("__riscv_th_v_intrinsic",
150 riscv_ext_version_value (0, 11));
152 /* Define architecture extension test macros. */
153 builtin_define_with_int_value ("__riscv_arch_test", 1);
155 const riscv_subset_list
*subset_list
= riscv_current_subset_list ();
159 size_t max_ext_len
= 0;
161 /* Figure out the max length of extension name for reserving buffer. */
162 for (const riscv_subset_t
*subset
= subset_list
->begin ();
163 subset
!= subset_list
->end ();
164 subset
= subset
->next
)
165 max_ext_len
= MAX (max_ext_len
, subset
->name
.length ());
167 char *buf
= (char *)alloca (max_ext_len
+ 10 /* For __riscv_ and '\0'. */);
169 for (const riscv_subset_t
*subset
= subset_list
->begin ();
170 subset
!= subset_list
->end ();
171 subset
= subset
->next
)
173 int version_value
= riscv_ext_version_value (subset
->major_version
,
174 subset
->minor_version
);
175 /* Special rule for zicsr and zifencei, it's used for ISA spec 2.2 or
177 if ((subset
->name
== "zicsr" || subset
->name
== "zifencei")
178 && version_value
== 0)
179 version_value
= riscv_ext_version_value (2, 0);
181 sprintf (buf
, "__riscv_%s", subset
->name
.c_str ());
182 builtin_define_with_int_value (buf
, version_value
);
186 /* Implement "#pragma riscv intrinsic". */
189 riscv_pragma_intrinsic (cpp_reader
*)
193 if (pragma_lex (&x
) != CPP_STRING
)
195 error ("%<#pragma riscv intrinsic%> requires a string parameter");
199 const char *name
= TREE_STRING_POINTER (x
);
201 if (strcmp (name
, "vector") == 0
202 || strcmp (name
, "xtheadvector") == 0)
205 riscv_vector::handle_pragma_vector ();
206 else /* Indicates riscv_vector.h is included but v is missing in arch */
208 /* To make the the rvv types and intrinsic API available for the
209 target("arch=+v") attribute, we need to temporally enable the
210 TARGET_VECTOR, and disable it after all initialized. */
211 target_flags
|= MASK_VECTOR
;
213 riscv_vector::init_builtins ();
214 riscv_vector::handle_pragma_vector ();
216 target_flags
&= ~MASK_VECTOR
;
220 error ("unknown %<#pragma riscv intrinsic%> option %qs", name
);
223 /* Implement TARGET_CHECK_BUILTIN_CALL. */
225 riscv_check_builtin_call (location_t loc
, vec
<location_t
> arg_loc
, tree fndecl
,
226 tree
, unsigned int nargs
, tree
*args
)
228 unsigned int code
= DECL_MD_FUNCTION_CODE (fndecl
);
229 unsigned int subcode
= code
>> RISCV_BUILTIN_SHIFT
;
230 switch (code
& RISCV_BUILTIN_CLASS
)
232 case RISCV_BUILTIN_GENERAL
:
235 case RISCV_BUILTIN_VECTOR
:
236 return riscv_vector::check_builtin_call (loc
, arg_loc
, subcode
,
237 fndecl
, nargs
, args
);
242 /* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN. */
244 riscv_resolve_overloaded_builtin (unsigned int uncast_location
, tree fndecl
,
245 void *uncast_arglist
)
247 vec
<tree
, va_gc
> empty
= {};
248 location_t loc
= (location_t
) uncast_location
;
249 vec
<tree
, va_gc
> *arglist
= (vec
<tree
, va_gc
> *) uncast_arglist
;
250 unsigned int code
= DECL_MD_FUNCTION_CODE (fndecl
);
251 unsigned int subcode
= code
>> RISCV_BUILTIN_SHIFT
;
252 tree new_fndecl
= NULL_TREE
;
257 switch (code
& RISCV_BUILTIN_CLASS
)
259 case RISCV_BUILTIN_GENERAL
:
261 case RISCV_BUILTIN_VECTOR
:
262 new_fndecl
= riscv_vector::resolve_overloaded_builtin (loc
, subcode
,
269 if (new_fndecl
== NULL_TREE
)
272 return build_function_call_vec (loc
, vNULL
, new_fndecl
, arglist
, NULL
,
276 /* Implement REGISTER_TARGET_PRAGMAS. */
279 riscv_register_pragmas (void)
281 targetm
.resolve_overloaded_builtin
= riscv_resolve_overloaded_builtin
;
282 targetm
.check_builtin_call
= riscv_check_builtin_call
;
283 c_register_pragma ("riscv", "intrinsic", riscv_pragma_intrinsic
);