Go to the documentation of this file.
14 #include <type_traits>
16 using namespace clang;
28 unsigned ParamOffset = 0;
30 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
36 ParamTypes.push_back(
PT_Ptr);
51 ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
52 Params.insert({PD, ParamOffset});
54 ParamTypes.push_back(Ty);
59 std::move(ParamDescriptors));
64 return llvm::make_error<ByteCodeGenError>(*BailLocation);
71 Scopes.emplace_back(std::move(DS));
75 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
82 NextLocalOffset +=
sizeof(
Block);
83 unsigned Location = NextLocalOffset;
89 const size_t Target = Code.size();
91 auto It = LabelRelocs.find(
Label);
92 if (It != LabelRelocs.end()) {
93 for (
unsigned Reloc : It->second) {
94 using namespace llvm::support;
97 void *Location = Code.data() + Reloc -
sizeof(int32_t);
98 const int32_t
Offset =
Target -
static_cast<int64_t
>(Reloc);
99 endian::write<int32_t, endianness::native, 1>(Location,
Offset);
101 LabelRelocs.erase(It);
107 const int64_t Position = Code.size() +
sizeof(
Opcode) +
sizeof(int32_t);
110 auto It = LabelOffsets.find(
Label);
111 if (It != LabelOffsets.end()) {
112 return It->second - Position;
116 LabelRelocs[
Label].push_back(Position);
128 template <
typename T>
129 static std::enable_if_t<!std::is_pointer<T>::value,
void>
130 emit(
Program &P, std::vector<char> &Code,
const T &Val,
bool &Success) {
131 size_t Size =
sizeof(Val);
137 const char *Data =
reinterpret_cast<const char *
>(&Val);
138 Code.insert(Code.end(), Data, Data + Size);
141 template <
typename T>
142 static std::enable_if_t<std::is_pointer<T>::value,
void>
143 emit(
Program &P, std::vector<char> &Code,
const T &Val,
bool &Success) {
144 size_t Size =
sizeof(uint32_t);
150 uint32_t
ID =
P.getOrCreateNativePointer(Val);
151 const char *Data =
reinterpret_cast<const char *
>(&
ID);
152 Code.insert(Code.end(), Data, Data + Size);
155 template <
typename... Tys>
156 bool ByteCodeEmitter::emitOp(
Opcode Op,
const Tys &... Args,
const SourceInfo &SI) {
161 emit(P, Code, Op, Success);
163 SrcMap.emplace_back(Code.size(), SI);
167 (void)std::initializer_list<int>{(
emit(P, Code, Args, Success), 0)...};
193 #define GET_LINK_IMPL
194 #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.
bool willHaveBody() const
True if this function will eventually have a body, once it's fully parsed.
Represents a parameter to a function.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
static std::enable_if_t<!std::is_pointer< T >::value, 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.
Information about a local's storage.
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
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::Expected< Function * > compileFunc(const FunctionDecl *F)
Compiles the function into the module.
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
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)
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
Represents a function declaration or definition.
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.