LLVM  5.0.0
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.mapRequired("DataAlignment", Section.DataAlignment);
62  IO.mapRequired("SymbolInfo", Section.SymbolInfos);
63 }
64 
66  commonSectionMapping(IO, Section);
67  IO.mapRequired("Name", Section.Name);
68  IO.mapRequired("Payload", Section.Payload);
69 }
70 
72  commonSectionMapping(IO, Section);
73  IO.mapOptional("Signatures", Section.Signatures);
74 }
75 
77  commonSectionMapping(IO, Section);
78  IO.mapOptional("Imports", Section.Imports);
79 }
80 
82  commonSectionMapping(IO, Section);
83  IO.mapOptional("FunctionTypes", Section.FunctionTypes);
84 }
85 
87  commonSectionMapping(IO, Section);
88  IO.mapOptional("Tables", Section.Tables);
89 }
90 
92  commonSectionMapping(IO, Section);
93  IO.mapOptional("Memories", Section.Memories);
94 }
95 
97  commonSectionMapping(IO, Section);
98  IO.mapOptional("Globals", Section.Globals);
99 }
100 
102  commonSectionMapping(IO, Section);
103  IO.mapOptional("Exports", Section.Exports);
104 }
105 
107  commonSectionMapping(IO, Section);
108  IO.mapOptional("StartFunction", Section.StartFunction);
109 }
110 
112  commonSectionMapping(IO, Section);
113  IO.mapOptional("Segments", Section.Segments);
114 }
115 
117  commonSectionMapping(IO, Section);
118  IO.mapRequired("Functions", Section.Functions);
119 }
120 
122  commonSectionMapping(IO, Section);
123  IO.mapRequired("Segments", Section.Segments);
124 }
125 
127  IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
129  if (IO.outputting())
130  SectionType = Section->Type;
131  else
132  IO.mapRequired("Type", SectionType);
133 
134  switch (SectionType) {
135  case wasm::WASM_SEC_CUSTOM: {
137  if (IO.outputting()) {
138  auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
139  SectionName = CustomSection->Name;
140  } else {
141  IO.mapRequired("Name", SectionName);
142  }
143  if (SectionName == "linking") {
144  if (!IO.outputting())
145  Section.reset(new WasmYAML::LinkingSection());
146  sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
147  } else if (SectionName == "name") {
148  if (!IO.outputting())
149  Section.reset(new WasmYAML::NameSection());
150  sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
151  } else {
152  if (!IO.outputting())
153  Section.reset(new WasmYAML::CustomSection(SectionName));
154  sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
155  }
156  break;
157  }
158  case wasm::WASM_SEC_TYPE:
159  if (!IO.outputting())
160  Section.reset(new WasmYAML::TypeSection());
161  sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
162  break;
164  if (!IO.outputting())
165  Section.reset(new WasmYAML::ImportSection());
166  sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
167  break;
169  if (!IO.outputting())
170  Section.reset(new WasmYAML::FunctionSection());
171  sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
172  break;
174  if (!IO.outputting())
175  Section.reset(new WasmYAML::TableSection());
176  sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
177  break;
179  if (!IO.outputting())
180  Section.reset(new WasmYAML::MemorySection());
181  sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
182  break;
184  if (!IO.outputting())
185  Section.reset(new WasmYAML::GlobalSection());
186  sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
187  break;
189  if (!IO.outputting())
190  Section.reset(new WasmYAML::ExportSection());
191  sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
192  break;
194  if (!IO.outputting())
195  Section.reset(new WasmYAML::StartSection());
196  sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
197  break;
198  case wasm::WASM_SEC_ELEM:
199  if (!IO.outputting())
200  Section.reset(new WasmYAML::ElemSection());
201  sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
202  break;
203  case wasm::WASM_SEC_CODE:
204  if (!IO.outputting())
205  Section.reset(new WasmYAML::CodeSection());
206  sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
207  break;
208  case wasm::WASM_SEC_DATA:
209  if (!IO.outputting())
210  Section.reset(new WasmYAML::DataSection());
211  sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
212  break;
213  default:
214  llvm_unreachable("Unknown section type");
215  }
216 }
217 
220 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
221  ECase(CUSTOM);
222  ECase(TYPE);
223  ECase(IMPORT);
224  ECase(FUNCTION);
225  ECase(TABLE);
226  ECase(MEMORY);
227  ECase(GLOBAL);
228  ECase(EXPORT);
229  ECase(START);
230  ECase(ELEM);
231  ECase(CODE);
232  ECase(DATA);
233 #undef ECase
234 }
235 
237  IO &IO, WasmYAML::Signature &Signature) {
238  IO.mapOptional("Index", Signature.Index);
239  IO.mapRequired("ReturnType", Signature.ReturnType);
240  IO.mapRequired("ParamTypes", Signature.ParamTypes);
241 }
242 
244  IO.mapRequired("ElemType", Table.ElemType);
245  IO.mapRequired("Limits", Table.TableLimits);
246 }
247 
250  IO.mapRequired("Locals", Function.Locals);
251  IO.mapRequired("Body", Function.Body);
252 }
253 
255  IO &IO, WasmYAML::Relocation &Relocation) {
256  IO.mapRequired("Type", Relocation.Type);
257  IO.mapRequired("Index", Relocation.Index);
258  IO.mapRequired("Offset", Relocation.Offset);
259  IO.mapOptional("Addend", Relocation.Addend, 0);
260 }
261 
263  IO &IO, WasmYAML::NameEntry &NameEntry) {
264  IO.mapRequired("Index", NameEntry.Index);
265  IO.mapRequired("Name", NameEntry.Name);
266 }
267 
269  IO &IO, WasmYAML::LocalDecl &LocalDecl) {
270  IO.mapRequired("Type", LocalDecl.Type);
271  IO.mapRequired("Count", LocalDecl.Count);
272 }
273 
275  WasmYAML::Limits &Limits) {
276  if (!IO.outputting() || Limits.Flags)
277  IO.mapOptional("Flags", Limits.Flags);
278  IO.mapRequired("Initial", Limits.Initial);
279  if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
280  IO.mapOptional("Maximum", Limits.Maximum);
281 }
282 
284  IO &IO, WasmYAML::ElemSegment &Segment) {
285  IO.mapRequired("Offset", Segment.Offset);
286  IO.mapRequired("Functions", Segment.Functions);
287 }
288 
290  WasmYAML::Import &Import) {
291  IO.mapRequired("Module", Import.Module);
292  IO.mapRequired("Field", Import.Field);
293  IO.mapRequired("Kind", Import.Kind);
294  if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
295  IO.mapRequired("SigIndex", Import.SigIndex);
296  } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
297  IO.mapRequired("GlobalType", Import.GlobalImport.Type);
298  IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
299  } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
300  IO.mapRequired("Table", Import.TableImport);
301  } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY ) {
302  IO.mapRequired("Memory", Import.Memory);
303  } else {
304  llvm_unreachable("unhandled import type");
305  }
306 }
307 
309  WasmYAML::Export &Export) {
310  IO.mapRequired("Name", Export.Name);
311  IO.mapRequired("Kind", Export.Kind);
312  IO.mapRequired("Index", Export.Index);
313 }
314 
316  WasmYAML::Global &Global) {
317  IO.mapRequired("Type", Global.Type);
318  IO.mapRequired("Mutable", Global.Mutable);
319  IO.mapRequired("InitExpr", Global.InitExpr);
320 }
321 
323  wasm::WasmInitExpr &Expr) {
324  WasmYAML::Opcode Op = Expr.Opcode;
325  IO.mapRequired("Opcode", Op);
326  Expr.Opcode = Op;
327  switch (Expr.Opcode) {
329  IO.mapRequired("Value", Expr.Value.Int32);
330  break;
332  IO.mapRequired("Value", Expr.Value.Int64);
333  break;
335  IO.mapRequired("Value", Expr.Value.Float32);
336  break;
338  IO.mapRequired("Value", Expr.Value.Float64);
339  break;
341  IO.mapRequired("Index", Expr.Value.Global);
342  break;
343  }
344 }
345 
347  IO &IO, WasmYAML::DataSegment &Segment) {
348  IO.mapOptional("SectionOffset", Segment.SectionOffset);
349  IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
350  IO.mapRequired("Offset", Segment.Offset);
351  IO.mapRequired("Content", Segment.Content);
352 }
353 
355  WasmYAML::SymbolInfo &Info) {
356  IO.mapRequired("Name", Info.Name);
357  IO.mapRequired("Flags", Info.Flags);
358 }
359 
362 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
363  ECase(I32);
364  ECase(I64);
365  ECase(F32);
366  ECase(F64);
367  ECase(ANYFUNC);
368  ECase(FUNC);
369  ECase(NORESULT);
370 #undef ECase
371 }
372 
374  IO &IO, WasmYAML::ExportKind &Kind) {
375 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
376  ECase(FUNCTION);
377  ECase(TABLE);
378  ECase(MEMORY);
379  ECase(GLOBAL);
380 #undef ECase
381 }
382 
384  IO &IO, WasmYAML::Opcode &Code) {
385 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
386  ECase(END);
387  ECase(I32_CONST);
388  ECase(I64_CONST);
389  ECase(F64_CONST);
390  ECase(F32_CONST);
391  ECase(GET_GLOBAL);
392 #undef ECase
393 }
394 
396  IO &IO, WasmYAML::TableType &Type) {
397 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
398  ECase(ANYFUNC);
399 #undef ECase
400 }
401 
403  IO &IO, WasmYAML::RelocType &Type) {
404 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
405 #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
406 #undef WASM_RELOC
407 }
408 
409 } // end namespace yaml
410 
411 } // end namespace llvm
std::vector< Relocation > Relocations
Definition: WasmYAML.h:129
std::vector< Limits > Memories
Definition: WasmYAML.h:215
void mapOptional(const char *Key, T &Val)
Definition: YAMLTraits.h:656
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
wasm::WasmInitExpr InitExpr
Definition: WasmYAML.h:68
yaml::BinaryRef Content
Definition: WasmYAML.h:104
std::vector< Import > Imports
Definition: WasmYAML.h:185
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:61
std::vector< Function > Functions
Definition: WasmYAML.h:265
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
yaml::Hex32 Initial
Definition: WasmYAML.h:44
virtual bool mapTag(StringRef Tag, bool Default=false)=0
This file declares classes for handling the YAML representation of wasm binaries. ...
static void commonSectionMapping(IO &IO, ELFYAML::Section &Section)
Definition: ELFYAML.cpp:687
yaml::Hex32 Flags
Definition: WasmYAML.h:43
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
std::vector< Export > Exports
Definition: WasmYAML.h:235
std::vector< SymbolInfo > SymbolInfos
Definition: WasmYAML.h:163
union llvm::wasm::WasmInitExpr::@125 Value
std::vector< uint32_t > FunctionTypes
Definition: WasmYAML.h:195
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition: YAMLTraits.h:98
#define ECase(X)
TableType ElemType
Definition: WasmYAML.h:49
std::vector< Table > Tables
Definition: WasmYAML.h:205
void setContext(void *)
Definition: YAMLTraits.cpp:47
std::vector< ElemSegment > Segments
Definition: WasmYAML.h:255
llvm_unreachable("Unknown RegBank!")
std::vector< uint32_t > Functions
Definition: WasmYAML.h:62
std::vector< DataSegment > Segments
Definition: WasmYAML.h:275
std::vector< ValueType > ParamTypes
Definition: WasmYAML.h:115
std::vector< LocalDecl > Locals
Definition: WasmYAML.h:89
yaml::BinaryRef Payload
Definition: WasmYAML.h:141
virtual bool outputting()=0
std::vector< NameEntry > FunctionNames
Definition: WasmYAML.h:152
const unsigned Kind
std::vector< Global > Globals
Definition: WasmYAML.h:225
static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section)
Definition: ELFYAML.cpp:697
wasm::WasmInitExpr Offset
Definition: WasmYAML.h:103
const char SectionName[]
Definition: AMDGPUPTNote.h:24
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void mapRequired(const char *Key, T &Val)
Definition: YAMLTraits.h:646
SectionType
These are the section type and attributes fields.
Definition: MachO.h:115