Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 "v8.h" |
| 29 | |
| 30 | #include "disassembler.h" |
| 31 | #include "factory.h" |
| 32 | #include "arm/simulator-arm.h" |
| 33 | #include "arm/assembler-arm-inl.h" |
| 34 | #include "cctest.h" |
| 35 | |
| 36 | using namespace v8::internal; |
| 37 | |
| 38 | |
| 39 | // Define these function prototypes to match JSEntryFunction in execution.cc. |
| 40 | typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4); |
| 41 | typedef Object* (*F2)(int x, int y, int p2, int p3, int p4); |
| 42 | typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4); |
| 43 | |
| 44 | |
| 45 | static v8::Persistent<v8::Context> env; |
| 46 | |
| 47 | |
| 48 | // The test framework does not accept flags on the command line, so we set them |
| 49 | static void InitializeVM() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | // enable generation of comments |
| 51 | FLAG_debug_code = true; |
| 52 | |
| 53 | if (env.IsEmpty()) { |
| 54 | env = v8::Context::New(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #define __ assm. |
| 60 | |
| 61 | TEST(0) { |
| 62 | InitializeVM(); |
| 63 | v8::HandleScope scope; |
| 64 | |
| 65 | Assembler assm(NULL, 0); |
| 66 | |
| 67 | __ add(r0, r0, Operand(r1)); |
| 68 | __ mov(pc, Operand(lr)); |
| 69 | |
| 70 | CodeDesc desc; |
| 71 | assm.GetCode(&desc); |
| 72 | Object* code = Heap::CreateCode(desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | Code::ComputeFlags(Code::STUB), |
| 74 | Handle<Object>(Heap::undefined_value())); |
| 75 | CHECK(code->IsCode()); |
| 76 | #ifdef DEBUG |
| 77 | Code::cast(code)->Print(); |
| 78 | #endif |
| 79 | F2 f = FUNCTION_CAST<F2>(Code::cast(code)->entry()); |
| 80 | int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 3, 4, 0, 0, 0)); |
| 81 | ::printf("f() = %d\n", res); |
| 82 | CHECK_EQ(7, res); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | TEST(1) { |
| 87 | InitializeVM(); |
| 88 | v8::HandleScope scope; |
| 89 | |
| 90 | Assembler assm(NULL, 0); |
| 91 | Label L, C; |
| 92 | |
| 93 | __ mov(r1, Operand(r0)); |
| 94 | __ mov(r0, Operand(0)); |
| 95 | __ b(&C); |
| 96 | |
| 97 | __ bind(&L); |
| 98 | __ add(r0, r0, Operand(r1)); |
| 99 | __ sub(r1, r1, Operand(1)); |
| 100 | |
| 101 | __ bind(&C); |
| 102 | __ teq(r1, Operand(0)); |
| 103 | __ b(ne, &L); |
| 104 | __ mov(pc, Operand(lr)); |
| 105 | |
| 106 | CodeDesc desc; |
| 107 | assm.GetCode(&desc); |
| 108 | Object* code = Heap::CreateCode(desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | Code::ComputeFlags(Code::STUB), |
| 110 | Handle<Object>(Heap::undefined_value())); |
| 111 | CHECK(code->IsCode()); |
| 112 | #ifdef DEBUG |
| 113 | Code::cast(code)->Print(); |
| 114 | #endif |
| 115 | F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry()); |
| 116 | int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 100, 0, 0, 0, 0)); |
| 117 | ::printf("f() = %d\n", res); |
| 118 | CHECK_EQ(5050, res); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | TEST(2) { |
| 123 | InitializeVM(); |
| 124 | v8::HandleScope scope; |
| 125 | |
| 126 | Assembler assm(NULL, 0); |
| 127 | Label L, C; |
| 128 | |
| 129 | __ mov(r1, Operand(r0)); |
| 130 | __ mov(r0, Operand(1)); |
| 131 | __ b(&C); |
| 132 | |
| 133 | __ bind(&L); |
| 134 | __ mul(r0, r1, r0); |
| 135 | __ sub(r1, r1, Operand(1)); |
| 136 | |
| 137 | __ bind(&C); |
| 138 | __ teq(r1, Operand(0)); |
| 139 | __ b(ne, &L); |
| 140 | __ mov(pc, Operand(lr)); |
| 141 | |
| 142 | // some relocated stuff here, not executed |
| 143 | __ RecordComment("dead code, just testing relocations"); |
| 144 | __ mov(r0, Operand(Factory::true_value())); |
| 145 | __ RecordComment("dead code, just testing immediate operands"); |
| 146 | __ mov(r0, Operand(-1)); |
| 147 | __ mov(r0, Operand(0xFF000000)); |
| 148 | __ mov(r0, Operand(0xF0F0F0F0)); |
| 149 | __ mov(r0, Operand(0xFFF0FFFF)); |
| 150 | |
| 151 | CodeDesc desc; |
| 152 | assm.GetCode(&desc); |
| 153 | Object* code = Heap::CreateCode(desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | Code::ComputeFlags(Code::STUB), |
| 155 | Handle<Object>(Heap::undefined_value())); |
| 156 | CHECK(code->IsCode()); |
| 157 | #ifdef DEBUG |
| 158 | Code::cast(code)->Print(); |
| 159 | #endif |
| 160 | F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry()); |
| 161 | int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 10, 0, 0, 0, 0)); |
| 162 | ::printf("f() = %d\n", res); |
| 163 | CHECK_EQ(3628800, res); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | TEST(3) { |
| 168 | InitializeVM(); |
| 169 | v8::HandleScope scope; |
| 170 | |
| 171 | typedef struct { |
| 172 | int i; |
| 173 | char c; |
| 174 | int16_t s; |
| 175 | } T; |
| 176 | T t; |
| 177 | |
| 178 | Assembler assm(NULL, 0); |
| 179 | Label L, C; |
| 180 | |
| 181 | __ mov(ip, Operand(sp)); |
| 182 | __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit()); |
| 183 | __ sub(fp, ip, Operand(4)); |
| 184 | __ mov(r4, Operand(r0)); |
| 185 | __ ldr(r0, MemOperand(r4, OFFSET_OF(T, i))); |
| 186 | __ mov(r2, Operand(r0, ASR, 1)); |
| 187 | __ str(r2, MemOperand(r4, OFFSET_OF(T, i))); |
| 188 | __ ldrsb(r2, MemOperand(r4, OFFSET_OF(T, c))); |
| 189 | __ add(r0, r2, Operand(r0)); |
| 190 | __ mov(r2, Operand(r2, LSL, 2)); |
| 191 | __ strb(r2, MemOperand(r4, OFFSET_OF(T, c))); |
| 192 | __ ldrsh(r2, MemOperand(r4, OFFSET_OF(T, s))); |
| 193 | __ add(r0, r2, Operand(r0)); |
| 194 | __ mov(r2, Operand(r2, ASR, 3)); |
| 195 | __ strh(r2, MemOperand(r4, OFFSET_OF(T, s))); |
| 196 | __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit()); |
| 197 | |
| 198 | CodeDesc desc; |
| 199 | assm.GetCode(&desc); |
| 200 | Object* code = Heap::CreateCode(desc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | Code::ComputeFlags(Code::STUB), |
| 202 | Handle<Object>(Heap::undefined_value())); |
| 203 | CHECK(code->IsCode()); |
| 204 | #ifdef DEBUG |
| 205 | Code::cast(code)->Print(); |
| 206 | #endif |
| 207 | F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry()); |
| 208 | t.i = 100000; |
| 209 | t.c = 10; |
| 210 | t.s = 1000; |
| 211 | int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0)); |
| 212 | ::printf("f() = %d\n", res); |
| 213 | CHECK_EQ(101010, res); |
| 214 | CHECK_EQ(100000/2, t.i); |
| 215 | CHECK_EQ(10*4, t.c); |
| 216 | CHECK_EQ(1000/8, t.s); |
| 217 | } |
| 218 | |
| 219 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 220 | TEST(4) { |
| 221 | // Test the VFP floating point instructions. |
| 222 | InitializeVM(); |
| 223 | v8::HandleScope scope; |
| 224 | |
| 225 | typedef struct { |
| 226 | double a; |
| 227 | double b; |
| 228 | double c; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 229 | double d; |
| 230 | double e; |
| 231 | double f; |
| 232 | int i; |
| 233 | float x; |
| 234 | float y; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 235 | } T; |
| 236 | T t; |
| 237 | |
| 238 | // Create a function that accepts &t, and loads, manipulates, and stores |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 239 | // the doubles and floats. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 240 | Assembler assm(NULL, 0); |
| 241 | Label L, C; |
| 242 | |
| 243 | |
| 244 | if (CpuFeatures::IsSupported(VFP3)) { |
| 245 | CpuFeatures::Scope scope(VFP3); |
| 246 | |
| 247 | __ mov(ip, Operand(sp)); |
| 248 | __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit()); |
| 249 | __ sub(fp, ip, Operand(4)); |
| 250 | |
| 251 | __ mov(r4, Operand(r0)); |
| 252 | __ vldr(d6, r4, OFFSET_OF(T, a)); |
| 253 | __ vldr(d7, r4, OFFSET_OF(T, b)); |
| 254 | __ vadd(d5, d6, d7); |
| 255 | __ vstr(d5, r4, OFFSET_OF(T, c)); |
| 256 | |
| 257 | __ vmov(r2, r3, d5); |
| 258 | __ vmov(d4, r2, r3); |
| 259 | __ vstr(d4, r4, OFFSET_OF(T, b)); |
| 260 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 261 | // Load t.x and t.y, switch values, and store back to the struct. |
| 262 | __ vldr(s0, r4, OFFSET_OF(T, x)); |
| 263 | __ vldr(s31, r4, OFFSET_OF(T, y)); |
| 264 | __ vmov(s16, s0); |
| 265 | __ vmov(s0, s31); |
| 266 | __ vmov(s31, s16); |
| 267 | __ vstr(s0, r4, OFFSET_OF(T, x)); |
| 268 | __ vstr(s31, r4, OFFSET_OF(T, y)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 269 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 270 | // Move a literal into a register that can be encoded in the instruction. |
| 271 | __ vmov(d4, 1.0); |
| 272 | __ vstr(d4, r4, OFFSET_OF(T, e)); |
| 273 | |
| 274 | // Move a literal into a register that requires 64 bits to encode. |
| 275 | // 0x3ff0000010000000 = 1.000000059604644775390625 |
| 276 | __ vmov(d4, 1.000000059604644775390625); |
| 277 | __ vstr(d4, r4, OFFSET_OF(T, d)); |
| 278 | |
| 279 | // Convert from floating point to integer. |
| 280 | __ vmov(d4, 2.0); |
| 281 | __ vcvt_s32_f64(s31, d4); |
| 282 | __ vstr(s31, r4, OFFSET_OF(T, i)); |
| 283 | |
| 284 | // Convert from integer to floating point. |
| 285 | __ mov(lr, Operand(42)); |
| 286 | __ vmov(s31, lr); |
| 287 | __ vcvt_f64_s32(d4, s31); |
| 288 | __ vstr(d4, r4, OFFSET_OF(T, f)); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 289 | __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit()); |
| 290 | |
| 291 | CodeDesc desc; |
| 292 | assm.GetCode(&desc); |
| 293 | Object* code = Heap::CreateCode(desc, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 294 | Code::ComputeFlags(Code::STUB), |
| 295 | Handle<Object>(Heap::undefined_value())); |
| 296 | CHECK(code->IsCode()); |
| 297 | #ifdef DEBUG |
| 298 | Code::cast(code)->Print(); |
| 299 | #endif |
| 300 | F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry()); |
| 301 | t.a = 1.5; |
| 302 | t.b = 2.75; |
| 303 | t.c = 17.17; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 304 | t.d = 0.0; |
| 305 | t.e = 0.0; |
| 306 | t.f = 0.0; |
| 307 | t.i = 0; |
| 308 | t.x = 4.5; |
| 309 | t.y = 9.0; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 310 | Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0); |
| 311 | USE(dummy); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 312 | CHECK_EQ(4.5, t.y); |
| 313 | CHECK_EQ(9.0, t.x); |
| 314 | CHECK_EQ(2, t.i); |
| 315 | CHECK_EQ(42.0, t.f); |
| 316 | CHECK_EQ(1.0, t.e); |
| 317 | CHECK_EQ(1.000000059604644775390625, t.d); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 318 | CHECK_EQ(4.25, t.c); |
| 319 | CHECK_EQ(4.25, t.b); |
| 320 | CHECK_EQ(1.5, t.a); |
| 321 | } |
| 322 | } |
| 323 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 324 | |
| 325 | TEST(5) { |
| 326 | // Test the ARMv7 bitfield instructions. |
| 327 | InitializeVM(); |
| 328 | v8::HandleScope scope; |
| 329 | |
| 330 | Assembler assm(NULL, 0); |
| 331 | |
| 332 | if (CpuFeatures::IsSupported(ARMv7)) { |
| 333 | CpuFeatures::Scope scope(ARMv7); |
| 334 | // On entry, r0 = 0xAAAAAAAA = 0b10..10101010. |
| 335 | __ ubfx(r0, r0, 1, 12); // 0b00..010101010101 = 0x555 |
| 336 | __ sbfx(r0, r0, 0, 5); // 0b11..111111110101 = -11 |
| 337 | __ bfc(r0, 1, 3); // 0b11..111111110001 = -15 |
| 338 | __ mov(r1, Operand(7)); |
| 339 | __ bfi(r0, r1, 3, 3); // 0b11..111111111001 = -7 |
| 340 | __ mov(pc, Operand(lr)); |
| 341 | |
| 342 | CodeDesc desc; |
| 343 | assm.GetCode(&desc); |
| 344 | Object* code = Heap::CreateCode(desc, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 345 | Code::ComputeFlags(Code::STUB), |
| 346 | Handle<Object>(Heap::undefined_value())); |
| 347 | CHECK(code->IsCode()); |
| 348 | #ifdef DEBUG |
| 349 | Code::cast(code)->Print(); |
| 350 | #endif |
| 351 | F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry()); |
| 352 | int res = reinterpret_cast<int>( |
| 353 | CALL_GENERATED_CODE(f, 0xAAAAAAAA, 0, 0, 0, 0)); |
| 354 | ::printf("f() = %d\n", res); |
| 355 | CHECK_EQ(-7, res); |
| 356 | } |
| 357 | } |
| 358 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 359 | |
| 360 | TEST(6) { |
| 361 | // Test saturating instructions. |
| 362 | InitializeVM(); |
| 363 | v8::HandleScope scope; |
| 364 | |
| 365 | Assembler assm(NULL, 0); |
| 366 | |
| 367 | if (CpuFeatures::IsSupported(ARMv7)) { |
| 368 | CpuFeatures::Scope scope(ARMv7); |
| 369 | __ usat(r1, 8, Operand(r0)); // Sat 0xFFFF to 0-255 = 0xFF. |
| 370 | __ usat(r2, 12, Operand(r0, ASR, 9)); // Sat (0xFFFF>>9) to 0-4095 = 0x7F. |
| 371 | __ usat(r3, 1, Operand(r0, LSL, 16)); // Sat (0xFFFF<<16) to 0-1 = 0x0. |
| 372 | __ add(r0, r1, Operand(r2)); |
| 373 | __ add(r0, r0, Operand(r3)); |
| 374 | __ mov(pc, Operand(lr)); |
| 375 | |
| 376 | CodeDesc desc; |
| 377 | assm.GetCode(&desc); |
| 378 | Object* code = Heap::CreateCode(desc, |
| 379 | Code::ComputeFlags(Code::STUB), |
| 380 | Handle<Object>(Heap::undefined_value())); |
| 381 | CHECK(code->IsCode()); |
| 382 | #ifdef DEBUG |
| 383 | Code::cast(code)->Print(); |
| 384 | #endif |
| 385 | F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry()); |
| 386 | int res = reinterpret_cast<int>( |
| 387 | CALL_GENERATED_CODE(f, 0xFFFF, 0, 0, 0, 0)); |
| 388 | ::printf("f() = %d\n", res); |
| 389 | CHECK_EQ(382, res); |
| 390 | } |
| 391 | } |
| 392 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | #undef __ |