blob: 8687f22949a2985f46868118e9934e0061136d84 [file] [log] [blame]
Derek Schuffd3d84fd2017-03-30 19:44:09 +00001//===- WasmYAML.cpp - Wasm YAMLIO implementation --------------------------===//
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// This file defines classes for handling the YAML representation of wasm.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ObjectYAML/WasmYAML.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000015#include "llvm/ADT/StringRef.h"
Derek Schuffd3d84fd2017-03-30 19:44:09 +000016#include "llvm/Support/Casting.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000017#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/YAMLTraits.h"
Derek Schuffd3d84fd2017-03-30 19:44:09 +000019
20namespace llvm {
Derek Schuffc5b472f2017-03-31 22:14:14 +000021
22namespace WasmYAML {
23
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
Eugene Zelenko28082ab2017-07-01 01:35:55 +000026Section::~Section() = default;
Derek Schuffc5b472f2017-03-31 22:14:14 +000027
28} // end namespace WasmYAML
29
Derek Schuffd3d84fd2017-03-30 19:44:09 +000030namespace yaml {
31
32void MappingTraits<WasmYAML::FileHeader>::mapping(
33 IO &IO, WasmYAML::FileHeader &FileHdr) {
34 IO.mapRequired("Version", FileHdr.Version);
35}
36
37void 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);
44}
45
46static void commonSectionMapping(IO &IO, WasmYAML::Section &Section) {
47 IO.mapRequired("Type", Section.Type);
48 IO.mapOptional("Relocations", Section.Relocations);
49}
50
Sam Cleggb7787fd2017-06-20 04:04:59 +000051static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
52 commonSectionMapping(IO, Section);
53 IO.mapRequired("Name", Section.Name);
54 IO.mapOptional("FunctionNames", Section.FunctionNames);
55}
56
57static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
58 commonSectionMapping(IO, Section);
59 IO.mapRequired("Name", Section.Name);
Sam Clegg9e1ade92017-06-27 20:27:59 +000060 IO.mapRequired("DataSize", Section.DataSize);
Sam Clegge7a60702017-09-06 22:05:41 +000061 IO.mapOptional("SymbolInfo", Section.SymbolInfos);
Sam Clegg63ebb812017-09-29 16:50:08 +000062 IO.mapOptional("SegmentInfo", Section.SegmentInfos);
Sam Clegg42739982017-12-14 21:10:03 +000063 IO.mapOptional("InitFunctions", Section.InitFunctions);
Sam Cleggb7787fd2017-06-20 04:04:59 +000064}
65
Derek Schuffd3d84fd2017-03-30 19:44:09 +000066static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
67 commonSectionMapping(IO, Section);
68 IO.mapRequired("Name", Section.Name);
Sam Cleggb7787fd2017-06-20 04:04:59 +000069 IO.mapRequired("Payload", Section.Payload);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000070}
71
72static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
73 commonSectionMapping(IO, Section);
74 IO.mapOptional("Signatures", Section.Signatures);
75}
76
77static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
78 commonSectionMapping(IO, Section);
79 IO.mapOptional("Imports", Section.Imports);
80}
81
82static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
83 commonSectionMapping(IO, Section);
84 IO.mapOptional("FunctionTypes", Section.FunctionTypes);
85}
86
87static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
88 commonSectionMapping(IO, Section);
89 IO.mapOptional("Tables", Section.Tables);
90}
91
92static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
93 commonSectionMapping(IO, Section);
94 IO.mapOptional("Memories", Section.Memories);
95}
96
97static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
98 commonSectionMapping(IO, Section);
99 IO.mapOptional("Globals", Section.Globals);
100}
101
102static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
103 commonSectionMapping(IO, Section);
104 IO.mapOptional("Exports", Section.Exports);
105}
106
107static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
108 commonSectionMapping(IO, Section);
109 IO.mapOptional("StartFunction", Section.StartFunction);
110}
111
112static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
113 commonSectionMapping(IO, Section);
114 IO.mapOptional("Segments", Section.Segments);
115}
116
117static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
118 commonSectionMapping(IO, Section);
119 IO.mapRequired("Functions", Section.Functions);
120}
121
122static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
123 commonSectionMapping(IO, Section);
124 IO.mapRequired("Segments", Section.Segments);
125}
126
127void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
128 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
129 WasmYAML::SectionType SectionType;
130 if (IO.outputting())
131 SectionType = Section->Type;
132 else
133 IO.mapRequired("Type", SectionType);
134
135 switch (SectionType) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000136 case wasm::WASM_SEC_CUSTOM: {
137 StringRef SectionName;
138 if (IO.outputting()) {
139 auto CustomSection = cast<WasmYAML::CustomSection>(Section.get());
140 SectionName = CustomSection->Name;
141 } else {
142 IO.mapRequired("Name", SectionName);
143 }
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()));
152 } else {
153 if (!IO.outputting())
154 Section.reset(new WasmYAML::CustomSection(SectionName));
155 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
156 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000157 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000158 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000159 case wasm::WASM_SEC_TYPE:
160 if (!IO.outputting())
161 Section.reset(new WasmYAML::TypeSection());
162 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
163 break;
164 case wasm::WASM_SEC_IMPORT:
165 if (!IO.outputting())
166 Section.reset(new WasmYAML::ImportSection());
167 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
168 break;
169 case wasm::WASM_SEC_FUNCTION:
170 if (!IO.outputting())
171 Section.reset(new WasmYAML::FunctionSection());
172 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
173 break;
174 case wasm::WASM_SEC_TABLE:
175 if (!IO.outputting())
176 Section.reset(new WasmYAML::TableSection());
177 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
178 break;
179 case wasm::WASM_SEC_MEMORY:
180 if (!IO.outputting())
181 Section.reset(new WasmYAML::MemorySection());
182 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
183 break;
184 case wasm::WASM_SEC_GLOBAL:
185 if (!IO.outputting())
186 Section.reset(new WasmYAML::GlobalSection());
187 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
188 break;
189 case wasm::WASM_SEC_EXPORT:
190 if (!IO.outputting())
191 Section.reset(new WasmYAML::ExportSection());
192 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
193 break;
194 case wasm::WASM_SEC_START:
195 if (!IO.outputting())
196 Section.reset(new WasmYAML::StartSection());
197 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
198 break;
199 case wasm::WASM_SEC_ELEM:
200 if (!IO.outputting())
201 Section.reset(new WasmYAML::ElemSection());
202 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
203 break;
204 case wasm::WASM_SEC_CODE:
205 if (!IO.outputting())
206 Section.reset(new WasmYAML::CodeSection());
207 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
208 break;
209 case wasm::WASM_SEC_DATA:
210 if (!IO.outputting())
211 Section.reset(new WasmYAML::DataSection());
212 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
213 break;
214 default:
215 llvm_unreachable("Unknown section type");
216 }
217}
218
219void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
220 IO &IO, WasmYAML::SectionType &Type) {
221#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
222 ECase(CUSTOM);
223 ECase(TYPE);
224 ECase(IMPORT);
225 ECase(FUNCTION);
226 ECase(TABLE);
227 ECase(MEMORY);
228 ECase(GLOBAL);
229 ECase(EXPORT);
230 ECase(START);
231 ECase(ELEM);
232 ECase(CODE);
233 ECase(DATA);
234#undef ECase
235}
236
237void 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);
242}
243
244void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
245 IO.mapRequired("ElemType", Table.ElemType);
246 IO.mapRequired("Limits", Table.TableLimits);
247}
248
249void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
250 WasmYAML::Function &Function) {
251 IO.mapRequired("Locals", Function.Locals);
252 IO.mapRequired("Body", Function.Body);
253}
254
255void 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);
Sam Cleggcc182aa2017-04-26 00:02:31 +0000260 IO.mapOptional("Addend", Relocation.Addend, 0);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000261}
262
Sam Clegg03cdd122017-05-05 18:12:34 +0000263void MappingTraits<WasmYAML::NameEntry>::mapping(
264 IO &IO, WasmYAML::NameEntry &NameEntry) {
265 IO.mapRequired("Index", NameEntry.Index);
266 IO.mapRequired("Name", NameEntry.Name);
267}
268
Sam Clegg63ebb812017-09-29 16:50:08 +0000269void MappingTraits<WasmYAML::SegmentInfo>::mapping(
270 IO &IO, WasmYAML::SegmentInfo &SegmentInfo) {
271 IO.mapRequired("Index", SegmentInfo.Index);
272 IO.mapRequired("Name", SegmentInfo.Name);
273 IO.mapRequired("Alignment", SegmentInfo.Alignment);
274 IO.mapRequired("Flags", SegmentInfo.Flags);
275}
276
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000277void MappingTraits<WasmYAML::LocalDecl>::mapping(
278 IO &IO, WasmYAML::LocalDecl &LocalDecl) {
279 IO.mapRequired("Type", LocalDecl.Type);
280 IO.mapRequired("Count", LocalDecl.Count);
281}
282
283void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
284 WasmYAML::Limits &Limits) {
285 if (!IO.outputting() || Limits.Flags)
286 IO.mapOptional("Flags", Limits.Flags);
287 IO.mapRequired("Initial", Limits.Initial);
288 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
289 IO.mapOptional("Maximum", Limits.Maximum);
290}
291
292void MappingTraits<WasmYAML::ElemSegment>::mapping(
293 IO &IO, WasmYAML::ElemSegment &Segment) {
294 IO.mapRequired("Offset", Segment.Offset);
295 IO.mapRequired("Functions", Segment.Functions);
296}
297
298void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
299 WasmYAML::Import &Import) {
300 IO.mapRequired("Module", Import.Module);
301 IO.mapRequired("Field", Import.Field);
302 IO.mapRequired("Kind", Import.Kind);
303 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
304 IO.mapRequired("SigIndex", Import.SigIndex);
305 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
Sam Clegg41db5192017-05-10 00:14:04 +0000306 IO.mapRequired("GlobalType", Import.GlobalImport.Type);
307 IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000308 } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) {
Sam Clegg41db5192017-05-10 00:14:04 +0000309 IO.mapRequired("Table", Import.TableImport);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000310 } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY ) {
311 IO.mapRequired("Memory", Import.Memory);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000312 } else {
313 llvm_unreachable("unhandled import type");
314 }
315}
316
317void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
318 WasmYAML::Export &Export) {
319 IO.mapRequired("Name", Export.Name);
320 IO.mapRequired("Kind", Export.Kind);
321 IO.mapRequired("Index", Export.Index);
322}
323
324void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
325 WasmYAML::Global &Global) {
326 IO.mapRequired("Type", Global.Type);
327 IO.mapRequired("Mutable", Global.Mutable);
328 IO.mapRequired("InitExpr", Global.InitExpr);
329}
330
331void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
332 wasm::WasmInitExpr &Expr) {
333 WasmYAML::Opcode Op = Expr.Opcode;
334 IO.mapRequired("Opcode", Op);
335 Expr.Opcode = Op;
336 switch (Expr.Opcode) {
337 case wasm::WASM_OPCODE_I32_CONST:
338 IO.mapRequired("Value", Expr.Value.Int32);
339 break;
340 case wasm::WASM_OPCODE_I64_CONST:
341 IO.mapRequired("Value", Expr.Value.Int64);
342 break;
343 case wasm::WASM_OPCODE_F32_CONST:
344 IO.mapRequired("Value", Expr.Value.Float32);
345 break;
346 case wasm::WASM_OPCODE_F64_CONST:
347 IO.mapRequired("Value", Expr.Value.Float64);
348 break;
Sam Clegg7fb391f2017-04-25 17:11:56 +0000349 case wasm::WASM_OPCODE_GET_GLOBAL:
350 IO.mapRequired("Index", Expr.Value.Global);
351 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000352 }
353}
354
355void MappingTraits<WasmYAML::DataSegment>::mapping(
356 IO &IO, WasmYAML::DataSegment &Segment) {
Sam Clegg9c07f942017-07-12 00:24:54 +0000357 IO.mapOptional("SectionOffset", Segment.SectionOffset);
358 IO.mapRequired("MemoryIndex", Segment.MemoryIndex);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000359 IO.mapRequired("Offset", Segment.Offset);
360 IO.mapRequired("Content", Segment.Content);
361}
362
Sam Clegg42739982017-12-14 21:10:03 +0000363void MappingTraits<WasmYAML::InitFunction>::mapping(
364 IO &IO, WasmYAML::InitFunction &Init) {
365 IO.mapRequired("Priority", Init.Priority);
366 IO.mapRequired("FunctionIndex", Init.FunctionIndex);
367}
368
Sam Cleggb7787fd2017-06-20 04:04:59 +0000369void MappingTraits<WasmYAML::SymbolInfo>::mapping(IO &IO,
370 WasmYAML::SymbolInfo &Info) {
371 IO.mapRequired("Name", Info.Name);
372 IO.mapRequired("Flags", Info.Flags);
373}
374
Sam Clegg0fc55992017-12-13 22:02:25 +0000375void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
376 IO &IO, WasmYAML::LimitFlags &Value) {
377#define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
378 BCase(HAS_MAX);
379#undef BCase
380}
381
382void ScalarBitSetTraits<WasmYAML::SegmentFlags>::bitset(
383 IO &IO, WasmYAML::SegmentFlags &Value) {
384}
385
386void ScalarBitSetTraits<WasmYAML::SymbolFlags>::bitset(
387 IO &IO, WasmYAML::SymbolFlags &Value) {
388#define BCaseMask(M, X) IO.maskedBitSetCase(Value, #X, wasm::WASM_SYMBOL_##X, wasm::WASM_SYMBOL_##M)
389 //BCaseMask(BINDING_MASK, BINDING_GLOBAL);
390 BCaseMask(BINDING_MASK, BINDING_WEAK);
391 BCaseMask(BINDING_MASK, BINDING_LOCAL);
392 //BCaseMask(VISIBILITY_MASK, VISIBILITY_DEFAULT);
393 BCaseMask(VISIBILITY_MASK, VISIBILITY_HIDDEN);
394#undef BCaseMask
395}
396
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000397void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
398 IO &IO, WasmYAML::ValueType &Type) {
399#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
400 ECase(I32);
401 ECase(I64);
402 ECase(F32);
403 ECase(F64);
404 ECase(ANYFUNC);
405 ECase(FUNC);
406 ECase(NORESULT);
407#undef ECase
408}
409
410void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
411 IO &IO, WasmYAML::ExportKind &Kind) {
412#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
413 ECase(FUNCTION);
414 ECase(TABLE);
415 ECase(MEMORY);
416 ECase(GLOBAL);
417#undef ECase
418}
419
420void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
421 IO &IO, WasmYAML::Opcode &Code) {
422#define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
423 ECase(END);
424 ECase(I32_CONST);
425 ECase(I64_CONST);
426 ECase(F64_CONST);
427 ECase(F32_CONST);
428 ECase(GET_GLOBAL);
429#undef ECase
430}
431
432void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
433 IO &IO, WasmYAML::TableType &Type) {
434#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
435 ECase(ANYFUNC);
436#undef ECase
437}
438
439void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
440 IO &IO, WasmYAML::RelocType &Type) {
441#define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
Zachary Turner264b5d92017-06-07 03:48:56 +0000442#include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000443#undef WASM_RELOC
444}
445
446} // end namespace yaml
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000447
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000448} // end namespace llvm