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