]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ObjectYAML/WasmYAML.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[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 Index;
102   wasm::WasmInitExpr Offset;
103   yaml::BinaryRef Content;
104 };
105
106 struct NameEntry {
107   uint32_t Index;
108   StringRef Name;
109 };
110
111 struct Signature {
112   uint32_t Index;
113   SignatureForm Form = wasm::WASM_TYPE_FUNC;
114   std::vector<ValueType> ParamTypes;
115   ValueType ReturnType;
116 };
117
118 struct SymbolInfo {
119   StringRef Name;
120   uint32_t Flags;
121 };
122
123 struct Section {
124   explicit Section(SectionType SecType) : Type(SecType) {}
125   virtual ~Section();
126
127   SectionType Type;
128   std::vector<Relocation> Relocations;
129 };
130
131 struct CustomSection : Section {
132   explicit CustomSection(StringRef Name)
133       : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
134
135   static bool classof(const Section *S) {
136     return S->Type == wasm::WASM_SEC_CUSTOM;
137   }
138
139   StringRef Name;
140   yaml::BinaryRef Payload;
141 };
142
143 struct NameSection : CustomSection {
144   NameSection() : CustomSection("name") {}
145
146   static bool classof(const Section *S) {
147     auto C = dyn_cast<CustomSection>(S);
148     return C && C->Name == "name";
149   }
150
151   std::vector<NameEntry> FunctionNames;
152 };
153
154 struct LinkingSection : CustomSection {
155   LinkingSection() : CustomSection("linking") {}
156
157   static bool classof(const Section *S) {
158     auto C = dyn_cast<CustomSection>(S);
159     return C && C->Name == "linking";
160   }
161
162   std::vector<SymbolInfo> SymbolInfos;
163   uint32_t DataSize;
164   uint32_t DataAlignment;
165 };
166
167 struct TypeSection : Section {
168   TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
169
170   static bool classof(const Section *S) {
171     return S->Type == wasm::WASM_SEC_TYPE;
172   }
173
174   std::vector<Signature> Signatures;
175 };
176
177 struct ImportSection : Section {
178   ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
179
180   static bool classof(const Section *S) {
181     return S->Type == wasm::WASM_SEC_IMPORT;
182   }
183
184   std::vector<Import> Imports;
185 };
186
187 struct FunctionSection : Section {
188   FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
189
190   static bool classof(const Section *S) {
191     return S->Type == wasm::WASM_SEC_FUNCTION;
192   }
193
194   std::vector<uint32_t> FunctionTypes;
195 };
196
197 struct TableSection : Section {
198   TableSection() : Section(wasm::WASM_SEC_TABLE) {}
199
200   static bool classof(const Section *S) {
201     return S->Type == wasm::WASM_SEC_TABLE;
202   }
203
204   std::vector<Table> Tables;
205 };
206
207 struct MemorySection : Section {
208   MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
209
210   static bool classof(const Section *S) {
211     return S->Type == wasm::WASM_SEC_MEMORY;
212   }
213
214   std::vector<Limits> Memories;
215 };
216
217 struct GlobalSection : Section {
218   GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
219
220   static bool classof(const Section *S) {
221     return S->Type == wasm::WASM_SEC_GLOBAL;
222   }
223
224   std::vector<Global> Globals;
225 };
226
227 struct ExportSection : Section {
228   ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
229
230   static bool classof(const Section *S) {
231     return S->Type == wasm::WASM_SEC_EXPORT;
232   }
233
234   std::vector<Export> Exports;
235 };
236
237 struct StartSection : Section {
238   StartSection() : Section(wasm::WASM_SEC_START) {}
239
240   static bool classof(const Section *S) {
241     return S->Type == wasm::WASM_SEC_START;
242   }
243
244   uint32_t StartFunction;
245 };
246
247 struct ElemSection : Section {
248   ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
249
250   static bool classof(const Section *S) {
251     return S->Type == wasm::WASM_SEC_ELEM;
252   }
253
254   std::vector<ElemSegment> Segments;
255 };
256
257 struct CodeSection : Section {
258   CodeSection() : Section(wasm::WASM_SEC_CODE) {}
259
260   static bool classof(const Section *S) {
261     return S->Type == wasm::WASM_SEC_CODE;
262   }
263
264   std::vector<Function> Functions;
265 };
266
267 struct DataSection : Section {
268   DataSection() : Section(wasm::WASM_SEC_DATA) {}
269
270   static bool classof(const Section *S) {
271     return S->Type == wasm::WASM_SEC_DATA;
272   }
273
274   std::vector<DataSegment> Segments;
275 };
276
277 struct Object {
278   FileHeader Header;
279   std::vector<std::unique_ptr<Section>> Sections;
280 };
281
282 } // end namespace WasmYAML
283 } // end namespace llvm
284
285 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
286 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
287 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
288 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
289 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
290 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
291 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
292 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
293 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
294 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
295 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
296 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
297 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
298 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
299 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
300
301 namespace llvm {
302 namespace yaml {
303
304 template <> struct MappingTraits<WasmYAML::FileHeader> {
305   static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
306 };
307
308 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
309   static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
310 };
311
312 template <> struct MappingTraits<WasmYAML::Object> {
313   static void mapping(IO &IO, WasmYAML::Object &Object);
314 };
315
316 template <> struct MappingTraits<WasmYAML::Import> {
317   static void mapping(IO &IO, WasmYAML::Import &Import);
318 };
319
320 template <> struct MappingTraits<WasmYAML::Export> {
321   static void mapping(IO &IO, WasmYAML::Export &Export);
322 };
323
324 template <> struct MappingTraits<WasmYAML::Global> {
325   static void mapping(IO &IO, WasmYAML::Global &Global);
326 };
327
328 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
329   static void enumeration(IO &IO, WasmYAML::SectionType &Type);
330 };
331
332 template <> struct MappingTraits<WasmYAML::Signature> {
333   static void mapping(IO &IO, WasmYAML::Signature &Signature);
334 };
335
336 template <> struct MappingTraits<WasmYAML::Table> {
337   static void mapping(IO &IO, WasmYAML::Table &Table);
338 };
339
340 template <> struct MappingTraits<WasmYAML::Limits> {
341   static void mapping(IO &IO, WasmYAML::Limits &Limits);
342 };
343
344 template <> struct MappingTraits<WasmYAML::Function> {
345   static void mapping(IO &IO, WasmYAML::Function &Function);
346 };
347
348 template <> struct MappingTraits<WasmYAML::Relocation> {
349   static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
350 };
351
352 template <> struct MappingTraits<WasmYAML::NameEntry> {
353   static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
354 };
355
356 template <> struct MappingTraits<WasmYAML::LocalDecl> {
357   static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
358 };
359
360 template <> struct MappingTraits<wasm::WasmInitExpr> {
361   static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
362 };
363
364 template <> struct MappingTraits<WasmYAML::DataSegment> {
365   static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
366 };
367
368 template <> struct MappingTraits<WasmYAML::ElemSegment> {
369   static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
370 };
371
372 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
373   static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
374 };
375
376 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
377   static void enumeration(IO &IO, WasmYAML::ValueType &Type);
378 };
379
380 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
381   static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
382 };
383
384 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
385   static void enumeration(IO &IO, WasmYAML::TableType &Type);
386 };
387
388 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
389   static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
390 };
391
392 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
393   static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
394 };
395
396 } // end namespace yaml
397 } // end namespace llvm
398
399 #endif // LLVM_OBJECTYAML_WASMYAML_H