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