clang 23.0.0git
ByteCodeEmitter.cpp
Go to the documentation of this file.
1//===--- ByteCodeEmitter.cpp - 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#include "ByteCodeEmitter.h"
10#include "Context.h"
11#include "Floating.h"
12#include "IntegralAP.h"
13#include "Opcode.h"
14#include "Program.h"
15#include "clang/AST/ASTLambda.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include <type_traits>
19
20using namespace clang;
21using namespace clang::interp;
22
24 Function *Func) {
25 assert(FuncDecl);
26 assert(Func);
27 assert(FuncDecl->isThisDeclarationADefinition());
28
29 // Manually created functions that haven't been assigned proper
30 // parameters yet.
31 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
32 return;
33
34 // Set up lambda captures.
35 if (Func->isLambdaCallOperator()) {
36 // Set up lambda capture to closure record field mapping.
37 const CXXRecordDecl *ParentDecl = Func->getParentDecl();
38 const Record *R = P.getOrCreateRecord(ParentDecl);
39 assert(R);
40 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
41 FieldDecl *LTC;
42
43 ParentDecl->getCaptureFields(LC, LTC);
44
45 for (auto Cap : LC) {
46 unsigned Offset = R->getField(Cap.second)->Offset;
47 this->LambdaCaptures[Cap.first] = {
48 Offset, Cap.second->getType()->isReferenceType()};
49 }
50 if (LTC) {
51 QualType CaptureType = R->getField(LTC)->Decl->getType();
52 this->LambdaThisCapture = {R->getField(LTC)->Offset,
53 CaptureType->isPointerOrReferenceType()};
54 }
55 }
56
57 bool IsValid = !FuncDecl->isInvalidDecl();
58 // Register parameters with their offset.
59 unsigned ParamIndex = 0;
60 unsigned Drop = Func->hasRVO() +
61 (Func->hasThisPointer() && !Func->isThisPointerExplicit());
62
63 for (const auto &ParamDesc : llvm::drop_begin(Func->ParamDescriptors, Drop)) {
64 const ParmVarDecl *PD = FuncDecl->getParamDecl(ParamIndex);
65 if (PD->isInvalidDecl())
66 IsValid = false;
67 this->Params.insert(
68 {PD, {ParamDesc.Offset, Ctx.canClassify(PD->getType())}});
69 ++ParamIndex;
70 }
71
72 Func->setDefined(true);
73
74 // Lambda static invokers are a special case that we emit custom code for.
75 bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
76 FuncDecl->isConstexpr() ||
77 FuncDecl->hasAttr<MSConstexprAttr>();
78
79 // Compile the function body.
80 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
81 Func->setIsFullyCompiled(true);
82 return;
83 }
84
85 // Create scopes from descriptors.
87 for (auto &DS : Descriptors) {
88 Scopes.emplace_back(std::move(DS));
89 }
90
91 // Set the function's code.
92 Func->setCode(FuncDecl, NextLocalOffset, std::move(Code), std::move(SrcMap),
93 std::move(Scopes), FuncDecl->hasBody(), IsValid);
94 Func->setIsFullyCompiled(true);
95}
96
98 NextLocalOffset += sizeof(Block);
99 unsigned Location = NextLocalOffset;
100 NextLocalOffset += align(D->getAllocSize());
101 return {Location, D};
102}
103
105 const size_t Target = Code.size();
106 LabelOffsets.insert({Label, Target});
107
108 if (auto It = LabelRelocs.find(Label); It != LabelRelocs.end()) {
109 for (unsigned Reloc : It->second) {
110 using namespace llvm::support;
111
112 // Rewrite the operand of all jumps to this label.
113 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
114 assert(aligned(Location));
115 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
116 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
117 }
118 LabelRelocs.erase(It);
119 }
120}
121
122int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
123 // Compute the PC offset which the jump is relative to.
124 const int64_t Position =
125 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
126 assert(aligned(Position));
127
128 // If target is known, compute jump offset.
129 if (auto It = LabelOffsets.find(Label); It != LabelOffsets.end())
130 return It->second - Position;
131
132 // Otherwise, record relocation and return dummy offset.
133 LabelRelocs[Label].push_back(Position);
134 return 0ull;
135}
136
137/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
138/// Pointers will be automatically marshalled as 32-bit IDs.
139template <typename T>
141 const T &Val, bool &Success) {
142 size_t ValPos = Code.size();
143 size_t Size;
144
145 if constexpr (std::is_pointer_v<T>)
146 Size = align(sizeof(uint32_t));
147 else
148 Size = align(sizeof(T));
149
150 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
151 Success = false;
152 return;
153 }
154
155 // Access must be aligned!
156 assert(aligned(ValPos));
157 assert(aligned(ValPos + Size));
158 Code.resize_for_overwrite(ValPos + Size);
159
160 if constexpr (!std::is_pointer_v<T>) {
161 new (Code.data() + ValPos) T(Val);
162 } else {
163 uint32_t ID = P.getOrCreateNativePointer(Val);
164 new (Code.data() + ValPos) uint32_t(ID);
165 }
166}
167
168/// Emits a serializable value. These usually (potentially) contain
169/// heap-allocated memory and aren't trivially copyable.
170template <typename T>
172 bool &Success) {
173 size_t ValPos = Code.size();
174 size_t Size = align(Val.bytesToSerialize());
175
176 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
177 Success = false;
178 return;
179 }
180
181 // Access must be aligned!
182 assert(aligned(ValPos));
183 assert(aligned(ValPos + Size));
184 Code.resize_for_overwrite(ValPos + Size);
185
186 Val.serialize(Code.data() + ValPos);
187}
188
189template <>
191 const Floating &Val, bool &Success) {
192 emitSerialized(Code, Val, Success);
193}
194
195template <>
197 const IntegralAP<false> &Val, bool &Success) {
198 emitSerialized(Code, Val, Success);
199}
200
201template <>
203 const IntegralAP<true> &Val, bool &Success) {
204 emitSerialized(Code, Val, Success);
205}
206
207template <>
209 const FixedPoint &Val, bool &Success) {
210 emitSerialized(Code, Val, Success);
211}
212
213template <typename... Tys>
214bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
215 bool Success = true;
216
217 // The opcode is followed by arguments. The source info is
218 // attached to the address after the opcode.
219 emit(P, Code, Op, Success);
220 if (LocOverride)
221 SrcMap.emplace_back(Code.size(), *LocOverride);
222 else if (SI)
223 SrcMap.emplace_back(Code.size(), SI);
224
225 (..., emit(P, Code, Args, Success));
226 return Success;
227}
228
230 return emitJt(getOffset(Label), SourceInfo{});
231}
232
234 return emitJf(getOffset(Label), SourceInfo{});
235}
236
237bool ByteCodeEmitter::jump(const LabelTy &Label) {
238 return emitJmp(getOffset(Label), SourceInfo{});
239}
240
242 emitLabel(Label);
243 return true;
244}
245
246bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
247 const Expr *Arg = E->getArg(0);
248 PrimType T = Ctx.classify(Arg->getType()).value_or(PT_Ptr);
249 if (!this->emitBCP(getOffset(EndLabel), T, E))
250 return false;
251 if (!this->visit(Arg))
252 return false;
253 return true;
254}
255
256//===----------------------------------------------------------------------===//
257// Opcode emitters
258//===----------------------------------------------------------------------===//
259
260#define GET_LINK_IMPL
261#include "Opcodes.inc"
262#undef GET_LINK_IMPL
This file provides some common utility functions for processing Lambda related AST Constructs.
static void emitSerialized(llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Emits a serializable value.
static void emit(Program &P, llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
void getCaptureFields(llvm::DenseMap< const ValueDecl *, FieldDecl * > &Captures, FieldDecl *&ThisCapture) const
For a closure type, retrieve the mapping from captured variables and this to the non-static data memb...
Definition DeclCXX.cpp:1784
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3147
bool isInvalidDecl() const
Definition DeclBase.h:588
bool hasAttr() const
Definition DeclBase.h:577
This represents one expression.
Definition Expr.h:112
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3160
Represents a function declaration or definition.
Definition Decl.h:2000
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2797
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2314
param_iterator param_begin()
Definition Decl.h:2786
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition Decl.h:2470
bool param_empty() const
Definition Decl.h:2785
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3199
Represents a parameter to a function.
Definition Decl.h:1790
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isPointerOrReferenceType() const
Definition TypeBase.h:8543
QualType getType() const
Definition Decl.h:723
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
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 visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
std::optional< SourceInfo > LocOverride
bool jumpFalse(const LabelTy &Label)
virtual bool visit(const Expr *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Wrapper around fixed point types.
Definition FixedPoint.h:23
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
Bytecode function.
Definition Function.h:99
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition IntegralAP.h:36
The program contains and links the bytecode for all functions.
Definition Program.h:36
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
Definition Program.cpp:21
Structure/Class descriptor.
Definition Record.h:25
const Field * getField(unsigned I) const
Definition Record.h:81
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
constexpr bool aligned(uintptr_t Value)
Definition PrimType.h:193
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:189
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
The JSON file list parser is used to communicate input to InstallAPI.
@ Success
Annotation was successful.
Definition Parser.h:65
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:121
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:242
const FieldDecl * Decl
Definition Record.h:29
Information about a local's storage.
Definition Function.h:39