clang 19.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 "Integral.h"
14#include "Opcode.h"
15#include "PrimType.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18
19using namespace clang;
20using namespace clang::interp;
21
22unsigned Program::getOrCreateNativePointer(const void *Ptr) {
23 auto It = NativePointerIndices.find(Ptr);
24 if (It != NativePointerIndices.end())
25 return It->second;
26
27 unsigned Idx = NativePointers.size();
28 NativePointers.push_back(Ptr);
29 NativePointerIndices[Ptr] = Idx;
30 return Idx;
31}
32
33const void *Program::getNativePointer(unsigned Idx) {
34 return NativePointers[Idx];
35}
36
38 const size_t CharWidth = S->getCharByteWidth();
39 const size_t BitWidth = CharWidth * Ctx.getCharBit();
40
41 PrimType CharType;
42 switch (CharWidth) {
43 case 1:
44 CharType = PT_Sint8;
45 break;
46 case 2:
47 CharType = PT_Uint16;
48 break;
49 case 4:
50 CharType = PT_Uint32;
51 break;
52 default:
53 llvm_unreachable("unsupported character width");
54 }
55
56 // Create a descriptor for the string.
57 Descriptor *Desc = allocateDescriptor(S, CharType, Descriptor::InlineDescMD,
58 S->getLength() + 1,
59 /*isConst=*/true,
60 /*isTemporary=*/false,
61 /*isMutable=*/false);
62
63 // Allocate storage for the string.
64 // The byte length does not include the null terminator.
65 unsigned I = Globals.size();
66 unsigned Sz = Desc->getAllocSize();
67 auto *G = new (Allocator, Sz) Global(Desc, /*isStatic=*/true,
68 /*isExtern=*/false);
69 G->block()->invokeCtor();
70
71 new (G->block()->rawData()) InlineDescriptor(Desc);
72 Globals.push_back(G);
73
74 // Construct the string in storage.
75 const Pointer Ptr(G->block());
76 for (unsigned I = 0, N = S->getLength(); I <= N; ++I) {
77 Pointer Field = Ptr.atIndex(I).narrow();
78 const uint32_t CodePoint = I == N ? 0 : S->getCodeUnit(I);
79 switch (CharType) {
80 case PT_Sint8: {
82 Field.deref<T>() = T::from(CodePoint, BitWidth);
83 Field.initialize();
84 break;
85 }
86 case PT_Uint16: {
88 Field.deref<T>() = T::from(CodePoint, BitWidth);
89 Field.initialize();
90 break;
91 }
92 case PT_Uint32: {
94 Field.deref<T>() = T::from(CodePoint, BitWidth);
95 Field.initialize();
96 break;
97 }
98 default:
99 llvm_unreachable("unsupported character type");
100 }
101 }
102 return I;
103}
104
105Pointer Program::getPtrGlobal(unsigned Idx) const {
106 assert(Idx < Globals.size());
107 return Pointer(Globals[Idx]->block());
108}
109
110std::optional<unsigned> Program::getGlobal(const ValueDecl *VD) {
111 if (auto It = GlobalIndices.find(VD); It != GlobalIndices.end())
112 return It->second;
113
114 // Find any previous declarations which were already evaluated.
115 std::optional<unsigned> Index;
116 for (const Decl *P = VD->getPreviousDecl(); P; P = P->getPreviousDecl()) {
117 if (auto It = GlobalIndices.find(P); It != GlobalIndices.end()) {
118 Index = It->second;
119 break;
120 }
121 }
122
123 // Map the decl to the existing index.
124 if (Index)
125 GlobalIndices[VD] = *Index;
126
127 return std::nullopt;
128}
129
130std::optional<unsigned> Program::getOrCreateGlobal(const ValueDecl *VD,
131 const Expr *Init) {
132 if (auto Idx = getGlobal(VD))
133 return Idx;
134
135 if (auto Idx = createGlobal(VD, Init)) {
136 GlobalIndices[VD] = *Idx;
137 return Idx;
138 }
139 return std::nullopt;
140}
141
142std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) {
143 // Dedup blocks since they are immutable and pointers cannot be compared.
144 if (auto It = DummyVariables.find(VD); It != DummyVariables.end())
145 return It->second;
146
147 QualType QT = VD->getType();
148 if (const auto *RT = QT->getAs<ReferenceType>())
149 QT = RT->getPointeeType();
150
151 Descriptor *Desc;
152 if (std::optional<PrimType> T = Ctx.classify(QT))
153 Desc = createDescriptor(VD, *T, std::nullopt, true, false);
154 else
155 Desc = createDescriptor(VD, QT.getTypePtr(), std::nullopt, true, false);
156 if (!Desc)
157 Desc = allocateDescriptor(VD);
158
159 assert(Desc);
160 Desc->makeDummy();
161
162 assert(Desc->isDummy());
163
164 // Allocate a block for storage.
165 unsigned I = Globals.size();
166
167 auto *G = new (Allocator, Desc->getAllocSize())
168 Global(getCurrentDecl(), Desc, /*IsStatic=*/true, /*IsExtern=*/false);
169 G->block()->invokeCtor();
170
171 Globals.push_back(G);
172 DummyVariables[VD] = I;
173 return I;
174}
175
176std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
177 const Expr *Init) {
178 bool IsStatic, IsExtern;
179 if (const auto *Var = dyn_cast<VarDecl>(VD)) {
181 IsExtern = Var->hasExternalStorage();
184 IsStatic = true;
185 IsExtern = false;
186 } else {
187 IsStatic = false;
188 IsExtern = true;
189 }
190 if (auto Idx = createGlobal(VD, VD->getType(), IsStatic, IsExtern, Init)) {
191 for (const Decl *P = VD; P; P = P->getPreviousDecl())
192 GlobalIndices[P] = *Idx;
193 return *Idx;
194 }
195 return std::nullopt;
196}
197
198std::optional<unsigned> Program::createGlobal(const Expr *E) {
199 return createGlobal(E, E->getType(), /*isStatic=*/true, /*isExtern=*/false);
200}
201
202std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,
203 bool IsStatic, bool IsExtern,
204 const Expr *Init) {
205 // Create a descriptor for the global.
206 Descriptor *Desc;
207 const bool IsConst = Ty.isConstQualified();
208 const bool IsTemporary = D.dyn_cast<const Expr *>();
209 if (std::optional<PrimType> T = Ctx.classify(Ty))
210 Desc =
211 createDescriptor(D, *T, Descriptor::InlineDescMD, IsConst, IsTemporary);
212 else
214 IsConst, IsTemporary);
215
216 if (!Desc)
217 return std::nullopt;
218
219 // Allocate a block for storage.
220 unsigned I = Globals.size();
221
222 auto *G = new (Allocator, Desc->getAllocSize())
223 Global(getCurrentDecl(), Desc, IsStatic, IsExtern);
224 G->block()->invokeCtor();
225
226 // Initialize InlineDescriptor fields.
227 new (G->block()->rawData()) InlineDescriptor(Desc);
228 Globals.push_back(G);
229
230 return I;
231}
232
234 F = F->getCanonicalDecl();
235 assert(F);
236 auto It = Funcs.find(F);
237 return It == Funcs.end() ? nullptr : It->second.get();
238}
239
241 // Use the actual definition as a key.
242 RD = RD->getDefinition();
243 if (!RD)
244 return nullptr;
245
246 if (!RD->isCompleteDefinition())
247 return nullptr;
248
249 // Deduplicate records.
250 if (auto It = Records.find(RD); It != Records.end())
251 return It->second;
252
253 // We insert nullptr now and replace that later, so recursive calls
254 // to this function with the same RecordDecl don't run into
255 // infinite recursion.
256 Records.insert({RD, nullptr});
257
258 // Number of bytes required by fields and base classes.
259 unsigned BaseSize = 0;
260 // Number of bytes required by virtual base.
261 unsigned VirtSize = 0;
262
263 // Helper to get a base descriptor.
264 auto GetBaseDesc = [this](const RecordDecl *BD,
265 const Record *BR) -> const Descriptor * {
266 if (!BR)
267 return nullptr;
268 return allocateDescriptor(BD, BR, std::nullopt, /*isConst=*/false,
269 /*isTemporary=*/false,
270 /*isMutable=*/false);
271 };
272
273 // Reserve space for base classes.
274 Record::BaseList Bases;
275 Record::VirtualBaseList VirtBases;
276 if (const auto *CD = dyn_cast<CXXRecordDecl>(RD)) {
277
278 for (const CXXBaseSpecifier &Spec : CD->bases()) {
279 if (Spec.isVirtual())
280 continue;
281
282 // In error cases, the base might not be a RecordType.
283 if (const auto *RT = Spec.getType()->getAs<RecordType>()) {
284 const RecordDecl *BD = RT->getDecl();
285 const Record *BR = getOrCreateRecord(BD);
286
287 if (const Descriptor *Desc = GetBaseDesc(BD, BR)) {
288 BaseSize += align(sizeof(InlineDescriptor));
289 Bases.push_back({BD, BaseSize, Desc, BR});
290 BaseSize += align(BR->getSize());
291 continue;
292 }
293 }
294 return nullptr;
295 }
296
297 for (const CXXBaseSpecifier &Spec : CD->vbases()) {
298
299 if (const auto *RT = Spec.getType()->getAs<RecordType>()) {
300 const RecordDecl *BD = RT->getDecl();
301 const Record *BR = getOrCreateRecord(BD);
302
303 if (const Descriptor *Desc = GetBaseDesc(BD, BR)) {
304 VirtSize += align(sizeof(InlineDescriptor));
305 VirtBases.push_back({BD, VirtSize, Desc, BR});
306 VirtSize += align(BR->getSize());
307 continue;
308 }
309 }
310 return nullptr;
311 }
312 }
313
314 // Reserve space for fields.
315 Record::FieldList Fields;
316 for (const FieldDecl *FD : RD->fields()) {
317 // Note that we DO create fields and descriptors
318 // for unnamed bitfields here, even though we later ignore
319 // them everywhere. That's so the FieldDecl's getFieldIndex() matches.
320
321 // Reserve space for the field's descriptor and the offset.
322 BaseSize += align(sizeof(InlineDescriptor));
323
324 // Classify the field and add its metadata.
325 QualType FT = FD->getType();
326 const bool IsConst = FT.isConstQualified();
327 const bool IsMutable = FD->isMutable();
328 const Descriptor *Desc;
329 if (std::optional<PrimType> T = Ctx.classify(FT)) {
330 Desc = createDescriptor(FD, *T, std::nullopt, IsConst,
331 /*isTemporary=*/false, IsMutable);
332 } else {
333 Desc = createDescriptor(FD, FT.getTypePtr(), std::nullopt, IsConst,
334 /*isTemporary=*/false, IsMutable);
335 }
336 if (!Desc)
337 return nullptr;
338 Fields.push_back({FD, BaseSize, Desc});
339 BaseSize += align(Desc->getAllocSize());
340 }
341
342 Record *R = new (Allocator) Record(RD, std::move(Bases), std::move(Fields),
343 std::move(VirtBases), VirtSize, BaseSize);
344 Records[RD] = R;
345 return R;
346}
347
350 bool IsConst, bool IsTemporary,
351 bool IsMutable, const Expr *Init) {
352
353 // Classes and structures.
354 if (const auto *RT = Ty->getAs<RecordType>()) {
355 if (const auto *Record = getOrCreateRecord(RT->getDecl()))
356 return allocateDescriptor(D, Record, MDSize, IsConst, IsTemporary,
357 IsMutable);
358 }
359
360 // Arrays.
361 if (const auto ArrayType = Ty->getAsArrayTypeUnsafe()) {
363 // Array of well-known bounds.
364 if (auto CAT = dyn_cast<ConstantArrayType>(ArrayType)) {
365 size_t NumElems = CAT->getZExtSize();
366 if (std::optional<PrimType> T = Ctx.classify(ElemTy)) {
367 // Arrays of primitives.
368 unsigned ElemSize = primSize(*T);
369 if (std::numeric_limits<unsigned>::max() / ElemSize <= NumElems) {
370 return {};
371 }
372 return allocateDescriptor(D, *T, MDSize, NumElems, IsConst, IsTemporary,
373 IsMutable);
374 } else {
375 // Arrays of composites. In this case, the array is a list of pointers,
376 // followed by the actual elements.
377 const Descriptor *ElemDesc = createDescriptor(
378 D, ElemTy.getTypePtr(), std::nullopt, IsConst, IsTemporary);
379 if (!ElemDesc)
380 return nullptr;
381 unsigned ElemSize =
382 ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
383 if (std::numeric_limits<unsigned>::max() / ElemSize <= NumElems)
384 return {};
385 return allocateDescriptor(D, ElemDesc, MDSize, NumElems, IsConst,
386 IsTemporary, IsMutable);
387 }
388 }
389
390 // Array of unknown bounds - cannot be accessed and pointer arithmetic
391 // is forbidden on pointers to such objects.
392 if (isa<IncompleteArrayType>(ArrayType) ||
393 isa<VariableArrayType>(ArrayType)) {
394 if (std::optional<PrimType> T = Ctx.classify(ElemTy)) {
395 return allocateDescriptor(D, *T, MDSize, IsTemporary,
397 } else {
398 const Descriptor *Desc = createDescriptor(D, ElemTy.getTypePtr(),
399 MDSize, IsConst, IsTemporary);
400 if (!Desc)
401 return nullptr;
402 return allocateDescriptor(D, Desc, MDSize, IsTemporary,
404 }
405 }
406 }
407
408 // Atomic types.
409 if (const auto *AT = Ty->getAs<AtomicType>()) {
410 const Type *InnerTy = AT->getValueType().getTypePtr();
411 return createDescriptor(D, InnerTy, MDSize, IsConst, IsTemporary,
412 IsMutable);
413 }
414
415 // Complex types - represented as arrays of elements.
416 if (const auto *CT = Ty->getAs<ComplexType>()) {
417 PrimType ElemTy = *Ctx.classify(CT->getElementType());
418 return allocateDescriptor(D, ElemTy, MDSize, 2, IsConst, IsTemporary,
419 IsMutable);
420 }
421
422 // Same with vector types.
423 if (const auto *VT = Ty->getAs<VectorType>()) {
424 PrimType ElemTy = *Ctx.classify(VT->getElementType());
425 return allocateDescriptor(D, ElemTy, MDSize, VT->getNumElements(), IsConst,
426 IsTemporary, IsMutable);
427 }
428
429 return nullptr;
430}
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
bool IsStatic
Definition: Format.cpp:2987
llvm::MachO::Records Records
Definition: MachO.h:39
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3518
QualType getElementType() const
Definition: Type.h:3530
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
Complex values, per C99 6.2.5p11.
Definition: Type.h:3086
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Definition: DeclBase.h:1051
This represents one expression.
Definition: Expr.h:110
QualType getType() const
Definition: Expr.h:142
Represents a member of a struct/union/class.
Definition: Decl.h:3058
Represents a function declaration or definition.
Definition: Decl.h:1971
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: Decl.cpp:3617
A global _GUID constant.
Definition: DeclCXX.h:4289
A (possibly-)qualified type.
Definition: Type.h:940
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7359
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:7432
Represents a struct/union/class.
Definition: Decl.h:4169
field_range fields() const
Definition: Decl.h:4375
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4360
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5549
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:3380
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1773
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3688
A template parameter object.
The base class of the type hierarchy.
Definition: Type.h:1813
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:695
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition: Type.h:8179
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8126
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition: DeclCXX.h:4346
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:706
QualType getType() const
Definition: Decl.h:717
Represents a GCC generic vector type.
Definition: Type.h:3969
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:182
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition: Context.h:97
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
Bytecode function.
Definition: Function.h:77
A pointer to a memory block, live or dead.
Definition: Pointer.h:80
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:170
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:137
std::optional< unsigned > getOrCreateGlobal(const ValueDecl *VD, const Expr *Init=nullptr)
Returns or creates a global an creates an index to it.
Definition: Program.cpp:130
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:233
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Definition: Program.h:72
std::optional< unsigned > createGlobal(const ValueDecl *VD, const Expr *Init)
Creates a global and returns its index.
Definition: Program.cpp:176
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
Definition: Program.cpp:33
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
Definition: Program.h:116
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
Definition: Program.cpp:22
unsigned createGlobalString(const StringLiteral *S)
Emits a string literal among global data.
Definition: Program.cpp:37
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
Definition: Program.cpp:105
std::optional< unsigned > getCurrentDecl() const
Returns the current declaration ID.
Definition: Program.h:143
std::optional< unsigned > getOrCreateDummy(const ValueDecl *VD)
Returns or creates a dummy value for unknown declarations.
Definition: Program.cpp:142
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:240
Structure/Class descriptor.
Definition: Record.h:25
unsigned getSize() const
Returns the size of the record.
Definition: Record.h:58
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:99
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:32
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:22
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:27
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition: Address.h:294
const FunctionProtoType * T
Token to denote structures of unknown size.
Definition: Descriptor.h:109
Describes a memory block created by an allocation site.
Definition: Descriptor.h:91
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:204
void makeDummy()
Make this descriptor a dummy descriptor.
Definition: Descriptor.h:166
bool isDummy() const
Checks if this is a dummy descriptor.
Definition: Descriptor.h:232
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:112
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:111
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56
Mapping from primitive types to their representation.
Definition: PrimType.h:73