clang 17.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 "ByteCodeGenError.h"
17#include "Context.h"
18#include "InterpStack.h"
19#include "InterpState.h"
20#include "PrimType.h"
21#include "Program.h"
22#include "Source.h"
23#include "llvm/Support/Error.h"
24
25namespace clang {
26namespace interp {
27class Context;
28class SourceInfo;
29enum Opcode : uint32_t;
30
31/// An emitter which links the program to bytecode for later use.
33protected:
34 using LabelTy = uint32_t;
37
38public:
39 /// Compiles the function into the module.
41
42protected:
43 ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
44
45 virtual ~ByteCodeEmitter() {}
46
47 /// Define a label.
49 /// Create a label.
50 LabelTy getLabel() { return ++NextLabel; }
51
52 /// Methods implemented by the compiler.
53 virtual bool visitFunc(const FunctionDecl *E) = 0;
54 virtual bool visitExpr(const Expr *E) = 0;
55 virtual bool visitDecl(const VarDecl *E) = 0;
56
57 /// Bails out if a given node cannot be compiled.
58 bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
59 bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
60 bool bail(const SourceLocation &Loc);
61
62 /// Emits jumps.
63 bool jumpTrue(const LabelTy &Label);
64 bool jumpFalse(const LabelTy &Label);
65 bool jump(const LabelTy &Label);
66 bool fallthrough(const LabelTy &Label);
67
68 /// Callback for local registration.
70
71 /// Parameter indices.
72 llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
73 /// Local descriptors.
75
76private:
77 /// Current compilation context.
78 Context &Ctx;
79 /// Program to link to.
80 Program &P;
81 /// Index of the next available label.
82 LabelTy NextLabel = 0;
83 /// Offset of the next local variable.
84 unsigned NextLocalOffset = 0;
85 /// Location of a failure.
86 std::optional<SourceLocation> BailLocation;
87 /// Label information for linker.
88 llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
89 /// Location of label relocations.
90 llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
91 /// Program code.
92 std::vector<char> Code;
93 /// Opcode to expression mapping.
94 SourceMap SrcMap;
95
96 /// Returns the offset for a jump or records a relocation.
97 int32_t getOffset(LabelTy Label);
98
99 /// Emits an opcode.
100 template <typename... Tys>
101 bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
102
103protected:
104#define GET_LINK_PROTO
105#include "Opcodes.inc"
106#undef GET_LINK_PROTO
107};
108
109} // namespace interp
110} // namespace clang
111
112#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:1917
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:72
Represents a variable declaration or definition.
Definition: Decl.h:913
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 *, unsigned > Params
Parameter indices.
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:35
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:70
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:88
__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:76
Information about a local's storage.
Definition: Function.h:35