Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/AST/Interp/ByteCodeEmitter.h
Line
Count
Source (jump to first uncovered line)
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
#include "llvm/Support/Error.h"
21
22
namespace clang {
23
namespace interp {
24
enum Opcode : uint32_t;
25
26
/// An emitter which links the program to bytecode for later use.
27
class ByteCodeEmitter {
28
protected:
29
  using LabelTy = uint32_t;
30
  using AddrTy = uintptr_t;
31
  using Local = Scope::Local;
32
33
public:
34
  /// Compiles the function into the module.
35
  llvm::Expected<Function *> compileFunc(const FunctionDecl *FuncDecl);
36
37
protected:
38
0
  ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
39
40
0
  virtual ~ByteCodeEmitter() {}
41
42
  /// Define a label.
43
  void emitLabel(LabelTy Label);
44
  /// Create a label.
45
0
  LabelTy getLabel() { return ++NextLabel; }
46
47
  /// Methods implemented by the compiler.
48
  virtual bool visitFunc(const FunctionDecl *E) = 0;
49
  virtual bool visitExpr(const Expr *E) = 0;
50
  virtual bool visitDecl(const VarDecl *E) = 0;
51
52
  /// Bails out if a given node cannot be compiled.
53
0
  bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
54
0
  bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
55
  bool bail(const SourceLocation &Loc);
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 local registration.
64
  Local createLocal(Descriptor *D);
65
66
  /// Parameter indices.
67
  llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
68
  /// Lambda captures.
69
  llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
70
  /// Offset of the This parameter in a lambda record.
71
  unsigned LambdaThisCapture = 0;
72
  /// Local descriptors.
73
  llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
74
75
private:
76
  /// Current compilation context.
77
  Context &Ctx;
78
  /// Program to link to.
79
  Program &P;
80
  /// Index of the next available label.
81
  LabelTy NextLabel = 0;
82
  /// Offset of the next local variable.
83
  unsigned NextLocalOffset = 0;
84
  /// Location of a failure.
85
  std::optional<SourceLocation> BailLocation;
86
  /// Label information for linker.
87
  llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
88
  /// Location of label relocations.
89
  llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
90
  /// Program code.
91
  std::vector<std::byte> Code;
92
  /// Opcode to expression mapping.
93
  SourceMap SrcMap;
94
95
  /// Returns the offset for a jump or records a relocation.
96
  int32_t getOffset(LabelTy Label);
97
98
  /// Emits an opcode.
99
  template <typename... Tys>
100
  bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
101
102
protected:
103
#define GET_LINK_PROTO
104
#include "Opcodes.inc"
105
#undef GET_LINK_PROTO
106
};
107
108
} // namespace interp
109
} // namespace clang
110
111
#endif