xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/ByteCodeEmitter.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1a7dea167SDimitry Andric //===--- ByteCodeEmitter.h - Instruction emitter for the VM ---------*- C++ -*-===//
2a7dea167SDimitry Andric //
3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7dea167SDimitry Andric //
7a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8a7dea167SDimitry Andric //
9a7dea167SDimitry Andric // Defines the instruction emitters.
10a7dea167SDimitry Andric //
11a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
12a7dea167SDimitry Andric 
13a7dea167SDimitry Andric #ifndef LLVM_CLANG_AST_INTERP_LINKEMITTER_H
14a7dea167SDimitry Andric #define LLVM_CLANG_AST_INTERP_LINKEMITTER_H
15a7dea167SDimitry Andric 
16a7dea167SDimitry Andric #include "Context.h"
17a7dea167SDimitry Andric #include "PrimType.h"
18a7dea167SDimitry Andric #include "Program.h"
19a7dea167SDimitry Andric #include "Source.h"
20a7dea167SDimitry Andric #include "llvm/Support/Error.h"
21a7dea167SDimitry Andric 
22a7dea167SDimitry Andric namespace clang {
23a7dea167SDimitry Andric namespace interp {
24a7dea167SDimitry Andric enum Opcode : uint32_t;
25a7dea167SDimitry Andric 
26a7dea167SDimitry Andric /// An emitter which links the program to bytecode for later use.
27a7dea167SDimitry Andric class ByteCodeEmitter {
28a7dea167SDimitry Andric protected:
29a7dea167SDimitry Andric   using LabelTy = uint32_t;
30a7dea167SDimitry Andric   using AddrTy = uintptr_t;
31a7dea167SDimitry Andric   using Local = Scope::Local;
32a7dea167SDimitry Andric 
33a7dea167SDimitry Andric public:
34a7dea167SDimitry Andric   /// Compiles the function into the module.
35bdd1243dSDimitry Andric   llvm::Expected<Function *> compileFunc(const FunctionDecl *FuncDecl);
36a7dea167SDimitry Andric 
37a7dea167SDimitry Andric protected:
38a7dea167SDimitry Andric   ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
39a7dea167SDimitry Andric 
40a7dea167SDimitry Andric   virtual ~ByteCodeEmitter() {}
41a7dea167SDimitry Andric 
42a7dea167SDimitry Andric   /// Define a label.
43a7dea167SDimitry Andric   void emitLabel(LabelTy Label);
44a7dea167SDimitry Andric   /// Create a label.
45a7dea167SDimitry Andric   LabelTy getLabel() { return ++NextLabel; }
46a7dea167SDimitry Andric 
47a7dea167SDimitry Andric   /// Methods implemented by the compiler.
48a7dea167SDimitry Andric   virtual bool visitFunc(const FunctionDecl *E) = 0;
49a7dea167SDimitry Andric   virtual bool visitExpr(const Expr *E) = 0;
50a7dea167SDimitry Andric   virtual bool visitDecl(const VarDecl *E) = 0;
51a7dea167SDimitry Andric 
52a7dea167SDimitry Andric   /// Bails out if a given node cannot be compiled.
53a7dea167SDimitry Andric   bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
54a7dea167SDimitry Andric   bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
55a7dea167SDimitry Andric   bool bail(const SourceLocation &Loc);
56a7dea167SDimitry Andric 
57a7dea167SDimitry Andric   /// Emits jumps.
58a7dea167SDimitry Andric   bool jumpTrue(const LabelTy &Label);
59a7dea167SDimitry Andric   bool jumpFalse(const LabelTy &Label);
60a7dea167SDimitry Andric   bool jump(const LabelTy &Label);
61a7dea167SDimitry Andric   bool fallthrough(const LabelTy &Label);
62a7dea167SDimitry Andric 
63a7dea167SDimitry Andric   /// Callback for local registration.
64a7dea167SDimitry Andric   Local createLocal(Descriptor *D);
65a7dea167SDimitry Andric 
66a7dea167SDimitry Andric   /// Parameter indices.
67*5f757f3fSDimitry Andric   llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
6806c3fb27SDimitry Andric   /// Lambda captures.
69*5f757f3fSDimitry Andric   llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
70*5f757f3fSDimitry Andric   /// Offset of the This parameter in a lambda record.
71*5f757f3fSDimitry Andric   unsigned LambdaThisCapture = 0;
72a7dea167SDimitry Andric   /// Local descriptors.
73a7dea167SDimitry Andric   llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
74a7dea167SDimitry Andric 
75a7dea167SDimitry Andric private:
76a7dea167SDimitry Andric   /// Current compilation context.
77a7dea167SDimitry Andric   Context &Ctx;
78a7dea167SDimitry Andric   /// Program to link to.
79a7dea167SDimitry Andric   Program &P;
80a7dea167SDimitry Andric   /// Index of the next available label.
81a7dea167SDimitry Andric   LabelTy NextLabel = 0;
82a7dea167SDimitry Andric   /// Offset of the next local variable.
83a7dea167SDimitry Andric   unsigned NextLocalOffset = 0;
84a7dea167SDimitry Andric   /// Location of a failure.
85bdd1243dSDimitry Andric   std::optional<SourceLocation> BailLocation;
86a7dea167SDimitry Andric   /// Label information for linker.
87a7dea167SDimitry Andric   llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
88a7dea167SDimitry Andric   /// Location of label relocations.
89a7dea167SDimitry Andric   llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
90a7dea167SDimitry Andric   /// Program code.
9106c3fb27SDimitry Andric   std::vector<std::byte> Code;
92a7dea167SDimitry Andric   /// Opcode to expression mapping.
93a7dea167SDimitry Andric   SourceMap SrcMap;
94a7dea167SDimitry Andric 
95a7dea167SDimitry Andric   /// Returns the offset for a jump or records a relocation.
96a7dea167SDimitry Andric   int32_t getOffset(LabelTy Label);
97a7dea167SDimitry Andric 
98a7dea167SDimitry Andric   /// Emits an opcode.
99a7dea167SDimitry Andric   template <typename... Tys>
100a7dea167SDimitry Andric   bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
101a7dea167SDimitry Andric 
102a7dea167SDimitry Andric protected:
103a7dea167SDimitry Andric #define GET_LINK_PROTO
104a7dea167SDimitry Andric #include "Opcodes.inc"
105a7dea167SDimitry Andric #undef GET_LINK_PROTO
106a7dea167SDimitry Andric };
107a7dea167SDimitry Andric 
108a7dea167SDimitry Andric } // namespace interp
109a7dea167SDimitry Andric } // namespace clang
110a7dea167SDimitry Andric 
111a7dea167SDimitry Andric #endif
112