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