clang  16.0.0git
Program.cpp
Go to the documentation of this file.
1 //===--- Program.cpp - Bytecode 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 
9 #include "Program.h"
10 #include "ByteCodeStmtGen.h"
11 #include "Context.h"
12 #include "Function.h"
13 #include "Opcode.h"
14 #include "PrimType.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclCXX.h"
17 
18 using namespace clang;
19 using namespace clang::interp;
20 
21 unsigned Program::getOrCreateNativePointer(const void *Ptr) {
22  auto It = NativePointerIndices.find(Ptr);
23  if (It != NativePointerIndices.end())
24  return It->second;
25 
26  unsigned Idx = NativePointers.size();
27  NativePointers.push_back(Ptr);
28  NativePointerIndices[Ptr] = Idx;
29  return Idx;
30 }
31 
32 const void *Program::getNativePointer(unsigned Idx) {
33  return NativePointers[Idx];
34 }
35 
37  const size_t CharWidth = S->getCharByteWidth();
38  const size_t BitWidth = CharWidth * Ctx.getCharBit();
39 
40  PrimType CharType;
41  switch (CharWidth) {
42  case 1:
43  CharType = PT_Sint8;
44  break;
45  case 2:
46  CharType = PT_Uint16;
47  break;
48  case 4:
49  CharType = PT_Uint32;
50  break;
51  default:
52  llvm_unreachable("unsupported character width");
53  }
54 
55  // Create a descriptor for the string.
56  Descriptor *Desc = allocateDescriptor(S, CharType, S->getLength() + 1,
57  /*isConst=*/true,
58  /*isTemporary=*/false,
59  /*isMutable=*/false);
60 
61  // Allocate storage for the string.
62  // The byte length does not include the null terminator.
63  unsigned I = Globals.size();
64  unsigned Sz = Desc->getAllocSize();
65  auto *G = new (Allocator, Sz) Global(Desc, /*isStatic=*/true,
66  /*isExtern=*/false);
67  G->block()->invokeCtor();
68  Globals.push_back(G);
69 
70  // Construct the string in storage.
71  const Pointer Ptr(G->block());
72  for (unsigned I = 0, N = S->getLength(); I <= N; ++I) {
73  Pointer Field = Ptr.atIndex(I).narrow();
74  const uint32_t CodePoint = I == N ? 0 : S->getCodeUnit(I);
75  switch (CharType) {
76  case PT_Sint8: {
77  using T = PrimConv<PT_Sint8>::T;
78  Field.deref<T>() = T::from(CodePoint, BitWidth);
79  break;
80  }
81  case PT_Uint16: {
82  using T = PrimConv<PT_Uint16>::T;
83  Field.deref<T>() = T::from(CodePoint, BitWidth);
84  break;
85  }
86  case PT_Uint32: {
87  using T = PrimConv<PT_Uint32>::T;
88  Field.deref<T>() = T::from(CodePoint, BitWidth);
89  break;
90  }
91  default:
92  llvm_unreachable("unsupported character type");
93  }
94  }
95  return I;
96 }
97 
99  assert(Idx < Globals.size());
100  return Pointer(Globals[Idx]->block());
101 }
102 
104  auto It = GlobalIndices.find(VD);
105  if (It != GlobalIndices.end())
106  return It->second;
107 
108  // Find any previous declarations which were already evaluated.
110  for (const Decl *P = VD; P; P = P->getPreviousDecl()) {
111  auto It = GlobalIndices.find(P);
112  if (It != GlobalIndices.end()) {
113  Index = It->second;
114  break;
115  }
116  }
117 
118  // Map the decl to the existing index.
119  if (Index) {
120  GlobalIndices[VD] = *Index;
121  return {};
122  }
123 
124  return Index;
125 }
126 
128  if (auto Idx = getGlobal(VD))
129  return Idx;
130 
131  if (auto Idx = createGlobal(VD, nullptr)) {
132  GlobalIndices[VD] = *Idx;
133  return Idx;
134  }
135  return {};
136 }
137 
139  auto &ASTCtx = Ctx.getASTContext();
140 
141  // Create a pointer to an incomplete array of the specified elements.
142  QualType ElemTy = PD->getType()->castAs<PointerType>()->getPointeeType();
143  QualType Ty = ASTCtx.getIncompleteArrayType(ElemTy, ArrayType::Normal, 0);
144 
145  // Dedup blocks since they are immutable and pointers cannot be compared.
146  auto It = DummyParams.find(PD);
147  if (It != DummyParams.end())
148  return It->second;
149 
150  if (auto Idx = createGlobal(PD, Ty, /*isStatic=*/true, /*isExtern=*/true)) {
151  DummyParams[PD] = *Idx;
152  return Idx;
153  }
154  return {};
155 }
156 
158  const Expr *Init) {
159  bool IsStatic, IsExtern;
160  if (auto *Var = dyn_cast<VarDecl>(VD)) {
161  IsStatic = !Var->hasLocalStorage();
162  IsExtern = !Var->getAnyInitializer();
163  } else {
164  IsStatic = false;
165  IsExtern = true;
166  }
167  if (auto Idx = createGlobal(VD, VD->getType(), IsStatic, IsExtern, Init)) {
168  for (const Decl *P = VD; P; P = P->getPreviousDecl())
169  GlobalIndices[P] = *Idx;
170  return *Idx;
171  }
172  return {};
173 }
174 
176  return createGlobal(E, E->getType(), /*isStatic=*/true, /*isExtern=*/false);
177 }
178 
180  bool IsStatic, bool IsExtern,
181  const Expr *Init) {
182  // Create a descriptor for the global.
183  Descriptor *Desc;
184  const bool IsConst = Ty.isConstQualified();
185  const bool IsTemporary = D.dyn_cast<const Expr *>();
186  if (auto T = Ctx.classify(Ty)) {
187  Desc = createDescriptor(D, *T, IsConst, IsTemporary);
188  } else {
189  Desc = createDescriptor(D, Ty.getTypePtr(), IsConst, IsTemporary);
190  }
191  if (!Desc)
192  return {};
193 
194  // Allocate a block for storage.
195  unsigned I = Globals.size();
196 
197  auto *G = new (Allocator, Desc->getAllocSize())
198  Global(getCurrentDecl(), Desc, IsStatic, IsExtern);
199  G->block()->invokeCtor();
200 
201  Globals.push_back(G);
202 
203  return I;
204 }
205 
207  F = F->getDefinition();
208  auto It = Funcs.find(F);
209  return It == Funcs.end() ? nullptr : It->second.get();
210 }
211 
213  // Use the actual definition as a key.
214  RD = RD->getDefinition();
215  if (!RD)
216  return nullptr;
217 
218  // Deduplicate records.
219  auto It = Records.find(RD);
220  if (It != Records.end()) {
221  return It->second;
222  }
223 
224  // We insert nullptr now and replace that later, so recursive calls
225  // to this function with the same RecordDecl don't run into
226  // infinite recursion.
227  Records.insert({RD, nullptr});
228 
229  // Number of bytes required by fields and base classes.
230  unsigned BaseSize = 0;
231  // Number of bytes required by virtual base.
232  unsigned VirtSize = 0;
233 
234  // Helper to get a base descriptor.
235  auto GetBaseDesc = [this](const RecordDecl *BD, Record *BR) -> Descriptor * {
236  if (!BR)
237  return nullptr;
238  return allocateDescriptor(BD, BR, /*isConst=*/false,
239  /*isTemporary=*/false,
240  /*isMutable=*/false);
241  };
242 
243  // Reserve space for base classes.
244  Record::BaseList Bases;
245  Record::VirtualBaseList VirtBases;
246  if (auto *CD = dyn_cast<CXXRecordDecl>(RD)) {
247  for (const CXXBaseSpecifier &Spec : CD->bases()) {
248  if (Spec.isVirtual())
249  continue;
250 
251  const RecordDecl *BD = Spec.getType()->castAs<RecordType>()->getDecl();
252  Record *BR = getOrCreateRecord(BD);
253  if (Descriptor *Desc = GetBaseDesc(BD, BR)) {
254  BaseSize += align(sizeof(InlineDescriptor));
255  Bases.push_back({BD, BaseSize, Desc, BR});
256  BaseSize += align(BR->getSize());
257  continue;
258  }
259  return nullptr;
260  }
261 
262  for (const CXXBaseSpecifier &Spec : CD->vbases()) {
263  const RecordDecl *BD = Spec.getType()->castAs<RecordType>()->getDecl();
264  Record *BR = getOrCreateRecord(BD);
265 
266  if (Descriptor *Desc = GetBaseDesc(BD, BR)) {
267  VirtSize += align(sizeof(InlineDescriptor));
268  VirtBases.push_back({BD, VirtSize, Desc, BR});
269  VirtSize += align(BR->getSize());
270  continue;
271  }
272  return nullptr;
273  }
274  }
275 
276  // Reserve space for fields.
277  Record::FieldList Fields;
278  for (const FieldDecl *FD : RD->fields()) {
279  // Reserve space for the field's descriptor and the offset.
280  BaseSize += align(sizeof(InlineDescriptor));
281 
282  // Classify the field and add its metadata.
283  QualType FT = FD->getType();
284  const bool IsConst = FT.isConstQualified();
285  const bool IsMutable = FD->isMutable();
286  Descriptor *Desc;
287  if (llvm::Optional<PrimType> T = Ctx.classify(FT)) {
288  Desc = createDescriptor(FD, *T, IsConst, /*isTemporary=*/false,
289  IsMutable);
290  } else {
291  Desc = createDescriptor(FD, FT.getTypePtr(), IsConst,
292  /*isTemporary=*/false, IsMutable);
293  }
294  if (!Desc)
295  return nullptr;
296  Fields.push_back({FD, BaseSize, Desc});
297  BaseSize += align(Desc->getAllocSize());
298  }
299 
300  Record *R = new (Allocator) Record(RD, std::move(Bases), std::move(Fields),
301  std::move(VirtBases), VirtSize, BaseSize);
302  Records[RD] = R;
303  return R;
304 }
305 
307  bool IsConst, bool IsTemporary,
308  bool IsMutable, const Expr *Init) {
309  // Classes and structures.
310  if (auto *RT = Ty->getAs<RecordType>()) {
311  if (auto *Record = getOrCreateRecord(RT->getDecl()))
312  return allocateDescriptor(D, Record, IsConst, IsTemporary, IsMutable);
313  }
314 
315  // Arrays.
316  if (auto ArrayType = Ty->getAsArrayTypeUnsafe()) {
317  QualType ElemTy = ArrayType->getElementType();
318  // Array of well-known bounds.
319  if (auto CAT = dyn_cast<ConstantArrayType>(ArrayType)) {
320  size_t NumElems = CAT->getSize().getZExtValue();
321  if (llvm::Optional<PrimType> T = Ctx.classify(ElemTy)) {
322  // Arrays of primitives.
323  unsigned ElemSize = primSize(*T);
324  if (std::numeric_limits<unsigned>::max() / ElemSize <= NumElems) {
325  return {};
326  }
327  return allocateDescriptor(D, *T, NumElems, IsConst, IsTemporary,
328  IsMutable);
329  } else {
330  // Arrays of composites. In this case, the array is a list of pointers,
331  // followed by the actual elements.
332  Descriptor *ElemDesc =
333  createDescriptor(D, ElemTy.getTypePtr(), IsConst, IsTemporary);
334  if (!ElemDesc)
335  return nullptr;
336  InterpSize ElemSize =
337  ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
338  if (std::numeric_limits<unsigned>::max() / ElemSize <= NumElems)
339  return {};
340  return allocateDescriptor(D, ElemDesc, NumElems, IsConst, IsTemporary,
341  IsMutable);
342  }
343  }
344 
345  // Array of unknown bounds - cannot be accessed and pointer arithmetic
346  // is forbidden on pointers to such objects.
347  if (isa<IncompleteArrayType>(ArrayType)) {
348  if (llvm::Optional<PrimType> T = Ctx.classify(ElemTy)) {
349  return allocateDescriptor(D, *T, IsTemporary,
351  } else {
352  Descriptor *Desc =
353  createDescriptor(D, ElemTy.getTypePtr(), IsConst, IsTemporary);
354  if (!Desc)
355  return nullptr;
356  return allocateDescriptor(D, Desc, IsTemporary,
358  }
359  }
360  }
361 
362  // Atomic types.
363  if (auto *AT = Ty->getAs<AtomicType>()) {
364  const Type *InnerTy = AT->getValueType().getTypePtr();
365  return createDescriptor(D, InnerTy, IsConst, IsTemporary, IsMutable);
366  }
367 
368  // Complex types - represented as arrays of elements.
369  if (auto *CT = Ty->getAs<ComplexType>()) {
370  PrimType ElemTy = *Ctx.classify(CT->getElementType());
371  return allocateDescriptor(D, ElemTy, 2, IsConst, IsTemporary, IsMutable);
372  }
373 
374  return nullptr;
375 }
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::Type::getAsArrayTypeUnsafe
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition: Type.h:7439
clang::interp::align
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:58
clang::QualType::isConstQualified
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:6694
Opcode.h
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:75
llvm::SmallVector< Base, 8 >
clang::interp::Context::getCharBit
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:122
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::FieldDecl
Represents a member of a struct/union/class.
Definition: Decl.h:2930
clang::ParmVarDecl
Represents a parameter to a function.
Definition: Decl.h:1712
DeclCXX.h
clang::interp::PT_Uint16
@ PT_Uint16
Definition: PrimType.h:32
llvm::Optional< unsigned >
clang::RecordDecl::getDefinition
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4194
clang::interp::Program::getCurrentDecl
llvm::Optional< unsigned > getCurrentDecl() const
Returns the current declaration ID.
Definition: Program.h:139
clang::interp::Descriptor::getAllocSize
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:134
clang::interp::Program::getFunction
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:206
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:36
clang::ArrayType::Normal
@ Normal
Definition: Type.h:3019
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1565
Decl.h
ByteCodeStmtGen.h
clang::FunctionDecl::getDefinition
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition: Decl.h:2173
clang::interp::Program::getGlobal
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Definition: Program.h:73
clang::interp::Descriptor::UnknownSize
Token to denote structures of unknown size.
Definition: Descriptor.h:67
clang::interp
Definition: ASTContext.h:155
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4816
clang::interp::Program::createDescriptor
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
Definition: Program.h:115
clang::ArrayType
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3012
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7386
clang::interp::PT_Uint32
@ PT_Uint32
Definition: PrimType.h:34
clang::interp::PrimType
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:28
clang::interp::Pointer::atIndex
Pointer atIndex(unsigned Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:55
clang::StringLiteral
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1776
PrimType.h
clang::interp::Program::getOrCreateNativePointer
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
Definition: Program.cpp:21
clang::interp::Program::getOrCreateGlobal
llvm::Optional< unsigned > getOrCreateGlobal(const ValueDecl *VD)
Returns or creates a global an creates an index to it.
Definition: Program.cpp:127
clang::interp::Program::getOrCreateRecord
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:212
clang::Type::castAs
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7453
clang::interp::Program::getPtrGlobal
Pointer getPtrGlobal(unsigned Idx)
Returns a pointer to a global.
Definition: Program.cpp:98
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::interp::primSize
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:18
clang::ValueDecl
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:701
clang::ComplexType
Complex values, per C99 6.2.5p11.
Definition: Type.h:2716
clang::interp::Program::createGlobal
llvm::Optional< unsigned > createGlobal(const ValueDecl *VD, const Expr *E)
Creates a global and returns its index.
Definition: Program.cpp:157
clang::RecordDecl::fields
field_range fields() const
Definition: Decl.h:4209
clang::interp::InlineDescriptor
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:163
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::interp::Program::getOrCreateDummy
llvm::Optional< unsigned > getOrCreateDummy(const ParmVarDecl *PD)
Returns or creates a dummy value for parameters.
Definition: Program.cpp:138
IsStatic
bool IsStatic
Definition: Format.cpp:2727
clang::interp::PT_Sint8
@ PT_Sint8
Definition: PrimType.h:29
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang::PointerType
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2769
clang
Definition: CalledOnceCheck.h:17
Function.h
clang::interp::Function
Bytecode function.
Definition: Function.h:74
clang::Expr::getType
QualType getType() const
Definition: Expr.h:141
clang::CXXBaseSpecifier
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
unsigned
clang::interp::Context::getASTContext
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:53
clang::QualType::getTypePtr
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6622
Program.h
clang::interp::DeclTy
unsigned llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:26
clang::interp::Program::createGlobalString
unsigned createGlobalString(const StringLiteral *S)
Emits a string literal among global data.
Definition: Program.cpp:36
clang::AtomicType
Definition: Type.h:6440
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:712
clang::ArrayType::getElementType
QualType getElementType() const
Definition: Type.h:3033
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::interp::Record
Structure/Class descriptor.
Definition: Record.h:24
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1904
clang::interp::PrimConv
Mapping from primitive types to their representation.
Definition: PrimType.h:42
clang::RecordDecl
Represents a struct/union/class.
Definition: Decl.h:3983
clang::interp::Program::getNativePointer
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
Definition: Program.cpp:32
Context.h
clang::interp::Pointer::narrow
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:73
clang::interp::Record::getSize
unsigned getSize() const
Returns the size of the record.
Definition: Record.h:56