1// Copyright 2013 the V8 project authors. All rights reserved.
2// Rrdistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Rrdistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Rrdistributions 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/base/platform/platform.h"
33#include "src/code-stubs.h"
34#include "src/factory.h"
35#include "src/macro-assembler.h"
36#include "src/mips64/constants-mips64.h"
37#include "src/register-configuration.h"
38#include "src/simulator.h"
39#include "test/cctest/cctest.h"
40#include "test/cctest/test-code-stubs.h"
41
42using namespace v8::internal;
43
44#define __ masm.
45
46ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate,
47                                              Register source_reg,
48                                              Register destination_reg,
49                                              bool inline_fastpath) {
50  // Allocate an executable page of memory.
51  size_t actual_size;
52  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
53      Assembler::kMinimalBufferSize, &actual_size, true));
54  CHECK(buffer);
55  HandleScope handles(isolate);
56  MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size),
57                      v8::internal::CodeObjectRequired::kYes);
58  DoubleToIStub stub(isolate, source_reg, destination_reg, 0, true,
59                     inline_fastpath);
60
61  byte* start = stub.GetCode()->instruction_start();
62  Label done;
63
64  // Save callee save registers.
65  __ MultiPush(kCalleeSaved | ra.bit());
66
67  // Save callee-saved FPU registers.
68  __ MultiPushFPU(kCalleeSavedFPU);
69  // Set up the reserved register for 0.0.
70  __ Move(kDoubleRegZero, 0.0);
71
72  // For softfp, move the input value into f12.
73  if (IsMipsSoftFloatABI) {
74    __ Move(f12, a0, a1);
75  }
76  // Push the double argument.
77  __ Dsubu(sp, sp, Operand(kDoubleSize));
78  __ sdc1(f12, MemOperand(sp));
79  __ Move(source_reg, sp);
80
81  // Save registers make sure they don't get clobbered.
82  int source_reg_offset = kDoubleSize;
83  int reg_num = 2;
84  const RegisterConfiguration* config = RegisterConfiguration::Crankshaft();
85  for (; reg_num < config->num_allocatable_general_registers(); ++reg_num) {
86    Register reg = Register::from_code(reg_num);
87    if (!reg.is(destination_reg)) {
88      __ push(reg);
89      source_reg_offset += kPointerSize;
90    }
91  }
92
93  // Re-push the double argument.
94  __ Dsubu(sp, sp, Operand(kDoubleSize));
95  __ sdc1(f12, MemOperand(sp));
96
97  // Call through to the actual stub
98  if (inline_fastpath) {
99    __ ldc1(f12, MemOperand(source_reg));
100    __ TryInlineTruncateDoubleToI(destination_reg, f12, &done);
101    if (destination_reg.is(source_reg) && !source_reg.is(sp)) {
102      // Restore clobbered source_reg.
103      __ Daddu(source_reg, sp, Operand(source_reg_offset));
104    }
105  }
106  __ Call(start, RelocInfo::EXTERNAL_REFERENCE);
107  __ bind(&done);
108
109  __ Daddu(sp, sp, Operand(kDoubleSize));
110
111  // Make sure no registers have been unexpectedly clobbered
112  for (--reg_num; reg_num >= 2; --reg_num) {
113    Register reg = Register::from_code(reg_num);
114    if (!reg.is(destination_reg)) {
115      __ ld(at, MemOperand(sp, 0));
116      __ Assert(eq, kRegisterWasClobbered, reg, Operand(at));
117      __ Daddu(sp, sp, Operand(kPointerSize));
118    }
119  }
120
121  __ Daddu(sp, sp, Operand(kDoubleSize));
122
123  __ Move(v0, destination_reg);
124  Label ok;
125  __ Branch(&ok, eq, v0, Operand(zero_reg));
126  __ bind(&ok);
127
128  // Restore callee-saved FPU registers.
129  __ MultiPopFPU(kCalleeSavedFPU);
130
131  // Restore callee save registers.
132  __ MultiPop(kCalleeSaved | ra.bit());
133
134  Label ok1;
135  __ Branch(&ok1, eq, v0, Operand(zero_reg));
136  __ bind(&ok1);
137  __ Ret();
138
139  CodeDesc desc;
140  masm.GetCode(&desc);
141  Assembler::FlushICache(isolate, buffer, actual_size);
142  return (reinterpret_cast<ConvertDToIFunc>(
143      reinterpret_cast<intptr_t>(buffer)));
144}
145
146#undef __
147
148
149static Isolate* GetIsolateFrom(LocalContext* context) {
150  return reinterpret_cast<Isolate*>((*context)->GetIsolate());
151}
152
153
154int32_t RunGeneratedCodeCallWrapper(ConvertDToIFunc func,
155                                    double from) {
156#ifdef USE_SIMULATOR
157  Simulator::current(CcTest::i_isolate())
158      ->CallFP(FUNCTION_ADDR(func), from, 0.);
159  return static_cast<int32_t>(
160      Simulator::current(CcTest::i_isolate())->get_register(v0.code()));
161#else
162  return (*func)(from);
163#endif
164}
165
166
167TEST(ConvertDToI) {
168  CcTest::InitializeVM();
169  LocalContext context;
170  Isolate* isolate = GetIsolateFrom(&context);
171  HandleScope scope(isolate);
172
173#if DEBUG
174  // Verify that the tests actually work with the C version. In the release
175  // code, the compiler optimizes it away because it's all constant, but does it
176  // wrong, triggering an assert on gcc.
177  RunAllTruncationTests(&ConvertDToICVersion);
178#endif
179
180  Register source_registers[] = {
181      sp, v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
182  Register dest_registers[] = {
183      v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
184
185  for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
186    for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
187      RunAllTruncationTests(
188          RunGeneratedCodeCallWrapper,
189          MakeConvertDToIFuncTrampoline(isolate,
190                                        source_registers[s],
191                                        dest_registers[d],
192                                        false));
193      RunAllTruncationTests(
194          RunGeneratedCodeCallWrapper,
195          MakeConvertDToIFuncTrampoline(isolate,
196                                        source_registers[s],
197                                        dest_registers[d],
198                                        true));
199    }
200  }
201}
202