Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include <cmath>
6 :
7 : #include "src/api-inl.h"
8 : #include "src/base/utils/random-number-generator.h"
9 : #include "src/builtins/builtins-promise-gen.h"
10 : #include "src/builtins/builtins-string-gen.h"
11 : #include "src/char-predicates.h"
12 : #include "src/code-factory.h"
13 : #include "src/code-stub-assembler.h"
14 : #include "src/compiler/node.h"
15 : #include "src/debug/debug.h"
16 : #include "src/elements-kind.h"
17 : #include "src/isolate.h"
18 : #include "src/objects-inl.h"
19 : #include "src/objects/promise-inl.h"
20 : #include "test/cctest/compiler/code-assembler-tester.h"
21 : #include "test/cctest/compiler/function-tester.h"
22 : #include "torque-generated/builtins-test-from-dsl-gen.h"
23 :
24 : namespace v8 {
25 : namespace internal {
26 : namespace compiler {
27 :
28 : namespace {
29 :
30 : typedef CodeAssemblerLabel Label;
31 : typedef CodeAssemblerVariable Variable;
32 :
33 256 : class TestTorqueAssembler : public CodeStubAssembler,
34 : public TestBuiltinsFromDSLAssembler {
35 : public:
36 : explicit TestTorqueAssembler(CodeAssemblerState* state)
37 128 : : CodeStubAssembler(state), TestBuiltinsFromDSLAssembler(state) {}
38 : };
39 :
40 : } // namespace
41 :
42 26643 : TEST(TestConstexpr1) {
43 : Isolate* isolate(CcTest::InitIsolateOnce());
44 8 : CodeAssemblerTester asm_tester(isolate, 0);
45 4 : TestTorqueAssembler m(asm_tester.state());
46 : {
47 4 : m.TestConstexpr1();
48 8 : m.Return(m.UndefinedConstant());
49 : }
50 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
51 4 : ft.Call();
52 4 : }
53 :
54 26643 : TEST(TestConstexprIf) {
55 : Isolate* isolate(CcTest::InitIsolateOnce());
56 8 : CodeAssemblerTester asm_tester(isolate, 0);
57 4 : TestTorqueAssembler m(asm_tester.state());
58 : {
59 4 : m.TestConstexprIf();
60 8 : m.Return(m.UndefinedConstant());
61 : }
62 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
63 4 : ft.Call();
64 4 : }
65 :
66 26643 : TEST(TestConstexprReturn) {
67 : Isolate* isolate(CcTest::InitIsolateOnce());
68 8 : CodeAssemblerTester asm_tester(isolate, 0);
69 4 : TestTorqueAssembler m(asm_tester.state());
70 : {
71 4 : m.TestConstexprReturn();
72 8 : m.Return(m.UndefinedConstant());
73 : }
74 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
75 4 : ft.Call();
76 4 : }
77 :
78 26643 : TEST(TestGotoLabel) {
79 : Isolate* isolate(CcTest::InitIsolateOnce());
80 8 : CodeAssemblerTester asm_tester(isolate, 0);
81 4 : TestTorqueAssembler m(asm_tester.state());
82 8 : { m.Return(m.TestGotoLabel()); }
83 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
84 4 : ft.CheckCall(ft.true_value());
85 4 : }
86 :
87 26643 : TEST(TestGotoLabelWithOneParameter) {
88 : Isolate* isolate(CcTest::InitIsolateOnce());
89 8 : CodeAssemblerTester asm_tester(isolate, 0);
90 4 : TestTorqueAssembler m(asm_tester.state());
91 8 : { m.Return(m.TestGotoLabelWithOneParameter()); }
92 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
93 4 : ft.CheckCall(ft.true_value());
94 4 : }
95 :
96 26643 : TEST(TestGotoLabelWithTwoParameters) {
97 : Isolate* isolate(CcTest::InitIsolateOnce());
98 8 : CodeAssemblerTester asm_tester(isolate, 0);
99 4 : TestTorqueAssembler m(asm_tester.state());
100 8 : { m.Return(m.TestGotoLabelWithTwoParameters()); }
101 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
102 4 : ft.CheckCall(ft.true_value());
103 4 : }
104 :
105 26643 : TEST(TestPartiallyUnusedLabel) {
106 : Isolate* isolate(CcTest::InitIsolateOnce());
107 8 : CodeAssemblerTester asm_tester(isolate, 0);
108 4 : TestTorqueAssembler m(asm_tester.state());
109 8 : { m.Return(m.TestPartiallyUnusedLabel()); }
110 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
111 4 : ft.CheckCall(ft.true_value());
112 4 : }
113 :
114 26643 : TEST(TestBuiltinSpecialization) {
115 : Isolate* isolate(CcTest::InitIsolateOnce());
116 8 : CodeAssemblerTester asm_tester(isolate, 0);
117 4 : TestTorqueAssembler m(asm_tester.state());
118 : {
119 8 : Node* temp = m.SmiConstant(0);
120 4 : m.TestBuiltinSpecialization(m.UncheckedCast<Context>(temp));
121 8 : m.Return(m.UndefinedConstant());
122 : }
123 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
124 4 : ft.Call();
125 4 : }
126 :
127 26643 : TEST(TestMacroSpecialization) {
128 : Isolate* isolate(CcTest::InitIsolateOnce());
129 8 : CodeAssemblerTester asm_tester(isolate, 0);
130 4 : TestTorqueAssembler m(asm_tester.state());
131 : {
132 4 : m.TestMacroSpecialization();
133 8 : m.Return(m.UndefinedConstant());
134 : }
135 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
136 4 : ft.Call();
137 4 : }
138 :
139 26643 : TEST(TestFunctionPointers) {
140 : Isolate* isolate(CcTest::InitIsolateOnce());
141 : const int kNumParams = 0;
142 8 : CodeAssemblerTester asm_tester(isolate, kNumParams);
143 4 : TestTorqueAssembler m(asm_tester.state());
144 : {
145 : TNode<Context> context =
146 4 : m.UncheckedCast<Context>(m.Parameter(kNumParams + 2));
147 8 : m.Return(m.TestFunctionPointers(context));
148 : }
149 4 : FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
150 4 : ft.CheckCall(ft.true_value());
151 4 : }
152 :
153 26643 : TEST(TestTernaryOperator) {
154 : Isolate* isolate(CcTest::InitIsolateOnce());
155 : const int kNumParams = 1;
156 8 : CodeAssemblerTester asm_tester(isolate, kNumParams);
157 4 : TestTorqueAssembler m(asm_tester.state());
158 : {
159 4 : TNode<Smi> arg = m.UncheckedCast<Smi>(m.Parameter(0));
160 8 : m.Return(m.TestTernaryOperator(arg));
161 : }
162 4 : FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
163 : Handle<Object> result1 =
164 8 : ft.Call(Handle<Smi>(Smi::FromInt(-5), isolate)).ToHandleChecked();
165 4 : CHECK_EQ(-15, Handle<Smi>::cast(result1)->value());
166 : Handle<Object> result2 =
167 8 : ft.Call(Handle<Smi>(Smi::FromInt(3), isolate)).ToHandleChecked();
168 4 : CHECK_EQ(103, Handle<Smi>::cast(result2)->value());
169 4 : }
170 :
171 26643 : TEST(TestFunctionPointerToGeneric) {
172 : Isolate* isolate(CcTest::InitIsolateOnce());
173 8 : CodeAssemblerTester asm_tester(isolate, 0);
174 4 : TestTorqueAssembler m(asm_tester.state());
175 : {
176 8 : Node* temp = m.SmiConstant(0);
177 4 : m.TestFunctionPointerToGeneric(m.UncheckedCast<Context>(temp));
178 8 : m.Return(m.UndefinedConstant());
179 : }
180 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
181 4 : ft.Call();
182 4 : }
183 :
184 26643 : TEST(TestUnsafeCast) {
185 : Isolate* isolate(CcTest::InitIsolateOnce());
186 8 : CodeAssemblerTester asm_tester(isolate, 0);
187 4 : TestTorqueAssembler m(asm_tester.state());
188 : {
189 8 : Node* temp = m.SmiConstant(0);
190 8 : Node* n = m.SmiConstant(10);
191 8 : m.Return(m.TestUnsafeCast(m.UncheckedCast<Context>(temp),
192 4 : m.UncheckedCast<Number>(n)));
193 : }
194 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
195 4 : ft.CheckCall(ft.true_value());
196 4 : }
197 :
198 26643 : TEST(TestHexLiteral) {
199 : Isolate* isolate(CcTest::InitIsolateOnce());
200 8 : CodeAssemblerTester asm_tester(isolate, 0);
201 4 : TestTorqueAssembler m(asm_tester.state());
202 : {
203 4 : m.TestHexLiteral();
204 8 : m.Return(m.UndefinedConstant());
205 : }
206 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
207 4 : ft.Call();
208 4 : }
209 :
210 26643 : TEST(TestModuleConstBindings) {
211 : Isolate* isolate(CcTest::InitIsolateOnce());
212 8 : CodeAssemblerTester asm_tester(isolate, 0);
213 4 : TestTorqueAssembler m(asm_tester.state());
214 : {
215 4 : m.TestModuleConstBindings();
216 8 : m.Return(m.UndefinedConstant());
217 : }
218 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
219 4 : ft.Call();
220 4 : }
221 :
222 26643 : TEST(TestLocalConstBindings) {
223 : Isolate* isolate(CcTest::InitIsolateOnce());
224 8 : CodeAssemblerTester asm_tester(isolate, 0);
225 4 : TestTorqueAssembler m(asm_tester.state());
226 : {
227 4 : m.TestLocalConstBindings();
228 8 : m.Return(m.UndefinedConstant());
229 : }
230 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
231 4 : ft.Call();
232 4 : }
233 :
234 26643 : TEST(TestForLoop) {
235 : Isolate* isolate(CcTest::InitIsolateOnce());
236 8 : CodeAssemblerTester asm_tester(isolate, 0);
237 4 : TestTorqueAssembler m(asm_tester.state());
238 : {
239 4 : m.TestForLoop();
240 8 : m.Return(m.UndefinedConstant());
241 : }
242 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
243 4 : ft.Call();
244 4 : }
245 :
246 26643 : TEST(TestTypeswitch) {
247 4 : CcTest::InitializeVM();
248 : Isolate* isolate(CcTest::i_isolate());
249 : i::HandleScope scope(isolate);
250 : Handle<Context> context =
251 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
252 8 : CodeAssemblerTester asm_tester(isolate, 0);
253 4 : TestTorqueAssembler m(asm_tester.state());
254 : {
255 4 : m.TestTypeswitch(m.UncheckedCast<Context>(m.HeapConstant(context)));
256 8 : m.Return(m.UndefinedConstant());
257 : }
258 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
259 4 : ft.Call();
260 4 : }
261 :
262 26643 : TEST(TestGenericOverload) {
263 4 : CcTest::InitializeVM();
264 : Isolate* isolate(CcTest::i_isolate());
265 : i::HandleScope scope(isolate);
266 : Handle<Context> context =
267 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
268 8 : CodeAssemblerTester asm_tester(isolate, 0);
269 4 : TestTorqueAssembler m(asm_tester.state());
270 : {
271 4 : m.TestGenericOverload(m.UncheckedCast<Context>(m.HeapConstant(context)));
272 8 : m.Return(m.UndefinedConstant());
273 : }
274 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
275 4 : ft.Call();
276 4 : }
277 :
278 26643 : TEST(TestEquality) {
279 4 : CcTest::InitializeVM();
280 : Isolate* isolate(CcTest::i_isolate());
281 : i::HandleScope scope(isolate);
282 : Handle<Context> context =
283 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
284 8 : CodeAssemblerTester asm_tester(isolate, 0);
285 4 : TestTorqueAssembler m(asm_tester.state());
286 : {
287 4 : m.TestEquality(m.UncheckedCast<Context>(m.HeapConstant(context)));
288 8 : m.Return(m.UndefinedConstant());
289 : }
290 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
291 4 : ft.Call();
292 4 : }
293 :
294 26643 : TEST(TestLogicalOperators) {
295 : Isolate* isolate(CcTest::InitIsolateOnce());
296 8 : CodeAssemblerTester asm_tester(isolate, 0);
297 4 : TestTorqueAssembler m(asm_tester.state());
298 : {
299 4 : m.TestLogicalOperators();
300 8 : m.Return(m.UndefinedConstant());
301 : }
302 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
303 4 : ft.Call();
304 4 : }
305 :
306 26643 : TEST(TestOtherwiseAndLabels) {
307 : Isolate* isolate(CcTest::InitIsolateOnce());
308 8 : CodeAssemblerTester asm_tester(isolate, 0);
309 4 : TestTorqueAssembler m(asm_tester.state());
310 : {
311 4 : m.TestOtherwiseWithCode1();
312 4 : m.TestOtherwiseWithCode2();
313 4 : m.TestOtherwiseWithCode3();
314 4 : m.TestForwardLabel();
315 8 : m.Return(m.UndefinedConstant());
316 : }
317 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
318 4 : ft.Call();
319 4 : }
320 :
321 26643 : TEST(TestCatch1) {
322 4 : CcTest::InitializeVM();
323 : Isolate* isolate(CcTest::i_isolate());
324 : i::HandleScope scope(isolate);
325 : Handle<Context> context =
326 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
327 8 : CodeAssemblerTester asm_tester(isolate, 0);
328 4 : TestTorqueAssembler m(asm_tester.state());
329 : {
330 : TNode<Smi> result =
331 4 : m.TestCatch1(m.UncheckedCast<Context>(m.HeapConstant(context)));
332 : USE(result);
333 : CSA_ASSERT(&m, m.WordEqual(result, m.SmiConstant(1)));
334 8 : m.Return(m.UndefinedConstant());
335 : }
336 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
337 4 : ft.Call();
338 4 : }
339 :
340 26643 : TEST(TestCatch2) {
341 4 : CcTest::InitializeVM();
342 : Isolate* isolate(CcTest::i_isolate());
343 : i::HandleScope scope(isolate);
344 : Handle<Context> context =
345 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
346 8 : CodeAssemblerTester asm_tester(isolate, 0);
347 4 : TestTorqueAssembler m(asm_tester.state());
348 : {
349 : TNode<Smi> result =
350 4 : m.TestCatch2(m.UncheckedCast<Context>(m.HeapConstant(context)));
351 : USE(result);
352 : CSA_ASSERT(&m, m.WordEqual(result, m.SmiConstant(2)));
353 8 : m.Return(m.UndefinedConstant());
354 : }
355 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
356 4 : ft.Call();
357 4 : }
358 :
359 26643 : TEST(TestCatch3) {
360 4 : CcTest::InitializeVM();
361 : Isolate* isolate(CcTest::i_isolate());
362 : i::HandleScope scope(isolate);
363 : Handle<Context> context =
364 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
365 8 : CodeAssemblerTester asm_tester(isolate, 0);
366 4 : TestTorqueAssembler m(asm_tester.state());
367 : {
368 : TNode<Smi> result =
369 4 : m.TestCatch3(m.UncheckedCast<Context>(m.HeapConstant(context)));
370 : USE(result);
371 : CSA_ASSERT(&m, m.WordEqual(result, m.SmiConstant(2)));
372 8 : m.Return(m.UndefinedConstant());
373 : }
374 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
375 4 : ft.Call();
376 4 : }
377 :
378 26643 : TEST(TestLookup) {
379 4 : CcTest::InitializeVM();
380 : Isolate* isolate(CcTest::i_isolate());
381 : i::HandleScope scope(isolate);
382 : Handle<Context> context =
383 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
384 8 : CodeAssemblerTester asm_tester(isolate, 0);
385 4 : TestTorqueAssembler m(asm_tester.state());
386 : {
387 4 : m.TestQualifiedAccess(m.UncheckedCast<Context>(m.HeapConstant(context)));
388 8 : m.Return(m.UndefinedConstant());
389 : }
390 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
391 4 : ft.Call();
392 4 : }
393 :
394 26643 : TEST(TestFrame1) {
395 4 : CcTest::InitializeVM();
396 : Isolate* isolate(CcTest::i_isolate());
397 : i::HandleScope scope(isolate);
398 : Handle<Context> context =
399 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
400 8 : CodeAssemblerTester asm_tester(isolate);
401 4 : TestTorqueAssembler m(asm_tester.state());
402 : {
403 4 : m.TestFrame1(m.UncheckedCast<Context>(m.HeapConstant(context)));
404 8 : m.Return(m.UndefinedConstant());
405 : }
406 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
407 4 : ft.Call();
408 4 : }
409 :
410 26643 : TEST(TestNew) {
411 4 : CcTest::InitializeVM();
412 : Isolate* isolate(CcTest::i_isolate());
413 : i::HandleScope scope(isolate);
414 : Handle<Context> context =
415 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
416 8 : CodeAssemblerTester asm_tester(isolate);
417 4 : TestTorqueAssembler m(asm_tester.state());
418 : {
419 4 : m.TestNew(m.UncheckedCast<Context>(m.HeapConstant(context)));
420 8 : m.Return(m.UndefinedConstant());
421 : }
422 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
423 4 : ft.Call();
424 4 : }
425 :
426 26643 : TEST(TestStructConstructor) {
427 4 : CcTest::InitializeVM();
428 : Isolate* isolate(CcTest::i_isolate());
429 : i::HandleScope scope(isolate);
430 : Handle<Context> context =
431 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
432 8 : CodeAssemblerTester asm_tester(isolate);
433 4 : TestTorqueAssembler m(asm_tester.state());
434 : {
435 4 : m.TestStructConstructor(m.UncheckedCast<Context>(m.HeapConstant(context)));
436 8 : m.Return(m.UndefinedConstant());
437 : }
438 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
439 4 : ft.Call();
440 4 : }
441 :
442 26643 : TEST(TestInternalClass) {
443 4 : CcTest::InitializeVM();
444 : Isolate* isolate(CcTest::i_isolate());
445 : i::HandleScope scope(isolate);
446 : Handle<Context> context =
447 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
448 8 : CodeAssemblerTester asm_tester(isolate);
449 4 : TestTorqueAssembler m(asm_tester.state());
450 : {
451 4 : m.TestInternalClass(m.UncheckedCast<Context>(m.HeapConstant(context)));
452 8 : m.Return(m.UndefinedConstant());
453 : }
454 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
455 4 : ft.Call();
456 4 : }
457 :
458 26643 : TEST(TestNewFixedArrayFromSpread) {
459 4 : CcTest::InitializeVM();
460 : Isolate* isolate(CcTest::i_isolate());
461 : i::HandleScope scope(isolate);
462 : Handle<Context> context =
463 8 : Utils::OpenHandle(*v8::Isolate::GetCurrent()->GetCurrentContext());
464 8 : CodeAssemblerTester asm_tester(isolate);
465 4 : TestTorqueAssembler m(asm_tester.state());
466 : {
467 : m.TestNewFixedArrayFromSpread(
468 4 : m.UncheckedCast<Context>(m.HeapConstant(context)));
469 8 : m.Return(m.UndefinedConstant());
470 : }
471 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
472 4 : ft.Call();
473 4 : }
474 :
475 26643 : TEST(TestReferences) {
476 4 : CcTest::InitializeVM();
477 : Isolate* isolate(CcTest::i_isolate());
478 : i::HandleScope scope(isolate);
479 8 : CodeAssemblerTester asm_tester(isolate);
480 4 : TestTorqueAssembler m(asm_tester.state());
481 : {
482 4 : m.TestReferences();
483 8 : m.Return(m.UndefinedConstant());
484 : }
485 4 : FunctionTester ft(asm_tester.GenerateCode(), 0);
486 4 : ft.Call();
487 4 : }
488 :
489 : } // namespace compiler
490 : } // namespace internal
491 79917 : } // namespace v8
|