Go to the documentation of this file.
15 #include <type_traits>
17 using namespace clang;
28 bool HasBody = FuncDecl->
hasBody(FuncDecl);
34 unsigned ParamOffset = 0;
36 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
45 ParamTypes.push_back(
PT_Ptr);
52 bool HasThisPointer =
false;
53 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
54 MD && MD->isInstance()) {
55 HasThisPointer =
true;
56 ParamTypes.push_back(
PT_Ptr);
65 ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
66 Params.insert({PD, ParamOffset});
68 ParamTypes.push_back(Ty);
73 std::move(ParamDescriptors), HasThisPointer, HasRVO);
84 return llvm::make_error<ByteCodeGenError>(*BailLocation);
86 Func->setIsFullyCompiled(
true);
93 Scopes.emplace_back(std::move(DS));
97 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
99 Func->setIsFullyCompiled(
true);
105 NextLocalOffset +=
sizeof(
Block);
106 unsigned Location = NextLocalOffset;
108 return {Location, D};
112 const size_t Target = Code.size();
114 auto It = LabelRelocs.find(
Label);
115 if (It != LabelRelocs.end()) {
116 for (
unsigned Reloc : It->second) {
117 using namespace llvm::support;
120 void *Location = Code.data() + Reloc -
align(
sizeof(int32_t));
123 endian::write<int32_t, endianness::native, 1>(Location,
Offset);
125 LabelRelocs.erase(It);
136 auto It = LabelOffsets.find(
Label);
137 if (It != LabelOffsets.end()) {
138 return It->second - Position;
142 LabelRelocs[
Label].push_back(Position);
154 template <
typename T>
155 static void emit(
Program &P, std::vector<char> &Code,
const T &Val,
159 if constexpr (std::is_pointer_v<T>)
160 Size =
sizeof(uint32_t);
170 size_t ValPos =
align(Code.size());
172 assert(
aligned(ValPos + Size));
173 Code.resize(ValPos + Size);
175 if constexpr (!std::is_pointer_v<T>) {
176 new (Code.data() + ValPos) T(Val);
178 uint32_t
ID =
P.getOrCreateNativePointer(Val);
179 new (Code.data() + ValPos) uint32_t(
ID);
183 template <
typename... Tys>
184 bool ByteCodeEmitter::emitOp(
Opcode Op,
const Tys &... Args,
const SourceInfo &SI) {
189 emit(P, Code, Op, Success);
191 SrcMap.emplace_back(Code.size(), SI);
195 (void)std::initializer_list<int>{(
emit(P, Code, Args, Success), 0)...};
221 #define GET_LINK_IMPL
222 #include "Opcodes.inc"
__DEVICE__ int max(int __a, int __b)
QualType getReturnType() const
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Encodes a location in the source.
Local createLocal(Descriptor *D)
Callback for local registration.
void emitLabel(LabelTy Label)
Define a label.
A (possibly-)qualified type.
Represents a parameter to a function.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Function * getFunction(const FunctionDecl *F)
Returns a function.
Information about a local's storage.
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
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.
A memory block, either on the stack or in the heap.
PrimType
Enumeration of the primitive types of the VM.
Describes the statement/declaration an opcode was generated from.
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
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.
bool fallthrough(const LabelTy &Label)
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Describes a memory block created by an allocation site.
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
bool jumpFalse(const LabelTy &Label)
llvm::Expected< Function * > compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
bool jump(const LabelTy &Label)
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
The program contains and links the bytecode for all functions.
ArrayRef< ParmVarDecl * > parameters() const
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Represents a function declaration or definition.
constexpr bool aligned(uintptr_t Value)