Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | #include "src/register-configuration.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
| 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); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size), |
| 57 | v8::internal::CodeObjectRequired::kYes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 67 | // Save callee-saved FPU registers. |
| 68 | __ MultiPushFPU(kCalleeSavedFPU); |
| 69 | // Set up the reserved register for 0.0. |
| 70 | __ Move(kDoubleRegZero, 0.0); |
| 71 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 84 | for (; reg_num < Register::kNumRegisters; ++reg_num) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 85 | if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode( |
| 86 | reg_num)) { |
| 87 | Register reg = Register::from_code(reg_num); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | if (!reg.is(destination_reg)) { |
| 89 | __ push(reg); |
| 90 | source_reg_offset += kPointerSize; |
| 91 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | // Re-push the double argument. |
| 96 | __ Subu(sp, sp, Operand(kDoubleSize)); |
| 97 | __ sdc1(f12, MemOperand(sp)); |
| 98 | |
| 99 | // Call through to the actual stub |
| 100 | if (inline_fastpath) { |
| 101 | __ ldc1(f12, MemOperand(source_reg)); |
| 102 | __ TryInlineTruncateDoubleToI(destination_reg, f12, &done); |
| 103 | if (destination_reg.is(source_reg) && !source_reg.is(sp)) { |
| 104 | // Restore clobbered source_reg. |
| 105 | __ Addu(source_reg, sp, Operand(source_reg_offset)); |
| 106 | } |
| 107 | } |
| 108 | __ Call(start, RelocInfo::EXTERNAL_REFERENCE); |
| 109 | __ bind(&done); |
| 110 | |
| 111 | __ Addu(sp, sp, Operand(kDoubleSize)); |
| 112 | |
| 113 | // Make sure no registers have been unexpectedly clobbered |
| 114 | for (--reg_num; reg_num >= 2; --reg_num) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 115 | if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode( |
| 116 | reg_num)) { |
| 117 | Register reg = Register::from_code(reg_num); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | if (!reg.is(destination_reg)) { |
| 119 | __ lw(at, MemOperand(sp, 0)); |
| 120 | __ Assert(eq, kRegisterWasClobbered, reg, Operand(at)); |
| 121 | __ Addu(sp, sp, Operand(kPointerSize)); |
| 122 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | __ Addu(sp, sp, Operand(kDoubleSize)); |
| 127 | |
| 128 | __ Move(v0, destination_reg); |
| 129 | Label ok; |
| 130 | __ Branch(&ok, eq, v0, Operand(zero_reg)); |
| 131 | __ bind(&ok); |
| 132 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | // Restore callee-saved FPU registers. |
| 134 | __ MultiPopFPU(kCalleeSavedFPU); |
| 135 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | // Restore callee save registers. |
| 137 | __ MultiPop(kCalleeSaved | ra.bit()); |
| 138 | |
| 139 | Label ok1; |
| 140 | __ Branch(&ok1, eq, v0, Operand(zero_reg)); |
| 141 | __ bind(&ok1); |
| 142 | __ Ret(); |
| 143 | |
| 144 | CodeDesc desc; |
| 145 | masm.GetCode(&desc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | Assembler::FlushICache(isolate, buffer, actual_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | return (reinterpret_cast<ConvertDToIFunc>( |
| 148 | reinterpret_cast<intptr_t>(buffer))); |
| 149 | } |
| 150 | |
| 151 | #undef __ |
| 152 | |
| 153 | |
| 154 | static Isolate* GetIsolateFrom(LocalContext* context) { |
| 155 | return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | int32_t RunGeneratedCodeCallWrapper(ConvertDToIFunc func, |
| 160 | double from) { |
| 161 | #ifdef USE_SIMULATOR |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | Simulator::current(CcTest::i_isolate()) |
| 163 | ->CallFP(FUNCTION_ADDR(func), from, 0.); |
| 164 | return Simulator::current(CcTest::i_isolate())->get_register(v0.code()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 165 | #else |
| 166 | return (*func)(from); |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | |
| 171 | TEST(ConvertDToI) { |
| 172 | CcTest::InitializeVM(); |
| 173 | LocalContext context; |
| 174 | Isolate* isolate = GetIsolateFrom(&context); |
| 175 | HandleScope scope(isolate); |
| 176 | |
| 177 | #if DEBUG |
| 178 | // Verify that the tests actually work with the C version. In the release |
| 179 | // code, the compiler optimizes it away because it's all constant, but does it |
| 180 | // wrong, triggering an assert on gcc. |
| 181 | RunAllTruncationTests(&ConvertDToICVersion); |
| 182 | #endif |
| 183 | |
| 184 | Register source_registers[] = { |
| 185 | sp, v0, v1, a0, a1, a2, a3, t0, t1, t2, t3, t4, t5}; |
| 186 | Register dest_registers[] = { |
| 187 | v0, v1, a0, a1, a2, a3, t0, t1, t2, t3, t4, t5}; |
| 188 | |
| 189 | for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) { |
| 190 | for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) { |
| 191 | RunAllTruncationTests( |
| 192 | RunGeneratedCodeCallWrapper, |
| 193 | MakeConvertDToIFuncTrampoline(isolate, |
| 194 | source_registers[s], |
| 195 | dest_registers[d], |
| 196 | false)); |
| 197 | RunAllTruncationTests( |
| 198 | RunGeneratedCodeCallWrapper, |
| 199 | MakeConvertDToIFuncTrampoline(isolate, |
| 200 | source_registers[s], |
| 201 | dest_registers[d], |
| 202 | true)); |
| 203 | } |
| 204 | } |
| 205 | } |