WasmYAML.cpp 21 KB

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