diff options
author | Alon Zakai <azakai@google.com> | 2021-09-23 12:36:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-23 12:36:17 -0700 |
commit | 675295888609a688ee81dc9ad2024169da5aa7ed (patch) | |
tree | 796dc5d72867300f97ab234e40729ae6b7db2875 /src/wasm/wasm-binary.cpp | |
parent | 6d46b436b54bdae040a64bd9dafb01a7f53a8233 (diff) | |
download | binaryen-675295888609a688ee81dc9ad2024169da5aa7ed.tar.gz binaryen-675295888609a688ee81dc9ad2024169da5aa7ed.tar.bz2 binaryen-675295888609a688ee81dc9ad2024169da5aa7ed.zip |
[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.
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 111 |
1 files changed, 71 insertions, 40 deletions
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<Expression*> 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<Expression*> 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<Expression*> 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<Expression*> 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<Expression*> 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<Expression*> 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) { |