clang  14.0.0git
ByteCodeEmitter.h
Go to the documentation of this file.
1 //===--- ByteCodeEmitter.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_LINKEMITTER_H
14 #define LLVM_CLANG_AST_INTERP_LINKEMITTER_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 namespace interp {
27 class Context;
28 class SourceInfo;
29 enum Opcode : uint32_t;
30 
31 /// An emitter which links the program to bytecode for later use.
33 protected:
34  using LabelTy = uint32_t;
35  using AddrTy = uintptr_t;
37 
38 public:
39  /// Compiles the function into the module.
41 
42 protected:
43  ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
44 
45  virtual ~ByteCodeEmitter() {}
46 
47  /// Define a label.
48  void emitLabel(LabelTy Label);
49  /// Create a label.
50  LabelTy getLabel() { return ++NextLabel; }
51 
52  /// Methods implemented by the compiler.
53  virtual bool visitFunc(const FunctionDecl *E) = 0;
54  virtual bool visitExpr(const Expr *E) = 0;
55  virtual bool visitDecl(const VarDecl *E) = 0;
56 
57  /// Bails out if a given node cannot be compiled.
58  bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
59  bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
60  bool bail(const SourceLocation &Loc);
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  /// Callback for local registration.
70 
71  /// Parameter indices.
72  llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
73  /// Local descriptors.
75 
76 private:
77  /// Current compilation context.
78  Context &Ctx;
79  /// Program to link to.
80  Program &P;
81  /// Index of the next available label.
82  LabelTy NextLabel = 0;
83  /// Offset of the next local variable.
84  unsigned NextLocalOffset = 0;
85  /// Location of a failure.
86  llvm::Optional<SourceLocation> BailLocation;
87  /// Label information for linker.
88  llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
89  /// Location of label relocations.
90  llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
91  /// Program code.
92  std::vector<char> Code;
93  /// Opcode to expression mapping.
94  SourceMap SrcMap;
95 
96  /// Returns the offset for a jump or records a relocation.
97  int32_t getOffset(LabelTy Label);
98 
99  /// Emits an opcode.
100  template <typename... Tys>
101  bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
102 
103 protected:
104 #define GET_LINK_PROTO
105 #include "Opcodes.inc"
106 #undef GET_LINK_PROTO
107 };
108 
109 } // namespace interp
110 } // namespace clang
111 
112 #endif
clang::Decl::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:422
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::interp::ByteCodeEmitter::createLocal
Local createLocal(Descriptor *D)
Callback for local registration.
Definition: ByteCodeEmitter.cpp:81
clang::interp::ByteCodeEmitter::emitLabel
void emitLabel(LabelTy Label)
Define a label.
Definition: ByteCodeEmitter.cpp:88
llvm::Optional
Definition: LLVM.h:40
clang::interp::Context
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:37
llvm::Expected
Definition: LLVM.h:41
clang::interp::Scope::Local
Information about a local's storage.
Definition: Function.h:35
clang::interp::ByteCodeEmitter::Descriptors
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: ByteCodeEmitter.h:74
Source.h
clang::interp::Opcode
Opcode
Definition: Opcode.h:21
clang::interp::ByteCodeEmitter::bail
bool bail(const Decl *D)
Definition: ByteCodeEmitter.h:59
clang::interp::ByteCodeEmitter::visitFunc
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
InterpState.h
clang::interp::ByteCodeEmitter::visitExpr
virtual bool visitExpr(const Expr *E)=0
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:124
clang::interp::ByteCodeEmitter::~ByteCodeEmitter
virtual ~ByteCodeEmitter()
Definition: ByteCodeEmitter.h:45
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::interp::ByteCodeEmitter::ByteCodeEmitter
ByteCodeEmitter(Context &Ctx, Program &P)
Definition: ByteCodeEmitter.h:43
PrimType.h
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:66
InterpStack.h
Label
std::string Label
Definition: UsingDeclarationsSorter.cpp:69
clang::interp::ByteCodeEmitter::visitDecl
virtual bool visitDecl(const VarDecl *E)=0
clang::interp::ByteCodeEmitter
An emitter which links the program to bytecode for later use.
Definition: ByteCodeEmitter.h:32
clang::interp::ByteCodeEmitter::LabelTy
uint32_t LabelTy
Definition: ByteCodeEmitter.h:34
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::interp::ByteCodeEmitter::compileFunc
llvm::Expected< Function * > compileFunc(const FunctionDecl *F)
Compiles the function into the module.
Definition: ByteCodeEmitter.cpp:22
clang::interp::ByteCodeEmitter::AddrTy
uintptr_t AddrTy
Definition: ByteCodeEmitter.h:35
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::interp::SourceMap
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:84
clang::interp::ByteCodeEmitter::Params
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
Definition: ByteCodeEmitter.h:72
clang::interp::ByteCodeEmitter::fallthrough
bool fallthrough(const LabelTy &Label)
Definition: ByteCodeEmitter.cpp:184
clang::interp::ByteCodeEmitter::jumpTrue
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Definition: ByteCodeEmitter.cpp:172
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang
Definition: CalledOnceCheck.h:17
clang::interp::ByteCodeEmitter::jumpFalse
bool jumpFalse(const LabelTy &Label)
Definition: ByteCodeEmitter.cpp:176
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
Program.h
clang::interp::ByteCodeEmitter::jump
bool jump(const LabelTy &Label)
Definition: ByteCodeEmitter.cpp:180
clang::interp::ByteCodeEmitter::bail
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
Definition: ByteCodeEmitter.h:58
clang::interp::ByteCodeEmitter::Local
Scope::Local Local
Definition: ByteCodeEmitter.h:36
clang::interp::ByteCodeEmitter::getLabel
LabelTy getLabel()
Create a label.
Definition: ByteCodeEmitter.h:50
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:43
clang::Expr
This represents one expression.
Definition: Expr.h:109
ByteCodeGenError.h
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1856
Context.h