xref: /freebsd-src/contrib/llvm-project/llvm/lib/ObjectYAML/WasmYAML.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines classes for handling the YAML representation of wasm.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ObjectYAML/WasmYAML.h"
140b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
15*0fca6ea1SDimitry Andric #include "llvm/BinaryFormat/Wasm.h"
160b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
170b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
180b57cec5SDimitry Andric #include "llvm/Support/YAMLTraits.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace WasmYAML {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric // Declared here rather than in the header to comply with:
250b57cec5SDimitry Andric // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers
260b57cec5SDimitry Andric Section::~Section() = default;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric } // end namespace WasmYAML
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric namespace yaml {
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric void MappingTraits<WasmYAML::FileHeader>::mapping(
330b57cec5SDimitry Andric     IO &IO, WasmYAML::FileHeader &FileHdr) {
340b57cec5SDimitry Andric   IO.mapRequired("Version", FileHdr.Version);
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric void MappingTraits<WasmYAML::Object>::mapping(IO &IO,
380b57cec5SDimitry Andric                                               WasmYAML::Object &Object) {
390b57cec5SDimitry Andric   IO.setContext(&Object);
400b57cec5SDimitry Andric   IO.mapTag("!WASM", true);
410b57cec5SDimitry Andric   IO.mapRequired("FileHeader", Object.Header);
420b57cec5SDimitry Andric   IO.mapOptional("Sections", Object.Sections);
430b57cec5SDimitry Andric   IO.setContext(nullptr);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) {
470b57cec5SDimitry Andric   IO.mapRequired("Type", Section.Type);
480b57cec5SDimitry Andric   IO.mapOptional("Relocations", Section.Relocations);
498a4dda33SDimitry Andric   IO.mapOptional("HeaderSecSizeEncodingLen", Section.HeaderSecSizeEncodingLen);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) {
530b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
540b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
550b57cec5SDimitry Andric   IO.mapRequired("MemorySize", Section.MemorySize);
560b57cec5SDimitry Andric   IO.mapRequired("MemoryAlignment", Section.MemoryAlignment);
570b57cec5SDimitry Andric   IO.mapRequired("TableSize", Section.TableSize);
580b57cec5SDimitry Andric   IO.mapRequired("TableAlignment", Section.TableAlignment);
590b57cec5SDimitry Andric   IO.mapRequired("Needed", Section.Needed);
60349cc55cSDimitry Andric   IO.mapOptional("ImportInfo", Section.ImportInfo);
61349cc55cSDimitry Andric   IO.mapOptional("ExportInfo", Section.ExportInfo);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
650b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
660b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
670b57cec5SDimitry Andric   IO.mapOptional("FunctionNames", Section.FunctionNames);
68e8d8bef9SDimitry Andric   IO.mapOptional("GlobalNames", Section.GlobalNames);
69e8d8bef9SDimitry Andric   IO.mapOptional("DataSegmentNames", Section.DataSegmentNames);
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
730b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
740b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
750b57cec5SDimitry Andric   IO.mapRequired("Version", Section.Version);
760b57cec5SDimitry Andric   IO.mapOptional("SymbolTable", Section.SymbolTable);
770b57cec5SDimitry Andric   IO.mapOptional("SegmentInfo", Section.SegmentInfos);
780b57cec5SDimitry Andric   IO.mapOptional("InitFunctions", Section.InitFunctions);
790b57cec5SDimitry Andric   IO.mapOptional("Comdats", Section.Comdats);
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) {
830b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
840b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
850b57cec5SDimitry Andric   IO.mapOptional("Languages", Section.Languages);
860b57cec5SDimitry Andric   IO.mapOptional("Tools", Section.Tools);
870b57cec5SDimitry Andric   IO.mapOptional("SDKs", Section.SDKs);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::TargetFeaturesSection &Section) {
910b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
920b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
930b57cec5SDimitry Andric   IO.mapRequired("Features", Section.Features);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
970b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
980b57cec5SDimitry Andric   IO.mapRequired("Name", Section.Name);
990b57cec5SDimitry Andric   IO.mapRequired("Payload", Section.Payload);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
1030b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1040b57cec5SDimitry Andric   IO.mapOptional("Signatures", Section.Signatures);
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
1080b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1090b57cec5SDimitry Andric   IO.mapOptional("Imports", Section.Imports);
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
1130b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1140b57cec5SDimitry Andric   IO.mapOptional("FunctionTypes", Section.FunctionTypes);
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
1180b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1190b57cec5SDimitry Andric   IO.mapOptional("Tables", Section.Tables);
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
1230b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1240b57cec5SDimitry Andric   IO.mapOptional("Memories", Section.Memories);
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric 
127fe6060f1SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::TagSection &Section) {
1280b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
129349cc55cSDimitry Andric   IO.mapOptional("TagTypes", Section.TagTypes);
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric 
1325ffd83dbSDimitry Andric static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
1335ffd83dbSDimitry Andric   commonSectionMapping(IO, Section);
1345ffd83dbSDimitry Andric   IO.mapOptional("Globals", Section.Globals);
1355ffd83dbSDimitry Andric }
1365ffd83dbSDimitry Andric 
1370b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
1380b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1390b57cec5SDimitry Andric   IO.mapOptional("Exports", Section.Exports);
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
1430b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1440b57cec5SDimitry Andric   IO.mapOptional("StartFunction", Section.StartFunction);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
1480b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1490b57cec5SDimitry Andric   IO.mapOptional("Segments", Section.Segments);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
1530b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1540b57cec5SDimitry Andric   IO.mapRequired("Functions", Section.Functions);
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
1580b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1590b57cec5SDimitry Andric   IO.mapRequired("Segments", Section.Segments);
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric static void sectionMapping(IO &IO, WasmYAML::DataCountSection &Section) {
1630b57cec5SDimitry Andric   commonSectionMapping(IO, Section);
1640b57cec5SDimitry Andric   IO.mapRequired("Count", Section.Count);
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
1680b57cec5SDimitry Andric     IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
1690b57cec5SDimitry Andric   WasmYAML::SectionType SectionType;
1700b57cec5SDimitry Andric   if (IO.outputting())
1710b57cec5SDimitry Andric     SectionType = Section->Type;
1720b57cec5SDimitry Andric   else
1730b57cec5SDimitry Andric     IO.mapRequired("Type", SectionType);
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   switch (SectionType) {
1760b57cec5SDimitry Andric   case wasm::WASM_SEC_CUSTOM: {
1770b57cec5SDimitry Andric     StringRef SectionName;
1780b57cec5SDimitry Andric     if (IO.outputting()) {
1790b57cec5SDimitry Andric       auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
1800b57cec5SDimitry Andric       SectionName = CustomSection->Name;
1810b57cec5SDimitry Andric     } else {
1820b57cec5SDimitry Andric       IO.mapRequired("Name", SectionName);
1830b57cec5SDimitry Andric     }
184349cc55cSDimitry Andric     if (SectionName == "dylink" || SectionName == "dylink.0") {
1850b57cec5SDimitry Andric       if (!IO.outputting())
1860b57cec5SDimitry Andric         Section.reset(new WasmYAML::DylinkSection());
1870b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get()));
1880b57cec5SDimitry Andric     } else if (SectionName == "linking") {
1890b57cec5SDimitry Andric       if (!IO.outputting())
1900b57cec5SDimitry Andric         Section.reset(new WasmYAML::LinkingSection());
1910b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
1920b57cec5SDimitry Andric     } else if (SectionName == "name") {
1930b57cec5SDimitry Andric       if (!IO.outputting())
1940b57cec5SDimitry Andric         Section.reset(new WasmYAML::NameSection());
1950b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
1960b57cec5SDimitry Andric     } else if (SectionName == "producers") {
1970b57cec5SDimitry Andric       if (!IO.outputting())
1980b57cec5SDimitry Andric         Section.reset(new WasmYAML::ProducersSection());
1990b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get()));
2000b57cec5SDimitry Andric     } else if (SectionName == "target_features") {
2010b57cec5SDimitry Andric       if (!IO.outputting())
2020b57cec5SDimitry Andric         Section.reset(new WasmYAML::TargetFeaturesSection());
2030b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::TargetFeaturesSection>(Section.get()));
2040b57cec5SDimitry Andric     } else {
2050b57cec5SDimitry Andric       if (!IO.outputting())
2060b57cec5SDimitry Andric         Section.reset(new WasmYAML::CustomSection(SectionName));
2070b57cec5SDimitry Andric       sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
2080b57cec5SDimitry Andric     }
2090b57cec5SDimitry Andric     break;
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric   case wasm::WASM_SEC_TYPE:
2120b57cec5SDimitry Andric     if (!IO.outputting())
2130b57cec5SDimitry Andric       Section.reset(new WasmYAML::TypeSection());
2140b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
2150b57cec5SDimitry Andric     break;
2160b57cec5SDimitry Andric   case wasm::WASM_SEC_IMPORT:
2170b57cec5SDimitry Andric     if (!IO.outputting())
2180b57cec5SDimitry Andric       Section.reset(new WasmYAML::ImportSection());
2190b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
2200b57cec5SDimitry Andric     break;
2210b57cec5SDimitry Andric   case wasm::WASM_SEC_FUNCTION:
2220b57cec5SDimitry Andric     if (!IO.outputting())
2230b57cec5SDimitry Andric       Section.reset(new WasmYAML::FunctionSection());
2240b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
2250b57cec5SDimitry Andric     break;
2260b57cec5SDimitry Andric   case wasm::WASM_SEC_TABLE:
2270b57cec5SDimitry Andric     if (!IO.outputting())
2280b57cec5SDimitry Andric       Section.reset(new WasmYAML::TableSection());
2290b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
2300b57cec5SDimitry Andric     break;
2310b57cec5SDimitry Andric   case wasm::WASM_SEC_MEMORY:
2320b57cec5SDimitry Andric     if (!IO.outputting())
2330b57cec5SDimitry Andric       Section.reset(new WasmYAML::MemorySection());
2340b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
2350b57cec5SDimitry Andric     break;
236fe6060f1SDimitry Andric   case wasm::WASM_SEC_TAG:
2370b57cec5SDimitry Andric     if (!IO.outputting())
238fe6060f1SDimitry Andric       Section.reset(new WasmYAML::TagSection());
239fe6060f1SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::TagSection>(Section.get()));
2400b57cec5SDimitry Andric     break;
2415ffd83dbSDimitry Andric   case wasm::WASM_SEC_GLOBAL:
2425ffd83dbSDimitry Andric     if (!IO.outputting())
2435ffd83dbSDimitry Andric       Section.reset(new WasmYAML::GlobalSection());
2445ffd83dbSDimitry Andric     sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
2455ffd83dbSDimitry Andric     break;
2460b57cec5SDimitry Andric   case wasm::WASM_SEC_EXPORT:
2470b57cec5SDimitry Andric     if (!IO.outputting())
2480b57cec5SDimitry Andric       Section.reset(new WasmYAML::ExportSection());
2490b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
2500b57cec5SDimitry Andric     break;
2510b57cec5SDimitry Andric   case wasm::WASM_SEC_START:
2520b57cec5SDimitry Andric     if (!IO.outputting())
2530b57cec5SDimitry Andric       Section.reset(new WasmYAML::StartSection());
2540b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
2550b57cec5SDimitry Andric     break;
2560b57cec5SDimitry Andric   case wasm::WASM_SEC_ELEM:
2570b57cec5SDimitry Andric     if (!IO.outputting())
2580b57cec5SDimitry Andric       Section.reset(new WasmYAML::ElemSection());
2590b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
2600b57cec5SDimitry Andric     break;
2610b57cec5SDimitry Andric   case wasm::WASM_SEC_CODE:
2620b57cec5SDimitry Andric     if (!IO.outputting())
2630b57cec5SDimitry Andric       Section.reset(new WasmYAML::CodeSection());
2640b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
2650b57cec5SDimitry Andric     break;
2660b57cec5SDimitry Andric   case wasm::WASM_SEC_DATA:
2670b57cec5SDimitry Andric     if (!IO.outputting())
2680b57cec5SDimitry Andric       Section.reset(new WasmYAML::DataSection());
2690b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
2700b57cec5SDimitry Andric     break;
2710b57cec5SDimitry Andric   case wasm::WASM_SEC_DATACOUNT:
2720b57cec5SDimitry Andric     if (!IO.outputting())
2730b57cec5SDimitry Andric       Section.reset(new WasmYAML::DataCountSection());
2740b57cec5SDimitry Andric     sectionMapping(IO, *cast<WasmYAML::DataCountSection>(Section.get()));
2750b57cec5SDimitry Andric     break;
2760b57cec5SDimitry Andric   default:
2770b57cec5SDimitry Andric     llvm_unreachable("Unknown section type");
2780b57cec5SDimitry Andric   }
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
2820b57cec5SDimitry Andric     IO &IO, WasmYAML::SectionType &Type) {
2830b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
2840b57cec5SDimitry Andric   ECase(CUSTOM);
2850b57cec5SDimitry Andric   ECase(TYPE);
2860b57cec5SDimitry Andric   ECase(IMPORT);
2870b57cec5SDimitry Andric   ECase(FUNCTION);
2880b57cec5SDimitry Andric   ECase(TABLE);
2890b57cec5SDimitry Andric   ECase(MEMORY);
2900b57cec5SDimitry Andric   ECase(GLOBAL);
291fe6060f1SDimitry Andric   ECase(TAG);
2920b57cec5SDimitry Andric   ECase(EXPORT);
2930b57cec5SDimitry Andric   ECase(START);
2940b57cec5SDimitry Andric   ECase(ELEM);
2950b57cec5SDimitry Andric   ECase(CODE);
2960b57cec5SDimitry Andric   ECase(DATA);
2970b57cec5SDimitry Andric   ECase(DATACOUNT);
2980b57cec5SDimitry Andric #undef ECase
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric void MappingTraits<WasmYAML::Signature>::mapping(
3020b57cec5SDimitry Andric     IO &IO, WasmYAML::Signature &Signature) {
3030b57cec5SDimitry Andric   IO.mapRequired("Index", Signature.Index);
3040b57cec5SDimitry Andric   IO.mapRequired("ParamTypes", Signature.ParamTypes);
3058bcb0991SDimitry Andric   IO.mapRequired("ReturnTypes", Signature.ReturnTypes);
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
309e8d8bef9SDimitry Andric   IO.mapRequired("Index", Table.Index);
3100b57cec5SDimitry Andric   IO.mapRequired("ElemType", Table.ElemType);
3110b57cec5SDimitry Andric   IO.mapRequired("Limits", Table.TableLimits);
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
3150b57cec5SDimitry Andric                                                 WasmYAML::Function &Function) {
3160b57cec5SDimitry Andric   IO.mapRequired("Index", Function.Index);
3170b57cec5SDimitry Andric   IO.mapRequired("Locals", Function.Locals);
3180b57cec5SDimitry Andric   IO.mapRequired("Body", Function.Body);
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric void MappingTraits<WasmYAML::Relocation>::mapping(
3220b57cec5SDimitry Andric     IO &IO, WasmYAML::Relocation &Relocation) {
3230b57cec5SDimitry Andric   IO.mapRequired("Type", Relocation.Type);
3240b57cec5SDimitry Andric   IO.mapRequired("Index", Relocation.Index);
3250b57cec5SDimitry Andric   IO.mapRequired("Offset", Relocation.Offset);
3260b57cec5SDimitry Andric   IO.mapOptional("Addend", Relocation.Addend, 0);
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric void MappingTraits<WasmYAML::NameEntry>::mapping(
3300b57cec5SDimitry Andric     IO &IO, WasmYAML::NameEntry &NameEntry) {
3310b57cec5SDimitry Andric   IO.mapRequired("Index", NameEntry.Index);
3320b57cec5SDimitry Andric   IO.mapRequired("Name", NameEntry.Name);
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric void MappingTraits<WasmYAML::ProducerEntry>::mapping(
3360b57cec5SDimitry Andric     IO &IO, WasmYAML::ProducerEntry &ProducerEntry) {
3370b57cec5SDimitry Andric   IO.mapRequired("Name", ProducerEntry.Name);
3380b57cec5SDimitry Andric   IO.mapRequired("Version", ProducerEntry.Version);
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix>::enumeration(
3420b57cec5SDimitry Andric     IO &IO, WasmYAML::FeaturePolicyPrefix &Kind) {
3430b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_FEATURE_PREFIX_##X);
3440b57cec5SDimitry Andric   ECase(USED);
3450b57cec5SDimitry Andric   ECase(REQUIRED);
3460b57cec5SDimitry Andric   ECase(DISALLOWED);
3470b57cec5SDimitry Andric #undef ECase
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric void MappingTraits<WasmYAML::FeatureEntry>::mapping(
3510b57cec5SDimitry Andric     IO &IO, WasmYAML::FeatureEntry &FeatureEntry) {
3520b57cec5SDimitry Andric   IO.mapRequired("Prefix", FeatureEntry.Prefix);
3530b57cec5SDimitry Andric   IO.mapRequired("Name", FeatureEntry.Name);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric void MappingTraits<WasmYAML::SegmentInfo>::mapping(
3570b57cec5SDimitry Andric     IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
3580b57cec5SDimitry Andric   IO.mapRequired("Index", SegmentInfo.Index);
3590b57cec5SDimitry Andric   IO.mapRequired("Name", SegmentInfo.Name);
3600b57cec5SDimitry Andric   IO.mapRequired("Alignment", SegmentInfo.Alignment);
3610b57cec5SDimitry Andric   IO.mapRequired("Flags", SegmentInfo.Flags);
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric void MappingTraits<WasmYAML::LocalDecl>::mapping(
3650b57cec5SDimitry Andric     IO &IO, WasmYAML::LocalDecl &LocalDecl) {
3660b57cec5SDimitry Andric   IO.mapRequired("Type", LocalDecl.Type);
3670b57cec5SDimitry Andric   IO.mapRequired("Count", LocalDecl.Count);
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
3710b57cec5SDimitry Andric                                               WasmYAML::Limits &Limits) {
37281ad6265SDimitry Andric   IO.mapOptional("Flags", Limits.Flags, 0);
373fe6060f1SDimitry Andric   IO.mapRequired("Minimum", Limits.Minimum);
3740b57cec5SDimitry Andric   if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
3750b57cec5SDimitry Andric     IO.mapOptional("Maximum", Limits.Maximum);
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric void MappingTraits<WasmYAML::ElemSegment>::mapping(
3790b57cec5SDimitry Andric     IO &IO, WasmYAML::ElemSegment &Segment) {
38081ad6265SDimitry Andric   IO.mapOptional("Flags", Segment.Flags, 0);
381fe6060f1SDimitry Andric   if (!IO.outputting() ||
382fe6060f1SDimitry Andric       Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
383fe6060f1SDimitry Andric     IO.mapOptional("TableNumber", Segment.TableNumber);
384fe6060f1SDimitry Andric   if (!IO.outputting() ||
385fe6060f1SDimitry Andric       Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND)
386fe6060f1SDimitry Andric     IO.mapOptional("ElemKind", Segment.ElemKind);
387*0fca6ea1SDimitry Andric   // TODO: Omit "offset" for passive segments? It's neither meaningful nor
388*0fca6ea1SDimitry Andric   // encoded.
3890b57cec5SDimitry Andric   IO.mapRequired("Offset", Segment.Offset);
3900b57cec5SDimitry Andric   IO.mapRequired("Functions", Segment.Functions);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
3940b57cec5SDimitry Andric                                               WasmYAML::Import &Import) {
3950b57cec5SDimitry Andric   IO.mapRequired("Module", Import.Module);
3960b57cec5SDimitry Andric   IO.mapRequired("Field", Import.Field);
3970b57cec5SDimitry Andric   IO.mapRequired("Kind", Import.Kind);
398349cc55cSDimitry Andric   if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION ||
399349cc55cSDimitry Andric       Import.Kind == wasm::WASM_EXTERNAL_TAG) {
4000b57cec5SDimitry Andric     IO.mapRequired("SigIndex", Import.SigIndex);
4010b57cec5SDimitry Andric   } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
4020b57cec5SDimitry Andric     IO.mapRequired("GlobalType", Import.GlobalImport.Type);
4030b57cec5SDimitry Andric     IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
4040b57cec5SDimitry Andric   } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
4050b57cec5SDimitry Andric     IO.mapRequired("Table", Import.TableImport);
4060b57cec5SDimitry Andric   } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) {
4070b57cec5SDimitry Andric     IO.mapRequired("Memory", Import.Memory);
4080b57cec5SDimitry Andric   } else {
4090b57cec5SDimitry Andric     llvm_unreachable("unhandled import type");
4100b57cec5SDimitry Andric   }
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
4140b57cec5SDimitry Andric                                               WasmYAML::Export &Export) {
4150b57cec5SDimitry Andric   IO.mapRequired("Name", Export.Name);
4160b57cec5SDimitry Andric   IO.mapRequired("Kind", Export.Kind);
4170b57cec5SDimitry Andric   IO.mapRequired("Index", Export.Index);
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
4210b57cec5SDimitry Andric                                               WasmYAML::Global &Global) {
4220b57cec5SDimitry Andric   IO.mapRequired("Index", Global.Index);
4230b57cec5SDimitry Andric   IO.mapRequired("Type", Global.Type);
4240b57cec5SDimitry Andric   IO.mapRequired("Mutable", Global.Mutable);
42581ad6265SDimitry Andric   IO.mapRequired("InitExpr", Global.Init);
4260b57cec5SDimitry Andric }
4270b57cec5SDimitry Andric 
42881ad6265SDimitry Andric void MappingTraits<WasmYAML::InitExpr>::mapping(IO &IO,
42981ad6265SDimitry Andric                                                 WasmYAML::InitExpr &Expr) {
43081ad6265SDimitry Andric   IO.mapOptional("Extended", Expr.Extended, false);
43181ad6265SDimitry Andric   if (Expr.Extended) {
43281ad6265SDimitry Andric     IO.mapRequired("Body", Expr.Body);
43381ad6265SDimitry Andric   } else {
43481ad6265SDimitry Andric     WasmYAML::Opcode Op = Expr.Inst.Opcode;
4350b57cec5SDimitry Andric     IO.mapRequired("Opcode", Op);
43681ad6265SDimitry Andric     Expr.Inst.Opcode = Op;
43781ad6265SDimitry Andric     switch (Expr.Inst.Opcode) {
4380b57cec5SDimitry Andric     case wasm::WASM_OPCODE_I32_CONST:
43981ad6265SDimitry Andric       IO.mapRequired("Value", Expr.Inst.Value.Int32);
4400b57cec5SDimitry Andric       break;
4410b57cec5SDimitry Andric     case wasm::WASM_OPCODE_I64_CONST:
44281ad6265SDimitry Andric       IO.mapRequired("Value", Expr.Inst.Value.Int64);
4430b57cec5SDimitry Andric       break;
4440b57cec5SDimitry Andric     case wasm::WASM_OPCODE_F32_CONST:
44581ad6265SDimitry Andric       IO.mapRequired("Value", Expr.Inst.Value.Float32);
4460b57cec5SDimitry Andric       break;
4470b57cec5SDimitry Andric     case wasm::WASM_OPCODE_F64_CONST:
44881ad6265SDimitry Andric       IO.mapRequired("Value", Expr.Inst.Value.Float64);
4490b57cec5SDimitry Andric       break;
4500b57cec5SDimitry Andric     case wasm::WASM_OPCODE_GLOBAL_GET:
45181ad6265SDimitry Andric       IO.mapRequired("Index", Expr.Inst.Value.Global);
4520b57cec5SDimitry Andric       break;
4535ffd83dbSDimitry Andric     case wasm::WASM_OPCODE_REF_NULL: {
4545ffd83dbSDimitry Andric       WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
4555ffd83dbSDimitry Andric       IO.mapRequired("Type", Ty);
4565ffd83dbSDimitry Andric       break;
4575ffd83dbSDimitry Andric     }
4580b57cec5SDimitry Andric     }
4590b57cec5SDimitry Andric   }
46081ad6265SDimitry Andric }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric void MappingTraits<WasmYAML::DataSegment>::mapping(
4630b57cec5SDimitry Andric     IO &IO, WasmYAML::DataSegment &Segment) {
4640b57cec5SDimitry Andric   IO.mapOptional("SectionOffset", Segment.SectionOffset);
4650b57cec5SDimitry Andric   IO.mapRequired("InitFlags", Segment.InitFlags);
466e8d8bef9SDimitry Andric   if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) {
4670b57cec5SDimitry Andric     IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
4680b57cec5SDimitry Andric   } else {
4690b57cec5SDimitry Andric     Segment.MemoryIndex = 0;
4700b57cec5SDimitry Andric   }
471e8d8bef9SDimitry Andric   if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
4720b57cec5SDimitry Andric     IO.mapRequired("Offset", Segment.Offset);
4730b57cec5SDimitry Andric   } else {
47481ad6265SDimitry Andric     Segment.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST;
47581ad6265SDimitry Andric     Segment.Offset.Inst.Value.Int32 = 0;
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric   IO.mapRequired("Content", Segment.Content);
4780b57cec5SDimitry Andric }
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric void MappingTraits<WasmYAML::InitFunction>::mapping(
4810b57cec5SDimitry Andric     IO &IO, WasmYAML::InitFunction &Init) {
4820b57cec5SDimitry Andric   IO.mapRequired("Priority", Init.Priority);
4830b57cec5SDimitry Andric   IO.mapRequired("Symbol", Init.Symbol);
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
4870b57cec5SDimitry Andric     IO &IO, WasmYAML::ComdatKind &Kind) {
4880b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
4890b57cec5SDimitry Andric   ECase(FUNCTION);
4900b57cec5SDimitry Andric   ECase(DATA);
491e8d8bef9SDimitry Andric   ECase(SECTION);
4920b57cec5SDimitry Andric #undef ECase
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric void MappingTraits<WasmYAML::ComdatEntry>::mapping(
4960b57cec5SDimitry Andric     IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
4970b57cec5SDimitry Andric   IO.mapRequired("Kind", ComdatEntry.Kind);
4980b57cec5SDimitry Andric   IO.mapRequired("Index", ComdatEntry.Index);
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO,
5020b57cec5SDimitry Andric                                               WasmYAML::Comdat &Comdat) {
5030b57cec5SDimitry Andric   IO.mapRequired("Name", Comdat.Name);
5040b57cec5SDimitry Andric   IO.mapRequired("Entries", Comdat.Entries);
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
5080b57cec5SDimitry Andric                                                   WasmYAML::SymbolInfo &Info) {
5090b57cec5SDimitry Andric   IO.mapRequired("Index", Info.Index);
5100b57cec5SDimitry Andric   IO.mapRequired("Kind", Info.Kind);
5110b57cec5SDimitry Andric   if (Info.Kind != wasm::WASM_SYMBOL_TYPE_SECTION)
5120b57cec5SDimitry Andric     IO.mapRequired("Name", Info.Name);
5130b57cec5SDimitry Andric   IO.mapRequired("Flags", Info.Flags);
5140b57cec5SDimitry Andric   if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) {
5150b57cec5SDimitry Andric     IO.mapRequired("Function", Info.ElementIndex);
5160b57cec5SDimitry Andric   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
5170b57cec5SDimitry Andric     IO.mapRequired("Global", Info.ElementIndex);
518e8d8bef9SDimitry Andric   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TABLE) {
519e8d8bef9SDimitry Andric     IO.mapRequired("Table", Info.ElementIndex);
520fe6060f1SDimitry Andric   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TAG) {
521fe6060f1SDimitry Andric     IO.mapRequired("Tag", Info.ElementIndex);
5220b57cec5SDimitry Andric   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) {
5230b57cec5SDimitry Andric     if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
5245f757f3fSDimitry Andric       if ((Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE) == 0) {
5250b57cec5SDimitry Andric         IO.mapRequired("Segment", Info.DataRef.Segment);
5265f757f3fSDimitry Andric       }
5270b57cec5SDimitry Andric       IO.mapOptional("Offset", Info.DataRef.Offset, 0u);
5280b57cec5SDimitry Andric       IO.mapRequired("Size", Info.DataRef.Size);
5290b57cec5SDimitry Andric     }
5300b57cec5SDimitry Andric   } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) {
5310b57cec5SDimitry Andric     IO.mapRequired("Section", Info.ElementIndex);
5320b57cec5SDimitry Andric   } else {
5330b57cec5SDimitry Andric     llvm_unreachable("unsupported symbol kind");
5340b57cec5SDimitry Andric   }
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric 
537349cc55cSDimitry Andric void MappingTraits<WasmYAML::DylinkImportInfo>::mapping(
538349cc55cSDimitry Andric     IO &IO, WasmYAML::DylinkImportInfo &Info) {
539349cc55cSDimitry Andric   IO.mapRequired("Module", Info.Module);
540349cc55cSDimitry Andric   IO.mapRequired("Field", Info.Field);
541349cc55cSDimitry Andric   IO.mapRequired("Flags", Info.Flags);
542349cc55cSDimitry Andric }
543349cc55cSDimitry Andric 
544349cc55cSDimitry Andric void MappingTraits<WasmYAML::DylinkExportInfo>::mapping(
545349cc55cSDimitry Andric     IO &IO, WasmYAML::DylinkExportInfo &Info) {
546349cc55cSDimitry Andric   IO.mapRequired("Name", Info.Name);
547349cc55cSDimitry Andric   IO.mapRequired("Flags", Info.Flags);
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
5510b57cec5SDimitry Andric     IO &IO, WasmYAML::LimitFlags &Value) {
5520b57cec5SDimitry Andric #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
5530b57cec5SDimitry Andric   BCase(HAS_MAX);
5540b57cec5SDimitry Andric   BCase(IS_SHARED);
5555ffd83dbSDimitry Andric   BCase(IS_64);
5560b57cec5SDimitry Andric #undef BCase
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
560fe6060f1SDimitry Andric     IO &IO, WasmYAML::SegmentFlags &Value) {
561fe6060f1SDimitry Andric #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_SEG_FLAG_##X)
562fe6060f1SDimitry Andric   BCase(STRINGS);
563fe6060f1SDimitry Andric   BCase(TLS);
564*0fca6ea1SDimitry Andric   BCase(RETAIN);
565fe6060f1SDimitry Andric #undef BCase
566fe6060f1SDimitry Andric }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
5690b57cec5SDimitry Andric     IO &IO, WasmYAML::SymbolFlags &Value) {
5700b57cec5SDimitry Andric #define BCaseMask(M, X)                                                        \
5710b57cec5SDimitry Andric   IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
5720b57cec5SDimitry Andric   // BCaseMask(BINDING_MASK, BINDING_GLOBAL);
5730b57cec5SDimitry Andric   BCaseMask(BINDING_MASK, BINDING_WEAK);
5740b57cec5SDimitry Andric   BCaseMask(BINDING_MASK, BINDING_LOCAL);
5750b57cec5SDimitry Andric   // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
5760b57cec5SDimitry Andric   BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
5770b57cec5SDimitry Andric   BCaseMask(UNDEFINED, UNDEFINED);
5780b57cec5SDimitry Andric   BCaseMask(EXPORTED, EXPORTED);
5790b57cec5SDimitry Andric   BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME);
5808bcb0991SDimitry Andric   BCaseMask(NO_STRIP, NO_STRIP);
581349cc55cSDimitry Andric   BCaseMask(TLS, TLS);
5825f757f3fSDimitry Andric   BCaseMask(ABSOLUTE, ABSOLUTE);
5830b57cec5SDimitry Andric #undef BCaseMask
5840b57cec5SDimitry Andric }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration(
5870b57cec5SDimitry Andric     IO &IO, WasmYAML::SymbolKind &Kind) {
5880b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X);
5890b57cec5SDimitry Andric   ECase(FUNCTION);
5900b57cec5SDimitry Andric   ECase(DATA);
5910b57cec5SDimitry Andric   ECase(GLOBAL);
592e8d8bef9SDimitry Andric   ECase(TABLE);
5930b57cec5SDimitry Andric   ECase(SECTION);
594fe6060f1SDimitry Andric   ECase(TAG);
5950b57cec5SDimitry Andric #undef ECase
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
5990b57cec5SDimitry Andric     IO &IO, WasmYAML::ValueType &Type) {
600*0fca6ea1SDimitry Andric #define CONCAT(X) (uint32_t) wasm::ValType::X
601*0fca6ea1SDimitry Andric #define ECase(X) IO.enumCase(Type, #X, CONCAT(X));
6020b57cec5SDimitry Andric   ECase(I32);
6030b57cec5SDimitry Andric   ECase(I64);
6040b57cec5SDimitry Andric   ECase(F32);
6050b57cec5SDimitry Andric   ECase(F64);
6060b57cec5SDimitry Andric   ECase(V128);
6070b57cec5SDimitry Andric   ECase(FUNCREF);
6085ffd83dbSDimitry Andric   ECase(EXTERNREF);
609*0fca6ea1SDimitry Andric   ECase(EXNREF);
610*0fca6ea1SDimitry Andric   ECase(OTHERREF);
6110b57cec5SDimitry Andric #undef ECase
6120b57cec5SDimitry Andric }
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
6150b57cec5SDimitry Andric     IO &IO, WasmYAML::ExportKind &Kind) {
6160b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
6170b57cec5SDimitry Andric   ECase(FUNCTION);
6180b57cec5SDimitry Andric   ECase(TABLE);
6190b57cec5SDimitry Andric   ECase(MEMORY);
6200b57cec5SDimitry Andric   ECase(GLOBAL);
621fe6060f1SDimitry Andric   ECase(TAG);
6220b57cec5SDimitry Andric #undef ECase
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
6260b57cec5SDimitry Andric     IO &IO, WasmYAML::Opcode &Code) {
6270b57cec5SDimitry Andric #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
6280b57cec5SDimitry Andric   ECase(END);
6290b57cec5SDimitry Andric   ECase(I32_CONST);
6300b57cec5SDimitry Andric   ECase(I64_CONST);
6310b57cec5SDimitry Andric   ECase(F64_CONST);
6320b57cec5SDimitry Andric   ECase(F32_CONST);
6330b57cec5SDimitry Andric   ECase(GLOBAL_GET);
6345ffd83dbSDimitry Andric   ECase(REF_NULL);
6350b57cec5SDimitry Andric #undef ECase
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
6390b57cec5SDimitry Andric     IO &IO, WasmYAML::TableType &Type) {
640*0fca6ea1SDimitry Andric #define CONCAT(X) (uint32_t) wasm::ValType::X
641*0fca6ea1SDimitry Andric #define ECase(X) IO.enumCase(Type, #X, CONCAT(X));
6420b57cec5SDimitry Andric   ECase(FUNCREF);
643e8d8bef9SDimitry Andric   ECase(EXTERNREF);
644*0fca6ea1SDimitry Andric   ECase(EXNREF);
645*0fca6ea1SDimitry Andric   ECase(OTHERREF);
6460b57cec5SDimitry Andric #undef ECase
6470b57cec5SDimitry Andric }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
6500b57cec5SDimitry Andric     IO &IO, WasmYAML::RelocType &Type) {
6510b57cec5SDimitry Andric #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
6520b57cec5SDimitry Andric #include "llvm/BinaryFormat/WasmRelocs.def"
6530b57cec5SDimitry Andric #undef WASM_RELOC
654fe6060f1SDimitry Andric   IO.enumFallback<Hex32>(Type);
6550b57cec5SDimitry Andric }
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric } // end namespace yaml
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric } // end namespace llvm
660