1//===------ SemaPPC.cpp ------ PowerPC target-specific routines -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis functions specific to PowerPC.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Sema/SemaPPC.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Type.h"
19#include "clang/Basic/DiagnosticSema.h"
20#include "clang/Basic/SourceLocation.h"
21#include "clang/Basic/TargetBuiltins.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Sema/Sema.h"
24#include "llvm/ADT/APSInt.h"
25
26namespace clang {
27
28SemaPPC::SemaPPC(Sema &S) : SemaBase(S) {}
29
30void SemaPPC::checkAIXMemberAlignment(SourceLocation Loc, const Expr *Arg) {
31 const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Arg->IgnoreParens());
32 if (!ICE)
33 return;
34
35 const auto *DR = dyn_cast<DeclRefExpr>(Val: ICE->getSubExpr());
36 if (!DR)
37 return;
38
39 const auto *PD = dyn_cast<ParmVarDecl>(Val: DR->getDecl());
40 if (!PD || !PD->getType()->isRecordType())
41 return;
42
43 QualType ArgType = Arg->getType();
44 for (const FieldDecl *FD : ArgType->castAsRecordDecl()->fields()) {
45 if (const auto *AA = FD->getAttr<AlignedAttr>()) {
46 CharUnits Alignment = getASTContext().toCharUnitsFromBits(
47 BitSize: AA->getAlignment(Ctx&: getASTContext()));
48 if (Alignment.getQuantity() == 16) {
49 Diag(Loc: FD->getLocation(), DiagID: diag::warn_not_xl_compatible) << FD;
50 Diag(Loc, DiagID: diag::note_misaligned_member_used_here) << PD;
51 }
52 }
53 }
54}
55
56static bool isPPC_64Builtin(unsigned BuiltinID) {
57 // These builtins only work on PPC 64bit targets.
58 switch (BuiltinID) {
59 case PPC::BI__builtin_divde:
60 case PPC::BI__builtin_divdeu:
61 case PPC::BI__builtin_bpermd:
62 case PPC::BI__builtin_pdepd:
63 case PPC::BI__builtin_pextd:
64 case PPC::BI__builtin_ppc_cdtbcd:
65 case PPC::BI__builtin_ppc_cbcdtd:
66 case PPC::BI__builtin_ppc_addg6s:
67 case PPC::BI__builtin_ppc_ldarx:
68 case PPC::BI__builtin_ppc_stdcx:
69 case PPC::BI__builtin_ppc_tdw:
70 case PPC::BI__builtin_ppc_trapd:
71 case PPC::BI__builtin_ppc_cmpeqb:
72 case PPC::BI__builtin_ppc_setb:
73 case PPC::BI__builtin_ppc_mulhd:
74 case PPC::BI__builtin_ppc_mulhdu:
75 case PPC::BI__builtin_ppc_maddhd:
76 case PPC::BI__builtin_ppc_maddhdu:
77 case PPC::BI__builtin_ppc_maddld:
78 case PPC::BI__builtin_ppc_load8r:
79 case PPC::BI__builtin_ppc_store8r:
80 case PPC::BI__builtin_ppc_insert_exp:
81 case PPC::BI__builtin_ppc_extract_sig:
82 case PPC::BI__builtin_ppc_addex:
83 case PPC::BI__builtin_darn:
84 case PPC::BI__builtin_darn_raw:
85 case PPC::BI__builtin_ppc_compare_and_swaplp:
86 case PPC::BI__builtin_ppc_fetch_and_addlp:
87 case PPC::BI__builtin_ppc_fetch_and_andlp:
88 case PPC::BI__builtin_ppc_fetch_and_orlp:
89 case PPC::BI__builtin_ppc_fetch_and_swaplp:
90 case PPC::BI__builtin_amo_lwat:
91 case PPC::BI__builtin_amo_ldat:
92 case PPC::BI__builtin_amo_lwat_s:
93 case PPC::BI__builtin_amo_ldat_s:
94 case PPC::BI__builtin_amo_lwat_cond:
95 case PPC::BI__builtin_amo_ldat_cond:
96 case PPC::BI__builtin_amo_lwat_cond_s:
97 case PPC::BI__builtin_amo_ldat_cond_s:
98 case PPC::BI__builtin_amo_stwat:
99 case PPC::BI__builtin_amo_stdat:
100 case PPC::BI__builtin_amo_stwat_s:
101 case PPC::BI__builtin_amo_stdat_s:
102 return true;
103 }
104 return false;
105}
106
107bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
108 unsigned BuiltinID,
109 CallExpr *TheCall) {
110 ASTContext &Context = getASTContext();
111 bool IsTarget64Bit = TI.getTypeWidth(T: TI.getIntPtrType()) == 64;
112
113 if (isPPC_64Builtin(BuiltinID) && !IsTarget64Bit)
114 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_64_bit_builtin_32_bit_tgt)
115 << TheCall->getSourceRange();
116
117 // Common BCD type-validation helpers
118 // Emit error diagnostics and return true on success
119 // - IsTypeVecUChar: enforces vector unsigned char
120 // - IsIntType: enforces any integer type
121 // Lambdas centralize type checks for BCD builtin handlers
122
123 // Lambda 1: verify vector unsigned char type
124 auto IsTypeVecUChar = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
125 QualType VecType = Context.getVectorType(VectorType: Context.UnsignedCharTy, NumElts: 16,
126 VecKind: VectorKind::AltiVecVector);
127 if (Context.hasSameType(T1: ArgTy, T2: VecType))
128 return true;
129
130 Diag(Loc: TheCall->getArg(Arg: ArgIndex)->getBeginLoc(),
131 DiagID: diag::err_ppc_invalid_arg_type)
132 << ArgIndex << VecType << ArgTy;
133 return false;
134 };
135
136 // Lambda 2: verify integer type
137 auto IsIntType = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
138 if (ArgTy->isIntegerType())
139 return true;
140
141 Diag(Loc: TheCall->getArg(Arg: ArgIndex)->getBeginLoc(),
142 DiagID: diag::err_ppc_invalid_arg_type)
143 << ArgIndex << "integer" << ArgTy;
144 return false;
145 };
146
147 switch (BuiltinID) {
148 default:
149 return false;
150 case PPC::BI__builtin_ppc_bcdsetsign:
151 case PPC::BI__builtin_ppc_national2packed:
152 case PPC::BI__builtin_ppc_packed2zoned:
153 case PPC::BI__builtin_ppc_zoned2packed:
154 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
155 case PPC::BI__builtin_ppc_bcdshift:
156 case PPC::BI__builtin_ppc_bcdshiftround:
157 case PPC::BI__builtin_ppc_bcdtruncate: {
158
159 // Arg0 must be vector unsigned char
160 if (!IsTypeVecUChar(TheCall->getArg(Arg: 0)->getType(), 0))
161 return false;
162
163 // Arg1 must be integer type
164 if (!IsIntType(TheCall->getArg(Arg: 1)->getType(), 1))
165 return false;
166
167 // Restrict Arg2 constant range (0–1)
168 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 1);
169 }
170 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
171 case PPC::BI__builtin_altivec_crypto_vshasigmad:
172 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1) ||
173 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 15);
174 case PPC::BI__builtin_altivec_dss:
175 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 3);
176 case PPC::BI__builtin_tbegin:
177 case PPC::BI__builtin_tend:
178 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 1);
179 case PPC::BI__builtin_tsr:
180 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 7);
181 case PPC::BI__builtin_tabortwc:
182 case PPC::BI__builtin_tabortdc:
183 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31);
184 case PPC::BI__builtin_tabortwci:
185 case PPC::BI__builtin_tabortdci:
186 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31) ||
187 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 31);
188 // According to GCC 'Basic PowerPC Built-in Functions Available on ISA 2.05',
189 // __builtin_(un)pack_longdouble are available only if long double uses IBM
190 // extended double representation.
191 case PPC::BI__builtin_unpack_longdouble:
192 if (SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1))
193 return true;
194 [[fallthrough]];
195 case PPC::BI__builtin_pack_longdouble:
196 if (&TI.getLongDoubleFormat() != &llvm::APFloat::PPCDoubleDouble())
197 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_ppc_builtin_requires_abi)
198 << "ibmlongdouble";
199 return false;
200 case PPC::BI__builtin_altivec_dst:
201 case PPC::BI__builtin_altivec_dstt:
202 case PPC::BI__builtin_altivec_dstst:
203 case PPC::BI__builtin_altivec_dststt:
204 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 3);
205 case PPC::BI__builtin_vsx_xxpermdi:
206 case PPC::BI__builtin_vsx_xxsldwi:
207 return BuiltinVSX(TheCall);
208 case PPC::BI__builtin_unpack_vector_int128:
209 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
210 case PPC::BI__builtin_altivec_vgnb:
211 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 2, High: 7);
212 case PPC::BI__builtin_vsx_xxeval:
213 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 3, Low: 0, High: 255);
214 case PPC::BI__builtin_altivec_vsldbi:
215 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 7);
216 case PPC::BI__builtin_altivec_vsrdbi:
217 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 7);
218 case PPC::BI__builtin_vsx_xxpermx:
219 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 3, Low: 0, High: 7);
220 case PPC::BI__builtin_ppc_tw:
221 case PPC::BI__builtin_ppc_tdw:
222 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 1, High: 31);
223 case PPC::BI__builtin_ppc_cmprb:
224 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 1);
225 // For __rlwnm, __rlwimi and __rldimi, the last parameter mask must
226 // be a constant that represents a contiguous bit field.
227 case PPC::BI__builtin_ppc_rlwnm:
228 return SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 2);
229 case PPC::BI__builtin_ppc_rlwimi:
230 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 31) ||
231 SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 3);
232 case PPC::BI__builtin_ppc_rldimi:
233 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 63) ||
234 SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 3);
235 case PPC::BI__builtin_ppc_addex: {
236 if (SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 3))
237 return true;
238 // Output warning for reserved values 1 to 3.
239 int ArgValue =
240 TheCall->getArg(Arg: 2)->getIntegerConstantExpr(Ctx: Context)->getSExtValue();
241 if (ArgValue != 0)
242 Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::warn_argument_undefined_behaviour)
243 << ArgValue;
244 return false;
245 }
246 case PPC::BI__builtin_ppc_mtfsb0:
247 case PPC::BI__builtin_ppc_mtfsb1:
248 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31);
249 case PPC::BI__builtin_ppc_mtfsf:
250 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 255);
251 case PPC::BI__builtin_ppc_mtfsfi:
252 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 7) ||
253 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 15);
254 case PPC::BI__builtin_ppc_alignx:
255 return SemaRef.BuiltinConstantArgPower2(TheCall, ArgNum: 0);
256 case PPC::BI__builtin_ppc_rdlam:
257 return SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 2);
258 case PPC::BI__builtin_vsx_ldrmb:
259 case PPC::BI__builtin_vsx_strmb:
260 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 1, High: 16);
261 case PPC::BI__builtin_altivec_vcntmbb:
262 case PPC::BI__builtin_altivec_vcntmbh:
263 case PPC::BI__builtin_altivec_vcntmbw:
264 case PPC::BI__builtin_altivec_vcntmbd:
265 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
266 case PPC::BI__builtin_vsx_xxgenpcvbm:
267 case PPC::BI__builtin_vsx_xxgenpcvhm:
268 case PPC::BI__builtin_vsx_xxgenpcvwm:
269 case PPC::BI__builtin_vsx_xxgenpcvdm:
270 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 3);
271 case PPC::BI__builtin_ppc_test_data_class: {
272 // Check if the first argument of the __builtin_ppc_test_data_class call is
273 // valid. The argument must be 'float' or 'double' or '__float128'.
274 QualType ArgType = TheCall->getArg(Arg: 0)->getType();
275 if (ArgType != QualType(Context.FloatTy) &&
276 ArgType != QualType(Context.DoubleTy) &&
277 ArgType != QualType(Context.Float128Ty))
278 return Diag(Loc: TheCall->getBeginLoc(),
279 DiagID: diag::err_ppc_invalid_test_data_class_type);
280 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 127);
281 }
282 case PPC::BI__builtin_ppc_maxfe:
283 case PPC::BI__builtin_ppc_minfe:
284 case PPC::BI__builtin_ppc_maxfl:
285 case PPC::BI__builtin_ppc_minfl:
286 case PPC::BI__builtin_ppc_maxfs:
287 case PPC::BI__builtin_ppc_minfs: {
288 if (Context.getTargetInfo().getTriple().isOSAIX() &&
289 (BuiltinID == PPC::BI__builtin_ppc_maxfe ||
290 BuiltinID == PPC::BI__builtin_ppc_minfe))
291 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_target_unsupported_type)
292 << "builtin" << true << 128 << QualType(Context.LongDoubleTy)
293 << false << Context.getTargetInfo().getTriple().str();
294 // Argument type should be exact.
295 QualType ArgType = QualType(Context.LongDoubleTy);
296 if (BuiltinID == PPC::BI__builtin_ppc_maxfl ||
297 BuiltinID == PPC::BI__builtin_ppc_minfl)
298 ArgType = QualType(Context.DoubleTy);
299 else if (BuiltinID == PPC::BI__builtin_ppc_maxfs ||
300 BuiltinID == PPC::BI__builtin_ppc_minfs)
301 ArgType = QualType(Context.FloatTy);
302 for (unsigned I = 0, E = TheCall->getNumArgs(); I < E; ++I)
303 if (TheCall->getArg(Arg: I)->getType() != ArgType)
304 return Diag(Loc: TheCall->getBeginLoc(),
305 DiagID: diag::err_typecheck_convert_incompatible)
306 << TheCall->getArg(Arg: I)->getType() << ArgType << 1 << 0 << 0;
307 return false;
308 }
309#define CUSTOM_BUILTIN(Name, Intr, Types, Acc, Feature) \
310 case PPC::BI__builtin_##Name: \
311 return BuiltinPPCMMACall(TheCall, BuiltinID, Types);
312#include "clang/Basic/BuiltinsPPC.def"
313 case PPC::BI__builtin_amo_lwat:
314 case PPC::BI__builtin_amo_ldat:
315 case PPC::BI__builtin_amo_lwat_s:
316 case PPC::BI__builtin_amo_ldat_s: {
317 llvm::APSInt Result;
318 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 2, Result))
319 return true;
320 unsigned Val = Result.getZExtValue();
321
322 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_lwat ||
323 BuiltinID == PPC::BI__builtin_amo_ldat);
324
325 bool IsValid = IsUnsigned
326 ? llvm::is_contained(Set: {0u, 1u, 2u, 3u, 4u, 6u, 8u}, Element: Val)
327 : llvm::is_contained(Set: {0u, 5u, 7u, 8u}, Element: Val);
328
329 if (IsValid)
330 return false;
331
332 Expr *Arg = TheCall->getArg(Arg: 2);
333 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
334 << toString(I: Result, Radix: 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7") << "8"
335 << Arg->getSourceRange();
336 }
337 case PPC::BI__builtin_amo_lwat_cond:
338 case PPC::BI__builtin_amo_ldat_cond:
339 case PPC::BI__builtin_amo_lwat_cond_s:
340 case PPC::BI__builtin_amo_ldat_cond_s: {
341 llvm::APSInt Result;
342 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 1, Result))
343 return true;
344 unsigned Val = Result.getZExtValue();
345 if (llvm::is_contained(Set: {24u, 25u, 28u}, Element: Val))
346 return false;
347
348 Expr *Arg = TheCall->getArg(Arg: 1);
349 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
350 << toString(I: Result, Radix: 10) << "24, 25" << "28" << Arg->getSourceRange();
351 }
352 case PPC::BI__builtin_amo_stwat:
353 case PPC::BI__builtin_amo_stdat:
354 case PPC::BI__builtin_amo_stwat_s:
355 case PPC::BI__builtin_amo_stdat_s: {
356 llvm::APSInt Result;
357 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 2, Result))
358 return true;
359 unsigned Val = Result.getZExtValue();
360
361 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_stwat ||
362 BuiltinID == PPC::BI__builtin_amo_stdat);
363
364 bool IsValid = IsUnsigned
365 ? llvm::is_contained(Set: {0u, 1u, 2u, 3u, 4u, 6u, 24u}, Element: Val)
366 : llvm::is_contained(Set: {0u, 5u, 7u, 24u}, Element: Val);
367
368 if (IsValid)
369 return false;
370
371 Expr *Arg = TheCall->getArg(Arg: 2);
372 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
373 << toString(I: Result, Radix: 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7")
374 << "24" << Arg->getSourceRange();
375 }
376 }
377 llvm_unreachable("must return from switch");
378}
379
380// Check if the given type is a non-pointer PPC MMA type. This function is used
381// in Sema to prevent invalid uses of restricted PPC MMA types.
382bool SemaPPC::CheckPPCMMAType(QualType Type, SourceLocation TypeLoc) {
383 ASTContext &Context = getASTContext();
384 if (Type->isPointerType() || Type->isArrayType())
385 return false;
386
387 QualType CoreType = Type.getCanonicalType().getUnqualifiedType();
388#define PPC_VECTOR_TYPE(Name, Id, Size) || CoreType == Context.Id##Ty
389 if (false
390#include "clang/Basic/PPCTypes.def"
391 ) {
392 Diag(Loc: TypeLoc, DiagID: diag::err_ppc_invalid_use_mma_type);
393 return true;
394 }
395 return false;
396}
397
398/// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str,
399/// advancing the pointer over the consumed characters. The decoded type is
400/// returned. If the decoded type represents a constant integer with a
401/// constraint on its value then Mask is set to that value. The type descriptors
402/// used in Str are specific to PPC MMA builtins and are documented in the file
403/// defining the PPC builtins.
404static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str,
405 unsigned &Mask) {
406 bool RequireICE = false;
407 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
408 switch (*Str++) {
409 case 'V':
410 return Context.getVectorType(VectorType: Context.UnsignedCharTy, NumElts: 16,
411 VecKind: VectorKind::AltiVecVector);
412 case 'i': {
413 char *End;
414 unsigned size = strtoul(nptr: Str, endptr: &End, base: 10);
415 assert(End != Str && "Missing constant parameter constraint");
416 Str = End;
417 Mask = size;
418 return Context.IntTy;
419 }
420 case 'W': {
421 char *End;
422 unsigned size = strtoul(nptr: Str, endptr: &End, base: 10);
423 assert(End != Str && "Missing PowerPC MMA type size");
424 Str = End;
425 QualType Type;
426 switch (size) {
427#define PPC_VECTOR_TYPE(typeName, Id, size) \
428 case size: \
429 Type = Context.Id##Ty; \
430 break;
431#include "clang/Basic/PPCTypes.def"
432 default:
433 llvm_unreachable("Invalid PowerPC MMA vector type");
434 }
435 bool CheckVectorArgs = false;
436 while (!CheckVectorArgs) {
437 switch (*Str++) {
438 case '*':
439 Type = Context.getPointerType(T: Type);
440 break;
441 case 'C':
442 Type = Type.withConst();
443 break;
444 default:
445 CheckVectorArgs = true;
446 --Str;
447 break;
448 }
449 }
450 return Type;
451 }
452 default:
453 return Context.DecodeTypeStr(Str&: --Str, Context, Error, RequireICE, AllowTypeModifiers: true);
454 }
455}
456
457bool SemaPPC::BuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID,
458 const char *TypeStr) {
459
460 assert((TypeStr[0] != '\0') &&
461 "Invalid types in PPC MMA builtin declaration");
462
463 ASTContext &Context = getASTContext();
464 unsigned Mask = 0;
465 unsigned ArgNum = 0;
466
467 // The first type in TypeStr is the type of the value returned by the
468 // builtin. So we first read that type and change the type of TheCall.
469 QualType type = DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
470 TheCall->setType(type);
471
472 while (*TypeStr != '\0') {
473 Mask = 0;
474 QualType ExpectedType = DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
475 if (ArgNum >= TheCall->getNumArgs()) {
476 ArgNum++;
477 break;
478 }
479
480 Expr *Arg = TheCall->getArg(Arg: ArgNum);
481 QualType PassedType = Arg->getType();
482 QualType StrippedRVType = PassedType.getCanonicalType();
483
484 // Strip Restrict/Volatile qualifiers.
485 if (StrippedRVType.isRestrictQualified() ||
486 StrippedRVType.isVolatileQualified())
487 StrippedRVType = StrippedRVType.getCanonicalType().getUnqualifiedType();
488
489 // The only case where the argument type and expected type are allowed to
490 // mismatch is if the argument type is a non-void pointer (or array) and
491 // expected type is a void pointer.
492 if (StrippedRVType != ExpectedType)
493 if (!(ExpectedType->isVoidPointerType() &&
494 (StrippedRVType->isPointerType() || StrippedRVType->isArrayType())))
495 return Diag(Loc: Arg->getBeginLoc(),
496 DiagID: diag::err_typecheck_convert_incompatible)
497 << PassedType << ExpectedType << 1 << 0 << 0;
498
499 // If the value of the Mask is not 0, we have a constraint in the size of
500 // the integer argument so here we ensure the argument is a constant that
501 // is in the valid range.
502 if (Mask != 0 &&
503 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum, Low: 0, High: Mask, RangeIsError: true))
504 return true;
505
506 ArgNum++;
507 }
508
509 // In case we exited early from the previous loop, there are other types to
510 // read from TypeStr. So we need to read them all to ensure we have the right
511 // number of arguments in TheCall and if it is not the case, to display a
512 // better error message.
513 while (*TypeStr != '\0') {
514 (void)DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
515 ArgNum++;
516 }
517 if (SemaRef.checkArgCount(Call: TheCall, DesiredArgCount: ArgNum))
518 return true;
519
520 return false;
521}
522
523bool SemaPPC::BuiltinVSX(CallExpr *TheCall) {
524 unsigned ExpectedNumArgs = 3;
525 if (SemaRef.checkArgCount(Call: TheCall, DesiredArgCount: ExpectedNumArgs))
526 return true;
527
528 // Check the third argument is a compile time constant
529 if (!TheCall->getArg(Arg: 2)->isIntegerConstantExpr(Ctx: getASTContext()))
530 return Diag(Loc: TheCall->getBeginLoc(),
531 DiagID: diag::err_vsx_builtin_nonconstant_argument)
532 << 3 /* argument index */ << TheCall->getDirectCallee()
533 << SourceRange(TheCall->getArg(Arg: 2)->getBeginLoc(),
534 TheCall->getArg(Arg: 2)->getEndLoc());
535
536 QualType Arg1Ty = TheCall->getArg(Arg: 0)->getType();
537 QualType Arg2Ty = TheCall->getArg(Arg: 1)->getType();
538
539 // Check the type of argument 1 and argument 2 are vectors.
540 SourceLocation BuiltinLoc = TheCall->getBeginLoc();
541 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
542 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
543 return Diag(Loc: BuiltinLoc, DiagID: diag::err_vec_builtin_non_vector)
544 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
545 << SourceRange(TheCall->getArg(Arg: 0)->getBeginLoc(),
546 TheCall->getArg(Arg: 1)->getEndLoc());
547 }
548
549 // Check the first two arguments are the same type.
550 if (!getASTContext().hasSameUnqualifiedType(T1: Arg1Ty, T2: Arg2Ty)) {
551 return Diag(Loc: BuiltinLoc, DiagID: diag::err_vec_builtin_incompatible_vector)
552 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
553 << SourceRange(TheCall->getArg(Arg: 0)->getBeginLoc(),
554 TheCall->getArg(Arg: 1)->getEndLoc());
555 }
556
557 // When default clang type checking is turned off and the customized type
558 // checking is used, the returning type of the function must be explicitly
559 // set. Otherwise it is _Bool by default.
560 TheCall->setType(Arg1Ty);
561
562 return false;
563}
564
565} // namespace clang
566