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 const Frame *getBottomFrame() const override { return &BottomFrame; }
58
59 bool stepsLeft() const override { return true; }
60 bool inConstantContext() const;
61
62 /// Deallocates a pointer.
63 void deallocate(Block *B);
64
65 /// Delegates source mapping to the mapper.
66 SourceInfo getSource(const Function *F, CodePtr PC) const override {
67 if (M)
68 return M->getSource(F, PC);
69
70 assert(F && "Function cannot be null");
71 return F->getSource(PC);
72 }
73
74 Context &getContext() const { return Ctx; }
75
77
79 if (!Alloc) {
80 Alloc = std::make_unique<DynamicAllocator>();
81 }
82
83 return *Alloc;
84 }
85
86 /// Diagnose any dynamic allocations that haven't been freed yet.
87 /// Will return \c false if there were any allocations to diagnose,
88 /// \c true otherwise.
90
91 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
92
93 void *allocate(size_t Size, unsigned Align = 8) const {
94 if (!Allocator)
95 Allocator.emplace();
96 return Allocator->Allocate(Size, Align);
97 }
98 template <typename T> T *allocate(size_t Num = 1) const {
99 return static_cast<T *>(allocate(Num * sizeof(T), alignof(T)));
100 }
101
102 template <typename T> T allocAP(unsigned BitWidth) {
103 unsigned NumWords = APInt::getNumWords(BitWidth);
104 if (NumWords == 1)
105 return T(BitWidth);
106 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
107 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
108 return T(Mem, BitWidth);
109 }
110
111 Floating allocFloat(const llvm::fltSemantics &Sem) {
112 if (Floating::singleWord(Sem))
113 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
114
115 unsigned NumWords =
116 APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem));
117 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
118 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
119 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
120 }
121 const CXXRecordDecl **allocMemberPointerPath(unsigned Length) {
122 return reinterpret_cast<const CXXRecordDecl **>(
123 this->allocate(Length * sizeof(CXXRecordDecl *)));
124 }
125
126 /// Note that a step has been executed. If there are no more steps remaining,
127 /// diagnoses and returns \c false.
128 bool noteStep(CodePtr OpPC);
129
130private:
131 friend class EvaluationResult;
133 /// Dead block chain.
134 DeadBlock *DeadBlocks = nullptr;
135 /// Reference to the offset-source mapping.
136 SourceMapper *M;
137 /// Allocator used for dynamic allocations performed via the program.
138 std::unique_ptr<DynamicAllocator> Alloc;
139 /// Allocator for everything else, e.g. floating-point values.
140 mutable std::optional<llvm::BumpPtrAllocator> Allocator;
141
142public:
143 /// Reference to the module containing all bytecode.
145 /// Temporary stack.
147 /// Interpreter Context.
149 /// Bottom function frame.
151 /// The current frame.
153 /// Source location of the evaluating expression
155 /// Declaration we're initializing/evaluting, if any.
156 const VarDecl *EvaluatingDecl = nullptr;
157 /// Steps left during evaluation.
158 unsigned StepsLeft = 1;
159 /// Whether infinite evaluation steps have been requested. If this is false,
160 /// we use the StepsLeft value above.
161 const bool InfiniteSteps = false;
162 /// ID identifying this evaluation.
163 const unsigned EvalID;
164
165 /// Things needed to do speculative execution.
167 unsigned SpeculationDepth = 0;
168 std::optional<bool> ConstantContextOverride;
169
171 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
173
174 /// List of blocks we're currently running either constructors or destructors
175 /// for.
177};
178
180public:
182 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
183 // We only override this if the new value is true.
184 Enabled = Value;
185 if (Enabled)
186 Ctx.ConstantContextOverride = Value;
187 }
189 if (Enabled)
190 Ctx.ConstantContextOverride = OldCC;
191 }
192
193private:
194 bool Enabled;
195 InterpState &Ctx;
196 std::optional<bool> OldCC;
197};
198
199} // namespace interp
200} // namespace clang
201
202#endif
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:926
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:42
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:65
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
const Frame * getBottomFrame() const override
Definition InterpState.h:57
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:74
DynamicAllocator & getAllocator()
Definition InterpState.h:78
Context & Ctx
Interpreter Context.
void * allocate(size_t Size, unsigned Align=8) const
Definition InterpState.h:93
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
Floating allocFloat(const llvm::fltSemantics &Sem)
const unsigned EvalID
ID identifying this evaluation.
llvm::SmallVector< const Block * > InitializingBlocks
List of blocks we're currently running either constructors or destructors for.
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition InterpState.h:66
InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
InterpState(const InterpState &)=delete
bool stepsLeft() const override
Definition InterpState.h:59
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:98
void deallocate(Block *B)
Deallocates a pointer.
T allocAP(unsigned BitWidth)
void setEvalLocation(SourceLocation SL)
Definition InterpState.h:76
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:92
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:83
#define bool
Definition gpuintrin.h:32
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