OLD | NEW |
(Empty) | |
| 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 |
| 49 #define __ assm-> |
| 50 |
| 51 |
| 52 void generate(MacroAssembler* assm, i::Vector<const char> string) { |
| 53 #ifdef V8_TARGET_ARCH_IA32 |
| 54 __ mov(eax, Immediate(0)); |
| 55 if (string.length() > 0) { |
| 56 __ mov(ebx, Immediate(string.at(0))); |
| 57 StringHelper::GenerateHashInit(assm, eax, ebx, ecx); |
| 58 } |
| 59 for (int i = 1; i < string.length(); i++) { |
| 60 __ mov(ebx, Immediate(string.at(i))); |
| 61 StringHelper::GenerateHashAddCharacter(assm, eax, ebx, ecx); |
| 62 } |
| 63 StringHelper::GenerateHashGetHash(assm, eax, ecx); |
| 64 __ Ret(); |
| 65 #elif V8_TARGET_ARCH_X64 |
| 66 __ movq(rax, Immediate(0)); |
| 67 if (string.length() > 0) { |
| 68 __ movq(rbx, Immediate(string.at(0))); |
| 69 StringHelper::GenerateHashInit(assm, rax, rbx, rcx); |
| 70 } |
| 71 for (int i = 1; i < string.length(); i++) { |
| 72 __ movq(rbx, Immediate(string.at(i))); |
| 73 StringHelper::GenerateHashAddCharacter(assm, rax, rbx, rcx); |
| 74 } |
| 75 StringHelper::GenerateHashGetHash(assm, rax, rcx); |
| 76 __ Ret(); |
| 77 #elif V8_TARGET_ARCH_ARM |
| 78 __ mov(r0, Operand(0)); |
| 79 if (string.length() > 0) { |
| 80 __ mov(r1, Operand(string.at(0))); |
| 81 StringHelper::GenerateHashInit(assm, r0, r1); |
| 82 } |
| 83 for (int i = 1; i < string.length(); i++) { |
| 84 __ mov(r1, Operand(string.at(i))); |
| 85 StringHelper::GenerateHashAddCharacter(assm, r0, r1); |
| 86 } |
| 87 StringHelper::GenerateHashGetHash(assm, r0); |
| 88 __ mov(pc, Operand(lr)); |
| 89 #elif V8_TARGET_ARCH_MIPS |
| 90 __ li(v0, Operand(0)); |
| 91 if (string.length() > 0) { |
| 92 __ li(v1, Operand(string.at(0))); |
| 93 StringHelper::GenerateHashInit(assm, v0, v1); |
| 94 } |
| 95 for (int i = 1; i < string.length(); i++) { |
| 96 __ li(v1, Operand(string.at(i))); |
| 97 StringHelper::GenerateHashAddCharacter(assm, v0, v1); |
| 98 } |
| 99 StringHelper::GenerateHashGetHash(assm, v0); |
| 100 __ jr(ra); |
| 101 #endif |
| 102 } |
| 103 |
| 104 |
| 105 void check(i::Vector<const char> string) { |
| 106 v8::HandleScope scope; |
| 107 v8::internal::byte buffer[2048]; |
| 108 MacroAssembler assm(Isolate::Current(), buffer, sizeof buffer); |
| 109 |
| 110 generate(&assm, string); |
| 111 |
| 112 CodeDesc desc; |
| 113 assm.GetCode(&desc); |
| 114 Code* code = Code::cast(HEAP->CreateCode( |
| 115 desc, |
| 116 Code::ComputeFlags(Code::STUB), |
| 117 Handle<Object>(HEAP->undefined_value()))->ToObjectChecked()); |
| 118 CHECK(code->IsCode()); |
| 119 |
| 120 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(Code::cast(code)->entry()); |
| 121 Handle<String> v8_string = FACTORY->NewStringFromAscii(string); |
| 122 v8_string->set_hash_field(String::kEmptyHashField); |
| 123 #ifdef USE_SIMULATOR |
| 124 uint32_t codegen_hash = |
| 125 reinterpret_cast<uint32_t>(CALL_GENERATED_CODE(hash, 0, 0, 0, 0, 0)); |
| 126 #else |
| 127 uint32_t codegen_hash = hash(); |
| 128 #endif |
| 129 uint32_t runtime_hash = v8_string->Hash(); |
| 130 CHECK(runtime_hash == codegen_hash); |
| 131 } |
| 132 |
| 133 |
| 134 void check_twochars(char a, char b) { |
| 135 char ab[2] = {a, b}; |
| 136 check(i::Vector<const char>(ab, 2)); |
| 137 } |
| 138 |
| 139 |
| 140 TEST(StringHash) { |
| 141 if (env.IsEmpty()) env = v8::Context::New(); |
| 142 for (int a = 0; a < String::kMaxAsciiCharCode; a++) { |
| 143 // Numbers are hashed differently. |
| 144 if (a >= '0' && a <= '9') continue; |
| 145 for (int b = 0; b < String::kMaxAsciiCharCode; b++) { |
| 146 if (b >= '0' && b <= '9') continue; |
| 147 check_twochars(static_cast<char>(a), static_cast<char>(b)); |
| 148 } |
| 149 } |
| 150 check(i::Vector<const char>("", 0)); |
| 151 check(i::Vector<const char>("*", 1)); |
| 152 check(i::Vector<const char>(".zZ", 3)); |
| 153 check(i::Vector<const char>("muc", 3)); |
| 154 check(i::Vector<const char>("(>'_')>", 7)); |
| 155 check(i::Vector<const char>("-=[ vee eight ftw ]=-", 21)); |
| 156 } |
| 157 |
| 158 #undef __ |
OLD | NEW |