29 : Obj(Obj), ErrHandler(EH) {}
66 bool HasError =
false;
71 class SubSectionWriter {
73 std::string OutString;
77 SubSectionWriter(
raw_ostream &OS) : OS(OS), StringStream(OutString) {}
162 void WasmWriter::writeSectionContent(
raw_ostream &OS,
167 SubSectionWriter SubSection(OS);
185 void WasmWriter::writeSectionContent(
raw_ostream &OS,
190 SubSectionWriter SubSection(OS);
193 if (
Section.SymbolTable.size()) {
231 if (
Section.SegmentInfos.size()) {
243 if (
Section.InitFunctions.size()) {
257 for (
const auto &
C :
Section.Comdats) {
262 writeUint8(SubSection.getStream(), Entry.Kind);
270 void WasmWriter::writeSectionContent(
raw_ostream &OS,
273 if (
Section.FunctionNames.size()) {
276 SubSectionWriter SubSection(OS);
286 if (
Section.GlobalNames.size()) {
289 SubSectionWriter SubSection(OS);
299 if (
Section.DataSegmentNames.size()) {
302 SubSectionWriter SubSection(OS);
314 void WasmWriter::writeSectionContent(
raw_ostream &OS,
325 if (
Field.second->empty())
329 for (
auto &Entry : *
Field.second) {
336 void WasmWriter::writeSectionContent(
raw_ostream &OS,
346 void WasmWriter::writeSectionContent(
raw_ostream &OS,
348 if (
auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
349 writeSectionContent(OS, *
S);
350 }
else if (
auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
351 writeSectionContent(OS, *
S);
352 }
else if (
auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
353 writeSectionContent(OS, *
S);
354 }
else if (
auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
355 writeSectionContent(OS, *
S);
356 }
else if (
auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
357 writeSectionContent(OS, *
S);
360 Section.Payload.writeAsBinary(OS);
364 void WasmWriter::writeSectionContent(
raw_ostream &OS,
369 if (Sig.
Index != ExpectedIndex) {
384 void WasmWriter::writeSectionContent(
raw_ostream &OS,
394 NumImportedFunctions++;
399 NumImportedGlobals++;
421 void WasmWriter::writeSectionContent(
raw_ostream &OS,
428 void WasmWriter::writeSectionContent(
raw_ostream &OS,
438 void WasmWriter::writeSectionContent(
raw_ostream &OS,
443 void WasmWriter::writeSectionContent(
raw_ostream &OS,
446 uint32_t ExpectedIndex = NumImportedTables;
447 for (
auto &Table :
Section.Tables) {
448 if (Table.Index != ExpectedIndex) {
458 void WasmWriter::writeSectionContent(
raw_ostream &OS,
465 void WasmWriter::writeSectionContent(
raw_ostream &OS,
474 void WasmWriter::writeSectionContent(
raw_ostream &OS,
477 uint32_t ExpectedIndex = NumImportedGlobals;
478 for (
auto &Global :
Section.Globals) {
479 if (
Global.Index != ExpectedIndex) {
486 writeInitExpr(OS,
Global.Init);
490 void WasmWriter::writeSectionContent(
raw_ostream &OS,
493 for (
auto &Segment :
Section.Segments) {
498 writeInitExpr(OS, Segment.Offset);
508 const uint8_t ElemKind = 0;
513 for (
auto &
Function : Segment.Functions)
518 void WasmWriter::writeSectionContent(
raw_ostream &OS,
521 uint32_t ExpectedIndex = NumImportedFunctions;
522 for (
auto &Func :
Section.Functions) {
523 std::string OutString;
525 if (
Func.Index != ExpectedIndex) {
532 for (
auto &LocalDecl :
Func.Locals) {
537 Func.Body.writeAsBinary(StringStream);
540 StringStream.
flush();
546 void WasmWriter::writeSectionContent(
raw_ostream &OS,
549 for (
auto &Segment :
Section.Segments) {
554 writeInitExpr(OS, Segment.Offset);
556 Segment.Content.writeAsBinary(OS);
560 void WasmWriter::writeSectionContent(
raw_ostream &OS,
575 auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
602 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
604 if (
auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
611 std::string OutString;
613 if (
auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
614 writeSectionContent(StringStream, *
S);
615 else if (
auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
616 writeSectionContent(StringStream, *
S);
617 else if (
auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
618 writeSectionContent(StringStream, *
S);
619 else if (
auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
620 writeSectionContent(StringStream, *
S);
621 else if (
auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
622 writeSectionContent(StringStream, *
S);
623 else if (
auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
624 writeSectionContent(StringStream, *
S);
625 else if (
auto S = dyn_cast<WasmYAML::TagSection>(Sec.get()))
626 writeSectionContent(StringStream, *
S);
627 else if (
auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
628 writeSectionContent(StringStream, *
S);
629 else if (
auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
630 writeSectionContent(StringStream, *
S);
631 else if (
auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
632 writeSectionContent(StringStream, *
S);
633 else if (
auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
634 writeSectionContent(StringStream, *
S);
635 else if (
auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
636 writeSectionContent(StringStream, *
S);
637 else if (
auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
638 writeSectionContent(StringStream, *
S);
639 else if (
auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
640 writeSectionContent(StringStream, *
S);
647 StringStream.
flush();
656 for (
const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
657 if (Sec->Relocations.empty()) {
663 std::string OutString;
665 writeRelocSection(StringStream, *Sec, SectionIndex++);
666 StringStream.
flush();
679 WasmWriter Writer(Doc, EH);
680 return Writer.writeWasm(Out);