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 "src/v8.h"
31
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"
37
38#ifdef USE_SIMULATOR
39#include "src/simulator.h"
40#endif
41
42using namespace v8::internal;
43
44
45typedef uint32_t (*HASH_FUNCTION)();
46
47#define __ masm->
48
49
50void generate(MacroAssembler* masm, uint32_t key) {
51#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
52  __ push(ebx);
53  __ mov(eax, Immediate(key));
54  __ GetNumberHash(eax, ebx);
55  __ pop(ebx);
56  __ Ret();
57#elif V8_TARGET_ARCH_X64
58  __ pushq(kRootRegister);
59  __ InitializeRootRegister();
60  __ pushq(rbx);
61  __ movp(rax, Immediate(key));
62  __ GetNumberHash(rax, rbx);
63  __ popq(rbx);
64  __ popq(kRootRegister);
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));
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
86  __ push(kRootRegister);
87  __ InitializeRootRegister();
88  __ li(v0, Operand(key));
89  __ GetNumberHash(v0, t1);
90  __ pop(kRootRegister);
91  __ jr(ra);
92  __ nop();
93#elif V8_TARGET_ARCH_PPC
94  __ function_descriptor();
95  __ push(kRootRegister);
96  __ InitializeRootRegister();
97  __ li(r3, Operand(key));
98  __ GetNumberHash(r3, ip);
99  __ pop(kRootRegister);
100  __ blr();
101#else
102#error Unsupported architecture.
103#endif
104}
105
106
107void check(uint32_t key) {
108  Isolate* isolate = CcTest::i_isolate();
109  Factory* factory = isolate->factory();
110  HandleScope scope(isolate);
111
112  v8::internal::byte buffer[2048];
113  MacroAssembler masm(CcTest::i_isolate(), buffer, sizeof(buffer),
114                      v8::internal::CodeObjectRequired::kYes);
115
116  generate(&masm, key);
117
118  CodeDesc desc;
119  masm.GetCode(&desc);
120  Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
121  Handle<Code> code = factory->NewCode(desc,
122                                       Code::ComputeFlags(Code::STUB),
123                                       undefined);
124  CHECK(code->IsCode());
125
126  HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
127#ifdef USE_SIMULATOR
128  uint32_t codegen_hash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
129      CALL_GENERATED_CODE(isolate, hash, 0, 0, 0, 0, 0)));
130#else
131  uint32_t codegen_hash = hash();
132#endif
133
134  uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed());
135  CHECK_EQ(runtime_hash, codegen_hash);
136}
137
138
139static uint32_t PseudoRandom(uint32_t i, uint32_t j) {
140  return ~(~((i * 781) ^ (j * 329)));
141}
142
143
144TEST(NumberHash) {
145  v8::Isolate* isolate = CcTest::isolate();
146  v8::HandleScope handle_scope(isolate);
147  v8::Context::Scope context_scope(v8::Context::New(isolate));
148
149  // Some specific numbers
150  for (uint32_t key = 0; key < 42; key += 7) {
151    check(key);
152  }
153
154  // Some pseudo-random numbers
155  static const uint32_t kLimit = 1000;
156  for (uint32_t i = 0; i < 5; i++) {
157    for (uint32_t j = 0; j < 5; j++) {
158      check(PseudoRandom(i, j) % kLimit);
159    }
160  }
161}
162
163#undef __
164