| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 #define __ assm. | 79 #define __ assm. |
| 80 | 80 |
| 81 | 81 |
| 82 TEST(AssemblerX64ReturnOperation) { | 82 TEST(AssemblerX64ReturnOperation) { |
| 83 // Allocate an executable page of memory. | 83 // Allocate an executable page of memory. |
| 84 size_t actual_size; | 84 size_t actual_size; |
| 85 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 85 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 86 &actual_size, | 86 &actual_size, |
| 87 true)); | 87 true)); |
| 88 CHECK(buffer); | 88 CHECK(buffer); |
| 89 Assembler assm(buffer, actual_size); | 89 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 90 | 90 |
| 91 // Assemble a simple function that copies argument 2 and returns it. | 91 // Assemble a simple function that copies argument 2 and returns it. |
| 92 __ movq(rax, arg2); | 92 __ movq(rax, arg2); |
| 93 __ nop(); | 93 __ nop(); |
| 94 __ ret(0); | 94 __ ret(0); |
| 95 | 95 |
| 96 CodeDesc desc; | 96 CodeDesc desc; |
| 97 assm.GetCode(&desc); | 97 assm.GetCode(&desc); |
| 98 // Call the function from C++. | 98 // Call the function from C++. |
| 99 int result = FUNCTION_CAST<F2>(buffer)(3, 2); | 99 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 100 CHECK_EQ(2, result); | 100 CHECK_EQ(2, result); |
| 101 } | 101 } |
| 102 | 102 |
| 103 TEST(AssemblerX64StackOperations) { | 103 TEST(AssemblerX64StackOperations) { |
| 104 // Allocate an executable page of memory. | 104 // Allocate an executable page of memory. |
| 105 size_t actual_size; | 105 size_t actual_size; |
| 106 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 106 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 107 &actual_size, | 107 &actual_size, |
| 108 true)); | 108 true)); |
| 109 CHECK(buffer); | 109 CHECK(buffer); |
| 110 Assembler assm(buffer, actual_size); | 110 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 111 | 111 |
| 112 // Assemble a simple function that copies argument 2 and returns it. | 112 // Assemble a simple function that copies argument 2 and returns it. |
| 113 // We compile without stack frame pointers, so the gdb debugger shows | 113 // We compile without stack frame pointers, so the gdb debugger shows |
| 114 // incorrect stack frames when debugging this function (which has them). | 114 // incorrect stack frames when debugging this function (which has them). |
| 115 __ push(rbp); | 115 __ push(rbp); |
| 116 __ movq(rbp, rsp); | 116 __ movq(rbp, rsp); |
| 117 __ push(arg2); // Value at (rbp - 8) | 117 __ push(arg2); // Value at (rbp - 8) |
| 118 __ push(arg2); // Value at (rbp - 16) | 118 __ push(arg2); // Value at (rbp - 16) |
| 119 __ push(arg1); // Value at (rbp - 24) | 119 __ push(arg1); // Value at (rbp - 24) |
| 120 __ pop(rax); | 120 __ pop(rax); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 131 CHECK_EQ(2, result); | 131 CHECK_EQ(2, result); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST(AssemblerX64ArithmeticOperations) { | 134 TEST(AssemblerX64ArithmeticOperations) { |
| 135 // Allocate an executable page of memory. | 135 // Allocate an executable page of memory. |
| 136 size_t actual_size; | 136 size_t actual_size; |
| 137 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 137 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 138 &actual_size, | 138 &actual_size, |
| 139 true)); | 139 true)); |
| 140 CHECK(buffer); | 140 CHECK(buffer); |
| 141 Assembler assm(buffer, actual_size); | 141 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 142 | 142 |
| 143 // Assemble a simple function that adds arguments returning the sum. | 143 // Assemble a simple function that adds arguments returning the sum. |
| 144 __ movq(rax, arg2); | 144 __ movq(rax, arg2); |
| 145 __ addq(rax, arg1); | 145 __ addq(rax, arg1); |
| 146 __ ret(0); | 146 __ ret(0); |
| 147 | 147 |
| 148 CodeDesc desc; | 148 CodeDesc desc; |
| 149 assm.GetCode(&desc); | 149 assm.GetCode(&desc); |
| 150 // Call the function from C++. | 150 // Call the function from C++. |
| 151 int result = FUNCTION_CAST<F2>(buffer)(3, 2); | 151 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 152 CHECK_EQ(5, result); | 152 CHECK_EQ(5, result); |
| 153 } | 153 } |
| 154 | 154 |
| 155 TEST(AssemblerX64ImulOperation) { | 155 TEST(AssemblerX64ImulOperation) { |
| 156 // Allocate an executable page of memory. | 156 // Allocate an executable page of memory. |
| 157 size_t actual_size; | 157 size_t actual_size; |
| 158 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 158 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 159 &actual_size, | 159 &actual_size, |
| 160 true)); | 160 true)); |
| 161 CHECK(buffer); | 161 CHECK(buffer); |
| 162 Assembler assm(buffer, actual_size); | 162 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 163 | 163 |
| 164 // Assemble a simple function that multiplies arguments returning the high | 164 // Assemble a simple function that multiplies arguments returning the high |
| 165 // word. | 165 // word. |
| 166 __ movq(rax, arg2); | 166 __ movq(rax, arg2); |
| 167 __ imul(arg1); | 167 __ imul(arg1); |
| 168 __ movq(rax, rdx); | 168 __ movq(rax, rdx); |
| 169 __ ret(0); | 169 __ ret(0); |
| 170 | 170 |
| 171 CodeDesc desc; | 171 CodeDesc desc; |
| 172 assm.GetCode(&desc); | 172 assm.GetCode(&desc); |
| 173 // Call the function from C++. | 173 // Call the function from C++. |
| 174 int result = FUNCTION_CAST<F2>(buffer)(3, 2); | 174 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 175 CHECK_EQ(0, result); | 175 CHECK_EQ(0, result); |
| 176 result = FUNCTION_CAST<F2>(buffer)(0x100000000l, 0x100000000l); | 176 result = FUNCTION_CAST<F2>(buffer)(0x100000000l, 0x100000000l); |
| 177 CHECK_EQ(1, result); | 177 CHECK_EQ(1, result); |
| 178 result = FUNCTION_CAST<F2>(buffer)(-0x100000000l, 0x100000000l); | 178 result = FUNCTION_CAST<F2>(buffer)(-0x100000000l, 0x100000000l); |
| 179 CHECK_EQ(-1, result); | 179 CHECK_EQ(-1, result); |
| 180 } | 180 } |
| 181 | 181 |
| 182 TEST(AssemblerX64MemoryOperands) { | 182 TEST(AssemblerX64MemoryOperands) { |
| 183 // Allocate an executable page of memory. | 183 // Allocate an executable page of memory. |
| 184 size_t actual_size; | 184 size_t actual_size; |
| 185 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 185 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 186 &actual_size, | 186 &actual_size, |
| 187 true)); | 187 true)); |
| 188 CHECK(buffer); | 188 CHECK(buffer); |
| 189 Assembler assm(buffer, actual_size); | 189 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 190 | 190 |
| 191 // Assemble a simple function that copies argument 2 and returns it. | 191 // Assemble a simple function that copies argument 2 and returns it. |
| 192 __ push(rbp); | 192 __ push(rbp); |
| 193 __ movq(rbp, rsp); | 193 __ movq(rbp, rsp); |
| 194 | 194 |
| 195 __ push(arg2); // Value at (rbp - 8) | 195 __ push(arg2); // Value at (rbp - 8) |
| 196 __ push(arg2); // Value at (rbp - 16) | 196 __ push(arg2); // Value at (rbp - 16) |
| 197 __ push(arg1); // Value at (rbp - 24) | 197 __ push(arg1); // Value at (rbp - 24) |
| 198 | 198 |
| 199 const int kStackElementSize = 8; | 199 const int kStackElementSize = 8; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 212 CHECK_EQ(3, result); | 212 CHECK_EQ(3, result); |
| 213 } | 213 } |
| 214 | 214 |
| 215 TEST(AssemblerX64ControlFlow) { | 215 TEST(AssemblerX64ControlFlow) { |
| 216 // Allocate an executable page of memory. | 216 // Allocate an executable page of memory. |
| 217 size_t actual_size; | 217 size_t actual_size; |
| 218 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 218 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 219 &actual_size, | 219 &actual_size, |
| 220 true)); | 220 true)); |
| 221 CHECK(buffer); | 221 CHECK(buffer); |
| 222 Assembler assm(buffer, actual_size); | 222 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 223 | 223 |
| 224 // Assemble a simple function that copies argument 1 and returns it. | 224 // Assemble a simple function that copies argument 1 and returns it. |
| 225 __ push(rbp); | 225 __ push(rbp); |
| 226 | 226 |
| 227 __ movq(rbp, rsp); | 227 __ movq(rbp, rsp); |
| 228 __ movq(rax, arg1); | 228 __ movq(rax, arg1); |
| 229 Label target; | 229 Label target; |
| 230 __ jmp(&target); | 230 __ jmp(&target); |
| 231 __ movq(rax, arg2); | 231 __ movq(rax, arg2); |
| 232 __ bind(&target); | 232 __ bind(&target); |
| 233 __ pop(rbp); | 233 __ pop(rbp); |
| 234 __ ret(0); | 234 __ ret(0); |
| 235 | 235 |
| 236 CodeDesc desc; | 236 CodeDesc desc; |
| 237 assm.GetCode(&desc); | 237 assm.GetCode(&desc); |
| 238 // Call the function from C++. | 238 // Call the function from C++. |
| 239 int result = FUNCTION_CAST<F2>(buffer)(3, 2); | 239 int result = FUNCTION_CAST<F2>(buffer)(3, 2); |
| 240 CHECK_EQ(3, result); | 240 CHECK_EQ(3, result); |
| 241 } | 241 } |
| 242 | 242 |
| 243 TEST(AssemblerX64LoopImmediates) { | 243 TEST(AssemblerX64LoopImmediates) { |
| 244 // Allocate an executable page of memory. | 244 // Allocate an executable page of memory. |
| 245 size_t actual_size; | 245 size_t actual_size; |
| 246 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 246 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 247 &actual_size, | 247 &actual_size, |
| 248 true)); | 248 true)); |
| 249 CHECK(buffer); | 249 CHECK(buffer); |
| 250 Assembler assm(buffer, actual_size); | 250 Assembler assm(buffer, static_cast<int>(actual_size)); |
| 251 // Assemble two loops using rax as counter, and verify the ending counts. | 251 // Assemble two loops using rax as counter, and verify the ending counts. |
| 252 Label Fail; | 252 Label Fail; |
| 253 __ movq(rax, Immediate(-3)); | 253 __ movq(rax, Immediate(-3)); |
| 254 Label Loop1_test; | 254 Label Loop1_test; |
| 255 Label Loop1_body; | 255 Label Loop1_body; |
| 256 __ jmp(&Loop1_test); | 256 __ jmp(&Loop1_test); |
| 257 __ bind(&Loop1_body); | 257 __ bind(&Loop1_body); |
| 258 __ addq(rax, Immediate(7)); | 258 __ addq(rax, Immediate(7)); |
| 259 __ bind(&Loop1_test); | 259 __ bind(&Loop1_test); |
| 260 __ cmpq(rax, Immediate(20)); | 260 __ cmpq(rax, Immediate(20)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 283 __ ret(0); | 283 __ ret(0); |
| 284 | 284 |
| 285 CodeDesc desc; | 285 CodeDesc desc; |
| 286 assm.GetCode(&desc); | 286 assm.GetCode(&desc); |
| 287 // Call the function from C++. | 287 // Call the function from C++. |
| 288 int result = FUNCTION_CAST<F0>(buffer)(); | 288 int result = FUNCTION_CAST<F0>(buffer)(); |
| 289 CHECK_EQ(1, result); | 289 CHECK_EQ(1, result); |
| 290 } | 290 } |
| 291 | 291 |
| 292 #undef __ | 292 #undef __ |
| OLD | NEW |