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
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.
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.
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/simulator.h"
38 #include "test/cctest/cctest.h"
39 #include "test/cctest/test-code-stubs.h"
41 using namespace v8::internal;
45 ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate,
47 Register destination_reg,
48 bool inline_fastpath) {
49 // Allocate an executable page of memory.
51 byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
52 Assembler::kMinimalBufferSize, &actual_size, true));
54 HandleScope handles(isolate);
55 MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size));
56 DoubleToIStub stub(isolate, source_reg, destination_reg, 0, true,
59 byte* start = stub.GetCode()->instruction_start();
62 // Save callee save registers.
63 __ MultiPush(kCalleeSaved | ra.bit());
65 // For softfp, move the input value into f12.
66 if (IsMipsSoftFloatABI) {
69 // Push the double argument.
70 __ Dsubu(sp, sp, Operand(kDoubleSize));
71 __ sdc1(f12, MemOperand(sp));
72 __ Move(source_reg, sp);
74 // Save registers make sure they don't get clobbered.
75 int source_reg_offset = kDoubleSize;
77 for (;reg_num < Register::NumAllocatableRegisters(); ++reg_num) {
78 Register reg = Register::from_code(reg_num);
79 if (!reg.is(destination_reg)) {
81 source_reg_offset += kPointerSize;
85 // Re-push the double argument.
86 __ Dsubu(sp, sp, Operand(kDoubleSize));
87 __ sdc1(f12, MemOperand(sp));
89 // Call through to the actual stub
90 if (inline_fastpath) {
91 __ ldc1(f12, MemOperand(source_reg));
92 __ TryInlineTruncateDoubleToI(destination_reg, f12, &done);
93 if (destination_reg.is(source_reg) && !source_reg.is(sp)) {
94 // Restore clobbered source_reg.
95 __ Daddu(source_reg, sp, Operand(source_reg_offset));
98 __ Call(start, RelocInfo::EXTERNAL_REFERENCE);
101 __ Daddu(sp, sp, Operand(kDoubleSize));
103 // Make sure no registers have been unexpectedly clobbered
104 for (--reg_num; reg_num >= 2; --reg_num) {
105 Register reg = Register::from_code(reg_num);
106 if (!reg.is(destination_reg)) {
107 __ lw(at, MemOperand(sp, 0));
108 __ Assert(eq, kRegisterWasClobbered, reg, Operand(at));
109 __ Daddu(sp, sp, Operand(kPointerSize));
113 __ Daddu(sp, sp, Operand(kDoubleSize));
115 __ Move(v0, destination_reg);
117 __ Branch(&ok, eq, v0, Operand(zero_reg));
120 // Restore callee save registers.
121 __ MultiPop(kCalleeSaved | ra.bit());
124 __ Branch(&ok1, eq, v0, Operand(zero_reg));
130 CpuFeatures::FlushICache(buffer, actual_size);
131 return (reinterpret_cast<ConvertDToIFunc>(
132 reinterpret_cast<intptr_t>(buffer)));
138 static Isolate* GetIsolateFrom(LocalContext* context) {
139 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
143 int32_t RunGeneratedCodeCallWrapper(ConvertDToIFunc func,
146 Simulator::current(Isolate::Current())->CallFP(FUNCTION_ADDR(func), from, 0.);
147 return Simulator::current(Isolate::Current())->get_register(v0.code());
149 return (*func)(from);
155 CcTest::InitializeVM();
156 LocalContext context;
157 Isolate* isolate = GetIsolateFrom(&context);
158 HandleScope scope(isolate);
161 // Verify that the tests actually work with the C version. In the release
162 // code, the compiler optimizes it away because it's all constant, but does it
163 // wrong, triggering an assert on gcc.
164 RunAllTruncationTests(&ConvertDToICVersion);
167 Register source_registers[] = {
168 sp, v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
169 Register dest_registers[] = {
170 v0, v1, a0, a1, a2, a3, a4, a5, a6, a7, t0, t1};
172 for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
173 for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
174 RunAllTruncationTests(
175 RunGeneratedCodeCallWrapper,
176 MakeConvertDToIFuncTrampoline(isolate,
180 RunAllTruncationTests(
181 RunGeneratedCodeCallWrapper,
182 MakeConvertDToIFuncTrampoline(isolate,