blob: c5d1b438ee2a0c4bee8eaa91b2b86115661c1adf [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"
15#include "llvm/Object/Wasm.h"
16#include "llvm/Support/Casting.h"
17#include "llvm/Support/MipsABIFlags.h"
18
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
25Section::~Section() {}
26
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
50static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
51 commonSectionMapping(IO, Section);
52 IO.mapRequired("Name", Section.Name);
Sam Clegg03cdd122017-05-05 18:12:34 +000053 if (Section.Name == "name") {
54 IO.mapOptional("FunctionNames", Section.FunctionNames);
55 } else {
56 IO.mapRequired("Payload", Section.Payload);
57 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +000058}
59
60static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
61 commonSectionMapping(IO, Section);
62 IO.mapOptional("Signatures", Section.Signatures);
63}
64
65static void sectionMapping(IO &IO, WasmYAML::ImportSection &Section) {
66 commonSectionMapping(IO, Section);
67 IO.mapOptional("Imports", Section.Imports);
68}
69
70static void sectionMapping(IO &IO, WasmYAML::FunctionSection &Section) {
71 commonSectionMapping(IO, Section);
72 IO.mapOptional("FunctionTypes", Section.FunctionTypes);
73}
74
75static void sectionMapping(IO &IO, WasmYAML::TableSection &Section) {
76 commonSectionMapping(IO, Section);
77 IO.mapOptional("Tables", Section.Tables);
78}
79
80static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
81 commonSectionMapping(IO, Section);
82 IO.mapOptional("Memories", Section.Memories);
83}
84
85static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
86 commonSectionMapping(IO, Section);
87 IO.mapOptional("Globals", Section.Globals);
88}
89
90static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
91 commonSectionMapping(IO, Section);
92 IO.mapOptional("Exports", Section.Exports);
93}
94
95static void sectionMapping(IO &IO, WasmYAML::StartSection &Section) {
96 commonSectionMapping(IO, Section);
97 IO.mapOptional("StartFunction", Section.StartFunction);
98}
99
100static void sectionMapping(IO &IO, WasmYAML::ElemSection &Section) {
101 commonSectionMapping(IO, Section);
102 IO.mapOptional("Segments", Section.Segments);
103}
104
105static void sectionMapping(IO &IO, WasmYAML::CodeSection &Section) {
106 commonSectionMapping(IO, Section);
107 IO.mapRequired("Functions", Section.Functions);
108}
109
110static void sectionMapping(IO &IO, WasmYAML::DataSection &Section) {
111 commonSectionMapping(IO, Section);
112 IO.mapRequired("Segments", Section.Segments);
113}
114
115void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
116 IO &IO, std::unique_ptr<WasmYAML::Section> &Section) {
117 WasmYAML::SectionType SectionType;
118 if (IO.outputting())
119 SectionType = Section->Type;
120 else
121 IO.mapRequired("Type", SectionType);
122
123 switch (SectionType) {
124 case wasm::WASM_SEC_CUSTOM:
125 if (!IO.outputting())
126 Section.reset(new WasmYAML::CustomSection());
127 sectionMapping(IO, *cast<WasmYAML::CustomSection>(Section.get()));
128 break;
129 case wasm::WASM_SEC_TYPE:
130 if (!IO.outputting())
131 Section.reset(new WasmYAML::TypeSection());
132 sectionMapping(IO, *cast<WasmYAML::TypeSection>(Section.get()));
133 break;
134 case wasm::WASM_SEC_IMPORT:
135 if (!IO.outputting())
136 Section.reset(new WasmYAML::ImportSection());
137 sectionMapping(IO, *cast<WasmYAML::ImportSection>(Section.get()));
138 break;
139 case wasm::WASM_SEC_FUNCTION:
140 if (!IO.outputting())
141 Section.reset(new WasmYAML::FunctionSection());
142 sectionMapping(IO, *cast<WasmYAML::FunctionSection>(Section.get()));
143 break;
144 case wasm::WASM_SEC_TABLE:
145 if (!IO.outputting())
146 Section.reset(new WasmYAML::TableSection());
147 sectionMapping(IO, *cast<WasmYAML::TableSection>(Section.get()));
148 break;
149 case wasm::WASM_SEC_MEMORY:
150 if (!IO.outputting())
151 Section.reset(new WasmYAML::MemorySection());
152 sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
153 break;
154 case wasm::WASM_SEC_GLOBAL:
155 if (!IO.outputting())
156 Section.reset(new WasmYAML::GlobalSection());
157 sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
158 break;
159 case wasm::WASM_SEC_EXPORT:
160 if (!IO.outputting())
161 Section.reset(new WasmYAML::ExportSection());
162 sectionMapping(IO, *cast<WasmYAML::ExportSection>(Section.get()));
163 break;
164 case wasm::WASM_SEC_START:
165 if (!IO.outputting())
166 Section.reset(new WasmYAML::StartSection());
167 sectionMapping(IO, *cast<WasmYAML::StartSection>(Section.get()));
168 break;
169 case wasm::WASM_SEC_ELEM:
170 if (!IO.outputting())
171 Section.reset(new WasmYAML::ElemSection());
172 sectionMapping(IO, *cast<WasmYAML::ElemSection>(Section.get()));
173 break;
174 case wasm::WASM_SEC_CODE:
175 if (!IO.outputting())
176 Section.reset(new WasmYAML::CodeSection());
177 sectionMapping(IO, *cast<WasmYAML::CodeSection>(Section.get()));
178 break;
179 case wasm::WASM_SEC_DATA:
180 if (!IO.outputting())
181 Section.reset(new WasmYAML::DataSection());
182 sectionMapping(IO, *cast<WasmYAML::DataSection>(Section.get()));
183 break;
184 default:
185 llvm_unreachable("Unknown section type");
186 }
187}
188
189void ScalarEnumerationTraits<WasmYAML::SectionType>::enumeration(
190 IO &IO, WasmYAML::SectionType &Type) {
191#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_SEC_##X);
192 ECase(CUSTOM);
193 ECase(TYPE);
194 ECase(IMPORT);
195 ECase(FUNCTION);
196 ECase(TABLE);
197 ECase(MEMORY);
198 ECase(GLOBAL);
199 ECase(EXPORT);
200 ECase(START);
201 ECase(ELEM);
202 ECase(CODE);
203 ECase(DATA);
204#undef ECase
205}
206
207void MappingTraits<WasmYAML::Signature>::mapping(
208 IO &IO, WasmYAML::Signature &Signature) {
209 IO.mapOptional("Index", Signature.Index);
210 IO.mapRequired("ReturnType", Signature.ReturnType);
211 IO.mapRequired("ParamTypes", Signature.ParamTypes);
212}
213
214void MappingTraits<WasmYAML::Table>::mapping(IO &IO, WasmYAML::Table &Table) {
215 IO.mapRequired("ElemType", Table.ElemType);
216 IO.mapRequired("Limits", Table.TableLimits);
217}
218
219void MappingTraits<WasmYAML::Function>::mapping(IO &IO,
220 WasmYAML::Function &Function) {
221 IO.mapRequired("Locals", Function.Locals);
222 IO.mapRequired("Body", Function.Body);
223}
224
225void MappingTraits<WasmYAML::Relocation>::mapping(
226 IO &IO, WasmYAML::Relocation &Relocation) {
227 IO.mapRequired("Type", Relocation.Type);
228 IO.mapRequired("Index", Relocation.Index);
229 IO.mapRequired("Offset", Relocation.Offset);
Sam Cleggcc182aa2017-04-26 00:02:31 +0000230 IO.mapOptional("Addend", Relocation.Addend, 0);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000231}
232
Sam Clegg03cdd122017-05-05 18:12:34 +0000233void MappingTraits<WasmYAML::NameEntry>::mapping(
234 IO &IO, WasmYAML::NameEntry &NameEntry) {
235 IO.mapRequired("Index", NameEntry.Index);
236 IO.mapRequired("Name", NameEntry.Name);
237}
238
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000239void MappingTraits<WasmYAML::LocalDecl>::mapping(
240 IO &IO, WasmYAML::LocalDecl &LocalDecl) {
241 IO.mapRequired("Type", LocalDecl.Type);
242 IO.mapRequired("Count", LocalDecl.Count);
243}
244
245void MappingTraits<WasmYAML::Limits>::mapping(IO &IO,
246 WasmYAML::Limits &Limits) {
247 if (!IO.outputting() || Limits.Flags)
248 IO.mapOptional("Flags", Limits.Flags);
249 IO.mapRequired("Initial", Limits.Initial);
250 if (!IO.outputting() || Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
251 IO.mapOptional("Maximum", Limits.Maximum);
252}
253
254void MappingTraits<WasmYAML::ElemSegment>::mapping(
255 IO &IO, WasmYAML::ElemSegment &Segment) {
256 IO.mapRequired("Offset", Segment.Offset);
257 IO.mapRequired("Functions", Segment.Functions);
258}
259
260void MappingTraits<WasmYAML::Import>::mapping(IO &IO,
261 WasmYAML::Import &Import) {
262 IO.mapRequired("Module", Import.Module);
263 IO.mapRequired("Field", Import.Field);
264 IO.mapRequired("Kind", Import.Kind);
265 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
266 IO.mapRequired("SigIndex", Import.SigIndex);
267 } else if (Import.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
268 IO.mapRequired("GlobalType", Import.GlobalType);
269 IO.mapRequired("GlobalMutable", Import.GlobalMutable);
270 } else {
271 llvm_unreachable("unhandled import type");
272 }
273}
274
275void MappingTraits<WasmYAML::Export>::mapping(IO &IO,
276 WasmYAML::Export &Export) {
277 IO.mapRequired("Name", Export.Name);
278 IO.mapRequired("Kind", Export.Kind);
279 IO.mapRequired("Index", Export.Index);
280}
281
282void MappingTraits<WasmYAML::Global>::mapping(IO &IO,
283 WasmYAML::Global &Global) {
284 IO.mapRequired("Type", Global.Type);
285 IO.mapRequired("Mutable", Global.Mutable);
286 IO.mapRequired("InitExpr", Global.InitExpr);
287}
288
289void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
290 wasm::WasmInitExpr &Expr) {
291 WasmYAML::Opcode Op = Expr.Opcode;
292 IO.mapRequired("Opcode", Op);
293 Expr.Opcode = Op;
294 switch (Expr.Opcode) {
295 case wasm::WASM_OPCODE_I32_CONST:
296 IO.mapRequired("Value", Expr.Value.Int32);
297 break;
298 case wasm::WASM_OPCODE_I64_CONST:
299 IO.mapRequired("Value", Expr.Value.Int64);
300 break;
301 case wasm::WASM_OPCODE_F32_CONST:
302 IO.mapRequired("Value", Expr.Value.Float32);
303 break;
304 case wasm::WASM_OPCODE_F64_CONST:
305 IO.mapRequired("Value", Expr.Value.Float64);
306 break;
Sam Clegg7fb391f2017-04-25 17:11:56 +0000307 case wasm::WASM_OPCODE_GET_GLOBAL:
308 IO.mapRequired("Index", Expr.Value.Global);
309 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000310 }
311}
312
313void MappingTraits<WasmYAML::DataSegment>::mapping(
314 IO &IO, WasmYAML::DataSegment &Segment) {
315 IO.mapRequired("Index", Segment.Index);
316 IO.mapRequired("Offset", Segment.Offset);
317 IO.mapRequired("Content", Segment.Content);
318}
319
320void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
321 IO &IO, WasmYAML::ValueType &Type) {
322#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
323 ECase(I32);
324 ECase(I64);
325 ECase(F32);
326 ECase(F64);
327 ECase(ANYFUNC);
328 ECase(FUNC);
329 ECase(NORESULT);
330#undef ECase
331}
332
333void ScalarEnumerationTraits<WasmYAML::ExportKind>::enumeration(
334 IO &IO, WasmYAML::ExportKind &Kind) {
335#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_EXTERNAL_##X);
336 ECase(FUNCTION);
337 ECase(TABLE);
338 ECase(MEMORY);
339 ECase(GLOBAL);
340#undef ECase
341}
342
343void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
344 IO &IO, WasmYAML::Opcode &Code) {
345#define ECase(X) IO.enumCase(Code, #X, wasm::WASM_OPCODE_##X);
346 ECase(END);
347 ECase(I32_CONST);
348 ECase(I64_CONST);
349 ECase(F64_CONST);
350 ECase(F32_CONST);
351 ECase(GET_GLOBAL);
352#undef ECase
353}
354
355void ScalarEnumerationTraits<WasmYAML::TableType>::enumeration(
356 IO &IO, WasmYAML::TableType &Type) {
357#define ECase(X) IO.enumCase(Type, #X, wasm::WASM_TYPE_##X);
358 ECase(ANYFUNC);
359#undef ECase
360}
361
362void ScalarEnumerationTraits<WasmYAML::RelocType>::enumeration(
363 IO &IO, WasmYAML::RelocType &Type) {
364#define WASM_RELOC(name, value) IO.enumCase(Type, #name, wasm::name);
365#include "llvm/Support/WasmRelocs/WebAssembly.def"
366#undef WASM_RELOC
367}
368
369} // end namespace yaml
370} // end namespace llvm