test-assembler-ia32.cc 14.7 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// 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 "disassembler.h"
#include "factory.h"
#include "macro-assembler.h"
#include "platform.h"
#include "serialize.h"
#include "cctest.h"

using namespace v8::internal;


typedef int (*F0)();
typedef int (*F1)(int x);
typedef int (*F2)(int x, int y);


#define __ assm.

TEST(AssemblerIa320) {
50 51
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
52
  HandleScope scope(isolate);
53 54

  v8::internal::byte buffer[256];
55
  Assembler assm(isolate, buffer, sizeof buffer);
56 57 58 59 60 61 62

  __ mov(eax, Operand(esp, 4));
  __ add(eax, Operand(esp, 8));
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
63
  Object* code = isolate->heap()->CreateCode(
64 65
      desc,
      Code::ComputeFlags(Code::STUB),
66
      Handle<Code>())->ToObjectChecked();
67
  CHECK(code->IsCode());
68
#ifdef OBJECT_PRINT
69 70 71 72 73 74 75 76 77 78
  Code::cast(code)->Print();
#endif
  F2 f = FUNCTION_CAST<F2>(Code::cast(code)->entry());
  int res = f(3, 4);
  ::printf("f() = %d\n", res);
  CHECK_EQ(7, res);
}


TEST(AssemblerIa321) {
79 80
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
81
  HandleScope scope(isolate);
82 83

  v8::internal::byte buffer[256];
84
  Assembler assm(isolate, buffer, sizeof buffer);
85 86 87
  Label L, C;

  __ mov(edx, Operand(esp, 4));
88
  __ xor_(eax, eax);  // clear eax
89 90 91
  __ jmp(&C);

  __ bind(&L);
92 93
  __ add(eax, edx);
  __ sub(edx, Immediate(1));
94 95

  __ bind(&C);
96
  __ test(edx, edx);
97
  __ j(not_zero, &L);
98 99 100 101
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
102
  Object* code = isolate->heap()->CreateCode(
103 104
      desc,
      Code::ComputeFlags(Code::STUB),
105
      Handle<Code>())->ToObjectChecked();
106
  CHECK(code->IsCode());
107
#ifdef OBJECT_PRINT
108 109 110 111 112 113 114 115 116 117
  Code::cast(code)->Print();
#endif
  F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry());
  int res = f(100);
  ::printf("f() = %d\n", res);
  CHECK_EQ(5050, res);
}


TEST(AssemblerIa322) {
118 119
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
120
  HandleScope scope(isolate);
121 122

  v8::internal::byte buffer[256];
123
  Assembler assm(isolate, buffer, sizeof buffer);
124 125 126 127 128 129 130
  Label L, C;

  __ mov(edx, Operand(esp, 4));
  __ mov(eax, 1);
  __ jmp(&C);

  __ bind(&L);
131 132
  __ imul(eax, edx);
  __ sub(edx, Immediate(1));
133 134

  __ bind(&C);
135
  __ test(edx, edx);
136
  __ j(not_zero, &L);
137 138 139
  __ ret(0);

  // some relocated stuff here, not executed
140
  __ mov(eax, isolate->factory()->true_value());
141
  __ jmp(NULL, RelocInfo::RUNTIME_ENTRY);
142 143 144

  CodeDesc desc;
  assm.GetCode(&desc);
145
  Object* code = isolate->heap()->CreateCode(
146 147
      desc,
      Code::ComputeFlags(Code::STUB),
148
      Handle<Code>())->ToObjectChecked();
149
  CHECK(code->IsCode());
150
#ifdef OBJECT_PRINT
151 152 153 154 155 156 157 158 159 160 161 162
  Code::cast(code)->Print();
#endif
  F1 f = FUNCTION_CAST<F1>(Code::cast(code)->entry());
  int res = f(10);
  ::printf("f() = %d\n", res);
  CHECK_EQ(3628800, res);
}


typedef int (*F3)(float x);

TEST(AssemblerIa323) {
163
  CcTest::InitializeVM();
164 165
  if (!CpuFeatures::IsSupported(SSE2)) return;

166
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
167
  HandleScope scope(isolate);
168 169

  v8::internal::byte buffer[256];
170
  Assembler assm(isolate, buffer, sizeof buffer);
171

172
  CHECK(CpuFeatures::IsSupported(SSE2));
173
  { CpuFeatureScope fscope(&assm, SSE2);
174 175 176 177 178 179
    __ cvttss2si(eax, Operand(esp, 4));
    __ ret(0);
  }

  CodeDesc desc;
  assm.GetCode(&desc);
180
  Code* code = Code::cast(isolate->heap()->CreateCode(
181 182
      desc,
      Code::ComputeFlags(Code::STUB),
183
      Handle<Code>())->ToObjectChecked());
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  // don't print the code - our disassembler can't handle cvttss2si
  // instead print bytes
  Disassembler::Dump(stdout,
                     code->instruction_start(),
                     code->instruction_start() + code->instruction_size());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  int res = f(static_cast<float>(-3.1415));
  ::printf("f() = %d\n", res);
  CHECK_EQ(-3, res);
}


typedef int (*F4)(double x);

TEST(AssemblerIa324) {
199
  CcTest::InitializeVM();
200 201
  if (!CpuFeatures::IsSupported(SSE2)) return;

202
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
203
  HandleScope scope(isolate);
204 205

  v8::internal::byte buffer[256];
206
  Assembler assm(isolate, buffer, sizeof buffer);
207

208
  CHECK(CpuFeatures::IsSupported(SSE2));
209
  CpuFeatureScope fscope(&assm, SSE2);
210 211 212 213 214
  __ cvttsd2si(eax, Operand(esp, 4));
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
215
  Code* code = Code::cast(isolate->heap()->CreateCode(
216 217
      desc,
      Code::ComputeFlags(Code::STUB),
218
      Handle<Code>())->ToObjectChecked());
219 220 221 222 223 224 225 226 227 228 229 230 231 232
  // don't print the code - our disassembler can't handle cvttsd2si
  // instead print bytes
  Disassembler::Dump(stdout,
                     code->instruction_start(),
                     code->instruction_start() + code->instruction_size());
  F4 f = FUNCTION_CAST<F4>(code->entry());
  int res = f(2.718281828);
  ::printf("f() = %d\n", res);
  CHECK_EQ(2, res);
}


static int baz = 42;
TEST(AssemblerIa325) {
233 234
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
235
  HandleScope scope(isolate);
236 237

  v8::internal::byte buffer[256];
238
  Assembler assm(isolate, buffer, sizeof buffer);
239

240
  __ mov(eax, Operand(reinterpret_cast<intptr_t>(&baz), RelocInfo::NONE32));
241 242 243 244
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
245
  Code* code = Code::cast(isolate->heap()->CreateCode(
246 247
      desc,
      Code::ComputeFlags(Code::STUB),
248
      Handle<Code>())->ToObjectChecked());
249 250 251 252 253 254 255 256 257
  F0 f = FUNCTION_CAST<F0>(code->entry());
  int res = f();
  CHECK_EQ(42, res);
}


typedef double (*F5)(double x, double y);

TEST(AssemblerIa326) {
258
  CcTest::InitializeVM();
259 260
  if (!CpuFeatures::IsSupported(SSE2)) return;

261
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
262
  HandleScope scope(isolate);
263
  v8::internal::byte buffer[256];
264
  Assembler assm(isolate, buffer, sizeof buffer);
265

266
  CpuFeatureScope fscope(&assm, SSE2);
267 268 269 270 271 272
  __ movdbl(xmm0, Operand(esp, 1 * kPointerSize));
  __ movdbl(xmm1, Operand(esp, 3 * kPointerSize));
  __ addsd(xmm0, xmm1);
  __ mulsd(xmm0, xmm1);
  __ subsd(xmm0, xmm1);
  __ divsd(xmm0, xmm1);
273
  // Copy xmm0 to st(0) using eight bytes of stack.
274
  __ sub(esp, Immediate(8));
275 276
  __ movdbl(Operand(esp, 0), xmm0);
  __ fld_d(Operand(esp, 0));
277
  __ add(esp, Immediate(8));
278 279 280 281
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
282
  Code* code = Code::cast(isolate->heap()->CreateCode(
283 284
      desc,
      Code::ComputeFlags(Code::STUB),
285
      Handle<Code>())->ToObjectChecked());
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
#ifdef DEBUG
  ::printf("\n---\n");
  // don't print the code - our disassembler can't handle SSE instructions
  // instead print bytes
  Disassembler::Dump(stdout,
                     code->instruction_start(),
                     code->instruction_start() + code->instruction_size());
#endif
  F5 f = FUNCTION_CAST<F5>(code->entry());
  double res = f(2.2, 1.1);
  ::printf("f() = %f\n", res);
  CHECK(2.29 < res && res < 2.31);
}


typedef double (*F6)(int x);

TEST(AssemblerIa328) {
304
  CcTest::InitializeVM();
305 306
  if (!CpuFeatures::IsSupported(SSE2)) return;

307
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
308
  HandleScope scope(isolate);
309
  v8::internal::byte buffer[256];
310
  Assembler assm(isolate, buffer, sizeof buffer);
311
  CpuFeatureScope fscope(&assm, SSE2);
312
  __ mov(eax, Operand(esp, 4));
313
  __ cvtsi2sd(xmm0, eax);
314
  // Copy xmm0 to st(0) using eight bytes of stack.
315
  __ sub(esp, Immediate(8));
316 317
  __ movdbl(Operand(esp, 0), xmm0);
  __ fld_d(Operand(esp, 0));
318
  __ add(esp, Immediate(8));
319 320 321
  __ ret(0);
  CodeDesc desc;
  assm.GetCode(&desc);
322
  Code* code = Code::cast(isolate->heap()->CreateCode(
323 324
      desc,
      Code::ComputeFlags(Code::STUB),
325
      Handle<Code>())->ToObjectChecked());
326
  CHECK(code->IsCode());
327
#ifdef OBJECT_PRINT
328 329 330 331 332 333 334 335 336 337 338 339 340
  Code::cast(code)->Print();
#endif
  F6 f = FUNCTION_CAST<F6>(Code::cast(code)->entry());
  double res = f(12);

  ::printf("f() = %f\n", res);
  CHECK(11.99 < res && res < 12.001);
}


typedef int (*F7)(double x, double y);

TEST(AssemblerIa329) {
341 342
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
343
  HandleScope scope(isolate);
344
  v8::internal::byte buffer[256];
345
  MacroAssembler assm(isolate, buffer, sizeof buffer);
346 347 348 349 350
  enum { kEqual = 0, kGreater = 1, kLess = 2, kNaN = 3, kUndefined = 4 };
  Label equal_l, less_l, greater_l, nan_l;
  __ fld_d(Operand(esp, 3 * kPointerSize));
  __ fld_d(Operand(esp, 1 * kPointerSize));
  __ FCmp();
351 352 353 354
  __ j(parity_even, &nan_l);
  __ j(equal, &equal_l);
  __ j(below, &less_l);
  __ j(above, &greater_l);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

  __ mov(eax, kUndefined);
  __ ret(0);

  __ bind(&equal_l);
  __ mov(eax, kEqual);
  __ ret(0);

  __ bind(&greater_l);
  __ mov(eax, kGreater);
  __ ret(0);

  __ bind(&less_l);
  __ mov(eax, kLess);
  __ ret(0);

  __ bind(&nan_l);
  __ mov(eax, kNaN);
  __ ret(0);


  CodeDesc desc;
  assm.GetCode(&desc);
378
  Code* code = Code::cast(isolate->heap()->CreateCode(
379 380
      desc,
      Code::ComputeFlags(Code::STUB),
381
      Handle<Code>())->ToObjectChecked());
382
  CHECK(code->IsCode());
383
#ifdef OBJECT_PRINT
384 385 386 387 388 389 390 391 392 393
  Code::cast(code)->Print();
#endif

  F7 f = FUNCTION_CAST<F7>(Code::cast(code)->entry());
  CHECK_EQ(kLess, f(1.1, 2.2));
  CHECK_EQ(kEqual, f(2.2, 2.2));
  CHECK_EQ(kGreater, f(3.3, 2.2));
  CHECK_EQ(kNaN, f(OS::nan_value(), 1.1));
}

394 395 396

TEST(AssemblerIa3210) {
  // Test chaining of label usages within instructions (issue 1644).
397 398
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
399
  HandleScope scope(isolate);
400
  Assembler assm(isolate, NULL, 0);
401 402 403 404 405 406 407 408

  Label target;
  __ j(equal, &target);
  __ j(not_equal, &target);
  __ bind(&target);
  __ nop();
}

409 410

TEST(AssemblerMultiByteNop) {
411 412
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
413
  HandleScope scope(isolate);
414
  v8::internal::byte buffer[1024];
415
  Assembler assm(isolate, buffer, sizeof(buffer));
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  __ push(ebx);
  __ push(ecx);
  __ push(edx);
  __ push(edi);
  __ push(esi);
  __ mov(eax, 1);
  __ mov(ebx, 2);
  __ mov(ecx, 3);
  __ mov(edx, 4);
  __ mov(edi, 5);
  __ mov(esi, 6);
  for (int i = 0; i < 16; i++) {
    int before = assm.pc_offset();
    __ Nop(i);
    CHECK_EQ(assm.pc_offset() - before, i);
  }

  Label fail;
  __ cmp(eax, 1);
  __ j(not_equal, &fail);
  __ cmp(ebx, 2);
  __ j(not_equal, &fail);
  __ cmp(ecx, 3);
  __ j(not_equal, &fail);
  __ cmp(edx, 4);
  __ j(not_equal, &fail);
  __ cmp(edi, 5);
  __ j(not_equal, &fail);
  __ cmp(esi, 6);
  __ j(not_equal, &fail);
  __ mov(eax, 42);
  __ pop(esi);
  __ pop(edi);
  __ pop(edx);
  __ pop(ecx);
  __ pop(ebx);
  __ ret(0);
  __ bind(&fail);
  __ mov(eax, 13);
  __ pop(esi);
  __ pop(edi);
  __ pop(edx);
  __ pop(ecx);
  __ pop(ebx);
  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);
464
  Code* code = Code::cast(isolate->heap()->CreateCode(
465 466
      desc,
      Code::ComputeFlags(Code::STUB),
467
      Handle<Code>())->ToObjectChecked());
468 469 470 471 472 473 474 475
  CHECK(code->IsCode());

  F0 f = FUNCTION_CAST<F0>(code->entry());
  int res = f();
  CHECK_EQ(42, res);
}


476
#ifdef __GNUC__
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
#define ELEMENT_COUNT 4

void DoSSE2(const v8::FunctionCallbackInfo<v8::Value>& args) {
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);

  CHECK(args[0]->IsArray());
  v8::Local<v8::Array> vec = v8::Local<v8::Array>::Cast(args[0]);
  CHECK_EQ(ELEMENT_COUNT, vec->Length());

  v8::internal::byte buffer[256];
  Assembler assm(isolate, buffer, sizeof buffer);

  ASSERT(CpuFeatures::IsSupported(SSE2));
  CpuFeatureScope fscope(&assm, SSE2);

  // Remove return address from the stack for fix stack frame alignment.
  __ pop(ecx);

  // Store input vector on the stack.
  for (int i = 0; i < ELEMENT_COUNT; ++i) {
    __ push(Immediate(vec->Get(i)->Int32Value()));
  }

  // Read vector into a xmm register.
  __ pxor(xmm0, xmm0);
  __ movdqa(xmm0, Operand(esp, 0));
  // Create mask and store it in the return register.
  __ movmskps(eax, xmm0);

  // Remove unused data from the stack.
  __ add(esp, Immediate(ELEMENT_COUNT * sizeof(int32_t)));
  // Restore return address.
  __ push(ecx);

  __ ret(0);

  CodeDesc desc;
  assm.GetCode(&desc);

  Object* code = isolate->heap()->CreateCode(
      desc,
      Code::ComputeFlags(Code::STUB),
      Handle<Code>())->ToObjectChecked();
  CHECK(code->IsCode());

  F0 f = FUNCTION_CAST<F0>(Code::cast(code)->entry());
  int res = f();
  args.GetReturnValue().Set(v8::Integer::New(res));
}

528

529 530 531 532 533 534
TEST(StackAlignmentForSSE2) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(SSE2)) return;

  CHECK_EQ(0, OS::ActivationFrameAlignment() % 16);

535
  v8::Isolate* isolate = CcTest::isolate();
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  v8::HandleScope handle_scope(isolate);
  v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
  global_template->Set(v8_str("do_sse2"), v8::FunctionTemplate::New(DoSSE2));

  LocalContext env(NULL, global_template);
  CompileRun(
      "function foo(vec) {"
      "  return do_sse2(vec);"
      "}");

  v8::Local<v8::Object> global_object = env->Global();
  v8::Local<v8::Function> foo =
      v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));

  int32_t vec[ELEMENT_COUNT] = { -1, 1, 1, 1 };
  v8::Local<v8::Array> v8_vec = v8::Array::New(ELEMENT_COUNT);
  for (int i = 0; i < ELEMENT_COUNT; i++) {
      v8_vec->Set(i, v8_num(vec[i]));
  }

  v8::Local<v8::Value> args[] = { v8_vec };
  v8::Local<v8::Value> result = foo->Call(global_object, 1, args);

  // The mask should be 0b1000.
  CHECK_EQ(8, result->Int32Value());
}

#undef ELEMENT_COUNT
564
#endif  // __GNUC__
565 566


567
#undef __