clang 20.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#include "llvm/Support/Error.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
38 bool ConvertResultToRValue = false,
39 bool DestroyToplevelScope = false);
40 EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);
41
42 /// Clean up all resources.
43 void cleanup();
44
45 InterpState &getState() { return S; }
46
47protected:
49
50 virtual ~EvalEmitter();
51
52 /// Define a label.
54 /// Create a label.
56
57 /// Methods implemented by the compiler.
58 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
59 virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) = 0;
60 virtual bool visitFunc(const FunctionDecl *F) = 0;
61
62 /// Emits jumps.
63 bool jumpTrue(const LabelTy &Label);
64 bool jumpFalse(const LabelTy &Label);
65 bool jump(const LabelTy &Label);
66 bool fallthrough(const LabelTy &Label);
67
68 /// Since expressions can only jump forward, predicated execution is
69 /// used to deal with if-else statements.
70 bool isActive() const { return CurrentLabel == ActiveLabel; }
71
72 /// Callback for registering a local.
74
75 /// Returns the source location of the current opcode.
76 SourceInfo getSource(const Function *F, CodePtr PC) const override {
77 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
78 }
79
80 /// Parameter indices.
81 llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
82 /// Lambda captures.
83 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
84 /// Offset of the This parameter in a lambda record.
86 /// Local descriptors.
88
89private:
90 /// Current compilation context.
91 Context &Ctx;
92 /// Current program.
93 Program &P;
94 /// Callee evaluation state.
96 /// Location to write the result to.
97 EvaluationResult EvalResult;
98 /// Whether the result should be converted to an RValue.
99 bool ConvertResultToRValue = false;
100 /// Whether we should check if the result has been fully
101 /// initialized.
102 bool CheckFullyInitialized = false;
103
104 /// Temporaries which require storage.
105 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
106
107 Block *getLocal(unsigned Index) const {
108 auto It = Locals.find(Index);
109 assert(It != Locals.end() && "Missing local variable");
110 return reinterpret_cast<Block *>(It->second.get());
111 }
112
113 void updateGlobalTemporaries();
114
115 // The emitter always tracks the current instruction and sets OpPC to a token
116 // value which is mapped to the location of the opcode being evaluated.
117 CodePtr OpPC;
118 /// Location of the current instruction.
119 SourceInfo CurrentSource;
120
121 /// Next label ID to generate - first label is 1.
122 LabelTy NextLabel = 1;
123 /// Label being executed - 0 is the entry label.
124 LabelTy CurrentLabel = 0;
125 /// Active block which should be executed.
126 LabelTy ActiveLabel = 0;
127
128protected:
129#define GET_EVAL_PROTO
130#include "Opcodes.inc"
131#undef GET_EVAL_PROTO
132};
133
134} // namespace interp
135} // namespace clang
136
137#endif
NodeId Parent
Definition: ASTDiff.cpp:191
const Decl * D
Expr * E
std::string Label
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1935
Represents a variable declaration or definition.
Definition: Decl.h:882
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
An emitter which evaluates opcodes as they are emitted.
Definition: EvalEmitter.h:31
bool jump(const LabelTy &Label)
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:56
virtual bool visitFunc(const FunctionDecl *F)=0
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
Definition: EvalEmitter.cpp:39
bool jumpFalse(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:83
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:79
bool isActive() const
Since expressions can only jump forward, predicated execution is used to deal with if-else statements...
Definition: EvalEmitter.h:70
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.
Definition: EvalEmitter.cpp:37
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:81
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
Definition: EvalEmitter.h:83
virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext)=0
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition: EvalEmitter.h:81
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
Definition: EvalEmitter.h:85
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition: EvalEmitter.h:76
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:87
bool jumpTrue(const LabelTy &Label)
Emits jumps.
InterpState & getState()
Definition: EvalEmitter.h:45
Defines the result of an evaluation.
Bytecode function.
Definition: Function.h:81
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:189
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:36
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:28
Interpreter context.
Definition: InterpState.h:36
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
Interface for classes which map locations to sources.
Definition: Source.h:103
Interface for the VM to interact with the AST walker's context.
Definition: State.h:57
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:116
Information about a local's storage.
Definition: Function.h:39