clang 17.0.0git
EvalEmitter.cpp
Go to the documentation of this file.
1//===--- EvalEmitter.cpp - Instruction emitter for the 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 "EvalEmitter.h"
10#include "Context.h"
11#include "Interp.h"
12#include "Opcode.h"
13#include "Program.h"
14#include "clang/AST/DeclCXX.h"
15
16using namespace clang;
17using namespace clang::interp;
18
19using APSInt = llvm::APSInt;
20template <typename T> using Expected = llvm::Expected<T>;
21
23 InterpStack &Stk, APValue &Result)
24 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), Result(Result) {
25 // Create a dummy frame for the interpreter which does not have locals.
26 S.Current =
27 new InterpFrame(S, /*Func=*/nullptr, /*Caller=*/nullptr, CodePtr());
28}
29
31 if (this->visitExpr(E))
32 return true;
33 if (BailLocation)
34 return llvm::make_error<ByteCodeGenError>(*BailLocation);
35 return false;
36}
37
39 if (this->visitDecl(VD))
40 return true;
41 if (BailLocation)
42 return llvm::make_error<ByteCodeGenError>(*BailLocation);
43 return false;
44}
45
47 CurrentLabel = Label;
48}
49
51
53 // Allocate memory for a local.
54 auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
55 auto *B = new (Memory.get()) Block(D, /*isStatic=*/false);
56 B->invokeCtor();
57
58 // Initialize local variable inline descriptor.
59 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
60 Desc.Desc = D;
61 Desc.Offset = sizeof(InlineDescriptor);
62 Desc.IsActive = true;
63 Desc.IsBase = false;
64 Desc.IsFieldMutable = false;
65 Desc.IsConst = false;
66 Desc.IsInitialized = false;
67
68 // Register the local.
69 unsigned Off = Locals.size();
70 Locals.insert({Off, std::move(Memory)});
71 return {Off, D};
72}
73
75 if (!BailLocation)
76 BailLocation = Loc;
77 return false;
78}
79
81 if (isActive()) {
82 if (S.Stk.pop<bool>())
83 ActiveLabel = Label;
84 }
85 return true;
86}
87
89 if (isActive()) {
90 if (!S.Stk.pop<bool>())
91 ActiveLabel = Label;
92 }
93 return true;
94}
95
97 if (isActive())
98 CurrentLabel = ActiveLabel = Label;
99 return true;
100}
101
103 if (isActive())
104 ActiveLabel = Label;
105 CurrentLabel = Label;
106 return true;
107}
108
109template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
110 if (!isActive())
111 return true;
112 using T = typename PrimConv<OpType>::T;
113 return ReturnValue<T>(S.Stk.pop<T>(), Result);
114}
115
116bool EvalEmitter::emitRetVoid(const SourceInfo &Info) { return true; }
117
118bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
119 // Method to recursively traverse composites.
120 std::function<bool(QualType, const Pointer &, APValue &)> Composite;
121 Composite = [this, &Composite](QualType Ty, const Pointer &Ptr, APValue &R) {
122 if (auto *AT = Ty->getAs<AtomicType>())
123 Ty = AT->getValueType();
124
125 if (auto *RT = Ty->getAs<RecordType>()) {
126 auto *Record = Ptr.getRecord();
127 assert(Record && "Missing record descriptor");
128
129 bool Ok = true;
130 if (RT->getDecl()->isUnion()) {
131 const FieldDecl *ActiveField = nullptr;
133 for (auto &F : Record->fields()) {
134 const Pointer &FP = Ptr.atField(F.Offset);
135 QualType FieldTy = F.Decl->getType();
136 if (FP.isActive()) {
137 if (std::optional<PrimType> T = Ctx.classify(FieldTy)) {
138 TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
139 } else {
140 Ok &= Composite(FieldTy, FP, Value);
141 }
142 break;
143 }
144 }
145 R = APValue(ActiveField, Value);
146 } else {
147 unsigned NF = Record->getNumFields();
148 unsigned NB = Record->getNumBases();
149 unsigned NV = Ptr.isBaseClass() ? 0 : Record->getNumVirtualBases();
150
151 R = APValue(APValue::UninitStruct(), NB, NF);
152
153 for (unsigned I = 0; I < NF; ++I) {
154 const Record::Field *FD = Record->getField(I);
155 QualType FieldTy = FD->Decl->getType();
156 const Pointer &FP = Ptr.atField(FD->Offset);
158
159 if (std::optional<PrimType> T = Ctx.classify(FieldTy)) {
160 TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
161 } else {
162 Ok &= Composite(FieldTy, FP, Value);
163 }
164 }
165
166 for (unsigned I = 0; I < NB; ++I) {
167 const Record::Base *BD = Record->getBase(I);
168 QualType BaseTy = Ctx.getASTContext().getRecordType(BD->Decl);
169 const Pointer &BP = Ptr.atField(BD->Offset);
170 Ok &= Composite(BaseTy, BP, R.getStructBase(I));
171 }
172
173 for (unsigned I = 0; I < NV; ++I) {
174 const Record::Base *VD = Record->getVirtualBase(I);
175 QualType VirtBaseTy = Ctx.getASTContext().getRecordType(VD->Decl);
176 const Pointer &VP = Ptr.atField(VD->Offset);
177 Ok &= Composite(VirtBaseTy, VP, R.getStructBase(NB + I));
178 }
179 }
180 return Ok;
181 }
182 if (auto *AT = Ty->getAsArrayTypeUnsafe()) {
183 const size_t NumElems = Ptr.getNumElems();
184 QualType ElemTy = AT->getElementType();
185 R = APValue(APValue::UninitArray{}, NumElems, NumElems);
186
187 bool Ok = true;
188 for (unsigned I = 0; I < NumElems; ++I) {
189 APValue &Slot = R.getArrayInitializedElt(I);
190 const Pointer &EP = Ptr.atIndex(I);
191 if (std::optional<PrimType> T = Ctx.classify(ElemTy)) {
192 TYPE_SWITCH(*T, Ok &= ReturnValue<T>(EP.deref<T>(), Slot));
193 } else {
194 Ok &= Composite(ElemTy, EP.narrow(), Slot);
195 }
196 }
197 return Ok;
198 }
199 llvm_unreachable("invalid value to return");
200 };
201
202 // Return the composite type.
203 const auto &Ptr = S.Stk.pop<Pointer>();
204 return Composite(Ptr.getType(), Ptr, Result);
205}
206
207bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
208 if (!isActive())
209 return true;
210
211 Block *B = getLocal(I);
212 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
213 return true;
214}
215
216template <PrimType OpType>
217bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
218 if (!isActive())
219 return true;
220
221 using T = typename PrimConv<OpType>::T;
222
223 Block *B = getLocal(I);
224 S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
225 return true;
226}
227
228template <PrimType OpType>
229bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
230 if (!isActive())
231 return true;
232
233 using T = typename PrimConv<OpType>::T;
234
235 Block *B = getLocal(I);
236 *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
237 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
238 Desc.IsInitialized = true;
239
240 return true;
241}
242
243bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
244 if (!isActive())
245 return true;
246
247 for (auto &Local : Descriptors[I]) {
248 Block *B = getLocal(Local.Offset);
249 S.deallocate(B);
250 }
251
252 return true;
253}
254
255//===----------------------------------------------------------------------===//
256// Opcode evaluators
257//===----------------------------------------------------------------------===//
258
259#define GET_EVAL_IMPL
260#include "Opcodes.inc"
261#undef GET_EVAL_IMPL
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
llvm::APSInt APSInt
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:79
std::string Label
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
APValue & getArrayInitializedElt(unsigned I)
Definition: APValue.h:502
APValue & getStructField(unsigned i)
Definition: APValue.h:543
QualType getRecordType(const RecordDecl *Decl) const
This represents one expression.
Definition: Expr.h:110
Represents a member of a struct/union/class.
Definition: Decl.h:2941
A (possibly-)qualified type.
Definition: Type.h:736
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4835
Encodes a location in the source.
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition: Type.h:7477
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7424
QualType getType() const
Definition: Decl.h:712
Represents a variable declaration or definition.
Definition: Decl.h:913
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:100
char * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:90
char * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:77
Pointer into the code segment.
Definition: Source.h:26
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:35
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:53
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:80
bool jump(const LabelTy &Label)
Definition: EvalEmitter.cpp:96
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk, APValue &Result)
Definition: EvalEmitter.cpp:22
bool jumpFalse(const LabelTy &Label)
Definition: EvalEmitter.cpp:88
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:52
bool bail(const Stmt *S)
Definition: EvalEmitter.h:59
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:46
bool fallthrough(const LabelTy &Label)
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:50
llvm::Expected< bool > interpretDecl(const VarDecl *VD)
Definition: EvalEmitter.cpp:38
llvm::Expected< bool > interpretExpr(const Expr *E)
Definition: EvalEmitter.cpp:30
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:80
virtual bool visitDecl(const VarDecl *VD)=0
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: EvalEmitter.cpp:80
Frame storing local variables.
Definition: InterpFrame.h:29
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:24
T pop()
Returns the value from the top of the stack and removes it.
Definition: InterpStack.h:40
void push(Tys &&... Args)
Constructs a value in place on the top of the stack.
Definition: InterpStack.h:32
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:101
InterpFrame * Current
The current frame.
Definition: InterpState.h:105
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:49
A pointer to a memory block, live or dead.
Definition: Pointer.h:61
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:99
Pointer atIndex(unsigned Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:81
bool isActive() const
Checks if the object is active.
Definition: Pointer.h:273
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition: Pointer.h:93
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:313
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Structure/Class descriptor.
Definition: Record.h:25
unsigned getNumBases() const
Definition: Record.h:87
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:30
const Base * getVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition: Record.cpp:42
unsigned getNumFields() const
Definition: Record.h:78
unsigned getNumVirtualBases() const
Definition: Record.h:95
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:74
const Base * getBase(const RecordDecl *FD) const
Returns a base descriptor.
Definition: Record.cpp:36
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:70
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
#define bool
Definition: stdbool.h:20
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:164
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:53
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:68
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:66
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:55
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:64
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:59
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:70
Mapping from primitive types to their representation.
Definition: PrimType.h:44
Describes a base class.
Definition: Record.h:35
const RecordDecl * Decl
Definition: Record.h:36
Describes a record field.
Definition: Record.h:28
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:35
unsigned Offset
Offset of the local in frame.
Definition: Function.h:37