2011-11-10 14:26:57 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "factory.h"
|
|
|
|
#include "macro-assembler.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
#include "code-stubs.h"
|
|
|
|
#include "objects.h"
|
|
|
|
|
|
|
|
#ifdef USE_SIMULATOR
|
|
|
|
#include "simulator.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
|
|
|
|
|
|
|
typedef uint32_t (*HASH_FUNCTION)();
|
|
|
|
|
2012-01-06 11:33:20 +00:00
|
|
|
#define __ masm->
|
2011-11-10 14:26:57 +00:00
|
|
|
|
|
|
|
|
2013-01-09 10:30:54 +00:00
|
|
|
void generate(MacroAssembler* masm, i::Vector<const uint8_t> string) {
|
2012-01-06 11:33:20 +00:00
|
|
|
// GenerateHashInit takes the first character as an argument so it can't
|
|
|
|
// handle the zero length string.
|
|
|
|
ASSERT(string.length() > 0);
|
2011-11-10 14:26:57 +00:00
|
|
|
#ifdef V8_TARGET_ARCH_IA32
|
2011-11-10 17:01:32 +00:00
|
|
|
__ push(ebx);
|
|
|
|
__ push(ecx);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ mov(eax, Immediate(0));
|
2012-01-06 11:33:20 +00:00
|
|
|
__ mov(ebx, Immediate(string.at(0)));
|
|
|
|
StringHelper::GenerateHashInit(masm, eax, ebx, ecx);
|
2011-11-10 14:26:57 +00:00
|
|
|
for (int i = 1; i < string.length(); i++) {
|
|
|
|
__ mov(ebx, Immediate(string.at(i)));
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashAddCharacter(masm, eax, ebx, ecx);
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashGetHash(masm, eax, ecx);
|
2011-11-10 17:01:32 +00:00
|
|
|
__ pop(ecx);
|
|
|
|
__ pop(ebx);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_X64
|
2012-01-06 11:33:20 +00:00
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
2011-11-10 17:01:32 +00:00
|
|
|
__ push(rbx);
|
|
|
|
__ push(rcx);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ movq(rax, Immediate(0));
|
2012-01-06 11:33:20 +00:00
|
|
|
__ movq(rbx, Immediate(string.at(0)));
|
|
|
|
StringHelper::GenerateHashInit(masm, rax, rbx, rcx);
|
2011-11-10 14:26:57 +00:00
|
|
|
for (int i = 1; i < string.length(); i++) {
|
|
|
|
__ movq(rbx, Immediate(string.at(i)));
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashAddCharacter(masm, rax, rbx, rcx);
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashGetHash(masm, rax, rcx);
|
2011-11-10 17:01:32 +00:00
|
|
|
__ pop(rcx);
|
|
|
|
__ pop(rbx);
|
2012-01-06 11:33:20 +00:00
|
|
|
__ pop(kRootRegister);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_ARM
|
2012-01-06 11:33:20 +00:00
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
__ mov(r0, Operand(0));
|
2012-01-06 11:33:20 +00:00
|
|
|
__ mov(ip, Operand(string.at(0)));
|
|
|
|
StringHelper::GenerateHashInit(masm, r0, ip);
|
2011-11-10 14:26:57 +00:00
|
|
|
for (int i = 1; i < string.length(); i++) {
|
2011-11-10 17:01:32 +00:00
|
|
|
__ mov(ip, Operand(string.at(i)));
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashAddCharacter(masm, r0, ip);
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashGetHash(masm, r0);
|
|
|
|
__ pop(kRootRegister);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ mov(pc, Operand(lr));
|
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
2012-01-06 11:33:20 +00:00
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
__ li(v0, Operand(0));
|
2012-01-06 11:33:20 +00:00
|
|
|
__ li(t1, Operand(string.at(0)));
|
|
|
|
StringHelper::GenerateHashInit(masm, v0, t1);
|
2011-11-10 14:26:57 +00:00
|
|
|
for (int i = 1; i < string.length(); i++) {
|
2011-11-10 17:01:32 +00:00
|
|
|
__ li(t1, Operand(string.at(i)));
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashAddCharacter(masm, v0, t1);
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
2012-01-06 11:33:20 +00:00
|
|
|
StringHelper::GenerateHashGetHash(masm, v0);
|
|
|
|
__ pop(kRootRegister);
|
2011-11-10 14:26:57 +00:00
|
|
|
__ jr(ra);
|
2011-12-15 14:24:07 +00:00
|
|
|
__ nop();
|
2011-11-10 14:26:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
void generate(MacroAssembler* masm, uint32_t key) {
|
|
|
|
#ifdef V8_TARGET_ARCH_IA32
|
|
|
|
__ push(ebx);
|
|
|
|
__ mov(eax, Immediate(key));
|
|
|
|
__ GetNumberHash(eax, ebx);
|
|
|
|
__ pop(ebx);
|
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_X64
|
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ push(rbx);
|
|
|
|
__ movq(rax, Immediate(key));
|
|
|
|
__ GetNumberHash(rax, rbx);
|
|
|
|
__ pop(rbx);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_ARM
|
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ mov(r0, Operand(key));
|
|
|
|
__ GetNumberHash(r0, ip);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ mov(pc, Operand(lr));
|
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ li(v0, Operand(key));
|
|
|
|
__ GetNumberHash(v0, t1);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ jr(ra);
|
|
|
|
__ nop();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 10:30:54 +00:00
|
|
|
void check(i::Vector<const uint8_t> string) {
|
2013-01-09 15:47:53 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
v8::internal::byte buffer[2048];
|
2013-01-09 15:47:53 +00:00
|
|
|
MacroAssembler masm(isolate, buffer, sizeof buffer);
|
2011-11-10 14:26:57 +00:00
|
|
|
|
2012-01-06 11:33:20 +00:00
|
|
|
generate(&masm, string);
|
2011-11-10 14:26:57 +00:00
|
|
|
|
|
|
|
CodeDesc desc;
|
2012-01-06 11:33:20 +00:00
|
|
|
masm.GetCode(&desc);
|
2013-01-09 15:47:53 +00:00
|
|
|
Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
|
|
|
|
Handle<Code> code = factory->NewCode(desc,
|
|
|
|
Code::ComputeFlags(Code::STUB),
|
|
|
|
undefined);
|
2011-11-10 14:26:57 +00:00
|
|
|
CHECK(code->IsCode());
|
|
|
|
|
2011-12-15 14:24:07 +00:00
|
|
|
HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
|
2013-01-09 15:47:53 +00:00
|
|
|
Handle<String> v8_string = factory->NewStringFromOneByte(string);
|
2011-11-10 14:26:57 +00:00
|
|
|
v8_string->set_hash_field(String::kEmptyHashField);
|
|
|
|
#ifdef USE_SIMULATOR
|
|
|
|
uint32_t codegen_hash =
|
|
|
|
reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0));
|
|
|
|
#else
|
|
|
|
uint32_t codegen_hash = hash();
|
|
|
|
#endif
|
|
|
|
uint32_t runtime_hash = v8_string->Hash();
|
|
|
|
CHECK(runtime_hash == codegen_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 10:30:54 +00:00
|
|
|
void check(i::Vector<const char> s) {
|
|
|
|
check(i::Vector<const uint8_t>::cast(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
void check(uint32_t key) {
|
2013-01-09 15:47:53 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
v8::internal::byte buffer[2048];
|
|
|
|
MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer);
|
|
|
|
|
|
|
|
generate(&masm, key);
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(&desc);
|
2013-01-09 15:47:53 +00:00
|
|
|
Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
|
|
|
|
Handle<Code> code = factory->NewCode(desc,
|
|
|
|
Code::ComputeFlags(Code::STUB),
|
|
|
|
undefined);
|
2012-01-10 12:58:41 +00:00
|
|
|
CHECK(code->IsCode());
|
|
|
|
|
|
|
|
HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
|
|
|
|
#ifdef USE_SIMULATOR
|
|
|
|
uint32_t codegen_hash =
|
|
|
|
reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0));
|
|
|
|
#else
|
|
|
|
uint32_t codegen_hash = hash();
|
|
|
|
#endif
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed());
|
2012-01-10 12:58:41 +00:00
|
|
|
CHECK(runtime_hash == codegen_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-09 10:30:54 +00:00
|
|
|
void check_twochars(uint8_t a, uint8_t b) {
|
|
|
|
uint8_t ab[2] = {a, b};
|
|
|
|
check(i::Vector<const uint8_t>(ab, 2));
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
static uint32_t PseudoRandom(uint32_t i, uint32_t j) {
|
|
|
|
return ~(~((i * 781) ^ (j * 329)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
TEST(StringHash) {
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope context_scope(v8::Context::New(isolate));
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
for (uint8_t a = 0; a < String::kMaxOneByteCharCode; a++) {
|
2011-11-10 14:26:57 +00:00
|
|
|
// Numbers are hashed differently.
|
|
|
|
if (a >= '0' && a <= '9') continue;
|
2013-01-09 15:47:53 +00:00
|
|
|
for (uint8_t b = 0; b < String::kMaxOneByteCharCode; b++) {
|
2011-11-10 14:26:57 +00:00
|
|
|
if (b >= '0' && b <= '9') continue;
|
2013-01-09 15:47:53 +00:00
|
|
|
check_twochars(a, b);
|
2011-11-10 14:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
check(i::Vector<const char>("*", 1));
|
|
|
|
check(i::Vector<const char>(".zZ", 3));
|
|
|
|
check(i::Vector<const char>("muc", 3));
|
|
|
|
check(i::Vector<const char>("(>'_')>", 7));
|
|
|
|
check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21));
|
|
|
|
}
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
|
|
|
|
TEST(NumberHash) {
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope context_scope(v8::Context::New(isolate));
|
2012-01-10 12:58:41 +00:00
|
|
|
|
|
|
|
// Some specific numbers
|
|
|
|
for (uint32_t key = 0; key < 42; key += 7) {
|
|
|
|
check(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pseudo-random numbers
|
|
|
|
static const uint32_t kLimit = 1000;
|
|
|
|
for (uint32_t i = 0; i < 5; i++) {
|
|
|
|
for (uint32_t j = 0; j < 5; j++) {
|
|
|
|
check(PseudoRandom(i, j) % kLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
#undef __
|