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