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