1//===--- InterpState.h - Interpreter state for the constexpr 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// Definition of the interpreter state and entry point.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H
14#define LLVM_CLANG_AST_INTERP_INTERPSTATE_H
15
16#include "Context.h"
17#include "DynamicAllocator.h"
18#include "Floating.h"
19#include "Function.h"
20#include "InterpFrame.h"
21#include "InterpStack.h"
22#include "State.h"
23
24namespace clang {
25namespace interp {
26class Context;
27class SourceMapper;
28
29struct StdAllocatorCaller {
30 const Expr *Call = nullptr;
31 QualType AllocType;
32 explicit operator bool() { return Call; }
33};
34
35// FIXME: Create one for the "checking potential constant expression"
36// evaluation.
37enum class EvaluationKind : uint8_t {
38 None,
39 Dtor, /// We're checking for constant destruction of a global variable.
40};
41
42/// Interpreter context.
43class InterpState final : public State, public SourceMapper {
44public:
45 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
46 SourceMapper *M = nullptr);
47 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
48 const Function *Func);
49
50 ~InterpState();
51
52 void cleanup();
53
54 InterpState(const InterpState &) = delete;
55 InterpState &operator=(const InterpState &) = delete;
56
57 bool diagnosing() const { return getEvalStatus().Diag != nullptr; }
58
59 // Stack frame accessors.
60 const Frame *getCurrentFrame() override;
61 unsigned getCallStackDepth() override {
62 return Current ? (Current->getDepth() + 1) : 1;
63 }
64 bool stepsLeft() const override { return true; }
65 bool inConstantContext() const;
66
67 /// Deallocates a pointer.
68 void deallocate(Block *B);
69
70 /// Delegates source mapping to the mapper.
71 SourceInfo getSource(const Function *F, CodePtr PC) const override {
72 if (M)
73 return M->getSource(F, PC);
74
75 assert(F && "Function cannot be null");
76 return F->getSource(PC);
77 }
78
79 Context &getContext() const { return Ctx; }
80
81 void setEvalLocation(SourceLocation SL) { this->EvalLocation = SL; }
82
83 DynamicAllocator &getAllocator() {
84 if (!Alloc) {
85 Alloc = std::make_unique<DynamicAllocator>();
86 }
87
88 return *Alloc;
89 }
90
91 /// Diagnose any dynamic allocations that haven't been freed yet.
92 /// Will return \c false if there were any allocations to diagnose,
93 /// \c true otherwise.
94 bool maybeDiagnoseDanglingAllocations();
95
96 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
97
98 void *allocate(size_t Size, unsigned Align = 8) const {
99 if (!Allocator)
100 Allocator.emplace();
101 return Allocator->Allocate(Size, Alignment: Align);
102 }
103 template <typename T> T *allocate(size_t Num = 1) const {
104 return static_cast<T *>(allocate(Size: Num * sizeof(T), Align: alignof(T)));
105 }
106
107 template <typename T> T allocAP(unsigned BitWidth) {
108 unsigned NumWords = APInt::getNumWords(BitWidth);
109 if (NumWords == 1)
110 return T(BitWidth);
111 uint64_t *Mem = (uint64_t *)this->allocate(Size: NumWords * sizeof(uint64_t));
112 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
113 return T(Mem, BitWidth);
114 }
115
116 Floating allocFloat(const llvm::fltSemantics &Sem) {
117 if (Floating::singleWord(Sem))
118 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
119
120 unsigned NumWords =
121 APInt::getNumWords(BitWidth: llvm::APFloatBase::getSizeInBits(Sem));
122 uint64_t *Mem = (uint64_t *)this->allocate(Size: NumWords * sizeof(uint64_t));
123 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
124 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
125 }
126 const CXXRecordDecl **allocMemberPointerPath(unsigned Length) {
127 return reinterpret_cast<const CXXRecordDecl **>(
128 this->allocate(Size: Length * sizeof(CXXRecordDecl *)));
129 }
130
131 /// Note that a step has been executed. If there are no more steps remaining,
132 /// diagnoses and returns \c false.
133 bool noteStep(CodePtr OpPC);
134
135 bool initializingBlock(const Block *B) const {
136 for (PtrView V : InitializingPtrs)
137 if (V.block() == B)
138 return true;
139 return false;
140 }
141
142 bool lifetimeStartedInEvaluation(const Block *B) const {
143 if (EvalKind == EvaluationKind::None)
144 return B->getEvalID() == EvalID;
145
146 if (EvalKind == EvaluationKind::Dtor) {
147 assert(EvaluatingDecl);
148 if (B->getDescriptor()->asVarDecl() == EvaluatingDecl)
149 return EvaluatingDecl->getType().isConstQualified();
150 }
151 return false;
152 }
153
154 /// Return if we're checking if a global variable has a constant destructor.
155 bool checkingConstantDestruction() const {
156 return EvalKind == EvaluationKind::Dtor;
157 }
158 /// Return if we're checking if a global variable has a constant destructor
159 /// and the given pointer is pointing to the variable we're checking that for.
160 bool checkingConstantDestruction(const Pointer &Ptr) const {
161 return checkingConstantDestruction(VD: Ptr.getDeclDesc()->asVarDecl());
162 }
163 bool checkingConstantDestruction(const VarDecl *VD) const {
164 return EvalKind == EvaluationKind::Dtor && VD == EvaluatingDecl;
165 }
166
167private:
168 friend class EvaluationResult;
169 friend class InterpStateCCOverride;
170 /// Dead block chain.
171 DeadBlock *DeadBlocks = nullptr;
172 /// Reference to the offset-source mapping.
173 SourceMapper *M;
174 /// Allocator used for dynamic allocations performed via the program.
175 std::unique_ptr<DynamicAllocator> Alloc;
176 /// Allocator for everything else, e.g. floating-point values.
177 mutable std::optional<llvm::BumpPtrAllocator> Allocator;
178
179public:
180 CodePtr PC;
181 /// Reference to the module containing all bytecode.
182 Program &P;
183 /// Temporary stack.
184 InterpStack &Stk;
185 /// Interpreter Context.
186 Context &Ctx;
187 /// Bottom function frame.
188 InterpFrame BottomFrame;
189 /// The current frame.
190 InterpFrame *Current = nullptr;
191 /// Source location of the evaluating expression
192 SourceLocation EvalLocation;
193 /// Declaration we're initializing/evaluting, if any.
194 const VarDecl *EvaluatingDecl = nullptr;
195 /// Steps left during evaluation.
196 unsigned StepsLeft = 1;
197 /// Whether infinite evaluation steps have been requested. If this is false,
198 /// we use the StepsLeft value above.
199 const bool InfiniteSteps = false;
200 /// ID identifying this evaluation.
201 const unsigned EvalID;
202
203 EvaluationKind EvalKind = EvaluationKind::None;
204
205 /// Things needed to do speculative execution.
206 SmallVectorImpl<PartialDiagnosticAt> *PrevDiags = nullptr;
207 bool PrevDiagsEmitted = false;
208#ifndef NDEBUG
209 unsigned SpeculationDepth = 0;
210#endif
211 unsigned DiagIgnoreDepth = 0;
212 std::optional<bool> ConstantContextOverride;
213
214 llvm::SmallVector<
215 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
216 SeenGlobalTemporaries;
217
218 /// List of blocks we're currently running either constructors or destructors
219 /// for.
220 llvm::SmallVector<PtrView> InitializingPtrs;
221};
222
223class InterpStateCCOverride final {
224public:
225 InterpStateCCOverride(InterpState &Ctx, bool Value)
226 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
227 // We only override this if the new value is true.
228 Enabled = Value;
229 if (Enabled)
230 Ctx.ConstantContextOverride = Value;
231 }
232 ~InterpStateCCOverride() {
233 if (Enabled)
234 Ctx.ConstantContextOverride = OldCC;
235 }
236
237private:
238 bool Enabled;
239 InterpState &Ctx;
240 std::optional<bool> OldCC;
241};
242
243} // namespace interp
244} // namespace clang
245
246#endif
247