blob: f5260bbb1ed4d75f3e2b648f6af84e3d47ce33c5 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- WasmYAML.h - Wasm YAMLIO implementation ------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010/// This file declares classes for handling the YAML representation
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011/// of wasm binaries.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OBJECTYAML_WASMYAML_H
16#define LLVM_OBJECTYAML_WASMYAML_H
17
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>
25
26namespace llvm {
27namespace WasmYAML {
28
29LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionType)
30LLVM_YAML_STRONG_TYPEDEF(uint32_t, ValueType)
31LLVM_YAML_STRONG_TYPEDEF(uint32_t, TableType)
32LLVM_YAML_STRONG_TYPEDEF(uint32_t, SignatureForm)
33LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportKind)
34LLVM_YAML_STRONG_TYPEDEF(uint32_t, Opcode)
35LLVM_YAML_STRONG_TYPEDEF(uint32_t, RelocType)
36LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
37LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolKind)
38LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
39LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
40LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind)
41
42struct FileHeader {
43 yaml::Hex32 Version;
44};
45
46struct Limits {
47 LimitFlags Flags;
48 yaml::Hex32 Initial;
49 yaml::Hex32 Maximum;
50};
51
52struct Table {
53 TableType ElemType;
54 Limits TableLimits;
55};
56
57struct Export {
58 StringRef Name;
59 ExportKind Kind;
60 uint32_t Index;
61};
62
63struct ElemSegment {
64 uint32_t TableIndex;
65 wasm::WasmInitExpr Offset;
66 std::vector<uint32_t> Functions;
67};
68
69struct Global {
70 uint32_t Index;
71 ValueType Type;
72 bool Mutable;
73 wasm::WasmInitExpr InitExpr;
74};
75
Andrew Walbran16937d02019-10-22 13:54:20 +010076struct Event {
77 uint32_t Index;
78 uint32_t Attribute;
79 uint32_t SigIndex;
80};
81
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010082struct 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;
Andrew Walbran16937d02019-10-22 13:54:20 +010091 Event EventImport;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 };
93};
94
95struct LocalDecl {
96 ValueType Type;
97 uint32_t Count;
98};
99
100struct Function {
101 uint32_t Index;
102 std::vector<LocalDecl> Locals;
103 yaml::BinaryRef Body;
104};
105
106struct Relocation {
107 RelocType Type;
108 uint32_t Index;
109 yaml::Hex32 Offset;
110 int32_t Addend;
111};
112
113struct DataSegment {
114 uint32_t MemoryIndex;
115 uint32_t SectionOffset;
116 wasm::WasmInitExpr Offset;
117 yaml::BinaryRef Content;
118};
119
120struct NameEntry {
121 uint32_t Index;
122 StringRef Name;
123};
124
Andrew Walbran16937d02019-10-22 13:54:20 +0100125struct ProducerEntry {
126 std::string Name;
127 std::string Version;
128};
129
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100130struct SegmentInfo {
131 uint32_t Index;
132 StringRef Name;
133 uint32_t Alignment;
134 SegmentFlags Flags;
135};
136
137struct Signature {
138 uint32_t Index;
139 SignatureForm Form = wasm::WASM_TYPE_FUNC;
140 std::vector<ValueType> ParamTypes;
141 ValueType ReturnType;
142};
143
144struct SymbolInfo {
145 uint32_t Index;
146 StringRef Name;
147 SymbolKind Kind;
148 SymbolFlags Flags;
149 union {
150 uint32_t ElementIndex;
151 wasm::WasmDataReference DataRef;
152 };
153};
154
155struct InitFunction {
156 uint32_t Priority;
157 uint32_t Symbol;
158};
159
160struct ComdatEntry {
161 ComdatKind Kind;
162 uint32_t Index;
163};
164
165struct Comdat {
166 StringRef Name;
167 std::vector<ComdatEntry> Entries;
168};
169
170struct Section {
171 explicit Section(SectionType SecType) : Type(SecType) {}
172 virtual ~Section();
173
174 SectionType Type;
175 std::vector<Relocation> Relocations;
176};
177
178struct CustomSection : Section {
179 explicit CustomSection(StringRef Name)
180 : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
181
182 static bool classof(const Section *S) {
183 return S->Type == wasm::WASM_SEC_CUSTOM;
184 }
185
186 StringRef Name;
187 yaml::BinaryRef Payload;
188};
189
Andrew Walbran16937d02019-10-22 13:54:20 +0100190struct DylinkSection : CustomSection {
191 DylinkSection() : CustomSection("dylink") {}
192
193 static bool classof(const Section *S) {
194 auto C = dyn_cast<CustomSection>(S);
195 return C && C->Name == "dylink";
196 }
197
198 uint32_t MemorySize;
199 uint32_t MemoryAlignment;
200 uint32_t TableSize;
201 uint32_t TableAlignment;
202 std::vector<StringRef> Needed;
203};
204
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100205struct NameSection : CustomSection {
206 NameSection() : CustomSection("name") {}
207
208 static bool classof(const Section *S) {
209 auto C = dyn_cast<CustomSection>(S);
210 return C && C->Name == "name";
211 }
212
213 std::vector<NameEntry> FunctionNames;
214};
215
216struct LinkingSection : CustomSection {
217 LinkingSection() : CustomSection("linking") {}
218
219 static bool classof(const Section *S) {
220 auto C = dyn_cast<CustomSection>(S);
221 return C && C->Name == "linking";
222 }
223
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100224 uint32_t Version;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100225 std::vector<SymbolInfo> SymbolTable;
226 std::vector<SegmentInfo> SegmentInfos;
227 std::vector<InitFunction> InitFunctions;
228 std::vector<Comdat> Comdats;
229};
230
Andrew Walbran16937d02019-10-22 13:54:20 +0100231struct ProducersSection : CustomSection {
232 ProducersSection() : CustomSection("producers") {}
233
234 static bool classof(const Section *S) {
235 auto C = dyn_cast<CustomSection>(S);
236 return C && C->Name == "producers";
237 }
238
239 std::vector<ProducerEntry> Languages;
240 std::vector<ProducerEntry> Tools;
241 std::vector<ProducerEntry> SDKs;
242};
243
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100244struct TypeSection : Section {
245 TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
246
247 static bool classof(const Section *S) {
248 return S->Type == wasm::WASM_SEC_TYPE;
249 }
250
251 std::vector<Signature> Signatures;
252};
253
254struct ImportSection : Section {
255 ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
256
257 static bool classof(const Section *S) {
258 return S->Type == wasm::WASM_SEC_IMPORT;
259 }
260
261 std::vector<Import> Imports;
262};
263
264struct FunctionSection : Section {
265 FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
266
267 static bool classof(const Section *S) {
268 return S->Type == wasm::WASM_SEC_FUNCTION;
269 }
270
271 std::vector<uint32_t> FunctionTypes;
272};
273
274struct TableSection : Section {
275 TableSection() : Section(wasm::WASM_SEC_TABLE) {}
276
277 static bool classof(const Section *S) {
278 return S->Type == wasm::WASM_SEC_TABLE;
279 }
280
281 std::vector<Table> Tables;
282};
283
284struct MemorySection : Section {
285 MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
286
287 static bool classof(const Section *S) {
288 return S->Type == wasm::WASM_SEC_MEMORY;
289 }
290
291 std::vector<Limits> Memories;
292};
293
294struct GlobalSection : Section {
295 GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
296
297 static bool classof(const Section *S) {
298 return S->Type == wasm::WASM_SEC_GLOBAL;
299 }
300
301 std::vector<Global> Globals;
302};
303
Andrew Walbran16937d02019-10-22 13:54:20 +0100304struct EventSection : Section {
305 EventSection() : Section(wasm::WASM_SEC_EVENT) {}
306
307 static bool classof(const Section *S) {
308 return S->Type == wasm::WASM_SEC_EVENT;
309 }
310
311 std::vector<Event> Events;
312};
313
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100314struct ExportSection : Section {
315 ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
316
317 static bool classof(const Section *S) {
318 return S->Type == wasm::WASM_SEC_EXPORT;
319 }
320
321 std::vector<Export> Exports;
322};
323
324struct StartSection : Section {
325 StartSection() : Section(wasm::WASM_SEC_START) {}
326
327 static bool classof(const Section *S) {
328 return S->Type == wasm::WASM_SEC_START;
329 }
330
331 uint32_t StartFunction;
332};
333
334struct ElemSection : Section {
335 ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
336
337 static bool classof(const Section *S) {
338 return S->Type == wasm::WASM_SEC_ELEM;
339 }
340
341 std::vector<ElemSegment> Segments;
342};
343
344struct CodeSection : Section {
345 CodeSection() : Section(wasm::WASM_SEC_CODE) {}
346
347 static bool classof(const Section *S) {
348 return S->Type == wasm::WASM_SEC_CODE;
349 }
350
351 std::vector<Function> Functions;
352};
353
354struct DataSection : Section {
355 DataSection() : Section(wasm::WASM_SEC_DATA) {}
356
357 static bool classof(const Section *S) {
358 return S->Type == wasm::WASM_SEC_DATA;
359 }
360
361 std::vector<DataSegment> Segments;
362};
363
364struct Object {
365 FileHeader Header;
366 std::vector<std::unique_ptr<Section>> Sections;
367};
368
369} // end namespace WasmYAML
370} // end namespace llvm
371
372LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
373LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
374LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
375LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
376LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
377LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
378LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
379LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
380LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
381LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
382LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
383LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
384LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
385LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
Andrew Walbran16937d02019-10-22 13:54:20 +0100386LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100387LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
388LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
389LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
390LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry)
391LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat)
Andrew Walbran16937d02019-10-22 13:54:20 +0100392LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Event)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100393
394namespace llvm {
395namespace yaml {
396
397template <> struct MappingTraits<WasmYAML::FileHeader> {
398 static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
399};
400
401template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
402 static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
403};
404
405template <> struct MappingTraits<WasmYAML::Object> {
406 static void mapping(IO &IO, WasmYAML::Object &Object);
407};
408
409template <> struct MappingTraits<WasmYAML::Import> {
410 static void mapping(IO &IO, WasmYAML::Import &Import);
411};
412
413template <> struct MappingTraits<WasmYAML::Export> {
414 static void mapping(IO &IO, WasmYAML::Export &Export);
415};
416
417template <> struct MappingTraits<WasmYAML::Global> {
418 static void mapping(IO &IO, WasmYAML::Global &Global);
419};
420
421template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
422 static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
423};
424
425template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
426 static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
427};
428
429template <> struct ScalarEnumerationTraits<WasmYAML::SymbolKind> {
430 static void enumeration(IO &IO, WasmYAML::SymbolKind &Kind);
431};
432
433template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
434 static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
435};
436
437template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
438 static void enumeration(IO &IO, WasmYAML::SectionType &Type);
439};
440
441template <> struct MappingTraits<WasmYAML::Signature> {
442 static void mapping(IO &IO, WasmYAML::Signature &Signature);
443};
444
445template <> struct MappingTraits<WasmYAML::Table> {
446 static void mapping(IO &IO, WasmYAML::Table &Table);
447};
448
449template <> struct MappingTraits<WasmYAML::Limits> {
450 static void mapping(IO &IO, WasmYAML::Limits &Limits);
451};
452
453template <> struct MappingTraits<WasmYAML::Function> {
454 static void mapping(IO &IO, WasmYAML::Function &Function);
455};
456
457template <> struct MappingTraits<WasmYAML::Relocation> {
458 static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
459};
460
461template <> struct MappingTraits<WasmYAML::NameEntry> {
462 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
463};
464
Andrew Walbran16937d02019-10-22 13:54:20 +0100465template <> struct MappingTraits<WasmYAML::ProducerEntry> {
466 static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
467};
468
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100469template <> struct MappingTraits<WasmYAML::SegmentInfo> {
470 static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
471};
472
473template <> struct MappingTraits<WasmYAML::LocalDecl> {
474 static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
475};
476
477template <> struct MappingTraits<wasm::WasmInitExpr> {
478 static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
479};
480
481template <> struct MappingTraits<WasmYAML::DataSegment> {
482 static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
483};
484
485template <> struct MappingTraits<WasmYAML::ElemSegment> {
486 static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
487};
488
489template <> struct MappingTraits<WasmYAML::SymbolInfo> {
490 static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
491};
492
493template <> struct MappingTraits<WasmYAML::InitFunction> {
494 static void mapping(IO &IO, WasmYAML::InitFunction &Init);
495};
496
497template <> struct ScalarEnumerationTraits<WasmYAML::ComdatKind> {
498 static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind);
499};
500
501template <> struct MappingTraits<WasmYAML::ComdatEntry> {
502 static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry);
503};
504
505template <> struct MappingTraits<WasmYAML::Comdat> {
506 static void mapping(IO &IO, WasmYAML::Comdat &Comdat);
507};
508
509template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
510 static void enumeration(IO &IO, WasmYAML::ValueType &Type);
511};
512
513template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
514 static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
515};
516
517template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
518 static void enumeration(IO &IO, WasmYAML::TableType &Type);
519};
520
521template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
522 static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
523};
524
525template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
526 static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
527};
528
Andrew Walbran16937d02019-10-22 13:54:20 +0100529template <> struct MappingTraits<WasmYAML::Event> {
530 static void mapping(IO &IO, WasmYAML::Event &Event);
531};
532
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100533} // end namespace yaml
534} // end namespace llvm
535
536#endif // LLVM_OBJECTYAML_WASMYAML_H