clang: lib/AST/ByteCode/Program.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
16
17using namespace clang;
19
21 auto It = NativePointerIndices.find(Ptr);
22 if (It != NativePointerIndices.end())
23 return It->second;
24
25 unsigned Idx = NativePointers.size();
26 NativePointers.push_back(Ptr);
27 NativePointerIndices[Ptr] = Idx;
28 return Idx;
29}
30
32 return NativePointers[Idx];
33}
34
36 const size_t CharWidth = S->getCharByteWidth();
37 const size_t BitWidth = CharWidth * Ctx.getCharBit();
38
40 switch (CharWidth) {
41 case 1:
43 break;
44 case 2:
46 break;
47 case 4:
49 break;
50 default:
51 llvm_unreachable("unsupported character width");
52 }
53
56
57
59 S->getLength() + 1,
60 true,
61 false,
62 false);
63
64
65
66 unsigned I = Globals.size();
68 auto *G = new (Allocator, Sz) Global(Ctx.getEvalID(), Desc, true,
69 false);
70 G->block()->invokeCtor();
71
73 Globals.push_back(G);
74
75
76 const Pointer Ptr(G->block());
77 for (unsigned I = 0, N = S->getLength(); I <= N; ++I) {
79 const uint32_t CodePoint = I == N ? 0 : S->getCodeUnit(I);
80 switch (CharType) {
83 Field.deref<T>() = T::from(CodePoint, BitWidth);
84 Field.initialize();
85 break;
86 }
89 Field.deref<T>() = T::from(CodePoint, BitWidth);
90 Field.initialize();
91 break;
92 }
95 Field.deref<T>() = T::from(CodePoint, BitWidth);
96 Field.initialize();
97 break;
98 }
99 default:
100 llvm_unreachable("unsupported character type");
101 }
102 }
103 return I;
104}
105
107 assert(Idx < Globals.size());
108 return Pointer(Globals[Idx]->block());
109}
110
112 if (auto It = GlobalIndices.find(VD); It != GlobalIndices.end())
113 return It->second;
114
115
116 std::optional Index;
118 if (auto It = GlobalIndices.find(P); It != GlobalIndices.end()) {
119 Index = It->second;
120 break;
121 }
122 }
123
124
125 if (Index)
126 GlobalIndices[VD] = *Index;
127
128 return std::nullopt;
129}
130
132 if (auto It = GlobalIndices.find(E); It != GlobalIndices.end())
133 return It->second;
134 return std::nullopt;
135}
136
140 return Idx;
141
143 GlobalIndices[VD] = *Idx;
144 return Idx;
145 }
146 return std::nullopt;
147}
148
150 assert(D);
151
152 if (auto It = DummyVariables.find(D.getOpaqueValue());
153 It != DummyVariables.end())
154 return It->second;
155
157 bool IsWeak = false;
158 if (const auto *E = D.dyn_cast<const Expr *>()) {
160 } else {
161 const ValueDecl *VD = cast(cast<const Decl *>(D));
162 IsWeak = VD->isWeak();
166 }
167 assert(!QT.isNull());
168
170 if (std::optional T = Ctx.classify(QT))
172 false);
173 else
175 true, false);
176 if (!Desc)
177 Desc = allocateDescriptor(D);
178
179 assert(Desc);
181
183
184
185 unsigned I = Globals.size();
186
187 auto *G = new (Allocator, Desc->getAllocSize())
189 false, IsWeak);
190 G->block()->invokeCtor();
191
192 Globals.push_back(G);
193 DummyVariables[D.getOpaqueValue()] = I;
194 return I;
195}
196
200 bool IsWeak = VD->isWeak();
201 if (const auto *Var = dyn_cast(VD)) {
203 IsExtern = Var->hasExternalStorage();
207 IsExtern = false;
208 } else {
210 IsExtern = true;
211 }
212
213
214
215 if (auto Idx =
217 for (const Decl *P = VD; P; P = P->getPreviousDecl()) {
218 if (P != VD) {
219 unsigned PIdx = GlobalIndices[P];
220 if (Globals[PIdx]->block()->isExtern())
221 Globals[PIdx] = Globals[*Idx];
222 }
223 GlobalIndices[P] = *Idx;
224 }
225 return *Idx;
226 }
227 return std::nullopt;
228}
229
232 return Idx;
234 false, false)) {
235 GlobalIndices[E] = *Idx;
236 return *Idx;
237 }
238 return std::nullopt;
239}
240
242 bool IsStatic, bool IsExtern,
243 bool IsWeak, const Expr *Init) {
244
247 const bool IsTemporary = D.dyn_cast<const Expr *>();
248 if (std::optional T = Ctx.classify(Ty))
250 else
252 IsTemporary);
253
254 if (!Desc)
255 return std::nullopt;
256
257
258 unsigned I = Globals.size();
259
262 G->block()->invokeCtor();
263
264
268 Globals.push_back(G);
269
270 return I;
271}
272
275 assert(F);
276 auto It = Funcs.find(F);
277 return It == Funcs.end() ? nullptr : It->second.get();
278}
279
281
283 if (!RD)
284 return nullptr;
285
287 return nullptr;
288
289
290
291
292 auto [It, Inserted] = Records.try_emplace(RD);
293 if (!Inserted)
294 return It->second;
295
296
297 unsigned BaseSize = 0;
298
299 unsigned VirtSize = 0;
300
301
302 auto GetBaseDesc = [this](const RecordDecl *BD,
304 if (!BR)
305 return nullptr;
306 return allocateDescriptor(BD, BR, std::nullopt, false,
307 false,
308 false);
309 };
310
311
312 Record::BaseList Bases;
313 Record::VirtualBaseList VirtBases;
314 if (const auto *CD = dyn_cast(RD)) {
316 if (Spec.isVirtual())
317 continue;
318
319
320 const auto *RT = Spec.getType()->getAs<RecordType>();
321 if (!RT)
322 return nullptr;
323 const RecordDecl *BD = RT->getDecl();
325
326 const Descriptor *Desc = GetBaseDesc(BD, BR);
327 if (!Desc)
328 return nullptr;
329
331 Bases.push_back({BD, BaseSize, Desc, BR});
333 }
334
336 const auto *RT = Spec.getType()->getAs<RecordType>();
337 if (!RT)
338 return nullptr;
339
340 const RecordDecl *BD = RT->getDecl();
342
343 const Descriptor *Desc = GetBaseDesc(BD, BR);
344 if (!Desc)
345 return nullptr;
346
348 VirtBases.push_back({BD, VirtSize, Desc, BR});
350 }
351 }
352
353
354 Record::FieldList Fields;
356 FD = FD->getFirstDecl();
357
358
359
360
361
363
364
367 const bool IsMutable = FD->isMutable();
369 if (std::optional T = Ctx.classify(FT)) {
371 false, IsMutable);
372 } else {
374 false, IsMutable);
375 }
376 if (!Desc)
377 return nullptr;
378 Fields.push_back({FD, BaseSize, Desc});
380 }
381
382 Record *R = new (Allocator) Record(RD, std::move(Bases), std::move(Fields),
383 std::move(VirtBases), VirtSize, BaseSize);
385 return R;
386}
387
390 bool IsConst, bool IsTemporary,
391 bool IsMutable, const Expr *Init) {
392
393
396 return allocateDescriptor(D, Record, MDSize, IsConst, IsTemporary,
397 IsMutable);
398 }
399
400
403
404 if (const auto *CAT = dyn_cast(ArrayType)) {
405 size_t NumElems = CAT->getZExtSize();
406 if (std::optional T = Ctx.classify(ElemTy)) {
407
408 unsigned ElemSize = primSize(*T);
409 if (std::numeric_limits::max() / ElemSize <= NumElems) {
410 return {};
411 }
412 return allocateDescriptor(D, *T, MDSize, NumElems, IsConst, IsTemporary,
413 IsMutable);
414 } else {
415
416
418 D, ElemTy.getTypePtr(), std::nullopt, IsConst, IsTemporary);
419 if (!ElemDesc)
420 return nullptr;
422 if (std::numeric_limits::max() / ElemSize <= NumElems)
423 return {};
424 return allocateDescriptor(D, ElemDesc, MDSize, NumElems, IsConst,
425 IsTemporary, IsMutable);
426 }
427 }
428
429
430
431 if (isa(ArrayType) ||
432 isa(ArrayType)) {
433 if (std::optional T = Ctx.classify(ElemTy)) {
434 return allocateDescriptor(D, *T, MDSize, IsTemporary,
436 } else {
438 MDSize, IsConst, IsTemporary);
439 if (!Desc)
440 return nullptr;
441 return allocateDescriptor(D, Desc, MDSize, IsTemporary,
443 }
444 }
445 }
446
447
449 const Type *InnerTy = AT->getValueType().getTypePtr();
450 return createDescriptor(D, InnerTy, MDSize, IsConst, IsTemporary,
451 IsMutable);
452 }
453
454
457 return allocateDescriptor(D, ElemTy, MDSize, 2, IsConst, IsTemporary,
458 IsMutable);
459 }
460
461
464 return allocateDescriptor(D, ElemTy, MDSize, VT->getNumElements(), IsConst,
465 IsTemporary, IsMutable);
466 }
467
468 return nullptr;
469}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Records Records
Represents an array type, per C99 6.7.5.2 - Array Declarators.
QualType getElementType() const
Represents a base class of a C++ class.
Complex values, per C99 6.2.5p11.
Decl - This represents one declaration (or definition), e.g.
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
This represents one expression.
Represents a member of a struct/union/class.
Represents a function declaration or definition.
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
bool isConstQualified() const
Determine whether this type is const-qualified.
Represents a struct/union/class.
field_range fields() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Base for LValueReferenceType and RValueReferenceType.
StringLiteral - This represents a string literal expression, e.g.
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
A template parameter object.
The base class of the type hierarchy.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
const T * getAs() const
Member-template getAs'.
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
bool isWeak() const
Determine whether this symbol is weakly-imported, or declared with the weak or weak-ref attr.
Represents a GCC generic vector type.
unsigned getCharBit() const
Returns CHAR_BIT.
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
std::optional< PrimType > classify(QualType T) const
Classifies a type.
unsigned getEvalID() const
A pointer to a memory block, live or dead.
Pointer narrow() const
Restricts the scope of an array element pointer.
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
std::optional< unsigned > getOrCreateGlobal(const ValueDecl *VD, const Expr *Init=nullptr)
Returns or creates a global an creates an index to it.
Function * getFunction(const FunctionDecl *F)
Returns a function.
Block * getGlobal(unsigned Idx)
Returns the value of a global.
std::optional< unsigned > createGlobal(const ValueDecl *VD, const Expr *Init)
Creates a global and returns its index.
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
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.
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
unsigned getOrCreateDummy(const DeclTy &D)
Returns or creates a dummy value for unknown declarations.
std::optional< unsigned > getCurrentDecl() const
Returns the current declaration ID.
unsigned createGlobalString(const StringLiteral *S, const Expr *Base=nullptr)
Emits a string literal among global data.
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Structure/Class descriptor.
unsigned getSize() const
Returns the size of the record.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
PrimType
Enumeration of the primitive types of the VM.
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
const FunctionProtoType * T
Token to denote structures of unknown size.
Describes a memory block created by an allocation site.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
void makeDummy()
Make this descriptor a dummy descriptor.
static constexpr MetadataSize GlobalMD
bool isDummy() const
Checks if this is a dummy descriptor.
std::optional< unsigned > MetadataSize
Descriptor used for global variables.
GlobalInitState InitState
Inline descriptor embedded in structures and arrays.
Mapping from primitive types to their representation.