clang  19.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 
22 namespace clang {
23 namespace interp {
24 class Context;
25 class Function;
26 class InterpStack;
27 class Program;
28 enum Opcode : uint32_t;
29 
30 /// An emitter which evaluates opcodes as they are emitted.
31 class EvalEmitter : public SourceMapper {
32 public:
33  using LabelTy = uint32_t;
34  using AddrTy = uintptr_t;
36 
38  bool ConvertResultToRValue = false);
39  EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);
40 
41  InterpState &getState() { return S; }
42 
43 protected:
45 
46  virtual ~EvalEmitter();
47 
48  /// Define a label.
49  void emitLabel(LabelTy Label);
50  /// Create a label.
51  LabelTy getLabel();
52 
53  /// Methods implemented by the compiler.
54  virtual bool visitExpr(const Expr *E) = 0;
55  virtual bool visitDecl(const VarDecl *VD) = 0;
56 
57  /// Emits jumps.
58  bool jumpTrue(const LabelTy &Label);
59  bool jumpFalse(const LabelTy &Label);
60  bool jump(const LabelTy &Label);
61  bool fallthrough(const LabelTy &Label);
62 
63  /// Callback for registering a local.
65 
66  /// Returns the source location of the current opcode.
67  SourceInfo getSource(const Function *F, CodePtr PC) const override {
68  return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
69  }
70 
71  /// Parameter indices.
72  llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
73  /// Lambda captures.
74  llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
75  /// Offset of the This parameter in a lambda record.
77  /// Local descriptors.
79 
80 private:
81  /// Current compilation context.
82  Context &Ctx;
83  /// Current program.
84  Program &P;
85  /// Callee evaluation state.
86  InterpState S;
87  /// Location to write the result to.
88  EvaluationResult EvalResult;
89  /// Whether the result should be converted to an RValue.
90  bool ConvertResultToRValue = false;
91  /// Whether we should check if the result has been fully
92  /// initialized.
93  bool CheckFullyInitialized = false;
94 
95  /// Temporaries which require storage.
96  llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
97 
98  Block *getLocal(unsigned Index) const {
99  auto It = Locals.find(Index);
100  assert(It != Locals.end() && "Missing local variable");
101  return reinterpret_cast<Block *>(It->second.get());
102  }
103 
104  // The emitter always tracks the current instruction and sets OpPC to a token
105  // value which is mapped to the location of the opcode being evaluated.
106  CodePtr OpPC;
107  /// Location of the current instruction.
108  SourceInfo CurrentSource;
109 
110  /// Next label ID to generate - first label is 1.
111  LabelTy NextLabel = 1;
112  /// Label being executed - 0 is the entry label.
113  LabelTy CurrentLabel = 0;
114  /// Active block which should be executed.
115  LabelTy ActiveLabel = 0;
116 
117  /// Since expressions can only jump forward, predicated execution is
118  /// used to deal with if-else statements.
119  bool isActive() const { return CurrentLabel == ActiveLabel; }
120 
121 protected:
122 #define GET_EVAL_PROTO
123 #include "Opcodes.inc"
124 #undef GET_EVAL_PROTO
125 };
126 
127 } // namespace interp
128 } // namespace clang
129 
130 #endif
NodeId Parent
Definition: ASTDiff.cpp:191
std::string Label
This represents one expression.
Definition: Expr.h:110
Represents a variable declaration or definition.
Definition: Decl.h:919
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:35
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:50
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:71
InterpState & getState()
Definition: EvalEmitter.h:41
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:65
bool fallthrough(const LabelTy &Label)
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:69
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
Definition: EvalEmitter.h:74
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
Definition: EvalEmitter.cpp:19
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition: EvalEmitter.h:72
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
Definition: EvalEmitter.h:76
SourceInfo getSource(const Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition: EvalEmitter.h:67
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:78
virtual bool visitDecl(const VarDecl *VD)=0
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: EvalEmitter.cpp:93
Defines the result of an evaluation.
Bytecode function.
Definition: Function.h:77
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:174
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:26
Interpreter context.
Definition: InterpState.h:35
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:72
Interface for classes which map locations to sources.
Definition: Source.h:94
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
The JSON file list parser is used to communicate input to InstallAPI.
__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:91
Information about a local's storage.
Definition: Function.h:38