xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/ByteCodeEmitter.h (revision d11f81afd5a4a71d5f725950b0592ca212084780)
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.
32 class ByteCodeEmitter {
33 protected:
34   using LabelTy = uint32_t;
35   using AddrTy = uintptr_t;
36   using Local = Scope::Local;
37 
38 public:
39   /// Compiles the function into the module.
40   llvm::Expected<Function *> compileFunc(const FunctionDecl *F);
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.
69   Local createLocal(Descriptor *D);
70 
71   /// Parameter indices.
72   llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
73   /// Local descriptors.
74   llvm::SmallVector<SmallVector<Local, 8>, 2> 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
113