[WebAssembly] Add support for init functions linking metadata
[llvm-core.git] / include / llvm / ObjectYAML / WasmYAML.h
blob188ce8e444919b84e693922157e485dce5f73774
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 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_OBJECTYAML_WASMYAML_H
17 #define LLVM_OBJECTYAML_WASMYAML_H
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>
27 namespace llvm {
28 namespace WasmYAML {
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 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags)
38 LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags)
39 LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags)
41 struct FileHeader {
42 yaml::Hex32 Version;
45 struct Limits {
46 LimitFlags Flags;
47 yaml::Hex32 Initial;
48 yaml::Hex32 Maximum;
51 struct Table {
52 TableType ElemType;
53 Limits TableLimits;
56 struct Export {
57 StringRef Name;
58 ExportKind Kind;
59 uint32_t Index;
62 struct ElemSegment {
63 uint32_t TableIndex;
64 wasm::WasmInitExpr Offset;
65 std::vector<uint32_t> Functions;
68 struct Global {
69 ValueType Type;
70 bool Mutable;
71 wasm::WasmInitExpr InitExpr;
74 struct Import {
75 StringRef Module;
76 StringRef Field;
77 ExportKind Kind;
78 union {
79 uint32_t SigIndex;
80 Global GlobalImport;
81 Table TableImport;
82 Limits Memory;
86 struct LocalDecl {
87 ValueType Type;
88 uint32_t Count;
91 struct Function {
92 std::vector<LocalDecl> Locals;
93 yaml::BinaryRef Body;
96 struct Relocation {
97 RelocType Type;
98 uint32_t Index;
99 yaml::Hex32 Offset;
100 int32_t Addend;
103 struct DataSegment {
104 uint32_t MemoryIndex;
105 uint32_t SectionOffset;
106 wasm::WasmInitExpr Offset;
107 yaml::BinaryRef Content;
110 struct NameEntry {
111 uint32_t Index;
112 StringRef Name;
115 struct SegmentInfo {
116 uint32_t Index;
117 StringRef Name;
118 uint32_t Alignment;
119 SegmentFlags Flags;
122 struct Signature {
123 uint32_t Index;
124 SignatureForm Form = wasm::WASM_TYPE_FUNC;
125 std::vector<ValueType> ParamTypes;
126 ValueType ReturnType;
129 struct SymbolInfo {
130 StringRef Name;
131 SymbolFlags Flags;
134 struct InitFunction {
135 uint32_t Priority;
136 uint32_t FunctionIndex;
139 struct Section {
140 explicit Section(SectionType SecType) : Type(SecType) {}
141 virtual ~Section();
143 SectionType Type;
144 std::vector<Relocation> Relocations;
147 struct CustomSection : Section {
148 explicit CustomSection(StringRef Name)
149 : Section(wasm::WASM_SEC_CUSTOM), Name(Name) {}
151 static bool classof(const Section *S) {
152 return S->Type == wasm::WASM_SEC_CUSTOM;
155 StringRef Name;
156 yaml::BinaryRef Payload;
159 struct NameSection : CustomSection {
160 NameSection() : CustomSection("name") {}
162 static bool classof(const Section *S) {
163 auto C = dyn_cast<CustomSection>(S);
164 return C && C->Name == "name";
167 std::vector<NameEntry> FunctionNames;
170 struct LinkingSection : CustomSection {
171 LinkingSection() : CustomSection("linking") {}
173 static bool classof(const Section *S) {
174 auto C = dyn_cast<CustomSection>(S);
175 return C && C->Name == "linking";
178 uint32_t DataSize;
179 std::vector<SymbolInfo> SymbolInfos;
180 std::vector<SegmentInfo> SegmentInfos;
181 std::vector<InitFunction> InitFunctions;
184 struct TypeSection : Section {
185 TypeSection() : Section(wasm::WASM_SEC_TYPE) {}
187 static bool classof(const Section *S) {
188 return S->Type == wasm::WASM_SEC_TYPE;
191 std::vector<Signature> Signatures;
194 struct ImportSection : Section {
195 ImportSection() : Section(wasm::WASM_SEC_IMPORT) {}
197 static bool classof(const Section *S) {
198 return S->Type == wasm::WASM_SEC_IMPORT;
201 std::vector<Import> Imports;
204 struct FunctionSection : Section {
205 FunctionSection() : Section(wasm::WASM_SEC_FUNCTION) {}
207 static bool classof(const Section *S) {
208 return S->Type == wasm::WASM_SEC_FUNCTION;
211 std::vector<uint32_t> FunctionTypes;
214 struct TableSection : Section {
215 TableSection() : Section(wasm::WASM_SEC_TABLE) {}
217 static bool classof(const Section *S) {
218 return S->Type == wasm::WASM_SEC_TABLE;
221 std::vector<Table> Tables;
224 struct MemorySection : Section {
225 MemorySection() : Section(wasm::WASM_SEC_MEMORY) {}
227 static bool classof(const Section *S) {
228 return S->Type == wasm::WASM_SEC_MEMORY;
231 std::vector<Limits> Memories;
234 struct GlobalSection : Section {
235 GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {}
237 static bool classof(const Section *S) {
238 return S->Type == wasm::WASM_SEC_GLOBAL;
241 std::vector<Global> Globals;
244 struct ExportSection : Section {
245 ExportSection() : Section(wasm::WASM_SEC_EXPORT) {}
247 static bool classof(const Section *S) {
248 return S->Type == wasm::WASM_SEC_EXPORT;
251 std::vector<Export> Exports;
254 struct StartSection : Section {
255 StartSection() : Section(wasm::WASM_SEC_START) {}
257 static bool classof(const Section *S) {
258 return S->Type == wasm::WASM_SEC_START;
261 uint32_t StartFunction;
264 struct ElemSection : Section {
265 ElemSection() : Section(wasm::WASM_SEC_ELEM) {}
267 static bool classof(const Section *S) {
268 return S->Type == wasm::WASM_SEC_ELEM;
271 std::vector<ElemSegment> Segments;
274 struct CodeSection : Section {
275 CodeSection() : Section(wasm::WASM_SEC_CODE) {}
277 static bool classof(const Section *S) {
278 return S->Type == wasm::WASM_SEC_CODE;
281 std::vector<Function> Functions;
284 struct DataSection : Section {
285 DataSection() : Section(wasm::WASM_SEC_DATA) {}
287 static bool classof(const Section *S) {
288 return S->Type == wasm::WASM_SEC_DATA;
291 std::vector<DataSegment> Segments;
294 struct Object {
295 FileHeader Header;
296 std::vector<std::unique_ptr<Section>> Sections;
299 } // end namespace WasmYAML
300 } // end namespace llvm
302 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::WasmYAML::Section>)
303 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Signature)
304 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ValueType)
305 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Table)
306 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Import)
307 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Export)
308 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ElemSegment)
309 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Limits)
310 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::DataSegment)
311 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Global)
312 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
313 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
314 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
315 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
316 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
317 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
318 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
320 namespace llvm {
321 namespace yaml {
323 template <> struct MappingTraits<WasmYAML::FileHeader> {
324 static void mapping(IO &IO, WasmYAML::FileHeader &FileHdr);
327 template <> struct MappingTraits<std::unique_ptr<WasmYAML::Section>> {
328 static void mapping(IO &IO, std::unique_ptr<WasmYAML::Section> &Section);
331 template <> struct MappingTraits<WasmYAML::Object> {
332 static void mapping(IO &IO, WasmYAML::Object &Object);
335 template <> struct MappingTraits<WasmYAML::Import> {
336 static void mapping(IO &IO, WasmYAML::Import &Import);
339 template <> struct MappingTraits<WasmYAML::Export> {
340 static void mapping(IO &IO, WasmYAML::Export &Export);
343 template <> struct MappingTraits<WasmYAML::Global> {
344 static void mapping(IO &IO, WasmYAML::Global &Global);
347 template <> struct ScalarBitSetTraits<WasmYAML::LimitFlags> {
348 static void bitset(IO &IO, WasmYAML::LimitFlags &Value);
351 template <> struct ScalarBitSetTraits<WasmYAML::SymbolFlags> {
352 static void bitset(IO &IO, WasmYAML::SymbolFlags &Value);
355 template <> struct ScalarBitSetTraits<WasmYAML::SegmentFlags> {
356 static void bitset(IO &IO, WasmYAML::SegmentFlags &Value);
359 template <> struct ScalarEnumerationTraits<WasmYAML::SectionType> {
360 static void enumeration(IO &IO, WasmYAML::SectionType &Type);
363 template <> struct MappingTraits<WasmYAML::Signature> {
364 static void mapping(IO &IO, WasmYAML::Signature &Signature);
367 template <> struct MappingTraits<WasmYAML::Table> {
368 static void mapping(IO &IO, WasmYAML::Table &Table);
371 template <> struct MappingTraits<WasmYAML::Limits> {
372 static void mapping(IO &IO, WasmYAML::Limits &Limits);
375 template <> struct MappingTraits<WasmYAML::Function> {
376 static void mapping(IO &IO, WasmYAML::Function &Function);
379 template <> struct MappingTraits<WasmYAML::Relocation> {
380 static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
383 template <> struct MappingTraits<WasmYAML::NameEntry> {
384 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
387 template <> struct MappingTraits<WasmYAML::SegmentInfo> {
388 static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
391 template <> struct MappingTraits<WasmYAML::LocalDecl> {
392 static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
395 template <> struct MappingTraits<wasm::WasmInitExpr> {
396 static void mapping(IO &IO, wasm::WasmInitExpr &Expr);
399 template <> struct MappingTraits<WasmYAML::DataSegment> {
400 static void mapping(IO &IO, WasmYAML::DataSegment &Segment);
403 template <> struct MappingTraits<WasmYAML::ElemSegment> {
404 static void mapping(IO &IO, WasmYAML::ElemSegment &Segment);
407 template <> struct MappingTraits<WasmYAML::SymbolInfo> {
408 static void mapping(IO &IO, WasmYAML::SymbolInfo &Info);
411 template <> struct MappingTraits<WasmYAML::InitFunction> {
412 static void mapping(IO &IO, WasmYAML::InitFunction &Init);
415 template <> struct ScalarEnumerationTraits<WasmYAML::ValueType> {
416 static void enumeration(IO &IO, WasmYAML::ValueType &Type);
419 template <> struct ScalarEnumerationTraits<WasmYAML::ExportKind> {
420 static void enumeration(IO &IO, WasmYAML::ExportKind &Kind);
423 template <> struct ScalarEnumerationTraits<WasmYAML::TableType> {
424 static void enumeration(IO &IO, WasmYAML::TableType &Type);
427 template <> struct ScalarEnumerationTraits<WasmYAML::Opcode> {
428 static void enumeration(IO &IO, WasmYAML::Opcode &Opcode);
431 template <> struct ScalarEnumerationTraits<WasmYAML::RelocType> {
432 static void enumeration(IO &IO, WasmYAML::RelocType &Kind);
435 } // end namespace yaml
436 } // end namespace llvm
438 #endif // LLVM_OBJECTYAML_WASMYAML_H