clang 22.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 "Source.h"
20
21namespace clang {
22namespace interp {
23class Context;
24class Function;
25class InterpStack;
26class Program;
27enum Opcode : uint32_t;
28
29/// An emitter which evaluates opcodes as they are emitted.
30class EvalEmitter : public SourceMapper {
31public:
32 using LabelTy = uint32_t;
35 using PtrCallback = llvm::function_ref<bool(const Pointer &)>;
36
38 bool ConvertResultToRValue = false,
39 bool DestroyToplevelScope = false);
40 EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);
41 /// Interpret the given Expr to a Pointer.
43 /// Interpret the given expression as if it was in the body of the given
44 /// function, i.e. the parameters of the function are available for use.
45 bool interpretCall(const FunctionDecl *FD, const Expr *E);
46
47 /// Clean up all resources.
48 void cleanup();
49
50protected:
51 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
52
53 virtual ~EvalEmitter();
54
55 /// Define a label.
56 void emitLabel(LabelTy Label);
57 /// Create a label.
59
60 /// Methods implemented by the compiler.
61 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
62 virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) = 0;
63 virtual bool visitFunc(const FunctionDecl *F) = 0;
64 virtual bool visit(const Expr *E) = 0;
65 virtual bool emitBool(bool V, const Expr *E) = 0;
66
67 /// Emits jumps.
68 bool jumpTrue(const LabelTy &Label);
69 bool jumpFalse(const LabelTy &Label);
70 bool jump(const LabelTy &Label);
71 bool fallthrough(const LabelTy &Label);
72 /// Speculative execution.
73 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
74
75 /// Since expressions can only jump forward, predicated execution is
76 /// used to deal with if-else statements.
77 bool isActive() const { return CurrentLabel == ActiveLabel; }
79 return S.checkingForUndefinedBehavior();
80 }
81
82 /// Callback for registering a local.
84
85 /// Returns the source location of the current opcode.
86 SourceInfo getSource(const Function *F, CodePtr PC) const override {
87 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
88 }
89
90 /// Parameter indices.
91 llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
92 /// Lambda captures.
93 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
94 /// Offset of the This parameter in a lambda record.
96 /// Local descriptors.
98 std::optional<SourceInfo> LocOverride = std::nullopt;
99
100private:
101 /// Current compilation context.
102 Context &Ctx;
103 /// Current program.
104 Program &P;
105 /// Callee evaluation state.
106 InterpState S;
107 /// Location to write the result to.
108 EvaluationResult EvalResult;
109 /// Whether the result should be converted to an RValue.
110 bool ConvertResultToRValue = false;
111 /// Whether we should check if the result has been fully
112 /// initialized.
113 bool CheckFullyInitialized = false;
114 /// Callback to call when using interpretAsPointer.
115 std::optional<PtrCallback> PtrCB;
116
117 /// Temporaries which require storage.
119
120 Block *getLocal(unsigned Index) const {
121 assert(Index < Locals.size());
122 return reinterpret_cast<Block *>(Locals[Index].get());
123 }
124
125 void updateGlobalTemporaries();
126
127 // The emitter always tracks the current instruction and sets OpPC to a token
128 // value which is mapped to the location of the opcode being evaluated.
129 CodePtr OpPC;
130 /// Location of the current instruction.
131 SourceInfo CurrentSource;
132
133 /// Next label ID to generate - first label is 1.
134 LabelTy NextLabel = 1;
135 /// Label being executed - 0 is the entry label.
136 LabelTy CurrentLabel = 0;
137 /// Active block which should be executed.
138 LabelTy ActiveLabel = 0;
139
140protected:
141#define GET_EVAL_PROTO
142#include "Opcodes.inc"
143#undef GET_EVAL_PROTO
144};
145
146} // namespace interp
147} // namespace clang
148
149#endif
#define V(N, I)
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2879
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:1999
Represents a variable declaration or definition.
Definition Decl.h:925
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:41
std::optional< SourceInfo > LocOverride
Definition EvalEmitter.h:98
bool jump(const LabelTy &Label)
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
virtual bool visitFunc(const FunctionDecl *F)=0
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
bool jumpFalse(const LabelTy &Label)
bool checkingForUndefinedBehavior() const
Definition EvalEmitter.h:78
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
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:35
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:77
virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope)=0
Methods implemented by the compiler.
bool fallthrough(const LabelTy &Label)
void cleanup()
Clean up all resources.
LabelTy getLabel()
Create a label.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
Definition EvalEmitter.h:93
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 visitDeclAndReturn(const VarDecl *VD, bool ConstantContext)=0
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition EvalEmitter.h:91
virtual bool emitBool(bool V, const Expr *E)=0
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
Definition EvalEmitter.h:95
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition EvalEmitter.h:86
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition EvalEmitter.h:97
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Defines the result of an evaluation.
Bytecode function.
Definition Function.h:86
bool hasBody() const
Checks if the function already has a body attached.
Definition Function.h:196
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:63
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:90
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:73
Interface for classes which map locations to sources.
Definition Source.h:99
Interface for the VM to interact with the AST walker's context.
Definition State.h:58
#define bool
Definition gpuintrin.h:32
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:122
Information about a local's storage.
Definition Function.h:39