diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/Print.cpp | 4 | ||||
-rw-r--r-- | src/wasm.h | 2 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 40 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 7 |
4 files changed, 52 insertions, 1 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 5c2acf3f0..0577c7c02 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -2771,6 +2771,10 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { doIndent(o, indent); o << '('; printMajor(o, "data "); + if (segment.name.is()) { + printName(segment.name, o); + o << ' '; + } if (segment.isPassive) { printMedium(o, "passive"); } else { diff --git a/src/wasm.h b/src/wasm.h index 0ea3ffeb7..c23819d51 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1631,6 +1631,8 @@ public: (uint64_t(4) * 1024 * 1024 * 1024) / kPageSize; struct Segment { + // For use in name section only + Name name; bool isPassive = false; Expression* offset = nullptr; std::vector<char> data; // TODO: optimize diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 314364f38..6d4689056 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -690,7 +690,31 @@ void WasmBinaryWriter::writeNames() { } } - // TODO: label, type, element and data names + // memory names + if (wasm->memory.exists) { + Index count = 0; + for (auto& seg : wasm->memory.segments) { + if (seg.name.is()) { + count++; + } + } + + if (count) { + auto substart = + startSubsection(BinaryConsts::UserSections::Subsection::NameData); + o << U32LEB(count); + for (Index i = 0; i < wasm->memory.segments.size(); i++) { + auto& seg = wasm->memory.segments[i]; + if (seg.name.is()) { + o << U32LEB(i); + writeEscapedName(seg.name.str); + } + } + finishSubsection(substart); + } + } + + // TODO: label, type, and element names // see: https://github.com/WebAssembly/extended-name-section finishSection(start); @@ -2566,6 +2590,20 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { << std::to_string(index) << std::endl; } } + } else if (nameType == BinaryConsts::UserSections::Subsection::NameData) { + auto num = getU32LEB(); + for (size_t i = 0; i < num; i++) { + auto index = getU32LEB(); + auto rawName = getInlineString(); + if (index < wasm.memory.segments.size()) { + wasm.memory.segments[i].name = rawName; + } else { + std::cerr << "warning: memory index out of bounds in name section, " + "memory subsection: " + << std::string(rawName.str) << " at index " + << std::to_string(index) << std::endl; + } + } } else if (nameType == BinaryConsts::UserSections::Subsection::NameGlobal) { auto num = getU32LEB(); std::set<Name> usedNames; diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 7efb2f2fb..3bb54f0c8 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2380,6 +2380,10 @@ void SExpressionWasmBuilder::parseData(Element& s) { bool isPassive = false; Expression* offset = nullptr; Index i = 1; + Name name; + if (s[i]->dollared()) { + name = s[i++]->str(); + } if (s[i]->isStr()) { // data is passive or named if (s[i]->str() == PASSIVE) { @@ -2394,6 +2398,9 @@ void SExpressionWasmBuilder::parseData(Element& s) { throw ParseException("Unexpected data items", s.line, s.col); } parseInnerData(s, s.size() - 1, offset, isPassive); + if (name.is()) { + wasm.memory.segments.back().name = name; + } } void SExpressionWasmBuilder::parseInnerData(Element& s, |