xref: /llvm-project/clang/lib/AST/ByteCode/ByteCodeEmitter.h (revision 83fea8b809b284594e6dd133150bb6d365775e5b)
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.
26 class ByteCodeEmitter {
27 protected:
28   using LabelTy = uint32_t;
29   using AddrTy = uintptr_t;
30   using Local = Scope::Local;
31 
32 public:
33   /// Compiles the function into the module.
34   Function *compileFunc(const FunctionDecl *FuncDecl);
35   Function *compileObjCBlock(const BlockExpr *BE);
36 
37 protected:
38   ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
39 
40   virtual ~ByteCodeEmitter() {}
41 
42   /// Define a label.
43   void emitLabel(LabelTy Label);
44   /// Create a label.
45   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, bool DestroyToplevelScope) = 0;
50   virtual bool visitDeclAndReturn(const VarDecl *E, bool ConstantContext) = 0;
51 
52   /// Emits jumps.
53   bool jumpTrue(const LabelTy &Label);
54   bool jumpFalse(const LabelTy &Label);
55   bool jump(const LabelTy &Label);
56   bool fallthrough(const LabelTy &Label);
57 
58   /// We're always emitting bytecode.
59   bool isActive() const { return true; }
60 
61   /// Callback for local registration.
62   Local createLocal(Descriptor *D);
63 
64   /// Parameter indices.
65   llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
66   /// Lambda captures.
67   llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
68   /// Offset of the This parameter in a lambda record.
69   ParamOffset LambdaThisCapture{0, false};
70   /// Local descriptors.
71   llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
72 
73 private:
74   /// Current compilation context.
75   Context &Ctx;
76   /// Program to link to.
77   Program &P;
78   /// Index of the next available label.
79   LabelTy NextLabel = 0;
80   /// Offset of the next local variable.
81   unsigned NextLocalOffset = 0;
82   /// Label information for linker.
83   llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
84   /// Location of label relocations.
85   llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
86   /// Program code.
87   std::vector<std::byte> Code;
88   /// Opcode to expression mapping.
89   SourceMap SrcMap;
90 
91   /// Returns the offset for a jump or records a relocation.
92   int32_t getOffset(LabelTy Label);
93 
94   /// Emits an opcode.
95   template <typename... Tys>
96   bool emitOp(Opcode Op, const Tys &...Args, const SourceInfo &L);
97 
98 protected:
99 #define GET_LINK_PROTO
100 #include "Opcodes.inc"
101 #undef GET_LINK_PROTO
102 };
103 
104 } // namespace interp
105 } // namespace clang
106 
107 #endif
108