Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / ObjectYAML / WasmYAML.h
blobf5260bbb1ed4d75f3e2b648f6af84e3d47ce33c5
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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECTYAML_WASMYAML_H
16 #define LLVM_OBJECTYAML_WASMYAML_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/ObjectYAML/YAML.h"
21 #include "llvm/Support/Casting.h"
22 #include <cstdint>
23 #include <memory>
24 #include <vector>
26 namespace llvm {
27 namespace WasmYAML {
29 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
30 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
31 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
32 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
33 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
34 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
35 LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
37 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
38 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
39 LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
40 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
42 struct FileHeader {
43 yaml::Hex32 Version;
46 struct Limits {
47 LimitFlags Flags;
48 yaml::Hex32 Initial;
49 yaml::Hex32 Maximum;
52 struct Table {
53 TableType ElemType;
54 Limits TableLimits;
57 struct Export {
58 StringRef Name;
59 ExportKind Kind;
60 uint32_t Index;
63 struct ElemSegment {
64 uint32_t TableIndex;
65 wasm::WasmInitExpr Offset;
66 std::vector<uint32_t> Functions;
69 struct Global {
70 uint32_t Index;
71 ValueType Type;
72 bool Mutable;
73 wasm::WasmInitExpr InitExpr;
76 struct Event {
77 uint32_t Index;
78 uint32_t Attribute;
79 uint32_t SigIndex;
82 struct Import {
83 StringRef Module;
84 StringRef Field;
85 ExportKind Kind;
86 union {
87 uint32_t SigIndex;
88 Global GlobalImport;
89 Table TableImport;
90 Limits Memory;
91 Event EventImport;
95 struct LocalDecl {
96 ValueType Type;
97 uint32_t Count;
100 struct Function {
101 uint32_t Index;
102 std::vector<LocalDecl> Locals;
103 yaml::BinaryRef Body;
106 struct Relocation {
107 RelocType Type;
108 uint32_t Index;
109 yaml::Hex32 Offset;
110 int32_t Addend;
113 struct DataSegment {
114 uint32_t MemoryIndex;
115 uint32_t SectionOffset;
116 wasm::WasmInitExpr Offset;
117 yaml::BinaryRef Content;
120 struct NameEntry {
121 uint32_t Index;
122 StringRef Name;
125 struct ProducerEntry {
126 std::string Name;
127 std::string Version;
130 struct SegmentInfo {
131 uint32_t Index;
132 StringRef Name;
133 uint32_t Alignment;
134 SegmentFlags Flags;
137 struct Signature {
138 uint32_t Index;
139 SignatureForm Form = wasm::WASM_TYPE_FUNC;
140 std::vector<ValueType> ParamTypes;
141 ValueType ReturnType;
144 struct SymbolInfo {
145 uint32_t Index;
146 StringRef Name;
147 SymbolKind Kind;
148 SymbolFlags Flags;
149 union {
150 uint32_t ElementIndex;
151 wasm::WasmDataReference DataRef;
155 struct InitFunction {
156 uint32_t Priority;
157 uint32_t Symbol;
160 struct ComdatEntry {
161 ComdatKind Kind;
162 uint32_t Index;
165 struct Comdat {
166 StringRef Name;
167 std::vector<ComdatEntry> Entries;
170 struct Section {
171 explicit Section(SectionType SecType) : Type(SecType) {}
172 virtual ~Section();
174 SectionType Type;
175 std::vector<Relocation> Relocations;
178 struct CustomSection : Section {
179 explicit CustomSection(StringRef Name)
180 : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
182 static bool classof(const Section *S) {
183 return S->Type == wasm::WASM_SEC_CUSTOM;
186 StringRef Name;
187 yaml::BinaryRef Payload;
190 struct DylinkSection : CustomSection {
191 DylinkSection() : CustomSection("dylink") {}
193 static bool classof(const Section *S) {
194 auto C = dyn_cast<CustomSection>(S);
195 return C && C->Name == "dylink";
198 uint32_t MemorySize;
199 uint32_t MemoryAlignment;
200 uint32_t TableSize;
201 uint32_t TableAlignment;
202 std::vector<StringRef> Needed;
205 struct NameSection : CustomSection {
206 NameSection() : CustomSection("name") {}
208 static bool classof(const Section *S) {
209 auto C = dyn_cast<CustomSection>(S);
210 return C && C->Name == "name";
213 std::vector<NameEntry> FunctionNames;
216 struct LinkingSection : CustomSection {
217 LinkingSection() : CustomSection("linking") {}
219 static bool classof(const Section *S) {
220 auto C = dyn_cast<CustomSection>(S);
221 return C && C->Name == "linking";
224 uint32_t Version;
225 std::vector<SymbolInfo> SymbolTable;
226 std::vector<SegmentInfo> SegmentInfos;
227 std::vector<InitFunction> InitFunctions;
228 std::vector<Comdat> Comdats;
231 struct ProducersSection : CustomSection {
232 ProducersSection() : CustomSection("producers") {}
234 static bool classof(const Section *S) {
235 auto C = dyn_cast<CustomSection>(S);
236 return C && C->Name == "producers";
239 std::vector<ProducerEntry> Languages;
240 std::vector<ProducerEntry> Tools;
241 std::vector<ProducerEntry> SDKs;
244 struct TypeSection : Section {
245 TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
247 static bool classof(const Section *S) {
248 return S->Type == wasm::WASM_SEC_TYPE;
251 std::vector<Signature> Signatures;
254 struct ImportSection : Section {
255 ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
257 static bool classof(const Section *S) {
258 return S->Type == wasm::WASM_SEC_IMPORT;
261 std::vector<Import> Imports;
264 struct FunctionSection : Section {
265 FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
267 static bool classof(const Section *S) {
268 return S->Type == wasm::WASM_SEC_FUNCTION;
271 std::vector<uint32_t> FunctionTypes;
274 struct TableSection : Section {
275 TableSection() : Section(wasm::WASM_SEC_TABLE) {}
277 static bool classof(const Section *S) {
278 return S->Type == wasm::WASM_SEC_TABLE;
281 std::vector<Table> Tables;
284 struct MemorySection : Section {
285 MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
287 static bool classof(const Section *S) {
288 return S->Type == wasm::WASM_SEC_MEMORY;
291 std::vector<Limits> Memories;
294 struct GlobalSection : Section {
295 GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
297 static bool classof(const Section *S) {
298 return S->Type == wasm::WASM_SEC_GLOBAL;
301 std::vector<Global> Globals;
304 struct EventSection : Section {
305 EventSection() : Section(wasm::WASM_SEC_EVENT) {}
307 static bool classof(const Section *S) {
308 return S->Type == wasm::WASM_SEC_EVENT;
311 std::vector<Event> Events;
314 struct ExportSection : Section {
315 ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
317 static bool classof(const Section *S) {
318 return S->Type == wasm::WASM_SEC_EXPORT;
321 std::vector<Export> Exports;
324 struct StartSection : Section {
325 StartSection() : Section(wasm::WASM_SEC_START) {}
327 static bool classof(const Section *S) {
328 return S->Type == wasm::WASM_SEC_START;
331 uint32_t StartFunction;
334 struct ElemSection : Section {
335 ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
337 static bool classof(const Section *S) {
338 return S->Type == wasm::WASM_SEC_ELEM;
341 std::vector<ElemSegment> Segments;
344 struct CodeSection : Section {
345 CodeSection() : Section(wasm::WASM_SEC_CODE) {}
347 static bool classof(const Section *S) {
348 return S->Type == wasm::WASM_SEC_CODE;
351 std::vector<Function> Functions;
354 struct DataSection : Section {
355 DataSection() : Section(wasm::WASM_SEC_DATA) {}
357 static bool classof(const Section *S) {
358 return S->Type == wasm::WASM_SEC_DATA;
361 std::vector<DataSegment> Segments;
364 struct Object {
365 FileHeader Header;
366 std::vector<std::unique_ptr<Section>> Sections;
369 } // end namespace WasmYAML
370 } // end namespace llvm
372 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
373 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
374 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
375 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
376 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
377 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
378 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
379 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
380 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
381 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
382 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
383 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
384 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
385 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
386 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
387 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
388 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
389 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
390 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
391 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
392 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Event)
394 namespace llvm {
395 namespace yaml {
397 template <> struct MappingTraits<WasmYAML::FileHeader> {
398 static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
401 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
402 static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
405 template <> struct MappingTraits<WasmYAML::Object> {
406 static void mapping(IO &IO, WasmYAML::Object &Object);
409 template <> struct MappingTraits<WasmYAML::Import> {
410 static void mapping(IO &IO, WasmYAML::Import &Import);
413 template <> struct MappingTraits<WasmYAML::Export> {
414 static void mapping(IO &IO, WasmYAML::Export &Export);
417 template <> struct MappingTraits<WasmYAML::Global> {
418 static void mapping(IO &IO, WasmYAML::Global &Global);
421 template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
422 static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
425 template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
426 static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
429 template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
430 static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
433 template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
434 static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
437 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
438 static void enumeration(IO &IO, WasmYAML::SectionType &Type);
441 template <> struct MappingTraits<WasmYAML::Signature> {
442 static void mapping(IO &IO, WasmYAML::Signature &Signature);
445 template <> struct MappingTraits<WasmYAML::Table> {
446 static void mapping(IO &IO, WasmYAML::Table &Table);
449 template <> struct MappingTraits<WasmYAML::Limits> {
450 static void mapping(IO &IO, WasmYAML::Limits &Limits);
453 template <> struct MappingTraits<WasmYAML::Function> {
454 static void mapping(IO &IO, WasmYAML::Function &Function);
457 template <> struct MappingTraits<WasmYAML::Relocation> {
458 static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
461 template <> struct MappingTraits<WasmYAML::NameEntry> {
462 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
465 template <> struct MappingTraits<WasmYAML::ProducerEntry> {
466 static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
469 template <> struct MappingTraits<WasmYAML::SegmentInfo> {
470 static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
473 template <> struct MappingTraits<WasmYAML::LocalDecl> {
474 static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
477 template <> struct MappingTraits<wasm::WasmInitExpr> {
478 static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
481 template <> struct MappingTraits<WasmYAML::DataSegment> {
482 static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
485 template <> struct MappingTraits<WasmYAML::ElemSegment> {
486 static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
489 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
490 static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
493 template <> struct MappingTraits<WasmYAML::InitFunction> {
494 static void mapping(IO &IO, WasmYAML::InitFunction &Init);
497 template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
498 static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
501 template <> struct MappingTraits<WasmYAML::ComdatEntry> {
502 static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
505 template <> struct MappingTraits<WasmYAML::Comdat> {
506 static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
509 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
510 static void enumeration(IO &IO, WasmYAML::ValueType &Type);
513 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
514 static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
517 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
518 static void enumeration(IO &IO, WasmYAML::TableType &Type);
521 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
522 static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
525 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
526 static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
529 template <> struct MappingTraits<WasmYAML::Event> {
530 static void mapping(IO &IO, WasmYAML::Event &Event);
533 } // end namespace yaml
534 } // end namespace llvm
536 #endif // LLVM_OBJECTYAML_WASMYAML_H