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
42 using namespace v8::internal;
43
44 #define __ masm.
45
MakeConvertDToIFuncTrampoline(Isolate * isolate,Register source_reg,Register destination_reg,bool inline_fastpath)46 ConvertDToIFunc 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 =
85 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
86 for (; reg_num < config->num_allocatable_general_registers(); ++reg_num) {
87 Register reg = Register::from_code(reg_num);
88 if (!reg.is(destination_reg)) {
89 __ push(reg);
90 source_reg_offset += kPointerSize;
91 }
92 }
93
94 // Re-push the double argument.
95 __ Dsubu(sp, sp, Operand(kDoubleSize));
96 __ sdc1(f12, MemOperand(sp));
97
98 // Call through to the actual stub
99 if (inline_fastpath) {
100 __ ldc1(f12, MemOperand(source_reg));
101 __ TryInlineTruncateDoubleToI(destination_reg, f12, &done);
102 if (destination_reg.is(source_reg) && !source_reg.is(sp)) {
103 // Restore clobbered source_reg.
104 __ Daddu(source_reg, sp, Operand(source_reg_offset));
105 }
106 }
107 __ Call(start, RelocInfo::EXTERNAL_REFERENCE);
108 __ bind(&done);
109
110 __ Daddu(sp, sp, Operand(kDoubleSize));
111
112 // Make sure no registers have been unexpectedly clobbered
113 for (--reg_num; reg_num >= 2; --reg_num) {
114 Register reg = Register::from_code(reg_num);
115 if (!reg.is(destination_reg)) {
116 __ ld(at, MemOperand(sp, 0));
117 __ Assert(eq, kRegisterWasClobbered, reg, Operand(at));
118 __ Daddu(sp, sp, Operand(kPointerSize));
119 }
120 }
121
122 __ Daddu(sp, sp, Operand(kDoubleSize));
123
124 __ Move(v0, destination_reg);
125 Label ok;
126 __ Branch(&ok, eq, v0, Operand(zero_reg));
127 __ bind(&ok);
128
129 // Restore callee-saved FPU registers.
130 __ MultiPopFPU(kCalleeSavedFPU);
131
132 // Restore callee save registers.
133 __ MultiPop(kCalleeSaved | ra.bit());
134
135 Label ok1;
136 __ Branch(&ok1, eq, v0, Operand(zero_reg));
137 __ bind(&ok1);
138 __ Ret();
139
140 CodeDesc desc;
141 masm.GetCode(&desc);
142 Assembler::FlushICache(isolate, buffer, actual_size);
143 return (reinterpret_cast<ConvertDToIFunc>(
144 reinterpret_cast<intptr_t>(buffer)));
145 }
146
147 #undef __
148
149
GetIsolateFrom(LocalContext * context)150 static Isolate* GetIsolateFrom(LocalContext* context) {
151 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
152 }
153
154
RunGeneratedCodeCallWrapper(ConvertDToIFunc func,double from)155 int32_t RunGeneratedCodeCallWrapper(ConvertDToIFunc func,
156 double from) {
157 #ifdef USE_SIMULATOR
158 Simulator::current(CcTest::i_isolate())
159 ->CallFP(FUNCTION_ADDR(func), from, 0.);
160 return static_cast<int32_t>(
161 Simulator::current(CcTest::i_isolate())->get_register(v0.code()));
162 #else
163 return (*func)(from);
164 #endif
165 }
166
167
TEST(ConvertDToI)168 TEST(ConvertDToI) {
169 CcTest::InitializeVM();
170 LocalContext context;
171 Isolate* isolate = GetIsolateFrom(&context);
172 HandleScope scope(isolate);
173
174 #if DEBUG
175 // Verify that the tests actually work with the C version. In the release
176 // code, the compiler optimizes it away because it's all constant, but does it
177 // wrong, triggering an assert on gcc.
178 RunAllTruncationTests(&ConvertDToICVersion);
179 #endif
180
181 Register source_registers[] = {
182 sp, v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
183 Register dest_registers[] = {
184 v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
185
186 for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
187 for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
188 RunAllTruncationTests(
189 RunGeneratedCodeCallWrapper,
190 MakeConvertDToIFuncTrampoline(isolate,
191 source_registers[s],
192 dest_registers[d],
193 false));
194 RunAllTruncationTests(
195 RunGeneratedCodeCallWrapper,
196 MakeConvertDToIFuncTrampoline(isolate,
197 source_registers[s],
198 dest_registers[d],
199 true));
200 }
201 }
202 }
203