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