blob: d6f055e3219091d0c814f093d10b6bd1d41b62c8 [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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
Ben Murdoch692be652012-01-10 18:47:50 +000031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#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 Murdoch692be652012-01-10 18:47:50 +000037
38#ifdef USE_SIMULATOR
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039#include "src/simulator.h"
Ben Murdoch692be652012-01-10 18:47:50 +000040#endif
41
42using namespace v8::internal;
43
44
45typedef uint32_t (*HASH_FUNCTION)();
46
Ben Murdochc7cc0282012-03-05 14:35:55 +000047#define __ masm->
Ben Murdoch692be652012-01-10 18:47:50 +000048
49
Ben Murdochc7cc0282012-03-05 14:35:55 +000050void generate(MacroAssembler* masm, uint32_t key) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
Ben Murdochc7cc0282012-03-05 14:35:55 +000052 __ push(ebx);
53 __ mov(eax, Immediate(key));
54 __ GetNumberHash(eax, ebx);
55 __ pop(ebx);
56 __ Ret();
57#elif V8_TARGET_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 __ pushq(kRootRegister);
Ben Murdochc7cc0282012-03-05 14:35:55 +000059 __ InitializeRootRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 __ pushq(rbx);
61 __ movp(rax, Immediate(key));
Ben Murdochc7cc0282012-03-05 14:35:55 +000062 __ GetNumberHash(rax, rbx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 __ popq(rbx);
64 __ popq(kRootRegister);
Ben Murdochc7cc0282012-03-05 14:35:55 +000065 __ 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 Murdochb8a8cc12014-11-26 15:28:44 +000073#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 Murdochc7cc0282012-03-05 14:35:55 +000086 __ push(kRootRegister);
87 __ InitializeRootRegister();
88 __ li(v0, Operand(key));
89 __ GetNumberHash(v0, t1);
90 __ pop(kRootRegister);
91 __ jr(ra);
92 __ nop();
Ben Murdochda12d292016-06-02 14:46:10 +010093#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 Murdoch4a90d5f2016-03-22 12:00:34 +0000102#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 Murdoch692be652012-01-10 18:47:50 +0000110#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111#error Unsupported architecture.
Ben Murdoch692be652012-01-10 18:47:50 +0000112#endif
Ben Murdoch692be652012-01-10 18:47:50 +0000113}
114
115
Ben Murdochc7cc0282012-03-05 14:35:55 +0000116void check(uint32_t key) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 Isolate* isolate = CcTest::i_isolate();
118 Factory* factory = isolate->factory();
119 HandleScope scope(isolate);
120
Ben Murdochc7cc0282012-03-05 14:35:55 +0000121 v8::internal::byte buffer[2048];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 MacroAssembler masm(CcTest::i_isolate(), buffer, sizeof(buffer),
123 v8::internal::CodeObjectRequired::kYes);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000124
125 generate(&masm, key);
126
127 CodeDesc desc;
128 masm.GetCode(&desc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
130 Handle<Code> code = factory->NewCode(desc,
131 Code::ComputeFlags(Code::STUB),
132 undefined);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000133 CHECK(code->IsCode());
134
135 HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
136#ifdef USE_SIMULATOR
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 uint32_t codegen_hash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
138 CALL_GENERATED_CODE(isolate, hash, 0, 0, 0, 0, 0)));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000139#else
140 uint32_t codegen_hash = hash();
141#endif
142
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 CHECK_EQ(runtime_hash, codegen_hash);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000145}
146
147
Ben Murdochc7cc0282012-03-05 14:35:55 +0000148static uint32_t PseudoRandom(uint32_t i, uint32_t j) {
149 return ~(~((i * 781) ^ (j * 329)));
150}
151
152
Ben Murdochc7cc0282012-03-05 14:35:55 +0000153TEST(NumberHash) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 v8::Isolate* isolate = CcTest::isolate();
155 v8::HandleScope handle_scope(isolate);
156 v8::Context::Scope context_scope(v8::Context::New(isolate));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000157
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 Murdoch692be652012-01-10 18:47:50 +0000172#undef __