clang 23.0.0git
InterpState.h
Go to the documentation of this file.
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
30 const Expr *Call = nullptr;
32 explicit operator bool() { return Call; }
33};
34
35/// Interpreter context.
36class InterpState final : public State, public SourceMapper {
37public:
38 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
39 SourceMapper *M = nullptr);
40 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
41 const Function *Func);
42
44
45 void cleanup();
46
47 InterpState(const InterpState &) = delete;
48 InterpState &operator=(const InterpState &) = delete;
49
50 bool diagnosing() const { return getEvalStatus().Diag != nullptr; }
51
52 // Stack frame accessors.
53 const Frame *getCurrentFrame() override;
54 unsigned getCallStackDepth() override {
55 return Current ? (Current->getDepth() + 1) : 1;
56 }
57 bool stepsLeft() const override { return true; }
58 bool inConstantContext() const;
59
60 /// Deallocates a pointer.
61 void deallocate(Block *B);
62
63 /// Delegates source mapping to the mapper.
64 SourceInfo getSource(const Function *F, CodePtr PC) const override {
65 if (M)
66 return M->getSource(F, PC);
67
68 assert(F && "Function cannot be null");
69 return F->getSource(PC);
70 }
71
72 Context &getContext() const { return Ctx; }
73
75
77 if (!Alloc) {
78 Alloc = std::make_unique<DynamicAllocator>();
79 }
80
81 return *Alloc;
82 }
83
84 /// Diagnose any dynamic allocations that haven't been freed yet.
85 /// Will return \c false if there were any allocations to diagnose,
86 /// \c true otherwise.
88
89 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
90
91 void *allocate(size_t Size, unsigned Align = 8) const {
92 if (!Allocator)
93 Allocator.emplace();
94 return Allocator->Allocate(Size, Align);
95 }
96 template <typename T> T *allocate(size_t Num = 1) const {
97 return static_cast<T *>(allocate(Num * sizeof(T), alignof(T)));
98 }
99
100 template <typename T> T allocAP(unsigned BitWidth) {
101 unsigned NumWords = APInt::getNumWords(BitWidth);
102 if (NumWords == 1)
103 return T(BitWidth);
104 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
105 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
106 return T(Mem, BitWidth);
107 }
108
109 Floating allocFloat(const llvm::fltSemantics &Sem) {
110 if (Floating::singleWord(Sem))
111 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
112
113 unsigned NumWords =
114 APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem));
115 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
116 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
117 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
118 }
119 const CXXRecordDecl **allocMemberPointerPath(unsigned Length) {
120 return reinterpret_cast<const CXXRecordDecl **>(
121 this->allocate(Length * sizeof(CXXRecordDecl *)));
122 }
123
124 /// Note that a step has been executed. If there are no more steps remaining,
125 /// diagnoses and returns \c false.
126 bool noteStep(CodePtr OpPC);
127
128 bool initializingBlock(const Block *B) const {
130 if (V.block() == B)
131 return true;
132 return false;
133 }
134
135private:
136 friend class EvaluationResult;
138 /// Dead block chain.
139 DeadBlock *DeadBlocks = nullptr;
140 /// Reference to the offset-source mapping.
141 SourceMapper *M;
142 /// Allocator used for dynamic allocations performed via the program.
143 std::unique_ptr<DynamicAllocator> Alloc;
144 /// Allocator for everything else, e.g. floating-point values.
145 mutable std::optional<llvm::BumpPtrAllocator> Allocator;
146
147public:
148 /// Reference to the module containing all bytecode.
150 /// Temporary stack.
152 /// Interpreter Context.
154 /// Bottom function frame.
156 /// The current frame.
158 /// Source location of the evaluating expression
160 /// Declaration we're initializing/evaluting, if any.
161 const VarDecl *EvaluatingDecl = nullptr;
162 /// Steps left during evaluation.
163 unsigned StepsLeft = 1;
164 /// Whether infinite evaluation steps have been requested. If this is false,
165 /// we use the StepsLeft value above.
166 const bool InfiniteSteps = false;
167 /// ID identifying this evaluation.
168 const unsigned EvalID;
169
170 /// Things needed to do speculative execution.
172#ifndef NDEBUG
173 unsigned SpeculationDepth = 0;
174#endif
175 unsigned DiagIgnoreDepth = 0;
176 std::optional<bool> ConstantContextOverride;
177
179 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
181
182 /// List of blocks we're currently running either constructors or destructors
183 /// for.
185};
186
188public:
190 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
191 // We only override this if the new value is true.
192 Enabled = Value;
193 if (Enabled)
194 Ctx.ConstantContextOverride = Value;
195 }
197 if (Enabled)
198 Ctx.ConstantContextOverride = OldCC;
199 }
200
201private:
202 bool Enabled;
203 InterpState &Ctx;
204 std::optional<bool> OldCC;
205};
206
207} // namespace interp
208} // namespace clang
209
210#endif
#define V(N, I)
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This represents one expression.
Definition Expr.h:112
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:924
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
Pointer into the code segment.
Definition Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
Descriptor for a dead block.
Manages dynamic memory allocations done during bytecode interpretation.
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
bool singleWord() const
Definition Floating.h:107
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:61
Frame storing local variables.
Definition InterpFrame.h:27
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpStateCCOverride(InterpState &Ctx, bool Value)
Interpreter context.
Definition InterpState.h:36
SmallVectorImpl< PartialDiagnosticAt > * PrevDiags
Things needed to do speculative execution.
unsigned getCallStackDepth() override
Definition InterpState.h:54
InterpFrame BottomFrame
Bottom function frame.
Context & getContext() const
Definition InterpState.h:72
bool initializingBlock(const Block *B) const
DynamicAllocator & getAllocator()
Definition InterpState.h:76
Context & Ctx
Interpreter Context.
void * allocate(size_t Size, unsigned Align=8) const
Definition InterpState.h:91
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
Floating allocFloat(const llvm::fltSemantics &Sem)
const unsigned EvalID
ID identifying this evaluation.
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition InterpState.h:64
InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
InterpState(const InterpState &)=delete
bool stepsLeft() const override
Definition InterpState.h:57
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
SourceLocation EvalLocation
Source location of the evaluating expression.
unsigned StepsLeft
Steps left during evaluation.
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
const CXXRecordDecl ** allocMemberPointerPath(unsigned Length)
const Frame * getCurrentFrame() override
std::optional< bool > ConstantContextOverride
const bool InfiniteSteps
Whether infinite evaluation steps have been requested.
InterpState & operator=(const InterpState &)=delete
friend class InterpStateCCOverride
T * allocate(size_t Num=1) const
Definition InterpState.h:96
void deallocate(Block *B)
Deallocates a pointer.
llvm::SmallVector< PtrView > InitializingPtrs
List of blocks we're currently running either constructors or destructors for.
T allocAP(unsigned BitWidth)
void setEvalLocation(SourceLocation SL)
Definition InterpState.h:74
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
The program contains and links the bytecode for all functions.
Definition Program.h:36
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
Interface for classes which map locations to sources.
Definition Source.h:101
Expr::EvalStatus & getEvalStatus() const
Definition State.h:91
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:83
The JSON file list parser is used to communicate input to InstallAPI.
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition Expr.h:636