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/code-stubs.h" 33 #include "src/factory.h" 34 #include "src/macro-assembler.h" 35 #include "src/platform.h" 36 #include "test/cctest/cctest.h" 37 #include "test/cctest/test-code-stubs.h" 38 39 using namespace v8::internal; 40 41 42 #define __ assm. 43 44 ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate, 45 Register source_reg, 46 Register destination_reg) { 47 // Allocate an executable page of memory. 48 size_t actual_size; 49 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, 50 &actual_size, 51 true)); 52 CHECK(buffer); 53 HandleScope handles(isolate); 54 MacroAssembler assm(isolate, buffer, static_cast<int>(actual_size)); 55 int offset = 56 source_reg.is(rsp) ? 0 : (HeapNumber::kValueOffset - kSmiTagSize); 57 DoubleToIStub stub(isolate, source_reg, destination_reg, offset, true); 58 byte* start = stub.GetCode()->instruction_start(); 59 60 __ pushq(rbx); 61 __ pushq(rcx); 62 __ pushq(rdx); 63 __ pushq(rsi); 64 __ pushq(rdi); 65 66 if (!source_reg.is(rsp)) { 67 // The argument we pass to the stub is not a heap number, but instead 68 // stack-allocated and offset-wise made to look like a heap number for 69 // the stub. We create that "heap number" after pushing all allocatable 70 // registers. 71 int double_argument_slot = 72 (Register::NumAllocatableRegisters() - 1) * kPointerSize + kDoubleSize; 73 __ leaq(source_reg, MemOperand(rsp, -double_argument_slot - offset)); 74 } 75 76 // Save registers make sure they don't get clobbered. 77 int reg_num = 0; 78 for (;reg_num < Register::NumAllocatableRegisters(); ++reg_num) { 79 Register reg = Register::FromAllocationIndex(reg_num); 80 if (!reg.is(rsp) && !reg.is(rbp) && !reg.is(destination_reg)) { 81 __ pushq(reg); 82 } 83 } 84 85 // Put the double argument into the designated double argument slot. 86 __ subq(rsp, Immediate(kDoubleSize)); 87 __ movsd(MemOperand(rsp, 0), xmm0); 88 89 // Call through to the actual stub 90 __ Call(start, RelocInfo::EXTERNAL_REFERENCE); 91 92 __ addq(rsp, Immediate(kDoubleSize)); 93 94 // Make sure no registers have been unexpectedly clobbered 95 for (--reg_num; reg_num >= 0; --reg_num) { 96 Register reg = Register::FromAllocationIndex(reg_num); 97 if (!reg.is(rsp) && !reg.is(rbp) && !reg.is(destination_reg)) { 98 __ cmpq(reg, MemOperand(rsp, 0)); 99 __ Assert(equal, kRegisterWasClobbered); 100 __ addq(rsp, Immediate(kPointerSize)); 101 } 102 } 103 104 __ movq(rax, destination_reg); 105 106 __ popq(rdi); 107 __ popq(rsi); 108 __ popq(rdx); 109 __ popq(rcx); 110 __ popq(rbx); 111 112 __ ret(0); 113 114 CodeDesc desc; 115 assm.GetCode(&desc); 116 return reinterpret_cast<ConvertDToIFunc>( 117 reinterpret_cast<intptr_t>(buffer)); 118 } 119 120 #undef __ 121 122 123 static Isolate* GetIsolateFrom(LocalContext* context) { 124 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); 125 } 126 127 128 TEST(ConvertDToI) { 129 CcTest::InitializeVM(); 130 LocalContext context; 131 Isolate* isolate = GetIsolateFrom(&context); 132 HandleScope scope(isolate); 133 134 #if DEBUG 135 // Verify that the tests actually work with the C version. In the release 136 // code, the compiler optimizes it away because it's all constant, but does it 137 // wrong, triggering an assert on gcc. 138 RunAllTruncationTests(&ConvertDToICVersion); 139 #endif 140 141 Register source_registers[] = {rsp, rax, rbx, rcx, rdx, rsi, rdi, r8, r9}; 142 Register dest_registers[] = {rax, rbx, rcx, rdx, rsi, rdi, r8, r9}; 143 144 for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) { 145 for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) { 146 RunAllTruncationTests( 147 MakeConvertDToIFuncTrampoline(isolate, 148 source_registers[s], 149 dest_registers[d])); 150 } 151 } 152 } 153