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