15 using namespace clang;
27 unsigned ParamOffset = 0;
29 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
35 ParamTypes.push_back(
PT_Ptr);
50 ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
51 Params.insert({PD, ParamOffset});
53 ParamTypes.push_back(Ty);
58 std::move(ParamDescriptors));
63 return llvm::make_error<ByteCodeGenError>(*BailLocation);
70 Scopes.emplace_back(std::move(DS));
74 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
81 NextLocalOffset +=
sizeof(
Block);
82 unsigned Location = NextLocalOffset;
88 const size_t Target = Code.size();
89 LabelOffsets.insert({
Label, Target});
90 auto It = LabelRelocs.find(Label);
91 if (It != LabelRelocs.end()) {
92 for (
unsigned Reloc : It->second) {
93 using namespace llvm::support;
96 void *Location = Code.data() + Reloc -
sizeof(int32_t);
97 const int32_t
Offset = Target -
static_cast<int64_t
>(Reloc);
98 endian::write<int32_t, endianness::native, 1>(Location,
Offset);
100 LabelRelocs.erase(It);
106 const int64_t Position = Code.size() +
sizeof(
Opcode) +
sizeof(int32_t);
109 auto It = LabelOffsets.find(Label);
110 if (It != LabelOffsets.end()) {
111 return It->second - Position;
115 LabelRelocs[
Label].push_back(Position);
125 template <
typename... Tys>
126 bool ByteCodeEmitter::emitOp(
Opcode Op,
const Tys &... Args,
const SourceInfo &SI) {
130 auto emit = [
this, &Success](
const char *Data,
size_t Size) {
135 Code.insert(Code.end(), Data, Data + Size);
140 emit(reinterpret_cast<const char *>(&Op),
sizeof(
Opcode));
142 SrcMap.emplace_back(Code.size(), SI);
146 (void)std::initializer_list<int>{
147 (emit(reinterpret_cast<const char *>(&Args),
sizeof(Args)), 0)...};
153 return emitJt(getOffset(Label),
SourceInfo{});
157 return emitJf(getOffset(Label),
SourceInfo{});
161 return emitJmp(getOffset(Label),
SourceInfo{});
173 #define GET_LINK_IMPL 174 #include "Opcodes.inc" Represents a function declaration or definition.
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
A (possibly-)qualified type.
bool willHaveBody() const
True if this function will eventually have a body, once it's fully parsed.
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
bool isDefined(const FunctionDecl *&Definition) const
Returns true if the function has a definition that does not need to be instantiated.
QualType getReturnType() const
Describes the statement/declaration an opcode was generated from.
Represents a parameter to a function.
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
llvm::DenseMap< const ParmVarDecl *, unsigned > Params
Parameter indices.
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
A memory block, either on the stack or in the heap.
__DEVICE__ int max(int __a, int __b)
llvm::Expected< Function * > compileFunc(const FunctionDecl *F)
Compiles the function into the module.
ArrayRef< ParmVarDecl * > parameters() const
PrimType
Enumeration of the primitive types of the VM.
bool fallthrough(const LabelTy &Label)
Describes a memory block created by an allocation site.
bool jumpFalse(const LabelTy &Label)
Information about a local's storage.
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
bool jump(const LabelTy &Label)
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Local createLocal(Descriptor *D)
Callback for local registration.
Encodes a location in the source.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
void emitLabel(LabelTy Label)
Define a label.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
Dataflow Directional Tag Classes.
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).