WasmYAML.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. IO.mapOptional("Flags", Limits.Flags, 0);
  329. IO.mapRequired("Minimum", Limits.Minimum);
  330. if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
  331. IO.mapOptional("Maximum", Limits.Maximum);
  332. }
  333. void MappingTraits<WasmYAML::ElemSegment>::mapping(
  334. IO &IO, WasmYAML::ElemSegment &Segment) {
  335. IO.mapOptional("Flags", Segment.Flags, 0);
  336. if (!IO.outputting() ||
  337. Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
  338. IO.mapOptional("TableNumber", Segment.TableNumber);
  339. if (!IO.outputting() ||
  340. Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND)
  341. IO.mapOptional("ElemKind", Segment.ElemKind);
  342. IO.mapRequired("Offset", Segment.Offset);
  343. IO.mapRequired("Functions", Segment.Functions);
  344. }
  345. void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
  346. WasmYAML::Import &Import) {
  347. IO.mapRequired("Module", Import.Module);
  348. IO.mapRequired("Field", Import.Field);
  349. IO.mapRequired("Kind", Import.Kind);
  350. if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION ||
  351. Import.Kind == wasm::WASM_EXTERNAL_TAG) {
  352. IO.mapRequired("SigIndex", Import.SigIndex);
  353. } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
  354. IO.mapRequired("GlobalType", Import.GlobalImport.Type);
  355. IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
  356. } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
  357. IO.mapRequired("Table", Import.TableImport);
  358. } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) {
  359. IO.mapRequired("Memory", Import.Memory);
  360. } else {
  361. llvm_unreachable("unhandled import type");
  362. }
  363. }
  364. void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
  365. WasmYAML::Export &Export) {
  366. IO.mapRequired("Name", Export.Name);
  367. IO.mapRequired("Kind", Export.Kind);
  368. IO.mapRequired("Index", Export.Index);
  369. }
  370. void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
  371. WasmYAML::Global &Global) {
  372. IO.mapRequired("Index", Global.Index);
  373. IO.mapRequired("Type", Global.Type);
  374. IO.mapRequired("Mutable", Global.Mutable);
  375. IO.mapRequired("InitExpr", Global.Init);
  376. }
  377. void MappingTraits<WasmYAML::InitExpr>::mapping(IO &IO,
  378. WasmYAML::InitExpr &Expr) {
  379. IO.mapOptional("Extended", Expr.Extended, false);
  380. if (Expr.Extended) {
  381. IO.mapRequired("Body", Expr.Body);
  382. } else {
  383. WasmYAML::Opcode Op = Expr.Inst.Opcode;
  384. IO.mapRequired("Opcode", Op);
  385. Expr.Inst.Opcode = Op;
  386. switch (Expr.Inst.Opcode) {
  387. case wasm::WASM_OPCODE_I32_CONST:
  388. IO.mapRequired("Value", Expr.Inst.Value.Int32);
  389. break;
  390. case wasm::WASM_OPCODE_I64_CONST:
  391. IO.mapRequired("Value", Expr.Inst.Value.Int64);
  392. break;
  393. case wasm::WASM_OPCODE_F32_CONST:
  394. IO.mapRequired("Value", Expr.Inst.Value.Float32);
  395. break;
  396. case wasm::WASM_OPCODE_F64_CONST:
  397. IO.mapRequired("Value", Expr.Inst.Value.Float64);
  398. break;
  399. case wasm::WASM_OPCODE_GLOBAL_GET:
  400. IO.mapRequired("Index", Expr.Inst.Value.Global);
  401. break;
  402. case wasm::WASM_OPCODE_REF_NULL: {
  403. WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
  404. IO.mapRequired("Type", Ty);
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. void MappingTraits<WasmYAML::DataSegment>::mapping(
  411. IO &IO, WasmYAML::DataSegment &Segment) {
  412. IO.mapOptional("SectionOffset", Segment.SectionOffset);
  413. IO.mapRequired("InitFlags", Segment.InitFlags);
  414. if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) {
  415. IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
  416. } else {
  417. Segment.MemoryIndex = 0;
  418. }
  419. if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
  420. IO.mapRequired("Offset", Segment.Offset);
  421. } else {
  422. Segment.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST;
  423. Segment.Offset.Inst.Value.Int32 = 0;
  424. }
  425. IO.mapRequired("Content", Segment.Content);
  426. }
  427. void MappingTraits<WasmYAML::InitFunction>::mapping(
  428. IO &IO, WasmYAML::InitFunction &Init) {
  429. IO.mapRequired("Priority", Init.Priority);
  430. IO.mapRequired("Symbol", Init.Symbol);
  431. }
  432. void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
  433. IO &IO, WasmYAML::ComdatKind &Kind) {
  434. #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
  435. ECase(FUNCTION);
  436. ECase(DATA);
  437. ECase(SECTION);
  438. #undef ECase
  439. }
  440. void MappingTraits<WasmYAML::ComdatEntry>::mapping(
  441. IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
  442. IO.mapRequired("Kind", ComdatEntry.Kind);
  443. IO.mapRequired("Index", ComdatEntry.Index);
  444. }
  445. void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO,
  446. WasmYAML::Comdat &Comdat) {
  447. IO.mapRequired("Name", Comdat.Name);
  448. IO.mapRequired("Entries", Comdat.Entries);
  449. }
  450. void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
  451. WasmYAML::SymbolInfo &Info) {
  452. IO.mapRequired("Index", Info.Index);
  453. IO.mapRequired("Kind", Info.Kind);
  454. if (Info.Kind != wasm::WASM_SYMBOL_TYPE_SECTION)
  455. IO.mapRequired("Name", Info.Name);
  456. IO.mapRequired("Flags", Info.Flags);
  457. if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) {
  458. IO.mapRequired("Function", Info.ElementIndex);
  459. } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
  460. IO.mapRequired("Global", Info.ElementIndex);
  461. } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TABLE) {
  462. IO.mapRequired("Table", Info.ElementIndex);
  463. } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_TAG) {
  464. IO.mapRequired("Tag", Info.ElementIndex);
  465. } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) {
  466. if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
  467. IO.mapRequired("Segment", Info.DataRef.Segment);
  468. IO.mapOptional("Offset", Info.DataRef.Offset, 0u);
  469. IO.mapRequired("Size", Info.DataRef.Size);
  470. }
  471. } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) {
  472. IO.mapRequired("Section", Info.ElementIndex);
  473. } else {
  474. llvm_unreachable("unsupported symbol kind");
  475. }
  476. }
  477. void MappingTraits<WasmYAML::DylinkImportInfo>::mapping(
  478. IO &IO, WasmYAML::DylinkImportInfo &Info) {
  479. IO.mapRequired("Module", Info.Module);
  480. IO.mapRequired("Field", Info.Field);
  481. IO.mapRequired("Flags", Info.Flags);
  482. }
  483. void MappingTraits<WasmYAML::DylinkExportInfo>::mapping(
  484. IO &IO, WasmYAML::DylinkExportInfo &Info) {
  485. IO.mapRequired("Name", Info.Name);
  486. IO.mapRequired("Flags", Info.Flags);
  487. }
  488. void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
  489. IO &IO, WasmYAML::LimitFlags &Value) {
  490. #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
  491. BCase(HAS_MAX);
  492. BCase(IS_SHARED);
  493. BCase(IS_64);
  494. #undef BCase
  495. }
  496. void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
  497. IO &IO, WasmYAML::SegmentFlags &Value) {
  498. #define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_SEG_FLAG_##X)
  499. BCase(STRINGS);
  500. BCase(TLS);
  501. #undef BCase
  502. }
  503. void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
  504. IO &IO, WasmYAML::SymbolFlags &Value) {
  505. #define BCaseMask(M, X) \
  506. IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
  507. // BCaseMask(BINDING_MASK, BINDING_GLOBAL);
  508. BCaseMask(BINDING_MASK, BINDING_WEAK);
  509. BCaseMask(BINDING_MASK, BINDING_LOCAL);
  510. // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
  511. BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
  512. BCaseMask(UNDEFINED, UNDEFINED);
  513. BCaseMask(EXPORTED, EXPORTED);
  514. BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME);
  515. BCaseMask(NO_STRIP, NO_STRIP);
  516. BCaseMask(TLS, TLS);
  517. #undef BCaseMask
  518. }
  519. void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration(
  520. IO &IO, WasmYAML::SymbolKind &Kind) {
  521. #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X);
  522. ECase(FUNCTION);
  523. ECase(DATA);
  524. ECase(GLOBAL);
  525. ECase(TABLE);
  526. ECase(SECTION);
  527. ECase(TAG);
  528. #undef ECase
  529. }
  530. void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
  531. IO &IO, WasmYAML::ValueType &Type) {
  532. #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
  533. ECase(I32);
  534. ECase(I64);
  535. ECase(F32);
  536. ECase(F64);
  537. ECase(V128);
  538. ECase(FUNCREF);
  539. ECase(EXTERNREF);
  540. ECase(FUNC);
  541. #undef ECase
  542. }
  543. void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
  544. IO &IO, WasmYAML::ExportKind &Kind) {
  545. #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
  546. ECase(FUNCTION);
  547. ECase(TABLE);
  548. ECase(MEMORY);
  549. ECase(GLOBAL);
  550. ECase(TAG);
  551. #undef ECase
  552. }
  553. void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
  554. IO &IO, WasmYAML::Opcode &Code) {
  555. #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
  556. ECase(END);
  557. ECase(I32_CONST);
  558. ECase(I64_CONST);
  559. ECase(F64_CONST);
  560. ECase(F32_CONST);
  561. ECase(GLOBAL_GET);
  562. ECase(REF_NULL);
  563. #undef ECase
  564. }
  565. void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
  566. IO &IO, WasmYAML::TableType &Type) {
  567. #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
  568. ECase(FUNCREF);
  569. ECase(EXTERNREF);
  570. #undef ECase
  571. }
  572. void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
  573. IO &IO, WasmYAML::RelocType &Type) {
  574. #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
  575. #include "llvm/BinaryFormat/WasmRelocs.def"
  576. #undef WASM_RELOC
  577. IO.enumFallback<Hex32>(Type);
  578. }
  579. } // end namespace yaml
  580. } // end namespace llvm