LLVM 23.0.0git
WasmEmitter.cpp
Go to the documentation of this file.
1//===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// The Wasm component of yaml2obj.
11///
12//===----------------------------------------------------------------------===//
13//
14
15#include "llvm/Object/Wasm.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/LEB128.h"
20
21using namespace llvm;
22
23namespace {
24/// This parses a yaml stream that represents a Wasm object file.
25/// See docs/yaml2obj for the yaml scheema.
26class WasmWriter {
27public:
28 WasmWriter(WasmYAML::Object &Obj, yaml::ErrorHandler EH)
29 : Obj(Obj), ErrHandler(EH) {}
30 bool writeWasm(raw_ostream &OS);
31
32private:
33 void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
34 uint32_t SectionIndex);
35
36 void writeInitExpr(raw_ostream &OS, const WasmYAML::InitExpr &InitExpr);
37
38 void writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
39 void writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
40 void writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
41 void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
42 void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
43 void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
44 void writeSectionContent(raw_ostream &OS, WasmYAML::TagSection &Section);
45 void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
46 void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
47 void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
48 void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
49 void writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
50 void writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
51 void writeSectionContent(raw_ostream &OS, WasmYAML::DataCountSection &Section);
52
53 // Custom section types
54 void writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
55 void writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
56 void writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
57 void writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section);
58 void writeSectionContent(raw_ostream &OS,
59 WasmYAML::TargetFeaturesSection &Section);
60 WasmYAML::Object &Obj;
61 uint32_t NumImportedFunctions = 0;
62 uint32_t NumImportedGlobals = 0;
63 uint32_t NumImportedTables = 0;
64 uint32_t NumImportedTags = 0;
65
66 bool HasError = false;
67 yaml::ErrorHandler ErrHandler;
68 void reportError(const Twine &Msg);
69};
70
71class SubSectionWriter {
72 raw_ostream &OS;
73 std::string OutString;
74 raw_string_ostream StringStream;
75
76public:
77 SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
78
79 void done() {
80 encodeULEB128(OutString.size(), OS);
81 OS << OutString;
82 OutString.clear();
83 }
84
85 raw_ostream &getStream() { return StringStream; }
86};
87
88} // end anonymous namespace
89
91 char Data[sizeof(Value)];
93 OS.write(Data, sizeof(Data));
94 return 0;
95}
96
98 char Data[sizeof(Value)];
100 OS.write(Data, sizeof(Data));
101 return 0;
102}
103
105 char Data[sizeof(Value)];
106 memcpy(Data, &Value, sizeof(Data));
107 OS.write(Data, sizeof(Data));
108 return 0;
109}
110
111static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
112 encodeULEB128(Str.size(), OS);
113 OS << Str;
114 return 0;
115}
116
117static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
118 writeUint8(OS, Lim.Flags);
119 encodeULEB128(Lim.Minimum, OS);
121 encodeULEB128(Lim.Maximum, OS);
122 return 0;
123}
124
125void WasmWriter::reportError(const Twine &Msg) {
126 ErrHandler(Msg);
127 HasError = true;
128}
129
130void WasmWriter::writeInitExpr(raw_ostream &OS,
131 const WasmYAML::InitExpr &InitExpr) {
132 if (InitExpr.Extended) {
133 InitExpr.Body.writeAsBinary(OS);
134 } else {
135 writeUint8(OS, InitExpr.Inst.Opcode);
136 switch (InitExpr.Inst.Opcode) {
138 encodeSLEB128(InitExpr.Inst.Value.Int32, OS);
139 break;
141 encodeSLEB128(InitExpr.Inst.Value.Int64, OS);
142 break;
144 writeUint32(OS, InitExpr.Inst.Value.Float32);
145 break;
147 writeUint64(OS, InitExpr.Inst.Value.Float64);
148 break;
150 encodeULEB128(InitExpr.Inst.Value.Global, OS);
151 break;
152 default:
153 reportError("unknown opcode in init_expr: " +
154 Twine(InitExpr.Inst.Opcode));
155 return;
156 }
158 }
159}
160
161void WasmWriter::writeSectionContent(raw_ostream &OS,
162 WasmYAML::DylinkSection &Section) {
163 writeStringRef(Section.Name, OS);
164
166 SubSectionWriter SubSection(OS);
167 raw_ostream &SubOS = SubSection.getStream();
168 encodeULEB128(Section.MemorySize, SubOS);
169 encodeULEB128(Section.MemoryAlignment, SubOS);
170 encodeULEB128(Section.TableSize, SubOS);
171 encodeULEB128(Section.TableAlignment, SubOS);
172 SubSection.done();
173
174 if (Section.Needed.size()) {
176 raw_ostream &SubOS = SubSection.getStream();
177 encodeULEB128(Section.Needed.size(), SubOS);
178 for (StringRef Needed : Section.Needed)
179 writeStringRef(Needed, SubOS);
180 SubSection.done();
181 }
182 if (Section.ExportInfo.size()) {
184 raw_ostream &SubOS = SubSection.getStream();
185 encodeULEB128(Section.ExportInfo.size(), SubOS);
186 for (const WasmYAML::DylinkExportInfo &Info : Section.ExportInfo) {
187 writeStringRef(Info.Name, SubOS);
188 encodeULEB128(Info.Flags, SubOS);
189 }
190 SubSection.done();
191 }
192 if (Section.ImportInfo.size()) {
194 raw_ostream &SubOS = SubSection.getStream();
195 encodeULEB128(Section.ImportInfo.size(), SubOS);
196 for (const WasmYAML::DylinkImportInfo &Info : Section.ImportInfo) {
197 writeStringRef(Info.Module, SubOS);
198 writeStringRef(Info.Field, SubOS);
199 encodeULEB128(Info.Flags, SubOS);
200 }
201 SubSection.done();
202 }
203 if (Section.RuntimePath.size()) {
205 raw_ostream &SubOS = SubSection.getStream();
206 encodeULEB128(Section.RuntimePath.size(), SubOS);
207 for (StringRef Path : Section.RuntimePath)
208 writeStringRef(Path, SubOS);
209 SubSection.done();
210 }
211}
212
213void WasmWriter::writeSectionContent(raw_ostream &OS,
214 WasmYAML::LinkingSection &Section) {
215 writeStringRef(Section.Name, OS);
216 encodeULEB128(Section.Version, OS);
217
218 SubSectionWriter SubSection(OS);
219
220 // SYMBOL_TABLE subsection
221 if (Section.SymbolTable.size()) {
223 encodeULEB128(Section.SymbolTable.size(), SubSection.getStream());
224 for (auto Sym : llvm::enumerate(Section.SymbolTable)) {
225 const WasmYAML::SymbolInfo &Info = Sym.value();
226 assert(Info.Index == Sym.index());
227 writeUint8(SubSection.getStream(), Info.Kind);
228 encodeULEB128(Info.Flags, SubSection.getStream());
229 switch (Info.Kind) {
234 encodeULEB128(Info.ElementIndex, SubSection.getStream());
235 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
237 writeStringRef(Info.Name, SubSection.getStream());
238 break;
240 writeStringRef(Info.Name, SubSection.getStream());
241 if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
242 encodeULEB128(Info.DataRef.Segment, SubSection.getStream());
243 encodeULEB128(Info.DataRef.Offset, SubSection.getStream());
244 encodeULEB128(Info.DataRef.Size, SubSection.getStream());
245 }
246 break;
248 encodeULEB128(Info.ElementIndex, SubSection.getStream());
249 break;
250 default:
251 llvm_unreachable("unexpected kind");
252 }
253 }
254
255 SubSection.done();
256 }
257
258 // SEGMENT_NAMES subsection
259 if (Section.SegmentInfos.size()) {
261 encodeULEB128(Section.SegmentInfos.size(), SubSection.getStream());
262 for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
263 writeStringRef(SegmentInfo.Name, SubSection.getStream());
264 encodeULEB128(SegmentInfo.Alignment, SubSection.getStream());
265 encodeULEB128(SegmentInfo.Flags, SubSection.getStream());
266 }
267 SubSection.done();
268 }
269
270 // INIT_FUNCS subsection
271 if (Section.InitFunctions.size()) {
273 encodeULEB128(Section.InitFunctions.size(), SubSection.getStream());
274 for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
275 encodeULEB128(Func.Priority, SubSection.getStream());
276 encodeULEB128(Func.Symbol, SubSection.getStream());
277 }
278 SubSection.done();
279 }
280
281 // COMDAT_INFO subsection
282 if (Section.Comdats.size()) {
284 encodeULEB128(Section.Comdats.size(), SubSection.getStream());
285 for (const auto &C : Section.Comdats) {
286 writeStringRef(C.Name, SubSection.getStream());
287 encodeULEB128(0, SubSection.getStream()); // flags for future use
288 encodeULEB128(C.Entries.size(), SubSection.getStream());
289 for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
290 writeUint8(SubSection.getStream(), Entry.Kind);
291 encodeULEB128(Entry.Index, SubSection.getStream());
292 }
293 }
294 SubSection.done();
295 }
296}
297
298void WasmWriter::writeSectionContent(raw_ostream &OS,
299 WasmYAML::NameSection &Section) {
300 writeStringRef(Section.Name, OS);
301 if (Section.FunctionNames.size()) {
303
304 SubSectionWriter SubSection(OS);
305
306 encodeULEB128(Section.FunctionNames.size(), SubSection.getStream());
307 for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
308 encodeULEB128(NameEntry.Index, SubSection.getStream());
309 writeStringRef(NameEntry.Name, SubSection.getStream());
310 }
311
312 SubSection.done();
313 }
314 if (Section.GlobalNames.size()) {
316
317 SubSectionWriter SubSection(OS);
318
319 encodeULEB128(Section.GlobalNames.size(), SubSection.getStream());
320 for (const WasmYAML::NameEntry &NameEntry : Section.GlobalNames) {
321 encodeULEB128(NameEntry.Index, SubSection.getStream());
322 writeStringRef(NameEntry.Name, SubSection.getStream());
323 }
324
325 SubSection.done();
326 }
327 if (Section.DataSegmentNames.size()) {
329
330 SubSectionWriter SubSection(OS);
331
332 encodeULEB128(Section.DataSegmentNames.size(), SubSection.getStream());
333 for (const WasmYAML::NameEntry &NameEntry : Section.DataSegmentNames) {
334 encodeULEB128(NameEntry.Index, SubSection.getStream());
335 writeStringRef(NameEntry.Name, SubSection.getStream());
336 }
337
338 SubSection.done();
339 }
340}
341
342void WasmWriter::writeSectionContent(raw_ostream &OS,
343 WasmYAML::ProducersSection &Section) {
344 writeStringRef(Section.Name, OS);
345 int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
346 int(!Section.SDKs.empty());
347 if (Fields == 0)
348 return;
349 encodeULEB128(Fields, OS);
350 for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
351 std::make_pair(StringRef("processed-by"), &Section.Tools),
352 std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
353 if (Field.second->empty())
354 continue;
355 writeStringRef(Field.first, OS);
356 encodeULEB128(Field.second->size(), OS);
357 for (auto &Entry : *Field.second) {
358 writeStringRef(Entry.Name, OS);
359 writeStringRef(Entry.Version, OS);
360 }
361 }
362}
363
364void WasmWriter::writeSectionContent(raw_ostream &OS,
365 WasmYAML::TargetFeaturesSection &Section) {
366 writeStringRef(Section.Name, OS);
367 encodeULEB128(Section.Features.size(), OS);
368 for (auto &E : Section.Features) {
369 writeUint8(OS, E.Prefix);
370 writeStringRef(E.Name, OS);
371 }
372}
373
374void WasmWriter::writeSectionContent(raw_ostream &OS,
375 WasmYAML::CustomSection &Section) {
376 if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
377 writeSectionContent(OS, *S);
378 } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
379 writeSectionContent(OS, *S);
380 } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
381 writeSectionContent(OS, *S);
382 } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
383 writeSectionContent(OS, *S);
384 } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
385 writeSectionContent(OS, *S);
386 } else {
387 writeStringRef(Section.Name, OS);
388 Section.Payload.writeAsBinary(OS);
389 }
390}
391
392void WasmWriter::writeSectionContent(raw_ostream &OS,
393 WasmYAML::TypeSection &Section) {
394 encodeULEB128(Section.Signatures.size(), OS);
395 uint32_t ExpectedIndex = 0;
396 for (const WasmYAML::Signature &Sig : Section.Signatures) {
397 if (Sig.Index != ExpectedIndex) {
398 reportError("unexpected type index: " + Twine(Sig.Index));
399 return;
400 }
401 ++ExpectedIndex;
402 writeUint8(OS, Sig.Form);
403 encodeULEB128(Sig.ParamTypes.size(), OS);
404 for (auto ParamType : Sig.ParamTypes)
405 writeUint8(OS, ParamType);
406 encodeULEB128(Sig.ReturnTypes.size(), OS);
407 for (auto ReturnType : Sig.ReturnTypes)
408 writeUint8(OS, ReturnType);
409 }
410}
411
412void WasmWriter::writeSectionContent(raw_ostream &OS,
413 WasmYAML::ImportSection &Section) {
414 encodeULEB128(Section.Imports.size(), OS);
415 for (const WasmYAML::Import &Import : Section.Imports) {
416 writeStringRef(Import.Module, OS);
417 writeStringRef(Import.Field, OS);
418 writeUint8(OS, Import.Kind);
419 switch (Import.Kind) {
421 encodeULEB128(Import.SigIndex, OS);
422 NumImportedFunctions++;
423 break;
425 writeUint8(OS, Import.GlobalImport.Type);
426 writeUint8(OS, Import.GlobalImport.Mutable);
427 NumImportedGlobals++;
428 break;
430 writeUint8(OS, 0); // Reserved 'attribute' field
431 encodeULEB128(Import.SigIndex, OS);
432 NumImportedTags++;
433 break;
435 writeLimits(Import.Memory, OS);
436 break;
438 writeUint8(OS, Import.TableImport.ElemType);
439 writeLimits(Import.TableImport.TableLimits, OS);
440 NumImportedTables++;
441 break;
442 default:
443 reportError("unknown import type: " +Twine(Import.Kind));
444 return;
445 }
446 }
447}
448
449void WasmWriter::writeSectionContent(raw_ostream &OS,
450 WasmYAML::FunctionSection &Section) {
451 encodeULEB128(Section.FunctionTypes.size(), OS);
452 for (uint32_t FuncType : Section.FunctionTypes)
453 encodeULEB128(FuncType, OS);
454}
455
456void WasmWriter::writeSectionContent(raw_ostream &OS,
457 WasmYAML::ExportSection &Section) {
458 encodeULEB128(Section.Exports.size(), OS);
459 for (const WasmYAML::Export &Export : Section.Exports) {
460 writeStringRef(Export.Name, OS);
461 writeUint8(OS, Export.Kind);
462 encodeULEB128(Export.Index, OS);
463 }
464}
465
466void WasmWriter::writeSectionContent(raw_ostream &OS,
467 WasmYAML::StartSection &Section) {
468 encodeULEB128(Section.StartFunction, OS);
469}
470
471void WasmWriter::writeSectionContent(raw_ostream &OS,
472 WasmYAML::TableSection &Section) {
473 encodeULEB128(Section.Tables.size(), OS);
474 uint32_t ExpectedIndex = NumImportedTables;
475 for (auto &Table : Section.Tables) {
476 if (Table.Index != ExpectedIndex) {
477 reportError("unexpected table index: " + Twine(Table.Index));
478 return;
479 }
480 ++ExpectedIndex;
481 writeUint8(OS, Table.ElemType);
482 writeLimits(Table.TableLimits, OS);
483 }
484}
485
486void WasmWriter::writeSectionContent(raw_ostream &OS,
487 WasmYAML::MemorySection &Section) {
488 encodeULEB128(Section.Memories.size(), OS);
489 for (const WasmYAML::Limits &Mem : Section.Memories)
490 writeLimits(Mem, OS);
491}
492
493void WasmWriter::writeSectionContent(raw_ostream &OS,
494 WasmYAML::TagSection &Section) {
495 encodeULEB128(Section.TagTypes.size(), OS);
496 for (uint32_t TagType : Section.TagTypes) {
497 writeUint8(OS, 0); // Reserved 'attribute' field
498 encodeULEB128(TagType, OS);
499 }
500}
501
502void WasmWriter::writeSectionContent(raw_ostream &OS,
503 WasmYAML::GlobalSection &Section) {
504 encodeULEB128(Section.Globals.size(), OS);
505 uint32_t ExpectedIndex = NumImportedGlobals;
506 for (auto &Global : Section.Globals) {
507 if (Global.Index != ExpectedIndex) {
508 reportError("unexpected global index: " + Twine(Global.Index));
509 return;
510 }
511 ++ExpectedIndex;
512 writeUint8(OS, Global.Type);
513 writeUint8(OS, Global.Mutable);
514 writeInitExpr(OS, Global.Init);
515 }
516}
517
518void WasmWriter::writeSectionContent(raw_ostream &OS,
519 WasmYAML::ElemSection &Section) {
520 encodeULEB128(Section.Segments.size(), OS);
521 for (auto &Segment : Section.Segments) {
522 encodeULEB128(Segment.Flags, OS);
524 encodeULEB128(Segment.TableNumber, OS);
525
526 writeInitExpr(OS, Segment.Offset);
527
528 if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC) {
529 // We only support active function table initializers, for which the elem
530 // kind is specified to be written as 0x00 and interpreted to mean
531 // "funcref".
532 if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) {
533 reportError("unexpected elemkind: " + Twine(Segment.ElemKind));
534 return;
535 }
536 const uint8_t ElemKind = 0;
537 writeUint8(OS, ElemKind);
538 }
539
540 encodeULEB128(Segment.Functions.size(), OS);
541 for (auto &Function : Segment.Functions)
542 encodeULEB128(Function, OS);
543 }
544}
545
546void WasmWriter::writeSectionContent(raw_ostream &OS,
547 WasmYAML::CodeSection &Section) {
548 encodeULEB128(Section.Functions.size(), OS);
549 uint32_t ExpectedIndex = NumImportedFunctions;
550 for (auto &Func : Section.Functions) {
551 std::string OutString;
552 raw_string_ostream StringStream(OutString);
553 if (Func.Index != ExpectedIndex) {
554 reportError("unexpected function index: " + Twine(Func.Index));
555 return;
556 }
557 ++ExpectedIndex;
558
559 encodeULEB128(Func.Locals.size(), StringStream);
560 for (auto &LocalDecl : Func.Locals) {
561 encodeULEB128(LocalDecl.Count, StringStream);
562 writeUint8(StringStream, LocalDecl.Type);
563 }
564
565 Func.Body.writeAsBinary(StringStream);
566
567 // Write the section size followed by the content
568 encodeULEB128(OutString.size(), OS);
569 OS << OutString;
570 }
571}
572
573void WasmWriter::writeSectionContent(raw_ostream &OS,
574 WasmYAML::DataSection &Section) {
575 encodeULEB128(Section.Segments.size(), OS);
576 for (auto &Segment : Section.Segments) {
577 encodeULEB128(Segment.InitFlags, OS);
578 if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
579 encodeULEB128(Segment.MemoryIndex, OS);
580 if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0)
581 writeInitExpr(OS, Segment.Offset);
582 encodeULEB128(Segment.Content.binary_size(), OS);
583 Segment.Content.writeAsBinary(OS);
584 }
585}
586
587void WasmWriter::writeSectionContent(raw_ostream &OS,
588 WasmYAML::DataCountSection &Section) {
589 encodeULEB128(Section.Count, OS);
590}
591
592void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
593 uint32_t SectionIndex) {
594 switch (Sec.Type) {
596 writeStringRef("reloc.CODE", OS);
597 break;
599 writeStringRef("reloc.DATA", OS);
600 break;
602 auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
603 writeStringRef(("reloc." + CustomSection->Name).str(), OS);
604 break;
605 }
606 default:
607 llvm_unreachable("not yet implemented");
608 }
609
610 encodeULEB128(SectionIndex, OS);
611 encodeULEB128(Sec.Relocations.size(), OS);
612
613 for (auto Reloc : Sec.Relocations) {
614 writeUint8(OS, Reloc.Type);
615 encodeULEB128(Reloc.Offset, OS);
616 encodeULEB128(Reloc.Index, OS);
617 if (wasm::relocTypeHasAddend(Reloc.Type))
618 encodeSLEB128(Reloc.Addend, OS);
619 }
620}
621
622bool WasmWriter::writeWasm(raw_ostream &OS) {
623 // Write headers
625 writeUint32(OS, Obj.Header.Version);
626
627 // Write each section
628 llvm::object::WasmSectionOrderChecker Checker;
629 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
630 StringRef SecName = "";
631 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
632 SecName = S->Name;
633 if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
634 reportError("out of order section type: " +
635 wasm::sectionTypeToString(Sec->Type));
636 return false;
637 }
638 encodeULEB128(Sec->Type, OS);
639 std::string OutString;
640 raw_string_ostream StringStream(OutString);
641 if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
642 writeSectionContent(StringStream, *S);
643 else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
644 writeSectionContent(StringStream, *S);
645 else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
646 writeSectionContent(StringStream, *S);
647 else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
648 writeSectionContent(StringStream, *S);
649 else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
650 writeSectionContent(StringStream, *S);
651 else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
652 writeSectionContent(StringStream, *S);
653 else if (auto S = dyn_cast<WasmYAML::TagSection>(Sec.get()))
654 writeSectionContent(StringStream, *S);
655 else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
656 writeSectionContent(StringStream, *S);
657 else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
658 writeSectionContent(StringStream, *S);
659 else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
660 writeSectionContent(StringStream, *S);
661 else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
662 writeSectionContent(StringStream, *S);
663 else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
664 writeSectionContent(StringStream, *S);
665 else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
666 writeSectionContent(StringStream, *S);
667 else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
668 writeSectionContent(StringStream, *S);
669 else
670 reportError("unknown section type: " + Twine(Sec->Type));
671
672 if (HasError)
673 return false;
674
675 unsigned HeaderSecSizeEncodingLen =
676 Sec->HeaderSecSizeEncodingLen.value_or(5);
677 unsigned RequiredLen = getULEB128Size(OutString.size());
678 // Wasm spec does not allow LEBs larger than 5 bytes
679 assert(RequiredLen <= 5);
680 if (HeaderSecSizeEncodingLen < RequiredLen) {
681 reportError("section header length can't be encoded in a LEB of size " +
682 Twine(HeaderSecSizeEncodingLen));
683 return false;
684 }
685 // Write the section size followed by the content
686 encodeULEB128(OutString.size(), OS, HeaderSecSizeEncodingLen);
687 OS << OutString;
688 }
689
690 // write reloc sections for any section that have relocations
691 uint32_t SectionIndex = 0;
692 for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
693 if (Sec->Relocations.empty()) {
694 SectionIndex++;
695 continue;
696 }
697
699 std::string OutString;
700 raw_string_ostream StringStream(OutString);
701 writeRelocSection(StringStream, *Sec, SectionIndex++);
702
703 encodeULEB128(OutString.size(), OS);
704 OS << OutString;
705 }
706
707 return true;
708}
709
710namespace llvm {
711namespace yaml {
712
714 WasmWriter Writer(Doc, EH);
715 return Writer.writeWasm(Out);
716}
717
718} // namespace yaml
719} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error reportError(StringRef Message)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
OptimizedStructLayoutField Field
static int writeUint32(raw_ostream &OS, uint32_t Value)
static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS)
static int writeStringRef(const StringRef &Str, raw_ostream &OS)
static int writeUint8(raw_ostream &OS, uint8_t Value)
static int writeUint64(raw_ostream &OS, uint64_t Value)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName="")
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write(unsigned char C)
LLVM_ABI void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
Definition YAML.cpp:39
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
void write64le(void *P, uint64_t V)
Definition Endian.h:478
void write32le(void *P, uint32_t V)
Definition Endian.h:475
const unsigned WASM_SYMBOL_UNDEFINED
Definition Wasm.h:247
@ WASM_INIT_FUNCS
Definition Wasm.h:196
@ WASM_COMDAT_INFO
Definition Wasm.h:197
@ WASM_SEGMENT_INFO
Definition Wasm.h:195
@ WASM_SYMBOL_TABLE
Definition Wasm.h:198
const char WasmMagic[]
Definition Wasm.h:27
@ WASM_SEC_CODE
Definition Wasm.h:47
@ WASM_SEC_CUSTOM
Definition Wasm.h:37
@ WASM_SEC_DATA
Definition Wasm.h:48
@ WASM_LIMITS_FLAG_HAS_MAX
Definition Wasm.h:159
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition Wasm.h:166
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition Wasm.h:167
@ WASM_SYMBOL_TYPE_GLOBAL
Definition Wasm.h:222
@ WASM_SYMBOL_TYPE_DATA
Definition Wasm.h:221
@ WASM_SYMBOL_TYPE_TAG
Definition Wasm.h:224
@ WASM_SYMBOL_TYPE_TABLE
Definition Wasm.h:225
@ WASM_SYMBOL_TYPE_SECTION
Definition Wasm.h:223
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:220
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition Wasm.h:173
LLVM_ABI bool relocTypeHasAddend(uint32_t type)
Definition Wasm.cpp:66
@ WASM_OPCODE_F64_CONST
Definition Wasm.h:107
@ WASM_OPCODE_END
Definition Wasm.h:95
@ WASM_OPCODE_F32_CONST
Definition Wasm.h:106
@ WASM_OPCODE_GLOBAL_GET
Definition Wasm.h:100
@ WASM_OPCODE_I64_CONST
Definition Wasm.h:105
@ WASM_OPCODE_I32_CONST
Definition Wasm.h:104
@ WASM_EXTERNAL_TABLE
Definition Wasm.h:87
@ WASM_EXTERNAL_FUNCTION
Definition Wasm.h:86
@ WASM_EXTERNAL_TAG
Definition Wasm.h:90
@ WASM_EXTERNAL_MEMORY
Definition Wasm.h:88
@ WASM_EXTERNAL_GLOBAL
Definition Wasm.h:89
@ WASM_DYLINK_RUNTIME_PATH
Definition Wasm.h:207
@ WASM_DYLINK_NEEDED
Definition Wasm.h:204
@ WASM_DYLINK_MEM_INFO
Definition Wasm.h:203
@ WASM_DYLINK_EXPORT_INFO
Definition Wasm.h:205
@ WASM_DYLINK_IMPORT_INFO
Definition Wasm.h:206
@ WASM_NAMES_DATA_SEGMENT
Definition Wasm.h:190
@ WASM_NAMES_GLOBAL
Definition Wasm.h:189
@ WASM_NAMES_FUNCTION
Definition Wasm.h:187
LLVM_ABI llvm::StringRef sectionTypeToString(uint32_t type)
Definition Wasm.cpp:41
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition Wasm.h:249
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
Definition Wasm.h:176
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
Definition yaml2obj.h:68
LLVM_ABI bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Export
Export information to summary.
Definition IPO.h:57
@ Import
Import information from summary.
Definition IPO.h:56
@ Global
Append to llvm.global_dtors.
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition LEB128.cpp:19
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
wasm::WasmInitExprMVP Inst
Definition WasmYAML.h:70
yaml::BinaryRef Body
Definition WasmYAML.h:71
yaml::Hex32 Minimum
Definition WasmYAML.h:49
yaml::Hex32 Maximum
Definition WasmYAML.h:50
std::vector< std::unique_ptr< Section > > Sections
Definition WasmYAML.h:421
std::vector< Relocation > Relocations
Definition WasmYAML.h:192
std::vector< ValueType > ReturnTypes
Definition WasmYAML.h:158
std::vector< ValueType > ParamTypes
Definition WasmYAML.h:157
union llvm::wasm::WasmInitExprMVP::@021046217255127373215144224227277301132130072341 Value
Common declarations for yaml2obj.