diff options
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 21 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 14 | ||||
-rw-r--r-- | src/wasm/wasm-stack.cpp | 6 | ||||
-rw-r--r-- | src/wasm/wasm-validator.cpp | 26 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 12 |
5 files changed, 79 insertions, 0 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 97dad88bb..12aa5f214 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -3547,6 +3547,9 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { if (maybeVisitArrayLen(curr, opcode)) { break; } + if (maybeVisitArrayCopy(curr, opcode)) { + break; + } if (opcode == BinaryConsts::RefIsFunc || opcode == BinaryConsts::RefIsData || opcode == BinaryConsts::RefIsI31) { @@ -6496,6 +6499,24 @@ bool WasmBinaryBuilder::maybeVisitArrayLen(Expression*& out, uint32_t code) { return true; } +bool WasmBinaryBuilder::maybeVisitArrayCopy(Expression*& out, uint32_t code) { + if (code != BinaryConsts::ArrayCopy) { + return false; + } + auto destHeapType = getIndexedHeapType(); + auto srcHeapType = getIndexedHeapType(); + auto* length = popNonVoidExpression(); + auto* srcIndex = popNonVoidExpression(); + auto* srcRef = popNonVoidExpression(); + auto* destIndex = popNonVoidExpression(); + auto* destRef = popNonVoidExpression(); + validateHeapTypeUsingChild(destRef, destHeapType); + validateHeapTypeUsingChild(srcRef, srcHeapType); + out = + Builder(wasm).makeArrayCopy(destRef, destIndex, srcRef, srcIndex, length); + return true; +} + void WasmBinaryBuilder::visitRefAs(RefAs* curr, uint8_t code) { BYN_TRACE("zz node: RefAs\n"); switch (code) { diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 016858395..25db9a993 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2687,6 +2687,20 @@ Expression* SExpressionWasmBuilder::makeArrayLen(Element& s) { return Builder(wasm).makeArrayLen(ref); } +Expression* SExpressionWasmBuilder::makeArrayCopy(Element& s) { + auto destHeapType = parseHeapType(*s[1]); + auto srcHeapType = parseHeapType(*s[2]); + auto destRef = parseExpression(*s[3]); + validateHeapTypeUsingChild(destRef, destHeapType, s); + auto destIndex = parseExpression(*s[4]); + auto srcRef = parseExpression(*s[5]); + validateHeapTypeUsingChild(srcRef, srcHeapType, s); + auto srcIndex = parseExpression(*s[6]); + auto length = parseExpression(*s[7]); + return Builder(wasm).makeArrayCopy( + destRef, destIndex, srcRef, srcIndex, length); +} + Expression* SExpressionWasmBuilder::makeRefAs(Element& s, RefAsOp op) { return Builder(wasm).makeRefAs(op, parseExpression(s[1])); } diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index ab38d5275..12c999c0d 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -2049,6 +2049,12 @@ void BinaryInstWriter::visitArrayLen(ArrayLen* curr) { parent.writeIndexedHeapType(curr->ref->type.getHeapType()); } +void BinaryInstWriter::visitArrayCopy(ArrayCopy* curr) { + o << int8_t(BinaryConsts::GCPrefix) << U32LEB(BinaryConsts::ArrayCopy); + parent.writeIndexedHeapType(curr->destRef->type.getHeapType()); + parent.writeIndexedHeapType(curr->srcRef->type.getHeapType()); +} + void BinaryInstWriter::visitRefAs(RefAs* curr) { switch (curr->op) { case RefAsNonNull: diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index d82542668..10b60e0a7 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -385,6 +385,7 @@ public: void visitArrayGet(ArrayGet* curr); void visitArraySet(ArraySet* curr); void visitArrayLen(ArrayLen* curr); + void visitArrayCopy(ArrayCopy* curr); void visitFunction(Function* curr); // helpers @@ -2457,6 +2458,31 @@ void FunctionValidator::visitArrayLen(ArrayLen* curr) { curr->type, Type(Type::i32), curr, "array.len result must be an i32"); } +void FunctionValidator::visitArrayCopy(ArrayCopy* curr) { + shouldBeTrue(getModule()->features.hasGC(), + curr, + "array.copy requires gc to be enabled"); + shouldBeEqualOrFirstIsUnreachable(curr->srcIndex->type, + Type(Type::i32), + curr, + "array.copy src index must be an i32"); + shouldBeEqualOrFirstIsUnreachable(curr->destIndex->type, + Type(Type::i32), + curr, + "array.copy dest index must be an i32"); + if (curr->type == Type::unreachable) { + return; + } + const auto& srcElement = curr->srcRef->type.getHeapType().getArray().element; + const auto& destElement = + curr->destRef->type.getHeapType().getArray().element; + shouldBeSubType(srcElement.type, + destElement.type, + curr, + "array.copy must have the proper types"); + shouldBeTrue(destElement.mutable_, curr, "array.copy type must be mutable"); +} + void FunctionValidator::visitFunction(Function* curr) { if (curr->sig.results.isTuple()) { shouldBeTrue(getModule()->features.hasMultivalue(), diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 69464e73f..f0d130d6b 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -1018,6 +1018,18 @@ void ArrayLen::finalize() { } } +void ArrayCopy::finalize() { + if (srcRef->type == Type::unreachable || + srcIndex->type == Type::unreachable || + destRef->type == Type::unreachable || + destIndex->type == Type::unreachable || + length->type == Type::unreachable) { + type = Type::unreachable; + } else { + type = Type::none; + } +} + void RefAs::finalize() { if (value->type == Type::unreachable) { type = Type::unreachable; |