1 //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
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 // This file defines classes for handling the YAML representation of wasm.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ObjectYAML/WasmYAML.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/YAMLTraits.h"
18 
19 namespace llvm {
20 
21 namespace WasmYAML {
22 
23 // Declared here rather than in the header to comply with:
24 // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers
25 Section::~Section() = default;
26 
27 } // end namespace WasmYAML
28 
29 namespace yaml {
30 
31 void MappingTraits<WasmYAML::FileHeader>::mapping(
32     IO &IO, WasmYAML::FileHeader &FileHdr) {
33   IO.mapRequired("Version", FileHdr.Version);
34 }
35 
36 void MappingTraits<WasmYAML::Object>::mapping(IO &IO,
37                                               WasmYAML::Object &Object) {
38   IO.setContext(&Object);
39   IO.mapTag("!WASM", true);
40   IO.mapRequired("FileHeader", Object.Header);
41   IO.mapOptional("Sections", Object.Sections);
42   IO.setContext(nullptr);
43 }
44 
45 static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) {
46   IO.mapRequired("Type", Section.Type);
47   IO.mapOptional("Relocations", Section.Relocations);
48 }
49 
50 static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) {
51   commonSectionMapping(IO, Section);
52   IO.mapRequired("Name", Section.Name);
53   IO.mapRequired("MemorySize", Section.MemorySize);
54   IO.mapRequired("MemoryAlignment", Section.MemoryAlignment);
55   IO.mapRequired("TableSize", Section.TableSize);
56   IO.mapRequired("TableAlignment", Section.TableAlignment);
57   IO.mapRequired("Needed", Section.Needed);
58   IO.mapOptional("ExportInfo", Section.ExportInfo);
59 }
60 
61 static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
62   commonSectionMapping(IO, Section);
63   IO.mapRequired("Name", Section.Name);
64   IO.mapOptional("FunctionNames", Section.FunctionNames);
65   IO.mapOptional("GlobalNames", Section.GlobalNames);
66   IO.mapOptional("DataSegmentNames", Section.DataSegmentNames);
67 }
68 
69 static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
70   commonSectionMapping(IO, Section);
71   IO.mapRequired("Name", Section.Name);
72   IO.mapRequired("Version", Section.Version);
73   IO.mapOptional("SymbolTable", Section.SymbolTable);
74   IO.mapOptional("SegmentInfo", Section.SegmentInfos);
75   IO.mapOptional("InitFunctions", Section.InitFunctions);
76   IO.mapOptional("Comdats", Section.Comdats);
77 }
78 
79 static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) {
80   commonSectionMapping(IO, Section);
81   IO.mapRequired("Name", Section.Name);
82   IO.mapOptional("Languages", Section.Languages);
83   IO.mapOptional("Tools", Section.Tools);
84   IO.mapOptional("SDKs", Section.SDKs);
85 }
86 
87 static void sectionMapping(IO &IO, WasmYAML::TargetFeaturesSection &Section) {
88   commonSectionMapping(IO, Section);
89   IO.mapRequired("Name", Section.Name);
90   IO.mapRequired("Features", Section.Features);
91 }
92 
93 static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
94   commonSectionMapping(IO, Section);
95   IO.mapRequired("Name", Section.Name);
96   IO.mapRequired("Payload", Section.Payload);
97 }
98 
99 static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
100   commonSectionMapping(IO, Section);
101   IO.mapOptional("Signatures", Section.Signatures);
102 }
103 
104 static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
105   commonSectionMapping(IO, Section);
106   IO.mapOptional("Imports", Section.Imports);
107 }
108 
109 static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
110   commonSectionMapping(IO, Section);
111   IO.mapOptional("FunctionTypes", Section.FunctionTypes);
112 }
113 
114 static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
115   commonSectionMapping(IO, Section);
116   IO.mapOptional("Tables", Section.Tables);
117 }
118 
119 static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
120   commonSectionMapping(IO, Section);
121   IO.mapOptional("Memories", Section.Memories);
122 }
123 
124 static void sectionMapping(IO &IO, WasmYAML::TagSection &Section) {
125   commonSectionMapping(IO, Section);
126   IO.mapOptional("TagTypes", Section.TagTypes);
127 }
128 
129 static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
130   commonSectionMapping(IO, Section);
131   IO.mapOptional("Globals", Section.Globals);
132 }
133 
134 static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
135   commonSectionMapping(IO, Section);
136   IO.mapOptional("Exports", Section.Exports);
137 }
138 
139 static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
140   commonSectionMapping(IO, Section);
141   IO.mapOptional("StartFunction", Section.StartFunction);
142 }
143 
144 static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
145   commonSectionMapping(IO, Section);
146   IO.mapOptional("Segments", Section.Segments);
147 }
148 
149 static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
150   commonSectionMapping(IO, Section);
151   IO.mapRequired("Functions", Section.Functions);
152 }
153 
154 static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
155   commonSectionMapping(IO, Section);
156   IO.mapRequired("Segments", Section.Segments);
157 }
158 
159 static void sectionMapping(IO &IO, WasmYAML::DataCountSection &Section) {
160   commonSectionMapping(IO, Section);
161   IO.mapRequired("Count", Section.Count);
162 }
163 
164 void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
165     IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
166   WasmYAML::SectionType SectionType;
167   if (IO.outputting())
168     SectionType = Section->Type;
169   else
170     IO.mapRequired("Type", SectionType);
171 
172   switch (SectionType) {
173   case wasm::WASM_SEC_CUSTOM: {
174     StringRef SectionName;
175     if (IO.outputting()) {
176       auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
177       SectionName = CustomSection->Name;
178     } else {
179       IO.mapRequired("Name", SectionName);
180     }
181     if (SectionName == "dylink" || SectionName == "dylink.0") {
182       if (!IO.outputting())
183         Section.reset(new WasmYAML::DylinkSection());
184       sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get()));
185     } else if (SectionName == "linking") {
186       if (!IO.outputting())
187         Section.reset(new WasmYAML::LinkingSection());
188       sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
189     } else if (SectionName == "name") {
190       if (!IO.outputting())
191         Section.reset(new WasmYAML::NameSection());
192       sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
193     } else if (SectionName == "producers") {
194       if (!IO.outputting())
195         Section.reset(new WasmYAML::ProducersSection());
196       sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get()));
197     } else if (SectionName == "target_features") {
198       if (!IO.outputting())
199         Section.reset(new WasmYAML::TargetFeaturesSection());
200       sectionMapping(IO, *cast<WasmYAML::TargetFeaturesSection>(Section.get()));
201     } else {
202       if (!IO.outputting())
203         Section.reset(new WasmYAML::CustomSection(SectionName));
204       sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
205     }
206     break;
207   }
208   case wasm::WASM_SEC_TYPE:
209     if (!IO.outputting())
210       Section.reset(new WasmYAML::TypeSection());
211     sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
212     break;
213   case wasm::WASM_SEC_IMPORT:
214     if (!IO.outputting())
215       Section.reset(new WasmYAML::ImportSection());
216     sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
217     break;
218   case wasm::WASM_SEC_FUNCTION:
219     if (!IO.outputting())
220       Section.reset(new WasmYAML::FunctionSection());
221     sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
222     break;
223   case wasm::WASM_SEC_TABLE:
224     if (!IO.outputting())
225       Section.reset(new WasmYAML::TableSection());
226     sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
227     break;
228   case wasm::WASM_SEC_MEMORY:
229     if (!IO.outputting())
230       Section.reset(new WasmYAML::MemorySection());
231     sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
232     break;
233   case wasm::WASM_SEC_TAG:
234     if (!IO.outputting())
235       Section.reset(new WasmYAML::TagSection());
236     sectionMapping(IO, *cast<WasmYAML::TagSection>(Section.get()));
237     break;
238   case wasm::WASM_SEC_GLOBAL:
239     if (!IO.outputting())
240       Section.reset(new WasmYAML::GlobalSection());
241     sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
242     break;
243   case wasm::WASM_SEC_EXPORT:
244     if (!IO.outputting())
245       Section.reset(new WasmYAML::ExportSection());
246     sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
247     break;
248   case wasm::WASM_SEC_START:
249     if (!IO.outputting())
250       Section.reset(new WasmYAML::StartSection());
251     sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
252     break;
253   case wasm::WASM_SEC_ELEM:
254     if (!IO.outputting())
255       Section.reset(new WasmYAML::ElemSection());
256     sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
257     break;
258   case wasm::WASM_SEC_CODE:
259     if (!IO.outputting())
260       Section.reset(new WasmYAML::CodeSection());
261     sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
262     break;
263   case wasm::WASM_SEC_DATA:
264     if (!IO.outputting())
265       Section.reset(new WasmYAML::DataSection());
266     sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
267     break;
268   case wasm::WASM_SEC_DATACOUNT:
269     if (!IO.outputting())
270       Section.reset(new WasmYAML::DataCountSection());
271     sectionMapping(IO, *cast<WasmYAML::DataCountSection>(Section.get()));
272     break;
273   default:
274     llvm_unreachable("Unknown section type");
275   }
276 }
277 
278 void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
279     IO &IO, WasmYAML::SectionType &Type) {
280 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
281   ECase(CUSTOM);
282   ECase(TYPE);
283   ECase(IMPORT);
284   ECase(FUNCTION);
285   ECase(TABLE);
286   ECase(MEMORY);
287   ECase(GLOBAL);
288   ECase(TAG);
289   ECase(EXPORT);
290   ECase(START);
291   ECase(ELEM);
292   ECase(CODE);
293   ECase(DATA);
294   ECase(DATACOUNT);
295 #undef ECase
296 }
297 
298 void MappingTraits<WasmYAML::Signature>::mapping(
299     IO &IO, WasmYAML::Signature &Signature) {
300   IO.mapRequired("Index", Signature.Index);
301   IO.mapRequired("ParamTypes", Signature.ParamTypes);
302   IO.mapRequired("ReturnTypes", Signature.ReturnTypes);
303 }
304 
305 void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
306   IO.mapRequired("Index", Table.Index);
307   IO.mapRequired("ElemType", Table.ElemType);
308   IO.mapRequired("Limits", Table.TableLimits);
309 }
310 
311 void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
312                                                 WasmYAML::Function &Function) {
313   IO.mapRequired("Index", Function.Index);
314   IO.mapRequired("Locals", Function.Locals);
315   IO.mapRequired("Body", Function.Body);
316 }
317 
318 void MappingTraits<WasmYAML::Relocation>::mapping(
319     IO &IO, WasmYAML::Relocation &Relocation) {
320   IO.mapRequired("Type", Relocation.Type);
321   IO.mapRequired("Index", Relocation.Index);
322   IO.mapRequired("Offset", Relocation.Offset);
323   IO.mapOptional("Addend", Relocation.Addend, 0);
324 }
325 
326 void MappingTraits<WasmYAML::NameEntry>::mapping(
327     IO &IO, WasmYAML::NameEntry &NameEntry) {
328   IO.mapRequired("Index", NameEntry.Index);
329   IO.mapRequired("Name", NameEntry.Name);
330 }
331 
332 void MappingTraits<WasmYAML::ProducerEntry>::mapping(
333     IO &IO, WasmYAML::ProducerEntry &ProducerEntry) {
334   IO.mapRequired("Name", ProducerEntry.Name);
335   IO.mapRequired("Version", ProducerEntry.Version);
336 }
337 
338 void ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix>::enumeration(
339     IO &IO, WasmYAML::FeaturePolicyPrefix &Kind) {
340 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_FEATURE_PREFIX_##X);
341   ECase(USED);
342   ECase(REQUIRED);
343   ECase(DISALLOWED);
344 #undef ECase
345 }
346 
347 void MappingTraits<WasmYAML::FeatureEntry>::mapping(
348     IO &IO, WasmYAML::FeatureEntry &FeatureEntry) {
349   IO.mapRequired("Prefix", FeatureEntry.Prefix);
350   IO.mapRequired("Name", FeatureEntry.Name);
351 }
352 
353 void MappingTraits<WasmYAML::SegmentInfo>::mapping(
354     IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
355   IO.mapRequired("Index", SegmentInfo.Index);
356   IO.mapRequired("Name", SegmentInfo.Name);
357   IO.mapRequired("Alignment", SegmentInfo.Alignment);
358   IO.mapRequired("Flags", SegmentInfo.Flags);
359 }
360 
361 void MappingTraits<WasmYAML::LocalDecl>::mapping(
362     IO &IO, WasmYAML::LocalDecl &LocalDecl) {
363   IO.mapRequired("Type", LocalDecl.Type);
364   IO.mapRequired("Count", LocalDecl.Count);
365 }
366 
367 void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
368                                               WasmYAML::Limits &Limits) {
369   if (!IO.outputting() || Limits.Flags)
370     IO.mapOptional("Flags", Limits.Flags);
371   IO.mapRequired("Minimum", Limits.Minimum);
372   if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
373     IO.mapOptional("Maximum", Limits.Maximum);
374 }
375 
376 void MappingTraits<WasmYAML::ElemSegment>::mapping(
377     IO &IO, WasmYAML::ElemSegment &Segment) {
378   if (!IO.outputting() || Segment.Flags)
379     IO.mapOptional("Flags", Segment.Flags);
380   if (!IO.outputting() ||
381       Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
382     IO.mapOptional("TableNumber", Segment.TableNumber);
383   if (!IO.outputting() ||
384       Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND)
385     IO.mapOptional("ElemKind", Segment.ElemKind);
386   IO.mapRequired("Offset", Segment.Offset);
387   IO.mapRequired("Functions", Segment.Functions);
388 }
389 
390 void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
391                                               WasmYAML::Import &Import) {
392   IO.mapRequired("Module", Import.Module);
393   IO.mapRequired("Field", Import.Field);
394   IO.mapRequired("Kind", Import.Kind);
395   if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION ||
396       Import.Kind == wasm::WASM_EXTERNAL_TAG) {
397     IO.mapRequired("SigIndex", Import.SigIndex);
398   } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
399     IO.mapRequired("GlobalType", Import.GlobalImport.Type);
400     IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
401   } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
402     IO.mapRequired("Table", Import.TableImport);
403   } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) {
404     IO.mapRequired("Memory", Import.Memory);
405   } else {
406     llvm_unreachable("unhandled import type");
407   }
408 }
409 
410 void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
411                                               WasmYAML::Export &Export) {
412   IO.mapRequired("Name", Export.Name);
413   IO.mapRequired("Kind", Export.Kind);
414   IO.mapRequired("Index", Export.Index);
415 }
416 
417 void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
418                                               WasmYAML::Global &Global) {
419   IO.mapRequired("Index", Global.Index);
420   IO.mapRequired("Type", Global.Type);
421   IO.mapRequired("Mutable", Global.Mutable);
422   IO.mapRequired("InitExpr", Global.InitExpr);
423 }
424 
425 void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
426                                                 wasm::WasmInitExpr &Expr) {
427   WasmYAML::Opcode Op = Expr.Opcode;
428   IO.mapRequired("Opcode", Op);
429   Expr.Opcode = Op;
430   switch (Expr.Opcode) {
431   case wasm::WASM_OPCODE_I32_CONST:
432     IO.mapRequired("Value", Expr.Value.Int32);
433     break;
434   case wasm::WASM_OPCODE_I64_CONST:
435     IO.mapRequired("Value", Expr.Value.Int64);
436     break;
437   case wasm::WASM_OPCODE_F32_CONST:
438     IO.mapRequired("Value", Expr.Value.Float32);
439     break;
440   case wasm::WASM_OPCODE_F64_CONST:
441     IO.mapRequired("Value", Expr.Value.Float64);
442     break;
443   case wasm::WASM_OPCODE_GLOBAL_GET:
444     IO.mapRequired("Index", Expr.Value.Global);
445     break;
446   case wasm::WASM_OPCODE_REF_NULL: {
447     WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
448     IO.mapRequired("Type", Ty);
449     break;
450   }
451   }
452 }
453 
454 void MappingTraits<WasmYAML::DataSegment>::mapping(
455     IO &IO, WasmYAML::DataSegment &Segment) {
456   IO.mapOptional("SectionOffset", Segment.SectionOffset);
457   IO.mapRequired("InitFlags", Segment.InitFlags);
458   if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) {
459     IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
460   } else {
461     Segment.MemoryIndex = 0;
462   }
463   if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
464     IO.mapRequired("Offset", Segment.Offset);
465   } else {
466     Segment.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST;
467     Segment.Offset.Value.Int32 = 0;
468   }
469   IO.mapRequired("Content", Segment.Content);
470 }
471 
472 void MappingTraits<WasmYAML::InitFunction>::mapping(
473     IO &IO, WasmYAML::InitFunction &Init) {
474   IO.mapRequired("Priority", Init.Priority);
475   IO.mapRequired("Symbol", Init.Symbol);
476 }
477 
478 void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
479     IO &IO, WasmYAML::ComdatKind &Kind) {
480 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
481   ECase(FUNCTION);
482   ECase(DATA);
483   ECase(SECTION);
484 #undef ECase
485 }
486 
487 void MappingTraits<WasmYAML::ComdatEntry>::mapping(
488     IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
489   IO.mapRequired("Kind", ComdatEntry.Kind);
490   IO.mapRequired("Index", ComdatEntry.Index);
491 }
492 
493 void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO,
494                                               WasmYAML::Comdat &Comdat) {
495   IO.mapRequired("Name", Comdat.Name);
496   IO.mapRequired("Entries", Comdat.Entries);
497 }
498 
499 void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
500                                                   WasmYAML::SymbolInfo &Info) {
501   IO.mapRequired("Index", Info.Index);
502   IO.mapRequired("Kind", Info.Kind);
503   if (Info.Kind != wasm::WASM_SYMBOL_TYPE_SECTION)
504     IO.mapRequired("Name", Info.Name);
505   IO.mapRequired("Flags", Info.Flags);
506   if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) {
507     IO.mapRequired("Function", Info.ElementIndex);
508   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
509     IO.mapRequired("Global", Info.ElementIndex);
510   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TABLE) {
511     IO.mapRequired("Table", Info.ElementIndex);
512   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TAG) {
513     IO.mapRequired("Tag", Info.ElementIndex);
514   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) {
515     if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
516       IO.mapRequired("Segment", Info.DataRef.Segment);
517       IO.mapOptional("Offset", Info.DataRef.Offset, 0u);
518       IO.mapRequired("Size", Info.DataRef.Size);
519     }
520   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) {
521     IO.mapRequired("Section", Info.ElementIndex);
522   } else {
523     llvm_unreachable("unsupported symbol kind");
524   }
525 }
526 
527 void MappingTraits<WasmYAML::DylinkExport>::mapping(
528     IO &IO, WasmYAML::DylinkExport &Export) {
529   IO.mapRequired("Name", Export.Name);
530   IO.mapRequired("Flags", Export.Flags);
531 }
532 
533 void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
534     IO &IO, WasmYAML::LimitFlags &Value) {
535 #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
536   BCase(HAS_MAX);
537   BCase(IS_SHARED);
538   BCase(IS_64);
539 #undef BCase
540 }
541 
542 void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
543     IO &IO, WasmYAML::SegmentFlags &Value) {
544 #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_SEG_FLAG_##X)
545   BCase(STRINGS);
546   BCase(TLS);
547 #undef BCase
548 }
549 
550 void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
551     IO &IO, WasmYAML::SymbolFlags &Value) {
552 #define BCaseMask(M, X)                                                        \
553   IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
554   // BCaseMask(BINDING_MASK, BINDING_GLOBAL);
555   BCaseMask(BINDING_MASK, BINDING_WEAK);
556   BCaseMask(BINDING_MASK, BINDING_LOCAL);
557   // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
558   BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
559   BCaseMask(UNDEFINED, UNDEFINED);
560   BCaseMask(EXPORTED, EXPORTED);
561   BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME);
562   BCaseMask(NO_STRIP, NO_STRIP);
563   BCaseMask(TLS, TLS);
564 #undef BCaseMask
565 }
566 
567 void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration(
568     IO &IO, WasmYAML::SymbolKind &Kind) {
569 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X);
570   ECase(FUNCTION);
571   ECase(DATA);
572   ECase(GLOBAL);
573   ECase(TABLE);
574   ECase(SECTION);
575   ECase(TAG);
576 #undef ECase
577 }
578 
579 void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
580     IO &IO, WasmYAML::ValueType &Type) {
581 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
582   ECase(I32);
583   ECase(I64);
584   ECase(F32);
585   ECase(F64);
586   ECase(V128);
587   ECase(FUNCREF);
588   ECase(EXTERNREF);
589   ECase(FUNC);
590 #undef ECase
591 }
592 
593 void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
594     IO &IO, WasmYAML::ExportKind &Kind) {
595 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
596   ECase(FUNCTION);
597   ECase(TABLE);
598   ECase(MEMORY);
599   ECase(GLOBAL);
600   ECase(TAG);
601 #undef ECase
602 }
603 
604 void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
605     IO &IO, WasmYAML::Opcode &Code) {
606 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
607   ECase(END);
608   ECase(I32_CONST);
609   ECase(I64_CONST);
610   ECase(F64_CONST);
611   ECase(F32_CONST);
612   ECase(GLOBAL_GET);
613   ECase(REF_NULL);
614 #undef ECase
615 }
616 
617 void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
618     IO &IO, WasmYAML::TableType &Type) {
619 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
620   ECase(FUNCREF);
621   ECase(EXTERNREF);
622 #undef ECase
623 }
624 
625 void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
626     IO &IO, WasmYAML::RelocType &Type) {
627 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
628 #include "llvm/BinaryFormat/WasmRelocs.def"
629 #undef WASM_RELOC
630   IO.enumFallback<Hex32>(Type);
631 }
632 
633 } // end namespace yaml
634 
635 } // end namespace llvm
636