clang 22.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 (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
36 MD && isLambdaCallOperator(MD)) {
37 // Set up lambda capture to closure record field mapping.
38 const Record *R = P.getOrCreateRecord(MD->getParent());
39 assert(R);
40 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
41 FieldDecl *LTC;
42
43 MD->getParent()->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 // Register parameters with their offset.
58 unsigned ParamIndex = 0;
59 unsigned Drop = Func->hasRVO() +
60 (Func->hasThisPointer() && !Func->isThisPointerExplicit());
61 for (auto ParamOffset : llvm::drop_begin(Func->ParamOffsets, Drop)) {
62 const ParmVarDecl *PD = FuncDecl->parameters()[ParamIndex];
63 OptPrimType T = Ctx.classify(PD->getType());
64 this->Params.insert({PD, {ParamOffset, T != std::nullopt}});
65 ++ParamIndex;
66 }
67
68 Func->setDefined(true);
69
70 // Lambda static invokers are a special case that we emit custom code for.
71 bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
72 FuncDecl->isConstexpr() ||
73 FuncDecl->hasAttr<MSConstexprAttr>();
74
75 // Compile the function body.
76 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
77 Func->setIsFullyCompiled(true);
78 return;
79 }
80
81 // Create scopes from descriptors.
83 for (auto &DS : Descriptors) {
84 Scopes.emplace_back(std::move(DS));
85 }
86
87 // Set the function's code.
88 Func->setCode(FuncDecl, NextLocalOffset, std::move(Code), std::move(SrcMap),
89 std::move(Scopes), FuncDecl->hasBody());
90 Func->setIsFullyCompiled(true);
91}
92
94 NextLocalOffset += sizeof(Block);
95 unsigned Location = NextLocalOffset;
96 NextLocalOffset += align(D->getAllocSize());
97 return {Location, D};
98}
99
101 const size_t Target = Code.size();
102 LabelOffsets.insert({Label, Target});
103
104 if (auto It = LabelRelocs.find(Label); It != LabelRelocs.end()) {
105 for (unsigned Reloc : It->second) {
106 using namespace llvm::support;
107
108 // Rewrite the operand of all jumps to this label.
109 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
110 assert(aligned(Location));
111 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
112 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
113 }
114 LabelRelocs.erase(It);
115 }
116}
117
118int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
119 // Compute the PC offset which the jump is relative to.
120 const int64_t Position =
121 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
122 assert(aligned(Position));
123
124 // If target is known, compute jump offset.
125 if (auto It = LabelOffsets.find(Label); It != LabelOffsets.end())
126 return It->second - Position;
127
128 // Otherwise, record relocation and return dummy offset.
129 LabelRelocs[Label].push_back(Position);
130 return 0ull;
131}
132
133/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
134/// Pointers will be automatically marshalled as 32-bit IDs.
135template <typename T>
137 const T &Val, bool &Success) {
138 size_t ValPos = Code.size();
139 size_t Size;
140
141 if constexpr (std::is_pointer_v<T>)
142 Size = align(sizeof(uint32_t));
143 else
144 Size = align(sizeof(T));
145
146 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
147 Success = false;
148 return;
149 }
150
151 // Access must be aligned!
152 assert(aligned(ValPos));
153 assert(aligned(ValPos + Size));
154 Code.resize_for_overwrite(ValPos + Size);
155
156 if constexpr (!std::is_pointer_v<T>) {
157 new (Code.data() + ValPos) T(Val);
158 } else {
159 uint32_t ID = P.getOrCreateNativePointer(Val);
160 new (Code.data() + ValPos) uint32_t(ID);
161 }
162}
163
164/// Emits a serializable value. These usually (potentially) contain
165/// heap-allocated memory and aren't trivially copyable.
166template <typename T>
168 bool &Success) {
169 size_t ValPos = Code.size();
170 size_t Size = align(Val.bytesToSerialize());
171
172 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
173 Success = false;
174 return;
175 }
176
177 // Access must be aligned!
178 assert(aligned(ValPos));
179 assert(aligned(ValPos + Size));
180 Code.resize_for_overwrite(ValPos + Size);
181
182 Val.serialize(Code.data() + ValPos);
183}
184
185template <>
187 const Floating &Val, bool &Success) {
188 emitSerialized(Code, Val, Success);
189}
190
191template <>
193 const IntegralAP<false> &Val, bool &Success) {
194 emitSerialized(Code, Val, Success);
195}
196
197template <>
199 const IntegralAP<true> &Val, bool &Success) {
200 emitSerialized(Code, Val, Success);
201}
202
203template <>
205 const FixedPoint &Val, bool &Success) {
206 emitSerialized(Code, Val, Success);
207}
208
209template <typename... Tys>
210bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
211 const SourceInfo &SI) {
212 bool Success = true;
213
214 // The opcode is followed by arguments. The source info is
215 // attached to the address after the opcode.
216 emit(P, Code, Op, Success);
217 if (LocOverride)
218 SrcMap.emplace_back(Code.size(), *LocOverride);
219 else if (SI)
220 SrcMap.emplace_back(Code.size(), SI);
221
222 (..., emit(P, Code, Args, Success));
223 return Success;
224}
225
227 return emitJt(getOffset(Label), SourceInfo{});
228}
229
231 return emitJf(getOffset(Label), SourceInfo{});
232}
233
234bool ByteCodeEmitter::jump(const LabelTy &Label) {
235 return emitJmp(getOffset(Label), SourceInfo{});
236}
237
239 emitLabel(Label);
240 return true;
241}
242
243bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
244 const Expr *Arg = E->getArg(0);
245 PrimType T = Ctx.classify(Arg->getType()).value_or(PT_Ptr);
246 if (!this->emitBCP(getOffset(EndLabel), T, E))
247 return false;
248 if (!this->visit(Arg))
249 return false;
250 return true;
251}
252
253//===----------------------------------------------------------------------===//
254// Opcode emitters
255//===----------------------------------------------------------------------===//
256
257#define GET_LINK_IMPL
258#include "Opcodes.inc"
259#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....
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2879
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3083
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:3157
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3393
Represents a function declaration or definition.
Definition Decl.h:1999
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2313
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2771
param_iterator param_begin()
Definition Decl.h:2783
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition Decl.h:2469
bool param_empty() const
Definition Decl.h:2782
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3191
Represents a parameter to a function.
Definition Decl.h:1789
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isPointerOrReferenceType() const
Definition TypeBase.h:8526
QualType getType() const
Definition Decl.h:722
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:86
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(const FieldDecl *FD) const
Returns a field.
Definition Record.cpp:47
Describes the statement/declaration an opcode was generated from.
Definition Source.h:73
constexpr bool aligned(uintptr_t Value)
Definition PrimType.h:189
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:185
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
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
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