OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 StringHelper::GenerateHashAddCharacter(masm, v0, t1); | 110 StringHelper::GenerateHashAddCharacter(masm, v0, t1); |
111 } | 111 } |
112 StringHelper::GenerateHashGetHash(masm, v0); | 112 StringHelper::GenerateHashGetHash(masm, v0); |
113 __ pop(kRootRegister); | 113 __ pop(kRootRegister); |
114 __ jr(ra); | 114 __ jr(ra); |
115 __ nop(); | 115 __ nop(); |
116 #endif | 116 #endif |
117 } | 117 } |
118 | 118 |
119 | 119 |
| 120 void generate(MacroAssembler* masm, uint32_t key) { |
| 121 #ifdef V8_TARGET_ARCH_IA32 |
| 122 __ push(ebx); |
| 123 __ mov(eax, Immediate(key)); |
| 124 __ GetNumberHash(eax, ebx); |
| 125 __ pop(ebx); |
| 126 __ Ret(); |
| 127 #elif V8_TARGET_ARCH_X64 |
| 128 __ push(kRootRegister); |
| 129 __ InitializeRootRegister(); |
| 130 __ push(rbx); |
| 131 __ movq(rax, Immediate(key)); |
| 132 __ GetNumberHash(rax, rbx); |
| 133 __ pop(rbx); |
| 134 __ pop(kRootRegister); |
| 135 __ Ret(); |
| 136 #elif V8_TARGET_ARCH_ARM |
| 137 __ push(kRootRegister); |
| 138 __ InitializeRootRegister(); |
| 139 __ mov(r0, Operand(key)); |
| 140 __ GetNumberHash(r0, ip); |
| 141 __ pop(kRootRegister); |
| 142 __ mov(pc, Operand(lr)); |
| 143 #elif V8_TARGET_ARCH_MIPS |
| 144 __ push(kRootRegister); |
| 145 __ InitializeRootRegister(); |
| 146 __ li(v0, Operand(key)); |
| 147 __ GetNumberHash(v0, t1); |
| 148 __ pop(kRootRegister); |
| 149 __ jr(ra); |
| 150 __ nop(); |
| 151 #endif |
| 152 } |
| 153 |
| 154 |
120 void check(i::Vector<const char> string) { | 155 void check(i::Vector<const char> string) { |
121 v8::HandleScope scope; | 156 v8::HandleScope scope; |
122 v8::internal::byte buffer[2048]; | 157 v8::internal::byte buffer[2048]; |
123 MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer); | 158 MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer); |
124 | 159 |
125 generate(&masm, string); | 160 generate(&masm, string); |
126 | 161 |
127 CodeDesc desc; | 162 CodeDesc desc; |
128 masm.GetCode(&desc); | 163 masm.GetCode(&desc); |
129 Code* code = Code::cast(HEAP->CreateCode( | 164 Code* code = Code::cast(HEAP->CreateCode( |
130 desc, | 165 desc, |
131 Code::ComputeFlags(Code::STUB), | 166 Code::ComputeFlags(Code::STUB), |
132 Handle<Object>(HEAP->undefined_value()))->ToObjectChecked()); | 167 Handle<Object>(HEAP->undefined_value()))->ToObjectChecked()); |
133 CHECK(code->IsCode()); | 168 CHECK(code->IsCode()); |
134 | 169 |
135 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); | 170 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); |
136 Handle<String> v8_string = FACTORY->NewStringFromAscii(string); | 171 Handle<String> v8_string = FACTORY->NewStringFromAscii(string); |
137 v8_string->set_hash_field(String::kEmptyHashField); | 172 v8_string->set_hash_field(String::kEmptyHashField); |
138 #ifdef USE_SIMULATOR | 173 #ifdef USE_SIMULATOR |
139 uint32_t codegen_hash = | 174 uint32_t codegen_hash = |
140 reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); | 175 reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); |
141 #else | 176 #else |
142 uint32_t codegen_hash = hash(); | 177 uint32_t codegen_hash = hash(); |
143 #endif | 178 #endif |
144 uint32_t runtime_hash = v8_string->Hash(); | 179 uint32_t runtime_hash = v8_string->Hash(); |
145 CHECK(runtime_hash == codegen_hash); | 180 CHECK(runtime_hash == codegen_hash); |
146 } | 181 } |
147 | 182 |
148 | 183 |
| 184 void check(uint32_t key) { |
| 185 v8::HandleScope scope; |
| 186 v8::internal::byte buffer[2048]; |
| 187 MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer); |
| 188 |
| 189 generate(&masm, key); |
| 190 |
| 191 CodeDesc desc; |
| 192 masm.GetCode(&desc); |
| 193 Code* code = Code::cast(HEAP->CreateCode( |
| 194 desc, |
| 195 Code::ComputeFlags(Code::STUB), |
| 196 Handle<Object>(HEAP->undefined_value()))->ToObjectChecked()); |
| 197 CHECK(code->IsCode()); |
| 198 |
| 199 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); |
| 200 #ifdef USE_SIMULATOR |
| 201 uint32_t codegen_hash = |
| 202 reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); |
| 203 #else |
| 204 uint32_t codegen_hash = hash(); |
| 205 #endif |
| 206 |
| 207 uint32_t runtime_hash = ComputeIntegerHash( |
| 208 key, |
| 209 Isolate::Current()->heap()->StringHashSeed()); |
| 210 CHECK(runtime_hash == codegen_hash); |
| 211 } |
| 212 |
| 213 |
149 void check_twochars(char a, char b) { | 214 void check_twochars(char a, char b) { |
150 char ab[2] = {a, b}; | 215 char ab[2] = {a, b}; |
151 check(i::Vector<const char>(ab, 2)); | 216 check(i::Vector<const char>(ab, 2)); |
152 } | 217 } |
153 | 218 |
154 | 219 |
| 220 static uint32_t PseudoRandom(uint32_t i, uint32_t j) { |
| 221 return ~(~((i * 781) ^ (j * 329))); |
| 222 } |
| 223 |
| 224 |
155 TEST(StringHash) { | 225 TEST(StringHash) { |
156 if (env.IsEmpty()) env = v8::Context::New(); | 226 if (env.IsEmpty()) env = v8::Context::New(); |
157 for (int a = 0; a < String::kMaxAsciiCharCode; a++) { | 227 for (int a = 0; a < String::kMaxAsciiCharCode; a++) { |
158 // Numbers are hashed differently. | 228 // Numbers are hashed differently. |
159 if (a >= '0' && a <= '9') continue; | 229 if (a >= '0' && a <= '9') continue; |
160 for (int b = 0; b < String::kMaxAsciiCharCode; b++) { | 230 for (int b = 0; b < String::kMaxAsciiCharCode; b++) { |
161 if (b >= '0' && b <= '9') continue; | 231 if (b >= '0' && b <= '9') continue; |
162 check_twochars(static_cast<char>(a), static_cast<char>(b)); | 232 check_twochars(static_cast<char>(a), static_cast<char>(b)); |
163 } | 233 } |
164 } | 234 } |
165 check(i::Vector<const char>("*", 1)); | 235 check(i::Vector<const char>("*", 1)); |
166 check(i::Vector<const char>(".zZ", 3)); | 236 check(i::Vector<const char>(".zZ", 3)); |
167 check(i::Vector<const char>("muc", 3)); | 237 check(i::Vector<const char>("muc", 3)); |
168 check(i::Vector<const char>("(>'_')>", 7)); | 238 check(i::Vector<const char>("(>'_')>", 7)); |
169 check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21)); | 239 check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21)); |
170 } | 240 } |
171 | 241 |
| 242 |
| 243 TEST(NumberHash) { |
| 244 if (env.IsEmpty()) env = v8::Context::New(); |
| 245 |
| 246 // Some specific numbers |
| 247 for (uint32_t key = 0; key < 42; key += 7) { |
| 248 check(key); |
| 249 } |
| 250 |
| 251 // Some pseudo-random numbers |
| 252 static const uint32_t kLimit = 1000; |
| 253 for (uint32_t i = 0; i < 5; i++) { |
| 254 for (uint32_t j = 0; j < 5; j++) { |
| 255 check(PseudoRandom(i, j) % kLimit); |
| 256 } |
| 257 } |
| 258 } |
| 259 |
172 #undef __ | 260 #undef __ |
OLD | NEW |