OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 #include "macro-assembler.h" | 32 #include "macro-assembler.h" |
33 #include "factory.h" | 33 #include "factory.h" |
34 #include "platform.h" | 34 #include "platform.h" |
35 #include "serialize.h" | 35 #include "serialize.h" |
36 #include "cctest.h" | 36 #include "cctest.h" |
37 | 37 |
38 using v8::internal::byte; | 38 using v8::internal::byte; |
39 using v8::internal::OS; | 39 using v8::internal::OS; |
40 using v8::internal::Assembler; | 40 using v8::internal::Assembler; |
| 41 using v8::internal::Operand; |
| 42 using v8::internal::Label; |
41 using v8::internal::rax; | 43 using v8::internal::rax; |
42 using v8::internal::rsi; | 44 using v8::internal::rsi; |
| 45 using v8::internal::rdi; |
| 46 using v8::internal::rbp; |
| 47 using v8::internal::rsp; |
43 using v8::internal::FUNCTION_CAST; | 48 using v8::internal::FUNCTION_CAST; |
44 using v8::internal::CodeDesc; | 49 using v8::internal::CodeDesc; |
45 | 50 |
46 | 51 |
47 // Test the x64 assembler by compiling some simple functions into | 52 // Test the x64 assembler by compiling some simple functions into |
48 // a buffer and executing them. These tests do not initialize the | 53 // a buffer and executing them. These tests do not initialize the |
49 // V8 library, create a context, or use any V8 objects. | 54 // V8 library, create a context, or use any V8 objects. |
50 // The AMD64 calling convention is used, with the first five arguments | 55 // The AMD64 calling convention is used, with the first five arguments |
51 // in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in | 56 // in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in |
52 // the XMM registers. The return value is in RAX. | 57 // the XMM registers. The return value is in RAX. |
53 // This calling convention is used on Linux, with GCC, and on Mac OS, | 58 // This calling convention is used on Linux, with GCC, and on Mac OS, |
54 // with GCC. A different convention is used on 64-bit windows. | 59 // with GCC. A different convention is used on 64-bit windows. |
55 | 60 |
56 typedef int (*F0)(); | 61 typedef int (*F0)(); |
57 typedef int (*F1)(int x); | 62 typedef int (*F1)(int x); |
58 typedef int (*F2)(int x, int y); | 63 typedef int (*F2)(int x, int y); |
59 | 64 |
60 #define __ assm. | 65 #define __ assm. |
61 | 66 |
62 | 67 |
63 TEST(AssemblerX640) { | 68 TEST(AssemblerX64ReturnOperation) { |
64 // Allocate an executable page of memory. | 69 // Allocate an executable page of memory. |
65 size_t actual_size; | 70 size_t actual_size; |
66 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 71 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
67 &actual_size, | 72 &actual_size, |
68 true)); | 73 true)); |
69 CHECK(buffer); | 74 CHECK(buffer); |
70 Assembler assm(buffer, actual_size); | 75 Assembler assm(buffer, actual_size); |
71 | 76 |
72 // Assemble a simple function that copies argument 2 and returns it. | 77 // Assemble a simple function that copies argument 2 and returns it. |
73 __ mov(rax, rsi); | 78 __ mov(rax, rsi); |
74 __ nop(); | 79 __ nop(); |
75 __ ret(0); | 80 __ ret(0); |
76 | 81 |
77 CodeDesc desc; | 82 CodeDesc desc; |
78 assm.GetCode(&desc); | 83 assm.GetCode(&desc); |
79 // Call the function from C++. | 84 // Call the function from C++. |
80 int result = FUNCTION_CAST<F2>(buffer)(3, 2); | 85 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
81 CHECK_EQ(2, result); | 86 CHECK_EQ(2, result); |
82 } | 87 } |
83 | 88 |
| 89 TEST(AssemblerX64StackOperations) { |
| 90 // Allocate an executable page of memory. |
| 91 size_t actual_size; |
| 92 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 93 &actual_size, |
| 94 true)); |
| 95 CHECK(buffer); |
| 96 Assembler assm(buffer, actual_size); |
| 97 |
| 98 // Assemble a simple function that copies argument 2 and returns it. |
| 99 // We compile without stack frame pointers, so the gdb debugger shows |
| 100 // incorrect stack frames when debugging this function (which has them). |
| 101 __ push(rbp); |
| 102 __ mov(rbp, rsp); |
| 103 __ push(rsi); // Value at (rbp - 8) |
| 104 __ push(rsi); // Value at (rbp - 16) |
| 105 __ push(rdi); // Value at (rbp - 24) |
| 106 __ pop(rax); |
| 107 __ pop(rax); |
| 108 __ pop(rax); |
| 109 __ pop(rbp); |
| 110 __ nop(); |
| 111 __ ret(0); |
| 112 |
| 113 CodeDesc desc; |
| 114 assm.GetCode(&desc); |
| 115 // Call the function from C++. |
| 116 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 117 CHECK_EQ(2, result); |
| 118 } |
| 119 |
| 120 TEST(AssemblerX64ArithmeticOperations) { |
| 121 // Allocate an executable page of memory. |
| 122 size_t actual_size; |
| 123 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 124 &actual_size, |
| 125 true)); |
| 126 CHECK(buffer); |
| 127 Assembler assm(buffer, actual_size); |
| 128 |
| 129 // Assemble a simple function that copies argument 2 and returns it. |
| 130 __ mov(rax, rsi); |
| 131 __ add(rax, rdi); |
| 132 __ ret(0); |
| 133 |
| 134 CodeDesc desc; |
| 135 assm.GetCode(&desc); |
| 136 // Call the function from C++. |
| 137 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 138 CHECK_EQ(5, result); |
| 139 } |
| 140 |
| 141 TEST(AssemblerX64MemoryOperands) { |
| 142 // Allocate an executable page of memory. |
| 143 size_t actual_size; |
| 144 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 145 &actual_size, |
| 146 true)); |
| 147 CHECK(buffer); |
| 148 Assembler assm(buffer, actual_size); |
| 149 |
| 150 // Assemble a simple function that copies argument 2 and returns it. |
| 151 __ push(rbp); |
| 152 __ mov(rbp, rsp); |
| 153 __ push(rsi); // Value at (rbp - 8) |
| 154 __ push(rsi); // Value at (rbp - 16) |
| 155 __ push(rdi); // Value at (rbp - 24) |
| 156 // const int kStackElementSize = 8; |
| 157 // __ mov(rax, Operand(rbp,-3 * kStackElementSize)); |
| 158 __ pop(rax); |
| 159 __ pop(rax); |
| 160 __ pop(rax); |
| 161 __ pop(rbp); |
| 162 __ nop(); |
| 163 __ ret(0); |
| 164 |
| 165 CodeDesc desc; |
| 166 assm.GetCode(&desc); |
| 167 // Call the function from C++. |
| 168 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 169 CHECK_EQ(2, result); |
| 170 } |
| 171 |
| 172 TEST(AssemblerX64ControlFlow) { |
| 173 // Allocate an executable page of memory. |
| 174 size_t actual_size; |
| 175 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 176 &actual_size, |
| 177 true)); |
| 178 CHECK(buffer); |
| 179 Assembler assm(buffer, actual_size); |
| 180 |
| 181 // Assemble a simple function that copies argument 2 and returns it. |
| 182 __ push(rbp); |
| 183 __ mov(rbp, rsp); |
| 184 __ mov(rax, rdi); |
| 185 Label target; |
| 186 __ jmp(&target); |
| 187 __ mov(rax, rsi); |
| 188 __ bind(&target); |
| 189 __ pop(rbp); |
| 190 __ ret(0); |
| 191 |
| 192 CodeDesc desc; |
| 193 assm.GetCode(&desc); |
| 194 // Call the function from C++. |
| 195 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 196 CHECK_EQ(3, result); |
| 197 } |
| 198 |
84 #undef __ | 199 #undef __ |
OLD | NEW |