LLVM  5.0.0
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 
32 LLVM_YAML_STRONG_TYPEDEF(int32_t, TableType)
33 LLVM_YAML_STRONG_TYPEDEF(int32_t, SignatureForm)
37 
38 struct FileHeader {
39  yaml::Hex32 Version;
40 };
41 
42 struct Limits {
43  yaml::Hex32 Flags;
44  yaml::Hex32 Initial;
45  yaml::Hex32 Maximum;
46 };
47 
48 struct Table {
49  TableType ElemType;
51 };
52 
53 struct Export {
55  ExportKind Kind;
57 };
58 
59 struct ElemSegment {
62  std::vector<uint32_t> Functions;
63 };
64 
65 struct Global {
67  bool Mutable;
69 };
70 
71 struct Import {
74  ExportKind Kind;
75  union {
80  };
81 };
82 
83 struct LocalDecl {
86 };
87 
88 struct Function {
89  std::vector<LocalDecl> Locals;
91 };
92 
93 struct Relocation {
94  RelocType Type;
96  yaml::Hex32 Offset;
97  int32_t Addend;
98 };
99 
100 struct DataSegment {
105 };
106 
107 struct NameEntry {
110 };
111 
112 struct Signature {
114  SignatureForm Form = wasm::WASM_TYPE_FUNC;
115  std::vector<ValueType> ParamTypes;
117 };
118 
119 struct SymbolInfo {
122 };
123 
124 struct Section {
125  explicit Section(SectionType SecType) : Type(SecType) {}
126  virtual ~Section();
127 
129  std::vector<Relocation> Relocations;
130 };
131 
134  : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
135 
136  static bool classof(const Section *S) {
137  return S->Type == wasm::WASM_SEC_CUSTOM;
138  }
139 
142 };
143 
145  NameSection() : CustomSection("name") {}
146 
147  static bool classof(const Section *S) {
148  auto C = dyn_cast<CustomSection>(S);
149  return C && C->Name == "name";
150  }
151 
152  std::vector<NameEntry> FunctionNames;
153 };
154 
156  LinkingSection() : CustomSection("linking") {}
157 
158  static bool classof(const Section *S) {
159  auto C = dyn_cast<CustomSection>(S);
160  return C && C->Name == "linking";
161  }
162 
163  std::vector<SymbolInfo> SymbolInfos;
166 };
167 
170 
171  static bool classof(const Section *S) {
172  return S->Type == wasm::WASM_SEC_TYPE;
173  }
174 
175  std::vector<Signature> Signatures;
176 };
177 
180 
181  static bool classof(const Section *S) {
182  return S->Type == wasm::WASM_SEC_IMPORT;
183  }
184 
185  std::vector<Import> Imports;
186 };
187 
190 
191  static bool classof(const Section *S) {
192  return S->Type == wasm::WASM_SEC_FUNCTION;
193  }
194 
195  std::vector<uint32_t> FunctionTypes;
196 };
197 
200 
201  static bool classof(const Section *S) {
202  return S->Type == wasm::WASM_SEC_TABLE;
203  }
204 
205  std::vector<Table> Tables;
206 };
207 
210 
211  static bool classof(const Section *S) {
212  return S->Type == wasm::WASM_SEC_MEMORY;
213  }
214 
215  std::vector<Limits> Memories;
216 };
217 
220 
221  static bool classof(const Section *S) {
222  return S->Type == wasm::WASM_SEC_GLOBAL;
223  }
224 
225  std::vector<Global> Globals;
226 };
227 
230 
231  static bool classof(const Section *S) {
232  return S->Type == wasm::WASM_SEC_EXPORT;
233  }
234 
235  std::vector<Export> Exports;
236 };
237 
240 
241  static bool classof(const Section *S) {
242  return S->Type == wasm::WASM_SEC_START;
243  }
244 
246 };
247 
250 
251  static bool classof(const Section *S) {
252  return S->Type == wasm::WASM_SEC_ELEM;
253  }
254 
255  std::vector<ElemSegment> Segments;
256 };
257 
260 
261  static bool classof(const Section *S) {
262  return S->Type == wasm::WASM_SEC_CODE;
263  }
264 
265  std::vector<Function> Functions;
266 };
267 
270 
271  static bool classof(const Section *S) {
272  return S->Type == wasm::WASM_SEC_DATA;
273  }
274 
275  std::vector<DataSegment> Segments;
276 };
277 
278 struct Object {
280  std::vector<std::unique_ptr<Section>> Sections;
281 };
282 
283 } // end namespace WasmYAML
284 } // end namespace llvm
285 
286 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
287 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
289 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
290 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
291 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
292 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
293 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
294 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
295 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
296 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
297 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
298 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
299 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
300 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
301 
302 namespace llvm {
303 namespace yaml {
304 
305 template <> struct MappingTraits<WasmYAML::FileHeader> {
306  static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
307 };
308 
309 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
310  static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
311 };
312 
313 template <> struct MappingTraits<WasmYAML::Object> {
314  static void mapping(IO &IO, WasmYAML::Object &Object);
315 };
316 
317 template <> struct MappingTraits<WasmYAML::Import> {
318  static void mapping(IO &IO, WasmYAML::Import &Import);
319 };
320 
321 template <> struct MappingTraits<WasmYAML::Export> {
322  static void mapping(IO &IO, WasmYAML::Export &Export);
323 };
324 
325 template <> struct MappingTraits<WasmYAML::Global> {
326  static void mapping(IO &IO, WasmYAML::Global &Global);
327 };
328 
329 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
330  static void enumeration(IO &IO, WasmYAML::SectionType &Type);
331 };
332 
333 template <> struct MappingTraits<WasmYAML::Signature> {
334  static void mapping(IO &IO, WasmYAML::Signature &Signature);
335 };
336 
337 template <> struct MappingTraits<WasmYAML::Table> {
338  static void mapping(IO &IO, WasmYAML::Table &Table);
339 };
340 
341 template <> struct MappingTraits<WasmYAML::Limits> {
342  static void mapping(IO &IO, WasmYAML::Limits &Limits);
343 };
344 
345 template <> struct MappingTraits<WasmYAML::Function> {
346  static void mapping(IO &IO, WasmYAML::Function &Function);
347 };
348 
349 template <> struct MappingTraits<WasmYAML::Relocation> {
350  static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
351 };
352 
353 template <> struct MappingTraits<WasmYAML::NameEntry> {
354  static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
355 };
356 
357 template <> struct MappingTraits<WasmYAML::LocalDecl> {
358  static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
359 };
360 
361 template <> struct MappingTraits<wasm::WasmInitExpr> {
362  static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
363 };
364 
365 template <> struct MappingTraits<WasmYAML::DataSegment> {
366  static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
367 };
368 
369 template <> struct MappingTraits<WasmYAML::ElemSegment> {
370  static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
371 };
372 
373 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
374  static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
375 };
376 
377 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
378  static void enumeration(IO &IO, WasmYAML::ValueType &Type);
379 };
380 
381 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
382  static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
383 };
384 
385 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
386  static void enumeration(IO &IO, WasmYAML::TableType &Type);
387 };
388 
389 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
390  static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
391 };
392 
393 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
394  static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
395 };
396 
397 } // end namespace yaml
398 } // end namespace llvm
399 
400 #endif // LLVM_OBJECTYAML_WASMYAML_H
static bool classof(const Section *S)
Definition: WasmYAML.h:147
std::vector< Relocation > Relocations
Definition: WasmYAML.h:129
std::vector< Limits > Memories
Definition: WasmYAML.h:215
static bool classof(const Section *S)
Definition: WasmYAML.h:261
static bool classof(const Section *S)
Definition: WasmYAML.h:251
std::vector< Signature > Signatures
Definition: WasmYAML.h:175
yaml::BinaryRef Body
Definition: WasmYAML.h:90
std::vector< std::unique_ptr< Section > > Sections
Definition: WasmYAML.h:280
static bool classof(const Section *S)
Definition: WasmYAML.h:181
static bool classof(const Section *S)
Definition: WasmYAML.h:241
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence...
Definition: YAMLTraits.h:1643
wasm::WasmInitExpr InitExpr
Definition: WasmYAML.h:68
yaml::BinaryRef Content
Definition: WasmYAML.h:104
std::vector< Import > Imports
Definition: WasmYAML.h:185
Export information to summary.
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:61
CustomSection(StringRef Name)
Definition: WasmYAML.h:133
static bool classof(const Section *S)
Definition: WasmYAML.h:221
std::vector< Function > Functions
Definition: WasmYAML.h:265
static bool classof(const Section *S)
Definition: WasmYAML.h:211
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
static bool classof(const Section *S)
Definition: WasmYAML.h:171
yaml::Hex32 Initial
Definition: WasmYAML.h:44
yaml::Hex32 Flags
Definition: WasmYAML.h:43
Import information from summary.
yaml::Hex32 Maximum
Definition: WasmYAML.h:45
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:231
std::vector< Export > Exports
Definition: WasmYAML.h:235
std::vector< SymbolInfo > SymbolInfos
Definition: WasmYAML.h:163
Section(SectionType SecType)
Definition: WasmYAML.h:125
std::vector< uint32_t > FunctionTypes
Definition: WasmYAML.h:195
static bool classof(const Section *S)
Definition: WasmYAML.h:271
static bool classof(const Section *S)
Definition: WasmYAML.h:136
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition: YAMLTraits.h:98
#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)
YAML I/O does conversion based on types.
Definition: YAMLTraits.h:1346
TableType ElemType
Definition: WasmYAML.h:49
std::vector< Table > Tables
Definition: WasmYAML.h:205
static bool classof(const Section *S)
Definition: WasmYAML.h:191
std::vector< ElemSegment > Segments
Definition: WasmYAML.h:255
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:64
std::vector< uint32_t > Functions
Definition: WasmYAML.h:62
static bool classof(const Section *S)
Definition: WasmYAML.h:201
#define C
Definition: LargeTest.cpp:25
std::vector< DataSegment > Segments
Definition: WasmYAML.h:275
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:115
std::vector< LocalDecl > Locals
Definition: WasmYAML.h:89
yaml::BinaryRef Payload
Definition: WasmYAML.h:141
std::vector< NameEntry > FunctionNames
Definition: WasmYAML.h:152
const unsigned Kind
static bool classof(const Section *S)
Definition: WasmYAML.h:158
std::vector< Global > Globals
Definition: WasmYAML.h:225
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:103
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
SignatureForm Form
Definition: WasmYAML.h:114
SectionType
These are the section type and attributes fields.
Definition: MachO.h:115