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:
33 using LabelTy = uint32_t;
36 using PtrCallback = llvm::function_ref<bool(const Pointer &)>;
37
39 bool ConvertResultToRValue = false,
40 bool DestroyToplevelScope = false);
42 bool CheckFullyInitialized);
44 /// Interpret the given Expr to a Pointer.
47 /// Interpret the given expression as if it was in the body of the given
48 /// function, i.e. the parameters of the function are available for use.
49 bool interpretCall(const FunctionDecl *FD, const Expr *E);
50
51 /// Clean up all resources.
52 void cleanup();
53
54protected:
55 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
56
57 virtual ~EvalEmitter();
58
59 /// Define a label.
60 void emitLabel(LabelTy Label);
61 /// Create a label.
63
64 /// Methods implemented by the compiler.
65 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
66 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
67 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
68 bool ConstantContext) = 0;
69 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
70 virtual bool visitFunc(const FunctionDecl *F) = 0;
71 virtual bool visit(const Expr *E) = 0;
72 virtual bool emitBool(bool V, const Expr *E) = 0;
73
74 /// Emits jumps.
75 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
76 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
77 bool jump(const LabelTy &Label, SourceInfo SI);
78 bool fallthrough(const LabelTy &Label);
79 /// Speculative execution.
80 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
81
82 /// Since expressions can only jump forward, predicated execution is
83 /// used to deal with if-else statements.
84 bool isActive() const { return CurrentLabel == ActiveLabel; }
86 return S.checkingForUndefinedBehavior();
87 }
88
89 /// Callback for registering a local.
91
92 /// Returns the source location of the current opcode.
93 SourceInfo getSource(const Function *F, CodePtr PC) const override {
94 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
95 }
96
97 /// Parameter indices.
98 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
99 /// Local descriptors.
101 std::optional<SourceInfo> LocOverride = std::nullopt;
102
103private:
104 /// Current compilation context.
105 Context &Ctx;
106 /// Current program.
107 Program &P;
108 /// Callee evaluation state.
109 InterpState S;
110 /// Location to write the result to.
111 EvaluationResult EvalResult;
112 /// Whether the result should be converted to an RValue.
113 bool ConvertResultToRValue = false;
114 /// Whether we should check if the result has been fully
115 /// initialized.
116 bool CheckFullyInitialized = false;
117 /// Callback to call when using interpretAsPointer.
118 std::optional<PtrCallback> PtrCB;
119
120 /// Temporaries which require storage.
122
123 Block *getLocal(unsigned Index) const {
124 assert(Index < Locals.size());
125 return reinterpret_cast<Block *>(Locals[Index].get());
126 }
127
128 void updateGlobalTemporaries();
129
130 // The emitter always tracks the current instruction and sets OpPC to a token
131 // value which is mapped to the location of the opcode being evaluated.
132 CodePtr OpPC;
133 /// Location of the current instruction.
134 SourceInfo CurrentSource;
135
136 /// Next label ID to generate - first label is 1.
137 LabelTy NextLabel = 1;
138 /// Label being executed - 0 is the entry label.
139 LabelTy CurrentLabel = 0;
140 /// Active block which should be executed.
141 LabelTy ActiveLabel = 0;
142
143protected:
144#define GET_EVAL_PROTO
145#include "Opcodes.inc"
146#undef GET_EVAL_PROTO
147};
148
149} // namespace interp
150} // namespace clang
151
152#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:2946
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2018
Represents a variable declaration or definition.
Definition Decl.h:924
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.
Definition EvalEmitter.h:98
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:85
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
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.
llvm::function_ref< bool(const Pointer &)> PtrCallback
Definition EvalEmitter.h:36
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:84
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.
Definition EvalEmitter.h:93
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
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:229
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:56
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
Interpreter context.
Definition InterpState.h:36
A pointer to a memory block, live or dead.
Definition Pointer.h:97
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 Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2329
The JSON file list parser is used to communicate input to InstallAPI.
unsigned int uint32_t
__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:123
Information about a local's storage.
Definition Function.h:39