96639577b46aef5b1c27cfd981ef4147751c77cf
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-code-stubs-ia32.cc
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   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;
74   for (;reg_num < Register::NumAllocatableRegisters(); ++reg_num) {
75     Register reg = Register::FromAllocationIndex(reg_num);
76     if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) {
77       __ push(reg);
78       param_offset += kPointerSize;
79     }
80   }
81
82   // Re-push the double argument
83   __ push(MemOperand(esp, param_offset));
84   __ push(MemOperand(esp, param_offset));
85
86   // Call through to the actual stub
87   __ call(start, RelocInfo::EXTERNAL_REFERENCE);
88
89   __ add(esp, Immediate(kDoubleSize));
90
91   // Make sure no registers have been unexpectedly clobbered
92   for (--reg_num; reg_num >= 0; --reg_num) {
93     Register reg = Register::FromAllocationIndex(reg_num);
94     if (!reg.is(esp) && !reg.is(ebp) && !reg.is(destination_reg)) {
95       __ cmp(reg, MemOperand(esp, 0));
96       __ Assert(equal, kRegisterWasClobbered);
97       __ add(esp, Immediate(kPointerSize));
98     }
99   }
100
101   __ mov(eax, destination_reg);
102
103   __ pop(edi);
104   __ pop(esi);
105   __ pop(edx);
106   __ pop(ecx);
107   __ pop(ebx);
108
109   __ ret(kDoubleSize);
110
111   CodeDesc desc;
112   assm.GetCode(&desc);
113   return reinterpret_cast<ConvertDToIFunc>(
114       reinterpret_cast<intptr_t>(buffer));
115 }
116
117 #undef __
118
119
120 static Isolate* GetIsolateFrom(LocalContext* context) {
121   return reinterpret_cast<Isolate*>((*context)->GetIsolate());
122 }
123
124
125 TEST(ConvertDToI) {
126   CcTest::InitializeVM();
127   LocalContext context;
128   Isolate* isolate = GetIsolateFrom(&context);
129   HandleScope scope(isolate);
130
131 #if DEBUG
132   // Verify that the tests actually work with the C version. In the release
133   // code, the compiler optimizes it away because it's all constant, but does it
134   // wrong, triggering an assert on gcc.
135   RunAllTruncationTests(&ConvertDToICVersion);
136 #endif
137
138   Register source_registers[] = {esp, eax, ebx, ecx, edx, edi, esi};
139   Register dest_registers[] = {eax, ebx, ecx, edx, edi, esi};
140
141   for (size_t s = 0; s < sizeof(source_registers) / sizeof(Register); s++) {
142     for (size_t d = 0; d < sizeof(dest_registers) / sizeof(Register); d++) {
143       RunAllTruncationTests(
144           MakeConvertDToIFuncTrampoline(isolate,
145                                         source_registers[s],
146                                         dest_registers[d]));
147     }
148   }
149 }