riscv-c.cc revision 1.1 1 1.1 mrg /* RISC-V-specific code for C family languages.
2 1.1 mrg Copyright (C) 2011-2022 Free Software Foundation, Inc.
3 1.1 mrg Contributed by Andrew Waterman (andrew (at) sifive.com).
4 1.1 mrg
5 1.1 mrg This file is part of GCC.
6 1.1 mrg
7 1.1 mrg GCC is free software; you can redistribute it and/or modify
8 1.1 mrg it under the terms of the GNU General Public License as published by
9 1.1 mrg the Free Software Foundation; either version 3, or (at your option)
10 1.1 mrg any later version.
11 1.1 mrg
12 1.1 mrg GCC is distributed in the hope that it will be useful,
13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 mrg GNU General Public License for more details.
16 1.1 mrg
17 1.1 mrg You should have received a copy of the GNU General Public License
18 1.1 mrg along with GCC; see the file COPYING3. If not see
19 1.1 mrg <http://www.gnu.org/licenses/>. */
20 1.1 mrg
21 1.1 mrg #define IN_TARGET_CODE 1
22 1.1 mrg
23 1.1 mrg #define INCLUDE_STRING
24 1.1 mrg #include "config.h"
25 1.1 mrg #include "system.h"
26 1.1 mrg #include "coretypes.h"
27 1.1 mrg #include "tm.h"
28 1.1 mrg #include "c-family/c-common.h"
29 1.1 mrg #include "cpplib.h"
30 1.1 mrg #include "riscv-subset.h"
31 1.1 mrg
32 1.1 mrg #define builtin_define(TXT) cpp_define (pfile, TXT)
33 1.1 mrg
34 1.1 mrg /* Implement TARGET_CPU_CPP_BUILTINS. */
35 1.1 mrg
36 1.1 mrg void
37 1.1 mrg riscv_cpu_cpp_builtins (cpp_reader *pfile)
38 1.1 mrg {
39 1.1 mrg builtin_define ("__riscv");
40 1.1 mrg
41 1.1 mrg if (TARGET_RVC)
42 1.1 mrg builtin_define ("__riscv_compressed");
43 1.1 mrg
44 1.1 mrg if (TARGET_RVE)
45 1.1 mrg builtin_define ("__riscv_32e");
46 1.1 mrg
47 1.1 mrg if (TARGET_ATOMIC)
48 1.1 mrg builtin_define ("__riscv_atomic");
49 1.1 mrg
50 1.1 mrg if (TARGET_MUL)
51 1.1 mrg builtin_define ("__riscv_mul");
52 1.1 mrg if (TARGET_DIV)
53 1.1 mrg builtin_define ("__riscv_div");
54 1.1 mrg if (TARGET_DIV && TARGET_MUL)
55 1.1 mrg builtin_define ("__riscv_muldiv");
56 1.1 mrg
57 1.1 mrg builtin_define_with_int_value ("__riscv_xlen", UNITS_PER_WORD * 8);
58 1.1 mrg if (TARGET_HARD_FLOAT)
59 1.1 mrg builtin_define_with_int_value ("__riscv_flen", UNITS_PER_FP_REG * 8);
60 1.1 mrg
61 1.1 mrg if (TARGET_HARD_FLOAT && TARGET_FDIV)
62 1.1 mrg {
63 1.1 mrg builtin_define ("__riscv_fdiv");
64 1.1 mrg builtin_define ("__riscv_fsqrt");
65 1.1 mrg }
66 1.1 mrg
67 1.1 mrg switch (riscv_abi)
68 1.1 mrg {
69 1.1 mrg case ABI_ILP32E:
70 1.1 mrg builtin_define ("__riscv_abi_rve");
71 1.1 mrg gcc_fallthrough ();
72 1.1 mrg
73 1.1 mrg case ABI_ILP32:
74 1.1 mrg case ABI_LP64:
75 1.1 mrg builtin_define ("__riscv_float_abi_soft");
76 1.1 mrg break;
77 1.1 mrg
78 1.1 mrg case ABI_ILP32F:
79 1.1 mrg case ABI_LP64F:
80 1.1 mrg builtin_define ("__riscv_float_abi_single");
81 1.1 mrg break;
82 1.1 mrg
83 1.1 mrg case ABI_ILP32D:
84 1.1 mrg case ABI_LP64D:
85 1.1 mrg builtin_define ("__riscv_float_abi_double");
86 1.1 mrg break;
87 1.1 mrg }
88 1.1 mrg
89 1.1 mrg switch (riscv_cmodel)
90 1.1 mrg {
91 1.1 mrg case CM_MEDLOW:
92 1.1 mrg builtin_define ("__riscv_cmodel_medlow");
93 1.1 mrg break;
94 1.1 mrg
95 1.1 mrg case CM_PIC:
96 1.1 mrg /* __riscv_cmodel_pic is deprecated, and will removed in next GCC release.
97 1.1 mrg see https://github.com/riscv/riscv-c-api-doc/pull/11 */
98 1.1 mrg builtin_define ("__riscv_cmodel_pic");
99 1.1 mrg /* FALLTHROUGH. */
100 1.1 mrg
101 1.1 mrg case CM_MEDANY:
102 1.1 mrg builtin_define ("__riscv_cmodel_medany");
103 1.1 mrg break;
104 1.1 mrg
105 1.1 mrg }
106 1.1 mrg
107 1.1 mrg if (TARGET_MIN_VLEN != 0)
108 1.1 mrg builtin_define_with_int_value ("__riscv_v_min_vlen", TARGET_MIN_VLEN);
109 1.1 mrg
110 1.1 mrg if (TARGET_VECTOR_ELEN_64)
111 1.1 mrg builtin_define_with_int_value ("__riscv_v_elen", 64);
112 1.1 mrg else if (TARGET_VECTOR_ELEN_32)
113 1.1 mrg builtin_define_with_int_value ("__riscv_v_elen", 32);
114 1.1 mrg
115 1.1 mrg if (TARGET_VECTOR_ELEN_FP_64)
116 1.1 mrg builtin_define_with_int_value ("__riscv_v_elen_fp", 64);
117 1.1 mrg else if (TARGET_VECTOR_ELEN_FP_32)
118 1.1 mrg builtin_define_with_int_value ("__riscv_v_elen_fp", 32);
119 1.1 mrg else if (TARGET_MIN_VLEN != 0)
120 1.1 mrg builtin_define_with_int_value ("__riscv_v_elen_fp", 0);
121 1.1 mrg
122 1.1 mrg if (TARGET_MIN_VLEN)
123 1.1 mrg builtin_define ("__riscv_vector");
124 1.1 mrg
125 1.1 mrg /* Define architecture extension test macros. */
126 1.1 mrg builtin_define_with_int_value ("__riscv_arch_test", 1);
127 1.1 mrg
128 1.1 mrg const riscv_subset_list *subset_list = riscv_current_subset_list ();
129 1.1 mrg if (!subset_list)
130 1.1 mrg return;
131 1.1 mrg
132 1.1 mrg size_t max_ext_len = 0;
133 1.1 mrg
134 1.1 mrg /* Figure out the max length of extension name for reserving buffer. */
135 1.1 mrg for (const riscv_subset_t *subset = subset_list->begin ();
136 1.1 mrg subset != subset_list->end ();
137 1.1 mrg subset = subset->next)
138 1.1 mrg max_ext_len = MAX (max_ext_len, subset->name.length ());
139 1.1 mrg
140 1.1 mrg char *buf = (char *)alloca (max_ext_len + 10 /* For __riscv_ and '\0'. */);
141 1.1 mrg
142 1.1 mrg for (const riscv_subset_t *subset = subset_list->begin ();
143 1.1 mrg subset != subset_list->end ();
144 1.1 mrg subset = subset->next)
145 1.1 mrg {
146 1.1 mrg int version_value = (subset->major_version * 1000000)
147 1.1 mrg + (subset->minor_version * 1000);
148 1.1 mrg /* Special rule for zicsr and zifencei, it's used for ISA spec 2.2 or
149 1.1 mrg earlier. */
150 1.1 mrg if ((subset->name == "zicsr" || subset->name == "zifencei")
151 1.1 mrg && version_value == 0)
152 1.1 mrg version_value = 2000000;
153 1.1 mrg
154 1.1 mrg sprintf (buf, "__riscv_%s", subset->name.c_str ());
155 1.1 mrg builtin_define_with_int_value (buf, version_value);
156 1.1 mrg }
157 1.1 mrg }
158