Descriptor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //===--- Descriptor.cpp - Types for the constexpr VM ------------*- 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 "Descriptor.h"
  9. #include "Pointer.h"
  10. #include "PrimType.h"
  11. #include "Record.h"
  12. using namespace clang;
  13. using namespace clang::interp;
  14. template <typename T>
  15. static void ctorTy(Block *, char *Ptr, bool, bool, bool, Descriptor *) {
  16. new (Ptr) T();
  17. }
  18. template <typename T> static void dtorTy(Block *, char *Ptr, Descriptor *) {
  19. reinterpret_cast<T *>(Ptr)->~T();
  20. }
  21. template <typename T>
  22. static void moveTy(Block *, char *Src, char *Dst, Descriptor *) {
  23. auto *SrcPtr = reinterpret_cast<T *>(Src);
  24. auto *DstPtr = reinterpret_cast<T *>(Dst);
  25. new (DstPtr) T(std::move(*SrcPtr));
  26. }
  27. template <typename T>
  28. static void ctorArrayTy(Block *, char *Ptr, bool, bool, bool, Descriptor *D) {
  29. for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
  30. new (&reinterpret_cast<T *>(Ptr)[I]) T();
  31. }
  32. }
  33. template <typename T>
  34. static void dtorArrayTy(Block *, char *Ptr, Descriptor *D) {
  35. for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
  36. reinterpret_cast<T *>(Ptr)[I].~T();
  37. }
  38. }
  39. template <typename T>
  40. static void moveArrayTy(Block *, char *Src, char *Dst, Descriptor *D) {
  41. for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
  42. auto *SrcPtr = &reinterpret_cast<T *>(Src)[I];
  43. auto *DstPtr = &reinterpret_cast<T *>(Dst)[I];
  44. new (DstPtr) T(std::move(*SrcPtr));
  45. }
  46. }
  47. static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
  48. bool IsActive, Descriptor *D) {
  49. const unsigned NumElems = D->getNumElems();
  50. const unsigned ElemSize =
  51. D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
  52. unsigned ElemOffset = 0;
  53. for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
  54. auto *ElemPtr = Ptr + ElemOffset;
  55. auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
  56. auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
  57. auto *SD = D->ElemDesc;
  58. Desc->Offset = ElemOffset + sizeof(InlineDescriptor);
  59. Desc->Desc = SD;
  60. Desc->IsInitialized = true;
  61. Desc->IsBase = false;
  62. Desc->IsActive = IsActive;
  63. Desc->IsConst = IsConst || D->IsConst;
  64. Desc->IsMutable = IsMutable || D->IsMutable;
  65. if (auto Fn = D->ElemDesc->CtorFn)
  66. Fn(B, ElemLoc, Desc->IsConst, Desc->IsMutable, IsActive, D->ElemDesc);
  67. }
  68. }
  69. static void dtorArrayDesc(Block *B, char *Ptr, Descriptor *D) {
  70. const unsigned NumElems = D->getNumElems();
  71. const unsigned ElemSize =
  72. D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
  73. unsigned ElemOffset = 0;
  74. for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
  75. auto *ElemPtr = Ptr + ElemOffset;
  76. auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
  77. auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
  78. if (auto Fn = D->ElemDesc->DtorFn)
  79. Fn(B, ElemLoc, D->ElemDesc);
  80. }
  81. }
  82. static void moveArrayDesc(Block *B, char *Src, char *Dst, Descriptor *D) {
  83. const unsigned NumElems = D->getNumElems();
  84. const unsigned ElemSize =
  85. D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
  86. unsigned ElemOffset = 0;
  87. for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
  88. auto *SrcPtr = Src + ElemOffset;
  89. auto *DstPtr = Dst + ElemOffset;
  90. auto *SrcDesc = reinterpret_cast<InlineDescriptor *>(SrcPtr);
  91. auto *SrcElemLoc = reinterpret_cast<char *>(SrcDesc + 1);
  92. auto *DstDesc = reinterpret_cast<InlineDescriptor *>(DstPtr);
  93. auto *DstElemLoc = reinterpret_cast<char *>(DstDesc + 1);
  94. *DstDesc = *SrcDesc;
  95. if (auto Fn = D->ElemDesc->MoveFn)
  96. Fn(B, SrcElemLoc, DstElemLoc, D->ElemDesc);
  97. }
  98. }
  99. static void ctorRecord(Block *B, char *Ptr, bool IsConst, bool IsMutable,
  100. bool IsActive, Descriptor *D) {
  101. const bool IsUnion = D->ElemRecord->isUnion();
  102. auto CtorSub = [=](unsigned SubOff, Descriptor *F, bool IsBase) {
  103. auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + SubOff) - 1;
  104. Desc->Offset = SubOff;
  105. Desc->Desc = F;
  106. Desc->IsInitialized = (B->isStatic() || F->IsArray) && !IsBase;
  107. Desc->IsBase = IsBase;
  108. Desc->IsActive = IsActive && !IsUnion;
  109. Desc->IsConst = IsConst || F->IsConst;
  110. Desc->IsMutable = IsMutable || F->IsMutable;
  111. if (auto Fn = F->CtorFn)
  112. Fn(B, Ptr + SubOff, Desc->IsConst, Desc->IsMutable, Desc->IsActive, F);
  113. };
  114. for (const auto &B : D->ElemRecord->bases())
  115. CtorSub(B.Offset, B.Desc, /*isBase=*/true);
  116. for (const auto &F : D->ElemRecord->fields())
  117. CtorSub(F.Offset, F.Desc, /*isBase=*/false);
  118. for (const auto &V : D->ElemRecord->virtual_bases())
  119. CtorSub(V.Offset, V.Desc, /*isBase=*/true);
  120. }
  121. static void dtorRecord(Block *B, char *Ptr, Descriptor *D) {
  122. auto DtorSub = [=](unsigned SubOff, Descriptor *F) {
  123. if (auto Fn = F->DtorFn)
  124. Fn(B, Ptr + SubOff, F);
  125. };
  126. for (const auto &F : D->ElemRecord->bases())
  127. DtorSub(F.Offset, F.Desc);
  128. for (const auto &F : D->ElemRecord->fields())
  129. DtorSub(F.Offset, F.Desc);
  130. for (const auto &F : D->ElemRecord->virtual_bases())
  131. DtorSub(F.Offset, F.Desc);
  132. }
  133. static void moveRecord(Block *B, char *Src, char *Dst, Descriptor *D) {
  134. for (const auto &F : D->ElemRecord->fields()) {
  135. auto FieldOff = F.Offset;
  136. auto FieldDesc = F.Desc;
  137. *(reinterpret_cast<Descriptor **>(Dst + FieldOff) - 1) = FieldDesc;
  138. if (auto Fn = FieldDesc->MoveFn)
  139. Fn(B, Src + FieldOff, Dst + FieldOff, FieldDesc);
  140. }
  141. }
  142. static BlockCtorFn getCtorPrim(PrimType Type) {
  143. COMPOSITE_TYPE_SWITCH(Type, return ctorTy<T>, return nullptr);
  144. }
  145. static BlockDtorFn getDtorPrim(PrimType Type) {
  146. COMPOSITE_TYPE_SWITCH(Type, return dtorTy<T>, return nullptr);
  147. }
  148. static BlockMoveFn getMovePrim(PrimType Type) {
  149. COMPOSITE_TYPE_SWITCH(Type, return moveTy<T>, return nullptr);
  150. }
  151. static BlockCtorFn getCtorArrayPrim(PrimType Type) {
  152. COMPOSITE_TYPE_SWITCH(Type, return ctorArrayTy<T>, return nullptr);
  153. }
  154. static BlockDtorFn getDtorArrayPrim(PrimType Type) {
  155. COMPOSITE_TYPE_SWITCH(Type, return dtorArrayTy<T>, return nullptr);
  156. }
  157. static BlockMoveFn getMoveArrayPrim(PrimType Type) {
  158. COMPOSITE_TYPE_SWITCH(Type, return moveArrayTy<T>, return nullptr);
  159. }
  160. Descriptor::Descriptor(const DeclTy &D, PrimType Type, bool IsConst,
  161. bool IsTemporary, bool IsMutable)
  162. : Source(D), ElemSize(primSize(Type)), Size(ElemSize), AllocSize(Size),
  163. IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
  164. CtorFn(getCtorPrim(Type)), DtorFn(getDtorPrim(Type)),
  165. MoveFn(getMovePrim(Type)) {
  166. assert(Source && "Missing source");
  167. }
  168. Descriptor::Descriptor(const DeclTy &D, PrimType Type, size_t NumElems,
  169. bool IsConst, bool IsTemporary, bool IsMutable)
  170. : Source(D), ElemSize(primSize(Type)), Size(ElemSize * NumElems),
  171. AllocSize(align(Size) + sizeof(InitMap *)), IsConst(IsConst),
  172. IsMutable(IsMutable), IsTemporary(IsTemporary), IsArray(true),
  173. CtorFn(getCtorArrayPrim(Type)), DtorFn(getDtorArrayPrim(Type)),
  174. MoveFn(getMoveArrayPrim(Type)) {
  175. assert(Source && "Missing source");
  176. }
  177. Descriptor::Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary,
  178. UnknownSize)
  179. : Source(D), ElemSize(primSize(Type)), Size(UnknownSizeMark),
  180. AllocSize(alignof(void *)), IsConst(true), IsMutable(false),
  181. IsTemporary(IsTemporary), IsArray(true), CtorFn(getCtorArrayPrim(Type)),
  182. DtorFn(getDtorArrayPrim(Type)), MoveFn(getMoveArrayPrim(Type)) {
  183. assert(Source && "Missing source");
  184. }
  185. Descriptor::Descriptor(const DeclTy &D, Descriptor *Elem, unsigned NumElems,
  186. bool IsConst, bool IsTemporary, bool IsMutable)
  187. : Source(D), ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
  188. Size(ElemSize * NumElems),
  189. AllocSize(std::max<size_t>(alignof(void *), Size)), ElemDesc(Elem),
  190. IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
  191. IsArray(true), CtorFn(ctorArrayDesc), DtorFn(dtorArrayDesc),
  192. MoveFn(moveArrayDesc) {
  193. assert(Source && "Missing source");
  194. }
  195. Descriptor::Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary,
  196. UnknownSize)
  197. : Source(D), ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
  198. Size(UnknownSizeMark), AllocSize(alignof(void *)), ElemDesc(Elem),
  199. IsConst(true), IsMutable(false), IsTemporary(IsTemporary), IsArray(true),
  200. CtorFn(ctorArrayDesc), DtorFn(dtorArrayDesc), MoveFn(moveArrayDesc) {
  201. assert(Source && "Missing source");
  202. }
  203. Descriptor::Descriptor(const DeclTy &D, Record *R, bool IsConst,
  204. bool IsTemporary, bool IsMutable)
  205. : Source(D), ElemSize(std::max<size_t>(alignof(void *), R->getFullSize())),
  206. Size(ElemSize), AllocSize(Size), ElemRecord(R), IsConst(IsConst),
  207. IsMutable(IsMutable), IsTemporary(IsTemporary), CtorFn(ctorRecord),
  208. DtorFn(dtorRecord), MoveFn(moveRecord) {
  209. assert(Source && "Missing source");
  210. }
  211. QualType Descriptor::getType() const {
  212. if (auto *E = asExpr())
  213. return E->getType();
  214. if (auto *D = asValueDecl())
  215. return D->getType();
  216. llvm_unreachable("Invalid descriptor type");
  217. }
  218. SourceLocation Descriptor::getLocation() const {
  219. if (auto *D = Source.dyn_cast<const Decl *>())
  220. return D->getLocation();
  221. if (auto *E = Source.dyn_cast<const Expr *>())
  222. return E->getExprLoc();
  223. llvm_unreachable("Invalid descriptor type");
  224. }
  225. InitMap::InitMap(unsigned N) : UninitFields(N) {
  226. for (unsigned I = 0; I < N / PER_FIELD; ++I) {
  227. data()[I] = 0;
  228. }
  229. }
  230. InitMap::T *InitMap::data() {
  231. auto *Start = reinterpret_cast<char *>(this) + align(sizeof(InitMap));
  232. return reinterpret_cast<T *>(Start);
  233. }
  234. bool InitMap::initialize(unsigned I) {
  235. unsigned Bucket = I / PER_FIELD;
  236. unsigned Mask = 1ull << static_cast<uint64_t>(I % PER_FIELD);
  237. if (!(data()[Bucket] & Mask)) {
  238. data()[Bucket] |= Mask;
  239. UninitFields -= 1;
  240. }
  241. return UninitFields == 0;
  242. }
  243. bool InitMap::isInitialized(unsigned I) {
  244. unsigned Bucket = I / PER_FIELD;
  245. unsigned Mask = 1ull << static_cast<uint64_t>(I % PER_FIELD);
  246. return data()[Bucket] & Mask;
  247. }
  248. InitMap *InitMap::allocate(unsigned N) {
  249. const size_t NumFields = ((N + PER_FIELD - 1) / PER_FIELD);
  250. const size_t Size = align(sizeof(InitMap)) + NumFields * PER_FIELD;
  251. return new (malloc(Size)) InitMap(N);
  252. }