diff options
author | Alon Zakai <azakai@google.com> | 2022-07-08 11:42:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-08 11:42:55 -0700 |
commit | 75d059ad249b9dcedbaaf858747d60d299269d2c (patch) | |
tree | 43545e9c86b62081f6127c9aa3b479cb2921c60b /src | |
parent | 838e8eed2cea3d7818b19620cdca732b8b05dfca (diff) | |
download | binaryen-75d059ad249b9dcedbaaf858747d60d299269d2c.tar.gz binaryen-75d059ad249b9dcedbaaf858747d60d299269d2c.tar.bz2 binaryen-75d059ad249b9dcedbaaf858747d60d299269d2c.zip |
[Strings] string.eq (#4781)
Diffstat (limited to 'src')
-rw-r--r-- | src/gen-s-parser.inc | 20 | ||||
-rw-r--r-- | src/ir/ReFinalize.cpp | 1 | ||||
-rw-r--r-- | src/ir/cost.h | 4 | ||||
-rw-r--r-- | src/ir/effects.h | 1 | ||||
-rw-r--r-- | src/ir/possible-contents.cpp | 4 | ||||
-rw-r--r-- | src/passes/Print.cpp | 1 | ||||
-rw-r--r-- | src/wasm-binary.h | 2 | ||||
-rw-r--r-- | src/wasm-builder.h | 7 | ||||
-rw-r--r-- | src/wasm-delegations-fields.def | 7 | ||||
-rw-r--r-- | src/wasm-delegations.def | 1 | ||||
-rw-r--r-- | src/wasm-interpreter.h | 3 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 1 | ||||
-rw-r--r-- | src/wasm.h | 11 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 13 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 5 | ||||
-rw-r--r-- | src/wasm/wasm-stack.cpp | 4 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 8 | ||||
-rw-r--r-- | src/wasm2js.h | 4 |
18 files changed, 91 insertions, 6 deletions
diff --git a/src/gen-s-parser.inc b/src/gen-s-parser.inc index be1ab85e7..9c3fa90e4 100644 --- a/src/gen-s-parser.inc +++ b/src/gen-s-parser.inc @@ -3141,12 +3141,20 @@ switch (op[0]) { } } case 'e': { - switch (op[17]) { - case '1': - if (strcmp(op, "string.encode_wtf16") == 0) { return makeStringEncode(s, StringEncodeWTF16); } - goto parse_error; - case '8': - if (strcmp(op, "string.encode_wtf8") == 0) { return makeStringEncode(s, StringEncodeWTF8); } + switch (op[8]) { + case 'n': { + switch (op[17]) { + case '1': + if (strcmp(op, "string.encode_wtf16") == 0) { return makeStringEncode(s, StringEncodeWTF16); } + goto parse_error; + case '8': + if (strcmp(op, "string.encode_wtf8") == 0) { return makeStringEncode(s, StringEncodeWTF8); } + goto parse_error; + default: goto parse_error; + } + } + case 'q': + if (strcmp(op, "string.eq") == 0) { return makeStringEq(s); } goto parse_error; default: goto parse_error; } diff --git a/src/ir/ReFinalize.cpp b/src/ir/ReFinalize.cpp index 102d701b9..f509ab261 100644 --- a/src/ir/ReFinalize.cpp +++ b/src/ir/ReFinalize.cpp @@ -177,6 +177,7 @@ void ReFinalize::visitStringConst(StringConst* curr) { curr->finalize(); } void ReFinalize::visitStringMeasure(StringMeasure* curr) { curr->finalize(); } void ReFinalize::visitStringEncode(StringEncode* curr) { curr->finalize(); } void ReFinalize::visitStringConcat(StringConcat* curr) { curr->finalize(); } +void ReFinalize::visitStringEq(StringEq* curr) { curr->finalize(); } void ReFinalize::visitFunction(Function* curr) { // we may have changed the body from unreachable to none, which might be bad diff --git a/src/ir/cost.h b/src/ir/cost.h index 2dcf7c456..ea07e6109 100644 --- a/src/ir/cost.h +++ b/src/ir/cost.h @@ -684,6 +684,10 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> { CostType visitStringConcat(StringConcat* curr) { return 10 + visit(curr->left) + visit(curr->right); } + CostType visitStringEq(StringEq* curr) { + // "3" is chosen since strings might or might not be interned in the engine. + return 3 + visit(curr->left) + visit(curr->right); + } private: CostType nullCheckCost(Expression* ref) { diff --git a/src/ir/effects.h b/src/ir/effects.h index dbd1bb48f..0dd0902b1 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -746,6 +746,7 @@ private: // traps when an input is null. parent.implicitTrap = true; } + void visitStringEq(StringEq* curr) {} }; public: diff --git a/src/ir/possible-contents.cpp b/src/ir/possible-contents.cpp index 21eadc50b..7db493f00 100644 --- a/src/ir/possible-contents.cpp +++ b/src/ir/possible-contents.cpp @@ -693,6 +693,10 @@ struct InfoCollector // TODO: optimize when possible addRoot(curr); } + void visitStringEq(StringEq* curr) { + // TODO: optimize when possible + addRoot(curr); + } // TODO: Model which throws can go to which catches. For now, anything thrown // is sent to the location of that tag, and any catch of that tag can diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index dc0214453..cf4e6c9e9 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -2275,6 +2275,7 @@ struct PrintExpressionContents void visitStringConcat(StringConcat* curr) { printMedium(o, "string.concat"); } + void visitStringEq(StringEq* curr) { printMedium(o, "string.eq"); } }; // Prints an expression in s-expr format, including both the diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 6734d3bd6..f89391186 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1145,6 +1145,7 @@ enum ASTNodes { StringEncodeWTF8 = 0x86, StringEncodeWTF16 = 0x87, StringConcat = 0x88, + StringEq = 0x89, }; enum MemoryAccess { @@ -1730,6 +1731,7 @@ public: bool maybeVisitStringMeasure(Expression*& out, uint32_t code); bool maybeVisitStringEncode(Expression*& out, uint32_t code); bool maybeVisitStringConcat(Expression*& out, uint32_t code); + bool maybeVisitStringEq(Expression*& out, uint32_t code); void visitSelect(Select* curr, uint8_t code); void visitReturn(Return* curr); void visitMemorySize(MemorySize* curr); diff --git a/src/wasm-builder.h b/src/wasm-builder.h index b9bb2f7aa..507aa510b 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -1027,6 +1027,13 @@ public: ret->finalize(); return ret; } + StringEq* makeStringEq(Expression* left, Expression* right) { + auto* ret = wasm.allocator.alloc<StringEq>(); + ret->left = left; + ret->right = right; + ret->finalize(); + return ret; + } // Additional helpers diff --git a/src/wasm-delegations-fields.def b/src/wasm-delegations-fields.def index 7f450b16d..0ac154726 100644 --- a/src/wasm-delegations-fields.def +++ b/src/wasm-delegations-fields.def @@ -749,6 +749,13 @@ switch (DELEGATE_ID) { DELEGATE_END(StringConcat); break; } + case Expression::Id::StringEqId: { + DELEGATE_START(StringEq); + DELEGATE_FIELD_CHILD(StringEq, right); + DELEGATE_FIELD_CHILD(StringEq, left); + DELEGATE_END(StringEq); + break; + } } #undef DELEGATE_ID diff --git a/src/wasm-delegations.def b/src/wasm-delegations.def index 7802c63fb..c5cfa6932 100644 --- a/src/wasm-delegations.def +++ b/src/wasm-delegations.def @@ -90,5 +90,6 @@ DELEGATE(StringConst); DELEGATE(StringMeasure); DELEGATE(StringEncode); DELEGATE(StringConcat); +DELEGATE(StringEq); #undef DELEGATE diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 05de81256..02c95957d 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -1970,6 +1970,9 @@ public: Flow visitStringConcat(StringConcat* curr) { WASM_UNREACHABLE("unimplemented string.concat"); } + Flow visitStringEq(StringEq* curr) { + WASM_UNREACHABLE("unimplemented string.eq"); + } virtual void trap(const char* why) { WASM_UNREACHABLE("unimp"); } diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 9eb4a2085..777d33e30 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -308,6 +308,7 @@ private: Expression* makeStringMeasure(Element& s, StringMeasureOp op); Expression* makeStringEncode(Element& s, StringEncodeOp op); Expression* makeStringConcat(Element& s); + Expression* makeStringEq(Element& s); // Helper functions Type parseOptionalResultType(Element& s, Index& i); diff --git a/src/wasm.h b/src/wasm.h index 957d2d368..41eaf328a 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -702,6 +702,7 @@ public: StringMeasureId, StringEncodeId, StringConcatId, + StringEqId, NumExpressionIds }; Id _id; @@ -1725,6 +1726,16 @@ public: void finalize(); }; +class StringEq : public SpecificExpression<Expression::StringEqId> { +public: + StringEq(MixedArena& allocator) {} + + Expression* left; + Expression* right; + + void finalize(); +}; + // Globals struct Named { diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 5aa5948fe..ef564db6e 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -3933,6 +3933,9 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { if (maybeVisitStringConcat(curr, opcode)) { break; } + if (maybeVisitStringEq(curr, opcode)) { + break; + } if (opcode == BinaryConsts::RefIsFunc || opcode == BinaryConsts::RefIsData || opcode == BinaryConsts::RefIsI31) { @@ -7234,6 +7237,16 @@ bool WasmBinaryBuilder::maybeVisitStringConcat(Expression*& out, return true; } +bool WasmBinaryBuilder::maybeVisitStringEq(Expression*& out, uint32_t code) { + if (code != BinaryConsts::StringEq) { + return false; + } + auto* right = popNonVoidExpression(); + auto* left = popNonVoidExpression(); + out = Builder(wasm).makeStringEq(left, right); + 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 fe52864d8..ecb8c2432 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2995,6 +2995,11 @@ Expression* SExpressionWasmBuilder::makeStringConcat(Element& s) { parseExpression(s[2])); } +Expression* SExpressionWasmBuilder::makeStringEq(Element& s) { + return Builder(wasm).makeStringEq(parseExpression(s[1]), + parseExpression(s[2])); +} + // converts an s-expression string representing binary data into an output // sequence of raw bytes this appends to data, which may already contain // content. diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 6596cccc7..9a5db2928 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -2304,6 +2304,10 @@ void BinaryInstWriter::visitStringConcat(StringConcat* curr) { o << int8_t(BinaryConsts::GCPrefix) << U32LEB(BinaryConsts::StringConcat); } +void BinaryInstWriter::visitStringEq(StringEq* curr) { + o << int8_t(BinaryConsts::GCPrefix) << U32LEB(BinaryConsts::StringEq); +} + void BinaryInstWriter::emitScopeEnd(Expression* curr) { assert(!breakStack.empty()); breakStack.pop_back(); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index d3b1c2151..5e316d4f4 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -1208,6 +1208,14 @@ void StringConcat::finalize() { } } +void StringEq::finalize() { + if (left->type == Type::unreachable || right->type == Type::unreachable) { + type = Type::unreachable; + } else { + type = Type::i32; + } +} + size_t Function::getNumParams() { return getParams().size(); } size_t Function::getNumVars() { return vars.size(); } diff --git a/src/wasm2js.h b/src/wasm2js.h index c6b5cbb6c..f8179379d 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -2323,6 +2323,10 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, unimplemented(curr); WASM_UNREACHABLE("unimp"); } + Ref visitStringEq(StringEq* curr) { + unimplemented(curr); + WASM_UNREACHABLE("unimp"); + } Ref visitRefAs(RefAs* curr) { unimplemented(curr); WASM_UNREACHABLE("unimp"); |