LLVM  9.0.0svn
WasmYAML.h
Go to the documentation of this file.
1 //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file declares classes for handling the YAML representation
11 /// of wasm binaries.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_OBJECTYAML_WASMYAML_H
16 #define LLVM_OBJECTYAML_WASMYAML_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/ObjectYAML/YAML.h"
21 #include "llvm/Support/Casting.h"
22 #include <cstdint>
23 #include <memory>
24 #include <vector>
25 
26 namespace llvm {
27 namespace WasmYAML {
28 
29 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
30 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
31 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
32 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
33 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
34 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
35 LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
37 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
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 Event {
80 };
81 
82 struct Import {
85  ExportKind Kind;
86  union {
92  };
93 };
94 
95 struct LocalDecl {
98 };
99 
100 struct Function {
102  std::vector<LocalDecl> Locals;
104 };
105 
106 struct Relocation {
107  RelocType Type;
109  yaml::Hex32 Offset;
110  int32_t Addend;
111 };
112 
113 struct DataSegment {
118 };
119 
120 struct NameEntry {
123 };
124 
126  std::string Name;
127  std::string Version;
128 };
129 
130 struct SegmentInfo {
134  SegmentFlags Flags;
135 };
136 
137 struct Signature {
139  SignatureForm Form = wasm::WASM_TYPE_FUNC;
140  std::vector<ValueType> ParamTypes;
142 };
143 
144 struct SymbolInfo {
148  SymbolFlags Flags;
149  union {
152  };
153 };
154 
155 struct InitFunction {
158 };
159 
160 struct ComdatEntry {
161  ComdatKind Kind;
163 };
164 
165 struct Comdat {
167  std::vector<ComdatEntry> Entries;
168 };
169 
170 struct Section {
171  explicit Section(SectionType SecType) : Type(SecType) {}
172  virtual ~Section();
173 
175  std::vector<Relocation> Relocations;
176 };
177 
180  : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
181 
182  static bool classof(const Section *S) {
183  return S->Type == wasm::WASM_SEC_CUSTOM;
184  }
185 
188 };
189 
191  DylinkSection() : CustomSection("dylink") {}
192 
193  static bool classof(const Section *S) {
194  auto C = dyn_cast<CustomSection>(S);
195  return C && C->Name == "dylink";
196  }
197 
202  std::vector<StringRef> Needed;
203 };
204 
206  NameSection() : CustomSection("name") {}
207 
208  static bool classof(const Section *S) {
209  auto C = dyn_cast<CustomSection>(S);
210  return C && C->Name == "name";
211  }
212 
213  std::vector<NameEntry> FunctionNames;
214 };
215 
217  LinkingSection() : CustomSection("linking") {}
218 
219  static bool classof(const Section *S) {
220  auto C = dyn_cast<CustomSection>(S);
221  return C && C->Name == "linking";
222  }
223 
225  std::vector<SymbolInfo> SymbolTable;
226  std::vector<SegmentInfo> SegmentInfos;
227  std::vector<InitFunction> InitFunctions;
228  std::vector<Comdat> Comdats;
229 };
230 
232  ProducersSection() : CustomSection("producers") {}
233 
234  static bool classof(const Section *S) {
235  auto C = dyn_cast<CustomSection>(S);
236  return C && C->Name == "producers";
237  }
238 
239  std::vector<ProducerEntry> Languages;
240  std::vector<ProducerEntry> Tools;
241  std::vector<ProducerEntry> SDKs;
242 };
243 
246 
247  static bool classof(const Section *S) {
248  return S->Type == wasm::WASM_SEC_TYPE;
249  }
250 
251  std::vector<Signature> Signatures;
252 };
253 
256 
257  static bool classof(const Section *S) {
258  return S->Type == wasm::WASM_SEC_IMPORT;
259  }
260 
261  std::vector<Import> Imports;
262 };
263 
266 
267  static bool classof(const Section *S) {
268  return S->Type == wasm::WASM_SEC_FUNCTION;
269  }
270 
271  std::vector<uint32_t> FunctionTypes;
272 };
273 
276 
277  static bool classof(const Section *S) {
278  return S->Type == wasm::WASM_SEC_TABLE;
279  }
280 
281  std::vector<Table> Tables;
282 };
283 
286 
287  static bool classof(const Section *S) {
288  return S->Type == wasm::WASM_SEC_MEMORY;
289  }
290 
291  std::vector<Limits> Memories;
292 };
293 
296 
297  static bool classof(const Section *S) {
298  return S->Type == wasm::WASM_SEC_GLOBAL;
299  }
300 
301  std::vector<Global> Globals;
302 };
303 
306 
307  static bool classof(const Section *S) {
308  return S->Type == wasm::WASM_SEC_EVENT;
309  }
310 
311  std::vector<Event> Events;
312 };
313 
316 
317  static bool classof(const Section *S) {
318  return S->Type == wasm::WASM_SEC_EXPORT;
319  }
320 
321  std::vector<Export> Exports;
322 };
323 
326 
327  static bool classof(const Section *S) {
328  return S->Type == wasm::WASM_SEC_START;
329  }
330 
332 };
333 
336 
337  static bool classof(const Section *S) {
338  return S->Type == wasm::WASM_SEC_ELEM;
339  }
340 
341  std::vector<ElemSegment> Segments;
342 };
343 
346 
347  static bool classof(const Section *S) {
348  return S->Type == wasm::WASM_SEC_CODE;
349  }
350 
351  std::vector<Function> Functions;
352 };
353 
356 
357  static bool classof(const Section *S) {
358  return S->Type == wasm::WASM_SEC_DATA;
359  }
360 
361  std::vector<DataSegment> Segments;
362 };
363 
364 struct Object {
366  std::vector<std::unique_ptr<Section>> Sections;
367 };
368 
369 } // end namespace WasmYAML
370 } // end namespace llvm
371 
372 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
373 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
374 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
375 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
376 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
377 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
378 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
379 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
380 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
381 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
382 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
383 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
384 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
385 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
386 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
387 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
388 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
389 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
390 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
391 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
392 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Event)
393 
394 namespace llvm {
395 namespace yaml {
396 
397 template <> struct MappingTraits<WasmYAML::FileHeader> {
398  static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
399 };
400 
401 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
402  static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
403 };
404 
405 template <> struct MappingTraits<WasmYAML::Object> {
406  static void mapping(IO &IO, WasmYAML::Object &Object);
407 };
408 
409 template <> struct MappingTraits<WasmYAML::Import> {
410  static void mapping(IO &IO, WasmYAML::Import &Import);
411 };
412 
413 template <> struct MappingTraits<WasmYAML::Export> {
414  static void mapping(IO &IO, WasmYAML::Export &Export);
415 };
416 
417 template <> struct MappingTraits<WasmYAML::Global> {
418  static void mapping(IO &IO, WasmYAML::Global &Global);
419 };
420 
421 template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
422  static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
423 };
424 
425 template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
426  static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
427 };
428 
429 template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
430  static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
431 };
432 
433 template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
434  static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
435 };
436 
437 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
438  static void enumeration(IO &IO, WasmYAML::SectionType &Type);
439 };
440 
441 template <> struct MappingTraits<WasmYAML::Signature> {
442  static void mapping(IO &IO, WasmYAML::Signature &Signature);
443 };
444 
445 template <> struct MappingTraits<WasmYAML::Table> {
446  static void mapping(IO &IO, WasmYAML::Table &Table);
447 };
448 
449 template <> struct MappingTraits<WasmYAML::Limits> {
450  static void mapping(IO &IO, WasmYAML::Limits &Limits);
451 };
452 
453 template <> struct MappingTraits<WasmYAML::Function> {
454  static void mapping(IO &IO, WasmYAML::Function &Function);
455 };
456 
457 template <> struct MappingTraits<WasmYAML::Relocation> {
458  static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
459 };
460 
461 template <> struct MappingTraits<WasmYAML::NameEntry> {
462  static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
463 };
464 
465 template <> struct MappingTraits<WasmYAML::ProducerEntry> {
466  static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
467 };
468 
469 template <> struct MappingTraits<WasmYAML::SegmentInfo> {
470  static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
471 };
472 
473 template <> struct MappingTraits<WasmYAML::LocalDecl> {
474  static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
475 };
476 
477 template <> struct MappingTraits<wasm::WasmInitExpr> {
478  static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
479 };
480 
481 template <> struct MappingTraits<WasmYAML::DataSegment> {
482  static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
483 };
484 
485 template <> struct MappingTraits<WasmYAML::ElemSegment> {
486  static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
487 };
488 
489 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
490  static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
491 };
492 
493 template <> struct MappingTraits<WasmYAML::InitFunction> {
494  static void mapping(IO &IO, WasmYAML::InitFunction &Init);
495 };
496 
497 template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
498  static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
499 };
500 
501 template <> struct MappingTraits<WasmYAML::ComdatEntry> {
502  static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
503 };
504 
505 template <> struct MappingTraits<WasmYAML::Comdat> {
506  static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
507 };
508 
509 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
510  static void enumeration(IO &IO, WasmYAML::ValueType &Type);
511 };
512 
513 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
514  static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
515 };
516 
517 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
518  static void enumeration(IO &IO, WasmYAML::TableType &Type);
519 };
520 
521 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
522  static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
523 };
524 
525 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
526  static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
527 };
528 
529 template <> struct MappingTraits<WasmYAML::Event> {
530  static void mapping(IO &IO, WasmYAML::Event &Event);
531 };
532 
533 } // end namespace yaml
534 } // end namespace llvm
535 
536 #endif // LLVM_OBJECTYAML_WASMYAML_H
std::vector< InitFunction > InitFunctions
Definition: WasmYAML.h:227
uint64_t CallInst * C
static bool classof(const Section *S)
Definition: WasmYAML.h:208
std::vector< Comdat > Comdats
Definition: WasmYAML.h:228
std::vector< Relocation > Relocations
Definition: WasmYAML.h:175
std::vector< Limits > Memories
Definition: WasmYAML.h:291
static bool classof(const Section *S)
Definition: WasmYAML.h:347
static bool classof(const Section *S)
Definition: WasmYAML.h:337
std::vector< Signature > Signatures
Definition: WasmYAML.h:251
This class represents lattice values for constants.
Definition: AllocatorList.h:23
yaml::BinaryRef Body
Definition: WasmYAML.h:103
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::vector< std::unique_ptr< Section > > Sections
Definition: WasmYAML.h:366
static bool classof(const Section *S)
Definition: WasmYAML.h:257
static bool classof(const Section *S)
Definition: WasmYAML.h:327
wasm::WasmInitExpr InitExpr
Definition: WasmYAML.h:73
yaml::BinaryRef Content
Definition: WasmYAML.h:117
std::vector< Import > Imports
Definition: WasmYAML.h:261
std::vector< ProducerEntry > Languages
Definition: WasmYAML.h:239
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:65
CustomSection(StringRef Name)
Definition: WasmYAML.h:179
Definition: BitVector.h:937
std::vector< SegmentInfo > SegmentInfos
Definition: WasmYAML.h:226
std::vector< SymbolInfo > SymbolTable
Definition: WasmYAML.h:225
static bool classof(const Section *S)
Definition: WasmYAML.h:307
static bool classof(const Section *S)
Definition: WasmYAML.h:297
std::vector< Function > Functions
Definition: WasmYAML.h:351
static bool classof(const Section *S)
Definition: WasmYAML.h:287
static bool classof(const Section *S)
Definition: WasmYAML.h:247
yaml::Hex32 Initial
Definition: WasmYAML.h:48
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
std::vector< ComdatEntry > Entries
Definition: WasmYAML.h:167
uint32_t Attribute
Definition: WasmYAML.h:78
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:45
static bool classof(const Section *S)
Definition: WasmYAML.h:317
std::vector< Export > Exports
Definition: WasmYAML.h:321
Section(SectionType SecType)
Definition: WasmYAML.h:171
std::vector< ProducerEntry > Tools
Definition: WasmYAML.h:240
std::vector< uint32_t > FunctionTypes
Definition: WasmYAML.h:271
static bool classof(const Section *S)
Definition: WasmYAML.h:357
static bool classof(const Section *S)
Definition: WasmYAML.h:182
std::vector< ProducerEntry > SDKs
Definition: WasmYAML.h:241
ValueType
Value types.
TableType ElemType
Definition: WasmYAML.h:53
std::vector< Table > Tables
Definition: WasmYAML.h:281
static bool classof(const Section *S)
Definition: WasmYAML.h:267
LimitFlags Flags
Definition: WasmYAML.h:47
std::vector< ElemSegment > Segments
Definition: WasmYAML.h:341
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
std::vector< Event > Events
Definition: WasmYAML.h:311
std::vector< uint32_t > Functions
Definition: WasmYAML.h:66
static bool classof(const Section *S)
Definition: WasmYAML.h:277
std::vector< DataSegment > Segments
Definition: WasmYAML.h:361
wasm::WasmDataReference DataRef
Definition: WasmYAML.h:151
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:322
std::vector< ValueType > ParamTypes
Definition: WasmYAML.h:140
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:47
std::vector< LocalDecl > Locals
Definition: WasmYAML.h:102
yaml::BinaryRef Payload
Definition: WasmYAML.h:187
std::vector< NameEntry > FunctionNames
Definition: WasmYAML.h:213
const unsigned Kind
static bool classof(const Section *S)
Definition: WasmYAML.h:219
std::vector< Global > Globals
Definition: WasmYAML.h:301
LLVM Value Representation.
Definition: Value.h:72
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:116
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
SectionType
These are the section type and attributes fields.
Definition: MachO.h:114
static bool classof(const Section *S)
Definition: WasmYAML.h:234
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:86