OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/code-assembler.h" | 5 #include "src/compiler/code-assembler.h" |
6 #include "src/isolate.h" | 6 #include "src/isolate.h" |
7 #include "test/cctest/compiler/code-assembler-tester.h" | 7 #include "test/cctest/compiler/code-assembler-tester.h" |
8 #include "test/cctest/compiler/function-tester.h" | 8 #include "test/cctest/compiler/function-tester.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); | 117 Node* context = m.HeapConstant(Handle<Context>(isolate->native_context())); |
118 Node* a = SmiTag(m, m.Int32Constant(2)); | 118 Node* a = SmiTag(m, m.Int32Constant(2)); |
119 Node* b = SmiTag(m, m.Int32Constant(4)); | 119 Node* b = SmiTag(m, m.Int32Constant(4)); |
120 m.TailCallRuntime(Runtime::kMathPow, context, a, b); | 120 m.TailCallRuntime(Runtime::kMathPow, context, a, b); |
121 Handle<Code> code = m.GenerateCode(); | 121 Handle<Code> code = m.GenerateCode(); |
122 FunctionTester ft(descriptor, code); | 122 FunctionTester ft(descriptor, code); |
123 MaybeHandle<Object> result = ft.Call(); | 123 MaybeHandle<Object> result = ft.Call(); |
124 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); | 124 CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value()); |
125 } | 125 } |
126 | 126 |
| 127 namespace { |
| 128 |
| 129 Handle<JSFunction> CreateSumAllArgumentsFunction(FunctionTester& ft) { |
| 130 const char* source = |
| 131 "(function() {\n" |
| 132 " var sum = 0 + this;\n" |
| 133 " for (var i = 0; i < arguments.length; i++) {\n" |
| 134 " sum += arguments[i];\n" |
| 135 " }\n" |
| 136 " return sum;\n" |
| 137 "})"; |
| 138 return ft.NewFunction(source); |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 |
| 143 TEST(SimpleCallJSFunction0Arg) { |
| 144 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 145 const int kNumParams = 1; |
| 146 CodeAssemblerTester m(isolate, kNumParams); |
| 147 { |
| 148 Node* function = m.Parameter(0); |
| 149 Node* context = m.Parameter(kNumParams + 2); |
| 150 |
| 151 Node* receiver = SmiTag(m, m.Int32Constant(42)); |
| 152 |
| 153 Callable callable = CodeFactory::Call(isolate); |
| 154 Node* result = m.CallJS(callable, context, function, receiver); |
| 155 m.Return(result); |
| 156 } |
| 157 Handle<Code> code = m.GenerateCode(); |
| 158 FunctionTester ft(code, kNumParams); |
| 159 |
| 160 Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft); |
| 161 MaybeHandle<Object> result = ft.Call(sum); |
| 162 CHECK_EQ(Smi::FromInt(42), *result.ToHandleChecked()); |
| 163 } |
| 164 |
| 165 TEST(SimpleCallJSFunction1Arg) { |
| 166 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 167 const int kNumParams = 2; |
| 168 CodeAssemblerTester m(isolate, kNumParams); |
| 169 { |
| 170 Node* function = m.Parameter(0); |
| 171 Node* context = m.Parameter(1); |
| 172 |
| 173 Node* receiver = SmiTag(m, m.Int32Constant(42)); |
| 174 Node* a = SmiTag(m, m.Int32Constant(13)); |
| 175 |
| 176 Callable callable = CodeFactory::Call(isolate); |
| 177 Node* result = m.CallJS(callable, context, function, receiver, a); |
| 178 m.Return(result); |
| 179 } |
| 180 Handle<Code> code = m.GenerateCode(); |
| 181 FunctionTester ft(code, kNumParams); |
| 182 |
| 183 Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft); |
| 184 MaybeHandle<Object> result = ft.Call(sum); |
| 185 CHECK_EQ(Smi::FromInt(55), *result.ToHandleChecked()); |
| 186 } |
| 187 |
| 188 TEST(SimpleCallJSFunction2Arg) { |
| 189 Isolate* isolate(CcTest::InitIsolateOnce()); |
| 190 const int kNumParams = 2; |
| 191 CodeAssemblerTester m(isolate, kNumParams); |
| 192 { |
| 193 Node* function = m.Parameter(0); |
| 194 Node* context = m.Parameter(1); |
| 195 |
| 196 Node* receiver = SmiTag(m, m.Int32Constant(42)); |
| 197 Node* a = SmiTag(m, m.Int32Constant(13)); |
| 198 Node* b = SmiTag(m, m.Int32Constant(153)); |
| 199 |
| 200 Callable callable = CodeFactory::Call(isolate); |
| 201 Node* result = m.CallJS(callable, context, function, receiver, a, b); |
| 202 m.Return(result); |
| 203 } |
| 204 Handle<Code> code = m.GenerateCode(); |
| 205 FunctionTester ft(code, kNumParams); |
| 206 |
| 207 Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft); |
| 208 MaybeHandle<Object> result = ft.Call(sum); |
| 209 CHECK_EQ(Smi::FromInt(208), *result.ToHandleChecked()); |
| 210 } |
| 211 |
127 TEST(VariableMerge1) { | 212 TEST(VariableMerge1) { |
128 Isolate* isolate(CcTest::InitIsolateOnce()); | 213 Isolate* isolate(CcTest::InitIsolateOnce()); |
129 VoidDescriptor descriptor(isolate); | 214 VoidDescriptor descriptor(isolate); |
130 CodeAssemblerTester m(isolate, descriptor); | 215 CodeAssemblerTester m(isolate, descriptor); |
131 CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged); | 216 CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged); |
132 CodeStubAssembler::Label l1(&m), l2(&m), merge(&m); | 217 CodeStubAssembler::Label l1(&m), l2(&m), merge(&m); |
133 Node* temp = m.Int32Constant(0); | 218 Node* temp = m.Int32Constant(0); |
134 var1.Bind(temp); | 219 var1.Bind(temp); |
135 m.Branch(m.Int32Constant(1), &l1, &l2); | 220 m.Branch(m.Int32Constant(1), &l1, &l2); |
136 m.Bind(&l1); | 221 m.Bind(&l1); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 var_object.Bind(m.IntPtrConstant(66)); | 429 var_object.Bind(m.IntPtrConstant(66)); |
345 m.Goto(&block1); | 430 m.Goto(&block1); |
346 } | 431 } |
347 m.Bind(&block1); | 432 m.Bind(&block1); |
348 CHECK(!m.GenerateCode().is_null()); | 433 CHECK(!m.GenerateCode().is_null()); |
349 } | 434 } |
350 | 435 |
351 } // namespace compiler | 436 } // namespace compiler |
352 } // namespace internal | 437 } // namespace internal |
353 } // namespace v8 | 438 } // namespace v8 |
OLD | NEW |