Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: src/ast.cc

Issue 7918012: Unify the handling of comparinsons against null and undefined. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ast.h ('k') | src/full-codegen.h » ('j') | src/v8.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 case Token::DIV: 320 case Token::DIV:
321 case Token::MOD: 321 case Token::MOD:
322 return true; 322 return true;
323 default: 323 default:
324 UNREACHABLE(); 324 UNREACHABLE();
325 } 325 }
326 return false; 326 return false;
327 } 327 }
328 328
329 329
330 static bool IsTypeof(Expression* expr) {
331 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
332 return maybe_unary != NULL && maybe_unary->op() == Token::TYPEOF;
333 }
334
335
336 // Check for the pattern: typeof <expression> equals <string literal>.
337 static bool MatchLiteralCompareTypeof(Expression* left,
338 Token::Value op,
339 Expression* right,
340 Expression** expr,
341 Handle<String>* check) {
342 if (IsTypeof(left) && right->IsStringLiteral() && Token::IsEqualityOp(op)) {
343 *expr = left->AsUnaryOperation()->expression();
344 *check = Handle<String>::cast(right->AsLiteral()->handle());
345 return true;
346 }
347 return false;
348 }
349
350
330 bool CompareOperation::IsLiteralCompareTypeof(Expression** expr, 351 bool CompareOperation::IsLiteralCompareTypeof(Expression** expr,
331 Handle<String>* check) { 352 Handle<String>* check) {
332 if (op_ != Token::EQ && op_ != Token::EQ_STRICT) return false; 353 return MatchLiteralCompareTypeof(left_, op_, right_, expr, check) ||
354 MatchLiteralCompareTypeof(right_, op_, left_, expr, check);
355 }
333 356
334 UnaryOperation* left_unary = left_->AsUnaryOperation();
335 UnaryOperation* right_unary = right_->AsUnaryOperation();
336 Literal* left_literal = left_->AsLiteral();
337 Literal* right_literal = right_->AsLiteral();
338 357
339 // Check for the pattern: typeof <expression> == <string literal>. 358 static bool IsVoidOfLiteral(Expression* expr) {
340 if (left_unary != NULL && left_unary->op() == Token::TYPEOF && 359 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
341 right_literal != NULL && right_literal->handle()->IsString()) { 360 return maybe_unary != NULL &&
342 *expr = left_unary->expression(); 361 maybe_unary->op() == Token::VOID &&
343 *check = Handle<String>::cast(right_literal->handle()); 362 maybe_unary->expression()->AsLiteral() != NULL;
363 }
364
365
366 // Check for the pattern: void <literal> equals <expression>
367 static bool MatchLiteralCompareUndefined(Expression* left,
368 Token::Value op,
369 Expression* right,
370 Expression** expr) {
371 if (IsVoidOfLiteral(left) && Token::IsEqualityOp(op)) {
372 *expr = right;
344 return true; 373 return true;
345 } 374 }
346
347 // Check for the pattern: <string literal> == typeof <expression>.
348 if (right_unary != NULL && right_unary->op() == Token::TYPEOF &&
349 left_literal != NULL && left_literal->handle()->IsString()) {
350 *expr = right_unary->expression();
351 *check = Handle<String>::cast(left_literal->handle());
352 return true;
353 }
354
355 return false; 375 return false;
356 } 376 }
357 377
358 378
359 bool CompareOperation::IsLiteralCompareUndefined(Expression** expr) { 379 bool CompareOperation::IsLiteralCompareUndefined(Expression** expr) {
360 if (op_ != Token::EQ_STRICT) return false; 380 return MatchLiteralCompareUndefined(left_, op_, right_, expr) ||
381 MatchLiteralCompareUndefined(right_, op_, left_, expr);
382 }
361 383
362 UnaryOperation* left_unary = left_->AsUnaryOperation();
363 UnaryOperation* right_unary = right_->AsUnaryOperation();
364 384
365 // Check for the pattern: <expression> === void <literal>. 385 // Check for the pattern: null equals <expression>
366 if (right_unary != NULL && right_unary->op() == Token::VOID && 386 static bool MatchLiteralCompareNull(Expression* left,
367 right_unary->expression()->AsLiteral() != NULL) { 387 Token::Value op,
368 *expr = left_; 388 Expression* right,
389 Expression** expr) {
390 if (left->IsNullLiteral() && Token::IsEqualityOp(op)) {
391 *expr = right;
369 return true; 392 return true;
370 } 393 }
371
372 // Check for the pattern: void <literal> === <expression>.
373 if (left_unary != NULL && left_unary->op() == Token::VOID &&
374 left_unary->expression()->AsLiteral() != NULL) {
375 *expr = right_;
376 return true;
377 }
378
379 return false; 394 return false;
380 } 395 }
381 396
382 397
383 bool CompareOperation::IsLiteralCompareNull(Expression** expr) { 398 bool CompareOperation::IsLiteralCompareNull(Expression** expr) {
384 if (op_ != Token::EQ && op_ != Token::EQ_STRICT) return false; 399 return MatchLiteralCompareNull(left_, op_, right_, expr) ||
385 400 MatchLiteralCompareNull(right_, op_, left_, expr);
386 // Check for the pattern: <expression> equals null.
387 Literal* right_literal = right_->AsLiteral();
388 if (right_literal != NULL && right_literal->handle()->IsNull()) {
389 *expr = left_;
390 return true;
391 }
392
393 // Check for the pattern: null equals <expression>.
394 Literal* left_literal = left_->AsLiteral();
395 if (left_literal != NULL && left_literal->handle()->IsNull()) {
396 *expr = right_;
397 return true;
398 }
399
400 return false;
401 } 401 }
402 402
403 403
404 // ---------------------------------------------------------------------------- 404 // ----------------------------------------------------------------------------
405 // Inlining support 405 // Inlining support
406 406
407 bool Declaration::IsInlineable() const { 407 bool Declaration::IsInlineable() const {
408 return proxy()->var()->IsStackAllocated() && fun() == NULL; 408 return proxy()->var()->IsStackAllocated() && fun() == NULL;
409 } 409 }
410 410
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 int pos) 1187 int pos)
1188 : label_(label), 1188 : label_(label),
1189 statements_(statements), 1189 statements_(statements),
1190 position_(pos), 1190 position_(pos),
1191 compare_type_(NONE), 1191 compare_type_(NONE),
1192 compare_id_(AstNode::GetNextId(isolate)), 1192 compare_id_(AstNode::GetNextId(isolate)),
1193 entry_id_(AstNode::GetNextId(isolate)) { 1193 entry_id_(AstNode::GetNextId(isolate)) {
1194 } 1194 }
1195 1195
1196 } } // namespace v8::internal 1196 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ast.h ('k') | src/full-codegen.h » ('j') | src/v8.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698