clang 24.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
60 /// Returns the source location of the current opcode.
61 SourceInfo getSource(CodePtr PC) const override { return CurrentSource; }
62
63protected:
64 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
65
66 virtual ~EvalEmitter();
67
68 /// Define a label.
69 void emitLabel(LabelTy Label);
70 /// Create a label.
72
73 /// Methods implemented by the compiler.
74 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
75 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
76 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
77 bool ConstantContext) = 0;
78 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
79 virtual bool visitWithSubstitutions(const FunctionDecl *Callee,
81 const Expr *This,
82 const Expr *Condition) = 0;
83 virtual bool visitFunc(const FunctionDecl *F) = 0;
84 virtual bool visit(const Expr *E) = 0;
85 virtual bool emitBool(bool V, const Expr *E) = 0;
86
87 /// Emits jumps.
88 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
89 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
90 bool jump(const LabelTy &Label, SourceInfo SI);
91 bool fallthrough(const LabelTy &Label);
92 /// Speculative execution.
93 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
94
95 /// Since expressions can only jump forward, predicated execution is
96 /// used to deal with if-else statements.
97 bool isActive() const { return CurrentLabel == ActiveLabel; }
99 return S.checkingForUndefinedBehavior();
100 }
101
102 /// Callback for registering a local.
104
105 /// Parameter indices.
106 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
107 /// Local descriptors.
109 std::optional<SourceInfo> LocOverride = std::nullopt;
110
111private:
112 /// Current compilation context.
113 Context &Ctx;
114 /// Current program.
115 Program &P;
116 /// Callee evaluation state.
117 InterpState S;
118 /// Location to write the result to.
119 EvaluationResult EvalResult;
120 /// Whether the result should be converted to an RValue.
121 bool ConvertResultToRValue = false;
122 /// Whether we should check if the result has been fully
123 /// initialized.
124 bool CheckFullyInitialized = false;
125 /// Callback to call when using interpretAsPointer.
126 std::optional<PtrCallback> PtrCB;
127
128 /// Temporaries which require storage.
130
131 Block *getLocal(unsigned Index) const {
132 assert(Index < Locals.size());
133 return reinterpret_cast<Block *>(Locals[Index].get());
134 }
135
136 void updateGlobalTemporaries();
137
138 /// Location of the current instruction.
139 SourceInfo CurrentSource;
140
141 /// Next label ID to generate - first label is 1.
142 LabelTy NextLabel = 1;
143 /// Label being executed - 0 is the entry label.
144 LabelTy CurrentLabel = 0;
145 /// Active block which should be executed.
146 LabelTy ActiveLabel = 0;
147
148protected:
149#define GET_EVAL_PROTO
150#include "Opcodes.inc"
151#undef GET_EVAL_PROTO
152};
153
154} // namespace interp
155} // namespace clang
156
157#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:2029
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:98
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:97
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.
SourceInfo getSource(CodePtr PC) const override
Returns the source location of the current opcode.
Definition EvalEmitter.h:61
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.
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
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:405
The program contains and links the bytecode for all functions.
Definition Program.h:37
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
Interface for classes which map locations to sources.
Definition Source.h:103
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:3180
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2412
Top level wrappers for InstallAPI frontend operations.
__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:122
Information about a local's storage.
Definition Function.h:39