clang  10.0.0svn
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
Represents a function declaration or definition.
Definition: Decl.h:1784
virtual bool visitDecl(const VarDecl *E)=0
Stmt - This represents one statement.
Definition: Stmt.h:66
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:421
Represents a variable declaration or definition.
Definition: Decl.h:827
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:48
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:274
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
LabelTy getLabel()
Create a label.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
llvm::Expected< Function * > compileFunc(const FunctionDecl *F)
Compiles the function into the module.
bool fallthrough(const LabelTy &Label)
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
bool jumpFalse(const LabelTy &Label)
This represents one expression.
Definition: Expr.h:108
std::string Label
Information about a local&#39;s storage.
Definition: Function.h:35
__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
bool jump(const LabelTy &Label)
The program contains and links the bytecode for all functions.
Definition: Program.h:43
Local createLocal(Descriptor *D)
Callback for local registration.
Encodes a location in the source.
void emitLabel(LabelTy Label)
Define a label.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
Dataflow Directional Tag Classes.
virtual bool visitExpr(const Expr *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:99
ByteCodeEmitter(Context &Ctx, Program &P)
An emitter which links the program to bytecode for later use.