1 //===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines classes for handling the YAML representation of wasm.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ObjectYAML/WasmYAML.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/YAMLTraits.h"
24 // Declared here rather than in the header to comply with:
25 // http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers
26 Section::~Section() = default;
28 } // end namespace WasmYAML
32 void MappingTraits
<WasmYAML::FileHeader
>::mapping(
33 IO
&IO
, WasmYAML::FileHeader
&FileHdr
) {
34 IO
.mapRequired("Version", FileHdr
.Version
);
37 void MappingTraits
<WasmYAML::Object
>::mapping(IO
&IO
,
38 WasmYAML::Object
&Object
) {
39 IO
.setContext(&Object
);
40 IO
.mapTag("!WASM", true);
41 IO
.mapRequired("FileHeader", Object
.Header
);
42 IO
.mapOptional("Sections", Object
.Sections
);
43 IO
.setContext(nullptr);
46 static void commonSectionMapping(IO
&IO
, WasmYAML::Section
&Section
) {
47 IO
.mapRequired("Type", Section
.Type
);
48 IO
.mapOptional("Relocations", Section
.Relocations
);
51 static void sectionMapping(IO
&IO
, WasmYAML::NameSection
&Section
) {
52 commonSectionMapping(IO
, Section
);
53 IO
.mapRequired("Name", Section
.Name
);
54 IO
.mapOptional("FunctionNames", Section
.FunctionNames
);
57 static void sectionMapping(IO
&IO
, WasmYAML::LinkingSection
&Section
) {
58 commonSectionMapping(IO
, Section
);
59 IO
.mapRequired("Name", Section
.Name
);
60 IO
.mapRequired("DataSize", Section
.DataSize
);
61 IO
.mapRequired("DataAlignment", Section
.DataAlignment
);
62 IO
.mapOptional("SymbolInfo", Section
.SymbolInfos
);
63 IO
.mapOptional("SegmentNames", Section
.SegmentNames
);
66 static void sectionMapping(IO
&IO
, WasmYAML::CustomSection
&Section
) {
67 commonSectionMapping(IO
, Section
);
68 IO
.mapRequired("Name", Section
.Name
);
69 IO
.mapRequired("Payload", Section
.Payload
);
72 static void sectionMapping(IO
&IO
, WasmYAML::TypeSection
&Section
) {
73 commonSectionMapping(IO
, Section
);
74 IO
.mapOptional("Signatures", Section
.Signatures
);
77 static void sectionMapping(IO
&IO
, WasmYAML::ImportSection
&Section
) {
78 commonSectionMapping(IO
, Section
);
79 IO
.mapOptional("Imports", Section
.Imports
);
82 static void sectionMapping(IO
&IO
, WasmYAML::FunctionSection
&Section
) {
83 commonSectionMapping(IO
, Section
);
84 IO
.mapOptional("FunctionTypes", Section
.FunctionTypes
);
87 static void sectionMapping(IO
&IO
, WasmYAML::TableSection
&Section
) {
88 commonSectionMapping(IO
, Section
);
89 IO
.mapOptional("Tables", Section
.Tables
);
92 static void sectionMapping(IO
&IO
, WasmYAML::MemorySection
&Section
) {
93 commonSectionMapping(IO
, Section
);
94 IO
.mapOptional("Memories", Section
.Memories
);
97 static void sectionMapping(IO
&IO
, WasmYAML::GlobalSection
&Section
) {
98 commonSectionMapping(IO
, Section
);
99 IO
.mapOptional("Globals", Section
.Globals
);
102 static void sectionMapping(IO
&IO
, WasmYAML::ExportSection
&Section
) {
103 commonSectionMapping(IO
, Section
);
104 IO
.mapOptional("Exports", Section
.Exports
);
107 static void sectionMapping(IO
&IO
, WasmYAML::StartSection
&Section
) {
108 commonSectionMapping(IO
, Section
);
109 IO
.mapOptional("StartFunction", Section
.StartFunction
);
112 static void sectionMapping(IO
&IO
, WasmYAML::ElemSection
&Section
) {
113 commonSectionMapping(IO
, Section
);
114 IO
.mapOptional("Segments", Section
.Segments
);
117 static void sectionMapping(IO
&IO
, WasmYAML::CodeSection
&Section
) {
118 commonSectionMapping(IO
, Section
);
119 IO
.mapRequired("Functions", Section
.Functions
);
122 static void sectionMapping(IO
&IO
, WasmYAML::DataSection
&Section
) {
123 commonSectionMapping(IO
, Section
);
124 IO
.mapRequired("Segments", Section
.Segments
);
127 void MappingTraits
<std::unique_ptr
<WasmYAML::Section
>>::mapping(
128 IO
&IO
, std::unique_ptr
<WasmYAML::Section
> &Section
) {
129 WasmYAML::SectionType SectionType
;
131 SectionType
= Section
->Type
;
133 IO
.mapRequired("Type", SectionType
);
135 switch (SectionType
) {
136 case wasm::WASM_SEC_CUSTOM
: {
137 StringRef SectionName
;
138 if (IO
.outputting()) {
139 auto CustomSection
= cast
<WasmYAML::CustomSection
>(Section
.get());
140 SectionName
= CustomSection
->Name
;
142 IO
.mapRequired("Name", SectionName
);
144 if (SectionName
== "linking") {
145 if (!IO
.outputting())
146 Section
.reset(new WasmYAML::LinkingSection());
147 sectionMapping(IO
, *cast
<WasmYAML::LinkingSection
>(Section
.get()));
148 } else if (SectionName
== "name") {
149 if (!IO
.outputting())
150 Section
.reset(new WasmYAML::NameSection());
151 sectionMapping(IO
, *cast
<WasmYAML::NameSection
>(Section
.get()));
153 if (!IO
.outputting())
154 Section
.reset(new WasmYAML::CustomSection(SectionName
));
155 sectionMapping(IO
, *cast
<WasmYAML::CustomSection
>(Section
.get()));
159 case wasm::WASM_SEC_TYPE
:
160 if (!IO
.outputting())
161 Section
.reset(new WasmYAML::TypeSection());
162 sectionMapping(IO
, *cast
<WasmYAML::TypeSection
>(Section
.get()));
164 case wasm::WASM_SEC_IMPORT
:
165 if (!IO
.outputting())
166 Section
.reset(new WasmYAML::ImportSection());
167 sectionMapping(IO
, *cast
<WasmYAML::ImportSection
>(Section
.get()));
169 case wasm::WASM_SEC_FUNCTION
:
170 if (!IO
.outputting())
171 Section
.reset(new WasmYAML::FunctionSection());
172 sectionMapping(IO
, *cast
<WasmYAML::FunctionSection
>(Section
.get()));
174 case wasm::WASM_SEC_TABLE
:
175 if (!IO
.outputting())
176 Section
.reset(new WasmYAML::TableSection());
177 sectionMapping(IO
, *cast
<WasmYAML::TableSection
>(Section
.get()));
179 case wasm::WASM_SEC_MEMORY
:
180 if (!IO
.outputting())
181 Section
.reset(new WasmYAML::MemorySection());
182 sectionMapping(IO
, *cast
<WasmYAML::MemorySection
>(Section
.get()));
184 case wasm::WASM_SEC_GLOBAL
:
185 if (!IO
.outputting())
186 Section
.reset(new WasmYAML::GlobalSection());
187 sectionMapping(IO
, *cast
<WasmYAML::GlobalSection
>(Section
.get()));
189 case wasm::WASM_SEC_EXPORT
:
190 if (!IO
.outputting())
191 Section
.reset(new WasmYAML::ExportSection());
192 sectionMapping(IO
, *cast
<WasmYAML::ExportSection
>(Section
.get()));
194 case wasm::WASM_SEC_START
:
195 if (!IO
.outputting())
196 Section
.reset(new WasmYAML::StartSection());
197 sectionMapping(IO
, *cast
<WasmYAML::StartSection
>(Section
.get()));
199 case wasm::WASM_SEC_ELEM
:
200 if (!IO
.outputting())
201 Section
.reset(new WasmYAML::ElemSection());
202 sectionMapping(IO
, *cast
<WasmYAML::ElemSection
>(Section
.get()));
204 case wasm::WASM_SEC_CODE
:
205 if (!IO
.outputting())
206 Section
.reset(new WasmYAML::CodeSection());
207 sectionMapping(IO
, *cast
<WasmYAML::CodeSection
>(Section
.get()));
209 case wasm::WASM_SEC_DATA
:
210 if (!IO
.outputting())
211 Section
.reset(new WasmYAML::DataSection());
212 sectionMapping(IO
, *cast
<WasmYAML::DataSection
>(Section
.get()));
215 llvm_unreachable("Unknown section type");
219 void ScalarEnumerationTraits
<WasmYAML::SectionType
>::enumeration(
220 IO
&IO
, WasmYAML::SectionType
&Type
) {
221 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
237 void MappingTraits
<WasmYAML::Signature
>::mapping(
238 IO
&IO
, WasmYAML::Signature
&Signature
) {
239 IO
.mapOptional("Index", Signature
.Index
);
240 IO
.mapRequired("ReturnType", Signature
.ReturnType
);
241 IO
.mapRequired("ParamTypes", Signature
.ParamTypes
);
244 void MappingTraits
<WasmYAML::Table
>::mapping(IO
&IO
, WasmYAML::Table
&Table
) {
245 IO
.mapRequired("ElemType", Table
.ElemType
);
246 IO
.mapRequired("Limits", Table
.TableLimits
);
249 void MappingTraits
<WasmYAML::Function
>::mapping(IO
&IO
,
250 WasmYAML::Function
&Function
) {
251 IO
.mapRequired("Locals", Function
.Locals
);
252 IO
.mapRequired("Body", Function
.Body
);
255 void MappingTraits
<WasmYAML::Relocation
>::mapping(
256 IO
&IO
, WasmYAML::Relocation
&Relocation
) {
257 IO
.mapRequired("Type", Relocation
.Type
);
258 IO
.mapRequired("Index", Relocation
.Index
);
259 IO
.mapRequired("Offset", Relocation
.Offset
);
260 IO
.mapOptional("Addend", Relocation
.Addend
, 0);
263 void MappingTraits
<WasmYAML::NameEntry
>::mapping(
264 IO
&IO
, WasmYAML::NameEntry
&NameEntry
) {
265 IO
.mapRequired("Index", NameEntry
.Index
);
266 IO
.mapRequired("Name", NameEntry
.Name
);
269 void MappingTraits
<WasmYAML::LocalDecl
>::mapping(
270 IO
&IO
, WasmYAML::LocalDecl
&LocalDecl
) {
271 IO
.mapRequired("Type", LocalDecl
.Type
);
272 IO
.mapRequired("Count", LocalDecl
.Count
);
275 void MappingTraits
<WasmYAML::Limits
>::mapping(IO
&IO
,
276 WasmYAML::Limits
&Limits
) {
277 if (!IO
.outputting() || Limits
.Flags
)
278 IO
.mapOptional("Flags", Limits
.Flags
);
279 IO
.mapRequired("Initial", Limits
.Initial
);
280 if (!IO
.outputting() || Limits
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
)
281 IO
.mapOptional("Maximum", Limits
.Maximum
);
284 void MappingTraits
<WasmYAML::ElemSegment
>::mapping(
285 IO
&IO
, WasmYAML::ElemSegment
&Segment
) {
286 IO
.mapRequired("Offset", Segment
.Offset
);
287 IO
.mapRequired("Functions", Segment
.Functions
);
290 void MappingTraits
<WasmYAML::Import
>::mapping(IO
&IO
,
291 WasmYAML::Import
&Import
) {
292 IO
.mapRequired("Module", Import
.Module
);
293 IO
.mapRequired("Field", Import
.Field
);
294 IO
.mapRequired("Kind", Import
.Kind
);
295 if (Import
.Kind
== wasm::WASM_EXTERNAL_FUNCTION
) {
296 IO
.mapRequired("SigIndex", Import
.SigIndex
);
297 } else if (Import
.Kind
== wasm::WASM_EXTERNAL_GLOBAL
) {
298 IO
.mapRequired("GlobalType", Import
.GlobalImport
.Type
);
299 IO
.mapRequired("GlobalMutable", Import
.GlobalImport
.Mutable
);
300 } else if (Import
.Kind
== wasm::WASM_EXTERNAL_TABLE
) {
301 IO
.mapRequired("Table", Import
.TableImport
);
302 } else if (Import
.Kind
== wasm::WASM_EXTERNAL_MEMORY
) {
303 IO
.mapRequired("Memory", Import
.Memory
);
305 llvm_unreachable("unhandled import type");
309 void MappingTraits
<WasmYAML::Export
>::mapping(IO
&IO
,
310 WasmYAML::Export
&Export
) {
311 IO
.mapRequired("Name", Export
.Name
);
312 IO
.mapRequired("Kind", Export
.Kind
);
313 IO
.mapRequired("Index", Export
.Index
);
316 void MappingTraits
<WasmYAML::Global
>::mapping(IO
&IO
,
317 WasmYAML::Global
&Global
) {
318 IO
.mapRequired("Type", Global
.Type
);
319 IO
.mapRequired("Mutable", Global
.Mutable
);
320 IO
.mapRequired("InitExpr", Global
.InitExpr
);
323 void MappingTraits
<wasm::WasmInitExpr
>::mapping(IO
&IO
,
324 wasm::WasmInitExpr
&Expr
) {
325 WasmYAML::Opcode Op
= Expr
.Opcode
;
326 IO
.mapRequired("Opcode", Op
);
328 switch (Expr
.Opcode
) {
329 case wasm::WASM_OPCODE_I32_CONST
:
330 IO
.mapRequired("Value", Expr
.Value
.Int32
);
332 case wasm::WASM_OPCODE_I64_CONST
:
333 IO
.mapRequired("Value", Expr
.Value
.Int64
);
335 case wasm::WASM_OPCODE_F32_CONST
:
336 IO
.mapRequired("Value", Expr
.Value
.Float32
);
338 case wasm::WASM_OPCODE_F64_CONST
:
339 IO
.mapRequired("Value", Expr
.Value
.Float64
);
341 case wasm::WASM_OPCODE_GET_GLOBAL
:
342 IO
.mapRequired("Index", Expr
.Value
.Global
);
347 void MappingTraits
<WasmYAML::DataSegment
>::mapping(
348 IO
&IO
, WasmYAML::DataSegment
&Segment
) {
349 IO
.mapOptional("SectionOffset", Segment
.SectionOffset
);
350 IO
.mapRequired("MemoryIndex", Segment
.MemoryIndex
);
351 IO
.mapRequired("Offset", Segment
.Offset
);
352 IO
.mapRequired("Content", Segment
.Content
);
355 void MappingTraits
<WasmYAML::SymbolInfo
>::mapping(IO
&IO
,
356 WasmYAML::SymbolInfo
&Info
) {
357 IO
.mapRequired("Name", Info
.Name
);
358 IO
.mapRequired("Flags", Info
.Flags
);
361 void ScalarEnumerationTraits
<WasmYAML::ValueType
>::enumeration(
362 IO
&IO
, WasmYAML::ValueType
&Type
) {
363 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
374 void ScalarEnumerationTraits
<WasmYAML::ExportKind
>::enumeration(
375 IO
&IO
, WasmYAML::ExportKind
&Kind
) {
376 #define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
384 void ScalarEnumerationTraits
<WasmYAML::Opcode
>::enumeration(
385 IO
&IO
, WasmYAML::Opcode
&Code
) {
386 #define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
396 void ScalarEnumerationTraits
<WasmYAML::TableType
>::enumeration(
397 IO
&IO
, WasmYAML::TableType
&Type
) {
398 #define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
403 void ScalarEnumerationTraits
<WasmYAML::RelocType
>::enumeration(
404 IO
&IO
, WasmYAML::RelocType
&Type
) {
405 #define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
406 #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
410 } // end namespace yaml
412 } // end namespace llvm