Debian Codegraph @

gcc/config/loongarch/loongarch-c.cc

raw
1 /* LoongArch-specific code for C family languages.
2 Copyright (C) 2021-2025 Free Software Foundation, Inc.
3 Contributed by Loongson Ltd.
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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/>. */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "target.h"
27 #include "tm.h"
28 #include "c-family/c-common.h"
29 #include "cpplib.h"
30 #include "c-family/c-pragma.h"
31 #include "tm_p.h"
32
33 #define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
34 #define builtin_define(TXT) cpp_define (pfile, TXT)
35 #define builtin_undef(TXT) cpp_undef (pfile, TXT)
36 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
37
38 static void
39 loongarch_def_or_undef (bool def_p, const char *macro, cpp_reader *pfile)
40 {
41 if (def_p)
42 cpp_define (pfile, macro);
43 else
44 cpp_undef (pfile, macro);
45 }
46
47 static void
48 loongarch_define_unconditional_macros (cpp_reader *pfile)
49 {
50 builtin_define ("__loongarch__");
51
52 /* Base architecture / ABI. */
53 if (TARGET_64BIT)
54 {
55 builtin_define ("__loongarch_grlen=64");
56 builtin_define ("__loongarch64");
57 }
58
59 if (TARGET_ABI_LP64)
60 {
61 builtin_define ("_ABILP64=3");
62 builtin_define ("_LOONGARCH_SIM=_ABILP64");
63 builtin_define ("__loongarch_lp64");
64 }
65
66 /* Add support for FLOAT128_TYPE on the LoongArch architecture. */
67 builtin_define ("__FLOAT128_TYPE__");
68
69 /* Map the old _Float128 'q' builtins into the new 'f128' builtins. */
70 builtin_define ("__builtin_fabsq=__builtin_fabsf128");
71 builtin_define ("__builtin_copysignq=__builtin_copysignf128");
72 builtin_define ("__builtin_nanq=__builtin_nanf128");
73 builtin_define ("__builtin_nansq=__builtin_nansf128");
74 builtin_define ("__builtin_infq=__builtin_inff128");
75 builtin_define ("__builtin_huge_valq=__builtin_huge_valf128");
76
77 /* Native Data Sizes. */
78 builtin_define_with_int_value ("_LOONGARCH_SZINT", INT_TYPE_SIZE);
79 builtin_define_with_int_value ("_LOONGARCH_SZLONG", LONG_TYPE_SIZE);
80 builtin_define_with_int_value ("_LOONGARCH_SZPTR", POINTER_SIZE);
81 builtin_define_with_int_value ("_LOONGARCH_FPSET", 32);
82 builtin_define_with_int_value ("_LOONGARCH_SPFPSET", 32);
83 }
84
85 static void
86 loongarch_update_cpp_builtins (cpp_reader *pfile)
87 {
88 /* Since the macros in this function might be redefined, it's necessary to
89 undef them first.*/
90 builtin_undef ("__loongarch_arch");
91 builtin_define_with_value ("__loongarch_arch",
92 loongarch_arch_strings[la_target.cpu_arch], 1);
93
94 builtin_undef ("__loongarch_tune");
95 builtin_define_with_value ("__loongarch_tune",
96 loongarch_tune_strings[la_target.cpu_tune], 1);
97
98 builtin_undef ("_LOONGARCH_ARCH");
99 builtin_define_with_value ("_LOONGARCH_ARCH",
100 loongarch_arch_strings[la_target.cpu_arch], 1);
101
102 builtin_undef ("_LOONGARCH_TUNE");
103 builtin_define_with_value ("_LOONGARCH_TUNE",
104 loongarch_tune_strings[la_target.cpu_tune], 1);
105
106 builtin_undef ("__loongarch_double_float");
107 builtin_undef ("__loongarch_single_float");
108 /* These defines reflect the ABI in use, not whether the
109 FPU is directly accessible. */
110 if (TARGET_DOUBLE_FLOAT_ABI)
111 builtin_define ("__loongarch_double_float=1");
112 else if (TARGET_SINGLE_FLOAT_ABI)
113 builtin_define ("__loongarch_single_float=1");
114
115 builtin_undef ("__loongarch_soft_float");
116 builtin_undef ("__loongarch_hard_float");
117 if (TARGET_DOUBLE_FLOAT_ABI || TARGET_SINGLE_FLOAT_ABI)
118 builtin_define ("__loongarch_hard_float=1");
119 else
120 builtin_define ("__loongarch_soft_float=1");
121
122
123 /* ISA Extensions. */
124 builtin_undef ("__loongarch_frlen");
125 if (TARGET_DOUBLE_FLOAT)
126 builtin_define ("__loongarch_frlen=64");
127 else if (TARGET_SINGLE_FLOAT)
128 builtin_define ("__loongarch_frlen=32");
129 else
130 builtin_define ("__loongarch_frlen=0");
131
132 loongarch_def_or_undef (ISA_HAS_LSX, "__loongarch_simd", pfile);
133 loongarch_def_or_undef (ISA_HAS_LSX, "__loongarch_sx", pfile);
134 loongarch_def_or_undef (ISA_HAS_LASX, "__loongarch_asx", pfile);
135
136 builtin_undef ("__loongarch_simd_width");
137 if (ISA_HAS_LSX)
138 {
139 if (ISA_HAS_LASX)
140 builtin_define ("__loongarch_simd_width=256");
141 else
142 builtin_define ("__loongarch_simd_width=128");
143 }
144
145 /* ISA evolution features */
146 int max_v_major = 1, max_v_minor = 0;
147
148 for (int i = 0; i < N_EVO_FEATURES; i++)
149 {
150 builtin_undef (la_evo_macro_name[i]);
151
152 if (la_target.isa.evolution & la_evo_feature_masks[i]
153 && (la_evo_feature_masks[i] != OPTION_MASK_ISA_FRECIPE
154 || TARGET_HARD_FLOAT))
155 {
156 builtin_define (la_evo_macro_name[i]);
157
158 int major = la_evo_version_major[i],
159 minor = la_evo_version_minor[i];
160
161 max_v_major = major > max_v_major ? major : max_v_major;
162 max_v_minor = major == max_v_major
163 ? (minor > max_v_minor ? minor : max_v_minor) : max_v_minor;
164 }
165 }
166
167 /* Find the minimum ISA version required to run the target program. */
168 builtin_undef ("__loongarch_version_major");
169 builtin_undef ("__loongarch_version_minor");
170 if (!(max_v_major == 1 && max_v_minor <= 1 && ISA_HAS_LASX))
171 {
172 builtin_define_with_int_value ("__loongarch_version_major", max_v_major);
173 builtin_define_with_int_value ("__loongarch_version_minor", max_v_minor);
174 }
175 }
176
177 void
178 loongarch_cpu_cpp_builtins (cpp_reader *pfile)
179 {
180 builtin_assert ("machine=loongarch");
181 builtin_assert ("cpu=loongarch");
182
183 loongarch_define_unconditional_macros (pfile);
184 loongarch_update_cpp_builtins (pfile);
185 }
186
187 /* Hook to validate the current #pragma GCC target and set the state, and
188 update the macros based on what was changed. If ARGS is NULL, then
189 POP_TARGET is used to reset the options. */
190
191 static bool
192 loongarch_pragma_target_parse (tree args, tree pop_target)
193 {
194 /* If args is not NULL then process it and setup the target-specific
195 information that it specifies. */
196 if (args)
197 {
198 if (!loongarch_process_target_attr (args, NULL))
199 return false;
200
201 loongarch_option_override_internal (&la_target,
202 &global_options,
203 &global_options_set);
204 }
205
206 /* args is NULL, restore to the state described in pop_target. */
207 else
208 {
209 pop_target = pop_target ? pop_target : target_option_default_node;
210 cl_target_option_restore (&global_options, &global_options_set,
211 TREE_TARGET_OPTION (pop_target));
212 }
213
214 target_option_current_node
215 = build_target_option_node (&global_options, &global_options_set);
216
217 loongarch_reset_previous_fndecl ();
218
219 /* For the definitions, ensure all newly defined macros are considered
220 as used for -Wunused-macros. There is no point warning about the
221 compiler predefined macros. */
222 cpp_options *cpp_opts = cpp_get_options (parse_in);
223 unsigned char saved_warn_unused_macros = cpp_opts->warn_unused_macros;
224 cpp_opts->warn_unused_macros = 0;
225
226 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
227 loongarch_update_cpp_builtins (parse_in);
228 cpp_stop_forcing_token_locations (parse_in);
229
230 cpp_opts->warn_unused_macros = saved_warn_unused_macros;
231
232 /* If we're popping or reseting make sure to update the globals so that
233 the optab availability predicates get recomputed. */
234 if (pop_target)
235 loongarch_save_restore_target_globals (pop_target);
236
237 return true;
238 }
239
240 /* Implement REGISTER_TARGET_PRAGMAS. */
241
242 void
243 loongarch_register_pragmas (void)
244 {
245 /* Update pragma hook to allow parsing #pragma GCC target. */
246 targetm.target_option.pragma_parse = loongarch_pragma_target_parse;
247 }