Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "src/code-stubs.h" |
| 33 | #include "src/factory.h" |
| 34 | #include "src/macro-assembler.h" |
| 35 | #include "src/objects.h" |
| 36 | #include "test/cctest/cctest.h" |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 37 | |
| 38 | #ifdef USE_SIMULATOR |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | #include "src/simulator.h" |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | using namespace v8::internal; |
| 43 | |
| 44 | |
| 45 | typedef uint32_t (*HASH_FUNCTION)(); |
| 46 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 47 | #define __ masm-> |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 48 | |
| 49 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 50 | void generate(MacroAssembler* masm, uint32_t key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87 |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 52 | __ push(ebx); |
| 53 | __ mov(eax, Immediate(key)); |
| 54 | __ GetNumberHash(eax, ebx); |
| 55 | __ pop(ebx); |
| 56 | __ Ret(); |
| 57 | #elif V8_TARGET_ARCH_X64 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | __ pushq(kRootRegister); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 59 | __ InitializeRootRegister(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | __ pushq(rbx); |
| 61 | __ movp(rax, Immediate(key)); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 62 | __ GetNumberHash(rax, rbx); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | __ popq(rbx); |
| 64 | __ popq(kRootRegister); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 65 | __ Ret(); |
| 66 | #elif V8_TARGET_ARCH_ARM |
| 67 | __ push(kRootRegister); |
| 68 | __ InitializeRootRegister(); |
| 69 | __ mov(r0, Operand(key)); |
| 70 | __ GetNumberHash(r0, ip); |
| 71 | __ pop(kRootRegister); |
| 72 | __ mov(pc, Operand(lr)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | #elif V8_TARGET_ARCH_ARM64 |
| 74 | // The ARM64 assembler usually uses jssp (x28) as a stack pointer, but only |
| 75 | // csp is initialized by the calling (C++) code. |
| 76 | Register old_stack_pointer = __ StackPointer(); |
| 77 | __ SetStackPointer(csp); |
| 78 | __ Push(root, xzr); |
| 79 | __ InitializeRootRegister(); |
| 80 | __ Mov(x0, key); |
| 81 | __ GetNumberHash(x0, x10); |
| 82 | __ Pop(xzr, root); |
| 83 | __ Ret(); |
| 84 | __ SetStackPointer(old_stack_pointer); |
| 85 | #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 86 | __ push(kRootRegister); |
| 87 | __ InitializeRootRegister(); |
| 88 | __ li(v0, Operand(key)); |
| 89 | __ GetNumberHash(v0, t1); |
| 90 | __ pop(kRootRegister); |
| 91 | __ jr(ra); |
| 92 | __ nop(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 93 | #elif V8_TARGET_ARCH_S390 |
| 94 | __ push(kRootRegister); |
| 95 | __ push(ip); |
| 96 | __ InitializeRootRegister(); |
| 97 | __ lhi(r2, Operand(key)); |
| 98 | __ GetNumberHash(r2, ip); |
| 99 | __ pop(ip); |
| 100 | __ pop(kRootRegister); |
| 101 | __ Ret(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | #elif V8_TARGET_ARCH_PPC |
| 103 | __ function_descriptor(); |
| 104 | __ push(kRootRegister); |
| 105 | __ InitializeRootRegister(); |
| 106 | __ li(r3, Operand(key)); |
| 107 | __ GetNumberHash(r3, ip); |
| 108 | __ pop(kRootRegister); |
| 109 | __ blr(); |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 110 | #else |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | #error Unsupported architecture. |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 112 | #endif |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 116 | void check(uint32_t key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 117 | Isolate* isolate = CcTest::i_isolate(); |
| 118 | Factory* factory = isolate->factory(); |
| 119 | HandleScope scope(isolate); |
| 120 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 121 | v8::internal::byte buffer[2048]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | MacroAssembler masm(CcTest::i_isolate(), buffer, sizeof(buffer), |
| 123 | v8::internal::CodeObjectRequired::kYes); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 124 | |
| 125 | generate(&masm, key); |
| 126 | |
| 127 | CodeDesc desc; |
| 128 | masm.GetCode(&desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 130 | Handle<Code> code = factory->NewCode(desc, |
| 131 | Code::ComputeFlags(Code::STUB), |
| 132 | undefined); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 133 | CHECK(code->IsCode()); |
| 134 | |
| 135 | HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry()); |
| 136 | #ifdef USE_SIMULATOR |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 137 | uint32_t codegen_hash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 138 | CALL_GENERATED_CODE(isolate, hash, 0, 0, 0, 0, 0))); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 139 | #else |
| 140 | uint32_t codegen_hash = hash(); |
| 141 | #endif |
| 142 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 144 | CHECK_EQ(runtime_hash, codegen_hash); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 148 | static uint32_t PseudoRandom(uint32_t i, uint32_t j) { |
| 149 | return ~(~((i * 781) ^ (j * 329))); |
| 150 | } |
| 151 | |
| 152 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 153 | TEST(NumberHash) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | v8::Isolate* isolate = CcTest::isolate(); |
| 155 | v8::HandleScope handle_scope(isolate); |
| 156 | v8::Context::Scope context_scope(v8::Context::New(isolate)); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 157 | |
| 158 | // Some specific numbers |
| 159 | for (uint32_t key = 0; key < 42; key += 7) { |
| 160 | check(key); |
| 161 | } |
| 162 | |
| 163 | // Some pseudo-random numbers |
| 164 | static const uint32_t kLimit = 1000; |
| 165 | for (uint32_t i = 0; i < 5; i++) { |
| 166 | for (uint32_t j = 0; j < 5; j++) { |
| 167 | check(PseudoRandom(i, j) % kLimit); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame] | 172 | #undef __ |