blob: 0e0a132146c18527ff59c4ca04d6b733ec51923d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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/simulator.h"
37#include "test/cctest/cctest.h"
38#include "test/cctest/test-code-stubs.h"
39
40using namespace v8::internal;
41
42#define __ masm.
43
44ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate,
45 Register source_reg,
46 Register destination_reg,
47 bool inline_fastpath) {
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 masm(isolate, buffer, static_cast<int>(actual_size),
55 v8::internal::CodeObjectRequired::kYes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 DoubleToIStub stub(isolate, source_reg, destination_reg, 0, true,
57 inline_fastpath);
58
59 byte* start = stub.GetCode()->instruction_start();
60 Label done;
61
62 // Save callee save registers.
63 __ Push(r7, r6, r5, r4);
64 __ Push(lr);
65
66 // For softfp, move the input value into d0.
67 if (!masm.use_eabi_hardfloat()) {
68 __ vmov(d0, r0, r1);
69 }
70 // Push the double argument.
71 __ sub(sp, sp, Operand(kDoubleSize));
72 __ vstr(d0, sp, 0);
73 if (!source_reg.is(sp)) {
74 __ mov(source_reg, sp);
75 }
76
77 // Save registers make sure they don't get clobbered.
78 int source_reg_offset = kDoubleSize;
79 int reg_num = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 for (; reg_num < Register::kNumRegisters; ++reg_num) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010081 if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode(
82 reg_num)) {
83 Register reg = Register::from_code(reg_num);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 if (!reg.is(destination_reg)) {
85 __ push(reg);
86 source_reg_offset += kPointerSize;
87 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 }
89 }
90
91 // Re-push the double argument.
92 __ sub(sp, sp, Operand(kDoubleSize));
93 __ vstr(d0, sp, 0);
94
95 // Call through to the actual stub
96 if (inline_fastpath) {
97 __ vldr(d0, MemOperand(source_reg));
98 __ TryInlineTruncateDoubleToI(destination_reg, d0, &done);
99 if (destination_reg.is(source_reg) && !source_reg.is(sp)) {
100 // Restore clobbered source_reg.
101 __ add(source_reg, sp, Operand(source_reg_offset));
102 }
103 }
104 __ Call(start, RelocInfo::EXTERNAL_REFERENCE);
105 __ bind(&done);
106
107 __ add(sp, sp, Operand(kDoubleSize));
108
109 // Make sure no registers have been unexpectedly clobbered
110 for (--reg_num; reg_num >= 0; --reg_num) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100111 if (RegisterConfiguration::Crankshaft()->IsAllocatableGeneralCode(
112 reg_num)) {
113 Register reg = Register::from_code(reg_num);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 if (!reg.is(destination_reg)) {
115 __ ldr(ip, MemOperand(sp, 0));
116 __ cmp(reg, ip);
117 __ Assert(eq, kRegisterWasClobbered);
118 __ add(sp, sp, Operand(kPointerSize));
119 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 }
121 }
122
123 __ add(sp, sp, Operand(kDoubleSize));
124
125 if (!destination_reg.is(r0))
126 __ mov(r0, destination_reg);
127
128 // Restore callee save registers.
129 __ Pop(lr);
130 __ Pop(r7, r6, r5, r4);
131
132 __ Ret(0);
133
134 CodeDesc desc;
135 masm.GetCode(&desc);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 Assembler::FlushICache(isolate, buffer, actual_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 return (reinterpret_cast<ConvertDToIFunc>(
138 reinterpret_cast<intptr_t>(buffer)));
139}
140
141#undef __
142
143
144static Isolate* GetIsolateFrom(LocalContext* context) {
145 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
146}
147
148
149int32_t RunGeneratedCodeCallWrapper(ConvertDToIFunc func,
150 double from) {
151#ifdef USE_SIMULATOR
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152 return CALL_GENERATED_FP_INT(CcTest::i_isolate(), func, from, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153#else
154 return (*func)(from);
155#endif
156}
157
158
159TEST(ConvertDToI) {
160 CcTest::InitializeVM();
161 LocalContext context;
162 Isolate* isolate = GetIsolateFrom(&context);
163 HandleScope scope(isolate);
164
165#if DEBUG
166 // Verify that the tests actually work with the C version. In the release
167 // code, the compiler optimizes it away because it's all constant, but does it
168 // wrong, triggering an assert on gcc.
169 RunAllTruncationTests(&ConvertDToICVersion);
170#endif
171
172 Register source_registers[] = {sp, r0, r1, r2, r3, r4, r5, r6, r7};
173 Register dest_registers[] = {r0, r1, r2, r3, r4, r5, r6, r7};
174
175 for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
176 for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
177 RunAllTruncationTests(
178 RunGeneratedCodeCallWrapper,
179 MakeConvertDToIFuncTrampoline(isolate,
180 source_registers[s],
181 dest_registers[d],
182 false));
183 RunAllTruncationTests(
184 RunGeneratedCodeCallWrapper,
185 MakeConvertDToIFuncTrampoline(isolate,
186 source_registers[s],
187 dest_registers[d],
188 true));
189 }
190 }
191}