blob: 0809cc458ace0c6cddd58653a69493dc4b1701d0 [file] [log] [blame]
Derek Schuffd3d84fd2017-03-30 19:44:09 +00001//===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
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/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// The Wasm component of yaml2obj.
Derek Schuffd3d84fd2017-03-30 19:44:09 +000012///
13//===----------------------------------------------------------------------===//
14//
Sam Clegg6c899ba2018-02-23 05:08:34 +000015
Derek Schuffd3d84fd2017-03-30 19:44:09 +000016#include "llvm/ObjectYAML/ObjectYAML.h"
17#include "llvm/Support/Endian.h"
18#include "llvm/Support/LEB128.h"
19
20using namespace llvm;
21
22/// This parses a yaml stream that represents a Wasm object file.
23/// See docs/yaml2obj for the yaml scheema.
24class WasmWriter {
25public:
26 WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {}
27 int writeWasm(raw_ostream &OS);
Sam Cleggb7787fd2017-06-20 04:04:59 +000028
29private:
Sam Clegg6f08c842018-04-24 18:11:36 +000030 int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
31 uint32_t SectionIndex);
Sam Clegg03cdd122017-05-05 18:12:34 +000032
Derek Schuffd3d84fd2017-03-30 19:44:09 +000033 int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
34 int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
35 int writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
36 int writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
37 int writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
38 int writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
39 int writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
Heejin Ahnda419bd2018-11-14 02:46:21 +000040 int writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000041 int writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
42 int writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
43 int writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
44 int writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
45 int writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
46
Sam Cleggb7787fd2017-06-20 04:04:59 +000047 // Custom section types
Sam Clegge4afbc62018-11-14 18:36:24 +000048 int writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
Sam Cleggb7787fd2017-06-20 04:04:59 +000049 int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
50 int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000051 WasmYAML::Object &Obj;
Sam Clegge53af7f2018-01-09 21:38:53 +000052 uint32_t NumImportedFunctions = 0;
53 uint32_t NumImportedGlobals = 0;
Heejin Ahnda419bd2018-11-14 02:46:21 +000054 uint32_t NumImportedEvents = 0;
Derek Schuffd3d84fd2017-03-30 19:44:09 +000055};
56
57static int writeUint64(raw_ostream &OS, uint64_t Value) {
58 char Data[sizeof(Value)];
59 support::endian::write64le(Data, Value);
60 OS.write(Data, sizeof(Data));
61 return 0;
62}
63
64static int writeUint32(raw_ostream &OS, uint32_t Value) {
65 char Data[sizeof(Value)];
66 support::endian::write32le(Data, Value);
67 OS.write(Data, sizeof(Data));
68 return 0;
69}
70
71static int writeUint8(raw_ostream &OS, uint8_t Value) {
72 char Data[sizeof(Value)];
73 memcpy(Data, &Value, sizeof(Data));
74 OS.write(Data, sizeof(Data));
75 return 0;
76}
77
Sam Clegg03cdd122017-05-05 18:12:34 +000078static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +000079 encodeULEB128(Str.size(), OS);
80 OS << Str;
81 return 0;
82}
83
Sam Clegg03cdd122017-05-05 18:12:34 +000084static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
Sam Clegg03e101f2018-03-01 18:06:21 +000085 writeUint8(OS, Lim.Flags);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000086 encodeULEB128(Lim.Initial, OS);
87 if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
88 encodeULEB128(Lim.Maximum, OS);
89 return 0;
90}
91
Sam Clegg03cdd122017-05-05 18:12:34 +000092static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +000093 writeUint8(OS, InitExpr.Opcode);
94 switch (InitExpr.Opcode) {
95 case wasm::WASM_OPCODE_I32_CONST:
96 encodeSLEB128(InitExpr.Value.Int32, OS);
97 break;
98 case wasm::WASM_OPCODE_I64_CONST:
99 encodeSLEB128(InitExpr.Value.Int64, OS);
100 break;
101 case wasm::WASM_OPCODE_F32_CONST:
102 writeUint32(OS, InitExpr.Value.Float32);
103 break;
104 case wasm::WASM_OPCODE_F64_CONST:
105 writeUint64(OS, InitExpr.Value.Float64);
106 break;
107 case wasm::WASM_OPCODE_GET_GLOBAL:
108 encodeULEB128(InitExpr.Value.Global, OS);
109 break;
110 default:
Sam Clegge53af7f2018-01-09 21:38:53 +0000111 errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n";
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000112 return 1;
113 }
114 writeUint8(OS, wasm::WASM_OPCODE_END);
115 return 0;
116}
117
Sam Clegg9e1ade92017-06-27 20:27:59 +0000118class SubSectionWriter {
119 raw_ostream &OS;
120 std::string OutString;
121 raw_string_ostream StringStream;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000122
Sam Clegg9e1ade92017-06-27 20:27:59 +0000123public:
124 SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
Sam Cleggb7787fd2017-06-20 04:04:59 +0000125
Sam Clegg9e1ade92017-06-27 20:27:59 +0000126 void Done() {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000127 StringStream.flush();
128 encodeULEB128(OutString.size(), OS);
129 OS << OutString;
Sam Clegg9e1ade92017-06-27 20:27:59 +0000130 OutString.clear();
131 }
132
Heejin Ahnf208f632018-09-05 01:27:38 +0000133 raw_ostream &GetStream() { return StringStream; }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000134};
135
Heejin Ahnf208f632018-09-05 01:27:38 +0000136int WasmWriter::writeSectionContent(raw_ostream &OS,
Sam Clegge4afbc62018-11-14 18:36:24 +0000137 WasmYAML::DylinkSection &Section) {
138 writeStringRef(Section.Name, OS);
139 encodeULEB128(Section.MemorySize, OS);
140 encodeULEB128(Section.MemoryAlignment, OS);
141 encodeULEB128(Section.TableSize, OS);
142 encodeULEB128(Section.TableAlignment, OS);
143 return 0;
144}
145
146int WasmWriter::writeSectionContent(raw_ostream &OS,
Heejin Ahnf208f632018-09-05 01:27:38 +0000147 WasmYAML::LinkingSection &Section) {
Sam Clegg9e1ade92017-06-27 20:27:59 +0000148 writeStringRef(Section.Name, OS);
Sam Clegg6bb5a412018-04-26 18:15:32 +0000149 encodeULEB128(Section.Version, OS);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000150
151 SubSectionWriter SubSection(OS);
152
Sam Clegg6c899ba2018-02-23 05:08:34 +0000153 // SYMBOL_TABLE subsection
154 if (Section.SymbolTable.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000155 writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000156
Sam Clegg6c899ba2018-02-23 05:08:34 +0000157 encodeULEB128(Section.SymbolTable.size(), SubSection.GetStream());
Benjamin Kramer8d71fdc2018-02-23 12:20:18 +0000158#ifndef NDEBUG
Sam Clegg6c899ba2018-02-23 05:08:34 +0000159 uint32_t SymbolIndex = 0;
160#endif
161 for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) {
162 assert(Info.Index == SymbolIndex++);
Sam Clegg03e101f2018-03-01 18:06:21 +0000163 writeUint8(SubSection.GetStream(), Info.Kind);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000164 encodeULEB128(Info.Flags, SubSection.GetStream());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000165 switch (Info.Kind) {
166 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
167 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
Heejin Ahnda419bd2018-11-14 02:46:21 +0000168 case wasm::WASM_SYMBOL_TYPE_EVENT:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000169 encodeULEB128(Info.ElementIndex, SubSection.GetStream());
170 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
171 writeStringRef(Info.Name, SubSection.GetStream());
172 break;
173 case wasm::WASM_SYMBOL_TYPE_DATA:
174 writeStringRef(Info.Name, SubSection.GetStream());
175 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
176 encodeULEB128(Info.DataRef.Segment, SubSection.GetStream());
177 encodeULEB128(Info.DataRef.Offset, SubSection.GetStream());
178 encodeULEB128(Info.DataRef.Size, SubSection.GetStream());
179 }
180 break;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000181 case wasm::WASM_SYMBOL_TYPE_SECTION:
182 encodeULEB128(Info.ElementIndex, SubSection.GetStream());
183 break;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000184 default:
185 llvm_unreachable("unexpected kind");
186 }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000187 }
188
189 SubSection.Done();
Sam Cleggb7787fd2017-06-20 04:04:59 +0000190 }
Sam Cleggd95ed952017-09-20 19:03:35 +0000191
192 // SEGMENT_NAMES subsection
Sam Clegg63ebb812017-09-29 16:50:08 +0000193 if (Section.SegmentInfos.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000194 writeUint8(OS, wasm::WASM_SEGMENT_INFO);
Sam Clegg63ebb812017-09-29 16:50:08 +0000195 encodeULEB128(Section.SegmentInfos.size(), SubSection.GetStream());
196 for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
Sam Clegg63ebb812017-09-29 16:50:08 +0000197 writeStringRef(SegmentInfo.Name, SubSection.GetStream());
198 encodeULEB128(SegmentInfo.Alignment, SubSection.GetStream());
199 encodeULEB128(SegmentInfo.Flags, SubSection.GetStream());
Sam Cleggd95ed952017-09-20 19:03:35 +0000200 }
201 SubSection.Done();
202 }
Sam Clegg42739982017-12-14 21:10:03 +0000203
204 // INIT_FUNCS subsection
205 if (Section.InitFunctions.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000206 writeUint8(OS, wasm::WASM_INIT_FUNCS);
Sam Clegg42739982017-12-14 21:10:03 +0000207 encodeULEB128(Section.InitFunctions.size(), SubSection.GetStream());
208 for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
209 encodeULEB128(Func.Priority, SubSection.GetStream());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000210 encodeULEB128(Func.Symbol, SubSection.GetStream());
Sam Clegg42739982017-12-14 21:10:03 +0000211 }
212 SubSection.Done();
213 }
Sam Cleggea7cace2018-01-09 23:43:14 +0000214
215 // COMDAT_INFO subsection
216 if (Section.Comdats.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000217 writeUint8(OS, wasm::WASM_COMDAT_INFO);
Sam Cleggea7cace2018-01-09 23:43:14 +0000218 encodeULEB128(Section.Comdats.size(), SubSection.GetStream());
219 for (const auto &C : Section.Comdats) {
220 writeStringRef(C.Name, SubSection.GetStream());
221 encodeULEB128(0, SubSection.GetStream()); // flags for future use
222 encodeULEB128(C.Entries.size(), SubSection.GetStream());
223 for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000224 writeUint8(SubSection.GetStream(), Entry.Kind);
Sam Cleggea7cace2018-01-09 23:43:14 +0000225 encodeULEB128(Entry.Index, SubSection.GetStream());
226 }
227 }
228 SubSection.Done();
229 }
230
Sam Cleggb7787fd2017-06-20 04:04:59 +0000231 return 0;
232}
233
Heejin Ahnf208f632018-09-05 01:27:38 +0000234int WasmWriter::writeSectionContent(raw_ostream &OS,
235 WasmYAML::NameSection &Section) {
Sam Clegg03cdd122017-05-05 18:12:34 +0000236 writeStringRef(Section.Name, OS);
237 if (Section.FunctionNames.size()) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000238 writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
Sam Clegg03cdd122017-05-05 18:12:34 +0000239
Sam Clegg9e1ade92017-06-27 20:27:59 +0000240 SubSectionWriter SubSection(OS);
241
242 encodeULEB128(Section.FunctionNames.size(), SubSection.GetStream());
Sam Clegg03cdd122017-05-05 18:12:34 +0000243 for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
Sam Clegg9e1ade92017-06-27 20:27:59 +0000244 encodeULEB128(NameEntry.Index, SubSection.GetStream());
245 writeStringRef(NameEntry.Name, SubSection.GetStream());
Sam Clegg03cdd122017-05-05 18:12:34 +0000246 }
247
Sam Clegg9e1ade92017-06-27 20:27:59 +0000248 SubSection.Done();
Sam Clegg03cdd122017-05-05 18:12:34 +0000249 }
250 return 0;
251}
252
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000253int WasmWriter::writeSectionContent(raw_ostream &OS,
254 WasmYAML::CustomSection &Section) {
Sam Clegge4afbc62018-11-14 18:36:24 +0000255 if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
256 if (auto Err = writeSectionContent(OS, *S))
257 return Err;
258 } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000259 if (auto Err = writeSectionContent(OS, *S))
260 return Err;
261 } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
262 if (auto Err = writeSectionContent(OS, *S))
263 return Err;
Sam Clegg03cdd122017-05-05 18:12:34 +0000264 } else {
Sam Clegg9745afa2018-04-12 20:31:12 +0000265 writeStringRef(Section.Name, OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000266 Section.Payload.writeAsBinary(OS);
267 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000268 return 0;
269}
270
271int WasmWriter::writeSectionContent(raw_ostream &OS,
272 WasmYAML::TypeSection &Section) {
273 encodeULEB128(Section.Signatures.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000274 uint32_t ExpectedIndex = 0;
Sam Clegg03cdd122017-05-05 18:12:34 +0000275 for (const WasmYAML::Signature &Sig : Section.Signatures) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000276 if (Sig.Index != ExpectedIndex) {
277 errs() << "Unexpected type index: " << Sig.Index << "\n";
278 return 1;
279 }
280 ++ExpectedIndex;
Sam Clegg03e101f2018-03-01 18:06:21 +0000281 writeUint8(OS, Sig.Form);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000282 encodeULEB128(Sig.ParamTypes.size(), OS);
283 for (auto ParamType : Sig.ParamTypes)
Sam Clegg03e101f2018-03-01 18:06:21 +0000284 writeUint8(OS, ParamType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000285 if (Sig.ReturnType == wasm::WASM_TYPE_NORESULT) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000286 encodeULEB128(0, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000287 } else {
288 encodeULEB128(1, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000289 writeUint8(OS, Sig.ReturnType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000290 }
291 }
292 return 0;
293}
294
295int WasmWriter::writeSectionContent(raw_ostream &OS,
296 WasmYAML::ImportSection &Section) {
297 encodeULEB128(Section.Imports.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000298 for (const WasmYAML::Import &Import : Section.Imports) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000299 writeStringRef(Import.Module, OS);
300 writeStringRef(Import.Field, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000301 writeUint8(OS, Import.Kind);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000302 switch (Import.Kind) {
303 case wasm::WASM_EXTERNAL_FUNCTION:
304 encodeULEB128(Import.SigIndex, OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000305 NumImportedFunctions++;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000306 break;
307 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg03e101f2018-03-01 18:06:21 +0000308 writeUint8(OS, Import.GlobalImport.Type);
Sam Clegg41db5192017-05-10 00:14:04 +0000309 writeUint8(OS, Import.GlobalImport.Mutable);
Sam Clegge53af7f2018-01-09 21:38:53 +0000310 NumImportedGlobals++;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000311 break;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000312 case wasm::WASM_EXTERNAL_EVENT:
313 writeUint32(OS, Import.EventImport.Attribute);
314 writeUint32(OS, Import.EventImport.SigIndex);
315 NumImportedGlobals++;
316 break;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000317 case wasm::WASM_EXTERNAL_MEMORY:
318 writeLimits(Import.Memory, OS);
319 break;
320 case wasm::WASM_EXTERNAL_TABLE:
Heejin Ahnf208f632018-09-05 01:27:38 +0000321 writeUint8(OS, Import.TableImport.ElemType);
Sam Clegg41db5192017-05-10 00:14:04 +0000322 writeLimits(Import.TableImport.TableLimits, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000323 break;
324 default:
Sam Clegge53af7f2018-01-09 21:38:53 +0000325 errs() << "Unknown import type: " << Import.Kind << "\n";
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000326 return 1;
327 }
328 }
329 return 0;
330}
331
332int WasmWriter::writeSectionContent(raw_ostream &OS,
333 WasmYAML::FunctionSection &Section) {
334 encodeULEB128(Section.FunctionTypes.size(), OS);
335 for (uint32_t FuncType : Section.FunctionTypes) {
336 encodeULEB128(FuncType, OS);
337 }
338 return 0;
339}
340
341int WasmWriter::writeSectionContent(raw_ostream &OS,
342 WasmYAML::ExportSection &Section) {
343 encodeULEB128(Section.Exports.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000344 for (const WasmYAML::Export &Export : Section.Exports) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000345 writeStringRef(Export.Name, OS);
Sam Clegg03e101f2018-03-01 18:06:21 +0000346 writeUint8(OS, Export.Kind);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000347 encodeULEB128(Export.Index, OS);
348 }
349 return 0;
350}
351
352int WasmWriter::writeSectionContent(raw_ostream &OS,
353 WasmYAML::StartSection &Section) {
354 encodeULEB128(Section.StartFunction, OS);
355 return 0;
356}
357
358int WasmWriter::writeSectionContent(raw_ostream &OS,
359 WasmYAML::TableSection &Section) {
360 encodeULEB128(Section.Tables.size(), OS);
361 for (auto &Table : Section.Tables) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000362 writeUint8(OS, Table.ElemType);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000363 writeLimits(Table.TableLimits, OS);
364 }
365 return 0;
366}
367
368int WasmWriter::writeSectionContent(raw_ostream &OS,
369 WasmYAML::MemorySection &Section) {
370 encodeULEB128(Section.Memories.size(), OS);
Sam Clegg03cdd122017-05-05 18:12:34 +0000371 for (const WasmYAML::Limits &Mem : Section.Memories) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000372 writeLimits(Mem, OS);
373 }
374 return 0;
375}
376
377int WasmWriter::writeSectionContent(raw_ostream &OS,
378 WasmYAML::GlobalSection &Section) {
379 encodeULEB128(Section.Globals.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000380 uint32_t ExpectedIndex = NumImportedGlobals;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000381 for (auto &Global : Section.Globals) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000382 if (Global.Index != ExpectedIndex) {
383 errs() << "Unexpected global index: " << Global.Index << "\n";
384 return 1;
385 }
386 ++ExpectedIndex;
Sam Clegg03e101f2018-03-01 18:06:21 +0000387 writeUint8(OS, Global.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000388 writeUint8(OS, Global.Mutable);
389 writeInitExpr(Global.InitExpr, OS);
390 }
391 return 0;
392}
393
394int WasmWriter::writeSectionContent(raw_ostream &OS,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000395 WasmYAML::EventSection &Section) {
396 encodeULEB128(Section.Events.size(), OS);
397 uint32_t ExpectedIndex = NumImportedEvents;
398 for (auto &Event : Section.Events) {
399 if (Event.Index != ExpectedIndex) {
400 errs() << "Unexpected event index: " << Event.Index << "\n";
401 return 1;
402 }
403 ++ExpectedIndex;
404 encodeULEB128(Event.Attribute, OS);
405 encodeULEB128(Event.SigIndex, OS);
406 }
407 return 0;
408}
409
410int WasmWriter::writeSectionContent(raw_ostream &OS,
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000411 WasmYAML::ElemSection &Section) {
412 encodeULEB128(Section.Segments.size(), OS);
413 for (auto &Segment : Section.Segments) {
414 encodeULEB128(Segment.TableIndex, OS);
415 writeInitExpr(Segment.Offset, OS);
416
417 encodeULEB128(Segment.Functions.size(), OS);
418 for (auto &Function : Segment.Functions) {
419 encodeULEB128(Function, OS);
420 }
421 }
422 return 0;
423}
424
425int WasmWriter::writeSectionContent(raw_ostream &OS,
426 WasmYAML::CodeSection &Section) {
427 encodeULEB128(Section.Functions.size(), OS);
Sam Clegge53af7f2018-01-09 21:38:53 +0000428 uint32_t ExpectedIndex = NumImportedFunctions;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000429 for (auto &Func : Section.Functions) {
430 std::string OutString;
431 raw_string_ostream StringStream(OutString);
Sam Clegge53af7f2018-01-09 21:38:53 +0000432 if (Func.Index != ExpectedIndex) {
433 errs() << "Unexpected function index: " << Func.Index << "\n";
434 return 1;
435 }
436 ++ExpectedIndex;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000437
438 encodeULEB128(Func.Locals.size(), StringStream);
439 for (auto &LocalDecl : Func.Locals) {
440 encodeULEB128(LocalDecl.Count, StringStream);
Sam Clegg03e101f2018-03-01 18:06:21 +0000441 writeUint8(StringStream, LocalDecl.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000442 }
443
444 Func.Body.writeAsBinary(StringStream);
445
446 // Write the section size followed by the content
447 StringStream.flush();
448 encodeULEB128(OutString.size(), OS);
449 OS << OutString;
450 }
451 return 0;
452}
453
454int WasmWriter::writeSectionContent(raw_ostream &OS,
455 WasmYAML::DataSection &Section) {
456 encodeULEB128(Section.Segments.size(), OS);
457 for (auto &Segment : Section.Segments) {
Sam Clegg9c07f942017-07-12 00:24:54 +0000458 encodeULEB128(Segment.MemoryIndex, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000459 writeInitExpr(Segment.Offset, OS);
460 encodeULEB128(Segment.Content.binary_size(), OS);
461 Segment.Content.writeAsBinary(OS);
462 }
463 return 0;
464}
465
Sam Clegg6f08c842018-04-24 18:11:36 +0000466int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
467 uint32_t SectionIndex) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000468 switch (Sec.Type) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000469 case wasm::WASM_SEC_CODE:
470 writeStringRef("reloc.CODE", OS);
471 break;
472 case wasm::WASM_SEC_DATA:
473 writeStringRef("reloc.DATA", OS);
474 break;
475 case wasm::WASM_SEC_CUSTOM: {
476 auto CustomSection = dyn_cast<WasmYAML::CustomSection>(&Sec);
477 if (!CustomSection->Name.startswith(".debug_")) {
478 llvm_unreachable("not yet implemented (only for debug sections)");
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000479 return 1;
Heejin Ahnf208f632018-09-05 01:27:38 +0000480 }
481
482 writeStringRef(("reloc." + CustomSection->Name).str(), OS);
483 break;
484 }
485 default:
486 llvm_unreachable("not yet implemented");
487 return 1;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000488 }
489
Sam Clegg6f08c842018-04-24 18:11:36 +0000490 encodeULEB128(SectionIndex, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000491 encodeULEB128(Sec.Relocations.size(), OS);
492
Heejin Ahnf208f632018-09-05 01:27:38 +0000493 for (auto Reloc : Sec.Relocations) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000494 writeUint8(OS, Reloc.Type);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000495 encodeULEB128(Reloc.Offset, OS);
496 encodeULEB128(Reloc.Index, OS);
497 switch (Reloc.Type) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000498 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
499 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
500 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
501 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
502 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
503 encodeULEB128(Reloc.Addend, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000504 }
505 }
506 return 0;
507}
508
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000509int WasmWriter::writeWasm(raw_ostream &OS) {
510 // Write headers
511 OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
512 writeUint32(OS, Obj.Header.Version);
513
514 // Write each section
515 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
Sam Clegge53af7f2018-01-09 21:38:53 +0000516 encodeULEB128(Sec->Type, OS);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000517 std::string OutString;
518 raw_string_ostream StringStream(OutString);
519 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) {
520 if (auto Err = writeSectionContent(StringStream, *S))
521 return Err;
522 } else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get())) {
523 if (auto Err = writeSectionContent(StringStream, *S))
524 return Err;
525 } else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get())) {
526 if (auto Err = writeSectionContent(StringStream, *S))
527 return Err;
528 } else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get())) {
529 if (auto Err = writeSectionContent(StringStream, *S))
530 return Err;
531 } else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get())) {
532 if (auto Err = writeSectionContent(StringStream, *S))
533 return Err;
534 } else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get())) {
535 if (auto Err = writeSectionContent(StringStream, *S))
536 return Err;
537 } else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get())) {
538 if (auto Err = writeSectionContent(StringStream, *S))
539 return Err;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000540 } else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get())) {
541 if (auto Err = writeSectionContent(StringStream, *S))
542 return Err;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000543 } else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get())) {
544 if (auto Err = writeSectionContent(StringStream, *S))
545 return Err;
546 } else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get())) {
547 if (auto Err = writeSectionContent(StringStream, *S))
548 return Err;
549 } else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get())) {
550 if (auto Err = writeSectionContent(StringStream, *S))
551 return Err;
552 } else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get())) {
553 if (auto Err = writeSectionContent(StringStream, *S))
554 return Err;
555 } else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get())) {
556 if (auto Err = writeSectionContent(StringStream, *S))
557 return Err;
558 } else {
559 errs() << "Unknown section type: " << Sec->Type << "\n";
560 return 1;
561 }
562 StringStream.flush();
563
564 // Write the section size followed by the content
565 encodeULEB128(OutString.size(), OS);
566 OS << OutString;
567 }
568
569 // write reloc sections for any section that have relocations
Sam Clegg6f08c842018-04-24 18:11:36 +0000570 uint32_t SectionIndex = 0;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000571 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
Sam Clegg6f08c842018-04-24 18:11:36 +0000572 if (Sec->Relocations.empty()) {
573 SectionIndex++;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000574 continue;
Sam Clegg6f08c842018-04-24 18:11:36 +0000575 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000576
Sam Clegg03e101f2018-03-01 18:06:21 +0000577 writeUint8(OS, wasm::WASM_SEC_CUSTOM);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000578 std::string OutString;
579 raw_string_ostream StringStream(OutString);
Sam Clegg6f08c842018-04-24 18:11:36 +0000580 writeRelocSection(StringStream, *Sec, SectionIndex++);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000581 StringStream.flush();
582
583 encodeULEB128(OutString.size(), OS);
584 OS << OutString;
585 }
586
587 return 0;
588}
589
590int yaml2wasm(llvm::WasmYAML::Object &Doc, raw_ostream &Out) {
591 WasmWriter Writer(Doc);
592
593 return Writer.writeWasm(Out);
594}