[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / AST / Interp / ByteCodeEmitter.h
blob5520f8c3006106fcf8c104267648b131518ee7c5
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_LINKEMITTER_H
14 #define LLVM_CLANG_AST_INTERP_LINKEMITTER_H
16 #include "Context.h"
17 #include "PrimType.h"
18 #include "Program.h"
19 #include "Source.h"
20 #include "llvm/Support/Error.h"
22 namespace clang {
23 namespace interp {
24 enum Opcode : uint32_t;
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;
33 public:
34 /// Compiles the function into the module.
35 llvm::Expected<Function *> compileFunc(const FunctionDecl *FuncDecl);
37 protected:
38 ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
40 virtual ~ByteCodeEmitter() {}
42 /// Define a label.
43 void emitLabel(LabelTy Label);
44 /// Create a label.
45 LabelTy getLabel() { return ++NextLabel; }
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;
52 /// Bails out if a given node cannot be compiled.
53 bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
54 bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
55 bool bail(const SourceLocation &Loc);
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);
63 /// Callback for local registration.
64 Local createLocal(Descriptor *D);
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;
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;
95 /// Returns the offset for a jump or records a relocation.
96 int32_t getOffset(LabelTy Label);
98 /// Emits an opcode.
99 template <typename... Tys>
100 bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
102 protected:
103 #define GET_LINK_PROTO
104 #include "Opcodes.inc"
105 #undef GET_LINK_PROTO
108 } // namespace interp
109 } // namespace clang
111 #endif