1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions 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 <limits> 31 32 #include "v8.h" 33 34 #include "cctest.h" 35 #include "code-stubs.h" 36 #include "test-code-stubs.h" 37 #include "factory.h" 38 #include "macro-assembler.h" 39 #include "platform.h" 40 41 using namespace v8::internal; 42 43 #define __ assm. 44 45 ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate, 46 Register source_reg, 47 Register destination_reg) { 48 // Allocate an executable page of memory. 49 size_t actual_size; 50 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, 51 &actual_size, 52 true)); 53 CHECK(buffer); 54 HandleScope handles(isolate); 55 MacroAssembler assm(isolate, buffer, static_cast<int>(actual_size)); 56 assm.set_allow_stub_calls(false); 57 int offset = 58 source_reg.is(esp) ? 0 : (HeapNumber::kValueOffset - kSmiTagSize); 59 DoubleToIStub stub(source_reg, destination_reg, offset, true); 60 byte* start = stub.GetCode(isolate)->instruction_start(); 61 62 __ push(ebx); 63 __ push(ecx); 64 __ push(edx); 65 __ push(esi); 66 __ push(edi); 67 68 if (!source_reg.is(esp)) { 69 __ lea(source_reg, MemOperand(esp, 6 * kPointerSize - offset)); 70 } 71 72 int param_offset = 7 * kPointerSize; 73 // Save registers make sure they don't get clobbered. 74 int reg_num = 0; 75 for (;reg_num < Register::NumAllocatableRegisters(); ++reg_num) { 76 Register reg = Register::from_code(reg_num); 77 if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) { 78 __ push(reg); 79 param_offset += kPointerSize; 80 } 81 } 82 83 // Re-push the double argument 84 __ push(MemOperand(esp, param_offset)); 85 __ push(MemOperand(esp, param_offset)); 86 87 // Call through to the actual stub 88 __ call(start, RelocInfo::EXTERNAL_REFERENCE); 89 90 __ add(esp, Immediate(kDoubleSize)); 91 92 // Make sure no registers have been unexpectedly clobbered 93 for (--reg_num; reg_num >= 0; --reg_num) { 94 Register reg = Register::from_code(reg_num); 95 if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) { 96 __ cmp(reg, MemOperand(esp, 0)); 97 __ Assert(equal, kRegisterWasClobbered); 98 __ add(esp, Immediate(kPointerSize)); 99 } 100 } 101 102 __ mov(eax, destination_reg); 103 104 __ pop(edi); 105 __ pop(esi); 106 __ pop(edx); 107 __ pop(ecx); 108 __ pop(ebx); 109 110 __ ret(kDoubleSize); 111 112 CodeDesc desc; 113 assm.GetCode(&desc); 114 return reinterpret_cast<ConvertDToIFunc>( 115 reinterpret_cast<intptr_t>(buffer)); 116 } 117 118 #undef __ 119 120 121 static Isolate* GetIsolateFrom(LocalContext* context) { 122 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); 123 } 124 125 126 TEST(ConvertDToI) { 127 CcTest::InitializeVM(); 128 LocalContext context; 129 Isolate* isolate = GetIsolateFrom(&context); 130 HandleScope scope(isolate); 131 132 #if DEBUG 133 // Verify that the tests actually work with the C version. In the release 134 // code, the compiler optimizes it away because it's all constant, but does it 135 // wrong, triggering an assert on gcc. 136 RunAllTruncationTests(&ConvertDToICVersion); 137 #endif 138 139 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, eax)); 140 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, ebx)); 141 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, ecx)); 142 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, edx)); 143 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, edi)); 144 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esp, esi)); 145 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, eax)); 146 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, ebx)); 147 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, ecx)); 148 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, edx)); 149 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, edi)); 150 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, eax, esi)); 151 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, eax)); 152 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, ebx)); 153 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, ecx)); 154 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, edx)); 155 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, edi)); 156 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ebx, esi)); 157 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, eax)); 158 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, ebx)); 159 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, ecx)); 160 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, edx)); 161 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, edi)); 162 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, ecx, esi)); 163 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, eax)); 164 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, ebx)); 165 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, ecx)); 166 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, edx)); 167 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, edi)); 168 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edx, esi)); 169 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, eax)); 170 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, ebx)); 171 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, ecx)); 172 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edx)); 173 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edi)); 174 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, esi)); 175 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, eax)); 176 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ebx)); 177 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ecx)); 178 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edx)); 179 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edi)); 180 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, esi)); 181 } 182