blob: 9aa84614d6bee442e7b619c37be08459f91c5b90 [file] [log] [blame]
Ben Murdoch692be652012-01-10 18:47:50 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "factory.h"
33#include "macro-assembler.h"
34#include "cctest.h"
35#include "code-stubs.h"
36#include "objects.h"
37
38#ifdef USE_SIMULATOR
39#include "simulator.h"
40#endif
41
42using namespace v8::internal;
43
44
45typedef uint32_t (*HASH_FUNCTION)();
46
47static v8::Persistent<v8::Context> env;
48
49#define __ masm->
50
51
52void generate(MacroAssembler* masm, i::Vector<const char> string) {
53 // GenerateHashInit takes the first character as an argument so it can't
54 // handle the zero length string.
55 ASSERT(string.length() > 0);
56#ifdef V8_TARGET_ARCH_IA32
57 __ push(ebx);
58 __ push(ecx);
59 __ mov(eax, Immediate(0));
60 __ mov(ebx, Immediate(string.at(0)));
61 StringHelper::GenerateHashInit(masm, eax, ebx, ecx);
62 for (int i = 1; i < string.length(); i++) {
63 __ mov(ebx, Immediate(string.at(i)));
64 StringHelper::GenerateHashAddCharacter(masm, eax, ebx, ecx);
65 }
66 StringHelper::GenerateHashGetHash(masm, eax, ecx);
67 __ pop(ecx);
68 __ pop(ebx);
69 __ Ret();
70#elif V8_TARGET_ARCH_X64
71 __ push(kRootRegister);
72 __ InitializeRootRegister();
73 __ push(rbx);
74 __ push(rcx);
75 __ movq(rax, Immediate(0));
76 __ movq(rbx, Immediate(string.at(0)));
77 StringHelper::GenerateHashInit(masm, rax, rbx, rcx);
78 for (int i = 1; i < string.length(); i++) {
79 __ movq(rbx, Immediate(string.at(i)));
80 StringHelper::GenerateHashAddCharacter(masm, rax, rbx, rcx);
81 }
82 StringHelper::GenerateHashGetHash(masm, rax, rcx);
83 __ pop(rcx);
84 __ pop(rbx);
85 __ pop(kRootRegister);
86 __ Ret();
87#elif V8_TARGET_ARCH_ARM
88 __ push(kRootRegister);
89 __ InitializeRootRegister();
90
91 __ mov(r0, Operand(0));
92 __ mov(ip, Operand(string.at(0)));
93 StringHelper::GenerateHashInit(masm, r0, ip);
94 for (int i = 1; i < string.length(); i++) {
95 __ mov(ip, Operand(string.at(i)));
96 StringHelper::GenerateHashAddCharacter(masm, r0, ip);
97 }
98 StringHelper::GenerateHashGetHash(masm, r0);
99 __ pop(kRootRegister);
100 __ mov(pc, Operand(lr));
101#elif V8_TARGET_ARCH_MIPS
102 __ push(kRootRegister);
103 __ InitializeRootRegister();
104
105 __ li(v0, Operand(0));
106 __ li(t1, Operand(string.at(0)));
107 StringHelper::GenerateHashInit(masm, v0, t1);
108 for (int i = 1; i < string.length(); i++) {
109 __ li(t1, Operand(string.at(i)));
110 StringHelper::GenerateHashAddCharacter(masm, v0, t1);
111 }
112 StringHelper::GenerateHashGetHash(masm, v0);
113 __ pop(kRootRegister);
114 __ jr(ra);
115 __ nop();
116#endif
117}
118
119
120void check(i::Vector<const char> string) {
121 v8::HandleScope scope;
122 v8::internal::byte buffer[2048];
123 MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer);
124
125 generate(&masm, string);
126
127 CodeDesc desc;
128 masm.GetCode(&desc);
129 Code* code = Code::cast(HEAP->CreateCode(
130 desc,
131 Code::ComputeFlags(Code::STUB),
132 Handle<Object>(HEAP->undefined_value()))->ToObjectChecked());
133 CHECK(code->IsCode());
134
135 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
136 Handle<String> v8_string = FACTORY->NewStringFromAscii(string);
137 v8_string->set_hash_field(String::kEmptyHashField);
138#ifdef USE_SIMULATOR
139 uint32_t codegen_hash =
140 reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0));
141#else
142 uint32_t codegen_hash = hash();
143#endif
144 uint32_t runtime_hash = v8_string->Hash();
145 CHECK(runtime_hash == codegen_hash);
146}
147
148
149void check_twochars(char a, char b) {
150 char ab[2] = {a, b};
151 check(i::Vector<const char>(ab, 2));
152}
153
154
155TEST(StringHash) {
156 if (env.IsEmpty()) env = v8::Context::New();
157 for (int a = 0; a < String::kMaxAsciiCharCode; a++) {
158 // Numbers are hashed differently.
159 if (a >= '0' && a <= '9') continue;
160 for (int b = 0; b < String::kMaxAsciiCharCode; b++) {
161 if (b >= '0' && b <= '9') continue;
162 check_twochars(static_cast<char>(a), static_cast<char>(b));
163 }
164 }
165 check(i::Vector<const char>("*", 1));
166 check(i::Vector<const char>(".zZ", 3));
167 check(i::Vector<const char>("muc", 3));
168 check(i::Vector<const char>("(>'_')>", 7));
169 check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21));
170}
171
172#undef __