danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 1 | // 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 | |
| 42 | using namespace v8::internal; |
| 43 | |
| 44 | |
| 45 | typedef uint32_t (*HASH_FUNCTION)(); |
| 46 | |
| 47 | static v8::Persistent<v8::Context> env; |
| 48 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 49 | #define __ masm-> |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 50 | |
| 51 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 52 | void generate(MacroAssembler* masm, i::Vector<const uint8_t> string) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 53 | // GenerateHashInit takes the first character as an argument so it can't |
| 54 | // handle the zero length string. |
| 55 | ASSERT(string.length() > 0); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 56 | #ifdef V8_TARGET_ARCH_IA32 |
| 57 | __ push(ebx); |
| 58 | __ push(ecx); |
| 59 | __ mov(eax, Immediate(0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 60 | __ mov(ebx, Immediate(string.at(0))); |
| 61 | StringHelper::GenerateHashInit(masm, eax, ebx, ecx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 62 | for (int i = 1; i < string.length(); i++) { |
| 63 | __ mov(ebx, Immediate(string.at(i))); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 64 | StringHelper::GenerateHashAddCharacter(masm, eax, ebx, ecx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 65 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 66 | StringHelper::GenerateHashGetHash(masm, eax, ecx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 67 | __ pop(ecx); |
| 68 | __ pop(ebx); |
| 69 | __ Ret(); |
| 70 | #elif V8_TARGET_ARCH_X64 |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 71 | __ push(kRootRegister); |
| 72 | __ InitializeRootRegister(); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 73 | __ push(rbx); |
| 74 | __ push(rcx); |
| 75 | __ movq(rax, Immediate(0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 76 | __ movq(rbx, Immediate(string.at(0))); |
| 77 | StringHelper::GenerateHashInit(masm, rax, rbx, rcx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 78 | for (int i = 1; i < string.length(); i++) { |
| 79 | __ movq(rbx, Immediate(string.at(i))); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 80 | StringHelper::GenerateHashAddCharacter(masm, rax, rbx, rcx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 81 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 82 | StringHelper::GenerateHashGetHash(masm, rax, rcx); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 83 | __ pop(rcx); |
| 84 | __ pop(rbx); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 85 | __ pop(kRootRegister); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 86 | __ Ret(); |
| 87 | #elif V8_TARGET_ARCH_ARM |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 88 | __ push(kRootRegister); |
| 89 | __ InitializeRootRegister(); |
| 90 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 91 | __ mov(r0, Operand(0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 92 | __ mov(ip, Operand(string.at(0))); |
| 93 | StringHelper::GenerateHashInit(masm, r0, ip); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 94 | for (int i = 1; i < string.length(); i++) { |
| 95 | __ mov(ip, Operand(string.at(i))); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 96 | StringHelper::GenerateHashAddCharacter(masm, r0, ip); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 97 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 98 | StringHelper::GenerateHashGetHash(masm, r0); |
| 99 | __ pop(kRootRegister); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 100 | __ mov(pc, Operand(lr)); |
| 101 | #elif V8_TARGET_ARCH_MIPS |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 102 | __ push(kRootRegister); |
| 103 | __ InitializeRootRegister(); |
| 104 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 105 | __ li(v0, Operand(0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 106 | __ li(t1, Operand(string.at(0))); |
| 107 | StringHelper::GenerateHashInit(masm, v0, t1); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 108 | for (int i = 1; i < string.length(); i++) { |
| 109 | __ li(t1, Operand(string.at(i))); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 110 | StringHelper::GenerateHashAddCharacter(masm, v0, t1); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 111 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 112 | StringHelper::GenerateHashGetHash(masm, v0); |
| 113 | __ pop(kRootRegister); |
| 114 | __ jr(ra); |
| 115 | __ nop(); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 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); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 149 | __ jr(ra); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 150 | __ nop(); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 151 | #endif |
| 152 | } |
| 153 | |
| 154 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 155 | void check(i::Vector<const uint8_t> string) { |
| 156 | Isolate* isolate = Isolate::Current(); |
| 157 | Factory* factory = isolate->factory(); |
| 158 | HandleScope scope(isolate); |
| 159 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 160 | v8::internal::byte buffer[2048]; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 161 | MacroAssembler masm(isolate, buffer, sizeof buffer); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 162 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 163 | generate(&masm, string); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 164 | |
| 165 | CodeDesc desc; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 166 | masm.GetCode(&desc); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 167 | Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 168 | Handle<Code> code = factory->NewCode(desc, |
| 169 | Code::ComputeFlags(Code::STUB), |
| 170 | undefined); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 171 | CHECK(code->IsCode()); |
| 172 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 173 | HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 174 | Handle<String> v8_string = factory->NewStringFromOneByte(string); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 175 | v8_string->set_hash_field(String::kEmptyHashField); |
| 176 | #ifdef USE_SIMULATOR |
| 177 | uint32_t codegen_hash = |
| 178 | reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); |
| 179 | #else |
| 180 | uint32_t codegen_hash = hash(); |
| 181 | #endif |
| 182 | uint32_t runtime_hash = v8_string->Hash(); |
| 183 | CHECK(runtime_hash == codegen_hash); |
| 184 | } |
| 185 | |
| 186 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 187 | void check(i::Vector<const char> s) { |
| 188 | check(i::Vector<const uint8_t>::cast(s)); |
| 189 | } |
| 190 | |
| 191 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 192 | void check(uint32_t key) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 193 | Isolate* isolate = Isolate::Current(); |
| 194 | Factory* factory = isolate->factory(); |
| 195 | HandleScope scope(isolate); |
| 196 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 197 | v8::internal::byte buffer[2048]; |
| 198 | MacroAssembler masm(Isolate::Current(), buffer, sizeof buffer); |
| 199 | |
| 200 | generate(&masm, key); |
| 201 | |
| 202 | CodeDesc desc; |
| 203 | masm.GetCode(&desc); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 204 | Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 205 | Handle<Code> code = factory->NewCode(desc, |
| 206 | Code::ComputeFlags(Code::STUB), |
| 207 | undefined); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 208 | CHECK(code->IsCode()); |
| 209 | |
| 210 | HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); |
| 211 | #ifdef USE_SIMULATOR |
| 212 | uint32_t codegen_hash = |
| 213 | reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); |
| 214 | #else |
| 215 | uint32_t codegen_hash = hash(); |
| 216 | #endif |
| 217 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 218 | uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 219 | CHECK(runtime_hash == codegen_hash); |
| 220 | } |
| 221 | |
| 222 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 223 | void check_twochars(uint8_t a, uint8_t b) { |
| 224 | uint8_t ab[2] = {a, b}; |
| 225 | check(i::Vector<const uint8_t>(ab, 2)); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 229 | static uint32_t PseudoRandom(uint32_t i, uint32_t j) { |
| 230 | return ~(~((i * 781) ^ (j * 329))); |
| 231 | } |
| 232 | |
| 233 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 234 | TEST(StringHash) { |
| 235 | if (env.IsEmpty()) env = v8::Context::New(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 236 | for (uint8_t a = 0; a < String::kMaxOneByteCharCode; a++) { |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 237 | // Numbers are hashed differently. |
| 238 | if (a >= '0' && a <= '9') continue; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 239 | for (uint8_t b = 0; b < String::kMaxOneByteCharCode; b++) { |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 240 | if (b >= '0' && b <= '9') continue; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 241 | check_twochars(a, b); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 244 | check(i::Vector<const char>("*", 1)); |
| 245 | check(i::Vector<const char>(".zZ", 3)); |
| 246 | check(i::Vector<const char>("muc", 3)); |
| 247 | check(i::Vector<const char>("(>'_')>", 7)); |
| 248 | check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21)); |
| 249 | } |
| 250 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 251 | |
| 252 | TEST(NumberHash) { |
| 253 | if (env.IsEmpty()) env = v8::Context::New(); |
| 254 | |
| 255 | // Some specific numbers |
| 256 | for (uint32_t key = 0; key < 42; key += 7) { |
| 257 | check(key); |
| 258 | } |
| 259 | |
| 260 | // Some pseudo-random numbers |
| 261 | static const uint32_t kLimit = 1000; |
| 262 | for (uint32_t i = 0; i < 5; i++) { |
| 263 | for (uint32_t j = 0; j < 5; j++) { |
| 264 | check(PseudoRandom(i, j) % kLimit); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 269 | #undef __ |