clang 23.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
21namespace clang {
22namespace interp {
23enum Opcode : uint32_t;
24
25/// An emitter which links the program to bytecode for later use.
27protected:
30
31public:
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 visitWithSubstitutions(const FunctionDecl *Callee,
55 const Expr *This,
56 const Expr *Condition) = 0;
57 virtual bool visit(const Expr *E) = 0;
58 virtual bool emitBool(bool V, const Expr *E) = 0;
59
60 /// Emits jumps.
61 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
62 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
63 bool jump(const LabelTy &Label, SourceInfo SI);
64 bool fallthrough(const LabelTy &Label);
65 /// Speculative execution.
66 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
67
68 /// We're always emitting bytecode.
69 bool isActive() const { return true; }
70 bool checkingForUndefinedBehavior() const { return false; }
71
72 /// Callback for local registration.
74
75 /// Parameter indices.
76 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
77 /// Lambda captures.
78 llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;
79 /// Offset of the This parameter in a lambda record.
81 /// Local descriptors.
83 std::optional<SourceInfo> LocOverride = std::nullopt;
84
85private:
86 /// Current compilation context.
87 Context &Ctx;
88 /// Program to link to.
89 Program &P;
90 /// Index of the next available label.
91 LabelTy NextLabel = 0;
92 /// Offset of the next local variable.
93 unsigned NextLocalOffset = 0;
94 /// Label information for linker.
95 llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
96 /// Location of label relocations.
97 llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
98 /// Program code.
100 /// Opcode to expression mapping.
101 SourceMap SrcMap;
102
103 /// Returns the offset for a jump or records a relocation.
104 int32_t getOffset(LabelTy Label);
105
106 /// Emits an opcode.
107 template <typename... Tys>
108 bool emitOp(Opcode Op, const Tys &...Args, SourceInfo L);
109
110protected:
111#define GET_LINK_PROTO
112#include "Opcodes.inc"
113#undef GET_LINK_PROTO
114};
115
116} // namespace interp
117} // namespace clang
118
119#endif
#define V(N, I)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2027
Represents a variable declaration or definition.
Definition Decl.h:932
bool jump(const LabelTy &Label, SourceInfo SI)
virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope)=0
void emitLabel(LabelTy Label)
Define a label.
virtual bool visitDtorCall(const VarDecl *VD, const APValue &)=0
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
ByteCodeEmitter(Context &Ctx, Program &P)
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool jumpTrue(const LabelTy &Label, SourceInfo SI)
Emits jumps.
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
void compileFunc(const FunctionDecl *FuncDecl, Function *Func=nullptr)
Compiles the function into the module.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
bool jumpFalse(const LabelTy &Label, SourceInfo SI)
virtual bool visitWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)=0
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext)=0
virtual bool emitBool(bool V, const Expr *E)=0
std::optional< SourceInfo > LocOverride
LabelTy getLabel()
Create a label.
llvm::DenseMap< const ParmVarDecl *, FuncParam > Params
Parameter indices.
bool isActive() const
We're always emitting bytecode.
virtual bool visit(const Expr *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:47
Bytecode function.
Definition Function.h:99
The program contains and links the bytecode for all functions.
Definition Program.h:36
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3150
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition Source.h:100
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2391
The JSON file list parser is used to communicate input to InstallAPI.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
Information about a local's storage.
Definition Function.h:39