clang 23.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 "EvaluationResult.h"
17#include "InterpState.h"
18#include "PrimType.h"
19#include "Record.h"
20#include "Source.h"
21
22namespace clang {
23namespace interp {
24class Context;
25class Function;
26class InterpStack;
27class Program;
28enum Opcode : uint32_t;
29
30/// An emitter which evaluates opcodes as they are emitted.
31class EvalEmitter : public SourceMapper {
32public:
37 llvm::function_ref<bool(InterpState &S, CodePtr OpPC, const Pointer &)>;
38
40 bool ConvertResultToRValue = false,
41 bool DestroyToplevelScope = false);
43 bool CheckFullyInitialized);
45 /// Interpret the given Expr to a Pointer.
48 /// Interpret the given expression as if it was in the body of the given
49 /// function, i.e. the parameters of the function are available for use.
50 bool interpretCall(const FunctionDecl *FD, const Expr *E);
51
52 std::optional<bool> interpretWithSubstitutions(const FunctionDecl *Callee,
54 const Expr *This,
55 const Expr *Condition);
56
57 /// Clean up all resources.
58 void cleanup();
59
60protected:
61 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
62
63 virtual ~EvalEmitter();
64
65 /// Define a label.
66 void emitLabel(LabelTy Label);
67 /// Create a label.
69
70 /// Methods implemented by the compiler.
71 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
72 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
73 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
74 bool ConstantContext) = 0;
75 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
76 virtual bool visitWithSubstitutions(const FunctionDecl *Callee,
78 const Expr *This,
79 const Expr *Condition) = 0;
80 virtual bool visitFunc(const FunctionDecl *F) = 0;
81 virtual bool visit(const Expr *E) = 0;
82 virtual bool emitBool(bool V, const Expr *E) = 0;
83
84 /// Emits jumps.
85 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
86 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
87 bool jump(const LabelTy &Label, SourceInfo SI);
88 bool fallthrough(const LabelTy &Label);
89 /// Speculative execution.
90 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
91
92 /// Since expressions can only jump forward, predicated execution is
93 /// used to deal with if-else statements.
94 bool isActive() const { return CurrentLabel == ActiveLabel; }
96 return S.checkingForUndefinedBehavior();
97 }
98
99 /// Callback for registering a local.
101
102 /// Returns the source location of the current opcode.
103 SourceInfo getSource(const Function *F, CodePtr PC) const override {
104 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
105 }
106
107 /// Parameter indices.
108 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
109 /// Local descriptors.
111 std::optional<SourceInfo> LocOverride = std::nullopt;
112
113private:
114 /// Current compilation context.
115 Context &Ctx;
116 /// Current program.
117 Program &P;
118 /// Callee evaluation state.
119 InterpState S;
120 /// Location to write the result to.
121 EvaluationResult EvalResult;
122 /// Whether the result should be converted to an RValue.
123 bool ConvertResultToRValue = false;
124 /// Whether we should check if the result has been fully
125 /// initialized.
126 bool CheckFullyInitialized = false;
127 /// Callback to call when using interpretAsPointer.
128 std::optional<PtrCallback> PtrCB;
129
130 /// Temporaries which require storage.
132
133 Block *getLocal(unsigned Index) const {
134 assert(Index < Locals.size());
135 return reinterpret_cast<Block *>(Locals[Index].get());
136 }
137
138 void updateGlobalTemporaries();
139
140 // The emitter always tracks the current instruction and sets OpPC to a token
141 // value which is mapped to the location of the opcode being evaluated.
142 CodePtr OpPC;
143 /// Location of the current instruction.
144 SourceInfo CurrentSource;
145
146 /// Next label ID to generate - first label is 1.
147 LabelTy NextLabel = 1;
148 /// Label being executed - 0 is the entry label.
149 LabelTy CurrentLabel = 0;
150 /// Active block which should be executed.
151 LabelTy ActiveLabel = 0;
152
153protected:
154#define GET_EVAL_PROTO
155#include "Opcodes.inc"
156#undef GET_EVAL_PROTO
157};
158
159} // namespace interp
160} // namespace clang
161
162#endif
#define V(N, I)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2027
Represents a variable declaration or definition.
Definition Decl.h:932
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
std::optional< SourceInfo > LocOverride
llvm::DenseMap< const ParmVarDecl *, FuncParam > Params
Parameter indices.
virtual bool visitFunc(const FunctionDecl *F)=0
EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init, bool CheckFullyInitialized)
EvaluationResult interpretDestructor(const VarDecl *VD, const APValue &Value)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
bool jump(const LabelTy &Label, SourceInfo SI)
bool checkingForUndefinedBehavior() const
Definition EvalEmitter.h:95
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
std::optional< bool > interpretWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)
virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value)=0
bool jumpFalse(const LabelTy &Label, SourceInfo SI)
Local createLocal(Descriptor *D)
Callback for registering a local.
bool interpretCall(const FunctionDecl *FD, const Expr *E)
Interpret the given expression as if it was in the body of the given function, i.e.
void emitLabel(LabelTy Label)
Define a label.
bool isActive() const
Since expressions can only jump forward, predicated execution is used to deal with if-else statements...
Definition EvalEmitter.h:94
virtual bool visitWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)=0
EvaluationResult interpretAsLValuePointer(const Expr *E, PtrCallback PtrCB)
virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope)=0
Methods implemented by the compiler.
bool fallthrough(const LabelTy &Label)
virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext)=0
void cleanup()
Clean up all resources.
LabelTy getLabel()
Create a label.
EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB)
Interpret the given Expr to a Pointer.
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
virtual bool emitBool(bool V, const Expr *E)=0
bool jumpTrue(const LabelTy &Label, SourceInfo SI)
Emits jumps.
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
llvm::function_ref< bool(InterpState &S, CodePtr OpPC, const Pointer &)> PtrCallback
Definition EvalEmitter.h:36
Defines the result of an evaluation.
Bytecode function.
Definition Function.h:99
bool hasBody() const
Checks if the function already has a body attached.
Definition Function.h:233
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:61
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:394
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
Interface for the VM to interact with the AST walker's context.
Definition State.h:81
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3172
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2382
The JSON file list parser is used to communicate input to InstallAPI.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
Information about a local's storage.
Definition Function.h:39