| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
| (...skipping 1257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1268 right_side = frame_->Pop(); | 1268 right_side = frame_->Pop(); |
| 1269 } else { | 1269 } else { |
| 1270 right_side = frame_->Pop(); | 1270 right_side = frame_->Pop(); |
| 1271 left_side = frame_->Pop(); | 1271 left_side = frame_->Pop(); |
| 1272 } | 1272 } |
| 1273 // If either side is a constant smi, optimize the comparison. | 1273 // If either side is a constant smi, optimize the comparison. |
| 1274 bool left_side_constant_smi = | 1274 bool left_side_constant_smi = |
| 1275 left_side.is_constant() && left_side.handle()->IsSmi(); | 1275 left_side.is_constant() && left_side.handle()->IsSmi(); |
| 1276 bool right_side_constant_smi = | 1276 bool right_side_constant_smi = |
| 1277 right_side.is_constant() && right_side.handle()->IsSmi(); | 1277 right_side.is_constant() && right_side.handle()->IsSmi(); |
| 1278 bool left_side_constant_null = |
| 1279 left_side.is_constant() && left_side.handle()->IsNull(); |
| 1280 bool right_side_constant_null = |
| 1281 right_side.is_constant() && right_side.handle()->IsNull(); |
| 1278 | 1282 |
| 1279 if (left_side_constant_smi || right_side_constant_smi) { | 1283 if (left_side_constant_smi || right_side_constant_smi) { |
| 1280 if (left_side_constant_smi && right_side_constant_smi) { | 1284 if (left_side_constant_smi && right_side_constant_smi) { |
| 1281 // Trivial case, comparing two constants. | 1285 // Trivial case, comparing two constants. |
| 1282 int left_value = Smi::cast(*left_side.handle())->value(); | 1286 int left_value = Smi::cast(*left_side.handle())->value(); |
| 1283 int right_value = Smi::cast(*right_side.handle())->value(); | 1287 int right_value = Smi::cast(*right_side.handle())->value(); |
| 1284 if (left_value < right_value && | 1288 if (left_value < right_value && |
| 1285 (cc == less || cc == less_equal || cc == not_equal) || | 1289 (cc == less || cc == less_equal || cc == not_equal) || |
| 1286 left_value == right_value && | 1290 left_value == right_value && |
| 1287 (cc == less_equal || cc == equal || cc == greater_equal) || | 1291 (cc == less_equal || cc == equal || cc == greater_equal) || |
| 1288 left_value > right_value && | 1292 left_value > right_value && |
| 1289 (cc == greater || cc == greater_equal || cc == not_equal)) { | 1293 (cc == greater || cc == greater_equal || cc == not_equal)) { |
| 1290 // The comparison is unconditionally true. | 1294 // The comparison is unconditionally true. |
| 1291 dest->Goto(true); | 1295 dest->Goto(true); |
| 1292 } else { | 1296 } else { |
| 1293 // The comparison is unconditionally false. | 1297 // The comparison is unconditionally false. |
| 1294 dest->Goto(false); | 1298 dest->Goto(false); |
| 1295 } | 1299 } |
| 1296 } else { // Only one side is a constant Smi. | 1300 } else { // Only one side is a constant Smi. |
| 1297 // If left side is a constant Smi, reverse the operands. | 1301 // If left side is a constant Smi, reverse the operands. |
| 1298 // Since one side is a constant Smi, conversion order does not matter. | 1302 // Since one side is a constant Smi, conversion order does not matter. |
| 1299 if (left_side_constant_smi) { | 1303 if (left_side_constant_smi) { |
| 1300 Result temp = left_side; | 1304 Result temp = left_side; |
| 1301 left_side = right_side; | 1305 left_side = right_side; |
| 1302 right_side = temp; | 1306 right_side = temp; |
| 1303 cc = ReverseCondition(cc); | 1307 cc = ReverseCondition(cc); |
| 1308 // This may reintroduce greater or less_equal as the value of cc. |
| 1309 // CompareStub and the inline code both support all values of cc. |
| 1304 } | 1310 } |
| 1305 // Implement comparison against a constant Smi, inlining the case | 1311 // Implement comparison against a constant Smi, inlining the case |
| 1306 // where both sides are Smis. | 1312 // where both sides are Smis. |
| 1307 left_side.ToRegister(); | 1313 left_side.ToRegister(); |
| 1308 ASSERT(left_side.is_valid()); | 1314 ASSERT(left_side.is_valid()); |
| 1309 JumpTarget is_smi(this); | 1315 JumpTarget is_smi(this); |
| 1310 __ test(left_side.reg(), Immediate(kSmiTagMask)); | 1316 __ test(left_side.reg(), Immediate(kSmiTagMask)); |
| 1311 is_smi.Branch(zero, &left_side, &right_side, taken); | 1317 is_smi.Branch(zero, &left_side, &right_side, taken); |
| 1312 | 1318 |
| 1313 // Setup and call the compare stub, which expects arguments in edx | 1319 // Setup and call the compare stub, which expects arguments in edx |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1329 right_side.ToRegister(); | 1335 right_side.ToRegister(); |
| 1330 ASSERT(right_side.is_valid()); | 1336 ASSERT(right_side.is_valid()); |
| 1331 __ cmp(left_side.reg(), Operand(right_side.reg())); | 1337 __ cmp(left_side.reg(), Operand(right_side.reg())); |
| 1332 } else { | 1338 } else { |
| 1333 __ cmp(Operand(left_side.reg()), Immediate(right_side.handle())); | 1339 __ cmp(Operand(left_side.reg()), Immediate(right_side.handle())); |
| 1334 } | 1340 } |
| 1335 left_side.Unuse(); | 1341 left_side.Unuse(); |
| 1336 right_side.Unuse(); | 1342 right_side.Unuse(); |
| 1337 dest->Split(cc); | 1343 dest->Split(cc); |
| 1338 } | 1344 } |
| 1339 } else { // Neither side is a constant Smi, normal comparison operation. | 1345 } else if (cc == equal && |
| 1346 (left_side_constant_null || right_side_constant_null)) { |
| 1347 // To make null checks efficient, we check if either the left side or |
| 1348 // the right side is the constant 'null'. |
| 1349 // If so, we optimize the code by inlining a null check instead of |
| 1350 // calling the (very) general runtime routine for checking equality. |
| 1351 Result operand = left_side_constant_null ? right_side : left_side; |
| 1352 right_side.Unuse(); |
| 1353 left_side.Unuse(); |
| 1354 operand.ToRegister(); |
| 1355 __ cmp(operand.reg(), Factory::null_value()); |
| 1356 if (strict) { |
| 1357 operand.Unuse(); |
| 1358 dest->Split(equal); |
| 1359 } else { |
| 1360 // The 'null' value is only equal to 'undefined' if using non-strict |
| 1361 // comparisons. |
| 1362 dest->true_target()->Branch(equal); |
| 1363 __ cmp(operand.reg(), Factory::undefined_value()); |
| 1364 dest->true_target()->Branch(equal); |
| 1365 __ test(operand.reg(), Immediate(kSmiTagMask)); |
| 1366 dest->false_target()->Branch(equal); |
| 1367 |
| 1368 // It can be an undetectable object. |
| 1369 // Use a scratch register in preference to spilling operand.reg(). |
| 1370 Result temp = allocator()->Allocate(); |
| 1371 ASSERT(temp.is_valid()); |
| 1372 __ mov(temp.reg(), |
| 1373 FieldOperand(operand.reg(), HeapObject::kMapOffset)); |
| 1374 __ movzx_b(temp.reg(), |
| 1375 FieldOperand(temp.reg(), Map::kBitFieldOffset)); |
| 1376 __ test(temp.reg(), Immediate(1 << Map::kIsUndetectable)); |
| 1377 temp.Unuse(); |
| 1378 operand.Unuse(); |
| 1379 dest->Split(not_zero); |
| 1380 } |
| 1381 } else { // Neither side is a constant Smi or null. |
| 1340 // If either side is a non-smi constant, skip the smi check. | 1382 // If either side is a non-smi constant, skip the smi check. |
| 1341 bool known_non_smi = | 1383 bool known_non_smi = |
| 1342 left_side.is_constant() && !left_side.handle()->IsSmi() || | 1384 left_side.is_constant() && !left_side.handle()->IsSmi() || |
| 1343 right_side.is_constant() && !right_side.handle()->IsSmi(); | 1385 right_side.is_constant() && !right_side.handle()->IsSmi(); |
| 1344 left_side.ToRegister(); | 1386 left_side.ToRegister(); |
| 1345 right_side.ToRegister(); | 1387 right_side.ToRegister(); |
| 1346 JumpTarget is_smi(this); | 1388 JumpTarget is_smi(this); |
| 1347 if (!known_non_smi) { | 1389 if (!known_non_smi) { |
| 1348 // Check for the smi case. | 1390 // Check for the smi case. |
| 1349 Result temp = allocator_->Allocate(); | 1391 Result temp = allocator_->Allocate(); |
| (...skipping 3181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4531 }; | 4573 }; |
| 4532 | 4574 |
| 4533 | 4575 |
| 4534 void CodeGenerator::VisitCompareOperation(CompareOperation* node) { | 4576 void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
| 4535 Comment cmnt(masm_, "[ CompareOperation"); | 4577 Comment cmnt(masm_, "[ CompareOperation"); |
| 4536 | 4578 |
| 4537 // Get the expressions from the node. | 4579 // Get the expressions from the node. |
| 4538 Expression* left = node->left(); | 4580 Expression* left = node->left(); |
| 4539 Expression* right = node->right(); | 4581 Expression* right = node->right(); |
| 4540 Token::Value op = node->op(); | 4582 Token::Value op = node->op(); |
| 4541 | |
| 4542 // To make null checks efficient, we check if either left or right is the | |
| 4543 // literal 'null'. If so, we optimize the code by inlining a null check | |
| 4544 // instead of calling the (very) general runtime routine for checking | |
| 4545 // equality. | |
| 4546 if (op == Token::EQ || op == Token::EQ_STRICT) { | |
| 4547 bool left_is_null = | |
| 4548 left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); | |
| 4549 bool right_is_null = | |
| 4550 right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); | |
| 4551 // The 'null' value can only be equal to 'null' or 'undefined'. | |
| 4552 if (left_is_null || right_is_null) { | |
| 4553 Load(left_is_null ? right : left); | |
| 4554 Result operand = frame_->Pop(); | |
| 4555 operand.ToRegister(); | |
| 4556 __ cmp(operand.reg(), Factory::null_value()); | |
| 4557 Condition cc = equal; | |
| 4558 | |
| 4559 // The 'null' value is only equal to 'undefined' if using non-strict | |
| 4560 // comparisons. | |
| 4561 if (op != Token::EQ_STRICT) { | |
| 4562 destination()->true_target()->Branch(cc); | |
| 4563 __ cmp(operand.reg(), Factory::undefined_value()); | |
| 4564 destination()->true_target()->Branch(equal); | |
| 4565 __ test(operand.reg(), Immediate(kSmiTagMask)); | |
| 4566 destination()->false_target()->Branch(equal); | |
| 4567 | |
| 4568 // It can be an undetectable object. | |
| 4569 // Use a scratch register in preference to spilling operand.reg(). | |
| 4570 Result temp = allocator()->Allocate(); | |
| 4571 ASSERT(temp.is_valid()); | |
| 4572 __ mov(temp.reg(), | |
| 4573 FieldOperand(operand.reg(), HeapObject::kMapOffset)); | |
| 4574 __ movzx_b(temp.reg(), | |
| 4575 FieldOperand(temp.reg(), Map::kBitFieldOffset)); | |
| 4576 __ test(temp.reg(), Immediate(1 << Map::kIsUndetectable)); | |
| 4577 cc = not_zero; | |
| 4578 } | |
| 4579 | |
| 4580 operand.Unuse(); | |
| 4581 destination()->Split(cc); | |
| 4582 return; | |
| 4583 } | |
| 4584 } | |
| 4585 | |
| 4586 // To make typeof testing for natives implemented in JavaScript really | 4583 // To make typeof testing for natives implemented in JavaScript really |
| 4587 // efficient, we generate special code for expressions of the form: | 4584 // efficient, we generate special code for expressions of the form: |
| 4588 // 'typeof <expression> == <string>'. | 4585 // 'typeof <expression> == <string>'. |
| 4589 UnaryOperation* operation = left->AsUnaryOperation(); | 4586 UnaryOperation* operation = left->AsUnaryOperation(); |
| 4590 if ((op == Token::EQ || op == Token::EQ_STRICT) && | 4587 if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 4591 (operation != NULL && operation->op() == Token::TYPEOF) && | 4588 (operation != NULL && operation->op() == Token::TYPEOF) && |
| 4592 (right->AsLiteral() != NULL && | 4589 (right->AsLiteral() != NULL && |
| 4593 right->AsLiteral()->handle()->IsString())) { | 4590 right->AsLiteral()->handle()->IsString())) { |
| 4594 Handle<String> check(String::cast(*right->AsLiteral()->handle())); | 4591 Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 4595 | 4592 |
| (...skipping 1950 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6546 | 6543 |
| 6547 // Slow-case: Go through the JavaScript implementation. | 6544 // Slow-case: Go through the JavaScript implementation. |
| 6548 __ bind(&slow); | 6545 __ bind(&slow); |
| 6549 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); | 6546 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); |
| 6550 } | 6547 } |
| 6551 | 6548 |
| 6552 | 6549 |
| 6553 #undef __ | 6550 #undef __ |
| 6554 | 6551 |
| 6555 } } // namespace v8::internal | 6552 } } // namespace v8::internal |
| OLD | NEW |