clang 10.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 "ByteCodeGenError.h"
17 #include "Context.h"
18 #include "InterpStack.h"
19 #include "InterpState.h"
20 #include "PrimType.h"
21 #include "Program.h"
22 #include "Source.h"
23 #include "llvm/Support/Error.h"
24
25 namespace clang {
26 class FunctionDecl;
27 namespace interp {
28 class Context;
29 class Function;
30 class InterpState;
31 class Program;
32 class SourceInfo;
33 enum Opcode : uint32_t;
34
35 /// An emitter which evaluates opcodes as they are emitted.
36 class EvalEmitter : public SourceMapper {
37 public:
38 using LabelTy = uint32_t;
39 using AddrTy = uintptr_t;
40 using Local = Scope::Local;
41
42 llvm::Expected<bool> interpretExpr(const Expr *E);
43 llvm::Expected<bool> interpretDecl(const VarDecl *VD);
44
45 protected:
46 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk,
47 APValue &Result);
48
49 virtual ~EvalEmitter() {}
50
51 /// Define a label.
52 void emitLabel(LabelTy Label);
53 /// Create a label.
54 LabelTy getLabel();
55
56 /// Methods implemented by the compiler.
57 virtual bool visitExpr(const Expr *E) = 0;
58 virtual bool visitDecl(const VarDecl *VD) = 0;
59
60 bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
61 bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
62 bool bail(const SourceLocation &Loc);
63
64 /// Emits jumps.
65 bool jumpTrue(const LabelTy &Label);
66 bool jumpFalse(const LabelTy &Label);
67 bool jump(const LabelTy &Label);
68 bool fallthrough(const LabelTy &Label);
69
70 /// Callback for registering a local.
71 Local createLocal(Descriptor *D);
72
73 /// Returns the source location of the current opcode.
74 SourceInfo getSource(Function *F, CodePtr PC) const override {
75 return F ? F->getSource(PC) : CurrentSource;
76 }
77
78 /// Parameter indices.
79 llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
80 /// Local descriptors.
81 llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
82
83 private:
84 /// Current compilation context.
85 Context &Ctx;
86 /// Current program.
87 Program &P;
88 /// Callee evaluation state.
89 InterpState S;
90 /// Location to write the result to.
91 APValue &Result;
92
93 /// Temporaries which require storage.
94 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
95
96 // The emitter always tracks the current instruction and sets OpPC to a token
97 // value which is mapped to the location of the opcode being evaluated.
98 CodePtr OpPC;
99 /// Location of a failure.
100 llvm::Optional<SourceLocation> BailLocation;
101 /// Location of the current instruction.
102 SourceInfo CurrentSource;
103
104 /// Next label ID to generate - first label is 1.
105 LabelTy NextLabel = 1;
106 /// Label being executed - 0 is the entry label.
107 LabelTy CurrentLabel = 0;
108 /// Active block which should be executed.
109 LabelTy ActiveLabel = 0;
110
111 /// Since expressions can only jump forward, predicated execution is
112 /// used to deal with if-else statements.
113 bool isActive() { return CurrentLabel == ActiveLabel; }
114
115 /// Helper to invoke a method.
116 bool ExecuteCall(Function *F, Pointer &&This, const SourceInfo &Info);
117 /// Helper to emit a diagnostic on a missing method.
118 bool ExecuteNoCall(const FunctionDecl *F, const SourceInfo &Info);
119
120 protected:
121 #define GET_EVAL_PROTO
122 #include "Opcodes.inc"
123 #undef GET_EVAL_PROTO
124 };
125
126 } // namespace interp
127 } // namespace clang
128
129 #endif
clang::interp::EvalEmitter::bail
bool bail(const Stmt *S)
Definition: EvalEmitter.h:60
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::interp::EvalEmitter::getSource
SourceInfo getSource(Function *F, CodePtr PC) const override
Returns the source location of the current opcode.
Definition: EvalEmitter.h:74
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::interp::SourceMapper
Interface for classes which map locations to sources.
Definition: Source.h:102
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::Decl::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:421
clang::XRayInstrKind::Function
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
clang::interp::EvalEmitter::getLabel
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:49
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::interp::This
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:827
clang::interp::Function::getSource
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:34
clang::interp::EvalEmitter::jumpFalse
bool jumpFalse(const LabelTy &Label)
Definition: EvalEmitter.cpp:77
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
clang::interp::Context
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:38
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
clang::interp::EvalEmitter::EvalEmitter
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk, APValue &Result)
Definition: EvalEmitter.cpp:22
llvm::Optional
Definition: LLVM.h:37
clang::Stmt::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:275
PrimType.h
Context.h
llvm::Expected
Definition: LLVM.h:38
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang::interp::EvalEmitter::visitDecl
virtual bool visitDecl(const VarDecl *VD)=0
clang::interp::EvalEmitter::interpretDecl
llvm::Expected< bool > interpretDecl(const VarDecl *VD)
Definition: EvalEmitter.cpp:37
clang::interp::InterpStack
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:22
Parent
NodeId Parent
Definition: ASTDiff.cpp:191
clang::interp::EvalEmitter::interpretExpr
llvm::Expected< bool > interpretExpr(const Expr *E)
Definition: EvalEmitter.cpp:29
Source.h
clang::Expr
This represents one expression.
Definition: Expr.h:108
InterpState.h
Label
std::string Label
Definition: UsingDeclarationsSorter.cpp:69
clang::interp::Scope::Local
Information about a local&#39;s storage.
Definition: Function.h:35
uintptr_t
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c-base.h:62
clang::interp::EvalEmitter::createLocal
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:51
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:43
Program.h
ByteCodeGenError.h
clang::interp::EvalEmitter::bail
bool bail(const Decl *D)
Definition: EvalEmitter.h:61
clang::interp::EvalEmitter::LabelTy
uint32_t LabelTy
Definition: EvalEmitter.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::interp::EvalEmitter
An emitter which evaluates opcodes as they are emitted.
Definition: EvalEmitter.h:36
clang::interp::EvalEmitter::Params
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
Definition: EvalEmitter.h:79
clang::interp::EvalEmitter::emitLabel
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:45
clang::interp::Function
Bytecode function.
Definition: Function.h:59
InterpStack.h
llvm::SmallVector
Definition: LLVM.h:35
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::interp::EvalEmitter::Descriptors
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:81
clang::interp::EvalEmitter::fallthrough
bool fallthrough(const LabelTy &Label)
Definition: EvalEmitter.cpp:91
clang::interp::EvalEmitter::jump
bool jump(const LabelTy &Label)
Definition: EvalEmitter.cpp:85
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::EvalEmitter::~EvalEmitter
virtual ~EvalEmitter()
Definition: EvalEmitter.h:49
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:115
clang::interp::State
Interface for the VM to interact with the AST walker&#39;s context.
Definition: State.h:55
clang::interp::EvalEmitter::visitExpr
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
clang::interp::Opcode
Opcode
Definition: Opcode.h:21
clang::interp::EvalEmitter::AddrTy
uintptr_t AddrTy
Definition: EvalEmitter.h:39
clang::interp::EvalEmitter::jumpTrue
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: EvalEmitter.cpp:69