WasmYAML.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
  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. /// \file
  10. /// This file declares classes for handling the YAML representation
  11. /// of wasm binaries.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_OBJECTYAML_WASMYAML_H
  15. #define LLVM_OBJECTYAML_WASMYAML_H
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/BinaryFormat/Wasm.h"
  18. #include "llvm/ObjectYAML/YAML.h"
  19. #include "llvm/Support/Casting.h"
  20. #include <cstdint>
  21. #include <memory>
  22. #include <vector>
  23. namespace llvm {
  24. namespace WasmYAML {
  25. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
  26. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
  27. LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
  28. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
  29. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
  30. LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
  31. LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
  32. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
  33. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
  34. LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
  35. LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
  36. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
  37. LLVM_YAML_STRONG_TYPEDEF(uint32_t, FeaturePolicyPrefix)
  38. struct FileHeader {
  39. yaml::Hex32 Version;
  40. };
  41. struct Limits {
  42. LimitFlags Flags;
  43. yaml::Hex32 Minimum;
  44. yaml::Hex32 Maximum;
  45. };
  46. struct Table {
  47. TableType ElemType;
  48. Limits TableLimits;
  49. uint32_t Index;
  50. };
  51. struct Export {
  52. StringRef Name;
  53. ExportKind Kind;
  54. uint32_t Index;
  55. };
  56. struct ElemSegment {
  57. uint32_t Flags;
  58. uint32_t TableNumber;
  59. ValueType ElemKind;
  60. wasm::WasmInitExpr Offset;
  61. std::vector<uint32_t> Functions;
  62. };
  63. struct Global {
  64. uint32_t Index;
  65. ValueType Type;
  66. bool Mutable;
  67. wasm::WasmInitExpr InitExpr;
  68. };
  69. struct Event {
  70. uint32_t Index;
  71. uint32_t Attribute;
  72. uint32_t SigIndex;
  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. Event EventImport;
  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 DylinkSection : CustomSection {
  172. DylinkSection() : CustomSection("dylink") {}
  173. static bool classof(const Section *S) {
  174. auto C = dyn_cast<CustomSection>(S);
  175. return C && C->Name == "dylink";
  176. }
  177. uint32_t MemorySize;
  178. uint32_t MemoryAlignment;
  179. uint32_t TableSize;
  180. uint32_t TableAlignment;
  181. std::vector<StringRef> Needed;
  182. };
  183. struct NameSection : CustomSection {
  184. NameSection() : CustomSection("name") {}
  185. static bool classof(const Section *S) {
  186. auto C = dyn_cast<CustomSection>(S);
  187. return C && C->Name == "name";
  188. }
  189. std::vector<NameEntry> FunctionNames;
  190. std::vector<NameEntry> GlobalNames;
  191. std::vector<NameEntry> DataSegmentNames;
  192. };
  193. struct LinkingSection : CustomSection {
  194. LinkingSection() : CustomSection("linking") {}
  195. static bool classof(const Section *S) {
  196. auto C = dyn_cast<CustomSection>(S);
  197. return C && C->Name == "linking";
  198. }
  199. uint32_t Version;
  200. std::vector<SymbolInfo> SymbolTable;
  201. std::vector<SegmentInfo> SegmentInfos;
  202. std::vector<InitFunction> InitFunctions;
  203. std::vector<Comdat> Comdats;
  204. };
  205. struct ProducersSection : CustomSection {
  206. ProducersSection() : CustomSection("producers") {}
  207. static bool classof(const Section *S) {
  208. auto C = dyn_cast<CustomSection>(S);
  209. return C && C->Name == "producers";
  210. }
  211. std::vector<ProducerEntry> Languages;
  212. std::vector<ProducerEntry> Tools;
  213. std::vector<ProducerEntry> SDKs;
  214. };
  215. struct TargetFeaturesSection : CustomSection {
  216. TargetFeaturesSection() : CustomSection("target_features") {}
  217. static bool classof(const Section *S) {
  218. auto C = dyn_cast<CustomSection>(S);
  219. return C && C->Name == "target_features";
  220. }
  221. std::vector<FeatureEntry> Features;
  222. };
  223. struct TypeSection : Section {
  224. TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
  225. static bool classof(const Section *S) {
  226. return S->Type == wasm::WASM_SEC_TYPE;
  227. }
  228. std::vector<Signature> Signatures;
  229. };
  230. struct ImportSection : Section {
  231. ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
  232. static bool classof(const Section *S) {
  233. return S->Type == wasm::WASM_SEC_IMPORT;
  234. }
  235. std::vector<Import> Imports;
  236. };
  237. struct FunctionSection : Section {
  238. FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
  239. static bool classof(const Section *S) {
  240. return S->Type == wasm::WASM_SEC_FUNCTION;
  241. }
  242. std::vector<uint32_t> FunctionTypes;
  243. };
  244. struct TableSection : Section {
  245. TableSection() : Section(wasm::WASM_SEC_TABLE) {}
  246. static bool classof(const Section *S) {
  247. return S->Type == wasm::WASM_SEC_TABLE;
  248. }
  249. std::vector<Table> Tables;
  250. };
  251. struct MemorySection : Section {
  252. MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
  253. static bool classof(const Section *S) {
  254. return S->Type == wasm::WASM_SEC_MEMORY;
  255. }
  256. std::vector<Limits> Memories;
  257. };
  258. struct EventSection : Section {
  259. EventSection() : Section(wasm::WASM_SEC_EVENT) {}
  260. static bool classof(const Section *S) {
  261. return S->Type == wasm::WASM_SEC_EVENT;
  262. }
  263. std::vector<Event> Events;
  264. };
  265. struct GlobalSection : Section {
  266. GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
  267. static bool classof(const Section *S) {
  268. return S->Type == wasm::WASM_SEC_GLOBAL;
  269. }
  270. std::vector<Global> Globals;
  271. };
  272. struct ExportSection : Section {
  273. ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
  274. static bool classof(const Section *S) {
  275. return S->Type == wasm::WASM_SEC_EXPORT;
  276. }
  277. std::vector<Export> Exports;
  278. };
  279. struct StartSection : Section {
  280. StartSection() : Section(wasm::WASM_SEC_START) {}
  281. static bool classof(const Section *S) {
  282. return S->Type == wasm::WASM_SEC_START;
  283. }
  284. uint32_t StartFunction;
  285. };
  286. struct ElemSection : Section {
  287. ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
  288. static bool classof(const Section *S) {
  289. return S->Type == wasm::WASM_SEC_ELEM;
  290. }
  291. std::vector<ElemSegment> Segments;
  292. };
  293. struct CodeSection : Section {
  294. CodeSection() : Section(wasm::WASM_SEC_CODE) {}
  295. static bool classof(const Section *S) {
  296. return S->Type == wasm::WASM_SEC_CODE;
  297. }
  298. std::vector<Function> Functions;
  299. };
  300. struct DataSection : Section {
  301. DataSection() : Section(wasm::WASM_SEC_DATA) {}
  302. static bool classof(const Section *S) {
  303. return S->Type == wasm::WASM_SEC_DATA;
  304. }
  305. std::vector<DataSegment> Segments;
  306. };
  307. struct DataCountSection : Section {
  308. DataCountSection() : Section(wasm::WASM_SEC_DATACOUNT) {}
  309. static bool classof(const Section *S) {
  310. return S->Type == wasm::WASM_SEC_DATACOUNT;
  311. }
  312. uint32_t Count;
  313. };
  314. struct Object {
  315. FileHeader Header;
  316. std::vector<std::unique_ptr<Section>> Sections;
  317. };
  318. } // end namespace WasmYAML
  319. } // end namespace llvm
  320. LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
  321. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
  322. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
  323. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
  324. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
  325. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
  326. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
  327. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
  328. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
  329. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
  330. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
  331. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
  332. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
  333. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
  334. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
  335. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::FeatureEntry)
  336. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
  337. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
  338. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
  339. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
  340. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
  341. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Event)
  342. namespace llvm {
  343. namespace yaml {
  344. template <> struct MappingTraits<WasmYAML::FileHeader> {
  345. static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
  346. };
  347. template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
  348. static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
  349. };
  350. template <> struct MappingTraits<WasmYAML::Object> {
  351. static void mapping(IO &IO, WasmYAML::Object &Object);
  352. };
  353. template <> struct MappingTraits<WasmYAML::Import> {
  354. static void mapping(IO &IO, WasmYAML::Import &Import);
  355. };
  356. template <> struct MappingTraits<WasmYAML::Export> {
  357. static void mapping(IO &IO, WasmYAML::Export &Export);
  358. };
  359. template <> struct MappingTraits<WasmYAML::Global> {
  360. static void mapping(IO &IO, WasmYAML::Global &Global);
  361. };
  362. template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
  363. static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
  364. };
  365. template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
  366. static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
  367. };
  368. template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
  369. static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
  370. };
  371. template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
  372. static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
  373. };
  374. template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
  375. static void enumeration(IO &IO, WasmYAML::SectionType &Type);
  376. };
  377. template <> struct MappingTraits<WasmYAML::Signature> {
  378. static void mapping(IO &IO, WasmYAML::Signature &Signature);
  379. };
  380. template <> struct MappingTraits<WasmYAML::Table> {
  381. static void mapping(IO &IO, WasmYAML::Table &Table);
  382. };
  383. template <> struct MappingTraits<WasmYAML::Limits> {
  384. static void mapping(IO &IO, WasmYAML::Limits &Limits);
  385. };
  386. template <> struct MappingTraits<WasmYAML::Function> {
  387. static void mapping(IO &IO, WasmYAML::Function &Function);
  388. };
  389. template <> struct MappingTraits<WasmYAML::Relocation> {
  390. static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
  391. };
  392. template <> struct MappingTraits<WasmYAML::NameEntry> {
  393. static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
  394. };
  395. template <> struct MappingTraits<WasmYAML::ProducerEntry> {
  396. static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
  397. };
  398. template <> struct ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix> {
  399. static void enumeration(IO &IO, WasmYAML::FeaturePolicyPrefix &Prefix);
  400. };
  401. template <> struct MappingTraits<WasmYAML::FeatureEntry> {
  402. static void mapping(IO &IO, WasmYAML::FeatureEntry &FeatureEntry);
  403. };
  404. template <> struct MappingTraits<WasmYAML::SegmentInfo> {
  405. static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
  406. };
  407. template <> struct MappingTraits<WasmYAML::LocalDecl> {
  408. static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
  409. };
  410. template <> struct MappingTraits<wasm::WasmInitExpr> {
  411. static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
  412. };
  413. template <> struct MappingTraits<WasmYAML::DataSegment> {
  414. static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
  415. };
  416. template <> struct MappingTraits<WasmYAML::ElemSegment> {
  417. static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
  418. };
  419. template <> struct MappingTraits<WasmYAML::SymbolInfo> {
  420. static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
  421. };
  422. template <> struct MappingTraits<WasmYAML::InitFunction> {
  423. static void mapping(IO &IO, WasmYAML::InitFunction &Init);
  424. };
  425. template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
  426. static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
  427. };
  428. template <> struct MappingTraits<WasmYAML::ComdatEntry> {
  429. static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
  430. };
  431. template <> struct MappingTraits<WasmYAML::Comdat> {
  432. static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
  433. };
  434. template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
  435. static void enumeration(IO &IO, WasmYAML::ValueType &Type);
  436. };
  437. template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
  438. static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
  439. };
  440. template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
  441. static void enumeration(IO &IO, WasmYAML::TableType &Type);
  442. };
  443. template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
  444. static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
  445. };
  446. template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
  447. static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
  448. };
  449. template <> struct MappingTraits<WasmYAML::Event> {
  450. static void mapping(IO &IO, WasmYAML::Event &Event);
  451. };
  452. } // end namespace yaml
  453. } // end namespace llvm
  454. #endif // LLVM_OBJECTYAML_WASMYAML_H