WasmYAML.h 14 KB

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