test-code-assembler.cc 15.4 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/codegen/code-factory.h"
6
#include "src/compiler/code-assembler.h"
7 8
#include "src/compiler/node-properties.h"
#include "src/compiler/opcodes.h"
9
#include "src/execution/isolate.h"
10
#include "src/objects/heap-number-inl.h"
11
#include "src/objects/js-function.h"
12
#include "src/objects/objects-inl.h"
13 14 15 16 17 18 19 20 21
#include "test/cctest/compiler/code-assembler-tester.h"
#include "test/cctest/compiler/function-tester.h"

namespace v8 {
namespace internal {
namespace compiler {

namespace {

22 23
template <class T>
using TVariable = TypedCodeAssemblerVariable<T>;
24

25
TNode<Smi> SmiTag(CodeAssembler* m, Node* value) {
26
  int32_t constant_value;
27
  if (m->ToInt32Constant(value, &constant_value) &&
28
      Smi::IsValid(constant_value)) {
29
    return m->SmiConstant(Smi::FromInt(constant_value));
30
  }
31 32
  return m->BitcastWordToTaggedSigned(
      m->WordShl(value, m->IntPtrConstant(kSmiShiftSize + kSmiTagSize)));
33 34
}

35 36
Node* UndefinedConstant(CodeAssembler* m) {
  return m->LoadRoot(RootIndex::kUndefinedValue);
37 38
}

39
Node* LoadObjectField(CodeAssembler* m, Node* object, int offset,
40
                      MachineType type = MachineType::AnyTagged()) {
41
  return m->Load(type, object, m->IntPtrConstant(offset - kHeapObjectTag));
42 43
}

44
Node* LoadMap(CodeAssembler* m, Node* object) {
45 46 47
  return LoadObjectField(m, object, JSObject::kMapOffset);
}

48 49 50 51
}  // namespace

TEST(SimpleSmiReturn) {
  Isolate* isolate(CcTest::InitIsolateOnce());
52 53
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
54
  m.Return(SmiTag(&m, m.Int32Constant(37)));
55 56
  FunctionTester ft(asm_tester.GenerateCode());
  CHECK_EQ(37, ft.CallChecked<Smi>()->value());
57 58 59 60
}

TEST(SimpleIntPtrReturn) {
  Isolate* isolate(CcTest::InitIsolateOnce());
61 62
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
63
  int test;
64 65
  m.Return(m.BitcastWordToTagged(
      m.IntPtrConstant(reinterpret_cast<intptr_t>(&test))));
66
  FunctionTester ft(asm_tester.GenerateCode());
67
  MaybeHandle<Object> result = ft.Call();
68
  CHECK_EQ(reinterpret_cast<Address>(&test), result.ToHandleChecked()->ptr());
69 70 71 72
}

TEST(SimpleDoubleReturn) {
  Isolate* isolate(CcTest::InitIsolateOnce());
73 74
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
75
  m.Return(m.NumberConstant(0.5));
76 77
  FunctionTester ft(asm_tester.GenerateCode());
  CHECK_EQ(0.5, ft.CallChecked<HeapNumber>()->value());
78 79 80 81
}

TEST(SimpleCallRuntime1Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
82 83
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
84 85
  TNode<Context> context =
      m.HeapConstant(Handle<Context>(isolate->native_context()));
86
  TNode<Smi> b = SmiTag(&m, m.Int32Constant(0));
87
  m.Return(m.CallRuntime(Runtime::kIsSmi, context, b));
88
  FunctionTester ft(asm_tester.GenerateCode());
89 90
  CHECK(ft.CallChecked<Oddball>().is_identical_to(
      isolate->factory()->true_value()));
91 92 93 94
}

TEST(SimpleTailCallRuntime1Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
95 96
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
97 98
  TNode<Context> context =
      m.HeapConstant(Handle<Context>(isolate->native_context()));
99
  TNode<Smi> b = SmiTag(&m, m.Int32Constant(0));
100
  m.TailCallRuntime(Runtime::kIsSmi, context, b);
101
  FunctionTester ft(asm_tester.GenerateCode());
102 103
  CHECK(ft.CallChecked<Oddball>().is_identical_to(
      isolate->factory()->true_value()));
104 105 106 107
}

TEST(SimpleCallRuntime2Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
108 109
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
110 111
  TNode<Context> context =
      m.HeapConstant(Handle<Context>(isolate->native_context()));
112 113
  TNode<Smi> a = SmiTag(&m, m.Int32Constant(2));
  TNode<Smi> b = SmiTag(&m, m.Int32Constant(4));
114
  m.Return(m.CallRuntime(Runtime::kAdd, context, a, b));
115 116
  FunctionTester ft(asm_tester.GenerateCode());
  CHECK_EQ(6, ft.CallChecked<Smi>()->value());
117 118 119 120
}

TEST(SimpleTailCallRuntime2Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
121 122
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
123 124
  TNode<Context> context =
      m.HeapConstant(Handle<Context>(isolate->native_context()));
125 126
  TNode<Smi> a = SmiTag(&m, m.Int32Constant(2));
  TNode<Smi> b = SmiTag(&m, m.Int32Constant(4));
127
  m.TailCallRuntime(Runtime::kAdd, context, a, b);
128 129
  FunctionTester ft(asm_tester.GenerateCode());
  CHECK_EQ(6, ft.CallChecked<Smi>()->value());
130 131
}

132 133
namespace {

134
Handle<JSFunction> CreateSumAllArgumentsFunction(FunctionTester* ft) {
135 136 137 138 139 140 141 142
  const char* source =
      "(function() {\n"
      "  var sum = 0 + this;\n"
      "  for (var i = 0; i < arguments.length; i++) {\n"
      "    sum += arguments[i];\n"
      "  }\n"
      "  return sum;\n"
      "})";
143
  return ft->NewFunction(source);
144 145 146 147 148 149 150
}

}  // namespace

TEST(SimpleCallJSFunction0Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
  const int kNumParams = 1;
151 152
  const int kContextOffset = kNumParams + 3;
  CodeAssemblerTester asm_tester(isolate, kNumParams + 1);  // Include receiver.
153
  CodeAssembler m(asm_tester.state());
154
  {
155 156
    auto function = m.Parameter<JSFunction>(1);
    auto context = m.Parameter<Context>(kContextOffset);
157

158
    auto receiver = SmiTag(&m, m.Int32Constant(42));
159 160

    Callable callable = CodeFactory::Call(isolate);
161
    TNode<Object> result = m.CallJS(callable, context, function, receiver);
162 163
    m.Return(result);
  }
164
  FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
165

166
  Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
167 168 169 170 171 172
  MaybeHandle<Object> result = ft.Call(sum);
  CHECK_EQ(Smi::FromInt(42), *result.ToHandleChecked());
}

TEST(SimpleCallJSFunction1Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
173 174 175
  const int kNumParams = 1;
  const int kContextOffset = kNumParams + 3;
  CodeAssemblerTester asm_tester(isolate, kNumParams + 1);  // Include receiver.
176
  CodeAssembler m(asm_tester.state());
177
  {
178 179
    auto function = m.Parameter<JSFunction>(1);
    auto context = m.Parameter<Context>(kContextOffset);
180

181 182
    Node* receiver = SmiTag(&m, m.Int32Constant(42));
    Node* a = SmiTag(&m, m.Int32Constant(13));
183 184

    Callable callable = CodeFactory::Call(isolate);
185
    TNode<Object> result = m.CallJS(callable, context, function, receiver, a);
186 187
    m.Return(result);
  }
188
  FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
189

190
  Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
191 192 193 194 195 196 197
  MaybeHandle<Object> result = ft.Call(sum);
  CHECK_EQ(Smi::FromInt(55), *result.ToHandleChecked());
}

TEST(SimpleCallJSFunction2Arg) {
  Isolate* isolate(CcTest::InitIsolateOnce());
  const int kNumParams = 2;
198 199
  const int kContextOffset = kNumParams + 3;
  CodeAssemblerTester asm_tester(isolate, kNumParams + 1);  // Include receiver.
200
  CodeAssembler m(asm_tester.state());
201
  {
202 203
    auto function = m.Parameter<JSFunction>(1);
    auto context = m.Parameter<Context>(kContextOffset);
204

205 206 207
    Node* receiver = SmiTag(&m, m.Int32Constant(42));
    Node* a = SmiTag(&m, m.Int32Constant(13));
    Node* b = SmiTag(&m, m.Int32Constant(153));
208 209

    Callable callable = CodeFactory::Call(isolate);
210 211
    TNode<Object> result =
        m.CallJS(callable, context, function, receiver, a, b);
212 213
    m.Return(result);
  }
214
  FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
215

216
  Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
217 218 219 220
  MaybeHandle<Object> result = ft.Call(sum);
  CHECK_EQ(Smi::FromInt(208), *result.ToHandleChecked());
}

221 222
TEST(VariableMerge1) {
  Isolate* isolate(CcTest::InitIsolateOnce());
223 224
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
225
  TVariable<Int32T> var1(&m);
226
  CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
227
  TNode<Int32T> temp = m.Int32Constant(0);
228
  var1 = temp;
229 230 231 232 233 234 235 236 237 238 239 240 241
  m.Branch(m.Int32Constant(1), &l1, &l2);
  m.Bind(&l1);
  CHECK_EQ(var1.value(), temp);
  m.Goto(&merge);
  m.Bind(&l2);
  CHECK_EQ(var1.value(), temp);
  m.Goto(&merge);
  m.Bind(&merge);
  CHECK_EQ(var1.value(), temp);
}

TEST(VariableMerge2) {
  Isolate* isolate(CcTest::InitIsolateOnce());
242 243
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
244
  TVariable<Int32T> var1(&m);
245
  CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
246
  TNode<Int32T> temp = m.Int32Constant(0);
247
  var1 = temp;
248 249 250 251 252
  m.Branch(m.Int32Constant(1), &l1, &l2);
  m.Bind(&l1);
  CHECK_EQ(var1.value(), temp);
  m.Goto(&merge);
  m.Bind(&l2);
253
  TNode<Int32T> temp2 = m.Int32Constant(2);
254
  var1 = temp2;
255 256 257 258 259 260 261 262
  CHECK_EQ(var1.value(), temp2);
  m.Goto(&merge);
  m.Bind(&merge);
  CHECK_NE(var1.value(), temp);
}

TEST(VariableMerge3) {
  Isolate* isolate(CcTest::InitIsolateOnce());
263 264
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
265 266
  TVariable<Int32T> var1(&m);
  TVariable<Int32T> var2(&m);
267
  CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
268
  TNode<Int32T> temp = m.Int32Constant(0);
269 270
  var1 = temp;
  var2 = temp;
271 272 273 274 275
  m.Branch(m.Int32Constant(1), &l1, &l2);
  m.Bind(&l1);
  CHECK_EQ(var1.value(), temp);
  m.Goto(&merge);
  m.Bind(&l2);
276
  TNode<Int32T> temp2 = m.Int32Constant(2);
277
  var1 = temp2;
278 279 280 281 282 283 284 285 286 287
  CHECK_EQ(var1.value(), temp2);
  m.Goto(&merge);
  m.Bind(&merge);
  CHECK_NE(var1.value(), temp);
  CHECK_NE(var1.value(), temp2);
  CHECK_EQ(var2.value(), temp);
}

TEST(VariableMergeBindFirst) {
  Isolate* isolate(CcTest::InitIsolateOnce());
288 289
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
290
  TVariable<Int32T> var1(&m);
291
  CodeAssemblerLabel l1(&m), l2(&m), merge(&m, &var1), end(&m);
292
  TNode<Int32T> temp = m.Int32Constant(0);
293
  var1 = temp;
294 295 296 297 298 299
  m.Branch(m.Int32Constant(1), &l1, &l2);
  m.Bind(&l1);
  CHECK_EQ(var1.value(), temp);
  m.Goto(&merge);
  m.Bind(&merge);
  CHECK(var1.value() != temp);
300
  CHECK_NOT_NULL(var1.value());
301 302
  m.Goto(&end);
  m.Bind(&l2);
303
  TNode<Int32T> temp2 = m.Int32Constant(2);
304
  var1 = temp2;
305 306 307 308
  CHECK_EQ(var1.value(), temp2);
  m.Goto(&merge);
  m.Bind(&end);
  CHECK(var1.value() != temp);
309
  CHECK_NOT_NULL(var1.value());
310 311 312 313
}

TEST(VariableMergeSwitch) {
  Isolate* isolate(CcTest::InitIsolateOnce());
314 315
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
316
  TVariable<Smi> var1(&m);
317 318
  CodeAssemblerLabel l1(&m), l2(&m), default_label(&m);
  CodeAssemblerLabel* labels[] = {&l1, &l2};
319
  int32_t values[] = {1, 2};
320
  TNode<Smi> temp1 = m.SmiConstant(0);
321
  var1 = temp1;
322 323
  m.Switch(m.Int32Constant(2), &default_label, values, labels, 2);
  m.Bind(&l1);
324 325
  CHECK_EQ(temp1, var1.value());
  m.Return(temp1);
326
  m.Bind(&l2);
327
  CHECK_EQ(temp1, var1.value());
328
  TNode<Smi> temp2 = m.SmiConstant(7);
329
  var1 = temp2;
330
  m.Goto(&default_label);
331
  m.Bind(&default_label);
332 333
  CHECK_EQ(IrOpcode::kPhi, (*var1.value()).opcode());
  CHECK_EQ(2, (*var1.value()).op()->ValueInputCount());
334 335 336
  CHECK_EQ(temp1, NodeProperties::GetValueInput(var1.value(), 0));
  CHECK_EQ(temp2, NodeProperties::GetValueInput(var1.value(), 1));
  m.Return(temp1);
337 338 339 340
}

TEST(SplitEdgeBranchMerge) {
  Isolate* isolate(CcTest::InitIsolateOnce());
341 342
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
343
  CodeAssemblerLabel l1(&m), merge(&m);
344 345 346 347
  m.Branch(m.Int32Constant(1), &l1, &merge);
  m.Bind(&l1);
  m.Goto(&merge);
  m.Bind(&merge);
348
  USE(asm_tester.GenerateCode());
349 350 351 352
}

TEST(SplitEdgeSwitchMerge) {
  Isolate* isolate(CcTest::InitIsolateOnce());
353 354
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
355 356
  CodeAssemblerLabel l1(&m), l2(&m), l3(&m), default_label(&m);
  CodeAssemblerLabel* labels[] = {&l1, &l2};
357 358 359 360 361 362 363 364 365
  int32_t values[] = {1, 2};
  m.Branch(m.Int32Constant(1), &l3, &l1);
  m.Bind(&l3);
  m.Switch(m.Int32Constant(2), &default_label, values, labels, 2);
  m.Bind(&l1);
  m.Goto(&l2);
  m.Bind(&l2);
  m.Goto(&default_label);
  m.Bind(&default_label);
366
  USE(asm_tester.GenerateCode());
367 368 369 370
}

TEST(TestToConstant) {
  Isolate* isolate(CcTest::InitIsolateOnce());
371 372
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
373 374 375
  int32_t value32;
  int64_t value64;
  Node* a = m.Int32Constant(5);
376 377
  CHECK(m.ToInt32Constant(a, &value32));
  CHECK(m.ToInt64Constant(a, &value64));
378 379

  a = m.Int64Constant(static_cast<int64_t>(1) << 32);
380 381
  CHECK(!m.ToInt32Constant(a, &value32));
  CHECK(m.ToInt64Constant(a, &value64));
382 383

  a = m.Int64Constant(13);
384 385
  CHECK(m.ToInt32Constant(a, &value32));
  CHECK(m.ToInt64Constant(a, &value64));
386

387
  a = UndefinedConstant(&m);
388 389
  CHECK(!m.ToInt32Constant(a, &value32));
  CHECK(!m.ToInt64Constant(a, &value64));
390

391
  a = UndefinedConstant(&m);
392 393
  CHECK(!m.ToInt32Constant(a, &value32));
  CHECK(!m.ToInt64Constant(a, &value64));
394 395 396 397
}

TEST(DeferredCodePhiHints) {
  Isolate* isolate(CcTest::InitIsolateOnce());
398 399
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
400
  CodeAssemblerLabel block1(&m, CodeAssemblerLabel::kDeferred);
401 402 403
  m.Goto(&block1);
  m.Bind(&block1);
  {
404
    TVariable<Map> var_object(&m);
405
    CodeAssemblerLabel loop(&m, &var_object);
406
    var_object = m.CAST(LoadMap(&m, m.SmiConstant(0)));
407 408 409
    m.Goto(&loop);
    m.Bind(&loop);
    {
410 411
      TNode<Map> map = m.CAST(LoadMap(&m, var_object.value()));
      var_object = map;
412 413 414
      m.Goto(&loop);
    }
  }
415
  CHECK(!asm_tester.GenerateCode().is_null());
416 417 418 419
}

TEST(TestOutOfScopeVariable) {
  Isolate* isolate(CcTest::InitIsolateOnce());
420 421
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
422 423 424 425
  CodeAssemblerLabel block1(&m);
  CodeAssemblerLabel block2(&m);
  CodeAssemblerLabel block3(&m);
  CodeAssemblerLabel block4(&m);
426
  m.Branch(m.WordEqual(m.UncheckedParameter<IntPtrT>(0), m.IntPtrConstant(0)),
427
           &block1, &block4);
428 429
  m.Bind(&block4);
  {
430
    TVariable<IntPtrT> var_object(&m);
431
    m.Branch(m.WordEqual(m.UncheckedParameter<IntPtrT>(0), m.IntPtrConstant(0)),
432
             &block2, &block3);
433 434

    m.Bind(&block2);
435
    var_object = m.IntPtrConstant(55);
436 437 438
    m.Goto(&block1);

    m.Bind(&block3);
439
    var_object = m.IntPtrConstant(66);
440 441 442
    m.Goto(&block1);
  }
  m.Bind(&block1);
443
  CHECK(!asm_tester.GenerateCode().is_null());
444 445
}

446 447 448 449 450 451
TEST(ExceptionHandler) {
  Isolate* isolate(CcTest::InitIsolateOnce());
  const int kNumParams = 0;
  CodeAssemblerTester asm_tester(isolate, kNumParams);
  CodeAssembler m(asm_tester.state());

452
  TVariable<Object> var(m.SmiConstant(0), &m);
453
  CodeAssemblerLabel exception(&m, {&var}, CodeAssemblerLabel::kDeferred);
454
  {
455
    ScopedExceptionHandler handler(&m, &exception, &var);
456 457
    TNode<Context> context =
        m.HeapConstant(Handle<Context>(isolate->native_context()));
458 459 460 461 462 463 464 465 466 467 468
    m.CallRuntime(Runtime::kThrow, context, m.SmiConstant(2));
  }
  m.Return(m.SmiConstant(1));

  m.Bind(&exception);
  m.Return(var.value());

  FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
  CHECK_EQ(2, ft.CallChecked<Smi>()->value());
}

469 470 471 472 473 474 475 476 477 478 479 480
TEST(TestCodeAssemblerCodeComment) {
  i::FLAG_code_comments = true;
  Isolate* isolate(CcTest::InitIsolateOnce());
  const int kNumParams = 0;
  CodeAssemblerTester asm_tester(isolate, kNumParams);
  CodeAssembler m(asm_tester.state());

  m.Comment("Comment1");
  m.Return(m.SmiConstant(1));

  Handle<Code> code = asm_tester.GenerateCode();
  CHECK_NE(code->code_comments(), kNullAddress);
481
  CodeCommentsIterator it(code->code_comments(), code->code_comments_size());
482 483 484 485 486 487 488 489 490
  CHECK(it.HasCurrent());
  bool found_comment = false;
  while (it.HasCurrent()) {
    if (strcmp(it.GetComment(), "Comment1") == 0) found_comment = true;
    it.Next();
  }
  CHECK(found_comment);
}

491 492 493 494 495 496 497 498
TEST(StaticAssert) {
  Isolate* isolate(CcTest::InitIsolateOnce());
  CodeAssemblerTester asm_tester(isolate);
  CodeAssembler m(asm_tester.state());
  m.StaticAssert(m.ReinterpretCast<BoolT>(m.Int32Constant(1)));
  USE(asm_tester.GenerateCode());
}

499 500 501
}  // namespace compiler
}  // namespace internal
}  // namespace v8