clang  19.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 "Context.h"
17 #include "PrimType.h"
18 #include "Program.h"
19 #include "Source.h"
20 
21 namespace clang {
22 namespace interp {
23 enum Opcode : uint32_t;
24 
25 /// An emitter which links the program to bytecode for later use.
27 protected:
28  using LabelTy = uint32_t;
29  using AddrTy = uintptr_t;
31 
32 public:
33  /// Compiles the function into the module.
34  Function *compileFunc(const FunctionDecl *FuncDecl);
35 
36 protected:
37  ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
38 
39  virtual ~ByteCodeEmitter() {}
40 
41  /// Define a label.
42  void emitLabel(LabelTy Label);
43  /// Create a label.
44  LabelTy getLabel() { return ++NextLabel; }
45 
46  /// Methods implemented by the compiler.
47  virtual bool visitFunc(const FunctionDecl *E) = 0;
48  virtual bool visitExpr(const Expr *E) = 0;
49  virtual bool visitDecl(const VarDecl *E) = 0;
50 
51  /// Emits jumps.
52  bool jumpTrue(const LabelTy &Label);
53  bool jumpFalse(const LabelTy &Label);
54  bool jump(const LabelTy &Label);
55  bool fallthrough(const LabelTy &Label);
56 
57  /// Callback for local registration.
59 
60  /// Parameter indices.
61  llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
62  /// Lambda captures.
63  llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
64  /// Offset of the This parameter in a lambda record.
66  /// Local descriptors.
68 
69 private:
70  /// Current compilation context.
71  Context &Ctx;
72  /// Program to link to.
73  Program &P;
74  /// Index of the next available label.
75  LabelTy NextLabel = 0;
76  /// Offset of the next local variable.
77  unsigned NextLocalOffset = 0;
78  /// Label information for linker.
79  llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
80  /// Location of label relocations.
81  llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
82  /// Program code.
83  std::vector<std::byte> Code;
84  /// Opcode to expression mapping.
85  SourceMap SrcMap;
86 
87  /// Returns the offset for a jump or records a relocation.
88  int32_t getOffset(LabelTy Label);
89 
90  /// Emits an opcode.
91  template <typename... Tys>
92  bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
93 
94 protected:
95 #define GET_LINK_PROTO
96 #include "Opcodes.inc"
97 #undef GET_LINK_PROTO
98 };
99 
100 } // namespace interp
101 } // namespace clang
102 
103 #endif
StringRef P
std::string Label
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1972
Represents a variable declaration or definition.
Definition: Decl.h:919
An emitter which links the program to bytecode for later use.
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
ByteCodeEmitter(Context &Ctx, Program &P)
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
Function * compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
virtual bool visitExpr(const Expr *E)=0
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
bool jumpFalse(const LabelTy &Label)
LabelTy getLabel()
Create a label.
virtual bool visitDecl(const VarDecl *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
Bytecode function.
Definition: Function.h:77
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
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:91
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