blob: b12fd448de5a7ef231a8d6a76f1b578caad5fc8d [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
Thomas Livelyf6f4f842019-03-20 20:26:45 +000084static void sectionMapping(IO &IO, WasmYAML::TargetFeaturesSection &Section) {
85 commonSectionMapping(IO, Section);
86 IO.mapRequired("Name", Section.Name);
87 IO.mapRequired("Features", Section.Features);
88}
89
Derek Schuffd3d84fd2017-03-30 19:44:09 +000090static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
91 commonSectionMapping(IO, Section);
92 IO.mapRequired("Name", Section.Name);
Sam Cleggb7787fd2017-06-20 04:04:59 +000093 IO.mapRequired("Payload", Section.Payload);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000094}
95
96static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
97 commonSectionMapping(IO, Section);
98 IO.mapOptional("Signatures", Section.Signatures);
99}
100
101static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
102 commonSectionMapping(IO, Section);
103 IO.mapOptional("Imports", Section.Imports);
104}
105
106static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
107 commonSectionMapping(IO, Section);
108 IO.mapOptional("FunctionTypes", Section.FunctionTypes);
109}
110
111static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
112 commonSectionMapping(IO, Section);
113 IO.mapOptional("Tables", Section.Tables);
114}
115
116static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
117 commonSectionMapping(IO, Section);
118 IO.mapOptional("Memories", Section.Memories);
119}
120
Heejin Ahnda419bd2018-11-14 02:46:21 +0000121static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) {
122 commonSectionMapping(IO, Section);
123 IO.mapOptional("Events", Section.Events);
124}
125
Heejin Ahnf93426c2020-03-24 19:36:13 -0700126static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
127 commonSectionMapping(IO, Section);
128 IO.mapOptional("Globals", Section.Globals);
129}
130
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000131static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
132 commonSectionMapping(IO, Section);
133 IO.mapOptional("Exports", Section.Exports);
134}
135
136static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
137 commonSectionMapping(IO, Section);
138 IO.mapOptional("StartFunction", Section.StartFunction);
139}
140
141static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
142 commonSectionMapping(IO, Section);
143 IO.mapOptional("Segments", Section.Segments);
144}
145
146static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
147 commonSectionMapping(IO, Section);
148 IO.mapRequired("Functions", Section.Functions);
149}
150
151static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
152 commonSectionMapping(IO, Section);
153 IO.mapRequired("Segments", Section.Segments);
154}
155
Thomas Livelyfef8de62019-04-12 22:27:48 +0000156static void sectionMapping(IO &IO, WasmYAML::DataCountSection &Section) {
157 commonSectionMapping(IO, Section);
158 IO.mapRequired("Count", Section.Count);
159}
160
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000161void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
162 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
163 WasmYAML::SectionType SectionType;
164 if (IO.outputting())
165 SectionType = Section->Type;
166 else
167 IO.mapRequired("Type", SectionType);
168
169 switch (SectionType) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000170 case wasm::WASM_SEC_CUSTOM: {
171 StringRef SectionName;
172 if (IO.outputting()) {
173 auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
174 SectionName = CustomSection->Name;
175 } else {
176 IO.mapRequired("Name", SectionName);
177 }
Sam Clegge4afbc62018-11-14 18:36:24 +0000178 if (SectionName == "dylink") {
179 if (!IO.outputting())
180 Section.reset(new WasmYAML::DylinkSection());
181 sectionMapping(IO, *cast<WasmYAML::DylinkSection>(Section.get()));
182 } else if (SectionName == "linking") {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000183 if (!IO.outputting())
184 Section.reset(new WasmYAML::LinkingSection());
185 sectionMapping(IO, *cast<WasmYAML::LinkingSection>(Section.get()));
186 } else if (SectionName == "name") {
187 if (!IO.outputting())
188 Section.reset(new WasmYAML::NameSection());
189 sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get()));
Thomas Livelycbda16e2019-01-17 02:29:55 +0000190 } else if (SectionName == "producers") {
191 if (!IO.outputting())
192 Section.reset(new WasmYAML::ProducersSection());
193 sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get()));
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000194 } else if (SectionName == "target_features") {
195 if (!IO.outputting())
196 Section.reset(new WasmYAML::TargetFeaturesSection());
197 sectionMapping(IO, *cast<WasmYAML::TargetFeaturesSection>(Section.get()));
Sam Cleggb7787fd2017-06-20 04:04:59 +0000198 } else {
199 if (!IO.outputting())
200 Section.reset(new WasmYAML::CustomSection(SectionName));
201 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
202 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000203 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000204 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000205 case wasm::WASM_SEC_TYPE:
206 if (!IO.outputting())
207 Section.reset(new WasmYAML::TypeSection());
208 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
209 break;
210 case wasm::WASM_SEC_IMPORT:
211 if (!IO.outputting())
212 Section.reset(new WasmYAML::ImportSection());
213 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
214 break;
215 case wasm::WASM_SEC_FUNCTION:
216 if (!IO.outputting())
217 Section.reset(new WasmYAML::FunctionSection());
218 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
219 break;
220 case wasm::WASM_SEC_TABLE:
221 if (!IO.outputting())
222 Section.reset(new WasmYAML::TableSection());
223 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
224 break;
225 case wasm::WASM_SEC_MEMORY:
226 if (!IO.outputting())
227 Section.reset(new WasmYAML::MemorySection());
228 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
229 break;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000230 case wasm::WASM_SEC_EVENT:
231 if (!IO.outputting())
232 Section.reset(new WasmYAML::EventSection());
233 sectionMapping(IO, *cast<WasmYAML::EventSection>(Section.get()));
234 break;
Heejin Ahnf93426c2020-03-24 19:36:13 -0700235 case wasm::WASM_SEC_GLOBAL:
236 if (!IO.outputting())
237 Section.reset(new WasmYAML::GlobalSection());
238 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
239 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000240 case wasm::WASM_SEC_EXPORT:
241 if (!IO.outputting())
242 Section.reset(new WasmYAML::ExportSection());
243 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
244 break;
245 case wasm::WASM_SEC_START:
246 if (!IO.outputting())
247 Section.reset(new WasmYAML::StartSection());
248 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
249 break;
250 case wasm::WASM_SEC_ELEM:
251 if (!IO.outputting())
252 Section.reset(new WasmYAML::ElemSection());
253 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
254 break;
255 case wasm::WASM_SEC_CODE:
256 if (!IO.outputting())
257 Section.reset(new WasmYAML::CodeSection());
258 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
259 break;
260 case wasm::WASM_SEC_DATA:
261 if (!IO.outputting())
262 Section.reset(new WasmYAML::DataSection());
263 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
264 break;
Thomas Livelyfef8de62019-04-12 22:27:48 +0000265 case wasm::WASM_SEC_DATACOUNT:
266 if (!IO.outputting())
267 Section.reset(new WasmYAML::DataCountSection());
268 sectionMapping(IO, *cast<WasmYAML::DataCountSection>(Section.get()));
269 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000270 default:
271 llvm_unreachable("Unknown section type");
272 }
273}
274
275void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
276 IO &IO, WasmYAML::SectionType &Type) {
277#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
278 ECase(CUSTOM);
279 ECase(TYPE);
280 ECase(IMPORT);
281 ECase(FUNCTION);
282 ECase(TABLE);
283 ECase(MEMORY);
284 ECase(GLOBAL);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000285 ECase(EVENT);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000286 ECase(EXPORT);
287 ECase(START);
288 ECase(ELEM);
289 ECase(CODE);
290 ECase(DATA);
Thomas Livelyfef8de62019-04-12 22:27:48 +0000291 ECase(DATACOUNT);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000292#undef ECase
293}
294
295void MappingTraits<WasmYAML::Signature>::mapping(
296 IO &IO, WasmYAML::Signature &Signature) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000297 IO.mapRequired("Index", Signature.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000298 IO.mapRequired("ParamTypes", Signature.ParamTypes);
Thomas Lively393d0f72019-10-18 20:27:30 +0000299 IO.mapRequired("ReturnTypes", Signature.ReturnTypes);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000300}
301
302void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
303 IO.mapRequired("ElemType", Table.ElemType);
304 IO.mapRequired("Limits", Table.TableLimits);
305}
306
307void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
308 WasmYAML::Function &Function) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000309 IO.mapRequired("Index", Function.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000310 IO.mapRequired("Locals", Function.Locals);
311 IO.mapRequired("Body", Function.Body);
312}
313
314void MappingTraits<WasmYAML::Relocation>::mapping(
315 IO &IO, WasmYAML::Relocation &Relocation) {
316 IO.mapRequired("Type", Relocation.Type);
317 IO.mapRequired("Index", Relocation.Index);
318 IO.mapRequired("Offset", Relocation.Offset);
Sam Cleggcc182aa2017-04-26 00:02:31 +0000319 IO.mapOptional("Addend", Relocation.Addend, 0);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000320}
321
Sam Clegg03cdd122017-05-05 18:12:34 +0000322void MappingTraits<WasmYAML::NameEntry>::mapping(
323 IO &IO, WasmYAML::NameEntry &NameEntry) {
324 IO.mapRequired("Index", NameEntry.Index);
325 IO.mapRequired("Name", NameEntry.Name);
326}
327
Thomas Livelycbda16e2019-01-17 02:29:55 +0000328void MappingTraits<WasmYAML::ProducerEntry>::mapping(
329 IO &IO, WasmYAML::ProducerEntry &ProducerEntry) {
330 IO.mapRequired("Name", ProducerEntry.Name);
331 IO.mapRequired("Version", ProducerEntry.Version);
332}
333
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000334void ScalarEnumerationTraits<WasmYAML::FeaturePolicyPrefix>::enumeration(
335 IO &IO, WasmYAML::FeaturePolicyPrefix &Kind) {
336#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_FEATURE_PREFIX_##X);
337 ECase(USED);
338 ECase(REQUIRED);
339 ECase(DISALLOWED);
340#undef ECase
341}
342
343void MappingTraits<WasmYAML::FeatureEntry>::mapping(
344 IO &IO, WasmYAML::FeatureEntry &FeatureEntry) {
345 IO.mapRequired("Prefix", FeatureEntry.Prefix);
346 IO.mapRequired("Name", FeatureEntry.Name);
347}
348
Sam Clegg63ebb812017-09-29 16:50:08 +0000349void MappingTraits<WasmYAML::SegmentInfo>::mapping(
350 IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
351 IO.mapRequired("Index", SegmentInfo.Index);
352 IO.mapRequired("Name", SegmentInfo.Name);
353 IO.mapRequired("Alignment", SegmentInfo.Alignment);
354 IO.mapRequired("Flags", SegmentInfo.Flags);
355}
356
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000357void MappingTraits<WasmYAML::LocalDecl>::mapping(
358 IO &IO, WasmYAML::LocalDecl &LocalDecl) {
359 IO.mapRequired("Type", LocalDecl.Type);
360 IO.mapRequired("Count", LocalDecl.Count);
361}
362
363void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
364 WasmYAML::Limits &Limits) {
365 if (!IO.outputting() || Limits.Flags)
366 IO.mapOptional("Flags", Limits.Flags);
367 IO.mapRequired("Initial", Limits.Initial);
368 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
369 IO.mapOptional("Maximum", Limits.Maximum);
370}
371
372void MappingTraits<WasmYAML::ElemSegment>::mapping(
373 IO &IO, WasmYAML::ElemSegment &Segment) {
374 IO.mapRequired("Offset", Segment.Offset);
375 IO.mapRequired("Functions", Segment.Functions);
376}
377
378void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
379 WasmYAML::Import &Import) {
380 IO.mapRequired("Module", Import.Module);
381 IO.mapRequired("Field", Import.Field);
382 IO.mapRequired("Kind", Import.Kind);
383 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
384 IO.mapRequired("SigIndex", Import.SigIndex);
385 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
Sam Clegg41db5192017-05-10 00:14:04 +0000386 IO.mapRequired("GlobalType", Import.GlobalImport.Type);
387 IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000388 } else if (Import.Kind == wasm::WASM_EXTERNAL_EVENT) {
389 IO.mapRequired("EventAttribute", Import.EventImport.Attribute);
390 IO.mapRequired("EventSigIndex", Import.EventImport.SigIndex);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000391 } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
Sam Clegg41db5192017-05-10 00:14:04 +0000392 IO.mapRequired("Table", Import.TableImport);
Heejin Ahnf208f632018-09-05 01:27:38 +0000393 } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000394 IO.mapRequired("Memory", Import.Memory);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000395 } else {
396 llvm_unreachable("unhandled import type");
397 }
398}
399
400void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
401 WasmYAML::Export &Export) {
402 IO.mapRequired("Name", Export.Name);
403 IO.mapRequired("Kind", Export.Kind);
404 IO.mapRequired("Index", Export.Index);
405}
406
407void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
408 WasmYAML::Global &Global) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000409 IO.mapRequired("Index", Global.Index);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000410 IO.mapRequired("Type", Global.Type);
411 IO.mapRequired("Mutable", Global.Mutable);
412 IO.mapRequired("InitExpr", Global.InitExpr);
413}
414
415void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
416 wasm::WasmInitExpr &Expr) {
417 WasmYAML::Opcode Op = Expr.Opcode;
418 IO.mapRequired("Opcode", Op);
419 Expr.Opcode = Op;
420 switch (Expr.Opcode) {
421 case wasm::WASM_OPCODE_I32_CONST:
422 IO.mapRequired("Value", Expr.Value.Int32);
423 break;
424 case wasm::WASM_OPCODE_I64_CONST:
425 IO.mapRequired("Value", Expr.Value.Int64);
426 break;
427 case wasm::WASM_OPCODE_F32_CONST:
428 IO.mapRequired("Value", Expr.Value.Float32);
429 break;
430 case wasm::WASM_OPCODE_F64_CONST:
431 IO.mapRequired("Value", Expr.Value.Float64);
432 break;
Thomas Lively6a87dda2019-01-08 06:25:55 +0000433 case wasm::WASM_OPCODE_GLOBAL_GET:
Sam Clegg7fb391f2017-04-25 17:11:56 +0000434 IO.mapRequired("Index", Expr.Value.Global);
435 break;
Sam Clegg79aad892020-06-16 15:41:20 -0700436 case wasm::WASM_OPCODE_REF_NULL: {
437 WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
438 IO.mapRequired("Type", Ty);
439 break;
440 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000441 }
442}
443
444void MappingTraits<WasmYAML::DataSegment>::mapping(
445 IO &IO, WasmYAML::DataSegment &Segment) {
Sam Clegg9c07f942017-07-12 00:24:54 +0000446 IO.mapOptional("SectionOffset", Segment.SectionOffset);
Thomas Lively2e150402019-02-19 22:56:19 +0000447 IO.mapRequired("InitFlags", Segment.InitFlags);
448 if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) {
449 IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
450 } else {
451 Segment.MemoryIndex = 0;
452 }
453 if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) {
454 IO.mapRequired("Offset", Segment.Offset);
455 } else {
456 Segment.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST;
457 Segment.Offset.Value.Int32 = 0;
458 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000459 IO.mapRequired("Content", Segment.Content);
460}
461
Sam Clegg42739982017-12-14 21:10:03 +0000462void MappingTraits<WasmYAML::InitFunction>::mapping(
463 IO &IO, WasmYAML::InitFunction &Init) {
464 IO.mapRequired("Priority", Init.Priority);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000465 IO.mapRequired("Symbol", Init.Symbol);
Sam Clegg42739982017-12-14 21:10:03 +0000466}
467
Sam Cleggea7cace2018-01-09 23:43:14 +0000468void ScalarEnumerationTraits<WasmYAML::ComdatKind>::enumeration(
469 IO &IO, WasmYAML::ComdatKind &Kind) {
470#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X);
471 ECase(FUNCTION);
472 ECase(DATA);
473#undef ECase
474}
475
476void MappingTraits<WasmYAML::ComdatEntry>::mapping(
477 IO &IO, WasmYAML::ComdatEntry &ComdatEntry) {
478 IO.mapRequired("Kind", ComdatEntry.Kind);
479 IO.mapRequired("Index", ComdatEntry.Index);
480}
481
Heejin Ahnf208f632018-09-05 01:27:38 +0000482void MappingTraits<WasmYAML::Comdat>::mapping(IO &IO,
483 WasmYAML::Comdat &Comdat) {
Sam Cleggea7cace2018-01-09 23:43:14 +0000484 IO.mapRequired("Name", Comdat.Name);
485 IO.mapRequired("Entries", Comdat.Entries);
486}
487
Sam Cleggb7787fd2017-06-20 04:04:59 +0000488void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
489 WasmYAML::SymbolInfo &Info) {
Sam Clegg6c899ba2018-02-23 05:08:34 +0000490 IO.mapRequired("Index", Info.Index);
491 IO.mapRequired("Kind", Info.Kind);
Sam Clegg5f8c2ed2019-05-07 03:53:16 +0000492 if (Info.Kind != wasm::WASM_SYMBOL_TYPE_SECTION)
493 IO.mapRequired("Name", Info.Name);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000494 IO.mapRequired("Flags", Info.Flags);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000495 if (Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION) {
496 IO.mapRequired("Function", Info.ElementIndex);
497 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
498 IO.mapRequired("Global", Info.ElementIndex);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000499 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT) {
500 IO.mapRequired("Event", Info.ElementIndex);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000501 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA) {
502 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
503 IO.mapRequired("Segment", Info.DataRef.Segment);
504 IO.mapOptional("Offset", Info.DataRef.Offset, 0u);
505 IO.mapRequired("Size", Info.DataRef.Size);
506 }
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000507 } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) {
508 IO.mapRequired("Section", Info.ElementIndex);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000509 } else {
510 llvm_unreachable("unsupported symbol kind");
511 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000512}
513
Heejin Ahnda419bd2018-11-14 02:46:21 +0000514void MappingTraits<WasmYAML::Event>::mapping(IO &IO, WasmYAML::Event &Event) {
515 IO.mapRequired("Index", Event.Index);
516 IO.mapRequired("Attribute", Event.Attribute);
517 IO.mapRequired("SigIndex", Event.SigIndex);
518}
519
Sam Clegg0fc55992017-12-13 22:02:25 +0000520void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
521 IO &IO, WasmYAML::LimitFlags &Value) {
522#define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
523 BCase(HAS_MAX);
Derek Schuff68818062018-11-06 17:27:25 +0000524 BCase(IS_SHARED);
Sam Clegg0fc55992017-12-13 22:02:25 +0000525#undef BCase
526}
527
528void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
Heejin Ahnf208f632018-09-05 01:27:38 +0000529 IO &IO, WasmYAML::SegmentFlags &Value) {}
Sam Clegg0fc55992017-12-13 22:02:25 +0000530
531void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
532 IO &IO, WasmYAML::SymbolFlags &Value) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000533#define BCaseMask(M, X) \
534 IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
535 // BCaseMask(BINDING_MASK, BINDING_GLOBAL);
Sam Clegg0fc55992017-12-13 22:02:25 +0000536 BCaseMask(BINDING_MASK, BINDING_WEAK);
537 BCaseMask(BINDING_MASK, BINDING_LOCAL);
Heejin Ahnf208f632018-09-05 01:27:38 +0000538 // BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
Sam Clegg0fc55992017-12-13 22:02:25 +0000539 BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000540 BCaseMask(UNDEFINED, UNDEFINED);
Sam Cleggd6ef8da2019-02-07 01:24:44 +0000541 BCaseMask(EXPORTED, EXPORTED);
Dan Gohman3b5b9d02019-04-30 19:30:24 +0000542 BCaseMask(EXPLICIT_NAME, EXPLICIT_NAME);
Dan Gohmanda84b682019-08-29 22:40:00 +0000543 BCaseMask(NO_STRIP, NO_STRIP);
Sam Clegg0fc55992017-12-13 22:02:25 +0000544#undef BCaseMask
545}
546
Sam Clegg6c899ba2018-02-23 05:08:34 +0000547void ScalarEnumerationTraits<WasmYAML::SymbolKind>::enumeration(
548 IO &IO, WasmYAML::SymbolKind &Kind) {
549#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_SYMBOL_TYPE_##X);
550 ECase(FUNCTION);
551 ECase(DATA);
552 ECase(GLOBAL);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000553 ECase(SECTION);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000554 ECase(EVENT);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000555#undef ECase
556}
557
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000558void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
559 IO &IO, WasmYAML::ValueType &Type) {
560#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
561 ECase(I32);
562 ECase(I64);
563 ECase(F32);
564 ECase(F64);
Thomas Lively6f21a132018-09-20 22:04:44 +0000565 ECase(V128);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000566 ECase(FUNCREF);
Sam Clegg79aad892020-06-16 15:41:20 -0700567 ECase(EXNREF);
568 ECase(EXTERNREF);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000569 ECase(FUNC);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000570#undef ECase
571}
572
573void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
574 IO &IO, WasmYAML::ExportKind &Kind) {
575#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
576 ECase(FUNCTION);
577 ECase(TABLE);
578 ECase(MEMORY);
579 ECase(GLOBAL);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000580 ECase(EVENT);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000581#undef ECase
582}
583
584void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
585 IO &IO, WasmYAML::Opcode &Code) {
586#define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
587 ECase(END);
588 ECase(I32_CONST);
589 ECase(I64_CONST);
590 ECase(F64_CONST);
591 ECase(F32_CONST);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000592 ECase(GLOBAL_GET);
Sam Clegg79aad892020-06-16 15:41:20 -0700593 ECase(REF_NULL);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000594#undef ECase
595}
596
597void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
598 IO &IO, WasmYAML::TableType &Type) {
599#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000600 ECase(FUNCREF);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000601#undef ECase
602}
603
604void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
605 IO &IO, WasmYAML::RelocType &Type) {
606#define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
Sam Cleggc5d8bc82017-12-21 03:16:34 +0000607#include "llvm/BinaryFormat/WasmRelocs.def"
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000608#undef WASM_RELOC
609}
610
611} // end namespace yaml
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000612
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000613} // end namespace llvm