js-create-lowering-unittest.cc 8.42 KB
Newer Older
1 2 3 4 5
// Copyright 2016 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.

#include "src/compiler/js-create-lowering.h"
6
#include "src/codegen/code-factory.h"
7
#include "src/codegen/tick-counter.h"
8
#include "src/compiler/access-builder.h"
9
#include "src/compiler/compilation-dependencies.h"
10 11 12 13 14
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/operator-properties.h"
15
#include "src/execution/isolate-inl.h"
16
#include "src/objects/arguments.h"
17
#include "src/objects/feedback-vector.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "test/unittests/compiler/compiler-test-utils.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"
#include "testing/gmock-support.h"

using testing::_;
using testing::BitEq;
using testing::IsNaN;

namespace v8 {
namespace internal {
namespace compiler {

class JSCreateLoweringTest : public TypedGraphTest {
 public:
  JSCreateLoweringTest()
34 35
      : TypedGraphTest(3),
        javascript_(zone()),
36 37
        deps_(broker(), zone()),
        handle_scope_(isolate()) {}
38
  ~JSCreateLoweringTest() override = default;
39 40 41 42 43 44 45 46

 protected:
  Reduction Reduce(Node* node) {
    MachineOperatorBuilder machine(zone());
    SimplifiedOperatorBuilder simplified(zone());
    JSGraph jsgraph(isolate(), graph(), common(), javascript(), &simplified,
                    &machine);
    // TODO(titzer): mock the GraphReducer here for better unit testing.
47
    GraphReducer graph_reducer(zone(), graph(), tick_counter());
48
    JSCreateLowering reducer(&graph_reducer, &deps_, &jsgraph, broker(),
49
                             zone());
50 51 52 53
    return reducer.Reduce(node);
  }

  Node* FrameState(Handle<SharedFunctionInfo> shared, Node* outer_frame_state) {
54 55
    Node* state_values =
        graph()->NewNode(common()->StateValues(0, SparseInputMask::Dense()));
56
    return graph()->NewNode(
57 58 59
        common()->FrameState(
            BailoutId::None(), OutputFrameStateCombine::Ignore(),
            common()->CreateFrameStateFunctionInfo(
60
                FrameStateType::kInterpretedFunction, 1, 0, shared)),
61 62 63 64 65 66 67 68 69
        state_values, state_values, state_values, NumberConstant(0),
        UndefinedConstant(), outer_frame_state);
  }

  JSOperatorBuilder* javascript() { return &javascript_; }

 private:
  JSOperatorBuilder javascript_;
  CompilationDependencies deps_;
70
  CanonicalHandleScope handle_scope_;
71 72
};

73 74 75
// -----------------------------------------------------------------------------
// JSCreate

76 77
TEST_F(JSCreateLoweringTest, JSCreate) {
  Handle<JSFunction> function = isolate()->object_function();
78
  Node* const target = graph()->NewNode(common()->HeapConstant(function));
79 80
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
81 82 83 84
  Node* const control = graph()->start();
  Reduction r =
      Reduce(graph()->NewNode(javascript()->Create(), target, target, context,
                              EmptyFrameState(), effect, control));
85 86 87 88
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(
      r.replacement(),
      IsFinishRegion(
89
          IsAllocate(IsNumberConstant(function->initial_map().instance_size()),
90
                     IsBeginRegion(effect), control),
91 92 93 94 95 96 97 98 99 100
          _));
}

// -----------------------------------------------------------------------------
// JSCreateArguments

TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedMapped) {
  Node* const closure = Parameter(Type::Any());
  Node* const context = UndefinedConstant();
  Node* const effect = graph()->start();
101
  Handle<SharedFunctionInfo> shared(isolate()->regexp_function()->shared(),
102
                                    isolate());
103 104 105 106
  Node* const frame_state_outer = FrameState(shared, graph()->start());
  Node* const frame_state_inner = FrameState(shared, frame_state_outer);
  Reduction r = Reduce(graph()->NewNode(
      javascript()->CreateArguments(CreateArgumentsType::kMappedArguments),
107
      closure, context, frame_state_inner, effect));
108
  ASSERT_TRUE(r.Changed());
109 110 111 112 113
  EXPECT_THAT(
      r.replacement(),
      IsFinishRegion(
          IsAllocate(IsNumberConstant(JSSloppyArgumentsObject::kSize), _, _),
          _));
114 115 116 117 118 119
}

TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedUnmapped) {
  Node* const closure = Parameter(Type::Any());
  Node* const context = UndefinedConstant();
  Node* const effect = graph()->start();
120
  Handle<SharedFunctionInfo> shared(isolate()->regexp_function()->shared(),
121
                                    isolate());
122 123 124 125
  Node* const frame_state_outer = FrameState(shared, graph()->start());
  Node* const frame_state_inner = FrameState(shared, frame_state_outer);
  Reduction r = Reduce(graph()->NewNode(
      javascript()->CreateArguments(CreateArgumentsType::kUnmappedArguments),
126
      closure, context, frame_state_inner, effect));
127
  ASSERT_TRUE(r.Changed());
128 129 130 131 132
  EXPECT_THAT(
      r.replacement(),
      IsFinishRegion(
          IsAllocate(IsNumberConstant(JSStrictArgumentsObject::kSize), _, _),
          _));
133 134 135 136 137 138
}

TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedRestArray) {
  Node* const closure = Parameter(Type::Any());
  Node* const context = UndefinedConstant();
  Node* const effect = graph()->start();
139
  Handle<SharedFunctionInfo> shared(isolate()->regexp_function()->shared(),
140
                                    isolate());
141 142 143 144
  Node* const frame_state_outer = FrameState(shared, graph()->start());
  Node* const frame_state_inner = FrameState(shared, frame_state_outer);
  Reduction r = Reduce(graph()->NewNode(
      javascript()->CreateArguments(CreateArgumentsType::kRestParameter),
145
      closure, context, frame_state_inner, effect));
146
  ASSERT_TRUE(r.Changed());
147 148 149
  EXPECT_THAT(
      r.replacement(),
      IsFinishRegion(IsAllocate(IsNumberConstant(JSArray::kSize), _, _), _));
150 151 152 153 154 155 156 157 158
}

// -----------------------------------------------------------------------------
// JSCreateFunctionContext

TEST_F(JSCreateLoweringTest, JSCreateFunctionContextViaInlinedAllocation) {
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
  Node* const control = graph()->start();
159
  Reduction const r = Reduce(graph()->NewNode(
160 161
      javascript()->CreateFunctionContext(
          handle(ScopeInfo::Empty(isolate()), isolate()), 8, FUNCTION_SCOPE),
162
      context, effect, control));
163 164 165 166 167 168 169 170 171 172 173 174
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor(
                                            8 + Context::MIN_CONTEXT_SLOTS)),
                                        IsBeginRegion(_), control),
                             _));
}

// -----------------------------------------------------------------------------
// JSCreateWithContext

TEST_F(JSCreateLoweringTest, JSCreateWithContext) {
175 176
  Handle<ScopeInfo> scope_info =
      ReadOnlyRoots(isolate()).empty_function_scope_info_handle();
177 178 179 180 181
  Node* const object = Parameter(Type::Receiver());
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
  Node* const control = graph()->start();
  Reduction r =
182
      Reduce(graph()->NewNode(javascript()->CreateWithContext(scope_info),
183
                              object, context, effect, control));
184 185 186 187 188 189 190 191 192 193 194 195
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor(
                                            Context::MIN_CONTEXT_SLOTS)),
                                        IsBeginRegion(_), control),
                             _));
}

// -----------------------------------------------------------------------------
// JSCreateCatchContext

TEST_F(JSCreateLoweringTest, JSCreateCatchContext) {
196 197
  Handle<ScopeInfo> scope_info =
      ReadOnlyRoots(isolate()).empty_function_scope_info_handle();
198 199 200 201
  Node* const exception = Parameter(Type::Receiver());
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
  Node* const control = graph()->start();
202 203 204
  Reduction r =
      Reduce(graph()->NewNode(javascript()->CreateCatchContext(scope_info),
                              exception, context, effect, control));
205 206 207 208 209 210 211 212 213 214 215
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor(
                                            Context::MIN_CONTEXT_SLOTS + 1)),
                                        IsBeginRegion(_), control),
                             _));
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8