blob: 507bbd44c3fc5afa4ecf22dbd968d9c1982a1499 [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 "v8.h"
29
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_X64)
31
Ben Murdoch8b112d22011-06-08 16:22:53 +010032#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34namespace v8 {
35namespace internal {
36
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010037// -------------------------------------------------------------------------
38// Platform-specific RuntimeCallHelper functions.
39
Ben Murdochb0fe1622011-05-05 13:52:32 +010040void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
Ben Murdoch85b71792012-04-11 18:30:58 +010041 masm->EnterInternalFrame();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010042}
43
44
Ben Murdochb0fe1622011-05-05 13:52:32 +010045void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
Ben Murdoch85b71792012-04-11 18:30:58 +010046 masm->LeaveInternalFrame();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010047}
48
49
Steve Block3ce2e202009-11-05 08:53:23 +000050#define __ masm.
51
52#ifdef _WIN64
53typedef double (*ModuloFunction)(double, double);
54// Define custom fmod implementation.
55ModuloFunction CreateModuloFunction() {
56 size_t actual_size;
57 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
58 &actual_size,
59 true));
60 CHECK(buffer);
Ben Murdoch8b112d22011-06-08 16:22:53 +010061 Assembler masm(NULL, buffer, static_cast<int>(actual_size));
Steve Block3ce2e202009-11-05 08:53:23 +000062 // Generated code is put into a fixed, unmovable, buffer, and not into
63 // the V8 heap. We can't, and don't, refer to any relocatable addresses
64 // (e.g. the JavaScript nan-object).
65
66 // Windows 64 ABI passes double arguments in xmm0, xmm1 and
67 // returns result in xmm0.
68 // Argument backing space is allocated on the stack above
69 // the return address.
70
71 // Compute x mod y.
72 // Load y and x (use argument backing store as temporary storage).
73 __ movsd(Operand(rsp, kPointerSize * 2), xmm1);
74 __ movsd(Operand(rsp, kPointerSize), xmm0);
75 __ fld_d(Operand(rsp, kPointerSize * 2));
76 __ fld_d(Operand(rsp, kPointerSize));
77
78 // Clear exception flags before operation.
79 {
80 Label no_exceptions;
81 __ fwait();
82 __ fnstsw_ax();
83 // Clear if Illegal Operand or Zero Division exceptions are set.
84 __ testb(rax, Immediate(5));
85 __ j(zero, &no_exceptions);
86 __ fnclex();
87 __ bind(&no_exceptions);
88 }
89
90 // Compute st(0) % st(1)
91 {
92 Label partial_remainder_loop;
93 __ bind(&partial_remainder_loop);
94 __ fprem();
95 __ fwait();
96 __ fnstsw_ax();
97 __ testl(rax, Immediate(0x400 /* C2 */));
98 // If C2 is set, computation only has partial result. Loop to
99 // continue computation.
100 __ j(not_zero, &partial_remainder_loop);
101 }
102
103 Label valid_result;
104 Label return_result;
105 // If Invalid Operand or Zero Division exceptions are set,
106 // return NaN.
107 __ testb(rax, Immediate(5));
108 __ j(zero, &valid_result);
109 __ fstp(0); // Drop result in st(0).
110 int64_t kNaNValue = V8_INT64_C(0x7ff8000000000000);
111 __ movq(rcx, kNaNValue, RelocInfo::NONE);
112 __ movq(Operand(rsp, kPointerSize), rcx);
113 __ movsd(xmm0, Operand(rsp, kPointerSize));
114 __ jmp(&return_result);
115
116 // If result is valid, return that.
117 __ bind(&valid_result);
118 __ fstp_d(Operand(rsp, kPointerSize));
119 __ movsd(xmm0, Operand(rsp, kPointerSize));
120
121 // Clean up FPU stack and exceptions and return xmm0
122 __ bind(&return_result);
123 __ fstp(0); // Unload y.
124
125 Label clear_exceptions;
126 __ testb(rax, Immediate(0x3f /* Any Exception*/));
127 __ j(not_zero, &clear_exceptions);
128 __ ret(0);
129 __ bind(&clear_exceptions);
130 __ fnclex();
131 __ ret(0);
132
133 CodeDesc desc;
134 masm.GetCode(&desc);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000135 OS::ProtectCode(buffer, actual_size);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100136 // Call the function from C++ through this pointer.
Steve Block3ce2e202009-11-05 08:53:23 +0000137 return FUNCTION_CAST<ModuloFunction>(buffer);
138}
139
140#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
Leon Clarkee46be812010-01-19 14:06:41 +0000142
Steve Blocka7e24c12009-10-30 11:49:00 +0000143#undef __
144
145} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +0100146
147#endif // V8_TARGET_ARCH_X64