blob: def4d58a1016428b70c9137b1941e6a2a545a257 [file] [log] [blame]
Derek Schuffd3d84fd2017-03-30 19:44:09 +00001//===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Derek Schuffd3d84fd2017-03-30 19:44:09 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines classes for handling the YAML representation of wasm.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ObjectYAML/WasmYAML.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000014#include "llvm/ADT/StringRef.h"
Derek Schuffd3d84fd2017-03-30 19:44:09 +000015#include "llvm/Support/Casting.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000016#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/YAMLTraits.h"
Derek Schuffd3d84fd2017-03-30 19:44:09 +000018
19namespace llvm {
Derek Schuffc5b472f2017-03-31 22:14:14 +000020
21namespace WasmYAML {
22
23// Declared here rather than in the header to comply with:
24// http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers
Eugene Zelenko28082ab2017-07-01 01:35:55 +000025Section::~Section() = default;
Derek Schuffc5b472f2017-03-31 22:14:14 +000026
27} // end namespace WasmYAML
28
Derek Schuffd3d84fd2017-03-30 19:44:09 +000029namespace yaml {
30
31void MappingTraits<WasmYAML::FileHeader>::mapping(
32 IO &IO, WasmYAML::FileHeader &FileHdr) {
33 IO.mapRequired("Version", FileHdr.Version);
34}
35
36void MappingTraits<WasmYAML::Object>::mapping(IO &IO,
37 WasmYAML::Object &Object) {
38 IO.setContext(&Object);
39 IO.mapTag("!WASM", true);
40 IO.mapRequired("FileHeader", Object.Header);
41 IO.mapOptional("Sections", Object.Sections);
42 IO.setContext(nullptr);
43}
44
45static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) {
46 IO.mapRequired("Type", Section.Type);
47 IO.mapOptional("Relocations", Section.Relocations);
48}
49
Sam Clegge4afbc62018-11-14 18:36:24 +000050static void sectionMapping(IO &IO, WasmYAML::DylinkSection &Section) {
51 commonSectionMapping(IO, Section);
52 IO.mapRequired("Name", Section.Name);
53 IO.mapRequired("MemorySize", Section.MemorySize);
54 IO.mapRequired("MemoryAlignment", Section.MemoryAlignment);
55 IO.mapRequired("TableSize", Section.TableSize);
56 IO.mapRequired("TableAlignment", Section.TableAlignment);
Sam Clegg03801252018-12-12 23:40:58 +000057 IO.mapRequired("Needed", Section.Needed);
Sam Clegge4afbc62018-11-14 18:36:24 +000058}
59
Sam Cleggb7787fd2017-06-20 04:04:59 +000060static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
61 commonSectionMapping(IO, Section);
62 IO.mapRequired("Name", Section.Name);
63 IO.mapOptional("FunctionNames", Section.FunctionNames);
64}
65
66static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
67 commonSectionMapping(IO, Section);
68 IO.mapRequired("Name", Section.Name);
Sam Clegg6bb5a412018-04-26 18:15:32 +000069 IO.mapRequired("Version", Section.Version);
Sam Clegg6c899ba2018-02-23 05:08:34 +000070 IO.mapOptional("SymbolTable", Section.SymbolTable);
Sam Clegg63ebb812017-09-29 16:50:08 +000071 IO.mapOptional("SegmentInfo", Section.SegmentInfos);
Sam Clegg42739982017-12-14 21:10:03 +000072 IO.mapOptional("InitFunctions", Section.InitFunctions);
Sam Cleggea7cace2018-01-09 23:43:14 +000073 IO.mapOptional("Comdats", Section.Comdats);
Sam Cleggb7787fd2017-06-20 04:04:59 +000074}
75
Thomas Livelycbda16e2019-01-17 02:29:55 +000076static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) {
77 commonSectionMapping(IO, Section);
78 IO.mapRequired("Name", Section.Name);
79 IO.mapOptional("Languages", Section.Languages);
80 IO.mapOptional("Tools", Section.Tools);
81 IO.mapOptional("SDKs", Section.SDKs);
82}
83
Derek Schuffd3d84fd2017-03-30 19:44:09 +000084static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
85 commonSectionMapping(IO, Section);
86 IO.mapRequired("Name", Section.Name);
Sam Cleggb7787fd2017-06-20 04:04:59 +000087 IO.mapRequired("Payload", Section.Payload);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000088}
89
90static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
91 commonSectionMapping(IO, Section);
92 IO.mapOptional("Signatures", Section.Signatures);
93}
94
95static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
96 commonSectionMapping(IO, Section);
97 IO.mapOptional("Imports", Section.Imports);
98}
99
100static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
101 commonSectionMapping(IO, Section);
102 IO.mapOptional("FunctionTypes", Section.FunctionTypes);
103}
104
105static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
106 commonSectionMapping(IO, Section);
107 IO.mapOptional("Tables", Section.Tables);
108}
109
110static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
111 commonSectionMapping(IO, Section);
112 IO.mapOptional("Memories", Section.Memories);
113}
114
115static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
116 commonSectionMapping(IO, Section);
117 IO.mapOptional("Globals", Section.Globals);
118}
119
Heejin Ahnda419bd2018-11-14 02:46:21 +0000120static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) {
121 commonSectionMapping(IO, Section);
122 IO.mapOptional("Events", Section.Events);
123}
124
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000125static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
126 commonSectionMapping(IO, Section);
127 IO.mapOptional("Exports", Section.Exports);
128}
129
130static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
131 commonSectionMapping(IO, Section);
132 IO.mapOptional("StartFunction", Section.StartFunction);
133}
134
135static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
136 commonSectionMapping(IO, Section);
137 IO.mapOptional("Segments", Section.Segments);
138}
139
140static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
141 commonSectionMapping(IO, Section);
142 IO.mapRequired("Functions", Section.Functions);
143}
144
145static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
146 commonSectionMapping(IO, Section);
147 IO.mapRequired("Segments", Section.Segments);
148}
149
150void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
151 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
152 WasmYAML::SectionType SectionType;
153 if (IO.outputting())
154 SectionType = Section->Type;
155 else
156 IO.mapRequired("Type", SectionType);
157
158 switch (SectionType) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000159 case wasm::WASM_SEC_CUSTOM: {
160 StringRef SectionName;
161 if (IO.outputting()) {
162 auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
163 SectionName = CustomSection->Name;
164 } else {
165 IO.mapRequired("Name", SectionName);
166 }
Sam Clegge4afbc62018-11-14 18:36:24 +0000167 if (SectionName == "dylink") {
168 if (!IO.outputting())
169 Section.reset(new WasmYAML::DylinkSection());
170 sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get()));
171 } else if (SectionName == "linking") {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000172 if (!IO.outputting())
173 Section.reset(new WasmYAML::LinkingSection());
174 sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
175 } else if (SectionName == "name") {
176 if (!IO.outputting())
177 Section.reset(new WasmYAML::NameSection());
178 sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
Thomas Livelycbda16e2019-01-17 02:29:55 +0000179 } else if (SectionName == "producers") {
180 if (!IO.outputting())
181 Section.reset(new WasmYAML::ProducersSection());
182 sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get()));
Sam Cleggb7787fd2017-06-20 04:04:59 +0000183 } else {
184 if (!IO.outputting())
185 Section.reset(new WasmYAML::CustomSection(SectionName));
186 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
187 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000188 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000189 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000190 case wasm::WASM_SEC_TYPE:
191 if (!IO.outputting())
192 Section.reset(new WasmYAML::TypeSection());
193 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
194 break;
195 case wasm::WASM_SEC_IMPORT:
196 if (!IO.outputting())
197 Section.reset(new WasmYAML::ImportSection());
198 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
199 break;
200 case wasm::WASM_SEC_FUNCTION:
201 if (!IO.outputting())
202 Section.reset(new WasmYAML::FunctionSection());
203 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
204 break;
205 case wasm::WASM_SEC_TABLE:
206 if (!IO.outputting())
207 Section.reset(new WasmYAML::TableSection());
208 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
209 break;
210 case wasm::WASM_SEC_MEMORY:
211 if (!IO.outputting())
212 Section.reset(new WasmYAML::MemorySection());
213 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
214 break;
215 case wasm::WASM_SEC_GLOBAL:
216 if (!IO.outputting())
217 Section.reset(new WasmYAML::GlobalSection());
218 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
219 break;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000220 case wasm::WASM_SEC_EVENT:
221 if (!IO.outputting())
222 Section.reset(new WasmYAML::EventSection());
223 sectionMapping(IO, *cast<WasmYAML::EventSection>(Section.get()));
224 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000225 case wasm::WASM_SEC_EXPORT:
226 if (!IO.outputting())
227 Section.reset(new WasmYAML::ExportSection());
228 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
229 break;
230 case wasm::WASM_SEC_START:
231 if (!IO.outputting())
232 Section.reset(new WasmYAML::StartSection());
233 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
234 break;
235 case wasm::WASM_SEC_ELEM:
236 if (!IO.outputting())
237 Section.reset(new WasmYAML::ElemSection());
238 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
239 break;
240 case wasm::WASM_SEC_CODE:
241 if (!IO.outputting())
242 Section.reset(new WasmYAML::CodeSection());
243 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
244 break;
245 case wasm::WASM_SEC_DATA:
246 if (!IO.outputting())
247 Section.reset(new WasmYAML::DataSection());
248 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
249 break;
250 default:
251 llvm_unreachable("Unknown section type");
252 }
253}
254
255void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
256 IO &IO, WasmYAML::SectionType &Type) {
257#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
258 ECase(CUSTOM);
259 ECase(TYPE);
260 ECase(IMPORT);
261 ECase(FUNCTION);
262 ECase(TABLE);
263 ECase(MEMORY);
264 ECase(GLOBAL);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000265 ECase(EVENT);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000266 ECase(EXPORT);
267 ECase(START);
268 ECase(ELEM);
269 ECase(CODE);
270 ECase(DATA);
271#undef ECase
272}
273
274void MappingTraits<WasmYAML::Signature>::mapping(
275 IO &IO, WasmYAML::Signature &Signature) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000276 IO.mapRequired("Index", Signature.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000277 IO.mapRequired("ReturnType", Signature.ReturnType);
278 IO.mapRequired("ParamTypes", Signature.ParamTypes);
279}
280
281void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
282 IO.mapRequired("ElemType", Table.ElemType);
283 IO.mapRequired("Limits", Table.TableLimits);
284}
285
286void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
287 WasmYAML::Function &Function) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000288 IO.mapRequired("Index", Function.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000289 IO.mapRequired("Locals", Function.Locals);
290 IO.mapRequired("Body", Function.Body);
291}
292
293void MappingTraits<WasmYAML::Relocation>::mapping(
294 IO &IO, WasmYAML::Relocation &Relocation) {
295 IO.mapRequired("Type", Relocation.Type);
296 IO.mapRequired("Index", Relocation.Index);
297 IO.mapRequired("Offset", Relocation.Offset);
Sam Cleggcc182aa2017-04-26 00:02:31 +0000298 IO.mapOptional("Addend", Relocation.Addend, 0);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000299}
300
Sam Clegg03cdd122017-05-05 18:12:34 +0000301void MappingTraits<WasmYAML::NameEntry>::mapping(
302 IO &IO, WasmYAML::NameEntry &NameEntry) {
303 IO.mapRequired("Index", NameEntry.Index);
304 IO.mapRequired("Name", NameEntry.Name);
305}
306
Thomas Livelycbda16e2019-01-17 02:29:55 +0000307void MappingTraits<WasmYAML::ProducerEntry>::mapping(
308 IO &IO, WasmYAML::ProducerEntry &ProducerEntry) {
309 IO.mapRequired("Name", ProducerEntry.Name);
310 IO.mapRequired("Version", ProducerEntry.Version);
311}
312
Sam Clegg63ebb812017-09-29 16:50:08 +0000313void MappingTraits<WasmYAML::SegmentInfo>::mapping(
314 IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
315 IO.mapRequired("Index", SegmentInfo.Index);
316 IO.mapRequired("Name", SegmentInfo.Name);
317 IO.mapRequired("Alignment", SegmentInfo.Alignment);
318 IO.mapRequired("Flags", SegmentInfo.Flags);
319}
320
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000321void MappingTraits<WasmYAML::LocalDecl>::mapping(
322 IO &IO, WasmYAML::LocalDecl &LocalDecl) {
323 IO.mapRequired("Type", LocalDecl.Type);
324 IO.mapRequired("Count", LocalDecl.Count);
325}
326
327void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
328 WasmYAML::Limits &Limits) {
329 if (!IO.outputting() || Limits.Flags)
330 IO.mapOptional("Flags", Limits.Flags);
331 IO.mapRequired("Initial", Limits.Initial);
332 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
333 IO.mapOptional("Maximum", Limits.Maximum);
334}
335
336void MappingTraits<WasmYAML::ElemSegment>::mapping(
337 IO &IO, WasmYAML::ElemSegment &Segment) {
338 IO.mapRequired("Offset", Segment.Offset);
339 IO.mapRequired("Functions", Segment.Functions);
340}
341
342void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
343 WasmYAML::Import &Import) {
344 IO.mapRequired("Module", Import.Module);
345 IO.mapRequired("Field", Import.Field);
346 IO.mapRequired("Kind", Import.Kind);
347 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
348 IO.mapRequired("SigIndex", Import.SigIndex);
349 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
Sam Clegg41db5192017-05-10 00:14:04 +0000350 IO.mapRequired("GlobalType", Import.GlobalImport.Type);
351 IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000352 } else if (Import.Kind == wasm::WASM_EXTERNAL_EVENT) {
353 IO.mapRequired("EventAttribute", Import.EventImport.Attribute);
354 IO.mapRequired("EventSigIndex", Import.EventImport.SigIndex);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000355 } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
Sam Clegg41db5192017-05-10 00:14:04 +0000356 IO.mapRequired("Table", Import.TableImport);
Heejin Ahnf208f632018-09-05 01:27:38 +0000357 } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000358 IO.mapRequired("Memory", Import.Memory);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000359 } else {
360 llvm_unreachable("unhandled import type");
361 }
362}
363
364void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
365 WasmYAML::Export &Export) {
366 IO.mapRequired("Name", Export.Name);
367 IO.mapRequired("Kind", Export.Kind);
368 IO.mapRequired("Index", Export.Index);
369}
370
371void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
372 WasmYAML::Global &Global) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000373 IO.mapRequired("Index", Global.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000374 IO.mapRequired("Type", Global.Type);
375 IO.mapRequired("Mutable", Global.Mutable);
376 IO.mapRequired("InitExpr", Global.InitExpr);
377}
378
379void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
380 wasm::WasmInitExpr &Expr) {
381 WasmYAML::Opcode Op = Expr.Opcode;
382 IO.mapRequired("Opcode", Op);
383 Expr.Opcode = Op;
384 switch (Expr.Opcode) {
385 case wasm::WASM_OPCODE_I32_CONST:
386 IO.mapRequired("Value", Expr.Value.Int32);
387 break;
388 case wasm::WASM_OPCODE_I64_CONST:
389 IO.mapRequired("Value", Expr.Value.Int64);
390 break;
391 case wasm::WASM_OPCODE_F32_CONST:
392 IO.mapRequired("Value", Expr.Value.Float32);
393 break;
394 case wasm::WASM_OPCODE_F64_CONST:
395 IO.mapRequired("Value", Expr.Value.Float64);
396 break;
Thomas Lively6a87dda2019-01-08 06:25:55 +0000397 case wasm::WASM_OPCODE_GLOBAL_GET:
Sam Clegg7fb391f2017-04-25 17:11:56 +0000398 IO.mapRequired("Index", Expr.Value.Global);
399 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000400 }
401}
402
403void MappingTraits<WasmYAML::DataSegment>::mapping(
404 IO &IO, WasmYAML::DataSegment &Segment) {
Sam Clegg9c07f942017-07-12 00:24:54 +0000405 IO.mapOptional("SectionOffset", Segment.SectionOffset);
406 IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000407 IO.mapRequired("Offset", Segment.Offset);
408 IO.mapRequired("Content", Segment.Content);
409}
410
Sam Clegg42739982017-12-14 21:10:03 +0000411void MappingTraits<WasmYAML::InitFunction>::mapping(
412 IO &IO, WasmYAML::InitFunction &Init) {
413 IO.mapRequired("Priority", Init.Priority);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000414 IO.mapRequired("Symbol", Init.Symbol);
Sam Clegg42739982017-12-14 21:10:03 +0000415}
416
Sam Cleggea7cace2018-01-09 23:43:14 +0000417void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
418 IO &IO, WasmYAML::ComdatKind &Kind) {
419#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
420 ECase(FUNCTION);
421 ECase(DATA);
422#undef ECase
423}
424
425void MappingTraits<WasmYAML::ComdatEntry>::mapping(
426 IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
427 IO.mapRequired("Kind", ComdatEntry.Kind);
428 IO.mapRequired("Index", ComdatEntry.Index);
429}
430
Heejin Ahnf208f632018-09-05 01:27:38 +0000431void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO,
432 WasmYAML::Comdat &Comdat) {
Sam Cleggea7cace2018-01-09 23:43:14 +0000433 IO.mapRequired("Name", Comdat.Name);
434 IO.mapRequired("Entries", Comdat.Entries);
435}
436
Sam Cleggb7787fd2017-06-20 04:04:59 +0000437void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
438 WasmYAML::SymbolInfo &Info) {
Sam Clegg6c899ba2018-02-23 05:08:34 +0000439 IO.mapRequired("Index", Info.Index);
440 IO.mapRequired("Kind", Info.Kind);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000441 IO.mapRequired("Name", Info.Name);
442 IO.mapRequired("Flags", Info.Flags);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000443 if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) {
444 IO.mapRequired("Function", Info.ElementIndex);
445 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
446 IO.mapRequired("Global", Info.ElementIndex);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000447 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT) {
448 IO.mapRequired("Event", Info.ElementIndex);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000449 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) {
450 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
451 IO.mapRequired("Segment", Info.DataRef.Segment);
452 IO.mapOptional("Offset", Info.DataRef.Offset, 0u);
453 IO.mapRequired("Size", Info.DataRef.Size);
454 }
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000455 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) {
456 IO.mapRequired("Section", Info.ElementIndex);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000457 } else {
458 llvm_unreachable("unsupported symbol kind");
459 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000460}
461
Heejin Ahnda419bd2018-11-14 02:46:21 +0000462void MappingTraits<WasmYAML::Event>::mapping(IO &IO, WasmYAML::Event &Event) {
463 IO.mapRequired("Index", Event.Index);
464 IO.mapRequired("Attribute", Event.Attribute);
465 IO.mapRequired("SigIndex", Event.SigIndex);
466}
467
Sam Clegg0fc55992017-12-13 22:02:25 +0000468void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
469 IO &IO, WasmYAML::LimitFlags &Value) {
470#define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
471 BCase(HAS_MAX);
Derek Schuff68818062018-11-06 17:27:25 +0000472 BCase(IS_SHARED);
Sam Clegg0fc55992017-12-13 22:02:25 +0000473#undef BCase
474}
475
476void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
Heejin Ahnf208f632018-09-05 01:27:38 +0000477 IO &IO, WasmYAML::SegmentFlags &Value) {}
Sam Clegg0fc55992017-12-13 22:02:25 +0000478
479void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
480 IO &IO, WasmYAML::SymbolFlags &Value) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000481#define BCaseMask(M, X) \
482 IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
483 // BCaseMask(BINDING_MASK, BINDING_GLOBAL);
Sam Clegg0fc55992017-12-13 22:02:25 +0000484 BCaseMask(BINDING_MASK, BINDING_WEAK);
485 BCaseMask(BINDING_MASK, BINDING_LOCAL);
Heejin Ahnf208f632018-09-05 01:27:38 +0000486 // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
Sam Clegg0fc55992017-12-13 22:02:25 +0000487 BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000488 BCaseMask(UNDEFINED, UNDEFINED);
Sam Cleggd6ef8da2019-02-07 01:24:44 +0000489 BCaseMask(EXPORTED, EXPORTED);
Sam Clegg0fc55992017-12-13 22:02:25 +0000490#undef BCaseMask
491}
492
Sam Clegg6c899ba2018-02-23 05:08:34 +0000493void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration(
494 IO &IO, WasmYAML::SymbolKind &Kind) {
495#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X);
496 ECase(FUNCTION);
497 ECase(DATA);
498 ECase(GLOBAL);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000499 ECase(SECTION);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000500 ECase(EVENT);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000501#undef ECase
502}
503
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000504void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
505 IO &IO, WasmYAML::ValueType &Type) {
506#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
507 ECase(I32);
508 ECase(I64);
509 ECase(F32);
510 ECase(F64);
Thomas Lively6f21a132018-09-20 22:04:44 +0000511 ECase(V128);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000512 ECase(FUNCREF);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000513 ECase(FUNC);
514 ECase(NORESULT);
515#undef ECase
516}
517
518void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
519 IO &IO, WasmYAML::ExportKind &Kind) {
520#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
521 ECase(FUNCTION);
522 ECase(TABLE);
523 ECase(MEMORY);
524 ECase(GLOBAL);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000525 ECase(EVENT);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000526#undef ECase
527}
528
529void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
530 IO &IO, WasmYAML::Opcode &Code) {
531#define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
532 ECase(END);
533 ECase(I32_CONST);
534 ECase(I64_CONST);
535 ECase(F64_CONST);
536 ECase(F32_CONST);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000537 ECase(GLOBAL_GET);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000538#undef ECase
539}
540
541void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
542 IO &IO, WasmYAML::TableType &Type) {
543#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000544 ECase(FUNCREF);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000545#undef ECase
546}
547
548void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
549 IO &IO, WasmYAML::RelocType &Type) {
550#define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
Sam Cleggc5d8bc82017-12-21 03:16:34 +0000551#include "llvm/BinaryFormat/WasmRelocs.def"
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000552#undef WASM_RELOC
553}
554
555} // end namespace yaml
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000556
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000557} // end namespace llvm