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
21namespace clang {
22namespace interp {
23enum Opcode : uint32_t;
24
25/// An emitter which links the program to bytecode for later use.
26class ByteCodeEmitter {
27protected:
28 using AddrTy = uintptr_t;
29 using Local = Scope::Local;
30
31public:
32 using LabelTy = uint32_t;
33 /// Compiles the function into the module.
34 void compileFunc(const FunctionDecl *FuncDecl, Function *Func = nullptr);
35
36protected:
37 ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
38
39 virtual ~ByteCodeEmitter() {}
40
41 /// Define a label.
42 void emitLabel(LabelTy Label);
43 /// Create a label.
44 LabelTy getLabel() { return ++NextLabel; }
45
46 /// Methods implemented by the compiler.
47 virtual bool visitFunc(const FunctionDecl *E) = 0;
48 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
49 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
50 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
51 bool ConstantContext) = 0;
52 virtual bool visitDtorCall(const VarDecl *VD, const APValue &) = 0;
53 virtual bool visit(const Expr *E) = 0;
54 virtual bool emitBool(bool V, const Expr *E) = 0;
55
56 /// Emits jumps.
57 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
58 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
59 bool jump(const LabelTy &Label, SourceInfo SI);
60 bool fallthrough(const LabelTy &Label);
61 /// Speculative execution.
62 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
63
64 /// We're always emitting bytecode.
65 bool isActive() const { return true; }
66 bool checkingForUndefinedBehavior() const { return false; }
67
68 /// Callback for local registration.
69 Local createLocal(Descriptor *D);
70
71 /// Parameter indices.
72 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
73 /// Lambda captures.
74 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
75 /// Offset of the This parameter in a lambda record.
76 ParamOffset LambdaThisCapture{.Offset: 0, .IsPtr: false};
77 /// Local descriptors.
78 llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
79 std::optional<SourceInfo> LocOverride = std::nullopt;
80
81private:
82 /// Current compilation context.
83 Context &Ctx;
84 /// Program to link to.
85 Program &P;
86 /// Index of the next available label.
87 LabelTy NextLabel = 0;
88 /// Offset of the next local variable.
89 unsigned NextLocalOffset = 0;
90 /// Label information for linker.
91 llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
92 /// Location of label relocations.
93 llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
94 /// Program code.
95 llvm::SmallVector<std::byte> Code;
96 /// Opcode to expression mapping.
97 SourceMap SrcMap;
98
99 /// Returns the offset for a jump or records a relocation.
100 int32_t getOffset(LabelTy Label);
101
102 /// Emits an opcode.
103 template <typename... Tys>
104 bool emitOp(Opcode Op, const Tys &...Args, SourceInfo L);
105
106protected:
107#define GET_LINK_PROTO
108#include "Opcodes.inc"
109#undef GET_LINK_PROTO
110};
111
112} // namespace interp
113} // namespace clang
114
115#endif
116