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 EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);
40
41 /// Clean up all resources.
42 void cleanup();
43
44 InterpState &getState() { return S; }
45
46protected:
48
49 virtual ~EvalEmitter();
50
51 /// Define a label.
53 /// Create a label.
55
56 /// Methods implemented by the compiler.
57 virtual bool visitExpr(const Expr *E) = 0;
58 virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) = 0;
59 virtual bool visitFunc(const FunctionDecl *F) = 0;
60
61 /// Emits jumps.
62 bool jumpTrue(const LabelTy &Label);
63 bool jumpFalse(const LabelTy &Label);
64 bool jump(const LabelTy &Label);
65 bool fallthrough(const LabelTy &Label);
66
67 /// Since expressions can only jump forward, predicated execution is
68 /// used to deal with if-else statements.
69 bool isActive() const { return CurrentLabel == ActiveLabel; }
70
71 /// Callback for registering a local.
73
74 /// Returns the source location of the current opcode.
75 SourceInfo getSource(const Function *F, CodePtr PC) const override {
76 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
77 }
78
79 /// Parameter indices.
80 llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
81 /// Lambda captures.
82 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
83 /// Offset of the This parameter in a lambda record.
85 /// Local descriptors.
87
88private:
89 /// Current compilation context.
90 Context &Ctx;
91 /// Current program.
92 Program &P;
93 /// Callee evaluation state.
95 /// Location to write the result to.
96 EvaluationResult EvalResult;
97 /// Whether the result should be converted to an RValue.
98 bool ConvertResultToRValue = false;
99 /// Whether we should check if the result has been fully
100 /// initialized.
101 bool CheckFullyInitialized = false;
102
103 /// Temporaries which require storage.
104 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
105
106 Block *getLocal(unsigned Index) const {
107 auto It = Locals.find(Index);
108 assert(It != Locals.end() && "Missing local variable");
109 return reinterpret_cast<Block *>(It->second.get());
110 }
111
112 void updateGlobalTemporaries();
113
114 // The emitter always tracks the current instruction and sets OpPC to a token
115 // value which is mapped to the location of the opcode being evaluated.
116 CodePtr OpPC;
117 /// Location of the current instruction.
118 SourceInfo CurrentSource;
119
120 /// Next label ID to generate - first label is 1.
121 LabelTy NextLabel = 1;
122 /// Label being executed - 0 is the entry label.
123 LabelTy CurrentLabel = 0;
124 /// Active block which should be executed.
125 LabelTy ActiveLabel = 0;
126
127protected:
128#define GET_EVAL_PROTO
129#include "Opcodes.inc"
130#undef GET_EVAL_PROTO
131};
132
133} // namespace interp
134} // namespace clang
135
136#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:1932
Represents a variable declaration or definition.
Definition: Decl.h:879
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 interpretExpr(const Expr *E, bool ConvertResultToRValue=false)
Definition: EvalEmitter.cpp:40
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:56
virtual bool visitFunc(const FunctionDecl *F)=0
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
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:69
bool fallthrough(const LabelTy &Label)
void cleanup()
Clean up all resources.
Definition: EvalEmitter.cpp:38
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:81
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
Definition: EvalEmitter.h:82
virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext)=0
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition: EvalEmitter.h:80
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
Definition: EvalEmitter.h:84
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition: EvalEmitter.h:75
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:86
bool jumpTrue(const LabelTy &Label)
Emits jumps.
InterpState & getState()
Definition: EvalEmitter.h:44
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:38
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:27
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:99
Interface for the VM to interact with the AST walker's context.
Definition: State.h:56
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:111
Information about a local's storage.
Definition: Function.h:39