LLVM  7.0.0svn
WasmYAML.cpp
Go to the documentation of this file.
1 //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
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 // This file defines classes for handling the YAML representation of wasm.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Casting.h"
19 
20 namespace llvm {
21 
22 namespace WasmYAML {
23 
24 // Declared here rather than in the header to comply with:
25 // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers
26 Section::~Section() = default;
27 
28 } // end namespace WasmYAML
29 
30 namespace yaml {
31 
33  IO &IO, WasmYAML::FileHeader &FileHdr) {
34  IO.mapRequired("Version", FileHdr.Version);
35 }
36 
38  WasmYAML::Object &Object) {
39  IO.setContext(&Object);
40  IO.mapTag("!WASM", true);
41  IO.mapRequired("FileHeader", Object.Header);
42  IO.mapOptional("Sections", Object.Sections);
43  IO.setContext(nullptr);
44 }
45 
47  IO.mapRequired("Type", Section.Type);
48  IO.mapOptional("Relocations", Section.Relocations);
49 }
50 
52  commonSectionMapping(IO, Section);
53  IO.mapRequired("Name", Section.Name);
54  IO.mapOptional("FunctionNames", Section.FunctionNames);
55 }
56 
58  commonSectionMapping(IO, Section);
59  IO.mapRequired("Name", Section.Name);
60  IO.mapRequired("DataSize", Section.DataSize);
61  IO.mapOptional("SymbolInfo", Section.SymbolInfos);
62  IO.mapOptional("SegmentInfo", Section.SegmentInfos);
63  IO.mapOptional("InitFunctions", Section.InitFunctions);
64  IO.mapOptional("Comdats", Section.Comdats);
65 }
66 
68  commonSectionMapping(IO, Section);
69  IO.mapRequired("Name", Section.Name);
70  IO.mapRequired("Payload", Section.Payload);
71 }
72 
74  commonSectionMapping(IO, Section);
75  IO.mapOptional("Signatures", Section.Signatures);
76 }
77 
79  commonSectionMapping(IO, Section);
80  IO.mapOptional("Imports", Section.Imports);
81 }
82 
84  commonSectionMapping(IO, Section);
85  IO.mapOptional("FunctionTypes", Section.FunctionTypes);
86 }
87 
89  commonSectionMapping(IO, Section);
90  IO.mapOptional("Tables", Section.Tables);
91 }
92 
94  commonSectionMapping(IO, Section);
95  IO.mapOptional("Memories", Section.Memories);
96 }
97 
99  commonSectionMapping(IO, Section);
100  IO.mapOptional("Globals", Section.Globals);
101 }
102 
104  commonSectionMapping(IO, Section);
105  IO.mapOptional("Exports", Section.Exports);
106 }
107 
109  commonSectionMapping(IO, Section);
110  IO.mapOptional("StartFunction", Section.StartFunction);
111 }
112 
114  commonSectionMapping(IO, Section);
115  IO.mapOptional("Segments", Section.Segments);
116 }
117 
119  commonSectionMapping(IO, Section);
120  IO.mapRequired("Functions", Section.Functions);
121 }
122 
124  commonSectionMapping(IO, Section);
125  IO.mapRequired("Segments", Section.Segments);
126 }
127 
129  IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
131  if (IO.outputting())
132  SectionType = Section->Type;
133  else
134  IO.mapRequired("Type", SectionType);
135 
136  switch (SectionType) {
137  case wasm::WASM_SEC_CUSTOM: {
139  if (IO.outputting()) {
140  auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
141  SectionName = CustomSection->Name;
142  } else {
143  IO.mapRequired("Name", SectionName);
144  }
145  if (SectionName == "linking") {
146  if (!IO.outputting())
147  Section.reset(new WasmYAML::LinkingSection());
148  sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
149  } else if (SectionName == "name") {
150  if (!IO.outputting())
151  Section.reset(new WasmYAML::NameSection());
152  sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
153  } else {
154  if (!IO.outputting())
155  Section.reset(new WasmYAML::CustomSection(SectionName));
156  sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
157  }
158  break;
159  }
160  case wasm::WASM_SEC_TYPE:
161  if (!IO.outputting())
162  Section.reset(new WasmYAML::TypeSection());
163  sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
164  break;
166  if (!IO.outputting())
167  Section.reset(new WasmYAML::ImportSection());
168  sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
169  break;
171  if (!IO.outputting())
172  Section.reset(new WasmYAML::FunctionSection());
173  sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
174  break;
176  if (!IO.outputting())
177  Section.reset(new WasmYAML::TableSection());
178  sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
179  break;
181  if (!IO.outputting())
182  Section.reset(new WasmYAML::MemorySection());
183  sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
184  break;
186  if (!IO.outputting())
187  Section.reset(new WasmYAML::GlobalSection());
188  sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
189  break;
191  if (!IO.outputting())
192  Section.reset(new WasmYAML::ExportSection());
193  sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
194  break;
196  if (!IO.outputting())
197  Section.reset(new WasmYAML::StartSection());
198  sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
199  break;
200  case wasm::WASM_SEC_ELEM:
201  if (!IO.outputting())
202  Section.reset(new WasmYAML::ElemSection());
203  sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
204  break;
205  case wasm::WASM_SEC_CODE:
206  if (!IO.outputting())
207  Section.reset(new WasmYAML::CodeSection());
208  sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
209  break;
210  case wasm::WASM_SEC_DATA:
211  if (!IO.outputting())
212  Section.reset(new WasmYAML::DataSection());
213  sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
214  break;
215  default:
216  llvm_unreachable("Unknown section type");
217  }
218 }
219 
220 void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
221  IO &IO, WasmYAML::SectionType &Type) {
222 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
223  ECase(CUSTOM);
224  ECase(TYPE);
225  ECase(IMPORT);
226  ECase(FUNCTION);
227  ECase(TABLE);
228  ECase(MEMORY);
229  ECase(GLOBAL);
230  ECase(EXPORT);
231  ECase(START);
232  ECase(ELEM);
233  ECase(CODE);
234  ECase(DATA);
235 #undef ECase
236 }
237 
239  IO &IO, WasmYAML::Signature &Signature) {
240  IO.mapRequired("Index", Signature.Index);
241  IO.mapRequired("ReturnType", Signature.ReturnType);
242  IO.mapRequired("ParamTypes", Signature.ParamTypes);
243 }
244 
246  IO.mapRequired("ElemType", Table.ElemType);
247  IO.mapRequired("Limits", Table.TableLimits);
248 }
249 
252  IO.mapRequired("Index", Function.Index);
253  IO.mapRequired("Locals", Function.Locals);
254  IO.mapRequired("Body", Function.Body);
255 }
256 
258  IO &IO, WasmYAML::Relocation &Relocation) {
259  IO.mapRequired("Type", Relocation.Type);
260  IO.mapRequired("Index", Relocation.Index);
261  IO.mapRequired("Offset", Relocation.Offset);
262  IO.mapOptional("Addend", Relocation.Addend, 0);
263 }
264 
266  IO &IO, WasmYAML::NameEntry &NameEntry) {
267  IO.mapRequired("Index", NameEntry.Index);
268  IO.mapRequired("Name", NameEntry.Name);
269 }
270 
272  IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
273  IO.mapRequired("Index", SegmentInfo.Index);
274  IO.mapRequired("Name", SegmentInfo.Name);
275  IO.mapRequired("Alignment", SegmentInfo.Alignment);
276  IO.mapRequired("Flags", SegmentInfo.Flags);
277 }
278 
280  IO &IO, WasmYAML::LocalDecl &LocalDecl) {
281  IO.mapRequired("Type", LocalDecl.Type);
282  IO.mapRequired("Count", LocalDecl.Count);
283 }
284 
286  WasmYAML::Limits &Limits) {
287  if (!IO.outputting() || Limits.Flags)
288  IO.mapOptional("Flags", Limits.Flags);
289  IO.mapRequired("Initial", Limits.Initial);
290  if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
291  IO.mapOptional("Maximum", Limits.Maximum);
292 }
293 
295  IO &IO, WasmYAML::ElemSegment &Segment) {
296  IO.mapRequired("Offset", Segment.Offset);
297  IO.mapRequired("Functions", Segment.Functions);
298 }
299 
301  WasmYAML::Import &Import) {
302  IO.mapRequired("Module", Import.Module);
303  IO.mapRequired("Field", Import.Field);
304  IO.mapRequired("Kind", Import.Kind);
305  if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
306  IO.mapRequired("SigIndex", Import.SigIndex);
307  } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
308  IO.mapRequired("GlobalType", Import.GlobalImport.Type);
309  IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
310  } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
311  IO.mapRequired("Table", Import.TableImport);
312  } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY ) {
313  IO.mapRequired("Memory", Import.Memory);
314  } else {
315  llvm_unreachable("unhandled import type");
316  }
317 }
318 
320  WasmYAML::Export &Export) {
321  IO.mapRequired("Name", Export.Name);
322  IO.mapRequired("Kind", Export.Kind);
323  IO.mapRequired("Index", Export.Index);
324 }
325 
327  WasmYAML::Global &Global) {
328  IO.mapRequired("Index", Global.Index);
329  IO.mapRequired("Type", Global.Type);
330  IO.mapRequired("Mutable", Global.Mutable);
331  IO.mapRequired("InitExpr", Global.InitExpr);
332 }
333 
335  wasm::WasmInitExpr &Expr) {
336  WasmYAML::Opcode Op = Expr.Opcode;
337  IO.mapRequired("Opcode", Op);
338  Expr.Opcode = Op;
339  switch (Expr.Opcode) {
341  IO.mapRequired("Value", Expr.Value.Int32);
342  break;
344  IO.mapRequired("Value", Expr.Value.Int64);
345  break;
347  IO.mapRequired("Value", Expr.Value.Float32);
348  break;
350  IO.mapRequired("Value", Expr.Value.Float64);
351  break;
353  IO.mapRequired("Index", Expr.Value.Global);
354  break;
355  }
356 }
357 
359  IO &IO, WasmYAML::DataSegment &Segment) {
360  IO.mapOptional("SectionOffset", Segment.SectionOffset);
361  IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
362  IO.mapRequired("Offset", Segment.Offset);
363  IO.mapRequired("Content", Segment.Content);
364 }
365 
367  IO &IO, WasmYAML::InitFunction &Init) {
368  IO.mapRequired("Priority", Init.Priority);
369  IO.mapRequired("FunctionIndex", Init.FunctionIndex);
370 }
371 
372 void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
373  IO &IO, WasmYAML::ComdatKind &Kind) {
374 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
375  ECase(FUNCTION);
376  ECase(DATA);
377 #undef ECase
378 }
379 
381  IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
382  IO.mapRequired("Kind", ComdatEntry.Kind);
383  IO.mapRequired("Index", ComdatEntry.Index);
384 }
385 
387  IO &IO, WasmYAML::Comdat &Comdat) {
388  IO.mapRequired("Name", Comdat.Name);
389  IO.mapRequired("Entries", Comdat.Entries);
390 }
391 
393  WasmYAML::SymbolInfo &Info) {
394  IO.mapRequired("Name", Info.Name);
395  IO.mapRequired("Flags", Info.Flags);
396 }
397 
398 void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
399  IO &IO, WasmYAML::LimitFlags &Value) {
400 #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
401  BCase(HAS_MAX);
402 #undef BCase
403 }
404 
405 void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
406  IO &IO, WasmYAML::SegmentFlags &Value) {
407 }
408 
409 void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
410  IO &IO, WasmYAML::SymbolFlags &Value) {
411 #define BCaseMask(M, X) IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
412  //BCaseMask(BINDING_MASK, BINDING_GLOBAL);
413  BCaseMask(BINDING_MASK, BINDING_WEAK);
414  BCaseMask(BINDING_MASK, BINDING_LOCAL);
415  //BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
416  BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
417 #undef BCaseMask
418 }
419 
420 void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
421  IO &IO, WasmYAML::ValueType &Type) {
422 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
423  ECase(I32);
424  ECase(I64);
425  ECase(F32);
426  ECase(F64);
427  ECase(ANYFUNC);
428  ECase(FUNC);
429  ECase(NORESULT);
430 #undef ECase
431 }
432 
433 void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
434  IO &IO, WasmYAML::ExportKind &Kind) {
435 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
436  ECase(FUNCTION);
437  ECase(TABLE);
438  ECase(MEMORY);
439  ECase(GLOBAL);
440 #undef ECase
441 }
442 
443 void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
444  IO &IO, WasmYAML::Opcode &Code) {
445 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
446  ECase(END);
447  ECase(I32_CONST);
448  ECase(I64_CONST);
449  ECase(F64_CONST);
450  ECase(F32_CONST);
451  ECase(GET_GLOBAL);
452 #undef ECase
453 }
454 
455 void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
456  IO &IO, WasmYAML::TableType &Type) {
457 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
458  ECase(ANYFUNC);
459 #undef ECase
460 }
461 
462 void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
463  IO &IO, WasmYAML::RelocType &Type) {
464 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
465 #include "llvm/BinaryFormat/WasmRelocs.def"
466 #undef WASM_RELOC
467 }
468 
469 } // end namespace yaml
470 
471 } // end namespace llvm
std::vector< InitFunction > InitFunctions
Definition: WasmYAML.h:194
std::vector< Comdat > Comdats
Definition: WasmYAML.h:195
std::vector< Relocation > Relocations
Definition: WasmYAML.h:157
std::vector< Limits > Memories
Definition: WasmYAML.h:245
union llvm::wasm::WasmInitExpr::@132 Value
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
wasm::WasmInitExpr InitExpr
Definition: WasmYAML.h:73
yaml::BinaryRef Content
Definition: WasmYAML.h:110
#define BCase(X)
std::vector< Import > Imports
Definition: WasmYAML.h:215
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:65
std::vector< SegmentInfo > SegmentInfos
Definition: WasmYAML.h:193
std::vector< Function > Functions
Definition: WasmYAML.h:295
yaml::Hex32 Initial
Definition: WasmYAML.h:48
std::vector< ComdatEntry > Entries
Definition: WasmYAML.h:149
This file declares classes for handling the YAML representation of wasm binaries. ...
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
std::vector< Export > Exports
Definition: WasmYAML.h:265
std::vector< SymbolInfo > SymbolInfos
Definition: WasmYAML.h:192
#define BCaseMask(M, X)
static void sectionMapping(IO &IO, WasmYAML::DataSection &Section)
Definition: WasmYAML.cpp:123
std::vector< uint32_t > FunctionTypes
Definition: WasmYAML.h:225
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define ECase(X)
ValueType
Value types.
TableType ElemType
Definition: WasmYAML.h:53
std::vector< Table > Tables
Definition: WasmYAML.h:235
LimitFlags Flags
Definition: WasmYAML.h:47
std::vector< ElemSegment > Segments
Definition: WasmYAML.h:285
std::vector< uint32_t > Functions
Definition: WasmYAML.h:66
std::vector< DataSegment > Segments
Definition: WasmYAML.h:305
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
std::vector< Global > Globals
Definition: WasmYAML.h:255
LLVM Value Representation.
Definition: Value.h:73
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:109
static void commonSectionMapping(IO &IO, WasmYAML::Section &Section)
Definition: WasmYAML.cpp:46
const char SectionName[]
Definition: AMDGPUPTNote.h:24
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