test-assembler-arm.cc 52.9 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
#include <iostream>  // NOLINT(readability/streams)

30
#include "src/v8.h"
31
#include "test/cctest/cctest.h"
32

33 34
#include "src/arm/assembler-arm-inl.h"
#include "src/arm/simulator-arm.h"
35 36
#include "src/disassembler.h"
#include "src/factory.h"
37
#include "src/ostreams.h"
38

39
using namespace v8::base;
40 41 42 43
using namespace v8::internal;


// Define these function prototypes to match JSEntryFunction in execution.cc.
44 45
typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4);
typedef Object* (*F2)(int x, int y, int p2, int p3, int p4);
46 47
typedef Object* (*F3)(void* p0, int p1, int p2, int p3, int p4);
typedef Object* (*F4)(void* p0, void* p1, int p2, int p3, int p4);
48 49 50 51 52


#define __ assm.

TEST(0) {
53
  CcTest::InitializeVM();
54
  Isolate* isolate = CcTest::i_isolate();
55 56
  HandleScope scope(isolate);

57
  Assembler assm(isolate, NULL, 0);
58 59 60 61 62 63

  __ add(r0, r0, Operand(r1));
  __ mov(pc, Operand(lr));

  CodeDesc desc;
  assm.GetCode(&desc);
64 65
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
66
#ifdef DEBUG
67 68
  OFStream os(stdout);
  code->Print(os);
69
#endif
70
  F2 f = FUNCTION_CAST<F2>(code->entry());
71 72 73 74 75 76 77
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 3, 4, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(7, res);
}


TEST(1) {
78
  CcTest::InitializeVM();
79
  Isolate* isolate = CcTest::i_isolate();
80 81
  HandleScope scope(isolate);

82
  Assembler assm(isolate, NULL, 0);
83 84 85
  Label L, C;

  __ mov(r1, Operand(r0));
86
  __ mov(r0, Operand::Zero());
87 88 89 90 91 92 93
  __ b(&C);

  __ bind(&L);
  __ add(r0, r0, Operand(r1));
  __ sub(r1, r1, Operand(1));

  __ bind(&C);
94
  __ teq(r1, Operand::Zero());
95 96 97 98 99
  __ b(ne, &L);
  __ mov(pc, Operand(lr));

  CodeDesc desc;
  assm.GetCode(&desc);
100 101
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
102
#ifdef DEBUG
103 104
  OFStream os(stdout);
  code->Print(os);
105
#endif
106
  F1 f = FUNCTION_CAST<F1>(code->entry());
107 108 109 110 111 112 113
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 100, 0, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(5050, res);
}


TEST(2) {
114
  CcTest::InitializeVM();
115
  Isolate* isolate = CcTest::i_isolate();
116 117
  HandleScope scope(isolate);

118
  Assembler assm(isolate, NULL, 0);
119 120 121 122 123 124 125 126 127 128 129
  Label L, C;

  __ mov(r1, Operand(r0));
  __ mov(r0, Operand(1));
  __ b(&C);

  __ bind(&L);
  __ mul(r0, r1, r0);
  __ sub(r1, r1, Operand(1));

  __ bind(&C);
130
  __ teq(r1, Operand::Zero());
131 132 133 134 135
  __ b(ne, &L);
  __ mov(pc, Operand(lr));

  // some relocated stuff here, not executed
  __ RecordComment("dead code, just testing relocations");
136
  __ mov(r0, Operand(isolate->factory()->true_value()));
137 138 139 140 141 142 143 144
  __ RecordComment("dead code, just testing immediate operands");
  __ mov(r0, Operand(-1));
  __ mov(r0, Operand(0xFF000000));
  __ mov(r0, Operand(0xF0F0F0F0));
  __ mov(r0, Operand(0xFFF0FFFF));

  CodeDesc desc;
  assm.GetCode(&desc);
145 146
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
147
#ifdef DEBUG
148 149
  OFStream os(stdout);
  code->Print(os);
150
#endif
151
  F1 f = FUNCTION_CAST<F1>(code->entry());
152 153 154 155 156 157 158
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 10, 0, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(3628800, res);
}


TEST(3) {
159
  CcTest::InitializeVM();
160
  Isolate* isolate = CcTest::i_isolate();
161
  HandleScope scope(isolate);
162 163 164 165 166 167 168 169

  typedef struct {
    int i;
    char c;
    int16_t s;
  } T;
  T t;

170
  Assembler assm(isolate, NULL, 0);
171 172 173
  Label L, C;

  __ mov(ip, Operand(sp));
lrn@chromium.org's avatar
lrn@chromium.org committed
174
  __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());
175 176 177 178 179 180 181 182 183 184 185 186 187
  __ sub(fp, ip, Operand(4));
  __ mov(r4, Operand(r0));
  __ ldr(r0, MemOperand(r4, OFFSET_OF(T, i)));
  __ mov(r2, Operand(r0, ASR, 1));
  __ str(r2, MemOperand(r4, OFFSET_OF(T, i)));
  __ ldrsb(r2, MemOperand(r4, OFFSET_OF(T, c)));
  __ add(r0, r2, Operand(r0));
  __ mov(r2, Operand(r2, LSL, 2));
  __ strb(r2, MemOperand(r4, OFFSET_OF(T, c)));
  __ ldrsh(r2, MemOperand(r4, OFFSET_OF(T, s)));
  __ add(r0, r2, Operand(r0));
  __ mov(r2, Operand(r2, ASR, 3));
  __ strh(r2, MemOperand(r4, OFFSET_OF(T, s)));
lrn@chromium.org's avatar
lrn@chromium.org committed
188
  __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());
189 190 191

  CodeDesc desc;
  assm.GetCode(&desc);
192 193
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
194
#ifdef DEBUG
195 196
  OFStream os(stdout);
  code->Print(os);
197
#endif
198
  F3 f = FUNCTION_CAST<F3>(code->entry());
199 200 201 202 203 204 205 206 207 208 209 210
  t.i = 100000;
  t.c = 10;
  t.s = 1000;
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(101010, res);
  CHECK_EQ(100000/2, t.i);
  CHECK_EQ(10*4, t.c);
  CHECK_EQ(1000/8, t.s);
}


211 212
TEST(4) {
  // Test the VFP floating point instructions.
213
  CcTest::InitializeVM();
214
  Isolate* isolate = CcTest::i_isolate();
215
  HandleScope scope(isolate);
216 217 218 219 220

  typedef struct {
    double a;
    double b;
    double c;
221 222 223
    double d;
    double e;
    double f;
224 225
    double g;
    double h;
226
    int i;
227
    double j;
228 229
    double m;
    double n;
230 231
    float x;
    float y;
232 233 234 235
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
236
  // the doubles and floats.
237
  Assembler assm(isolate, NULL, 0);
238 239 240
  Label L, C;


241
  if (CpuFeatures::IsSupported(VFP3)) {
242
    CpuFeatureScope scope(&assm, VFP3);
243 244 245 246 247 248 249 250 251 252 253

    __ mov(ip, Operand(sp));
    __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());
    __ sub(fp, ip, Operand(4));

    __ mov(r4, Operand(r0));
    __ vldr(d6, r4, OFFSET_OF(T, a));
    __ vldr(d7, r4, OFFSET_OF(T, b));
    __ vadd(d5, d6, d7);
    __ vstr(d5, r4, OFFSET_OF(T, c));

254
    __ vmla(d5, d6, d7);
255
    __ vmls(d5, d5, d6);
256

257 258 259 260
    __ vmov(r2, r3, d5);
    __ vmov(d4, r2, r3);
    __ vstr(d4, r4, OFFSET_OF(T, b));

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    // Load t.x and t.y, switch values, and store back to the struct.
    __ vldr(s0, r4, OFFSET_OF(T, x));
    __ vldr(s31, r4, OFFSET_OF(T, y));
    __ vmov(s16, s0);
    __ vmov(s0, s31);
    __ vmov(s31, s16);
    __ vstr(s0, r4, OFFSET_OF(T, x));
    __ vstr(s31, r4, OFFSET_OF(T, y));

    // Move a literal into a register that can be encoded in the instruction.
    __ vmov(d4, 1.0);
    __ vstr(d4, r4, OFFSET_OF(T, e));

    // Move a literal into a register that requires 64 bits to encode.
    // 0x3ff0000010000000 = 1.000000059604644775390625
    __ vmov(d4, 1.000000059604644775390625);
    __ vstr(d4, r4, OFFSET_OF(T, d));

    // Convert from floating point to integer.
    __ vmov(d4, 2.0);
    __ vcvt_s32_f64(s31, d4);
    __ vstr(s31, r4, OFFSET_OF(T, i));

    // Convert from integer to floating point.
    __ mov(lr, Operand(42));
    __ vmov(s31, lr);
    __ vcvt_f64_s32(d4, s31);
    __ vstr(d4, r4, OFFSET_OF(T, f));
289

290
    // Convert from fixed point to floating point.
291
    __ mov(lr, Operand(2468));
292
    __ vmov(s8, lr);
293
    __ vcvt_f64_s32(d4, 2);
294 295
    __ vstr(d4, r4, OFFSET_OF(T, j));

296 297 298 299 300 301 302 303
    // Test vabs.
    __ vldr(d1, r4, OFFSET_OF(T, g));
    __ vabs(d0, d1);
    __ vstr(d0, r4, OFFSET_OF(T, g));
    __ vldr(d2, r4, OFFSET_OF(T, h));
    __ vabs(d0, d2);
    __ vstr(d0, r4, OFFSET_OF(T, h));

304 305 306 307 308 309 310 311
    // Test vneg.
    __ vldr(d1, r4, OFFSET_OF(T, m));
    __ vneg(d0, d1);
    __ vstr(d0, r4, OFFSET_OF(T, m));
    __ vldr(d1, r4, OFFSET_OF(T, n));
    __ vneg(d0, d1);
    __ vstr(d0, r4, OFFSET_OF(T, n));

312
    __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());
313 314 315

    CodeDesc desc;
    assm.GetCode(&desc);
316 317
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
318
#ifdef DEBUG
319 320
    OFStream os(stdout);
    code->Print(os);
321
#endif
322
    F3 f = FUNCTION_CAST<F3>(code->entry());
323 324 325
    t.a = 1.5;
    t.b = 2.75;
    t.c = 17.17;
326 327 328
    t.d = 0.0;
    t.e = 0.0;
    t.f = 0.0;
329 330
    t.g = -2718.2818;
    t.h = 31415926.5;
331
    t.i = 0;
332
    t.j = 0;
333 334
    t.m = -2718.2818;
    t.n = 123.456;
335 336
    t.x = 4.5;
    t.y = 9.0;
337 338
    Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
    USE(dummy);
339 340
    CHECK_EQ(4.5, t.y);
    CHECK_EQ(9.0, t.x);
341 342
    CHECK_EQ(-123.456, t.n);
    CHECK_EQ(2718.2818, t.m);
343
    CHECK_EQ(2, t.i);
344 345
    CHECK_EQ(2718.2818, t.g);
    CHECK_EQ(31415926.5, t.h);
346
    CHECK_EQ(617.0, t.j);
347 348 349
    CHECK_EQ(42.0, t.f);
    CHECK_EQ(1.0, t.e);
    CHECK_EQ(1.000000059604644775390625, t.d);
350
    CHECK_EQ(4.25, t.c);
351
    CHECK_EQ(-4.1875, t.b);
352 353
    CHECK_EQ(1.5, t.a);
  }
354 355
}

356 357 358

TEST(5) {
  // Test the ARMv7 bitfield instructions.
359
  CcTest::InitializeVM();
360
  Isolate* isolate = CcTest::i_isolate();
361 362
  HandleScope scope(isolate);

363
  Assembler assm(isolate, NULL, 0);
364

365
  if (CpuFeatures::IsSupported(ARMv7)) {
366
    CpuFeatureScope scope(&assm, ARMv7);
367 368 369 370 371 372 373 374 375 376
    // On entry, r0 = 0xAAAAAAAA = 0b10..10101010.
    __ ubfx(r0, r0, 1, 12);  // 0b00..010101010101 = 0x555
    __ sbfx(r0, r0, 0, 5);   // 0b11..111111110101 = -11
    __ bfc(r0, 1, 3);        // 0b11..111111110001 = -15
    __ mov(r1, Operand(7));
    __ bfi(r0, r1, 3, 3);    // 0b11..111111111001 = -7
    __ mov(pc, Operand(lr));

    CodeDesc desc;
    assm.GetCode(&desc);
377 378
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
379
#ifdef DEBUG
380 381
    OFStream os(stdout);
    code->Print(os);
382
#endif
383
    F1 f = FUNCTION_CAST<F1>(code->entry());
384 385 386 387 388 389 390
    int res = reinterpret_cast<int>(
                CALL_GENERATED_CODE(f, 0xAAAAAAAA, 0, 0, 0, 0));
    ::printf("f() = %d\n", res);
    CHECK_EQ(-7, res);
  }
}

391 392 393

TEST(6) {
  // Test saturating instructions.
394
  CcTest::InitializeVM();
395
  Isolate* isolate = CcTest::i_isolate();
396 397
  HandleScope scope(isolate);

398
  Assembler assm(isolate, NULL, 0);
399

400
  if (CpuFeatures::IsSupported(ARMv7)) {
401
    CpuFeatureScope scope(&assm, ARMv7);
402 403 404 405 406 407 408 409 410
    __ usat(r1, 8, Operand(r0));           // Sat 0xFFFF to 0-255 = 0xFF.
    __ usat(r2, 12, Operand(r0, ASR, 9));  // Sat (0xFFFF>>9) to 0-4095 = 0x7F.
    __ usat(r3, 1, Operand(r0, LSL, 16));  // Sat (0xFFFF<<16) to 0-1 = 0x0.
    __ add(r0, r1, Operand(r2));
    __ add(r0, r0, Operand(r3));
    __ mov(pc, Operand(lr));

    CodeDesc desc;
    assm.GetCode(&desc);
411 412
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
413
#ifdef DEBUG
414 415
    OFStream os(stdout);
    code->Print(os);
416
#endif
417
    F1 f = FUNCTION_CAST<F1>(code->entry());
418 419 420 421 422 423 424
    int res = reinterpret_cast<int>(
                CALL_GENERATED_CODE(f, 0xFFFF, 0, 0, 0, 0));
    ::printf("f() = %d\n", res);
    CHECK_EQ(382, res);
  }
}

425

426 427 428 429 430 431 432 433 434 435
enum VCVTTypes {
  s32_f64,
  u32_f64
};

static void TestRoundingMode(VCVTTypes types,
                             VFPRoundingMode mode,
                             double value,
                             int expected,
                             bool expected_exception = false) {
436
  Isolate* isolate = CcTest::i_isolate();
437 438
  HandleScope scope(isolate);

439
  Assembler assm(isolate, NULL, 0);
440

441
  if (CpuFeatures::IsSupported(VFP3)) {
442
    CpuFeatureScope scope(&assm, VFP3);
443

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    Label wrong_exception;

    __ vmrs(r1);
    // Set custom FPSCR.
    __ bic(r2, r1, Operand(kVFPRoundingModeMask | kVFPExceptionMask));
    __ orr(r2, r2, Operand(mode));
    __ vmsr(r2);

    // Load value, convert, and move back result to r0 if everything went well.
    __ vmov(d1, value);
    switch (types) {
      case s32_f64:
        __ vcvt_s32_f64(s0, d1, kFPSCRRounding);
        break;

      case u32_f64:
        __ vcvt_u32_f64(s0, d1, kFPSCRRounding);
        break;

      default:
        UNREACHABLE();
        break;
    }
    // Check for vfp exceptions
    __ vmrs(r2);
    __ tst(r2, Operand(kVFPExceptionMask));
    // Check that we behaved as expected.
    __ b(&wrong_exception,
         expected_exception ? eq : ne);
    // There was no exception. Retrieve the result and return.
    __ vmov(r0, s0);
    __ mov(pc, Operand(lr));
476

477 478 479 480 481
    // The exception behaviour is not what we expected.
    // Load a special value and return.
    __ bind(&wrong_exception);
    __ mov(r0, Operand(11223344));
    __ mov(pc, Operand(lr));
482

483 484
    CodeDesc desc;
    assm.GetCode(&desc);
485 486
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
487
#ifdef DEBUG
488 489
    OFStream os(stdout);
    code->Print(os);
490
#endif
491
    F1 f = FUNCTION_CAST<F1>(code->entry());
492 493 494 495 496
    int res = reinterpret_cast<int>(
                CALL_GENERATED_CODE(f, 0, 0, 0, 0, 0));
    ::printf("res = %d\n", res);
    CHECK_EQ(expected, res);
  }
497 498 499 500
}


TEST(7) {
501
  CcTest::InitializeVM();
502 503
  // Test vfp rounding modes.

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  // s32_f64 (double to integer).

  TestRoundingMode(s32_f64, RN,  0, 0);
  TestRoundingMode(s32_f64, RN,  0.5, 0);
  TestRoundingMode(s32_f64, RN, -0.5, 0);
  TestRoundingMode(s32_f64, RN,  1.5, 2);
  TestRoundingMode(s32_f64, RN, -1.5, -2);
  TestRoundingMode(s32_f64, RN,  123.7, 124);
  TestRoundingMode(s32_f64, RN, -123.7, -124);
  TestRoundingMode(s32_f64, RN,  123456.2,  123456);
  TestRoundingMode(s32_f64, RN, -123456.2, -123456);
  TestRoundingMode(s32_f64, RN, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(s32_f64, RN, (kMaxInt + 0.49), kMaxInt);
  TestRoundingMode(s32_f64, RN, (kMaxInt + 1.0), kMaxInt, true);
  TestRoundingMode(s32_f64, RN, (kMaxInt + 0.5), kMaxInt, true);
  TestRoundingMode(s32_f64, RN, static_cast<double>(kMinInt), kMinInt);
  TestRoundingMode(s32_f64, RN, (kMinInt - 0.5), kMinInt);
  TestRoundingMode(s32_f64, RN, (kMinInt - 1.0), kMinInt, true);
  TestRoundingMode(s32_f64, RN, (kMinInt - 0.51), kMinInt, true);

  TestRoundingMode(s32_f64, RM,  0, 0);
  TestRoundingMode(s32_f64, RM,  0.5, 0);
  TestRoundingMode(s32_f64, RM, -0.5, -1);
  TestRoundingMode(s32_f64, RM,  123.7, 123);
  TestRoundingMode(s32_f64, RM, -123.7, -124);
  TestRoundingMode(s32_f64, RM,  123456.2,  123456);
  TestRoundingMode(s32_f64, RM, -123456.2, -123457);
  TestRoundingMode(s32_f64, RM, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(s32_f64, RM, (kMaxInt + 0.5), kMaxInt);
  TestRoundingMode(s32_f64, RM, (kMaxInt + 1.0), kMaxInt, true);
  TestRoundingMode(s32_f64, RM, static_cast<double>(kMinInt), kMinInt);
  TestRoundingMode(s32_f64, RM, (kMinInt - 0.5), kMinInt, true);
  TestRoundingMode(s32_f64, RM, (kMinInt + 0.5), kMinInt);

  TestRoundingMode(s32_f64, RZ,  0, 0);
  TestRoundingMode(s32_f64, RZ,  0.5, 0);
  TestRoundingMode(s32_f64, RZ, -0.5, 0);
  TestRoundingMode(s32_f64, RZ,  123.7,  123);
  TestRoundingMode(s32_f64, RZ, -123.7, -123);
  TestRoundingMode(s32_f64, RZ,  123456.2,  123456);
  TestRoundingMode(s32_f64, RZ, -123456.2, -123456);
  TestRoundingMode(s32_f64, RZ, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(s32_f64, RZ, (kMaxInt + 0.5), kMaxInt);
  TestRoundingMode(s32_f64, RZ, (kMaxInt + 1.0), kMaxInt, true);
  TestRoundingMode(s32_f64, RZ, static_cast<double>(kMinInt), kMinInt);
  TestRoundingMode(s32_f64, RZ, (kMinInt - 0.5), kMinInt);
  TestRoundingMode(s32_f64, RZ, (kMinInt - 1.0), kMinInt, true);


  // u32_f64 (double to integer).

  // Negative values.
  TestRoundingMode(u32_f64, RN, -0.5, 0);
  TestRoundingMode(u32_f64, RN, -123456.7, 0, true);
  TestRoundingMode(u32_f64, RN, static_cast<double>(kMinInt), 0, true);
  TestRoundingMode(u32_f64, RN, kMinInt - 1.0, 0, true);

  TestRoundingMode(u32_f64, RM, -0.5, 0, true);
  TestRoundingMode(u32_f64, RM, -123456.7, 0, true);
  TestRoundingMode(u32_f64, RM, static_cast<double>(kMinInt), 0, true);
  TestRoundingMode(u32_f64, RM, kMinInt - 1.0, 0, true);

  TestRoundingMode(u32_f64, RZ, -0.5, 0);
  TestRoundingMode(u32_f64, RZ, -123456.7, 0, true);
  TestRoundingMode(u32_f64, RZ, static_cast<double>(kMinInt), 0, true);
  TestRoundingMode(u32_f64, RZ, kMinInt - 1.0, 0, true);

  // Positive values.
  // kMaxInt is the maximum *signed* integer: 0x7fffffff.
  static const uint32_t kMaxUInt = 0xffffffffu;
  TestRoundingMode(u32_f64, RZ,  0, 0);
  TestRoundingMode(u32_f64, RZ,  0.5, 0);
  TestRoundingMode(u32_f64, RZ,  123.7,  123);
  TestRoundingMode(u32_f64, RZ,  123456.2,  123456);
  TestRoundingMode(u32_f64, RZ, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(u32_f64, RZ, (kMaxInt + 0.5), kMaxInt);
  TestRoundingMode(u32_f64, RZ, (kMaxInt + 1.0),
                                static_cast<uint32_t>(kMaxInt) + 1);
  TestRoundingMode(u32_f64, RZ, (kMaxUInt + 0.5), kMaxUInt);
  TestRoundingMode(u32_f64, RZ, (kMaxUInt + 1.0), kMaxUInt, true);

  TestRoundingMode(u32_f64, RM,  0, 0);
  TestRoundingMode(u32_f64, RM,  0.5, 0);
  TestRoundingMode(u32_f64, RM,  123.7, 123);
  TestRoundingMode(u32_f64, RM,  123456.2,  123456);
  TestRoundingMode(u32_f64, RM, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(u32_f64, RM, (kMaxInt + 0.5), kMaxInt);
  TestRoundingMode(u32_f64, RM, (kMaxInt + 1.0),
                                static_cast<uint32_t>(kMaxInt) + 1);
  TestRoundingMode(u32_f64, RM, (kMaxUInt + 0.5), kMaxUInt);
  TestRoundingMode(u32_f64, RM, (kMaxUInt + 1.0), kMaxUInt, true);

  TestRoundingMode(u32_f64, RN,  0, 0);
  TestRoundingMode(u32_f64, RN,  0.5, 0);
  TestRoundingMode(u32_f64, RN,  1.5, 2);
  TestRoundingMode(u32_f64, RN,  123.7, 124);
  TestRoundingMode(u32_f64, RN,  123456.2,  123456);
  TestRoundingMode(u32_f64, RN, static_cast<double>(kMaxInt), kMaxInt);
  TestRoundingMode(u32_f64, RN, (kMaxInt + 0.49), kMaxInt);
  TestRoundingMode(u32_f64, RN, (kMaxInt + 0.5),
                                static_cast<uint32_t>(kMaxInt) + 1);
  TestRoundingMode(u32_f64, RN, (kMaxUInt + 0.49), kMaxUInt);
  TestRoundingMode(u32_f64, RN, (kMaxUInt + 0.5), kMaxUInt, true);
  TestRoundingMode(u32_f64, RN, (kMaxUInt + 1.0), kMaxUInt, true);
608 609
}

610

611 612
TEST(8) {
  // Test VFP multi load/store with ia_w.
613
  CcTest::InitializeVM();
614
  Isolate* isolate = CcTest::i_isolate();
615
  HandleScope scope(isolate);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
    double h;
  } D;
  D d;

  typedef struct {
    float a;
    float b;
    float c;
    float d;
    float e;
    float f;
    float g;
    float h;
  } F;
  F f;

  // Create a function that uses vldm/vstm to move some double and
  // single precision values around in memory.
643
  Assembler assm(isolate, NULL, 0);
644

645 646 647
  __ mov(ip, Operand(sp));
  __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());
  __ sub(fp, ip, Operand(4));
648

649 650 651
  __ add(r4, r0, Operand(OFFSET_OF(D, a)));
  __ vldm(ia_w, r4, d0, d3);
  __ vldm(ia_w, r4, d4, d7);
652

653 654 655
  __ add(r4, r0, Operand(OFFSET_OF(D, a)));
  __ vstm(ia_w, r4, d6, d7);
  __ vstm(ia_w, r4, d0, d5);
656

657 658 659
  __ add(r4, r1, Operand(OFFSET_OF(F, a)));
  __ vldm(ia_w, r4, s0, s3);
  __ vldm(ia_w, r4, s4, s7);
660

661 662 663
  __ add(r4, r1, Operand(OFFSET_OF(F, a)));
  __ vstm(ia_w, r4, s6, s7);
  __ vstm(ia_w, r4, s0, s5);
664

665
  __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());
666

667 668
  CodeDesc desc;
  assm.GetCode(&desc);
669 670
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
671
#ifdef DEBUG
672 673
  OFStream os(stdout);
  code->Print(os);
674
#endif
675
  F4 fn = FUNCTION_CAST<F4>(code->entry());
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  d.a = 1.1;
  d.b = 2.2;
  d.c = 3.3;
  d.d = 4.4;
  d.e = 5.5;
  d.f = 6.6;
  d.g = 7.7;
  d.h = 8.8;

  f.a = 1.0;
  f.b = 2.0;
  f.c = 3.0;
  f.d = 4.0;
  f.e = 5.0;
  f.f = 6.0;
  f.g = 7.0;
  f.h = 8.0;

  Object* dummy = CALL_GENERATED_CODE(fn, &d, &f, 0, 0, 0);
  USE(dummy);

  CHECK_EQ(7.7, d.a);
  CHECK_EQ(8.8, d.b);
  CHECK_EQ(1.1, d.c);
  CHECK_EQ(2.2, d.d);
  CHECK_EQ(3.3, d.e);
  CHECK_EQ(4.4, d.f);
  CHECK_EQ(5.5, d.g);
  CHECK_EQ(6.6, d.h);

  CHECK_EQ(7.0, f.a);
  CHECK_EQ(8.0, f.b);
  CHECK_EQ(1.0, f.c);
  CHECK_EQ(2.0, f.d);
  CHECK_EQ(3.0, f.e);
  CHECK_EQ(4.0, f.f);
  CHECK_EQ(5.0, f.g);
  CHECK_EQ(6.0, f.h);
714 715 716 717 718
}


TEST(9) {
  // Test VFP multi load/store with ia.
719
  CcTest::InitializeVM();
720
  Isolate* isolate = CcTest::i_isolate();
721
  HandleScope scope(isolate);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
    double h;
  } D;
  D d;

  typedef struct {
    float a;
    float b;
    float c;
    float d;
    float e;
    float f;
    float g;
    float h;
  } F;
  F f;

  // Create a function that uses vldm/vstm to move some double and
  // single precision values around in memory.
749
  Assembler assm(isolate, NULL, 0);
750

751 752 753
  __ mov(ip, Operand(sp));
  __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());
  __ sub(fp, ip, Operand(4));
754

755 756 757 758
  __ add(r4, r0, Operand(OFFSET_OF(D, a)));
  __ vldm(ia, r4, d0, d3);
  __ add(r4, r4, Operand(4 * 8));
  __ vldm(ia, r4, d4, d7);
759

760 761 762 763
  __ add(r4, r0, Operand(OFFSET_OF(D, a)));
  __ vstm(ia, r4, d6, d7);
  __ add(r4, r4, Operand(2 * 8));
  __ vstm(ia, r4, d0, d5);
764

765 766 767 768
  __ add(r4, r1, Operand(OFFSET_OF(F, a)));
  __ vldm(ia, r4, s0, s3);
  __ add(r4, r4, Operand(4 * 4));
  __ vldm(ia, r4, s4, s7);
769

770 771 772 773
  __ add(r4, r1, Operand(OFFSET_OF(F, a)));
  __ vstm(ia, r4, s6, s7);
  __ add(r4, r4, Operand(2 * 4));
  __ vstm(ia, r4, s0, s5);
774

775
  __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());
776

777 778
  CodeDesc desc;
  assm.GetCode(&desc);
779 780
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
781
#ifdef DEBUG
782 783
  OFStream os(stdout);
  code->Print(os);
784
#endif
785
  F4 fn = FUNCTION_CAST<F4>(code->entry());
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  d.a = 1.1;
  d.b = 2.2;
  d.c = 3.3;
  d.d = 4.4;
  d.e = 5.5;
  d.f = 6.6;
  d.g = 7.7;
  d.h = 8.8;

  f.a = 1.0;
  f.b = 2.0;
  f.c = 3.0;
  f.d = 4.0;
  f.e = 5.0;
  f.f = 6.0;
  f.g = 7.0;
  f.h = 8.0;

  Object* dummy = CALL_GENERATED_CODE(fn, &d, &f, 0, 0, 0);
  USE(dummy);

  CHECK_EQ(7.7, d.a);
  CHECK_EQ(8.8, d.b);
  CHECK_EQ(1.1, d.c);
  CHECK_EQ(2.2, d.d);
  CHECK_EQ(3.3, d.e);
  CHECK_EQ(4.4, d.f);
  CHECK_EQ(5.5, d.g);
  CHECK_EQ(6.6, d.h);

  CHECK_EQ(7.0, f.a);
  CHECK_EQ(8.0, f.b);
  CHECK_EQ(1.0, f.c);
  CHECK_EQ(2.0, f.d);
  CHECK_EQ(3.0, f.e);
  CHECK_EQ(4.0, f.f);
  CHECK_EQ(5.0, f.g);
  CHECK_EQ(6.0, f.h);
824 825 826 827 828
}


TEST(10) {
  // Test VFP multi load/store with db_w.
829
  CcTest::InitializeVM();
830
  Isolate* isolate = CcTest::i_isolate();
831
  HandleScope scope(isolate);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
    double h;
  } D;
  D d;

  typedef struct {
    float a;
    float b;
    float c;
    float d;
    float e;
    float f;
    float g;
    float h;
  } F;
  F f;

  // Create a function that uses vldm/vstm to move some double and
  // single precision values around in memory.
859
  Assembler assm(isolate, NULL, 0);
860

861 862 863
  __ mov(ip, Operand(sp));
  __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());
  __ sub(fp, ip, Operand(4));
864

865 866 867
  __ add(r4, r0, Operand(OFFSET_OF(D, h) + 8));
  __ vldm(db_w, r4, d4, d7);
  __ vldm(db_w, r4, d0, d3);
868

869 870 871
  __ add(r4, r0, Operand(OFFSET_OF(D, h) + 8));
  __ vstm(db_w, r4, d0, d5);
  __ vstm(db_w, r4, d6, d7);
872

873 874 875
  __ add(r4, r1, Operand(OFFSET_OF(F, h) + 4));
  __ vldm(db_w, r4, s4, s7);
  __ vldm(db_w, r4, s0, s3);
876

877 878 879
  __ add(r4, r1, Operand(OFFSET_OF(F, h) + 4));
  __ vstm(db_w, r4, s0, s5);
  __ vstm(db_w, r4, s6, s7);
880

881
  __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());
882

883 884
  CodeDesc desc;
  assm.GetCode(&desc);
885 886
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
887
#ifdef DEBUG
888 889
  OFStream os(stdout);
  code->Print(os);
890
#endif
891
  F4 fn = FUNCTION_CAST<F4>(code->entry());
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
  d.a = 1.1;
  d.b = 2.2;
  d.c = 3.3;
  d.d = 4.4;
  d.e = 5.5;
  d.f = 6.6;
  d.g = 7.7;
  d.h = 8.8;

  f.a = 1.0;
  f.b = 2.0;
  f.c = 3.0;
  f.d = 4.0;
  f.e = 5.0;
  f.f = 6.0;
  f.g = 7.0;
  f.h = 8.0;

  Object* dummy = CALL_GENERATED_CODE(fn, &d, &f, 0, 0, 0);
  USE(dummy);

  CHECK_EQ(7.7, d.a);
  CHECK_EQ(8.8, d.b);
  CHECK_EQ(1.1, d.c);
  CHECK_EQ(2.2, d.d);
  CHECK_EQ(3.3, d.e);
  CHECK_EQ(4.4, d.f);
  CHECK_EQ(5.5, d.g);
  CHECK_EQ(6.6, d.h);

  CHECK_EQ(7.0, f.a);
  CHECK_EQ(8.0, f.b);
  CHECK_EQ(1.0, f.c);
  CHECK_EQ(2.0, f.d);
  CHECK_EQ(3.0, f.e);
  CHECK_EQ(4.0, f.f);
  CHECK_EQ(5.0, f.g);
  CHECK_EQ(6.0, f.h);
930 931
}

932 933 934

TEST(11) {
  // Test instructions using the carry flag.
935
  CcTest::InitializeVM();
936
  Isolate* isolate = CcTest::i_isolate();
937
  HandleScope scope(isolate);
938 939 940 941 942 943 944 945 946 947 948 949

  typedef struct {
    int32_t a;
    int32_t b;
    int32_t c;
    int32_t d;
  } I;
  I i;

  i.a = 0xabcd0001;
  i.b = 0xabcd0000;

950
  Assembler assm(isolate, NULL, 0);
951 952 953 954 955 956 957 958 959 960 961 962 963 964

  // Test HeapObject untagging.
  __ ldr(r1, MemOperand(r0, OFFSET_OF(I, a)));
  __ mov(r1, Operand(r1, ASR, 1), SetCC);
  __ adc(r1, r1, Operand(r1), LeaveCC, cs);
  __ str(r1, MemOperand(r0, OFFSET_OF(I, a)));

  __ ldr(r2, MemOperand(r0, OFFSET_OF(I, b)));
  __ mov(r2, Operand(r2, ASR, 1), SetCC);
  __ adc(r2, r2, Operand(r2), LeaveCC, cs);
  __ str(r2, MemOperand(r0, OFFSET_OF(I, b)));

  // Test corner cases.
  __ mov(r1, Operand(0xffffffff));
965
  __ mov(r2, Operand::Zero());
966 967 968 969 970
  __ mov(r3, Operand(r1, ASR, 1), SetCC);  // Set the carry.
  __ adc(r3, r1, Operand(r2));
  __ str(r3, MemOperand(r0, OFFSET_OF(I, c)));

  __ mov(r1, Operand(0xffffffff));
971
  __ mov(r2, Operand::Zero());
972 973 974 975 976 977 978 979
  __ mov(r3, Operand(r2, ASR, 1), SetCC);  // Unset the carry.
  __ adc(r3, r1, Operand(r2));
  __ str(r3, MemOperand(r0, OFFSET_OF(I, d)));

  __ mov(pc, Operand(lr));

  CodeDesc desc;
  assm.GetCode(&desc);
980 981
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
982
#ifdef DEBUG
983 984
  OFStream os(stdout);
  code->Print(os);
985
#endif
986
  F3 f = FUNCTION_CAST<F3>(code->entry());
987 988 989
  Object* dummy = CALL_GENERATED_CODE(f, &i, 0, 0, 0, 0);
  USE(dummy);

990
  CHECK_EQ(static_cast<int32_t>(0xabcd0001), i.a);
991 992
  CHECK_EQ(static_cast<int32_t>(0xabcd0000) >> 1, i.b);
  CHECK_EQ(0x00000000, i.c);
993
  CHECK_EQ(static_cast<int32_t>(0xffffffff), i.d);
994 995
}

996 997 998

TEST(12) {
  // Test chaining of label usages within instructions (issue 1644).
999
  CcTest::InitializeVM();
1000
  Isolate* isolate = CcTest::i_isolate();
1001
  HandleScope scope(isolate);
1002

1003
  Assembler assm(isolate, NULL, 0);
1004 1005 1006 1007 1008 1009 1010
  Label target;
  __ b(eq, &target);
  __ b(ne, &target);
  __ bind(&target);
  __ nop();
}

1011 1012 1013

TEST(13) {
  // Test VFP instructions using registers d16-d31.
1014
  CcTest::InitializeVM();
1015
  Isolate* isolate = CcTest::i_isolate();
1016
  HandleScope scope(isolate);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

  if (!CpuFeatures::IsSupported(VFP32DREGS)) {
    return;
  }

  typedef struct {
    double a;
    double b;
    double c;
    double x;
    double y;
    double z;
    double i;
    double j;
    double k;
1032 1033
    uint32_t low;
    uint32_t high;
1034 1035 1036 1037 1038
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles and floats.
1039
  Assembler assm(isolate, NULL, 0);
1040 1041 1042 1043
  Label L, C;


  if (CpuFeatures::IsSupported(VFP3)) {
1044
    CpuFeatureScope scope(&assm, VFP3);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

    __ stm(db_w, sp, r4.bit() | lr.bit());

    // Load a, b, c into d16, d17, d18.
    __ mov(r4, Operand(r0));
    __ vldr(d16, r4, OFFSET_OF(T, a));
    __ vldr(d17, r4, OFFSET_OF(T, b));
    __ vldr(d18, r4, OFFSET_OF(T, c));

    __ vneg(d25, d16);
    __ vadd(d25, d25, d17);
    __ vsub(d25, d25, d18);
    __ vmul(d25, d25, d25);
    __ vdiv(d25, d25, d18);

    __ vmov(d16, d25);
    __ vsqrt(d17, d25);
    __ vneg(d17, d17);
    __ vabs(d17, d17);
    __ vmla(d18, d16, d17);

    // Store d16, d17, d18 into a, b, c.
    __ mov(r4, Operand(r0));
    __ vstr(d16, r4, OFFSET_OF(T, a));
    __ vstr(d17, r4, OFFSET_OF(T, b));
    __ vstr(d18, r4, OFFSET_OF(T, c));

    // Load x, y, z into d29-d31.
    __ add(r4, r0, Operand(OFFSET_OF(T, x)));
    __ vldm(ia_w, r4, d29, d31);

    // Swap d29 and d30 via r registers.
    __ vmov(r1, r2, d29);
    __ vmov(d29, d30);
    __ vmov(d30, r1, r2);

    // Convert to and from integer.
    __ vcvt_s32_f64(s1, d31);
    __ vcvt_f64_u32(d31, s1);

    // Store d29-d31 into x, y, z.
    __ add(r4, r0, Operand(OFFSET_OF(T, x)));
    __ vstm(ia_w, r4, d29, d31);

    // Move constants into d20, d21, d22 and store into i, j, k.
    __ vmov(d20, 14.7610017472335499);
    __ vmov(d21, 16.0);
    __ mov(r1, Operand(372106121));
    __ mov(r2, Operand(1079146608));
1094 1095
    __ vmov(d22, VmovIndexLo, r1);
    __ vmov(d22, VmovIndexHi, r2);
1096 1097
    __ add(r4, r0, Operand(OFFSET_OF(T, i)));
    __ vstm(ia_w, r4, d20, d22);
1098 1099 1100 1101 1102
    // Move d22 into low and high.
    __ vmov(r4, VmovIndexLo, d22);
    __ str(r4, MemOperand(r0, OFFSET_OF(T, low)));
    __ vmov(r4, VmovIndexHi, d22);
    __ str(r4, MemOperand(r0, OFFSET_OF(T, high)));
1103 1104 1105 1106 1107

    __ ldm(ia_w, sp, r4.bit() | pc.bit());

    CodeDesc desc;
    assm.GetCode(&desc);
1108 1109
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
1110
#ifdef DEBUG
1111 1112
    OFStream os(stdout);
    code->Print(os);
1113
#endif
1114
    F3 f = FUNCTION_CAST<F3>(code->entry());
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    t.a = 1.5;
    t.b = 2.75;
    t.c = 17.17;
    t.x = 1.5;
    t.y = 2.75;
    t.z = 17.17;
    Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
    USE(dummy);
    CHECK_EQ(14.7610017472335499, t.a);
    CHECK_EQ(3.84200491244266251, t.b);
    CHECK_EQ(73.8818412254460241, t.c);
    CHECK_EQ(2.75, t.x);
    CHECK_EQ(1.5, t.y);
    CHECK_EQ(17.0, t.z);
    CHECK_EQ(14.7610017472335499, t.i);
    CHECK_EQ(16.0, t.j);
    CHECK_EQ(73.8818412254460241, t.k);
1132 1133
    CHECK_EQ(372106121u, t.low);
    CHECK_EQ(1079146608u, t.high);
1134 1135 1136
  }
}

1137 1138 1139 1140

TEST(14) {
  // Test the VFP Canonicalized Nan mode.
  CcTest::InitializeVM();
1141
  Isolate* isolate = CcTest::i_isolate();
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  HandleScope scope(isolate);

  typedef struct {
    double left;
    double right;
    double add_result;
    double sub_result;
    double mul_result;
    double div_result;
  } T;
  T t;

  // Create a function that makes the four basic operations.
  Assembler assm(isolate, NULL, 0);

  // Ensure FPSCR state (as JSEntryStub does).
  Label fpscr_done;
  __ vmrs(r1);
  __ tst(r1, Operand(kVFPDefaultNaNModeControlBit));
  __ b(ne, &fpscr_done);
  __ orr(r1, r1, Operand(kVFPDefaultNaNModeControlBit));
  __ vmsr(r1);
  __ bind(&fpscr_done);

  __ vldr(d0, r0, OFFSET_OF(T, left));
  __ vldr(d1, r0, OFFSET_OF(T, right));
  __ vadd(d2, d0, d1);
  __ vstr(d2, r0, OFFSET_OF(T, add_result));
  __ vsub(d2, d0, d1);
  __ vstr(d2, r0, OFFSET_OF(T, sub_result));
  __ vmul(d2, d0, d1);
  __ vstr(d2, r0, OFFSET_OF(T, mul_result));
  __ vdiv(d2, d0, d1);
  __ vstr(d2, r0, OFFSET_OF(T, div_result));

  __ mov(pc, Operand(lr));

  CodeDesc desc;
  assm.GetCode(&desc);
1181 1182
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
1183
#ifdef DEBUG
1184 1185
  OFStream os(stdout);
  code->Print(os);
1186
#endif
1187
  F3 f = FUNCTION_CAST<F3>(code->entry());
1188
  t.left = bit_cast<double>(kHoleNanInt64);
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  t.right = 1;
  t.add_result = 0;
  t.sub_result = 0;
  t.mul_result = 0;
  t.div_result = 0;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
  const uint32_t kArmNanUpper32 = 0x7ff80000;
  const uint32_t kArmNanLower32 = 0x00000000;
#ifdef DEBUG
  const uint64_t kArmNanInt64 =
      (static_cast<uint64_t>(kArmNanUpper32) << 32) | kArmNanLower32;
1201
  DCHECK(kArmNanInt64 != kHoleNanInt64);
1202 1203 1204
#endif
  // With VFP2 the sign of the canonicalized Nan is undefined. So
  // we remove the sign bit for the upper tests.
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
  CHECK_EQ(kArmNanUpper32,
           (bit_cast<int64_t>(t.add_result) >> 32) & 0x7fffffff);
  CHECK_EQ(kArmNanLower32, bit_cast<int64_t>(t.add_result) & 0xffffffffu);
  CHECK_EQ(kArmNanUpper32,
           (bit_cast<int64_t>(t.sub_result) >> 32) & 0x7fffffff);
  CHECK_EQ(kArmNanLower32, bit_cast<int64_t>(t.sub_result) & 0xffffffffu);
  CHECK_EQ(kArmNanUpper32,
           (bit_cast<int64_t>(t.mul_result) >> 32) & 0x7fffffff);
  CHECK_EQ(kArmNanLower32, bit_cast<int64_t>(t.mul_result) & 0xffffffffu);
  CHECK_EQ(kArmNanUpper32,
           (bit_cast<int64_t>(t.div_result) >> 32) & 0x7fffffff);
  CHECK_EQ(kArmNanLower32, bit_cast<int64_t>(t.div_result) & 0xffffffffu);
1217 1218
}

1219 1220 1221 1222

TEST(15) {
  // Test the Neon instructions.
  CcTest::InitializeVM();
1223
  Isolate* isolate = CcTest::i_isolate();
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
  HandleScope scope(isolate);

  typedef struct {
    uint32_t src0;
    uint32_t src1;
    uint32_t src2;
    uint32_t src3;
    uint32_t src4;
    uint32_t src5;
    uint32_t src6;
    uint32_t src7;
    uint32_t dst0;
    uint32_t dst1;
    uint32_t dst2;
    uint32_t dst3;
    uint32_t dst4;
    uint32_t dst5;
    uint32_t dst6;
    uint32_t dst7;
    uint32_t srcA0;
    uint32_t srcA1;
    uint32_t dstA0;
    uint32_t dstA1;
    uint32_t dstA2;
    uint32_t dstA3;
1249 1250 1251 1252
    uint32_t dstA4;
    uint32_t dstA5;
    uint32_t dstA6;
    uint32_t dstA7;
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles and floats.
  Assembler assm(isolate, NULL, 0);


  if (CpuFeatures::IsSupported(NEON)) {
    CpuFeatureScope scope(&assm, NEON);

    __ stm(db_w, sp, r4.bit() | lr.bit());
    // Move 32 bytes with neon.
    __ add(r4, r0, Operand(OFFSET_OF(T, src0)));
    __ vld1(Neon8, NeonListOperand(d0, 4), NeonMemOperand(r4));
    __ add(r4, r0, Operand(OFFSET_OF(T, dst0)));
    __ vst1(Neon8, NeonListOperand(d0, 4), NeonMemOperand(r4));

    // Expand 8 bytes into 8 words(16 bits).
    __ add(r4, r0, Operand(OFFSET_OF(T, srcA0)));
    __ vld1(Neon8, NeonListOperand(d0), NeonMemOperand(r4));
    __ vmovl(NeonU8, q0, d0);
    __ add(r4, r0, Operand(OFFSET_OF(T, dstA0)));
    __ vst1(Neon8, NeonListOperand(d0, 2), NeonMemOperand(r4));

1278 1279 1280 1281 1282 1283 1284 1285
    // The same expansion, but with different source and destination registers.
    __ add(r4, r0, Operand(OFFSET_OF(T, srcA0)));
    __ vld1(Neon8, NeonListOperand(d1), NeonMemOperand(r4));
    __ vmovl(NeonU8, q1, d1);
    __ add(r4, r0, Operand(OFFSET_OF(T, dstA4)));
    __ vst1(Neon8, NeonListOperand(d2, 2), NeonMemOperand(r4));

    __ ldm(ia_w, sp, r4.bit() | pc.bit());
1286 1287 1288

    CodeDesc desc;
    assm.GetCode(&desc);
1289 1290
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
1291
#ifdef DEBUG
1292 1293
    OFStream os(stdout);
    code->Print(os);
1294
#endif
1295
    F3 f = FUNCTION_CAST<F3>(code->entry());
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    t.src0 = 0x01020304;
    t.src1 = 0x11121314;
    t.src2 = 0x21222324;
    t.src3 = 0x31323334;
    t.src4 = 0x41424344;
    t.src5 = 0x51525354;
    t.src6 = 0x61626364;
    t.src7 = 0x71727374;
    t.dst0 = 0;
    t.dst1 = 0;
    t.dst2 = 0;
    t.dst3 = 0;
    t.dst4 = 0;
    t.dst5 = 0;
    t.dst6 = 0;
    t.dst7 = 0;
    t.srcA0 = 0x41424344;
    t.srcA1 = 0x81828384;
    t.dstA0 = 0;
    t.dstA1 = 0;
    t.dstA2 = 0;
    t.dstA3 = 0;
1318 1319 1320 1321
    t.dstA4 = 0;
    t.dstA5 = 0;
    t.dstA6 = 0;
    t.dstA7 = 0;
1322 1323
    Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
    USE(dummy);
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
    CHECK_EQ(0x01020304u, t.dst0);
    CHECK_EQ(0x11121314u, t.dst1);
    CHECK_EQ(0x21222324u, t.dst2);
    CHECK_EQ(0x31323334u, t.dst3);
    CHECK_EQ(0x41424344u, t.dst4);
    CHECK_EQ(0x51525354u, t.dst5);
    CHECK_EQ(0x61626364u, t.dst6);
    CHECK_EQ(0x71727374u, t.dst7);
    CHECK_EQ(0x00430044u, t.dstA0);
    CHECK_EQ(0x00410042u, t.dstA1);
    CHECK_EQ(0x00830084u, t.dstA2);
    CHECK_EQ(0x00810082u, t.dstA3);
    CHECK_EQ(0x00430044u, t.dstA4);
    CHECK_EQ(0x00410042u, t.dstA5);
    CHECK_EQ(0x00830084u, t.dstA6);
    CHECK_EQ(0x00810082u, t.dstA7);
1340 1341 1342 1343 1344 1345 1346
  }
}


TEST(16) {
  // Test the pkh, uxtb, uxtab and uxtb16 instructions.
  CcTest::InitializeVM();
1347
  Isolate* isolate = CcTest::i_isolate();
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
  HandleScope scope(isolate);

  typedef struct {
    uint32_t src0;
    uint32_t src1;
    uint32_t src2;
    uint32_t dst0;
    uint32_t dst1;
    uint32_t dst2;
    uint32_t dst3;
    uint32_t dst4;
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles and floats.
  Assembler assm(isolate, NULL, 0);

  __ stm(db_w, sp, r4.bit() | lr.bit());

  __ mov(r4, Operand(r0));
  __ ldr(r0, MemOperand(r4, OFFSET_OF(T, src0)));
  __ ldr(r1, MemOperand(r4, OFFSET_OF(T, src1)));

  __ pkhbt(r2, r0, Operand(r1, LSL, 8));
  __ str(r2, MemOperand(r4, OFFSET_OF(T, dst0)));

  __ pkhtb(r2, r0, Operand(r1, ASR, 8));
  __ str(r2, MemOperand(r4, OFFSET_OF(T, dst1)));

1378
  __ uxtb16(r2, r0, 8);
1379 1380
  __ str(r2, MemOperand(r4, OFFSET_OF(T, dst2)));

1381
  __ uxtb(r2, r0, 8);
1382 1383 1384
  __ str(r2, MemOperand(r4, OFFSET_OF(T, dst3)));

  __ ldr(r0, MemOperand(r4, OFFSET_OF(T, src2)));
1385
  __ uxtab(r2, r0, r1, 8);
1386 1387 1388 1389 1390 1391
  __ str(r2, MemOperand(r4, OFFSET_OF(T, dst4)));

  __ ldm(ia_w, sp, r4.bit() | pc.bit());

  CodeDesc desc;
  assm.GetCode(&desc);
1392 1393
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
1394
#ifdef DEBUG
1395 1396
  OFStream os(stdout);
  code->Print(os);
1397
#endif
1398
  F3 f = FUNCTION_CAST<F3>(code->entry());
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  t.src0 = 0x01020304;
  t.src1 = 0x11121314;
  t.src2 = 0x11121300;
  t.dst0 = 0;
  t.dst1 = 0;
  t.dst2 = 0;
  t.dst3 = 0;
  t.dst4 = 0;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
1409 1410 1411 1412 1413
  CHECK_EQ(0x12130304u, t.dst0);
  CHECK_EQ(0x01021213u, t.dst1);
  CHECK_EQ(0x00010003u, t.dst2);
  CHECK_EQ(0x00000003u, t.dst3);
  CHECK_EQ(0x11121313u, t.dst4);
1414 1415
}

1416 1417 1418 1419 1420

TEST(17) {
  // Test generating labels at high addresses.
  // Should not assert.
  CcTest::InitializeVM();
1421
  Isolate* isolate = CcTest::i_isolate();
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
  HandleScope scope(isolate);

  // Generate a code segment that will be longer than 2^24 bytes.
  Assembler assm(isolate, NULL, 0);
  for (size_t i = 0; i < 1 << 23 ; ++i) {  // 2^23
    __ nop();
  }

  Label target;
  __ b(eq, &target);
  __ bind(&target);
  __ nop();
}


1437 1438 1439 1440 1441 1442 1443 1444
#define TEST_SDIV(expected_, dividend_, divisor_) \
    t.dividend = dividend_; \
    t.divisor = divisor_; \
    t.result = 0; \
    dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0); \
    CHECK_EQ(expected_, t.result);


1445
TEST(sdiv) {
1446 1447 1448 1449 1450 1451
  // Test the sdiv.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, NULL, 0);

1452 1453 1454 1455 1456 1457
  struct T {
    int32_t dividend;
    int32_t divisor;
    int32_t result;
  } t;

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
  if (CpuFeatures::IsSupported(SUDIV)) {
    CpuFeatureScope scope(&assm, SUDIV);

    __ mov(r3, Operand(r0));

    __ ldr(r0, MemOperand(r3, OFFSET_OF(T, dividend)));
    __ ldr(r1, MemOperand(r3, OFFSET_OF(T, divisor)));

    __ sdiv(r2, r0, r1);
    __ str(r2, MemOperand(r3, OFFSET_OF(T, result)));

  __ bx(lr);

    CodeDesc desc;
    assm.GetCode(&desc);
1473 1474
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
1475
#ifdef DEBUG
1476 1477
    OFStream os(stdout);
    code->Print(os);
1478
#endif
1479
    F3 f = FUNCTION_CAST<F3>(code->entry());
1480
    Object* dummy;
1481 1482
    TEST_SDIV(0, kMinInt, 0);
    TEST_SDIV(0, 1024, 0);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
    TEST_SDIV(1073741824, kMinInt, -2);
    TEST_SDIV(kMinInt, kMinInt, -1);
    TEST_SDIV(5, 10, 2);
    TEST_SDIV(3, 10, 3);
    TEST_SDIV(-5, 10, -2);
    TEST_SDIV(-3, 10, -3);
    TEST_SDIV(-5, -10, 2);
    TEST_SDIV(-3, -10, 3);
    TEST_SDIV(5, -10, -2);
    TEST_SDIV(3, -10, -3);
    USE(dummy);
  }
}


#undef TEST_SDIV


1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
#define TEST_UDIV(expected_, dividend_, divisor_) \
  t.dividend = dividend_;                         \
  t.divisor = divisor_;                           \
  t.result = 0;                                   \
  dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0); \
  CHECK_EQ(expected_, t.result);


TEST(udiv) {
  // Test the udiv.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, NULL, 0);

  struct T {
    uint32_t dividend;
    uint32_t divisor;
    uint32_t result;
  } t;

  if (CpuFeatures::IsSupported(SUDIV)) {
    CpuFeatureScope scope(&assm, SUDIV);

    __ mov(r3, Operand(r0));

    __ ldr(r0, MemOperand(r3, OFFSET_OF(T, dividend)));
    __ ldr(r1, MemOperand(r3, OFFSET_OF(T, divisor)));

    __ sdiv(r2, r0, r1);
    __ str(r2, MemOperand(r3, OFFSET_OF(T, result)));

    __ bx(lr);

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef DEBUG
    OFStream os(stdout);
    code->Print(os);
#endif
    F3 f = FUNCTION_CAST<F3>(code->entry());
    Object* dummy;
1545 1546 1547 1548
    TEST_UDIV(0u, 0, 0);
    TEST_UDIV(0u, 1024, 0);
    TEST_UDIV(5u, 10, 2);
    TEST_UDIV(3u, 10, 3);
1549 1550 1551 1552 1553 1554 1555 1556
    USE(dummy);
  }
}


#undef TEST_UDIV


1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
TEST(smmla) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ smmla(r1, r1, r2, r3);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt(), z = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, z, 0);
    CHECK_EQ(bits::SignedMulHighAndAdd32(x, y, z), r);
    USE(dummy);
  }
}


TEST(smmul) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ smmul(r1, r1, r2);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, 0, 0);
    CHECK_EQ(bits::SignedMulHigh32(x, y), r);
    USE(dummy);
  }
}


1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
TEST(sxtb) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ sxtb(r1, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, 0, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<int8_t>(x)), r);
    USE(dummy);
  }
}


TEST(sxtab) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ sxtab(r1, r2, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<int8_t>(x)) + y, r);
    USE(dummy);
  }
}


TEST(sxth) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ sxth(r1, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, 0, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<int16_t>(x)), r);
    USE(dummy);
  }
}


TEST(sxtah) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ sxtah(r1, r2, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<int16_t>(x)) + y, r);
    USE(dummy);
  }
}


TEST(uxtb) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ uxtb(r1, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, 0, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<uint8_t>(x)), r);
    USE(dummy);
  }
}


TEST(uxtab) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ uxtab(r1, r2, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<uint8_t>(x)) + y, r);
    USE(dummy);
  }
}


TEST(uxth) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ uxth(r1, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, 0, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<uint16_t>(x)), r);
    USE(dummy);
  }
}


TEST(uxtah) {
  CcTest::InitializeVM();
  Isolate* const isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  RandomNumberGenerator* const rng = isolate->random_number_generator();
  Assembler assm(isolate, nullptr, 0);
  __ uxtah(r1, r2, r1);
  __ str(r1, MemOperand(r0));
  __ bx(lr);
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (size_t i = 0; i < 128; ++i) {
    int32_t r, x = rng->NextInt(), y = rng->NextInt();
    Object* dummy = CALL_GENERATED_CODE(f, &r, x, y, 0, 0);
    CHECK_EQ(static_cast<int32_t>(static_cast<uint16_t>(x)) + y, r);
    USE(dummy);
  }
}


1817 1818 1819 1820
TEST(code_relative_offset) {
  // Test extracting the offset of a label from the beginning of the code
  // in a register.
  CcTest::InitializeVM();
1821
  Isolate* isolate = CcTest::i_isolate();
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
  HandleScope scope(isolate);
  // Initialize a code object that will contain the code.
  Handle<Object> code_object(isolate->heap()->undefined_value(), isolate);

  Assembler assm(isolate, NULL, 0);

  Label start, target_away, target_faraway;

  __ stm(db_w, sp, r4.bit() | r5.bit() | lr.bit());

  // r3 is used as the address zero, the test will crash when we load it.
  __ mov(r3, Operand::Zero());

  // r5 will be a pointer to the start of the code.
  __ mov(r5, Operand(code_object));
  __ mov_label_offset(r4, &start);

  __ mov_label_offset(r1, &target_faraway);
  __ str(r1, MemOperand(sp, kPointerSize, NegPreIndex));

  __ mov_label_offset(r1, &target_away);

  // Jump straight to 'target_away' the first time and use the relative
  // position the second time. This covers the case when extracting the
  // position of a label which is linked.
  __ mov(r2, Operand::Zero());
  __ bind(&start);
  __ cmp(r2, Operand::Zero());
  __ b(eq, &target_away);
  __ add(pc, r5, r1);
  // Emit invalid instructions to push the label between 2^8 and 2^16
  // instructions away. The test will crash if they are reached.
  for (int i = 0; i < (1 << 10); i++) {
    __ ldr(r3, MemOperand(r3));
  }
  __ bind(&target_away);
  // This will be hit twice: r0 = r0 + 5 + 5.
  __ add(r0, r0, Operand(5));

  __ ldr(r1, MemOperand(sp, kPointerSize, PostIndex), ne);
  __ add(pc, r5, r4, LeaveCC, ne);

  __ mov(r2, Operand(1));
  __ b(&start);
  // Emit invalid instructions to push the label between 2^16 and 2^24
  // instructions away. The test will crash if they are reached.
  for (int i = 0; i < (1 << 21); i++) {
    __ ldr(r3, MemOperand(r3));
  }
  __ bind(&target_faraway);
  // r0 = r0 + 5 + 5 + 11
  __ add(r0, r0, Operand(11));

  __ ldm(ia_w, sp, r4.bit() | r5.bit() | pc.bit());

  CodeDesc desc;
  assm.GetCode(&desc);
1879 1880
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), code_object);
1881 1882 1883 1884 1885 1886
  F1 f = FUNCTION_CAST<F1>(code->entry());
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 21, 0, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(42, res);
}

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

TEST(ARMv8_vrintX) {
  // Test the vrintX floating point instructions.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double input;
    double ar;
    double nr;
    double mr;
    double pr;
    double zr;
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles and floats.
  Assembler assm(isolate, NULL, 0);
  Label L, C;


  if (CpuFeatures::IsSupported(ARMv8)) {
    CpuFeatureScope scope(&assm, ARMv8);

    __ mov(ip, Operand(sp));
    __ stm(db_w, sp, r4.bit() | fp.bit() | lr.bit());

    __ mov(r4, Operand(r0));

    // Test vrinta
    __ vldr(d6, r4, OFFSET_OF(T, input));
    __ vrinta(d5, d6);
    __ vstr(d5, r4, OFFSET_OF(T, ar));

    // Test vrintn
    __ vldr(d6, r4, OFFSET_OF(T, input));
    __ vrintn(d5, d6);
    __ vstr(d5, r4, OFFSET_OF(T, nr));

    // Test vrintp
    __ vldr(d6, r4, OFFSET_OF(T, input));
    __ vrintp(d5, d6);
    __ vstr(d5, r4, OFFSET_OF(T, pr));

    // Test vrintm
    __ vldr(d6, r4, OFFSET_OF(T, input));
    __ vrintm(d5, d6);
    __ vstr(d5, r4, OFFSET_OF(T, mr));

    // Test vrintz
    __ vldr(d6, r4, OFFSET_OF(T, input));
    __ vrintz(d5, d6);
    __ vstr(d5, r4, OFFSET_OF(T, zr));

    __ ldm(ia_w, sp, r4.bit() | fp.bit() | pc.bit());

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef DEBUG
    OFStream os(stdout);
    code->Print(os);
#endif
    F3 f = FUNCTION_CAST<F3>(code->entry());

    Object* dummy = nullptr;
    USE(dummy);

#define CHECK_VRINT(input_val, ares, nres, mres, pres, zres) \
  t.input = input_val;                                       \
  dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);            \
  CHECK_EQ(ares, t.ar);                                      \
  CHECK_EQ(nres, t.nr);                                      \
  CHECK_EQ(mres, t.mr);                                      \
  CHECK_EQ(pres, t.pr);                                      \
  CHECK_EQ(zres, t.zr);

    CHECK_VRINT(-0.5, -1.0, -0.0, -1.0, -0.0, -0.0)
    CHECK_VRINT(-0.6, -1.0, -1.0, -1.0, -0.0, -0.0)
    CHECK_VRINT(-1.1, -1.0, -1.0, -2.0, -1.0, -1.0)
    CHECK_VRINT(0.5, 1.0, 0.0, 0.0, 1.0, 0.0)
    CHECK_VRINT(0.6, 1.0, 1.0, 0.0, 1.0, 0.0)
    CHECK_VRINT(1.1, 1.0, 1.0, 1.0, 2.0, 1.0)
    double inf = std::numeric_limits<double>::infinity();
    CHECK_VRINT(inf, inf, inf, inf, inf, inf)
    CHECK_VRINT(-inf, -inf, -inf, -inf, -inf, -inf)
    CHECK_VRINT(-0.0, -0.0, -0.0, -0.0, -0.0, -0.0)
    double nan = std::numeric_limits<double>::quiet_NaN();
    CHECK_VRINT(nan, nan, nan, nan, nan, nan)

#undef CHECK_VRINT
  }
}
1983
#undef __