clang 22.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#include "clang/AST/APValue.h"
25#include "clang/AST/Expr.h"
27
28namespace clang {
29namespace interp {
30class Context;
31class Function;
32class InterpStack;
33class InterpFrame;
34class SourceMapper;
35
37 const Expr *Call = nullptr;
39 explicit operator bool() { return Call; }
40};
41
42/// Interpreter context.
43class InterpState final : public State, public SourceMapper {
44public:
46 SourceMapper *M = nullptr);
48 const Function *Func);
49
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 Frame *getCurrentFrame() override;
61 unsigned getCallStackDepth() override {
62 return Current ? (Current->getDepth() + 1) : 1;
63 }
64 const Frame *getBottomFrame() const override { return &BottomFrame; }
65
66 // Access objects from the walker context.
67 Expr::EvalStatus &getEvalStatus() const override {
68 return Parent.getEvalStatus();
69 }
70 ASTContext &getASTContext() const override { return Ctx.getASTContext(); }
71 const LangOptions &getLangOpts() const {
72 return Ctx.getASTContext().getLangOpts();
73 }
74
75 // Forward status checks and updates to the walker.
76 bool keepEvaluatingAfterFailure() const override {
77 return Parent.keepEvaluatingAfterFailure();
78 }
79 bool keepEvaluatingAfterSideEffect() const override {
80 return Parent.keepEvaluatingAfterSideEffect();
81 }
82 bool noteUndefinedBehavior() override {
83 return Parent.noteUndefinedBehavior();
84 }
85 bool inConstantContext() const;
86 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
87 void setActiveDiagnostic(bool Flag) override {
88 Parent.setActiveDiagnostic(Flag);
89 }
90 void setFoldFailureDiagnostic(bool Flag) override {
91 Parent.setFoldFailureDiagnostic(Flag);
92 }
93 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
94 bool noteSideEffect() override { return Parent.noteSideEffect(); }
95
96 /// Deallocates a pointer.
97 void deallocate(Block *B);
98
99 /// Delegates source mapping to the mapper.
100 SourceInfo getSource(const Function *F, CodePtr PC) const override {
101 if (M)
102 return M->getSource(F, PC);
103
104 assert(F && "Function cannot be null");
105 return F->getSource(PC);
106 }
107
108 Context &getContext() const { return Ctx; }
109
111
113 if (!Alloc) {
114 Alloc = std::make_unique<DynamicAllocator>();
115 }
116
117 return *Alloc.get();
118 }
119
120 /// Diagnose any dynamic allocations that haven't been freed yet.
121 /// Will return \c false if there were any allocations to diagnose,
122 /// \c true otherwise.
124
125 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
126
127 void *allocate(size_t Size, unsigned Align = 8) const {
128 if (!Allocator)
129 Allocator.emplace();
130 return Allocator->Allocate(Size, Align);
131 }
132 template <typename T> T *allocate(size_t Num = 1) const {
133 return static_cast<T *>(allocate(Num * sizeof(T), alignof(T)));
134 }
135
136 template <typename T> T allocAP(unsigned BitWidth) {
137 unsigned NumWords = APInt::getNumWords(BitWidth);
138 if (NumWords == 1)
139 return T(BitWidth);
140 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
141 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
142 return T(Mem, BitWidth);
143 }
144
145 Floating allocFloat(const llvm::fltSemantics &Sem) {
146 if (Floating::singleWord(Sem))
147 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
148
149 unsigned NumWords =
150 APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem));
151 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
152 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
153 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
154 }
155
156private:
157 friend class EvaluationResult;
159 /// AST Walker state.
160 State &Parent;
161 /// Dead block chain.
162 DeadBlock *DeadBlocks = nullptr;
163 /// Reference to the offset-source mapping.
164 SourceMapper *M;
165 /// Allocator used for dynamic allocations performed via the program.
166 std::unique_ptr<DynamicAllocator> Alloc;
167
168public:
169 /// Reference to the module containing all bytecode.
171 /// Temporary stack.
173 /// Interpreter Context.
175 /// Bottom function frame.
177 /// The current frame.
179 /// Source location of the evaluating expression
181 /// Declaration we're initializing/evaluting, if any.
182 const VarDecl *EvaluatingDecl = nullptr;
183 /// Things needed to do speculative execution.
185 unsigned SpeculationDepth = 0;
186 std::optional<bool> ConstantContextOverride;
187
189 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
191
192 /// List of blocks we're currently running either constructors or destructors
193 /// for.
195
196 mutable std::optional<llvm::BumpPtrAllocator> Allocator;
197};
198
200public:
202 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
203 // We only override this if the new value is true.
204 Enabled = Value;
205 if (Enabled)
206 Ctx.ConstantContextOverride = Value;
207 }
209 if (Enabled)
210 Ctx.ConstantContextOverride = OldCC;
211 }
212
213private:
214 bool Enabled;
215 InterpState &Ctx;
216 std::optional<bool> OldCC;
217};
218
219} // namespace interp
220} // namespace clang
221
222#endif
Implements a partial diagnostic which may not be emitted.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:188
This represents one expression.
Definition Expr.h:112
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:925
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:41
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:106
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:86
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:63
Frame storing local variables.
Definition InterpFrame.h:26
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpStateCCOverride(InterpState &Ctx, bool Value)
Interpreter context.
Definition InterpState.h:43
const Frame * getBottomFrame() const override
Definition InterpState.h:64
SmallVectorImpl< PartialDiagnosticAt > * PrevDiags
Things needed to do speculative execution.
std::optional< llvm::BumpPtrAllocator > Allocator
unsigned getCallStackDepth() override
Definition InterpState.h:61
Expr::EvalStatus & getEvalStatus() const override
Definition InterpState.h:67
InterpFrame BottomFrame
Bottom function frame.
Context & getContext() const
bool keepEvaluatingAfterFailure() const override
Definition InterpState.h:76
bool noteUndefinedBehavior() override
Definition InterpState.h:82
DynamicAllocator & getAllocator()
Context & Ctx
Interpreter Context.
void * allocate(size_t Size, unsigned Align=8) const
Floating allocFloat(const llvm::fltSemantics &Sem)
llvm::SmallVector< const Block * > InitializingBlocks
List of blocks we're currently running either constructors or destructors for.
ASTContext & getASTContext() const override
Definition InterpState.h:70
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Frame * getCurrentFrame() override
InterpState(const InterpState &)=delete
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.
bool keepEvaluatingAfterSideEffect() const override
Definition InterpState.h:79
bool noteSideEffect() override
Definition InterpState.h:94
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
bool hasActiveDiagnostic() override
Definition InterpState.h:86
std::optional< bool > ConstantContextOverride
void setActiveDiagnostic(bool Flag) override
Definition InterpState.h:87
void setFoldFailureDiagnostic(bool Flag) override
Definition InterpState.h:90
InterpState & operator=(const InterpState &)=delete
friend class InterpStateCCOverride
T * allocate(size_t Num=1) const
void deallocate(Block *B)
Deallocates a pointer.
T allocAP(unsigned BitWidth)
const LangOptions & getLangOpts() const
Definition InterpState.h:71
void setEvalLocation(SourceLocation SL)
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
bool hasPriorDiagnostic() override
Definition InterpState.h:93
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:73
Interface for classes which map locations to sources.
Definition Source.h:99
#define bool
Definition gpuintrin.h:32
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:609
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:633