OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
| 28 #include <iostream> // NOLINT(readability/streams) |
| 29 |
28 #include "src/v8.h" | 30 #include "src/v8.h" |
29 | 31 |
30 #include "src/disassembler.h" | 32 #include "src/disassembler.h" |
31 #include "src/factory.h" | 33 #include "src/factory.h" |
32 #include "src/macro-assembler.h" | 34 #include "src/macro-assembler.h" |
33 #include "src/mips/macro-assembler-mips.h" | 35 #include "src/mips/macro-assembler-mips.h" |
34 #include "src/mips/simulator-mips.h" | 36 #include "src/mips/simulator-mips.h" |
35 | 37 |
36 #include "test/cctest/cctest.h" | 38 #include "test/cctest/cctest.h" |
37 | 39 |
| 40 |
38 using namespace v8::internal; | 41 using namespace v8::internal; |
39 | 42 |
40 | 43 |
41 // Define these function prototypes to match JSEntryFunction in execution.cc. | 44 // Define these function prototypes to match JSEntryFunction in execution.cc. |
42 typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4); | 45 typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4); |
43 typedef Object* (*F2)(int x, int y, int p2, int p3, int p4); | 46 typedef Object* (*F2)(int x, int y, int p2, int p3, int p4); |
44 typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4); | 47 typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4); |
45 | 48 |
46 | 49 |
47 #define __ assm. | 50 #define __ assm. |
(...skipping 1217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1265 | 1268 |
1266 Label target; | 1269 Label target; |
1267 __ beq(v0, v1, &target); | 1270 __ beq(v0, v1, &target); |
1268 __ nop(); | 1271 __ nop(); |
1269 __ bne(v0, v1, &target); | 1272 __ bne(v0, v1, &target); |
1270 __ nop(); | 1273 __ nop(); |
1271 __ bind(&target); | 1274 __ bind(&target); |
1272 __ nop(); | 1275 __ nop(); |
1273 } | 1276 } |
1274 | 1277 |
| 1278 |
| 1279 TEST(jump_tables1) { |
| 1280 // Test jump tables with forward jumps. |
| 1281 CcTest::InitializeVM(); |
| 1282 Isolate* isolate = CcTest::i_isolate(); |
| 1283 HandleScope scope(isolate); |
| 1284 Assembler assm(isolate, nullptr, 0); |
| 1285 |
| 1286 const int kNumCases = 512; |
| 1287 int values[kNumCases]; |
| 1288 isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 1289 Label labels[kNumCases]; |
| 1290 |
| 1291 __ addiu(sp, sp, -4); |
| 1292 __ sw(ra, MemOperand(sp)); |
| 1293 |
| 1294 Label done; |
| 1295 { |
| 1296 PredictableCodeSizeScope predictable( |
| 1297 &assm, (kNumCases + 7) * Assembler::kInstrSize); |
| 1298 Label here; |
| 1299 |
| 1300 __ bal(&here); |
| 1301 __ nop(); |
| 1302 __ bind(&here); |
| 1303 __ sll(at, a0, 2); |
| 1304 __ addu(at, at, ra); |
| 1305 __ lw(at, MemOperand(at, 5 * Assembler::kInstrSize)); |
| 1306 __ jr(at); |
| 1307 __ nop(); |
| 1308 for (int i = 0; i < kNumCases; ++i) { |
| 1309 __ dd(&labels[i]); |
| 1310 } |
| 1311 } |
| 1312 |
| 1313 for (int i = 0; i < kNumCases; ++i) { |
| 1314 __ bind(&labels[i]); |
| 1315 __ lui(v0, (values[i] >> 16) & 0xffff); |
| 1316 __ ori(v0, v0, values[i] & 0xffff); |
| 1317 __ b(&done); |
| 1318 __ nop(); |
| 1319 } |
| 1320 |
| 1321 __ bind(&done); |
| 1322 __ lw(ra, MemOperand(sp)); |
| 1323 __ addiu(sp, sp, 4); |
| 1324 __ jr(ra); |
| 1325 __ nop(); |
| 1326 |
| 1327 CodeDesc desc; |
| 1328 assm.GetCode(&desc); |
| 1329 Handle<Code> code = isolate->factory()->NewCode( |
| 1330 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 1331 #ifdef OBJECT_PRINT |
| 1332 code->Print(std::cout); |
| 1333 #endif |
| 1334 F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 1335 for (int i = 0; i < kNumCases; ++i) { |
| 1336 int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, i, 0, 0, 0, 0)); |
| 1337 ::printf("f(%d) = %d\n", i, res); |
| 1338 CHECK_EQ(values[i], res); |
| 1339 } |
| 1340 } |
| 1341 |
| 1342 |
| 1343 TEST(jump_tables2) { |
| 1344 // Test jump tables with backward jumps. |
| 1345 CcTest::InitializeVM(); |
| 1346 Isolate* isolate = CcTest::i_isolate(); |
| 1347 HandleScope scope(isolate); |
| 1348 Assembler assm(isolate, nullptr, 0); |
| 1349 |
| 1350 const int kNumCases = 512; |
| 1351 int values[kNumCases]; |
| 1352 isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 1353 Label labels[kNumCases]; |
| 1354 |
| 1355 __ addiu(sp, sp, -4); |
| 1356 __ sw(ra, MemOperand(sp)); |
| 1357 |
| 1358 Label done, dispatch; |
| 1359 __ b(&dispatch); |
| 1360 __ nop(); |
| 1361 |
| 1362 for (int i = 0; i < kNumCases; ++i) { |
| 1363 __ bind(&labels[i]); |
| 1364 __ lui(v0, (values[i] >> 16) & 0xffff); |
| 1365 __ ori(v0, v0, values[i] & 0xffff); |
| 1366 __ b(&done); |
| 1367 __ nop(); |
| 1368 } |
| 1369 |
| 1370 __ bind(&dispatch); |
| 1371 { |
| 1372 PredictableCodeSizeScope predictable( |
| 1373 &assm, (kNumCases + 7) * Assembler::kInstrSize); |
| 1374 Label here; |
| 1375 |
| 1376 __ bal(&here); |
| 1377 __ nop(); |
| 1378 __ bind(&here); |
| 1379 __ sll(at, a0, 2); |
| 1380 __ addu(at, at, ra); |
| 1381 __ lw(at, MemOperand(at, 5 * Assembler::kInstrSize)); |
| 1382 __ jr(at); |
| 1383 __ nop(); |
| 1384 for (int i = 0; i < kNumCases; ++i) { |
| 1385 __ dd(&labels[i]); |
| 1386 } |
| 1387 } |
| 1388 |
| 1389 __ bind(&done); |
| 1390 __ lw(ra, MemOperand(sp)); |
| 1391 __ addiu(sp, sp, 4); |
| 1392 __ jr(ra); |
| 1393 __ nop(); |
| 1394 |
| 1395 CodeDesc desc; |
| 1396 assm.GetCode(&desc); |
| 1397 Handle<Code> code = isolate->factory()->NewCode( |
| 1398 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 1399 #ifdef OBJECT_PRINT |
| 1400 code->Print(std::cout); |
| 1401 #endif |
| 1402 F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 1403 for (int i = 0; i < kNumCases; ++i) { |
| 1404 int res = reinterpret_cast<int>(CALL_GENERATED_CODE(f, i, 0, 0, 0, 0)); |
| 1405 ::printf("f(%d) = %d\n", i, res); |
| 1406 CHECK_EQ(values[i], res); |
| 1407 } |
| 1408 } |
| 1409 |
| 1410 |
| 1411 TEST(jump_tables3) { |
| 1412 // Test jump tables with backward jumps and embedded heap objects. |
| 1413 CcTest::InitializeVM(); |
| 1414 Isolate* isolate = CcTest::i_isolate(); |
| 1415 HandleScope scope(isolate); |
| 1416 Assembler assm(isolate, nullptr, 0); |
| 1417 |
| 1418 const int kNumCases = 256; |
| 1419 Handle<Object> values[kNumCases]; |
| 1420 for (int i = 0; i < kNumCases; ++i) { |
| 1421 double value = isolate->random_number_generator()->NextDouble(); |
| 1422 values[i] = isolate->factory()->NewHeapNumber(value, IMMUTABLE, TENURED); |
| 1423 } |
| 1424 Label labels[kNumCases]; |
| 1425 Object* obj; |
| 1426 int32_t imm32; |
| 1427 |
| 1428 __ addiu(sp, sp, -4); |
| 1429 __ sw(ra, MemOperand(sp)); |
| 1430 |
| 1431 Label done, dispatch; |
| 1432 __ b(&dispatch); |
| 1433 |
| 1434 |
| 1435 for (int i = 0; i < kNumCases; ++i) { |
| 1436 __ bind(&labels[i]); |
| 1437 obj = *values[i]; |
| 1438 imm32 = reinterpret_cast<intptr_t>(obj); |
| 1439 __ lui(v0, (imm32 >> 16) & 0xffff); |
| 1440 __ ori(v0, v0, imm32 & 0xffff); |
| 1441 __ b(&done); |
| 1442 __ nop(); |
| 1443 } |
| 1444 |
| 1445 __ bind(&dispatch); |
| 1446 { |
| 1447 PredictableCodeSizeScope predictable( |
| 1448 &assm, (kNumCases + 7) * Assembler::kInstrSize); |
| 1449 Label here; |
| 1450 |
| 1451 __ bal(&here); |
| 1452 __ nop(); |
| 1453 __ bind(&here); |
| 1454 __ sll(at, a0, 2); |
| 1455 __ addu(at, at, ra); |
| 1456 __ lw(at, MemOperand(at, 5 * Assembler::kInstrSize)); |
| 1457 __ jr(at); |
| 1458 __ nop(); |
| 1459 for (int i = 0; i < kNumCases; ++i) { |
| 1460 __ dd(&labels[i]); |
| 1461 } |
| 1462 } |
| 1463 |
| 1464 __ bind(&done); |
| 1465 __ lw(ra, MemOperand(sp)); |
| 1466 __ addiu(sp, sp, 4); |
| 1467 __ jr(ra); |
| 1468 __ nop(); |
| 1469 |
| 1470 CodeDesc desc; |
| 1471 assm.GetCode(&desc); |
| 1472 Handle<Code> code = isolate->factory()->NewCode( |
| 1473 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 1474 #ifdef OBJECT_PRINT |
| 1475 code->Print(std::cout); |
| 1476 #endif |
| 1477 F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 1478 for (int i = 0; i < kNumCases; ++i) { |
| 1479 Handle<Object> result(CALL_GENERATED_CODE(f, i, 0, 0, 0, 0), isolate); |
| 1480 #ifdef OBJECT_PRINT |
| 1481 ::printf("f(%d) = ", i); |
| 1482 result->Print(std::cout); |
| 1483 ::printf("\n"); |
| 1484 #endif |
| 1485 CHECK(values[i].is_identical_to(result)); |
| 1486 } |
| 1487 } |
| 1488 |
| 1489 |
1275 #undef __ | 1490 #undef __ |
OLD | NEW |