• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2009 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 "v8.h"
31 
32 #include "macro-assembler.h"
33 #include "factory.h"
34 #include "platform.h"
35 #include "serialize.h"
36 #include "cctest.h"
37 
38 using v8::internal::byte;
39 using v8::internal::OS;
40 using v8::internal::Assembler;
41 using v8::internal::Operand;
42 using v8::internal::Immediate;
43 using v8::internal::Label;
44 using v8::internal::rax;
45 using v8::internal::rsi;
46 using v8::internal::rdi;
47 using v8::internal::rdx;
48 using v8::internal::rbp;
49 using v8::internal::rsp;
50 using v8::internal::FUNCTION_CAST;
51 using v8::internal::CodeDesc;
52 using v8::internal::less_equal;
53 using v8::internal::not_equal;
54 using v8::internal::greater;
55 
56 
57 // Test the x64 assembler by compiling some simple functions into
58 // a buffer and executing them.  These tests do not initialize the
59 // V8 library, create a context, or use any V8 objects.
60 // The AMD64 calling convention is used, with the first five arguments
61 // in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in
62 // the XMM registers.  The return value is in RAX.
63 // This calling convention is used on Linux, with GCC, and on Mac OS,
64 // with GCC.  A different convention is used on 64-bit windows.
65 
66 typedef int (*F0)();
67 typedef int (*F1)(int64_t x);
68 typedef int (*F2)(int64_t x, int64_t y);
69 
70 #define __ assm.
71 
72 
TEST(AssemblerX64ReturnOperation)73 TEST(AssemblerX64ReturnOperation) {
74   // Allocate an executable page of memory.
75   size_t actual_size;
76   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
77                                                  &actual_size,
78                                                  true));
79   CHECK(buffer);
80   Assembler assm(buffer, actual_size);
81 
82   // Assemble a simple function that copies argument 2 and returns it.
83   __ movq(rax, rsi);
84   __ nop();
85   __ ret(0);
86 
87   CodeDesc desc;
88   assm.GetCode(&desc);
89   // Call the function from C++.
90   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
91   CHECK_EQ(2, result);
92 }
93 
TEST(AssemblerX64StackOperations)94 TEST(AssemblerX64StackOperations) {
95   // Allocate an executable page of memory.
96   size_t actual_size;
97   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
98                                                  &actual_size,
99                                                  true));
100   CHECK(buffer);
101   Assembler assm(buffer, actual_size);
102 
103   // Assemble a simple function that copies argument 2 and returns it.
104   // We compile without stack frame pointers, so the gdb debugger shows
105   // incorrect stack frames when debugging this function (which has them).
106   __ push(rbp);
107   __ movq(rbp, rsp);
108   __ push(rsi);  // Value at (rbp - 8)
109   __ push(rsi);  // Value at (rbp - 16)
110   __ push(rdi);  // Value at (rbp - 24)
111   __ pop(rax);
112   __ pop(rax);
113   __ pop(rax);
114   __ pop(rbp);
115   __ nop();
116   __ ret(0);
117 
118   CodeDesc desc;
119   assm.GetCode(&desc);
120   // Call the function from C++.
121   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
122   CHECK_EQ(2, result);
123 }
124 
TEST(AssemblerX64ArithmeticOperations)125 TEST(AssemblerX64ArithmeticOperations) {
126   // Allocate an executable page of memory.
127   size_t actual_size;
128   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
129                                                  &actual_size,
130                                                  true));
131   CHECK(buffer);
132   Assembler assm(buffer, actual_size);
133 
134   // Assemble a simple function that adds arguments returning the sum.
135   __ movq(rax, rsi);
136   __ addq(rax, rdi);
137   __ ret(0);
138 
139   CodeDesc desc;
140   assm.GetCode(&desc);
141   // Call the function from C++.
142   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
143   CHECK_EQ(5, result);
144 }
145 
TEST(AssemblerX64ImulOperation)146 TEST(AssemblerX64ImulOperation) {
147   // Allocate an executable page of memory.
148   size_t actual_size;
149   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
150                                                  &actual_size,
151                                                  true));
152   CHECK(buffer);
153   Assembler assm(buffer, actual_size);
154 
155   // Assemble a simple function that multiplies arguments returning the high
156   // word.
157   __ movq(rax, rsi);
158   __ imul(rdi);
159   __ movq(rax, rdx);
160   __ ret(0);
161 
162   CodeDesc desc;
163   assm.GetCode(&desc);
164   // Call the function from C++.
165   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
166   CHECK_EQ(0, result);
167   result =  FUNCTION_CAST<F2>(buffer)(0x100000000l, 0x100000000l);
168   CHECK_EQ(1, result);
169   result =  FUNCTION_CAST<F2>(buffer)(-0x100000000l, 0x100000000l);
170   CHECK_EQ(-1, result);
171 }
172 
TEST(AssemblerX64MemoryOperands)173 TEST(AssemblerX64MemoryOperands) {
174   // Allocate an executable page of memory.
175   size_t actual_size;
176   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
177                                                  &actual_size,
178                                                  true));
179   CHECK(buffer);
180   Assembler assm(buffer, actual_size);
181 
182   // Assemble a simple function that copies argument 2 and returns it.
183   __ push(rbp);
184   __ movq(rbp, rsp);
185   __ push(rsi);  // Value at (rbp - 8)
186   __ push(rsi);  // Value at (rbp - 16)
187   __ push(rdi);  // Value at (rbp - 24)
188   const int kStackElementSize = 8;
189   __ movq(rax, Operand(rbp, -3 * kStackElementSize));
190   __ pop(rsi);
191   __ pop(rsi);
192   __ pop(rsi);
193   __ pop(rbp);
194   __ nop();
195   __ ret(0);
196 
197   CodeDesc desc;
198   assm.GetCode(&desc);
199   // Call the function from C++.
200   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
201   CHECK_EQ(3, result);
202 }
203 
TEST(AssemblerX64ControlFlow)204 TEST(AssemblerX64ControlFlow) {
205   // Allocate an executable page of memory.
206   size_t actual_size;
207   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
208                                                  &actual_size,
209                                                  true));
210   CHECK(buffer);
211   Assembler assm(buffer, actual_size);
212 
213   // Assemble a simple function that copies argument 2 and returns it.
214   __ push(rbp);
215   __ movq(rbp, rsp);
216   __ movq(rax, rdi);
217   Label target;
218   __ jmp(&target);
219   __ movq(rax, rsi);
220   __ bind(&target);
221   __ pop(rbp);
222   __ ret(0);
223 
224   CodeDesc desc;
225   assm.GetCode(&desc);
226   // Call the function from C++.
227   int result =  FUNCTION_CAST<F2>(buffer)(3, 2);
228   CHECK_EQ(3, result);
229 }
230 
TEST(AssemblerX64LoopImmediates)231 TEST(AssemblerX64LoopImmediates) {
232   // Allocate an executable page of memory.
233   size_t actual_size;
234   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
235                                                  &actual_size,
236                                                  true));
237   CHECK(buffer);
238   Assembler assm(buffer, actual_size);
239   // Assemble two loops using rax as counter, and verify the ending counts.
240   Label Fail;
241   __ movq(rax, Immediate(-3));
242   Label Loop1_test;
243   Label Loop1_body;
244   __ jmp(&Loop1_test);
245   __ bind(&Loop1_body);
246   __ addq(rax, Immediate(7));
247   __ bind(&Loop1_test);
248   __ cmpq(rax, Immediate(20));
249   __ j(less_equal, &Loop1_body);
250   // Did the loop terminate with the expected value?
251   __ cmpq(rax, Immediate(25));
252   __ j(not_equal, &Fail);
253 
254   Label Loop2_test;
255   Label Loop2_body;
256   __ movq(rax, Immediate(0x11FEED00));
257   __ jmp(&Loop2_test);
258   __ bind(&Loop2_body);
259   __ addq(rax, Immediate(-0x1100));
260   __ bind(&Loop2_test);
261   __ cmpq(rax, Immediate(0x11FE8000));
262   __ j(greater, &Loop2_body);
263   // Did the loop terminate with the expected value?
264   __ cmpq(rax, Immediate(0x11FE7600));
265   __ j(not_equal, &Fail);
266 
267   __ movq(rax, Immediate(1));
268   __ ret(0);
269   __ bind(&Fail);
270   __ movq(rax, Immediate(0));
271   __ ret(0);
272 
273   CodeDesc desc;
274   assm.GetCode(&desc);
275   // Call the function from C++.
276   int result =  FUNCTION_CAST<F0>(buffer)();
277   CHECK_EQ(1, result);
278 }
279 
280 #undef __
281