WasmYAML.cpp 21 KB

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