clang 19.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 "IntegralAP.h"
12#include "Interp.h"
13#include "Opcode.h"
14#include "clang/AST/DeclCXX.h"
15
16using namespace clang;
17using namespace clang::interp;
18
20 InterpStack &Stk)
21 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {
22 // Create a dummy frame for the interpreter which does not have locals.
23 S.Current =
24 new InterpFrame(S, /*Func=*/nullptr, /*Caller=*/nullptr, CodePtr(), 0);
25}
26
28 for (auto &[K, V] : Locals) {
29 Block *B = reinterpret_cast<Block *>(V.get());
30 if (B->isInitialized())
31 B->invokeDtor();
32 }
33}
34
35/// Clean up all our resources. This needs to done in failed evaluations before
36/// we call InterpStack::clear(), because there might be a Pointer on the stack
37/// pointing into a Block in the EvalEmitter.
39
41 bool ConvertResultToRValue) {
43 this->ConvertResultToRValue = ConvertResultToRValue && !isa<ConstantExpr>(E);
44 this->CheckFullyInitialized = isa<ConstantExpr>(E);
45 EvalResult.setSource(E);
46
47 if (!this->visitExpr(E)) {
48 // EvalResult may already have a result set, but something failed
49 // after that (e.g. evaluating destructors).
50 EvalResult.setInvalid();
51 }
52
53 return std::move(this->EvalResult);
54}
55
58 this->CheckFullyInitialized = CheckFullyInitialized;
59
60 if (const Expr *Init = VD->getAnyInitializer()) {
61 QualType T = VD->getType();
62 this->ConvertResultToRValue = !Init->isGLValue() && !T->isPointerType() &&
64 } else
65 this->ConvertResultToRValue = false;
66
67 EvalResult.setSource(VD);
68
69 if (!this->visitDecl(VD) && EvalResult.empty())
70 EvalResult.setInvalid();
71
72 return std::move(this->EvalResult);
73}
74
76 CurrentLabel = Label;
77}
78
80
82 // Allocate memory for a local.
83 auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
84 auto *B = new (Memory.get()) Block(D, /*isStatic=*/false);
85 B->invokeCtor();
86
87 // Initialize local variable inline descriptor.
88 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
89 Desc.Desc = D;
90 Desc.Offset = sizeof(InlineDescriptor);
91 Desc.IsActive = true;
92 Desc.IsBase = false;
93 Desc.IsFieldMutable = false;
94 Desc.IsConst = false;
95 Desc.IsInitialized = false;
96
97 // Register the local.
98 unsigned Off = Locals.size();
99 Locals.insert({Off, std::move(Memory)});
100 return {Off, D};
101}
102
104 if (isActive()) {
105 if (S.Stk.pop<bool>())
106 ActiveLabel = Label;
107 }
108 return true;
109}
110
112 if (isActive()) {
113 if (!S.Stk.pop<bool>())
114 ActiveLabel = Label;
115 }
116 return true;
117}
118
120 if (isActive())
121 CurrentLabel = ActiveLabel = Label;
122 return true;
123}
124
126 if (isActive())
127 ActiveLabel = Label;
128 CurrentLabel = Label;
129 return true;
130}
131
132template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
133 if (!isActive())
134 return true;
135 using T = typename PrimConv<OpType>::T;
136 EvalResult.setValue(S.Stk.pop<T>().toAPValue());
137 return true;
138}
139
140template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
141 if (!isActive())
142 return true;
143
144 const Pointer &Ptr = S.Stk.pop<Pointer>();
145
146 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
147 return false;
148
149 // Implicitly convert lvalue to rvalue, if requested.
150 if (ConvertResultToRValue) {
151 if (std::optional<APValue> V = Ptr.toRValue(Ctx)) {
152 EvalResult.setValue(*V);
153 } else {
154 return false;
155 }
156 } else {
157 EvalResult.setValue(Ptr.toAPValue());
158 }
159
160 return true;
161}
162template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
163 if (!isActive())
164 return true;
165 // Function pointers cannot be converted to rvalues.
166 EvalResult.setFunctionPointer(S.Stk.pop<FunctionPointer>());
167 return true;
168}
169
170bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
171 EvalResult.setValid();
172 return true;
173}
174
175bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
176 const auto &Ptr = S.Stk.pop<Pointer>();
177
178 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
179 return false;
180
181 if (std::optional<APValue> APV = Ptr.toRValue(S.getCtx())) {
182 EvalResult.setValue(*APV);
183 return true;
184 }
185
186 EvalResult.setInvalid();
187 return false;
188}
189
190bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
191 if (!isActive())
192 return true;
193
194 Block *B = getLocal(I);
195 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
196 return true;
197}
198
199template <PrimType OpType>
200bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
201 if (!isActive())
202 return true;
203
204 using T = typename PrimConv<OpType>::T;
205
206 Block *B = getLocal(I);
207 S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
208 return true;
209}
210
211template <PrimType OpType>
212bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
213 if (!isActive())
214 return true;
215
216 using T = typename PrimConv<OpType>::T;
217
218 Block *B = getLocal(I);
219 *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
220 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
221 Desc.IsInitialized = true;
222
223 return true;
224}
225
226bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
227 if (!isActive())
228 return true;
229
230 for (auto &Local : Descriptors[I]) {
231 Block *B = getLocal(Local.Offset);
232 S.deallocate(B);
233 }
234
235 return true;
236}
237
238//===----------------------------------------------------------------------===//
239// Opcode evaluators
240//===----------------------------------------------------------------------===//
241
242#define GET_EVAL_IMPL
243#include "Opcodes.inc"
244#undef GET_EVAL_IMPL
#define V(N, I)
Definition: ASTContext.h:3301
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, const APValue &Value)
Check that this evaluated value is fully-initialized and can be loaded by an lvalue-to-rvalue convers...
std::string Label
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
A (possibly-)qualified type.
Definition: Type.h:940
bool isPointerType() const
Definition: Type.h:7625
bool isObjCObjectPointerType() const
Definition: Type.h:7761
QualType getType() const
Definition: Decl.h:717
Represents a variable declaration or definition.
Definition: Decl.h:918
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition: Decl.h:1345
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:119
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:82
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:110
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:95
bool isInitialized() const
Definition: InterpBlock.h:78
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
bool jump(const LabelTy &Label)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false)
Definition: EvalEmitter.cpp:40
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:56
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
bool jumpFalse(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:81
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:75
bool fallthrough(const LabelTy &Label)
void cleanup()
Clean up all resources.
Definition: EvalEmitter.cpp:38
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:79
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
Definition: EvalEmitter.cpp:19
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:81
virtual bool visitDecl(const VarDecl *VD)=0
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Defines the result of an evaluation.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Frame storing local variables.
Definition: InterpFrame.h:28
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:27
T pop()
Returns the value from the top of the stack and removes it.
Definition: InterpStack.h:43
void push(Tys &&... Args)
Constructs a value in place on the top of the stack.
Definition: InterpStack.h:35
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:117
InterpFrame * Current
The current frame.
Definition: InterpState.h:121
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:50
ASTContext & getCtx() const override
Definition: InterpState.h:61
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:103
A pointer to a memory block, live or dead.
Definition: Pointer.h:80
std::optional< APValue > toRValue(const Context &Ctx) const
Converts the pointer to an APValue that is an rvalue.
Definition: Pointer.cpp:322
APValue toAPValue() const
Converts the pointer to an APValue.
Definition: Pointer.cpp:122
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
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
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:75
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:72
const Descriptor * Desc
Definition: Descriptor.h:80
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:58
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:69
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:63
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:78
Mapping from primitive types to their representation.
Definition: PrimType.h:75
Information about a local's storage.
Definition: Function.h:38
unsigned Offset
Offset of the local in frame.
Definition: Function.h:40