Commit 3d5f2e08 authored by Jaroslav Sevcik's avatar Jaroslav Sevcik Committed by Commit Bot

[builtins] Port Set.prototype.(add|delete) to CSA.

Bug: v8:5717
Change-Id: Iac82b4960cc3ed89820c49b091d6860136839300
Reviewed-on: https://chromium-review.googlesource.com/583147
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46846}
parent fab6fe09
......@@ -546,7 +546,6 @@ action("js2c") {
"src/js/array.js",
"src/js/string.js",
"src/js/typedarray.js",
"src/js/collection.js",
"src/js/weak-collection.js",
"src/js/messages.js",
"src/js/templates.js",
......
......@@ -3074,6 +3074,15 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Handle<JSFunction> set_has =
SimpleInstallFunction(prototype, "has", Builtins::kSetHas, 1, true);
native_context()->set_set_has(*set_has);
Handle<JSFunction> set_add =
SimpleInstallFunction(prototype, "add", Builtins::kSetAdd, 1, true);
native_context()->set_set_add(*set_add);
Handle<JSFunction> set_delete = SimpleInstallFunction(
prototype, "delete", Builtins::kSetDelete, 1, true);
native_context()->set_set_delete(*set_delete);
SimpleInstallFunction(prototype, "clear", Builtins::kSetClear, 0, true);
SimpleInstallFunction(prototype, "entries", Builtins::kSetPrototypeEntries,
0, true);
......
......@@ -106,15 +106,22 @@ class CollectionsBuiltinsAssembler : public CodeStubAssembler {
Node* key, Variable* result,
Label* entry_found,
Label* not_found);
void TryLookupOrderedHashMapIndex(Node* const table, Node* const key,
Node* const context, Variable* result,
Label* if_entry_found, Label* if_not_found);
template <typename CollectionType>
void TryLookupOrderedHashTableIndex(Node* const table, Node* const key,
Node* const context, Variable* result,
Label* if_entry_found,
Label* if_not_found);
Node* NormalizeNumberKey(Node* key);
void StoreOrderedHashMapNewEntry(Node* const table, Node* const key,
Node* const value, Node* const hash,
Node* const number_of_buckets,
Node* const occupancy);
void StoreOrderedHashSetNewEntry(Node* const table, Node* const key,
Node* const hash,
Node* const number_of_buckets,
Node* const occupancy);
};
template <typename CollectionType>
......@@ -869,9 +876,9 @@ TF_BUILTIN(MapSet, CollectionsBuiltinsAssembler) {
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashMapIndex(table, key, context,
&entry_start_position_or_hash, &entry_found,
&not_found);
TryLookupOrderedHashTableIndex<OrderedHashMap>(table, key, context,
&entry_start_position_or_hash,
&entry_found, &not_found);
BIND(&entry_found);
// If we found the entry, we just store the value there.
......@@ -980,9 +987,9 @@ TF_BUILTIN(MapDelete, CollectionsBuiltinsAssembler) {
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashMapIndex(table, key, context,
&entry_start_position_or_hash, &entry_found,
&not_found);
TryLookupOrderedHashTableIndex<OrderedHashMap>(table, key, context,
&entry_start_position_or_hash,
&entry_found, &not_found);
BIND(&not_found);
Return(FalseConstant());
......@@ -1024,6 +1031,165 @@ TF_BUILTIN(MapDelete, CollectionsBuiltinsAssembler) {
Return(TrueConstant());
}
TF_BUILTIN(SetAdd, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* key = Parameter(Descriptor::kKey);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_SET_TYPE, "Set.prototype.add");
key = NormalizeNumberKey(key);
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
VARIABLE(entry_start_position_or_hash, MachineType::PointerRepresentation(),
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashTableIndex<OrderedHashSet>(table, key, context,
&entry_start_position_or_hash,
&entry_found, &not_found);
BIND(&entry_found);
// The entry was found, there is nothing to do.
Return(receiver);
Label no_hash(this), add_entry(this), store_new_entry(this);
BIND(&not_found);
{
// If we have a hash code, we can start adding the new entry.
GotoIf(IntPtrGreaterThanOrEqual(entry_start_position_or_hash.value(),
IntPtrConstant(0)),
&add_entry);
// Otherwise, go to runtime to compute the hash code.
entry_start_position_or_hash.Bind(
SmiUntag(CallRuntime(Runtime::kGenericHash, context, key)));
Goto(&add_entry);
}
BIND(&add_entry);
VARIABLE(number_of_buckets, MachineType::PointerRepresentation());
VARIABLE(occupancy, MachineType::PointerRepresentation());
VARIABLE(table_var, MachineRepresentation::kTaggedPointer, table);
{
// Check we have enough space for the entry.
number_of_buckets.Bind(SmiUntag(
LoadFixedArrayElement(table, OrderedHashSet::kNumberOfBucketsIndex)));
STATIC_ASSERT(OrderedHashSet::kLoadFactor == 2);
Node* const capacity = WordShl(number_of_buckets.value(), 1);
Node* const number_of_elements = SmiUntag(
LoadObjectField(table, OrderedHashSet::kNumberOfElementsOffset));
Node* const number_of_deleted = SmiUntag(
LoadObjectField(table, OrderedHashSet::kNumberOfDeletedElementsOffset));
occupancy.Bind(IntPtrAdd(number_of_elements, number_of_deleted));
GotoIf(IntPtrLessThan(occupancy.value(), capacity), &store_new_entry);
// We do not have enough space, grow the table and reload the relevant
// fields.
CallRuntime(Runtime::kSetGrow, context, receiver);
table_var.Bind(LoadObjectField(receiver, JSMap::kTableOffset));
number_of_buckets.Bind(SmiUntag(LoadFixedArrayElement(
table_var.value(), OrderedHashSet::kNumberOfBucketsIndex)));
Node* const new_number_of_elements = SmiUntag(LoadObjectField(
table_var.value(), OrderedHashSet::kNumberOfElementsOffset));
Node* const new_number_of_deleted = SmiUntag(LoadObjectField(
table_var.value(), OrderedHashSet::kNumberOfDeletedElementsOffset));
occupancy.Bind(IntPtrAdd(new_number_of_elements, new_number_of_deleted));
Goto(&store_new_entry);
}
BIND(&store_new_entry);
// Store the key, value and connect the element to the bucket chain.
StoreOrderedHashSetNewEntry(table_var.value(), key,
entry_start_position_or_hash.value(),
number_of_buckets.value(), occupancy.value());
Return(receiver);
}
void CollectionsBuiltinsAssembler::StoreOrderedHashSetNewEntry(
Node* const table, Node* const key, Node* const hash,
Node* const number_of_buckets, Node* const occupancy) {
Node* const bucket =
WordAnd(hash, IntPtrSub(number_of_buckets, IntPtrConstant(1)));
Node* const bucket_entry = LoadFixedArrayElement(
table, bucket, OrderedHashSet::kHashTableStartIndex * kPointerSize);
// Store the entry elements.
Node* const entry_start = IntPtrAdd(
IntPtrMul(occupancy, IntPtrConstant(OrderedHashSet::kEntrySize)),
number_of_buckets);
StoreFixedArrayElement(table, entry_start, key, UPDATE_WRITE_BARRIER,
kPointerSize * OrderedHashSet::kHashTableStartIndex);
StoreFixedArrayElement(table, entry_start, bucket_entry, SKIP_WRITE_BARRIER,
kPointerSize * (OrderedHashSet::kHashTableStartIndex +
OrderedHashSet::kChainOffset));
// Update the bucket head.
StoreFixedArrayElement(table, bucket, SmiTag(occupancy), SKIP_WRITE_BARRIER,
OrderedHashSet::kHashTableStartIndex * kPointerSize);
// Bump the elements count.
Node* const number_of_elements =
LoadObjectField(table, OrderedHashSet::kNumberOfElementsOffset);
StoreObjectFieldNoWriteBarrier(table, OrderedHashSet::kNumberOfElementsOffset,
SmiAdd(number_of_elements, SmiConstant(1)));
}
TF_BUILTIN(SetDelete, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* key = Parameter(Descriptor::kKey);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_SET_TYPE,
"Set.prototype.delete");
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
VARIABLE(entry_start_position_or_hash, MachineType::PointerRepresentation(),
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashTableIndex<OrderedHashSet>(table, key, context,
&entry_start_position_or_hash,
&entry_found, &not_found);
BIND(&not_found);
Return(FalseConstant());
BIND(&entry_found);
// If we found the entry, mark the entry as deleted.
StoreFixedArrayElement(table, entry_start_position_or_hash.value(),
TheHoleConstant(), UPDATE_WRITE_BARRIER,
kPointerSize * OrderedHashSet::kHashTableStartIndex);
// Decrement the number of elements, increment the number of deleted elements.
Node* const number_of_elements =
SmiSub(LoadObjectField(table, OrderedHashSet::kNumberOfElementsOffset),
SmiConstant(1));
StoreObjectFieldNoWriteBarrier(table, OrderedHashSet::kNumberOfElementsOffset,
number_of_elements);
Node* const number_of_deleted = SmiAdd(
LoadObjectField(table, OrderedHashSet::kNumberOfDeletedElementsOffset),
SmiConstant(1));
StoreObjectFieldNoWriteBarrier(
table, OrderedHashSet::kNumberOfDeletedElementsOffset, number_of_deleted);
Node* const number_of_buckets =
LoadFixedArrayElement(table, OrderedHashSet::kNumberOfBucketsIndex);
// If there fewer elements than #buckets / 2, shrink the table.
Label shrink(this);
GotoIf(SmiLessThan(SmiAdd(number_of_elements, number_of_elements),
number_of_buckets),
&shrink);
Return(TrueConstant());
BIND(&shrink);
CallRuntime(Runtime::kSetShrink, context, receiver);
Return(TrueConstant());
}
TF_BUILTIN(MapPrototypeEntries, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* const context = Parameter(Descriptor::kContext);
......@@ -1392,7 +1558,8 @@ TF_BUILTIN(SetIteratorPrototypeNext, CollectionsBuiltinsAssembler) {
}
}
void CollectionsBuiltinsAssembler::TryLookupOrderedHashMapIndex(
template <typename CollectionType>
void CollectionsBuiltinsAssembler::TryLookupOrderedHashTableIndex(
Node* const table, Node* const key, Node* const context, Variable* result,
Label* if_entry_found, Label* if_not_found) {
Label if_key_smi(this), if_key_string(this), if_key_heap_number(this);
......@@ -1401,24 +1568,24 @@ void CollectionsBuiltinsAssembler::TryLookupOrderedHashMapIndex(
GotoIf(IsString(key), &if_key_string);
GotoIf(IsHeapNumber(key), &if_key_heap_number);
FindOrderedHashTableEntryForOtherKey<OrderedHashMap>(
FindOrderedHashTableEntryForOtherKey<CollectionType>(
context, table, key, result, if_entry_found, if_not_found);
BIND(&if_key_smi);
{
FindOrderedHashTableEntryForSmiKey<OrderedHashMap>(
FindOrderedHashTableEntryForSmiKey<CollectionType>(
table, key, result, if_entry_found, if_not_found);
}
BIND(&if_key_string);
{
FindOrderedHashTableEntryForStringKey<OrderedHashMap>(
FindOrderedHashTableEntryForStringKey<CollectionType>(
context, table, key, result, if_entry_found, if_not_found);
}
BIND(&if_key_heap_number);
{
FindOrderedHashTableEntryForHeapNumberKey<OrderedHashMap>(
FindOrderedHashTableEntryForHeapNumberKey<CollectionType>(
context, table, key, result, if_entry_found, if_not_found);
}
}
......@@ -1432,8 +1599,8 @@ TF_BUILTIN(MapLookupHashIndex, CollectionsBuiltinsAssembler) {
IntPtrConstant(0));
Label entry_found(this), not_found(this);
TryLookupOrderedHashMapIndex(table, key, context, &entry_start_position,
&entry_found, &not_found);
TryLookupOrderedHashTableIndex<OrderedHashMap>(
table, key, context, &entry_start_position, &entry_found, &not_found);
BIND(&entry_found);
Node* index = IntPtrAdd(entry_start_position.value(),
......
......@@ -874,6 +874,8 @@ namespace internal {
/* Set */ \
TFJ(SetConstructor, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
TFJ(SetHas, 1, kKey) \
TFJ(SetAdd, 1, kKey) \
TFJ(SetDelete, 1, kKey) \
CPP(SetClear) \
/* ES #sec-set.prototype.entries */ \
TFJ(SetPrototypeEntries, 0) \
......
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(function(global, utils) {
"use strict";
%CheckIsBootstrapping();
// -------------------------------------------------------------------
// Imports
var GlobalMap = global.Map;
var GlobalObject = global.Object;
var GlobalSet = global.Set;
var hashCodeSymbol = utils.ImportNow("hash_code_symbol");
var MathRandom = global.Math.random;
var MapIterator;
var SetIterator;
utils.Import(function(from) {
MapIterator = from.MapIterator;
SetIterator = from.SetIterator;
});
// -------------------------------------------------------------------
function HashToEntry(table, hash, numBuckets) {
var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets);
return ORDERED_HASH_TABLE_BUCKET_AT(table, bucket);
}
%SetForceInlineFlag(HashToEntry);
function SetFindEntry(table, numBuckets, key, hash) {
var entry = HashToEntry(table, hash, numBuckets);
if (entry === NOT_FOUND) return entry;
var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets);
if (key === candidate) return entry;
var keyIsNaN = NUMBER_IS_NAN(key);
while (true) {
if (keyIsNaN && NUMBER_IS_NAN(candidate)) {
return entry;
}
entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets);
if (entry === NOT_FOUND) return entry;
candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets);
if (key === candidate) return entry;
}
return NOT_FOUND;
}
%SetForceInlineFlag(SetFindEntry);
function MapFindEntry(table, numBuckets, key, hash) {
var entry = HashToEntry(table, hash, numBuckets);
if (entry === NOT_FOUND) return entry;
var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets);
if (key === candidate) return entry;
var keyIsNaN = NUMBER_IS_NAN(key);
while (true) {
if (keyIsNaN && NUMBER_IS_NAN(candidate)) {
return entry;
}
entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets);
if (entry === NOT_FOUND) return entry;
candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets);
if (key === candidate) return entry;
}
return NOT_FOUND;
}
%SetForceInlineFlag(MapFindEntry);
function ComputeIntegerHash(key, seed) {
var hash = key;
hash = hash ^ seed;
hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
hash = hash ^ (hash >>> 12);
hash = hash + (hash << 2);
hash = hash ^ (hash >>> 4);
hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11);
hash = hash ^ (hash >>> 16);
return hash & 0x3fffffff;
}
%SetForceInlineFlag(ComputeIntegerHash);
function GetExistingHash(key) {
if (%_IsSmi(key)) {
return ComputeIntegerHash(key, 0);
}
if (IS_STRING(key)) {
var field = %_StringGetRawHashField(key);
if ((field & 1 /* Name::kHashNotComputedMask */) === 0) {
return field >>> 2 /* Name::kHashShift */;
}
} else if (IS_RECEIVER(key) && !IS_PROXY(key) && !IS_GLOBAL(key)) {
var hash = GET_PRIVATE(key, hashCodeSymbol);
return hash;
}
return %GenericHash(key);
}
%SetForceInlineFlag(GetExistingHash);
function GetHash(key) {
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) {
hash = (MathRandom() * 0x40000000) | 0;
if (hash === 0) hash = 1;
SET_PRIVATE(key, hashCodeSymbol, hash);
}
return hash;
}
%SetForceInlineFlag(GetHash);
// -------------------------------------------------------------------
// Harmony Set
//Set up the non-enumerable functions on the Set prototype object.
DEFINE_METHODS(
GlobalSet.prototype,
{
add(key) {
if (!IS_SET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, 'Set.prototype.add', this);
}
// Normalize -0 to +0 as required by the spec.
// Even though we use SameValueZero as the comparison for the keys we don't
// want to ever store -0 as the key since the key is directly exposed when
// doing iteration.
if (key === 0) {
key = 0;
}
var table = %_JSCollectionGetTable(this);
var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table);
var hash = GetHash(key);
if (SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND) return this;
var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table);
var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table);
var capacity = numBuckets << 1;
if ((nof + nod) >= capacity) {
// Need to grow, bail out to runtime.
%SetGrow(this);
// Re-load state from the grown backing store.
table = %_JSCollectionGetTable(this);
numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table);
nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table);
nod = ORDERED_HASH_TABLE_DELETED_COUNT(table);
}
var entry = nof + nod;
var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets);
var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets);
var chainEntry = ORDERED_HASH_TABLE_BUCKET_AT(table, bucket);
ORDERED_HASH_TABLE_SET_BUCKET_AT(table, bucket, entry);
ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof + 1);
FIXED_ARRAY_SET(table, index, key);
FIXED_ARRAY_SET_SMI(table, index + 1, chainEntry);
return this;
}
delete(key) {
if (!IS_SET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'Set.prototype.delete', this);
}
var table = %_JSCollectionGetTable(this);
var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table);
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false;
var entry = SetFindEntry(table, numBuckets, key, hash);
if (entry === NOT_FOUND) return false;
var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1;
var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1;
var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets);
FIXED_ARRAY_SET(table, index, %_TheHole());
ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof);
ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod);
if (nof < (numBuckets >>> 1)) %SetShrink(this);
return true;
}
}
);
// -----------------------------------------------------------------------
// Exports
%InstallToContext([
"set_add", GlobalSet.prototype.add,
"set_delete", GlobalSet.prototype.delete,
]);
utils.Export(function(to) {
to.GetExistingHash = GetExistingHash;
to.GetHash = GetHash;
});
})
......@@ -11,15 +11,34 @@
// -------------------------------------------------------------------
// Imports
var GetExistingHash;
var GetHash;
var hashCodeSymbol = utils.ImportNow("hash_code_symbol");
var GlobalWeakMap = global.WeakMap;
var GlobalWeakSet = global.WeakSet;
var MathRandom = global.Math.random;
// -------------------------------------------------------------------
function GetExistingHash(key) {
if (IS_RECEIVER(key) && !IS_PROXY(key) && !IS_GLOBAL(key)) {
var hash = GET_PRIVATE(key, hashCodeSymbol);
return hash;
}
return %GenericHash(key);
}
%SetForceInlineFlag(GetExistingHash);
function GetHash(key) {
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) {
hash = (MathRandom() * 0x40000000) | 0;
if (hash === 0) hash = 1;
SET_PRIVATE(key, hashCodeSymbol, hash);
}
return hash;
}
%SetForceInlineFlag(GetHash);
utils.Import(function(from) {
GetExistingHash = from.GetExistingHash;
GetHash = from.GetHash;
});
// -------------------------------------------------------------------
// Harmony WeakMap
......
......@@ -2312,7 +2312,6 @@
'js/array.js',
'js/string.js',
'js/typedarray.js',
'js/collection.js',
'js/weak-collection.js',
'js/messages.js',
'js/templates.js',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment