js-create-lowering-unittest.cc 8.31 KB
Newer Older
1 2 3 4 5 6 7
// 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"
#include "src/code-factory.h"
#include "src/compiler/access-builder.h"
8
#include "src/compiler/compilation-dependencies.h"
9 10 11 12 13
#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"
14
#include "src/feedback-vector.h"
15
#include "src/isolate-inl.h"
16
#include "src/objects/arguments.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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()
33 34 35 36
      : TypedGraphTest(3),
        javascript_(zone()),
        deps_(isolate(), zone()),
        handle_scope_(isolate()) {}
37 38 39 40 41 42 43 44 45 46
  ~JSCreateLoweringTest() override {}

 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.
    GraphReducer graph_reducer(zone(), graph());
47 48
    JSCreateLowering reducer(&graph_reducer, &deps_, &jsgraph, js_heap_broker(),
                             native_context(), zone());
49 50 51 52
    return reducer.Reduce(node);
  }

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

  JSOperatorBuilder* javascript() { return &javascript_; }

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

72 73 74
// -----------------------------------------------------------------------------
// JSCreate

75 76
TEST_F(JSCreateLoweringTest, JSCreate) {
  Handle<JSFunction> function = isolate()->object_function();
77 78
  Node* const target = Parameter(
      Type::HeapConstant(js_heap_broker(), function, graph()->zone()));
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 89
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(
      r.replacement(),
      IsFinishRegion(
          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
  Handle<ScopeInfo> scope_info(factory()->NewScopeInfo(1));
176 177 178 179 180
  Node* const object = Parameter(Type::Receiver());
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
  Node* const control = graph()->start();
  Reduction r =
181
      Reduce(graph()->NewNode(javascript()->CreateWithContext(scope_info),
182
                              object, context, effect, control));
183 184 185 186 187 188 189 190 191 192 193 194
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor(
                                            Context::MIN_CONTEXT_SLOTS)),
                                        IsBeginRegion(_), control),
                             _));
}

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

TEST_F(JSCreateLoweringTest, JSCreateCatchContext) {
195
  Handle<ScopeInfo> scope_info(factory()->NewScopeInfo(1));
196 197 198 199
  Node* const exception = Parameter(Type::Receiver());
  Node* const context = Parameter(Type::Any());
  Node* const effect = graph()->start();
  Node* const control = graph()->start();
200 201 202
  Reduction r =
      Reduce(graph()->NewNode(javascript()->CreateCatchContext(scope_info),
                              exception, context, effect, control));
203 204 205 206 207 208 209 210 211 212 213
  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