clang: lib/AST/ByteCode/Program.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_CLANG_AST_INTERP_PROGRAM_H

14#define LLVM_CLANG_AST_INTERP_PROGRAM_H

15

21#include "llvm/ADT/DenseMap.h"

22#include "llvm/ADT/PointerUnion.h"

23#include "llvm/ADT/StringRef.h"

24#include "llvm/Support/Allocator.h"

25#include

26#include

27

29class RecordDecl;

30class Expr;

31class FunctionDecl;

32class StringLiteral;

33class VarDecl;

34

35namespace interp {

36class Context;

37

38

40public:

42

44

45

46

47 for (Global *G : Globals)

49 B->invokeDtor();

50

51

52

53

54 for (auto RecordPair : Records) {

55 if (Record *R = RecordPair.second)

56 R->~Record();

57 }

58 }

59

60

62

63

65

66

69

70

72

73

75 assert(Idx < Globals.size());

76 return Globals[Idx]->block();

77 }

78

79

82

83

86

87

89

90

92

93

95

96

97 template <typename... Ts>

100 auto *Func = new Function(*this, Def, std::forward(Args)...);

101 Funcs.insert({Def, std::unique_ptr(Func)});

103 }

104

106 auto *Func = new Function(*this, std::forward(Args)...);

107 AnonFuncs.emplace_back(Func);

109 }

110

111

113

114

116

117

120 bool IsConst = false, bool IsTemporary = false,

121 bool IsMutable = false) {

122 return allocateDescriptor(D, Type, MDSize, IsConst, IsTemporary, IsMutable);

123 }

124

125

128 bool IsConst = false, bool IsTemporary = false,

129 bool IsMutable = false,

131

132

134 public:

136 P.startDeclaration(VD);

137 }

139

140 private:

142 };

143

144

146 if (CurrentDeclaration == NoDeclaration)

147 return std::optional{};

148 return LastDeclaration;

149 }

150

151private:

153

155 bool IsStatic, bool IsExtern,

156 bool IsWeak, const Expr *Init = nullptr);

157

158

160

161 llvm::DenseMap<const FunctionDecl *, std::unique_ptr> Funcs;

162

163 std::vector<std::unique_ptr> AnonFuncs;

164

165

166 llvm::DenseMap<const FunctionDecl *, std::vector> Relocs;

167

168

169 std::vector<const void *> NativePointers;

170

171 llvm::DenseMap<const void *, unsigned> NativePointerIndices;

172

173

174 using PoolAllocTy = llvm::BumpPtrAllocator;

175

176

177

178

180 public:

181

182 template <typename... Tys>

184

185

186 void *operator new(size_t Meta, PoolAllocTy &Alloc, size_t Data) {

187 return Alloc.Allocate(Meta + Data, alignof(void *));

188 }

189

190

191 std::byte *data() { return B.data(); }

192

193 Block *block() { return &B; }

194 const Block *block() const { return &B; }

195

196 private:

197

199 };

200

201

202 PoolAllocTy Allocator;

203

204

205 std::vector<Global *> Globals;

206

207 llvm::DenseMap<const void *, unsigned> GlobalIndices;

208

209

210 llvm::DenseMap<const RecordDecl *, Record *> Records;

211

212

213 llvm::DenseMap<const void *, unsigned> DummyVariables;

214

215

216 template <typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) {

217 return new (Allocator) Descriptor(std::forward(Args)...);

218 }

219

220

221 static constexpr unsigned NoDeclaration = (unsigned)-1;

222

223 unsigned LastDeclaration = 0;

224

225 unsigned CurrentDeclaration = NoDeclaration;

226

227

228 void startDeclaration(const ValueDecl *Decl) {

229 LastDeclaration += 1;

230 CurrentDeclaration = LastDeclaration;

231 }

232

233

234 void endDeclaration() { CurrentDeclaration = NoDeclaration; }

235

236public:

237

238 void dump() const;

239 void dump(llvm::raw_ostream &OS) const;

240};

241

242}

243}

244

245#endif

llvm::MachO::Records Records

This represents one expression.

Represents a function declaration or definition.

FunctionDecl * getCanonicalDecl() override

Retrieves the "canonical" declaration of the given declaration.

A (possibly-)qualified type.

Represents a struct/union/class.

StringLiteral - This represents a string literal expression, e.g.

The base class of the type hierarchy.

Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...

A memory block, either on the stack or in the heap.

bool isInitialized() const

Returns whether the data of this block has been initialized via invoking the Ctor func.

Holds all information required to evaluate constexpr code in a module.

A pointer to a memory block, live or dead.

Context to manage declaration lifetimes.

DeclScope(Program &P, const ValueDecl *VD)

The program contains and links the bytecode for all functions.

std::optional< unsigned > getOrCreateGlobal(const ValueDecl *VD, const Expr *Init=nullptr)

Returns or creates a global an creates an index to it.

Function * getFunction(const FunctionDecl *F)

Returns a function.

Block * getGlobal(unsigned Idx)

Returns the value of a global.

std::optional< unsigned > createGlobal(const ValueDecl *VD, const Expr *Init)

Creates a global and returns its index.

const void * getNativePointer(unsigned Idx)

Returns the value of a marshalled native pointer.

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.

unsigned getOrCreateNativePointer(const void *Ptr)

Marshals a native pointer to an ID for embedding in bytecode.

Function * createFunction(const FunctionDecl *Def, Ts &&...Args)

Creates a new function from a code range.

Pointer getPtrGlobal(unsigned Idx) const

Returns a pointer to a global.

unsigned getOrCreateDummy(const DeclTy &D)

Returns or creates a dummy value for unknown declarations.

void dump() const

Dumps the disassembled bytecode to llvm::errs().

std::optional< unsigned > getCurrentDecl() const

Returns the current declaration ID.

unsigned createGlobalString(const StringLiteral *S, const Expr *Base=nullptr)

Emits a string literal among global data.

Function * createFunction(Ts &&...Args)

Creates an anonymous function.

Record * getOrCreateRecord(const RecordDecl *RD)

Returns a record or creates one if it does not exist.

Structure/Class descriptor.

PrimType

Enumeration of the primitive types of the VM.

bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)

llvm::PointerUnion< const Decl *, const Expr * > DeclTy

The JSON file list parser is used to communicate input to InstallAPI.

Describes a memory block created by an allocation site.

std::optional< unsigned > MetadataSize