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 FrameAllocator;
28class Program;
29enum Opcode : uint32_t;
30
31/// An emitter which evaluates opcodes as they are emitted.
32class EvalEmitter : public SourceMapper {
33public:
38 llvm::function_ref<bool(InterpState &S, CodePtr OpPC, const Pointer &)>;
39
41 bool ConvertResultToRValue = false,
42 bool DestroyToplevelScope = false);
44 bool CheckFullyInitialized);
46 /// Interpret the given Expr to a Pointer.
49 /// Interpret the given expression as if it was in the body of the given
50 /// function, i.e. the parameters of the function are available for use.
51 bool interpretCall(const FunctionDecl *FD, const Expr *E);
52
53 std::optional<bool> interpretWithSubstitutions(const FunctionDecl *Callee,
55 const Expr *This,
56 const Expr *Condition);
57
58 /// Clean up all resources.
59 void cleanup();
60
61 /// Returns the source location of the current opcode.
62 SourceInfo getSource(CodePtr PC) const override { return CurrentSource; }
63
64protected:
65 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk,
66 FrameAllocator &FrameAlloc);
67
69 InterpStack &Stk, FrameAllocator &FrameAlloc);
70
71 /// Define a label.
72 void emitLabel(LabelTy Label);
73 /// Create a label.
75
76 /// Methods implemented by the compiler.
77 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
78 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
79 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
80 bool ConstantContext) = 0;
81 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
82 virtual bool visitWithSubstitutions(const FunctionDecl *Callee,
84 const Expr *This,
85 const Expr *Condition) = 0;
86 virtual bool visitFunc(const FunctionDecl *F) = 0;
87 virtual bool visit(const Expr *E) = 0;
88 virtual bool emitBool(bool V, const Expr *E) = 0;
89
90 /// Emits jumps.
91 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
92 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
93 bool jump(const LabelTy &Label, SourceInfo SI);
94 bool fallthrough(const LabelTy &Label);
95 /// Speculative execution.
96 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
97
98 /// Since expressions can only jump forward, predicated execution is
99 /// used to deal with if-else statements.
100 bool isActive() const { return CurrentLabel == ActiveLabel; }
102 return S.checkingForUndefinedBehavior();
103 }
104
105 /// Callback for registering a local.
106 Local createLocal(const Descriptor *D);
107
108 /// Parameter indices.
109 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
110 /// Local descriptors.
112 std::optional<SourceInfo> LocOverride = std::nullopt;
113
114private:
115 /// Current compilation context.
116 Context &Ctx;
117 /// Current program.
118 Program &P;
119 /// Callee evaluation state.
120 InterpState S;
121 /// Location to write the result to.
122 EvaluationResult EvalResult;
123 /// Whether the result should be converted to an RValue.
124 bool ConvertResultToRValue = false;
125 /// Whether we should check if the result has been fully
126 /// initialized.
127 bool CheckFullyInitialized = false;
128 /// Callback to call when using interpretAsPointer.
129 std::optional<PtrCallback> PtrCB;
130
131 /// Temporaries which require storage.
133
134 Block *getLocal(unsigned Index) const {
135 assert(Index < Locals.size());
136 return reinterpret_cast<Block *>(Locals[Index]);
137 }
138
139 void updateGlobalTemporaries();
140
141 /// Location of the current instruction.
142 SourceInfo CurrentSource;
143
144 /// Next label ID to generate - first label is 1.
145 LabelTy NextLabel = 1;
146 /// Label being executed - 0 is the entry label.
147 LabelTy CurrentLabel = 0;
148 /// Active block which should be executed.
149 LabelTy ActiveLabel = 0;
150
151protected:
152#define GET_EVAL_PROTO
153#include "Opcodes.inc"
154#undef GET_EVAL_PROTO
155};
156
157} // namespace interp
158} // namespace clang
159
160#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:2987
This represents one expression.
Definition Expr.h:113
Represents a function declaration or definition.
Definition Decl.h:2059
Represents a variable declaration or definition.
Definition Decl.h:933
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:43
Pointer into the code segment.
Definition Source.h:31
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:48
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk, FrameAllocator &FrameAlloc)
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
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
Local createLocal(const Descriptor *D)
Callback for registering a local.
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)
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...
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:62
llvm::SmallVector< SmallVector< Local, 2 >, 1 > Descriptors
Local descriptors.
virtual bool emitBool(bool V, const Expr *E)=0
bool jumpTrue(const LabelTy &Label, SourceInfo SI)
Emits jumps.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
llvm::function_ref< bool(InterpState &S, CodePtr OpPC, const Pointer &)> PtrCallback
Definition EvalEmitter.h:37
Defines the result of an evaluation.
Allocator for function frames.
Bytecode function.
Definition Function.h:98
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
Interpreter context.
Definition InterpState.h:46
A pointer to a memory block, live or dead.
Definition Pointer.h:536
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:77
Interface for classes which map locations to sources.
Definition Source.h:138
Interface for the VM to interact with the AST walker's context.
Definition State.h:79
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3222
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2432
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...
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:622
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
Information about a local's storage.
Definition Function.h:38