Commit fae807b3 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Elements kind conversion in generated code (ia32).

BUG=
TEST=

Review URL: http://codereview.chromium.org/8241003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9605 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 87f3ff1e
......@@ -415,4 +415,28 @@ bool ToBooleanStub::Types::CanBeUndetectable() const {
}
void FastElementsConversionStub::Generate(MacroAssembler* masm) {
#if defined(V8_TARGET_ARCH_IA32)
if (to_ == FAST_ELEMENTS) {
if (from_ == FAST_SMI_ONLY_ELEMENTS) {
GenerateSmiOnlyToObject(masm);
} else if (from_ == FAST_DOUBLE_ELEMENTS) {
GenerateDoubleToObject(masm, strict_mode_);
} else {
UNREACHABLE();
}
KeyedStoreStubCompiler::GenerateStoreFastElement(masm,
is_jsarray_,
FAST_ELEMENTS);
} else if (from_ == FAST_SMI_ONLY_ELEMENTS && to_ == FAST_DOUBLE_ELEMENTS) {
GenerateSmiOnlyToDouble(masm, strict_mode_);
KeyedStoreStubCompiler::GenerateStoreFastDoubleElement(masm, is_jsarray_);
} else {
UNREACHABLE();
}
#else
KeyedStoreIC::GenerateRuntimeSetProperty(masm, strict_mode_);
#endif // V8_TARGET_ARCH_IA32
}
} } // namespace v8::internal
......@@ -69,7 +69,8 @@ namespace internal {
V(KeyedLoadElement) \
V(KeyedStoreElement) \
V(DebuggerStatement) \
V(StringDictionaryLookup)
V(StringDictionaryLookup) \
V(FastElementsConversion)
// List of code stubs only used on ARM platforms.
#ifdef V8_TARGET_ARCH_ARM
......@@ -1025,6 +1026,47 @@ class ToBooleanStub: public CodeStub {
Types types_;
};
class FastElementsConversionStub : public CodeStub {
public:
FastElementsConversionStub(ElementsKind from,
ElementsKind to,
bool is_jsarray,
StrictModeFlag strict_mode)
: from_(from),
to_(to),
is_jsarray_(is_jsarray),
strict_mode_(strict_mode) {}
private:
class FromBits: public BitField<ElementsKind, 0, 8> {};
class ToBits: public BitField<ElementsKind, 8, 8> {};
class IsJSArrayBits: public BitField<bool, 16, 8> {};
class StrictModeBits: public BitField<StrictModeFlag, 24, 8> {};
Major MajorKey() { return FastElementsConversion; }
int MinorKey() {
return FromBits::encode(from_) |
ToBits::encode(to_) |
IsJSArrayBits::encode(is_jsarray_) |
StrictModeBits::encode(strict_mode_);
}
void Generate(MacroAssembler* masm);
static void GenerateSmiOnlyToObject(MacroAssembler* masm);
static void GenerateSmiOnlyToDouble(MacroAssembler* masm,
StrictModeFlag strict_mode);
static void GenerateDoubleToObject(MacroAssembler* masm,
StrictModeFlag strict_mode);
ElementsKind from_;
ElementsKind to_;
bool is_jsarray_;
StrictModeFlag strict_mode_;
DISALLOW_COPY_AND_ASSIGN(FastElementsConversionStub);
};
} } // namespace v8::internal
#endif // V8_CODE_STUBS_H_
......@@ -34,6 +34,7 @@
#include "isolate.h"
#include "jsregexp.h"
#include "regexp-macro-assembler.h"
#include "stub-cache.h"
namespace v8 {
namespace internal {
......@@ -6749,6 +6750,13 @@ struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
{ ebx, edx, ecx, EMIT_REMEMBERED_SET},
// KeyedStoreStubCompiler::GenerateStoreFastElement.
{ edi, edx, ecx, EMIT_REMEMBERED_SET},
// FastElementConversionStub::GenerateSmiOnlyToObject
// and FastElementsConversionStub::GenerateSmiOnlyToDouble
// and FastElementsConversionStub::GenerateDoubleToObject
{ edx, ebx, edi, EMIT_REMEMBERED_SET},
// FastElementConversionStub::GenerateDoubleToObject
{ eax, edx, esi, EMIT_REMEMBERED_SET},
{ edx, eax, edi, EMIT_REMEMBERED_SET},
// Null termination.
{ no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
};
......@@ -6992,6 +7000,255 @@ void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
}
void FastElementsConversionStub::GenerateSmiOnlyToObject(MacroAssembler* masm) {
// ----------- S t a t e -------------
// -- eax : value
// -- ebx : target map
// -- ecx : key
// -- edx : receiver
// -- esp[0] : return address
// -----------------------------------
// Set transitioned map.
__ mov(FieldOperand(edx, HeapObject::kMapOffset), ebx);
__ RecordWriteField(edx,
HeapObject::kMapOffset,
ebx,
edi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
}
void FastElementsConversionStub::GenerateSmiOnlyToDouble(
MacroAssembler* masm, StrictModeFlag strict_mode) {
// ----------- S t a t e -------------
// -- eax : value
// -- ebx : target map
// -- ecx : key
// -- edx : receiver
// -- esp[0] : return address
// -----------------------------------
Label loop, entry, convert_hole, gc_required;
__ push(eax);
__ push(ebx);
__ mov(edi, FieldOperand(edx, JSObject::kElementsOffset));
__ mov(edi, FieldOperand(edi, FixedArray::kLengthOffset));
// Allocate new FixedDoubleArray.
// edx: receiver
// edi: length of source FixedArray (smi-tagged)
__ lea(esi, Operand(edi, times_4, FixedDoubleArray::kHeaderSize));
__ AllocateInNewSpace(esi, eax, ebx, no_reg, &gc_required, TAG_OBJECT);
// eax: destination FixedDoubleArray
// edi: number of elements
// edx: receiver
__ mov(FieldOperand(eax, HeapObject::kMapOffset),
Immediate(masm->isolate()->factory()->fixed_double_array_map()));
__ mov(FieldOperand(eax, FixedDoubleArray::kLengthOffset), edi);
__ mov(esi, FieldOperand(edx, JSObject::kElementsOffset));
// Replace receiver's backing store with newly created FixedDoubleArray.
__ mov(FieldOperand(edx, JSObject::kElementsOffset), eax);
__ mov(ebx, eax);
__ RecordWriteField(edx,
JSObject::kElementsOffset,
ebx,
edi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
__ mov(edi, FieldOperand(esi, FixedArray::kLengthOffset));
// Convert and copy elements
// esi: source FixedArray
// edi: number of elements to convert/copy
ExternalReference canonical_the_hole_nan_reference =
ExternalReference::address_of_the_hole_nan();
XMMRegister the_hole_nan = xmm1;
if (CpuFeatures::IsSupported(SSE2)) {
CpuFeatures::Scope use_sse2(SSE2);
__ movdbl(the_hole_nan,
Operand::StaticVariable(canonical_the_hole_nan_reference));
}
__ jmp(&entry);
__ bind(&loop);
__ sub(edi, Immediate(Smi::FromInt(1)));
__ mov(ebx, FieldOperand(esi, edi, times_2, FixedArray::kHeaderSize));
// ebx: current element from source
// edi: index of current element
__ JumpIfNotSmi(ebx, &convert_hole);
// Normal smi, convert it to double and store.
__ SmiUntag(ebx);
if (CpuFeatures::IsSupported(SSE2)) {
CpuFeatures::Scope fscope(SSE2);
__ cvtsi2sd(xmm0, ebx);
__ movdbl(FieldOperand(eax, edi, times_4, FixedDoubleArray::kHeaderSize),
xmm0);
} else {
__ push(ebx);
__ fild_s(Operand(esp, 0));
__ pop(ebx);
__ fstp_d(FieldOperand(eax, edi, times_4, FixedDoubleArray::kHeaderSize));
}
__ jmp(&entry);
// Found hole, store hole_nan_as_double instead.
__ bind(&convert_hole);
if (CpuFeatures::IsSupported(SSE2)) {
CpuFeatures::Scope use_sse2(SSE2);
__ movdbl(FieldOperand(eax, edi, times_4, FixedDoubleArray::kHeaderSize),
the_hole_nan);
} else {
__ fld_d(Operand::StaticVariable(canonical_the_hole_nan_reference));
__ fstp_d(FieldOperand(eax, edi, times_4, FixedDoubleArray::kHeaderSize));
}
__ bind(&entry);
__ test(edi, edi);
__ j(not_zero, &loop);
Label done;
__ pop(ebx);
__ pop(eax);
// eax: value
// ebx: target map
// Set transitioned map.
__ mov(FieldOperand(edx, HeapObject::kMapOffset), ebx);
__ RecordWriteField(edx,
HeapObject::kMapOffset,
ebx,
edi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
// Restore esi.
__ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
__ jmp(&done, Label::kNear);
__ bind(&gc_required);
// Restore registers before jumping into runtime.
__ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
__ pop(ebx);
__ pop(eax);
KeyedStoreIC::GenerateRuntimeSetProperty(masm, strict_mode);
__ bind(&done);
}
void FastElementsConversionStub::GenerateDoubleToObject(
MacroAssembler* masm, StrictModeFlag strict_mode) {
// ----------- S t a t e -------------
// -- eax : value
// -- ebx : target map
// -- ecx : key
// -- edx : receiver
// -- esp[0] : return address
// -----------------------------------
Label loop, entry, convert_hole, gc_required;
__ push(eax);
__ push(edx);
__ push(ebx);
__ mov(edi, FieldOperand(edx, JSObject::kElementsOffset));
__ mov(ebx, FieldOperand(edi, FixedDoubleArray::kLengthOffset));
// Allocate new FixedArray.
// ebx: length of source FixedDoubleArray (smi-tagged)
__ lea(edi, Operand(ebx, times_2, FixedArray::kHeaderSize));
__ AllocateInNewSpace(edi, eax, esi, no_reg, &gc_required, TAG_OBJECT);
// eax: destination FixedArray
// ebx: number of elements
__ mov(FieldOperand(eax, HeapObject::kMapOffset),
Immediate(masm->isolate()->factory()->fixed_array_map()));
__ mov(FieldOperand(eax, FixedArray::kLengthOffset), ebx);
__ mov(edi, FieldOperand(edx, JSObject::kElementsOffset));
// Box doubles into heap numbers.
// edi: source FixedDoubleArray
// eax: destination FixedArray
__ jmp(&entry);
__ bind(&loop);
__ sub(ebx, Immediate(Smi::FromInt(1)));
// ebx: index of current element (smi-tagged)
uint32_t offset = FixedDoubleArray::kHeaderSize + sizeof(kHoleNanLower32);
__ cmp(FieldOperand(edi, ebx, times_4, offset), Immediate(kHoleNanUpper32));
__ j(equal, &convert_hole);
// Non-hole double, copy value into a heap number.
__ AllocateHeapNumber(edx, esi, no_reg, &gc_required);
// edx: new heap number
if (CpuFeatures::IsSupported(SSE2)) {
CpuFeatures::Scope fscope(SSE2);
__ movdbl(xmm0,
FieldOperand(edi, ebx, times_4, FixedDoubleArray::kHeaderSize));
__ movdbl(FieldOperand(edx, HeapNumber::kValueOffset), xmm0);
} else {
__ mov(esi, FieldOperand(edi, ebx, times_4, FixedDoubleArray::kHeaderSize));
__ mov(FieldOperand(edx, HeapNumber::kValueOffset), esi);
__ mov(esi, FieldOperand(edi, ebx, times_4, offset));
__ mov(FieldOperand(edx, HeapNumber::kValueOffset + kPointerSize), esi);
}
__ mov(FieldOperand(eax, ebx, times_2, FixedArray::kHeaderSize), edx);
__ mov(esi, ebx);
__ RecordWriteArray(eax,
edx,
esi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
__ jmp(&entry);
// Replace the-hole nan with the-hole pointer.
__ bind(&convert_hole);
__ mov(FieldOperand(eax, ebx, times_2, FixedArray::kHeaderSize),
masm->isolate()->factory()->the_hole_value());
__ bind(&entry);
__ test(ebx, ebx);
__ j(not_zero, &loop);
__ pop(ebx);
__ pop(edx);
// ebx: target map
// edx: receiver
// Set transitioned map.
__ mov(FieldOperand(edx, HeapObject::kMapOffset), ebx);
__ RecordWriteField(edx,
HeapObject::kMapOffset,
ebx,
edi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
// Replace receiver's backing store with newly created and filled FixedArray.
__ mov(FieldOperand(edx, JSObject::kElementsOffset), eax);
__ RecordWriteField(edx,
JSObject::kElementsOffset,
eax,
edi,
kDontSaveFPRegs,
EMIT_REMEMBERED_SET,
OMIT_SMI_CHECK);
// Restore registers.
Label done;
__ pop(eax);
__ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
__ jmp(&done, Label::kNear);
__ bind(&gc_required);
__ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
__ pop(ebx);
__ pop(edx);
__ pop(eax);
KeyedStoreIC::GenerateRuntimeSetProperty(masm, strict_mode);
__ bind(&done);
}
#undef __
} } // namespace v8::internal
......
......@@ -1593,7 +1593,7 @@ void KeyedIC::GetReceiverMapsForStub(Code* stub, MapList* result) {
RelocInfo* info = it.rinfo();
Object* object = info->target_object();
ASSERT(object->IsMap());
result->Add(Map::cast(object));
AddOneReceiverMapIfMissing(result, Map::cast(object));
}
}
}
......@@ -1781,12 +1781,6 @@ Map* GetTransitionedMap(Map* map, MapList* maps_list) {
MaybeObject* KeyedStoreIC::ComputePolymorphicStub(
MapList* receiver_maps,
StrictModeFlag strict_mode) {
// TODO(yangguo): <remove>
Code* generic_stub = (strict_mode == kStrictMode)
? isolate()->builtins()->builtin(Builtins::kKeyedStoreIC_Generic_Strict)
: isolate()->builtins()->builtin(Builtins::kKeyedStoreIC_Generic);
// </remove>
// Collect MONOMORPHIC stubs for all target_receiver_maps.
CodeList handler_ics(receiver_maps->length());
MapList transitioned_maps(receiver_maps->length());
......@@ -1795,15 +1789,11 @@ MaybeObject* KeyedStoreIC::ComputePolymorphicStub(
MaybeObject* maybe_cached_stub = NULL;
Map* transitioned_map = GetTransitionedMap(receiver_map, receiver_maps);
if (transitioned_map != NULL) {
// TODO(yangguo): Enable this code!
// maybe_cached_stub = FastElementsConversionStub(
// receiver_map->elements_kind(), // original elements_kind
// transitioned_map->elements_kind(),
// receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array
// strict_mode_).TryGetCode();
// TODO(yangguo): <remove>
maybe_cached_stub = generic_stub;
// </remove>
maybe_cached_stub = FastElementsConversionStub(
receiver_map->elements_kind(), // original elements_kind
transitioned_map->elements_kind(),
receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array
strict_mode).TryGetCode();
} else {
maybe_cached_stub = ComputeMonomorphicStubWithoutMapCheck(
receiver_map, strict_mode);
......
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "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 THE COPYRIGHT
// OWNER OR CONTRIBUTORS 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.
// Flags: --allow-natives-syntax --smi-only-arrays
support_smi_only_arrays = %HasFastSmiOnlyElements([]);
if (support_smi_only_arrays) {
function test(test_double, test_object, set, length) {
// We apply the same operations to two identical arrays. The first array
// triggers an IC miss, upon which the conversion stub is generated, but the
// actual conversion is done in runtime. The second array, arriving at
// the previously patched IC, is then converted using the conversion stub.
var array_1 = new Array(length);
var array_2 = new Array(length);
assertTrue(%HasFastSmiOnlyElements(array_1));
assertTrue(%HasFastSmiOnlyElements(array_2));
for (var i = 0; i < length; i++) {
if (i == length - 8 && test_double) {
set(array_1, i, 0.5);
set(array_2, i, 0.5);
assertTrue(%HasFastDoubleElements(array_1));
assertTrue(%HasFastDoubleElements(array_2));
} else if (i == length - 5 && test_object) {
set(array_1, i, 'object');
set(array_2, i, 'object');
assertTrue(%HasFastElements(array_1));
assertTrue(%HasFastElements(array_2));
} else {
set(array_1, i, 2*i+1);
set(array_2, i, 2*i+1);
}
}
for (var i = 0; i < length; i++) {
if (i == length - 8 && test_double) {
assertEquals(0.5, array_1[i]);
assertEquals(0.5, array_2[i]);
} else if (i == length - 5 && test_object) {
assertEquals('object', array_1[i]);
assertEquals('object', array_2[i]);
} else {
assertEquals(2*i+1, array_1[i]);
assertEquals(2*i+1, array_2[i]);
}
}
}
test(false, false, function(a,i,v){ a[i] = v; }, 100);
test(true, false, function(a,i,v){ a[i] = v; }, 100);
test(false, true, function(a,i,v){ a[i] = v; }, 100);
test(true, true, function(a,i,v){ a[i] = v; }, 100);
test(false, false, function(a,i,v){ a[i] = v; }, 10000);
test(true, false, function(a,i,v){ a[i] = v; }, 10000);
test(false, true, function(a,i,v){ a[i] = v; }, 10000);
test(true, true, function(a,i,v){ a[i] = v; }, 10000);
} else {
print("Test skipped because smi only arrays are not supported.");
}
\ No newline at end of file
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