test-macro-assembler-x64.cc 68.9 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 160
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                   &actual_size,
                                                   true));
  CHECK(buffer);
  HandleScope handles;
161 162 163
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
164 165
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
  masm->set_allow_stub_calls(false);
166
  EntryCode(masm);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  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);
184
  ExitCode(masm);
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 229
  __ 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 {
230
    __ cmpq(rcx, rcx);
231 232 233 234 235 236 237 238 239 240 241
    __ 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) {
242
  v8::internal::V8::Initialize(NULL);
243 244 245
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
246
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 2,
247 248 249 250
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
251 252 253
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
254 255 256

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
257
  EntryCode(masm);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  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);
282
  ExitCode(masm);
283 284 285 286 287 288 289 290 291 292 293 294
  __ ret(0);

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



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

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
309
  EntryCode(masm);
310 311 312 313 314 315
  Label exit;

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


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

  __ incq(rax);
414
  __ cmpq(r11, rcx);
415 416 417 418
  __ j(not_equal, exit);

  __ incq(rax);
  __ Integer64PlusConstantToSmi(rcx, rcx, y);
419
  __ cmpq(rcx, r8);
420 421 422 423 424
  __ j(not_equal, exit);
}


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

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
439
  EntryCode(masm);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  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);
459
  ExitCode(masm);
460 461 462 463 464 465 466 467 468 469 470
  __ ret(0);

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


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

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
485
  EntryCode(masm);
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 536 537 538 539 540
  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);
541
  cond = masm->CheckNonNegativeSmi(rcx);
542 543 544 545
  __ j(NegateCondition(cond), &exit);

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

  __ incq(rax);
  __ movq(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
552
  cond = masm->CheckNonNegativeSmi(rcx);  // Negative smis are not positive.
553 554 555 556 557
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
558
  cond = masm->CheckNonNegativeSmi(rcx);  // Most negative smi is not positive.
559 560 561 562
  __ j(cond, &exit);

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
563
  cond = masm->CheckNonNegativeSmi(rcx);  // "Negative" non-smi.
564 565 566 567 568
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
569
  cond = masm->CheckNonNegativeSmi(rcx);  // Most positive smi is positive.
570 571 572 573
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
  __ xor_(rcx, Immediate(kSmiTagMask));
574
  cond = masm->CheckNonNegativeSmi(rcx);  // "Positive" non-smi.
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 656 657 658 659 660
  __ 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);
661
  ExitCode(masm);
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
  __ 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);
682
    __ cmpq(r11, rcx);
683 684 685 686 687 688
    __ j(not_equal, exit);

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

    __ incq(rax);
689
    __ cmpq(r11, rcx);
690 691 692 693 694 695 696 697 698 699 700
    __ 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);
701
    __ cmpq(r9, r8);
702 703 704
    __ j(not_equal, exit);

    __ incq(rax);
705
    __ cmpq(r11, rcx);
706 707 708 709 710 711 712
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiNeg(rcx, rcx, &smi_ok2);
    __ jmp(exit);
    __ bind(&smi_ok2);
    __ incq(rax);
713
    __ cmpq(rcx, r8);
714 715 716 717 718 719
    __ j(not_equal, exit);
  }
}


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

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
735
  EntryCode(masm);
736 737 738 739 740 741 742 743 744 745 746 747 748
  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);
749
  ExitCode(masm);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
  __ 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);
776
  __ cmpq(r9, r8);
777 778 779 780
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAdd(rcx, rcx, rdx, exit);                              \
781
  __ cmpq(rcx, r8);
782 783 784 785 786 787 788
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second));
789
  __ cmpq(r9, r8);
790 791 792
  __ j(not_equal, exit);

  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second));
793
  __ cmpq(rcx, r8);
794 795 796 797 798 799 800
  __ j(not_equal, exit);

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

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

  __ incq(rax);
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), exit);
806
  __ cmpq(rcx, r8);
807 808 809 810
  __ j(not_equal, exit);
}

TEST(SmiAdd) {
811
  v8::internal::V8::Initialize(NULL);
812 813 814 815 816 817 818
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
  HandleScope handles;
819 820 821
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
822 823 824

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
825
  EntryCode(masm);
826 827 828 829 830 831 832 833 834 835 836 837 838 839
  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);
840
  ExitCode(masm);
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
  __ 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);
862
  __ cmpq(r9, r8);
863 864 865 866
  __ j(not_equal, exit);

  __ incq(rax);  // Test 1.
  __ SmiSub(rcx, rcx, rdx, exit);
867
  __ cmpq(rcx, r8);
868 869 870 871 872 873
  __ j(not_equal, exit);

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

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

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

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

  __ incq(rax);  // Test 4.
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second), exit);
886
  __ cmpq(r9, r8);
887 888 889 890
  __ j(not_equal, exit);

  __ incq(rax);  // Test 5.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), exit);
891
  __ cmpq(rcx, r8);
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  __ 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);
914
    __ cmpq(rcx, r11);
915 916 917 918 919 920 921 922 923 924
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
925
    __ cmpq(rcx, r11);
926 927 928 929 930 931 932 933 934 935 936
    __ 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);
937
    __ cmpq(rcx, r11);
938 939 940 941 942 943 944 945 946 947
    __ 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);
948
    __ cmpq(rcx, r11);
949 950 951 952 953 954 955 956 957 958 959 960
    __ 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);
961
    __ cmpq(rcx, r11);
962 963 964 965 966 967 968 969 970 971
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
972
    __ cmpq(rcx, r11);
973 974 975 976 977 978 979 980 981 982 983
    __ 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);
984
    __ cmpq(rcx, r11);
985 986 987 988 989 990 991 992 993 994
    __ 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);
995
    __ cmpq(rcx, r11);
996 997 998 999 1000 1001
    __ j(not_equal, exit);
  }
}


TEST(SmiSub) {
1002
  v8::internal::V8::Initialize(NULL);
1003 1004 1005 1006 1007 1008 1009 1010
  // 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);
  HandleScope handles;
1011 1012 1013
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1014 1015 1016

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1017
  EntryCode(masm);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  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);
1040
  ExitCode(masm);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  __ 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);
1063
    __ cmpq(r11, rcx);
1064 1065
    __ j(not_equal, exit);
    __ incq(rax);
1066
    __ cmpq(r9, r8);
1067 1068 1069 1070
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiMul(rcx, rcx, rdx, exit);
1071
    __ cmpq(rcx, r8);
1072 1073 1074 1075 1076 1077 1078 1079
    __ 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);
1080
    __ cmpq(r11, rcx);
1081 1082 1083 1084 1085 1086 1087
    __ 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);
1088
    // __ cmpq(r11, rcx);
1089 1090 1091 1092 1093 1094
    // __ j(not_equal, exit);
  }
}


TEST(SmiMul) {
1095
  v8::internal::V8::Initialize(NULL);
1096 1097 1098 1099 1100 1101 1102
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                                 &actual_size,
                                                 true));
  CHECK(buffer);
  HandleScope handles;
1103 1104 1105
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1106 1107 1108

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1109
  EntryCode(masm);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  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);
1129
  ExitCode(masm);
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
  __ 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);
lrn@chromium.org's avatar
lrn@chromium.org committed
1143
#ifdef V8_TARGET_ARCH_X64
1144 1145 1146 1147 1148 1149
  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));
1150
  __ Move(r14, Smi::FromInt(y));
1151 1152 1153
  if (!fraction && !overflow && !negative_zero && !division_by_zero) {
    // Division succeeds
    __ movq(rcx, r11);
1154
    __ movq(r15, Immediate(id));
1155 1156
    int result = x / y;
    __ Move(r8, Smi::FromInt(result));
1157 1158
    __ SmiDiv(r9, rcx, r14, exit);
    // Might have destroyed rcx and r14.
1159
    __ incq(r15);
1160
    __ cmpq(r9, r8);
1161 1162
    __ j(not_equal, exit);

1163
    __ incq(r15);
1164
    __ movq(rcx, r11);
1165
    __ Move(r14, Smi::FromInt(y));
1166
    __ cmpq(rcx, r11);
1167 1168
    __ j(not_equal, exit);

1169
    __ incq(r15);
1170
    __ SmiDiv(rcx, rcx, r14, exit);
1171

1172
    __ incq(r15);
1173
    __ cmpq(rcx, r8);
1174 1175 1176
    __ j(not_equal, exit);
  } else {
    // Division fails.
1177
    __ movq(r15, Immediate(id + 8));
1178 1179 1180

    Label fail_ok, fail_ok2;
    __ movq(rcx, r11);
1181
    __ SmiDiv(r9, rcx, r14, &fail_ok);
1182 1183 1184
    __ jmp(exit);
    __ bind(&fail_ok);

1185
    __ incq(r15);
1186
    __ cmpq(rcx, r11);
1187 1188
    __ j(not_equal, exit);

1189
    __ incq(r15);
1190
    __ SmiDiv(rcx, rcx, r14, &fail_ok2);
1191 1192 1193
    __ jmp(exit);
    __ bind(&fail_ok2);

1194
    __ incq(r15);
1195
    __ cmpq(rcx, r11);
1196 1197 1198 1199 1200 1201
    __ j(not_equal, exit);
  }
}


TEST(SmiDiv) {
1202
  v8::internal::V8::Initialize(NULL);
1203 1204 1205 1206 1207 1208 1209 1210
  // 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);
  HandleScope handles;
1211 1212 1213
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1214 1215 1216

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1217
  EntryCode(masm);
1218 1219
  Label exit;

1220
  __ push(r14);
1221
  __ push(r15);
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  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);

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

1274
    __ incq(r15);
1275
    __ cmpq(r9, r8);
1276 1277
    __ j(not_equal, exit);

1278
    __ incq(r15);
1279
    __ cmpq(rcx, r11);
1280 1281
    __ j(not_equal, exit);

1282
    __ incq(r15);
1283
    __ SmiMod(rcx, rcx, r14, exit);
1284

1285
    __ incq(r15);
1286
    __ cmpq(rcx, r8);
1287 1288 1289
    __ j(not_equal, exit);
  } else {
    // Modulo fails.
1290
    __ movq(r15, Immediate(id + 8));
1291 1292

    Label fail_ok, fail_ok2;
1293
    __ SmiMod(r9, rcx, r14, &fail_ok);
1294 1295 1296
    __ jmp(exit);
    __ bind(&fail_ok);

1297
    __ incq(r15);
1298
    __ cmpq(rcx, r11);
1299 1300
    __ j(not_equal, exit);

1301
    __ incq(r15);
1302
    __ SmiMod(rcx, rcx, r14, &fail_ok2);
1303 1304 1305
    __ jmp(exit);
    __ bind(&fail_ok2);

1306
    __ incq(r15);
1307
    __ cmpq(rcx, r11);
1308 1309 1310 1311 1312 1313
    __ j(not_equal, exit);
  }
}


TEST(SmiMod) {
1314
  v8::internal::V8::Initialize(NULL);
1315 1316 1317 1318 1319 1320 1321 1322
  // 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);
  HandleScope handles;
1323 1324 1325
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1326 1327 1328

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1329
  EntryCode(masm);
1330 1331
  Label exit;

1332
  __ push(r14);
1333
  __ push(r15);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  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);

1355
  __ xor_(r15, r15);  // Success.
1356
  __ bind(&exit);
1357 1358
  __ movq(rax, r15);
  __ pop(r15);
1359 1360
  __ pop(r14);
  ExitCode(masm);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
  __ 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);
1380
    __ cmpq(index.reg, r8);
1381 1382 1383 1384 1385 1386 1387
    __ 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);
1388
    __ cmpq(rcx, r8);
1389 1390 1391 1392 1393 1394 1395 1396
    __ 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);
1397
    __ cmpq(index.reg, r8);
1398 1399 1400 1401 1402 1403 1404
    __ 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);
1405
    __ cmpq(rcx, r8);
1406 1407 1408 1409 1410 1411
    __ j(not_equal, exit);
    __ incq(rax);
  }
}

TEST(SmiIndex) {
1412
  v8::internal::V8::Initialize(NULL);
1413 1414 1415
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
1416
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 3,
1417 1418 1419 1420
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1421 1422 1423
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1424 1425 1426

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1427
  EntryCode(masm);
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  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);
1438
  ExitCode(masm);
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  __ 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);
1457
  __ cmpq(r9, rdx);
1458 1459 1460 1461 1462 1463 1464 1465 1466
  __ 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);
1467
  __ cmpq(r9, rcx);
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
  __ 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) {
1483
  v8::internal::V8::Initialize(NULL);
1484 1485 1486 1487 1488 1489 1490 1491
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1492 1493 1494
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1495 1496 1497

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);  // Avoid inline checks.
1498
  EntryCode(masm);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
  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);
1513
  ExitCode(masm);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
  __ 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);
1534
  __ cmpq(r8, r9);
1535 1536 1537
  __ j(not_equal, exit);

  __ incq(rax);
1538
  __ cmpq(r11, rcx);
1539 1540 1541 1542
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAnd(rcx, rcx, rdx);
1543
  __ cmpq(r8, rcx);
1544 1545 1546 1547 1548
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiAndConstant(r9, rcx, Smi::FromInt(y));
1549
  __ cmpq(r8, r9);
1550 1551 1552
  __ j(not_equal, exit);

  __ incq(rax);
1553
  __ cmpq(r11, rcx);
1554 1555 1556 1557
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAndConstant(rcx, rcx, Smi::FromInt(y));
1558
  __ cmpq(r8, rcx);
1559 1560 1561 1562 1563
  __ j(not_equal, exit);
}


TEST(SmiAnd) {
1564
  v8::internal::V8::Initialize(NULL);
1565 1566 1567 1568 1569 1570 1571 1572
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1573 1574 1575
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1576 1577 1578

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1579
  EntryCode(masm);
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
  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);
1596
  ExitCode(masm);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
  __ 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);
1617
  __ cmpq(r8, r9);
1618 1619 1620
  __ j(not_equal, exit);

  __ incq(rax);
1621
  __ cmpq(r11, rcx);
1622 1623 1624 1625
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOr(rcx, rcx, rdx);
1626
  __ cmpq(r8, rcx);
1627 1628 1629 1630 1631
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiOrConstant(r9, rcx, Smi::FromInt(y));
1632
  __ cmpq(r8, r9);
1633 1634 1635
  __ j(not_equal, exit);

  __ incq(rax);
1636
  __ cmpq(r11, rcx);
1637 1638 1639 1640
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiOrConstant(rcx, rcx, Smi::FromInt(y));
1641
  __ cmpq(r8, rcx);
1642 1643 1644 1645 1646
  __ j(not_equal, exit);
}


TEST(SmiOr) {
1647
  v8::internal::V8::Initialize(NULL);
1648 1649 1650 1651 1652 1653 1654 1655
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1656 1657 1658
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1659 1660 1661

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1662
  EntryCode(masm);
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
  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);
1681
  ExitCode(masm);
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
  __ 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);
1702
  __ cmpq(r8, r9);
1703 1704 1705
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiXor(rcx, rcx, rdx);
1711
  __ cmpq(r8, rcx);
1712 1713 1714 1715 1716
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiXorConstant(r9, rcx, Smi::FromInt(y));
1717
  __ cmpq(r8, r9);
1718 1719 1720
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiXorConstant(rcx, rcx, Smi::FromInt(y));
1726
  __ cmpq(r8, rcx);
1727 1728 1729 1730 1731
  __ j(not_equal, exit);
}


TEST(SmiXor) {
1732
  v8::internal::V8::Initialize(NULL);
1733 1734 1735 1736 1737 1738 1739 1740
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1741 1742 1743
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1744 1745 1746

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1747
  EntryCode(masm);
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
  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);
1766
  ExitCode(masm);
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  __ 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);
1786
  __ cmpq(r9, r8);
1787 1788 1789
  __ j(not_equal, exit);

  __ incq(rax);
1790
  __ cmpq(r11, rcx);
1791 1792 1793 1794
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiNot(rcx, rcx);
1795
  __ cmpq(rcx, r8);
1796 1797 1798 1799 1800
  __ j(not_equal, exit);
}


TEST(SmiNot) {
1801
  v8::internal::V8::Initialize(NULL);
1802 1803 1804 1805 1806 1807 1808 1809
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1810 1811 1812
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1813 1814 1815

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1816
  EntryCode(masm);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
  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);
1830
  ExitCode(masm);
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
  __ 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;
1849 1850 1851 1852 1853 1854
    CHECK(Smi::IsValid(result));
    __ Move(r8, Smi::FromInt(result));
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftLeftConstant(r9, rcx, shift);

    __ incq(rax);
1855
    __ cmpq(r9, r8);
1856 1857 1858 1859 1860 1861 1862
    __ j(not_equal, exit);

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

    __ incq(rax);
1863
    __ cmpq(rcx, r8);
1864 1865 1866 1867 1868 1869 1870 1871
    __ j(not_equal, exit);

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

    __ incq(rax);
1872
    __ cmpq(r9, r8);
1873 1874 1875 1876 1877 1878 1879 1880
    __ j(not_equal, exit);

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

    __ incq(rax);
1881
    __ cmpq(r9, r8);
1882 1883 1884 1885 1886 1887 1888 1889
    __ j(not_equal, exit);

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

    __ incq(rax);
1890
    __ cmpq(rdx, r8);
1891 1892 1893
    __ j(not_equal, exit);

    __ incq(rax);
1894 1895 1896 1897 1898
  }
}


TEST(SmiShiftLeft) {
1899
  v8::internal::V8::Initialize(NULL);
1900 1901 1902
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
1903
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 4,
1904 1905 1906 1907
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
1908 1909 1910
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
1911 1912 1913

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
1914
  EntryCode(masm);
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
  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);
1927
  ExitCode(masm);
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
  __ 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)) {
1949
      __ Move(r8, Smi::FromInt(static_cast<int>(result)));
1950 1951 1952 1953
      __ Move(rcx, Smi::FromInt(x));
      __ SmiShiftLogicalRightConstant(r9, rcx, shift, exit);

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

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

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

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

      __ incq(rax);
1972
      __ cmpq(r9, r8);
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
      __ 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);
1986
      __ cmpq(rcx, r11);
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
      __ 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);
1997
      __ cmpq(rcx, r11);
1998 1999 2000 2001 2002 2003 2004 2005 2006
      __ j(not_equal, exit);

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


TEST(SmiShiftLogicalRight) {
2007
  v8::internal::V8::Initialize(NULL);
2008 2009 2010
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
2011
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 3,
2012 2013 2014 2015
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
2016 2017 2018
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
2019 2020 2021

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2022
  EntryCode(masm);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
  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);
2035
  ExitCode(masm);
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
  __ 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);

2061
    __ cmpq(rcx, r8);
2062 2063 2064 2065 2066 2067 2068
    __ j(not_equal, exit);

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

2069
    __ cmpq(rdx, r8);
2070 2071 2072 2073 2074 2075 2076 2077
    __ j(not_equal, exit);

    __ incq(rax);
  }
}


TEST(SmiShiftArithmeticRight) {
2078
  v8::internal::V8::Initialize(NULL);
2079 2080 2081 2082 2083 2084 2085 2086
  // 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);
  HandleScope handles;
2087 2088 2089
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
2090 2091 2092

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2093
  EntryCode(masm);
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
  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);
2106
  ExitCode(masm);
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
  __ 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);
2129
    __ cmpq(rdx, r8);
2130 2131
    __ j(not_equal, exit);
    __ incq(rax);
2132
    __ cmpq(r11, rcx);  // rcx unchanged.
2133 2134 2135
    __ j(not_equal, exit);
    __ incq(rax);
    __ PositiveSmiTimesPowerOfTwoToInteger64(rcx, rcx, power);
2136
    __ cmpq(rdx, r8);
2137 2138 2139 2140 2141 2142 2143
    __ j(not_equal, exit);
    __ incq(rax);
  }
}


TEST(PositiveSmiTimesPowerOfTwoToInteger64) {
2144
  v8::internal::V8::Initialize(NULL);
2145 2146 2147
  // Allocate an executable page of memory.
  size_t actual_size;
  byte* buffer =
2148
      static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize * 4,
2149 2150 2151 2152
                                      &actual_size,
                                      true));
  CHECK(buffer);
  HandleScope handles;
2153 2154 2155
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
2156 2157 2158

  MacroAssembler* masm = &assembler;
  masm->set_allow_stub_calls(false);
2159
  EntryCode(masm);
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
  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);
2174
  ExitCode(masm);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
  __ ret(0);

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


2185
TEST(OperandOffset) {
2186
  v8::internal::V8::Initialize(NULL);
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
  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);
  HandleScope handles;
2198 2199 2200
  MacroAssembler assembler(Isolate::Current(),
                           buffer,
                           static_cast<int>(actual_size));
2201 2202 2203 2204 2205

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

2206
  EntryCode(masm);
2207
  __ push(r13);
2208
  __ push(r14);
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
  __ 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]
2223
  // r15 = rsp[3]
2224 2225
  // rbx = rsp[5]
  // r13 = rsp[7]
2226
  __ lea(r14, Operand(rsp, 3 * kPointerSize));
2227 2228 2229
  __ lea(r13, Operand(rbp, -3 * kPointerSize));
  __ lea(rbx, Operand(rbp, -5 * kPointerSize));
  __ movl(rcx, Immediate(2));
2230
  __ movq(r8, reinterpret_cast<uintptr_t>(&data[128]), RelocInfo::NONE64);
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 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
  __ 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);
2529
  __ pop(r14);
2530
  __ pop(r13);
2531
  ExitCode(masm);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
  __ ret(0);


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



2544
#undef __