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 Parent.getASTContext(); }
71
72 // Forward status checks and updates to the walker.
73 bool keepEvaluatingAfterFailure() const override {
74 return Parent.keepEvaluatingAfterFailure();
75 }
76 bool keepEvaluatingAfterSideEffect() const override {
77 return Parent.keepEvaluatingAfterSideEffect();
78 }
79 bool noteUndefinedBehavior() override {
80 return Parent.noteUndefinedBehavior();
81 }
82 bool inConstantContext() const;
83 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
84 void setActiveDiagnostic(bool Flag) override {
85 Parent.setActiveDiagnostic(Flag);
86 }
87 void setFoldFailureDiagnostic(bool Flag) override {
88 Parent.setFoldFailureDiagnostic(Flag);
89 }
90 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
91 bool noteSideEffect() override { return Parent.noteSideEffect(); }
92
93 /// Deallocates a pointer.
94 void deallocate(Block *B);
95
96 /// Delegates source mapping to the mapper.
97 SourceInfo getSource(const Function *F, CodePtr PC) const override {
98 if (M)
99 return M->getSource(F, PC);
100
101 assert(F && "Function cannot be null");
102 return F->getSource(PC);
103 }
104
105 Context &getContext() const { return Ctx; }
106
108
110 if (!Alloc) {
111 Alloc = std::make_unique<DynamicAllocator>();
112 }
113
114 return *Alloc.get();
115 }
116
117 /// Diagnose any dynamic allocations that haven't been freed yet.
118 /// Will return \c false if there were any allocations to diagnose,
119 /// \c true otherwise.
121
122 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
123
124 void *allocate(size_t Size, unsigned Align = 8) const {
125 return Allocator.Allocate(Size, Align);
126 }
127 template <typename T> T *allocate(size_t Num = 1) const {
128 return static_cast<T *>(allocate(Num * sizeof(T), alignof(T)));
129 }
130
131 template <typename T> T allocAP(unsigned BitWidth) {
132 unsigned NumWords = APInt::getNumWords(BitWidth);
133 if (NumWords == 1)
134 return T(BitWidth);
135 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
136 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
137 return T(Mem, BitWidth);
138 }
139
140 Floating allocFloat(const llvm::fltSemantics &Sem) {
141 if (Floating::singleWord(Sem))
142 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
143
144 unsigned NumWords =
145 APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem));
146 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
147 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
148 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
149 }
150
151private:
152 friend class EvaluationResult;
154 /// AST Walker state.
155 State &Parent;
156 /// Dead block chain.
157 DeadBlock *DeadBlocks = nullptr;
158 /// Reference to the offset-source mapping.
159 SourceMapper *M;
160 /// Allocator used for dynamic allocations performed via the program.
161 std::unique_ptr<DynamicAllocator> Alloc;
162
163public:
164 /// Reference to the module containing all bytecode.
166 /// Temporary stack.
168 /// Interpreter Context.
170 /// Bottom function frame.
172 /// The current frame.
174 /// Source location of the evaluating expression
176 /// Declaration we're initializing/evaluting, if any.
177 const VarDecl *EvaluatingDecl = nullptr;
178 /// Things needed to do speculative execution.
180 unsigned SpeculationDepth = 0;
181 std::optional<bool> ConstantContextOverride;
182
184 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
186
187 /// List of blocks we're currently running either constructors or destructors
188 /// for.
190
191 mutable llvm::BumpPtrAllocator Allocator;
192};
193
195public:
197 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
198 // We only override this if the new value is true.
199 Enabled = Value;
200 if (Enabled)
201 Ctx.ConstantContextOverride = Value;
202 }
204 if (Enabled)
205 Ctx.ConstantContextOverride = OldCC;
206 }
207
208private:
209 bool Enabled;
210 InterpState &Ctx;
211 std::optional<bool> OldCC;
212};
213
214} // namespace interp
215} // namespace clang
216
217#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
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.
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:73
bool noteUndefinedBehavior() override
Definition InterpState.h:79
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.
Definition InterpState.h:97
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:76
bool noteSideEffect() override
Definition InterpState.h:91
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:83
std::optional< bool > ConstantContextOverride
void setActiveDiagnostic(bool Flag) override
Definition InterpState.h:84
void setFoldFailureDiagnostic(bool Flag) override
Definition InterpState.h:87
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)
void setEvalLocation(SourceLocation SL)
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
bool hasPriorDiagnostic() override
Definition InterpState.h:90
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