From b48e4de5ea13434ded315b2bc99a713db0361f63 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Mon, 7 Nov 2022 12:45:01 -0800 Subject: Implement `array.new_data` and `array.new_elem` (#5214) In order to test them, fix the binary and text parsers to accept passive data segments even if a module has no memory. In addition to parsing and emitting the new instructions, also implement their validation and interpretation. Test the interpretation directly with wasm-shell tests adapted from the upstream spec tests. Running the upstream spec tests directly would require fixing too many bugs in the legacy text parser, so it will have to wait for the new text parser to be ready. --- src/wasm/wasm-binary.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d023a56c6..e2bb0075b 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -3122,12 +3122,15 @@ void WasmBinaryBuilder::readDataSegments() { } curr->setName(Name::fromInt(i), false); curr->isPassive = flags & BinaryConsts::IsPassive; - Index memIdx = 0; - if (flags & BinaryConsts::HasIndex) { - memIdx = getU32LEB(); - } - memoryRefs[memIdx].push_back(&curr->memory); - if (!curr->isPassive) { + if (curr->isPassive) { + curr->memory = Name(); + curr->offset = nullptr; + } else { + Index memIdx = 0; + if (flags & BinaryConsts::HasIndex) { + memIdx = getU32LEB(); + } + memoryRefs[memIdx].push_back(&curr->memory); curr->offset = readExpression(); } auto size = getU32LEB(); @@ -3973,6 +3976,9 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { if (maybeVisitArrayNew(curr, opcode)) { break; } + if (maybeVisitArrayNewSeg(curr, opcode)) { + break; + } if (maybeVisitArrayInit(curr, opcode)) { break; } @@ -7058,6 +7064,20 @@ bool WasmBinaryBuilder::maybeVisitArrayNew(Expression*& out, uint32_t code) { return false; } +bool WasmBinaryBuilder::maybeVisitArrayNewSeg(Expression*& out, uint32_t code) { + if (code == BinaryConsts::ArrayNewData || + code == BinaryConsts::ArrayNewElem) { + auto op = code == BinaryConsts::ArrayNewData ? NewData : NewElem; + auto heapType = getIndexedHeapType(); + auto seg = getU32LEB(); + auto* size = popNonVoidExpression(); + auto* offset = popNonVoidExpression(); + out = Builder(wasm).makeArrayNewSeg(op, heapType, seg, offset, size); + return true; + } + return false; +} + bool WasmBinaryBuilder::maybeVisitArrayInit(Expression*& out, uint32_t code) { if (code == BinaryConsts::ArrayInitStatic) { auto heapType = getIndexedHeapType(); -- cgit v1.2.3