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

#include <stdlib.h>

30
#include "src/v8.h"
31

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

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

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

97 98 99

static void EntryCode(MacroAssembler* masm) {
  // Smi constant register is callee save.
100
  __ pushq(i::kRootRegister);
101
  __ InitializeRootRegister();
102 103 104 105
}


static void ExitCode(MacroAssembler* masm) {
106
  __ popq(i::kRootRegister);
107 108 109
}


110 111
TEST(Smi) {
  // Check that C++ Smi operations work as expected.
112
  int64_t test_numbers[] = {
113
      0, 1, -1, 127, 128, -128, -129, 255, 256, -256, -257,
114 115
      Smi::kMaxValue, static_cast<int64_t>(Smi::kMaxValue) + 1,
      Smi::kMinValue, static_cast<int64_t>(Smi::kMinValue) - 1
116 117 118
  };
  int test_number_count = 15;
  for (int i = 0; i < test_number_count; i++) {
119
    int64_t number = test_numbers[i];
120 121 122 123 124 125 126 127 128
    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);
      }
129 130
      int64_t smi_value = smi_from_intptr->value();
      CHECK_EQ(number, smi_value);
131 132 133 134 135 136 137
    }
  }
}


static void TestMoveSmi(MacroAssembler* masm, Label* exit, int id, Smi* value) {
  __ movl(rax, Immediate(id));
138 139
  __ Move(rcx, value);
  __ Set(rdx, reinterpret_cast<intptr_t>(value));
140 141 142 143 144 145 146 147 148
  __ cmpq(rcx, rdx);
  __ j(not_equal, exit);
}


// Test that we can move a Smi value literally into a register.
TEST(SmiMove) {
  // Allocate an executable page of memory.
  size_t actual_size;
149 150
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
151
  CHECK(buffer);
152
  Isolate* isolate = CcTest::i_isolate();
153
  HandleScope handles(isolate);
154 155
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
156
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
157
  EntryCode(masm);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  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));

173
  __ xorq(rax, rax);  // Success.
174
  __ bind(&exit);
175
  ExitCode(masm);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  __ 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 {
199
    CHECK_EQ(x, y);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    __ 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 {
216
      CHECK(y > x);
217 218 219 220
      __ movl(rax, Immediate(id + 10));
      __ j(less_equal, exit);
    }
  } else {
221
    __ cmpq(rcx, rcx);
222 223 224 225 226 227 228 229 230 231 232 233 234
    __ movl(rax, Immediate(id + 11));
    __ j(not_equal, exit);
    __ incq(rax);
    __ cmpq(rcx, r8);
    __ j(not_equal, exit);
  }
}


// Test that we can compare smis for equality (and more).
TEST(SmiCompare) {
  // Allocate an executable page of memory.
  size_t actual_size;
235 236
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
237
  CHECK(buffer);
238
  Isolate* isolate = CcTest::i_isolate();
239
  HandleScope handles(isolate);
240 241
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
242 243

  MacroAssembler* masm = &assembler;
244
  EntryCode(masm);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  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);

267
  __ xorq(rax, rax);  // Success.
268
  __ bind(&exit);
269
  ExitCode(masm);
270 271 272 273 274 275 276 277 278 279 280 281 282 283
  __ ret(0);

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



TEST(Integer32ToSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
284 285
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
286
  CHECK(buffer);
287
  Isolate* isolate = CcTest::i_isolate();
288
  HandleScope handles(isolate);
289 290
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
291 292

  MacroAssembler* masm = &assembler;
293
  EntryCode(masm);
294 295 296 297 298 299
  Label exit;

  __ movq(rax, Immediate(1));  // Test number.
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
  __ Set(rdx, reinterpret_cast<intptr_t>(Smi::FromInt(0)));
300
  __ cmpq(rcx, rdx);
301 302 303 304 305 306
  __ 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)));
307
  __ cmpq(rcx, rdx);
308 309 310 311 312 313
  __ 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)));
314
  __ cmpq(rcx, rdx);
315 316 317 318 319 320
  __ 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)));
321
  __ cmpq(rcx, rdx);
322 323 324 325 326 327
  __ 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)));
328
  __ cmpq(rcx, rdx);
329 330 331 332 333 334 335 336
  __ 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)));
337
  __ cmpq(r8, rdx);
338 339 340 341 342 343
  __ 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)));
344
  __ cmpq(r8, rdx);
345 346 347 348 349 350
  __ 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)));
351
  __ cmpq(r8, rdx);
352 353 354 355 356 357
  __ 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)));
358
  __ cmpq(r8, rdx);
359 360 361 362 363 364
  __ 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)));
365
  __ cmpq(r8, rdx);
366 367 368
  __ j(not_equal, &exit);


369
  __ xorq(rax, rax);  // Success.
370
  __ bind(&exit);
371
  ExitCode(masm);
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  __ 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;
388
  CHECK(Smi::IsValid(result));
389
  __ movl(rax, Immediate(id));
390
  __ Move(r8, Smi::FromInt(static_cast<int>(result)));
391
  __ movq(rcx, x);
392 393
  __ movq(r11, rcx);
  __ Integer64PlusConstantToSmi(rdx, rcx, y);
394
  __ cmpq(rdx, r8);
395 396 397
  __ j(not_equal, exit);

  __ incq(rax);
398
  __ cmpq(r11, rcx);
399 400 401 402
  __ j(not_equal, exit);

  __ incq(rax);
  __ Integer64PlusConstantToSmi(rcx, rcx, y);
403
  __ cmpq(rcx, r8);
404 405 406 407 408 409 410
  __ j(not_equal, exit);
}


TEST(Integer64PlusConstantToSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
411 412
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
413
  CHECK(buffer);
414
  Isolate* isolate = CcTest::i_isolate();
415
  HandleScope handles(isolate);
416 417
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
418 419

  MacroAssembler* masm = &assembler;
420
  EntryCode(masm);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  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);

438
  __ xorq(rax, rax);  // Success.
439
  __ bind(&exit);
440
  ExitCode(masm);
441 442 443 444 445 446 447 448 449 450 451 452 453
  __ ret(0);

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


TEST(SmiCheck) {
  // Allocate an executable page of memory.
  size_t actual_size;
454 455
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
456
  CHECK(buffer);
457
  Isolate* isolate = CcTest::i_isolate();
458
  HandleScope handles(isolate);
459 460
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
461 462

  MacroAssembler* masm = &assembler;
463
  EntryCode(masm);
464 465 466 467 468 469 470 471 472 473 474 475 476
  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);
477
  __ xorq(rcx, Immediate(kSmiTagMask));
478 479 480 481 482 483 484 485 486 487
  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);
488
  __ xorq(rcx, Immediate(kSmiTagMask));
489 490 491 492 493 494 495 496 497 498
  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);
499
  __ xorq(rcx, Immediate(kSmiTagMask));
500 501 502 503 504 505 506 507 508 509
  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);
510
  __ xorq(rcx, Immediate(kSmiTagMask));
511 512 513 514 515 516 517 518
  cond = masm->CheckSmi(rcx);
  __ j(cond, &exit);

  // CheckPositiveSmi

  __ incq(rax);
  __ movl(rcx, Immediate(0));
  __ Integer32ToSmi(rcx, rcx);
519
  cond = masm->CheckNonNegativeSmi(rcx);
520 521 522
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
523
  __ xorq(rcx, Immediate(kSmiTagMask));
524
  cond = masm->CheckNonNegativeSmi(rcx);  // "zero" non-smi.
525 526 527 528 529
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(-1));
  __ Integer32ToSmi(rcx, rcx);
530
  cond = masm->CheckNonNegativeSmi(rcx);  // Negative smis are not positive.
531 532 533 534 535
  __ j(cond, &exit);

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMinValue));
  __ Integer32ToSmi(rcx, rcx);
536
  cond = masm->CheckNonNegativeSmi(rcx);  // Most negative smi is not positive.
537 538 539
  __ j(cond, &exit);

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

  __ incq(rax);
  __ movq(rcx, Immediate(Smi::kMaxValue));
  __ Integer32ToSmi(rcx, rcx);
547
  cond = masm->CheckNonNegativeSmi(rcx);  // Most positive smi is positive.
548 549 550
  __ j(NegateCondition(cond), &exit);

  __ incq(rax);
551
  __ xorq(rcx, Immediate(kSmiTagMask));
552
  cond = masm->CheckNonNegativeSmi(rcx);  // "Positive" non-smi.
553 554 555 556 557 558 559 560 561 562 563 564 565
  __ 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);
566
  __ xorq(rcx, Immediate(kSmiTagMask));
567 568 569 570
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

  __ incq(rax);
571
  __ xorq(rdx, Immediate(kSmiTagMask));
572 573 574 575
  cond = masm->CheckBothSmi(rcx, rdx);
  __ j(cond, &exit);

  __ incq(rax);
576
  __ xorq(rcx, Immediate(kSmiTagMask));
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
  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
610
  __ xorq(rax, rax);
611 612

  __ bind(&exit);
613
  ExitCode(masm);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  __ 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);
634
    __ cmpq(r11, rcx);
635 636 637 638 639 640
    __ j(not_equal, exit);

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

    __ incq(rax);
641
    __ cmpq(r11, rcx);
642 643 644 645 646 647 648 649 650 651 652
    __ 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);
653
    __ cmpq(r9, r8);
654 655 656
    __ j(not_equal, exit);

    __ incq(rax);
657
    __ cmpq(r11, rcx);
658 659 660 661 662 663 664
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiNeg(rcx, rcx, &smi_ok2);
    __ jmp(exit);
    __ bind(&smi_ok2);
    __ incq(rax);
665
    __ cmpq(rcx, r8);
666 667 668 669 670 671 672 673
    __ j(not_equal, exit);
  }
}


TEST(SmiNeg) {
  // Allocate an executable page of memory.
  size_t actual_size;
674 675
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
676
  CHECK(buffer);
677
  Isolate* isolate = CcTest::i_isolate();
678
  HandleScope handles(isolate);
679 680
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
681 682

  MacroAssembler* masm = &assembler;
683
  EntryCode(masm);
684 685 686 687 688 689 690 691 692 693 694
  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);

695
  __ xorq(rax, rax);  // Success.
696
  __ bind(&exit);
697
  ExitCode(masm);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  __ 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);
722
  __ cmpq(r9, r8);
723 724 725
  __ j(not_equal, exit);

  __ incq(rax);
726
  __ SmiAdd(rcx, rcx, rdx, exit);
727
  __ cmpq(rcx, r8);
728 729 730 731 732 733 734
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second));
735
  __ cmpq(r9, r8);
736 737 738
  __ j(not_equal, exit);

  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second));
739
  __ cmpq(rcx, r8);
740 741 742 743 744
  __ j(not_equal, exit);

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

745 746 747
  i::SmiOperationConstraints constraints =
      i::SmiOperationConstraint::kPreserveSourceRegister |
      i::SmiOperationConstraint::kBailoutOnOverflow;
748
  __ incq(rax);
749
  __ SmiAddConstant(r9, rcx, Smi::FromInt(second), constraints, exit);
750
  __ cmpq(r9, r8);
751 752 753
  __ j(not_equal, exit);

  __ incq(rax);
754
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), constraints, exit);
755 756 757 758 759 760
  __ cmpq(rcx, r8);
  __ j(not_equal, exit);

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

761 762
  constraints = i::SmiOperationConstraint::kPreserveSourceRegister |
                i::SmiOperationConstraint::kBailoutOnNoOverflow;
763 764
  Label done;
  __ incq(rax);
765
  __ SmiAddConstant(rcx, rcx, Smi::FromInt(second), constraints, &done);
766 767
  __ jmp(exit);
  __ bind(&done);
768
  __ cmpq(rcx, r8);
769 770 771
  __ j(not_equal, exit);
}

772

773 774 775 776 777
static void SmiAddOverflowTest(MacroAssembler* masm,
                               Label* exit,
                               int id,
                               int x) {
  // Adds a Smi to x so that the addition overflows.
778
  CHECK(x != 0);  // Can't overflow by adding a Smi.
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  int y_max = (x > 0) ? (Smi::kMaxValue + 0) : (Smi::kMinValue - x - 1);
  int y_min = (x > 0) ? (Smi::kMaxValue - x + 1) : (Smi::kMinValue + 0);

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

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

807 808 809
  i::SmiOperationConstraints constraints =
      i::SmiOperationConstraint::kPreserveSourceRegister |
      i::SmiOperationConstraint::kBailoutOnOverflow;
810 811 812 813
  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
814
    __ SmiAddConstant(r9, rcx, Smi::FromInt(y_min), constraints, &overflow_ok);
815 816 817 818 819 820 821 822 823 824
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
825
    __ SmiAddConstant(rcx, rcx, Smi::FromInt(y_min), constraints, &overflow_ok);
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

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

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

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

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
861
    __ SmiAddConstant(r9, rcx, Smi::FromInt(y_max), constraints, &overflow_ok);
862 863 864 865 866 867 868
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
    __ j(not_equal, exit);
  }

869
  constraints = i::SmiOperationConstraint::kBailoutOnOverflow;
870 871 872
  {
    Label overflow_ok;
    __ incq(rax);
873
    __ SmiAddConstant(rcx, rcx, Smi::FromInt(y_max), constraints, &overflow_ok);
874 875 876 877
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
    __ cmpq(rcx, r11);
878
    __ j(equal, exit);
879 880 881 882
  }
}


883 884 885
TEST(SmiAdd) {
  // Allocate an executable page of memory.
  size_t actual_size;
886 887
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 3, &actual_size, true));
888
  CHECK(buffer);
889
  Isolate* isolate = CcTest::i_isolate();
890
  HandleScope handles(isolate);
891 892
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
893 894

  MacroAssembler* masm = &assembler;
895
  EntryCode(masm);
896 897 898 899 900 901 902 903 904 905 906 907
  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);

908 909 910 911 912 913 914 915
  SmiAddOverflowTest(masm, &exit, 0x90, -1);
  SmiAddOverflowTest(masm, &exit, 0xA0, 1);
  SmiAddOverflowTest(masm, &exit, 0xB0, 1024);
  SmiAddOverflowTest(masm, &exit, 0xC0, Smi::kMaxValue);
  SmiAddOverflowTest(masm, &exit, 0xD0, -2);
  SmiAddOverflowTest(masm, &exit, 0xE0, -42000);
  SmiAddOverflowTest(masm, &exit, 0xF0, Smi::kMinValue);

916
  __ xorq(rax, rax);  // Success.
917
  __ bind(&exit);
918
  ExitCode(masm);
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
  __ 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);
940
  __ cmpq(r9, r8);
941 942 943 944
  __ j(not_equal, exit);

  __ incq(rax);  // Test 1.
  __ SmiSub(rcx, rcx, rdx, exit);
945
  __ cmpq(rcx, r8);
946 947 948 949 950 951
  __ j(not_equal, exit);

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

  __ incq(rax);  // Test 2.
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second));
952
  __ cmpq(r9, r8);
953 954 955 956
  __ j(not_equal, exit);

  __ incq(rax);  // Test 3.
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second));
957
  __ cmpq(rcx, r8);
958 959
  __ j(not_equal, exit);

960 961 962
  i::SmiOperationConstraints constraints =
      i::SmiOperationConstraint::kPreserveSourceRegister |
      i::SmiOperationConstraint::kBailoutOnOverflow;
963 964
  __ Move(rcx, Smi::FromInt(first));
  __ incq(rax);  // Test 4.
965
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), constraints, exit);
966
  __ cmpq(rcx, r8);
967 968
  __ j(not_equal, exit);

969
  __ Move(rcx, Smi::FromInt(first));
970
  __ incq(rax);  // Test 5.
971
  __ SmiSubConstant(r9, rcx, Smi::FromInt(second), constraints, exit);
972 973 974
  __ cmpq(r9, r8);
  __ j(not_equal, exit);

975 976
  constraints = i::SmiOperationConstraint::kPreserveSourceRegister |
                i::SmiOperationConstraint::kBailoutOnNoOverflow;
977 978 979
  __ Move(rcx, Smi::FromInt(first));
  Label done;
  __ incq(rax);  // Test 6.
980
  __ SmiSubConstant(rcx, rcx, Smi::FromInt(second), constraints, &done);
981 982
  __ jmp(exit);
  __ bind(&done);
983
  __ cmpq(rcx, r8);
984 985 986
  __ j(not_equal, exit);
}

987

988 989 990 991 992
static void SmiSubOverflowTest(MacroAssembler* masm,
                               Label* exit,
                               int id,
                               int x) {
  // Subtracts a Smi from x so that the subtraction overflows.
993
  CHECK(x != -1);  // Can't overflow by subtracting a Smi.
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
  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);
1007
    __ cmpq(rcx, r11);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1018
    __ cmpq(rcx, r11);
1019 1020 1021
    __ j(not_equal, exit);
  }

1022 1023 1024
  i::SmiOperationConstraints constraints =
      i::SmiOperationConstraint::kPreserveSourceRegister |
      i::SmiOperationConstraint::kBailoutOnOverflow;
1025

1026 1027 1028 1029
  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
1030
    __ SmiSubConstant(r9, rcx, Smi::FromInt(y_min), constraints, &overflow_ok);
1031 1032 1033
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1034
    __ cmpq(rcx, r11);
1035 1036 1037 1038 1039 1040
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
1041
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_min), constraints, &overflow_ok);
1042 1043 1044
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1045
    __ cmpq(rcx, r11);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    __ 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);
1058
    __ cmpq(rcx, r11);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    __ j(not_equal, exit);
  }

  {
    Label overflow_ok;
    __ incq(rax);
    __ SmiSub(rcx, rcx, rdx, &overflow_ok);
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1069
    __ cmpq(rcx, r11);
1070 1071 1072 1073 1074 1075 1076
    __ j(not_equal, exit);
  }

  __ movq(rcx, r11);
  {
    Label overflow_ok;
    __ incq(rax);
1077
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_max), constraints, &overflow_ok);
1078 1079 1080
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1081
    __ cmpq(rcx, r11);
1082 1083 1084
    __ j(not_equal, exit);
  }

1085
  constraints = i::SmiOperationConstraint::kBailoutOnOverflow;
1086
  __ movq(rcx, r11);
1087 1088 1089
  {
    Label overflow_ok;
    __ incq(rax);
1090
    __ SmiSubConstant(rcx, rcx, Smi::FromInt(y_max), constraints, &overflow_ok);
1091 1092 1093
    __ jmp(exit);
    __ bind(&overflow_ok);
    __ incq(rax);
1094
    __ cmpq(rcx, r11);
1095
    __ j(equal, exit);
1096 1097 1098 1099 1100 1101 1102
  }
}


TEST(SmiSub) {
  // Allocate an executable page of memory.
  size_t actual_size;
1103 1104
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 4, &actual_size, true));
1105
  CHECK(buffer);
1106
  Isolate* isolate = CcTest::i_isolate();
1107
  HandleScope handles(isolate);
1108 1109
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1110 1111

  MacroAssembler* masm = &assembler;
1112
  EntryCode(masm);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  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);

1133
  __ xorq(rax, rax);  // Success.
1134
  __ bind(&exit);
1135
  ExitCode(masm);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  __ 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);
1158
    __ cmpq(r11, rcx);
1159 1160
    __ j(not_equal, exit);
    __ incq(rax);
1161
    __ cmpq(r9, r8);
1162 1163 1164 1165
    __ j(not_equal, exit);

    __ incq(rax);
    __ SmiMul(rcx, rcx, rdx, exit);
1166
    __ cmpq(rcx, r8);
1167 1168 1169 1170 1171 1172 1173 1174
    __ 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);
1175
    __ cmpq(r11, rcx);
1176 1177 1178 1179 1180 1181 1182
    __ 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);
1183
    // __ cmpq(r11, rcx);
1184 1185 1186 1187 1188 1189 1190 1191
    // __ j(not_equal, exit);
  }
}


TEST(SmiMul) {
  // Allocate an executable page of memory.
  size_t actual_size;
1192 1193
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
1194
  CHECK(buffer);
1195
  Isolate* isolate = CcTest::i_isolate();
1196
  HandleScope handles(isolate);
1197 1198
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1199 1200

  MacroAssembler* masm = &assembler;
1201
  EntryCode(masm);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  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);

1219
  __ xorq(rax, rax);  // Success.
1220
  __ bind(&exit);
1221
  ExitCode(masm);
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
  __ 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);
1235
#if V8_TARGET_ARCH_X64
1236 1237 1238 1239 1240 1241
  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));
1242
  __ Move(r14, Smi::FromInt(y));
1243 1244 1245
  if (!fraction && !overflow && !negative_zero && !division_by_zero) {
    // Division succeeds
    __ movq(rcx, r11);
1246
    __ movq(r15, Immediate(id));
1247 1248
    int result = x / y;
    __ Move(r8, Smi::FromInt(result));
1249 1250
    __ SmiDiv(r9, rcx, r14, exit);
    // Might have destroyed rcx and r14.
1251
    __ incq(r15);
1252
    __ cmpq(r9, r8);
1253 1254
    __ j(not_equal, exit);

1255
    __ incq(r15);
1256
    __ movq(rcx, r11);
1257
    __ Move(r14, Smi::FromInt(y));
1258
    __ cmpq(rcx, r11);
1259 1260
    __ j(not_equal, exit);

1261
    __ incq(r15);
1262
    __ SmiDiv(rcx, rcx, r14, exit);
1263

1264
    __ incq(r15);
1265
    __ cmpq(rcx, r8);
1266 1267 1268
    __ j(not_equal, exit);
  } else {
    // Division fails.
1269
    __ movq(r15, Immediate(id + 8));
1270 1271 1272

    Label fail_ok, fail_ok2;
    __ movq(rcx, r11);
1273
    __ SmiDiv(r9, rcx, r14, &fail_ok);
1274 1275 1276
    __ jmp(exit);
    __ bind(&fail_ok);

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

1281
    __ incq(r15);
1282
    __ SmiDiv(rcx, rcx, r14, &fail_ok2);
1283 1284 1285
    __ jmp(exit);
    __ bind(&fail_ok2);

1286
    __ incq(r15);
1287
    __ cmpq(rcx, r11);
1288 1289 1290 1291 1292 1293 1294 1295
    __ j(not_equal, exit);
  }
}


TEST(SmiDiv) {
  // Allocate an executable page of memory.
  size_t actual_size;
1296 1297
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1298
  CHECK(buffer);
1299
  Isolate* isolate = CcTest::i_isolate();
1300
  HandleScope handles(isolate);
1301 1302
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1303 1304

  MacroAssembler* masm = &assembler;
1305
  EntryCode(masm);
1306 1307
  Label exit;

1308 1309
  __ pushq(r14);
  __ pushq(r15);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
  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);

1331
  __ xorq(r15, r15);  // Success.
1332
  __ bind(&exit);
1333
  __ movq(rax, r15);
1334 1335
  __ popq(r15);
  __ popq(r14);
1336
  ExitCode(masm);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
  __ 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);
1354
  __ Move(r14, Smi::FromInt(y));
1355 1356
  if (!division_overflow && !negative_zero && !division_by_zero) {
    // Modulo succeeds
1357
    __ movq(r15, Immediate(id));
1358 1359
    int result = x % y;
    __ Move(r8, Smi::FromInt(result));
1360
    __ SmiMod(r9, rcx, r14, exit);
1361

1362
    __ incq(r15);
1363
    __ cmpq(r9, r8);
1364 1365
    __ j(not_equal, exit);

1366
    __ incq(r15);
1367
    __ cmpq(rcx, r11);
1368 1369
    __ j(not_equal, exit);

1370
    __ incq(r15);
1371
    __ SmiMod(rcx, rcx, r14, exit);
1372

1373
    __ incq(r15);
1374
    __ cmpq(rcx, r8);
1375 1376 1377
    __ j(not_equal, exit);
  } else {
    // Modulo fails.
1378
    __ movq(r15, Immediate(id + 8));
1379 1380

    Label fail_ok, fail_ok2;
1381
    __ SmiMod(r9, rcx, r14, &fail_ok);
1382 1383 1384
    __ jmp(exit);
    __ bind(&fail_ok);

1385
    __ incq(r15);
1386
    __ cmpq(rcx, r11);
1387 1388
    __ j(not_equal, exit);

1389
    __ incq(r15);
1390
    __ SmiMod(rcx, rcx, r14, &fail_ok2);
1391 1392 1393
    __ jmp(exit);
    __ bind(&fail_ok2);

1394
    __ incq(r15);
1395
    __ cmpq(rcx, r11);
1396 1397 1398 1399 1400 1401 1402 1403
    __ j(not_equal, exit);
  }
}


TEST(SmiMod) {
  // Allocate an executable page of memory.
  size_t actual_size;
1404 1405
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1406
  CHECK(buffer);
1407
  Isolate* isolate = CcTest::i_isolate();
1408
  HandleScope handles(isolate);
1409 1410
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1411 1412

  MacroAssembler* masm = &assembler;
1413
  EntryCode(masm);
1414 1415
  Label exit;

1416 1417
  __ pushq(r14);
  __ pushq(r15);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
  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);

1439
  __ xorq(r15, r15);  // Success.
1440
  __ bind(&exit);
1441
  __ movq(rax, r15);
1442 1443
  __ popq(r15);
  __ popq(r14);
1444
  ExitCode(masm);
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
  __ 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);
1461
    CHECK(index.reg.is(rcx) || index.reg.is(rdx));
1462
    __ shlq(index.reg, Immediate(index.scale));
1463
    __ Set(r8, static_cast<intptr_t>(x) << i);
1464
    __ cmpq(index.reg, r8);
1465 1466 1467 1468
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToIndex(rcx, rcx, i);
1469
    CHECK(index.reg.is(rcx));
1470
    __ shlq(rcx, Immediate(index.scale));
1471
    __ Set(r8, static_cast<intptr_t>(x) << i);
1472
    __ cmpq(rcx, r8);
1473 1474 1475 1476 1477
    __ j(not_equal, exit);
    __ incq(rax);

    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rdx, rcx, i);
1478
    CHECK(index.reg.is(rcx) || index.reg.is(rdx));
1479
    __ shlq(index.reg, Immediate(index.scale));
1480
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1481
    __ cmpq(index.reg, r8);
1482 1483 1484 1485
    __ j(not_equal, exit);
    __ incq(rax);
    __ Move(rcx, Smi::FromInt(x));
    index = masm->SmiToNegativeIndex(rcx, rcx, i);
1486
    CHECK(index.reg.is(rcx));
1487
    __ shlq(rcx, Immediate(index.scale));
1488
    __ Set(r8, static_cast<intptr_t>(-x) << i);
1489
    __ cmpq(rcx, r8);
1490 1491 1492 1493 1494
    __ j(not_equal, exit);
    __ incq(rax);
  }
}

1495

1496 1497 1498
TEST(SmiIndex) {
  // Allocate an executable page of memory.
  size_t actual_size;
1499 1500
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 5, &actual_size, true));
1501
  CHECK(buffer);
1502
  Isolate* isolate = CcTest::i_isolate();
1503
  HandleScope handles(isolate);
1504 1505
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1506 1507

  MacroAssembler* masm = &assembler;
1508
  EntryCode(masm);
1509 1510 1511 1512 1513 1514 1515 1516
  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);

1517
  __ xorq(rax, rax);  // Success.
1518
  __ bind(&exit);
1519
  ExitCode(masm);
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 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));
1534
  __ xorq(rdx, Immediate(kSmiTagMask));
1535 1536 1537
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1538
  __ cmpq(r9, rdx);
1539 1540 1541 1542 1543
  __ j(not_equal, exit);

  __ incq(rax);
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
1544
  __ xorq(rcx, Immediate(kSmiTagMask));
1545 1546 1547
  __ SelectNonSmi(r9, rcx, rdx, exit);

  __ incq(rax);
1548
  __ cmpq(r9, rcx);
1549 1550 1551 1552 1553 1554
  __ j(not_equal, exit);

  __ incq(rax);
  Label fail_ok;
  __ Move(rcx, Smi::FromInt(x));
  __ Move(rdx, Smi::FromInt(y));
1555 1556
  __ xorq(rcx, Immediate(kSmiTagMask));
  __ xorq(rdx, Immediate(kSmiTagMask));
1557 1558 1559 1560 1561 1562 1563 1564 1565
  __ SelectNonSmi(r9, rcx, rdx, &fail_ok);
  __ jmp(exit);
  __ bind(&fail_ok);
}


TEST(SmiSelectNonSmi) {
  // Allocate an executable page of memory.
  size_t actual_size;
1566 1567
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1568
  CHECK(buffer);
1569
  Isolate* isolate = CcTest::i_isolate();
1570
  HandleScope handles(isolate);
1571 1572
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1573 1574

  MacroAssembler* masm = &assembler;
1575
  EntryCode(masm);
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
  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);

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

  __ incq(rax);
1615
  __ cmpq(r11, rcx);
1616 1617 1618 1619
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAnd(rcx, rcx, rdx);
1620
  __ cmpq(r8, rcx);
1621 1622 1623 1624 1625
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiAndConstant(r9, rcx, Smi::FromInt(y));
1626
  __ cmpq(r8, r9);
1627 1628 1629
  __ j(not_equal, exit);

  __ incq(rax);
1630
  __ cmpq(r11, rcx);
1631 1632 1633 1634
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiAndConstant(rcx, rcx, Smi::FromInt(y));
1635
  __ cmpq(r8, rcx);
1636 1637 1638 1639 1640 1641 1642
  __ j(not_equal, exit);
}


TEST(SmiAnd) {
  // Allocate an executable page of memory.
  size_t actual_size;
1643 1644
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1645
  CHECK(buffer);
1646
  Isolate* isolate = CcTest::i_isolate();
1647
  HandleScope handles(isolate);
1648 1649
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1650 1651

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

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

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

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

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

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

  __ incq(rax);
  __ SmiOrConstant(rcx, rcx, Smi::FromInt(y));
1714
  __ cmpq(r8, rcx);
1715 1716 1717 1718 1719 1720 1721
  __ j(not_equal, exit);
}


TEST(SmiOr) {
  // Allocate an executable page of memory.
  size_t actual_size;
1722 1723
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1724
  CHECK(buffer);
1725
  Isolate* isolate = CcTest::i_isolate();
1726
  HandleScope handles(isolate);
1727 1728
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1729 1730

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

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

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

  __ incq(rax);
  __ SmiXor(rcx, rcx, rdx);
1780
  __ cmpq(r8, rcx);
1781 1782 1783 1784 1785
  __ j(not_equal, exit);

  __ movq(rcx, r11);
  __ incq(rax);
  __ SmiXorConstant(r9, rcx, Smi::FromInt(y));
1786
  __ cmpq(r8, r9);
1787 1788 1789
  __ j(not_equal, exit);

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

  __ incq(rax);
  __ SmiXorConstant(rcx, rcx, Smi::FromInt(y));
1795
  __ cmpq(r8, rcx);
1796 1797 1798 1799 1800 1801 1802
  __ j(not_equal, exit);
}


TEST(SmiXor) {
  // Allocate an executable page of memory.
  size_t actual_size;
1803 1804
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
1805
  CHECK(buffer);
1806
  Isolate* isolate = CcTest::i_isolate();
1807
  HandleScope handles(isolate);
1808 1809
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1810 1811

  MacroAssembler* masm = &assembler;
1812
  EntryCode(masm);
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
  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);

1829
  __ xorq(rax, rax);  // Success.
1830
  __ bind(&exit);
1831
  ExitCode(masm);
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
  __ 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);
1851
  __ cmpq(r9, r8);
1852 1853 1854
  __ j(not_equal, exit);

  __ incq(rax);
1855
  __ cmpq(r11, rcx);
1856 1857 1858 1859
  __ j(not_equal, exit);

  __ incq(rax);
  __ SmiNot(rcx, rcx);
1860
  __ cmpq(rcx, r8);
1861 1862 1863 1864 1865 1866 1867
  __ j(not_equal, exit);
}


TEST(SmiNot) {
  // Allocate an executable page of memory.
  size_t actual_size;
1868 1869
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
1870
  CHECK(buffer);
1871
  Isolate* isolate = CcTest::i_isolate();
1872
  HandleScope handles(isolate);
1873 1874
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1875 1876

  MacroAssembler* masm = &assembler;
1877
  EntryCode(masm);
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
  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);

1889
  __ xorq(rax, rax);  // Success.
1890
  __ bind(&exit);
1891
  ExitCode(masm);
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
  __ 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;
1910 1911 1912 1913 1914 1915
    CHECK(Smi::IsValid(result));
    __ Move(r8, Smi::FromInt(result));
    __ Move(rcx, Smi::FromInt(x));
    __ SmiShiftLeftConstant(r9, rcx, shift);

    __ incq(rax);
1916
    __ cmpq(r9, r8);
1917 1918 1919 1920 1921 1922 1923
    __ j(not_equal, exit);

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

    __ incq(rax);
1924
    __ cmpq(rcx, r8);
1925 1926 1927 1928 1929 1930 1931 1932
    __ j(not_equal, exit);

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

    __ incq(rax);
1933
    __ cmpq(r9, r8);
1934 1935 1936 1937 1938 1939 1940 1941
    __ j(not_equal, exit);

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

    __ incq(rax);
1942
    __ cmpq(r9, r8);
1943 1944 1945 1946 1947 1948 1949 1950
    __ j(not_equal, exit);

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

    __ incq(rax);
1951
    __ cmpq(rdx, r8);
1952 1953 1954
    __ j(not_equal, exit);

    __ incq(rax);
1955 1956 1957 1958 1959 1960 1961
  }
}


TEST(SmiShiftLeft) {
  // Allocate an executable page of memory.
  size_t actual_size;
1962 1963
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 7, &actual_size, true));
1964
  CHECK(buffer);
1965
  Isolate* isolate = CcTest::i_isolate();
1966
  HandleScope handles(isolate);
1967 1968
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
1969 1970

  MacroAssembler* masm = &assembler;
1971
  EntryCode(masm);
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
  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);

1982
  __ xorq(rax, rax);  // Success.
1983
  __ bind(&exit);
1984
  ExitCode(masm);
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
  __ 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)) {
2006
      __ Move(r8, Smi::FromInt(static_cast<int>(result)));
2007 2008 2009 2010
      __ Move(rcx, Smi::FromInt(x));
      __ SmiShiftLogicalRightConstant(r9, rcx, shift, exit);

      __ incq(rax);
2011
      __ cmpq(r9, r8);
2012 2013 2014 2015 2016 2017 2018 2019
      __ j(not_equal, exit);

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

      __ incq(rax);
2020
      __ cmpq(r9, r8);
2021 2022 2023 2024 2025 2026 2027 2028
      __ j(not_equal, exit);

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

      __ incq(rax);
2029
      __ cmpq(r9, r8);
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
      __ 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);
2043
      __ cmpq(rcx, r11);
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
      __ 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);
2054
      __ cmpq(rcx, r11);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
      __ j(not_equal, exit);

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


TEST(SmiShiftLogicalRight) {
  // Allocate an executable page of memory.
  size_t actual_size;
2066 2067
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 5, &actual_size, true));
2068
  CHECK(buffer);
2069
  Isolate* isolate = CcTest::i_isolate();
2070
  HandleScope handles(isolate);
2071 2072
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
2073 2074

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

2086
  __ xorq(rax, rax);  // Success.
2087
  __ 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 2111 2112 2113
  __ 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);

2114
    __ cmpq(rcx, r8);
2115 2116 2117 2118 2119 2120 2121
    __ j(not_equal, exit);

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

2122
    __ cmpq(rdx, r8);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    __ j(not_equal, exit);

    __ incq(rax);
  }
}


TEST(SmiShiftArithmeticRight) {
  // Allocate an executable page of memory.
  size_t actual_size;
2133 2134
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 3, &actual_size, true));
2135
  CHECK(buffer);
2136
  Isolate* isolate = CcTest::i_isolate();
2137
  HandleScope handles(isolate);
2138 2139
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
2140 2141

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

2153
  __ xorq(rax, rax);  // Success.
2154
  __ bind(&exit);
2155
  ExitCode(masm);
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
  __ 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) {
2167
  CHECK(x >= 0);
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
  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);
2178
    __ cmpq(rdx, r8);
2179 2180
    __ j(not_equal, exit);
    __ incq(rax);
2181
    __ cmpq(r11, rcx);  // rcx unchanged.
2182 2183 2184
    __ j(not_equal, exit);
    __ incq(rax);
    __ PositiveSmiTimesPowerOfTwoToInteger64(rcx, rcx, power);
2185
    __ cmpq(rdx, r8);
2186 2187 2188 2189 2190 2191 2192 2193 2194
    __ j(not_equal, exit);
    __ incq(rax);
  }
}


TEST(PositiveSmiTimesPowerOfTwoToInteger64) {
  // Allocate an executable page of memory.
  size_t actual_size;
2195 2196
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 4, &actual_size, true));
2197
  CHECK(buffer);
2198
  Isolate* isolate = CcTest::i_isolate();
2199
  HandleScope handles(isolate);
2200 2201
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
2202 2203

  MacroAssembler* masm = &assembler;
2204
  EntryCode(masm);
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
  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);

2217
  __ xorq(rax, rax);  // Success.
2218
  __ bind(&exit);
2219
  ExitCode(masm);
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
  __ ret(0);

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


2230
TEST(OperandOffset) {
2231 2232
  uint32_t data[256];
  for (uint32_t i = 0; i < 256; i++) { data[i] = i * 0x01010101; }
2233 2234 2235

  // Allocate an executable page of memory.
  size_t actual_size;
2236 2237
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize * 2, &actual_size, true));
2238
  CHECK(buffer);
2239
  Isolate* isolate = CcTest::i_isolate();
2240
  HandleScope handles(isolate);
2241 2242
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
2243 2244 2245 2246

  MacroAssembler* masm = &assembler;
  Label exit;

2247
  EntryCode(masm);
2248 2249 2250 2251 2252
  __ pushq(r13);
  __ pushq(r14);
  __ pushq(rbx);
  __ pushq(rbp);
  __ pushq(Immediate(0x100));  // <-- rbp
2253
  __ movq(rbp, rsp);
2254 2255 2256 2257 2258 2259 2260 2261 2262
  __ pushq(Immediate(0x101));
  __ pushq(Immediate(0x102));
  __ pushq(Immediate(0x103));
  __ pushq(Immediate(0x104));
  __ pushq(Immediate(0x105));  // <-- rbx
  __ pushq(Immediate(0x106));
  __ pushq(Immediate(0x107));
  __ pushq(Immediate(0x108));
  __ pushq(Immediate(0x109));  // <-- rsp
2263
  // rbp = rsp[9]
2264
  // r15 = rsp[3]
2265 2266
  // rbx = rsp[5]
  // r13 = rsp[7]
2267 2268 2269
  __ leaq(r14, Operand(rsp, 3 * kPointerSize));
  __ leaq(r13, Operand(rbp, -3 * kPointerSize));
  __ leaq(rbx, Operand(rbp, -5 * kPointerSize));
2270
  __ movl(rcx, Immediate(2));
2271
  __ Move(r8, reinterpret_cast<Address>(&data[128]), RelocInfo::NONE64);
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 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
  __ 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);
2567
  __ leaq(rsp, Operand(rbp, kPointerSize));
2568 2569 2570 2571
  __ popq(rbp);
  __ popq(rbx);
  __ popq(r14);
  __ popq(r13);
2572
  ExitCode(masm);
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
  __ ret(0);


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


2584 2585 2586
TEST(LoadAndStoreWithRepresentation) {
  // Allocate an executable page of memory.
  size_t actual_size;
2587 2588
  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
      Assembler::kMinimalBufferSize, &actual_size, true));
2589 2590 2591
  CHECK(buffer);
  Isolate* isolate = CcTest::i_isolate();
  HandleScope handles(isolate);
2592 2593
  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
                           v8::internal::CodeObjectRequired::kYes);
2594 2595 2596 2597 2598 2599 2600 2601 2602
  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
  EntryCode(masm);
  __ subq(rsp, Immediate(1 * kPointerSize));
  Label exit;

  // Test 1.
  __ movq(rax, Immediate(1));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
2603
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::UInteger8());
2604 2605 2606 2607
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(255));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
2608
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::UInteger8());
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

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

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

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

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

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

2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
  // Test 7.
  __ movq(rax, Immediate(7));  // Test number.
  __ movq(Operand(rsp, 0 * kPointerSize), Immediate(0));
  __ movq(rcx, Immediate(-1));
  __ Store(Operand(rsp, 0 * kPointerSize), rcx, Representation::Integer8());
  __ movq(rcx, Operand(rsp, 0 * kPointerSize));
  __ movl(rdx, Immediate(255));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);
  __ Load(rdx, Operand(rsp, 0 * kPointerSize), Representation::Integer8());
  __ movq(rcx, Immediate(-1));
  __ cmpq(rcx, rdx);
  __ j(not_equal, &exit);

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

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

2718
  __ xorq(rax, rax);  // Success.
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
  __ bind(&exit);
  __ addq(rsp, Immediate(1 * kPointerSize));
  ExitCode(masm);
  __ ret(0);

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

2731

2732
#undef __