OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/compiler/js-create-lowering.h" |
| 6 #include "src/code-factory.h" |
| 7 #include "src/compiler/access-builder.h" |
| 8 #include "src/compiler/js-graph.h" |
| 9 #include "src/compiler/js-operator.h" |
| 10 #include "src/compiler/machine-operator.h" |
| 11 #include "src/compiler/node-properties.h" |
| 12 #include "src/compiler/operator-properties.h" |
| 13 #include "src/isolate-inl.h" |
| 14 #include "test/unittests/compiler/compiler-test-utils.h" |
| 15 #include "test/unittests/compiler/graph-unittest.h" |
| 16 #include "test/unittests/compiler/node-test-utils.h" |
| 17 #include "testing/gmock-support.h" |
| 18 |
| 19 using testing::_; |
| 20 using testing::BitEq; |
| 21 using testing::IsNaN; |
| 22 |
| 23 namespace v8 { |
| 24 namespace internal { |
| 25 namespace compiler { |
| 26 |
| 27 class JSCreateLoweringTest : public TypedGraphTest { |
| 28 public: |
| 29 JSCreateLoweringTest() |
| 30 : TypedGraphTest(3), javascript_(zone()), deps_(isolate(), zone()) {} |
| 31 ~JSCreateLoweringTest() override {} |
| 32 |
| 33 protected: |
| 34 Reduction Reduce(Node* node) { |
| 35 MachineOperatorBuilder machine(zone()); |
| 36 SimplifiedOperatorBuilder simplified(zone()); |
| 37 JSGraph jsgraph(isolate(), graph(), common(), javascript(), &simplified, |
| 38 &machine); |
| 39 // TODO(titzer): mock the GraphReducer here for better unit testing. |
| 40 GraphReducer graph_reducer(zone(), graph()); |
| 41 JSCreateLowering reducer(&graph_reducer, &deps_, &jsgraph); |
| 42 return reducer.Reduce(node); |
| 43 } |
| 44 |
| 45 Node* FrameState(Handle<SharedFunctionInfo> shared, Node* outer_frame_state) { |
| 46 Node* state_values = graph()->NewNode(common()->StateValues(0)); |
| 47 return graph()->NewNode( |
| 48 common()->FrameState(BailoutId::None(), |
| 49 OutputFrameStateCombine::Ignore(), |
| 50 common()->CreateFrameStateFunctionInfo( |
| 51 FrameStateType::kJavaScriptFunction, 1, 0, |
| 52 shared, CALL_MAINTAINS_NATIVE_CONTEXT)), |
| 53 state_values, state_values, state_values, NumberConstant(0), |
| 54 UndefinedConstant(), outer_frame_state); |
| 55 } |
| 56 |
| 57 JSOperatorBuilder* javascript() { return &javascript_; } |
| 58 |
| 59 private: |
| 60 JSOperatorBuilder javascript_; |
| 61 CompilationDependencies deps_; |
| 62 }; |
| 63 |
| 64 TEST_F(JSCreateLoweringTest, JSCreate) { |
| 65 Handle<JSFunction> function = isolate()->object_function(); |
| 66 Node* const target = Parameter(Type::Constant(function, graph()->zone())); |
| 67 Node* const context = Parameter(Type::Any()); |
| 68 Node* const effect = graph()->start(); |
| 69 Reduction r = Reduce(graph()->NewNode(javascript()->Create(), target, target, |
| 70 context, EmptyFrameState(), effect)); |
| 71 ASSERT_TRUE(r.Changed()); |
| 72 EXPECT_THAT( |
| 73 r.replacement(), |
| 74 IsFinishRegion( |
| 75 IsAllocate(IsNumberConstant(function->initial_map()->instance_size()), |
| 76 IsBeginRegion(effect), _), |
| 77 _)); |
| 78 } |
| 79 |
| 80 // ----------------------------------------------------------------------------- |
| 81 // JSCreateArguments |
| 82 |
| 83 TEST_F(JSCreateLoweringTest, JSCreateArgumentsViaStub) { |
| 84 Node* const closure = Parameter(Type::Any()); |
| 85 Node* const context = UndefinedConstant(); |
| 86 Node* const effect = graph()->start(); |
| 87 Node* const control = graph()->start(); |
| 88 Handle<SharedFunctionInfo> shared(isolate()->object_function()->shared()); |
| 89 Node* const frame_state = FrameState(shared, graph()->start()); |
| 90 Reduction r = Reduce(graph()->NewNode( |
| 91 javascript()->CreateArguments(CreateArgumentsType::kMappedArguments), |
| 92 closure, context, frame_state, effect, control)); |
| 93 ASSERT_TRUE(r.Changed()); |
| 94 EXPECT_THAT( |
| 95 r.replacement(), |
| 96 IsCall(_, |
| 97 IsHeapConstant( |
| 98 CodeFactory::ArgumentsAccess(isolate(), false, false).code()), |
| 99 closure, IsNumberConstant(0), _, effect, control)); |
| 100 } |
| 101 |
| 102 TEST_F(JSCreateLoweringTest, JSCreateArgumentsRestParameterViaStub) { |
| 103 Node* const closure = Parameter(Type::Any()); |
| 104 Node* const context = UndefinedConstant(); |
| 105 Node* const effect = graph()->start(); |
| 106 Node* const control = graph()->start(); |
| 107 Handle<SharedFunctionInfo> shared(isolate()->object_function()->shared()); |
| 108 Node* const frame_state = FrameState(shared, graph()->start()); |
| 109 Reduction r = Reduce(graph()->NewNode( |
| 110 javascript()->CreateArguments(CreateArgumentsType::kRestParameter), |
| 111 closure, context, frame_state, effect, control)); |
| 112 ASSERT_TRUE(r.Changed()); |
| 113 EXPECT_THAT( |
| 114 r.replacement(), |
| 115 IsCall(_, IsHeapConstant( |
| 116 CodeFactory::FastNewRestParameter(isolate()).code()), |
| 117 closure, context, frame_state, effect, control)); |
| 118 } |
| 119 |
| 120 TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedMapped) { |
| 121 Node* const closure = Parameter(Type::Any()); |
| 122 Node* const context = UndefinedConstant(); |
| 123 Node* const effect = graph()->start(); |
| 124 Node* const control = graph()->start(); |
| 125 Handle<SharedFunctionInfo> shared(isolate()->object_function()->shared()); |
| 126 Node* const frame_state_outer = FrameState(shared, graph()->start()); |
| 127 Node* const frame_state_inner = FrameState(shared, frame_state_outer); |
| 128 Reduction r = Reduce(graph()->NewNode( |
| 129 javascript()->CreateArguments(CreateArgumentsType::kMappedArguments), |
| 130 closure, context, frame_state_inner, effect, control)); |
| 131 ASSERT_TRUE(r.Changed()); |
| 132 EXPECT_THAT(r.replacement(), |
| 133 IsFinishRegion( |
| 134 IsAllocate(IsNumberConstant(Heap::kSloppyArgumentsObjectSize), |
| 135 _, control), |
| 136 _)); |
| 137 } |
| 138 |
| 139 TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedUnmapped) { |
| 140 Node* const closure = Parameter(Type::Any()); |
| 141 Node* const context = UndefinedConstant(); |
| 142 Node* const effect = graph()->start(); |
| 143 Node* const control = graph()->start(); |
| 144 Handle<SharedFunctionInfo> shared(isolate()->object_function()->shared()); |
| 145 Node* const frame_state_outer = FrameState(shared, graph()->start()); |
| 146 Node* const frame_state_inner = FrameState(shared, frame_state_outer); |
| 147 Reduction r = Reduce(graph()->NewNode( |
| 148 javascript()->CreateArguments(CreateArgumentsType::kUnmappedArguments), |
| 149 closure, context, frame_state_inner, effect, control)); |
| 150 ASSERT_TRUE(r.Changed()); |
| 151 EXPECT_THAT(r.replacement(), |
| 152 IsFinishRegion( |
| 153 IsAllocate(IsNumberConstant(Heap::kStrictArgumentsObjectSize), |
| 154 _, control), |
| 155 _)); |
| 156 } |
| 157 |
| 158 TEST_F(JSCreateLoweringTest, JSCreateArgumentsInlinedRestArray) { |
| 159 Node* const closure = Parameter(Type::Any()); |
| 160 Node* const context = UndefinedConstant(); |
| 161 Node* const effect = graph()->start(); |
| 162 Node* const control = graph()->start(); |
| 163 Handle<SharedFunctionInfo> shared(isolate()->object_function()->shared()); |
| 164 Node* const frame_state_outer = FrameState(shared, graph()->start()); |
| 165 Node* const frame_state_inner = FrameState(shared, frame_state_outer); |
| 166 Reduction r = Reduce(graph()->NewNode( |
| 167 javascript()->CreateArguments(CreateArgumentsType::kRestParameter), |
| 168 closure, context, frame_state_inner, effect, control)); |
| 169 ASSERT_TRUE(r.Changed()); |
| 170 EXPECT_THAT(r.replacement(), |
| 171 IsFinishRegion( |
| 172 IsAllocate(IsNumberConstant(JSArray::kSize), _, control), _)); |
| 173 } |
| 174 |
| 175 // ----------------------------------------------------------------------------- |
| 176 // JSCreateFunctionContext |
| 177 |
| 178 TEST_F(JSCreateLoweringTest, JSCreateFunctionContextViaInlinedAllocation) { |
| 179 Node* const closure = Parameter(Type::Any()); |
| 180 Node* const context = Parameter(Type::Any()); |
| 181 Node* const effect = graph()->start(); |
| 182 Node* const control = graph()->start(); |
| 183 Reduction const r = |
| 184 Reduce(graph()->NewNode(javascript()->CreateFunctionContext(8), closure, |
| 185 context, effect, control)); |
| 186 ASSERT_TRUE(r.Changed()); |
| 187 EXPECT_THAT(r.replacement(), |
| 188 IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor( |
| 189 8 + Context::MIN_CONTEXT_SLOTS)), |
| 190 IsBeginRegion(_), control), |
| 191 _)); |
| 192 } |
| 193 |
| 194 // ----------------------------------------------------------------------------- |
| 195 // JSCreateWithContext |
| 196 |
| 197 TEST_F(JSCreateLoweringTest, JSCreateWithContext) { |
| 198 Node* const object = Parameter(Type::Receiver()); |
| 199 Node* const closure = Parameter(Type::Function()); |
| 200 Node* const context = Parameter(Type::Any()); |
| 201 Node* const effect = graph()->start(); |
| 202 Node* const control = graph()->start(); |
| 203 Reduction r = |
| 204 Reduce(graph()->NewNode(javascript()->CreateWithContext(), object, |
| 205 closure, context, effect, control)); |
| 206 ASSERT_TRUE(r.Changed()); |
| 207 EXPECT_THAT(r.replacement(), |
| 208 IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor( |
| 209 Context::MIN_CONTEXT_SLOTS)), |
| 210 IsBeginRegion(_), control), |
| 211 _)); |
| 212 } |
| 213 |
| 214 // ----------------------------------------------------------------------------- |
| 215 // JSCreateCatchContext |
| 216 |
| 217 TEST_F(JSCreateLoweringTest, JSCreateCatchContext) { |
| 218 Handle<String> name = factory()->length_string(); |
| 219 Node* const exception = Parameter(Type::Receiver()); |
| 220 Node* const closure = Parameter(Type::Function()); |
| 221 Node* const context = Parameter(Type::Any()); |
| 222 Node* const effect = graph()->start(); |
| 223 Node* const control = graph()->start(); |
| 224 Reduction r = |
| 225 Reduce(graph()->NewNode(javascript()->CreateCatchContext(name), exception, |
| 226 closure, context, effect, control)); |
| 227 ASSERT_TRUE(r.Changed()); |
| 228 EXPECT_THAT(r.replacement(), |
| 229 IsFinishRegion(IsAllocate(IsNumberConstant(Context::SizeFor( |
| 230 Context::MIN_CONTEXT_SLOTS + 1)), |
| 231 IsBeginRegion(_), control), |
| 232 _)); |
| 233 } |
| 234 |
| 235 } // namespace compiler |
| 236 } // namespace internal |
| 237 } // namespace v8 |
OLD | NEW |