1//===-------------------- InterpBuiltinBitCast.cpp --------------*- C++ -*-===//
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#include "InterpBuiltinBitCast.h"
9#include "BitcastBuffer.h"
10#include "Boolean.h"
11#include "Char.h"
12#include "Context.h"
13#include "Floating.h"
14#include "Integral.h"
15#include "InterpState.h"
16#include "MemberPointer.h"
17#include "Pointer.h"
18#include "Record.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/RecordLayout.h"
21#include "clang/Basic/TargetInfo.h"
22
23#include <variant>
24
25using namespace clang;
26using namespace clang::interp;
27
28/// Implement __builtin_bit_cast and related operations.
29/// Since our internal representation for data is more complex than
30/// something we can simply memcpy or memcmp, we first bitcast all the data
31/// into a buffer, which we then later use to copy the data into the target.
32
33// TODO:
34// - Try to minimize heap allocations.
35// - Optimize the common case of only pushing and pulling full
36// bytes to/from the buffer.
37
38enum class Result { Success, Skip, Failure };
39
40/// Used to iterate over pointer fields.
41using DataFunc =
42 llvm::function_ref<Result(PtrView P, PrimType Ty, Bits BitOffset,
43 Bits FullBitWidth, bool PackedBools)>;
44
45#define BITCAST_TYPE_SWITCH(Expr, B) \
46 do { \
47 switch (Expr) { \
48 TYPE_SWITCH_CASE(PT_Sint8, B) \
49 TYPE_SWITCH_CASE(PT_Uint8, B) \
50 TYPE_SWITCH_CASE(PT_Sint16, B) \
51 TYPE_SWITCH_CASE(PT_Uint16, B) \
52 TYPE_SWITCH_CASE(PT_Sint32, B) \
53 TYPE_SWITCH_CASE(PT_Uint32, B) \
54 TYPE_SWITCH_CASE(PT_Sint64, B) \
55 TYPE_SWITCH_CASE(PT_Uint64, B) \
56 TYPE_SWITCH_CASE(PT_IntAP, B) \
57 TYPE_SWITCH_CASE(PT_IntAPS, B) \
58 TYPE_SWITCH_CASE(PT_Bool, B) \
59 default: \
60 llvm_unreachable("Unhandled bitcast type"); \
61 } \
62 } while (0)
63
64#define BITCAST_TYPE_SWITCH_FIXED_SIZE(Expr, B) \
65 do { \
66 switch (Expr) { \
67 TYPE_SWITCH_CASE(PT_Sint8, B) \
68 TYPE_SWITCH_CASE(PT_Uint8, B) \
69 TYPE_SWITCH_CASE(PT_Sint16, B) \
70 TYPE_SWITCH_CASE(PT_Uint16, B) \
71 TYPE_SWITCH_CASE(PT_Sint32, B) \
72 TYPE_SWITCH_CASE(PT_Uint32, B) \
73 TYPE_SWITCH_CASE(PT_Sint64, B) \
74 TYPE_SWITCH_CASE(PT_Uint64, B) \
75 TYPE_SWITCH_CASE(PT_Bool, B) \
76 default: \
77 llvm_unreachable("Unhandled bitcast type"); \
78 } \
79 } while (0)
80
81// FIXME: It is unfortunate that we have this function at all, but we can read
82// from a StringPointer. In the later callback-based reading and writing paths,
83// we do assume a BlockPointer though.
84static std::pair<Block *, std::unique_ptr<Descriptor>>
85convertToBlockPointer(const Context &Ctx, const StringPointer &SP) {
86 const StringLiteral *S = SP.getLiteral();
87 const size_t CharWidth = S->getCharByteWidth();
88 const size_t BitWidth = CharWidth * Ctx.getCharBit();
89 unsigned StringLength = S->getLength();
90
91 OptPrimType CharType =
92 Ctx.classify(T: S->getType()->castAsArrayTypeUnsafe()->getElementType());
93 assert(CharType);
94
95 // Create a descriptor for the string.
96 std::unique_ptr<Descriptor> Desc = std::make_unique<Descriptor>(
97 args&: S, args: S->getType().getTypePtr(), args: *CharType, args: StringLength + 1,
98 /*IsConst=*/args: true,
99 /*isTemporary=*/args: false,
100 /*isMutable=*/args: false,
101 /*IsVolatile=*/args: false);
102
103 // Allocate storage for the string.
104 // The byte length does not include the null terminator.
105 auto *Memory = new std::byte[sizeof(Block) + Desc->getAllocSize()];
106 auto *B = new (Memory) Block(Ctx.getEvalID(), Desc.get(), Block::None);
107 B->invokeCtor();
108
109 Pointer Ptr(B);
110 if (CharWidth == 1) {
111 std::memcpy(dest: &Ptr.elem<char>(I: 0), src: S->getString().data(), n: StringLength);
112 } else {
113 // Construct the string in storage.
114 for (unsigned I = 0; I <= StringLength; ++I) {
115 uint32_t CodePoint = I == StringLength ? 0 : S->getCodeUnit(I);
116 INT_TYPE_SWITCH_NO_BOOL(*CharType,
117 Ptr.elem<T>(I) = T::from(CodePoint, BitWidth););
118 }
119 }
120 Ptr.initializeAllElements();
121 return std::make_pair(x: std::move(B), y: std::move(Desc));
122}
123
124/// We use this to recursively iterate over all fields and elements of a pointer
125/// and extract relevant data for a bitcast.
126static Result enumerateData(PtrView P, const Context &Ctx, Bits Offset,
127 Bits BitsToRead, DataFunc F, bool Initialize) {
128 const Descriptor *FieldDesc = P.getFieldDesc();
129 assert(FieldDesc);
130
131 // Primitives.
132 if (FieldDesc->isPrimitive()) {
133 Bits FullBitWidth =
134 Bits(Ctx.getASTContext().getTypeSize(T: FieldDesc->getType()));
135 return F(P, FieldDesc->getPrimType(), Offset, FullBitWidth,
136 /*PackedBools=*/false);
137 }
138
139 // Primitive arrays.
140 if (FieldDesc->isPrimitiveArray()) {
141 QualType ElemType = FieldDesc->getElemQualType();
142 Bits ElemSize = Bits(Ctx.getASTContext().getTypeSize(T: ElemType));
143 PrimType ElemT = *Ctx.classify(T: ElemType);
144 // Special case, since the bools here are packed.
145 bool PackedBools =
146 FieldDesc->getType()->isPackedVectorBoolType(ctx: Ctx.getASTContext());
147 unsigned NumElems = FieldDesc->getNumElems();
148 bool Ok = true;
149 for (unsigned I = P.getIndex(); I != NumElems; ++I) {
150 Result Res = F(P.atIndex(Idx: I), ElemT, Offset, ElemSize, PackedBools);
151
152 Ok = Ok && (Res == Result::Success);
153 Offset += PackedBools ? Bits(1) : ElemSize;
154 if (Offset >= BitsToRead)
155 break;
156 }
157 return Ok ? Result::Success : Result::Skip;
158 }
159
160 // Composite arrays.
161 if (FieldDesc->isCompositeArray()) {
162 QualType ElemType = FieldDesc->getElemQualType();
163 Bits ElemSize = Bits(Ctx.getASTContext().getTypeSize(T: ElemType));
164 for (unsigned I = P.getIndex(); I != FieldDesc->getNumElems(); ++I) {
165 enumerateData(P: P.atIndex(Idx: I).narrow(), Ctx, Offset, BitsToRead, F,
166 Initialize);
167 Offset += ElemSize;
168 if (Offset >= BitsToRead)
169 break;
170 }
171 return Result::Success;
172 }
173
174 // Records.
175 if (FieldDesc->isRecord()) {
176 const Record *R = FieldDesc->ElemRecord;
177 if (R->getDecl()->isInvalidDecl())
178 return Result::Failure;
179 const ASTRecordLayout &Layout =
180 Ctx.getASTContext().getASTRecordLayout(D: R->getDecl());
181 bool Ok = true;
182
183 for (const Record::Field &Fi : R->fields()) {
184 if (Fi.isUnnamedBitField())
185 continue;
186
187 PtrView Elem = P.atField(Offset: Fi.Offset);
188 Bits BitOffset =
189 Offset + Bits(Layout.getFieldOffset(FieldNo: Fi.Decl->getFieldIndex()));
190 Result Res =
191 enumerateData(P: Elem, Ctx, Offset: BitOffset, BitsToRead, F, Initialize);
192 if (Initialize) {
193 if (Res == Result::Success)
194 Elem.initialize();
195 else if (Res == Result::Skip)
196 Elem.startLifetime();
197 }
198 Ok = Ok && Res != Result::Failure;
199 }
200 for (const Record::Base &B : R->bases()) {
201 PtrView Elem = P.atField(Offset: B.Offset);
202 if (!Initialize && !Elem.isInitialized())
203 return Result::Failure;
204
205 CharUnits ByteOffset =
206 Layout.getBaseClassOffset(Base: cast<CXXRecordDecl>(Val: B.Decl));
207 Bits BitOffset = Offset + Bits(Ctx.getASTContext().toBits(CharSize: ByteOffset));
208 Result Res =
209 enumerateData(P: Elem, Ctx, Offset: BitOffset, BitsToRead, F, Initialize);
210 if (Initialize) {
211 if (Res == Result::Success)
212 Elem.initialize();
213 else if (Res == Result::Skip)
214 Elem.startLifetime();
215 }
216 Ok = Ok && Res != Result::Failure;
217 }
218 return Ok ? Result::Success : Result::Failure;
219 }
220
221 llvm_unreachable("Unhandled data type");
222}
223
224static bool enumeratePointerFields(const Pointer &P, const Context &Ctx,
225 Bits BitsToRead, DataFunc F,
226 bool Initialize) {
227
228 if (P.isStringPointer()) {
229 auto [B, Desc] = convertToBlockPointer(Ctx, SP: P.asStringPointer());
230
231 bool Result = enumerateData(P: Pointer(B).atIndex(Idx: P.getIndex()).view(), Ctx,
232 Offset: Bits::zero(), BitsToRead, F,
233 Initialize) == Result::Failure;
234 delete[] reinterpret_cast<std::byte *>(B);
235 return Result;
236 }
237
238 return enumerateData(P: P.view(), Ctx, Offset: Bits::zero(), BitsToRead, F,
239 Initialize) != Result::Failure;
240}
241
242// This function is constexpr if and only if To, From, and the types of
243// all subobjects of To and From are types T such that...
244// (3.1) - is_union_v<T> is false;
245// (3.2) - is_pointer_v<T> is false;
246// (3.3) - is_member_pointer_v<T> is false;
247// (3.4) - is_volatile_v<T> is false; and
248// (3.5) - T has no non-static data members of reference type
249//
250// NOTE: This is a version of checkBitCastConstexprEligibilityType() in
251// ExprConstant.cpp.
252static bool checkBitcastType(InterpState &S, CodePtr OpPC, QualType T,
253 bool IsToType) {
254 enum {
255 E_Union = 0,
256 E_Pointer,
257 E_MemberPointer,
258 E_Volatile,
259 E_Reference,
260 };
261 enum { C_Member, C_Base };
262
263 auto diag = [&](int Reason) -> bool {
264 const Expr *E = S.Current->getExpr(PC: OpPC);
265 S.FFDiag(E, DiagId: diag::note_constexpr_bit_cast_invalid_type)
266 << static_cast<int>(IsToType) << (Reason == E_Reference) << Reason
267 << E->getSourceRange();
268 return false;
269 };
270 auto note = [&](int Construct, QualType NoteType,
271 SourceRange NoteRange) -> bool {
272 S.Note(Loc: NoteRange.getBegin(), DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
273 << NoteType << Construct << T.getUnqualifiedType() << NoteRange;
274 return false;
275 };
276 auto unsupported = [&](QualType T) -> bool {
277 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
278 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
279 << T;
280 return false;
281 };
282
283 T = T.getCanonicalType();
284
285 if (T->isUnionType())
286 return diag(E_Union);
287 if (T->isPointerType())
288 return diag(E_Pointer);
289 if (T->isMemberPointerType())
290 return diag(E_MemberPointer);
291 if (T.isVolatileQualified())
292 return diag(E_Volatile);
293
294 if (const RecordDecl *RD = T->getAsRecordDecl()) {
295 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
296 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
297 if (!checkBitcastType(S, OpPC, T: BS.getType(), IsToType))
298 return note(C_Base, BS.getType(), BS.getBeginLoc());
299 }
300 }
301 for (const FieldDecl *FD : RD->fields()) {
302 if (FD->getType()->isReferenceType())
303 return diag(E_Reference);
304 if (!checkBitcastType(S, OpPC, T: FD->getType(), IsToType))
305 return note(C_Member, FD->getType(), FD->getSourceRange());
306 }
307 }
308
309 if (T->isArrayType() &&
310 !checkBitcastType(S, OpPC, T: S.getASTContext().getBaseElementType(QT: T),
311 IsToType))
312 return false;
313
314 if (const auto *VT = T->getAs<VectorType>()) {
315 const ASTContext &ASTCtx = S.getASTContext();
316 QualType EltTy = VT->getElementType();
317 unsigned NElts = VT->getNumElements();
318 unsigned EltSize =
319 VT->isPackedVectorBoolType(ctx: ASTCtx) ? 1 : ASTCtx.getTypeSize(T: EltTy);
320
321 if ((NElts * EltSize) % ASTCtx.getCharWidth() != 0) {
322 // The vector's size in bits is not a multiple of the target's byte size,
323 // so its layout is unspecified. For now, we'll simply treat these cases
324 // as unsupported (this should only be possible with OpenCL bool vectors
325 // whose element count isn't a multiple of the byte size).
326 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
327 DiagId: diag::note_constexpr_bit_cast_invalid_vector)
328 << QualType(VT, 0) << EltSize << NElts << ASTCtx.getCharWidth();
329 return false;
330 }
331
332 if (EltTy->isRealFloatingType() &&
333 &ASTCtx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
334 // The layout for x86_fp80 vectors seems to be handled very inconsistently
335 // by both clang and LLVM, so for now we won't allow bit_casts involving
336 // it in a constexpr context.
337 return unsupported(EltTy);
338 }
339 }
340
341 if (T->isBlockPointerType())
342 return unsupported(T);
343
344 return true;
345}
346
347bool clang::interp::readPointerToBuffer(const Context &Ctx,
348 const Pointer &FromPtr,
349 BitcastBuffer &Buffer,
350 bool ReturnOnUninit) {
351 const ASTContext &ASTCtx = Ctx.getASTContext();
352 Endian TargetEndianness =
353 ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
354
355 return enumeratePointerFields(
356 P: FromPtr, Ctx, BitsToRead: Buffer.size(),
357 F: [&](PtrView P, PrimType T, Bits BitOffset, Bits FullBitWidth,
358 bool PackedBools) -> Result {
359 Bits BitWidth = FullBitWidth;
360
361 if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
362 BitWidth = Bits(std::min(a: FD->getBitWidthValue(),
363 b: (unsigned)FullBitWidth.getQuantity()));
364 else if (T == PT_Bool && PackedBools)
365 BitWidth = Bits(1);
366
367 if (BitWidth.isZero())
368 return Result::Skip;
369
370 // Bits will be left uninitialized and diagnosed when reading.
371 if (!P.isInitialized())
372 return Result::Skip;
373
374 if (T == PT_Ptr) {
375 assert(P.getType()->isNullPtrType());
376 // Clang treats nullptr_t has having NO bits in its value
377 // representation. So, we accept it here and leave its bits
378 // uninitialized.
379 return Result::Skip;
380 }
381
382 assert(P.isInitialized());
383 auto Buff = std::make_unique<std::byte[]>(num: FullBitWidth.roundToBytes());
384 // Work around floating point types that contain unused padding bytes.
385 // This is really just `long double` on x86, which is the only
386 // fundamental type with padding bytes.
387 if (T == PT_Float) {
388 const Floating &F = P.deref<Floating>();
389 Bits NumBits = Bits(
390 llvm::APFloatBase::getSizeInBits(Sem: F.getAPFloat().getSemantics()));
391 assert(NumBits.isFullByte());
392 assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
393 F.bitcastToMemory(Buff: Buff.get());
394 // Now, only (maybe) swap the actual size of the float, excluding
395 // the padding bits.
396 if (llvm::sys::IsBigEndianHost)
397 swapBytes(M: Buff.get(), N: NumBits.roundToBytes());
398
399 Buffer.markInitialized(Start: BitOffset, Length: NumBits);
400 } else {
401 BITCAST_TYPE_SWITCH(T, {
402 auto Val = P.deref<T>();
403 if (!Val.isNumber())
404 return Result::Failure;
405 Val.bitcastToMemory(Buff.get());
406 });
407
408 if (llvm::sys::IsBigEndianHost)
409 swapBytes(M: Buff.get(), N: FullBitWidth.roundToBytes());
410 Buffer.markInitialized(Start: BitOffset, Length: BitWidth);
411 }
412
413 Buffer.pushData(In: Buff.get(), BitOffset, BitWidth, TargetEndianness);
414 return Result::Success;
415 },
416 Initialize: false);
417}
418
419bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
420 std::byte *Buff, Bits BitWidth, Bits FullBitWidth,
421 bool &HasIndeterminateBits) {
422 assert(Ptr.isLive());
423 assert(Ptr.isReadablePointerType());
424 assert(Buff);
425 assert(BitWidth <= FullBitWidth);
426 assert(FullBitWidth.isFullByte());
427 assert(BitWidth.isFullByte());
428
429 BitcastBuffer Buffer(FullBitWidth);
430 size_t BuffSize = FullBitWidth.roundToBytes();
431 QualType DataType = Ptr.getType();
432 if (!checkBitcastType(S, OpPC, T: DataType, /*IsToType=*/false))
433 return false;
434
435 bool Success = readPointerToBuffer(Ctx: S.getContext(), FromPtr: Ptr, Buffer,
436 /*ReturnOnUninit=*/false);
437 HasIndeterminateBits = !Buffer.rangeInitialized(Offset: Bits::zero(), Length: BitWidth);
438
439 const ASTContext &ASTCtx = S.getASTContext();
440 Endian TargetEndianness =
441 ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
442 auto B =
443 Buffer.copyBits(BitOffset: Bits::zero(), BitWidth, FullBitWidth, TargetEndianness);
444
445 std::memcpy(dest: Buff, src: B.get(), n: BuffSize);
446
447 if (llvm::sys::IsBigEndianHost)
448 swapBytes(M: Buff, N: BitWidth.roundToBytes());
449
450 return Success;
451}
452bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
453 const Pointer &FromPtr, Pointer &ToPtr) {
454 const ASTContext &ASTCtx = S.getASTContext();
455 CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(T: ToPtr.getType());
456
457 return DoBitCastPtr(S, OpPC, FromPtr, ToPtr, Size: ObjectReprChars.getQuantity());
458}
459
460bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
461 const Pointer &FromPtr, Pointer &ToPtr,
462 size_t Size) {
463 assert(FromPtr.isLive());
464 assert(FromPtr.isBlockPointer());
465 assert(ToPtr.isBlockPointer());
466
467 QualType FromType = FromPtr.getFieldDesc()->getDataType(Ctx: S.getASTContext());
468 QualType ToType = ToPtr.getFieldDesc()->getDataType(Ctx: S.getASTContext());
469
470 if (!checkBitcastType(S, OpPC, T: ToType, /*IsToType=*/true))
471 return false;
472 if (!checkBitcastType(S, OpPC, T: FromType, /*IsToType=*/false))
473 return false;
474
475 const ASTContext &ASTCtx = S.getASTContext();
476 BitcastBuffer Buffer(Bytes(Size).toBits());
477 readPointerToBuffer(Ctx: S.getContext(), FromPtr, Buffer,
478 /*ReturnOnUninit=*/false);
479
480 // Now read the values out of the buffer again and into ToPtr.
481 Endian TargetEndianness =
482 ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
483 bool Success = enumeratePointerFields(
484 P: ToPtr, Ctx: S.getContext(), BitsToRead: Buffer.size(),
485 F: [&](PtrView P, PrimType T, Bits BitOffset, Bits FullBitWidth,
486 bool PackedBools) -> Result {
487 QualType PtrType = P.getType();
488 if (T == PT_Float) {
489 const auto &Semantics = ASTCtx.getFloatTypeSemantics(T: PtrType);
490 Bits NumBits = Bits(llvm::APFloatBase::getSizeInBits(Sem: Semantics));
491 assert(NumBits.isFullByte());
492 assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
493 auto M = Buffer.copyBits(BitOffset, BitWidth: NumBits, FullBitWidth,
494 TargetEndianness);
495
496 if (llvm::sys::IsBigEndianHost)
497 swapBytes(M: M.get(), N: NumBits.roundToBytes());
498
499 Floating R = S.allocFloat(Sem: Semantics);
500 Floating::bitcastFromMemory(Buff: M.get(), Sem: Semantics, Result: &R);
501 P.deref<Floating>() = R;
502 P.initialize();
503 return Result::Success;
504 }
505
506 Bits BitWidth;
507 if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
508 BitWidth = Bits(std::min(a: FD->getBitWidthValue(),
509 b: (unsigned)FullBitWidth.getQuantity()));
510 else if (T == PT_Bool && PackedBools)
511 BitWidth = Bits(1);
512 else
513 BitWidth = FullBitWidth;
514
515 // If any of the bits are uninitialized, we need to abort unless the
516 // target type is std::byte or unsigned char.
517 bool Initialized = Buffer.rangeInitialized(Offset: BitOffset, Length: BitWidth);
518 if (!Initialized) {
519 if (!PtrType->isStdByteType() &&
520 !PtrType->isSpecificBuiltinType(K: BuiltinType::UChar) &&
521 !PtrType->isSpecificBuiltinType(K: BuiltinType::Char_U)) {
522 const Expr *E = S.Current->getExpr(PC: OpPC);
523 S.FFDiag(E, DiagId: diag::note_constexpr_bit_cast_indet_dest)
524 << PtrType << S.getLangOpts().CharIsSigned
525 << E->getSourceRange();
526
527 return Result::Failure;
528 }
529 return Result::Skip;
530 }
531
532 auto Memory = Buffer.copyBits(BitOffset, BitWidth, FullBitWidth,
533 TargetEndianness);
534 if (llvm::sys::IsBigEndianHost)
535 swapBytes(M: Memory.get(), N: FullBitWidth.roundToBytes());
536
537 if (T == PT_IntAPS) {
538 P.deref<IntegralAP<true>>() =
539 S.allocAP<IntegralAP<true>>(BitWidth: FullBitWidth.getQuantity());
540 IntegralAP<true>::bitcastFromMemory(Src: Memory.get(),
541 BitWidth: FullBitWidth.getQuantity(),
542 Result: &P.deref<IntegralAP<true>>());
543 } else if (T == PT_IntAP) {
544 P.deref<IntegralAP<false>>() =
545 S.allocAP<IntegralAP<false>>(BitWidth: FullBitWidth.getQuantity());
546 IntegralAP<false>::bitcastFromMemory(Src: Memory.get(),
547 BitWidth: FullBitWidth.getQuantity(),
548 Result: &P.deref<IntegralAP<false>>());
549 } else {
550 BITCAST_TYPE_SWITCH_FIXED_SIZE(T, {
551 if (BitWidth.nonZero())
552 P.deref<T>() = T::bitcastFromMemory(Memory.get(), T::bitWidth())
553 .truncate(BitWidth.getQuantity());
554 else
555 P.deref<T>() = T::zero();
556 });
557 }
558 P.initialize();
559 return Result::Success;
560 },
561 Initialize: true);
562
563 return Success;
564}
565
566using PrimTypeVariant =
567 std::variant<Pointer, MemberPointer, FixedPoint, Char<false>, Char<true>,
568 Integral<16, false>, Integral<16, true>, Integral<32, false>,
569 Integral<32, true>, Integral<64, false>, Integral<64, true>,
570 IntegralAP<true>, IntegralAP<false>, Boolean, Floating,
571 Reflect>;
572
573// NB: This implementation isn't exactly ideal, but:
574// 1) We can't just do a bitcast here since we need to be able to
575// copy pointers.
576// 2) This also needs to handle overlapping regions.
577// 3) We currently have no way of iterating over the fields of a pointer
578// backwards.
579bool clang::interp::DoMemcpy(InterpState &S, CodePtr OpPC,
580 const Pointer &SrcPtr, const Pointer &DestPtr,
581 Bits Size) {
582 assert(SrcPtr.isReadablePointerType());
583 assert(DestPtr.isBlockPointer());
584
585 llvm::SmallVector<PrimTypeVariant> Values;
586
587 if (SrcPtr.isStringPointer()) {
588 const auto &SP = SrcPtr.asStringPointer();
589
590 auto [B, Desc] = convertToBlockPointer(Ctx: S.getContext(), SP);
591 enumeratePointerFields(
592 P: Pointer(B).atIndex(Idx: SrcPtr.getIndex()), Ctx: S.getContext(), BitsToRead: Size,
593 F: [&](const PtrView P, PrimType T, Bits BitOffset, Bits FullBitWidth,
594 bool PackedBools) -> Result {
595 TYPE_SWITCH(T, { Values.push_back(P.deref<T>()); });
596 return Result::Success;
597 },
598 Initialize: false);
599
600 delete[] B;
601 } else {
602
603 enumeratePointerFields(
604 P: SrcPtr, Ctx: S.getContext(), BitsToRead: Size,
605 F: [&](const PtrView P, PrimType T, Bits BitOffset, Bits FullBitWidth,
606 bool PackedBools) -> Result {
607 TYPE_SWITCH(T, { Values.push_back(P.deref<T>()); });
608 return Result::Success;
609 },
610 Initialize: false);
611 }
612
613 unsigned ValueIndex = 0;
614 enumeratePointerFields(
615 P: DestPtr, Ctx: S.getContext(), BitsToRead: Size,
616 F: [&](const PtrView P, PrimType T, Bits BitOffset, Bits FullBitWidth,
617 bool PackedBools) -> Result {
618 TYPE_SWITCH(T, {
619 P.deref<T>() = std::get<T>(Values[ValueIndex]);
620 P.initialize();
621 });
622
623 ++ValueIndex;
624 return Result::Success;
625 },
626 Initialize: true);
627
628 // We should've read all the values into DestPtr.
629 assert(ValueIndex == Values.size());
630
631 return true;
632}
633