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
36 bool ConvertResultToRValue) {
37 this->ConvertResultToRValue = ConvertResultToRValue;
38 EvalResult.setSource(E);
39
40 if (!this->visitExpr(E)) {
41 // EvalResult may already have a result set, but something failed
42 // after that (e.g. evaluating destructors).
43 EvalResult.setInvalid();
44 }
45
46 return std::move(this->EvalResult);
47}
48
51 this->CheckFullyInitialized = CheckFullyInitialized;
52 this->ConvertResultToRValue =
53 VD->getAnyInitializer() &&
55 EvalResult.setSource(VD);
56
57 if (!this->visitDecl(VD) && EvalResult.empty())
58 EvalResult.setInvalid();
59
60 return std::move(this->EvalResult);
61}
62
64 CurrentLabel = Label;
65}
66
68
70 // Allocate memory for a local.
71 auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
72 auto *B = new (Memory.get()) Block(D, /*isStatic=*/false);
73 B->invokeCtor();
74
75 // Initialize local variable inline descriptor.
76 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
77 Desc.Desc = D;
78 Desc.Offset = sizeof(InlineDescriptor);
79 Desc.IsActive = true;
80 Desc.IsBase = false;
81 Desc.IsFieldMutable = false;
82 Desc.IsConst = false;
83 Desc.IsInitialized = false;
84
85 // Register the local.
86 unsigned Off = Locals.size();
87 Locals.insert({Off, std::move(Memory)});
88 return {Off, D};
89}
90
92 if (isActive()) {
93 if (S.Stk.pop<bool>())
94 ActiveLabel = Label;
95 }
96 return true;
97}
98
100 if (isActive()) {
101 if (!S.Stk.pop<bool>())
102 ActiveLabel = Label;
103 }
104 return true;
105}
106
108 if (isActive())
109 CurrentLabel = ActiveLabel = Label;
110 return true;
111}
112
114 if (isActive())
115 ActiveLabel = Label;
116 CurrentLabel = Label;
117 return true;
118}
119
120template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
121 if (!isActive())
122 return true;
123 using T = typename PrimConv<OpType>::T;
124 EvalResult.setValue(S.Stk.pop<T>().toAPValue());
125 return true;
126}
127
128template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
129 if (!isActive())
130 return true;
131
132 const Pointer &Ptr = S.Stk.pop<Pointer>();
133 // Implicitly convert lvalue to rvalue, if requested.
134 if (ConvertResultToRValue) {
135 if (std::optional<APValue> V = Ptr.toRValue(Ctx)) {
136 EvalResult.setValue(*V);
137 } else {
138 return false;
139 }
140 } else {
141 if (CheckFullyInitialized) {
142 if (!EvalResult.checkFullyInitialized(S, Ptr))
143 return false;
144
145 std::optional<APValue> RValueResult = Ptr.toRValue(Ctx);
146 if (!RValueResult)
147 return false;
148 EvalResult.setValue(*RValueResult);
149 } else {
150 EvalResult.setValue(Ptr.toAPValue());
151 }
152 }
153
154 return true;
155}
156template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
157 if (!isActive())
158 return true;
159 // Function pointers cannot be converted to rvalues.
160 EvalResult.setFunctionPointer(S.Stk.pop<FunctionPointer>());
161 return true;
162}
163
164bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
165 EvalResult.setValid();
166 return true;
167}
168
169bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
170 const auto &Ptr = S.Stk.pop<Pointer>();
171 if (std::optional<APValue> APV = Ptr.toRValue(S.getCtx())) {
172 EvalResult.setValue(*APV);
173 return true;
174 }
175
176 EvalResult.setInvalid();
177 return false;
178}
179
180bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
181 if (!isActive())
182 return true;
183
184 Block *B = getLocal(I);
185 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
186 return true;
187}
188
189template <PrimType OpType>
190bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
191 if (!isActive())
192 return true;
193
194 using T = typename PrimConv<OpType>::T;
195
196 Block *B = getLocal(I);
197 S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
198 return true;
199}
200
201template <PrimType OpType>
202bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
203 if (!isActive())
204 return true;
205
206 using T = typename PrimConv<OpType>::T;
207
208 Block *B = getLocal(I);
209 *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
210 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
211 Desc.IsInitialized = true;
212
213 return true;
214}
215
216bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
217 if (!isActive())
218 return true;
219
220 for (auto &Local : Descriptors[I]) {
221 Block *B = getLocal(Local.Offset);
222 S.deallocate(B);
223 }
224
225 return true;
226}
227
228//===----------------------------------------------------------------------===//
229// Opcode evaluators
230//===----------------------------------------------------------------------===//
231
232#define GET_EVAL_IMPL
233#include "Opcodes.inc"
234#undef GET_EVAL_IMPL
#define V(N, I)
Definition: ASTContext.h:3266
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
QualType getType() const
Definition: Expr.h:142
bool isAnyComplexType() const
Definition: Type.h:7469
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:1342
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:115
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:78
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:106
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:91
bool isInitialized() const
Definition: InterpBlock.h:74
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:35
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:49
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
bool jumpFalse(const LabelTy &Label)
Definition: EvalEmitter.cpp:99
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:69
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:63
bool fallthrough(const LabelTy &Label)
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:67
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:78
virtual bool visitDecl(const VarDecl *VD)=0
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: EvalEmitter.cpp:91
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:26
T pop()
Returns the value from the top of the stack and removes it.
Definition: InterpStack.h:42
void push(Tys &&... Args)
Constructs a value in place on the top of the stack.
Definition: InterpStack.h:34
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:109
InterpFrame * Current
The current frame.
Definition: InterpState.h:113
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:48
ASTContext & getCtx() const override
Definition: InterpState.h:59
A pointer to a memory block, live or dead.
Definition: Pointer.h:65
std::optional< APValue > toRValue(const Context &Ctx) const
Converts the pointer to an APValue that is an rvalue.
Definition: Pointer.cpp:221
APValue toAPValue() const
Converts the pointer to an APValue.
Definition: Pointer.cpp:85
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.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:88
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:200
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:69
Information about a local's storage.
Definition: Function.h:38
unsigned Offset
Offset of the local in frame.
Definition: Function.h:40