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