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 /// Reference to the module containing all bytecode.
181 Program &P;
182 /// Temporary stack.
183 InterpStack &Stk;
184 /// Interpreter Context.
185 Context &Ctx;
186 /// Bottom function frame.
187 InterpFrame BottomFrame;
188 /// The current frame.
189 InterpFrame *Current = nullptr;
190 /// Source location of the evaluating expression
191 SourceLocation EvalLocation;
192 /// Declaration we're initializing/evaluting, if any.
193 const VarDecl *EvaluatingDecl = nullptr;
194 /// Steps left during evaluation.
195 unsigned StepsLeft = 1;
196 /// Whether infinite evaluation steps have been requested. If this is false,
197 /// we use the StepsLeft value above.
198 const bool InfiniteSteps = false;
199 /// ID identifying this evaluation.
200 const unsigned EvalID;
201
202 EvaluationKind EvalKind = EvaluationKind::None;
203
204 /// Things needed to do speculative execution.
205 SmallVectorImpl<PartialDiagnosticAt> *PrevDiags = nullptr;
206 bool PrevDiagsEmitted = false;
207#ifndef NDEBUG
208 unsigned SpeculationDepth = 0;
209#endif
210 unsigned DiagIgnoreDepth = 0;
211 std::optional<bool> ConstantContextOverride;
212
213 llvm::SmallVector<
214 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
215 SeenGlobalTemporaries;
216
217 /// List of blocks we're currently running either constructors or destructors
218 /// for.
219 llvm::SmallVector<PtrView> InitializingPtrs;
220};
221
222class InterpStateCCOverride final {
223public:
224 InterpStateCCOverride(InterpState &Ctx, bool Value)
225 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
226 // We only override this if the new value is true.
227 Enabled = Value;
228 if (Enabled)
229 Ctx.ConstantContextOverride = Value;
230 }
231 ~InterpStateCCOverride() {
232 if (Enabled)
233 Ctx.ConstantContextOverride = OldCC;
234 }
235
236private:
237 bool Enabled;
238 InterpState &Ctx;
239 std::optional<bool> OldCC;
240};
241
242} // namespace interp
243} // namespace clang
244
245#endif
246