From 675295888609a688ee81dc9ad2024169da5aa7ed Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 23 Sep 2021 12:36:17 -0700 Subject: [Wasm GC] Implement static (rtt-free) StructNew, ArrayNew, ArrayInit (#4172) See #4149 This modifies the test added in #4163 which used static casts on dynamically-created structs and arrays. That was technically not valid (as we won't want users to "mix" the two forms). This makes that test 100% static, which both fixes the test and gives test coverage to the new instructions added here. --- src/wasm/wasm-binary.cpp | 111 ++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 40 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index e1ff43988..74895c868 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -6527,23 +6527,36 @@ bool WasmBinaryBuilder::maybeVisitRttSub(Expression*& out, uint32_t code) { } bool WasmBinaryBuilder::maybeVisitStructNew(Expression*& out, uint32_t code) { - if (code != BinaryConsts::StructNewWithRtt && - code != BinaryConsts::StructNewDefaultWithRtt) { - return false; - } - auto heapType = getIndexedHeapType(); - auto* rtt = popNonVoidExpression(); - validateHeapTypeUsingChild(rtt, heapType); - std::vector operands; - if (code == BinaryConsts::StructNewWithRtt) { - auto numOperands = heapType.getStruct().fields.size(); - operands.resize(numOperands); - for (Index i = 0; i < numOperands; i++) { - operands[numOperands - i - 1] = popNonVoidExpression(); + if (code == BinaryConsts::StructNew || + code == BinaryConsts::StructNewDefault) { + auto heapType = getIndexedHeapType(); + std::vector operands; + if (code == BinaryConsts::StructNew) { + auto numOperands = heapType.getStruct().fields.size(); + operands.resize(numOperands); + for (Index i = 0; i < numOperands; i++) { + operands[numOperands - i - 1] = popNonVoidExpression(); + } + } + out = Builder(wasm).makeStructNew(heapType, operands); + return true; + } else if (code == BinaryConsts::StructNewWithRtt || + code == BinaryConsts::StructNewDefaultWithRtt) { + auto heapType = getIndexedHeapType(); + auto* rtt = popNonVoidExpression(); + validateHeapTypeUsingChild(rtt, heapType); + std::vector operands; + if (code == BinaryConsts::StructNewWithRtt) { + auto numOperands = heapType.getStruct().fields.size(); + operands.resize(numOperands); + for (Index i = 0; i < numOperands; i++) { + operands[numOperands - i - 1] = popNonVoidExpression(); + } } + out = Builder(wasm).makeStructNew(rtt, operands); + return true; } - out = Builder(wasm).makeStructNew(rtt, operands); - return true; + return false; } bool WasmBinaryBuilder::maybeVisitStructGet(Expression*& out, uint32_t code) { @@ -6588,36 +6601,54 @@ bool WasmBinaryBuilder::maybeVisitStructSet(Expression*& out, uint32_t code) { } bool WasmBinaryBuilder::maybeVisitArrayNew(Expression*& out, uint32_t code) { - if (code != BinaryConsts::ArrayNewWithRtt && - code != BinaryConsts::ArrayNewDefaultWithRtt) { - return false; + if (code == BinaryConsts::ArrayNew || code == BinaryConsts::ArrayNewDefault) { + auto heapType = getIndexedHeapType(); + auto* size = popNonVoidExpression(); + Expression* init = nullptr; + if (code == BinaryConsts::ArrayNew) { + init = popNonVoidExpression(); + } + out = Builder(wasm).makeArrayNew(heapType, size, init); + return true; + } else if (code == BinaryConsts::ArrayNewWithRtt || + code == BinaryConsts::ArrayNewDefaultWithRtt) { + auto heapType = getIndexedHeapType(); + auto* rtt = popNonVoidExpression(); + validateHeapTypeUsingChild(rtt, heapType); + auto* size = popNonVoidExpression(); + Expression* init = nullptr; + if (code == BinaryConsts::ArrayNewWithRtt) { + init = popNonVoidExpression(); + } + out = Builder(wasm).makeArrayNew(rtt, size, init); + return true; } - auto heapType = getIndexedHeapType(); - auto* rtt = popNonVoidExpression(); - validateHeapTypeUsingChild(rtt, heapType); - auto* size = popNonVoidExpression(); - Expression* init = nullptr; - if (code == BinaryConsts::ArrayNewWithRtt) { - init = popNonVoidExpression(); - } - out = Builder(wasm).makeArrayNew(rtt, size, init); - return true; + return false; } bool WasmBinaryBuilder::maybeVisitArrayInit(Expression*& out, uint32_t code) { - if (code != BinaryConsts::ArrayInit) { - return false; - } - auto heapType = getIndexedHeapType(); - auto size = getU32LEB(); - auto* rtt = popNonVoidExpression(); - validateHeapTypeUsingChild(rtt, heapType); - std::vector values(size); - for (size_t i = 0; i < size; i++) { - values[size - i - 1] = popNonVoidExpression(); + if (code == BinaryConsts::ArrayInitStatic) { + auto heapType = getIndexedHeapType(); + auto size = getU32LEB(); + std::vector values(size); + for (size_t i = 0; i < size; i++) { + values[size - i - 1] = popNonVoidExpression(); + } + out = Builder(wasm).makeArrayInit(heapType, values); + return true; + } else if (code == BinaryConsts::ArrayInit) { + auto heapType = getIndexedHeapType(); + auto size = getU32LEB(); + auto* rtt = popNonVoidExpression(); + validateHeapTypeUsingChild(rtt, heapType); + std::vector values(size); + for (size_t i = 0; i < size; i++) { + values[size - i - 1] = popNonVoidExpression(); + } + out = Builder(wasm).makeArrayInit(rtt, values); + return true; } - out = Builder(wasm).makeArrayInit(rtt, values); - return true; + return false; } bool WasmBinaryBuilder::maybeVisitArrayGet(Expression*& out, uint32_t code) { -- cgit v1.2.3