From 823222ff566b38495327bc28b4726871b0a86b26 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 9 Dec 2020 11:17:28 -0800 Subject: [GC] Add struct.new and start to test interesting execution (#3433) With struct.new read/write support, we can start to do interesting things! This adds a test of creating a struct and seeing that references behave like references, that is, if we write to the value X refers to, and if Y refers to the same thing, when reading from Y's value we see the change as well. The test is run through all of -O1, which uncovered a minor issue in Precompute: We can't try to precompute a reference type, as we can't replace a reference with a value. Note btw that the test shows the optimizer properly running CoalesceLocals on reference types, merging two locals. --- src/wasm/wasm-s-parser.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/wasm/wasm-s-parser.cpp') diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 63b6e34bd..7efb2f2fb 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2124,10 +2124,24 @@ Expression* SExpressionWasmBuilder::makeRttSub(Element& s) { } Expression* SExpressionWasmBuilder::makeStructNew(Element& s, bool default_) { - auto ret = allocator.alloc(); - WASM_UNREACHABLE("TODO (gc): struct.new"); - ret->finalize(); - return ret; + auto heapType = parseHeapType(*s[1]); + auto* rtt = parseExpression(*s[2]); + if (rtt->type != Type::unreachable) { + if (!rtt->type.isRtt() || rtt->type.getHeapType() != heapType) { + throw ParseException("bad struct.new heap type", s.line, s.col); + } + } + auto numOperands = s.size() - 3; + if (default_ && numOperands > 0) { + throw ParseException( + "arguments provided for struct.new_with_default", s.line, s.col); + } + std::vector operands; + operands.resize(numOperands); + for (Index i = 0; i < numOperands; i++) { + operands[i] = parseExpression(*s[i + 3]); + } + return Builder(wasm).makeStructNew(rtt, operands); } Index SExpressionWasmBuilder::getStructIndex(const HeapType& type, Element& s) { -- cgit v1.2.3