test-macro-assembler-x64.cc 68.6 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 30 31 32 33 34 35 36 37 38
// 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>

#include "v8.h"

#include "macro-assembler.h"
#include "factory.h"
#include "platform.h"
#include "serialize.h"
#include "cctest.h"

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

// 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->

95 96 97 98

static void EntryCode(MacroAssembler* masm) {
  // Smi constant register is callee save.
  __ push(v8::internal::kSmiConstantRegister);
99
  __ push(v8::internal::kRootRegister);
100
  __ InitializeSmiConstantRegister();
101
  __ InitializeRootRegister();
102 103 104 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));
  __ cmpq(rdx, v8::internal::kSmiConstantRegister);
  __ movq(rdx, Immediate(-1));
  __ cmovq(not_equal, rax, rdx);
111
  __ pop(v8::internal::kRootRegister);
112 113 114 115
  __ pop(v8::internal::kSmiConstantRegister);
}


116 117
TEST(Smi) {
  // Check that C++ Smi operations work as expected.
118
  int64_t test_numbers[] = {
119
      0, 1, -1, 127, 128, -128, -129, 255, 256, -256, -257,
120 121
      Smi::kMaxValue, static_cast<int64_t>(Smi::kMaxValue) + 1,
      Smi::kMinValue, static_cast<int64_t>(Smi::kMinValue) - 1
122 123 124
  };
  int test_number_count = 15;
  for (int i = 0; i < test_number_count; i++) {
125
    int64_t number = test_numbers[i];
126 127 128 129 130 131 132 133 134
    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);
      }
135 136
      int64_t smi_value = smi_from_intptr->value();
      CHECK_EQ(number, smi_value);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }
  }
}


static void TestMoveSmi(MacroAssembler* masm, Label* exit, int id, Smi* value) {
  __ movl(rax, Immediate(id));
  __ Move(rcx, Smi::FromInt(0));
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(0)));
  __ cmpq(rcx, rdx);
  __ j(not_equal, exit);
}


// Test that we can move a Smi value literally into a register.
TEST(SmiMove) {
153
  v8::internal::V8::Initialize(NULL);
154 155 156 157 158 159
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                   &actual_size,
                                                   true));
  CHECK(buffer);
160 161 162
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
163 164
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
  masm->set_allow_stub_calls(false);
165
  EntryCode(masm);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  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));

  __ xor_(rax, rax);  // Success.
  __ 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  __ 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 {
    ASSERT_EQ(x, y);
    __ 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 {
      ASSERT(y > x);
      __ 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
    __ 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) {
241
  v8::internal::V8::Initialize(NULL);
242 243 244
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
245
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
246 247 248
                                      &actual_size,
                                      true));
  CHECK(buffer);
249 250 251
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
252 253 254

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
255
  EntryCode(masm);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
280
  ExitCode(masm);
281 282 283 284 285 286 287 288 289 290 291 292
  __ ret(0);

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



TEST(Integer32ToSmi) {
293
  v8::internal::V8::Initialize(NULL);
294 295 296 297 298 299
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
300 301 302
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
303 304 305

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
306
  EntryCode(masm);
307 308 309 310 311 312
  Label exit;

  __ movq(rax, Immediate(1));  // Test number.
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(0)));
313
  __ cmpq(rcx, rdx);
314 315 316 317 318 319
  __ 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)));
320
  __ cmpq(rcx, rdx);
321 322 323 324 325 326
  __ 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)));
327
  __ cmpq(rcx, rdx);
328 329 330 331 332 333
  __ 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)));
334
  __ cmpq(rcx, rdx);
335 336 337 338 339 340
  __ 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)));
341
  __ cmpq(rcx, rdx);
342 343 344 345 346 347 348 349
  __ 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)));
350
  __ cmpq(r8, rdx);
351 352 353 354 355 356
  __ 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)));
357
  __ cmpq(r8, rdx);
358 359 360 361 362 363
  __ 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)));
364
  __ cmpq(r8, rdx);
365 366 367 368 369 370
  __ 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)));
371
  __ cmpq(r8, rdx);
372 373 374 375 376 377
  __ 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)));
378
  __ cmpq(r8, rdx);
379 380 381 382 383
  __ j(not_equal, &exit);


  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
384
  ExitCode(masm);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  __ 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;
  ASSERT(Smi::IsValid(result));
  __ movl(rax, Immediate(id));
403
  __ Move(r8, Smi::FromInt(static_cast<int>(result)));
404
  __ movq(rcx, x, RelocInfo::NONE64);
405 406
  __ movq(r11, rcx);
  __ Integer64PlusConstantToSmi(rdx, rcx, y);
407
  __ cmpq(rdx, r8);
408 409 410
  __ j(not_equal, exit);

  __ incq(rax);
411
  __ cmpq(r11, rcx);
412 413 414 415
  __ j(not_equal, exit);

  __ incq(rax);
  __ Integer64PlusConstantToSmi(rcx, rcx, y);
416
  __ cmpq(rcx, r8);
417 418 419 420 421
  __ j(not_equal, exit);
}


TEST(Integer64PlusConstantToSmi) {
422
  v8::internal::V8::Initialize(NULL);
423 424 425 426 427 428
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
429 430 431
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
432 433 434

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
435
  EntryCode(masm);
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
455
  ExitCode(masm);
456 457 458 459 460 461 462 463 464 465 466
  __ ret(0);

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


TEST(SmiCheck) {
467
  v8::internal::V8::Initialize(NULL);
468 469 470 471 472 473
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                   &actual_size,
                                                   true));
  CHECK(buffer);
474 475 476
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
477 478 479

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
480
  EntryCode(masm);
481 482 483 484 485 486 487 488 489 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 527 528 529 530 531 532 533 534 535
  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);
  __ xor_(rcx, Immediate(kSmiTagMask));
  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);
  __ xor_(rcx, Immediate(kSmiTagMask));
  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);
  __ xor_(rcx, Immediate(kSmiTagMask));
  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);
  __ xor_(rcx, Immediate(kSmiTagMask));
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  // CheckPositiveSmi

  __ incq(rax);
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
536
  cond = masm->CheckNonNegativeSmi(rcx);
537 538 539 540
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
541
  cond = masm->CheckNonNegativeSmi(rcx);  // "zero" non-smi.
542 543 544 545 546
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
547
  cond = masm->CheckNonNegativeSmi(rcx);  // Negative smis are not positive.
548 549 550 551 552
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
553
  cond = masm->CheckNonNegativeSmi(rcx);  // Most negative smi is not positive.
554 555 556 557
  __ j(cond, &exit);

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
558
  cond = masm->CheckNonNegativeSmi(rcx);  // "Negative" non-smi.
559 560 561 562 563
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
564
  cond = masm->CheckNonNegativeSmi(rcx);  // Most positive smi is positive.
565 566 567 568
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
569
  cond = masm->CheckNonNegativeSmi(rcx);  // "Positive" non-smi.
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
  __ 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);
  __ xor_(rcx, Immediate(kSmiTagMask));
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

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

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
  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
  __ xor_(rax, rax);

  __ bind(&exit);
656
  ExitCode(masm);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  __ 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);
677
    __ cmpq(r11, rcx);
678 679 680 681 682 683
    __ j(not_equal, exit);

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

    __ incq(rax);
684
    __ cmpq(r11, rcx);
685 686 687 688 689 690 691 692 693 694 695
    __ 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);
696
    __ cmpq(r9, r8);
697 698 699
    __ j(not_equal, exit);

    __ incq(rax);
700
    __ cmpq(r11, rcx);
701 702 703 704 705 706 707
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiNeg(rcx, rcx, &smi_ok2);
    __ jmp(exit);
    __ bind(&smi_ok2);
    __ incq(rax);
708
    __ cmpq(rcx, r8);
709 710 711 712 713 714
    __ j(not_equal, exit);
  }
}


TEST(SmiNeg) {
715
  v8::internal::V8::Initialize(NULL);
716 717 718 719 720 721 722
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
723 724 725
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
726 727 728

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
729
  EntryCode(masm);
730 731 732 733 734 735 736 737 738 739 740 741 742
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
743
  ExitCode(masm);
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  __ 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);
770
  __ cmpq(r9, r8);
771 772 773 774
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAdd(rcx, rcx, rdx, exit);                              \
775
  __ cmpq(rcx, r8);
776 777 778 779 780 781 782
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second));
783
  __ cmpq(r9, r8);
784 785 786
  __ j(not_equal, exit);

  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second));
787
  __ cmpq(rcx, r8);
788 789 790 791 792 793 794
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second), exit);
795
  __ cmpq(r9, r8);
796 797 798 799
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), exit);
800
  __ cmpq(rcx, r8);
801 802 803
  __ j(not_equal, exit);
}

804

805
TEST(SmiAdd) {
806
  v8::internal::V8::Initialize(NULL);
807 808 809 810 811 812
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
813 814 815
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
816 817 818

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
819
  EntryCode(masm);
820 821 822 823 824 825 826 827 828 829 830 831 832 833
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
834
  ExitCode(masm);
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
  __ 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);
856
  __ cmpq(r9, r8);
857 858 859 860
  __ j(not_equal, exit);

  __ incq(rax);  // Test 1.
  __ SmiSub(rcx, rcx, rdx, exit);
861
  __ cmpq(rcx, r8);
862 863 864 865 866 867
  __ j(not_equal, exit);

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

  __ incq(rax);  // Test 2.
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second));
868
  __ cmpq(r9, r8);
869 870 871 872
  __ j(not_equal, exit);

  __ incq(rax);  // Test 3.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second));
873
  __ cmpq(rcx, r8);
874 875 876 877 878 879
  __ j(not_equal, exit);

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

  __ incq(rax);  // Test 4.
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second), exit);
880
  __ cmpq(r9, r8);
881 882 883 884
  __ j(not_equal, exit);

  __ incq(rax);  // Test 5.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), exit);
885
  __ cmpq(rcx, r8);
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
  __ j(not_equal, exit);
}

static void SmiSubOverflowTest(MacroAssembler* masm,
                               Label* exit,
                               int id,
                               int x) {
  // Subtracts a Smi from x so that the subtraction overflows.
  ASSERT(x != -1);  // Can't overflow by subtracting a Smi.
  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);
908
    __ cmpq(rcx, r11);
909 910 911 912 913 914 915 916 917 918
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
919
    __ cmpq(rcx, r11);
920 921 922 923 924 925 926 927 928 929 930
    __ j(not_equal, exit);
  }

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSubConstant(r9, rcx, Smi::FromInt(y_min), &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
931
    __ cmpq(rcx, r11);
932 933 934 935 936 937 938 939 940 941
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_min), &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
942
    __ cmpq(rcx, r11);
943 944 945 946 947 948 949 950 951 952 953 954
    __ 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);
955
    __ cmpq(rcx, r11);
956 957 958 959 960 961 962 963 964 965
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
966
    __ cmpq(rcx, r11);
967 968 969 970 971 972 973 974 975 976 977
    __ j(not_equal, exit);
  }

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSubConstant(r9, rcx, Smi::FromInt(y_max), &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
978
    __ cmpq(rcx, r11);
979 980 981 982 983 984 985 986 987 988
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_max), &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
989
    __ cmpq(rcx, r11);
990 991 992 993 994 995
    __ j(not_equal, exit);
  }
}


TEST(SmiSub) {
996
  v8::internal::V8::Initialize(NULL);
997 998 999 1000 1001 1002 1003
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1004 1005 1006
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1007 1008 1009

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1010
  EntryCode(masm);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1033
  ExitCode(masm);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  __ 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);
1056
    __ cmpq(r11, rcx);
1057 1058
    __ j(not_equal, exit);
    __ incq(rax);
1059
    __ cmpq(r9, r8);
1060 1061 1062 1063
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiMul(rcx, rcx, rdx, exit);
1064
    __ cmpq(rcx, r8);
1065 1066 1067 1068 1069 1070 1071 1072
    __ 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);
1073
    __ cmpq(r11, rcx);
1074 1075 1076 1077 1078 1079 1080
    __ 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);
1081
    // __ cmpq(r11, rcx);
1082 1083 1084 1085 1086 1087
    // __ j(not_equal, exit);
  }
}


TEST(SmiMul) {
1088
  v8::internal::V8::Initialize(NULL);
1089 1090 1091 1092 1093 1094
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
1095 1096 1097
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1098 1099 1100

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1101
  EntryCode(masm);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1121
  ExitCode(masm);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  __ 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);
1135
#if V8_TARGET_ARCH_X64
1136 1137 1138 1139 1140 1141
  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));
1142
  __ Move(r14, Smi::FromInt(y));
1143 1144 1145
  if (!fraction && !overflow && !negative_zero && !division_by_zero) {
    // Division succeeds
    __ movq(rcx, r11);
1146
    __ movq(r15, Immediate(id));
1147 1148
    int result = x / y;
    __ Move(r8, Smi::FromInt(result));
1149 1150
    __ SmiDiv(r9, rcx, r14, exit);
    // Might have destroyed rcx and r14.
1151
    __ incq(r15);
1152
    __ cmpq(r9, r8);
1153 1154
    __ j(not_equal, exit);

1155
    __ incq(r15);
1156
    __ movq(rcx, r11);
1157
    __ Move(r14, Smi::FromInt(y));
1158
    __ cmpq(rcx, r11);
1159 1160
    __ j(not_equal, exit);

1161
    __ incq(r15);
1162
    __ SmiDiv(rcx, rcx, r14, exit);
1163

1164
    __ incq(r15);
1165
    __ cmpq(rcx, r8);
1166 1167 1168
    __ j(not_equal, exit);
  } else {
    // Division fails.
1169
    __ movq(r15, Immediate(id + 8));
1170 1171 1172

    Label fail_ok, fail_ok2;
    __ movq(rcx, r11);
1173
    __ SmiDiv(r9, rcx, r14, &fail_ok);
1174 1175 1176
    __ jmp(exit);
    __ bind(&fail_ok);

1177
    __ incq(r15);
1178
    __ cmpq(rcx, r11);
1179 1180
    __ j(not_equal, exit);

1181
    __ incq(r15);
1182
    __ SmiDiv(rcx, rcx, r14, &fail_ok2);
1183 1184 1185
    __ jmp(exit);
    __ bind(&fail_ok2);

1186
    __ incq(r15);
1187
    __ cmpq(rcx, r11);
1188 1189 1190 1191 1192 1193
    __ j(not_equal, exit);
  }
}


TEST(SmiDiv) {
1194
  v8::internal::V8::Initialize(NULL);
1195 1196 1197 1198 1199 1200 1201
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1202 1203 1204
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1205 1206 1207

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1208
  EntryCode(masm);
1209 1210
  Label exit;

1211
  __ push(r14);
1212
  __ push(r15);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  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);

1234
  __ xor_(r15, r15);  // Success.
1235
  __ bind(&exit);
1236 1237
  __ movq(rax, r15);
  __ pop(r15);
1238 1239
  __ pop(r14);
  ExitCode(masm);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  __ 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);
1257
  __ Move(r14, Smi::FromInt(y));
1258 1259
  if (!division_overflow && !negative_zero && !division_by_zero) {
    // Modulo succeeds
1260
    __ movq(r15, Immediate(id));
1261 1262
    int result = x % y;
    __ Move(r8, Smi::FromInt(result));
1263
    __ SmiMod(r9, rcx, r14, exit);
1264

1265
    __ incq(r15);
1266
    __ cmpq(r9, r8);
1267 1268
    __ j(not_equal, exit);

1269
    __ incq(r15);
1270
    __ cmpq(rcx, r11);
1271 1272
    __ j(not_equal, exit);

1273
    __ incq(r15);
1274
    __ SmiMod(rcx, rcx, r14, exit);
1275

1276
    __ incq(r15);
1277
    __ cmpq(rcx, r8);
1278 1279 1280
    __ j(not_equal, exit);
  } else {
    // Modulo fails.
1281
    __ movq(r15, Immediate(id + 8));
1282 1283

    Label fail_ok, fail_ok2;
1284
    __ SmiMod(r9, rcx, r14, &fail_ok);
1285 1286 1287
    __ jmp(exit);
    __ bind(&fail_ok);

1288
    __ incq(r15);
1289
    __ cmpq(rcx, r11);
1290 1291
    __ j(not_equal, exit);

1292
    __ incq(r15);
1293
    __ SmiMod(rcx, rcx, r14, &fail_ok2);
1294 1295 1296
    __ jmp(exit);
    __ bind(&fail_ok2);

1297
    __ incq(r15);
1298
    __ cmpq(rcx, r11);
1299 1300 1301 1302 1303 1304
    __ j(not_equal, exit);
  }
}


TEST(SmiMod) {
1305
  v8::internal::V8::Initialize(NULL);
1306 1307 1308 1309 1310 1311 1312
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1313 1314 1315
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1316 1317 1318

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1319
  EntryCode(masm);
1320 1321
  Label exit;

1322
  __ push(r14);
1323
  __ push(r15);
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  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);

1345
  __ xor_(r15, r15);  // Success.
1346
  __ bind(&exit);
1347 1348
  __ movq(rax, r15);
  __ pop(r15);
1349 1350
  __ pop(r14);
  ExitCode(masm);
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  __ 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);
    ASSERT(index.reg.is(rcx) || index.reg.is(rdx));
    __ shl(index.reg, Immediate(index.scale));
    __ Set(r8, static_cast<intptr_t>(x) << i);
1370
    __ cmpq(index.reg, r8);
1371 1372 1373 1374 1375 1376 1377
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToIndex(rcx, rcx, i);
    ASSERT(index.reg.is(rcx));
    __ shl(rcx, Immediate(index.scale));
    __ Set(r8, static_cast<intptr_t>(x) << i);
1378
    __ cmpq(rcx, r8);
1379 1380 1381 1382 1383 1384 1385 1386
    __ j(not_equal, exit);
    __ incq(rax);

    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rdx, rcx, i);
    ASSERT(index.reg.is(rcx) || index.reg.is(rdx));
    __ shl(index.reg, Immediate(index.scale));
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1387
    __ cmpq(index.reg, r8);
1388 1389 1390 1391 1392 1393 1394
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rcx, rcx, i);
    ASSERT(index.reg.is(rcx));
    __ shl(rcx, Immediate(index.scale));
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1395
    __ cmpq(rcx, r8);
1396 1397 1398 1399 1400
    __ j(not_equal, exit);
    __ incq(rax);
  }
}

1401

1402
TEST(SmiIndex) {
1403
  v8::internal::V8::Initialize(NULL);
1404 1405 1406
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
1407
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 3,
1408 1409 1410
                                      &actual_size,
                                      true));
  CHECK(buffer);
1411 1412 1413
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1414 1415 1416

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1417
  EntryCode(masm);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1428
  ExitCode(masm);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
  __ 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));
  __ xor_(rdx, Immediate(kSmiTagMask));
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1447
  __ cmpq(r9, rdx);
1448 1449 1450 1451 1452 1453 1454 1455 1456
  __ j(not_equal, exit);

  __ incq(rax);
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
  __ xor_(rcx, Immediate(kSmiTagMask));
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1457
  __ cmpq(r9, rcx);
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
  __ j(not_equal, exit);

  __ incq(rax);
  Label fail_ok;
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
  __ xor_(rcx, Immediate(kSmiTagMask));
  __ xor_(rdx, Immediate(kSmiTagMask));
  __ SelectNonSmi(r9, rcx, rdx, &fail_ok);
  __ jmp(exit);
  __ bind(&fail_ok);
}


TEST(SmiSelectNonSmi) {
1473
  v8::internal::V8::Initialize(NULL);
1474 1475 1476 1477 1478 1479 1480
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1481 1482 1483
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1484 1485 1486

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);  // Avoid inline checks.
1487
  EntryCode(masm);
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1502
  ExitCode(masm);
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
  __ 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);
1523
  __ cmpq(r8, r9);
1524 1525 1526
  __ j(not_equal, exit);

  __ incq(rax);
1527
  __ cmpq(r11, rcx);
1528 1529 1530 1531
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAnd(rcx, rcx, rdx);
1532
  __ cmpq(r8, rcx);
1533 1534 1535 1536 1537
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiAndConstant(r9, rcx, Smi::FromInt(y));
1538
  __ cmpq(r8, r9);
1539 1540 1541
  __ j(not_equal, exit);

  __ incq(rax);
1542
  __ cmpq(r11, rcx);
1543 1544 1545 1546
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAndConstant(rcx, rcx, Smi::FromInt(y));
1547
  __ cmpq(r8, rcx);
1548 1549 1550 1551 1552
  __ j(not_equal, exit);
}


TEST(SmiAnd) {
1553
  v8::internal::V8::Initialize(NULL);
1554 1555 1556 1557 1558 1559 1560
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1561 1562 1563
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1564 1565 1566

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1567
  EntryCode(masm);
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1584
  ExitCode(masm);
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  __ 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);
1605
  __ cmpq(r8, r9);
1606 1607 1608
  __ j(not_equal, exit);

  __ incq(rax);
1609
  __ cmpq(r11, rcx);
1610 1611 1612 1613
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOr(rcx, rcx, rdx);
1614
  __ cmpq(r8, rcx);
1615 1616 1617 1618 1619
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiOrConstant(r9, rcx, Smi::FromInt(y));
1620
  __ cmpq(r8, r9);
1621 1622 1623
  __ j(not_equal, exit);

  __ incq(rax);
1624
  __ cmpq(r11, rcx);
1625 1626 1627 1628
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOrConstant(rcx, rcx, Smi::FromInt(y));
1629
  __ cmpq(r8, rcx);
1630 1631 1632 1633 1634
  __ j(not_equal, exit);
}


TEST(SmiOr) {
1635
  v8::internal::V8::Initialize(NULL);
1636 1637 1638 1639 1640 1641 1642
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1643 1644 1645
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1646 1647 1648

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1649
  EntryCode(masm);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1668
  ExitCode(masm);
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
  __ 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);
1689
  __ cmpq(r8, r9);
1690 1691 1692
  __ j(not_equal, exit);

  __ incq(rax);
1693
  __ cmpq(r11, rcx);
1694 1695 1696 1697
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiXor(rcx, rcx, rdx);
1698
  __ cmpq(r8, rcx);
1699 1700 1701 1702 1703
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiXorConstant(r9, rcx, Smi::FromInt(y));
1704
  __ cmpq(r8, r9);
1705 1706 1707
  __ j(not_equal, exit);

  __ incq(rax);
1708
  __ cmpq(r11, rcx);
1709 1710 1711 1712
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiXorConstant(rcx, rcx, Smi::FromInt(y));
1713
  __ cmpq(r8, rcx);
1714 1715 1716 1717 1718
  __ j(not_equal, exit);
}


TEST(SmiXor) {
1719
  v8::internal::V8::Initialize(NULL);
1720 1721 1722 1723 1724 1725 1726
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1727 1728 1729
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1730 1731 1732

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1733
  EntryCode(masm);
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1752
  ExitCode(masm);
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
  __ 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);
1772
  __ cmpq(r9, r8);
1773 1774 1775
  __ j(not_equal, exit);

  __ incq(rax);
1776
  __ cmpq(r11, rcx);
1777 1778 1779 1780
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiNot(rcx, rcx);
1781
  __ cmpq(rcx, r8);
1782 1783 1784 1785 1786
  __ j(not_equal, exit);
}


TEST(SmiNot) {
1787
  v8::internal::V8::Initialize(NULL);
1788 1789 1790 1791 1792 1793 1794
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
1795 1796 1797
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1798 1799 1800

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1801
  EntryCode(masm);
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1815
  ExitCode(masm);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
  __ 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;
1834 1835 1836 1837 1838 1839
    CHECK(Smi::IsValid(result));
    __ Move(r8, Smi::FromInt(result));
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftLeftConstant(r9, rcx, shift);

    __ incq(rax);
1840
    __ cmpq(r9, r8);
1841 1842 1843 1844 1845 1846 1847
    __ j(not_equal, exit);

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

    __ incq(rax);
1848
    __ cmpq(rcx, r8);
1849 1850 1851 1852 1853 1854 1855 1856
    __ j(not_equal, exit);

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

    __ incq(rax);
1857
    __ cmpq(r9, r8);
1858 1859 1860 1861 1862 1863 1864 1865
    __ j(not_equal, exit);

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

    __ incq(rax);
1866
    __ cmpq(r9, r8);
1867 1868 1869 1870 1871 1872 1873 1874
    __ j(not_equal, exit);

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

    __ incq(rax);
1875
    __ cmpq(rdx, r8);
1876 1877 1878
    __ j(not_equal, exit);

    __ incq(rax);
1879 1880 1881 1882 1883
  }
}


TEST(SmiShiftLeft) {
1884
  v8::internal::V8::Initialize(NULL);
1885 1886 1887
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
1888
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 4,
1889 1890 1891
                                      &actual_size,
                                      true));
  CHECK(buffer);
1892 1893 1894
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
1895 1896 1897

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1898
  EntryCode(masm);
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
1911
  ExitCode(masm);
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
  __ 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)) {
1933
      __ Move(r8, Smi::FromInt(static_cast<int>(result)));
1934 1935 1936 1937
      __ Move(rcx, Smi::FromInt(x));
      __ SmiShiftLogicalRightConstant(r9, rcx, shift, exit);

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

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

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

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

      __ incq(rax);
1956
      __ cmpq(r9, r8);
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
      __ 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);
1970
      __ cmpq(rcx, r11);
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
      __ 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);
1981
      __ cmpq(rcx, r11);
1982 1983 1984 1985 1986 1987 1988 1989 1990
      __ j(not_equal, exit);

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


TEST(SmiShiftLogicalRight) {
1991
  v8::internal::V8::Initialize(NULL);
1992 1993 1994
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
1995
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 3,
1996 1997 1998
                                      &actual_size,
                                      true));
  CHECK(buffer);
1999 2000 2001
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2002 2003 2004

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2005
  EntryCode(masm);
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
2018
  ExitCode(masm);
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
  __ 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);

2044
    __ cmpq(rcx, r8);
2045 2046 2047 2048 2049 2050 2051
    __ j(not_equal, exit);

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

2052
    __ cmpq(rdx, r8);
2053 2054 2055 2056 2057 2058 2059 2060
    __ j(not_equal, exit);

    __ incq(rax);
  }
}


TEST(SmiShiftArithmeticRight) {
2061
  v8::internal::V8::Initialize(NULL);
2062 2063 2064 2065 2066 2067 2068
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
                                      &actual_size,
                                      true));
  CHECK(buffer);
2069 2070 2071
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2072 2073 2074

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2075
  EntryCode(masm);
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
2088
  ExitCode(masm);
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
  __ 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) {
  ASSERT(x >= 0);
  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);
2111
    __ cmpq(rdx, r8);
2112 2113
    __ j(not_equal, exit);
    __ incq(rax);
2114
    __ cmpq(r11, rcx);  // rcx unchanged.
2115 2116 2117
    __ j(not_equal, exit);
    __ incq(rax);
    __ PositiveSmiTimesPowerOfTwoToInteger64(rcx, rcx, power);
2118
    __ cmpq(rdx, r8);
2119 2120 2121 2122 2123 2124 2125
    __ j(not_equal, exit);
    __ incq(rax);
  }
}


TEST(PositiveSmiTimesPowerOfTwoToInteger64) {
2126
  v8::internal::V8::Initialize(NULL);
2127 2128 2129
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
2130
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 4,
2131 2132 2133
                                      &actual_size,
                                      true));
  CHECK(buffer);
2134 2135 2136
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2137 2138 2139

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2140
  EntryCode(masm);
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
  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);

  __ xor_(rax, rax);  // Success.
  __ bind(&exit);
2155
  ExitCode(masm);
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
  __ ret(0);

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


2166
TEST(OperandOffset) {
2167
  v8::internal::V8::Initialize(NULL);
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
  int data[256];
  for (int i = 0; i < 256; i++) { data[i] = i * 0x01010101; }

  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
                                      &actual_size,
                                      true));
  CHECK(buffer);
2178 2179 2180
  Isolate* isolate = Isolate::Current();
  HandleScope handles(isolate);
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
2181 2182 2183 2184 2185

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
  Label exit;

2186
  EntryCode(masm);
2187
  __ push(r13);
2188
  __ push(r14);
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
  __ push(rbx);
  __ push(rbp);
  __ push(Immediate(0x100));  // <-- rbp
  __ movq(rbp, rsp);
  __ push(Immediate(0x101));
  __ push(Immediate(0x102));
  __ push(Immediate(0x103));
  __ push(Immediate(0x104));
  __ push(Immediate(0x105));  // <-- rbx
  __ push(Immediate(0x106));
  __ push(Immediate(0x107));
  __ push(Immediate(0x108));
  __ push(Immediate(0x109));  // <-- rsp
  // rbp = rsp[9]
2203
  // r15 = rsp[3]
2204 2205
  // rbx = rsp[5]
  // r13 = rsp[7]
2206
  __ lea(r14, Operand(rsp, 3 * kPointerSize));
2207 2208 2209
  __ lea(r13, Operand(rbp, -3 * kPointerSize));
  __ lea(rbx, Operand(rbp, -5 * kPointerSize));
  __ movl(rcx, Immediate(2));
2210
  __ movq(r8, reinterpret_cast<uintptr_t>(&data[128]), RelocInfo::NONE64);
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 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
  __ 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);
  __ lea(rsp, Operand(rbp, kPointerSize));
  __ pop(rbp);
  __ pop(rbx);
2509
  __ pop(r14);
2510
  __ pop(r13);
2511
  ExitCode(masm);
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
  __ ret(0);


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



2524
#undef __