clang 17.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 "Opcode.h"
13#include "Program.h"
14#include "clang/AST/DeclCXX.h"
15#include <type_traits>
16
17using namespace clang;
18using namespace clang::interp;
19
20using APSInt = llvm::APSInt;
21using Error = llvm::Error;
22
25 // Set up argument indices.
26 unsigned ParamOffset = 0;
27 SmallVector<PrimType, 8> ParamTypes;
28 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
29
30 // If the return is not a primitive, a pointer to the storage where the
31 // value is initialized in is passed as the first argument. See 'RVO'
32 // elsewhere in the code.
33 QualType Ty = FuncDecl->getReturnType();
34 bool HasRVO = false;
35 if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
36 HasRVO = true;
37 ParamTypes.push_back(PT_Ptr);
38 ParamOffset += align(primSize(PT_Ptr));
39 }
40
41 // If the function decl is a member decl, the next parameter is
42 // the 'this' pointer. This parameter is pop()ed from the
43 // InterpStack when calling the function.
44 bool HasThisPointer = false;
45 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
46 MD && MD->isInstance()) {
47 HasThisPointer = true;
48 ParamTypes.push_back(PT_Ptr);
49 ParamOffset += align(primSize(PT_Ptr));
50 }
51
52 // Assign descriptors to all parameters.
53 // Composite objects are lowered to pointers.
54 for (const ParmVarDecl *PD : FuncDecl->parameters()) {
55 PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
56 Descriptor *Desc = P.createDescriptor(PD, Ty);
57 ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
58 Params.insert({PD, ParamOffset});
59 ParamOffset += align(primSize(Ty));
60 ParamTypes.push_back(Ty);
61 }
62
63 // Create a handle over the emitted code.
64 Function *Func = P.getFunction(FuncDecl);
65 if (!Func)
66 Func =
67 P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
68 std::move(ParamDescriptors), HasThisPointer, HasRVO);
69
70 assert(Func);
71 // For not-yet-defined functions, we only create a Function instance and
72 // compile their body later.
73 if (!FuncDecl->isDefined())
74 return Func;
75
76 // Compile the function body.
77 if (!FuncDecl->isConstexpr() || !visitFunc(FuncDecl)) {
78 // Return a dummy function if compilation failed.
79 if (BailLocation)
80 return llvm::make_error<ByteCodeGenError>(*BailLocation);
81 else {
82 Func->setIsFullyCompiled(true);
83 return Func;
84 }
85 } else {
86 // Create scopes from descriptors.
88 for (auto &DS : Descriptors) {
89 Scopes.emplace_back(std::move(DS));
90 }
91
92 // Set the function's code.
93 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
94 std::move(Scopes), FuncDecl->hasBody());
95 Func->setIsFullyCompiled(true);
96 return Func;
97 }
98}
99
101 NextLocalOffset += sizeof(Block);
102 unsigned Location = NextLocalOffset;
103 NextLocalOffset += align(D->getAllocSize());
104 return {Location, D};
105}
106
108 const size_t Target = Code.size();
109 LabelOffsets.insert({Label, Target});
110 auto It = LabelRelocs.find(Label);
111 if (It != LabelRelocs.end()) {
112 for (unsigned Reloc : It->second) {
113 using namespace llvm::support;
114
115 /// Rewrite the operand of all jumps to this label.
116 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
117 assert(aligned(Location));
118 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
119 endian::write<int32_t, endianness::native, 1>(Location, Offset);
120 }
121 LabelRelocs.erase(It);
122 }
123}
124
125int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
126 // Compute the PC offset which the jump is relative to.
127 const int64_t Position =
128 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
129 assert(aligned(Position));
130
131 // If target is known, compute jump offset.
132 auto It = LabelOffsets.find(Label);
133 if (It != LabelOffsets.end()) {
134 return It->second - Position;
135 }
136
137 // Otherwise, record relocation and return dummy offset.
138 LabelRelocs[Label].push_back(Position);
139 return 0ull;
140}
141
143 if (!BailLocation)
144 BailLocation = Loc;
145 return false;
146}
147
148/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
149/// Pointers will be automatically marshalled as 32-bit IDs.
150template <typename T>
151static void emit(Program &P, std::vector<char> &Code, const T &Val,
152 bool &Success) {
153 size_t Size;
154
155 if constexpr (std::is_pointer_v<T>)
156 Size = sizeof(uint32_t);
157 else
158 Size = sizeof(T);
159
160 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
161 Success = false;
162 return;
163 }
164
165 // Access must be aligned!
166 size_t ValPos = align(Code.size());
167 Size = align(Size);
168 assert(aligned(ValPos + Size));
169 Code.resize(ValPos + Size);
170
171 if constexpr (!std::is_pointer_v<T>) {
172 new (Code.data() + ValPos) T(Val);
173 } else {
174 uint32_t ID = P.getOrCreateNativePointer(Val);
175 new (Code.data() + ValPos) uint32_t(ID);
176 }
177}
178
179template <typename... Tys>
180bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
181 bool Success = true;
182
183 /// The opcode is followed by arguments. The source info is
184 /// attached to the address after the opcode.
185 emit(P, Code, Op, Success);
186 if (SI)
187 SrcMap.emplace_back(Code.size(), SI);
188
189 /// The initializer list forces the expression to be evaluated
190 /// for each argument in the variadic template, in order.
191 (void)std::initializer_list<int>{(emit(P, Code, Args, Success), 0)...};
192
193 return Success;
194}
195
197 return emitJt(getOffset(Label), SourceInfo{});
198}
199
201 return emitJf(getOffset(Label), SourceInfo{});
202}
203
205 return emitJmp(getOffset(Label), SourceInfo{});
206}
207
210 return true;
211}
212
213//===----------------------------------------------------------------------===//
214// Opcode emitters
215//===----------------------------------------------------------------------===//
216
217#define GET_LINK_IMPL
218#include "Opcodes.inc"
219#undef GET_LINK_IMPL
StringRef P
static void emit(Program &P, std::vector< char > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
llvm::Error Error
llvm::APSInt APSInt
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
unsigned Offset
Definition: Format.cpp:2797
std::string Label
Represents a function declaration or definition.
Definition: Decl.h:1917
QualType getReturnType() const
Definition: Decl.h:2640
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2586
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2365
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3058
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition: Decl.cpp:3105
Represents a parameter to a function.
Definition: Decl.h:1722
A (possibly-)qualified type.
Definition: Type.h:736
Encodes a location in the source.
bool isVoidType() const
Definition: Type.h:7224
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
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.
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)
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:80
Bytecode function.
Definition: Function.h:74
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:211
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
Definition: Program.h:117
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Definition: Program.h:96
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:70
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:72
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:68
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:30
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:21
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:164
Information about a local's storage.
Definition: Function.h:35