LLVM  7.0.0svn
WasmYAML.h
Go to the documentation of this file.
1 //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file declares classes for handling the YAML representation
12 /// of wasm binaries.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_OBJECTYAML_WASMYAML_H
17 #define LLVM_OBJECTYAML_WASMYAML_H
18 
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/BinaryFormat/Wasm.h"
21 #include "llvm/ObjectYAML/YAML.h"
22 #include "llvm/Support/Casting.h"
23 #include <cstdint>
24 #include <memory>
25 #include <vector>
26 
27 namespace llvm {
28 namespace WasmYAML {
29 
30 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
31 LLVM_YAML_STRONG_TYPEDEF(int32_t, ValueType)
32 LLVM_YAML_STRONG_TYPEDEF(int32_t, TableType)
33 LLVM_YAML_STRONG_TYPEDEF(int32_t, SignatureForm)
34 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
35 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
37 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
38 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
39 LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
40 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
41 
42 struct FileHeader {
43  yaml::Hex32 Version;
44 };
45 
46 struct Limits {
47  LimitFlags Flags;
48  yaml::Hex32 Initial;
49  yaml::Hex32 Maximum;
50 };
51 
52 struct Table {
53  TableType ElemType;
55 };
56 
57 struct Export {
59  ExportKind Kind;
61 };
62 
63 struct ElemSegment {
66  std::vector<uint32_t> Functions;
67 };
68 
69 struct Global {
72  bool Mutable;
74 };
75 
76 struct Import {
79  ExportKind Kind;
80  union {
85  };
86 };
87 
88 struct LocalDecl {
91 };
92 
93 struct Function {
95  std::vector<LocalDecl> Locals;
97 };
98 
99 struct Relocation {
100  RelocType Type;
102  yaml::Hex32 Offset;
103  int32_t Addend;
104 };
105 
106 struct DataSegment {
111 };
112 
113 struct NameEntry {
116 };
117 
118 struct SegmentInfo {
122  SegmentFlags Flags;
123 };
124 
125 struct Signature {
127  SignatureForm Form = wasm::WASM_TYPE_FUNC;
128  std::vector<ValueType> ParamTypes;
130 };
131 
132 struct SymbolInfo {
134  SymbolFlags Flags;
135 };
136 
137 struct InitFunction {
140 };
141 
142 struct ComdatEntry {
143  ComdatKind Kind;
145 };
146 
147 struct Comdat {
149  std::vector<ComdatEntry> Entries;
150 };
151 
152 struct Section {
153  explicit Section(SectionType SecType) : Type(SecType) {}
154  virtual ~Section();
155 
157  std::vector<Relocation> Relocations;
158 };
159 
162  : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
163 
164  static bool classof(const Section *S) {
165  return S->Type == wasm::WASM_SEC_CUSTOM;
166  }
167 
170 };
171 
173  NameSection() : CustomSection("name") {}
174 
175  static bool classof(const Section *S) {
176  auto C = dyn_cast<CustomSection>(S);
177  return C && C->Name == "name";
178  }
179 
180  std::vector<NameEntry> FunctionNames;
181 };
182 
184  LinkingSection() : CustomSection("linking") {}
185 
186  static bool classof(const Section *S) {
187  auto C = dyn_cast<CustomSection>(S);
188  return C && C->Name == "linking";
189  }
190 
192  std::vector<SymbolInfo> SymbolInfos;
193  std::vector<SegmentInfo> SegmentInfos;
194  std::vector<InitFunction> InitFunctions;
195  std::vector<Comdat> Comdats;
196 };
197 
200 
201  static bool classof(const Section *S) {
202  return S->Type == wasm::WASM_SEC_TYPE;
203  }
204 
205  std::vector<Signature> Signatures;
206 };
207 
210 
211  static bool classof(const Section *S) {
212  return S->Type == wasm::WASM_SEC_IMPORT;
213  }
214 
215  std::vector<Import> Imports;
216 };
217 
220 
221  static bool classof(const Section *S) {
222  return S->Type == wasm::WASM_SEC_FUNCTION;
223  }
224 
225  std::vector<uint32_t> FunctionTypes;
226 };
227 
230 
231  static bool classof(const Section *S) {
232  return S->Type == wasm::WASM_SEC_TABLE;
233  }
234 
235  std::vector<Table> Tables;
236 };
237 
240 
241  static bool classof(const Section *S) {
242  return S->Type == wasm::WASM_SEC_MEMORY;
243  }
244 
245  std::vector<Limits> Memories;
246 };
247 
250 
251  static bool classof(const Section *S) {
252  return S->Type == wasm::WASM_SEC_GLOBAL;
253  }
254 
255  std::vector<Global> Globals;
256 };
257 
260 
261  static bool classof(const Section *S) {
262  return S->Type == wasm::WASM_SEC_EXPORT;
263  }
264 
265  std::vector<Export> Exports;
266 };
267 
270 
271  static bool classof(const Section *S) {
272  return S->Type == wasm::WASM_SEC_START;
273  }
274 
276 };
277 
280 
281  static bool classof(const Section *S) {
282  return S->Type == wasm::WASM_SEC_ELEM;
283  }
284 
285  std::vector<ElemSegment> Segments;
286 };
287 
290 
291  static bool classof(const Section *S) {
292  return S->Type == wasm::WASM_SEC_CODE;
293  }
294 
295  std::vector<Function> Functions;
296 };
297 
300 
301  static bool classof(const Section *S) {
302  return S->Type == wasm::WASM_SEC_DATA;
303  }
304 
305  std::vector<DataSegment> Segments;
306 };
307 
308 struct Object {
310  std::vector<std::unique_ptr<Section>> Sections;
311 };
312 
313 } // end namespace WasmYAML
314 } // end namespace llvm
315 
316 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
317 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
318 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
319 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
320 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
321 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
322 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
323 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
324 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
325 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
326 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
327 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
328 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
329 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
330 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
331 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
332 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
333 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
334 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
335 
336 namespace llvm {
337 namespace yaml {
338 
339 template <> struct MappingTraits<WasmYAML::FileHeader> {
340  static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
341 };
342 
343 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
344  static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
345 };
346 
347 template <> struct MappingTraits<WasmYAML::Object> {
348  static void mapping(IO &IO, WasmYAML::Object &Object);
349 };
350 
351 template <> struct MappingTraits<WasmYAML::Import> {
352  static void mapping(IO &IO, WasmYAML::Import &Import);
353 };
354 
355 template <> struct MappingTraits<WasmYAML::Export> {
356  static void mapping(IO &IO, WasmYAML::Export &Export);
357 };
358 
359 template <> struct MappingTraits<WasmYAML::Global> {
360  static void mapping(IO &IO, WasmYAML::Global &Global);
361 };
362 
363 template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
364  static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
365 };
366 
367 template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
368  static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
369 };
370 
371 template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
372  static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
373 };
374 
375 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
376  static void enumeration(IO &IO, WasmYAML::SectionType &Type);
377 };
378 
379 template <> struct MappingTraits<WasmYAML::Signature> {
380  static void mapping(IO &IO, WasmYAML::Signature &Signature);
381 };
382 
383 template <> struct MappingTraits<WasmYAML::Table> {
384  static void mapping(IO &IO, WasmYAML::Table &Table);
385 };
386 
387 template <> struct MappingTraits<WasmYAML::Limits> {
388  static void mapping(IO &IO, WasmYAML::Limits &Limits);
389 };
390 
391 template <> struct MappingTraits<WasmYAML::Function> {
392  static void mapping(IO &IO, WasmYAML::Function &Function);
393 };
394 
395 template <> struct MappingTraits<WasmYAML::Relocation> {
396  static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
397 };
398 
399 template <> struct MappingTraits<WasmYAML::NameEntry> {
400  static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
401 };
402 
403 template <> struct MappingTraits<WasmYAML::SegmentInfo> {
404  static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
405 };
406 
407 template <> struct MappingTraits<WasmYAML::LocalDecl> {
408  static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
409 };
410 
411 template <> struct MappingTraits<wasm::WasmInitExpr> {
412  static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
413 };
414 
415 template <> struct MappingTraits<WasmYAML::DataSegment> {
416  static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
417 };
418 
419 template <> struct MappingTraits<WasmYAML::ElemSegment> {
420  static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
421 };
422 
423 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
424  static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
425 };
426 
427 template <> struct MappingTraits<WasmYAML::InitFunction> {
428  static void mapping(IO &IO, WasmYAML::InitFunction &Init);
429 };
430 
431 template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
432  static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
433 };
434 
435 template <> struct MappingTraits<WasmYAML::ComdatEntry> {
436  static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
437 };
438 
439 template <> struct MappingTraits<WasmYAML::Comdat> {
440  static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
441 };
442 
443 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
444  static void enumeration(IO &IO, WasmYAML::ValueType &Type);
445 };
446 
447 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
448  static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
449 };
450 
451 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
452  static void enumeration(IO &IO, WasmYAML::TableType &Type);
453 };
454 
455 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
456  static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
457 };
458 
459 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
460  static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
461 };
462 
463 } // end namespace yaml
464 } // end namespace llvm
465 
466 #endif // LLVM_OBJECTYAML_WASMYAML_H
std::vector< InitFunction > InitFunctions
Definition: WasmYAML.h:194
uint64_t CallInst * C
static bool classof(const Section *S)
Definition: WasmYAML.h:175
std::vector< Comdat > Comdats
Definition: WasmYAML.h:195
std::vector< Relocation > Relocations
Definition: WasmYAML.h:157
std::vector< Limits > Memories
Definition: WasmYAML.h:245
static bool classof(const Section *S)
Definition: WasmYAML.h:291
static bool classof(const Section *S)
Definition: WasmYAML.h:281
std::vector< Signature > Signatures
Definition: WasmYAML.h:205
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
yaml::BinaryRef Body
Definition: WasmYAML.h:96
std::vector< std::unique_ptr< Section > > Sections
Definition: WasmYAML.h:310
static bool classof(const Section *S)
Definition: WasmYAML.h:211
static bool classof(const Section *S)
Definition: WasmYAML.h:271
wasm::WasmInitExpr InitExpr
Definition: WasmYAML.h:73
yaml::BinaryRef Content
Definition: WasmYAML.h:110
std::vector< Import > Imports
Definition: WasmYAML.h:215
Export information to summary.
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:65
CustomSection(StringRef Name)
Definition: WasmYAML.h:161
Definition: BitVector.h:921
std::vector< SegmentInfo > SegmentInfos
Definition: WasmYAML.h:193
static bool classof(const Section *S)
Definition: WasmYAML.h:251
std::vector< Function > Functions
Definition: WasmYAML.h:295
static bool classof(const Section *S)
Definition: WasmYAML.h:241
static bool classof(const Section *S)
Definition: WasmYAML.h:201
yaml::Hex32 Initial
Definition: WasmYAML.h:48
std::vector< ComdatEntry > Entries
Definition: WasmYAML.h:149
Import information from summary.
yaml::Hex32 Maximum
Definition: WasmYAML.h:49
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static bool classof(const Section *S)
Definition: WasmYAML.h:261
std::vector< Export > Exports
Definition: WasmYAML.h:265
std::vector< SymbolInfo > SymbolInfos
Definition: WasmYAML.h:192
Section(SectionType SecType)
Definition: WasmYAML.h:153
std::vector< uint32_t > FunctionTypes
Definition: WasmYAML.h:225
static bool classof(const Section *S)
Definition: WasmYAML.h:301
static bool classof(const Section *S)
Definition: WasmYAML.h:164
ValueType
Value types.
TableType ElemType
Definition: WasmYAML.h:53
std::vector< Table > Tables
Definition: WasmYAML.h:235
static bool classof(const Section *S)
Definition: WasmYAML.h:221
LimitFlags Flags
Definition: WasmYAML.h:47
std::vector< ElemSegment > Segments
Definition: WasmYAML.h:285
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:64
std::vector< uint32_t > Functions
Definition: WasmYAML.h:66
static bool classof(const Section *S)
Definition: WasmYAML.h:231
std::vector< DataSegment > Segments
Definition: WasmYAML.h:305
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
std::vector< ValueType > ParamTypes
Definition: WasmYAML.h:128
std::vector< LocalDecl > Locals
Definition: WasmYAML.h:95
yaml::BinaryRef Payload
Definition: WasmYAML.h:169
std::vector< NameEntry > FunctionNames
Definition: WasmYAML.h:180
const unsigned Kind
static bool classof(const Section *S)
Definition: WasmYAML.h:186
std::vector< Global > Globals
Definition: WasmYAML.h:255
LLVM Value Representation.
Definition: Value.h:73
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:109
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
SectionType
These are the section type and attributes fields.
Definition: MachO.h:115
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87