26 unsigned ParamOffset = 0;
28 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
37 ParamTypes.push_back(
PT_Ptr);
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);
57 ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
58 Params.insert({PD, ParamOffset});
60 ParamTypes.push_back(Ty);
68 std::move(ParamDescriptors), HasThisPointer, HasRVO);
80 return llvm::make_error<ByteCodeGenError>(*BailLocation);
82 Func->setIsFullyCompiled(
true);
89 Scopes.emplace_back(std::move(DS));
93 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
94 std::move(Scopes), FuncDecl->
hasBody());
95 Func->setIsFullyCompiled(
true);
101 NextLocalOffset +=
sizeof(
Block);
102 unsigned Location = NextLocalOffset;
104 return {Location, D};
108 const size_t Target = Code.size();
110 auto It = LabelRelocs.find(
Label);
111 if (It != LabelRelocs.end()) {
112 for (
unsigned Reloc : It->second) {
113 using namespace llvm::support;
116 void *Location = Code.data() + Reloc -
align(
sizeof(int32_t));
118 const int32_t
Offset =
Target -
static_cast<int64_t
>(Reloc);
119 endian::write<int32_t, endianness::native, 1>(Location,
Offset);
121 LabelRelocs.erase(It);
125int32_t ByteCodeEmitter::getOffset(LabelTy
Label) {
127 const int64_t Position =
132 auto It = LabelOffsets.find(
Label);
133 if (It != LabelOffsets.end()) {
134 return It->second - Position;
138 LabelRelocs[
Label].push_back(Position);
155 if constexpr (std::is_pointer_v<T>)
156 Size =
sizeof(uint32_t);
160 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
166 size_t ValPos =
align(Code.size());
168 assert(
aligned(ValPos + Size));
169 Code.resize(ValPos + Size);
171 if constexpr (!std::is_pointer_v<T>) {
172 new (Code.data() + ValPos) T(Val);
174 uint32_t ID =
P.getOrCreateNativePointer(Val);
175 new (Code.data() + ValPos) uint32_t(ID);
179template <
typename... Tys>
180bool ByteCodeEmitter::emitOp(
Opcode Op,
const Tys &... Args,
const SourceInfo &SI) {
185 emit(P, Code, Op, Success);
187 SrcMap.emplace_back(Code.size(), SI);
191 (void)std::initializer_list<int>{(
emit(P, Code, Args, Success), 0)...};
218#include "Opcodes.inc"
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.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Represents a function declaration or definition.
QualType getReturnType() const
ArrayRef< ParmVarDecl * > parameters() const
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Represents a parameter to a function.
A (possibly-)qualified type.
Encodes a location in the source.
A memory block, either on the stack or in the heap.
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.
The program contains and links the bytecode for all functions.
Function * getFunction(const FunctionDecl *F)
Returns a function.
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.
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Describes the statement/declaration an opcode was generated from.
constexpr bool aligned(uintptr_t Value)
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
PrimType
Enumeration of the primitive types of the VM.
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Describes a memory block created by an allocation site.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Information about a local's storage.