test-assembler-mips.cc 33.7 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// 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.

#include "v8.h"

#include "disassembler.h"
#include "factory.h"
#include "macro-assembler.h"
#include "mips/macro-assembler-mips.h"
#include "mips/simulator-mips.h"

#include "cctest.h"

using namespace v8::internal;


// Define these function prototypes to match JSEntryFunction in execution.cc.
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);
typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4);


#define __ assm.

49

50
TEST(MIPS0) {
51
  CcTest::InitializeVM();
52 53
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
54

55
  MacroAssembler assm(isolate, NULL, 0);
56 57 58 59 60 61 62 63

  // Addition.
  __ addu(v0, a0, a1);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
64 65 66
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
67
      Handle<Code>())->ToObjectChecked();
68 69 70 71 72 73 74 75 76
  CHECK(code->IsCode());
  F2 f = FUNCTION_CAST<F2>(Code::cast(code)->entry());
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 0xab0, 0xc, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(0xabc, res);
}


TEST(MIPS1) {
77
  CcTest::InitializeVM();
78 79
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
80

81
  MacroAssembler assm(isolate, NULL, 0);
82 83 84 85 86 87 88 89
  Label L, C;

  __ mov(a1, a0);
  __ li(v0, 0);
  __ b(&C);
  __ nop();

  __ bind(&L);
90
  __ addu(v0, v0, a1);
91 92 93 94
  __ addiu(a1, a1, -1);

  __ bind(&C);
  __ xori(v1, a1, 0);
95
  __ Branch(&L, ne, v1, Operand(0));
96 97 98 99 100 101 102
  __ nop();

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
103 104 105
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
106
      Handle<Code>())->ToObjectChecked();
107 108 109 110 111 112 113 114 115
  CHECK(code->IsCode());
  F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry());
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 50, 0, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(1275, res);
}


TEST(MIPS2) {
116
  CcTest::InitializeVM();
117 118
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
119

120
  MacroAssembler assm(isolate, NULL, 0);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  Label exit, error;

  // ----- Test all instructions.

  // Test lui, ori, and addiu, used in the li pseudo-instruction.
  // This way we can then safely load registers with chosen values.

  __ ori(t0, zero_reg, 0);
  __ lui(t0, 0x1234);
  __ ori(t0, t0, 0);
  __ ori(t0, t0, 0x0f0f);
  __ ori(t0, t0, 0xf0f0);
  __ addiu(t1, t0, 1);
  __ addiu(t2, t1, -0x10);

  // Load values in temporary registers.
  __ li(t0, 0x00000004);
  __ li(t1, 0x00001234);
  __ li(t2, 0x12345678);
  __ li(t3, 0x7fffffff);
  __ li(t4, 0xfffffffc);
  __ li(t5, 0xffffedcc);
  __ li(t6, 0xedcba988);
  __ li(t7, 0x80000000);

  // SPECIAL class.
  __ srl(v0, t2, 8);    // 0x00123456
  __ sll(v0, v0, 11);   // 0x91a2b000
  __ sra(v0, v0, 3);    // 0xf2345600
  __ srav(v0, v0, t0);  // 0xff234560
  __ sllv(v0, v0, t0);  // 0xf2345600
  __ srlv(v0, v0, t0);  // 0x0f234560
154
  __ Branch(&error, ne, v0, Operand(0x0f234560));
155 156
  __ nop();

157 158 159
  __ addu(v0, t0, t1);   // 0x00001238
  __ subu(v0, v0, t0);  // 0x00001234
  __ Branch(&error, ne, v0, Operand(0x00001234));
160 161
  __ nop();
  __ addu(v1, t3, t0);
162
  __ Branch(&error, ne, v1, Operand(0x80000003));
163 164
  __ nop();
  __ subu(v1, t7, t0);  // 0x7ffffffc
165
  __ Branch(&error, ne, v1, Operand(0x7ffffffc));
166 167 168 169 170 171
  __ nop();

  __ and_(v0, t1, t2);  // 0x00001230
  __ or_(v0, v0, t1);   // 0x00001234
  __ xor_(v0, v0, t2);  // 0x1234444c
  __ nor(v0, v0, t2);   // 0xedcba987
172
  __ Branch(&error, ne, v0, Operand(0xedcba983));
173 174 175
  __ nop();

  __ slt(v0, t7, t3);
176
  __ Branch(&error, ne, v0, Operand(0x1));
177 178
  __ nop();
  __ sltu(v0, t7, t3);
179
  __ Branch(&error, ne, v0, Operand(0x0));
180 181 182
  __ nop();
  // End of SPECIAL class.

183 184
  __ addiu(v0, zero_reg, 0x7421);  // 0x00007421
  __ addiu(v0, v0, -0x1);  // 0x00007420
185
  __ addiu(v0, v0, -0x20);  // 0x00007400
186
  __ Branch(&error, ne, v0, Operand(0x00007400));
187 188
  __ nop();
  __ addiu(v1, t3, 0x1);  // 0x80000000
189
  __ Branch(&error, ne, v1, Operand(0x80000000));
190 191 192 193
  __ nop();

  __ slti(v0, t1, 0x00002000);  // 0x1
  __ slti(v0, v0, 0xffff8000);  // 0x0
194
  __ Branch(&error, ne, v0, Operand(0x0));
195 196 197
  __ nop();
  __ sltiu(v0, t1, 0x00002000);  // 0x1
  __ sltiu(v0, v0, 0x00008000);  // 0x1
198
  __ Branch(&error, ne, v0, Operand(0x1));
199 200 201 202 203
  __ nop();

  __ andi(v0, t1, 0xf0f0);  // 0x00001030
  __ ori(v0, v0, 0x8a00);  // 0x00009a30
  __ xori(v0, v0, 0x83cc);  // 0x000019fc
204
  __ Branch(&error, ne, v0, Operand(0x000019fc));
205 206
  __ nop();
  __ lui(v1, 0x8123);  // 0x81230000
207
  __ Branch(&error, ne, v1, Operand(0x81230000));
208 209
  __ nop();

210 211
  // Bit twiddling instructions & conditional moves.
  // Uses t0-t7 as set above.
212 213
  __ Clz(v0, t0);       // 29
  __ Clz(v1, t1);       // 19
214
  __ addu(v0, v0, v1);  // 48
215
  __ Clz(v1, t2);       // 3
216
  __ addu(v0, v0, v1);  // 51
217
  __ Clz(v1, t7);       // 0
218 219
  __ addu(v0, v0, v1);  // 51
  __ Branch(&error, ne, v0, Operand(51));
220
  __ Movn(a0, t3, t0);  // Move a0<-t3 (t0 is NOT 0).
221 222
  __ Ins(a0, t1, 12, 8);  // 0x7ff34fff
  __ Branch(&error, ne, a0, Operand(0x7ff34fff));
223
  __ Movz(a0, t6, t7);    // a0 not updated (t7 is NOT 0).
224 225
  __ Ext(a1, a0, 8, 12);  // 0x34f
  __ Branch(&error, ne, a1, Operand(0x34f));
226
  __ Movz(a0, t6, v1);    // a0<-t6, v0 is 0, from 8 instr back.
227 228
  __ Branch(&error, ne, a0, Operand(t6));

229 230 231 232 233 234 235
  // Everything was correctly executed. Load the expected result.
  __ li(v0, 0x31415926);
  __ b(&exit);
  __ nop();

  __ bind(&error);
  // Got an error. Return a wrong result.
236
  __ li(v0, 666);
237 238 239 240 241 242 243

  __ bind(&exit);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
244 245 246
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
247
      Handle<Code>())->ToObjectChecked();
248 249 250 251 252 253 254
  CHECK(code->IsCode());
  F2 f = FUNCTION_CAST<F2>(Code::cast(code)->entry());
  int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, 0xab0, 0xc, 0, 0, 0));
  ::printf("f() = %d\n", res);
  CHECK_EQ(0x31415926, res);
}

255 256 257

TEST(MIPS3) {
  // Test floating point instructions.
258
  CcTest::InitializeVM();
259 260
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
261 262 263 264 265 266 267 268 269

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
270 271
    double h;
    double i;
272 273 274 275 276
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles t.a ... t.f.
277
  MacroAssembler assm(isolate, NULL, 0);
278 279
  Label L, C;

280 281 282 283
  __ ldc1(f4, MemOperand(a0, OFFSET_OF(T, a)) );
  __ ldc1(f6, MemOperand(a0, OFFSET_OF(T, b)) );
  __ add_d(f8, f4, f6);
  __ sdc1(f8, MemOperand(a0, OFFSET_OF(T, c)) );  // c = a + b.
284

285 286 287 288
  __ mov_d(f10, f8);  // c
  __ neg_d(f12, f6);  // -b
  __ sub_d(f10, f10, f12);
  __ sdc1(f10, MemOperand(a0, OFFSET_OF(T, d)) );  // d = c - (-b).
289

290
  __ sdc1(f4, MemOperand(a0, OFFSET_OF(T, b)) );   // b = a.
291

292 293 294 295 296
  __ li(t0, 120);
  __ mtc1(t0, f14);
  __ cvt_d_w(f14, f14);   // f14 = 120.0.
  __ mul_d(f10, f10, f14);
  __ sdc1(f10, MemOperand(a0, OFFSET_OF(T, e)) );  // e = d * 120 = 1.8066e16.
297

298 299
  __ div_d(f12, f10, f4);
  __ sdc1(f12, MemOperand(a0, OFFSET_OF(T, f)) );  // f = e / a = 120.44.
300

301 302 303
  __ sqrt_d(f14, f12);
  __ sdc1(f14, MemOperand(a0, OFFSET_OF(T, g)) );
  // g = sqrt(f) = 10.97451593465515908537
304

305 306 307 308 309 310 311
  if (kArchVariant == kMips32r2) {
    __ ldc1(f4, MemOperand(a0, OFFSET_OF(T, h)) );
    __ ldc1(f6, MemOperand(a0, OFFSET_OF(T, i)) );
    __ madd_d(f14, f6, f4, f6);
    __ sdc1(f14, MemOperand(a0, OFFSET_OF(T, h)) );
  }

312 313
  __ jr(ra);
  __ nop();
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.a = 1.5e14;
  t.b = 2.75e11;
  t.c = 0.0;
  t.d = 0.0;
  t.e = 0.0;
  t.f = 0.0;
  t.h = 1.5;
  t.i = 2.75;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
  CHECK_EQ(1.5e14, t.a);
  CHECK_EQ(1.5e14, t.b);
  CHECK_EQ(1.50275e14, t.c);
  CHECK_EQ(1.50550e14, t.d);
  CHECK_EQ(1.8066e16, t.e);
  CHECK_EQ(120.44, t.f);
  CHECK_EQ(10.97451593465515908537, t.g);
340 341 342
  if (kArchVariant == kMips32r2) {
    CHECK_EQ(6.875, t.h);
  }
343 344 345 346 347
}


TEST(MIPS4) {
  // Test moves between floating point and integer registers.
348
  CcTest::InitializeVM();
349 350
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
351 352 353 354 355 356 357 358

  typedef struct {
    double a;
    double b;
    double c;
  } T;
  T t;

359
  Assembler assm(isolate, NULL, 0);
360 361
  Label L, C;

362 363
  __ ldc1(f4, MemOperand(a0, OFFSET_OF(T, a)) );
  __ ldc1(f6, MemOperand(a0, OFFSET_OF(T, b)) );
364

365 366 367 368 369
  // Swap f4 and f6, by using four integer registers, t0-t3.
  __ mfc1(t0, f4);
  __ mfc1(t1, f5);
  __ mfc1(t2, f6);
  __ mfc1(t3, f7);
370

371 372 373 374
  __ mtc1(t0, f6);
  __ mtc1(t1, f7);
  __ mtc1(t2, f4);
  __ mtc1(t3, f5);
375

376 377 378
  // Store the swapped f4 and f5 back to memory.
  __ sdc1(f4, MemOperand(a0, OFFSET_OF(T, a)) );
  __ sdc1(f6, MemOperand(a0, OFFSET_OF(T, c)) );
379

380 381
  __ jr(ra);
  __ nop();
382

383 384 385 386 387 388 389 390 391 392 393 394 395
  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.a = 1.5e22;
  t.b = 2.75e11;
  t.c = 17.17;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
396

397 398 399
  CHECK_EQ(2.75e11, t.a);
  CHECK_EQ(2.75e11, t.b);
  CHECK_EQ(1.5e22, t.c);
400 401 402 403 404
}


TEST(MIPS5) {
  // Test conversions between doubles and integers.
405
  CcTest::InitializeVM();
406 407
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
408 409 410 411 412 413 414 415 416

  typedef struct {
    double a;
    double b;
    int i;
    int j;
  } T;
  T t;

417
  Assembler assm(isolate, NULL, 0);
418 419
  Label L, C;

420 421 422 423 424
  // Load all structure elements to registers.
  __ ldc1(f4, MemOperand(a0, OFFSET_OF(T, a)) );
  __ ldc1(f6, MemOperand(a0, OFFSET_OF(T, b)) );
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, i)) );
  __ lw(t1, MemOperand(a0, OFFSET_OF(T, j)) );
425

426 427 428 429
  // Convert double in f4 to int in element i.
  __ cvt_w_d(f8, f4);
  __ mfc1(t2, f8);
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, i)) );
430

431 432 433 434
  // Convert double in f6 to int in element j.
  __ cvt_w_d(f10, f6);
  __ mfc1(t3, f10);
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, j)) );
435

436 437 438 439
  // Convert int in original i (t0) to double in a.
  __ mtc1(t0, f12);
  __ cvt_d_w(f0, f12);
  __ sdc1(f0, MemOperand(a0, OFFSET_OF(T, a)) );
440

441 442 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
  // Convert int in original j (t1) to double in b.
  __ mtc1(t1, f14);
  __ cvt_d_w(f2, f14);
  __ sdc1(f2, MemOperand(a0, OFFSET_OF(T, b)) );

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.a = 1.5e4;
  t.b = 2.75e8;
  t.i = 12345678;
  t.j = -100000;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);

  CHECK_EQ(12345678.0, t.a);
  CHECK_EQ(-100000.0, t.b);
  CHECK_EQ(15000, t.i);
  CHECK_EQ(275000000, t.j);
468 469 470 471 472
}


TEST(MIPS6) {
  // Test simple memory loads and stores.
473
  CcTest::InitializeVM();
474 475
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
476 477 478 479 480 481 482 483 484 485 486 487 488

  typedef struct {
    uint32_t ui;
    int32_t si;
    int32_t r1;
    int32_t r2;
    int32_t r3;
    int32_t r4;
    int32_t r5;
    int32_t r6;
  } T;
  T t;

489
  Assembler assm(isolate, NULL, 0);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  Label L, C;

  // Basic word load/store.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, ui)) );
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, r1)) );

  // lh with positive data.
  __ lh(t1, MemOperand(a0, OFFSET_OF(T, ui)) );
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, r2)) );

  // lh with negative data.
  __ lh(t2, MemOperand(a0, OFFSET_OF(T, si)) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, r3)) );

  // lhu with negative data.
  __ lhu(t3, MemOperand(a0, OFFSET_OF(T, si)) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, r4)) );

  // lb with negative data.
  __ lb(t4, MemOperand(a0, OFFSET_OF(T, si)) );
  __ sw(t4, MemOperand(a0, OFFSET_OF(T, r5)) );

  // sh writes only 1/2 of word.
  __ lui(t5, 0x3333);
  __ ori(t5, t5, 0x3333);
  __ sw(t5, MemOperand(a0, OFFSET_OF(T, r6)) );
  __ lhu(t5, MemOperand(a0, OFFSET_OF(T, si)) );
  __ sh(t5, MemOperand(a0, OFFSET_OF(T, r6)) );

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
527
      Handle<Code>())->ToObjectChecked();
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.ui = 0x11223344;
  t.si = 0x99aabbcc;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);

  CHECK_EQ(0x11223344, t.r1);
  CHECK_EQ(0x3344, t.r2);
  CHECK_EQ(0xffffbbcc, t.r3);
  CHECK_EQ(0x0000bbcc, t.r4);
  CHECK_EQ(0xffffffcc, t.r5);
  CHECK_EQ(0x3333bbcc, t.r6);
}


TEST(MIPS7) {
  // Test floating point compare and branch instructions.
546
  CcTest::InitializeVM();
547 548
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
549 550 551 552 553 554 555 556 557 558 559 560 561 562

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    int32_t result;
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles t.a ... t.f.
563
  MacroAssembler assm(isolate, NULL, 0);
564 565
  Label neither_is_nan, less_than, outa_here;

566 567 568 569 570 571 572
  __ ldc1(f4, MemOperand(a0, OFFSET_OF(T, a)) );
  __ ldc1(f6, MemOperand(a0, OFFSET_OF(T, b)) );
  __ c(UN, D, f4, f6);
  __ bc1f(&neither_is_nan);
  __ nop();
  __ sw(zero_reg, MemOperand(a0, OFFSET_OF(T, result)) );
  __ Branch(&outa_here);
573

574
  __ bind(&neither_is_nan);
575

576 577 578 579 580 581 582 583 584 585
  if (kArchVariant == kLoongson) {
    __ c(OLT, D, f6, f4);
    __ bc1t(&less_than);
  } else {
    __ c(OLT, D, f6, f4, 2);
    __ bc1t(&less_than, 2);
  }
  __ nop();
  __ sw(zero_reg, MemOperand(a0, OFFSET_OF(T, result)) );
  __ Branch(&outa_here);
586

587 588 589
  __ bind(&less_than);
  __ Addu(t0, zero_reg, Operand(1));
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, result)) );  // Set true.
590 591


592
  // This test-case should have additional tests.
593

594
  __ bind(&outa_here);
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.a = 1.5e14;
  t.b = 2.75e11;
  t.c = 2.0;
  t.d = -4.0;
  t.e = 0.0;
  t.f = 0.0;
  t.result = 0;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
  CHECK_EQ(1.5e14, t.a);
  CHECK_EQ(2.75e11, t.b);
  CHECK_EQ(1, t.result);
619 620 621 622 623
}


TEST(MIPS8) {
  // Test ROTR and ROTRV instructions.
624
  CcTest::InitializeVM();
625 626
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

  typedef struct {
    int32_t input;
    int32_t result_rotr_4;
    int32_t result_rotr_8;
    int32_t result_rotr_12;
    int32_t result_rotr_16;
    int32_t result_rotr_20;
    int32_t result_rotr_24;
    int32_t result_rotr_28;
    int32_t result_rotrv_4;
    int32_t result_rotrv_8;
    int32_t result_rotrv_12;
    int32_t result_rotrv_16;
    int32_t result_rotrv_20;
    int32_t result_rotrv_24;
    int32_t result_rotrv_28;
  } T;
  T t;

647
  MacroAssembler assm(isolate, NULL, 0);
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 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

  // Basic word load.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, input)) );

  // ROTR instruction (called through the Ror macro).
  __ Ror(t1, t0, 0x0004);
  __ Ror(t2, t0, 0x0008);
  __ Ror(t3, t0, 0x000c);
  __ Ror(t4, t0, 0x0010);
  __ Ror(t5, t0, 0x0014);
  __ Ror(t6, t0, 0x0018);
  __ Ror(t7, t0, 0x001c);

  // Basic word store.
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, result_rotr_4)) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, result_rotr_8)) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, result_rotr_12)) );
  __ sw(t4, MemOperand(a0, OFFSET_OF(T, result_rotr_16)) );
  __ sw(t5, MemOperand(a0, OFFSET_OF(T, result_rotr_20)) );
  __ sw(t6, MemOperand(a0, OFFSET_OF(T, result_rotr_24)) );
  __ sw(t7, MemOperand(a0, OFFSET_OF(T, result_rotr_28)) );

  // ROTRV instruction (called through the Ror macro).
  __ li(t7, 0x0004);
  __ Ror(t1, t0, t7);
  __ li(t7, 0x0008);
  __ Ror(t2, t0, t7);
  __ li(t7, 0x000C);
  __ Ror(t3, t0, t7);
  __ li(t7, 0x0010);
  __ Ror(t4, t0, t7);
  __ li(t7, 0x0014);
  __ Ror(t5, t0, t7);
  __ li(t7, 0x0018);
  __ Ror(t6, t0, t7);
  __ li(t7, 0x001C);
  __ Ror(t7, t0, t7);

  // Basic word store.
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, result_rotrv_4)) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, result_rotrv_8)) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, result_rotrv_12)) );
  __ sw(t4, MemOperand(a0, OFFSET_OF(T, result_rotrv_16)) );
  __ sw(t5, MemOperand(a0, OFFSET_OF(T, result_rotrv_20)) );
  __ sw(t6, MemOperand(a0, OFFSET_OF(T, result_rotrv_24)) );
  __ sw(t7, MemOperand(a0, OFFSET_OF(T, result_rotrv_28)) );

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
703
      Handle<Code>())->ToObjectChecked();
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.input = 0x12345678;
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0x0, 0, 0, 0);
  USE(dummy);
  CHECK_EQ(0x81234567, t.result_rotr_4);
  CHECK_EQ(0x78123456, t.result_rotr_8);
  CHECK_EQ(0x67812345, t.result_rotr_12);
  CHECK_EQ(0x56781234, t.result_rotr_16);
  CHECK_EQ(0x45678123, t.result_rotr_20);
  CHECK_EQ(0x34567812, t.result_rotr_24);
  CHECK_EQ(0x23456781, t.result_rotr_28);

  CHECK_EQ(0x81234567, t.result_rotrv_4);
  CHECK_EQ(0x78123456, t.result_rotrv_8);
  CHECK_EQ(0x67812345, t.result_rotrv_12);
  CHECK_EQ(0x56781234, t.result_rotrv_16);
  CHECK_EQ(0x45678123, t.result_rotrv_20);
  CHECK_EQ(0x34567812, t.result_rotrv_24);
  CHECK_EQ(0x23456781, t.result_rotrv_28);
}


TEST(MIPS9) {
  // Test BRANCH improvements.
729
  CcTest::InitializeVM();
730 731
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
732

733
  MacroAssembler assm(isolate, NULL, 0);
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  Label exit, exit2, exit3;

  __ Branch(&exit, ge, a0, Operand(0x00000000));
  __ Branch(&exit2, ge, a0, Operand(0x00001FFF));
  __ Branch(&exit3, ge, a0, Operand(0x0001FFFF));

  __ bind(&exit);
  __ bind(&exit2);
  __ bind(&exit3);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
751
      Handle<Code>())->ToObjectChecked();
752 753 754 755 756 757 758
  CHECK(code->IsCode());
}


TEST(MIPS10) {
  // Test conversions between doubles and long integers.
  // Test hos the long ints map to FP regs pairs.
759
  CcTest::InitializeVM();
760 761
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
762 763 764 765 766 767 768 769 770 771 772 773 774

  typedef struct {
    double a;
    double b;
    int32_t dbl_mant;
    int32_t dbl_exp;
    int32_t long_hi;
    int32_t long_lo;
    int32_t b_long_hi;
    int32_t b_long_lo;
  } T;
  T t;

775
  Assembler assm(isolate, NULL, 0);
776 777
  Label L, C;

778
  if (kArchVariant == kMips32r2) {
779 780 781 782 783 784 785 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
    // Load all structure elements to registers.
    __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, a)));

    // Save the raw bits of the double.
    __ mfc1(t0, f0);
    __ mfc1(t1, f1);
    __ sw(t0, MemOperand(a0, OFFSET_OF(T, dbl_mant)));
    __ sw(t1, MemOperand(a0, OFFSET_OF(T, dbl_exp)));

    // Convert double in f0 to long, save hi/lo parts.
    __ cvt_l_d(f0, f0);
    __ mfc1(t0, f0);  // f0 has LS 32 bits of long.
    __ mfc1(t1, f1);  // f1 has MS 32 bits of long.
    __ sw(t0, MemOperand(a0, OFFSET_OF(T, long_lo)));
    __ sw(t1, MemOperand(a0, OFFSET_OF(T, long_hi)));

    // Convert the b long integers to double b.
    __ lw(t0, MemOperand(a0, OFFSET_OF(T, b_long_lo)));
    __ lw(t1, MemOperand(a0, OFFSET_OF(T, b_long_hi)));
    __ mtc1(t0, f8);  // f8 has LS 32-bits.
    __ mtc1(t1, f9);  // f9 has MS 32-bits.
    __ cvt_d_l(f10, f8);
    __ sdc1(f10, MemOperand(a0, OFFSET_OF(T, b)));

    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Object* code = HEAP->CreateCode(
        desc,
        Code::ComputeFlags(Code::STUB),
811
        Handle<Code>())->ToObjectChecked();
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    CHECK(code->IsCode());
    F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
    t.a = 2.147483647e9;       // 0x7fffffff -> 0x41DFFFFFFFC00000 as double.
    t.b_long_hi = 0x000000ff;  // 0xFF00FF00FF -> 0x426FE01FE01FE000 as double.
    t.b_long_lo = 0x00ff00ff;
    Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
    USE(dummy);

    CHECK_EQ(0x41DFFFFF, t.dbl_exp);
    CHECK_EQ(0xFFC00000, t.dbl_mant);
    CHECK_EQ(0, t.long_hi);
    CHECK_EQ(0x7fffffff, t.long_lo);
    // 0xFF00FF00FF -> 1.095233372415e12.
    CHECK_EQ(1.095233372415e12, t.b);
  }
}


TEST(MIPS11) {
  // Test LWL, LWR, SWL and SWR instructions.
832
  CcTest::InitializeVM();
833 834
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

  typedef struct {
    int32_t reg_init;
    int32_t mem_init;
    int32_t lwl_0;
    int32_t lwl_1;
    int32_t lwl_2;
    int32_t lwl_3;
    int32_t lwr_0;
    int32_t lwr_1;
    int32_t lwr_2;
    int32_t lwr_3;
    int32_t swl_0;
    int32_t swl_1;
    int32_t swl_2;
    int32_t swl_3;
    int32_t swr_0;
    int32_t swr_1;
    int32_t swr_2;
    int32_t swr_3;
  } T;
  T t;

858
  Assembler assm(isolate, NULL, 0);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 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 930 931 932 933 934 935 936 937 938 939 940 941 942 943

  // Test all combinations of LWL and vAddr.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwl(t0, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, lwl_0)) );

  __ lw(t1, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwl(t1, MemOperand(a0, OFFSET_OF(T, mem_init) + 1) );
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, lwl_1)) );

  __ lw(t2, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwl(t2, MemOperand(a0, OFFSET_OF(T, mem_init) + 2) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, lwl_2)) );

  __ lw(t3, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwl(t3, MemOperand(a0, OFFSET_OF(T, mem_init) + 3) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, lwl_3)) );

  // Test all combinations of LWR and vAddr.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwr(t0, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, lwr_0)) );

  __ lw(t1, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwr(t1, MemOperand(a0, OFFSET_OF(T, mem_init) + 1) );
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, lwr_1)) );

  __ lw(t2, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwr(t2, MemOperand(a0, OFFSET_OF(T, mem_init) + 2) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, lwr_2)) );

  __ lw(t3, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ lwr(t3, MemOperand(a0, OFFSET_OF(T, mem_init) + 3) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, lwr_3)) );

  // Test all combinations of SWL and vAddr.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, swl_0)) );
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swl(t0, MemOperand(a0, OFFSET_OF(T, swl_0)) );

  __ lw(t1, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, swl_1)) );
  __ lw(t1, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swl(t1, MemOperand(a0, OFFSET_OF(T, swl_1) + 1) );

  __ lw(t2, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, swl_2)) );
  __ lw(t2, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swl(t2, MemOperand(a0, OFFSET_OF(T, swl_2) + 2) );

  __ lw(t3, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, swl_3)) );
  __ lw(t3, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swl(t3, MemOperand(a0, OFFSET_OF(T, swl_3) + 3) );

  // Test all combinations of SWR and vAddr.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, swr_0)) );
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swr(t0, MemOperand(a0, OFFSET_OF(T, swr_0)) );

  __ lw(t1, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t1, MemOperand(a0, OFFSET_OF(T, swr_1)) );
  __ lw(t1, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swr(t1, MemOperand(a0, OFFSET_OF(T, swr_1) + 1) );

  __ lw(t2, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t2, MemOperand(a0, OFFSET_OF(T, swr_2)) );
  __ lw(t2, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swr(t2, MemOperand(a0, OFFSET_OF(T, swr_2) + 2) );

  __ lw(t3, MemOperand(a0, OFFSET_OF(T, mem_init)) );
  __ sw(t3, MemOperand(a0, OFFSET_OF(T, swr_3)) );
  __ lw(t3, MemOperand(a0, OFFSET_OF(T, reg_init)) );
  __ swr(t3, MemOperand(a0, OFFSET_OF(T, swr_3) + 3) );

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
944
      Handle<Code>())->ToObjectChecked();
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.reg_init = 0xaabbccdd;
  t.mem_init = 0x11223344;

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

  CHECK_EQ(0x44bbccdd, t.lwl_0);
  CHECK_EQ(0x3344ccdd, t.lwl_1);
  CHECK_EQ(0x223344dd, t.lwl_2);
  CHECK_EQ(0x11223344, t.lwl_3);

  CHECK_EQ(0x11223344, t.lwr_0);
  CHECK_EQ(0xaa112233, t.lwr_1);
  CHECK_EQ(0xaabb1122, t.lwr_2);
  CHECK_EQ(0xaabbcc11, t.lwr_3);

  CHECK_EQ(0x112233aa, t.swl_0);
  CHECK_EQ(0x1122aabb, t.swl_1);
  CHECK_EQ(0x11aabbcc, t.swl_2);
  CHECK_EQ(0xaabbccdd, t.swl_3);

  CHECK_EQ(0xaabbccdd, t.swr_0);
  CHECK_EQ(0xbbccdd44, t.swr_1);
  CHECK_EQ(0xccdd3344, t.swr_2);
  CHECK_EQ(0xdd223344, t.swr_3);
}


TEST(MIPS12) {
976
  CcTest::InitializeVM();
977 978
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
979 980 981 982 983 984 985 986 987 988 989

  typedef struct {
      int32_t  x;
      int32_t  y;
      int32_t  y1;
      int32_t  y2;
      int32_t  y3;
      int32_t  y4;
  } T;
  T t;

990
  MacroAssembler assm(isolate, NULL, 0);
991 992 993 994 995 996 997 998 999

  __ mov(t6, fp);  // Save frame pointer.
  __ mov(fp, a0);  // Access struct T by fp.
  __ lw(t0, MemOperand(a0, OFFSET_OF(T, y)) );
  __ lw(t3, MemOperand(a0, OFFSET_OF(T, y4)) );

  __ addu(t1, t0, t3);
  __ subu(t4, t0, t3);
  __ nop();
1000
  __ push(t0);  // These instructions disappear after opt.
1001 1002 1003 1004
  __ Pop();
  __ addu(t0, t0, t0);
  __ nop();
  __ Pop();     // These instructions disappear after opt.
1005
  __ push(t3);
1006
  __ nop();
1007 1008
  __ push(t3);  // These instructions disappear after opt.
  __ pop(t3);
1009
  __ nop();
1010 1011
  __ push(t3);
  __ pop(t4);
1012 1013 1014 1015 1016 1017 1018
  __ nop();
  __ sw(t0, MemOperand(fp, OFFSET_OF(T, y)) );
  __ lw(t0, MemOperand(fp, OFFSET_OF(T, y)) );
  __ nop();
  __ sw(t0, MemOperand(fp, OFFSET_OF(T, y)) );
  __ lw(t1, MemOperand(fp, OFFSET_OF(T, y)) );
  __ nop();
1019
  __ push(t1);
1020
  __ lw(t1, MemOperand(fp, OFFSET_OF(T, y)) );
1021
  __ pop(t1);
1022
  __ nop();
1023
  __ push(t1);
1024
  __ lw(t2, MemOperand(fp, OFFSET_OF(T, y)) );
1025
  __ pop(t1);
1026
  __ nop();
1027
  __ push(t1);
1028
  __ lw(t2, MemOperand(fp, OFFSET_OF(T, y)) );
1029
  __ pop(t2);
1030
  __ nop();
1031
  __ push(t2);
1032
  __ lw(t2, MemOperand(fp, OFFSET_OF(T, y)) );
1033
  __ pop(t1);
1034
  __ nop();
1035
  __ push(t1);
1036
  __ lw(t2, MemOperand(fp, OFFSET_OF(T, y)) );
1037
  __ pop(t3);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  __ nop();

  __ mov(fp, t6);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
1049
      Handle<Code>())->ToObjectChecked();
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
  t.x = 1;
  t.y = 2;
  t.y1 = 3;
  t.y2 = 4;
  t.y3 = 0XBABA;
  t.y4 = 0xDEDA;

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

  CHECK_EQ(3, t.y1);
}


TEST(MIPS13) {
  // Test Cvt_d_uw and Trunc_uw_d macros.
1068
  CcTest::InitializeVM();
1069 1070
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

  typedef struct {
    double cvt_big_out;
    double cvt_small_out;
    uint32_t trunc_big_out;
    uint32_t trunc_small_out;
    uint32_t cvt_big_in;
    uint32_t cvt_small_in;
  } T;
  T t;

1082
  MacroAssembler assm(isolate, NULL, 0);
1083

1084 1085 1086
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, cvt_small_in)));
  __ Cvt_d_uw(f10, t0, f22);
  __ sdc1(f10, MemOperand(a0, OFFSET_OF(T, cvt_small_out)));
1087

1088 1089
  __ Trunc_uw_d(f10, f10, f22);
  __ swc1(f10, MemOperand(a0, OFFSET_OF(T, trunc_small_out)));
1090

1091 1092 1093
  __ sw(t0, MemOperand(a0, OFFSET_OF(T, cvt_big_in)));
  __ Cvt_d_uw(f8, t0, f22);
  __ sdc1(f8, MemOperand(a0, OFFSET_OF(T, cvt_big_out)));
1094

1095 1096
  __ Trunc_uw_d(f8, f8, f22);
  __ swc1(f8, MemOperand(a0, OFFSET_OF(T, trunc_big_out)));
1097

1098 1099
  __ jr(ra);
  __ nop();
1100

1101 1102 1103 1104 1105 1106 1107 1108
  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
1109

1110 1111
  t.cvt_big_in = 0xFFFFFFFF;
  t.cvt_small_in  = 333;
1112

1113 1114
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
1115

1116 1117
  CHECK_EQ(t.cvt_big_out, static_cast<double>(t.cvt_big_in));
  CHECK_EQ(t.cvt_small_out, static_cast<double>(t.cvt_small_in));
1118

1119 1120 1121
  CHECK_EQ(static_cast<int>(t.trunc_big_out), static_cast<int>(t.cvt_big_in));
  CHECK_EQ(static_cast<int>(t.trunc_small_out),
           static_cast<int>(t.cvt_small_in));
1122 1123 1124 1125 1126
}


TEST(MIPS14) {
  // Test round, floor, ceil, trunc, cvt.
1127
  CcTest::InitializeVM();
1128 1129
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
1130 1131 1132 1133 1134 1135

#define ROUND_STRUCT_ELEMENT(x) \
  int32_t x##_up_out; \
  int32_t x##_down_out; \
  int32_t neg_##x##_up_out; \
  int32_t neg_##x##_down_out; \
1136 1137 1138 1139
  uint32_t x##_err1_out; \
  uint32_t x##_err2_out; \
  uint32_t x##_err3_out; \
  uint32_t x##_err4_out; \
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  int32_t x##_invalid_result;

  typedef struct {
    double round_up_in;
    double round_down_in;
    double neg_round_up_in;
    double neg_round_down_in;
    double err1_in;
    double err2_in;
    double err3_in;
    double err4_in;

    ROUND_STRUCT_ELEMENT(round)
    ROUND_STRUCT_ELEMENT(floor)
    ROUND_STRUCT_ELEMENT(ceil)
    ROUND_STRUCT_ELEMENT(trunc)
    ROUND_STRUCT_ELEMENT(cvt)
  } T;
  T t;

#undef ROUND_STRUCT_ELEMENT

1162
  MacroAssembler assm(isolate, NULL, 0);
1163

1164 1165 1166 1167
  // Save FCSR.
  __ cfc1(a1, FCSR);
  // Disable FPU exceptions.
  __ ctc1(zero_reg, FCSR);
1168
#define RUN_ROUND_TEST(x) \
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, round_up_in))); \
  __ x##_w_d(f0, f0); \
  __ swc1(f0, MemOperand(a0, OFFSET_OF(T, x##_up_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, round_down_in))); \
  __ x##_w_d(f0, f0); \
  __ swc1(f0, MemOperand(a0, OFFSET_OF(T, x##_down_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, neg_round_up_in))); \
  __ x##_w_d(f0, f0); \
  __ swc1(f0, MemOperand(a0, OFFSET_OF(T, neg_##x##_up_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, neg_round_down_in))); \
  __ x##_w_d(f0, f0); \
  __ swc1(f0, MemOperand(a0, OFFSET_OF(T, neg_##x##_down_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, err1_in))); \
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
  __ sw(a2, MemOperand(a0, OFFSET_OF(T, x##_err1_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, err2_in))); \
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
  __ sw(a2, MemOperand(a0, OFFSET_OF(T, x##_err2_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, err3_in))); \
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
  __ sw(a2, MemOperand(a0, OFFSET_OF(T, x##_err3_out))); \
  \
  __ ldc1(f0, MemOperand(a0, OFFSET_OF(T, err4_in))); \
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
  __ sw(a2, MemOperand(a0, OFFSET_OF(T, x##_err4_out))); \
  __ swc1(f0, MemOperand(a0, OFFSET_OF(T, x##_invalid_result)));

  RUN_ROUND_TEST(round)
  RUN_ROUND_TEST(floor)
  RUN_ROUND_TEST(ceil)
  RUN_ROUND_TEST(trunc)
  RUN_ROUND_TEST(cvt)

  // Restore FCSR.
  __ ctc1(a1, FCSR);
1218

1219 1220
  __ jr(ra);
  __ nop();
1221

1222 1223 1224 1225 1226 1227 1228 1229
  CodeDesc desc;
  assm.GetCode(&desc);
  Object* code = HEAP->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());
  F3 f = FUNCTION_CAST<F3>(Code::cast(code)->entry());
1230

1231 1232 1233 1234 1235 1236 1237 1238
  t.round_up_in = 123.51;
  t.round_down_in = 123.49;
  t.neg_round_up_in = -123.5;
  t.neg_round_down_in = -123.49;
  t.err1_in = 123.51;
  t.err2_in = 1;
  t.err3_in = static_cast<double>(1) + 0xFFFFFFFF;
  t.err4_in = NAN;
1239

1240 1241
  Object* dummy = CALL_GENERATED_CODE(f, &t, 0, 0, 0, 0);
  USE(dummy);
1242

1243 1244 1245 1246 1247 1248 1249 1250
#define GET_FPU_ERR(x) (static_cast<int>(x & kFCSRFlagMask))
#define CHECK_ROUND_RESULT(type) \
  CHECK(GET_FPU_ERR(t.type##_err1_out) & kFCSRInexactFlagMask); \
  CHECK_EQ(0, GET_FPU_ERR(t.type##_err2_out)); \
  CHECK(GET_FPU_ERR(t.type##_err3_out) & kFCSRInvalidOpFlagMask); \
  CHECK(GET_FPU_ERR(t.type##_err4_out) & kFCSRInvalidOpFlagMask); \
  CHECK_EQ(kFPUInvalidResult, t.type##_invalid_result);

1251 1252 1253 1254
  CHECK_ROUND_RESULT(round);
  CHECK_ROUND_RESULT(floor);
  CHECK_ROUND_RESULT(ceil);
  CHECK_ROUND_RESULT(cvt);
1255 1256
}

1257 1258 1259

TEST(MIPS15) {
  // Test chaining of label usages within instructions (issue 1644).
1260
  CcTest::InitializeVM();
1261 1262 1263
  Isolate* isolate = Isolate::Current();
  HandleScope scope(isolate);
  Assembler assm(isolate, NULL, 0);
1264 1265 1266

  Label target;
  __ beq(v0, v1, &target);
1267
  __ nop();
1268
  __ bne(v0, v1, &target);
1269
  __ nop();
1270 1271 1272 1273
  __ bind(&target);
  __ nop();
}

1274
#undef __