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