clang 18.0.0git
EvalEmitter.h
Go to the documentation of this file.
1//===--- EvalEmitter.h - Instruction emitter for the 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// Defines the instruction emitters.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H
14#define LLVM_CLANG_AST_INTERP_EVALEMITTER_H
15
16#include "InterpState.h"
17#include "PrimType.h"
18#include "Source.h"
19#include "llvm/Support/Error.h"
20
21namespace clang {
22namespace interp {
23class Context;
24class Function;
25class InterpStack;
26class Program;
27enum Opcode : uint32_t;
28
29/// An emitter which evaluates opcodes as they are emitted.
30class EvalEmitter : public SourceMapper {
31public:
32 using LabelTy = uint32_t;
35
38
39protected:
41 APValue &Result);
42
43 virtual ~EvalEmitter();
44
45 /// Define a label.
47 /// Create a label.
49
50 /// Methods implemented by the compiler.
51 virtual bool visitExpr(const Expr *E) = 0;
52 virtual bool visitDecl(const VarDecl *VD) = 0;
53
54 bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
55 bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
56 bool bail(const SourceLocation &Loc);
57
58 /// Emits jumps.
59 bool jumpTrue(const LabelTy &Label);
60 bool jumpFalse(const LabelTy &Label);
61 bool jump(const LabelTy &Label);
62 bool fallthrough(const LabelTy &Label);
63
64 /// Callback for registering a local.
66
67 /// Returns the source location of the current opcode.
68 SourceInfo getSource(const Function *F, CodePtr PC) const override {
69 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
70 }
71
72 /// Parameter indices.
73 llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
74 /// Lambda captures.
75 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
76 /// Offset of the This parameter in a lambda record.
77 unsigned LambdaThisCapture = 0;
78 /// Local descriptors.
80
81private:
82 /// Current compilation context.
83 Context &Ctx;
84 /// Current program.
85 Program &P;
86 /// Callee evaluation state.
88 /// Location to write the result to.
89 APValue &Result;
90
91 /// Temporaries which require storage.
92 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
93
94 Block *getLocal(unsigned Index) const {
95 auto It = Locals.find(Index);
96 assert(It != Locals.end() && "Missing local variable");
97 return reinterpret_cast<Block *>(It->second.get());
98 }
99
100 // The emitter always tracks the current instruction and sets OpPC to a token
101 // value which is mapped to the location of the opcode being evaluated.
102 CodePtr OpPC;
103 /// Location of a failure.
104 std::optional<SourceLocation> BailLocation;
105 /// Location of the current instruction.
106 SourceInfo CurrentSource;
107
108 /// Next label ID to generate - first label is 1.
109 LabelTy NextLabel = 1;
110 /// Label being executed - 0 is the entry label.
111 LabelTy CurrentLabel = 0;
112 /// Active block which should be executed.
113 LabelTy ActiveLabel = 0;
114
115 /// Since expressions can only jump forward, predicated execution is
116 /// used to deal with if-else statements.
117 bool isActive() const { return CurrentLabel == ActiveLabel; }
118
119protected:
120#define GET_EVAL_PROTO
121#include "Opcodes.inc"
122#undef GET_EVAL_PROTO
123};
124
125} // namespace interp
126} // namespace clang
127
128#endif
NodeId Parent
Definition: ASTDiff.cpp:191
std::string Label
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:436
This represents one expression.
Definition: Expr.h:110
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:84
Represents a variable declaration or definition.
Definition: Decl.h:916
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
An emitter which evaluates opcodes as they are emitted.
Definition: EvalEmitter.h:30
bool jump(const LabelTy &Label)
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
bool jumpFalse(const LabelTy &Label)
Definition: EvalEmitter.cpp:94
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:58
bool bail(const Stmt *S)
Definition: EvalEmitter.h:54
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:52
bool fallthrough(const LabelTy &Label)
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:56
llvm::Expected< bool > interpretDecl(const VarDecl *VD)
Definition: EvalEmitter.cpp:44
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
Definition: EvalEmitter.h:75
bool bail(const Decl *D)
Definition: EvalEmitter.h:55
llvm::Expected< bool > interpretExpr(const Expr *E)
Definition: EvalEmitter.cpp:36
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition: EvalEmitter.h:73
unsigned LambdaThisCapture
Offset of the This parameter in a lambda record.
Definition: EvalEmitter.h:77
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition: EvalEmitter.h:68
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:79
virtual bool visitDecl(const VarDecl *VD)=0
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: EvalEmitter.cpp:86
Bytecode function.
Definition: Function.h:76
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:171
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:36
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:26
Interpreter context.
Definition: InterpState.h:35
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
Interface for classes which map locations to sources.
Definition: Source.h:94
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Describes a memory block created by an allocation site.
Definition: Descriptor.h:79
Information about a local's storage.
Definition: Function.h:37