1 //===--- ByteCodeEmitter.h - Instruction emitter for the VM ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines the instruction emitters.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_LINKEMITTER_H
14 #define LLVM_CLANG_AST_INTERP_LINKEMITTER_H
20 #include "llvm/Support/Error.h"
24 enum Opcode
: uint32_t;
26 /// An emitter which links the program to bytecode for later use.
27 class ByteCodeEmitter
{
29 using LabelTy
= uint32_t;
30 using AddrTy
= uintptr_t;
31 using Local
= Scope::Local
;
34 /// Compiles the function into the module.
35 llvm::Expected
<Function
*> compileFunc(const FunctionDecl
*FuncDecl
);
38 ByteCodeEmitter(Context
&Ctx
, Program
&P
) : Ctx(Ctx
), P(P
) {}
40 virtual ~ByteCodeEmitter() {}
43 void emitLabel(LabelTy 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
);
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
;
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
;
76 /// Current compilation context.
78 /// Program to link to.
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
;
91 std::vector
<std::byte
> Code
;
92 /// Opcode to expression mapping.
95 /// Returns the offset for a jump or records a relocation.
96 int32_t getOffset(LabelTy Label
);
99 template <typename
... Tys
>
100 bool emitOp(Opcode Op
, const Tys
&... Args
, const SourceInfo
&L
);
103 #define GET_LINK_PROTO
104 #include "Opcodes.inc"
105 #undef GET_LINK_PROTO
108 } // namespace interp