haikuwebkit/JSTests/stress/big-int-bit-not-general.js

67 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

[ESNext][BigInt] Implement "~" unary operation https://bugs.webkit.org/show_bug.cgi?id=182216 Reviewed by Keith Miller. JSTests: * stress/big-int-bit-not-general.js: Added. * stress/big-int-bitwise-not-jit.js: Added. * stress/big-int-bitwise-not-wrapped-value.js: Added. * stress/bit-op-with-object-returning-int32.js: * stress/bitwise-not-fixup-rules.js: Added. * stress/value-bit-not-ai-rule.js: Added. PerformanceTests: * BigIntBench/big-int-simple-bit-not.js: Added. Source/JavaScriptCore: This patch is adding support of BigInt into op_bitnot operations. In addition, we are changing ArithBitNot to handle only Number operands, while introducing a new node named ValueBitNot to handle Untyped and BigInt. This node follows the same approach we are doing into other arithimetic operations into DFG. * dfg/DFGAbstractInterpreterInlines.h: (JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects): It is possible that fixup and prediction propagation don't convert a ValueBitNot(ConstInt32) into ArithBitNot(ConstInt32) because these analysis are conservative. In such case, we are adding constant folding rules to ValueBitNot AI. * dfg/DFGBackwardsPropagationPhase.cpp: (JSC::DFG::BackwardsPropagationPhase::propagate): ValueBitNot has same rules as ArithBitNot on backwards propagation. * dfg/DFGByteCodeParser.cpp: (JSC::DFG::ByteCodeParser::parseBlock): We can emit ArithBitNot if we know that operand of op_bitnot is a Number or any int. Otherwise we fallback to ValueBitNot and rely on fixup to convert the node to ArithBitNot when it is possible. ValueBitNot uses heap prediction on prediction propagation and we collect its type from op_bitnot's value profiler. * dfg/DFGClobberize.h: (JSC::DFG::clobberize): When we have the case with ValueBitNot(BigInt), we don't clobberize world. * dfg/DFGDoesGC.cpp: (JSC::DFG::doesGC): ValueBitNot can GC on BigIntUse because, right now, all bitNot operation allocates temporary BigInts to perform calculations and it can potentially trigger GC. * dfg/DFGFixupPhase.cpp: (JSC::DFG::FixupPhase::fixupNode): ValueBitNot is responsible do handle BigIntUse and UntypedUse. To all other uses, we fallback to ArithBitNot. * dfg/DFGNode.h: (JSC::DFG::Node::hasHeapPrediction): * dfg/DFGNodeType.h: * dfg/DFGOperations.cpp: (JSC::DFG::bitwiseBinaryOp): This template function is abstracting the new semantics of numeric values operations on bitwise operations. These operations usually folow these steps: 1. rhsNumeric = GetInt32OrBigInt(rhs) 2. lhsNumeric = GetInt32OrBigInt(lhs) 3. trhow error if TypeOf(rhsNumeric) != TypeOf(lhsNumeric) 4. return BigInt::bitwiseOp(bitOp, rhs, lhs) if TypeOf(lhsNumeric) == BigInt 5. return rhs <int32BitOp> lhs Since we have almost the same code for every bitwise op, we use such template to avoid code duplication. The template receives Int32 and BigInt operations as parameter. Error message is received as `const char*` instead of `String&` to avoid String allocation even when there is no error to throw. * dfg/DFGOperations.h: * dfg/DFGPredictionPropagationPhase.cpp: * dfg/DFGSafeToExecute.h: (JSC::DFG::safeToExecute): * dfg/DFGSpeculativeJIT.cpp: (JSC::DFG::SpeculativeJIT::compileValueBitNot): ValueBitNot generates speculative code for BigIntUse and this code is a call to `operationBitNotBigInt`. This operation is faster than `operationValueBitNot` because there is no need to check types of operands and execute properly operation. We still need to check exceptions after `operationBitNotBigInt` because it can throw OOM. (JSC::DFG::SpeculativeJIT::compileBitwiseNot): * dfg/DFGSpeculativeJIT.h: * dfg/DFGSpeculativeJIT32_64.cpp: (JSC::DFG::SpeculativeJIT::compile): * dfg/DFGSpeculativeJIT64.cpp: (JSC::DFG::SpeculativeJIT::compile): * ftl/FTLCapabilities.cpp: (JSC::FTL::canCompile): * ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileNode): (JSC::FTL::DFG::LowerDFGToB3::compileValueBitNot): (JSC::FTL::DFG::LowerDFGToB3::compileArithBitNot): * runtime/CommonSlowPaths.cpp: (JSC::SLOW_PATH_DECL): * runtime/JSBigInt.cpp: (JSC::JSBigInt::bitwiseNot): * runtime/JSBigInt.h: Canonical link: https://commits.webkit.org/209847@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242715 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2019-03-11 17:21:41 +00:00
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license below.
//
// The << Software identified by reference to the Ecma Standard* ("Software)">> is protected by copyright and is being
// made available under the "BSD License", included below. This Software may be subject to third party rights (rights
// from parties other than Ecma International), including patent rights, and no licenses under such third party rights
// are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA
// CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR
// INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS*.
//
// Copyright (C) 2012-2013 Ecma International
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
// 3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// * Ecma International Standards hereafter means Ecma International Standards as well as Ecma Technical Reports
function assert(a) {
if (!a)
throw new Error("Bad assertion");
}
assert.sameValue = function (input, expected, message) {
if (input !== expected)
throw new Error(message);
}
assert.sameValue(~0n, -1n, "~0n === -1n");
assert.sameValue(~(0n), -1n, "~(0n) === -1n");
assert.sameValue(~1n, -2n, "~1n === -2n");
assert.sameValue(~-1n, 0n, "~-1n === 0n");
assert.sameValue(~(-1n), 0n, "~(-1n) === 0n");
assert.sameValue(~~1n, 1n, "~~1n === 1n");
assert.sameValue(~0x5an, -0x5bn, "~0x5an === -0x5bn");
assert.sameValue(~-0x5an, 0x59n, "~-0x5an === 0x59n");
assert.sameValue(~0xffn, -0x100n, "~0xffn === -0x100n");
assert.sameValue(~-0xffn, 0xfen, "~-0xffn === 0xfen");
assert.sameValue(~0xffffn, -0x10000n, "~0xffffn === -0x10000n");
assert.sameValue(~-0xffffn, 0xfffen, "~-0xffffn === 0xfffen");
assert.sameValue(~0xffffffffn, -0x100000000n, "~0xffffffffn === -0x100000000n");
assert.sameValue(~-0xffffffffn, 0xfffffffen, "~-0xffffffffn === 0xfffffffen");
assert.sameValue(
~0xffffffffffffffffn, -0x10000000000000000n,
"~0xffffffffffffffffn === -0x10000000000000000n");
assert.sameValue(
~-0xffffffffffffffffn, 0xfffffffffffffffen,
"~-0xffffffffffffffffn === 0xfffffffffffffffen");
assert.sameValue(
~0x123456789abcdef0fedcba9876543210n, -0x123456789abcdef0fedcba9876543211n,
"~0x123456789abcdef0fedcba9876543210n === -0x123456789abcdef0fedcba9876543211n");