Descriptor.cpp 11 KB

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