]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ObjectYAML/WasmYAML.h
MFV r322232: 8426 mark immutable buffer arguments as such in abd.h
[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 /// \brief 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(int32_t, ValueType)
32 LLVM_YAML_STRONG_TYPEDEF(int32_t, TableType)
33 LLVM_YAML_STRONG_TYPEDEF(int32_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
38 struct FileHeader {
39   yaml::Hex32 Version;
40 };
41
42 struct Limits {
43   yaml::Hex32 Flags;
44   yaml::Hex32 Initial;
45   yaml::Hex32 Maximum;
46 };
47
48 struct Table {
49   TableType ElemType;
50   Limits TableLimits;
51 };
52
53 struct Export {
54   StringRef Name;
55   ExportKind Kind;
56   uint32_t Index;
57 };
58
59 struct ElemSegment {
60   uint32_t TableIndex;
61   wasm::WasmInitExpr Offset;
62   std::vector<uint32_t> Functions;
63 };
64
65 struct Global {
66   ValueType Type;
67   bool Mutable;
68   wasm::WasmInitExpr InitExpr;
69 };
70
71 struct Import {
72   StringRef Module;
73   StringRef Field;
74   ExportKind Kind;
75   union {
76     uint32_t SigIndex;
77     Global GlobalImport;
78     Table TableImport;
79     Limits Memory;
80   };
81 };
82
83 struct LocalDecl {
84   ValueType Type;
85   uint32_t Count;
86 };
87
88 struct Function {
89   std::vector<LocalDecl> Locals;
90   yaml::BinaryRef Body;
91 };
92
93 struct Relocation {
94   RelocType Type;
95   uint32_t Index;
96   yaml::Hex32 Offset;
97   int32_t Addend;
98 };
99
100 struct DataSegment {
101   uint32_t MemoryIndex;
102   uint32_t SectionOffset;
103   wasm::WasmInitExpr Offset;
104   yaml::BinaryRef Content;
105 };
106
107 struct NameEntry {
108   uint32_t Index;
109   StringRef Name;
110 };
111
112 struct Signature {
113   uint32_t Index;
114   SignatureForm Form = wasm::WASM_TYPE_FUNC;
115   std::vector<ValueType> ParamTypes;
116   ValueType ReturnType;
117 };
118
119 struct SymbolInfo {
120   StringRef Name;
121   uint32_t Flags;
122 };
123
124 struct Section {
125   explicit Section(SectionType SecType) : Type(SecType) {}
126   virtual ~Section();
127
128   SectionType Type;
129   std::vector<Relocation> Relocations;
130 };
131
132 struct CustomSection : Section {
133   explicit CustomSection(StringRef Name)
134       : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
135
136   static bool classof(const Section *S) {
137     return S->Type == wasm::WASM_SEC_CUSTOM;
138   }
139
140   StringRef Name;
141   yaml::BinaryRef Payload;
142 };
143
144 struct NameSection : CustomSection {
145   NameSection() : CustomSection("name") {}
146
147   static bool classof(const Section *S) {
148     auto C = dyn_cast<CustomSection>(S);
149     return C && C->Name == "name";
150   }
151
152   std::vector<NameEntry> FunctionNames;
153 };
154
155 struct LinkingSection : CustomSection {
156   LinkingSection() : CustomSection("linking") {}
157
158   static bool classof(const Section *S) {
159     auto C = dyn_cast<CustomSection>(S);
160     return C && C->Name == "linking";
161   }
162
163   std::vector<SymbolInfo> SymbolInfos;
164   uint32_t DataSize;
165   uint32_t DataAlignment;
166 };
167
168 struct TypeSection : Section {
169   TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
170
171   static bool classof(const Section *S) {
172     return S->Type == wasm::WASM_SEC_TYPE;
173   }
174
175   std::vector<Signature> Signatures;
176 };
177
178 struct ImportSection : Section {
179   ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
180
181   static bool classof(const Section *S) {
182     return S->Type == wasm::WASM_SEC_IMPORT;
183   }
184
185   std::vector<Import> Imports;
186 };
187
188 struct FunctionSection : Section {
189   FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
190
191   static bool classof(const Section *S) {
192     return S->Type == wasm::WASM_SEC_FUNCTION;
193   }
194
195   std::vector<uint32_t> FunctionTypes;
196 };
197
198 struct TableSection : Section {
199   TableSection() : Section(wasm::WASM_SEC_TABLE) {}
200
201   static bool classof(const Section *S) {
202     return S->Type == wasm::WASM_SEC_TABLE;
203   }
204
205   std::vector<Table> Tables;
206 };
207
208 struct MemorySection : Section {
209   MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
210
211   static bool classof(const Section *S) {
212     return S->Type == wasm::WASM_SEC_MEMORY;
213   }
214
215   std::vector<Limits> Memories;
216 };
217
218 struct GlobalSection : Section {
219   GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
220
221   static bool classof(const Section *S) {
222     return S->Type == wasm::WASM_SEC_GLOBAL;
223   }
224
225   std::vector<Global> Globals;
226 };
227
228 struct ExportSection : Section {
229   ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
230
231   static bool classof(const Section *S) {
232     return S->Type == wasm::WASM_SEC_EXPORT;
233   }
234
235   std::vector<Export> Exports;
236 };
237
238 struct StartSection : Section {
239   StartSection() : Section(wasm::WASM_SEC_START) {}
240
241   static bool classof(const Section *S) {
242     return S->Type == wasm::WASM_SEC_START;
243   }
244
245   uint32_t StartFunction;
246 };
247
248 struct ElemSection : Section {
249   ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
250
251   static bool classof(const Section *S) {
252     return S->Type == wasm::WASM_SEC_ELEM;
253   }
254
255   std::vector<ElemSegment> Segments;
256 };
257
258 struct CodeSection : Section {
259   CodeSection() : Section(wasm::WASM_SEC_CODE) {}
260
261   static bool classof(const Section *S) {
262     return S->Type == wasm::WASM_SEC_CODE;
263   }
264
265   std::vector<Function> Functions;
266 };
267
268 struct DataSection : Section {
269   DataSection() : Section(wasm::WASM_SEC_DATA) {}
270
271   static bool classof(const Section *S) {
272     return S->Type == wasm::WASM_SEC_DATA;
273   }
274
275   std::vector<DataSegment> Segments;
276 };
277
278 struct Object {
279   FileHeader Header;
280   std::vector<std::unique_ptr<Section>> Sections;
281 };
282
283 } // end namespace WasmYAML
284 } // end namespace llvm
285
286 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
287 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
288 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
289 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
290 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
291 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
292 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
293 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
294 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
295 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
296 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
297 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
298 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
299 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
300 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
301
302 namespace llvm {
303 namespace yaml {
304
305 template <> struct MappingTraits<WasmYAML::FileHeader> {
306   static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
307 };
308
309 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
310   static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
311 };
312
313 template <> struct MappingTraits<WasmYAML::Object> {
314   static void mapping(IO &IO, WasmYAML::Object &Object);
315 };
316
317 template <> struct MappingTraits<WasmYAML::Import> {
318   static void mapping(IO &IO, WasmYAML::Import &Import);
319 };
320
321 template <> struct MappingTraits<WasmYAML::Export> {
322   static void mapping(IO &IO, WasmYAML::Export &Export);
323 };
324
325 template <> struct MappingTraits<WasmYAML::Global> {
326   static void mapping(IO &IO, WasmYAML::Global &Global);
327 };
328
329 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
330   static void enumeration(IO &IO, WasmYAML::SectionType &Type);
331 };
332
333 template <> struct MappingTraits<WasmYAML::Signature> {
334   static void mapping(IO &IO, WasmYAML::Signature &Signature);
335 };
336
337 template <> struct MappingTraits<WasmYAML::Table> {
338   static void mapping(IO &IO, WasmYAML::Table &Table);
339 };
340
341 template <> struct MappingTraits<WasmYAML::Limits> {
342   static void mapping(IO &IO, WasmYAML::Limits &Limits);
343 };
344
345 template <> struct MappingTraits<WasmYAML::Function> {
346   static void mapping(IO &IO, WasmYAML::Function &Function);
347 };
348
349 template <> struct MappingTraits<WasmYAML::Relocation> {
350   static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
351 };
352
353 template <> struct MappingTraits<WasmYAML::NameEntry> {
354   static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
355 };
356
357 template <> struct MappingTraits<WasmYAML::LocalDecl> {
358   static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
359 };
360
361 template <> struct MappingTraits<wasm::WasmInitExpr> {
362   static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
363 };
364
365 template <> struct MappingTraits<WasmYAML::DataSegment> {
366   static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
367 };
368
369 template <> struct MappingTraits<WasmYAML::ElemSegment> {
370   static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
371 };
372
373 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
374   static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
375 };
376
377 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
378   static void enumeration(IO &IO, WasmYAML::ValueType &Type);
379 };
380
381 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
382   static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
383 };
384
385 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
386   static void enumeration(IO &IO, WasmYAML::TableType &Type);
387 };
388
389 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
390   static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
391 };
392
393 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
394   static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
395 };
396
397 } // end namespace yaml
398 } // end namespace llvm
399
400 #endif // LLVM_OBJECTYAML_WASMYAML_H