test-macro-assembler-x64.cc 74.3 KB
Newer Older
1 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
// Copyright 2009 the V8 project authors. All rights reserved.
// 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 <stdlib.h>

30
#include "src/v8.h"
31

32
#include "src/base/platform/platform.h"
33
#include "src/factory.h"
34
#include "src/macro-assembler.h"
35 36
#include "src/serialize.h"
#include "test/cctest/cctest.h"
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
namespace i = v8::internal;
using i::Address;
using i::Assembler;
using i::CodeDesc;
using i::Condition;
using i::FUNCTION_CAST;
using i::HandleScope;
using i::Immediate;
using i::Isolate;
using i::Label;
using i::MacroAssembler;
using i::Operand;
using i::RelocInfo;
using i::Representation;
using i::Smi;
using i::SmiIndex;
using i::byte;
using i::carry;
using i::greater;
using i::greater_equal;
using i::kIntSize;
using i::kPointerSize;
using i::kSmiTagMask;
using i::kSmiValueSize;
using i::less_equal;
using i::negative;
using i::not_carry;
using i::not_equal;
using i::equal;
using i::not_zero;
using i::positive;
using i::r11;
using i::r13;
using i::r14;
using i::r15;
using i::r8;
using i::r9;
using i::rax;
using i::rbp;
using i::rbx;
using i::rcx;
using i::rdi;
using i::rdx;
using i::rsi;
using i::rsp;
using i::times_pointer_size;
84 85 86 87 88 89 90 91 92 93 94 95 96 97

// Test the x64 assembler by compiling some simple functions into
// a buffer and executing them.  These tests do not initialize the
// V8 library, create a context, or use any V8 objects.
// The AMD64 calling convention is used, with the first five arguments
// in RSI, RDI, RDX, RCX, R8, and R9, and floating point arguments in
// the XMM registers.  The return value is in RAX.
// This calling convention is used on Linux, with GCC, and on Mac OS,
// with GCC.  A different convention is used on 64-bit windows.

typedef int (*F0)();

#define __ masm->

98 99 100

static void EntryCode(MacroAssembler* masm) {
  // Smi constant register is callee save.
101 102
  __ pushq(i::kSmiConstantRegister);
  __ pushq(i::kRootRegister);
103
  __ InitializeSmiConstantRegister();
104
  __ InitializeRootRegister();
105 106 107 108 109 110
}


static void ExitCode(MacroAssembler* masm) {
  // Return -1 if kSmiConstantRegister was clobbered during the test.
  __ Move(rdx, Smi::FromInt(1));
111
  __ cmpq(rdx, i::kSmiConstantRegister);
112 113
  __ movq(rdx, Immediate(-1));
  __ cmovq(not_equal, rax, rdx);
114 115
  __ popq(i::kRootRegister);
  __ popq(i::kSmiConstantRegister);
116 117 118
}


119 120
TEST(Smi) {
  // Check that C++ Smi operations work as expected.
121
  int64_t test_numbers[] = {
122
      0, 1, -1, 127, 128, -128, -129, 255, 256, -256, -257,
123 124
      Smi::kMaxValue, static_cast<int64_t>(Smi::kMaxValue) + 1,
      Smi::kMinValue, static_cast<int64_t>(Smi::kMinValue) - 1
125 126 127
  };
  int test_number_count = 15;
  for (int i = 0; i < test_number_count; i++) {
128
    int64_t number = test_numbers[i];
129 130 131 132 133 134 135 136 137
    bool is_valid = Smi::IsValid(number);
    bool is_in_range = number >= Smi::kMinValue && number <= Smi::kMaxValue;
    CHECK_EQ(is_in_range, is_valid);
    if (is_valid) {
      Smi* smi_from_intptr = Smi::FromIntptr(number);
      if (static_cast<int>(number) == number) {  // Is a 32-bit int.
        Smi* smi_from_int = Smi::FromInt(static_cast<int32_t>(number));
        CHECK_EQ(smi_from_int, smi_from_intptr);
      }
138 139
      int64_t smi_value = smi_from_intptr->value();
      CHECK_EQ(number, smi_value);
140 141 142 143 144 145 146
    }
  }
}


static void TestMoveSmi(MacroAssembler* masm, Label* exit, int id, Smi* value) {
  __ movl(rax, Immediate(id));
147 148
  __ Move(rcx, value);
  __ Set(rdx, reinterpret_cast<intptr_t>(value));
149 150 151 152 153 154 155 156 157
  __ cmpq(rcx, rdx);
  __ j(not_equal, exit);
}


// Test that we can move a Smi value literally into a register.
TEST(SmiMove) {
  // Allocate an executable page of memory.
  size_t actual_size;
158 159
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
160
  CHECK(buffer);
161
  Isolate* isolate = CcTest::i_isolate();
162 163
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
164
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
165
  EntryCode(masm);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  Label exit;

  TestMoveSmi(masm, &exit, 1, Smi::FromInt(0));
  TestMoveSmi(masm, &exit, 2, Smi::FromInt(127));
  TestMoveSmi(masm, &exit, 3, Smi::FromInt(128));
  TestMoveSmi(masm, &exit, 4, Smi::FromInt(255));
  TestMoveSmi(masm, &exit, 5, Smi::FromInt(256));
  TestMoveSmi(masm, &exit, 6, Smi::FromInt(Smi::kMaxValue));
  TestMoveSmi(masm, &exit, 7, Smi::FromInt(-1));
  TestMoveSmi(masm, &exit, 8, Smi::FromInt(-128));
  TestMoveSmi(masm, &exit, 9, Smi::FromInt(-129));
  TestMoveSmi(masm, &exit, 10, Smi::FromInt(-256));
  TestMoveSmi(masm, &exit, 11, Smi::FromInt(-257));
  TestMoveSmi(masm, &exit, 12, Smi::FromInt(Smi::kMinValue));

181
  __ xorq(rax, rax);  // Success.
182
  __ bind(&exit);
183
  ExitCode(masm);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiCompare(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r8, rcx);
  __ Move(rdx, Smi::FromInt(y));
  __ movq(r9, rdx);
  __ SmiCompare(rcx, rdx);
  if (x < y) {
    __ movl(rax, Immediate(id + 1));
    __ j(greater_equal, exit);
  } else if (x > y) {
    __ movl(rax, Immediate(id + 2));
    __ j(less_equal, exit);
  } else {
207
    DCHECK_EQ(x, y);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    __ movl(rax, Immediate(id + 3));
    __ j(not_equal, exit);
  }
  __ movl(rax, Immediate(id + 4));
  __ cmpq(rcx, r8);
  __ j(not_equal, exit);
  __ incq(rax);
  __ cmpq(rdx, r9);
  __ j(not_equal, exit);

  if (x != y) {
    __ SmiCompare(rdx, rcx);
    if (y < x) {
      __ movl(rax, Immediate(id + 9));
      __ j(greater_equal, exit);
    } else {
224
      DCHECK(y > x);
225 226 227 228
      __ movl(rax, Immediate(id + 10));
      __ j(less_equal, exit);
    }
  } else {
229
    __ cmpq(rcx, rcx);
230 231 232 233 234 235 236 237 238 239 240 241 242
    __ movl(rax, Immediate(id + 11));
    __ j(not_equal, exit);
    __ incq(rax);
    __ cmpq(rcx, r8);
    __ j(not_equal, exit);
  }
}


// Test that we can compare smis for equality (and more).
TEST(SmiCompare) {
  // Allocate an executable page of memory.
  size_t actual_size;
243 244
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
245
  CHECK(buffer);
246
  Isolate* isolate = CcTest::i_isolate();
247 248
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
249 250

  MacroAssembler* masm = &assembler;
251
  EntryCode(masm);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  Label exit;

  TestSmiCompare(masm, &exit, 0x10, 0, 0);
  TestSmiCompare(masm, &exit, 0x20, 0, 1);
  TestSmiCompare(masm, &exit, 0x30, 1, 0);
  TestSmiCompare(masm, &exit, 0x40, 1, 1);
  TestSmiCompare(masm, &exit, 0x50, 0, -1);
  TestSmiCompare(masm, &exit, 0x60, -1, 0);
  TestSmiCompare(masm, &exit, 0x70, -1, -1);
  TestSmiCompare(masm, &exit, 0x80, 0, Smi::kMinValue);
  TestSmiCompare(masm, &exit, 0x90, Smi::kMinValue, 0);
  TestSmiCompare(masm, &exit, 0xA0, 0, Smi::kMaxValue);
  TestSmiCompare(masm, &exit, 0xB0, Smi::kMaxValue, 0);
  TestSmiCompare(masm, &exit, 0xC0, -1, Smi::kMinValue);
  TestSmiCompare(masm, &exit, 0xD0, Smi::kMinValue, -1);
  TestSmiCompare(masm, &exit, 0xE0, -1, Smi::kMaxValue);
  TestSmiCompare(masm, &exit, 0xF0, Smi::kMaxValue, -1);
  TestSmiCompare(masm, &exit, 0x100, Smi::kMinValue, Smi::kMinValue);
  TestSmiCompare(masm, &exit, 0x110, Smi::kMinValue, Smi::kMaxValue);
  TestSmiCompare(masm, &exit, 0x120, Smi::kMaxValue, Smi::kMinValue);
  TestSmiCompare(masm, &exit, 0x130, Smi::kMaxValue, Smi::kMaxValue);

274
  __ xorq(rax, rax);  // Success.
275
  __ bind(&exit);
276
  ExitCode(masm);
277 278 279 280 281 282 283 284 285 286 287 288 289 290
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}



TEST(Integer32ToSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
291 292
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
293
  CHECK(buffer);
294
  Isolate* isolate = CcTest::i_isolate();
295 296
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
297 298

  MacroAssembler* masm = &assembler;
299
  EntryCode(masm);
300 301 302 303 304 305
  Label exit;

  __ movq(rax, Immediate(1));  // Test number.
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(0)));
306
  __ cmpq(rcx, rdx);
307 308 309 310 311 312
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(2));  // Test number.
  __ movl(rcx, Immediate(1024));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(1024)));
313
  __ cmpq(rcx, rdx);
314 315 316 317 318 319
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(3));  // Test number.
  __ movl(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(-1)));
320
  __ cmpq(rcx, rdx);
321 322 323 324 325 326
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(4));  // Test number.
  __ movl(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(Smi::kMaxValue)));
327
  __ cmpq(rcx, rdx);
328 329 330 331 332 333
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(5));  // Test number.
  __ movl(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(Smi::kMinValue)));
334
  __ cmpq(rcx, rdx);
335 336 337 338 339 340 341 342
  __ j(not_equal, &exit);

  // Different target register.

  __ movq(rax, Immediate(6));  // Test number.
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(r8, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(0)));
343
  __ cmpq(r8, rdx);
344 345 346 347 348 349
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(7));  // Test number.
  __ movl(rcx, Immediate(1024));
  __ Integer32ToSmi(r8, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(1024)));
350
  __ cmpq(r8, rdx);
351 352 353 354 355 356
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(8));  // Test number.
  __ movl(rcx, Immediate(-1));
  __ Integer32ToSmi(r8, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(-1)));
357
  __ cmpq(r8, rdx);
358 359 360 361 362 363
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(9));  // Test number.
  __ movl(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(r8, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(Smi::kMaxValue)));
364
  __ cmpq(r8, rdx);
365 366 367 368 369 370
  __ j(not_equal, &exit);

  __ movq(rax, Immediate(10));  // Test number.
  __ movl(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(r8, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(Smi::kMinValue)));
371
  __ cmpq(r8, rdx);
372 373 374
  __ j(not_equal, &exit);


375
  __ xorq(rax, rax);  // Success.
376
  __ bind(&exit);
377
  ExitCode(masm);
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestI64PlusConstantToSmi(MacroAssembler* masm,
                              Label* exit,
                              int id,
                              int64_t x,
                              int y) {
  int64_t result = x + y;
394
  DCHECK(Smi::IsValid(result));
395
  __ movl(rax, Immediate(id));
396
  __ Move(r8, Smi::FromInt(static_cast<int>(result)));
397
  __ movq(rcx, x);
398 399
  __ movq(r11, rcx);
  __ Integer64PlusConstantToSmi(rdx, rcx, y);
400
  __ cmpq(rdx, r8);
401 402 403
  __ j(not_equal, exit);

  __ incq(rax);
404
  __ cmpq(r11, rcx);
405 406 407 408
  __ j(not_equal, exit);

  __ incq(rax);
  __ Integer64PlusConstantToSmi(rcx, rcx, y);
409
  __ cmpq(rcx, r8);
410 411 412 413 414 415 416
  __ j(not_equal, exit);
}


TEST(Integer64PlusConstantToSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
417 418
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
419
  CHECK(buffer);
420
  Isolate* isolate = CcTest::i_isolate();
421 422
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
423 424

  MacroAssembler* masm = &assembler;
425
  EntryCode(masm);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  Label exit;

  int64_t twice_max = static_cast<int64_t>(Smi::kMaxValue) * 2;

  TestI64PlusConstantToSmi(masm, &exit, 0x10, 0, 0);
  TestI64PlusConstantToSmi(masm, &exit, 0x20, 0, 1);
  TestI64PlusConstantToSmi(masm, &exit, 0x30, 1, 0);
  TestI64PlusConstantToSmi(masm, &exit, 0x40, Smi::kMaxValue - 5, 5);
  TestI64PlusConstantToSmi(masm, &exit, 0x50, Smi::kMinValue + 5, 5);
  TestI64PlusConstantToSmi(masm, &exit, 0x60, twice_max, -Smi::kMaxValue);
  TestI64PlusConstantToSmi(masm, &exit, 0x70, -twice_max, Smi::kMaxValue);
  TestI64PlusConstantToSmi(masm, &exit, 0x80, 0, Smi::kMinValue);
  TestI64PlusConstantToSmi(masm, &exit, 0x90, 0, Smi::kMaxValue);
  TestI64PlusConstantToSmi(masm, &exit, 0xA0, Smi::kMinValue, 0);
  TestI64PlusConstantToSmi(masm, &exit, 0xB0, Smi::kMaxValue, 0);
  TestI64PlusConstantToSmi(masm, &exit, 0xC0, twice_max, Smi::kMinValue);

443
  __ xorq(rax, rax);  // Success.
444
  __ bind(&exit);
445
  ExitCode(masm);
446 447 448 449 450 451 452 453 454 455 456 457 458
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


TEST(SmiCheck) {
  // Allocate an executable page of memory.
  size_t actual_size;
459 460
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
461
  CHECK(buffer);
462
  Isolate* isolate = CcTest::i_isolate();
463 464
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
465 466

  MacroAssembler* masm = &assembler;
467
  EntryCode(masm);
468 469 470 471 472 473 474 475 476 477 478 479 480
  Label exit;
  Condition cond;

  __ movl(rax, Immediate(1));  // Test number.

  // CheckSmi

  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckSmi(rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
481
  __ xorq(rcx, Immediate(kSmiTagMask));
482 483 484 485 486 487 488 489 490 491
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  __ incq(rax);
  __ movl(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckSmi(rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
492
  __ xorq(rcx, Immediate(kSmiTagMask));
493 494 495 496 497 498 499 500 501 502
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  __ incq(rax);
  __ movl(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckSmi(rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
503
  __ xorq(rcx, Immediate(kSmiTagMask));
504 505 506 507 508 509 510 511 512 513
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  __ incq(rax);
  __ movl(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckSmi(rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
514
  __ xorq(rcx, Immediate(kSmiTagMask));
515 516 517 518 519 520 521 522
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  // CheckPositiveSmi

  __ incq(rax);
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
523
  cond = masm->CheckNonNegativeSmi(rcx);
524 525 526
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
527
  __ xorq(rcx, Immediate(kSmiTagMask));
528
  cond = masm->CheckNonNegativeSmi(rcx);  // "zero" non-smi.
529 530 531 532 533
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
534
  cond = masm->CheckNonNegativeSmi(rcx);  // Negative smis are not positive.
535 536 537 538 539
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
540
  cond = masm->CheckNonNegativeSmi(rcx);  // Most negative smi is not positive.
541 542 543
  __ j(cond, &exit);

  __ incq(rax);
544
  __ xorq(rcx, Immediate(kSmiTagMask));
545
  cond = masm->CheckNonNegativeSmi(rcx);  // "Negative" non-smi.
546 547 548 549 550
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
551
  cond = masm->CheckNonNegativeSmi(rcx);  // Most positive smi is positive.
552 553 554
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
555
  __ xorq(rcx, Immediate(kSmiTagMask));
556
  cond = masm->CheckNonNegativeSmi(rcx);  // "Positive" non-smi.
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
  __ j(cond, &exit);

  // CheckIsMinSmi

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckIsMinSmi(rcx);
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckIsMinSmi(rcx);
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckIsMinSmi(rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue + 1));
  __ Integer32ToSmi(rcx, rcx);
  cond = masm->CheckIsMinSmi(rcx);
  __ j(cond, &exit);

  // CheckBothSmi

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
  __ movq(rdx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rdx, rdx);
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
596
  __ xorq(rcx, Immediate(kSmiTagMask));
597 598 599 600
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

  __ incq(rax);
601
  __ xorq(rdx, Immediate(kSmiTagMask));
602 603 604 605
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

  __ incq(rax);
606
  __ xorq(rcx, Immediate(kSmiTagMask));
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

  __ incq(rax);
  cond = masm->CheckBothSmi(rcx, rcx);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  cond = masm->CheckBothSmi(rdx, rdx);
  __ j(cond, &exit);

  // CheckInteger32ValidSmiValue
  __ incq(rax);
  __ movq(rcx, Immediate(0));
  cond = masm->CheckInteger32ValidSmiValue(rax);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(-1));
  cond = masm->CheckInteger32ValidSmiValue(rax);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  cond = masm->CheckInteger32ValidSmiValue(rax);
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  cond = masm->CheckInteger32ValidSmiValue(rax);
  __ j(NegateCondition(cond), &exit);

  // Success
640
  __ xorq(rax, rax);
641 642

  __ bind(&exit);
643
  ExitCode(masm);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}



void TestSmiNeg(MacroAssembler* masm, Label* exit, int id, int x) {
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
  if (x == Smi::kMinValue || x == 0) {
    // Negation fails.
    __ movl(rax, Immediate(id + 8));
    __ SmiNeg(r9, rcx, exit);

    __ incq(rax);
664
    __ cmpq(r11, rcx);
665 666 667 668 669 670
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiNeg(rcx, rcx, exit);

    __ incq(rax);
671
    __ cmpq(r11, rcx);
672 673 674 675 676 677 678 679 680 681 682
    __ j(not_equal, exit);
  } else {
    Label smi_ok, smi_ok2;
    int result = -x;
    __ movl(rax, Immediate(id));
    __ Move(r8, Smi::FromInt(result));

    __ SmiNeg(r9, rcx, &smi_ok);
    __ jmp(exit);
    __ bind(&smi_ok);
    __ incq(rax);
683
    __ cmpq(r9, r8);
684 685 686
    __ j(not_equal, exit);

    __ incq(rax);
687
    __ cmpq(r11, rcx);
688 689 690 691 692 693 694
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiNeg(rcx, rcx, &smi_ok2);
    __ jmp(exit);
    __ bind(&smi_ok2);
    __ incq(rax);
695
    __ cmpq(rcx, r8);
696 697 698 699 700 701 702 703
    __ j(not_equal, exit);
  }
}


TEST(SmiNeg) {
  // Allocate an executable page of memory.
  size_t actual_size;
704 705
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
706
  CHECK(buffer);
707
  Isolate* isolate = CcTest::i_isolate();
708 709
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
710 711

  MacroAssembler* masm = &assembler;
712
  EntryCode(masm);
713 714 715 716 717 718 719 720 721 722 723
  Label exit;

  TestSmiNeg(masm, &exit, 0x10, 0);
  TestSmiNeg(masm, &exit, 0x20, 1);
  TestSmiNeg(masm, &exit, 0x30, -1);
  TestSmiNeg(masm, &exit, 0x40, 127);
  TestSmiNeg(masm, &exit, 0x50, 65535);
  TestSmiNeg(masm, &exit, 0x60, Smi::kMinValue);
  TestSmiNeg(masm, &exit, 0x70, Smi::kMaxValue);
  TestSmiNeg(masm, &exit, 0x80, -Smi::kMaxValue);

724
  __ xorq(rax, rax);  // Success.
725
  __ bind(&exit);
726
  ExitCode(masm);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


static void SmiAddTest(MacroAssembler* masm,
                       Label* exit,
                       int id,
                       int first,
                       int second) {
  __ movl(rcx, Immediate(first));
  __ Integer32ToSmi(rcx, rcx);
  __ movl(rdx, Immediate(second));
  __ Integer32ToSmi(rdx, rdx);
  __ movl(r8, Immediate(first + second));
  __ Integer32ToSmi(r8, r8);

  __ movl(rax, Immediate(id));  // Test number.
  __ SmiAdd(r9, rcx, rdx, exit);
751
  __ cmpq(r9, r8);
752 753 754
  __ j(not_equal, exit);

  __ incq(rax);
755
  __ SmiAdd(rcx, rcx, rdx, exit);
756
  __ cmpq(rcx, r8);
757 758 759 760 761 762 763
  __ j(not_equal, exit);

  __ movl(rcx, Immediate(first));
  __ Integer32ToSmi(rcx, rcx);

  __ incq(rax);
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second));
764
  __ cmpq(r9, r8);
765 766 767
  __ j(not_equal, exit);

  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second));
768
  __ cmpq(rcx, r8);
769 770 771 772 773
  __ j(not_equal, exit);

  __ movl(rcx, Immediate(first));
  __ Integer32ToSmi(rcx, rcx);

774 775 776
  i::SmiOperationExecutionMode mode;
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_OVERFLOW);
777
  __ incq(rax);
778
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second), mode, exit);
779
  __ cmpq(r9, r8);
780 781 782
  __ j(not_equal, exit);

  __ incq(rax);
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), mode, exit);
  __ cmpq(rcx, r8);
  __ j(not_equal, exit);

  __ movl(rcx, Immediate(first));
  __ Integer32ToSmi(rcx, rcx);

  mode.RemoveAll();
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_NO_OVERFLOW);
  Label done;
  __ incq(rax);
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), mode, &done);
  __ jmp(exit);
  __ bind(&done);
798
  __ cmpq(rcx, r8);
799 800 801
  __ j(not_equal, exit);
}

802

803 804 805 806 807
static void SmiAddOverflowTest(MacroAssembler* masm,
                               Label* exit,
                               int id,
                               int x) {
  // Adds a Smi to x so that the addition overflows.
808
  DCHECK(x != 0);  // Can't overflow by adding a Smi.
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
  int y_max = (x > 0) ? (Smi::kMaxValue + 0) : (Smi::kMinValue - x - 1);
  int y_min = (x > 0) ? (Smi::kMaxValue - x + 1) : (Smi::kMinValue + 0);

  __ movl(rax, Immediate(id));
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);  // Store original Smi value of x in r11.
  __ Move(rdx, Smi::FromInt(y_min));
  {
    Label overflow_ok;
    __ SmiAdd(r9, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiAdd(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

837 838 839
  i::SmiOperationExecutionMode mode;
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_OVERFLOW);
840 841 842 843
  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
844
    __ SmiAddConstant(r9, rcx, Smi::FromInt(y_min), mode, &overflow_ok);
845 846 847 848 849 850 851 852 853 854
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
855
    __ SmiAddConstant(rcx, rcx, Smi::FromInt(y_min), mode, &overflow_ok);
856 857 858 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
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  __ Move(rdx, Smi::FromInt(y_max));

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiAdd(r9, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiAdd(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
891
    __ SmiAddConstant(r9, rcx, Smi::FromInt(y_max), mode, &overflow_ok);
892 893 894 895 896 897 898
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

899 900
  mode.RemoveAll();
  mode.Add(i::BAILOUT_ON_OVERFLOW);
901 902 903
  {
    Label overflow_ok;
    __ incq(rax);
904
    __ SmiAddConstant(rcx, rcx, Smi::FromInt(y_max), mode, &overflow_ok);
905 906 907 908
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
909
    __ j(equal, exit);
910 911 912 913
  }
}


914 915 916
TEST(SmiAdd) {
  // Allocate an executable page of memory.
  size_t actual_size;
917 918
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 3, &actual_size, true));
919
  CHECK(buffer);
920
  Isolate* isolate = CcTest::i_isolate();
921 922
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
923 924

  MacroAssembler* masm = &assembler;
925
  EntryCode(masm);
926 927 928 929 930 931 932 933 934 935 936 937
  Label exit;

  // No-overflow tests.
  SmiAddTest(masm, &exit, 0x10, 1, 2);
  SmiAddTest(masm, &exit, 0x20, 1, -2);
  SmiAddTest(masm, &exit, 0x30, -1, 2);
  SmiAddTest(masm, &exit, 0x40, -1, -2);
  SmiAddTest(masm, &exit, 0x50, 0x1000, 0x2000);
  SmiAddTest(masm, &exit, 0x60, Smi::kMinValue, 5);
  SmiAddTest(masm, &exit, 0x70, Smi::kMaxValue, -5);
  SmiAddTest(masm, &exit, 0x80, Smi::kMaxValue, Smi::kMinValue);

938 939 940 941 942 943 944 945
  SmiAddOverflowTest(masm, &exit, 0x90, -1);
  SmiAddOverflowTest(masm, &exit, 0xA0, 1);
  SmiAddOverflowTest(masm, &exit, 0xB0, 1024);
  SmiAddOverflowTest(masm, &exit, 0xC0, Smi::kMaxValue);
  SmiAddOverflowTest(masm, &exit, 0xD0, -2);
  SmiAddOverflowTest(masm, &exit, 0xE0, -42000);
  SmiAddOverflowTest(masm, &exit, 0xF0, Smi::kMinValue);

946
  __ xorq(rax, rax);  // Success.
947
  __ bind(&exit);
948
  ExitCode(masm);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


static void SmiSubTest(MacroAssembler* masm,
                      Label* exit,
                      int id,
                      int first,
                      int second) {
  __ Move(rcx, Smi::FromInt(first));
  __ Move(rdx, Smi::FromInt(second));
  __ Move(r8, Smi::FromInt(first - second));

  __ movl(rax, Immediate(id));  // Test 0.
  __ SmiSub(r9, rcx, rdx, exit);
970
  __ cmpq(r9, r8);
971 972 973 974
  __ j(not_equal, exit);

  __ incq(rax);  // Test 1.
  __ SmiSub(rcx, rcx, rdx, exit);
975
  __ cmpq(rcx, r8);
976 977 978 979 980 981
  __ j(not_equal, exit);

  __ Move(rcx, Smi::FromInt(first));

  __ incq(rax);  // Test 2.
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second));
982
  __ cmpq(r9, r8);
983 984 985 986
  __ j(not_equal, exit);

  __ incq(rax);  // Test 3.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second));
987
  __ cmpq(rcx, r8);
988 989
  __ j(not_equal, exit);

990 991 992
  i::SmiOperationExecutionMode mode;
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_OVERFLOW);
993 994
  __ Move(rcx, Smi::FromInt(first));
  __ incq(rax);  // Test 4.
995 996
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), mode, exit);
  __ cmpq(rcx, r8);
997 998
  __ j(not_equal, exit);

999
  __ Move(rcx, Smi::FromInt(first));
1000
  __ incq(rax);  // Test 5.
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second), mode, exit);
  __ cmpq(r9, r8);
  __ j(not_equal, exit);

  mode.RemoveAll();
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_NO_OVERFLOW);
  __ Move(rcx, Smi::FromInt(first));
  Label done;
  __ incq(rax);  // Test 6.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), mode, &done);
  __ jmp(exit);
  __ bind(&done);
1014
  __ cmpq(rcx, r8);
1015 1016 1017
  __ j(not_equal, exit);
}

1018

1019 1020 1021 1022 1023
static void SmiSubOverflowTest(MacroAssembler* masm,
                               Label* exit,
                               int id,
                               int x) {
  // Subtracts a Smi from x so that the subtraction overflows.
1024
  DCHECK(x != -1);  // Can't overflow by subtracting a Smi.
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  int y_max = (x < 0) ? (Smi::kMaxValue + 0) : (Smi::kMinValue + 0);
  int y_min = (x < 0) ? (Smi::kMaxValue + x + 2) : (Smi::kMinValue + x);

  __ movl(rax, Immediate(id));
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);  // Store original Smi value of x in r11.
  __ Move(rdx, Smi::FromInt(y_min));
  {
    Label overflow_ok;
    __ SmiSub(r9, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1038
    __ cmpq(rcx, r11);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1049
    __ cmpq(rcx, r11);
1050 1051 1052
    __ j(not_equal, exit);
  }

1053 1054 1055 1056
  i::SmiOperationExecutionMode mode;
  mode.Add(i::PRESERVE_SOURCE_REGISTER);
  mode.Add(i::BAILOUT_ON_OVERFLOW);

1057 1058 1059 1060
  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
1061
    __ SmiSubConstant(r9, rcx, Smi::FromInt(y_min), mode, &overflow_ok);
1062 1063 1064
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1065
    __ cmpq(rcx, r11);
1066 1067 1068 1069 1070 1071
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
1072
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_min), mode, &overflow_ok);
1073 1074 1075
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1076
    __ cmpq(rcx, r11);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    __ j(not_equal, exit);
  }

  __ Move(rdx, Smi::FromInt(y_max));

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(r9, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1089
    __ cmpq(rcx, r11);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1100
    __ cmpq(rcx, r11);
1101 1102 1103 1104 1105 1106 1107
    __ j(not_equal, exit);
  }

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
1108
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_max), mode, &overflow_ok);
1109 1110 1111
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1112
    __ cmpq(rcx, r11);
1113 1114 1115
    __ j(not_equal, exit);
  }

1116 1117 1118
  mode.RemoveAll();
  mode.Add(i::BAILOUT_ON_OVERFLOW);
  __ movq(rcx, r11);
1119 1120 1121
  {
    Label overflow_ok;
    __ incq(rax);
1122
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_max), mode, &overflow_ok);
1123 1124 1125
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1126
    __ cmpq(rcx, r11);
1127
    __ j(equal, exit);
1128 1129 1130 1131 1132 1133 1134
  }
}


TEST(SmiSub) {
  // Allocate an executable page of memory.
  size_t actual_size;
1135 1136
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 4, &actual_size, true));
1137
  CHECK(buffer);
1138
  Isolate* isolate = CcTest::i_isolate();
1139 1140
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1141 1142

  MacroAssembler* masm = &assembler;
1143
  EntryCode(masm);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
  Label exit;

  SmiSubTest(masm, &exit, 0x10, 1, 2);
  SmiSubTest(masm, &exit, 0x20, 1, -2);
  SmiSubTest(masm, &exit, 0x30, -1, 2);
  SmiSubTest(masm, &exit, 0x40, -1, -2);
  SmiSubTest(masm, &exit, 0x50, 0x1000, 0x2000);
  SmiSubTest(masm, &exit, 0x60, Smi::kMinValue, -5);
  SmiSubTest(masm, &exit, 0x70, Smi::kMaxValue, 5);
  SmiSubTest(masm, &exit, 0x80, -Smi::kMaxValue, Smi::kMinValue);
  SmiSubTest(masm, &exit, 0x90, 0, Smi::kMaxValue);

  SmiSubOverflowTest(masm, &exit, 0xA0, 1);
  SmiSubOverflowTest(masm, &exit, 0xB0, 1024);
  SmiSubOverflowTest(masm, &exit, 0xC0, Smi::kMaxValue);
  SmiSubOverflowTest(masm, &exit, 0xD0, -2);
  SmiSubOverflowTest(masm, &exit, 0xE0, -42000);
  SmiSubOverflowTest(masm, &exit, 0xF0, Smi::kMinValue);
  SmiSubOverflowTest(masm, &exit, 0x100, 0);

1164
  __ xorq(rax, rax);  // Success.
1165
  __ bind(&exit);
1166
  ExitCode(masm);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}



void TestSmiMul(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  int64_t result = static_cast<int64_t>(x) * static_cast<int64_t>(y);
  bool negative_zero = (result == 0) && (x < 0 || y < 0);
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
  __ Move(rdx, Smi::FromInt(y));
  if (Smi::IsValid(result) && !negative_zero) {
    __ movl(rax, Immediate(id));
    __ Move(r8, Smi::FromIntptr(result));
    __ SmiMul(r9, rcx, rdx, exit);
    __ incq(rax);
1189
    __ cmpq(r11, rcx);
1190 1191
    __ j(not_equal, exit);
    __ incq(rax);
1192
    __ cmpq(r9, r8);
1193 1194 1195 1196
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiMul(rcx, rcx, rdx, exit);
1197
    __ cmpq(rcx, r8);
1198 1199 1200 1201 1202 1203 1204 1205
    __ j(not_equal, exit);
  } else {
    __ movl(rax, Immediate(id + 8));
    Label overflow_ok, overflow_ok2;
    __ SmiMul(r9, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1206
    __ cmpq(r11, rcx);
1207 1208 1209 1210 1211 1212 1213
    __ j(not_equal, exit);
    __ incq(rax);
    __ SmiMul(rcx, rcx, rdx, &overflow_ok2);
    __ jmp(exit);
    __ bind(&overflow_ok2);
    // 31-bit version doesn't preserve rcx on failure.
    // __ incq(rax);
1214
    // __ cmpq(r11, rcx);
1215 1216 1217 1218 1219 1220 1221 1222
    // __ j(not_equal, exit);
  }
}


TEST(SmiMul) {
  // Allocate an executable page of memory.
  size_t actual_size;
1223 1224
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
1225
  CHECK(buffer);
1226
  Isolate* isolate = CcTest::i_isolate();
1227 1228
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1229 1230

  MacroAssembler* masm = &assembler;
1231
  EntryCode(masm);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
  Label exit;

  TestSmiMul(masm, &exit, 0x10, 0, 0);
  TestSmiMul(masm, &exit, 0x20, -1, 0);
  TestSmiMul(masm, &exit, 0x30, 0, -1);
  TestSmiMul(masm, &exit, 0x40, -1, -1);
  TestSmiMul(masm, &exit, 0x50, 0x10000, 0x10000);
  TestSmiMul(masm, &exit, 0x60, 0x10000, 0xffff);
  TestSmiMul(masm, &exit, 0x70, 0x10000, 0xffff);
  TestSmiMul(masm, &exit, 0x80, Smi::kMaxValue, -1);
  TestSmiMul(masm, &exit, 0x90, Smi::kMaxValue, -2);
  TestSmiMul(masm, &exit, 0xa0, Smi::kMaxValue, 2);
  TestSmiMul(masm, &exit, 0xb0, (Smi::kMaxValue / 2), 2);
  TestSmiMul(masm, &exit, 0xc0, (Smi::kMaxValue / 2) + 1, 2);
  TestSmiMul(masm, &exit, 0xd0, (Smi::kMinValue / 2), 2);
  TestSmiMul(masm, &exit, 0xe0, (Smi::kMinValue / 2) - 1, 2);

1249
  __ xorq(rax, rax);  // Success.
1250
  __ bind(&exit);
1251
  ExitCode(masm);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiDiv(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  bool division_by_zero = (y == 0);
  bool negative_zero = (x == 0 && y < 0);
1265
#if V8_TARGET_ARCH_X64
1266 1267 1268 1269 1270 1271
  bool overflow = (x == Smi::kMinValue && y < 0);  // Safe approx. used.
#else
  bool overflow = (x == Smi::kMinValue && y == -1);
#endif
  bool fraction = !division_by_zero && !overflow && (x % y != 0);
  __ Move(r11, Smi::FromInt(x));
1272
  __ Move(r14, Smi::FromInt(y));
1273 1274 1275
  if (!fraction && !overflow && !negative_zero && !division_by_zero) {
    // Division succeeds
    __ movq(rcx, r11);
1276
    __ movq(r15, Immediate(id));
1277 1278
    int result = x / y;
    __ Move(r8, Smi::FromInt(result));
1279 1280
    __ SmiDiv(r9, rcx, r14, exit);
    // Might have destroyed rcx and r14.
1281
    __ incq(r15);
1282
    __ cmpq(r9, r8);
1283 1284
    __ j(not_equal, exit);

1285
    __ incq(r15);
1286
    __ movq(rcx, r11);
1287
    __ Move(r14, Smi::FromInt(y));
1288
    __ cmpq(rcx, r11);
1289 1290
    __ j(not_equal, exit);

1291
    __ incq(r15);
1292
    __ SmiDiv(rcx, rcx, r14, exit);
1293

1294
    __ incq(r15);
1295
    __ cmpq(rcx, r8);
1296 1297 1298
    __ j(not_equal, exit);
  } else {
    // Division fails.
1299
    __ movq(r15, Immediate(id + 8));
1300 1301 1302

    Label fail_ok, fail_ok2;
    __ movq(rcx, r11);
1303
    __ SmiDiv(r9, rcx, r14, &fail_ok);
1304 1305 1306
    __ jmp(exit);
    __ bind(&fail_ok);

1307
    __ incq(r15);
1308
    __ cmpq(rcx, r11);
1309 1310
    __ j(not_equal, exit);

1311
    __ incq(r15);
1312
    __ SmiDiv(rcx, rcx, r14, &fail_ok2);
1313 1314 1315
    __ jmp(exit);
    __ bind(&fail_ok2);

1316
    __ incq(r15);
1317
    __ cmpq(rcx, r11);
1318 1319 1320 1321 1322 1323 1324 1325
    __ j(not_equal, exit);
  }
}


TEST(SmiDiv) {
  // Allocate an executable page of memory.
  size_t actual_size;
1326 1327
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1328
  CHECK(buffer);
1329
  Isolate* isolate = CcTest::i_isolate();
1330 1331
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1332 1333

  MacroAssembler* masm = &assembler;
1334
  EntryCode(masm);
1335 1336
  Label exit;

1337 1338
  __ pushq(r14);
  __ pushq(r15);
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
  TestSmiDiv(masm, &exit, 0x10, 1, 1);
  TestSmiDiv(masm, &exit, 0x20, 1, 0);
  TestSmiDiv(masm, &exit, 0x30, -1, 0);
  TestSmiDiv(masm, &exit, 0x40, 0, 1);
  TestSmiDiv(masm, &exit, 0x50, 0, -1);
  TestSmiDiv(masm, &exit, 0x60, 4, 2);
  TestSmiDiv(masm, &exit, 0x70, -4, 2);
  TestSmiDiv(masm, &exit, 0x80, 4, -2);
  TestSmiDiv(masm, &exit, 0x90, -4, -2);
  TestSmiDiv(masm, &exit, 0xa0, 3, 2);
  TestSmiDiv(masm, &exit, 0xb0, 3, 4);
  TestSmiDiv(masm, &exit, 0xc0, 1, Smi::kMaxValue);
  TestSmiDiv(masm, &exit, 0xd0, -1, Smi::kMaxValue);
  TestSmiDiv(masm, &exit, 0xe0, Smi::kMaxValue, 1);
  TestSmiDiv(masm, &exit, 0xf0, Smi::kMaxValue, Smi::kMaxValue);
  TestSmiDiv(masm, &exit, 0x100, Smi::kMaxValue, -Smi::kMaxValue);
  TestSmiDiv(masm, &exit, 0x110, Smi::kMaxValue, -1);
  TestSmiDiv(masm, &exit, 0x120, Smi::kMinValue, 1);
  TestSmiDiv(masm, &exit, 0x130, Smi::kMinValue, Smi::kMinValue);
  TestSmiDiv(masm, &exit, 0x140, Smi::kMinValue, -1);

1360
  __ xorq(r15, r15);  // Success.
1361
  __ bind(&exit);
1362
  __ movq(rax, r15);
1363 1364
  __ popq(r15);
  __ popq(r14);
1365
  ExitCode(masm);
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiMod(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  bool division_by_zero = (y == 0);
  bool division_overflow = (x == Smi::kMinValue) && (y == -1);
  bool fraction = !division_by_zero && !division_overflow && ((x % y) != 0);
  bool negative_zero = (!fraction && x < 0);
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
1383
  __ Move(r14, Smi::FromInt(y));
1384 1385
  if (!division_overflow && !negative_zero && !division_by_zero) {
    // Modulo succeeds
1386
    __ movq(r15, Immediate(id));
1387 1388
    int result = x % y;
    __ Move(r8, Smi::FromInt(result));
1389
    __ SmiMod(r9, rcx, r14, exit);
1390

1391
    __ incq(r15);
1392
    __ cmpq(r9, r8);
1393 1394
    __ j(not_equal, exit);

1395
    __ incq(r15);
1396
    __ cmpq(rcx, r11);
1397 1398
    __ j(not_equal, exit);

1399
    __ incq(r15);
1400
    __ SmiMod(rcx, rcx, r14, exit);
1401

1402
    __ incq(r15);
1403
    __ cmpq(rcx, r8);
1404 1405 1406
    __ j(not_equal, exit);
  } else {
    // Modulo fails.
1407
    __ movq(r15, Immediate(id + 8));
1408 1409

    Label fail_ok, fail_ok2;
1410
    __ SmiMod(r9, rcx, r14, &fail_ok);
1411 1412 1413
    __ jmp(exit);
    __ bind(&fail_ok);

1414
    __ incq(r15);
1415
    __ cmpq(rcx, r11);
1416 1417
    __ j(not_equal, exit);

1418
    __ incq(r15);
1419
    __ SmiMod(rcx, rcx, r14, &fail_ok2);
1420 1421 1422
    __ jmp(exit);
    __ bind(&fail_ok2);

1423
    __ incq(r15);
1424
    __ cmpq(rcx, r11);
1425 1426 1427 1428 1429 1430 1431 1432
    __ j(not_equal, exit);
  }
}


TEST(SmiMod) {
  // Allocate an executable page of memory.
  size_t actual_size;
1433 1434
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1435
  CHECK(buffer);
1436
  Isolate* isolate = CcTest::i_isolate();
1437 1438
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1439 1440

  MacroAssembler* masm = &assembler;
1441
  EntryCode(masm);
1442 1443
  Label exit;

1444 1445
  __ pushq(r14);
  __ pushq(r15);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
  TestSmiMod(masm, &exit, 0x10, 1, 1);
  TestSmiMod(masm, &exit, 0x20, 1, 0);
  TestSmiMod(masm, &exit, 0x30, -1, 0);
  TestSmiMod(masm, &exit, 0x40, 0, 1);
  TestSmiMod(masm, &exit, 0x50, 0, -1);
  TestSmiMod(masm, &exit, 0x60, 4, 2);
  TestSmiMod(masm, &exit, 0x70, -4, 2);
  TestSmiMod(masm, &exit, 0x80, 4, -2);
  TestSmiMod(masm, &exit, 0x90, -4, -2);
  TestSmiMod(masm, &exit, 0xa0, 3, 2);
  TestSmiMod(masm, &exit, 0xb0, 3, 4);
  TestSmiMod(masm, &exit, 0xc0, 1, Smi::kMaxValue);
  TestSmiMod(masm, &exit, 0xd0, -1, Smi::kMaxValue);
  TestSmiMod(masm, &exit, 0xe0, Smi::kMaxValue, 1);
  TestSmiMod(masm, &exit, 0xf0, Smi::kMaxValue, Smi::kMaxValue);
  TestSmiMod(masm, &exit, 0x100, Smi::kMaxValue, -Smi::kMaxValue);
  TestSmiMod(masm, &exit, 0x110, Smi::kMaxValue, -1);
  TestSmiMod(masm, &exit, 0x120, Smi::kMinValue, 1);
  TestSmiMod(masm, &exit, 0x130, Smi::kMinValue, Smi::kMinValue);
  TestSmiMod(masm, &exit, 0x140, Smi::kMinValue, -1);

1467
  __ xorq(r15, r15);  // Success.
1468
  __ bind(&exit);
1469
  __ movq(rax, r15);
1470 1471
  __ popq(r15);
  __ popq(r14);
1472
  ExitCode(masm);
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiIndex(MacroAssembler* masm, Label* exit, int id, int x) {
  __ movl(rax, Immediate(id));

  for (int i = 0; i < 8; i++) {
    __ Move(rcx, Smi::FromInt(x));
    SmiIndex index = masm->SmiToIndex(rdx, rcx, i);
1489
    DCHECK(index.reg.is(rcx) || index.reg.is(rdx));
1490
    __ shlq(index.reg, Immediate(index.scale));
1491
    __ Set(r8, static_cast<intptr_t>(x) << i);
1492
    __ cmpq(index.reg, r8);
1493 1494 1495 1496
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToIndex(rcx, rcx, i);
1497
    DCHECK(index.reg.is(rcx));
1498
    __ shlq(rcx, Immediate(index.scale));
1499
    __ Set(r8, static_cast<intptr_t>(x) << i);
1500
    __ cmpq(rcx, r8);
1501 1502 1503 1504 1505
    __ j(not_equal, exit);
    __ incq(rax);

    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rdx, rcx, i);
1506
    DCHECK(index.reg.is(rcx) || index.reg.is(rdx));
1507
    __ shlq(index.reg, Immediate(index.scale));
1508
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1509
    __ cmpq(index.reg, r8);
1510 1511 1512 1513
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rcx, rcx, i);
1514
    DCHECK(index.reg.is(rcx));
1515
    __ shlq(rcx, Immediate(index.scale));
1516
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1517
    __ cmpq(rcx, r8);
1518 1519 1520 1521 1522
    __ j(not_equal, exit);
    __ incq(rax);
  }
}

1523

1524 1525 1526
TEST(SmiIndex) {
  // Allocate an executable page of memory.
  size_t actual_size;
1527 1528
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 5, &actual_size, true));
1529
  CHECK(buffer);
1530
  Isolate* isolate = CcTest::i_isolate();
1531 1532
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1533 1534

  MacroAssembler* masm = &assembler;
1535
  EntryCode(masm);
1536 1537 1538 1539 1540 1541 1542 1543
  Label exit;

  TestSmiIndex(masm, &exit, 0x10, 0);
  TestSmiIndex(masm, &exit, 0x20, 1);
  TestSmiIndex(masm, &exit, 0x30, 100);
  TestSmiIndex(masm, &exit, 0x40, 1000);
  TestSmiIndex(masm, &exit, 0x50, Smi::kMaxValue);

1544
  __ xorq(rax, rax);  // Success.
1545
  __ bind(&exit);
1546
  ExitCode(masm);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSelectNonSmi(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  __ movl(rax, Immediate(id));
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
1561
  __ xorq(rdx, Immediate(kSmiTagMask));
1562 1563 1564
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1565
  __ cmpq(r9, rdx);
1566 1567 1568 1569 1570
  __ j(not_equal, exit);

  __ incq(rax);
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
1571
  __ xorq(rcx, Immediate(kSmiTagMask));
1572 1573 1574
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1575
  __ cmpq(r9, rcx);
1576 1577 1578 1579 1580 1581
  __ j(not_equal, exit);

  __ incq(rax);
  Label fail_ok;
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
1582 1583
  __ xorq(rcx, Immediate(kSmiTagMask));
  __ xorq(rdx, Immediate(kSmiTagMask));
1584 1585 1586 1587 1588 1589 1590 1591 1592
  __ SelectNonSmi(r9, rcx, rdx, &fail_ok);
  __ jmp(exit);
  __ bind(&fail_ok);
}


TEST(SmiSelectNonSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
1593 1594
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1595
  CHECK(buffer);
1596
  Isolate* isolate = CcTest::i_isolate();
1597 1598
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1599 1600

  MacroAssembler* masm = &assembler;
1601
  EntryCode(masm);
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
  Label exit;

  TestSelectNonSmi(masm, &exit, 0x10, 0, 0);
  TestSelectNonSmi(masm, &exit, 0x20, 0, 1);
  TestSelectNonSmi(masm, &exit, 0x30, 1, 0);
  TestSelectNonSmi(masm, &exit, 0x40, 0, -1);
  TestSelectNonSmi(masm, &exit, 0x50, -1, 0);
  TestSelectNonSmi(masm, &exit, 0x60, -1, -1);
  TestSelectNonSmi(masm, &exit, 0x70, 1, 1);
  TestSelectNonSmi(masm, &exit, 0x80, Smi::kMinValue, Smi::kMaxValue);
  TestSelectNonSmi(masm, &exit, 0x90, Smi::kMinValue, Smi::kMinValue);

1614
  __ xorq(rax, rax);  // Success.
1615
  __ bind(&exit);
1616
  ExitCode(masm);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiAnd(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  int result = x & y;

  __ movl(rax, Immediate(id));

  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
  __ Move(rdx, Smi::FromInt(y));
  __ Move(r8, Smi::FromInt(result));
  __ SmiAnd(r9, rcx, rdx);
1637
  __ cmpq(r8, r9);
1638 1639 1640
  __ j(not_equal, exit);

  __ incq(rax);
1641
  __ cmpq(r11, rcx);
1642 1643 1644 1645
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAnd(rcx, rcx, rdx);
1646
  __ cmpq(r8, rcx);
1647 1648 1649 1650 1651
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiAndConstant(r9, rcx, Smi::FromInt(y));
1652
  __ cmpq(r8, r9);
1653 1654 1655
  __ j(not_equal, exit);

  __ incq(rax);
1656
  __ cmpq(r11, rcx);
1657 1658 1659 1660
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAndConstant(rcx, rcx, Smi::FromInt(y));
1661
  __ cmpq(r8, rcx);
1662 1663 1664 1665 1666 1667 1668
  __ j(not_equal, exit);
}


TEST(SmiAnd) {
  // Allocate an executable page of memory.
  size_t actual_size;
1669 1670
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1671
  CHECK(buffer);
1672
  Isolate* isolate = CcTest::i_isolate();
1673 1674
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1675 1676

  MacroAssembler* masm = &assembler;
1677
  EntryCode(masm);
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
  Label exit;

  TestSmiAnd(masm, &exit, 0x10, 0, 0);
  TestSmiAnd(masm, &exit, 0x20, 0, 1);
  TestSmiAnd(masm, &exit, 0x30, 1, 0);
  TestSmiAnd(masm, &exit, 0x40, 0, -1);
  TestSmiAnd(masm, &exit, 0x50, -1, 0);
  TestSmiAnd(masm, &exit, 0x60, -1, -1);
  TestSmiAnd(masm, &exit, 0x70, 1, 1);
  TestSmiAnd(masm, &exit, 0x80, Smi::kMinValue, Smi::kMaxValue);
  TestSmiAnd(masm, &exit, 0x90, Smi::kMinValue, Smi::kMinValue);
  TestSmiAnd(masm, &exit, 0xA0, Smi::kMinValue, -1);
  TestSmiAnd(masm, &exit, 0xB0, Smi::kMinValue, -1);

1692
  __ xorq(rax, rax);  // Success.
1693
  __ bind(&exit);
1694
  ExitCode(masm);
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiOr(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  int result = x | y;

  __ movl(rax, Immediate(id));

  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
  __ Move(rdx, Smi::FromInt(y));
  __ Move(r8, Smi::FromInt(result));
  __ SmiOr(r9, rcx, rdx);
1715
  __ cmpq(r8, r9);
1716 1717 1718
  __ j(not_equal, exit);

  __ incq(rax);
1719
  __ cmpq(r11, rcx);
1720 1721 1722 1723
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOr(rcx, rcx, rdx);
1724
  __ cmpq(r8, rcx);
1725 1726 1727 1728 1729
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiOrConstant(r9, rcx, Smi::FromInt(y));
1730
  __ cmpq(r8, r9);
1731 1732 1733
  __ j(not_equal, exit);

  __ incq(rax);
1734
  __ cmpq(r11, rcx);
1735 1736 1737 1738
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOrConstant(rcx, rcx, Smi::FromInt(y));
1739
  __ cmpq(r8, rcx);
1740 1741 1742 1743 1744 1745 1746
  __ j(not_equal, exit);
}


TEST(SmiOr) {
  // Allocate an executable page of memory.
  size_t actual_size;
1747 1748
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1749
  CHECK(buffer);
1750
  Isolate* isolate = CcTest::i_isolate();
1751 1752
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1753 1754

  MacroAssembler* masm = &assembler;
1755
  EntryCode(masm);
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
  Label exit;

  TestSmiOr(masm, &exit, 0x10, 0, 0);
  TestSmiOr(masm, &exit, 0x20, 0, 1);
  TestSmiOr(masm, &exit, 0x30, 1, 0);
  TestSmiOr(masm, &exit, 0x40, 0, -1);
  TestSmiOr(masm, &exit, 0x50, -1, 0);
  TestSmiOr(masm, &exit, 0x60, -1, -1);
  TestSmiOr(masm, &exit, 0x70, 1, 1);
  TestSmiOr(masm, &exit, 0x80, Smi::kMinValue, Smi::kMaxValue);
  TestSmiOr(masm, &exit, 0x90, Smi::kMinValue, Smi::kMinValue);
  TestSmiOr(masm, &exit, 0xA0, Smi::kMinValue, -1);
  TestSmiOr(masm, &exit, 0xB0, 0x05555555, 0x01234567);
  TestSmiOr(masm, &exit, 0xC0, 0x05555555, 0x0fedcba9);
  TestSmiOr(masm, &exit, 0xD0, Smi::kMinValue, -1);

1772
  __ xorq(rax, rax);  // Success.
1773
  __ bind(&exit);
1774
  ExitCode(masm);
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiXor(MacroAssembler* masm, Label* exit, int id, int x, int y) {
  int result = x ^ y;

  __ movl(rax, Immediate(id));

  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);
  __ Move(rdx, Smi::FromInt(y));
  __ Move(r8, Smi::FromInt(result));
  __ SmiXor(r9, rcx, rdx);
1795
  __ cmpq(r8, r9);
1796 1797 1798
  __ j(not_equal, exit);

  __ incq(rax);
1799
  __ cmpq(r11, rcx);
1800 1801 1802 1803
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiXor(rcx, rcx, rdx);
1804
  __ cmpq(r8, rcx);
1805 1806 1807 1808 1809
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiXorConstant(r9, rcx, Smi::FromInt(y));
1810
  __ cmpq(r8, r9);
1811 1812 1813
  __ j(not_equal, exit);

  __ incq(rax);
1814
  __ cmpq(r11, rcx);
1815 1816 1817 1818
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiXorConstant(rcx, rcx, Smi::FromInt(y));
1819
  __ cmpq(r8, rcx);
1820 1821 1822 1823 1824 1825 1826
  __ j(not_equal, exit);
}


TEST(SmiXor) {
  // Allocate an executable page of memory.
  size_t actual_size;
1827 1828
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1829
  CHECK(buffer);
1830
  Isolate* isolate = CcTest::i_isolate();
1831 1832
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1833 1834

  MacroAssembler* masm = &assembler;
1835
  EntryCode(masm);
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
  Label exit;

  TestSmiXor(masm, &exit, 0x10, 0, 0);
  TestSmiXor(masm, &exit, 0x20, 0, 1);
  TestSmiXor(masm, &exit, 0x30, 1, 0);
  TestSmiXor(masm, &exit, 0x40, 0, -1);
  TestSmiXor(masm, &exit, 0x50, -1, 0);
  TestSmiXor(masm, &exit, 0x60, -1, -1);
  TestSmiXor(masm, &exit, 0x70, 1, 1);
  TestSmiXor(masm, &exit, 0x80, Smi::kMinValue, Smi::kMaxValue);
  TestSmiXor(masm, &exit, 0x90, Smi::kMinValue, Smi::kMinValue);
  TestSmiXor(masm, &exit, 0xA0, Smi::kMinValue, -1);
  TestSmiXor(masm, &exit, 0xB0, 0x5555555, 0x01234567);
  TestSmiXor(masm, &exit, 0xC0, 0x5555555, 0x0fedcba9);
  TestSmiXor(masm, &exit, 0xD0, Smi::kMinValue, -1);

1852
  __ xorq(rax, rax);  // Success.
1853
  __ bind(&exit);
1854
  ExitCode(masm);
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiNot(MacroAssembler* masm, Label* exit, int id, int x) {
  int result = ~x;
  __ movl(rax, Immediate(id));

  __ Move(r8, Smi::FromInt(result));
  __ Move(rcx, Smi::FromInt(x));
  __ movq(r11, rcx);

  __ SmiNot(r9, rcx);
1874
  __ cmpq(r9, r8);
1875 1876 1877
  __ j(not_equal, exit);

  __ incq(rax);
1878
  __ cmpq(r11, rcx);
1879 1880 1881 1882
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiNot(rcx, rcx);
1883
  __ cmpq(rcx, r8);
1884 1885 1886 1887 1888 1889 1890
  __ j(not_equal, exit);
}


TEST(SmiNot) {
  // Allocate an executable page of memory.
  size_t actual_size;
1891 1892
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
1893
  CHECK(buffer);
1894
  Isolate* isolate = CcTest::i_isolate();
1895 1896
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1897 1898

  MacroAssembler* masm = &assembler;
1899
  EntryCode(masm);
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
  Label exit;

  TestSmiNot(masm, &exit, 0x10, 0);
  TestSmiNot(masm, &exit, 0x20, 1);
  TestSmiNot(masm, &exit, 0x30, -1);
  TestSmiNot(masm, &exit, 0x40, 127);
  TestSmiNot(masm, &exit, 0x50, 65535);
  TestSmiNot(masm, &exit, 0x60, Smi::kMinValue);
  TestSmiNot(masm, &exit, 0x70, Smi::kMaxValue);
  TestSmiNot(masm, &exit, 0x80, 0x05555555);

1911
  __ xorq(rax, rax);  // Success.
1912
  __ bind(&exit);
1913
  ExitCode(masm);
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiShiftLeft(MacroAssembler* masm, Label* exit, int id, int x) {
  const int shifts[] = { 0, 1, 7, 24, kSmiValueSize - 1};
  const int kNumShifts = 5;
  __ movl(rax, Immediate(id));
  for (int i = 0; i < kNumShifts; i++) {
    // rax == id + i * 10.
    int shift = shifts[i];
    int result = x << shift;
1932 1933 1934 1935 1936 1937
    CHECK(Smi::IsValid(result));
    __ Move(r8, Smi::FromInt(result));
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftLeftConstant(r9, rcx, shift);

    __ incq(rax);
1938
    __ cmpq(r9, r8);
1939 1940 1941 1942 1943 1944 1945
    __ j(not_equal, exit);

    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftLeftConstant(rcx, rcx, shift);

    __ incq(rax);
1946
    __ cmpq(rcx, r8);
1947 1948 1949 1950 1951 1952 1953 1954
    __ j(not_equal, exit);

    __ incq(rax);
    __ Move(rdx, Smi::FromInt(x));
    __ Move(rcx, Smi::FromInt(shift));
    __ SmiShiftLeft(r9, rdx, rcx);

    __ incq(rax);
1955
    __ cmpq(r9, r8);
1956 1957 1958 1959 1960 1961 1962 1963
    __ j(not_equal, exit);

    __ incq(rax);
    __ Move(rdx, Smi::FromInt(x));
    __ Move(r11, Smi::FromInt(shift));
    __ SmiShiftLeft(r9, rdx, r11);

    __ incq(rax);
1964
    __ cmpq(r9, r8);
1965 1966 1967 1968 1969 1970 1971 1972
    __ j(not_equal, exit);

    __ incq(rax);
    __ Move(rdx, Smi::FromInt(x));
    __ Move(r11, Smi::FromInt(shift));
    __ SmiShiftLeft(rdx, rdx, r11);

    __ incq(rax);
1973
    __ cmpq(rdx, r8);
1974 1975 1976
    __ j(not_equal, exit);

    __ incq(rax);
1977 1978 1979 1980 1981 1982 1983
  }
}


TEST(SmiShiftLeft) {
  // Allocate an executable page of memory.
  size_t actual_size;
1984 1985
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 7, &actual_size, true));
1986
  CHECK(buffer);
1987
  Isolate* isolate = CcTest::i_isolate();
1988 1989
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1990 1991

  MacroAssembler* masm = &assembler;
1992
  EntryCode(masm);
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
  Label exit;

  TestSmiShiftLeft(masm, &exit, 0x10, 0);
  TestSmiShiftLeft(masm, &exit, 0x50, 1);
  TestSmiShiftLeft(masm, &exit, 0x90, 127);
  TestSmiShiftLeft(masm, &exit, 0xD0, 65535);
  TestSmiShiftLeft(masm, &exit, 0x110, Smi::kMaxValue);
  TestSmiShiftLeft(masm, &exit, 0x150, Smi::kMinValue);
  TestSmiShiftLeft(masm, &exit, 0x190, -1);

2003
  __ xorq(rax, rax);  // Success.
2004
  __ bind(&exit);
2005
  ExitCode(masm);
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiShiftLogicalRight(MacroAssembler* masm,
                              Label* exit,
                              int id,
                              int x) {
  const int shifts[] = { 0, 1, 7, 24, kSmiValueSize - 1};
  const int kNumShifts = 5;
  __ movl(rax, Immediate(id));
  for (int i = 0; i < kNumShifts; i++) {
    int shift = shifts[i];
    intptr_t result = static_cast<unsigned int>(x) >> shift;
    if (Smi::IsValid(result)) {
2027
      __ Move(r8, Smi::FromInt(static_cast<int>(result)));
2028 2029 2030 2031
      __ Move(rcx, Smi::FromInt(x));
      __ SmiShiftLogicalRightConstant(r9, rcx, shift, exit);

      __ incq(rax);
2032
      __ cmpq(r9, r8);
2033 2034 2035 2036 2037 2038 2039 2040
      __ j(not_equal, exit);

      __ incq(rax);
      __ Move(rdx, Smi::FromInt(x));
      __ Move(rcx, Smi::FromInt(shift));
      __ SmiShiftLogicalRight(r9, rdx, rcx, exit);

      __ incq(rax);
2041
      __ cmpq(r9, r8);
2042 2043 2044 2045 2046 2047 2048 2049
      __ j(not_equal, exit);

      __ incq(rax);
      __ Move(rdx, Smi::FromInt(x));
      __ Move(r11, Smi::FromInt(shift));
      __ SmiShiftLogicalRight(r9, rdx, r11, exit);

      __ incq(rax);
2050
      __ cmpq(r9, r8);
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
      __ j(not_equal, exit);

      __ incq(rax);
    } else {
      // Cannot happen with long smis.
      Label fail_ok;
      __ Move(rcx, Smi::FromInt(x));
      __ movq(r11, rcx);
      __ SmiShiftLogicalRightConstant(r9, rcx, shift, &fail_ok);
      __ jmp(exit);
      __ bind(&fail_ok);

      __ incq(rax);
2064
      __ cmpq(rcx, r11);
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
      __ j(not_equal, exit);

      __ incq(rax);
      __ Move(r8, Smi::FromInt(shift));
      Label fail_ok3;
      __ SmiShiftLogicalRight(r9, rcx, r8, &fail_ok3);
      __ jmp(exit);
      __ bind(&fail_ok3);

      __ incq(rax);
2075
      __ cmpq(rcx, r11);
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
      __ j(not_equal, exit);

      __ addq(rax, Immediate(3));
    }
  }
}


TEST(SmiShiftLogicalRight) {
  // Allocate an executable page of memory.
  size_t actual_size;
2087 2088
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 5, &actual_size, true));
2089
  CHECK(buffer);
2090
  Isolate* isolate = CcTest::i_isolate();
2091 2092
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2093 2094

  MacroAssembler* masm = &assembler;
2095
  EntryCode(masm);
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
  Label exit;

  TestSmiShiftLogicalRight(masm, &exit, 0x10, 0);
  TestSmiShiftLogicalRight(masm, &exit, 0x30, 1);
  TestSmiShiftLogicalRight(masm, &exit, 0x50, 127);
  TestSmiShiftLogicalRight(masm, &exit, 0x70, 65535);
  TestSmiShiftLogicalRight(masm, &exit, 0x90, Smi::kMaxValue);
  TestSmiShiftLogicalRight(masm, &exit, 0xB0, Smi::kMinValue);
  TestSmiShiftLogicalRight(masm, &exit, 0xD0, -1);

2106
  __ xorq(rax, rax);  // Success.
2107
  __ bind(&exit);
2108
  ExitCode(masm);
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestSmiShiftArithmeticRight(MacroAssembler* masm,
                                 Label* exit,
                                 int id,
                                 int x) {
  const int shifts[] = { 0, 1, 7, 24, kSmiValueSize - 1};
  const int kNumShifts = 5;
  __ movl(rax, Immediate(id));
  for (int i = 0; i < kNumShifts; i++) {
    int shift = shifts[i];
    // Guaranteed arithmetic shift.
    int result = (x < 0) ? ~((~x) >> shift) : (x >> shift);
    __ Move(r8, Smi::FromInt(result));
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftArithmeticRightConstant(rcx, rcx, shift);

2134
    __ cmpq(rcx, r8);
2135 2136 2137 2138 2139 2140 2141
    __ j(not_equal, exit);

    __ incq(rax);
    __ Move(rdx, Smi::FromInt(x));
    __ Move(r11, Smi::FromInt(shift));
    __ SmiShiftArithmeticRight(rdx, rdx, r11);

2142
    __ cmpq(rdx, r8);
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
    __ j(not_equal, exit);

    __ incq(rax);
  }
}


TEST(SmiShiftArithmeticRight) {
  // Allocate an executable page of memory.
  size_t actual_size;
2153 2154
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 3, &actual_size, true));
2155
  CHECK(buffer);
2156
  Isolate* isolate = CcTest::i_isolate();
2157 2158
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2159 2160

  MacroAssembler* masm = &assembler;
2161
  EntryCode(masm);
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
  Label exit;

  TestSmiShiftArithmeticRight(masm, &exit, 0x10, 0);
  TestSmiShiftArithmeticRight(masm, &exit, 0x20, 1);
  TestSmiShiftArithmeticRight(masm, &exit, 0x30, 127);
  TestSmiShiftArithmeticRight(masm, &exit, 0x40, 65535);
  TestSmiShiftArithmeticRight(masm, &exit, 0x50, Smi::kMaxValue);
  TestSmiShiftArithmeticRight(masm, &exit, 0x60, Smi::kMinValue);
  TestSmiShiftArithmeticRight(masm, &exit, 0x70, -1);

2172
  __ xorq(rax, rax);  // Success.
2173
  __ bind(&exit);
2174
  ExitCode(masm);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


void TestPositiveSmiPowerUp(MacroAssembler* masm, Label* exit, int id, int x) {
2186
  DCHECK(x >= 0);
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
  int powers[] = { 0, 1, 2, 3, 8, 16, 24, 31 };
  int power_count = 8;
  __ movl(rax, Immediate(id));
  for (int i = 0; i  < power_count; i++) {
    int power = powers[i];
    intptr_t result = static_cast<intptr_t>(x) << power;
    __ Set(r8, result);
    __ Move(rcx, Smi::FromInt(x));
    __ movq(r11, rcx);
    __ PositiveSmiTimesPowerOfTwoToInteger64(rdx, rcx, power);
2197
    __ cmpq(rdx, r8);
2198 2199
    __ j(not_equal, exit);
    __ incq(rax);
2200
    __ cmpq(r11, rcx);  // rcx unchanged.
2201 2202 2203
    __ j(not_equal, exit);
    __ incq(rax);
    __ PositiveSmiTimesPowerOfTwoToInteger64(rcx, rcx, power);
2204
    __ cmpq(rdx, r8);
2205 2206 2207 2208 2209 2210 2211 2212 2213
    __ j(not_equal, exit);
    __ incq(rax);
  }
}


TEST(PositiveSmiTimesPowerOfTwoToInteger64) {
  // Allocate an executable page of memory.
  size_t actual_size;
2214 2215
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 4, &actual_size, true));
2216
  CHECK(buffer);
2217
  Isolate* isolate = CcTest::i_isolate();
2218 2219
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2220 2221

  MacroAssembler* masm = &assembler;
2222
  EntryCode(masm);
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
  Label exit;

  TestPositiveSmiPowerUp(masm, &exit, 0x20, 0);
  TestPositiveSmiPowerUp(masm, &exit, 0x40, 1);
  TestPositiveSmiPowerUp(masm, &exit, 0x60, 127);
  TestPositiveSmiPowerUp(masm, &exit, 0x80, 128);
  TestPositiveSmiPowerUp(masm, &exit, 0xA0, 255);
  TestPositiveSmiPowerUp(masm, &exit, 0xC0, 256);
  TestPositiveSmiPowerUp(masm, &exit, 0x100, 65535);
  TestPositiveSmiPowerUp(masm, &exit, 0x120, 65536);
  TestPositiveSmiPowerUp(masm, &exit, 0x140, Smi::kMaxValue);

2235
  __ xorq(rax, rax);  // Success.
2236
  __ bind(&exit);
2237
  ExitCode(masm);
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


2248
TEST(OperandOffset) {
2249 2250
  uint32_t data[256];
  for (uint32_t i = 0; i < 256; i++) { data[i] = i * 0x01010101; }
2251 2252 2253

  // Allocate an executable page of memory.
  size_t actual_size;
2254 2255
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
2256
  CHECK(buffer);
2257
  Isolate* isolate = CcTest::i_isolate();
2258 2259
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2260 2261 2262 2263

  MacroAssembler* masm = &assembler;
  Label exit;

2264
  EntryCode(masm);
2265 2266 2267 2268 2269
  __ pushq(r13);
  __ pushq(r14);
  __ pushq(rbx);
  __ pushq(rbp);
  __ pushq(Immediate(0x100));  // <-- rbp
2270
  __ movq(rbp, rsp);
2271 2272 2273 2274 2275 2276 2277 2278 2279
  __ pushq(Immediate(0x101));
  __ pushq(Immediate(0x102));
  __ pushq(Immediate(0x103));
  __ pushq(Immediate(0x104));
  __ pushq(Immediate(0x105));  // <-- rbx
  __ pushq(Immediate(0x106));
  __ pushq(Immediate(0x107));
  __ pushq(Immediate(0x108));
  __ pushq(Immediate(0x109));  // <-- rsp
2280
  // rbp = rsp[9]
2281
  // r15 = rsp[3]
2282 2283
  // rbx = rsp[5]
  // r13 = rsp[7]
2284 2285 2286
  __ leaq(r14, Operand(rsp, 3 * kPointerSize));
  __ leaq(r13, Operand(rbp, -3 * kPointerSize));
  __ leaq(rbx, Operand(rbp, -5 * kPointerSize));
2287
  __ movl(rcx, Immediate(2));
2288
  __ Move(r8, reinterpret_cast<Address>(&data[128]), RelocInfo::NONE64);
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
  __ movl(rax, Immediate(1));

  Operand sp0 = Operand(rsp, 0);

  // Test 1.
  __ movl(rdx, sp0);  // Sanity check.
  __ cmpl(rdx, Immediate(0x109));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Test 2.
  // Zero to non-zero displacement.
  __ movl(rdx, Operand(sp0, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x107));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand sp2 = Operand(rsp, 2 * kPointerSize);

  // Test 3.
  __ movl(rdx, sp2);  // Sanity check.
  __ cmpl(rdx, Immediate(0x107));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(sp2, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x105));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Non-zero to zero displacement.
  __ movl(rdx, Operand(sp2, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x109));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand sp2c2 = Operand(rsp, rcx, times_pointer_size, 2 * kPointerSize);

  // Test 6.
  __ movl(rdx, sp2c2);  // Sanity check.
  __ cmpl(rdx, Immediate(0x105));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(sp2c2, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x103));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Non-zero to zero displacement.
  __ movl(rdx, Operand(sp2c2, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x107));
  __ j(not_equal, &exit);
  __ incq(rax);


  Operand bp0 = Operand(rbp, 0);

  // Test 9.
  __ movl(rdx, bp0);  // Sanity check.
  __ cmpl(rdx, Immediate(0x100));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Zero to non-zero displacement.
  __ movl(rdx, Operand(bp0, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x102));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand bp2 = Operand(rbp, -2 * kPointerSize);

  // Test 11.
  __ movl(rdx, bp2);  // Sanity check.
  __ cmpl(rdx, Immediate(0x102));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Non-zero to zero displacement.
  __ movl(rdx, Operand(bp2, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x100));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bp2, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x104));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand bp2c4 = Operand(rbp, rcx, times_pointer_size, -4 * kPointerSize);

  // Test 14:
  __ movl(rdx, bp2c4);  // Sanity check.
  __ cmpl(rdx, Immediate(0x102));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bp2c4, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x100));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bp2c4, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x104));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand bx0 = Operand(rbx, 0);

  // Test 17.
  __ movl(rdx, bx0);  // Sanity check.
  __ cmpl(rdx, Immediate(0x105));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bx0, 5 * kPointerSize));
  __ cmpl(rdx, Immediate(0x100));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bx0, -4 * kPointerSize));
  __ cmpl(rdx, Immediate(0x109));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand bx2 = Operand(rbx, 2 * kPointerSize);

  // Test 20.
  __ movl(rdx, bx2);  // Sanity check.
  __ cmpl(rdx, Immediate(0x103));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bx2, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x101));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Non-zero to zero displacement.
  __ movl(rdx, Operand(bx2, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x105));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand bx2c2 = Operand(rbx, rcx, times_pointer_size, -2 * kPointerSize);

  // Test 23.
  __ movl(rdx, bx2c2);  // Sanity check.
  __ cmpl(rdx, Immediate(0x105));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bx2c2, 2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x103));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(bx2c2, -2 * kPointerSize));
  __ cmpl(rdx, Immediate(0x107));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand r80 = Operand(r8, 0);

  // Test 26.
  __ movl(rdx, r80);  // Sanity check.
  __ cmpl(rdx, Immediate(0x80808080));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, -8 * kIntSize));
  __ cmpl(rdx, Immediate(0x78787878));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, 8 * kIntSize));
  __ cmpl(rdx, Immediate(0x88888888));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, -64 * kIntSize));
  __ cmpl(rdx, Immediate(0x40404040));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, 64 * kIntSize));
  __ cmpl(rdx, Immediate(0xC0C0C0C0));
  __ j(not_equal, &exit);
  __ incq(rax);

  Operand r88 = Operand(r8, 8 * kIntSize);

  // Test 31.
  __ movl(rdx, r88);  // Sanity check.
  __ cmpl(rdx, Immediate(0x88888888));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r88, -8 * kIntSize));
  __ cmpl(rdx, Immediate(0x80808080));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r88, 8 * kIntSize));
  __ cmpl(rdx, Immediate(0x90909090));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r88, -64 * kIntSize));
  __ cmpl(rdx, Immediate(0x48484848));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r88, 64 * kIntSize));
  __ cmpl(rdx, Immediate(0xC8C8C8C8));
  __ j(not_equal, &exit);
  __ incq(rax);


  Operand r864 = Operand(r8, 64 * kIntSize);

  // Test 36.
  __ movl(rdx, r864);  // Sanity check.
  __ cmpl(rdx, Immediate(0xC0C0C0C0));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r864, -8 * kIntSize));
  __ cmpl(rdx, Immediate(0xB8B8B8B8));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r864, 8 * kIntSize));
  __ cmpl(rdx, Immediate(0xC8C8C8C8));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r864, -64 * kIntSize));
  __ cmpl(rdx, Immediate(0x80808080));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r864, 32 * kIntSize));
  __ cmpl(rdx, Immediate(0xE0E0E0E0));
  __ j(not_equal, &exit);
  __ incq(rax);

  // 32-bit offset to 8-bit offset.
  __ movl(rdx, Operand(r864, -60 * kIntSize));
  __ cmpl(rdx, Immediate(0x84848484));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r864, 60 * kIntSize));
  __ cmpl(rdx, Immediate(0xFCFCFCFC));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Test unaligned offsets.

  // Test 43.
  __ movl(rdx, Operand(r80, 2));
  __ cmpl(rdx, Immediate(0x81818080));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, -2));
  __ cmpl(rdx, Immediate(0x80807F7F));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, 126));
  __ cmpl(rdx, Immediate(0xA0A09F9F));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, -126));
  __ cmpl(rdx, Immediate(0x61616060));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, 254));
  __ cmpl(rdx, Immediate(0xC0C0BFBF));
  __ j(not_equal, &exit);
  __ incq(rax);

  __ movl(rdx, Operand(r80, -254));
  __ cmpl(rdx, Immediate(0x41414040));
  __ j(not_equal, &exit);
  __ incq(rax);

  // Success.

  __ movl(rax, Immediate(0));
  __ bind(&exit);
2584
  __ leaq(rsp, Operand(rbp, kPointerSize));
2585 2586 2587 2588
  __ popq(rbp);
  __ popq(rbx);
  __ popq(r14);
  __ popq(r13);
2589
  ExitCode(masm);
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
  __ ret(0);


  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}


2601 2602 2603
TEST(LoadAndStoreWithRepresentation) {
  // Allocate an executable page of memory.
  size_t actual_size;
2604 2605
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
  CHECK(buffer);
  Isolate* isolate = CcTest::i_isolate();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
  EntryCode(masm);
  __ subq(rsp, Immediate(1 * kPointerSize));
  Label exit;

  // Test 1.
  __ movq(rax, Immediate(1));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
2619
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::UInteger8());
2620 2621 2622 2623
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(255));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
2624
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::UInteger8());
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 2.
  __ movq(rax, Immediate(2));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ Set(rcx, V8_2PART_UINT64_C(0xdeadbeaf, 12345678));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Smi());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ Set(rdx, V8_2PART_UINT64_C(0xdeadbeaf, 12345678));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Smi());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 3.
  __ movq(rax, Immediate(3));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Integer32());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(-1));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Integer32());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 4.
  __ movq(rax, Immediate(4));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movl(rcx, Immediate(0x44332211));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::HeapObject());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(0x44332211));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::HeapObject());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 5.
  __ movq(rax, Immediate(5));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ Set(rcx, V8_2PART_UINT64_C(0x12345678, deadbeaf));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Tagged());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ Set(rdx, V8_2PART_UINT64_C(0x12345678, deadbeaf));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Tagged());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 6.
  __ movq(rax, Immediate(6));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ Set(rcx, V8_2PART_UINT64_C(0x11223344, 55667788));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::External());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ Set(rdx, V8_2PART_UINT64_C(0x11223344, 55667788));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::External());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733
  // Test 7.
  __ movq(rax, Immediate(7));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Integer8());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(255));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Integer8());
  __ movq(rcx, Immediate(-1));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 8.
  __ movq(rax, Immediate(8));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Integer16());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(65535));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Integer16());
  __ movq(rcx, Immediate(-1));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

  // Test 9.
  __ movq(rax, Immediate(9));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::UInteger16());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(65535));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::UInteger16());
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

2734
  __ xorq(rax, rax);  // Success.
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
  __ bind(&exit);
  __ addq(rsp, Immediate(1 * kPointerSize));
  ExitCode(masm);
  __ ret(0);

  CodeDesc desc;
  masm->GetCode(&desc);
  // Call the function from C++.
  int result = FUNCTION_CAST<F0>(buffer)();
  CHECK_EQ(0, result);
}

2747

2748
#undef __