WasmYAML.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file declares classes for handling the YAML representation
  16. /// of wasm binaries.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_OBJECTYAML_WASMYAML_H
  20. #define LLVM_OBJECTYAML_WASMYAML_H
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/BinaryFormat/Wasm.h"
  23. #include "llvm/ObjectYAML/YAML.h"
  24. #include "llvm/Support/Casting.h"
  25. #include <cstdint>
  26. #include <memory>
  27. #include <vector>
  28. namespace llvm {
  29. namespace WasmYAML {
  30. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
  31. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
  32. LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
  33. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
  34. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
  35. LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
  36. LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
  37. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
  38. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
  39. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
  40. LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
  41. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
  42. LLVM_YAML_STRONG_TYPEDEF(uint32_t, FeaturePolicyPrefix)
  43. struct FileHeader {
  44. yaml::Hex32 Version;
  45. };
  46. struct Limits {
  47. LimitFlags Flags;
  48. yaml::Hex32 Minimum;
  49. yaml::Hex32 Maximum;
  50. };
  51. struct Table {
  52. TableType ElemType;
  53. Limits TableLimits;
  54. uint32_t Index;
  55. };
  56. struct Export {
  57. StringRef Name;
  58. ExportKind Kind;
  59. uint32_t Index;
  60. };
  61. struct InitExpr {
  62. InitExpr() {}
  63. bool Extended;
  64. union {
  65. wasm::WasmInitExprMVP Inst;
  66. yaml::BinaryRef Body;
  67. };
  68. };
  69. struct ElemSegment {
  70. uint32_t Flags;
  71. uint32_t TableNumber;
  72. ValueType ElemKind;
  73. InitExpr Offset;
  74. std::vector<uint32_t> Functions;
  75. };
  76. struct Global {
  77. uint32_t Index;
  78. ValueType Type;
  79. bool Mutable;
  80. InitExpr Init;
  81. };
  82. struct Import {
  83. Import() {}
  84. StringRef Module;
  85. StringRef Field;
  86. ExportKind Kind;
  87. union {
  88. uint32_t SigIndex;
  89. Table TableImport;
  90. Limits Memory;
  91. uint32_t TagIndex;
  92. Global GlobalImport;
  93. };
  94. };
  95. struct LocalDecl {
  96. ValueType Type;
  97. uint32_t Count;
  98. };
  99. struct Function {
  100. uint32_t Index;
  101. std::vector<LocalDecl> Locals;
  102. yaml::BinaryRef Body;
  103. };
  104. struct Relocation {
  105. RelocType Type;
  106. uint32_t Index;
  107. // TODO(wvo): this would strictly be better as Hex64, but that will change
  108. // all existing obj2yaml output.
  109. yaml::Hex32 Offset;
  110. int64_t Addend;
  111. };
  112. struct DataSegment {
  113. uint32_t SectionOffset;
  114. uint32_t InitFlags;
  115. uint32_t MemoryIndex;
  116. InitExpr Offset;
  117. yaml::BinaryRef Content;
  118. };
  119. struct NameEntry {
  120. uint32_t Index;
  121. StringRef Name;
  122. };
  123. struct ProducerEntry {
  124. std::string Name;
  125. std::string Version;
  126. };
  127. struct FeatureEntry {
  128. FeaturePolicyPrefix Prefix;
  129. std::string Name;
  130. };
  131. struct SegmentInfo {
  132. uint32_t Index;
  133. StringRef Name;
  134. uint32_t Alignment;
  135. SegmentFlags Flags;
  136. };
  137. struct Signature {
  138. uint32_t Index;
  139. SignatureForm Form = wasm::WASM_TYPE_FUNC;
  140. std::vector<ValueType> ParamTypes;
  141. std::vector<ValueType> ReturnTypes;
  142. };
  143. struct SymbolInfo {
  144. uint32_t Index;
  145. StringRef Name;
  146. SymbolKind Kind;
  147. SymbolFlags Flags;
  148. union {
  149. uint32_t ElementIndex;
  150. wasm::WasmDataReference DataRef;
  151. };
  152. };
  153. struct InitFunction {
  154. uint32_t Priority;
  155. uint32_t Symbol;
  156. };
  157. struct ComdatEntry {
  158. ComdatKind Kind;
  159. uint32_t Index;
  160. };
  161. struct Comdat {
  162. StringRef Name;
  163. std::vector<ComdatEntry> Entries;
  164. };
  165. struct Section {
  166. explicit Section(SectionType SecType) : Type(SecType) {}
  167. virtual ~Section();
  168. SectionType Type;
  169. std::vector<Relocation> Relocations;
  170. };
  171. struct CustomSection : Section {
  172. explicit CustomSection(StringRef Name)
  173. : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
  174. static bool classof(const Section *S) {
  175. return S->Type == wasm::WASM_SEC_CUSTOM;
  176. }
  177. StringRef Name;
  178. yaml::BinaryRef Payload;
  179. };
  180. struct DylinkImportInfo {
  181. StringRef Module;
  182. StringRef Field;
  183. SymbolFlags Flags;
  184. };
  185. struct DylinkExportInfo {
  186. StringRef Name;
  187. SymbolFlags Flags;
  188. };
  189. struct DylinkSection : CustomSection {
  190. DylinkSection() : CustomSection("dylink.0") {}
  191. static bool classof(const Section *S) {
  192. auto C = dyn_cast<CustomSection>(S);
  193. return C && C->Name == "dylink.0";
  194. }
  195. uint32_t MemorySize;
  196. uint32_t MemoryAlignment;
  197. uint32_t TableSize;
  198. uint32_t TableAlignment;
  199. std::vector<StringRef> Needed;
  200. std::vector<DylinkImportInfo> ImportInfo;
  201. std::vector<DylinkExportInfo> ExportInfo;
  202. };
  203. struct NameSection : CustomSection {
  204. NameSection() : CustomSection("name") {}
  205. static bool classof(const Section *S) {
  206. auto C = dyn_cast<CustomSection>(S);
  207. return C && C->Name == "name";
  208. }
  209. std::vector<NameEntry> FunctionNames;
  210. std::vector<NameEntry> GlobalNames;
  211. std::vector<NameEntry> DataSegmentNames;
  212. };
  213. struct LinkingSection : CustomSection {
  214. LinkingSection() : CustomSection("linking") {}
  215. static bool classof(const Section *S) {
  216. auto C = dyn_cast<CustomSection>(S);
  217. return C && C->Name == "linking";
  218. }
  219. uint32_t Version;
  220. std::vector<SymbolInfo> SymbolTable;
  221. std::vector<SegmentInfo> SegmentInfos;
  222. std::vector<InitFunction> InitFunctions;
  223. std::vector<Comdat> Comdats;
  224. };
  225. struct ProducersSection : CustomSection {
  226. ProducersSection() : CustomSection("producers") {}
  227. static bool classof(const Section *S) {
  228. auto C = dyn_cast<CustomSection>(S);
  229. return C && C->Name == "producers";
  230. }
  231. std::vector<ProducerEntry> Languages;
  232. std::vector<ProducerEntry> Tools;
  233. std::vector<ProducerEntry> SDKs;
  234. };
  235. struct TargetFeaturesSection : CustomSection {
  236. TargetFeaturesSection() : CustomSection("target_features") {}
  237. static bool classof(const Section *S) {
  238. auto C = dyn_cast<CustomSection>(S);
  239. return C && C->Name == "target_features";
  240. }
  241. std::vector<FeatureEntry> Features;
  242. };
  243. struct TypeSection : Section {
  244. TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
  245. static bool classof(const Section *S) {
  246. return S->Type == wasm::WASM_SEC_TYPE;
  247. }
  248. std::vector<Signature> Signatures;
  249. };
  250. struct ImportSection : Section {
  251. ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
  252. static bool classof(const Section *S) {
  253. return S->Type == wasm::WASM_SEC_IMPORT;
  254. }
  255. std::vector<Import> Imports;
  256. };
  257. struct FunctionSection : Section {
  258. FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
  259. static bool classof(const Section *S) {
  260. return S->Type == wasm::WASM_SEC_FUNCTION;
  261. }
  262. std::vector<uint32_t> FunctionTypes;
  263. };
  264. struct TableSection : Section {
  265. TableSection() : Section(wasm::WASM_SEC_TABLE) {}
  266. static bool classof(const Section *S) {
  267. return S->Type == wasm::WASM_SEC_TABLE;
  268. }
  269. std::vector<Table> Tables;
  270. };
  271. struct MemorySection : Section {
  272. MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
  273. static bool classof(const Section *S) {
  274. return S->Type == wasm::WASM_SEC_MEMORY;
  275. }
  276. std::vector<Limits> Memories;
  277. };
  278. struct TagSection : Section {
  279. TagSection() : Section(wasm::WASM_SEC_TAG) {}
  280. static bool classof(const Section *S) {
  281. return S->Type == wasm::WASM_SEC_TAG;
  282. }
  283. std::vector<uint32_t> TagTypes;
  284. };
  285. struct GlobalSection : Section {
  286. GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
  287. static bool classof(const Section *S) {
  288. return S->Type == wasm::WASM_SEC_GLOBAL;
  289. }
  290. std::vector<Global> Globals;
  291. };
  292. struct ExportSection : Section {
  293. ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
  294. static bool classof(const Section *S) {
  295. return S->Type == wasm::WASM_SEC_EXPORT;
  296. }
  297. std::vector<Export> Exports;
  298. };
  299. struct StartSection : Section {
  300. StartSection() : Section(wasm::WASM_SEC_START) {}
  301. static bool classof(const Section *S) {
  302. return S->Type == wasm::WASM_SEC_START;
  303. }
  304. uint32_t StartFunction;
  305. };
  306. struct ElemSection : Section {
  307. ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
  308. static bool classof(const Section *S) {
  309. return S->Type == wasm::WASM_SEC_ELEM;
  310. }
  311. std::vector<ElemSegment> Segments;
  312. };
  313. struct CodeSection : Section {
  314. CodeSection() : Section(wasm::WASM_SEC_CODE) {}
  315. static bool classof(const Section *S) {
  316. return S->Type == wasm::WASM_SEC_CODE;
  317. }
  318. std::vector<Function> Functions;
  319. };
  320. struct DataSection : Section {
  321. DataSection() : Section(wasm::WASM_SEC_DATA) {}
  322. static bool classof(const Section *S) {
  323. return S->Type == wasm::WASM_SEC_DATA;
  324. }
  325. std::vector<DataSegment> Segments;
  326. };
  327. struct DataCountSection : Section {
  328. DataCountSection() : Section(wasm::WASM_SEC_DATACOUNT) {}
  329. static bool classof(const Section *S) {
  330. return S->Type == wasm::WASM_SEC_DATACOUNT;
  331. }
  332. uint32_t Count;
  333. };
  334. struct Object {
  335. FileHeader Header;
  336. std::vector<std::unique_ptr<Section>> Sections;
  337. };
  338. } // end namespace WasmYAML
  339. } // end namespace llvm
  340. LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
  341. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
  342. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
  343. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
  344. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
  345. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
  346. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
  347. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
  348. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
  349. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
  350. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
  351. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
  352. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
  353. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
  354. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
  355. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::FeatureEntry)
  356. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
  357. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
  358. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
  359. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
  360. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
  361. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkImportInfo)
  362. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DylinkExportInfo)
  363. namespace llvm {
  364. namespace yaml {
  365. template <> struct MappingTraits<WasmYAML::FileHeader> {
  366. static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
  367. };
  368. template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
  369. static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
  370. };
  371. template <> struct MappingTraits<WasmYAML::Object> {
  372. static void mapping(IO &IO, WasmYAML::Object &Object);
  373. };
  374. template <> struct MappingTraits<WasmYAML::Import> {
  375. static void mapping(IO &IO, WasmYAML::Import &Import);
  376. };
  377. template <> struct MappingTraits<WasmYAML::Export> {
  378. static void mapping(IO &IO, WasmYAML::Export &Export);
  379. };
  380. template <> struct MappingTraits<WasmYAML::Global> {
  381. static void mapping(IO &IO, WasmYAML::Global &Global);
  382. };
  383. template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
  384. static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
  385. };
  386. template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
  387. static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
  388. };
  389. template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
  390. static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
  391. };
  392. template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
  393. static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
  394. };
  395. template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
  396. static void enumeration(IO &IO, WasmYAML::SectionType &Type);
  397. };
  398. template <> struct MappingTraits<WasmYAML::Signature> {
  399. static void mapping(IO &IO, WasmYAML::Signature &Signature);
  400. };
  401. template <> struct MappingTraits<WasmYAML::Table> {
  402. static void mapping(IO &IO, WasmYAML::Table &Table);
  403. };
  404. template <> struct MappingTraits<WasmYAML::Limits> {
  405. static void mapping(IO &IO, WasmYAML::Limits &Limits);
  406. };
  407. template <> struct MappingTraits<WasmYAML::Function> {
  408. static void mapping(IO &IO, WasmYAML::Function &Function);
  409. };
  410. template <> struct MappingTraits<WasmYAML::Relocation> {
  411. static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
  412. };
  413. template <> struct MappingTraits<WasmYAML::NameEntry> {
  414. static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
  415. };
  416. template <> struct MappingTraits<WasmYAML::ProducerEntry> {
  417. static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
  418. };
  419. template <> struct ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix> {
  420. static void enumeration(IO &IO, WasmYAML::FeaturePolicyPrefix &Prefix);
  421. };
  422. template <> struct MappingTraits<WasmYAML::FeatureEntry> {
  423. static void mapping(IO &IO, WasmYAML::FeatureEntry &FeatureEntry);
  424. };
  425. template <> struct MappingTraits<WasmYAML::SegmentInfo> {
  426. static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
  427. };
  428. template <> struct MappingTraits<WasmYAML::LocalDecl> {
  429. static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
  430. };
  431. template <> struct MappingTraits<WasmYAML::InitExpr> {
  432. static void mapping(IO &IO, WasmYAML::InitExpr &Expr);
  433. };
  434. template <> struct MappingTraits<WasmYAML::DataSegment> {
  435. static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
  436. };
  437. template <> struct MappingTraits<WasmYAML::ElemSegment> {
  438. static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
  439. };
  440. template <> struct MappingTraits<WasmYAML::SymbolInfo> {
  441. static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
  442. };
  443. template <> struct MappingTraits<WasmYAML::InitFunction> {
  444. static void mapping(IO &IO, WasmYAML::InitFunction &Init);
  445. };
  446. template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
  447. static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
  448. };
  449. template <> struct MappingTraits<WasmYAML::ComdatEntry> {
  450. static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
  451. };
  452. template <> struct MappingTraits<WasmYAML::Comdat> {
  453. static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
  454. };
  455. template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
  456. static void enumeration(IO &IO, WasmYAML::ValueType &Type);
  457. };
  458. template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
  459. static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
  460. };
  461. template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
  462. static void enumeration(IO &IO, WasmYAML::TableType &Type);
  463. };
  464. template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
  465. static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
  466. };
  467. template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
  468. static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
  469. };
  470. template <> struct MappingTraits<WasmYAML::DylinkImportInfo> {
  471. static void mapping(IO &IO, WasmYAML::DylinkImportInfo &Info);
  472. };
  473. template <> struct MappingTraits<WasmYAML::DylinkExportInfo> {
  474. static void mapping(IO &IO, WasmYAML::DylinkExportInfo &Info);
  475. };
  476. } // end namespace yaml
  477. } // end namespace llvm
  478. #endif // LLVM_OBJECTYAML_WASMYAML_H
  479. #ifdef __GNUC__
  480. #pragma GCC diagnostic pop
  481. #endif