clang 18.0.0git
ByteCodeEmitter.h
Go to the documentation of this file.
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#include "llvm/Support/Error.h"
21
22namespace clang {
23namespace interp {
24enum Opcode : uint32_t;
25
26/// An emitter which links the program to bytecode for later use.
28protected:
29 using LabelTy = uint32_t;
32
33public:
34 /// Compiles the function into the module.
36
37protected:
38 ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
39
40 virtual ~ByteCodeEmitter() {}
41
42 /// Define a 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) = 0;
50 virtual bool visitDecl(const VarDecl *E) = 0;
51
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);
56
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);
62
63 /// Callback for local registration.
65
66 /// Parameter indices.
67 llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;
68 /// Lambda captures.
69 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
71 /// Local descriptors.
73
74private:
75 /// Current compilation context.
76 Context &Ctx;
77 /// Program to link to.
78 Program &P;
79 /// Index of the next available label.
80 LabelTy NextLabel = 0;
81 /// Offset of the next local variable.
82 unsigned NextLocalOffset = 0;
83 /// Location of a failure.
84 std::optional<SourceLocation> BailLocation;
85 /// Label information for linker.
86 llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
87 /// Location of label relocations.
88 llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
89 /// Program code.
90 std::vector<std::byte> Code;
91 /// Opcode to expression mapping.
92 SourceMap SrcMap;
93
94 /// Returns the offset for a jump or records a relocation.
95 int32_t getOffset(LabelTy Label);
96
97 /// Emits an opcode.
98 template <typename... Tys>
99 bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
100
101protected:
102#define GET_LINK_PROTO
103#include "Opcodes.inc"
104#undef GET_LINK_PROTO
105};
106
107} // namespace interp
108} // namespace clang
109
110#endif
StringRef P
std::string Label
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:424
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1919
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:72
Represents a variable declaration or definition.
Definition: Decl.h:915
An emitter which links the program to bytecode for later use.
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
ByteCodeEmitter(Context &Ctx, Program &P)
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
virtual bool visitExpr(const Expr *E)=0
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
llvm::Expected< Function * > compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
bool jumpFalse(const LabelTy &Label)
LabelTy getLabel()
Create a label.
virtual bool visitDecl(const VarDecl *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:91
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Describes a memory block created by an allocation site.
Definition: Descriptor.h:77
Information about a local's storage.
Definition: Function.h:36