blob: 5a420b1a8052ad053bf1870fad23e2256fa12ad1 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 "src/v8.h"
33
34#include "src/base/platform/platform.h"
35#include "src/code-stubs.h"
36#include "src/factory.h"
37#include "src/macro-assembler.h"
38#include "test/cctest/cctest.h"
39#include "test/cctest/test-code-stubs.h"
40
41using namespace v8::internal;
42
43#define __ assm.
44
45ConvertDToIFunc 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*>(v8::base::OS::Allocate(
51 Assembler::kMinimalBufferSize, &actual_size, true));
52 CHECK(buffer);
53 HandleScope handles(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 MacroAssembler assm(isolate, buffer, static_cast<int>(actual_size),
55 v8::internal::CodeObjectRequired::kYes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 int offset =
57 source_reg.is(esp) ? 0 : (HeapNumber::kValueOffset - kSmiTagSize);
58 DoubleToIStub stub(isolate, source_reg, destination_reg, offset, true);
59 byte* start = stub.GetCode()->instruction_start();
60
61 __ push(ebx);
62 __ push(ecx);
63 __ push(edx);
64 __ push(esi);
65 __ push(edi);
66
67 if (!source_reg.is(esp)) {
68 __ lea(source_reg, MemOperand(esp, 6 * kPointerSize - offset));
69 }
70
71 int param_offset = 7 * kPointerSize;
72 // Save registers make sure they don't get clobbered.
73 int reg_num = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 for (; reg_num < Register::kNumRegisters; ++reg_num) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010075 if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode(
76 reg_num)) {
77 Register reg = Register::from_code(reg_num);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) {
79 __ push(reg);
80 param_offset += kPointerSize;
81 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 }
83 }
84
85 // Re-push the double argument
86 __ push(MemOperand(esp, param_offset));
87 __ push(MemOperand(esp, param_offset));
88
89 // Call through to the actual stub
90 __ call(start, RelocInfo::EXTERNAL_REFERENCE);
91
92 __ add(esp, Immediate(kDoubleSize));
93
94 // Make sure no registers have been unexpectedly clobbered
95 for (--reg_num; reg_num >= 0; --reg_num) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010096 if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode(
97 reg_num)) {
98 Register reg = Register::from_code(reg_num);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) {
100 __ cmp(reg, MemOperand(esp, 0));
101 __ Assert(equal, kRegisterWasClobbered);
102 __ add(esp, Immediate(kPointerSize));
103 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 }
105 }
106
107 __ mov(eax, destination_reg);
108
109 __ pop(edi);
110 __ pop(esi);
111 __ pop(edx);
112 __ pop(ecx);
113 __ pop(ebx);
114
115 __ ret(kDoubleSize);
116
117 CodeDesc desc;
118 assm.GetCode(&desc);
119 return reinterpret_cast<ConvertDToIFunc>(
120 reinterpret_cast<intptr_t>(buffer));
121}
122
123#undef __
124
125
126static Isolate* GetIsolateFrom(LocalContext* context) {
127 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
128}
129
130
131TEST(ConvertDToI) {
132 CcTest::InitializeVM();
133 LocalContext context;
134 Isolate* isolate = GetIsolateFrom(&context);
135 HandleScope scope(isolate);
136
137#if DEBUG
138 // Verify that the tests actually work with the C version. In the release
139 // code, the compiler optimizes it away because it's all constant, but does it
140 // wrong, triggering an assert on gcc.
141 RunAllTruncationTests(&ConvertDToICVersion);
142#endif
143
144 Register source_registers[] = {esp, eax, ebx, ecx, edx, edi, esi};
145 Register dest_registers[] = {eax, ebx, ecx, edx, edi, esi};
146
147 for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
148 for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
149 RunAllTruncationTests(
150 MakeConvertDToIFuncTrampoline(isolate,
151 source_registers[s],
152 dest_registers[d]));
153 }
154 }
155}