WasmYAML.h 15 KB

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